2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.raft;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Stopwatch;
14 import java.util.concurrent.TimeUnit;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.cluster.raft.behaviors.LeaderInstallSnapshotState;
20 * The state of the followers log as known by the Leader.
23 * @author Thomas Pantelis
25 public final class FollowerLogInformation {
26 public static final long NO_INDEX = -1;
28 private final Stopwatch stopwatch = Stopwatch.createUnstarted();
30 private final RaftActorContext context;
32 private long nextIndex;
34 private long matchIndex;
36 private long lastReplicatedIndex = -1L;
38 private final Stopwatch lastReplicatedStopwatch = Stopwatch.createUnstarted();
40 private short payloadVersion = -1;
42 // Assume the HELIUM_VERSION version initially for backwards compatibility until we obtain the follower's
43 // actual version via AppendEntriesReply. Although we no longer support the Helium version, a pre-Boron
44 // follower will not have the version field in AppendEntriesReply so it will be set to 0 which is
46 private short raftVersion = RaftVersions.HELIUM_VERSION;
48 private final PeerInfo peerInfo;
50 private LeaderInstallSnapshotState installSnapshotState;
52 private long slicedLogEntryIndex = NO_INDEX;
54 private boolean needsLeaderAddress;
57 * Constructs an instance.
59 * @param peerInfo the associated PeerInfo of the follower.
60 * @param matchIndex the initial match index.
61 * @param context the RaftActorContext.
64 FollowerLogInformation(final PeerInfo peerInfo, final long matchIndex, final RaftActorContext context) {
65 this.nextIndex = context.getCommitIndex();
66 this.matchIndex = matchIndex;
67 this.context = context;
68 this.peerInfo = Preconditions.checkNotNull(peerInfo);
72 * Constructs an instance with no matching index.
74 * @param peerInfo the associated PeerInfo of the follower.
75 * @param context the RaftActorContext.
77 public FollowerLogInformation(final PeerInfo peerInfo, final RaftActorContext context) {
78 this(peerInfo, NO_INDEX, context);
82 * Increments the value of the follower's next index.
84 * @return the new value of nextIndex.
87 long incrNextIndex() {
92 * Decrements the value of the follower's next index, taking into account its reported last log index.
94 * @param followerLastIndex follower's last reported index.
95 * @return true if the next index was decremented, i.e. it was previously >= 0, false otherwise.
97 public boolean decrNextIndex(final long followerLastIndex) {
102 if (followerLastIndex >= 0 && nextIndex > followerLastIndex) {
103 // If the follower's last log index is lower than nextIndex, jump directly to it, so we converge
104 // on a common index more quickly.
105 nextIndex = followerLastIndex;
113 * Sets the index of the follower's next log entry.
115 * @param nextIndex the new index.
116 * @return true if the new index differed from the current index and the current index was updated, false
119 @SuppressWarnings("checkstyle:hiddenField")
120 public boolean setNextIndex(final long nextIndex) {
121 if (this.nextIndex != nextIndex) {
122 this.nextIndex = nextIndex;
130 * Increments the value of the follower's match index.
132 * @return the new value of matchIndex.
134 public long incrMatchIndex() {
139 * Sets the index of the follower's highest log entry.
141 * @param matchIndex the new index.
142 * @return true if the new index differed from the current index and the current index was updated, false
145 @SuppressWarnings("checkstyle:hiddenField")
146 public boolean setMatchIndex(final long matchIndex) {
147 // If the new match index is the index of the entry currently being sliced, then we know slicing is complete
148 // and the follower received the entry and responded so clear the slicedLogEntryIndex
149 if (isLogEntrySlicingInProgress() && slicedLogEntryIndex == matchIndex) {
150 slicedLogEntryIndex = NO_INDEX;
153 if (this.matchIndex != matchIndex) {
154 this.matchIndex = matchIndex;
162 * Returns the identifier of the follower.
164 * @return the identifier of the follower.
166 public String getId() {
167 return peerInfo.getId();
171 * Returns the index of the next log entry to send to the follower.
173 * @return index of the follower's next log entry.
175 public long getNextIndex() {
180 * Returns the index of highest log entry known to be replicated on the follower.
182 * @return the index of highest log entry.
184 public long getMatchIndex() {
189 * Checks if the follower is active by comparing the time of the last activity with the election time out. The
190 * follower is active if some activity has occurred for the follower within the election time out interval.
192 * @return true if follower is active, false otherwise.
194 public boolean isFollowerActive() {
195 if (peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
199 long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
200 return stopwatch.isRunning()
201 && elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis();
205 * Marks the follower as active. This should be called when some activity has occurred for the follower.
207 public void markFollowerActive() {
208 if (stopwatch.isRunning()) {
215 * Marks the follower as inactive. This should only be called from unit tests.
218 public void markFollowerInActive() {
219 if (stopwatch.isRunning()) {
225 * Returns the time since the last activity occurred for the follower.
227 * @return time in nanoseconds since the last activity from the follower.
229 public long nanosSinceLastActivity() {
230 return stopwatch.elapsed(TimeUnit.NANOSECONDS);
234 * This method checks if the next replicate message can be sent to the follower. This is an optimization to avoid
235 * sending duplicate message too frequently if the last replicate message was sent and no reply has been received
236 * yet within the current heart beat interval
238 * @return true if it is OK to replicate, false otherwise
240 public boolean okToReplicate() {
241 if (peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
245 // Return false if we are trying to send duplicate data before the heartbeat interval
246 if (getNextIndex() == lastReplicatedIndex && lastReplicatedStopwatch.elapsed(TimeUnit.MILLISECONDS)
247 < context.getConfigParams().getHeartBeatInterval().toMillis()) {
251 resetLastReplicated();
255 private void resetLastReplicated() {
256 lastReplicatedIndex = getNextIndex();
257 if (lastReplicatedStopwatch.isRunning()) {
258 lastReplicatedStopwatch.reset();
260 lastReplicatedStopwatch.start();
264 * Returns the log entry payload data version of the follower.
266 * @return the payload data version.
268 public short getPayloadVersion() {
269 return payloadVersion;
273 * Sets the payload data version of the follower.
275 * @param payloadVersion the payload data version.
277 public void setPayloadVersion(final short payloadVersion) {
278 this.payloadVersion = payloadVersion;
282 * Returns the the raft version of the follower.
284 * @return the raft version of the follower.
286 public short getRaftVersion() {
291 * Sets the raft version of the follower.
293 * @param raftVersion the raft version.
295 public void setRaftVersion(final short raftVersion) {
296 this.raftVersion = raftVersion;
300 * Returns the LeaderInstallSnapshotState for the in progress install snapshot.
302 * @return the LeaderInstallSnapshotState if a snapshot install is in progress, null otherwise.
305 public LeaderInstallSnapshotState getInstallSnapshotState() {
306 return installSnapshotState;
310 * Sets the LeaderInstallSnapshotState when an install snapshot is initiated.
312 * @param state the LeaderInstallSnapshotState
314 public void setLeaderInstallSnapshotState(@Nonnull final LeaderInstallSnapshotState state) {
315 if (this.installSnapshotState == null) {
316 this.installSnapshotState = Preconditions.checkNotNull(state);
321 * Clears the LeaderInstallSnapshotState when an install snapshot is complete.
323 public void clearLeaderInstallSnapshotState() {
324 Preconditions.checkState(installSnapshotState != null);
325 installSnapshotState.close();
326 installSnapshotState = null;
330 * Sets the index of the log entry whose payload size exceeds the maximum size for a single message and thus
331 * needs to be sliced into smaller chunks.
333 * @param index the log entry index or NO_INDEX to clear it
335 public void setSlicedLogEntryIndex(final long index) {
336 slicedLogEntryIndex = index;
340 * Return whether or not log entry slicing is currently in progress.
342 * @return true if slicing is currently in progress, false otherwise
344 public boolean isLogEntrySlicingInProgress() {
345 return slicedLogEntryIndex != NO_INDEX;
348 public void setNeedsLeaderAddress(boolean value) {
349 needsLeaderAddress = value;
353 public String needsLeaderAddress(String leaderId) {
354 return needsLeaderAddress ? context.getPeerAddress(leaderId) : null;
358 public String toString() {
359 return "FollowerLogInformation [id=" + getId() + ", nextIndex=" + nextIndex + ", matchIndex=" + matchIndex
360 + ", lastReplicatedIndex=" + lastReplicatedIndex + ", votingState=" + peerInfo.getVotingState()
361 + ", stopwatch=" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + ", followerTimeoutMillis="
362 + context.getConfigParams().getElectionTimeOutInterval().toMillis() + "]";