81023562801926fb0aa05c394afb9014c4be5240
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeNotificationPublisherActor.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.Props;
11 import com.google.common.base.Stopwatch;
12 import java.util.concurrent.TimeUnit;
13 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
15
16 /**
17  * Actor used to generate and publish data tree notifications. This is used to offload the potentially
18  * expensive notification generation from the Shard actor.
19  *
20  * @author Thomas Pantelis
21  */
22 public class ShardDataTreeNotificationPublisherActor extends AbstractUntypedActor {
23     private final Stopwatch timer = Stopwatch.createUnstarted();
24     private final String name;
25
26     private ShardDataTreeNotificationPublisherActor(String name) {
27         this.name = name;
28     }
29
30     @Override
31     protected void handleReceive(Object message) {
32         if(message instanceof PublishNotifications) {
33             PublishNotifications publisher = (PublishNotifications)message;
34             timer.start();
35
36             try {
37                 publisher.publish();
38             } finally {
39                 long elapsedTime = timer.elapsed(TimeUnit.MILLISECONDS);
40
41                 if(elapsedTime >= ShardDataTreeNotificationPublisher.PUBLISH_DELAY_THRESHOLD_IN_MS) {
42                     LOG.warn("{}: Generation of change events for {} took longer than expected. Elapsed time: {}",
43                             publisher.logContext, name, timer);
44                 } else {
45                     LOG.debug("{}: Elapsed time for generation of change events for {}: {}", publisher.logContext,
46                             name, timer);
47                 }
48
49                 timer.reset();
50             }
51         }
52     }
53
54     static Props props(String notificationType) {
55         return Props.create(ShardDataTreeNotificationPublisherActor.class, notificationType);
56     }
57
58     static class PublishNotifications {
59         private final ShardDataTreeNotificationPublisher publisher;
60         private final DataTreeCandidate candidate;
61         private final String logContext;
62
63         PublishNotifications(ShardDataTreeNotificationPublisher publisher, DataTreeCandidate candidate,
64                 String logContext) {
65             this.publisher = publisher;
66             this.candidate = candidate;
67             this.logContext = logContext;
68         }
69
70         private void publish() {
71             publisher.publishChanges(candidate, logContext);
72         }
73     }
74 }