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.
94 * @return true if the next index was decremented, ie it was previously >= 0, false otherwise.
96 public boolean decrNextIndex() {
106 * Sets the index of the follower's next log entry.
108 * @param nextIndex the new index.
109 * @return true if the new index differed from the current index and the current index was updated, false
112 @SuppressWarnings("checkstyle:hiddenField")
113 public boolean setNextIndex(final long nextIndex) {
114 if (this.nextIndex != nextIndex) {
115 this.nextIndex = nextIndex;
123 * Increments the value of the follower's match index.
125 * @return the new value of matchIndex.
127 public long incrMatchIndex() {
132 * Sets the index of the follower's highest log entry.
134 * @param matchIndex the new index.
135 * @return true if the new index differed from the current index and the current index was updated, false
138 @SuppressWarnings("checkstyle:hiddenField")
139 public boolean setMatchIndex(final long matchIndex) {
140 // If the new match index is the index of the entry currently being sliced, then we know slicing is complete
141 // and the follower received the entry and responded so clear the slicedLogEntryIndex
142 if (isLogEntrySlicingInProgress() && slicedLogEntryIndex == matchIndex) {
143 slicedLogEntryIndex = NO_INDEX;
146 if (this.matchIndex != matchIndex) {
147 this.matchIndex = matchIndex;
155 * Returns the identifier of the follower.
157 * @return the identifier of the follower.
159 public String getId() {
160 return peerInfo.getId();
164 * Returns the index of the next log entry to send to the follower.
166 * @return index of the follower's next log entry.
168 public long getNextIndex() {
173 * Returns the index of highest log entry known to be replicated on the follower.
175 * @return the index of highest log entry.
177 public long getMatchIndex() {
182 * Checks if the follower is active by comparing the time of the last activity with the election time out. The
183 * follower is active if some activity has occurred for the follower within the election time out interval.
185 * @return true if follower is active, false otherwise.
187 public boolean isFollowerActive() {
188 if (peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
192 long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
193 return stopwatch.isRunning()
194 && elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis();
198 * Marks the follower as active. This should be called when some activity has occurred for the follower.
200 public void markFollowerActive() {
201 if (stopwatch.isRunning()) {
208 * Marks the follower as inactive. This should only be called from unit tests.
211 public void markFollowerInActive() {
212 if (stopwatch.isRunning()) {
218 * Returns the time since the last activity occurred for the follower.
220 * @return time in nanoseconds since the last activity from the follower.
222 public long nanosSinceLastActivity() {
223 return stopwatch.elapsed(TimeUnit.NANOSECONDS);
227 * This method checks if the next replicate message can be sent to the follower. This is an optimization to avoid
228 * sending duplicate message too frequently if the last replicate message was sent and no reply has been received
229 * yet within the current heart beat interval
231 * @return true if it is OK to replicate, false otherwise
233 public boolean okToReplicate() {
234 if (peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
238 // Return false if we are trying to send duplicate data before the heartbeat interval
239 if (getNextIndex() == lastReplicatedIndex && lastReplicatedStopwatch.elapsed(TimeUnit.MILLISECONDS)
240 < context.getConfigParams().getHeartBeatInterval().toMillis()) {
244 resetLastReplicated();
248 private void resetLastReplicated() {
249 lastReplicatedIndex = getNextIndex();
250 if (lastReplicatedStopwatch.isRunning()) {
251 lastReplicatedStopwatch.reset();
253 lastReplicatedStopwatch.start();
257 * Returns the log entry payload data version of the follower.
259 * @return the payload data version.
261 public short getPayloadVersion() {
262 return payloadVersion;
266 * Sets the payload data version of the follower.
268 * @param payloadVersion the payload data version.
270 public void setPayloadVersion(final short payloadVersion) {
271 this.payloadVersion = payloadVersion;
275 * Returns the the raft version of the follower.
277 * @return the raft version of the follower.
279 public short getRaftVersion() {
284 * Sets the raft version of the follower.
286 * @param raftVersion the raft version.
288 public void setRaftVersion(final short raftVersion) {
289 this.raftVersion = raftVersion;
293 * Returns the LeaderInstallSnapshotState for the in progress install snapshot.
295 * @return the LeaderInstallSnapshotState if a snapshot install is in progress, null otherwise.
298 public LeaderInstallSnapshotState getInstallSnapshotState() {
299 return installSnapshotState;
303 * Sets the LeaderInstallSnapshotState when an install snapshot is initiated.
305 * @param state the LeaderInstallSnapshotState
307 public void setLeaderInstallSnapshotState(@Nonnull final LeaderInstallSnapshotState state) {
308 if (this.installSnapshotState == null) {
309 this.installSnapshotState = Preconditions.checkNotNull(state);
314 * Clears the LeaderInstallSnapshotState when an install snapshot is complete.
316 public void clearLeaderInstallSnapshotState() {
317 Preconditions.checkState(installSnapshotState != null);
318 installSnapshotState.close();
319 installSnapshotState = null;
323 * Sets the index of the log entry whose payload size exceeds the maximum size for a single message and thus
324 * needs to be sliced into smaller chunks.
326 * @param index the log entry index or NO_INDEX to clear it
328 public void setSlicedLogEntryIndex(final long index) {
329 slicedLogEntryIndex = index;
333 * Return whether or not log entry slicing is currently in progress.
335 * @return true if slicing is currently in progress, false otherwise
337 public boolean isLogEntrySlicingInProgress() {
338 return slicedLogEntryIndex != NO_INDEX;
341 public void setNeedsLeaderAddress(boolean value) {
342 needsLeaderAddress = value;
346 public String needsLeaderAddress(String leaderId) {
347 return needsLeaderAddress ? context.getPeerAddress(leaderId) : null;
351 public String toString() {
352 return "FollowerLogInformation [id=" + getId() + ", nextIndex=" + nextIndex + ", matchIndex=" + matchIndex
353 + ", lastReplicatedIndex=" + lastReplicatedIndex + ", votingState=" + peerInfo.getVotingState()
354 + ", stopwatch=" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + ", followerTimeoutMillis="
355 + context.getConfigParams().getElectionTimeOutInterval().toMillis() + "]";