Fix free-standing transaction lookup with module-based shards
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / FrontendClientMetadataBuilder.java
index e4ab4b5ceff823663de9c6658cc1d98b16032537..6270b380cb701bbcf7f513a8bfa9bacb2923ddf4 100644 (file)
@@ -7,14 +7,11 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
+import static com.google.common.base.Verify.verify;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.base.MoreObjects;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Verify;
 import com.google.common.collect.Collections2;
-import com.google.common.collect.Range;
-import com.google.common.collect.RangeSet;
-import com.google.common.collect.TreeRangeSet;
-import com.google.common.primitives.UnsignedLong;
 import java.util.HashMap;
 import java.util.Map;
 import javax.annotation.Nonnull;
@@ -24,6 +21,7 @@ import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifie
 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
 import org.opendaylight.controller.cluster.datastore.persisted.FrontendClientMetadata;
 import org.opendaylight.controller.cluster.datastore.persisted.FrontendHistoryMetadata;
+import org.opendaylight.controller.cluster.datastore.utils.UnsignedLongRangeSet;
 import org.opendaylight.yangtools.concepts.Builder;
 import org.opendaylight.yangtools.concepts.Identifiable;
 import org.slf4j.Logger;
@@ -34,24 +32,25 @@ final class FrontendClientMetadataBuilder implements Builder<FrontendClientMetad
     private static final Logger LOG = LoggerFactory.getLogger(FrontendClientMetadataBuilder.class);
 
     private final Map<LocalHistoryIdentifier, FrontendHistoryMetadataBuilder> currentHistories = new HashMap<>();
-    private final RangeSet<UnsignedLong> purgedHistories;
+    private final UnsignedLongRangeSet purgedHistories;
+    private final LocalHistoryIdentifier standaloneId;
     private final ClientIdentifier identifier;
     private final String shardName;
 
     FrontendClientMetadataBuilder(final String shardName, final ClientIdentifier identifier) {
-        this.shardName = Preconditions.checkNotNull(shardName);
-        this.identifier = Preconditions.checkNotNull(identifier);
-        purgedHistories = TreeRangeSet.create();
+        this.shardName = requireNonNull(shardName);
+        this.identifier = requireNonNull(identifier);
+        purgedHistories = UnsignedLongRangeSet.create();
 
         // History for stand-alone transactions is always present
-        final LocalHistoryIdentifier standaloneId = standaloneHistoryId();
+        standaloneId = standaloneHistoryId();
         currentHistories.put(standaloneId, new FrontendHistoryMetadataBuilder(standaloneId));
     }
 
     FrontendClientMetadataBuilder(final String shardName, final FrontendClientMetadata meta) {
-        this.shardName = Preconditions.checkNotNull(shardName);
-        this.identifier = Preconditions.checkNotNull(meta.getIdentifier());
-        purgedHistories = TreeRangeSet.create(meta.getPurgedHistories());
+        this.shardName = requireNonNull(shardName);
+        this.identifier = meta.getIdentifier();
+        purgedHistories = UnsignedLongRangeSet.create(meta.getPurgedHistories());
 
         for (FrontendHistoryMetadata h : meta.getCurrentHistories()) {
             final FrontendHistoryMetadataBuilder b = new FrontendHistoryMetadataBuilder(identifier, h);
@@ -59,7 +58,7 @@ final class FrontendClientMetadataBuilder implements Builder<FrontendClientMetad
         }
 
         // Sanity check and recovery
-        final LocalHistoryIdentifier standaloneId = standaloneHistoryId();
+        standaloneId = standaloneHistoryId();
         if (!currentHistories.containsKey(standaloneId)) {
             LOG.warn("{}: Client {} recovered histories {} do not contain stand-alone history, attempting recovery",
                 shardName, identifier, currentHistories);
@@ -73,7 +72,7 @@ final class FrontendClientMetadataBuilder implements Builder<FrontendClientMetad
 
     @Override
     public FrontendClientMetadata build() {
-        return new FrontendClientMetadata(identifier, purgedHistories,
+        return new FrontendClientMetadata(identifier, purgedHistories.toImmutable(),
             Collections2.transform(currentHistories.values(), FrontendHistoryMetadataBuilder::build));
     }
 
@@ -105,20 +104,25 @@ final class FrontendClientMetadataBuilder implements Builder<FrontendClientMetad
 
     void onHistoryPurged(final LocalHistoryIdentifier historyId) {
         final FrontendHistoryMetadataBuilder history = currentHistories.remove(historyId);
+        final long historyBits = historyId.getHistoryId();
         if (history == null) {
-            LOG.warn("{}: Purging unknown history {}", shardName, historyId);
+            if (!purgedHistories.contains(historyBits)) {
+                purgedHistories.add(historyBits);
+                LOG.warn("{}: Purging unknown history {}", shardName, historyId);
+            } else {
+                LOG.warn("{}: Duplicate purge of history {}", shardName, historyId);
+            }
+        } else {
+            purgedHistories.add(historyBits);
+            LOG.debug("{}: Purged history {}", shardName, historyId);
         }
-
-        // XXX: do we need to account for cookies?
-        purgedHistories.add(Range.singleton(UnsignedLong.fromLongBits(historyId.getHistoryId())));
-        LOG.debug("{}: Purged history {}", historyId);
     }
 
     void onTransactionAborted(final TransactionIdentifier txId) {
         final FrontendHistoryMetadataBuilder history = getHistory(txId);
         if (history != null) {
             history.onTransactionAborted(txId);
-            LOG.debug("{}: Committed transaction {}", shardName, txId);
+            LOG.debug("{}: Aborted transaction {}", shardName, txId);
         } else {
             LOG.warn("{}: Unknown history for aborted transaction {}, ignoring", shardName, txId);
         }
@@ -128,7 +132,7 @@ final class FrontendClientMetadataBuilder implements Builder<FrontendClientMetad
         final FrontendHistoryMetadataBuilder history = getHistory(txId);
         if (history != null) {
             history.onTransactionCommitted(txId);
-            LOG.debug("{}: Aborted transaction {}", shardName, txId);
+            LOG.debug("{}: Committed transaction {}", shardName, txId);
         } else {
             LOG.warn("{}: Unknown history for commited transaction {}, ignoring", shardName, txId);
         }
@@ -157,7 +161,7 @@ final class FrontendClientMetadataBuilder implements Builder<FrontendClientMetad
         for (FrontendHistoryMetadataBuilder e : currentHistories.values()) {
             if (e.getIdentifier().getHistoryId() != 0) {
                 final AbstractFrontendHistory state = e.toLeaderState(shard);
-                Verify.verify(state instanceof LocalFrontendHistory);
+                verify(state instanceof LocalFrontendHistory, "Unexpected state %s", state);
                 histories.put(e.getIdentifier(), (LocalFrontendHistory) state);
             }
         }
@@ -173,11 +177,19 @@ final class FrontendClientMetadataBuilder implements Builder<FrontendClientMetad
         }
 
         return new LeaderFrontendState(shard.persistenceId(), getIdentifier(), shard.getDataStore(),
-            TreeRangeSet.create(purgedHistories), singleHistory, histories);
+            purgedHistories.copy(), singleHistory, histories);
     }
 
     private FrontendHistoryMetadataBuilder getHistory(final TransactionIdentifier txId) {
-        return currentHistories.get(txId.getHistoryId());
+        LocalHistoryIdentifier historyId = txId.getHistoryId();
+        if (historyId.getHistoryId() == 0 && historyId.getCookie() != 0) {
+            // We are pre-creating the history for free-standing transactions with a zero cookie, hence our lookup
+            // needs to account for that.
+            LOG.debug("{}: looking up {} instead of {}", shardName, standaloneId, historyId);
+            historyId = standaloneId;
+        }
+
+        return currentHistories.get(historyId);
     }
 
     @Override