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