View Javadoc
1   /*
2    * Copyright (C) 2016, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.internal.ketch;
45  
46  import static org.eclipse.jgit.internal.ketch.KetchReplica.State.OFFLINE;
47  
48  import java.util.ArrayList;
49  import java.util.Collection;
50  import java.util.Collections;
51  import java.util.List;
52  
53  import org.eclipse.jgit.annotations.Nullable;
54  import org.eclipse.jgit.lib.ObjectId;
55  
56  /**
57   * A snapshot of a leader and its view of the world.
58   */
59  public class LeaderSnapshot {
60  	final List<ReplicaSnapshot> replicas = new ArrayList<>();
61  	KetchLeader.State state;
62  	long term;
63  	LogIndex headIndex;
64  	LogIndex committedIndex;
65  	boolean idle;
66  
67  	LeaderSnapshot() {
68  	}
69  
70  	/**
71  	 * Get unmodifiable view of configured replicas.
72  	 *
73  	 * @return unmodifiable view of configured replicas.
74  	 */
75  	public Collection<ReplicaSnapshot> getReplicas() {
76  		return Collections.unmodifiableList(replicas);
77  	}
78  
79  	/**
80  	 * Get current state of the leader.
81  	 *
82  	 * @return current state of the leader.
83  	 */
84  	public KetchLeader.State getState() {
85  		return state;
86  	}
87  
88  	/**
89  	 * Whether the leader is not running a round to reach consensus, and has no
90  	 * rounds queued.
91  	 *
92  	 * @return {@code true} if the leader is not running a round to reach
93  	 *         consensus, and has no rounds queued.
94  	 */
95  	public boolean isIdle() {
96  		return idle;
97  	}
98  
99  	/**
100 	 * Get term of this leader
101 	 *
102 	 * @return term of this leader. Valid only if {@link #getState()} is
103 	 *         currently
104 	 *         {@link org.eclipse.jgit.internal.ketch.KetchLeader.State#LEADER}.
105 	 */
106 	public long getTerm() {
107 		return term;
108 	}
109 
110 	/**
111 	 * Get end of the leader's log
112 	 *
113 	 * @return end of the leader's log; null if leader hasn't started up enough
114 	 *         to begin its own election.
115 	 */
116 	@Nullable
117 	public LogIndex getHead() {
118 		return headIndex;
119 	}
120 
121 	/**
122 	 * Get state the leader knows is committed on a majority of participant
123 	 * replicas
124 	 *
125 	 * @return state the leader knows is committed on a majority of participant
126 	 *         replicas. Null until the leader instance has committed a log
127 	 *         index within its own term.
128 	 */
129 	@Nullable
130 	public LogIndex getCommitted() {
131 		return committedIndex;
132 	}
133 
134 	/** {@inheritDoc} */
135 	@Override
136 	public String toString() {
137 		StringBuilder s = new StringBuilder();
138 		s.append(isIdle() ? "IDLE" : "RUNNING"); //$NON-NLS-1$ //$NON-NLS-2$
139 		s.append(" state ").append(getState()); //$NON-NLS-1$
140 		if (getTerm() > 0) {
141 			s.append(" term ").append(getTerm()); //$NON-NLS-1$
142 		}
143 		s.append('\n');
144 		s.append(String.format(
145 				"%-10s %12s %12s\n", //$NON-NLS-1$
146 				"Replica", "Accepted", "Committed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
147 		s.append("------------------------------------\n"); //$NON-NLS-1$
148 		debug(s, "(leader)", getHead(), getCommitted()); //$NON-NLS-1$
149 		s.append('\n');
150 		for (ReplicaSnapshot r : getReplicas()) {
151 			debug(s, r);
152 			s.append('\n');
153 		}
154 		s.append('\n');
155 		return s.toString();
156 	}
157 
158 	private static void debug(StringBuilder b, ReplicaSnapshot s) {
159 		KetchReplica replica = s.getReplica();
160 		debug(b, replica.getName(), s.getAccepted(), s.getCommitted());
161 		b.append(String.format(" %-8s %s", //$NON-NLS-1$
162 				replica.getParticipation(), s.getState()));
163 		if (s.getState() == OFFLINE) {
164 			String err = s.getErrorMessage();
165 			if (err != null) {
166 				b.append(" (").append(err).append(')'); //$NON-NLS-1$
167 			}
168 		}
169 	}
170 
171 	private static void debug(StringBuilder s, String name,
172 			ObjectId accepted, ObjectId committed) {
173 		s.append(String.format(
174 				"%-10s %-12s %-12s", //$NON-NLS-1$
175 				name, str(accepted), str(committed)));
176 	}
177 
178 	static String str(ObjectId c) {
179 		if (c instanceof LogIndex) {
180 			return ((LogIndex) c).describeForLog();
181 		} else if (c != null) {
182 			return c.abbreviate(8).name();
183 		}
184 		return "-"; //$NON-NLS-1$
185 	}
186 }