Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LocalReplica.java 8.1KB

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