Search in sources :

Example 1 with RequestVote

use of org.opendaylight.controller.cluster.raft.messages.RequestVote 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 RequestVote

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

the class Candidate method startNewTerm.

private void startNewTerm() {
    // set voteCount back to 1 (that is voting for self)
    voteCount = 1;
    // Increment the election term and vote for self
    long currentTerm = context.getTermInformation().getCurrentTerm();
    long newTerm = currentTerm + 1;
    context.getTermInformation().updateAndPersist(newTerm, context.getId());
    log.info("{}: Starting new election term {}", logName(), newTerm);
    // amount of time TBD
    for (String peerId : votingPeers) {
        ActorSelection peerActor = context.getPeerActorSelection(peerId);
        if (peerActor != null) {
            RequestVote requestVote = new RequestVote(context.getTermInformation().getCurrentTerm(), context.getId(), context.getReplicatedLog().lastIndex(), context.getReplicatedLog().lastTerm());
            log.debug("{}: Sending {} to peer {}", logName(), requestVote, peerId);
            peerActor.tell(requestVote, context.getActor());
        }
    }
}
Also used : ActorSelection(akka.actor.ActorSelection) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote)

Example 3 with RequestVote

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

the class Candidate method handleMessage.

@Override
public RaftActorBehavior handleMessage(ActorRef sender, Object message) {
    if (message instanceof ElectionTimeout) {
        log.debug("{}: Received ElectionTimeout", logName());
        if (votesRequired == 0) {
            return internalSwitchBehavior(RaftState.Leader);
        }
        startNewTerm();
        scheduleElection(electionDuration());
        return this;
    }
    if (message instanceof RaftRPC) {
        RaftRPC rpc = (RaftRPC) message;
        log.debug("{}: RaftRPC message received {}, my term is {}", logName(), rpc, context.getTermInformation().getCurrentTerm());
        // This applies to all RPC messages and responses
        if (rpc.getTerm() > context.getTermInformation().getCurrentTerm()) {
            log.info("{}: Term {} in \"{}\" message is greater than Candidate's term {} - switching to Follower", logName(), rpc.getTerm(), rpc, context.getTermInformation().getCurrentTerm());
            context.getTermInformation().updateAndPersist(rpc.getTerm(), null);
            // this case but doing so gains quicker convergence when the sender's log is more up-to-date.
            if (message instanceof RequestVote) {
                super.handleMessage(sender, message);
            }
            return internalSwitchBehavior(RaftState.Follower);
        }
    }
    return super.handleMessage(sender, message);
}
Also used : RaftRPC(org.opendaylight.controller.cluster.raft.messages.RaftRPC) ElectionTimeout(org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote)

Example 4 with RequestVote

use of org.opendaylight.controller.cluster.raft.messages.RequestVote 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 5 with RequestVote

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

the class DummyShard method onReceive.

@Override
public void onReceive(Object message) throws Exception {
    if (message instanceof RequestVote) {
        RequestVote req = (RequestVote) message;
        sender().tell(new RequestVoteReply(req.getTerm(), true), self());
    } else if (message instanceof AppendEntries) {
        handleAppendEntries((AppendEntries) message);
    } else if (message instanceof InstallSnapshot) {
        handleInstallSnapshot((InstallSnapshot) message);
    } else {
        LOG.error("Unknown message : {}", message.getClass());
    }
}
Also used : RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)

Aggregations

RequestVote (org.opendaylight.controller.cluster.raft.messages.RequestVote)17 Test (org.junit.Test)11 RequestVoteReply (org.opendaylight.controller.cluster.raft.messages.RequestVoteReply)10 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)8 AbstractActorTest (org.opendaylight.controller.cluster.raft.AbstractActorTest)3 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)3 RaftRPC (org.opendaylight.controller.cluster.raft.messages.RaftRPC)3 FiniteDuration (scala.concurrent.duration.FiniteDuration)3 ActorRef (akka.actor.ActorRef)2 TestActorRef (akka.testkit.TestActorRef)2 ElectionTimeout (org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout)2 TimeoutNow (org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow)2 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)2 ActorSelection (akka.actor.ActorSelection)1 Props (akka.actor.Props)1 UntypedActor (akka.actor.UntypedActor)1 Dispatchers (akka.dispatch.Dispatchers)1 TestKit (akka.testkit.javadsl.TestKit)1 Optional (com.google.common.base.Optional)1 Stopwatch (com.google.common.base.Stopwatch)1