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.

Proposal.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 static org.eclipse.jgit.internal.ketch.Proposal.State.ABORTED;
  12. import static org.eclipse.jgit.internal.ketch.Proposal.State.EXECUTED;
  13. import static org.eclipse.jgit.internal.ketch.Proposal.State.NEW;
  14. import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED;
  15. import static org.eclipse.jgit.transport.ReceiveCommand.Result.OK;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.concurrent.CopyOnWriteArrayList;
  22. import java.util.concurrent.TimeUnit;
  23. import java.util.concurrent.atomic.AtomicReference;
  24. import org.eclipse.jgit.annotations.Nullable;
  25. import org.eclipse.jgit.errors.MissingObjectException;
  26. import org.eclipse.jgit.internal.storage.reftree.Command;
  27. import org.eclipse.jgit.lib.ObjectId;
  28. import org.eclipse.jgit.lib.PersonIdent;
  29. import org.eclipse.jgit.lib.Ref;
  30. import org.eclipse.jgit.revwalk.RevWalk;
  31. import org.eclipse.jgit.transport.PushCertificate;
  32. import org.eclipse.jgit.transport.ReceiveCommand;
  33. import org.eclipse.jgit.util.time.ProposedTimestamp;
  34. /**
  35. * A proposal to be applied in a Ketch system.
  36. * <p>
  37. * Pushing to a Ketch leader results in the leader making a proposal. The
  38. * proposal includes the list of reference updates. The leader attempts to send
  39. * the proposal to a quorum of replicas by pushing the proposal to a "staging"
  40. * area under the {@code refs/txn/stage/} namespace. If the proposal succeeds
  41. * then the changes are durable and the leader can commit the proposal.
  42. * <p>
  43. * Proposals are executed by
  44. * {@link org.eclipse.jgit.internal.ketch.KetchLeader#queueProposal(Proposal)},
  45. * which runs them asynchronously in the background. Proposals are thread-safe
  46. * futures allowing callers to {@link #await()} for results or be notified by
  47. * callback using {@link #addListener(Runnable)}.
  48. */
  49. public class Proposal {
  50. /** Current state of the proposal. */
  51. public enum State {
  52. /** Proposal has not yet been given to a {@link KetchLeader}. */
  53. NEW(false),
  54. /**
  55. * Proposal was validated and has entered the queue, but a round
  56. * containing this proposal has not started yet.
  57. */
  58. QUEUED(false),
  59. /** Round containing the proposal has begun and is in progress. */
  60. RUNNING(false),
  61. /**
  62. * Proposal was executed through a round. Individual results from
  63. * {@link Proposal#getCommands()}, {@link Command#getResult()} explain
  64. * the success or failure outcome.
  65. */
  66. EXECUTED(true),
  67. /** Proposal was aborted and did not reach consensus. */
  68. ABORTED(true);
  69. private final boolean done;
  70. private State(boolean done) {
  71. this.done = done;
  72. }
  73. /** @return true if this is a terminal state. */
  74. public boolean isDone() {
  75. return done;
  76. }
  77. }
  78. private final List<Command> commands;
  79. private PersonIdent author;
  80. private String message;
  81. private PushCertificate pushCert;
  82. private List<ProposedTimestamp> timestamps;
  83. private final List<Runnable> listeners = new CopyOnWriteArrayList<>();
  84. private final AtomicReference<State> state = new AtomicReference<>(NEW);
  85. /**
  86. * Create a proposal from a list of Ketch commands.
  87. *
  88. * @param cmds
  89. * prepared list of commands.
  90. */
  91. public Proposal(List<Command> cmds) {
  92. commands = Collections.unmodifiableList(new ArrayList<>(cmds));
  93. }
  94. /**
  95. * Create a proposal from a collection of received commands.
  96. *
  97. * @param rw
  98. * walker to assist in preparing commands.
  99. * @param cmds
  100. * list of pending commands.
  101. * @throws org.eclipse.jgit.errors.MissingObjectException
  102. * newId of a command is not found locally.
  103. * @throws java.io.IOException
  104. * local objects cannot be accessed.
  105. */
  106. public Proposal(RevWalk rw, Collection<ReceiveCommand> cmds)
  107. throws MissingObjectException, IOException {
  108. commands = asCommandList(rw, cmds);
  109. }
  110. private static List<Command> asCommandList(RevWalk rw,
  111. Collection<ReceiveCommand> cmds)
  112. throws MissingObjectException, IOException {
  113. List<Command> commands = new ArrayList<>(cmds.size());
  114. for (ReceiveCommand cmd : cmds) {
  115. commands.add(new Command(rw, cmd));
  116. }
  117. return Collections.unmodifiableList(commands);
  118. }
  119. /**
  120. * Get commands from this proposal.
  121. *
  122. * @return commands from this proposal.
  123. */
  124. public Collection<Command> getCommands() {
  125. return commands;
  126. }
  127. /**
  128. * Get optional author of the proposal.
  129. *
  130. * @return optional author of the proposal.
  131. */
  132. @Nullable
  133. public PersonIdent getAuthor() {
  134. return author;
  135. }
  136. /**
  137. * Set the author for the proposal.
  138. *
  139. * @param who
  140. * optional identity of the author of the proposal.
  141. * @return {@code this}
  142. */
  143. public Proposal setAuthor(@Nullable PersonIdent who) {
  144. author = who;
  145. return this;
  146. }
  147. /**
  148. * Get optional message for the commit log of the RefTree.
  149. *
  150. * @return optional message for the commit log of the RefTree.
  151. */
  152. @Nullable
  153. public String getMessage() {
  154. return message;
  155. }
  156. /**
  157. * Set the message to appear in the commit log of the RefTree.
  158. *
  159. * @param msg
  160. * message text for the commit.
  161. * @return {@code this}
  162. */
  163. public Proposal setMessage(@Nullable String msg) {
  164. message = msg != null && !msg.isEmpty() ? msg : null;
  165. return this;
  166. }
  167. /**
  168. * Get optional certificate signing the references.
  169. *
  170. * @return optional certificate signing the references.
  171. */
  172. @Nullable
  173. public PushCertificate getPushCertificate() {
  174. return pushCert;
  175. }
  176. /**
  177. * Set the push certificate signing the references.
  178. *
  179. * @param cert
  180. * certificate, may be null.
  181. * @return {@code this}
  182. */
  183. public Proposal setPushCertificate(@Nullable PushCertificate cert) {
  184. pushCert = cert;
  185. return this;
  186. }
  187. /**
  188. * Get timestamps that Ketch must block for.
  189. *
  190. * @return timestamps that Ketch must block for. These may have been used as
  191. * commit times inside the objects involved in the proposal.
  192. */
  193. public List<ProposedTimestamp> getProposedTimestamps() {
  194. if (timestamps != null) {
  195. return timestamps;
  196. }
  197. return Collections.emptyList();
  198. }
  199. /**
  200. * Request the proposal to wait for the affected timestamps to resolve.
  201. *
  202. * @param ts
  203. * a {@link org.eclipse.jgit.util.time.ProposedTimestamp} object.
  204. * @return {@code this}.
  205. */
  206. public Proposal addProposedTimestamp(ProposedTimestamp ts) {
  207. if (timestamps == null) {
  208. timestamps = new ArrayList<>(4);
  209. }
  210. timestamps.add(ts);
  211. return this;
  212. }
  213. /**
  214. * Add a callback to be invoked when the proposal is done.
  215. * <p>
  216. * A proposal is done when it has entered either
  217. * {@link org.eclipse.jgit.internal.ketch.Proposal.State#EXECUTED} or
  218. * {@link org.eclipse.jgit.internal.ketch.Proposal.State#ABORTED} state. If
  219. * the proposal is already done {@code callback.run()} is immediately
  220. * invoked on the caller's thread.
  221. *
  222. * @param callback
  223. * method to run after the proposal is done. The callback may be
  224. * run on a Ketch system thread and should be completed quickly.
  225. */
  226. public void addListener(Runnable callback) {
  227. boolean runNow = false;
  228. synchronized (state) {
  229. if (state.get().isDone()) {
  230. runNow = true;
  231. } else {
  232. listeners.add(callback);
  233. }
  234. }
  235. if (runNow) {
  236. callback.run();
  237. }
  238. }
  239. /** Set command result as OK. */
  240. void success() {
  241. for (Command c : commands) {
  242. if (c.getResult() == NOT_ATTEMPTED) {
  243. c.setResult(OK);
  244. }
  245. }
  246. notifyState(EXECUTED);
  247. }
  248. /** Mark commands as "transaction aborted". */
  249. void abort() {
  250. Command.abort(commands, null);
  251. notifyState(ABORTED);
  252. }
  253. /**
  254. * Read the current state of the proposal.
  255. *
  256. * @return read the current state of the proposal.
  257. */
  258. public State getState() {
  259. return state.get();
  260. }
  261. /**
  262. * Whether the proposal was attempted
  263. *
  264. * @return {@code true} if the proposal was attempted. A true value does not
  265. * mean consensus was reached, only that the proposal was considered
  266. * and will not be making any more progress beyond its current
  267. * state.
  268. */
  269. public boolean isDone() {
  270. return state.get().isDone();
  271. }
  272. /**
  273. * Wait for the proposal to be attempted and {@link #isDone()} to be true.
  274. *
  275. * @throws java.lang.InterruptedException
  276. * caller was interrupted before proposal executed.
  277. */
  278. public void await() throws InterruptedException {
  279. synchronized (state) {
  280. while (!state.get().isDone()) {
  281. state.wait();
  282. }
  283. }
  284. }
  285. /**
  286. * Wait for the proposal to be attempted and {@link #isDone()} to be true.
  287. *
  288. * @param wait
  289. * how long to wait.
  290. * @param unit
  291. * unit describing the wait time.
  292. * @return true if the proposal is done; false if the method timed out.
  293. * @throws java.lang.InterruptedException
  294. * caller was interrupted before proposal executed.
  295. */
  296. public boolean await(long wait, TimeUnit unit) throws InterruptedException {
  297. synchronized (state) {
  298. if (state.get().isDone()) {
  299. return true;
  300. }
  301. state.wait(unit.toMillis(wait));
  302. return state.get().isDone();
  303. }
  304. }
  305. /**
  306. * Wait for the proposal to exit a state.
  307. *
  308. * @param notIn
  309. * state the proposal should not be in to return.
  310. * @param wait
  311. * how long to wait.
  312. * @param unit
  313. * unit describing the wait time.
  314. * @return true if the proposal exited the state; false on time out.
  315. * @throws java.lang.InterruptedException
  316. * caller was interrupted before proposal executed.
  317. */
  318. public boolean awaitStateChange(State notIn, long wait, TimeUnit unit)
  319. throws InterruptedException {
  320. synchronized (state) {
  321. if (state.get() != notIn) {
  322. return true;
  323. }
  324. state.wait(unit.toMillis(wait));
  325. return state.get() != notIn;
  326. }
  327. }
  328. void notifyState(State s) {
  329. synchronized (state) {
  330. state.set(s);
  331. state.notifyAll();
  332. }
  333. if (s.isDone()) {
  334. for (Runnable callback : listeners) {
  335. callback.run();
  336. }
  337. listeners.clear();
  338. }
  339. }
  340. /** {@inheritDoc} */
  341. @Override
  342. public String toString() {
  343. StringBuilder s = new StringBuilder();
  344. s.append("Ketch Proposal {\n"); //$NON-NLS-1$
  345. s.append(" ").append(state.get()).append('\n'); //$NON-NLS-1$
  346. if (author != null) {
  347. s.append(" author ").append(author).append('\n'); //$NON-NLS-1$
  348. }
  349. if (message != null) {
  350. s.append(" message ").append(message).append('\n'); //$NON-NLS-1$
  351. }
  352. for (Command c : commands) {
  353. s.append(" "); //$NON-NLS-1$
  354. format(s, c.getOldRef(), "CREATE"); //$NON-NLS-1$
  355. s.append(' ');
  356. format(s, c.getNewRef(), "DELETE"); //$NON-NLS-1$
  357. s.append(' ').append(c.getRefName());
  358. if (c.getResult() != ReceiveCommand.Result.NOT_ATTEMPTED) {
  359. s.append(' ').append(c.getResult()); // $NON-NLS-1$
  360. }
  361. s.append('\n');
  362. }
  363. s.append('}');
  364. return s.toString();
  365. }
  366. private static void format(StringBuilder s, @Nullable Ref r, String n) {
  367. if (r == null) {
  368. s.append(n);
  369. } else if (r.isSymbolic()) {
  370. s.append(r.getTarget().getName());
  371. } else {
  372. ObjectId id = r.getObjectId();
  373. if (id != null) {
  374. s.append(id.abbreviate(8).name());
  375. }
  376. }
  377. }
  378. }