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