Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / actors / DataTreeNotificationListenerRegistrationActor.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies 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.actors;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.actor.Cancellable;
14 import akka.actor.PoisonPill;
15 import akka.actor.Props;
16 import com.google.common.annotations.VisibleForTesting;
17 import java.util.concurrent.TimeUnit;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
20 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
21 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistrationReply;
22 import org.opendaylight.yangtools.concepts.Registration;
23 import scala.concurrent.duration.FiniteDuration;
24
25 /**
26  * Actor co-located with a shard. It exists only to terminate the registration when
27  * asked to do so via {@link CloseDataTreeNotificationListenerRegistration}.
28  */
29 public final class DataTreeNotificationListenerRegistrationActor extends AbstractUntypedActor {
30     // FIXME: rework this constant to a duration and its injection
31     @VisibleForTesting
32     static long killDelay = TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS);
33
34     private SetRegistration registration = null;
35     private Cancellable killSchedule = null;
36     private boolean closed;
37
38     @Override
39     protected void handleReceive(final Object message) {
40         if (message instanceof CloseDataTreeNotificationListenerRegistration) {
41             closeListenerRegistration();
42             if (isValidSender(getSender())) {
43                 getSender().tell(CloseDataTreeNotificationListenerRegistrationReply.getInstance(), getSelf());
44             }
45         } else if (message instanceof SetRegistration setRegistration) {
46             registration = setRegistration;
47             if (closed) {
48                 closeListenerRegistration();
49             }
50         } else {
51             unknownMessage(message);
52         }
53     }
54
55     private void closeListenerRegistration() {
56         closed = true;
57
58         final var reg = registration;
59         if (reg != null) {
60             registration = null;
61             reg.registration.close();
62             reg.onClose.run();
63
64             if (killSchedule == null) {
65                 killSchedule = getContext().system().scheduler().scheduleOnce(FiniteDuration.create(killDelay,
66                         TimeUnit.MILLISECONDS), getSelf(), PoisonPill.getInstance(), getContext().dispatcher(),
67                         ActorRef.noSender());
68             }
69         }
70     }
71
72     public static Props props() {
73         return Props.create(DataTreeNotificationListenerRegistrationActor.class);
74     }
75
76     @NonNullByDefault
77     public record SetRegistration(Registration registration, Runnable onClose) {
78         public SetRegistration {
79             requireNonNull(registration);
80             requireNonNull(onClose);
81         }
82     }
83 }