Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | package org.onlab.util; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Collections; |
| 20 | import java.util.List; |
| 21 | import java.util.concurrent.Executors; |
| 22 | import java.util.concurrent.ScheduledExecutorService; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | import java.util.concurrent.atomic.AtomicLong; |
| 25 | import java.util.stream.Collectors; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | |
| 29 | /** |
| 30 | * Maintains a sliding window of value counts. The sliding window counter is |
| 31 | * initialized with a number of window slots. Calls to #incrementCount() will |
| 32 | * increment the value in the current window slot. Periodically the window |
| 33 | * slides and the oldest value count is dropped. Calls to #get() will get the |
| 34 | * total count for the last N window slots. |
| 35 | */ |
| 36 | public final class SlidingWindowCounter { |
| 37 | |
| 38 | private volatile int headSlot; |
| 39 | private final int windowSlots; |
| 40 | |
| 41 | private final List<AtomicLong> counters; |
| 42 | |
| 43 | private final ScheduledExecutorService background; |
| 44 | |
| 45 | private static final int SLIDE_WINDOW_PERIOD_SECONDS = 1; |
| 46 | |
| 47 | /** |
| 48 | * Creates a new sliding window counter with the given total number of |
| 49 | * window slots. |
| 50 | * |
| 51 | * @param windowSlots total number of window slots |
| 52 | */ |
| 53 | public SlidingWindowCounter(int windowSlots) { |
| 54 | checkArgument(windowSlots > 0, "Window size must be a positive integer"); |
| 55 | |
| 56 | this.windowSlots = windowSlots; |
| 57 | this.headSlot = 0; |
| 58 | |
| 59 | // Initialize each item in the list to an AtomicLong of 0 |
| 60 | this.counters = Collections.nCopies(windowSlots, 0) |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 61 | .stream() |
| 62 | .map(AtomicLong::new) |
| 63 | .collect(Collectors.toCollection(ArrayList::new)); |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 64 | |
| 65 | background = Executors.newSingleThreadScheduledExecutor(); |
| 66 | background.scheduleWithFixedDelay(this::advanceHead, 0, |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 67 | SLIDE_WINDOW_PERIOD_SECONDS, TimeUnit.SECONDS); |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Releases resources used by the SlidingWindowCounter. |
| 72 | */ |
| 73 | public void destroy() { |
| 74 | background.shutdownNow(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Increments the count of the current window slot by 1. |
| 79 | */ |
| 80 | public void incrementCount() { |
| 81 | incrementCount(headSlot, 1); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Increments the count of the current window slot by the given value. |
| 86 | * |
| 87 | * @param value value to increment by |
| 88 | */ |
| 89 | public void incrementCount(long value) { |
| 90 | incrementCount(headSlot, value); |
| 91 | } |
| 92 | |
| 93 | private void incrementCount(int slot, long value) { |
| 94 | counters.get(slot).addAndGet(value); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Gets the total count for the last N window slots. |
| 99 | * |
| 100 | * @param slots number of slots to include in the count |
| 101 | * @return total count for last N slots |
| 102 | */ |
| 103 | public long get(int slots) { |
| 104 | checkArgument(slots <= windowSlots, |
| 105 | "Requested window must be less than the total window slots"); |
| 106 | |
| 107 | long sum = 0; |
| 108 | |
| 109 | for (int i = 0; i < slots; i++) { |
| 110 | int currentIndex = headSlot - i; |
| 111 | if (currentIndex < 0) { |
| 112 | currentIndex = counters.size() + currentIndex; |
| 113 | } |
| 114 | sum += counters.get(currentIndex).get(); |
| 115 | } |
| 116 | |
| 117 | return sum; |
| 118 | } |
| 119 | |
| 120 | void advanceHead() { |
| 121 | counters.get(slotAfter(headSlot)).set(0); |
| 122 | headSlot = slotAfter(headSlot); |
| 123 | } |
| 124 | |
| 125 | private int slotAfter(int slot) { |
| 126 | return (slot + 1) % windowSlots; |
| 127 | } |
| 128 | |
| 129 | } |