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 scala.concurrent.duration.FiniteDuration;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.atomic.AtomicLong;
17 public class FollowerLogInformationImpl implements FollowerLogInformation{
19 private final String id;
21 private final AtomicLong nextIndex;
23 private final AtomicLong matchIndex;
25 private final Stopwatch stopwatch;
27 private final long followerTimeoutMillis;
29 public FollowerLogInformationImpl(String id, AtomicLong nextIndex,
30 AtomicLong matchIndex, FiniteDuration followerTimeoutDuration) {
32 this.nextIndex = nextIndex;
33 this.matchIndex = matchIndex;
34 this.stopwatch = new Stopwatch();
35 this.followerTimeoutMillis = followerTimeoutDuration.toMillis();
38 public long incrNextIndex(){
39 return nextIndex.incrementAndGet();
42 @Override public long decrNextIndex() {
43 return nextIndex.decrementAndGet();
46 @Override public void setNextIndex(long nextIndex) {
47 this.nextIndex.set(nextIndex);
50 public long incrMatchIndex(){
51 return matchIndex.incrementAndGet();
54 @Override public void setMatchIndex(long matchIndex) {
55 this.matchIndex.set(matchIndex);
58 public String getId() {
62 public AtomicLong getNextIndex() {
66 public AtomicLong getMatchIndex() {
71 public boolean isFollowerActive() {
72 long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
73 return (stopwatch.isRunning()) && (elapsed <= followerTimeoutMillis);
77 public void markFollowerActive() {
78 if (stopwatch.isRunning()) {
85 public void markFollowerInActive() {
86 if (stopwatch.isRunning()) {