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%2FAbstractLeader.java;h=4993d25f202ecee0be55f7ec17b26528924a2e67;hb=refs%2Fchanges%2F45%2F14445%2F3;hp=e5c5dc752d3a257e9ce2f5852bf3350b006b8116;hpb=191ad68d71f30f6ad6258ac458c60c663e5b1b85;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java index e5c5dc752d..4993d25f20 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java @@ -14,16 +14,18 @@ import akka.actor.Cancellable; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; import com.google.protobuf.ByteString; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; +import java.util.Map.Entry; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; import org.opendaylight.controller.cluster.raft.ClientRequestTracker; import org.opendaylight.controller.cluster.raft.ClientRequestTrackerImpl; import org.opendaylight.controller.cluster.raft.FollowerLogInformation; @@ -77,10 +79,8 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { // This would be passed as the hash code of the last chunk when sending the first chunk public static final int INITIAL_LAST_CHUNK_HASH_CODE = -1; - protected final Map followerToLog = new HashMap<>(); - protected final Map mapFollowerToSnapshot = new HashMap<>(); - - protected final Set followers; + private final Map followerToLog; + private final Map mapFollowerToSnapshot = new HashMap<>(); private Cancellable heartbeatSchedule = null; @@ -95,25 +95,22 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { public AbstractLeader(RaftActorContext context) { super(context); - followers = context.getPeerAddresses().keySet(); - - for (String followerId : followers) { + final Builder ftlBuilder = ImmutableMap.builder(); + for (String followerId : context.getPeerAddresses().keySet()) { FollowerLogInformation followerLogInformation = new FollowerLogInformationImpl(followerId, - new AtomicLong(context.getCommitIndex()), - new AtomicLong(-1), + context.getCommitIndex(), -1, context.getConfigParams().getElectionTimeOutInterval()); - followerToLog.put(followerId, followerLogInformation); + ftlBuilder.put(followerId, followerLogInformation); } + followerToLog = ftlBuilder.build(); leaderId = context.getId(); - if(LOG.isDebugEnabled()) { - LOG.debug("Election:Leader has following peers: {}", followers); - } + LOG.debug("Election:Leader has following peers: {}", getFollowerIds()); - minReplicationCount = getMajorityVoteCount(followers.size()); + minReplicationCount = getMajorityVoteCount(getFollowerIds().size()); // the isolated Leader peer count will be 1 less than the majority vote count. // this is because the vote count has the self vote counted in it @@ -132,6 +129,15 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { scheduleHeartBeat(new FiniteDuration(0, TimeUnit.SECONDS)); } + /** + * Return an immutable collection of follower identifiers. + * + * @return Collection of follower IDs + */ + protected final Collection getFollowerIds() { + return followerToLog.keySet(); + } + private Optional getSnapshot() { return snapshot; } @@ -376,7 +382,7 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { logIndex) ); - if (followers.size() == 0) { + if (followerToLog.isEmpty()) { context.setCommitIndex(logIndex); applyLogToStateMachine(logIndex); } else { @@ -386,7 +392,8 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { private void sendAppendEntries() { // Send an AppendEntries to all followers - for (String followerId : followers) { + for (Entry e : followerToLog.entrySet()) { + final String followerId = e.getKey(); ActorSelection followerActor = context.getPeerActorSelection(followerId); if (followerActor != null) { @@ -473,23 +480,19 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { * */ private void installSnapshotIfNeeded() { - for (String followerId : followers) { - ActorSelection followerActor = - context.getPeerActorSelection(followerId); - - if(followerActor != null) { - FollowerLogInformation followerLogInformation = - followerToLog.get(followerId); + for (Entry e : followerToLog.entrySet()) { + final ActorSelection followerActor = context.getPeerActorSelection(e.getKey()); - long nextIndex = followerLogInformation.getNextIndex().get(); + if (followerActor != null) { + long nextIndex = e.getValue().getNextIndex().get(); if (!context.getReplicatedLog().isPresent(nextIndex) && context.getReplicatedLog().isInSnapshot(nextIndex)) { - LOG.info("{} follower needs a snapshot install", followerId); + LOG.info("{} follower needs a snapshot install", e.getKey()); if (snapshot.isPresent()) { // if a snapshot is present in the memory, most likely another install is in progress // no need to capture snapshot - sendSnapshotChunk(followerActor, followerId); + sendSnapshotChunk(followerActor, e.getKey()); } else { initiateCaptureSnapshot(); @@ -528,16 +531,15 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { private void sendInstallSnapshot() { - for (String followerId : followers) { - ActorSelection followerActor = context.getPeerActorSelection(followerId); + for (Entry e : followerToLog.entrySet()) { + ActorSelection followerActor = context.getPeerActorSelection(e.getKey()); - if(followerActor != null) { - FollowerLogInformation followerLogInformation = followerToLog.get(followerId); - long nextIndex = followerLogInformation.getNextIndex().get(); + if (followerActor != null) { + long nextIndex = e.getValue().getNextIndex().get(); if (!context.getReplicatedLog().isPresent(nextIndex) && context.getReplicatedLog().isInSnapshot(nextIndex)) { - sendSnapshotChunk(followerActor, followerId); + sendSnapshotChunk(followerActor, e.getKey()); } } } @@ -588,7 +590,7 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { } private void sendHeartBeat() { - if (followers.size() > 0) { + if (!followerToLog.isEmpty()) { sendAppendEntries(); } } @@ -600,7 +602,7 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { } private void scheduleHeartBeat(FiniteDuration interval) { - if(followers.size() == 0){ + if (followerToLog.isEmpty()) { // Optimization - do not bother scheduling a heartbeat as there are // no followers return; @@ -759,17 +761,38 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { // called from example-actor for printing the follower-states public String printFollowerStates() { - StringBuilder sb = new StringBuilder(); - for(FollowerLogInformation followerLogInformation : followerToLog.values()) { - boolean isFollowerActive = followerLogInformation.isFollowerActive(); - sb.append("{"+followerLogInformation.getId() + " state:" + isFollowerActive + "},"); + final StringBuilder sb = new StringBuilder(); + sb.append('['); + for (FollowerLogInformation followerLogInformation : followerToLog.values()) { + sb.append('{'); + sb.append(followerLogInformation.getId()); + sb.append(" state:"); + sb.append(followerLogInformation.isFollowerActive()); + sb.append("},"); } - return "[" + sb.toString() + "]"; + sb.append(']'); + + return sb.toString(); + } + + @VisibleForTesting + public FollowerLogInformation getFollower(String followerId) { + return followerToLog.get(followerId); + } + + @VisibleForTesting + protected void setFollowerSnapshot(String followerId, FollowerToSnapshot snapshot) { + mapFollowerToSnapshot.put(followerId, snapshot); + } + + @VisibleForTesting + public int followerSnapshotSize() { + return mapFollowerToSnapshot.size(); } @VisibleForTesting - void markFollowerActive(String followerId) { - followerToLog.get(followerId).markFollowerActive(); + public int followerLogSize() { + return followerToLog.size(); } }