use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction in project controller by opendaylight.
the class ClientBackedDataStoreTest method testNewReadWriteTransaction.
@Test
public void testNewReadWriteTransaction() throws Exception {
try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(actorContext, UNKNOWN_ID, clientActor)) {
final DOMStoreReadWriteTransaction tx = clientBackedDataStore.newReadWriteTransaction();
Assert.assertNotNull(tx);
Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
}
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction in project controller by opendaylight.
the class ConcurrentDOMDataBrokerTest method testSubmitWithOnlyOneSubTransaction.
@Test
public void testSubmitWithOnlyOneSubTransaction() throws InterruptedException {
DOMStore configDomStore = mock(DOMStore.class);
DOMStore operationalDomStore = mock(DOMStore.class);
DOMStoreReadWriteTransaction mockStoreReadWriteTransaction = mock(DOMStoreReadWriteTransaction.class);
DOMStoreThreePhaseCommitCohort mockCohort = mock(DOMStoreThreePhaseCommitCohort.class);
doReturn(mockStoreReadWriteTransaction).when(operationalDomStore).newReadWriteTransaction();
doReturn(mockCohort).when(mockStoreReadWriteTransaction).ready();
doReturn(Futures.immediateFuture(false)).when(mockCohort).canCommit();
doReturn(Futures.immediateFuture(null)).when(mockCohort).abort();
final CountDownLatch latch = new CountDownLatch(1);
final List<DOMStoreThreePhaseCommitCohort> commitCohorts = new ArrayList<>();
try (ConcurrentDOMDataBroker dataBroker = new ConcurrentDOMDataBroker(ImmutableMap.of(LogicalDatastoreType.OPERATIONAL, operationalDomStore, LogicalDatastoreType.CONFIGURATION, configDomStore), futureExecutor) {
@Override
public CheckedFuture<Void, TransactionCommitFailedException> submit(DOMDataTreeWriteTransaction writeTx, Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
commitCohorts.addAll(cohorts);
latch.countDown();
return super.submit(writeTx, cohorts);
}
}) {
DOMDataTreeReadWriteTransaction domDataReadWriteTransaction = dataBroker.newReadWriteTransaction();
domDataReadWriteTransaction.delete(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY);
domDataReadWriteTransaction.submit();
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertTrue(commitCohorts.size() == 1);
}
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction in project controller by opendaylight.
the class DistributedDataStoreIntegrationTest method testTransactionReadsWithShardNotInitiallyReady.
@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testTransactionReadsWithShardNotInitiallyReady() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
final String testName = "testTransactionReadsWithShardNotInitiallyReady";
final String shardName = "test-1";
// Setup the InMemoryJournal to block shard recovery to ensure
// the shard isn't
// initialized until we create the Tx.
final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, testName, false, shardName)) {
// Create the read-write Tx
final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
assertNotNull("newReadWriteTransaction returned null", readWriteTx);
// Do some reads on the Tx on a separate thread.
final AtomicReference<CheckedFuture<Boolean, ReadFailedException>> txExistsFuture = new AtomicReference<>();
final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>> txReadFuture = new AtomicReference<>();
final AtomicReference<Exception> caughtEx = new AtomicReference<>();
final CountDownLatch txReadsDone = new CountDownLatch(1);
final Thread txThread = new Thread(() -> {
try {
readWriteTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
txExistsFuture.set(readWriteTx.exists(TestModel.TEST_PATH));
txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
} catch (Exception e) {
caughtEx.set(e);
} finally {
txReadsDone.countDown();
}
});
txThread.start();
// Wait for the Tx operations to complete.
boolean done = Uninterruptibles.awaitUninterruptibly(txReadsDone, 5, TimeUnit.SECONDS);
if (caughtEx.get() != null) {
throw caughtEx.get();
}
assertEquals("Tx reads done", true, done);
// At this point the Tx operations should be waiting for the
// shard to initialize so
// trigger the latch to let the shard recovery to continue.
blockRecoveryLatch.countDown();
// Wait for the reads to complete and verify.
assertEquals("exists", true, txExistsFuture.get().checkedGet(5, TimeUnit.SECONDS));
assertEquals("read", true, txReadFuture.get().checkedGet(5, TimeUnit.SECONDS).isPresent());
readWriteTx.close();
}
}
};
}
use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction 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.DOMStoreReadWriteTransaction in project controller by opendaylight.
the class DistributedDataStoreIntegrationTest method testReadWriteTransactionWithSingleShard.
@Test
public void testReadWriteTransactionWithSingleShard() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testReadWriteTransactionWithSingleShard", "test-1")) {
// 1. Create a read-write Tx
final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
assertNotNull("newReadWriteTransaction returned null", readWriteTx);
// 2. Write some data
final YangInstanceIdentifier nodePath = TestModel.TEST_PATH;
final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
readWriteTx.write(nodePath, nodeToWrite);
// 3. Read the data from Tx
final Boolean exists = readWriteTx.exists(nodePath).checkedGet(5, TimeUnit.SECONDS);
assertEquals("exists", true, exists);
Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(nodePath).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
assertEquals("Data node", nodeToWrite, optional.get());
// 4. Ready the Tx for commit
final DOMStoreThreePhaseCommitCohort cohort = readWriteTx.ready();
// 5. Commit the Tx
doCommit(cohort);
// 6. Verify the data in the store
final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
assertEquals("isPresent", true, optional.isPresent());
assertEquals("Data node", nodeToWrite, optional.get());
}
}
};
}
Aggregations