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.CommitMethod.ALL_REFS;
47  import static org.eclipse.jgit.internal.ketch.KetchReplica.CommitMethod.TXN_COMMITTED;
48  import static org.eclipse.jgit.lib.RefDatabase.ALL;
49  import static org.eclipse.jgit.transport.ReceiveCommand.Result.OK;
50  import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_REASON;
51  
52  import java.io.IOException;
53  import java.text.MessageFormat;
54  import java.util.ArrayList;
55  import java.util.Collection;
56  import java.util.List;
57  import java.util.Map;
58  
59  import org.eclipse.jgit.internal.storage.reftree.RefTreeDatabase;
60  import org.eclipse.jgit.lib.BatchRefUpdate;
61  import org.eclipse.jgit.lib.NullProgressMonitor;
62  import org.eclipse.jgit.lib.Ref;
63  import org.eclipse.jgit.lib.RefDatabase;
64  import org.eclipse.jgit.lib.Repository;
65  import org.eclipse.jgit.revwalk.RevWalk;
66  import org.eclipse.jgit.transport.ReceiveCommand;
67  import org.eclipse.jgit.util.time.MonotonicClock;
68  import org.eclipse.jgit.util.time.ProposedTimestamp;
69  
70  /**
71   * Ketch replica running on the same system as the
72   * {@link org.eclipse.jgit.internal.ketch.KetchLeader}.
73   */
74  public class LocalReplica extends KetchReplica {
75  	/**
76  	 * Configure a local replica.
77  	 *
78  	 * @param leader
79  	 *            instance this replica follows.
80  	 * @param name
81  	 *            unique-ish name identifying this replica for debugging.
82  	 * @param cfg
83  	 *            how Ketch should treat the local system.
84  	 */
85  	public LocalReplica(KetchLeader leader, String name, ReplicaConfig cfg) {
86  		super(leader, name, cfg);
87  	}
88  
89  	/** {@inheritDoc} */
90  	@Override
91  	protected String describeForLog() {
92  		return String.format("%s (leader)", getName()); //$NON-NLS-1$
93  	}
94  
95  	/**
96  	 * Initializes local replica by reading accepted and committed references.
97  	 * <p>
98  	 * Loads accepted and committed references from the reference database of
99  	 * the local replica and stores their current ObjectIds in memory.
100 	 *
101 	 * @param repo
102 	 *            repository to initialize state from.
103 	 * @throws IOException
104 	 *             cannot read repository state.
105 	 */
106 	void initialize(Repository repo) throws IOException {
107 		RefDatabase refdb = repo.getRefDatabase();
108 		if (refdb instanceof RefTreeDatabase) {
109 			RefTreeDatabase treeDb = (RefTreeDatabase) refdb;
110 			String txnNamespace = getSystem().getTxnNamespace();
111 			if (!txnNamespace.equals(treeDb.getTxnNamespace())) {
112 				throw new IOException(MessageFormat.format(
113 						KetchText.get().mismatchedTxnNamespace,
114 						txnNamespace, treeDb.getTxnNamespace()));
115 			}
116 			refdb = treeDb.getBootstrap();
117 		}
118 		initialize(refdb.exactRef(
119 				getSystem().getTxnAccepted(),
120 				getSystem().getTxnCommitted()));
121 	}
122 
123 	/** {@inheritDoc} */
124 	@Override
125 	protected void startPush(ReplicaPushRequest req) {
126 		getSystem().getExecutor().execute(new Runnable() {
127 			@Override
128 			public void run() {
129 				MonotonicClock clk = getSystem().getClock();
130 				try (Repository git = getLeader().openRepository();
131 						ProposedTimestamp ts = clk.propose()) {
132 					try {
133 						update(git, req, ts);
134 						req.done(git);
135 					} catch (Throwable err) {
136 						req.setException(git, err);
137 					}
138 				} catch (IOException err) {
139 					req.setException(null, err);
140 				}
141 			}
142 		});
143 	}
144 
145 	/** {@inheritDoc} */
146 	@Override
147 	protected void blockingFetch(Repository repo, ReplicaFetchRequest req)
148 			throws IOException {
149 		throw new IOException(KetchText.get().cannotFetchFromLocalReplica);
150 	}
151 
152 	private void update(Repository git, ReplicaPushRequest req,
153 			ProposedTimestamp ts) throws IOException {
154 		RefDatabase refdb = git.getRefDatabase();
155 		CommitMethod method = getCommitMethod();
156 
157 		// Local replica probably uses RefTreeDatabase, the request should
158 		// be only for the txnNamespace, so drop to the bootstrap layer.
159 		if (refdb instanceof RefTreeDatabase) {
160 			if (!isOnlyTxnNamespace(req.getCommands())) {
161 				return;
162 			}
163 
164 			refdb = ((RefTreeDatabase) refdb).getBootstrap();
165 			method = TXN_COMMITTED;
166 		}
167 
168 		BatchRefUpdate batch = refdb.newBatchUpdate();
169 		batch.addProposedTimestamp(ts);
170 		batch.setRefLogIdent(getSystem().newCommitter(ts));
171 		batch.setRefLogMessage("ketch", false); //$NON-NLS-1$
172 		batch.setAllowNonFastForwards(true);
173 
174 		// RefDirectory updates multiple references sequentially.
175 		// Run everything else first, then accepted (if present),
176 		// then committed (if present). This ensures an earlier
177 		// failure will not update these critical references.
178 		ReceiveCommand accepted = null;
179 		ReceiveCommand committed = null;
180 		for (ReceiveCommand cmd : req.getCommands()) {
181 			String name = cmd.getRefName();
182 			if (name.equals(getSystem().getTxnAccepted())) {
183 				accepted = cmd;
184 			} else if (name.equals(getSystem().getTxnCommitted())) {
185 				committed = cmd;
186 			} else {
187 				batch.addCommand(cmd);
188 			}
189 		}
190 		if (committed != null && method == ALL_REFS) {
191 			Map<String, Ref> refs = refdb.getRefs(ALL);
192 			batch.addCommand(prepareCommit(git, refs, committed.getNewId()));
193 		}
194 		if (accepted != null) {
195 			batch.addCommand(accepted);
196 		}
197 		if (committed != null) {
198 			batch.addCommand(committed);
199 		}
200 
201 		try (RevWalk rw = new RevWalk(git)) {
202 			batch.execute(rw, NullProgressMonitor.INSTANCE);
203 		}
204 
205 		// KetchReplica only cares about accepted and committed in
206 		// advertisement. If they failed, store the current values
207 		// back in the ReplicaPushRequest.
208 		List<String> failed = new ArrayList<>(2);
209 		checkFailed(failed, accepted);
210 		checkFailed(failed, committed);
211 		if (!failed.isEmpty()) {
212 			String[] arr = failed.toArray(new String[failed.size()]);
213 			req.setRefs(refdb.exactRef(arr));
214 		}
215 	}
216 
217 	private static void checkFailed(List<String> failed, ReceiveCommand cmd) {
218 		if (cmd != null && cmd.getResult() != OK) {
219 			failed.add(cmd.getRefName());
220 		}
221 	}
222 
223 	private boolean isOnlyTxnNamespace(Collection<ReceiveCommand> cmdList) {
224 		// Be paranoid and reject non txnNamespace names, this
225 		// is a programming error in Ketch that should not occur.
226 
227 		String txnNamespace = getSystem().getTxnNamespace();
228 		for (ReceiveCommand cmd : cmdList) {
229 			if (!cmd.getRefName().startsWith(txnNamespace)) {
230 				cmd.setResult(REJECTED_OTHER_REASON,
231 						MessageFormat.format(
232 								KetchText.get().outsideTxnNamespace,
233 								cmd.getRefName(), txnNamespace));
234 				ReceiveCommand.abort(cmdList);
235 				return false;
236 			}
237 		}
238 		return true;
239 	}
240 }