1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.internal.ketch;
12
13 import static org.eclipse.jgit.internal.ketch.KetchReplica.State.OFFLINE;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19
20 import org.eclipse.jgit.annotations.Nullable;
21 import org.eclipse.jgit.lib.ObjectId;
22
23
24
25
26 public class LeaderSnapshot {
27 final List<ReplicaSnapshot> replicas = new ArrayList<>();
28 KetchLeader.State state;
29 long term;
30 LogIndex headIndex;
31 LogIndex committedIndex;
32 boolean idle;
33
34 LeaderSnapshot() {
35 }
36
37
38
39
40
41
42 public Collection<ReplicaSnapshot> getReplicas() {
43 return Collections.unmodifiableList(replicas);
44 }
45
46
47
48
49
50
51 public KetchLeader.State getState() {
52 return state;
53 }
54
55
56
57
58
59
60
61
62 public boolean isIdle() {
63 return idle;
64 }
65
66
67
68
69
70
71
72
73 public long getTerm() {
74 return term;
75 }
76
77
78
79
80
81
82
83 @Nullable
84 public LogIndex getHead() {
85 return headIndex;
86 }
87
88
89
90
91
92
93
94
95
96 @Nullable
97 public LogIndex getCommitted() {
98 return committedIndex;
99 }
100
101
102 @Override
103 public String toString() {
104 StringBuilder s = new StringBuilder();
105 s.append(isIdle() ? "IDLE" : "RUNNING");
106 s.append(" state ").append(getState());
107 if (getTerm() > 0) {
108 s.append(" term ").append(getTerm());
109 }
110 s.append('\n');
111 s.append(String.format(
112 "%-10s %12s %12s\n",
113 "Replica", "Accepted", "Committed"));
114 s.append("------------------------------------\n");
115 debug(s, "(leader)", getHead(), getCommitted());
116 s.append('\n');
117 for (ReplicaSnapshot r : getReplicas()) {
118 debug(s, r);
119 s.append('\n');
120 }
121 s.append('\n');
122 return s.toString();
123 }
124
125 private static void debug(StringBuilder b, ReplicaSnapshot s) {
126 KetchReplica replica = s.getReplica();
127 debug(b, replica.getName(), s.getAccepted(), s.getCommitted());
128 b.append(String.format(" %-8s %s",
129 replica.getParticipation(), s.getState()));
130 if (s.getState() == OFFLINE) {
131 String err = s.getErrorMessage();
132 if (err != null) {
133 b.append(" (").append(err).append(')');
134 }
135 }
136 }
137
138 private static void debug(StringBuilder s, String name,
139 ObjectId accepted, ObjectId committed) {
140 s.append(String.format(
141 "%-10s %-12s %-12s",
142 name, str(accepted), str(committed)));
143 }
144
145 static String str(ObjectId c) {
146 if (c instanceof LogIndex) {
147 return ((LogIndex) c).describeForLog();
148 } else if (c != null) {
149 return c.abbreviate(8).name();
150 }
151 return "-";
152 }
153 }