Search in sources :

Example 1 with OptimisticLockFailedException

use of org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException in project controller by opendaylight.

the class LegacyDOMDataBrokerAdapterTest method testWriteOnlyTransaction.

@Test
public void testWriteOnlyTransaction() throws Exception {
    // Test successful write operations and submit
    DOMDataWriteTransaction tx = adapter.newWriteOnlyTransaction();
    tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
    verify(mockWriteTx).write(TestModel.TEST_PATH, dataNode);
    tx.merge(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
    verify(mockWriteTx).merge(TestModel.TEST_PATH, dataNode);
    tx.delete(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
    verify(mockWriteTx).delete(TestModel.TEST_PATH);
    CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
    submitFuture.get(5, TimeUnit.SECONDS);
    InOrder inOrder = inOrder(mockCommitCohort);
    inOrder.verify(mockCommitCohort).canCommit();
    inOrder.verify(mockCommitCohort).preCommit();
    inOrder.verify(mockCommitCohort).commit();
    // Test cancel
    tx = adapter.newWriteOnlyTransaction();
    tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
    tx.cancel();
    verify(mockWriteTx).close();
    // Test submit with OptimisticLockFailedException
    String errorMsg = "mock OptimisticLockFailedException";
    Throwable cause = new ConflictingModificationAppliedException(TestModel.TEST_PATH, "mock");
    doReturn(Futures.immediateFailedFuture(new org.opendaylight.mdsal.common.api.OptimisticLockFailedException(errorMsg, cause))).when(mockCommitCohort).canCommit();
    try {
        tx = adapter.newWriteOnlyTransaction();
        tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
        submitFuture = tx.submit();
        submitFuture.checkedGet(5, TimeUnit.SECONDS);
        fail("Expected OptimisticLockFailedException");
    } catch (OptimisticLockFailedException e) {
        assertEquals("getMessage", errorMsg, e.getMessage());
        assertEquals("getCause", cause, e.getCause());
    }
    // Test submit with TransactionCommitFailedException
    errorMsg = "mock TransactionCommitFailedException";
    cause = new DataValidationFailedException(TestModel.TEST_PATH, "mock");
    doReturn(Futures.immediateFailedFuture(new org.opendaylight.mdsal.common.api.TransactionCommitFailedException(errorMsg, cause))).when(mockCommitCohort).canCommit();
    try {
        tx = adapter.newWriteOnlyTransaction();
        tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
        submitFuture = tx.submit();
        submitFuture.checkedGet(5, TimeUnit.SECONDS);
        fail("Expected TransactionCommitFailedException");
    } catch (TransactionCommitFailedException e) {
        assertEquals("getMessage", errorMsg, e.getMessage());
        assertEquals("getCause", cause, e.getCause());
    }
    // Test submit with DataStoreUnavailableException
    errorMsg = "mock NoShardLeaderException";
    cause = new NoShardLeaderException("mock");
    doReturn(Futures.immediateFailedFuture(cause)).when(mockCommitCohort).canCommit();
    try {
        tx = adapter.newWriteOnlyTransaction();
        tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
        submitFuture = tx.submit();
        submitFuture.checkedGet(5, TimeUnit.SECONDS);
        fail("Expected TransactionCommitFailedException");
    } catch (TransactionCommitFailedException e) {
        assertEquals("getCause type", DataStoreUnavailableException.class, e.getCause().getClass());
        assertEquals("Root cause", cause, e.getCause().getCause());
    }
    // Test submit with RuntimeException
    errorMsg = "mock RuntimeException";
    cause = new RuntimeException(errorMsg);
    doReturn(Futures.immediateFailedFuture(cause)).when(mockCommitCohort).canCommit();
    try {
        tx = adapter.newWriteOnlyTransaction();
        tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
        submitFuture = tx.submit();
        submitFuture.checkedGet(5, TimeUnit.SECONDS);
        fail("Expected TransactionCommitFailedException");
    } catch (TransactionCommitFailedException e) {
        assertEquals("getCause", cause, e.getCause());
    }
}
Also used : DataValidationFailedException(org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException) InOrder(org.mockito.InOrder) DataStoreUnavailableException(org.opendaylight.controller.md.sal.common.api.data.DataStoreUnavailableException) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) ConflictingModificationAppliedException(org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException) DOMDataWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException) Test(org.junit.Test)

Example 2 with OptimisticLockFailedException

use of org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException in project controller by opendaylight.

the class WriteTransactionsHandler method start.

public static ListenableFuture<RpcResult<WriteTransactionsOutput>> start(final DOMDataBroker domDataBroker, final WriteTransactionsInput input) {
    LOG.debug("Starting write-transactions.");
    final String id = input.getId();
    final MapEntryNode entry = ImmutableNodes.mapEntryBuilder(ID_INT, ID, id).withChild(ImmutableNodes.mapNodeBuilder(ITEM).build()).build();
    final YangInstanceIdentifier idListItem = ID_INT_YID.node(entry.getIdentifier());
    final ContainerNode containerNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(ID_INTS)).withChild(ImmutableNodes.mapNodeBuilder(ID_INT).build()).build();
    DOMDataWriteTransaction tx = domDataBroker.newWriteOnlyTransaction();
    // write only the top list
    tx.merge(LogicalDatastoreType.CONFIGURATION, ID_INTS_YID, containerNode);
    try {
        tx.submit().checkedGet(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    } catch (final OptimisticLockFailedException e) {
        // when multiple write-transactions are executed concurrently we need to ignore this.
        // If we get optimistic lock here it means id-ints already exists and we can continue.
        LOG.debug("Got an optimistic lock when writing initial top level list element.", e);
    } catch (final TransactionCommitFailedException | TimeoutException e) {
        LOG.warn("Unable to ensure IdInts list for id: {} exists.", id, e);
        return Futures.immediateFuture(RpcResultBuilder.<WriteTransactionsOutput>failed().withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
    }
    tx = domDataBroker.newWriteOnlyTransaction();
    tx.merge(LogicalDatastoreType.CONFIGURATION, idListItem, entry);
    try {
        tx.submit().get(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        LOG.warn("Unable to ensure IdInts list for id: {} exists.", id, e);
        return Futures.immediateFuture(RpcResultBuilder.<WriteTransactionsOutput>failed().withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
    }
    LOG.debug("Filling the item list with initial values.");
    final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = ImmutableNodes.mapNodeBuilder(ITEM);
    final YangInstanceIdentifier itemListId = idListItem.node(ITEM);
    tx = domDataBroker.newWriteOnlyTransaction();
    tx.put(LogicalDatastoreType.CONFIGURATION, itemListId, mapBuilder.build());
    try {
        tx.submit().get(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        LOG.warn("Unable to fill the initial item list.", e);
        return Futures.immediateFuture(RpcResultBuilder.<WriteTransactionsOutput>failed().withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
    }
    final WriteTransactionsHandler handler;
    if (input.isChainedTransactions()) {
        handler = new Chained(domDataBroker, idListItem, input);
    } else {
        handler = new Simple(domDataBroker, idListItem, input);
    }
    handler.doStart();
    return handler.completionFuture;
}
Also used : MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) WriteTransactionsOutput(org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.WriteTransactionsOutput) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) ExecutionException(java.util.concurrent.ExecutionException) DOMDataWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with OptimisticLockFailedException

use of org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException in project genius by opendaylight.

the class LockManagerTest method test3sOptimisticLockFailedExceptionOnLock.

@Test
public void test3sOptimisticLockFailedExceptionOnLock() throws InterruptedException, ExecutionException, TimeoutException {
    dbFailureSimulator.failSubmits(new OptimisticLockFailedException("bada boum bam!"));
    LockInput lockInput = new LockInputBuilder().setLockName("testLock").build();
    // see other tests above
    runUnfailSubmitsTimerTask(3000);
    assertVoidRpcSuccess(lockManager.lock(lockInput));
}
Also used : TryLockInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInput) LockInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockInput) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException) TryLockInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInputBuilder) LockInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockInputBuilder) AbstractConcurrentDataBrokerTest(org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest) Test(org.junit.Test)

Example 4 with OptimisticLockFailedException

use of org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException in project genius by opendaylight.

the class ManagedNewTransactionRunnerImplTest method testCallWithNewWriteOnlyTransactionOptimisticLockFailedException.

@Test
public void testCallWithNewWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
    try {
        testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
        managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
        fail("This should have lead to an ExecutionException!");
    } catch (ExecutionException e) {
        assertThat(e.getCause() instanceof OptimisticLockFailedException).isTrue();
    }
    assertThat(singleTransactionDataBroker.syncReadOptional(OPERATIONAL, TEST_PATH)).isAbsent();
}
Also used : SingleTransactionDataBroker(org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker) ListsBindingUtils.topLevelList(org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList) ContainerWithUsesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder) TopLevelList(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList) AsyncWriteTransaction(org.opendaylight.controller.md.sal.common.api.data.AsyncWriteTransaction) OPERATIONAL(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL) ManagedNewTransactionRunnerImpl(org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) TreeComplexUsesAugmentBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugmentBuilder) TOP_FOO_KEY(org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY) ManagedNewTransactionRunner(org.opendaylight.genius.infra.ManagedNewTransactionRunner) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) IOException(java.io.IOException) Test(org.junit.Test) TreeComplexUsesAugment(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment) Truth.assertThat(com.google.common.truth.Truth.assertThat) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) DataBrokerFailuresImpl(org.opendaylight.genius.datastoreutils.testutils.DataBrokerFailuresImpl) ListsBindingUtils.path(org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path) DataBrokerTestModule(org.opendaylight.controller.md.sal.binding.test.DataBrokerTestModule) LogRule(org.opendaylight.infrautils.testutils.LogRule) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) ExecutionException(java.util.concurrent.ExecutionException) Rule(org.junit.Rule) LogCaptureRule(org.opendaylight.infrautils.testutils.LogCaptureRule) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException) ExecutionException(java.util.concurrent.ExecutionException) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException) Test(org.junit.Test)

Example 5 with OptimisticLockFailedException

use of org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException in project genius by opendaylight.

the class MDSALManager method installGroupInternal.

public CheckedFuture<Void, TransactionCommitFailedException> installGroupInternal(GroupEntity groupEntity) {
    WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
    writeGroupEntityInternal(groupEntity, tx);
    CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
    Futures.addCallback(submitFuture, new FutureCallback<Void>() {

        @Override
        public void onSuccess(final Void result) {
            // Committed successfully
            LOG.debug("Install Group -- Committedsuccessfully ");
        }

        @Override
        public void onFailure(final Throwable throwable) {
            if (throwable instanceof OptimisticLockFailedException) {
                // Failed because of concurrent transaction modifying same
                // data
                LOG.error("Install Group -- Failed because of concurrent transaction modifying same data");
            } else {
                // Some other type of TransactionCommitFailedException
                LOG.error("Install Group -- Some other type of TransactionCommitFailedException", throwable);
            }
        }
    }, MoreExecutors.directExecutor());
    return submitFuture;
}
Also used : WriteTransaction(org.opendaylight.controller.md.sal.binding.api.WriteTransaction) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException)

Aggregations

OptimisticLockFailedException (org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException)14 TransactionCommitFailedException (org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException)8 Test (org.junit.Test)7 WriteTransaction (org.opendaylight.controller.md.sal.binding.api.WriteTransaction)4 ExecutionException (java.util.concurrent.ExecutionException)2 DataBroker (org.opendaylight.controller.md.sal.binding.api.DataBroker)2 LogicalDatastoreType (org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType)2 OPERATIONAL (org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL)2 DOMDataWriteTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)2 ManagedNewTransactionRunner (org.opendaylight.genius.infra.ManagedNewTransactionRunner)2 Optional (com.google.common.base.Optional)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Assert.fail (org.junit.Assert.fail)1 Before (org.junit.Before)1 Rule (org.junit.Rule)1 InOrder (org.mockito.InOrder)1 NoShardLeaderException (org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException)1 ReadWriteTransaction (org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction)1