2 * Copyright 2015-present Open Networking Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.atomix.utils.misc;
18 import io.atomix.utils.concurrent.Scheduled;
19 import io.atomix.utils.concurrent.SingleThreadContext;
20 import io.atomix.utils.concurrent.ThreadContext;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicLong;
29 import java.util.stream.Collectors;
31 import static com.google.common.base.Preconditions.checkArgument;
34 * Maintains a sliding window of value counts. The sliding window counter is
35 * initialized with a number of window slots. Calls to #incrementCount() will
36 * increment the value in the current window slot. Periodically the window
37 * slides and the oldest value count is dropped. Calls to #get() will get the
38 * total count for the last N window slots.
40 public final class SlidingWindowCounter {
41 private final Logger log = LoggerFactory.getLogger(getClass());
42 private volatile int headSlot;
43 private final int windowSlots;
45 private final List<AtomicLong> counters;
47 private final Scheduled schedule;
49 private static final int SLIDE_WINDOW_PERIOD_SECONDS = 1;
51 public SlidingWindowCounter(int windowSlots) {
52 this(windowSlots, new SingleThreadContext("sliding-window-counter-%d"));
56 * Creates a new sliding window counter with the given total number of
59 * @param windowSlots total number of window slots
61 public SlidingWindowCounter(int windowSlots, ThreadContext context) {
62 checkArgument(windowSlots > 0, "Window size must be a positive integer");
64 this.windowSlots = windowSlots;
67 // Initialize each item in the list to an AtomicLong of 0
68 this.counters = Collections.nCopies(windowSlots, 0)
71 .collect(Collectors.toCollection(ArrayList::new));
72 this.schedule = context.schedule(0, SLIDE_WINDOW_PERIOD_SECONDS, TimeUnit.SECONDS, this::advanceHead);
76 * Releases resources used by the SlidingWindowCounter.
78 public void destroy() {
83 * Increments the count of the current window slot by 1.
85 public void incrementCount() {
86 incrementCount(headSlot, 1);
90 * Increments the count of the current window slot by the given value.
92 * @param value value to increment by
94 public void incrementCount(long value) {
95 incrementCount(headSlot, value);
98 private void incrementCount(int slot, long value) {
99 counters.get(slot).addAndGet(value);
103 * Gets the total count for the last N window slots.
105 * @param slots number of slots to include in the count
106 * @return total count for last N slots
108 public long get(int slots) {
109 checkArgument(slots <= windowSlots,
110 "Requested window must be less than the total window slots");
114 for (int i = 0; i < slots; i++) {
115 int currentIndex = headSlot - i;
116 if (currentIndex < 0) {
117 currentIndex = counters.size() + currentIndex;
119 sum += counters.get(currentIndex).get();
126 counters.get(slotAfter(headSlot)).set(0);
127 headSlot = slotAfter(headSlot);
130 private int slotAfter(int slot) {
131 return (slot + 1) % windowSlots;