Search in sources :

Example 46 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class RaftActorTest method testFakeSnapshotsForFollowerWithInRealSnapshots.

@Test
public void testFakeSnapshotsForFollowerWithInRealSnapshots() throws Exception {
    final String persistenceId = factory.generateActorId("follower-");
    final String leaderId = factory.generateActorId("leader-");
    ActorRef leaderActor1 = 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(leaderId, leaderActor1.path().toString());
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, peerAddresses, config, dataPersistenceProvider), persistenceId);
    MockRaftActor followerActor = mockActorRef.underlyingActor();
    followerActor.getRaftActorContext().setCommitIndex(4);
    followerActor.getRaftActorContext().setLastApplied(4);
    followerActor.getRaftActorContext().getTermInformation().update(1, persistenceId);
    followerActor.waitForInitializeBehaviorComplete();
    Follower follower = new Follower(followerActor.getRaftActorContext());
    followerActor.setCurrentBehavior(follower);
    assertEquals(RaftState.Follower, followerActor.getCurrentBehavior().state());
    // create 6 entries in the log - 0 to 4 are applied and will get picked up as part of the capture snapshot
    MockRaftActorContext.MockReplicatedLogBuilder logBuilder = new MockRaftActorContext.MockReplicatedLogBuilder();
    followerActor.getRaftActorContext().setReplicatedLog(logBuilder.createEntries(0, 6, 1).build());
    // log has indices 0-5
    assertEquals(6, followerActor.getReplicatedLog().size());
    // snapshot on 4
    followerActor.getRaftActorContext().getSnapshotManager().capture(new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("D")), 4);
    verify(followerActor.snapshotCohortDelegate).createSnapshot(anyObject(), anyObject());
    assertEquals(6, followerActor.getReplicatedLog().size());
    // fake snapshot on index 6
    List<ReplicatedLogEntry> entries = Arrays.asList((ReplicatedLogEntry) new SimpleReplicatedLogEntry(6, 1, new MockRaftActorContext.MockPayload("foo-6")));
    followerActor.onReceiveCommand(new AppendEntries(1, leaderId, 5, 1, entries, 5, 5, (short) 0));
    assertEquals(7, followerActor.getReplicatedLog().size());
    // fake snapshot on index 7
    assertEquals(RaftState.Follower, followerActor.getCurrentBehavior().state());
    entries = Arrays.asList((ReplicatedLogEntry) new SimpleReplicatedLogEntry(7, 1, new MockRaftActorContext.MockPayload("foo-7")));
    followerActor.onReceiveCommand(new AppendEntries(1, leaderId, 6, 1, entries, 6, 6, (short) 0));
    assertEquals(8, followerActor.getReplicatedLog().size());
    assertEquals(RaftState.Follower, followerActor.getCurrentBehavior().state());
    ByteString snapshotBytes = fromObject(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")));
    followerActor.onReceiveCommand(new CaptureSnapshotReply(ByteState.of(snapshotBytes.toByteArray()), java.util.Optional.empty()));
    assertTrue(followerActor.getRaftActorContext().getSnapshotManager().isCapturing());
    // The commit is needed to complete the snapshot creation process
    followerActor.getRaftActorContext().getSnapshotManager().commit(-1, -1);
    // capture snapshot reply should remove the snapshotted entries only till replicatedToAllIndex
    // indexes 5,6,7 left in the log
    assertEquals(3, followerActor.getReplicatedLog().size());
    assertEquals(7, followerActor.getReplicatedLog().lastIndex());
    entries = Arrays.asList((ReplicatedLogEntry) new SimpleReplicatedLogEntry(8, 1, new MockRaftActorContext.MockPayload("foo-7")));
    // send an additional entry 8 with leaderCommit = 7
    followerActor.onReceiveCommand(new AppendEntries(1, leaderId, 7, 1, entries, 7, 7, (short) 0));
    // 7 and 8, as lastapplied is 7
    assertEquals(2, followerActor.getReplicatedLog().size());
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) HashMap(java.util.HashMap) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) ByteString(com.google.protobuf.ByteString) FiniteDuration(scala.concurrent.duration.FiniteDuration) Follower(org.opendaylight.controller.cluster.raft.behaviors.Follower) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) Test(org.junit.Test)

Example 47 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class RaftActorTest method testLeaderTransitioning.

@Test
public void testLeaderTransitioning() throws Exception {
    TEST_LOG.info("testLeaderTransitioning starting");
    ActorRef notifierActor = factory.createActor(MessageCollectorActor.props());
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
    String persistenceId = factory.generateActorId("test-actor-");
    TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).roleChangeNotifier(notifierActor).props().withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
    MockRaftActor mockRaftActor = raftActorRef.underlyingActor();
    mockRaftActor.waitForInitializeBehaviorComplete();
    raftActorRef.tell(new AppendEntries(1L, "leader", 0L, 1L, Collections.<ReplicatedLogEntry>emptyList(), 0L, -1L, (short) 1), ActorRef.noSender());
    LeaderStateChanged leaderStateChange = MessageCollectorActor.expectFirstMatching(notifierActor, LeaderStateChanged.class);
    assertEquals("getLeaderId", "leader", leaderStateChange.getLeaderId());
    MessageCollectorActor.clearMessages(notifierActor);
    raftActorRef.tell(new LeaderTransitioning("leader"), ActorRef.noSender());
    leaderStateChange = MessageCollectorActor.expectFirstMatching(notifierActor, LeaderStateChanged.class);
    assertEquals("getMemberId", persistenceId, leaderStateChange.getMemberId());
    assertEquals("getLeaderId", null, leaderStateChange.getLeaderId());
    TEST_LOG.info("testLeaderTransitioning ending");
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) LeaderTransitioning(org.opendaylight.controller.cluster.raft.base.messages.LeaderTransitioning) LeaderStateChanged(org.opendaylight.controller.cluster.notifications.LeaderStateChanged) Test(org.junit.Test)

Example 48 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class RaftActorTest method testReplicateWithBatchHint.

@Test
public void testReplicateWithBatchHint() throws Exception {
    final String leaderId = factory.generateActorId("leader-");
    final String followerId = factory.generateActorId("follower-");
    final ActorRef followerActor = factory.createActor(MessageCollectorActor.props());
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
    TestActorRef<MockRaftActor> leaderActorRef = factory.createTestActor(MockRaftActor.props(leaderId, ImmutableMap.of(followerId, followerActor.path().toString()), config), leaderId);
    MockRaftActor leaderActor = leaderActorRef.underlyingActor();
    leaderActor.waitForInitializeBehaviorComplete();
    leaderActor.getRaftActorContext().getTermInformation().update(1, leaderId);
    Leader leader = new Leader(leaderActor.getRaftActorContext());
    leaderActor.setCurrentBehavior(leader);
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    MessageCollectorActor.clearMessages(followerActor);
    leaderActor.onReceiveCommand(new AppendEntriesReply(followerId, 1, true, -1, -1, (short) 0));
    leaderActor.persistData(leaderActorRef, new MockIdentifier("1"), new MockPayload("1"), true);
    MessageCollectorActor.assertNoneMatching(followerActor, AppendEntries.class, 500);
    leaderActor.persistData(leaderActorRef, new MockIdentifier("2"), new MockPayload("2"), true);
    MessageCollectorActor.assertNoneMatching(followerActor, AppendEntries.class, 500);
    leaderActor.persistData(leaderActorRef, new MockIdentifier("3"), new MockPayload("3"), false);
    AppendEntries appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertEquals("AppendEntries size", 3, appendEntries.getEntries().size());
}
Also used : Leader(org.opendaylight.controller.cluster.raft.behaviors.Leader) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Test(org.junit.Test)

Example 49 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class AbstractRaftActorBehaviorTest method testHandleAppendEntriesAddSameEntryToLog.

@Test
public void testHandleAppendEntriesAddSameEntryToLog() {
    MockRaftActorContext context = createActorContext();
    context.getTermInformation().update(2, "test");
    // Prepare the receivers log
    MockRaftActorContext.MockPayload payload = new MockRaftActorContext.MockPayload("zero");
    setLastLogEntry(context, 2, 0, payload);
    List<ReplicatedLogEntry> entries = new ArrayList<>();
    entries.add(new SimpleReplicatedLogEntry(0, 2, payload));
    final AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 2, -1, (short) 0);
    behavior = createBehavior(context);
    assertFalse("This test should be overridden when testing Candidate", behavior instanceof Candidate);
    RaftState expected = behavior.state();
    // Check that the behavior does not handle unknwon message
    assertNull(behavior.handleMessage(behaviorActor, "unknown"));
    RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
    assertEquals("Raft state", expected, raftBehavior.state());
    assertEquals("ReplicatedLog size", 1, context.getReplicatedLog().size());
    handleAppendEntriesAddSameEntryToLogReply(behaviorActor);
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) RaftState(org.opendaylight.controller.cluster.raft.RaftState) ArrayList(java.util.ArrayList) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) AbstractActorTest(org.opendaylight.controller.cluster.raft.AbstractActorTest) Test(org.junit.Test)

Example 50 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class AbstractRaftActorBehaviorTest method testHandleAppendEntriesSenderTermLessThanReceiverTerm.

/**
 * This test verifies that when an AppendEntries is received with a term that
 * is less that the currentTerm of the RaftActor then the RaftActor does not
 * change it's state and it responds back with a failure.
 */
@Test
public void testHandleAppendEntriesSenderTermLessThanReceiverTerm() {
    MockRaftActorContext context = createActorContext();
    short payloadVersion = 5;
    context.setPayloadVersion(payloadVersion);
    // First set the receivers term to a high number (1000)
    context.getTermInformation().update(1000, "test");
    AppendEntries appendEntries = new AppendEntries(100, "leader-1", 0, 0, Collections.emptyList(), 101, -1, (short) 4);
    behavior = createBehavior(context);
    RaftState expected = behavior.state();
    RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
    assertEquals("Raft state", expected, raftBehavior.state());
    // Also expect an AppendEntriesReply to be sent where success is false
    AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor, AppendEntriesReply.class);
    assertEquals("isSuccess", false, reply.isSuccess());
    assertEquals("getPayloadVersion", payloadVersion, reply.getPayloadVersion());
}
Also used : AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) RaftState(org.opendaylight.controller.cluster.raft.RaftState) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) AbstractActorTest(org.opendaylight.controller.cluster.raft.AbstractActorTest) Test(org.junit.Test)

Aggregations

AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)62 Test (org.junit.Test)58 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)44 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)34 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)34 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)30 FiniteDuration (scala.concurrent.duration.FiniteDuration)19 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)17 ByteString (com.google.protobuf.ByteString)15 ActorRef (akka.actor.ActorRef)9 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)9 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)9 TestActorRef (akka.testkit.TestActorRef)8 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)5 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)5 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)4 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)4 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)4 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)4