X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fmd-sal%2Fcds-access-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Faccess%2Fclient%2FRecoveringClientActorBehavior.java;h=f40deab30d5bed8bbf1a7eed8da3e76653ce27bf;hb=HEAD;hp=5e2b455abc4f8f88e685d0aff9cea882d6fbb7f3;hpb=50664aceae387ef6dc9a952f5a6d4105d0d3b4a7;p=controller.git diff --git a/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/RecoveringClientActorBehavior.java b/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/RecoveringClientActorBehavior.java index 5e2b455abc..729d53dfc3 100644 --- a/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/RecoveringClientActorBehavior.java +++ b/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/RecoveringClientActorBehavior.java @@ -7,9 +7,10 @@ */ package org.opendaylight.controller.cluster.access.client; +import static java.util.Objects.requireNonNull; + import akka.persistence.RecoveryCompleted; import akka.persistence.SnapshotOffer; -import com.google.common.base.Preconditions; import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier; import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier; import org.slf4j.Logger; @@ -22,12 +23,20 @@ import org.slf4j.LoggerFactory; */ final class RecoveringClientActorBehavior extends AbstractClientActorBehavior { private static final Logger LOG = LoggerFactory.getLogger(RecoveringClientActorBehavior.class); + + /* + * Base for the property name which overrides the initial generation when we fail to find anything from persistence. + * The actual property name has the frontend type name appended. + */ + private static final String GENERATION_OVERRIDE_PROP_BASE = + "org.opendaylight.controller.cluster.access.client.initial.generation."; + private final FrontendIdentifier currentFrontend; private ClientIdentifier lastId = null; - RecoveringClientActorBehavior(final InitialClientActorContext context, final FrontendIdentifier frontendId) { - super(context); - currentFrontend = Preconditions.checkNotNull(frontendId); + RecoveringClientActorBehavior(final AbstractClientActor actor, final FrontendIdentifier frontendId) { + super(new InitialClientActorContext(actor, frontendId.toPersistentId())); + currentFrontend = requireNonNull(frontendId); } @Override @@ -37,30 +46,58 @@ final class RecoveringClientActorBehavior extends AbstractClientActorBehavior onReceiveRecover(final Object recover) { - if (recover instanceof RecoveryCompleted) { - final ClientIdentifier nextId; - if (lastId != null) { - if (!currentFrontend.equals(lastId.getFrontendId())) { - LOG.error("{}: Mismatched frontend identifier, shutting down. Current: {} Saved: {}", - persistenceId(), currentFrontend, lastId.getFrontendId()); - return null; - } + if (recover instanceof RecoveryCompleted msg) { + return onRecoveryCompleted(msg); + } else if (recover instanceof SnapshotOffer snapshotOffer) { + onSnapshotOffer(snapshotOffer); + } else { + LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover); + } + return this; + } + + private void onSnapshotOffer(final SnapshotOffer snapshotOffer) { + lastId = (ClientIdentifier) snapshotOffer.snapshot(); + LOG.debug("{}: recovered identifier {}", persistenceId(), lastId); + } - nextId = ClientIdentifier.create(currentFrontend, lastId.getGeneration() + 1); - } else { - nextId = ClientIdentifier.create(currentFrontend, 0); + private SavingClientActorBehavior onRecoveryCompleted(final RecoveryCompleted msg) { + final ClientIdentifier nextId; + if (lastId != null) { + if (!currentFrontend.equals(lastId.getFrontendId())) { + LOG.error("{}: Mismatched frontend identifier, shutting down. Current: {} Saved: {}", + persistenceId(), currentFrontend, lastId.getFrontendId()); + return null; } - LOG.debug("{}: persisting new identifier {}", persistenceId(), nextId); - context().saveSnapshot(nextId); - return new SavingClientActorBehavior(context(), nextId); - } else if (recover instanceof SnapshotOffer) { - lastId = (ClientIdentifier) ((SnapshotOffer)recover).snapshot(); - LOG.debug("{}: recovered identifier {}", persistenceId(), lastId); + nextId = ClientIdentifier.create(currentFrontend, lastId.getGeneration() + 1); } else { - LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover); + nextId = ClientIdentifier.create(currentFrontend, initialGeneration()); } - return this; + LOG.debug("{}: persisting new identifier {}", persistenceId(), nextId); + context().saveSnapshot(nextId); + return new SavingClientActorBehavior(context(), nextId); + } + + private long initialGeneration() { + final String propName = GENERATION_OVERRIDE_PROP_BASE + currentFrontend.getClientType().getName(); + final String propValue = System.getProperty(propName); + if (propValue == null) { + LOG.debug("{}: no initial generation override, starting from 0", persistenceId()); + return 0; + } + + final long ret; + try { + ret = Long.parseUnsignedLong(propValue); + } catch (NumberFormatException e) { + LOG.warn("{}: failed to parse initial generation override '{}', starting from 0", persistenceId(), + propValue, e); + return 0; + } + + LOG.info("{}: initial generation set to {}", persistenceId(), ret); + return ret; } -} \ No newline at end of file +}