Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -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 com.google.common.collect.Lists; |
| 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | import java.util.List; |
| 23 | import java.util.Timer; |
| 24 | import java.util.TimerTask; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 30 | * Base implementation of an item accumulator. It allows triggering based on |
| 31 | * item inter-arrival time threshold, maximum batch life threshold and maximum |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 32 | * batch size. |
| 33 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 34 | public abstract class AbstractAccumulator<T> implements Accumulator<T> { |
| 35 | |
| 36 | private Logger log = LoggerFactory.getLogger(AbstractAccumulator.class); |
| 37 | |
| 38 | private final Timer timer; |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 39 | private final int maxItems; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 40 | private final int maxBatchMillis; |
| 41 | private final int maxIdleMillis; |
| 42 | |
Ray Milkey | 71081a6 | 2015-08-17 17:44:01 -0700 | [diff] [blame] | 43 | private volatile TimerTask idleTask = new ProcessorTask(); |
| 44 | private volatile TimerTask maxTask = new ProcessorTask(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 45 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 46 | private List<T> items = Lists.newArrayList(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 49 | * Creates an item accumulator capable of triggering on the specified |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 50 | * thresholds. |
| 51 | * |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 52 | * @param timer timer to use for scheduling check-points |
| 53 | * @param maxItems maximum number of items to accumulate before |
| 54 | * processing is triggered |
| 55 | * @param maxBatchMillis maximum number of millis allowed since the first |
| 56 | * item before processing is triggered |
| 57 | * @param maxIdleMillis maximum number millis between items before |
| 58 | * processing is triggered |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 59 | */ |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 60 | protected AbstractAccumulator(Timer timer, int maxItems, |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 61 | int maxBatchMillis, int maxIdleMillis) { |
| 62 | this.timer = checkNotNull(timer, "Timer cannot be null"); |
| 63 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 64 | checkArgument(maxItems > 1, "Maximum number of items must be > 1"); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 65 | checkArgument(maxBatchMillis > 0, "Maximum millis must be positive"); |
| 66 | checkArgument(maxIdleMillis > 0, "Maximum idle millis must be positive"); |
| 67 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 68 | this.maxItems = maxItems; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 69 | this.maxBatchMillis = maxBatchMillis; |
| 70 | this.maxIdleMillis = maxIdleMillis; |
| 71 | } |
| 72 | |
| 73 | @Override |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 74 | public synchronized void add(T item) { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 75 | idleTask = cancelIfActive(idleTask); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 76 | items.add(checkNotNull(item, "Item cannot be null")); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 77 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 78 | // Did we hit the max item threshold? |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 79 | if (items.size() >= maxItems) { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 80 | maxTask = cancelIfActive(maxTask); |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 81 | scheduleNow(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 82 | } else { |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 83 | // Otherwise, schedule idle task and if this is a first item |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 84 | // also schedule the max batch age task. |
| 85 | idleTask = schedule(maxIdleMillis); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 86 | if (items.size() == 1) { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 87 | maxTask = schedule(maxBatchMillis); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Finalizes the current batch, if ready, and schedules a new processor |
| 94 | * in the immediate future. |
| 95 | */ |
| 96 | private void scheduleNow() { |
| 97 | if (isReady()) { |
| 98 | TimerTask task = new ProcessorTask(finalizeCurrentBatch()); |
| 99 | timer.schedule(task, 1); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Schedules a new processor task given number of millis in the future. |
| 105 | * Batch finalization is deferred to time of execution. |
| 106 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 107 | private TimerTask schedule(int millis) { |
| 108 | TimerTask task = new ProcessorTask(); |
| 109 | timer.schedule(task, millis); |
| 110 | return task; |
| 111 | } |
| 112 | |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Cancels the specified task if it is active. |
| 115 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 116 | private TimerTask cancelIfActive(TimerTask task) { |
| 117 | if (task != null) { |
| 118 | task.cancel(); |
| 119 | } |
| 120 | return task; |
| 121 | } |
| 122 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 123 | // Task for triggering processing of accumulated items |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 124 | private class ProcessorTask extends TimerTask { |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 125 | |
| 126 | private final List<T> items; |
| 127 | |
| 128 | // Creates a new processor task with deferred batch finalization. |
| 129 | ProcessorTask() { |
| 130 | this.items = null; |
| 131 | } |
| 132 | |
| 133 | // Creates a new processor task with pre-emptive batch finalization. |
| 134 | ProcessorTask(List<T> items) { |
| 135 | this.items = items; |
| 136 | } |
| 137 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 138 | @Override |
| 139 | public void run() { |
Ray Milkey | 71081a6 | 2015-08-17 17:44:01 -0700 | [diff] [blame] | 140 | synchronized (AbstractAccumulator.this) { |
| 141 | idleTask = cancelIfActive(idleTask); |
| 142 | } |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 143 | if (isReady()) { |
| 144 | try { |
Ray Milkey | 71081a6 | 2015-08-17 17:44:01 -0700 | [diff] [blame] | 145 | synchronized (AbstractAccumulator.this) { |
| 146 | maxTask = cancelIfActive(maxTask); |
| 147 | } |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 148 | List<T> batch = items != null ? items : finalizeCurrentBatch(); |
| 149 | if (!batch.isEmpty()) { |
| 150 | processItems(batch); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 151 | } |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 152 | } catch (Exception e) { |
Thomas Vachuska | 91caf1a | 2015-08-26 10:14:29 -0700 | [diff] [blame] | 153 | log.warn("Unable to process batch due to", e); |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 154 | } |
| 155 | } else { |
Ray Milkey | 71081a6 | 2015-08-17 17:44:01 -0700 | [diff] [blame] | 156 | synchronized (AbstractAccumulator.this) { |
| 157 | idleTask = schedule(maxIdleMillis); |
| 158 | } |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 163 | // Demotes and returns the current batch of items and promotes a new one. |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 164 | private synchronized List<T> finalizeCurrentBatch() { |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 165 | List<T> toBeProcessed = items; |
| 166 | items = Lists.newArrayList(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 167 | return toBeProcessed; |
| 168 | } |
| 169 | |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 170 | @Override |
| 171 | public boolean isReady() { |
| 172 | return true; |
| 173 | } |
| 174 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 175 | /** |
| 176 | * Returns the backing timer. |
| 177 | * |
| 178 | * @return backing timer |
| 179 | */ |
| 180 | public Timer timer() { |
| 181 | return timer; |
| 182 | } |
| 183 | |
| 184 | /** |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 185 | * Returns the maximum number of items allowed to accumulate before |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 186 | * processing is triggered. |
| 187 | * |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 188 | * @return max number of items |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 189 | */ |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 190 | public int maxItems() { |
| 191 | return maxItems; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns the maximum number of millis allowed to expire since the first |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 196 | * item before processing is triggered. |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 197 | * |
| 198 | * @return max number of millis a batch is allowed to last |
| 199 | */ |
| 200 | public int maxBatchMillis() { |
| 201 | return maxBatchMillis; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns the maximum number of millis allowed to expire since the last |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 206 | * item arrival before processing is triggered. |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 207 | * |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 208 | * @return max number of millis since the last item |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 209 | */ |
| 210 | public int maxIdleMillis() { |
| 211 | return maxIdleMillis; |
| 212 | } |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 213 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 214 | } |