progress.go 7.7 KB

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