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;
13 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
15 public class FollowerLogInformationImpl implements FollowerLogInformation {
16 private static final AtomicLongFieldUpdater<FollowerLogInformationImpl> NEXT_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(FollowerLogInformationImpl.class, "nextIndex");
17 private static final AtomicLongFieldUpdater<FollowerLogInformationImpl> MATCH_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(FollowerLogInformationImpl.class, "matchIndex");
19 private final String id;
21 private final Stopwatch stopwatch = Stopwatch.createUnstarted();
23 private final RaftActorContext context;
25 private volatile long nextIndex;
27 private volatile long matchIndex;
29 public FollowerLogInformationImpl(String id, long matchIndex, RaftActorContext context) {
31 this.nextIndex = context.getCommitIndex();
32 this.matchIndex = matchIndex;
33 this.context = context;
37 public long incrNextIndex(){
38 return NEXT_INDEX_UPDATER.incrementAndGet(this);
42 public long decrNextIndex() {
43 return NEXT_INDEX_UPDATER.decrementAndGet(this);
47 public boolean setNextIndex(long nextIndex) {
48 if(this.nextIndex != nextIndex) {
49 this.nextIndex = nextIndex;
57 public long incrMatchIndex(){
58 return MATCH_INDEX_UPDATER.incrementAndGet(this);
62 public boolean setMatchIndex(long matchIndex) {
63 if(this.matchIndex != matchIndex) {
64 this.matchIndex = matchIndex;
72 public String getId() {
77 public long getNextIndex() {
82 public long getMatchIndex() {
87 public boolean isFollowerActive() {
88 long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
89 return (stopwatch.isRunning()) &&
90 (elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis());
94 public void markFollowerActive() {
95 if (stopwatch.isRunning()) {
102 public void markFollowerInActive() {
103 if (stopwatch.isRunning()) {
109 public long timeSinceLastActivity() {
110 return stopwatch.elapsed(TimeUnit.MILLISECONDS);
114 public String toString() {
115 StringBuilder builder = new StringBuilder();
116 builder.append("FollowerLogInformationImpl [id=").append(id).append(", nextIndex=").append(nextIndex)
117 .append(", matchIndex=").append(matchIndex).append(", stopwatch=")
118 .append(stopwatch.elapsed(TimeUnit.MILLISECONDS))
119 .append(", followerTimeoutMillis=")
120 .append(context.getConfigParams().getElectionTimeOutInterval().toMillis()).append("]");
121 return builder.toString();