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

public Class LockFreeFixedSizeSlidingWindowMetrics

extends Object
implements Metrics
Class Inheritance
All Implemented Interfaces
io.github.resilience4j.core.metrics.Metrics
Imports
java.lang.invoke.MethodHandles, .VarHandle, java.util.Objects, java.util.concurrent.TimeUnit

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:

  1. A new node is added at the tail of the list.
  2. The head of the list is moved to the next node.
  3. 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:

All these operations are done via compare-and-swap operations.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final VarHandle
private volatile LockFreeFixedSizeSlidingWindowMetrics.Node
private static final VarHandle
private static final VarHandle
private volatile LockFreeFixedSizeSlidingWindowMetrics.Node
private final int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Snapshot
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

headRefback to summary
private volatile LockFreeFixedSizeSlidingWindowMetrics.Node headRef
NEXTback to summary
private static final VarHandle NEXT
TAILback to summary
private static final VarHandle TAIL
tailRefback to summary
private volatile LockFreeFixedSizeSlidingWindowMetrics.Node tailRef
windowSizeback to summary
private final int windowSize

Constructor Detail

LockFreeFixedSizeSlidingWindowMetricsback to summary
public LockFreeFixedSizeSlidingWindowMetrics(int windowSize)

Method Detail

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

Annotations
@Override
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
io.github.resilience4j.core.metrics back to summary

private Class LockFreeFixedSizeSlidingWindowMetrics.Node

extends Object
Class Inheritance
  • java.lang.Object
  • io.github.resilience4j.core.metrics.LockFreeFixedSizeSlidingWindowMetrics.Node

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv volatile LockFreeFixedSizeSlidingWindowMetrics.Node
pack-priv PackedAggregation

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

idback to summary
pack-priv int id
nextback to summary
pack-priv volatile LockFreeFixedSizeSlidingWindowMetrics.Node next
statsback to summary
pack-priv PackedAggregation stats

Constructor Detail

Nodeback to summary
pack-priv Node(int id, PackedAggregation stats, LockFreeFixedSizeSlidingWindowMetrics.Node next)