Package alluxio.time

Class ExponentialTimer


  • public class ExponentialTimer
    extends java.lang.Object
    The ExponentialTimer can be used for generating a sequence of events that are exponentially distributed in time. The intended usage is to associate the timer with an operation that should to be re-attempted if it fails using exponential back-off. For instance, an event loop iterates over scheduled operations each of which is associated with a timer and the event loop can use the timer to check if the operation is ready to be attempted and whether it should be re-attempted again in the future in case it fails.
     while (true) {
       Operation op = operations.pop();
       switch (op.getTimer().tick()) {
       case EXPIRED:
         // operation will not be re-attempted
         break;
       case NOT_READY:
         // operation is not ready to be re-attempted
         operations.push(op);
         break;
       case READY:
         boolean success = op.run();
         if (!success) {
           operations.push(op);
         }
       }
       Thread.sleep(10);
     }
     
    Note that in the above scenario, the ExponentialBackoffRetry policy cannot be used because the RetryPolicy#attemptRetry() is blocking.
    • Constructor Summary

      Constructors 
      Constructor Description
      ExponentialTimer​(long initialIntervalMs, long maxIntervalMs, long initialWaitTimeMs, long maxTotalWaitTimeMs)
      Creates a new instance of ExponentialTimer.
    • Constructor Detail

      • ExponentialTimer

        public ExponentialTimer​(long initialIntervalMs,
                                long maxIntervalMs,
                                long initialWaitTimeMs,
                                long maxTotalWaitTimeMs)
        Creates a new instance of ExponentialTimer.
        Parameters:
        initialIntervalMs - the initial interval between events (in milliseconds)
        maxIntervalMs - the maximum interval between events (in milliseconds)
        initialWaitTimeMs - the initial wait time before first event (in milliseconds)
        maxTotalWaitTimeMs - the maximum total wait time (in milliseconds)
    • Method Detail

      • getNumEvents

        public long getNumEvents()
        Returns:
        the number of generated events so far
      • tick

        public ExponentialTimer.Result tick()
        Attempts to perform a timer tick. One of three scenarios can be encountered: 1. the timer has expired 2. the next event is not ready 3. the next event is generated The last option has the side-effect of updating the internal state of the timer to reflect the generation of the event.
        Returns:
        the ExponentialTimer.Result that indicates which scenario was encountered