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.base.Stopwatch;
12 import java.util.concurrent.TimeUnit;
14 public class FollowerLogInformationImpl implements FollowerLogInformation {
15 private final String id;
17 private final Stopwatch stopwatch = Stopwatch.createUnstarted();
19 private final RaftActorContext context;
21 private long nextIndex;
23 private long matchIndex;
25 private long lastReplicatedIndex = -1L;
27 private final Stopwatch lastReplicatedStopwatch = Stopwatch.createUnstarted();
29 private short payloadVersion = -1;
31 public FollowerLogInformationImpl(String id, long matchIndex, RaftActorContext context) {
33 this.nextIndex = context.getCommitIndex();
34 this.matchIndex = matchIndex;
35 this.context = context;
39 public long incrNextIndex() {
44 public long decrNextIndex() {
49 public boolean setNextIndex(long nextIndex) {
50 if(this.nextIndex != nextIndex) {
51 this.nextIndex = nextIndex;
59 public long incrMatchIndex(){
64 public boolean setMatchIndex(long matchIndex) {
65 if(this.matchIndex != matchIndex) {
66 this.matchIndex = matchIndex;
74 public String getId() {
79 public long getNextIndex() {
84 public long getMatchIndex() {
89 public boolean isFollowerActive() {
90 long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
91 return (stopwatch.isRunning()) &&
92 (elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis());
96 public void markFollowerActive() {
97 if (stopwatch.isRunning()) {
104 public void markFollowerInActive() {
105 if (stopwatch.isRunning()) {
111 public long timeSinceLastActivity() {
112 return stopwatch.elapsed(TimeUnit.MILLISECONDS);
116 public boolean okToReplicate() {
117 // Return false if we are trying to send duplicate data before the heartbeat interval
118 if(getNextIndex() == lastReplicatedIndex){
119 if(lastReplicatedStopwatch.elapsed(TimeUnit.MILLISECONDS) < context.getConfigParams()
120 .getHeartBeatInterval().toMillis()){
125 resetLastReplicated();
129 private void resetLastReplicated(){
130 lastReplicatedIndex = getNextIndex();
131 if(lastReplicatedStopwatch.isRunning()){
132 lastReplicatedStopwatch.reset();
134 lastReplicatedStopwatch.start();
138 public String toString() {
139 StringBuilder builder = new StringBuilder();
140 builder.append("FollowerLogInformationImpl [id=").append(id).append(", nextIndex=").append(nextIndex)
141 .append(", matchIndex=").append(matchIndex).append(", stopwatch=")
142 .append(stopwatch.elapsed(TimeUnit.MILLISECONDS))
143 .append(", followerTimeoutMillis=")
144 .append(context.getConfigParams().getElectionTimeOutInterval().toMillis()).append("]");
145 return builder.toString();
149 public short getPayloadVersion() {
150 return payloadVersion;
154 public void setPayloadVersion(short payloadVersion) {
155 this.payloadVersion = payloadVersion;