2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.client;
10 import static java.util.Objects.requireNonNull;
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;
20 * Transient behavior handling messages during initial actor recovery.
22 * @author Robert Varga
24 final class RecoveringClientActorBehavior extends AbstractClientActorBehavior<InitialClientActorContext> {
25 private static final Logger LOG = LoggerFactory.getLogger(RecoveringClientActorBehavior.class);
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.
31 private static final String GENERATION_OVERRIDE_PROP_BASE =
32 "org.opendaylight.controller.cluster.access.client.initial.generation.";
34 private final FrontendIdentifier currentFrontend;
35 private ClientIdentifier lastId = null;
37 RecoveringClientActorBehavior(final AbstractClientActor actor, final FrontendIdentifier frontendId) {
38 super(new InitialClientActorContext(actor, frontendId.toPersistentId()));
39 currentFrontend = requireNonNull(frontendId);
43 AbstractClientActorBehavior<?> onReceiveCommand(final Object command) {
44 throw new IllegalStateException("Frontend is recovering");
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);
54 LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover);
59 private void onSnapshotOffer(final SnapshotOffer snapshotOffer) {
60 lastId = (ClientIdentifier) snapshotOffer.snapshot();
61 LOG.debug("{}: recovered identifier {}", persistenceId(), lastId);
64 private SavingClientActorBehavior onRecoveryCompleted(final RecoveryCompleted msg) {
65 final ClientIdentifier nextId;
67 if (!currentFrontend.equals(lastId.getFrontendId())) {
68 LOG.error("{}: Mismatched frontend identifier, shutting down. Current: {} Saved: {}",
69 persistenceId(), currentFrontend, lastId.getFrontendId());
73 nextId = ClientIdentifier.create(currentFrontend, lastId.getGeneration() + 1);
75 nextId = ClientIdentifier.create(currentFrontend, initialGeneration());
78 LOG.debug("{}: persisting new identifier {}", persistenceId(), nextId);
79 context().saveSnapshot(nextId);
80 return new SavingClientActorBehavior(context(), nextId);
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());
93 ret = Long.parseUnsignedLong(propValue);
94 } catch (NumberFormatException e) {
95 LOG.warn("{}: failed to parse initial generation override '{}', starting from 0", persistenceId(),
100 LOG.info("{}: initial generation set to {}", persistenceId(), ret);