Allow AbstractClientActor generation to start from non-zero
[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 InitialClientActorContext context, final FrontendIdentifier frontendId) {
38         super(context);
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) {
50             final ClientIdentifier nextId;
51             if (lastId != null) {
52                 if (!currentFrontend.equals(lastId.getFrontendId())) {
53                     LOG.error("{}: Mismatched frontend identifier, shutting down. Current: {} Saved: {}",
54                         persistenceId(), currentFrontend, lastId.getFrontendId());
55                     return null;
56                 }
57
58                 nextId = ClientIdentifier.create(currentFrontend, lastId.getGeneration() + 1);
59             } else {
60                 nextId = ClientIdentifier.create(currentFrontend, initialGeneration());
61             }
62
63             LOG.debug("{}: persisting new identifier {}", persistenceId(), nextId);
64             context().saveSnapshot(nextId);
65             return new SavingClientActorBehavior(context(), nextId);
66         } else if (recover instanceof SnapshotOffer) {
67             lastId = (ClientIdentifier) ((SnapshotOffer)recover).snapshot();
68             LOG.debug("{}: recovered identifier {}", persistenceId(), lastId);
69         } else {
70             LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover);
71         }
72
73         return this;
74     }
75
76     private long initialGeneration() {
77         final String propName = GENERATION_OVERRIDE_PROP_BASE + currentFrontend.getClientType().getName();
78         final String propValue = System.getProperty(propName);
79         if (propValue == null) {
80             LOG.debug("{}: no initial generation override, starting from 0", persistenceId());
81             return 0;
82         }
83
84         final long ret;
85         try {
86             ret = Long.parseUnsignedLong(propValue);
87         } catch (NumberFormatException e) {
88             LOG.warn("{}: failed to parse initial generation override '{}', starting from 0", persistenceId(),
89                 propValue, e);
90             return 0;
91         }
92
93         LOG.info("{}: initial generation set to {}", persistenceId(), ret);
94         return ret;
95     }
96 }