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

public Class LockFreeSlidingTimeWindowMetrics

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

Implements a lock-free time sliding window using a single linked list, represented by a head and a tail reference. The algorithm of the sliding window is very similar as for LockFreeFixedSizeSlidingWindowMetrics.

Each node of the sliding window represents the aggregated stats across 1 second (time slice). During a time slice, the algorithm is very simple consisting of a classical CAS-loop, in which the stats are copied, incremented, and a swap is attempted.

The complexity of the algorithm comes when the time slice is advanced. The algorithm needs to ensure that only one thread succeeds in advancing the time slice, i.e. a single time slice is advanced not multiple. This is achieved via an extra check when adding a new entry to the window, which guarantees that it is added only if it respects the time order, i.e. ith time slice is added only if it is preceded by the (i-1)th time slice.

It also needs to ensure that no stats updates are lost by updating a time slice in the past. This is accounted via a processed flag in the time slice, which marks that no further increments should happen for it.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static class

Field Summary

Modifier and TypeField and Description
private final Clock
private static final VarHandle
private volatile LockFreeSlidingTimeWindowMetrics.Node
private static final VarHandle
private static final VarHandle
private volatile LockFreeSlidingTimeWindowMetrics.Node
private static final VarHandle
private static final long
private final int

Constructor Summary

AccessConstructor and Description
public
LockFreeSlidingTimeWindowMetrics(int windowSize, Clock clock)

public

Method Summary

Modifier and TypeMethod and Description
private void
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.

private void
updateWindow(int second, long time)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

clockback to summary
private final Clock clock
headRefback to summary
private volatile LockFreeSlidingTimeWindowMetrics.Node headRef
NEXTback to summary
private static final VarHandle NEXT
TAILback to summary
private static final VarHandle TAIL
tailRefback to summary
private volatile LockFreeSlidingTimeWindowMetrics.Node tailRef
TIME_SLICEback to summary
private static final VarHandle TIME_SLICE
TIME_SLICE_DURATION_IN_NANOSback to summary
private static final long TIME_SLICE_DURATION_IN_NANOS
windowSizeback to summary
private final int windowSize

Constructor Detail

LockFreeSlidingTimeWindowMetricsback to summary
public LockFreeSlidingTimeWindowMetrics(int windowSize, Clock clock)
LockFreeSlidingTimeWindowMetricsback to summary
public LockFreeSlidingTimeWindowMetrics(int windowSize)

Method Detail

advanceTimeSliceback to summary
private void advanceTimeSlice()
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
updateWindowback to summary
private void updateWindow(int second, long time)
io.github.resilience4j.core.metrics back to summary

private Class LockFreeSlidingTimeWindowMetrics.Node

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

Field Summary

Modifier and TypeField and Description
pack-priv volatile LockFreeSlidingTimeWindowMetrics.Node
pack-priv volatile LockFreeSlidingTimeWindowMetrics.TimeSlice

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

nextback to summary
pack-priv volatile LockFreeSlidingTimeWindowMetrics.Node next
timeSliceback to summary
pack-priv volatile LockFreeSlidingTimeWindowMetrics.TimeSlice timeSlice

Constructor Detail

Nodeback to summary
pack-priv Node(LockFreeSlidingTimeWindowMetrics.TimeSlice timeSlice, LockFreeSlidingTimeWindowMetrics.Node next)
io.github.resilience4j.core.metrics back to summary

public Class LockFreeSlidingTimeWindowMetrics.TimeSlice

extends Object
Class Inheritance
  • java.lang.Object
  • io.github.resilience4j.core.metrics.LockFreeSlidingTimeWindowMetrics.TimeSlice

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
pack-priv final int
pack-priv final PackedAggregation
pack-priv final long

Constructor Summary

AccessConstructor and Description
public
TimeSlice(int second, long time, PackedAggregation stats, boolean processed)

Method Summary

Modifier and TypeMethod and Description
public LockFreeSlidingTimeWindowMetrics.TimeSlice
copy()

public void
record(Long duration, TimeUnit durationUnit, Metrics.Outcome outcome)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

processedback to summary
pack-priv final boolean processed
secondback to summary
pack-priv final int second
statsback to summary
pack-priv final PackedAggregation stats
timeback to summary
pack-priv final long time

Constructor Detail

TimeSliceback to summary
public TimeSlice(int second, long time, PackedAggregation stats, boolean processed)

Method Detail

copyback to summary
public LockFreeSlidingTimeWindowMetrics.TimeSlice copy()
recordback to summary
public void record(Long duration, TimeUnit durationUnit, Metrics.Outcome outcome)