Class LockFreeSlidingTimeWindowMetrics

java.lang.Object
io.github.resilience4j.core.metrics.LockFreeSlidingTimeWindowMetrics
All Implemented Interfaces:
Metrics

public class LockFreeSlidingTimeWindowMetrics extends Object implements Metrics
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.

  • Constructor Details

    • LockFreeSlidingTimeWindowMetrics

      public LockFreeSlidingTimeWindowMetrics(int windowSize, Clock clock)
    • LockFreeSlidingTimeWindowMetrics

      public LockFreeSlidingTimeWindowMetrics(int windowSize)
  • Method Details

    • record

      public Snapshot record(long duration, TimeUnit durationUnit, Metrics.Outcome outcome)
      Description copied from interface: Metrics
      Records a call.
      Specified by:
      record in interface Metrics
      Parameters:
      duration - the duration of the call
      durationUnit - the time unit of the duration
      outcome - the outcome of the call
    • getSnapshot

      public Snapshot getSnapshot()
      Description copied from interface: Metrics
      Returns a snapshot.
      Specified by:
      getSnapshot in interface Metrics
      Returns:
      a snapshot