X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fbehaviors%2FFollower.java;h=1a721335068336c7b9d00e95da720d2fba48ee80;hb=d3e310b940b60f6590f0e94a576aece95a055942;hp=8650d96f60b5b9b64f3bb69e4f8f649cdccab617;hpb=9d5ec5cdd146a56bc03e35b6718e9492a5c8410a;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Follower.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Follower.java index 8650d96f60..1a72133506 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Follower.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Follower.java @@ -9,10 +9,18 @@ package org.opendaylight.controller.cluster.raft.behaviors; import akka.actor.ActorRef; +import akka.actor.ActorSelection; +import akka.actor.Address; +import akka.cluster.Cluster; +import akka.cluster.ClusterEvent.CurrentClusterState; +import akka.cluster.Member; +import akka.cluster.MemberStatus; import akka.japi.Procedure; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Stopwatch; import java.util.ArrayList; +import java.util.Optional; +import java.util.Set; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; import org.opendaylight.controller.cluster.raft.RaftActorContext; @@ -44,16 +52,14 @@ import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPay public class Follower extends AbstractRaftActorBehavior { private static final int SYNC_THRESHOLD = 10; + private static final long MAX_ELECTION_TIMEOUT_FACTOR = 18; + private final SyncStatusTracker initialSyncStatusTracker; - private final Procedure appendAndPersistCallback = new Procedure() { - @Override - public void apply(ReplicatedLogEntry logEntry) { - context.getReplicatedLog().captureSnapshotIfReady(logEntry); - } - }; + private final Procedure appendAndPersistCallback = + logEntry -> context.getReplicatedLog().captureSnapshotIfReady(logEntry); - private final Stopwatch lastLeaderMessageTimer = Stopwatch.createUnstarted(); + private final Stopwatch lastLeaderMessageTimer = Stopwatch.createStarted(); private SnapshotTracker snapshotTracker = null; private String leaderId; private short leaderPayloadVersion; @@ -132,6 +138,12 @@ public class Follower extends AbstractRaftActorBehavior { // to make it easier to read. Before refactoring ensure tests // cover the code properly + if (snapshotTracker != null && !snapshotTracker.getLeaderId().equals(appendEntries.getLeaderId())) { + LOG.debug("{}: snapshot install is in progress but the prior snapshot leaderId {} does not match the " + + "AppendEntries leaderId {}", logName(), snapshotTracker.getLeaderId(), appendEntries.getLeaderId()); + snapshotTracker = null; + } + if (snapshotTracker != null || context.getSnapshotManager().isApplying()) { // if snapshot install is in progress, follower should just acknowledge append entries with a reply. AppendEntriesReply reply = new AppendEntriesReply(context.getId(), currentTerm(), true, @@ -354,12 +366,11 @@ public class Follower extends AbstractRaftActorBehavior { } @Override - public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) { - if (originalMessage instanceof ElectionTimeout || originalMessage instanceof TimeoutNow) { - return handleElectionTimeout(originalMessage); + public RaftActorBehavior handleMessage(ActorRef sender, Object message) { + if (message instanceof ElectionTimeout || message instanceof TimeoutNow) { + return handleElectionTimeout(message); } - final Object message = fromSerializableMessage(originalMessage); if (!(message instanceof RaftRPC)) { // The rest of the processing requires the message to be a RaftRPC return null; @@ -398,16 +409,30 @@ public class Follower extends AbstractRaftActorBehavior { // queue but would be processed before the ElectionTimeout message and thus would restart the // lastLeaderMessageTimer. long lastLeaderMessageInterval = lastLeaderMessageTimer.elapsed(TimeUnit.MILLISECONDS); - boolean noLeaderMessageReceived = !lastLeaderMessageTimer.isRunning() || lastLeaderMessageInterval >= - context.getConfigParams().getElectionTimeOutInterval().toMillis(); + long electionTimeoutInMillis = context.getConfigParams().getElectionTimeOutInterval().toMillis(); + boolean noLeaderMessageReceived = !lastLeaderMessageTimer.isRunning() || + lastLeaderMessageInterval >= electionTimeoutInMillis; if(canStartElection()) { - if(message instanceof TimeoutNow || noLeaderMessageReceived) { - LOG.debug("{}: Received {} - switching to Candidate", logName(), message.getClass().getSimpleName()); + if(message instanceof TimeoutNow) { + LOG.debug("{}: Received TimeoutNow - switching to Candidate", logName()); return internalSwitchBehavior(RaftState.Candidate); + } else if(noLeaderMessageReceived) { + // Check the cluster state to see if the leader is known to be up before we go to Candidate. + // However if we haven't heard from the leader in a long time even though the cluster state + // indicates it's up then something is wrong - leader might be stuck indefinitely - so switch + // to Candidate, + long maxElectionTimeout = electionTimeoutInMillis * MAX_ELECTION_TIMEOUT_FACTOR; + if(isLeaderAvailabilityKnown() && lastLeaderMessageInterval < maxElectionTimeout) { + LOG.debug("{}: Received ElectionTimeout but leader appears to be available", logName()); + scheduleElection(electionDuration()); + } else { + LOG.debug("{}: Received ElectionTimeout - switching to Candidate", logName()); + return internalSwitchBehavior(RaftState.Candidate); + } } else { - LOG.debug("{}: Received ElectionTimeout but lastLeaderMessageInterval {} < election timeout", - logName(), lastLeaderMessageInterval); + LOG.debug("{}: Received ElectionTimeout but lastLeaderMessageInterval {} < election timeout {}", + logName(), lastLeaderMessageInterval, context.getConfigParams().getElectionTimeOutInterval()); scheduleElection(electionDuration()); } } else if(message instanceof ElectionTimeout) { @@ -421,6 +446,55 @@ public class Follower extends AbstractRaftActorBehavior { return this; } + private boolean isLeaderAvailabilityKnown() { + if(leaderId == null) { + return false; + } + + Optional cluster = context.getCluster(); + if(!cluster.isPresent()) { + return false; + } + + ActorSelection leaderActor = context.getPeerActorSelection(leaderId); + if(leaderActor == null) { + return false; + } + + Address leaderAddress = leaderActor.anchorPath().address(); + + CurrentClusterState state = cluster.get().state(); + Set unreachable = state.getUnreachable(); + + LOG.debug("{}: Checking for leader {} in the cluster unreachable set {}", logName(), leaderAddress, + unreachable); + + for(Member m: unreachable) { + if(leaderAddress.equals(m.address())) { + LOG.info("{}: Leader {} is unreachable", logName(), leaderAddress); + return false; + } + } + + for(Member m: state.getMembers()) { + if(leaderAddress.equals(m.address())) { + if(m.status() == MemberStatus.up() || m.status() == MemberStatus.weaklyUp()) { + LOG.debug("{}: Leader {} cluster status is {} - leader is available", logName(), + leaderAddress, m.status()); + return true; + } else { + LOG.debug("{}: Leader {} cluster status is {} - leader is unavailable", logName(), + leaderAddress, m.status()); + return false; + } + } + } + + LOG.debug("{}: Leader {} not found in the cluster member set", logName(), leaderAddress); + + return false; + } + private void handleInstallSnapshot(final ActorRef sender, InstallSnapshot installSnapshot) { LOG.debug("{}: handleInstallSnapshot: {}", logName(), installSnapshot); @@ -428,7 +502,7 @@ public class Follower extends AbstractRaftActorBehavior { leaderId = installSnapshot.getLeaderId(); if(snapshotTracker == null){ - snapshotTracker = new SnapshotTracker(LOG, installSnapshot.getTotalChunks()); + snapshotTracker = new SnapshotTracker(LOG, installSnapshot.getTotalChunks(), installSnapshot.getLeaderId()); } updateInitialSyncStatus(installSnapshot.getLastIncludedIndex(), installSnapshot.getLeaderId());