Search in sources :

Example 6 with ShardSnapshotState

use of org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState in project controller by opendaylight.

the class ShardTest method testCreateSnapshot.

private void testCreateSnapshot(final boolean persistent, final String shardActorName) throws Exception {
    final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));
    final AtomicReference<Object> savedSnapshot = new AtomicReference<>();
    class TestPersistentDataProvider extends DelegatingPersistentDataProvider {

        TestPersistentDataProvider(final DataPersistenceProvider delegate) {
            super(delegate);
        }

        @Override
        public void saveSnapshot(final Object obj) {
            savedSnapshot.set(obj);
            super.saveSnapshot(obj);
        }
    }
    dataStoreContextBuilder.persistent(persistent);
    class TestShard extends Shard {

        protected TestShard(final AbstractBuilder<?, ?> builder) {
            super(builder);
            setPersistence(new TestPersistentDataProvider(super.persistence()));
        }

        @Override
        public void handleCommand(final Object message) {
            super.handleCommand(message);
            // XXX:  commit_snapshot equality check references RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT
            if (message instanceof SaveSnapshotSuccess || "commit_snapshot".equals(message.toString())) {
                latch.get().countDown();
            }
        }

        @Override
        public RaftActorContext getRaftActorContext() {
            return super.getRaftActorContext();
        }
    }
    new ShardTestKit(getSystem()) {

        {
            final Creator<Shard> creator = () -> new TestShard(newShardBuilder());
            final TestActorRef<Shard> shard = actorFactory.createTestActor(Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()), shardActorName);
            waitUntilLeader(shard);
            writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
            final NormalizedNode<?, ?> expectedRoot = readStore(shard, YangInstanceIdentifier.EMPTY);
            // Trigger creation of a snapshot by ensuring
            final RaftActorContext raftActorContext = ((TestShard) shard.underlyingActor()).getRaftActorContext();
            raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
            awaitAndValidateSnapshot(expectedRoot);
            raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
            awaitAndValidateSnapshot(expectedRoot);
        }

        private void awaitAndValidateSnapshot(final NormalizedNode<?, ?> expectedRoot) throws InterruptedException, IOException {
            assertEquals("Snapshot saved", true, latch.get().await(5, TimeUnit.SECONDS));
            assertTrue("Invalid saved snapshot " + savedSnapshot.get(), savedSnapshot.get() instanceof Snapshot);
            verifySnapshot((Snapshot) savedSnapshot.get(), expectedRoot);
            latch.set(new CountDownLatch(1));
            savedSnapshot.set(null);
        }

        private void verifySnapshot(final Snapshot snapshot, final NormalizedNode<?, ?> expectedRoot) throws IOException {
            final NormalizedNode<?, ?> actual = ((ShardSnapshotState) snapshot.getState()).getSnapshot().getRootNode().get();
            assertEquals("Root node", expectedRoot, actual);
        }
    };
}
Also used : ShardSnapshotState(org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) DelegatingPersistentDataProvider(org.opendaylight.controller.cluster.DelegatingPersistentDataProvider) RaftActorContext(org.opendaylight.controller.cluster.raft.RaftActorContext) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) SaveSnapshotSuccess(akka.persistence.SaveSnapshotSuccess)

Example 7 with ShardSnapshotState

use of org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState in project controller by opendaylight.

the class ShardTest method testApplySnapshot.

@Test
public void testApplySnapshot() throws Exception {
    final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testApplySnapshot");
    ShardTestKit.waitUntilLeader(shard);
    final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SCHEMA_CONTEXT);
    final ContainerNode container = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)).build()).build();
    writeToStore(store, TestModel.TEST_PATH, container);
    final YangInstanceIdentifier root = YangInstanceIdentifier.EMPTY;
    final NormalizedNode<?, ?> expected = readStore(store, root);
    final Snapshot snapshot = Snapshot.create(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(expected)), Collections.<ReplicatedLogEntry>emptyList(), 1, 2, 3, 4, -1, null, null);
    shard.tell(new ApplySnapshot(snapshot), ActorRef.noSender());
    final Stopwatch sw = Stopwatch.createStarted();
    while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
        Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
        try {
            assertEquals("Root node", expected, readStore(shard, root));
            return;
        } catch (final AssertionError e) {
        // try again
        }
    }
    fail("Snapshot was not applied");
}
Also used : ShardSnapshotState(org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) Stopwatch(com.google.common.base.Stopwatch) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) InMemoryDataTreeFactory(org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) Test(org.junit.Test)

Example 8 with ShardSnapshotState

use of org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState in project controller by opendaylight.

the class DatastoreSnapshotRestoreTest method newSnapshot.

private static Snapshot newSnapshot(final YangInstanceIdentifier path, final NormalizedNode<?, ?> node) throws Exception {
    DataTree dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
    AbstractShardTest.writeToStore(dataTree, path, node);
    NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
    return Snapshot.create(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)), Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null);
}
Also used : DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) ShardSnapshotState(org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) InMemoryDataTreeFactory(org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory)

Example 9 with ShardSnapshotState

use of org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState in project controller by opendaylight.

the class DistributedDataStoreIntegrationTest method testRestoreFromDatastoreSnapshot.

@Test
public void testRestoreFromDatastoreSnapshot() throws Exception {
    new IntegrationTestKit(getSystem(), datastoreContextBuilder) {

        {
            final String name = "transactionIntegrationTest";
            final ContainerNode carsNode = CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)), CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
            DataTree dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
            AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
            NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
            final Snapshot carsSnapshot = Snapshot.create(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)), Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
            dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
            final NormalizedNode<?, ?> peopleNode = PeopleModel.create();
            AbstractShardTest.writeToStore(dataTree, PeopleModel.BASE_PATH, peopleNode);
            root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
            final Snapshot peopleSnapshot = Snapshot.create(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)), Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
            restoreFromSnapshot = new DatastoreSnapshot(name, null, Arrays.asList(new DatastoreSnapshot.ShardSnapshot("cars", carsSnapshot), new DatastoreSnapshot.ShardSnapshot("people", peopleSnapshot)));
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, name, "module-shards-member1.conf", true, "cars", "people")) {
                final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
                // two reads
                Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("Data node", carsNode, optional.get());
                optional = readTx.read(PeopleModel.BASE_PATH).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("Data node", peopleNode, optional.get());
            }
        }
    };
}
Also used : ShardSnapshotState(org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState) DOMStoreReadTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) AddressFromURIString(akka.actor.AddressFromURIString) InMemoryDataTreeFactory(org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Test(org.junit.Test)

Example 10 with ShardSnapshotState

use of org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState in project controller by opendaylight.

the class DistributedDataStoreRemotingIntegrationTest method testInstallSnapshot.

@Test
public void testInstallSnapshot() throws Exception {
    final String testName = "testInstallSnapshot";
    final String leaderCarShardName = "member-1-shard-cars-" + testName;
    final String followerCarShardName = "member-2-shard-cars-" + testName;
    // Setup a saved snapshot on the leader. The follower will startup with no data and the leader should
    // install a snapshot to sync the follower.
    DataTree tree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, SchemaContextHelper.full());
    final ContainerNode carsNode = CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000))));
    AbstractShardTest.writeToStore(tree, CarsModel.BASE_PATH, carsNode);
    final NormalizedNode<?, ?> snapshotRoot = AbstractShardTest.readStore(tree, YangInstanceIdentifier.EMPTY);
    final Snapshot initialSnapshot = Snapshot.create(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(snapshotRoot)), Collections.emptyList(), 5, 1, 5, 1, 1, null, null);
    InMemorySnapshotStore.addSnapshot(leaderCarShardName, initialSnapshot);
    InMemorySnapshotStore.addSnapshotSavedLatch(leaderCarShardName);
    InMemorySnapshotStore.addSnapshotSavedLatch(followerCarShardName);
    initDatastoresWithCars(testName);
    final Optional<NormalizedNode<?, ?>> readOptional = leaderDistributedDataStore.newReadOnlyTransaction().read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
    assertEquals("isPresent", true, readOptional.isPresent());
    assertEquals("Node", carsNode, readOptional.get());
    verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(leaderCarShardName, Snapshot.class), initialSnapshot, snapshotRoot);
    verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(followerCarShardName, Snapshot.class), initialSnapshot, snapshotRoot);
}
Also used : MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) GetShardDataTree(org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) ShardSnapshotState(org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) AddressFromURIString(akka.actor.AddressFromURIString) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) InMemoryDataTreeFactory(org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory) Test(org.junit.Test)

Aggregations

ShardSnapshotState (org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState)10 MetadataShardDataTreeSnapshot (org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot)7 DataTree (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree)6 InMemoryDataTreeFactory (org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory)6 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)4 Test (org.junit.Test)3 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)3 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)3 AddressFromURIString (akka.actor.AddressFromURIString)2 IOException (java.io.IOException)2 ShardDataTreeSnapshot (org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot)2 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)2 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)2 SaveSnapshotSuccess (akka.persistence.SaveSnapshotSuccess)1 Stopwatch (com.google.common.base.Stopwatch)1 File (java.io.File)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1