729d53dfc36445debce67c52174ea3729eee7ed0
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / RecoveringClientActorBehavior.java
1 /*
2  * Copyright (c) 2016 Cisco 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.access.client;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.persistence.RecoveryCompleted;
13 import akka.persistence.SnapshotOffer;
14 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
15 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Transient behavior handling messages during initial actor recovery.
21  *
22  * @author Robert Varga
23  */
24 final class RecoveringClientActorBehavior extends AbstractClientActorBehavior<InitialClientActorContext> {
25     private static final Logger LOG = LoggerFactory.getLogger(RecoveringClientActorBehavior.class);
26
27     /*
28      * Base for the property name which overrides the initial generation when we fail to find anything from persistence.
29      * The actual property name has the frontend type name appended.
30      */
31     private static final String GENERATION_OVERRIDE_PROP_BASE =
32             "org.opendaylight.controller.cluster.access.client.initial.generation.";
33
34     private final FrontendIdentifier currentFrontend;
35     private ClientIdentifier lastId = null;
36
37     RecoveringClientActorBehavior(final AbstractClientActor actor, final FrontendIdentifier frontendId) {
38         super(new InitialClientActorContext(actor, frontendId.toPersistentId()));
39         currentFrontend = requireNonNull(frontendId);
40     }
41
42     @Override
43     AbstractClientActorBehavior<?> onReceiveCommand(final Object command) {
44         throw new IllegalStateException("Frontend is recovering");
45     }
46
47     @Override
48     AbstractClientActorBehavior<?> onReceiveRecover(final Object recover) {
49         if (recover instanceof RecoveryCompleted msg) {
50             return onRecoveryCompleted(msg);
51         } else if (recover instanceof SnapshotOffer snapshotOffer) {
52             onSnapshotOffer(snapshotOffer);
53         } else {
54             LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover);
55         }
56         return this;
57     }
58
59     private void onSnapshotOffer(final SnapshotOffer snapshotOffer) {
60         lastId = (ClientIdentifier) snapshotOffer.snapshot();
61         LOG.debug("{}: recovered identifier {}", persistenceId(), lastId);
62     }
63
64     private SavingClientActorBehavior onRecoveryCompleted(final RecoveryCompleted msg) {
65         final ClientIdentifier nextId;
66         if (lastId != null) {
67             if (!currentFrontend.equals(lastId.getFrontendId())) {
68                 LOG.error("{}: Mismatched frontend identifier, shutting down. Current: {} Saved: {}",
69                     persistenceId(), currentFrontend, lastId.getFrontendId());
70                 return null;
71             }
72
73             nextId = ClientIdentifier.create(currentFrontend, lastId.getGeneration() + 1);
74         } else {
75             nextId = ClientIdentifier.create(currentFrontend, initialGeneration());
76         }
77
78         LOG.debug("{}: persisting new identifier {}", persistenceId(), nextId);
79         context().saveSnapshot(nextId);
80         return new SavingClientActorBehavior(context(), nextId);
81     }
82
83     private long initialGeneration() {
84         final String propName = GENERATION_OVERRIDE_PROP_BASE + currentFrontend.getClientType().getName();
85         final String propValue = System.getProperty(propName);
86         if (propValue == null) {
87             LOG.debug("{}: no initial generation override, starting from 0", persistenceId());
88             return 0;
89         }
90
91         final long ret;
92         try {
93             ret = Long.parseUnsignedLong(propValue);
94         } catch (NumberFormatException e) {
95             LOG.warn("{}: failed to parse initial generation override '{}', starting from 0", persistenceId(),
96                 propValue, e);
97             return 0;
98         }
99
100         LOG.info("{}: initial generation set to {}", persistenceId(), ret);
101         return ret;
102     }
103 }