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.

Round.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.io.IOException;
  12. import java.util.List;
  13. import org.eclipse.jgit.lib.AnyObjectId;
  14. import org.eclipse.jgit.transport.ReceiveCommand;
  15. /**
  16. * One round-trip to all replicas proposing a log entry.
  17. * <p>
  18. * In Raft a log entry represents a state transition at a specific index in the
  19. * replicated log. The leader can only append log entries to the log.
  20. * <p>
  21. * In Ketch a log entry is recorded under the {@code refs/txn} namespace. This
  22. * occurs when:
  23. * <ul>
  24. * <li>a replica wants to establish itself as a new leader by proposing a new
  25. * term (see {@link ElectionRound})
  26. * <li>an established leader wants to gain consensus on new {@link Proposal}s
  27. * (see {@link ProposalRound})
  28. * </ul>
  29. */
  30. abstract class Round {
  31. final KetchLeader leader;
  32. final LogIndex acceptedOldIndex;
  33. LogIndex acceptedNewIndex;
  34. List<ReceiveCommand> stageCommands;
  35. Round(KetchLeader leader, LogIndex head) {
  36. this.leader = leader;
  37. this.acceptedOldIndex = head;
  38. }
  39. KetchSystem getSystem() {
  40. return leader.getSystem();
  41. }
  42. /**
  43. * Creates a commit for {@code refs/txn/accepted} and calls
  44. * {@link #runAsync(AnyObjectId)} to begin execution of the round across
  45. * the system.
  46. * <p>
  47. * If references are being updated (such as in a {@link ProposalRound}) the
  48. * RefTree may be modified.
  49. * <p>
  50. * Invoked without {@link KetchLeader#lock} to build objects.
  51. *
  52. * @throws IOException
  53. * the round cannot build new objects within the leader's
  54. * repository. The leader may be unable to execute.
  55. */
  56. abstract void start() throws IOException;
  57. /**
  58. * Asynchronously distribute the round's new value for
  59. * {@code refs/txn/accepted} to all replicas.
  60. * <p>
  61. * Invoked by {@link #start()} after new commits have been created for the
  62. * log. The method passes {@code newId} to {@link KetchLeader} to be
  63. * distributed to all known replicas.
  64. *
  65. * @param newId
  66. * new value for {@code refs/txn/accepted}.
  67. */
  68. void runAsync(AnyObjectId newId) {
  69. acceptedNewIndex = acceptedOldIndex.nextIndex(newId);
  70. leader.runAsync(this);
  71. }
  72. /**
  73. * Notify the round it was accepted by a majority of the system.
  74. * <p>
  75. * Invoked by the leader with {@link KetchLeader#lock} held by the caller.
  76. */
  77. abstract void success();
  78. }