You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoteGitReplica.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. package org.eclipse.jgit.internal.ketch;
  44. import static org.eclipse.jgit.internal.ketch.KetchReplica.CommitMethod.ALL_REFS;
  45. import static org.eclipse.jgit.lib.Ref.Storage.NETWORK;
  46. import static org.eclipse.jgit.transport.ReceiveCommand.Result.LOCK_FAILURE;
  47. import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED;
  48. import static org.eclipse.jgit.transport.ReceiveCommand.Result.OK;
  49. import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_NODELETE;
  50. import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_NONFASTFORWARD;
  51. import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_REASON;
  52. import java.io.IOException;
  53. import java.util.ArrayList;
  54. import java.util.Collection;
  55. import java.util.Collections;
  56. import java.util.LinkedHashMap;
  57. import java.util.List;
  58. import java.util.Map;
  59. import org.eclipse.jgit.annotations.Nullable;
  60. import org.eclipse.jgit.errors.NotSupportedException;
  61. import org.eclipse.jgit.errors.TransportException;
  62. import org.eclipse.jgit.lib.AnyObjectId;
  63. import org.eclipse.jgit.lib.NullProgressMonitor;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.ObjectIdRef;
  66. import org.eclipse.jgit.lib.Ref;
  67. import org.eclipse.jgit.lib.Repository;
  68. import org.eclipse.jgit.transport.FetchConnection;
  69. import org.eclipse.jgit.transport.PushConnection;
  70. import org.eclipse.jgit.transport.ReceiveCommand;
  71. import org.eclipse.jgit.transport.RemoteConfig;
  72. import org.eclipse.jgit.transport.RemoteRefUpdate;
  73. import org.eclipse.jgit.transport.Transport;
  74. import org.eclipse.jgit.transport.URIish;
  75. /**
  76. * Representation of a Git repository on a remote replica system.
  77. * <p>
  78. * {@link KetchLeader} will contact the replica using the Git wire protocol.
  79. * <p>
  80. * The remote replica may be fully Ketch-aware, or a standard Git server.
  81. */
  82. public class RemoteGitReplica extends KetchReplica {
  83. private final URIish uri;
  84. private final RemoteConfig remoteConfig;
  85. /**
  86. * Configure a new remote.
  87. *
  88. * @param leader
  89. * instance this replica follows.
  90. * @param name
  91. * unique-ish name identifying this remote for debugging.
  92. * @param uri
  93. * URI to connect to the follower's repository.
  94. * @param cfg
  95. * how Ketch should treat the remote system.
  96. * @param rc
  97. * optional remote configuration describing how to contact the
  98. * peer repository.
  99. */
  100. public RemoteGitReplica(KetchLeader leader, String name, URIish uri,
  101. ReplicaConfig cfg, @Nullable RemoteConfig rc) {
  102. super(leader, name, cfg);
  103. this.uri = uri;
  104. this.remoteConfig = rc;
  105. }
  106. /** @return URI to contact the remote peer repository. */
  107. public URIish getURI() {
  108. return uri;
  109. }
  110. /** @return optional configuration describing how to contact the peer. */
  111. @Nullable
  112. protected RemoteConfig getRemoteConfig() {
  113. return remoteConfig;
  114. }
  115. @Override
  116. protected String describeForLog() {
  117. return String.format("%s @ %s", getName(), getURI()); //$NON-NLS-1$
  118. }
  119. @Override
  120. protected void startPush(final ReplicaPushRequest req) {
  121. getSystem().getExecutor().execute(new Runnable() {
  122. @Override
  123. public void run() {
  124. try (Repository git = getLeader().openRepository()) {
  125. try {
  126. push(git, req);
  127. req.done(git);
  128. } catch (Throwable err) {
  129. req.setException(git, err);
  130. }
  131. } catch (IOException err) {
  132. req.setException(null, err);
  133. }
  134. }
  135. });
  136. }
  137. private void push(Repository repo, ReplicaPushRequest req)
  138. throws NotSupportedException, TransportException, IOException {
  139. Map<String, Ref> adv;
  140. List<RemoteCommand> cmds = asUpdateList(req.getCommands());
  141. try (Transport transport = Transport.open(repo, uri)) {
  142. RemoteConfig rc = getRemoteConfig();
  143. if (rc != null) {
  144. transport.applyConfig(rc);
  145. }
  146. transport.setPushAtomic(true);
  147. adv = push(repo, transport, cmds);
  148. }
  149. for (RemoteCommand c : cmds) {
  150. c.copyStatusToResult();
  151. }
  152. req.setRefs(adv);
  153. }
  154. private Map<String, Ref> push(Repository git, Transport transport,
  155. List<RemoteCommand> cmds) throws IOException {
  156. Map<String, RemoteRefUpdate> updates = asUpdateMap(cmds);
  157. try (PushConnection connection = transport.openPush()) {
  158. Map<String, Ref> adv = connection.getRefsMap();
  159. RemoteRefUpdate accepted = updates.get(getSystem().getTxnAccepted());
  160. if (accepted != null && !isExpectedValue(adv, accepted)) {
  161. abort(cmds);
  162. return adv;
  163. }
  164. RemoteRefUpdate committed = updates.get(getSystem().getTxnCommitted());
  165. if (committed != null && !isExpectedValue(adv, committed)) {
  166. abort(cmds);
  167. return adv;
  168. }
  169. if (committed != null && getCommitMethod() == ALL_REFS) {
  170. prepareCommit(git, cmds, updates, adv,
  171. committed.getNewObjectId());
  172. }
  173. connection.push(NullProgressMonitor.INSTANCE, updates);
  174. return adv;
  175. }
  176. }
  177. private static boolean isExpectedValue(Map<String, Ref> adv,
  178. RemoteRefUpdate u) {
  179. Ref r = adv.get(u.getRemoteName());
  180. if (!AnyObjectId.equals(getId(r), u.getExpectedOldObjectId())) {
  181. ((RemoteCommand) u).cmd.setResult(LOCK_FAILURE);
  182. return false;
  183. }
  184. return true;
  185. }
  186. private void prepareCommit(Repository git, List<RemoteCommand> cmds,
  187. Map<String, RemoteRefUpdate> updates, Map<String, Ref> adv,
  188. ObjectId committed) throws IOException {
  189. for (ReceiveCommand cmd : prepareCommit(git, adv, committed)) {
  190. RemoteCommand c = new RemoteCommand(cmd);
  191. cmds.add(c);
  192. updates.put(c.getRemoteName(), c);
  193. }
  194. }
  195. private static List<RemoteCommand> asUpdateList(
  196. Collection<ReceiveCommand> cmds) {
  197. try {
  198. List<RemoteCommand> toPush = new ArrayList<>(cmds.size());
  199. for (ReceiveCommand cmd : cmds) {
  200. toPush.add(new RemoteCommand(cmd));
  201. }
  202. return toPush;
  203. } catch (IOException e) {
  204. // Cannot occur as no IO was required to build the command.
  205. throw new IllegalStateException(e);
  206. }
  207. }
  208. private static Map<String, RemoteRefUpdate> asUpdateMap(
  209. List<RemoteCommand> cmds) {
  210. Map<String, RemoteRefUpdate> m = new LinkedHashMap<>();
  211. for (RemoteCommand cmd : cmds) {
  212. m.put(cmd.getRemoteName(), cmd);
  213. }
  214. return m;
  215. }
  216. private static void abort(List<RemoteCommand> cmds) {
  217. List<ReceiveCommand> tmp = new ArrayList<>(cmds.size());
  218. for (RemoteCommand cmd : cmds) {
  219. tmp.add(cmd.cmd);
  220. }
  221. ReceiveCommand.abort(tmp);
  222. }
  223. protected void blockingFetch(Repository repo, ReplicaFetchRequest req)
  224. throws NotSupportedException, TransportException {
  225. try (Transport transport = Transport.open(repo, uri)) {
  226. RemoteConfig rc = getRemoteConfig();
  227. if (rc != null) {
  228. transport.applyConfig(rc);
  229. }
  230. fetch(transport, req);
  231. }
  232. }
  233. private void fetch(Transport transport, ReplicaFetchRequest req)
  234. throws NotSupportedException, TransportException {
  235. try (FetchConnection conn = transport.openFetch()) {
  236. Map<String, Ref> remoteRefs = conn.getRefsMap();
  237. req.setRefs(remoteRefs);
  238. List<Ref> want = new ArrayList<>();
  239. for (String name : req.getWantRefs()) {
  240. Ref ref = remoteRefs.get(name);
  241. if (ref != null && ref.getObjectId() != null) {
  242. want.add(ref);
  243. }
  244. }
  245. for (ObjectId id : req.getWantObjects()) {
  246. want.add(new ObjectIdRef.Unpeeled(NETWORK, id.name(), id));
  247. }
  248. conn.fetch(NullProgressMonitor.INSTANCE, want,
  249. Collections.<ObjectId> emptySet());
  250. }
  251. }
  252. static class RemoteCommand extends RemoteRefUpdate {
  253. final ReceiveCommand cmd;
  254. RemoteCommand(ReceiveCommand cmd) throws IOException {
  255. super(null, null,
  256. cmd.getNewId(), cmd.getRefName(),
  257. true /* force update */,
  258. null /* no local tracking ref */,
  259. cmd.getOldId());
  260. this.cmd = cmd;
  261. }
  262. void copyStatusToResult() {
  263. if (cmd.getResult() == NOT_ATTEMPTED) {
  264. switch (getStatus()) {
  265. case OK:
  266. case UP_TO_DATE:
  267. case NON_EXISTING:
  268. cmd.setResult(OK);
  269. break;
  270. case REJECTED_NODELETE:
  271. cmd.setResult(REJECTED_NODELETE);
  272. break;
  273. case REJECTED_NONFASTFORWARD:
  274. cmd.setResult(REJECTED_NONFASTFORWARD);
  275. break;
  276. case REJECTED_OTHER_REASON:
  277. cmd.setResult(REJECTED_OTHER_REASON, getMessage());
  278. break;
  279. default:
  280. cmd.setResult(REJECTED_OTHER_REASON, getStatus().name());
  281. break;
  282. }
  283. }
  284. }
  285. }
  286. }