Search in sources :

Example 1 with SendHeartBeat

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

the class AbstractLeader method handleMessage.

@Override
public RaftActorBehavior handleMessage(final ActorRef sender, final Object message) {
    Preconditions.checkNotNull(sender, "sender should not be null");
    if (appendEntriesMessageSlicer.handleMessage(message)) {
        return this;
    }
    if (message instanceof RaftRPC) {
        RaftRPC rpc = (RaftRPC) message;
        // This applies to all RPC messages and responses
        if (rpc.getTerm() > context.getTermInformation().getCurrentTerm()) {
            log.info("{}: Term {} in \"{}\" message is greater than leader's term {} - switching to Follower", logName(), rpc.getTerm(), rpc, context.getTermInformation().getCurrentTerm());
            context.getTermInformation().updateAndPersist(rpc.getTerm(), null);
            // leadership, we should make every effort to get the requesting node elected.
            if (message instanceof RequestVote && context.getRaftActorLeadershipTransferCohort() != null) {
                log.debug("{}: Leadership transfer in progress - processing RequestVote", logName());
                super.handleMessage(sender, message);
            }
            return internalSwitchBehavior(RaftState.Follower);
        }
    }
    if (message instanceof SendHeartBeat) {
        beforeSendHeartbeat();
        sendHeartBeat();
        scheduleHeartBeat(context.getConfigParams().getHeartBeatInterval());
    } else if (message instanceof SendInstallSnapshot) {
        SendInstallSnapshot sendInstallSnapshot = (SendInstallSnapshot) message;
        setSnapshotHolder(new SnapshotHolder(sendInstallSnapshot.getSnapshot(), sendInstallSnapshot.getSnapshotBytes()));
        sendInstallSnapshot();
    } else if (message instanceof Replicate) {
        replicate((Replicate) message);
    } else if (message instanceof InstallSnapshotReply) {
        handleInstallSnapshotReply((InstallSnapshotReply) message);
    } else if (message instanceof CheckConsensusReached) {
        possiblyUpdateCommitIndex();
    } else {
        return super.handleMessage(sender, message);
    }
    return this;
}
Also used : RaftRPC(org.opendaylight.controller.cluster.raft.messages.RaftRPC) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Replicate(org.opendaylight.controller.cluster.raft.base.messages.Replicate) CheckConsensusReached(org.opendaylight.controller.cluster.raft.base.messages.CheckConsensusReached) InstallSnapshotReply(org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply) SendHeartBeat(org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote)

Example 2 with SendHeartBeat

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

the class LeaderTest method testLaggingFollowerStarvation.

@Test
public void testLaggingFollowerStarvation() throws Exception {
    logStart("testLaggingFollowerStarvation");
    String leaderActorId = actorFactory.generateActorId("leader");
    String follower1ActorId = actorFactory.generateActorId("follower");
    String follower2ActorId = actorFactory.generateActorId("follower");
    final ActorRef follower1Actor = actorFactory.createActor(MessageCollectorActor.props(), follower1ActorId);
    final ActorRef follower2Actor = actorFactory.createActor(MessageCollectorActor.props(), follower2ActorId);
    MockRaftActorContext leaderActorContext = new MockRaftActorContext(leaderActorId, getSystem(), leaderActor);
    DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
    configParams.setHeartBeatInterval(new FiniteDuration(200, TimeUnit.MILLISECONDS));
    configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(10, TimeUnit.SECONDS));
    leaderActorContext.setConfigParams(configParams);
    leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(1, 5, 1).build());
    Map<String, String> peerAddresses = new HashMap<>();
    peerAddresses.put(follower1ActorId, follower1Actor.path().toString());
    peerAddresses.put(follower2ActorId, follower2Actor.path().toString());
    leaderActorContext.setPeerAddresses(peerAddresses);
    leaderActorContext.getTermInformation().update(1, leaderActorId);
    leader = createBehavior(leaderActorContext);
    leaderActor.underlyingActor().setBehavior(leader);
    for (int i = 1; i < 6; i++) {
        // Each AppendEntriesReply could end up rescheduling the heartbeat (without the fix for bug 2733)
        RaftActorBehavior newBehavior = leader.handleMessage(follower1Actor, new AppendEntriesReply(follower1ActorId, 1, true, i, 1, (short) 0));
        assertTrue(newBehavior == leader);
        Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS);
    }
    // Check if the leader has been receiving SendHeartbeat messages despite getting AppendEntriesReply
    List<SendHeartBeat> heartbeats = MessageCollectorActor.getAllMatching(leaderActor, SendHeartBeat.class);
    assertTrue(String.format("%s heartbeat(s) is less than expected", heartbeats.size()), heartbeats.size() > 1);
    // Check if follower-2 got AppendEntries during this time and was not starved
    List<AppendEntries> appendEntries = MessageCollectorActor.getAllMatching(follower2Actor, AppendEntries.class);
    assertTrue(String.format("%s append entries is less than expected", appendEntries.size()), appendEntries.size() > 1);
}
Also used : HashMap(java.util.HashMap) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) SendHeartBeat(org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat) Test(org.junit.Test)

Example 3 with SendHeartBeat

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

the class AbstractLeaderTest method testLeaderSchedulesHeartbeatsEvenWhenNoFollowersRespondToInitialAppendEntries.

/**
 * When we removed scheduling of heartbeat in the AbstractLeader constructor we ended up with a situation where
 * if no follower responded to an initial AppendEntries heartbeats would not be sent to it. This test verifies
 * that regardless of whether followers respond or not we schedule heartbeats.
 */
@Test
public void testLeaderSchedulesHeartbeatsEvenWhenNoFollowersRespondToInitialAppendEntries() throws Exception {
    logStart("testLeaderSchedulesHeartbeatsEvenWhenNoFollowersRespondToInitialAppendEntries");
    String leaderActorId = actorFactory.generateActorId("leader");
    String follower1ActorId = actorFactory.generateActorId("follower");
    String follower2ActorId = actorFactory.generateActorId("follower");
    TestActorRef<ForwardMessageToBehaviorActor> leaderActor = actorFactory.createTestActor(ForwardMessageToBehaviorActor.props(), leaderActorId);
    final ActorRef follower1Actor = actorFactory.createActor(MessageCollectorActor.props(), follower1ActorId);
    final ActorRef follower2Actor = actorFactory.createActor(MessageCollectorActor.props(), follower2ActorId);
    MockRaftActorContext leaderActorContext = new MockRaftActorContext(leaderActorId, getSystem(), leaderActor);
    DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
    configParams.setHeartBeatInterval(new FiniteDuration(200, TimeUnit.MILLISECONDS));
    configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(10, TimeUnit.SECONDS));
    leaderActorContext.setConfigParams(configParams);
    leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(1, 5, 1).build());
    Map<String, String> peerAddresses = new HashMap<>();
    peerAddresses.put(follower1ActorId, follower1Actor.path().toString());
    peerAddresses.put(follower2ActorId, follower2Actor.path().toString());
    leaderActorContext.setPeerAddresses(peerAddresses);
    RaftActorBehavior leader = createBehavior(leaderActorContext);
    leaderActor.underlyingActor().setBehavior(leader);
    Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
    List<SendHeartBeat> allMessages = MessageCollectorActor.getAllMatching(leaderActor, SendHeartBeat.class);
    // Need more than 1 heartbeat to be delivered because we waited for 1 second with heartbeat interval 200ms
    assertTrue(String.format("%s messages is less than expected", allMessages.size()), allMessages.size() > 1);
}
Also used : HashMap(java.util.HashMap) TestActorRef(akka.testkit.TestActorRef) ActorRef(akka.actor.ActorRef) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) SendHeartBeat(org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat) ForwardMessageToBehaviorActor(org.opendaylight.controller.cluster.raft.utils.ForwardMessageToBehaviorActor) Test(org.junit.Test)

Aggregations

SendHeartBeat (org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat)3 ActorRef (akka.actor.ActorRef)2 TestActorRef (akka.testkit.TestActorRef)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)2 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)2 FiniteDuration (scala.concurrent.duration.FiniteDuration)2 ByteString (com.google.protobuf.ByteString)1 CheckConsensusReached (org.opendaylight.controller.cluster.raft.base.messages.CheckConsensusReached)1 Replicate (org.opendaylight.controller.cluster.raft.base.messages.Replicate)1 SendInstallSnapshot (org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot)1 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)1 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)1 InstallSnapshotReply (org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply)1 RaftRPC (org.opendaylight.controller.cluster.raft.messages.RaftRPC)1 RequestVote (org.opendaylight.controller.cluster.raft.messages.RequestVote)1 ForwardMessageToBehaviorActor (org.opendaylight.controller.cluster.raft.utils.ForwardMessageToBehaviorActor)1