Search in sources :

Example 1 with MockSnapshotState

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

Example 2 with MockSnapshotState

use of org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState in project controller by opendaylight.

the class RaftActorTest method testFakeSnapshotsForLeaderWithInRealSnapshots.

@Test
public void testFakeSnapshotsForLeaderWithInRealSnapshots() throws Exception {
    final String persistenceId = factory.generateActorId("leader-");
    final String follower1Id = factory.generateActorId("follower-");
    ActorRef followerActor1 = factory.createActor(MessageCollectorActor.props());
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
    DataPersistenceProvider dataPersistenceProvider = mock(DataPersistenceProvider.class);
    Map<String, String> peerAddresses = new HashMap<>();
    peerAddresses.put(follower1Id, followerActor1.path().toString());
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, peerAddresses, config, dataPersistenceProvider), persistenceId);
    MockRaftActor leaderActor = mockActorRef.underlyingActor();
    leaderActor.getRaftActorContext().setCommitIndex(4);
    leaderActor.getRaftActorContext().setLastApplied(4);
    leaderActor.getRaftActorContext().getTermInformation().update(1, persistenceId);
    leaderActor.waitForInitializeBehaviorComplete();
    // create 8 entries in the log - 0 to 4 are applied and will get picked up as part of the capture snapshot
    Leader leader = new Leader(leaderActor.getRaftActorContext());
    leaderActor.setCurrentBehavior(leader);
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    MockRaftActorContext.MockReplicatedLogBuilder logBuilder = new MockRaftActorContext.MockReplicatedLogBuilder();
    leaderActor.getRaftActorContext().setReplicatedLog(logBuilder.createEntries(0, 8, 1).build());
    assertEquals(8, leaderActor.getReplicatedLog().size());
    leaderActor.getRaftActorContext().getSnapshotManager().capture(new SimpleReplicatedLogEntry(6, 1, new MockRaftActorContext.MockPayload("x")), 4);
    verify(leaderActor.snapshotCohortDelegate).createSnapshot(anyObject(), anyObject());
    assertEquals(8, leaderActor.getReplicatedLog().size());
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    // fake snapshot on index 5
    leaderActor.onReceiveCommand(new AppendEntriesReply(follower1Id, 1, true, 5, 1, (short) 0));
    assertEquals(8, leaderActor.getReplicatedLog().size());
    // fake snapshot on index 6
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    leaderActor.onReceiveCommand(new AppendEntriesReply(follower1Id, 1, true, 6, 1, (short) 0));
    assertEquals(8, leaderActor.getReplicatedLog().size());
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    assertEquals(8, leaderActor.getReplicatedLog().size());
    MockSnapshotState snapshotState = new MockSnapshotState(Arrays.asList(new MockRaftActorContext.MockPayload("foo-0"), new MockRaftActorContext.MockPayload("foo-1"), new MockRaftActorContext.MockPayload("foo-2"), new MockRaftActorContext.MockPayload("foo-3"), new MockRaftActorContext.MockPayload("foo-4")));
    leaderActor.getRaftActorContext().getSnapshotManager().persist(snapshotState, java.util.Optional.empty(), Runtime.getRuntime().totalMemory());
    assertTrue(leaderActor.getRaftActorContext().getSnapshotManager().isCapturing());
    // The commit is needed to complete the snapshot creation process
    leaderActor.getRaftActorContext().getSnapshotManager().commit(-1, -1);
    // capture snapshot reply should remove the snapshotted entries only
    assertEquals(3, leaderActor.getReplicatedLog().size());
    assertEquals(7, leaderActor.getReplicatedLog().lastIndex());
    // add another non-replicated entry
    leaderActor.getReplicatedLog().append(new SimpleReplicatedLogEntry(8, 1, new MockRaftActorContext.MockPayload("foo-8")));
    // fake snapshot on index 7, since lastApplied = 7 , we would keep the last applied
    leaderActor.onReceiveCommand(new AppendEntriesReply(follower1Id, 1, true, 7, 1, (short) 0));
    assertEquals(2, leaderActor.getReplicatedLog().size());
    assertEquals(8, leaderActor.getReplicatedLog().lastIndex());
}
Also used : MockSnapshotState(org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState) Leader(org.opendaylight.controller.cluster.raft.behaviors.Leader) HashMap(java.util.HashMap) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) ByteString(com.google.protobuf.ByteString) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) Test(org.junit.Test)

Example 3 with MockSnapshotState

use of org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState in project controller by opendaylight.

the class RaftActorRecoverySupportTest method testDataRecoveredWithPersistenceDisabled.

@Test
public void testDataRecoveredWithPersistenceDisabled() {
    doNothing().when(mockCohort).applyRecoverySnapshot(anyObject());
    doReturn(false).when(mockPersistence).isRecoveryApplicable();
    doReturn(10L).when(mockPersistentProvider).getLastSequenceNumber();
    Snapshot snapshot = Snapshot.create(new MockSnapshotState(Arrays.asList(new MockPayload("1"))), Collections.<ReplicatedLogEntry>emptyList(), 3, 1, 3, 1, -1, null, null);
    SnapshotOffer snapshotOffer = new SnapshotOffer(new SnapshotMetadata("test", 6, 12345), snapshot);
    sendMessageToSupport(snapshotOffer);
    sendMessageToSupport(new UpdateElectionTerm(5, "member2"));
    sendMessageToSupport(new SimpleReplicatedLogEntry(4, 1, new MockRaftActorContext.MockPayload("4")));
    sendMessageToSupport(new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("5")));
    sendMessageToSupport(new ApplyJournalEntries(4));
    sendMessageToSupport(new DeleteEntries(5));
    assertEquals("Journal log size", 0, context.getReplicatedLog().size());
    assertEquals("Last index", -1, context.getReplicatedLog().lastIndex());
    assertEquals("Last applied", -1, context.getLastApplied());
    assertEquals("Commit index", -1, context.getCommitIndex());
    assertEquals("Snapshot term", -1, context.getReplicatedLog().getSnapshotTerm());
    assertEquals("Snapshot index", -1, context.getReplicatedLog().getSnapshotIndex());
    assertEquals("Current term", 5, context.getTermInformation().getCurrentTerm());
    assertEquals("Voted For", "member2", context.getTermInformation().getVotedFor());
    sendMessageToSupport(RecoveryCompleted.getInstance(), true);
    verify(mockCohort, never()).applyRecoverySnapshot(anyObject());
    verify(mockCohort, never()).getRestoreFromSnapshot();
    verifyNoMoreInteractions(mockCohort);
    verify(mockPersistentProvider).deleteMessages(10L);
}
Also used : MockSnapshotState(org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) SnapshotMetadata(akka.persistence.SnapshotMetadata) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ApplyJournalEntries(org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries) SnapshotOffer(akka.persistence.SnapshotOffer) DeleteEntries(org.opendaylight.controller.cluster.raft.persisted.DeleteEntries) UpdateElectionTerm(org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Test(org.junit.Test)

Example 4 with MockSnapshotState

use of org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState in project controller by opendaylight.

the class RaftActorRecoverySupportTest method testOnSnapshotOfferWithServerConfiguration.

@Test
public void testOnSnapshotOfferWithServerConfiguration() {
    long electionTerm = 2;
    String electionVotedFor = "member-2";
    ServerConfigurationPayload serverPayload = new ServerConfigurationPayload(Arrays.asList(new ServerInfo(localId, true), new ServerInfo("follower1", true), new ServerInfo("follower2", true)));
    MockSnapshotState snapshotState = new MockSnapshotState(Arrays.asList(new MockPayload("1")));
    Snapshot snapshot = Snapshot.create(snapshotState, Collections.<ReplicatedLogEntry>emptyList(), -1, -1, -1, -1, electionTerm, electionVotedFor, serverPayload);
    SnapshotMetadata metadata = new SnapshotMetadata("test", 6, 12345);
    SnapshotOffer snapshotOffer = new SnapshotOffer(metadata, snapshot);
    sendMessageToSupport(snapshotOffer);
    assertEquals("Journal log size", 0, context.getReplicatedLog().size());
    assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm());
    assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor());
    assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
    assertEquals("Peer List", Sets.newHashSet("follower1", "follower2"), Sets.newHashSet(context.getPeerIds()));
}
Also used : MockSnapshotState(org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) ServerConfigurationPayload(org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload) SnapshotMetadata(akka.persistence.SnapshotMetadata) ServerInfo(org.opendaylight.controller.cluster.raft.persisted.ServerInfo) SnapshotOffer(akka.persistence.SnapshotOffer) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Test(org.junit.Test)

Example 5 with MockSnapshotState

use of org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState in project controller by opendaylight.

the class RaftActorRecoverySupportTest method testOnSnapshotOffer.

@Test
public void testOnSnapshotOffer() {
    ReplicatedLog replicatedLog = context.getReplicatedLog();
    replicatedLog.append(new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1")));
    replicatedLog.append(new SimpleReplicatedLogEntry(2, 1, new MockRaftActorContext.MockPayload("2")));
    replicatedLog.append(new SimpleReplicatedLogEntry(3, 1, new MockRaftActorContext.MockPayload("3")));
    ReplicatedLogEntry unAppliedEntry1 = new SimpleReplicatedLogEntry(4, 1, new MockRaftActorContext.MockPayload("4", 4));
    ReplicatedLogEntry unAppliedEntry2 = new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("5", 5));
    long lastAppliedDuringSnapshotCapture = 3;
    long lastIndexDuringSnapshotCapture = 5;
    long electionTerm = 2;
    String electionVotedFor = "member-2";
    MockSnapshotState snapshotState = new MockSnapshotState(Arrays.asList(new MockPayload("1")));
    Snapshot snapshot = Snapshot.create(snapshotState, Arrays.asList(unAppliedEntry1, unAppliedEntry2), lastIndexDuringSnapshotCapture, 1, lastAppliedDuringSnapshotCapture, 1, electionTerm, electionVotedFor, null);
    SnapshotMetadata metadata = new SnapshotMetadata("test", 6, 12345);
    SnapshotOffer snapshotOffer = new SnapshotOffer(metadata, snapshot);
    sendMessageToSupport(snapshotOffer);
    assertEquals("Journal log size", 2, context.getReplicatedLog().size());
    assertEquals("Journal data size", 9, context.getReplicatedLog().dataSize());
    assertEquals("Last index", lastIndexDuringSnapshotCapture, context.getReplicatedLog().lastIndex());
    assertEquals("Last applied", lastAppliedDuringSnapshotCapture, context.getLastApplied());
    assertEquals("Commit index", lastAppliedDuringSnapshotCapture, context.getCommitIndex());
    assertEquals("Snapshot term", 1, context.getReplicatedLog().getSnapshotTerm());
    assertEquals("Snapshot index", lastAppliedDuringSnapshotCapture, context.getReplicatedLog().getSnapshotIndex());
    assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm());
    assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor());
    assertFalse("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
    verify(mockCohort).applyRecoverySnapshot(snapshotState);
}
Also used : MockSnapshotState(org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SnapshotMetadata(akka.persistence.SnapshotMetadata) SnapshotOffer(akka.persistence.SnapshotOffer) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 MockSnapshotState (org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState)6 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)6 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)5 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)5 SnapshotMetadata (akka.persistence.SnapshotMetadata)3 SnapshotOffer (akka.persistence.SnapshotOffer)3 ByteString (com.google.protobuf.ByteString)3 ActorRef (akka.actor.ActorRef)2 TestActorRef (akka.testkit.TestActorRef)2 ArrayList (java.util.ArrayList)2 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)2 GetSnapshot (org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot)2 ApplyJournalEntries (org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries)2 FiniteDuration (scala.concurrent.duration.FiniteDuration)2 TestKit (akka.testkit.javadsl.TestKit)1 HashMap (java.util.HashMap)1 DataPersistenceProvider (org.opendaylight.controller.cluster.DataPersistenceProvider)1 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)1 Leader (org.opendaylight.controller.cluster.raft.behaviors.Leader)1