use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction in project controller by opendaylight.
the class ConcurrentDOMDataBrokerTest method testLazySubTransactionCreationForWriteOnlyTransactions.
@Test
public void testLazySubTransactionCreationForWriteOnlyTransactions() {
DOMStore configDomStore = mock(DOMStore.class);
DOMStore operationalDomStore = mock(DOMStore.class);
DOMStoreWriteTransaction storeTxn = mock(DOMStoreWriteTransaction.class);
doReturn(storeTxn).when(operationalDomStore).newWriteOnlyTransaction();
doReturn(storeTxn).when(configDomStore).newWriteOnlyTransaction();
try (ConcurrentDOMDataBroker dataBroker = new ConcurrentDOMDataBroker(ImmutableMap.of(LogicalDatastoreType.OPERATIONAL, operationalDomStore, LogicalDatastoreType.CONFIGURATION, configDomStore), futureExecutor)) {
DOMDataTreeWriteTransaction dataTxn = dataBroker.newWriteOnlyTransaction();
dataTxn.put(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY, mock(NormalizedNode.class));
dataTxn.put(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY, mock(NormalizedNode.class));
verify(configDomStore, never()).newWriteOnlyTransaction();
verify(operationalDomStore, times(1)).newWriteOnlyTransaction();
dataTxn.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY, mock(NormalizedNode.class));
verify(configDomStore, times(1)).newWriteOnlyTransaction();
verify(operationalDomStore, times(1)).newWriteOnlyTransaction();
}
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction in project controller by opendaylight.
the class DistributedDataStoreIntegrationTest method testTransactionAbort.
@Test
public void testTransactionAbort() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "transactionAbortIntegrationTest", "test-1")) {
final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
assertNotNull("newWriteOnlyTransaction returned null", writeTx);
writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
cohort.canCommit().get(5, TimeUnit.SECONDS);
cohort.abort().get(5, TimeUnit.SECONDS);
testWriteTransaction(dataStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
}
}
};
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction in project controller by opendaylight.
the class DistributedDataStoreIntegrationTest method testTransactionChainWithSingleShard.
@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testTransactionChainWithSingleShard() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testTransactionChainWithSingleShard", "test-1")) {
// 1. Create a Tx chain and write-only Tx
final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
assertNotNull("newWriteOnlyTransaction returned null", writeTx);
// 2. Write some data
final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
writeTx.write(TestModel.TEST_PATH, testNode);
// 3. Ready the Tx for commit
final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
// 4. Commit the Tx on another thread that first waits for
// the second read Tx.
final CountDownLatch continueCommit1 = new CountDownLatch(1);
final CountDownLatch commit1Done = new CountDownLatch(1);
final AtomicReference<Exception> commit1Error = new AtomicReference<>();
new Thread(() -> {
try {
continueCommit1.await();
doCommit(cohort1);
} catch (Exception e) {
commit1Error.set(e);
} finally {
commit1Done.countDown();
}
}).start();
// 5. Create a new read Tx from the chain to read and verify
// the data from the first
// Tx is visible after being readied.
DOMStoreReadTransaction readTx = txChain.newReadOnlyTransaction();
Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
assertEquals("Data node", testNode, optional.get());
// 6. Create a new RW Tx from the chain, write more data,
// and ready it
final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
final MapNode outerNode = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
rwTx.write(TestModel.OUTER_LIST_PATH, outerNode);
final DOMStoreThreePhaseCommitCohort cohort2 = rwTx.ready();
// 7. Create a new read Tx from the chain to read the data
// from the last RW Tx to
// verify it is visible.
readTx = txChain.newReadWriteTransaction();
optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
assertEquals("Data node", outerNode, optional.get());
// 8. Wait for the 2 commits to complete and close the
// chain.
continueCommit1.countDown();
Uninterruptibles.awaitUninterruptibly(commit1Done, 5, TimeUnit.SECONDS);
if (commit1Error.get() != null) {
throw commit1Error.get();
}
doCommit(cohort2);
txChain.close();
// 9. Create a new read Tx from the data store and verify
// committed data.
readTx = dataStore.newReadOnlyTransaction();
optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
assertEquals("Data node", outerNode, optional.get());
}
}
};
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction in project controller by opendaylight.
the class DistributedDataStoreIntegrationTest method testWriteTransactionWithMultipleShards.
@Test
public void testWriteTransactionWithMultipleShards() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
assertNotNull("newWriteOnlyTransaction returned null", writeTx);
writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
doCommit(writeTx.ready());
writeTx = dataStore.newWriteOnlyTransaction();
writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
doCommit(writeTx.ready());
writeTx = dataStore.newWriteOnlyTransaction();
final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
writeTx.write(carPath, car);
final MapEntryNode person = PeopleModel.newPersonEntry("jack");
final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
writeTx.write(personPath, person);
doCommit(writeTx.ready());
// Verify the data in the store
final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
Optional<NormalizedNode<?, ?>> optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
assertEquals("Data node", car, optional.get());
optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
assertEquals("Data node", person, optional.get());
}
}
};
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction in project controller by opendaylight.
the class DistributedDataStoreIntegrationTest method testTransactionCommitFailureWithNoShardLeader.
@SuppressWarnings("checkstyle:IllegalCatch")
private void testTransactionCommitFailureWithNoShardLeader(final boolean writeOnly, final String testName) throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
final String shardName = "default";
// We don't want the shard to become the leader so prevent shard
// elections.
datastoreContextBuilder.customRaftPolicyImplementation("org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy");
// The ShardManager uses the election timeout for FindPrimary so
// reset it low so it will timeout quickly.
datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1).shardInitializationTimeout(200, TimeUnit.MILLISECONDS).frontendRequestTimeoutInSeconds(2);
try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, testName, false, shardName)) {
final Object result = dataStore.getActorContext().executeOperation(dataStore.getActorContext().getShardManager(), new FindLocalShard(shardName, true));
assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
// Create the write Tx.
DOMStoreWriteTransaction writeTxToClose = null;
try {
writeTxToClose = writeOnly ? dataStore.newWriteOnlyTransaction() : dataStore.newReadWriteTransaction();
final DOMStoreWriteTransaction writeTx = writeTxToClose;
assertNotNull("newReadWriteTransaction returned null", writeTx);
// Do some modifications and ready the Tx on a separate
// thread.
final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
final AtomicReference<Exception> caughtEx = new AtomicReference<>();
final CountDownLatch txReady = new CountDownLatch(1);
final Thread txThread = new Thread(() -> {
try {
writeTx.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
txCohort.set(writeTx.ready());
} catch (Exception e) {
caughtEx.set(e);
} finally {
txReady.countDown();
}
});
txThread.start();
// Wait for the Tx operations to complete.
boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
if (caughtEx.get() != null) {
throw caughtEx.get();
}
assertEquals("Tx ready", true, done);
// exception cause.
try {
txCohort.get().canCommit().get(10, TimeUnit.SECONDS);
fail("Expected NoShardLeaderException");
} catch (final ExecutionException e) {
final String msg = "Unexpected exception: " + Throwables.getStackTraceAsString(e.getCause());
if (DistributedDataStore.class.equals(testParameter)) {
assertTrue(Throwables.getRootCause(e) instanceof NoShardLeaderException);
} else {
assertTrue(msg, Throwables.getRootCause(e) instanceof RequestTimeoutException);
}
}
} finally {
try {
if (writeTxToClose != null) {
writeTxToClose.close();
}
} catch (Exception e) {
// FIXME TransactionProxy.close throws IllegalStateException:
// Transaction is ready, it cannot be closed
}
}
}
}
};
}
Aggregations