Fix intermittent PreLeaderScenarioTest failure
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / AbstractShardTest.java
index 8da5bf738718c24b45a575dd03b7e612c6b0de14..6f7d8fb9df716fc474a3759e312be2816d1920ba 100644 (file)
@@ -55,7 +55,7 @@ import org.opendaylight.controller.cluster.datastore.modification.MergeModificat
 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload;
-import org.opendaylight.controller.cluster.datastore.persisted.PreBoronShardDataTreeSnapshot;
+import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
 import org.opendaylight.controller.cluster.raft.TestActorFactory;
@@ -95,11 +95,14 @@ public abstract class AbstractShardTest extends AbstractActorTest {
 
     private static final AtomicInteger NEXT_SHARD_NUM = new AtomicInteger();
 
+    protected static final int HEARTBEAT_MILLIS = 100;
+
     protected final ShardIdentifier shardID = ShardIdentifier.create("inventory", MemberName.forName("member-1"),
         "config" + NEXT_SHARD_NUM.getAndIncrement());
 
     protected final Builder dataStoreContextBuilder = DatastoreContext.newBuilder()
-            .shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).shardHeartbeatIntervalInMillis(100);
+            .shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000)
+            .shardHeartbeatIntervalInMillis(HEARTBEAT_MILLIS);
 
     protected final TestActorFactory actorFactory = new TestActorFactory(getSystem());
 
@@ -125,7 +128,8 @@ public abstract class AbstractShardTest extends AbstractActorTest {
     }
 
     protected Shard.Builder newShardBuilder() {
-        return Shard.builder().id(shardID).datastoreContext(newDatastoreContext()).schemaContext(SCHEMA_CONTEXT);
+        return Shard.builder().id(shardID).datastoreContext(newDatastoreContext())
+            .schemaContextProvider(() -> SCHEMA_CONTEXT);
     }
 
     protected void testRecovery(final Set<Integer> listEntryKeys) throws Exception {
@@ -206,9 +210,8 @@ public abstract class AbstractShardTest extends AbstractActorTest {
             return null;
         }).when(mock).validate(any(DataTreeModification.class));
 
-        doAnswer(invocation -> {
-            return actual.prepare(invocation.getArgumentAt(0, DataTreeModification.class));
-        }).when(mock).prepare(any(DataTreeModification.class));
+        doAnswer(invocation -> actual.prepare(invocation.getArgumentAt(0, DataTreeModification.class))).when(
+                mock).prepare(any(DataTreeModification.class));
 
         doAnswer(invocation -> {
             actual.commit(invocation.getArgumentAt(0, DataTreeCandidate.class));
@@ -220,13 +223,9 @@ public abstract class AbstractShardTest extends AbstractActorTest {
             return null;
         }).when(mock).setSchemaContext(any(SchemaContext.class));
 
-        doAnswer(invocation -> {
-            return actual.takeSnapshot();
-        }).when(mock).takeSnapshot();
+        doAnswer(invocation -> actual.takeSnapshot()).when(mock).takeSnapshot();
 
-        doAnswer(invocation -> {
-            return actual.getRootPath();
-        }).when(mock).getRootPath();
+        doAnswer(invocation -> actual.getRootPath()).when(mock).getRootPath();
 
         return mock;
     }
@@ -310,11 +309,11 @@ public abstract class AbstractShardTest extends AbstractActorTest {
     }
 
     public static void writeToStore(final ShardDataTree store, final YangInstanceIdentifier id,
-            final NormalizedNode<?,?> node) throws Exception {
+            final NormalizedNode<?,?> node) throws DataValidationFailedException {
         BatchedModifications batched = newBatchedModifications(nextTransactionId(), id, node, true, true, 1);
         DataTreeModification modification = store.getDataTree().takeSnapshot().newModification();
         batched.apply(modification);
-        store.notifyListeners(store.commit(modification));
+        store.notifyListeners(commitTransaction(store.getDataTree(), modification));
     }
 
     public static void writeToStore(final DataTree store, final YangInstanceIdentifier id,
@@ -329,7 +328,7 @@ public abstract class AbstractShardTest extends AbstractActorTest {
     }
 
     public void mergeToStore(final ShardDataTree store, final YangInstanceIdentifier id,
-            final NormalizedNode<?,?> node) throws Exception {
+            final NormalizedNode<?,?> node) throws DataValidationFailedException {
         final BatchedModifications batched = new BatchedModifications(nextTransactionId(), CURRENT_VERSION);
         batched.addModification(new MergeModification(id, node));
         batched.setReady(true);
@@ -338,7 +337,7 @@ public abstract class AbstractShardTest extends AbstractActorTest {
 
         DataTreeModification modification = store.getDataTree().takeSnapshot().newModification();
         batched.apply(modification);
-        store.notifyListeners(store.commit(modification));
+        store.notifyListeners(commitTransaction(store.getDataTree(), modification));
     }
 
     DataTree setupInMemorySnapshotStore() throws DataValidationFailedException {
@@ -350,7 +349,7 @@ public abstract class AbstractShardTest extends AbstractActorTest {
         final NormalizedNode<?, ?> root = readStore(testStore, YangInstanceIdentifier.EMPTY);
 
         InMemorySnapshotStore.addSnapshot(shardID.toString(), Snapshot.create(
-                new ShardSnapshotState(new PreBoronShardDataTreeSnapshot(root)),
+                new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
                 Collections.<ReplicatedLogEntry>emptyList(), 0, 1, -1, -1, 1, null, null));
         return testStore;
     }
@@ -410,11 +409,13 @@ public abstract class AbstractShardTest extends AbstractActorTest {
         return mockCandidate;
     }
 
-    static void commitTransaction(final DataTree store, final DataTreeModification modification)
+    static DataTreeCandidate commitTransaction(final DataTree store, final DataTreeModification modification)
             throws DataValidationFailedException {
         modification.ready();
         store.validate(modification);
-        store.commit(store.prepare(modification));
+        final DataTreeCandidate candidate = store.prepare(modification);
+        store.commit(candidate);
+        return candidate;
     }
 
     @SuppressWarnings("serial")
@@ -490,7 +491,7 @@ public abstract class AbstractShardTest extends AbstractActorTest {
         }
 
         @SuppressWarnings("unchecked")
-        private <T> FutureCallback<T> mockFutureCallback(final FutureCallback<T> actual) {
+        private static <T> FutureCallback<T> mockFutureCallback(final FutureCallback<T> actual) {
             FutureCallback<T> mock = mock(FutureCallback.class);
             doAnswer(invocation -> {
                 actual.onFailure(invocation.getArgumentAt(0, Throwable.class));