Search in sources :

Example 11 with DOMStoreWriteTransaction

use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction 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);
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreReadTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 12 with DOMStoreWriteTransaction

use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction in project controller by opendaylight.

the class DistributedDataStoreRemotingIntegrationTest method testTransactionChainWithSingleShard.

@Test
public void testTransactionChainWithSingleShard() throws Exception {
    initDatastoresWithCars("testTransactionChainWithSingleShard");
    final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
    // Add the top-level cars container with write-only.
    final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
    assertNotNull("newWriteOnlyTransaction returned null", writeTx);
    writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
    final DOMStoreThreePhaseCommitCohort writeTxReady = writeTx.ready();
    // Verify the top-level cars container with read-only.
    verifyNode(txChain.newReadOnlyTransaction(), CarsModel.BASE_PATH, CarsModel.emptyContainer());
    // Perform car operations with read-write.
    final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
    verifyNode(rwTx, CarsModel.BASE_PATH, CarsModel.emptyContainer());
    rwTx.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
    final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
    final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
    rwTx.write(car1Path, car1);
    verifyExists(rwTx, car1Path);
    verifyCars(rwTx, car1);
    final MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
    rwTx.merge(CarsModel.newCarPath("sportage"), car2);
    rwTx.delete(car1Path);
    followerTestKit.doCommit(writeTxReady);
    followerTestKit.doCommit(rwTx.ready());
    txChain.close();
    verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreTransactionChain(org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain) DOMStoreReadWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) Test(org.junit.Test)

Example 13 with DOMStoreWriteTransaction

use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction 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);
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreReadTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction) DOMStoreReadWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) LinkedList(java.util.LinkedList) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) Test(org.junit.Test)

Example 14 with DOMStoreWriteTransaction

use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction in project controller by opendaylight.

the class DataTreeCohortIntegrationTest method testCanCommitWithListEntries.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testCanCommitWithListEntries() throws Exception {
    final DOMDataTreeCommitCohort cohort = mock(DOMDataTreeCommitCohort.class);
    doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
    IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
    try (AbstractDataStore dataStore = kit.setupAbstractDataStore(DistributedDataStore.class, "testCanCommitWithMultipleListEntries", "cars-1")) {
        final ObjectRegistration<DOMDataTreeCommitCohort> cohortReg = dataStore.registerCommitCohort(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH.node(CarsModel.CAR_QNAME)), cohort);
        assertNotNull(cohortReg);
        IntegrationTestKit.verifyShardState(dataStore, "cars-1", state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
        // First write an empty base container and verify the cohort isn't invoked.
        DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
        writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
        writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
        kit.doCommit(writeTx.ready());
        verifyNoMoreInteractions(cohort);
        // Write a single car entry and verify the cohort is invoked.
        writeTx = dataStore.newWriteOnlyTransaction();
        final YangInstanceIdentifier optimaPath = CarsModel.newCarPath("optima");
        final MapEntryNode optimaNode = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
        writeTx.write(optimaPath, optimaNode);
        kit.doCommit(writeTx.ready());
        ArgumentCaptor<Collection> candidateCapture = ArgumentCaptor.forClass(Collection.class);
        verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
        assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapture.getValue().iterator().next(), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.WRITE, Optional.of(optimaNode), Optional.absent());
        // Write replace the cars container with 2 new car entries. The cohort should get invoked with 3
        // DOMDataTreeCandidates: once for each of the 2 new car entries (WRITE mod) and once for the deleted prior
        // car entry (DELETE mod).
        reset(cohort);
        doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
        writeTx = dataStore.newWriteOnlyTransaction();
        final YangInstanceIdentifier sportagePath = CarsModel.newCarPath("sportage");
        final MapEntryNode sportageNode = CarsModel.newCarEntry("sportage", BigInteger.valueOf(20000));
        final YangInstanceIdentifier soulPath = CarsModel.newCarPath("soul");
        final MapEntryNode soulNode = CarsModel.newCarEntry("soul", BigInteger.valueOf(20000));
        writeTx.write(CarsModel.BASE_PATH, CarsModel.newCarsNode(CarsModel.newCarsMapNode(sportageNode, soulNode)));
        kit.doCommit(writeTx.ready());
        candidateCapture = ArgumentCaptor.forClass(Collection.class);
        verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
        assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.WRITE, Optional.of(sportageNode), Optional.absent());
        assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.WRITE, Optional.of(soulNode), Optional.absent());
        assertDataTreeCandidate(findCandidate(candidateCapture, optimaPath), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.DELETE, Optional.absent(), Optional.of(optimaNode));
        // Delete the cars container - cohort should be invoked for the 2 deleted car entries.
        reset(cohort);
        doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
        writeTx = dataStore.newWriteOnlyTransaction();
        writeTx.delete(CarsModel.BASE_PATH);
        kit.doCommit(writeTx.ready());
        candidateCapture = ArgumentCaptor.forClass(Collection.class);
        verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
        assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.DELETE, Optional.absent(), Optional.of(sportageNode));
        assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.DELETE, Optional.absent(), Optional.of(soulNode));
    }
}
Also used : DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) Collection(java.util.Collection) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) DOMDataTreeCommitCohort(org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 15 with DOMStoreWriteTransaction

use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction in project controller by opendaylight.

the class DataTreeCohortIntegrationTest method testFailedCanCommit.

@SuppressWarnings("unchecked")
@Test
public void testFailedCanCommit() throws Exception {
    final DOMDataTreeCommitCohort failedCohort = mock(DOMDataTreeCommitCohort.class);
    doReturn(FAILED_CAN_COMMIT_FUTURE).when(failedCohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
    IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
    try (AbstractDataStore dataStore = kit.setupAbstractDataStore(DistributedDataStore.class, "testFailedCanCommit", "test-1")) {
        dataStore.registerCommitCohort(TEST_ID, failedCohort);
        IntegrationTestKit.verifyShardState(dataStore, "test-1", state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
        DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
        writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
        DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
        try {
            dsCohort.canCommit().get(5, TimeUnit.SECONDS);
            fail("Exception should be raised.");
        } catch (ExecutionException e) {
            assertSame(FAILED_CAN_COMMIT, Throwables.getRootCause(e));
        }
    }
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) Collection(java.util.Collection) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) ExecutionException(java.util.concurrent.ExecutionException) DOMDataTreeCommitCohort(org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) Test(org.junit.Test)

Aggregations

DOMStoreWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction)30 Test (org.junit.Test)25 DOMStoreThreePhaseCommitCohort (org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)17 DOMStoreReadTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction)9 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)9 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)9 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)9 AddressFromURIString (akka.actor.AddressFromURIString)7 DOMStoreTransactionChain (org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExecutionException (java.util.concurrent.ExecutionException)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ReadFailedException (org.opendaylight.mdsal.common.api.ReadFailedException)6 NoShardLeaderException (org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException)5 DOMStoreReadWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction)5 ActorRef (akka.actor.ActorRef)4 IOException (java.io.IOException)4 RequestTimeoutException (org.opendaylight.controller.cluster.access.client.RequestTimeoutException)4 NotInitializedException (org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException)4 TransactionChainClosedException (org.opendaylight.mdsal.common.api.TransactionChainClosedException)4