Class AbstractMonotonicTimestampGenerator

  • All Implemented Interfaces:
    TimestampGenerator
    Direct Known Subclasses:
    LoggingMonotonicTimestampGenerator

    public abstract class AbstractMonotonicTimestampGenerator
    extends Object
    implements TimestampGenerator
    Base implementation for monotonic timestamp generators.

    The accuracy of the generated timestamps is largely dependent on the granularity of the underlying operating system's clock.

    Generally speaking, this granularity is millisecond, and the sub-millisecond part is simply a counter that gets incremented until the next clock tick, as provided by System.currentTimeMillis().

    On some systems, however, it is possible to have a better granularity by using a JNR call to gettimeofday. The driver will use this system call automatically whenever available, unless the system property com.datastax.driver.USE_NATIVE_CLOCK is explicitly set to false.

    Beware that to guarantee monotonicity, if more than one call to TimestampGenerator.next() is made within the same microsecond, or in the event of a system clock skew, this generator might return timestamps that drift out in the future. Whe this happens, onDrift(long, long) is invoked.

    • Constructor Detail

      • AbstractMonotonicTimestampGenerator

        public AbstractMonotonicTimestampGenerator()
    • Method Detail

      • computeNext

        protected long computeNext​(long last)
        Compute the next timestamp, given the last timestamp previously generated.

        To guarantee monotonicity, the next timestamp should be strictly greater than the last one. If the underlying clock fails to generate monotonically increasing timestamps, the generator will simply increment the previous timestamp, and onDrift(long, long) will be invoked.

        This implementation is inspired by org.apache.cassandra.service.ClientState#getTimestamp().

        Parameters:
        last - the last timestamp generated by this generator, in microseconds.
        Returns:
        the next timestamp to use, in microseconds.
      • onDrift

        protected abstract void onDrift​(long currentTick,
                                        long lastTimestamp)
        Called when generated timestamps drift into the future compared to the underlying clock (in other words, if lastTimestamp >= currentTick).

        This could happen if timestamps are requested faster than the clock granularity, or on a clock skew (for example because of a leap second).

        Parameters:
        currentTick - the current clock tick, in microseconds.
        lastTimestamp - the last timestamp that was generated, in microseconds.