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%2FAbstractRaftActorBehavior.java;h=43704d8cfed1ccc2c95266164258c6ad7999f7a1;hb=3676d1686706dbee6656e86a23c4bdb516d5267b;hp=b20671f9d8de3ca4b8ec83b402107aa5763fb5b3;hpb=a3adcd6cd7659b30e5115efe86440f7a2123ec20;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehavior.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehavior.java index b20671f9d8..43704d8cfe 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehavior.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehavior.java @@ -10,8 +10,7 @@ package org.opendaylight.controller.cluster.raft.behaviors; import akka.actor.ActorRef; import akka.actor.Cancellable; -import java.util.HashMap; -import java.util.Map; +import com.google.common.base.Preconditions; import java.util.Random; import java.util.concurrent.TimeUnit; import org.opendaylight.controller.cluster.raft.ClientRequestTracker; @@ -19,7 +18,6 @@ import org.opendaylight.controller.cluster.raft.RaftActorContext; import org.opendaylight.controller.cluster.raft.RaftState; import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry; import org.opendaylight.controller.cluster.raft.SerializationUtils; -import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload; import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries; import org.opendaylight.controller.cluster.raft.base.messages.ApplyState; import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout; @@ -41,9 +39,6 @@ import scala.concurrent.duration.FiniteDuration; * set currentTerm = T, convert to follower (§5.1) */ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { - - protected static final ElectionTimeout ELECTION_TIMEOUT = new ElectionTimeout(); - /** * Information about the RaftActor whose behavior this class represents */ @@ -72,16 +67,31 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { private final RaftState state; - protected AbstractRaftActorBehavior(RaftActorContext context, RaftState state) { - this.context = context; - this.state = state; + AbstractRaftActorBehavior(final RaftActorContext context, final RaftState state) { + this.context = Preconditions.checkNotNull(context); + this.state = Preconditions.checkNotNull(state); this.LOG = context.getLogger(); logName = String.format("%s (%s)", context.getId(), state); } + public static RaftActorBehavior createBehavior(final RaftActorContext context, final RaftState state) { + switch (state) { + case Candidate: + return new Candidate(context); + case Follower: + return new Follower(context); + case IsolatedLeader: + return new IsolatedLeader(context); + case Leader: + return new Leader(context); + default: + throw new IllegalArgumentException("Unhandled state " + state); + } + } + @Override - public RaftState state() { + public final RaftState state() { return state; } @@ -255,6 +265,10 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { } } + protected boolean canStartElection() { + return context.getRaftPolicy().automaticElectionsEnabled() && context.isVotingMember(); + } + /** * schedule a new election * @@ -263,12 +277,11 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { protected void scheduleElection(FiniteDuration interval) { stopElection(); - // Schedule an election. When the scheduler triggers an ElectionTimeout - // message is sent to itself - electionCancel = - context.getActorSystem().scheduler().scheduleOnce(interval, - context.getActor(), ELECTION_TIMEOUT, - context.getActorSystem().dispatcher(), context.getActor()); + if(canStartElection()) { + // Schedule an election. When the scheduler triggers an ElectionTimeout message is sent to itself + electionCancel = context.getActorSystem().scheduler().scheduleOnce(interval, context.getActor(), + ElectionTimeout.INSTANCE, context.getActorSystem().dispatcher(), context.getActor()); + } } /** @@ -415,7 +428,8 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { return this; } - @Override public String getLeaderId() { + @Override + public String getLeaderId() { return leaderId; } @@ -435,7 +449,7 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { protected RaftActorBehavior internalSwitchBehavior(RaftState newState) { if(context.getRaftPolicy().automaticElectionsEnabled()){ - return internalSwitchBehavior(newState.createBehavior(context)); + return internalSwitchBehavior(createBehavior(context, newState)); } return this; } @@ -482,7 +496,7 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { * @param snapshotCapturedIndex */ protected void performSnapshotWithoutCapture(final long snapshotCapturedIndex) { - long actualIndex = context.getSnapshotManager().trimLog(snapshotCapturedIndex, this); + long actualIndex = context.getSnapshotManager().trimLog(snapshotCapturedIndex); if(actualIndex != -1){ setReplicatedToAllIndex(actualIndex); @@ -492,21 +506,4 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { protected String getId(){ return context.getId(); } - - public void applyServerConfiguration(ServerConfigurationPayload serverConfig) { - Map currentPeers = new HashMap<>(context.getPeerAddresses()); - for(String peerId: serverConfig.getNewServerConfig()) { - if(!getId().equals(peerId)) { - if(!currentPeers.containsKey(peerId)) { - context.addToPeers(peerId, null); - } else { - currentPeers.remove(peerId); - } - } - } - - for(String peerIdToRemove: currentPeers.keySet()) { - context.removePeer(peerIdToRemove); - } - } }