use of org.opendaylight.controller.cluster.raft.messages.RaftRPC 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;
}
use of org.opendaylight.controller.cluster.raft.messages.RaftRPC 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);
}
use of org.opendaylight.controller.cluster.raft.messages.RaftRPC 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);
}
use of org.opendaylight.controller.cluster.raft.messages.RaftRPC in project controller by opendaylight.
the class FollowerTest method testElectionScheduledWhenAnyRaftRPCReceived.
@Test
public void testElectionScheduledWhenAnyRaftRPCReceived() {
MockRaftActorContext context = createActorContext();
follower = createBehavior(context);
follower.handleMessage(leaderActor, new RaftRPC() {
private static final long serialVersionUID = 1L;
@Override
public long getTerm() {
return 100;
}
});
verify(follower).scheduleElection(any(FiniteDuration.class));
}
Aggregations