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.

ReftableBatchRefUpdate.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (C) 2019, 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.storage.reftable;
  44. import org.eclipse.jgit.annotations.Nullable;
  45. import org.eclipse.jgit.errors.MissingObjectException;
  46. import org.eclipse.jgit.internal.JGitText;
  47. import org.eclipse.jgit.lib.AnyObjectId;
  48. import org.eclipse.jgit.lib.BatchRefUpdate;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. import org.eclipse.jgit.lib.ObjectIdRef;
  51. import org.eclipse.jgit.lib.PersonIdent;
  52. import org.eclipse.jgit.lib.ProgressMonitor;
  53. import org.eclipse.jgit.lib.Ref;
  54. import org.eclipse.jgit.lib.RefDatabase;
  55. import org.eclipse.jgit.lib.ReflogEntry;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.lib.SymbolicRef;
  58. import org.eclipse.jgit.revwalk.RevObject;
  59. import org.eclipse.jgit.revwalk.RevTag;
  60. import org.eclipse.jgit.revwalk.RevWalk;
  61. import org.eclipse.jgit.transport.ReceiveCommand;
  62. import java.io.IOException;
  63. import java.util.ArrayList;
  64. import java.util.Collections;
  65. import java.util.HashMap;
  66. import java.util.List;
  67. import java.util.Map;
  68. import java.util.Set;
  69. import java.util.TreeSet;
  70. import java.util.concurrent.locks.Lock;
  71. import java.util.stream.Collectors;
  72. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  73. import static org.eclipse.jgit.lib.Ref.Storage.PACKED;
  74. import static org.eclipse.jgit.transport.ReceiveCommand.Result.LOCK_FAILURE;
  75. import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED;
  76. import static org.eclipse.jgit.transport.ReceiveCommand.Result.OK;
  77. import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_MISSING_OBJECT;
  78. import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_NONFASTFORWARD;
  79. import static org.eclipse.jgit.transport.ReceiveCommand.Type.DELETE;
  80. import static org.eclipse.jgit.transport.ReceiveCommand.Type.UPDATE_NONFASTFORWARD;
  81. /**
  82. * {@link org.eclipse.jgit.lib.BatchRefUpdate} for Reftable based RefDatabase.
  83. */
  84. public abstract class ReftableBatchRefUpdate extends BatchRefUpdate {
  85. private final Lock lock;
  86. private final ReftableDatabase refDb;
  87. private final Repository repository;
  88. /**
  89. * Initialize.
  90. *
  91. * @param refdb
  92. * The RefDatabase
  93. * @param reftableDb
  94. * The ReftableDatabase
  95. * @param lock
  96. * A lock protecting the refdatabase's state
  97. * @param repository
  98. * The repository on which this update will run
  99. */
  100. protected ReftableBatchRefUpdate(RefDatabase refdb, ReftableDatabase reftableDb, Lock lock,
  101. Repository repository) {
  102. super(refdb);
  103. this.refDb = reftableDb;
  104. this.lock = lock;
  105. this.repository = repository;
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public void execute(RevWalk rw, ProgressMonitor pm, List<String> options) {
  110. List<ReceiveCommand> pending = getPending();
  111. if (pending.isEmpty()) {
  112. return;
  113. }
  114. if (options != null) {
  115. setPushOptions(options);
  116. }
  117. try {
  118. if (!checkObjectExistence(rw, pending)) {
  119. return;
  120. }
  121. // if we are here, checkObjectExistence might have flagged some problems
  122. // but the transaction is not atomic, so we should proceed with the other
  123. // pending commands.
  124. pending = getPending();
  125. if (!checkNonFastForwards(rw, pending)) {
  126. return;
  127. }
  128. pending = getPending();
  129. lock.lock();
  130. try {
  131. if (!checkExpected(pending)) {
  132. return;
  133. }
  134. pending = getPending();
  135. if (!checkConflicting(pending)) {
  136. return;
  137. }
  138. pending = getPending();
  139. if (!blockUntilTimestamps(MAX_WAIT)) {
  140. return;
  141. }
  142. List<Ref> newRefs = toNewRefs(rw, pending);
  143. applyUpdates(newRefs, pending);
  144. for (ReceiveCommand cmd : pending) {
  145. if (cmd.getResult() == NOT_ATTEMPTED) {
  146. // XXX this is a bug in DFS ?
  147. cmd.setResult(OK);
  148. }
  149. }
  150. } finally {
  151. lock.unlock();
  152. }
  153. } catch (IOException e) {
  154. pending.get(0).setResult(LOCK_FAILURE, "io error"); //$NON-NLS-1$
  155. ReceiveCommand.abort(pending);
  156. }
  157. }
  158. /**
  159. * Implements the storage-specific part of the update.
  160. *
  161. * @param newRefs
  162. * the new refs to create
  163. * @param pending
  164. * the pending receive commands to be executed
  165. * @throws IOException
  166. * if any of the writes fail.
  167. */
  168. protected abstract void applyUpdates(List<Ref> newRefs,
  169. List<ReceiveCommand> pending) throws IOException;
  170. private List<ReceiveCommand> getPending() {
  171. return ReceiveCommand.filter(getCommands(), NOT_ATTEMPTED);
  172. }
  173. private boolean checkObjectExistence(RevWalk rw,
  174. List<ReceiveCommand> pending) throws IOException {
  175. for (ReceiveCommand cmd : pending) {
  176. try {
  177. if (!cmd.getNewId().equals(ObjectId.zeroId())) {
  178. rw.parseAny(cmd.getNewId());
  179. }
  180. } catch (MissingObjectException e) {
  181. // ReceiveCommand#setResult(Result) converts REJECTED to
  182. // REJECTED_NONFASTFORWARD, even though that result is also
  183. // used for a missing object. Eagerly handle this case so we
  184. // can set the right result.
  185. cmd.setResult(REJECTED_MISSING_OBJECT);
  186. if (isAtomic()) {
  187. ReceiveCommand.abort(pending);
  188. return false;
  189. }
  190. }
  191. }
  192. return true;
  193. }
  194. private boolean checkNonFastForwards(RevWalk rw,
  195. List<ReceiveCommand> pending) throws IOException {
  196. if (isAllowNonFastForwards()) {
  197. return true;
  198. }
  199. for (ReceiveCommand cmd : pending) {
  200. cmd.updateType(rw);
  201. if (cmd.getType() == UPDATE_NONFASTFORWARD) {
  202. cmd.setResult(REJECTED_NONFASTFORWARD);
  203. if (isAtomic()) {
  204. ReceiveCommand.abort(pending);
  205. return false;
  206. }
  207. }
  208. }
  209. return true;
  210. }
  211. private boolean checkConflicting(List<ReceiveCommand> pending)
  212. throws IOException {
  213. TreeSet<String> added = new TreeSet<>();
  214. Set<String> deleted =
  215. pending.stream()
  216. .filter(cmd -> cmd.getType() == DELETE)
  217. .map(c -> c.getRefName())
  218. .collect(Collectors.toSet());
  219. boolean ok = true;
  220. for (ReceiveCommand cmd : pending) {
  221. if (cmd.getType() == DELETE) {
  222. continue;
  223. }
  224. String name = cmd.getRefName();
  225. if (refDb.isNameConflicting(name, added, deleted)) {
  226. if (isAtomic()) {
  227. cmd.setResult(
  228. ReceiveCommand.Result.REJECTED_OTHER_REASON, JGitText.get().transactionAborted);
  229. } else {
  230. cmd.setResult(LOCK_FAILURE);
  231. }
  232. ok = false;
  233. }
  234. added.add(name);
  235. }
  236. if (isAtomic()) {
  237. if (!ok) {
  238. pending.stream()
  239. .filter(cmd -> cmd.getResult() == NOT_ATTEMPTED)
  240. .forEach(cmd -> cmd.setResult(LOCK_FAILURE));
  241. }
  242. return ok;
  243. }
  244. for (ReceiveCommand cmd : pending) {
  245. if (cmd.getResult() == NOT_ATTEMPTED) {
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. private boolean checkExpected(List<ReceiveCommand> pending)
  252. throws IOException {
  253. for (ReceiveCommand cmd : pending) {
  254. if (!matchOld(cmd, refDb.exactRef(cmd.getRefName()))) {
  255. cmd.setResult(LOCK_FAILURE);
  256. if (isAtomic()) {
  257. ReceiveCommand.abort(pending);
  258. return false;
  259. }
  260. }
  261. }
  262. return true;
  263. }
  264. private static boolean matchOld(ReceiveCommand cmd, @Nullable Ref ref) {
  265. if (ref == null) {
  266. return AnyObjectId.isEqual(ObjectId.zeroId(), cmd.getOldId())
  267. && cmd.getOldSymref() == null;
  268. } else if (ref.isSymbolic()) {
  269. return ref.getTarget().getName().equals(cmd.getOldSymref());
  270. }
  271. ObjectId id = ref.getObjectId();
  272. if (id == null) {
  273. id = ObjectId.zeroId();
  274. }
  275. return cmd.getOldId().equals(id);
  276. }
  277. /**
  278. * Writes the refs to the writer, and calls finish.
  279. *
  280. * @param writer
  281. * the writer on which we should write.
  282. * @param newRefs
  283. * the ref data to write..
  284. * @param pending
  285. * the log data to write.
  286. * @throws IOException
  287. * in case of problems.
  288. */
  289. protected void write(ReftableWriter writer, List<Ref> newRefs,
  290. List<ReceiveCommand> pending) throws IOException {
  291. long updateIndex = refDb.nextUpdateIndex();
  292. writer.setMinUpdateIndex(updateIndex).setMaxUpdateIndex(updateIndex)
  293. .begin().sortAndWriteRefs(newRefs);
  294. if (!isRefLogDisabled()) {
  295. writeLog(writer, updateIndex, pending);
  296. }
  297. }
  298. private void writeLog(ReftableWriter writer, long updateIndex,
  299. List<ReceiveCommand> pending) throws IOException {
  300. Map<String, ReceiveCommand> cmds = new HashMap<>();
  301. List<String> byName = new ArrayList<>(pending.size());
  302. for (ReceiveCommand cmd : pending) {
  303. cmds.put(cmd.getRefName(), cmd);
  304. byName.add(cmd.getRefName());
  305. }
  306. Collections.sort(byName);
  307. PersonIdent ident = getRefLogIdent();
  308. if (ident == null) {
  309. ident = new PersonIdent(repository);
  310. }
  311. for (String name : byName) {
  312. ReceiveCommand cmd = cmds.get(name);
  313. if (isRefLogDisabled(cmd)) {
  314. continue;
  315. }
  316. String msg = getRefLogMessage(cmd);
  317. if (isRefLogIncludingResult(cmd)) {
  318. String strResult = toResultString(cmd);
  319. if (strResult != null) {
  320. msg = msg.isEmpty() ? strResult : msg + ": " + strResult; //$NON-NLS-1$
  321. }
  322. }
  323. writer.writeLog(name, updateIndex, ident, cmd.getOldId(),
  324. cmd.getNewId(), msg);
  325. }
  326. }
  327. private String toResultString(ReceiveCommand cmd) {
  328. switch (cmd.getType()) {
  329. case CREATE:
  330. return ReflogEntry.PREFIX_CREATED;
  331. case UPDATE:
  332. // Match the behavior of a single RefUpdate. In that case, setting
  333. // the force bit completely bypasses the potentially expensive
  334. // isMergedInto check, by design, so the reflog message may be
  335. // inaccurate.
  336. //
  337. // Similarly, this class bypasses the isMergedInto checks when the
  338. // force bit is set, meaning we can't actually distinguish between
  339. // UPDATE and UPDATE_NONFASTFORWARD when isAllowNonFastForwards()
  340. // returns true.
  341. return isAllowNonFastForwards() ? ReflogEntry.PREFIX_FORCED_UPDATE
  342. : ReflogEntry.PREFIX_FAST_FORWARD;
  343. case UPDATE_NONFASTFORWARD:
  344. return ReflogEntry.PREFIX_FORCED_UPDATE;
  345. default:
  346. return null;
  347. }
  348. }
  349. // Extracts and peels the refs out of the ReceiveCommands
  350. private static List<Ref> toNewRefs(RevWalk rw, List<ReceiveCommand> pending)
  351. throws IOException {
  352. List<Ref> refs = new ArrayList<>(pending.size());
  353. for (ReceiveCommand cmd : pending) {
  354. if (cmd.getResult() != NOT_ATTEMPTED) {
  355. continue;
  356. }
  357. String name = cmd.getRefName();
  358. ObjectId newId = cmd.getNewId();
  359. String newSymref = cmd.getNewSymref();
  360. if (AnyObjectId.isEqual(ObjectId.zeroId(), newId)
  361. && newSymref == null) {
  362. refs.add(new ObjectIdRef.Unpeeled(NEW, name, null));
  363. continue;
  364. } else if (newSymref != null) {
  365. refs.add(new SymbolicRef(name,
  366. new ObjectIdRef.Unpeeled(NEW, newSymref, null)));
  367. continue;
  368. }
  369. RevObject obj = rw.parseAny(newId);
  370. RevObject peel = null;
  371. if (obj instanceof RevTag) {
  372. peel = rw.peel(obj);
  373. }
  374. if (peel != null) {
  375. refs.add(new ObjectIdRef.PeeledTag(PACKED, name, newId,
  376. peel.copy()));
  377. } else {
  378. refs.add(new ObjectIdRef.PeeledNonTag(PACKED, name, newId));
  379. }
  380. }
  381. return refs;
  382. }
  383. }