Search in sources :

Example 1 with TimeoutNow

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

the class Follower method handleMessage.

@Override
public RaftActorBehavior handleMessage(final ActorRef sender, final Object message) {
    if (message instanceof ElectionTimeout || message instanceof TimeoutNow) {
        return handleElectionTimeout(message);
    }
    if (appendEntriesMessageAssembler.handleMessage(message, actor())) {
        return this;
    }
    if (!(message instanceof RaftRPC)) {
        // The rest of the processing requires the message to be a RaftRPC
        return null;
    }
    final 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 follower's term {} - updating term", logName(), rpc.getTerm(), rpc, context.getTermInformation().getCurrentTerm());
        context.getTermInformation().updateAndPersist(rpc.getTerm(), null);
    }
    if (rpc instanceof InstallSnapshot) {
        handleInstallSnapshot(sender, (InstallSnapshot) rpc);
        restartLastLeaderMessageTimer();
        scheduleElection(electionDuration());
        return this;
    }
    if (!(rpc instanceof RequestVote) || canGrantVote((RequestVote) rpc)) {
        restartLastLeaderMessageTimer();
        scheduleElection(electionDuration());
    }
    return super.handleMessage(sender, rpc);
}
Also used : RaftRPC(org.opendaylight.controller.cluster.raft.messages.RaftRPC) TimeoutNow(org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow) ElectionTimeout(org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)

Example 2 with TimeoutNow

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

the class FollowerTest method testFollowerSchedulesElectionTimeoutImmediatelyWhenItHasNoPeers.

@Test
public void testFollowerSchedulesElectionTimeoutImmediatelyWhenItHasNoPeers() {
    MockRaftActorContext context = createActorContext();
    Stopwatch stopwatch = Stopwatch.createStarted();
    follower = createBehavior(context);
    TimeoutNow timeoutNow = MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class);
    long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
    assertTrue(elapsed < context.getConfigParams().getElectionTimeOutInterval().toMillis());
    RaftActorBehavior newBehavior = follower.handleMessage(ActorRef.noSender(), timeoutNow);
    assertTrue("Expected Candidate", newBehavior instanceof Candidate);
}
Also used : TimeoutNow(org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) Stopwatch(com.google.common.base.Stopwatch) Test(org.junit.Test)

Example 3 with TimeoutNow

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

the class FollowerTest method testFollowerSchedulesElectionIfAutomaticElectionsAreDisabled.

@Test
public void testFollowerSchedulesElectionIfAutomaticElectionsAreDisabled() {
    MockRaftActorContext context = createActorContext();
    context.setConfigParams(new DefaultConfigParamsImpl() {

        @Override
        public FiniteDuration getElectionTimeOutInterval() {
            return FiniteDuration.apply(100, TimeUnit.MILLISECONDS);
        }
    });
    context.setRaftPolicy(createRaftPolicy(false, false));
    follower = createBehavior(context);
    TimeoutNow timeoutNow = MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class);
    RaftActorBehavior newBehavior = follower.handleMessage(ActorRef.noSender(), timeoutNow);
    assertSame("handleMessage result", follower, newBehavior);
}
Also used : TimeoutNow(org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) Test(org.junit.Test)

Aggregations

TimeoutNow (org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow)3 Test (org.junit.Test)2 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)2 Stopwatch (com.google.common.base.Stopwatch)1 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)1 ElectionTimeout (org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout)1 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)1 RaftRPC (org.opendaylight.controller.cluster.raft.messages.RaftRPC)1 RequestVote (org.opendaylight.controller.cluster.raft.messages.RequestVote)1 FiniteDuration (scala.concurrent.duration.FiniteDuration)1