Search in sources :

Example 6 with Follower

use of org.opendaylight.controller.cluster.raft.behaviors.Follower in project controller by opendaylight.

the class RaftActorServerConfigurationSupportTest method testAddServerWithExistingFollower.

@Test
public void testAddServerWithExistingFollower() throws Exception {
    LOG.info("testAddServerWithExistingFollower starting");
    setupNewFollower();
    RaftActorContextImpl followerActorContext = newFollowerContext(FOLLOWER_ID, followerActor);
    followerActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 3, 1).build());
    followerActorContext.setCommitIndex(2);
    followerActorContext.setLastApplied(2);
    Follower follower = new Follower(followerActorContext);
    followerActor.underlyingActor().setBehavior(follower);
    followerActorContext.setCurrentBehavior(follower);
    TestActorRef<MockLeaderRaftActor> leaderActor = actorFactory.createTestActor(MockLeaderRaftActor.props(ImmutableMap.of(FOLLOWER_ID, followerActor.path().toString()), followerActorContext).withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(LEADER_ID));
    // Expect initial heartbeat from the leader.
    expectFirstMatching(followerActor, AppendEntries.class);
    clearMessages(followerActor);
    MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor();
    final ActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor);
    leaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef());
    // Leader should install snapshot - capture and verify ApplySnapshot contents
    ApplySnapshot applySnapshot = expectFirstMatching(newFollowerCollectorActor, ApplySnapshot.class);
    List<Object> snapshotState = MockRaftActor.fromState(applySnapshot.getSnapshot().getState());
    assertEquals("Snapshot state", snapshotState, leaderRaftActor.getState());
    AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class);
    assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
    assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get());
    // Verify ServerConfigurationPayload entry in leader's log
    expectFirstMatching(leaderCollectorActor, ApplyState.class);
    RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext();
    assertEquals("Leader journal last index", 3, leaderActorContext.getReplicatedLog().lastIndex());
    assertEquals("Leader commit index", 3, leaderActorContext.getCommitIndex());
    assertEquals("Leader last applied index", 3, leaderActorContext.getLastApplied());
    verifyServerConfigurationPayloadEntry(leaderActorContext.getReplicatedLog(), votingServer(LEADER_ID), votingServer(FOLLOWER_ID), votingServer(NEW_SERVER_ID));
    // Verify ServerConfigurationPayload entry in both followers
    expectFirstMatching(followerActor, ApplyState.class);
    assertEquals("Follower journal last index", 3, followerActorContext.getReplicatedLog().lastIndex());
    verifyServerConfigurationPayloadEntry(followerActorContext.getReplicatedLog(), votingServer(LEADER_ID), votingServer(FOLLOWER_ID), votingServer(NEW_SERVER_ID));
    expectFirstMatching(newFollowerCollectorActor, ApplyState.class);
    assertEquals("New follower journal last index", 3, newFollowerActorContext.getReplicatedLog().lastIndex());
    verifyServerConfigurationPayloadEntry(newFollowerActorContext.getReplicatedLog(), votingServer(LEADER_ID), votingServer(FOLLOWER_ID), votingServer(NEW_SERVER_ID));
    // Verify new server config was applied in both followers
    assertEquals("Follower peers", Sets.newHashSet(LEADER_ID, NEW_SERVER_ID), followerActorContext.getPeerIds());
    assertEquals("New follower peers", Sets.newHashSet(LEADER_ID, FOLLOWER_ID), newFollowerActorContext.getPeerIds());
    assertEquals("Follower commit index", 3, followerActorContext.getCommitIndex());
    assertEquals("Follower last applied index", 3, followerActorContext.getLastApplied());
    assertEquals("New follower commit index", 3, newFollowerActorContext.getCommitIndex());
    assertEquals("New follower last applied index", 3, newFollowerActorContext.getLastApplied());
    assertEquals("Leader persisted ReplicatedLogImplEntry entries", 0, InMemoryJournal.get(LEADER_ID, SimpleReplicatedLogEntry.class).size());
    assertEquals("Leader persisted ServerConfigurationPayload entries", 1, InMemoryJournal.get(LEADER_ID, ServerConfigurationPayload.class).size());
    assertEquals("New follower persisted ReplicatedLogImplEntry entries", 0, InMemoryJournal.get(NEW_SERVER_ID, SimpleReplicatedLogEntry.class).size());
    assertEquals("New follower persisted ServerConfigurationPayload entries", 1, InMemoryJournal.get(NEW_SERVER_ID, ServerConfigurationPayload.class).size());
    LOG.info("testAddServerWithExistingFollower ending");
}
Also used : ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) Follower(org.opendaylight.controller.cluster.raft.behaviors.Follower) AddServer(org.opendaylight.controller.cluster.raft.messages.AddServer) AddServerReply(org.opendaylight.controller.cluster.raft.messages.AddServerReply) Test(org.junit.Test)

Example 7 with Follower

use of org.opendaylight.controller.cluster.raft.behaviors.Follower 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 8 with Follower

use of org.opendaylight.controller.cluster.raft.behaviors.Follower in project controller by opendaylight.

the class RaftActorTest method testRaftRoleChangeNotifierWhenRaftActorHasNoPeers.

@Test
public void testRaftRoleChangeNotifierWhenRaftActorHasNoPeers() throws Exception {
    ActorRef notifierActor = factory.createActor(MessageCollectorActor.props());
    MessageCollectorActor.waitUntilReady(notifierActor);
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    long heartBeatInterval = 100;
    config.setHeartBeatInterval(FiniteDuration.create(heartBeatInterval, TimeUnit.MILLISECONDS));
    config.setElectionTimeoutFactor(20);
    String persistenceId = factory.generateActorId("notifier-");
    final TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).roleChangeNotifier(notifierActor).dataPersistenceProvider(createProvider()).props().withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
    List<RoleChanged> matches = MessageCollectorActor.expectMatching(notifierActor, RoleChanged.class, 3);
    // check if the notifier got a role change from null to Follower
    RoleChanged raftRoleChanged = matches.get(0);
    assertEquals(persistenceId, raftRoleChanged.getMemberId());
    assertNull(raftRoleChanged.getOldRole());
    assertEquals(RaftState.Follower.name(), raftRoleChanged.getNewRole());
    // check if the notifier got a role change from Follower to Candidate
    raftRoleChanged = matches.get(1);
    assertEquals(persistenceId, raftRoleChanged.getMemberId());
    assertEquals(RaftState.Follower.name(), raftRoleChanged.getOldRole());
    assertEquals(RaftState.Candidate.name(), raftRoleChanged.getNewRole());
    // check if the notifier got a role change from Candidate to Leader
    raftRoleChanged = matches.get(2);
    assertEquals(persistenceId, raftRoleChanged.getMemberId());
    assertEquals(RaftState.Candidate.name(), raftRoleChanged.getOldRole());
    assertEquals(RaftState.Leader.name(), raftRoleChanged.getNewRole());
    LeaderStateChanged leaderStateChange = MessageCollectorActor.expectFirstMatching(notifierActor, LeaderStateChanged.class);
    assertEquals(raftRoleChanged.getMemberId(), leaderStateChange.getLeaderId());
    assertEquals(MockRaftActor.PAYLOAD_VERSION, leaderStateChange.getLeaderPayloadVersion());
    MessageCollectorActor.clearMessages(notifierActor);
    MockRaftActor raftActor = raftActorRef.underlyingActor();
    final String newLeaderId = "new-leader";
    final short newLeaderVersion = 6;
    Follower follower = new Follower(raftActor.getRaftActorContext()) {

        @Override
        public RaftActorBehavior handleMessage(final ActorRef sender, final Object message) {
            setLeaderId(newLeaderId);
            setLeaderPayloadVersion(newLeaderVersion);
            return this;
        }
    };
    raftActor.newBehavior(follower);
    leaderStateChange = MessageCollectorActor.expectFirstMatching(notifierActor, LeaderStateChanged.class);
    assertEquals(persistenceId, leaderStateChange.getMemberId());
    assertEquals(null, leaderStateChange.getLeaderId());
    raftRoleChanged = MessageCollectorActor.expectFirstMatching(notifierActor, RoleChanged.class);
    assertEquals(RaftState.Leader.name(), raftRoleChanged.getOldRole());
    assertEquals(RaftState.Follower.name(), raftRoleChanged.getNewRole());
    MessageCollectorActor.clearMessages(notifierActor);
    raftActor.handleCommand("any");
    leaderStateChange = MessageCollectorActor.expectFirstMatching(notifierActor, LeaderStateChanged.class);
    assertEquals(persistenceId, leaderStateChange.getMemberId());
    assertEquals(newLeaderId, leaderStateChange.getLeaderId());
    assertEquals(newLeaderVersion, leaderStateChange.getLeaderPayloadVersion());
    MessageCollectorActor.clearMessages(notifierActor);
    raftActor.handleCommand("any");
    Uninterruptibles.sleepUninterruptibly(505, TimeUnit.MILLISECONDS);
    leaderStateChange = MessageCollectorActor.getFirstMatching(notifierActor, LeaderStateChanged.class);
    assertNull(leaderStateChange);
}
Also used : ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) Follower(org.opendaylight.controller.cluster.raft.behaviors.Follower) Matchers.anyObject(org.mockito.Matchers.anyObject) ByteString(com.google.protobuf.ByteString) RoleChanged(org.opendaylight.controller.cluster.notifications.RoleChanged) LeaderStateChanged(org.opendaylight.controller.cluster.notifications.LeaderStateChanged) Test(org.junit.Test)

Aggregations

Follower (org.opendaylight.controller.cluster.raft.behaviors.Follower)8 ActorRef (akka.actor.ActorRef)7 TestActorRef (akka.testkit.TestActorRef)7 Test (org.junit.Test)7 AddServer (org.opendaylight.controller.cluster.raft.messages.AddServer)5 AddServerReply (org.opendaylight.controller.cluster.raft.messages.AddServerReply)5 ByteString (com.google.protobuf.ByteString)2 TestKit (akka.testkit.javadsl.TestKit)1 HashMap (java.util.HashMap)1 Matchers.anyObject (org.mockito.Matchers.anyObject)1 DataPersistenceProvider (org.opendaylight.controller.cluster.DataPersistenceProvider)1 LeaderStateChanged (org.opendaylight.controller.cluster.notifications.LeaderStateChanged)1 RoleChanged (org.opendaylight.controller.cluster.notifications.RoleChanged)1 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)1 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)1 CaptureSnapshotReply (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply)1 InitiateCaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.InitiateCaptureSnapshot)1 AbstractRaftActorBehavior (org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior)1 RaftActorBehavior (org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior)1 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)1