use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction in project controller by opendaylight.
the class DistributedDataStoreRemotingIntegrationTest method testTransactionChainWithMultipleShards.
@Test
public void testTransactionChainWithMultipleShards() throws Exception {
initDatastoresWithCarsAndPeople("testTransactionChainWithMultipleShards");
final DOMStoreTransactionChain txChain = followerDistributedDataStore.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());
followerTestKit.doCommit(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(personPath);
final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
followerTestKit.doCommit(cohort2);
followerTestKit.doCommit(cohort3);
txChain.close();
final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
verifyCars(readTx, car);
optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", false, optional.isPresent());
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction in project controller by opendaylight.
the class DistributedDataStoreRemotingIntegrationTest method testReadWriteTransactionWithMultipleShards.
@Test
public void testReadWriteTransactionWithMultipleShards() throws Exception {
initDatastoresWithCarsAndPeople("testReadWriteTransactionWithMultipleShards");
final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
assertNotNull("newReadWriteTransaction returned null", rwTx);
final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
final NormalizedNode<?, ?> carsNode = CarsModel.emptyContainer();
rwTx.write(carsPath, carsNode);
final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
final NormalizedNode<?, ?> peopleNode = PeopleModel.emptyContainer();
rwTx.write(peoplePath, peopleNode);
followerTestKit.doCommit(rwTx.ready());
final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
verifyNode(readTx, carsPath, carsNode);
verifyNode(readTx, peoplePath, peopleNode);
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction in project controller by opendaylight.
the class DistributedDataStoreRemotingIntegrationTest method testWriteTransactionWithMultipleShards.
@Test
public void testWriteTransactionWithMultipleShards() throws Exception {
initDatastoresWithCarsAndPeople("testWriteTransactionWithMultipleShards");
final DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
assertNotNull("newWriteOnlyTransaction returned null", writeTx);
final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
final NormalizedNode<?, ?> carsNode = CarsModel.emptyContainer();
writeTx.write(carsPath, carsNode);
final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
final NormalizedNode<?, ?> peopleNode = PeopleModel.emptyContainer();
writeTx.write(peoplePath, peopleNode);
followerTestKit.doCommit(writeTx.ready());
final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
verifyNode(readTx, carsPath, carsNode);
verifyNode(readTx, peoplePath, peopleNode);
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction in project controller by opendaylight.
the class DistributedDataStoreRemotingIntegrationTest method testTransactionForwardedToLeaderAfterRetry.
@Test
public void testTransactionForwardedToLeaderAfterRetry() throws Exception {
// TODO remove when test passes also for ClientBackedDataStore
Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
followerDatastoreContextBuilder.shardBatchedModificationCount(2);
leaderDatastoreContextBuilder.shardBatchedModificationCount(2);
initDatastoresWithCarsAndPeople("testTransactionForwardedToLeaderAfterRetry");
// Do an initial write to get the primary shard info cached.
final DOMStoreWriteTransaction initialWriteTx = followerDistributedDataStore.newWriteOnlyTransaction();
initialWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
initialWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
followerTestKit.doCommit(initialWriteTx.ready());
// Wait for the commit to be replicated to the follower.
MemberNode.verifyRaftState(followerDistributedDataStore, "cars", raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
MemberNode.verifyRaftState(followerDistributedDataStore, "people", raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
// Prepare, ready and canCommit a WO tx that writes to 2 shards. This will become the current tx in
// the leader shard.
final DOMStoreWriteTransaction writeTx1 = followerDistributedDataStore.newWriteOnlyTransaction();
writeTx1.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
writeTx1.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
final DOMStoreThreePhaseCommitCohort writeTx1Cohort = writeTx1.ready();
final ListenableFuture<Boolean> writeTx1CanCommit = writeTx1Cohort.canCommit();
writeTx1CanCommit.get(5, TimeUnit.SECONDS);
// Prepare and ready another WO tx that writes to 2 shards but don't canCommit yet. This will be queued
// in the leader shard.
final DOMStoreWriteTransaction writeTx2 = followerDistributedDataStore.newWriteOnlyTransaction();
final LinkedList<MapEntryNode> cars = new LinkedList<>();
int carIndex = 1;
cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
writeTx2.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
carIndex++;
NormalizedNode<?, ?> people = PeopleModel.newPersonMapNode();
writeTx2.write(PeopleModel.PERSON_LIST_PATH, people);
final DOMStoreThreePhaseCommitCohort writeTx2Cohort = writeTx2.ready();
// Prepare another WO that writes to a single shard and thus will be directly committed on ready. This
// tx writes 5 cars so 2 BatchedModidifications messages will be sent initially and cached in the
// leader shard (with shardBatchedModificationCount set to 2). The 3rd BatchedModidifications will be
// sent on ready.
final DOMStoreWriteTransaction writeTx3 = followerDistributedDataStore.newWriteOnlyTransaction();
for (int i = 1; i <= 5; i++, carIndex++) {
cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
writeTx3.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
}
// Prepare another WO that writes to a single shard. This will send a single BatchedModidifications
// message on ready.
final DOMStoreWriteTransaction writeTx4 = followerDistributedDataStore.newWriteOnlyTransaction();
cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
writeTx4.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
carIndex++;
// Prepare a RW tx that will create a tx actor and send a ForwardedReadyTransaciton message to the
// leader shard on ready.
final DOMStoreReadWriteTransaction readWriteTx = followerDistributedDataStore.newReadWriteTransaction();
cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
readWriteTx.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars", stats -> assertEquals("getReadWriteTransactionCount", 1, stats.getReadWriteTransactionCount()));
// Disable elections on the leader so it switches to follower.
sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder.customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName()).shardElectionTimeoutFactor(10));
leaderTestKit.waitUntilNoLeader(leaderDistributedDataStore.getActorContext(), "cars");
// Submit all tx's - the messages should get queued for retry.
final ListenableFuture<Boolean> writeTx2CanCommit = writeTx2Cohort.canCommit();
final DOMStoreThreePhaseCommitCohort writeTx3Cohort = writeTx3.ready();
final DOMStoreThreePhaseCommitCohort writeTx4Cohort = writeTx4.ready();
final DOMStoreThreePhaseCommitCohort rwTxCohort = readWriteTx.ready();
// Enable elections on the other follower so it becomes the leader, at which point the
// tx's should get forwarded from the previous leader to the new leader to complete the commits.
sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder.customRaftPolicyImplementation(null).shardElectionTimeoutFactor(1));
IntegrationTestKit.findLocalShard(followerDistributedDataStore.getActorContext(), "cars").tell(TimeoutNow.INSTANCE, ActorRef.noSender());
IntegrationTestKit.findLocalShard(followerDistributedDataStore.getActorContext(), "people").tell(TimeoutNow.INSTANCE, ActorRef.noSender());
followerTestKit.doCommit(writeTx1CanCommit, writeTx1Cohort);
followerTestKit.doCommit(writeTx2CanCommit, writeTx2Cohort);
followerTestKit.doCommit(writeTx3Cohort);
followerTestKit.doCommit(writeTx4Cohort);
followerTestKit.doCommit(rwTxCohort);
DOMStoreReadTransaction readTx = leaderDistributedDataStore.newReadOnlyTransaction();
verifyCars(readTx, cars.toArray(new MapEntryNode[cars.size()]));
verifyNode(readTx, PeopleModel.PERSON_LIST_PATH, people);
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction in project controller by opendaylight.
the class NormalizedNodeAggregatorTest method getRootNode.
public static NormalizedNode<?, ?> getRootNode(NormalizedNode<?, ?> moduleNode, SchemaContext schemaContext) throws ReadFailedException, ExecutionException, InterruptedException {
try (InMemoryDOMDataStore store = new InMemoryDOMDataStore("test", Executors.newSingleThreadExecutor())) {
store.onGlobalContextUpdated(schemaContext);
DOMStoreWriteTransaction writeTransaction = store.newWriteOnlyTransaction();
writeTransaction.merge(YangInstanceIdentifier.of(moduleNode.getNodeType()), moduleNode);
DOMStoreThreePhaseCommitCohort ready = writeTransaction.ready();
ready.canCommit().get();
ready.preCommit().get();
ready.commit().get();
DOMStoreReadTransaction readTransaction = store.newReadOnlyTransaction();
CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = readTransaction.read(YangInstanceIdentifier.EMPTY);
Optional<NormalizedNode<?, ?>> nodeOptional = read.checkedGet();
return nodeOptional.get();
}
}
Aggregations