BUG-5280: define RetiredGenerationException
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / actors / client / ClientActorBehavior.java
index b770b99871d79aaf95daea317a179e73fcf58bb5..1d812b7590b0715468e9e53793a6529320785f0b 100644 (file)
@@ -11,7 +11,12 @@ import com.google.common.annotations.Beta;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
+import org.opendaylight.controller.cluster.access.concepts.RequestException;
+import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
+import org.opendaylight.controller.cluster.access.concepts.RetiredGenerationException;
 import org.opendaylight.yangtools.concepts.Identifiable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * A behavior, which handles messages sent to a {@link AbstractClientActor}.
@@ -23,12 +28,24 @@ import org.opendaylight.yangtools.concepts.Identifiable;
 @Beta
 public abstract class ClientActorBehavior extends RecoveredClientActorBehavior<ClientActorContext>
         implements Identifiable<ClientIdentifier> {
+    private static final Logger LOG = LoggerFactory.getLogger(ClientActorBehavior.class);
+
     protected ClientActorBehavior(final @Nonnull ClientActorContext context) {
         super(context);
     }
 
     @Override
     final ClientActorBehavior onReceiveCommand(final Object command) {
+        if (command instanceof RequestFailure) {
+            final RequestFailure<?, ?> failure = (RequestFailure<?, ?>) command;
+            final RequestException cause = failure.getCause();
+            if (cause instanceof RetiredGenerationException) {
+                LOG.error("{}: current generation {} has been superseded", persistenceId(), getIdentifier(), cause);
+                haltClient(cause);
+                return null;
+            }
+        }
+
         // TODO: any client-common logic (such as validation and common dispatch) needs to go here
         return onCommand(command);
     }
@@ -38,6 +55,16 @@ public abstract class ClientActorBehavior extends RecoveredClientActorBehavior<C
         return context().getIdentifier();
     }
 
+    /**
+     * Halt And Catch Fire.
+     *
+     * Halt processing on this client. Implementations need to ensure they initiate state flush procedures. No attempt
+     * to use this instance should be made after this method returns. Any such use may result in undefined behavior.
+     *
+     * @param cause Failure cause
+     */
+    protected abstract void haltClient(@Nonnull Throwable cause);
+
     /**
      * Override this method to handle any command which is not handled by the base behavior.
      *