Class LockFreeFixedSizeSlidingWindowMetrics
java.lang.Object
io.github.resilience4j.core.metrics.LockFreeFixedSizeSlidingWindowMetrics
- All Implemented Interfaces:
Metrics
Implements a lock-free sliding window using a single linked list, represented by a head and a tail reference.
The idea is to initialize the list to the window size, where node N represents the stats for the N-th entry. Besides the stats for the N-th entry, it also contains the stats for the whole window, which are carried over on each new added entry.
When a new record is added, the size of the window is maintained:
- A new node is added at the tail of the list.
- The head of the list is moved to the next node.
- The tail of the list is moved to the next node.
As these operations need to happen atomically, we use the VarHandle API to perform CAS operations on the head, tail and next references. However, these updates are not atomically happening across these references, so we can end up that each reference is updated by a different thread.
The algorithm is based on the following conditions:
- if there is no next node -> try to add one
- else, if the head and tail represent the same N-th stats -> advance the head
- else -> advance the tail
All these operations are done via compare-and-swap operations.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.github.resilience4j.core.metrics.Metrics
Metrics.Outcome -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns a snapshot.record(long duration, TimeUnit durationUnit, Metrics.Outcome outcome) Records a call.
-
Constructor Details
-
LockFreeFixedSizeSlidingWindowMetrics
public LockFreeFixedSizeSlidingWindowMetrics(int windowSize)
-
-
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
-