Improve LocalProxyTransaction.doExists()
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeNotificationPublisherActor.java
index e4e7eb33e9d4b176a4bb87f3212d12eb1d9b11bc..095a542f6c69417b03b9d17f78dc1e9db5d17e4c 100644 (file)
@@ -7,9 +7,10 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
-import akka.actor.Props;
+import com.google.common.base.Stopwatch;
+import java.util.concurrent.TimeUnit;
 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
 
 /**
  * Actor used to generate and publish data tree notifications. This is used to offload the potentially
@@ -17,33 +18,55 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
  *
  * @author Thomas Pantelis
  */
-public class ShardDataTreeNotificationPublisherActor extends AbstractUntypedActor {
+public class ShardDataTreeNotificationPublisherActor<T extends ShardDataTreeNotificationPublisher>
+        extends AbstractUntypedActor {
+    private final T publisher;
+    private final Stopwatch timer = Stopwatch.createUnstarted();
+    private final String name;
+    private final String logContext;
 
-    @Override
-    protected void handleReceive(Object message) {
-        if(message instanceof PublishNotifications) {
-            ((PublishNotifications)message).publish();
-        }
+    protected ShardDataTreeNotificationPublisherActor(final T publisher, final String name, final String logContext) {
+        this.publisher = publisher;
+        this.name = name;
+        this.logContext = logContext;
+    }
+
+    protected T publisher() {
+        return publisher;
     }
 
-    static Props props() {
-        return Props.create(ShardDataTreeNotificationPublisherActor.class);
+    protected String logContext() {
+        return logContext;
+    }
+
+    @Override
+    protected void handleReceive(final Object message) {
+        if (message instanceof PublishNotifications) {
+            PublishNotifications toPublish = (PublishNotifications)message;
+            timer.start();
+
+            try {
+                publisher.publishChanges(toPublish.candidate);
+            } finally {
+                long elapsedTime = timer.elapsed(TimeUnit.MILLISECONDS);
+
+                if (elapsedTime >= ShardDataTreeNotificationPublisher.PUBLISH_DELAY_THRESHOLD_IN_MS) {
+                    LOG.warn("{}: Generation of change events for {} took longer than expected. Elapsed time: {}",
+                            logContext, name, timer);
+                } else {
+                    LOG.debug("{}: Elapsed time for generation of change events for {}: {}", logContext, name, timer);
+                }
+
+                timer.reset();
+            }
+        }
     }
 
     static class PublishNotifications {
-        private final ShardDataTreeNotificationPublisher publisher;
         private final DataTreeCandidate candidate;
-        private final String logContext;
 
-        PublishNotifications(ShardDataTreeNotificationPublisher publisher, DataTreeCandidate candidate,
-                String logContext) {
-            this.publisher = publisher;
+        PublishNotifications(final DataTreeCandidate candidate) {
             this.candidate = candidate;
-            this.logContext = logContext;
-        }
-
-        private void publish() {
-            publisher.publishChanges(candidate, logContext);
         }
     }
 }