Top Description Fields Constructors Methods
io.github.resilience4j.core.metrics

public Class SlidingTimeWindowMetrics

extends Object
implements Metrics
Class Inheritance
All Implemented Interfaces
io.github.resilience4j.core.metrics.Metrics
Imports
java.time.Clock, java.util.concurrent.TimeUnit, java.util.concurrent.locks.ReentrantLock

A 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.

Field Summary

Modifier and TypeField and Description
private final Clock
pack-priv int
private final ReentrantLock
pack-priv final PartialAggregation[]
private final int
private final TotalAggregation

Constructor Summary

AccessConstructor and Description
public
SlidingTimeWindowMetrics(int
the window time size in seconds
timeWindowSizeInSeconds
,
Clock
the Clock to use
clock
)

Creates a new SlidingTimeWindowMetrics with the given clock and window of time.

Method Summary

Modifier and TypeMethod and Description
private PartialAggregation

Returns:

the head partial aggregation of the circular array
getLatestPartialAggregation
()

Returns the head partial aggregation of the circular array.

public Snapshot
pack-priv void
moveHeadIndexByOne()

Moves the headIndex to the next bucket.

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.Outcome
the outcome of the call
outcome
)

Implements io.github.resilience4j.core.metrics.Metrics.record.

Records a call.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

clockback to summary
private final Clock clock
headIndexback to summary
pack-priv int headIndex
lockback to summary
private final ReentrantLock lock
partialAggregationsback to summary
pack-priv final PartialAggregation[] partialAggregations
timeWindowSizeInSecondsback to summary
private final int timeWindowSizeInSeconds
totalAggregationback to summary
private final TotalAggregation totalAggregation

Constructor Detail

SlidingTimeWindowMetricsback to summary
public SlidingTimeWindowMetrics(int timeWindowSizeInSeconds, Clock clock)

Creates a new SlidingTimeWindowMetrics with the given clock and window of time.

Parameters
timeWindowSizeInSeconds:int

the window time size in seconds

clock:Clock

the Clock to use

Method Detail

getLatestPartialAggregationback to summary
private PartialAggregation getLatestPartialAggregation()

Returns the head partial aggregation of the circular array.

Returns:PartialAggregation

the head partial aggregation of the circular array

getSnapshotback to summary
public Snapshot getSnapshot()

Implements io.github.resilience4j.core.metrics.Metrics.getSnapshot.

Doc from io.github.resilience4j.core.metrics.Metrics.getSnapshot.

Returns a snapshot.

Returns:Snapshot

a snapshot

moveHeadIndexByOneback to summary
pack-priv void moveHeadIndexByOne()

Moves the headIndex to the next bucket.

moveWindowToCurrentEpochSecondback 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.

Parameters
latestPartialAggregation:PartialAggregation

the latest partial aggregation of the circular array

recordback to summary
public Snapshot record(long duration, TimeUnit durationUnit, Metrics.Outcome outcome)

Implements io.github.resilience4j.core.metrics.Metrics.record.

Doc from io.github.resilience4j.core.metrics.Metrics.record.

Records a call.

Parameters
duration:long

the duration of the call

durationUnit:TimeUnit

the time unit of the duration

outcome:Metrics.Outcome

the outcome of the call

Annotations
@Override