progress.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2019 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tracker
  15. import "fmt"
  16. // Progress represents a follower’s progress in the view of the leader. Leader
  17. // maintains progresses of all followers, and sends entries to the follower
  18. // based on its progress.
  19. //
  20. // NB(tbg): Progress is basically a state machine whose transitions are mostly
  21. // strewn around `*raft.raft`. Additionally, some fields are only used when in a
  22. // certain State. All of this isn't ideal.
  23. type Progress struct {
  24. Match, Next uint64
  25. // State defines how the leader should interact with the follower.
  26. //
  27. // When in StateProbe, leader sends at most one replication message
  28. // per heartbeat interval. It also probes actual progress of the follower.
  29. //
  30. // When in StateReplicate, leader optimistically increases next
  31. // to the latest entry sent after sending replication message. This is
  32. // an optimized state for fast replicating log entries to the follower.
  33. //
  34. // When in StateSnapshot, leader should have sent out snapshot
  35. // before and stops sending any replication message.
  36. State StateType
  37. // PendingSnapshot is used in StateSnapshot.
  38. // If there is a pending snapshot, the pendingSnapshot will be set to the
  39. // index of the snapshot. If pendingSnapshot is set, the replication process of
  40. // this Progress will be paused. raft will not resend snapshot until the pending one
  41. // is reported to be failed.
  42. PendingSnapshot uint64
  43. // RecentActive is true if the progress is recently active. Receiving any messages
  44. // from the corresponding follower indicates the progress is active.
  45. // RecentActive can be reset to false after an election timeout.
  46. RecentActive bool
  47. // ProbeSent is used while this follow is in StateProbe. When ProbeSent is
  48. // true, raft should pause sending replication message to this peer until
  49. // ProbeSent is reset. See ProbeAcked() and IsPaused().
  50. ProbeSent bool
  51. // Inflights is a sliding window for the inflight messages.
  52. // Each inflight message contains one or more log entries.
  53. // The max number of entries per message is defined in raft config as MaxSizePerMsg.
  54. // Thus inflight effectively limits both the number of inflight messages
  55. // and the bandwidth each Progress can use.
  56. // When inflights is Full, no more message should be sent.
  57. // When a leader sends out a message, the index of the last
  58. // entry should be added to inflights. The index MUST be added
  59. // into inflights in order.
  60. // When a leader receives a reply, the previous inflights should
  61. // be freed by calling inflights.FreeLE with the index of the last
  62. // received entry.
  63. Inflights *Inflights
  64. // IsLearner is true if this progress is tracked for a learner.
  65. IsLearner bool
  66. }
  67. // ResetState moves the Progress into the specified State, resetting ProbeSent,
  68. // PendingSnapshot, and Inflights.
  69. func (pr *Progress) ResetState(state StateType) {
  70. pr.ProbeSent = false
  71. pr.PendingSnapshot = 0
  72. pr.State = state
  73. pr.Inflights.reset()
  74. }
  75. func max(a, b uint64) uint64 {
  76. if a > b {
  77. return a
  78. }
  79. return b
  80. }
  81. func min(a, b uint64) uint64 {
  82. if a > b {
  83. return b
  84. }
  85. return a
  86. }
  87. // ProbeAcked is called when this peer has accepted an append. It resets
  88. // ProbeSent to signal that additional append messages should be sent without
  89. // further delay.
  90. func (pr *Progress) ProbeAcked() {
  91. pr.ProbeSent = false
  92. }
  93. // BecomeProbe transitions into StateProbe. Next is reset to Match+1 or,
  94. // optionally and if larger, the index of the pending snapshot.
  95. func (pr *Progress) BecomeProbe() {
  96. // If the original state is StateSnapshot, progress knows that
  97. // the pending snapshot has been sent to this peer successfully, then
  98. // probes from pendingSnapshot + 1.
  99. if pr.State == StateSnapshot {
  100. pendingSnapshot := pr.PendingSnapshot
  101. pr.ResetState(StateProbe)
  102. pr.Next = max(pr.Match+1, pendingSnapshot+1)
  103. } else {
  104. pr.ResetState(StateProbe)
  105. pr.Next = pr.Match + 1
  106. }
  107. }
  108. // BecomeReplicate transitions into StateReplicate, resetting Next to Match+1.
  109. func (pr *Progress) BecomeReplicate() {
  110. pr.ResetState(StateReplicate)
  111. pr.Next = pr.Match + 1
  112. }
  113. // BecomeSnapshot moves the Progress to StateSnapshot with the specified pending
  114. // snapshot index.
  115. func (pr *Progress) BecomeSnapshot(snapshoti uint64) {
  116. pr.ResetState(StateSnapshot)
  117. pr.PendingSnapshot = snapshoti
  118. }
  119. // MaybeUpdate is called when an MsgAppResp arrives from the follower, with the
  120. // index acked by it. The method returns false if the given n index comes from
  121. // an outdated message. Otherwise it updates the progress and returns true.
  122. func (pr *Progress) MaybeUpdate(n uint64) bool {
  123. var updated bool
  124. if pr.Match < n {
  125. pr.Match = n
  126. updated = true
  127. pr.ProbeAcked()
  128. }
  129. if pr.Next < n+1 {
  130. pr.Next = n + 1
  131. }
  132. return updated
  133. }
  134. // OptimisticUpdate signals that appends all the way up to and including index n
  135. // are in-flight. As a result, Next is increased to n+1.
  136. func (pr *Progress) OptimisticUpdate(n uint64) { pr.Next = n + 1 }
  137. // MaybeDecrTo adjusts the Progress to the receipt of a MsgApp rejection. The
  138. // arguments are the index the follower rejected to append to its log, and its
  139. // last index.
  140. //
  141. // Rejections can happen spuriously as messages are sent out of order or
  142. // duplicated. In such cases, the rejection pertains to an index that the
  143. // Progress already knows were previously acknowledged, and false is returned
  144. // without changing the Progress.
  145. //
  146. // If the rejection is genuine, Next is lowered sensibly, and the Progress is
  147. // cleared for sending log entries.
  148. func (pr *Progress) MaybeDecrTo(rejected, last uint64) bool {
  149. if pr.State == StateReplicate {
  150. // The rejection must be stale if the progress has matched and "rejected"
  151. // is smaller than "match".
  152. if rejected <= pr.Match {
  153. return false
  154. }
  155. // Directly decrease next to match + 1.
  156. //
  157. // TODO(tbg): why not use last if it's larger?
  158. pr.Next = pr.Match + 1
  159. return true
  160. }
  161. // The rejection must be stale if "rejected" does not match next - 1. This
  162. // is because non-replicating followers are probed one entry at a time.
  163. if pr.Next-1 != rejected {
  164. return false
  165. }
  166. if pr.Next = min(rejected, last+1); pr.Next < 1 {
  167. pr.Next = 1
  168. }
  169. pr.ProbeSent = false
  170. return true
  171. }
  172. // IsPaused returns whether sending log entries to this node has been throttled.
  173. // This is done when a node has rejected recent MsgApps, is currently waiting
  174. // for a snapshot, or has reached the MaxInflightMsgs limit. In normal
  175. // operation, this is false. A throttled node will be contacted less frequently
  176. // until it has reached a state in which it's able to accept a steady stream of
  177. // log entries again.
  178. func (pr *Progress) IsPaused() bool {
  179. switch pr.State {
  180. case StateProbe:
  181. return pr.ProbeSent
  182. case StateReplicate:
  183. return pr.Inflights.Full()
  184. case StateSnapshot:
  185. return true
  186. default:
  187. panic("unexpected state")
  188. }
  189. }
  190. func (pr *Progress) String() string {
  191. return fmt.Sprintf("next = %d, match = %d, state = %s, waiting = %v, pendingSnapshot = %d, recentActive = %v, isLearner = %v",
  192. pr.Next, pr.Match, pr.State, pr.IsPaused(), pr.PendingSnapshot, pr.RecentActive, pr.IsLearner)
  193. }