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  /** A snapshot of a leader and its view of the world. */
57  public class LeaderSnapshot {
58  	final List<ReplicaSnapshot> replicas = new ArrayList<>();
59  	KetchLeader.State state;
60  	long term;
61  	LogIndex headIndex;
62  	LogIndex committedIndex;
63  	boolean idle;
64  
65  	LeaderSnapshot() {
66  	}
67  
68  	/** @return unmodifiable view of configured replicas. */
69  	public Collection<ReplicaSnapshot> getReplicas() {
70  		return Collections.unmodifiableList(replicas);
71  	}
72  
73  	/** @return current state of the leader. */
74  	public KetchLeader.State getState() {
75  		return state;
76  	}
77  
78  	/**
79  	 * @return {@code true} if the leader is not running a round to reach
80  	 *         consensus, and has no rounds queued.
81  	 */
82  	public boolean isIdle() {
83  		return idle;
84  	}
85  
86  	/**
87  	 * @return term of this leader. Valid only if {@link #getState()} is
88  	 *         currently {@link KetchLeader.State#LEADER}.
89  	 */
90  	public long getTerm() {
91  		return term;
92  	}
93  
94  	/**
95  	 * @return end of the leader's log; null if leader hasn't started up enough
96  	 *         to begin its own election.
97  	 */
98  	@Nullable
99  	public LogIndex getHead() {
100 		return headIndex;
101 	}
102 
103 	/**
104 	 * @return state the leader knows is committed on a majority of participant
105 	 *         replicas. Null until the leader instance has committed a log
106 	 *         index within its own term.
107 	 */
108 	@Nullable
109 	public LogIndex getCommitted() {
110 		return committedIndex;
111 	}
112 
113 	@Override
114 	public String toString() {
115 		StringBuilder s = new StringBuilder();
116 		s.append(isIdle() ? "IDLE" : "RUNNING"); //$NON-NLS-1$ //$NON-NLS-2$
117 		s.append(" state ").append(getState()); //$NON-NLS-1$
118 		if (getTerm() > 0) {
119 			s.append(" term ").append(getTerm()); //$NON-NLS-1$
120 		}
121 		s.append('\n');
122 		s.append(String.format(
123 				"%-10s %12s %12s\n", //$NON-NLS-1$
124 				"Replica", "Accepted", "Committed")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
125 		s.append("------------------------------------\n"); //$NON-NLS-1$
126 		debug(s, "(leader)", getHead(), getCommitted()); //$NON-NLS-1$
127 		s.append('\n');
128 		for (ReplicaSnapshot r : getReplicas()) {
129 			debug(s, r);
130 			s.append('\n');
131 		}
132 		s.append('\n');
133 		return s.toString();
134 	}
135 
136 	private static void debug(StringBuilder b, ReplicaSnapshot s) {
137 		KetchReplica replica = s.getReplica();
138 		debug(b, replica.getName(), s.getAccepted(), s.getCommitted());
139 		b.append(String.format(" %-8s %s", //$NON-NLS-1$
140 				replica.getParticipation(), s.getState()));
141 		if (s.getState() == OFFLINE) {
142 			String err = s.getErrorMessage();
143 			if (err != null) {
144 				b.append(" (").append(err).append(')'); //$NON-NLS-1$
145 			}
146 		}
147 	}
148 
149 	private static void debug(StringBuilder s, String name,
150 			ObjectId accepted, ObjectId committed) {
151 		s.append(String.format(
152 				"%-10s %-12s %-12s", //$NON-NLS-1$
153 				name, str(accepted), str(committed)));
154 	}
155 
156 	static String str(ObjectId c) {
157 		if (c instanceof LogIndex) {
158 			return ((LogIndex) c).describeForLog();
159 		} else if (c != null) {
160 			return c.abbreviate(8).name();
161 		}
162 		return "-"; //$NON-NLS-1$
163 	}
164 }