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.

LogIndex.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 org.eclipse.jgit.lib.AnyObjectId;
  12. import org.eclipse.jgit.lib.ObjectId;
  13. /**
  14. * An ObjectId for a commit extended with incrementing log index.
  15. * <p>
  16. * For any two LogIndex instances, {@code A} is an ancestor of {@code C}
  17. * reachable through parent edges in the graph if {@code A.index < C.index}.
  18. * LogIndex provides a performance optimization for Ketch, the same information
  19. * can be obtained from {@link org.eclipse.jgit.revwalk.RevWalk}.
  20. * <p>
  21. * Index values are only valid within a single
  22. * {@link org.eclipse.jgit.internal.ketch.KetchLeader} instance after it has won
  23. * an election. By restricting scope to a single leader new leaders do not need
  24. * to traverse the entire history to determine the next {@code index} for new
  25. * proposals. This differs from Raft, where leader election uses the log index
  26. * and the term number to determine which replica holds a sufficiently
  27. * up-to-date log. Since Ketch uses Git objects for storage of its replicated
  28. * log, it keeps the term number as Raft does but uses standard Git operations
  29. * to imply the log index.
  30. * <p>
  31. * {@link org.eclipse.jgit.internal.ketch.Round#runAsync(AnyObjectId)} bumps the
  32. * index as each new round is constructed.
  33. */
  34. public class LogIndex extends ObjectId {
  35. static LogIndex unknown(AnyObjectId id) {
  36. return new LogIndex(id, 0);
  37. }
  38. private final long index;
  39. private LogIndex(AnyObjectId id, long index) {
  40. super(id);
  41. this.index = index;
  42. }
  43. LogIndex nextIndex(AnyObjectId id) {
  44. return new LogIndex(id, index + 1);
  45. }
  46. /**
  47. * Get index provided by the current leader instance.
  48. *
  49. * @return index provided by the current leader instance.
  50. */
  51. public long getIndex() {
  52. return index;
  53. }
  54. /**
  55. * Check if this log position committed before another log position.
  56. * <p>
  57. * Only valid for log positions in memory for the current leader.
  58. *
  59. * @param c
  60. * other (more recent) log position.
  61. * @return true if this log position was before {@code c} or equal to c and
  62. * therefore any agreement of {@code c} implies agreement on this
  63. * log position.
  64. */
  65. boolean isBefore(LogIndex c) {
  66. return index <= c.index;
  67. }
  68. /**
  69. * Create string suitable for debug logging containing the log index and
  70. * abbreviated ObjectId.
  71. *
  72. * @return string suitable for debug logging containing the log index and
  73. * abbreviated ObjectId.
  74. */
  75. @SuppressWarnings("boxing")
  76. public String describeForLog() {
  77. return String.format("%5d/%s", index, abbreviate(6).name()); //$NON-NLS-1$
  78. }
  79. /** {@inheritDoc} */
  80. @SuppressWarnings("boxing")
  81. @Override
  82. public String toString() {
  83. return String.format("LogId[%5d/%s]", index, name()); //$NON-NLS-1$
  84. }
  85. }