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

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