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