Add UnsignedLongBitmap
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalFrontendHistory.java
index cd0cc30a09738e49915939cd967a0dc04df472c4..3125ed651a5c6cbe64a511e83a2348beb4b3cb14 100644 (file)
@@ -7,17 +7,18 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
-import com.google.common.base.Preconditions;
-import com.google.common.base.Ticker;
-import org.opendaylight.controller.cluster.access.commands.DeadTransactionException;
-import org.opendaylight.controller.cluster.access.commands.LocalHistorySuccess;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.primitives.UnsignedLong;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.SortedSet;
 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
-import org.opendaylight.controller.cluster.access.concepts.RequestException;
 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
+import org.opendaylight.controller.cluster.datastore.utils.MutableUnsignedLongSet;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 
 /**
  * Chained transaction specialization of {@link AbstractFrontendHistory}. It prevents concurrent open transactions.
@@ -25,21 +26,25 @@ import org.slf4j.LoggerFactory;
  * @author Robert Varga
  */
 final class LocalFrontendHistory extends AbstractFrontendHistory {
-    private enum State {
-        OPEN,
-        CLOSED,
-    }
-
-    private static final Logger LOG = LoggerFactory.getLogger(LocalFrontendHistory.class);
-
     private final ShardDataTreeTransactionChain chain;
 
-    private Long lastSeenTransaction;
-    private State state = State.OPEN;
+    private LocalFrontendHistory(final String persistenceId, final ShardDataTree tree,
+            final ShardDataTreeTransactionChain chain, final Map<UnsignedLong, Boolean> closedTransactions,
+            final MutableUnsignedLongSet purgedTransactions) {
+        super(persistenceId, tree, closedTransactions, purgedTransactions);
+        this.chain = requireNonNull(chain);
+    }
+
+    static LocalFrontendHistory create(final String persistenceId, final ShardDataTree tree,
+            final ShardDataTreeTransactionChain chain) {
+        return new LocalFrontendHistory(persistenceId, tree, chain, ImmutableMap.of(), MutableUnsignedLongSet.of());
+    }
 
-    LocalFrontendHistory(final String persistenceId, final Ticker ticker, final ShardDataTreeTransactionChain chain) {
-        super(persistenceId, ticker);
-        this.chain = Preconditions.checkNotNull(chain);
+    static LocalFrontendHistory recreate(final String persistenceId, final ShardDataTree tree,
+            final ShardDataTreeTransactionChain chain, final Map<UnsignedLong, Boolean> closedTransactions,
+            final MutableUnsignedLongSet purgedTransactions) {
+        return new LocalFrontendHistory(persistenceId, tree, chain, new HashMap<>(closedTransactions),
+            purgedTransactions.mutableCopy());
     }
 
     @Override
@@ -48,55 +53,29 @@ final class LocalFrontendHistory extends AbstractFrontendHistory {
     }
 
     @Override
-    FrontendTransaction createOpenSnapshot(final TransactionIdentifier id) throws RequestException {
-        checkDeadTransaction(id);
-        lastSeenTransaction = id.getTransactionId();
+    FrontendTransaction createOpenSnapshot(final TransactionIdentifier id) {
         return FrontendReadOnlyTransaction.create(this, chain.newReadOnlyTransaction(id));
     }
 
     @Override
-    FrontendTransaction createOpenTransaction(final TransactionIdentifier id) throws RequestException {
-        checkDeadTransaction(id);
-        lastSeenTransaction = id.getTransactionId();
+    FrontendTransaction createOpenTransaction(final TransactionIdentifier id) {
         return FrontendReadWriteTransaction.createOpen(this, chain.newReadWriteTransaction(id));
     }
 
     @Override
-    FrontendTransaction createReadyTransaction(final TransactionIdentifier id, final DataTreeModification mod)
-            throws RequestException {
-        checkDeadTransaction(id);
-        lastSeenTransaction = id.getTransactionId();
+    FrontendTransaction createReadyTransaction(final TransactionIdentifier id, final DataTreeModification mod) {
         return FrontendReadWriteTransaction.createReady(this, id, mod);
     }
 
     @Override
-    ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod) {
-        return chain.createReadyCohort(id, mod);
+    ShardDataTreeCohort createFailedCohort(final TransactionIdentifier id, final DataTreeModification mod,
+            final Exception failure) {
+        return chain.createFailedCohort(id, mod, failure);
     }
 
-    LocalHistorySuccess destroy(final long sequence, final long now) throws RequestException {
-        if (state != State.CLOSED) {
-            LOG.debug("{}: closing history {}", persistenceId(), getIdentifier());
-
-            // FIXME: add any finalization as needed
-            state = State.CLOSED;
-        }
-
-        // FIXME: record a DESTROY tombstone in the journal
-        return new LocalHistorySuccess(getIdentifier(), sequence);
-    }
-
-    boolean isDestroyed() {
-        return state == State.CLOSED;
-    }
-
-    private void checkDeadTransaction(final TransactionIdentifier id) throws RequestException {
-        // FIXME: check if this history is still open
-        // FIXME: check if the last transaction has been submitted
-
-        // Transaction identifiers within a local history have to have increasing IDs
-        if (lastSeenTransaction != null && Long.compareUnsigned(lastSeenTransaction, id.getTransactionId()) >= 0) {
-            throw new DeadTransactionException(lastSeenTransaction);
-        }
+    @Override
+    ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod,
+            final Optional<SortedSet<String>> participatingShardNames) {
+        return chain.createReadyCohort(id, mod, participatingShardNames);
     }
 }