Metrics implementation is backed by a sliding time window that aggregates only the
calls made in the last N seconds.
The sliding time window is implemented with a circular array of N partial aggregations
(buckets). If the time window size is 10 seconds, the circular array has always 10 partial
aggregations (buckets). Every bucket aggregates the outcome of all calls which happen in a
certain epoch second. (Partial aggregation) The head bucket of the circular array stores the call
outcomes of the current epoch second. The other partial aggregations store the call outcomes of
the previous N-1 epoch seconds.
The sliding window does not store call outcomes (tuples) individually, but incrementally updates partial aggregations (bucket) and a total aggregation. The total aggregation is updated incrementally when a new call outcome is recorded. When the oldest bucket is evicted, the partial total aggregation of that bucket is subtracted from the total aggregation. (Subtract-on-Evict)
The time to retrieve a Snapshot is constant 0(1), since the Snapshot is pre-aggregated and is
independent of the time window size. The space requirement (memory consumption) of this
implementation should be nearly constant O(n), since the call outcomes (tuples) are not stored
individually. Only N partial aggregations and 1 total aggregation are created.
| Modifier and Type | Field and Description |
|---|---|
| private final Clock | |
| pack-priv int | |
| private final ReentrantLock | |
| pack-priv final PartialAggregation[] | |
| private final int | |
| private final TotalAggregation |
| Access | Constructor and Description |
|---|---|
| public | SlidingTimeWindowMetrics(int
the window time size in seconds timeWindowSizeInSeconds, Clock the clock)Clock to useCreates a new |
| Modifier and Type | Method and Description |
|---|---|
| private PartialAggregation | Returns: the head partial aggregation of the circular arrayReturns the head partial aggregation of the circular array. |
| public Snapshot | getSnapshot()
Implements io. Returns a snapshot. |
| pack-priv void | |
| private PartialAggregation | moveWindowToCurrentEpochSecond(PartialAggregation
the latest partial aggregation of the circular array latestPartialAggregation)Moves the end of the time window to the current epoch second. |
| public Snapshot | record(long
the duration of the call duration, TimeUnit the time unit of the duration durationUnit, Metrics.the outcome of the call outcomeImplements io. Records a call. |
| clock | back to summary |
|---|---|
| private final Clock clock | |
| headIndex | back to summary |
|---|---|
| pack-priv int headIndex | |
| lock | back to summary |
|---|---|
| private final ReentrantLock lock | |
| partialAggregations | back to summary |
|---|---|
| pack-priv final PartialAggregation[] partialAggregations | |
| timeWindowSizeInSeconds | back to summary |
|---|---|
| private final int timeWindowSizeInSeconds | |
| totalAggregation | back to summary |
|---|---|
| private final TotalAggregation totalAggregation | |
| SlidingTimeWindowMetrics | back to summary |
|---|---|
| public SlidingTimeWindowMetrics(int timeWindowSizeInSeconds, Clock clock) Creates a new | |
| getLatestPartialAggregation | back to summary |
|---|---|
| private PartialAggregation getLatestPartialAggregation() Returns the head partial aggregation of the circular array.
| |
| getSnapshot | back to summary |
|---|---|
| public Snapshot getSnapshot() Implements io. Doc from io. Returns a snapshot.
| |
| moveHeadIndexByOne | back to summary |
|---|---|
| pack-priv void moveHeadIndexByOne() Moves the headIndex to the next bucket. | |
| moveWindowToCurrentEpochSecond | back to summary |
|---|---|
| private PartialAggregation moveWindowToCurrentEpochSecond(PartialAggregation latestPartialAggregation) Moves the end of the time window to the current epoch second. The latest bucket of the circular array is used to calculate how many seconds the window must be moved. The difference is calculated by subtracting the epoch second from the latest bucket from the current epoch second. If the difference is greater than the time window size, the time window size is used.
| |
| record | back to summary |
|---|---|
| public Snapshot record(long duration, TimeUnit durationUnit, Metrics. Implements io. Doc from io. Records a call. | |