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.KetchConstants.ACCEPTED;
47  import static org.eclipse.jgit.internal.ketch.KetchConstants.COMMITTED;
48  import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_TYPE;
49  import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_SECTION_KETCH;
50  import static org.eclipse.jgit.internal.ketch.KetchConstants.DEFAULT_TXN_NAMESPACE;
51  import static org.eclipse.jgit.internal.ketch.KetchConstants.STAGE;
52  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_NAME;
53  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_REMOTE;
54  
55  import java.net.URISyntaxException;
56  import java.time.Duration;
57  import java.util.ArrayList;
58  import java.util.List;
59  import java.util.Random;
60  import java.util.concurrent.Executors;
61  import java.util.concurrent.ScheduledExecutorService;
62  import java.util.concurrent.ThreadFactory;
63  import java.util.concurrent.atomic.AtomicInteger;
64  
65  import org.eclipse.jgit.annotations.Nullable;
66  import org.eclipse.jgit.lib.Config;
67  import org.eclipse.jgit.lib.PersonIdent;
68  import org.eclipse.jgit.lib.Repository;
69  import org.eclipse.jgit.transport.RemoteConfig;
70  import org.eclipse.jgit.transport.URIish;
71  import org.eclipse.jgit.util.time.MonotonicClock;
72  import org.eclipse.jgit.util.time.MonotonicSystemClock;
73  import org.eclipse.jgit.util.time.ProposedTimestamp;
74  import org.slf4j.Logger;
75  import org.slf4j.LoggerFactory;
76  
77  /**
78   * Ketch system-wide configuration.
79   * <p>
80   * This class provides useful defaults for testing and small proof of concepts.
81   * Full scale installations are expected to subclass and override methods to
82   * provide consistent configuration across all managed repositories.
83   * <p>
84   * Servers should configure their own {@link ScheduledExecutorService}.
85   */
86  public class KetchSystem {
87  	private static final Random RNG = new Random();
88  
89  	/** @return default executor, one thread per available processor. */
90  	public static ScheduledExecutorService defaultExecutor() {
91  		return DefaultExecutorHolder.I;
92  	}
93  
94  	private final ScheduledExecutorService executor;
95  	private final MonotonicClock clock;
96  	private final String txnNamespace;
97  	private final String txnAccepted;
98  	private final String txnCommitted;
99  	private final String txnStage;
100 
101 	/** Create a default system with a thread pool of 1 thread per CPU. */
102 	public KetchSystem() {
103 		this(defaultExecutor(), new MonotonicSystemClock(), DEFAULT_TXN_NAMESPACE);
104 	}
105 
106 	/**
107 	 * Create a Ketch system with the provided executor service.
108 	 *
109 	 * @param executor
110 	 *            thread pool to run background operations.
111 	 * @param clock
112 	 *            clock to create timestamps.
113 	 * @param txnNamespace
114 	 *            reference namespace for the RefTree graph and associated
115 	 *            transaction state. Must begin with {@code "refs/"} and end
116 	 *            with {@code '/'}, for example {@code "refs/txn/"}.
117 	 */
118 	public KetchSystem(ScheduledExecutorService executor, MonotonicClock clock,
119 			String txnNamespace) {
120 		this.executor = executor;
121 		this.clock = clock;
122 		this.txnNamespace = txnNamespace;
123 		this.txnAccepted = txnNamespace + ACCEPTED;
124 		this.txnCommitted = txnNamespace + COMMITTED;
125 		this.txnStage = txnNamespace + STAGE;
126 	}
127 
128 	/** @return executor to perform background operations. */
129 	public ScheduledExecutorService getExecutor() {
130 		return executor;
131 	}
132 
133 	/** @return clock to obtain timestamps from. */
134 	public MonotonicClock getClock() {
135 		return clock;
136 	}
137 
138 	/**
139 	 * @return how long the leader will wait for the {@link #getClock()}'s
140 	 *         {@code ProposedTimestamp} used in commits proposed to the RefTree
141 	 *         graph ({@link #getTxnAccepted()}). Defaults to 5 seconds.
142 	 */
143 	public Duration getMaxWaitForMonotonicClock() {
144 		return Duration.ofSeconds(5);
145 	}
146 
147 	/**
148 	 * @return true if elections should require monotonically increasing commit
149 	 *         timestamps. This requires a very good {@link MonotonicClock}.
150 	 */
151 	public boolean requireMonotonicLeaderElections() {
152 		return false;
153 	}
154 
155 	/**
156 	 * Get the namespace used for the RefTree graph and transaction management.
157 	 *
158 	 * @return reference namespace such as {@code "refs/txn/"}.
159 	 */
160 	public String getTxnNamespace() {
161 		return txnNamespace;
162 	}
163 
164 	/** @return name of the accepted RefTree graph. */
165 	public String getTxnAccepted() {
166 		return txnAccepted;
167 	}
168 
169 	/** @return name of the committed RefTree graph. */
170 	public String getTxnCommitted() {
171 		return txnCommitted;
172 	}
173 
174 	/** @return prefix for staged objects, e.g. {@code "refs/txn/stage/"}. */
175 	public String getTxnStage() {
176 		return txnStage;
177 	}
178 
179 	/**
180 	 * @param time
181 	 *            timestamp for the committer.
182 	 * @return identity line for the committer header of a RefTreeGraph.
183 	 */
184 	public PersonIdent newCommitter(ProposedTimestamp time) {
185 		String name = "ketch"; //$NON-NLS-1$
186 		String email = "ketch@system"; //$NON-NLS-1$
187 		return new PersonIdent(name, email, time);
188 	}
189 
190 	/**
191 	 * Construct a random tag to identify a candidate during leader election.
192 	 * <p>
193 	 * Multiple processes trying to elect themselves leaders at exactly the same
194 	 * time (rounded to seconds) using the same
195 	 * {@link #newCommitter(ProposedTimestamp)} identity strings, for the same
196 	 * term, may generate the same ObjectId for the election commit and falsely
197 	 * assume they have both won.
198 	 * <p>
199 	 * Candidates add this tag to their election ballot commit to disambiguate
200 	 * the election. The tag only needs to be unique for a given triplet of
201 	 * {@link #newCommitter(ProposedTimestamp)}, system time (rounded to
202 	 * seconds), and term. If every replica in the system uses a unique
203 	 * {@code newCommitter} (such as including the host name after the
204 	 * {@code "@"} in the email address) the tag could be the empty string.
205 	 * <p>
206 	 * The default implementation generates a few bytes of random data.
207 	 *
208 	 * @return unique tag; null or empty string if {@code newCommitter()} is
209 	 *         sufficiently unique to identify the leader.
210 	 */
211 	@Nullable
212 	public String newLeaderTag() {
213 		int n = RNG.nextInt(1 << (6 * 4));
214 		return String.format("%06x", Integer.valueOf(n)); //$NON-NLS-1$
215 	}
216 
217 	/**
218 	 * Construct the KetchLeader instance of a repository.
219 	 *
220 	 * @param repo
221 	 *            local repository stored by the leader.
222 	 * @return leader instance.
223 	 * @throws URISyntaxException
224 	 *             a follower configuration contains an unsupported URI.
225 	 */
226 	public KetchLeader createLeader(final Repository repo)
227 			throws URISyntaxException {
228 		KetchLeader leader = new KetchLeader(this) {
229 			@Override
230 			protected Repository openRepository() {
231 				repo.incrementOpen();
232 				return repo;
233 			}
234 		};
235 		leader.setReplicas(createReplicas(leader, repo));
236 		return leader;
237 	}
238 
239 	/**
240 	 * Get the collection of replicas for a repository.
241 	 * <p>
242 	 * The collection of replicas must include the local repository.
243 	 *
244 	 * @param leader
245 	 *            the leader driving these replicas.
246 	 * @param repo
247 	 *            repository to get the replicas of.
248 	 * @return collection of replicas for the specified repository.
249 	 * @throws URISyntaxException
250 	 *             a configured URI is invalid.
251 	 */
252 	protected List<KetchReplica> createReplicas(KetchLeader leader,
253 			Repository repo) throws URISyntaxException {
254 		List<KetchReplica> replicas = new ArrayList<>();
255 		Config cfg = repo.getConfig();
256 		String localName = getLocalName(cfg);
257 		for (String name : cfg.getSubsections(CONFIG_KEY_REMOTE)) {
258 			if (!hasParticipation(cfg, name)) {
259 				continue;
260 			}
261 
262 			ReplicaConfig kc = ReplicaConfig.newFromConfig(cfg, name);
263 			if (name.equals(localName)) {
264 				replicas.add(new LocalReplica(leader, name, kc));
265 				continue;
266 			}
267 
268 			RemoteConfig rc = new RemoteConfig(cfg, name);
269 			List<URIish> uris = rc.getPushURIs();
270 			if (uris.isEmpty()) {
271 				uris = rc.getURIs();
272 			}
273 			for (URIish uri : uris) {
274 				String n = uris.size() == 1 ? name : uri.getHost();
275 				replicas.add(new RemoteGitReplica(leader, n, uri, kc, rc));
276 			}
277 		}
278 		return replicas;
279 	}
280 
281 	private static boolean hasParticipation(Config cfg, String name) {
282 		return cfg.getString(CONFIG_KEY_REMOTE, name, CONFIG_KEY_TYPE) != null;
283 	}
284 
285 	private static String getLocalName(Config cfg) {
286 		return cfg.getString(CONFIG_SECTION_KETCH, null, CONFIG_KEY_NAME);
287 	}
288 
289 	static class DefaultExecutorHolder {
290 		private static final Logger log = LoggerFactory.getLogger(KetchSystem.class);
291 		static final ScheduledExecutorService I = create();
292 
293 		private static ScheduledExecutorService create() {
294 			int cores = Runtime.getRuntime().availableProcessors();
295 			int threads = Math.max(5, cores);
296 			log.info("Using {} threads", Integer.valueOf(threads)); //$NON-NLS-1$
297 			return Executors.newScheduledThreadPool(
298 				threads,
299 				new ThreadFactory() {
300 					private final AtomicInteger threadCnt = new AtomicInteger();
301 
302 					@Override
303 					public Thread newThread(Runnable r) {
304 						int id = threadCnt.incrementAndGet();
305 						Thread thr = new Thread(r);
306 						thr.setName("KetchExecutor-" + id); //$NON-NLS-1$
307 						return thr;
308 					}
309 				});
310 		}
311 
312 		private DefaultExecutorHolder() {
313 		}
314 	}
315 
316 	/**
317 	 * Compute a delay in a {@code min..max} interval with random jitter.
318 	 *
319 	 * @param last
320 	 *            amount of delay waited before the last attempt. This is used
321 	 *            to seed the next delay interval. Should be 0 if there was no
322 	 *            prior delay.
323 	 * @param min
324 	 *            shortest amount of allowable delay between attempts.
325 	 * @param max
326 	 *            longest amount of allowable delay between attempts.
327 	 * @return new amount of delay to wait before the next attempt.
328 	 */
329 	static long delay(long last, long min, long max) {
330 		long r = Math.max(0, last * 3 - min);
331 		if (r > 0) {
332 			int c = (int) Math.min(r + 1, Integer.MAX_VALUE);
333 			r = RNG.nextInt(c);
334 		}
335 		return Math.max(Math.min(min + r, max), min);
336 	}
337 }