Madan Jampani
Committed by Gerrit Code Review

Make CoreEventDispatcher's listener processing time tracking thread-safe

Change-Id: Ib9f109e41fd1b78ce9771a2bb54e8bf3dda38d6c
...@@ -27,11 +27,14 @@ import org.onosproject.event.EventDeliveryService; ...@@ -27,11 +27,14 @@ import org.onosproject.event.EventDeliveryService;
27 import org.onosproject.event.EventSink; 27 import org.onosproject.event.EventSink;
28 import org.slf4j.Logger; 28 import org.slf4j.Logger;
29 29
30 +import com.google.common.base.Stopwatch;
31 +
30 import java.util.TimerTask; 32 import java.util.TimerTask;
31 import java.util.concurrent.BlockingQueue; 33 import java.util.concurrent.BlockingQueue;
32 import java.util.concurrent.ExecutorService; 34 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Future; 35 import java.util.concurrent.Future;
34 import java.util.concurrent.LinkedBlockingQueue; 36 import java.util.concurrent.LinkedBlockingQueue;
37 +import java.util.concurrent.TimeUnit;
35 38
36 import static com.google.common.base.Preconditions.checkArgument; 39 import static com.google.common.base.Preconditions.checkArgument;
37 import static java.util.concurrent.Executors.newSingleThreadExecutor; 40 import static java.util.concurrent.Executors.newSingleThreadExecutor;
...@@ -71,7 +74,7 @@ public class CoreEventDispatcher extends DefaultEventSinkRegistry ...@@ -71,7 +74,7 @@ public class CoreEventDispatcher extends DefaultEventSinkRegistry
71 // Means to detect long-running sinks 74 // Means to detect long-running sinks
72 private TimerTask watchdog; 75 private TimerTask watchdog;
73 private EventSink lastSink; 76 private EventSink lastSink;
74 - private long lastStart = 0; 77 + private final Stopwatch stopwatch = Stopwatch.createUnstarted();
75 private Future<?> dispatchFuture; 78 private Future<?> dispatchFuture;
76 79
77 @Override 80 @Override
...@@ -166,9 +169,9 @@ public class CoreEventDispatcher extends DefaultEventSinkRegistry ...@@ -166,9 +169,9 @@ public class CoreEventDispatcher extends DefaultEventSinkRegistry
166 EventSink sink = getSink(event.getClass()); 169 EventSink sink = getSink(event.getClass());
167 if (sink != null) { 170 if (sink != null) {
168 lastSink = sink; 171 lastSink = sink;
169 - lastStart = System.currentTimeMillis(); 172 + stopwatch.start();
170 sink.process(event); 173 sink.process(event);
171 - lastStart = 0; 174 + stopwatch.reset();
172 } else { 175 } else {
173 log.warn("No sink registered for event class {}", 176 log.warn("No sink registered for event class {}",
174 event.getClass().getName()); 177 event.getClass().getName());
...@@ -184,11 +187,11 @@ public class CoreEventDispatcher extends DefaultEventSinkRegistry ...@@ -184,11 +187,11 @@ public class CoreEventDispatcher extends DefaultEventSinkRegistry
184 private class Watchdog extends TimerTask { 187 private class Watchdog extends TimerTask {
185 @Override 188 @Override
186 public void run() { 189 public void run() {
187 - long delta = System.currentTimeMillis() - lastStart; 190 + long elapsedTimeMillis = stopwatch.elapsed(TimeUnit.MILLISECONDS);
188 - if (lastStart > 0 && delta > maxProcessMillis) { 191 + if (elapsedTimeMillis > maxProcessMillis) {
189 - lastStart = 0; 192 + stopwatch.reset();
190 log.warn("Event sink {} exceeded execution time limit: {} ms; spawning new dispatch loop", 193 log.warn("Event sink {} exceeded execution time limit: {} ms; spawning new dispatch loop",
191 - lastSink.getClass().getName(), delta); 194 + lastSink.getClass().getName(), elapsedTimeMillis);
192 195
193 // Notify the sink that it has exceeded its time limit. 196 // Notify the sink that it has exceeded its time limit.
194 lastSink.onProcessLimit(); 197 lastSink.onProcessLimit();
......