Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / RecoveringClientActorBehavior.java
index 38ce67a5085ec6b5b77137321619566da1e12a8e..b44d54921d9de38b898a06633f6c5dec25b2c36e 100644 (file)
@@ -7,27 +7,36 @@
  */
 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;
 import org.slf4j.LoggerFactory;
 
 /**
- * @param <T> Frontend type
+ * Transient behavior handling messages during initial actor recovery.
  *
  * @author Robert Varga
  */
 final class RecoveringClientActorBehavior extends AbstractClientActorBehavior<InitialClientActorContext> {
     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);
+        currentFrontend = requireNonNull(frontendId);
     }
 
     @Override
@@ -41,26 +50,47 @@ final class RecoveringClientActorBehavior extends AbstractClientActorBehavior<In
             final ClientIdentifier nextId;
             if (lastId != null) {
                 if (!currentFrontend.equals(lastId.getFrontendId())) {
-                    LOG.error("Mismatched frontend identifier, shutting down. Current: {} Saved: {}", currentFrontend,
-                    lastId.getFrontendId());
+                    LOG.error("{}: Mismatched frontend identifier, shutting down. Current: {} Saved: {}",
+                        persistenceId(), currentFrontend, lastId.getFrontendId());
                     return null;
                 }
 
                 nextId = ClientIdentifier.create(currentFrontend, lastId.getGeneration() + 1);
             } else {
-                nextId = ClientIdentifier.create(currentFrontend, 0);
+                nextId = ClientIdentifier.create(currentFrontend, initialGeneration());
             }
 
             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 {}", lastId);
+        } else if (recover instanceof SnapshotOffer snapshotOffer) {
+            lastId = (ClientIdentifier) snapshotOffer.snapshot();
+            LOG.debug("{}: recovered identifier {}", persistenceId(), lastId);
         } else {
-            LOG.warn("{}: ignoring recovery message {}", recover);
+            LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover);
         }
 
         return this;
     }
-}
\ No newline at end of file
+
+    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;
+    }
+}