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