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.

LagCheck.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.State.AHEAD;
  45. import static org.eclipse.jgit.internal.ketch.KetchReplica.State.DIVERGENT;
  46. import static org.eclipse.jgit.internal.ketch.KetchReplica.State.LAGGING;
  47. import static org.eclipse.jgit.internal.ketch.KetchReplica.State.UNKNOWN;
  48. import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
  49. import java.io.IOException;
  50. import java.util.Collections;
  51. import java.util.Map;
  52. import org.eclipse.jgit.errors.MissingObjectException;
  53. import org.eclipse.jgit.lib.AnyObjectId;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.Ref;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.revwalk.RevCommit;
  58. import org.eclipse.jgit.revwalk.RevWalk;
  59. import org.eclipse.jgit.transport.ReceiveCommand;
  60. /**
  61. * A helper to check if a {@link KetchReplica} is ahead or behind the leader.
  62. */
  63. class LagCheck implements AutoCloseable {
  64. private final KetchReplica replica;
  65. private final Repository repo;
  66. private RevWalk rw;
  67. private ObjectId remoteId;
  68. LagCheck(KetchReplica replica, Repository repo) {
  69. this.replica = replica;
  70. this.repo = repo;
  71. initRevWalk();
  72. }
  73. private void initRevWalk() {
  74. if (rw != null) {
  75. rw.close();
  76. }
  77. rw = new RevWalk(repo);
  78. rw.setRetainBody(false);
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public void close() {
  83. if (rw != null) {
  84. rw.close();
  85. rw = null;
  86. }
  87. }
  88. ObjectId getRemoteId() {
  89. return remoteId;
  90. }
  91. KetchReplica.State check(ObjectId acceptId, ReceiveCommand acceptCmd) {
  92. remoteId = acceptId;
  93. if (remoteId == null) {
  94. // Nothing advertised by the replica, value is unknown.
  95. return UNKNOWN;
  96. }
  97. if (AnyObjectId.isEqual(remoteId, ObjectId.zeroId())) {
  98. // Replica does not have the txnAccepted reference.
  99. return LAGGING;
  100. }
  101. try {
  102. RevCommit remote;
  103. try {
  104. remote = parseRemoteCommit(acceptCmd.getRefName());
  105. } catch (RefGoneException gone) {
  106. // Replica does not have the txnAccepted reference.
  107. return LAGGING;
  108. } catch (MissingObjectException notFound) {
  109. // Local repository does not know this commit so it cannot
  110. // be including the replica's log.
  111. return DIVERGENT;
  112. }
  113. RevCommit head = rw.parseCommit(acceptCmd.getNewId());
  114. if (rw.isMergedInto(remote, head)) {
  115. return LAGGING;
  116. }
  117. // TODO(sop) Check term to see if my leader was deposed.
  118. if (rw.isMergedInto(head, remote)) {
  119. return AHEAD;
  120. } else {
  121. return DIVERGENT;
  122. }
  123. } catch (IOException err) {
  124. KetchReplica.log.error(String.format(
  125. "Cannot compare %s", //$NON-NLS-1$
  126. acceptCmd.getRefName()), err);
  127. return UNKNOWN;
  128. }
  129. }
  130. private RevCommit parseRemoteCommit(String refName)
  131. throws IOException, MissingObjectException, RefGoneException {
  132. try {
  133. return rw.parseCommit(remoteId);
  134. } catch (MissingObjectException notLocal) {
  135. // Fall through and try to acquire the object by fetching it.
  136. }
  137. ReplicaFetchRequest fetch = new ReplicaFetchRequest(
  138. Collections.singleton(refName),
  139. Collections.<ObjectId> emptySet());
  140. try {
  141. replica.blockingFetch(repo, fetch);
  142. } catch (IOException fetchErr) {
  143. KetchReplica.log.error(String.format(
  144. "Cannot fetch %s (%s) from %s", //$NON-NLS-1$
  145. remoteId.abbreviate(8).name(), refName,
  146. replica.describeForLog()), fetchErr);
  147. throw new MissingObjectException(remoteId, OBJ_COMMIT);
  148. }
  149. Map<String, Ref> adv = fetch.getRefs();
  150. if (adv == null) {
  151. throw new MissingObjectException(remoteId, OBJ_COMMIT);
  152. }
  153. Ref ref = adv.get(refName);
  154. if (ref == null || ref.getObjectId() == null) {
  155. throw new RefGoneException();
  156. }
  157. initRevWalk();
  158. remoteId = ref.getObjectId();
  159. return rw.parseCommit(remoteId);
  160. }
  161. private static class RefGoneException extends Exception {
  162. private static final long serialVersionUID = 1L;
  163. }
  164. }