use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction in project controller by opendaylight.
the class ClientBackedDataStoreTest method testNewWriteOnlyTransaction.
@Test
public void testNewWriteOnlyTransaction() throws Exception {
try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(actorContext, UNKNOWN_ID, clientActor)) {
final DOMStoreWriteTransaction tx = clientBackedDataStore.newWriteOnlyTransaction();
Assert.assertNotNull(tx);
Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
}
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction in project controller by opendaylight.
the class DistributedDataStoreIntegrationTest method testChainWithReadOnlyTxAfterPreviousReady.
@Test
public void testChainWithReadOnlyTxAfterPreviousReady() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
// Create a write tx and submit.
final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
// Create read-only tx's and issue a read.
CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture1 = txChain.newReadOnlyTransaction().read(TestModel.TEST_PATH);
CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture2 = txChain.newReadOnlyTransaction().read(TestModel.TEST_PATH);
// Create another write tx and issue the write.
DOMStoreWriteTransaction writeTx2 = txChain.newWriteOnlyTransaction();
writeTx2.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
// Ensure the reads succeed.
assertEquals("isPresent", true, readFuture1.checkedGet(5, TimeUnit.SECONDS).isPresent());
assertEquals("isPresent", true, readFuture2.checkedGet(5, TimeUnit.SECONDS).isPresent());
// Ensure the writes succeed.
DOMStoreThreePhaseCommitCohort cohort2 = writeTx2.ready();
doCommit(cohort1);
doCommit(cohort2);
assertEquals("isPresent", true, txChain.newReadOnlyTransaction().read(TestModel.OUTER_LIST_PATH).checkedGet(5, TimeUnit.SECONDS).isPresent());
}
}
};
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction 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.DOMStoreWriteTransaction 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.DOMStoreWriteTransaction in project controller by opendaylight.
the class DistributedDataStoreRemotingIntegrationTest method testSingleShardTransactionsWithLeaderChanges.
@Test
public void testSingleShardTransactionsWithLeaderChanges() throws Exception {
followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
final String testName = "testSingleShardTransactionsWithLeaderChanges";
initDatastoresWithCars(testName);
final String followerCarShardName = "member-2-shard-cars-" + testName;
InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 1, ApplyJournalEntries.class);
// Write top-level car container from the follower so it uses a remote Tx.
DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
followerTestKit.doCommit(writeTx.ready());
InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
// Switch the leader to the follower
sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder.shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
TestKit.shutdownActorSystem(leaderSystem, true);
Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
followerTestKit.waitUntilNoLeader(followerDistributedDataStore.getActorContext(), CARS);
leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
Cluster.get(leaderSystem).join(MEMBER_2_ADDRESS);
final DatastoreContext.Builder newMember1Builder = DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
IntegrationTestKit newMember1TestKit = new IntegrationTestKit(leaderSystem, newMember1Builder, commitTimeout);
try (AbstractDataStore ds = newMember1TestKit.setupAbstractDataStore(testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS)) {
followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), CARS);
// Write a car entry to the new leader - should switch to local Tx
writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
writeTx.merge(car1Path, car1);
followerTestKit.doCommit(writeTx.ready());
verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1);
}
}
Aggregations