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());
}
}
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;
}
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));
}
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();
}
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;
}
Aggregations