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 InitialClientActorContext context, final FrontendIdentifier frontendId) {
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) {
50 final ClientIdentifier nextId;
52 if (!currentFrontend.equals(lastId.getFrontendId())) {
53 LOG.error("{}: Mismatched frontend identifier, shutting down. Current: {} Saved: {}",
54 persistenceId(), currentFrontend, lastId.getFrontendId());
58 nextId = ClientIdentifier.create(currentFrontend, lastId.getGeneration() + 1);
60 nextId = ClientIdentifier.create(currentFrontend, initialGeneration());
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);
70 LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover);
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());
86 ret = Long.parseUnsignedLong(propValue);
87 } catch (NumberFormatException e) {
88 LOG.warn("{}: failed to parse initial generation override '{}', starting from 0", persistenceId(),
93 LOG.info("{}: initial generation set to {}", persistenceId(), ret);