BUG-5280: add a transction purge step
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / AbstractFrontendHistory.java
index d7e3c6533f901b443becfefdf3302ac6cb74d3cc..dbea3a1cbb5cd00058c0bcf38dff6ce7f0b1e1b1 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
+import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Ticker;
 import java.util.HashMap;
@@ -16,6 +17,8 @@ import javax.annotation.Nullable;
 import org.opendaylight.controller.cluster.access.commands.AbstractReadTransactionRequest;
 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
 import org.opendaylight.controller.cluster.access.commands.OutOfOrderRequestException;
+import org.opendaylight.controller.cluster.access.commands.TransactionPurgeRequest;
+import org.opendaylight.controller.cluster.access.commands.TransactionPurgeResponse;
 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
@@ -56,26 +59,34 @@ abstract class AbstractFrontendHistory implements Identifiable<LocalHistoryIdent
 
     final @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
             final RequestEnvelope envelope, final long now) throws RequestException {
-
-        // FIXME: handle purging of transactions
-
         final TransactionIdentifier id = request.getTarget();
-        FrontendTransaction tx = transactions.get(id);
-        if (tx == null) {
-            // The transaction does not exist and we are about to create it, check sequence number
-            if (request.getSequence() != 0) {
-                LOG.debug("{}: no transaction state present, unexpected request {}", persistenceId(), request);
-                throw UNSEQUENCED_START;
-            }
 
-            tx = createTransaction(request, id);
-            transactions.put(id, tx);
+        FrontendTransaction tx;
+        if (request instanceof TransactionPurgeRequest) {
+            tx = transactions.remove(id);
+            if (tx == null) {
+                // We have no record of the transaction, nothing to do
+                LOG.debug("{}: no state for transaction {}, purge is complete", persistenceId(), id);
+                return new TransactionPurgeResponse(id, request.getSequence());
+            }
         } else {
-            final Optional<TransactionSuccess<?>> maybeReplay = tx.replaySequence(request.getSequence());
-            if (maybeReplay.isPresent()) {
-                final TransactionSuccess<?> replay = maybeReplay.get();
-                LOG.debug("{}: envelope {} replaying response {}", persistenceId(), envelope, replay);
-                return replay;
+            tx = transactions.get(id);
+            if (tx == null) {
+                // The transaction does not exist and we are about to create it, check sequence number
+                if (request.getSequence() != 0) {
+                    LOG.debug("{}: no transaction state present, unexpected request {}", persistenceId(), request);
+                    throw UNSEQUENCED_START;
+                }
+
+                tx = createTransaction(request, id);
+                transactions.put(id, tx);
+            } else {
+                final Optional<TransactionSuccess<?>> maybeReplay = tx.replaySequence(request.getSequence());
+                if (maybeReplay.isPresent()) {
+                    final TransactionSuccess<?> replay = maybeReplay.get();
+                    LOG.debug("{}: envelope {} replaying response {}", persistenceId(), envelope, replay);
+                    return replay;
+                }
             }
         }
 
@@ -107,4 +118,10 @@ abstract class AbstractFrontendHistory implements Identifiable<LocalHistoryIdent
         throws RequestException;
 
     abstract ShardDataTreeCohort createReadyCohort(TransactionIdentifier id, DataTreeModification mod);
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this).omitNullValues().add("identifier", getIdentifier())
+                .add("persistenceId", persistenceId).add("transactions", transactions).toString();
+    }
 }