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:
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.
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private static final VarHandle | |
| private volatile LockFreeFixedSizeSlidingWindowMetrics. | |
| private static final VarHandle | |
| private static final VarHandle | |
| private volatile LockFreeFixedSizeSlidingWindowMetrics. | |
| private final int |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Snapshot | getSnapshot()
Implements io. Returns a snapshot. |
| public Snapshot | record(long
the duration of the call duration, TimeUnit the time unit of the duration durationUnit, Metrics.the outcome of the call outcomeImplements io. Records a call. |
| HEAD | back to summary |
|---|---|
| private static final VarHandle HEAD | |
| headRef | back to summary |
|---|---|
| private volatile LockFreeFixedSizeSlidingWindowMetrics. | |
| NEXT | back to summary |
|---|---|
| private static final VarHandle NEXT | |
| TAIL | back to summary |
|---|---|
| private static final VarHandle TAIL | |
| tailRef | back to summary |
|---|---|
| private volatile LockFreeFixedSizeSlidingWindowMetrics. | |
| windowSize | back to summary |
|---|---|
| private final int windowSize | |
| LockFreeFixedSizeSlidingWindowMetrics | back to summary |
|---|---|
| public LockFreeFixedSizeSlidingWindowMetrics(int windowSize) | |
| getSnapshot | back to summary |
|---|---|
| public Snapshot getSnapshot() Implements io. Doc from io. Returns a snapshot. | |
| record | back to summary |
|---|---|
| public Snapshot record(long duration, TimeUnit durationUnit, Metrics. Implements io. Doc from io. Records a call. | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | |
| pack-priv volatile LockFreeFixedSizeSlidingWindowMetrics. | |
| pack-priv PackedAggregation |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| id | back to summary |
|---|---|
| pack-priv int id | |
| next | back to summary |
|---|---|
| pack-priv volatile LockFreeFixedSizeSlidingWindowMetrics. | |
| stats | back to summary |
|---|---|
| pack-priv PackedAggregation stats | |
| Node | back to summary |
|---|---|
| pack-priv Node(int id, PackedAggregation stats, LockFreeFixedSizeSlidingWindowMetrics. | |