Search in sources :

Example 1 with SaveSnapshotSuccess

use of akka.persistence.SaveSnapshotSuccess in project controller by opendaylight.

the class RaftActorTest method testRaftActorForwardsToRaftActorSnapshotMessageSupport.

@Test
public void testRaftActorForwardsToRaftActorSnapshotMessageSupport() {
    String persistenceId = factory.generateActorId("leader-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    RaftActorSnapshotMessageSupport mockSupport = mock(RaftActorSnapshotMessageSupport.class);
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).snapshotMessageSupport(mockSupport).props());
    MockRaftActor mockRaftActor = mockActorRef.underlyingActor();
    // Wait for akka's recovery to complete so it doesn't interfere.
    mockRaftActor.waitForRecoveryComplete();
    ApplySnapshot applySnapshot = new ApplySnapshot(mock(Snapshot.class));
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(applySnapshot), any(ActorRef.class));
    mockRaftActor.handleCommand(applySnapshot);
    CaptureSnapshotReply captureSnapshotReply = new CaptureSnapshotReply(ByteState.empty(), java.util.Optional.empty());
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(captureSnapshotReply), any(ActorRef.class));
    mockRaftActor.handleCommand(captureSnapshotReply);
    SaveSnapshotSuccess saveSnapshotSuccess = new SaveSnapshotSuccess(new SnapshotMetadata("", 0L, 0L));
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(saveSnapshotSuccess), any(ActorRef.class));
    mockRaftActor.handleCommand(saveSnapshotSuccess);
    SaveSnapshotFailure saveSnapshotFailure = new SaveSnapshotFailure(new SnapshotMetadata("", 0L, 0L), new Throwable());
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(saveSnapshotFailure), any(ActorRef.class));
    mockRaftActor.handleCommand(saveSnapshotFailure);
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT), any(ActorRef.class));
    mockRaftActor.handleCommand(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT);
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(GetSnapshot.INSTANCE), any(ActorRef.class));
    mockRaftActor.handleCommand(GetSnapshot.INSTANCE);
    verify(mockSupport).handleSnapshotMessage(same(applySnapshot), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(captureSnapshotReply), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(saveSnapshotSuccess), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(saveSnapshotFailure), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(GetSnapshot.INSTANCE), any(ActorRef.class));
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) ByteString(com.google.protobuf.ByteString) SaveSnapshotFailure(akka.persistence.SaveSnapshotFailure) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) GetSnapshot(org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot) SnapshotMetadata(akka.persistence.SnapshotMetadata) SaveSnapshotSuccess(akka.persistence.SaveSnapshotSuccess) Test(org.junit.Test)

Example 2 with SaveSnapshotSuccess

use of akka.persistence.SaveSnapshotSuccess in project controller by opendaylight.

the class RaftActorSnapshotMessageSupportTest method testOnSaveSnapshotSuccess.

@Test
public void testOnSaveSnapshotSuccess() {
    long sequenceNumber = 100;
    long timeStamp = 1234L;
    sendMessageToSupport(new SaveSnapshotSuccess(new SnapshotMetadata("foo", sequenceNumber, timeStamp)));
    verify(mockSnapshotManager).commit(eq(sequenceNumber), eq(timeStamp));
}
Also used : SnapshotMetadata(akka.persistence.SnapshotMetadata) SaveSnapshotSuccess(akka.persistence.SaveSnapshotSuccess) Test(org.junit.Test)

Example 3 with SaveSnapshotSuccess

use of akka.persistence.SaveSnapshotSuccess 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

SaveSnapshotSuccess (akka.persistence.SaveSnapshotSuccess)3 SnapshotMetadata (akka.persistence.SnapshotMetadata)2 Test (org.junit.Test)2 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)2 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)2 ActorRef (akka.actor.ActorRef)1 SaveSnapshotFailure (akka.persistence.SaveSnapshotFailure)1 TestActorRef (akka.testkit.TestActorRef)1 ByteString (com.google.protobuf.ByteString)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 DataPersistenceProvider (org.opendaylight.controller.cluster.DataPersistenceProvider)1 DelegatingPersistentDataProvider (org.opendaylight.controller.cluster.DelegatingPersistentDataProvider)1 MetadataShardDataTreeSnapshot (org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot)1 ShardSnapshotState (org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState)1 RaftActorContext (org.opendaylight.controller.cluster.raft.RaftActorContext)1 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)1 CaptureSnapshotReply (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply)1 GetSnapshot (org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot)1 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)1