Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.event.impl; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 17 | |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 18 | import java.util.Map; |
| 19 | import java.util.Set; |
| 20 | import java.util.TimerTask; |
| 21 | import java.util.concurrent.BlockingQueue; |
| 22 | import java.util.concurrent.ExecutorService; |
| 23 | import java.util.concurrent.Future; |
| 24 | import java.util.concurrent.LinkedBlockingQueue; |
| 25 | import java.util.concurrent.TimeUnit; |
| 26 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Activate; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Component; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 31 | import org.onlab.util.SharedExecutors; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.event.AbstractEvent; |
| 33 | import org.onosproject.event.DefaultEventSinkRegistry; |
| 34 | import org.onosproject.event.Event; |
| 35 | import org.onosproject.event.EventDeliveryService; |
| 36 | import org.onosproject.event.EventSink; |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceEvent; |
| 38 | import org.onosproject.net.flow.FlowRuleEvent; |
| 39 | import org.onosproject.net.host.HostEvent; |
| 40 | import org.onosproject.net.intent.IntentEvent; |
| 41 | import org.onosproject.net.link.LinkEvent; |
| 42 | import org.onosproject.net.topology.TopologyEvent; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 44 | |
Madan Jampani | 6a29231 | 2016-06-24 09:19:59 -0700 | [diff] [blame] | 45 | import com.google.common.base.Stopwatch; |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 46 | import com.google.common.collect.ImmutableMap; |
| 47 | import com.google.common.collect.ImmutableSet; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 48 | |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 49 | import static com.google.common.base.Preconditions.checkArgument; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 50 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 51 | import static org.onlab.util.Tools.groupedThreads; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 52 | import static org.onosproject.security.AppGuard.checkPermission; |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 53 | import static org.onosproject.security.AppPermission.Type.EVENT_READ; |
| 54 | import static org.onosproject.security.AppPermission.Type.EVENT_WRITE; |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Simple implementation of an event dispatching service. |
| 58 | */ |
| 59 | @Component(immediate = true) |
| 60 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 61 | public class CoreEventDispatcher extends DefaultEventSinkRegistry |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 62 | implements EventDeliveryService { |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 63 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 64 | private final Logger log = getLogger(getClass()); |
| 65 | |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 66 | |
| 67 | private DispatchLoop topologyDispatcher = new DispatchLoop("topology"); |
| 68 | private DispatchLoop programmingDispatcher = new DispatchLoop("programming"); |
| 69 | private DispatchLoop defaultDispatcher = new DispatchLoop("default"); |
| 70 | |
| 71 | private Map<Class, DispatchLoop> dispatcherMap = |
| 72 | new ImmutableMap.Builder<Class, DispatchLoop>() |
| 73 | .put(TopologyEvent.class, topologyDispatcher) |
| 74 | .put(DeviceEvent.class, topologyDispatcher) |
| 75 | .put(LinkEvent.class, topologyDispatcher) |
| 76 | .put(HostEvent.class, topologyDispatcher) |
| 77 | .put(FlowRuleEvent.class, programmingDispatcher) |
| 78 | .put(IntentEvent.class, programmingDispatcher) |
| 79 | .build(); |
| 80 | |
| 81 | private Set<DispatchLoop> dispatchers = |
| 82 | new ImmutableSet.Builder<DispatchLoop>() |
| 83 | .addAll(dispatcherMap.values()) |
| 84 | .add(defaultDispatcher) |
| 85 | .build(); |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 86 | |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 87 | // Default number of millis a sink can take to process an event. |
Thomas Vachuska | 409b9cb | 2015-07-31 13:07:12 -0700 | [diff] [blame] | 88 | private static final long DEFAULT_EXECUTE_MS = 5_000; // ms |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 89 | private static final long WATCHDOG_MS = 250; // ms |
| 90 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 91 | @SuppressWarnings("unchecked") |
| 92 | private static final Event KILL_PILL = new AbstractEvent(null, 0) { |
| 93 | }; |
| 94 | |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 95 | private long maxProcessMillis = DEFAULT_EXECUTE_MS; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 96 | |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 97 | private DispatchLoop getDispatcher(Event event) { |
| 98 | DispatchLoop dispatcher = dispatcherMap.get(event.getClass()); |
| 99 | if (dispatcher == null) { |
| 100 | dispatcher = defaultDispatcher; |
| 101 | } |
| 102 | return dispatcher; |
| 103 | } |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 104 | |
| 105 | @Override |
| 106 | public void post(Event event) { |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 107 | |
| 108 | if (!getDispatcher(event).add(event)) { |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 109 | log.error("Unable to post event {}", event); |
| 110 | } |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 111 | } |
| 112 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 113 | @Activate |
| 114 | public void activate() { |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 115 | |
| 116 | if (maxProcessMillis != 0) { |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 117 | dispatchers.forEach(DispatchLoop::startWatchdog); |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 118 | } |
| 119 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 120 | log.info("Started"); |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 121 | } |
| 122 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 123 | @Deactivate |
| 124 | public void deactivate() { |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 125 | dispatchers.forEach(DispatchLoop::stop); |
| 126 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 127 | log.info("Stopped"); |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public void setDispatchTimeLimit(long millis) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 132 | checkPermission(EVENT_WRITE); |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 133 | checkArgument(millis == 0 || millis >= WATCHDOG_MS, |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 134 | "Time limit must be greater than %s", WATCHDOG_MS); |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 135 | long oldMillis = maxProcessMillis; |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 136 | maxProcessMillis = millis; |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 137 | |
| 138 | if (millis == 0 && oldMillis != 0) { |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 139 | dispatchers.forEach(DispatchLoop::stopWatchdog); |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 140 | } else if (millis != 0 && oldMillis == 0) { |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 141 | dispatchers.forEach(DispatchLoop::startWatchdog); |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 142 | } |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public long getDispatchTimeLimit() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 147 | checkPermission(EVENT_READ); |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 148 | return maxProcessMillis; |
| 149 | } |
| 150 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 151 | // Auxiliary event dispatching loop that feeds off the events queue. |
| 152 | private class DispatchLoop implements Runnable { |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 153 | private final String name; |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 154 | private volatile boolean stopped; |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 155 | private volatile EventSink lastSink; |
| 156 | // Means to detect long-running sinks |
| 157 | private final Stopwatch stopwatch = Stopwatch.createUnstarted(); |
| 158 | private TimerTask watchdog; |
| 159 | private volatile Future<?> dispatchFuture; |
| 160 | private final BlockingQueue<Event> eventsQueue; |
| 161 | private final ExecutorService executor; |
| 162 | |
| 163 | DispatchLoop(String name) { |
| 164 | this.name = name; |
| 165 | executor = newSingleThreadExecutor( |
| 166 | groupedThreads("onos/event", |
| 167 | "dispatch-" + name + "%d", log)); |
| 168 | eventsQueue = new LinkedBlockingQueue<>(); |
| 169 | dispatchFuture = executor.submit(this); |
| 170 | } |
| 171 | |
| 172 | public boolean add(Event event) { |
| 173 | return eventsQueue.add(event); |
| 174 | } |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 175 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 176 | @Override |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 177 | public void run() { |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 178 | stopped = false; |
jaegonkim | a9ff68b | 2017-08-20 09:08:22 +0900 | [diff] [blame] | 179 | log.info("Dispatch loop({}) initiated", name); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 180 | while (!stopped) { |
| 181 | try { |
| 182 | // Fetch the next event and if it is the kill-pill, bail |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 183 | Event event = eventsQueue.take(); |
jaegonkim | a9ff68b | 2017-08-20 09:08:22 +0900 | [diff] [blame] | 184 | if (event != KILL_PILL) { |
| 185 | process(event); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 186 | } |
Thomas Vachuska | 409b9cb | 2015-07-31 13:07:12 -0700 | [diff] [blame] | 187 | } catch (InterruptedException e) { |
| 188 | log.warn("Dispatch loop interrupted"); |
Jonathan Hart | b68919e | 2016-02-05 15:22:36 -0800 | [diff] [blame] | 189 | } catch (Exception | Error e) { |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 190 | log.warn("Error encountered while dispatching event:", e); |
| 191 | } |
| 192 | } |
jaegonkim | a9ff68b | 2017-08-20 09:08:22 +0900 | [diff] [blame] | 193 | log.info("Dispatch loop({}) terminated", name); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 194 | } |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 195 | |
| 196 | // Locate the sink for the event class and use it to process the event |
| 197 | @SuppressWarnings("unchecked") |
| 198 | private void process(Event event) { |
| 199 | EventSink sink = getSink(event.getClass()); |
| 200 | if (sink != null) { |
| 201 | lastSink = sink; |
Madan Jampani | 6a29231 | 2016-06-24 09:19:59 -0700 | [diff] [blame] | 202 | stopwatch.start(); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 203 | sink.process(event); |
Madan Jampani | 6a29231 | 2016-06-24 09:19:59 -0700 | [diff] [blame] | 204 | stopwatch.reset(); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 205 | } else { |
| 206 | log.warn("No sink registered for event class {}", |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 207 | event.getClass().getName()); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | void stop() { |
| 212 | stopped = true; |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 213 | add(KILL_PILL); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 214 | } |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 215 | |
Tian Jian | 3796476 | 2017-07-28 16:06:16 +0800 | [diff] [blame] | 216 | void restart() { |
| 217 | dispatchFuture.cancel(true); |
| 218 | dispatchFuture = executor.submit(this); |
| 219 | } |
| 220 | |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 221 | // Monitors event sinks to make sure none take too long to execute. |
| 222 | private class Watchdog extends TimerTask { |
| 223 | @Override |
| 224 | public void run() { |
| 225 | long elapsedTimeMillis = stopwatch.elapsed(TimeUnit.MILLISECONDS); |
| 226 | if (elapsedTimeMillis > maxProcessMillis) { |
| 227 | stopwatch.reset(); |
| 228 | log.warn("Event sink {} exceeded execution time limit: {} ms; " + |
| 229 | "spawning new dispatch loop", |
| 230 | lastSink.getClass().getName(), elapsedTimeMillis); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 231 | |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 232 | // Notify the sink that it has exceeded its time limit. |
| 233 | lastSink.onProcessLimit(); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 234 | |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 235 | // Cancel the old dispatch loop and submit a new one. |
| 236 | |
Tian Jian | 3796476 | 2017-07-28 16:06:16 +0800 | [diff] [blame] | 237 | stop(); |
| 238 | restart(); |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | private void startWatchdog() { |
| 244 | log.info("Starting watchdog task for dispatcher {}", name); |
| 245 | watchdog = new Watchdog(); |
| 246 | SharedExecutors.getTimer().schedule(watchdog, WATCHDOG_MS, WATCHDOG_MS); |
| 247 | } |
| 248 | |
| 249 | private void stopWatchdog() { |
| 250 | log.info("Stopping watchdog task for dispatcher {}", name); |
| 251 | if (watchdog != null) { |
| 252 | watchdog.cancel(); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | } |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 256 | |
| 257 | |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 258 | } |