Search in sources :

Example 11 with DataPersistenceProvider

use of org.opendaylight.controller.cluster.DataPersistenceProvider in project controller by opendaylight.

the class RaftActor method setPersistence.

protected void setPersistence(final boolean persistent) {
    DataPersistenceProvider currentPersistence = persistence();
    if (persistent && (currentPersistence == null || !currentPersistence.isRecoveryApplicable())) {
        setPersistence(new PersistentDataProvider(this));
        if (getCurrentBehavior() != null) {
            LOG.info("{}: Persistence has been enabled - capturing snapshot", persistenceId());
            captureSnapshot();
        }
    } else if (!persistent && (currentPersistence == null || currentPersistence.isRecoveryApplicable())) {
        setPersistence(new NonPersistentDataProvider(this) {

            /*
                 * The way snapshotting works is,
                 * <ol>
                 * <li> RaftActor calls createSnapshot on the Shard
                 * <li> Shard sends a CaptureSnapshotReply and RaftActor then calls saveSnapshot
                 * <li> When saveSnapshot is invoked on the akka-persistence API it uses the SnapshotStore to save
                 * the snapshot. The SnapshotStore sends SaveSnapshotSuccess or SaveSnapshotFailure. When the
                 * RaftActor gets SaveSnapshot success it commits the snapshot to the in-memory journal. This
                 * commitSnapshot is mimicking what is done in SaveSnapshotSuccess.
                 * </ol>
                 */
            @Override
            public void saveSnapshot(final Object object) {
                // Make saving Snapshot successful
                // Committing the snapshot here would end up calling commit in the creating state which would
                // be a state violation. That's why now we send a message to commit the snapshot.
                self().tell(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT, self());
            }
        });
    }
}
Also used : PersistentDataProvider(org.opendaylight.controller.cluster.PersistentDataProvider) DelegatingPersistentDataProvider(org.opendaylight.controller.cluster.DelegatingPersistentDataProvider) NonPersistentDataProvider(org.opendaylight.controller.cluster.NonPersistentDataProvider) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) NonPersistentDataProvider(org.opendaylight.controller.cluster.NonPersistentDataProvider)

Example 12 with DataPersistenceProvider

use of org.opendaylight.controller.cluster.DataPersistenceProvider 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)

Aggregations

DataPersistenceProvider (org.opendaylight.controller.cluster.DataPersistenceProvider)12 ByteString (com.google.protobuf.ByteString)10 Test (org.junit.Test)10 FiniteDuration (scala.concurrent.duration.FiniteDuration)9 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)7 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)6 Leader (org.opendaylight.controller.cluster.raft.behaviors.Leader)5 ActorRef (akka.actor.ActorRef)4 TestActorRef (akka.testkit.TestActorRef)4 CaptureSnapshotReply (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply)4 HashMap (java.util.HashMap)3 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)3 Procedure (akka.japi.Procedure)2 DelegatingPersistentDataProvider (org.opendaylight.controller.cluster.DelegatingPersistentDataProvider)2 SaveSnapshotSuccess (akka.persistence.SaveSnapshotSuccess)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 NonPersistentDataProvider (org.opendaylight.controller.cluster.NonPersistentDataProvider)1 PersistentDataProvider (org.opendaylight.controller.cluster.PersistentDataProvider)1 MetadataShardDataTreeSnapshot (org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot)1