X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FFollowerLogInformationImpl.java;h=1c8d5e6e10647bcbac9ad7f55dd247e6f51f94c6;hp=90e128256132437c5f2acd4cb861e0e74a759d44;hb=388dd012c7b36177808ff5c5ad692b16dd58c944;hpb=95771c01e72fca4414c3b4d75734126d7c53f6df diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/FollowerLogInformationImpl.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/FollowerLogInformationImpl.java index 90e1282561..1c8d5e6e10 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/FollowerLogInformationImpl.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/FollowerLogInformationImpl.java @@ -8,59 +8,72 @@ package org.opendaylight.controller.cluster.raft; +import com.google.common.base.Preconditions; import com.google.common.base.Stopwatch; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLongFieldUpdater; public class FollowerLogInformationImpl implements FollowerLogInformation { - private static final AtomicLongFieldUpdater NEXT_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(FollowerLogInformationImpl.class, "nextIndex"); - private static final AtomicLongFieldUpdater MATCH_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(FollowerLogInformationImpl.class, "matchIndex"); - - private final String id; - private final Stopwatch stopwatch = Stopwatch.createUnstarted(); private final RaftActorContext context; - private volatile long nextIndex; + private long nextIndex; + + private long matchIndex; + + private long lastReplicatedIndex = -1L; - private volatile long matchIndex; + private final Stopwatch lastReplicatedStopwatch = Stopwatch.createUnstarted(); - public FollowerLogInformationImpl(String id, long matchIndex, RaftActorContext context) { - this.id = id; + private short payloadVersion = -1; + + private final PeerInfo peerInfo; + + public FollowerLogInformationImpl(PeerInfo peerInfo, long matchIndex, RaftActorContext context) { this.nextIndex = context.getCommitIndex(); this.matchIndex = matchIndex; this.context = context; + this.peerInfo = Preconditions.checkNotNull(peerInfo); } @Override - public long incrNextIndex(){ - return NEXT_INDEX_UPDATER.incrementAndGet(this); + public long incrNextIndex() { + return nextIndex++; } @Override public long decrNextIndex() { - return NEXT_INDEX_UPDATER.decrementAndGet(this); + return nextIndex--; } @Override - public void setNextIndex(long nextIndex) { - this.nextIndex = nextIndex; + public boolean setNextIndex(long nextIndex) { + if(this.nextIndex != nextIndex) { + this.nextIndex = nextIndex; + return true; + } + + return false; } @Override public long incrMatchIndex(){ - return MATCH_INDEX_UPDATER.incrementAndGet(this); + return matchIndex++; } @Override - public void setMatchIndex(long matchIndex) { - this.matchIndex = matchIndex; + public boolean setMatchIndex(long matchIndex) { + if(this.matchIndex != matchIndex) { + this.matchIndex = matchIndex; + return true; + } + + return false; } @Override public String getId() { - return id; + return peerInfo.getId(); } @Override @@ -75,6 +88,10 @@ public class FollowerLogInformationImpl implements FollowerLogInformation { @Override public boolean isFollowerActive() { + if(peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) { + return false; + } + long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS); return (stopwatch.isRunning()) && (elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis()); @@ -101,15 +118,46 @@ public class FollowerLogInformationImpl implements FollowerLogInformation { } @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("FollowerLogInformationImpl [id=").append(id).append(", nextIndex=").append(nextIndex) - .append(", matchIndex=").append(matchIndex).append(", stopwatch=") - .append(stopwatch.elapsed(TimeUnit.MILLISECONDS)) - .append(", followerTimeoutMillis=") - .append(context.getConfigParams().getElectionTimeOutInterval().toMillis()).append("]"); - return builder.toString(); + public boolean okToReplicate() { + if(peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) { + return false; + } + + // Return false if we are trying to send duplicate data before the heartbeat interval + if(getNextIndex() == lastReplicatedIndex){ + if(lastReplicatedStopwatch.elapsed(TimeUnit.MILLISECONDS) < context.getConfigParams() + .getHeartBeatInterval().toMillis()){ + return false; + } + } + + resetLastReplicated(); + return true; + } + + private void resetLastReplicated(){ + lastReplicatedIndex = getNextIndex(); + if(lastReplicatedStopwatch.isRunning()){ + lastReplicatedStopwatch.reset(); + } + lastReplicatedStopwatch.start(); + } + + @Override + public short getPayloadVersion() { + return payloadVersion; } + @Override + public void setPayloadVersion(short payloadVersion) { + this.payloadVersion = payloadVersion; + } + @Override + public String toString() { + return "FollowerLogInformationImpl [id=" + getId() + ", nextIndex=" + nextIndex + ", matchIndex=" + matchIndex + + ", lastReplicatedIndex=" + lastReplicatedIndex + ", votingState=" + peerInfo.getVotingState() + + ", stopwatch=" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + ", followerTimeoutMillis=" + + context.getConfigParams().getElectionTimeOutInterval().toMillis() + "]"; + } }