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.

LocalReplica.java 8.0KB

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