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.

ReplicaPushRequest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 java.util.Collection;
  45. import java.util.Map;
  46. import org.eclipse.jgit.annotations.Nullable;
  47. import org.eclipse.jgit.lib.Ref;
  48. import org.eclipse.jgit.lib.Repository;
  49. import org.eclipse.jgit.transport.ReceiveCommand;
  50. /**
  51. * A push request sending objects to a replica, and its result.
  52. * <p>
  53. * Implementors of {@link KetchReplica} must populate the command result fields,
  54. * {@link #setRefs(Map)}, and call one of
  55. * {@link #setException(Repository, Throwable)} or {@link #done(Repository)} to
  56. * finish processing.
  57. */
  58. public class ReplicaPushRequest {
  59. private final KetchReplica replica;
  60. private final Collection<ReceiveCommand> commands;
  61. private Map<String, Ref> refs;
  62. private Throwable exception;
  63. private boolean notified;
  64. /**
  65. * Construct a new push request for a replica.
  66. *
  67. * @param replica
  68. * the replica being pushed to.
  69. * @param commands
  70. * commands to be executed.
  71. */
  72. public ReplicaPushRequest(KetchReplica replica,
  73. Collection<ReceiveCommand> commands) {
  74. this.replica = replica;
  75. this.commands = commands;
  76. }
  77. /** @return commands to be executed, and their results. */
  78. public Collection<ReceiveCommand> getCommands() {
  79. return commands;
  80. }
  81. /** @return remote references, usually from the advertisement. */
  82. @Nullable
  83. public Map<String, Ref> getRefs() {
  84. return refs;
  85. }
  86. /**
  87. * @param refs
  88. * references observed from the replica.
  89. */
  90. public void setRefs(Map<String, Ref> refs) {
  91. this.refs = refs;
  92. }
  93. /** @return exception thrown, if any. */
  94. @Nullable
  95. public Throwable getException() {
  96. return exception;
  97. }
  98. /**
  99. * Mark the request as crashing with a communication error.
  100. * <p>
  101. * This method may take significant time acquiring the leader lock and
  102. * updating the Ketch state machine with the failure.
  103. *
  104. * @param repo
  105. * local repository reference used by the push attempt.
  106. * @param err
  107. * exception thrown during communication.
  108. */
  109. public void setException(@Nullable Repository repo, Throwable err) {
  110. if (KetchReplica.log.isErrorEnabled()) {
  111. KetchReplica.log.error(describe("failed"), err); //$NON-NLS-1$
  112. }
  113. if (!notified) {
  114. notified = true;
  115. exception = err;
  116. replica.afterPush(repo, this);
  117. }
  118. }
  119. /**
  120. * Mark the request as completed without exception.
  121. * <p>
  122. * This method may take significant time acquiring the leader lock and
  123. * updating the Ketch state machine with results from this replica.
  124. *
  125. * @param repo
  126. * local repository reference used by the push attempt.
  127. */
  128. public void done(Repository repo) {
  129. if (KetchReplica.log.isDebugEnabled()) {
  130. KetchReplica.log.debug(describe("completed")); //$NON-NLS-1$
  131. }
  132. if (!notified) {
  133. notified = true;
  134. replica.afterPush(repo, this);
  135. }
  136. }
  137. private String describe(String heading) {
  138. StringBuilder b = new StringBuilder();
  139. b.append("push to "); //$NON-NLS-1$
  140. b.append(replica.describeForLog());
  141. b.append(' ').append(heading).append(":\n"); //$NON-NLS-1$
  142. for (ReceiveCommand cmd : commands) {
  143. b.append(String.format(
  144. " %-12s %-12s %s %s", //$NON-NLS-1$
  145. LeaderSnapshot.str(cmd.getOldId()),
  146. LeaderSnapshot.str(cmd.getNewId()),
  147. cmd.getRefName(),
  148. cmd.getResult()));
  149. if (cmd.getMessage() != null) {
  150. b.append(' ').append(cmd.getMessage());
  151. }
  152. b.append('\n');
  153. }
  154. return b.toString();
  155. }
  156. }