Search in sources :

Example 6 with Snapshot

use of org.opendaylight.controller.cluster.raft.persisted.Snapshot in project controller by opendaylight.

the class LeaderTest method testSendSnapshotfromInstallSnapshotReply.

@Test
public void testSendSnapshotfromInstallSnapshotReply() throws Exception {
    logStart("testSendSnapshotfromInstallSnapshotReply");
    MockRaftActorContext actorContext = createActorContextWithFollower();
    final int commitIndex = 3;
    final int snapshotIndex = 2;
    final int snapshotTerm = 1;
    final int currentTerm = 2;
    DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl() {

        @Override
        public int getSnapshotChunkSize() {
            return 50;
        }
    };
    configParams.setHeartBeatInterval(new FiniteDuration(9, TimeUnit.SECONDS));
    configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(10, TimeUnit.SECONDS));
    actorContext.setConfigParams(configParams);
    actorContext.setCommitIndex(commitIndex);
    leader = new Leader(actorContext);
    actorContext.setCurrentBehavior(leader);
    leader.getFollower(FOLLOWER_ID).setMatchIndex(-1);
    leader.getFollower(FOLLOWER_ID).setNextIndex(0);
    Map<String, String> leadersSnapshot = new HashMap<>();
    leadersSnapshot.put("1", "A");
    leadersSnapshot.put("2", "B");
    leadersSnapshot.put("3", "C");
    // set the snapshot variables in replicatedlog
    actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
    actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
    actorContext.getTermInformation().update(currentTerm, leaderActor.path().toString());
    ByteString bs = toByteString(leadersSnapshot);
    Snapshot snapshot = Snapshot.create(ByteState.of(bs.toByteArray()), Collections.<ReplicatedLogEntry>emptyList(), commitIndex, snapshotTerm, commitIndex, snapshotTerm, -1, null, null);
    leader.handleMessage(leaderActor, new SendInstallSnapshot(snapshot, ByteSource.wrap(bs.toByteArray())));
    InstallSnapshot installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    assertEquals(1, installSnapshot.getChunkIndex());
    assertEquals(3, installSnapshot.getTotalChunks());
    followerActor.underlyingActor().clear();
    leader.handleMessage(followerActor, new InstallSnapshotReply(actorContext.getTermInformation().getCurrentTerm(), FOLLOWER_ID, installSnapshot.getChunkIndex(), true));
    installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    assertEquals(2, installSnapshot.getChunkIndex());
    assertEquals(3, installSnapshot.getTotalChunks());
    followerActor.underlyingActor().clear();
    leader.handleMessage(followerActor, new InstallSnapshotReply(actorContext.getTermInformation().getCurrentTerm(), FOLLOWER_ID, installSnapshot.getChunkIndex(), true));
    installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    // Send snapshot reply one more time and make sure that a new snapshot message should not be sent to follower
    followerActor.underlyingActor().clear();
    leader.handleMessage(followerActor, new InstallSnapshotReply(actorContext.getTermInformation().getCurrentTerm(), FOLLOWER_ID, installSnapshot.getChunkIndex(), true));
    installSnapshot = MessageCollectorActor.getFirstMatching(followerActor, InstallSnapshot.class);
    assertNull(installSnapshot);
}
Also used : SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) HashMap(java.util.HashMap) InstallSnapshotReply(org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply) ByteString(com.google.protobuf.ByteString) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) ByteString(com.google.protobuf.ByteString) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Test(org.junit.Test)

Example 7 with Snapshot

use of org.opendaylight.controller.cluster.raft.persisted.Snapshot in project controller by opendaylight.

the class LeaderTest method testInstallSnapshot.

@Test
public void testInstallSnapshot() throws Exception {
    logStart("testInstallSnapshot");
    final MockRaftActorContext actorContext = createActorContextWithFollower();
    Map<String, String> leadersSnapshot = new HashMap<>();
    leadersSnapshot.put("1", "A");
    leadersSnapshot.put("2", "B");
    leadersSnapshot.put("3", "C");
    // clears leaders log
    actorContext.getReplicatedLog().removeFrom(0);
    final int lastAppliedIndex = 3;
    final int snapshotIndex = 2;
    final int snapshotTerm = 1;
    final int currentTerm = 2;
    // set the snapshot variables in replicatedlog
    actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
    actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
    actorContext.getTermInformation().update(currentTerm, leaderActor.path().toString());
    actorContext.setCommitIndex(lastAppliedIndex);
    actorContext.setLastApplied(lastAppliedIndex);
    leader = new Leader(actorContext);
    // Initial heartbeat.
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    leader.getFollower(FOLLOWER_ID).setMatchIndex(-1);
    leader.getFollower(FOLLOWER_ID).setNextIndex(0);
    byte[] bytes = toByteString(leadersSnapshot).toByteArray();
    Snapshot snapshot = Snapshot.create(ByteState.of(bytes), Collections.<ReplicatedLogEntry>emptyList(), lastAppliedIndex, snapshotTerm, lastAppliedIndex, snapshotTerm, -1, null, null);
    RaftActorBehavior raftBehavior = leader.handleMessage(leaderActor, new SendInstallSnapshot(snapshot, ByteSource.wrap(bytes)));
    assertTrue(raftBehavior instanceof Leader);
    // check if installsnapshot gets called with the correct values.
    InstallSnapshot installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    assertNotNull(installSnapshot.getData());
    assertEquals(lastAppliedIndex, installSnapshot.getLastIncludedIndex());
    assertEquals(snapshotTerm, installSnapshot.getLastIncludedTerm());
    assertEquals(currentTerm, installSnapshot.getTerm());
}
Also used : Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) HashMap(java.util.HashMap) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ByteString(com.google.protobuf.ByteString) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Test(org.junit.Test)

Example 8 with Snapshot

use of org.opendaylight.controller.cluster.raft.persisted.Snapshot in project controller by opendaylight.

the class LeaderTest method testForceInstallSnapshot.

@Test
public void testForceInstallSnapshot() throws Exception {
    logStart("testForceInstallSnapshot");
    final MockRaftActorContext actorContext = createActorContextWithFollower();
    Map<String, String> leadersSnapshot = new HashMap<>();
    leadersSnapshot.put("1", "A");
    leadersSnapshot.put("2", "B");
    leadersSnapshot.put("3", "C");
    final int lastAppliedIndex = 3;
    final int snapshotIndex = -1;
    final int snapshotTerm = -1;
    final int currentTerm = 2;
    // set the snapshot variables in replicatedlog
    actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
    actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
    actorContext.getTermInformation().update(currentTerm, leaderActor.path().toString());
    actorContext.setCommitIndex(lastAppliedIndex);
    actorContext.setLastApplied(lastAppliedIndex);
    leader = new Leader(actorContext);
    // Initial heartbeat.
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    leader.getFollower(FOLLOWER_ID).setMatchIndex(-1);
    leader.getFollower(FOLLOWER_ID).setNextIndex(-1);
    byte[] bytes = toByteString(leadersSnapshot).toByteArray();
    Snapshot snapshot = Snapshot.create(ByteState.of(bytes), Collections.<ReplicatedLogEntry>emptyList(), lastAppliedIndex, snapshotTerm, lastAppliedIndex, snapshotTerm, -1, null, null);
    RaftActorBehavior raftBehavior = leader.handleMessage(leaderActor, new SendInstallSnapshot(snapshot, ByteSource.wrap(bytes)));
    assertTrue(raftBehavior instanceof Leader);
    // check if installsnapshot gets called with the correct values.
    InstallSnapshot installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    assertNotNull(installSnapshot.getData());
    assertEquals(lastAppliedIndex, installSnapshot.getLastIncludedIndex());
    assertEquals(snapshotTerm, installSnapshot.getLastIncludedTerm());
    assertEquals(currentTerm, installSnapshot.getTerm());
}
Also used : Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) HashMap(java.util.HashMap) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ByteString(com.google.protobuf.ByteString) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Test(org.junit.Test)

Example 9 with Snapshot

use of org.opendaylight.controller.cluster.raft.persisted.Snapshot in project controller by opendaylight.

the class RaftActorTest method testRaftActorForwardsToRaftActorRecoverySupport.

@Test
public void testRaftActorForwardsToRaftActorRecoverySupport() {
    String persistenceId = factory.generateActorId("leader-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, Collections.<String, String>emptyMap(), config), persistenceId);
    MockRaftActor mockRaftActor = mockActorRef.underlyingActor();
    // Wait for akka's recovery to complete so it doesn't interfere.
    mockRaftActor.waitForRecoveryComplete();
    RaftActorRecoverySupport mockSupport = mock(RaftActorRecoverySupport.class);
    mockRaftActor.setRaftActorRecoverySupport(mockSupport);
    Snapshot snapshot = Snapshot.create(ByteState.of(new byte[] { 1 }), Collections.<ReplicatedLogEntry>emptyList(), 3, 1, 3, 1, -1, null, null);
    SnapshotOffer snapshotOffer = new SnapshotOffer(new SnapshotMetadata("test", 6, 12345), snapshot);
    mockRaftActor.handleRecover(snapshotOffer);
    ReplicatedLogEntry logEntry = new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1", 5));
    mockRaftActor.handleRecover(logEntry);
    ApplyJournalEntries applyJournalEntries = new ApplyJournalEntries(2);
    mockRaftActor.handleRecover(applyJournalEntries);
    DeleteEntries deleteEntries = new DeleteEntries(1);
    mockRaftActor.handleRecover(deleteEntries);
    UpdateElectionTerm updateElectionTerm = new UpdateElectionTerm(5, "member2");
    mockRaftActor.handleRecover(updateElectionTerm);
    verify(mockSupport).handleRecoveryMessage(same(snapshotOffer), any(PersistentDataProvider.class));
    verify(mockSupport).handleRecoveryMessage(same(logEntry), any(PersistentDataProvider.class));
    verify(mockSupport).handleRecoveryMessage(same(applyJournalEntries), any(PersistentDataProvider.class));
    verify(mockSupport).handleRecoveryMessage(same(deleteEntries), any(PersistentDataProvider.class));
    verify(mockSupport).handleRecoveryMessage(same(updateElectionTerm), any(PersistentDataProvider.class));
}
Also used : DeleteEntries(org.opendaylight.controller.cluster.raft.persisted.DeleteEntries) FiniteDuration(scala.concurrent.duration.FiniteDuration) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) ByteString(com.google.protobuf.ByteString) 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) PersistentDataProvider(org.opendaylight.controller.cluster.PersistentDataProvider) NonPersistentDataProvider(org.opendaylight.controller.cluster.NonPersistentDataProvider) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SnapshotMetadata(akka.persistence.SnapshotMetadata) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ApplyJournalEntries(org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries) SnapshotOffer(akka.persistence.SnapshotOffer) UpdateElectionTerm(org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm) Test(org.junit.Test)

Example 10 with Snapshot

use of org.opendaylight.controller.cluster.raft.persisted.Snapshot in project controller by opendaylight.

the class RaftActorTest method testRestoreFromSnapshot.

@Test
public void testRestoreFromSnapshot() throws Exception {
    TEST_LOG.info("testRestoreFromSnapshot starting");
    String persistenceId = factory.generateActorId("test-actor-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
    List<ReplicatedLogEntry> snapshotUnappliedEntries = new ArrayList<>();
    snapshotUnappliedEntries.add(new SimpleReplicatedLogEntry(4, 1, new MockRaftActorContext.MockPayload("E")));
    int snapshotLastApplied = 3;
    int snapshotLastIndex = 4;
    MockSnapshotState snapshotState = new MockSnapshotState(Arrays.asList(new MockRaftActorContext.MockPayload("A"), new MockRaftActorContext.MockPayload("B"), new MockRaftActorContext.MockPayload("C"), new MockRaftActorContext.MockPayload("D")));
    Snapshot snapshot = Snapshot.create(snapshotState, snapshotUnappliedEntries, snapshotLastIndex, 1, snapshotLastApplied, 1, 1, "member-1", null);
    InMemorySnapshotStore.addSnapshotSavedLatch(persistenceId);
    TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).restoreFromSnapshot(snapshot).props().withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
    MockRaftActor mockRaftActor = raftActorRef.underlyingActor();
    mockRaftActor.waitForRecoveryComplete();
    Snapshot savedSnapshot = InMemorySnapshotStore.waitForSavedSnapshot(persistenceId, Snapshot.class);
    assertEquals("getElectionTerm", snapshot.getElectionTerm(), savedSnapshot.getElectionTerm());
    assertEquals("getElectionVotedFor", snapshot.getElectionVotedFor(), savedSnapshot.getElectionVotedFor());
    assertEquals("getLastAppliedIndex", snapshot.getLastAppliedIndex(), savedSnapshot.getLastAppliedIndex());
    assertEquals("getLastAppliedTerm", snapshot.getLastAppliedTerm(), savedSnapshot.getLastAppliedTerm());
    assertEquals("getLastIndex", snapshot.getLastIndex(), savedSnapshot.getLastIndex());
    assertEquals("getLastTerm", snapshot.getLastTerm(), savedSnapshot.getLastTerm());
    assertEquals("getState", snapshot.getState(), savedSnapshot.getState());
    assertEquals("getUnAppliedEntries", snapshot.getUnAppliedEntries(), savedSnapshot.getUnAppliedEntries());
    verify(mockRaftActor.snapshotCohortDelegate, timeout(5000)).applySnapshot(any(Snapshot.State.class));
    RaftActorContext context = mockRaftActor.getRaftActorContext();
    assertEquals("Journal log size", 1, context.getReplicatedLog().size());
    assertEquals("Last index", snapshotLastIndex, context.getReplicatedLog().lastIndex());
    assertEquals("Last applied", snapshotLastApplied, context.getLastApplied());
    assertEquals("Commit index", snapshotLastApplied, context.getCommitIndex());
    assertEquals("Recovered state", snapshotState.getState(), mockRaftActor.getState());
    assertEquals("Current term", 1L, context.getTermInformation().getCurrentTerm());
    assertEquals("Voted for", "member-1", context.getTermInformation().getVotedFor());
    // Test with data persistence disabled
    snapshot = Snapshot.create(EmptyState.INSTANCE, Collections.<ReplicatedLogEntry>emptyList(), -1, -1, -1, -1, 5, "member-1", null);
    persistenceId = factory.generateActorId("test-actor-");
    raftActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).restoreFromSnapshot(snapshot).persistent(Optional.of(Boolean.FALSE)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
    mockRaftActor = raftActorRef.underlyingActor();
    mockRaftActor.waitForRecoveryComplete();
    assertEquals("snapshot committed", true, Uninterruptibles.awaitUninterruptibly(mockRaftActor.snapshotCommitted, 5, TimeUnit.SECONDS));
    context = mockRaftActor.getRaftActorContext();
    assertEquals("Current term", 5L, context.getTermInformation().getCurrentTerm());
    assertEquals("Voted for", "member-1", context.getTermInformation().getVotedFor());
    TEST_LOG.info("testRestoreFromSnapshot ending");
}
Also used : MockSnapshotState(org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) 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) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) EmptyState(org.opendaylight.controller.cluster.raft.persisted.EmptyState) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState) MockSnapshotState(org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState) ByteState(org.opendaylight.controller.cluster.raft.persisted.ByteState) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) Test(org.junit.Test)

Aggregations

Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)42 Test (org.junit.Test)28 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)23 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)21 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)16 ByteString (com.google.protobuf.ByteString)14 CaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot)14 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)12 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)8 DisableElectionsRaftPolicy (org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy)8 ActorRef (akka.actor.ActorRef)7 InstallSnapshotReply (org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply)7 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)6 MockSnapshotState (org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState)6 SendInstallSnapshot (org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot)6 GetSnapshot (org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot)6 ApplyJournalEntries (org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries)6 ByteState (org.opendaylight.controller.cluster.raft.persisted.ByteState)6 UpdateElectionTerm (org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm)6 TestActorRef (akka.testkit.TestActorRef)5