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 8.5KB

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