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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2016, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.ketch;
  11. import java.util.Collection;
  12. import java.util.Map;
  13. import org.eclipse.jgit.annotations.Nullable;
  14. import org.eclipse.jgit.lib.Ref;
  15. import org.eclipse.jgit.lib.Repository;
  16. import org.eclipse.jgit.transport.ReceiveCommand;
  17. /**
  18. * A push request sending objects to a replica, and its result.
  19. * <p>
  20. * Implementors of {@link org.eclipse.jgit.internal.ketch.KetchReplica} must
  21. * populate the command result fields, {@link #setRefs(Map)}, and call one of
  22. * {@link #setException(Repository, Throwable)} or {@link #done(Repository)} to
  23. * finish processing.
  24. */
  25. public class ReplicaPushRequest {
  26. private final KetchReplica replica;
  27. private final Collection<ReceiveCommand> commands;
  28. private Map<String, Ref> refs;
  29. private Throwable exception;
  30. private boolean notified;
  31. /**
  32. * Construct a new push request for a replica.
  33. *
  34. * @param replica
  35. * the replica being pushed to.
  36. * @param commands
  37. * commands to be executed.
  38. */
  39. public ReplicaPushRequest(KetchReplica replica,
  40. Collection<ReceiveCommand> commands) {
  41. this.replica = replica;
  42. this.commands = commands;
  43. }
  44. /**
  45. * Get commands to be executed, and their results.
  46. *
  47. * @return commands to be executed, and their results.
  48. */
  49. public Collection<ReceiveCommand> getCommands() {
  50. return commands;
  51. }
  52. /**
  53. * Get remote references, usually from the advertisement.
  54. *
  55. * @return remote references, usually from the advertisement.
  56. */
  57. @Nullable
  58. public Map<String, Ref> getRefs() {
  59. return refs;
  60. }
  61. /**
  62. * Set references observed from the replica.
  63. *
  64. * @param refs
  65. * references observed from the replica.
  66. */
  67. public void setRefs(Map<String, Ref> refs) {
  68. this.refs = refs;
  69. }
  70. /**
  71. * Get exception thrown, if any.
  72. *
  73. * @return exception thrown, if any.
  74. */
  75. @Nullable
  76. public Throwable getException() {
  77. return exception;
  78. }
  79. /**
  80. * Mark the request as crashing with a communication error.
  81. * <p>
  82. * This method may take significant time acquiring the leader lock and
  83. * updating the Ketch state machine with the failure.
  84. *
  85. * @param repo
  86. * local repository reference used by the push attempt.
  87. * @param err
  88. * exception thrown during communication.
  89. */
  90. public void setException(@Nullable Repository repo, Throwable err) {
  91. if (KetchReplica.log.isErrorEnabled()) {
  92. KetchReplica.log.error(describe("failed"), err); //$NON-NLS-1$
  93. }
  94. if (!notified) {
  95. notified = true;
  96. exception = err;
  97. replica.afterPush(repo, this);
  98. }
  99. }
  100. /**
  101. * Mark the request as completed without exception.
  102. * <p>
  103. * This method may take significant time acquiring the leader lock and
  104. * updating the Ketch state machine with results from this replica.
  105. *
  106. * @param repo
  107. * local repository reference used by the push attempt.
  108. */
  109. public void done(Repository repo) {
  110. if (KetchReplica.log.isDebugEnabled()) {
  111. KetchReplica.log.debug(describe("completed")); //$NON-NLS-1$
  112. }
  113. if (!notified) {
  114. notified = true;
  115. replica.afterPush(repo, this);
  116. }
  117. }
  118. private String describe(String heading) {
  119. StringBuilder b = new StringBuilder();
  120. b.append("push to "); //$NON-NLS-1$
  121. b.append(replica.describeForLog());
  122. b.append(' ').append(heading).append(":\n"); //$NON-NLS-1$
  123. for (ReceiveCommand cmd : commands) {
  124. b.append(String.format(
  125. " %-12s %-12s %s %s", //$NON-NLS-1$
  126. LeaderSnapshot.str(cmd.getOldId()),
  127. LeaderSnapshot.str(cmd.getNewId()),
  128. cmd.getRefName(),
  129. cmd.getResult()));
  130. if (cmd.getMessage() != null) {
  131. b.append(' ').append(cmd.getMessage());
  132. }
  133. b.append('\n');
  134. }
  135. return b.toString();
  136. }
  137. }