Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 43 | private TimerTask idleTask = new ProcessorTask(); |
| 44 | private TimerTask maxTask = new ProcessorTask(); |
| 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 | ecb63c5 | 2015-02-19 10:03:48 -0800 | [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); |
| 81 | schedule(1); |
| 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 | |
| 92 | // Schedules a new processor task given number of millis in the future. |
| 93 | private TimerTask schedule(int millis) { |
| 94 | TimerTask task = new ProcessorTask(); |
| 95 | timer.schedule(task, millis); |
| 96 | return task; |
| 97 | } |
| 98 | |
| 99 | // Cancels the specified task if it is active. |
| 100 | private TimerTask cancelIfActive(TimerTask task) { |
| 101 | if (task != null) { |
| 102 | task.cancel(); |
| 103 | } |
| 104 | return task; |
| 105 | } |
| 106 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 107 | // Task for triggering processing of accumulated items |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 108 | private class ProcessorTask extends TimerTask { |
| 109 | @Override |
| 110 | public void run() { |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 111 | idleTask = cancelIfActive(idleTask); |
| 112 | if (isReady()) { |
| 113 | try { |
| 114 | maxTask = cancelIfActive(maxTask); |
| 115 | processItems(finalizeCurrentBatch()); |
| 116 | } catch (Exception e) { |
| 117 | log.warn("Unable to process batch due to {}", e); |
| 118 | } |
| 119 | } else { |
| 120 | idleTask = schedule(maxIdleMillis); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 125 | // 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] | 126 | private synchronized List<T> finalizeCurrentBatch() { |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 127 | List<T> toBeProcessed = items; |
| 128 | items = Lists.newArrayList(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 129 | return toBeProcessed; |
| 130 | } |
| 131 | |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 132 | @Override |
| 133 | public boolean isReady() { |
| 134 | return true; |
| 135 | } |
| 136 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 137 | /** |
| 138 | * Returns the backing timer. |
| 139 | * |
| 140 | * @return backing timer |
| 141 | */ |
| 142 | public Timer timer() { |
| 143 | return timer; |
| 144 | } |
| 145 | |
| 146 | /** |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 147 | * Returns the maximum number of items allowed to accumulate before |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 148 | * processing is triggered. |
| 149 | * |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 150 | * @return max number of items |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 151 | */ |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 152 | public int maxItems() { |
| 153 | return maxItems; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns the maximum number of millis allowed to expire since the first |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 158 | * item before processing is triggered. |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 159 | * |
| 160 | * @return max number of millis a batch is allowed to last |
| 161 | */ |
| 162 | public int maxBatchMillis() { |
| 163 | return maxBatchMillis; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the maximum number of millis allowed to expire since the last |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 168 | * item arrival before processing is triggered. |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 169 | * |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 170 | * @return max number of millis since the last item |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 171 | */ |
| 172 | public int maxIdleMillis() { |
| 173 | return maxIdleMillis; |
| 174 | } |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 175 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 176 | } |