use of org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort in project controller by opendaylight.
the class DistributedDataStoreIntegrationTest method testTransactionWritesWithShardNotInitiallyReady.
@SuppressWarnings("checkstyle:IllegalCatch")
private void testTransactionWritesWithShardNotInitiallyReady(final String testName, final boolean writeOnly) throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
final String shardName = "test-1";
// Setup the InMemoryJournal to block shard recovery to ensure
// the shard isn't
// initialized until we create and submit the write the Tx.
final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, testName, false, shardName)) {
// Create the write Tx
final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction() : dataStore.newReadWriteTransaction();
assertNotNull("newReadWriteTransaction returned null", writeTx);
// Do some modification operations and ready the Tx on a
// separate thread.
final YangInstanceIdentifier listEntryPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
final AtomicReference<Exception> caughtEx = new AtomicReference<>();
final CountDownLatch txReady = new CountDownLatch(1);
final Thread txThread = new Thread(() -> {
try {
writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
writeTx.merge(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
writeTx.write(listEntryPath, ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
writeTx.delete(listEntryPath);
txCohort.set(writeTx.ready());
} catch (Exception e) {
caughtEx.set(e);
} finally {
txReady.countDown();
}
});
txThread.start();
// Wait for the Tx operations to complete.
final boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
if (caughtEx.get() != null) {
throw caughtEx.get();
}
assertEquals("Tx ready", true, done);
// At this point the Tx operations should be waiting for the
// shard to initialize so
// trigger the latch to let the shard recovery to continue.
blockRecoveryLatch.countDown();
// Wait for the Tx commit to complete.
doCommit(txCohort.get());
// Verify the data in the store
final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
optional = readTx.read(listEntryPath).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", false, optional.isPresent());
}
}
};
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort in project controller by opendaylight.
the class DistributedDataStoreIntegrationTest method testTransactionChainWithMultipleShards.
@Test
public void testTransactionChainWithMultipleShards() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
assertNotNull("newWriteOnlyTransaction returned null", writeTx);
writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
readWriteTx.write(carPath, car);
final MapEntryNode person = PeopleModel.newPersonEntry("jack");
final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
readWriteTx.merge(personPath, person);
Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
assertEquals("Data node", car, optional.get());
optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
assertEquals("Data node", person, optional.get());
final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
writeTx = txChain.newWriteOnlyTransaction();
writeTx.delete(carPath);
final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
final ListenableFuture<Boolean> canCommit1 = cohort1.canCommit();
final ListenableFuture<Boolean> canCommit2 = cohort2.canCommit();
doCommit(canCommit1, cohort1);
doCommit(canCommit2, cohort2);
doCommit(cohort3);
txChain.close();
final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", false, optional.isPresent());
optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
assertEquals("Data node", person, optional.get());
}
}
};
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort in project controller by opendaylight.
the class DistributedDataStoreRemotingIntegrationTest method testLeadershipTransferOnShutdown.
@Test
public void testLeadershipTransferOnShutdown() throws Exception {
// TODO remove when test passes also for ClientBackedDataStore
Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
leaderDatastoreContextBuilder.shardBatchedModificationCount(1);
followerDatastoreContextBuilder.shardElectionTimeoutFactor(10).customRaftPolicyImplementation(null);
final String testName = "testLeadershipTransferOnShutdown";
initDatastores(testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, CARS_AND_PEOPLE);
final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System, DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).operationTimeoutInMillis(100), commitTimeout);
try (AbstractDataStore follower2DistributedDataStore = follower2TestKit.setupAbstractDataStore(testParameter, testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, false)) {
followerTestKit.waitForMembersUp("member-3");
follower2TestKit.waitForMembersUp("member-1", "member-2");
// Create and submit a couple tx's so they're pending.
DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars", stats -> assertEquals("getTxCohortCacheSize", 1, stats.getTxCohortCacheSize()));
writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
writeTx.write(CarsModel.newCarPath("optima"), car);
final DOMStoreThreePhaseCommitCohort cohort2 = writeTx.ready();
IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars", stats -> assertEquals("getTxCohortCacheSize", 2, stats.getTxCohortCacheSize()));
// Gracefully stop the leader via a Shutdown message.
sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder.shardElectionTimeoutFactor(100));
final FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
final Future<ActorRef> future = leaderDistributedDataStore.getActorContext().findLocalShardAsync("cars");
final ActorRef leaderActor = Await.result(future, duration);
final Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
// Commit the 2 transactions. They should finish and succeed.
followerTestKit.doCommit(cohort1);
followerTestKit.doCommit(cohort2);
// Wait for the leader actor stopped.
final Boolean stopped = Await.result(stopFuture, duration);
assertEquals("Stopped", Boolean.TRUE, stopped);
// Verify leadership was transferred by reading the committed data from the other nodes.
verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car);
verifyCars(follower2DistributedDataStore.newReadOnlyTransaction(), car);
}
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort in project controller by opendaylight.
the class TransactionProxyTest method testReadyWithLocalTransactionWithFailure.
@Test
public void testReadyWithLocalTransactionWithFailure() throws Exception {
ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
doReturn(getSystem().actorSelection(shardActorRef.path())).when(mockActorContext).actorSelection(shardActorRef.path().toString());
DataTree mockDataTree = createDataTree();
DataTreeModification mockModification = mockDataTree.takeSnapshot().newModification();
doThrow(new RuntimeException("mock")).when(mockModification).ready();
doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, mockDataTree))).when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
expectReadyLocalTransaction(shardActorRef, true);
NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
assertTrue(ready instanceof SingleCommitCohortProxy);
verifyCohortFutures((SingleCommitCohortProxy) ready, RuntimeException.class);
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort in project controller by opendaylight.
the class TransactionProxyTest method testReadyWithWriteOnlyAndLastBatchEmpty.
@Test
public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
expectBatchedModificationsReady(actorRef, true);
TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
assertTrue(ready instanceof SingleCommitCohortProxy);
verifyCohortFutures((SingleCommitCohortProxy) ready, new CommitTransactionReply().toSerializable());
List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(TestModel.TEST_PATH, nodeToWrite));
verifyBatchedModifications(batchedModifications.get(1), true, true);
}
Aggregations