Class SlidingTimeWindowMetrics
- All Implemented Interfaces:
Metrics
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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.github.resilience4j.core.metrics.Metrics
Metrics.Outcome -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Clock(package private) int(package private) final PartialAggregation[]private final intprivate final TotalAggregation -
Constructor Summary
ConstructorsConstructorDescriptionSlidingTimeWindowMetrics(int timeWindowSizeInSeconds, Clock clock) Creates a newSlidingTimeWindowMetricswith the given clock and window of time. -
Method Summary
Modifier and TypeMethodDescriptionprivate PartialAggregationReturns the head partial aggregation of the circular array.Returns a snapshot.(package private) voidMoves the headIndex to the next bucket.private PartialAggregationmoveWindowToCurrentEpochSecond(PartialAggregation latestPartialAggregation) Moves the end of the time window to the current epoch second.record(long duration, TimeUnit durationUnit, Metrics.Outcome outcome) Records a call.
-
Field Details
-
partialAggregations
-
timeWindowSizeInSeconds
private final int timeWindowSizeInSeconds -
totalAggregation
-
clock
-
headIndex
int headIndex
-
-
Constructor Details
-
SlidingTimeWindowMetrics
Creates a newSlidingTimeWindowMetricswith the given clock and window of time.- Parameters:
timeWindowSizeInSeconds- the window time size in secondsclock- theClockto use
-
-
Method Details
-
record
Description copied from interface:MetricsRecords a call. -
getSnapshot
Description copied from interface:MetricsReturns a snapshot.- Specified by:
getSnapshotin interfaceMetrics- Returns:
- a snapshot
-
moveWindowToCurrentEpochSecond
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.- Parameters:
latestPartialAggregation- the latest partial aggregation of the circular array
-
getLatestPartialAggregation
Returns the head partial aggregation of the circular array.- Returns:
- the head partial aggregation of the circular array
-
moveHeadIndexByOne
void moveHeadIndexByOne()Moves the headIndex to the next bucket.
-