/*
- * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.After;
-import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opendaylight.controller.cluster.datastore.persisted.PayloadVersion;
import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
+import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
@Before
public void setUp() throws IOException {
+ InMemorySnapshotStore.clear();
+ InMemoryJournal.clear();
system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
Cluster.get(system).join(member1Address);
public void testWriteTransactionWithSingleShard() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "transactionIntegrationTest", "test-1")) {
testWriteTransaction(dataStore, TestModel.TEST_PATH,
public void testWriteTransactionWithMultipleShards() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
public void testReadWriteTransactionWithSingleShard() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testReadWriteTransactionWithSingleShard", "test-1")) {
// 1. Create a read-write Tx
public void testReadWriteTransactionWithMultipleShards() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testReadWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testSingleTransactionsWritesInQuickSuccession", "cars-1")) {
final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, testName, false, shardName)) {
// Create the write Tx
final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, testName, false, shardName)) {
// Create the read-write Tx
InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, testName, false, shardName)) {
// Create the read-write Tx
datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1)
.shardInitializationTimeout(200, TimeUnit.MILLISECONDS);
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, testName, false, shardName)) {
final Object result = dataStore.getActorContext().executeOperation(
assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
// Create the write Tx.
- try (final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
- : dataStore.newReadWriteTransaction()) {
+ 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
txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
fail("Expected NoShardLeaderException");
} catch (final ExecutionException e) {
- Throwables.propagate(Throwables.getRootCause(e));
+ assertTrue(Throwables.getRootCause(e) instanceof NoShardLeaderException);
+ assertEquals(DistributedDataStore.class, testParameter);
+ } catch (TimeoutException e) {
+ // ClientBackedDataStore doesn't set cause to ExecutionException, future just time outs
+ assertEquals(ClientBackedDataStore.class, testParameter);
+ }
+ } finally {
+ try {
+ if (writeTxToClose != null) {
+ writeTxToClose.close();
+ }
+ } catch (Exception e) {
+ // FIXME TransactionProxy.close throws IllegalStateException:
+ // Transaction is ready, it cannot be closed
}
}
}
};
}
- @Test(expected = NoShardLeaderException.class)
+ @Test
public void testWriteOnlyTransactionCommitFailureWithNoShardLeader() throws Exception {
datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
testTransactionCommitFailureWithNoShardLeader(true, "testWriteOnlyTransactionCommitFailureWithNoShardLeader");
}
- @Test(expected = NoShardLeaderException.class)
+ @Test
public void testReadWriteTransactionCommitFailureWithNoShardLeader() throws Exception {
testTransactionCommitFailureWithNoShardLeader(false, "testReadWriteTransactionCommitFailureWithNoShardLeader");
}
public void testTransactionAbort() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "transactionAbortIntegrationTest", "test-1")) {
final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
public void testTransactionChainWithSingleShard() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testTransactionChainWithSingleShard", "test-1")) {
// 1. Create a Tx chain and write-only Tx
public void testTransactionChainWithMultipleShards() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
public void testCreateChainedTransactionsInQuickSuccession() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testCreateChainedTransactionsInQuickSuccession", "cars-1")) {
final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
public void testCreateChainedTransactionAfterEmptyTxReadied() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testCreateChainedTransactionAfterEmptyTxReadied", "test-1")) {
final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
public void testCreateChainedTransactionWhenPreviousNotReady() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testCreateChainedTransactionWhenPreviousNotReady", "test-1")) {
final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
public void testChainWithReadOnlyTxAfterPreviousReady() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
@Test
public void testChainedTransactionFailureWithSingleShard() throws Exception {
- //TODO remove when test passes also for ClientBackedDataStore
- Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
-
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testChainedTransactionFailureWithSingleShard", "cars-1")) {
final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
@Test
public void testChainedTransactionFailureWithMultipleShards() throws Exception {
- //TODO remove when test passes also for ClientBackedDataStore
- Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
-
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testChainedTransactionFailureWithMultipleShards", "cars-1", "people-1")) {
final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
public void testChangeListenerRegistration() throws Exception {
new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
{
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, "testChangeListenerRegistration", "test-1")) {
testWriteTransaction(dataStore, TestModel.TEST_PATH,
assertNotNull("registerChangeListener returned null", listenerReg);
+ IntegrationTestKit.verifyShardState(dataStore, "test-1",
+ state -> assertEquals("getDataChangeListenerActors", 1,
+ state.getDataChangeListenerActors().size()));
+
// Wait for the initial notification
listener.waitForChangeEvents(TestModel.TEST_PATH);
listener.reset(2);
listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
listenerReg.close();
+ IntegrationTestKit.verifyShardState(dataStore, "test-1",
+ state -> assertEquals("getDataChangeListenerActors", 0,
+ state.getDataChangeListenerActors().size()));
+
+ testWriteTransaction(dataStore,
+ YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
+ .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
+ ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
+
+ listener.expectNoMoreChanges("Received unexpected change after close");
+ }
+ }
+ };
+ }
+
+ @Test
+ public void testDataTreeChangeListenerRegistration() throws Exception {
+ new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
+ {
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
+ testParameter, "testDataTreeChangeListenerRegistration", "test-1")) {
+
+ testWriteTransaction(dataStore, TestModel.TEST_PATH,
+ ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+
+ final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
+
+ ListenerRegistration<MockDataTreeChangeListener> listenerReg = dataStore
+ .registerTreeChangeListener(TestModel.TEST_PATH, listener);
+
+ assertNotNull("registerTreeChangeListener returned null", listenerReg);
+
+ IntegrationTestKit.verifyShardState(dataStore, "test-1",
+ state -> assertEquals("getTreeChangeListenerActors", 1,
+ state.getTreeChangeListenerActors().size()));
+
+ // Wait for the initial notification
+ listener.waitForChangeEvents(TestModel.TEST_PATH);
+ listener.reset(2);
+
+ // Write 2 updates.
+ testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
+ ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
+
+ YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
+ .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
+ testWriteTransaction(dataStore, listPath,
+ ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
+
+ // Wait for the 2 updates.
+ listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
+ listenerReg.close();
+
+ IntegrationTestKit.verifyShardState(dataStore, "test-1",
+ state -> assertEquals("getTreeChangeListenerActors", 0,
+ state.getTreeChangeListenerActors().size()));
+
testWriteTransaction(dataStore,
YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
.nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
new DatastoreSnapshot.ShardSnapshot("cars", carsSnapshot),
new DatastoreSnapshot.ShardSnapshot("people", peopleSnapshot)));
- try (final AbstractDataStore dataStore = setupAbstractDataStore(
+ try (AbstractDataStore dataStore = setupAbstractDataStore(
testParameter, name, "module-shards-member1.conf", true, "cars", "people")) {
final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
MetadataShardDataTreeSnapshot shardSnapshot = new MetadataShardDataTreeSnapshot(root);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- try (final DataOutputStream dos = new DataOutputStream(bos)) {
+ try (DataOutputStream dos = new DataOutputStream(bos)) {
PayloadVersion.BORON.writeTo(dos);
try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
oos.writeObject(shardSnapshot);