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