Merge "Startup archetype: remove 'Impl' from config subsystem Module name."
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / FollowerLogInformationImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft;
10
11 import com.google.common.base.Stopwatch;
12 import java.util.concurrent.TimeUnit;
13 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
14 import scala.concurrent.duration.FiniteDuration;
15
16 public class FollowerLogInformationImpl implements FollowerLogInformation {
17     private static final AtomicLongFieldUpdater<FollowerLogInformationImpl> NEXT_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(FollowerLogInformationImpl.class, "nextIndex");
18     private static final AtomicLongFieldUpdater<FollowerLogInformationImpl> MATCH_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(FollowerLogInformationImpl.class, "matchIndex");
19
20     private final String id;
21
22     private final Stopwatch stopwatch = Stopwatch.createUnstarted();
23
24     private final long followerTimeoutMillis;
25
26     private volatile long nextIndex;
27
28     private volatile long matchIndex;
29
30     public FollowerLogInformationImpl(String id, long nextIndex,
31         long matchIndex, FiniteDuration followerTimeoutDuration) {
32         this.id = id;
33         this.nextIndex = nextIndex;
34         this.matchIndex = matchIndex;
35         this.followerTimeoutMillis = followerTimeoutDuration.toMillis();
36     }
37
38     @Override
39     public long incrNextIndex(){
40         return NEXT_INDEX_UPDATER.incrementAndGet(this);
41     }
42
43     @Override
44     public long decrNextIndex() {
45         return NEXT_INDEX_UPDATER.decrementAndGet(this);
46     }
47
48     @Override
49     public void setNextIndex(long nextIndex) {
50         this.nextIndex = nextIndex;
51     }
52
53     @Override
54     public long incrMatchIndex(){
55         return MATCH_INDEX_UPDATER.incrementAndGet(this);
56     }
57
58     @Override
59     public void setMatchIndex(long matchIndex) {
60         this.matchIndex = matchIndex;
61     }
62
63     @Override
64     public String getId() {
65         return id;
66     }
67
68     @Override
69     public long getNextIndex() {
70         return nextIndex;
71     }
72
73     @Override
74     public long getMatchIndex() {
75         return matchIndex;
76     }
77
78     @Override
79     public boolean isFollowerActive() {
80         long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
81         return (stopwatch.isRunning()) && (elapsed <= followerTimeoutMillis);
82     }
83
84     @Override
85     public void markFollowerActive() {
86         if (stopwatch.isRunning()) {
87             stopwatch.reset();
88         }
89         stopwatch.start();
90     }
91
92     @Override
93     public void markFollowerInActive() {
94         if (stopwatch.isRunning()) {
95             stopwatch.stop();
96         }
97     }
98
99     @Override
100     public long timeSinceLastActivity() {
101         return stopwatch.elapsed(TimeUnit.MILLISECONDS);
102     }
103
104     @Override
105     public String toString() {
106         StringBuilder builder = new StringBuilder();
107         builder.append("FollowerLogInformationImpl [id=").append(id).append(", nextIndex=").append(nextIndex)
108                 .append(", matchIndex=").append(matchIndex).append(", stopwatch=")
109                 .append(stopwatch.elapsed(TimeUnit.MILLISECONDS))
110                 .append(", followerTimeoutMillis=").append(followerTimeoutMillis).append("]");
111         return builder.toString();
112     }
113
114
115 }