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=99824b0bb4e6235a56d12cb719384fee85fd309a;hb=5fd83cdc5cf9d185a690cc40ec8acb67156c3a5f;hp=eed74bba82fbf8a4f8e4c1b5431887451c49d59d;hpb=73e969cf365dd78772596c71e940ae44fe2f22d3;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 eed74bba82..99824b0bb4 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 @@ -11,6 +11,8 @@ package org.opendaylight.controller.cluster.raft.behaviors; import akka.actor.ActorRef; import akka.actor.Cancellable; import akka.event.LoggingAdapter; +import java.util.Random; +import java.util.concurrent.TimeUnit; import org.opendaylight.controller.cluster.raft.ClientRequestTracker; import org.opendaylight.controller.cluster.raft.RaftActorContext; import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry; @@ -24,9 +26,6 @@ import org.opendaylight.controller.cluster.raft.messages.RequestVote; import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply; import scala.concurrent.duration.FiniteDuration; -import java.util.Random; -import java.util.concurrent.TimeUnit; - /** * Abstract class that represents the behavior of a RaftActor *

@@ -95,8 +94,8 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { // 1. Reply false if term < currentTerm (§5.1) if (appendEntries.getTerm() < currentTerm()) { if(LOG.isDebugEnabled()) { - LOG.debug("Cannot append entries because sender term {} is less than {}", - appendEntries.getTerm(), currentTerm()); + LOG.debug("{}: Cannot append entries because sender term {} is less than {}", + context.getId(), appendEntries.getTerm(), currentTerm()); } sender.tell( @@ -137,7 +136,7 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { RequestVote requestVote) { if(LOG.isDebugEnabled()) { - LOG.debug(requestVote.toString()); + LOG.debug("{}: Received {}", context.getId(), requestVote); } boolean grantVote = false; @@ -202,7 +201,7 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { protected FiniteDuration electionDuration() { long variance = new Random().nextInt(context.getConfigParams().getElectionTimeVariance()); return context.getConfigParams().getElectionTimeOutInterval().$plus( - new FiniteDuration(variance, TimeUnit.MILLISECONDS)); + new FiniteDuration(variance, TimeUnit.MILLISECONDS)); } /** @@ -351,12 +350,13 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { //if one index is not present in the log, no point in looping // around as the rest wont be present either LOG.warning( - "Missing index {} from log. Cannot apply state. Ignoring {} to {}", i, i, index); + "{}: Missing index {} from log. Cannot apply state. Ignoring {} to {}", + context.getId(), i, i, index); break; } } if(LOG.isDebugEnabled()) { - LOG.debug("Setting last applied to {}", newLastApplied); + LOG.debug("{}: Setting last applied to {}", context.getId(), newLastApplied); } context.setLastApplied(newLastApplied); @@ -390,13 +390,50 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { } protected RaftActorBehavior switchBehavior(RaftActorBehavior behavior) { - LOG.info("Switching from behavior {} to {}", this.state(), behavior.state()); + LOG.info("{} :- Switching from behavior {} to {}", context.getId(), this.state(), behavior.state()); try { close(); } catch (Exception e) { - LOG.error(e, "Failed to close behavior : {}", this.state()); + LOG.error(e, "{}: Failed to close behavior : {}", context.getId(), this.state()); } return behavior; } + + protected int getMajorityVoteCount(int numPeers) { + // Votes are required from a majority of the peers including self. + // The numMajority field therefore stores a calculated value + // of the number of votes required for this candidate to win an + // election based on it's known peers. + // If a peer was added during normal operation and raft replicas + // came to know about them then the new peer would also need to be + // taken into consideration when calculating this value. + // Here are some examples for what the numMajority would be for n + // peers + // 0 peers = 1 numMajority -: (0 + 1) / 2 + 1 = 1 + // 2 peers = 2 numMajority -: (2 + 1) / 2 + 1 = 2 + // 4 peers = 3 numMajority -: (4 + 1) / 2 + 1 = 3 + + int numMajority = 0; + if (numPeers > 0) { + int self = 1; + numMajority = (numPeers + self) / 2 + 1; + } + return numMajority; + + } + + protected long fakeSnapshot(final long minReplicatedToAllIndex, final long currentReplicatedIndex) { + + // we would want to keep the lastApplied as its used while capturing snapshots + long tempMin = Math.min(minReplicatedToAllIndex, + (context.getLastApplied() > -1 ? context.getLastApplied() - 1 : -1)); + + if (tempMin > -1 && context.getReplicatedLog().isPresent(tempMin)) { + context.getReplicatedLog().snapshotPreCommit(tempMin, context.getTermInformation().getCurrentTerm()); + context.getReplicatedLog().snapshotCommit(); + return tempMin; + } + return currentReplicatedIndex; + } }