Package alluxio.time
Class ExponentialTimer
- java.lang.Object
-
- alluxio.time.ExponentialTimer
-
public class ExponentialTimer extends java.lang.Object
TheExponentialTimer
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, theExponentialBackoffRetry
policy cannot be used because theRetryPolicy#attemptRetry()
is blocking.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ExponentialTimer.Result
Represents the result oftick()
.
-
Constructor Summary
Constructors Constructor Description ExponentialTimer(long initialIntervalMs, long maxIntervalMs, long initialWaitTimeMs, long maxTotalWaitTimeMs)
Creates a new instance ofExponentialTimer
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
getNumEvents()
ExponentialTimer.Result
tick()
Attempts to perform a timer tick.
-
-
-
Constructor Detail
-
ExponentialTimer
public ExponentialTimer(long initialIntervalMs, long maxIntervalMs, long initialWaitTimeMs, long maxTotalWaitTimeMs)
Creates a new instance ofExponentialTimer
.- 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
-
-