Search in sources :

Example 6 with OptimisticLockFailedException

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

the class MDSALManager method removeFlowInternal.

public CheckedFuture<Void, TransactionCommitFailedException> removeFlowInternal(FlowEntity flowEntity) {
    WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
    deleteFlowEntityInternal(flowEntity, tx);
    CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
    Futures.addCallback(submitFuture, new FutureCallback<Void>() {

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

        @Override
        public void onFailure(final Throwable throwable) {
            // Transaction failed
            if (throwable instanceof OptimisticLockFailedException) {
                // Failed because of concurrent transaction modifying same
                // data
                LOG.error("Delete Flow -- Failed because of concurrent transaction modifying same data");
            } else {
                // Some other type of TransactionCommitFailedException
                LOG.error("Delete Flow -- Some other type of TransactionCommitFailedException", throwable);
            }
        }
    });
    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)

Example 7 with OptimisticLockFailedException

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

the class DataBrokerFailuresTest method testFailTwoReadWriteTransactionSubmit.

@Test
public void testFailTwoReadWriteTransactionSubmit() throws TransactionCommitFailedException {
    dbFailures.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
    checkSubmitFails();
    // Now make sure that it still fails again a 2nd time, and not just once:
    checkSubmitFails();
    // But now it should pass.. because we specified howManyTimes = 2 above
    dataBroker.newReadWriteTransaction().submit().checkedGet();
    dataBroker.newWriteOnlyTransaction().submit().checkedGet();
    dataBroker.newReadWriteTransaction().submit().checkedGet();
}
Also used : OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException) Test(org.junit.Test)

Example 8 with OptimisticLockFailedException

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

the class OpendaylightToaster method checkStatusAndMakeToast.

private void checkStatusAndMakeToast(final MakeToastInput input, final SettableFuture<RpcResult<Void>> futureResult, final int tries) {
    // Read the ToasterStatus and, if currently Up, try to write the status to Down.
    // If that succeeds, then we essentially have an exclusive lock and can proceed
    // to make toast.
    final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
    ListenableFuture<Optional<Toaster>> readFuture = tx.read(OPERATIONAL, TOASTER_IID);
    final ListenableFuture<Void> commitFuture = Futures.transformAsync(readFuture, toasterData -> {
        ToasterStatus toasterStatus = ToasterStatus.Up;
        if (toasterData.isPresent()) {
            toasterStatus = toasterData.get().getToasterStatus();
        }
        LOG.debug("Read toaster status: {}", toasterStatus);
        if (toasterStatus == ToasterStatus.Up) {
            if (outOfBread()) {
                LOG.debug("Toaster is out of bread");
                return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("", makeToasterOutOfBreadError()));
            }
            LOG.debug("Setting Toaster status to Down");
            // We're not currently making toast - try to update the status to Down
            // to indicate we're going to make toast. This acts as a lock to prevent
            // concurrent toasting.
            tx.put(OPERATIONAL, TOASTER_IID, buildToaster(ToasterStatus.Down));
            return tx.submit();
        }
        LOG.debug("Oops - already making toast!");
        // TransactionStatus in the RpcResult as an error condition.
        return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("", makeToasterInUseError()));
    });
    Futures.addCallback(commitFuture, new FutureCallback<Void>() {

        @Override
        public void onSuccess(final Void result) {
            // OK to make toast
            currentMakeToastTask.set(executor.submit(new MakeToastTask(input, futureResult)));
        }

        @Override
        public void onFailure(final Throwable ex) {
            if (ex instanceof OptimisticLockFailedException) {
                if (tries - 1 > 0) {
                    LOG.debug("Got OptimisticLockFailedException - trying again");
                    checkStatusAndMakeToast(input, futureResult, tries - 1);
                } else {
                    futureResult.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, ex.getMessage()).build());
                }
            } else if (ex instanceof TransactionCommitFailedException) {
                LOG.debug("Failed to commit Toaster status", ex);
                // Probably already making toast.
                futureResult.set(RpcResultBuilder.<Void>failed().withRpcErrors(((TransactionCommitFailedException) ex).getErrorList()).build());
            } else {
                LOG.debug("Unexpected error committing Toaster status", ex);
                futureResult.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, "Unexpected error committing Toaster status", ex).build());
            }
        }
    });
}
Also used : TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) Optional(com.google.common.base.Optional) ReadWriteTransaction(org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction) ToasterStatus(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster.ToasterStatus) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException)

Example 9 with OptimisticLockFailedException

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

the class RetryingManagedNewTransactionRunnerTest method testCallWithNewWriteOnlyTransactionOptimisticLockFailedException.

@Override
public void testCallWithNewWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
    // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
    // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
    testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
    managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
    singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH);
}
Also used : ManagedNewTransactionRunner(org.opendaylight.genius.infra.ManagedNewTransactionRunner) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException) RetryingManagedNewTransactionRunner(org.opendaylight.genius.infra.RetryingManagedNewTransactionRunner) OPERATIONAL(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException)

Example 10 with OptimisticLockFailedException

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

the class MDSALManager method removeGroupInternal.

protected CheckedFuture<Void, TransactionCommitFailedException> removeGroupInternal(BigInteger dpnId, long groupId) {
    WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
    removeGroupInternal(dpnId, groupId, tx);
    CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
    Futures.addCallback(submitFuture, new FutureCallback<Void>() {

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

        @Override
        public void onFailure(final Throwable throwable) {
            if (throwable instanceof OptimisticLockFailedException) {
                LOG.error("Install Group -- Failed because of concurrent transaction modifying same data");
            } else {
                LOG.error("Install Group -- Some other type of TransactionCommitFailedException", throwable);
            }
        }
    });
    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