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