progress.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2015 CoreOS, Inc.
  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 raft
  15. import "fmt"
  16. const (
  17. ProgressStateProbe ProgressStateType = iota
  18. ProgressStateReplicate
  19. ProgressStateSnapshot
  20. )
  21. type ProgressStateType uint64
  22. var prstmap = [...]string{
  23. "ProgressStateProbe",
  24. "ProgressStateReplicate",
  25. "ProgressStateSnapshot",
  26. }
  27. func (st ProgressStateType) String() string { return prstmap[uint64(st)] }
  28. // Progress represents a follower’s progress in the view of the leader. Leader maintains
  29. // progresses of all followers, and sends entries to the follower based on its progress.
  30. type Progress struct {
  31. Match, Next uint64
  32. // When in ProgressStateProbe, leader sends at most one replication message
  33. // per heartbeat interval. It also probes actual progress of the follower.
  34. //
  35. // When in ProgressStateReplicate, leader optimistically increases next
  36. // to the latest entry sent after sending replication message. This is
  37. // an optimized state for fast replicating log entries to the follower.
  38. //
  39. // When in ProgressStateSnapshot, leader should have sent out snapshot
  40. // before and stops sending any replication message.
  41. State ProgressStateType
  42. // Paused is used in ProgressStateProbe.
  43. // When Paused is true, raft should pause sending replication message to this peer.
  44. Paused bool
  45. // PendingSnapshot is used in ProgressStateSnapshot.
  46. // If there is a pending snapshot, the pendingSnapshot will be set to the
  47. // index of the snapshot. If pendingSnapshot is set, the replication process of
  48. // this Progress will be paused. raft will not resend snapshot until the pending one
  49. // is reported to be failed.
  50. PendingSnapshot uint64
  51. // recentActive is true if the progress is recently active. Receiving any messages
  52. // from the corresponding follower indicates the progress is active.
  53. // recentActive can be reset to false after an election timeout.
  54. recentActive bool
  55. // inflights is a sliding window for the inflight messages.
  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.freeTo.
  62. ins *inflights
  63. }
  64. func (pr *Progress) resetState(state ProgressStateType) {
  65. pr.Paused = false
  66. pr.recentActive = false
  67. pr.PendingSnapshot = 0
  68. pr.State = state
  69. pr.ins.reset()
  70. }
  71. func (pr *Progress) becomeProbe() {
  72. // If the original state is ProgressStateSnapshot, progress knows that
  73. // the pending snapshot has been sent to this peer successfully, then
  74. // probes from pendingSnapshot + 1.
  75. if pr.State == ProgressStateSnapshot {
  76. pendingSnapshot := pr.PendingSnapshot
  77. pr.resetState(ProgressStateProbe)
  78. pr.Next = max(pr.Match+1, pendingSnapshot+1)
  79. } else {
  80. pr.resetState(ProgressStateProbe)
  81. pr.Next = pr.Match + 1
  82. }
  83. }
  84. func (pr *Progress) becomeReplicate() {
  85. pr.resetState(ProgressStateReplicate)
  86. pr.Next = pr.Match + 1
  87. }
  88. func (pr *Progress) becomeSnapshot(snapshoti uint64) {
  89. pr.resetState(ProgressStateSnapshot)
  90. pr.PendingSnapshot = snapshoti
  91. }
  92. // maybeUpdate returns false if the given n index comes from an outdated message.
  93. // Otherwise it updates the progress and returns true.
  94. func (pr *Progress) maybeUpdate(n uint64) bool {
  95. var updated bool
  96. if pr.Match < n {
  97. pr.Match = n
  98. updated = true
  99. pr.resume()
  100. }
  101. if pr.Next < n+1 {
  102. pr.Next = n + 1
  103. }
  104. return updated
  105. }
  106. func (pr *Progress) optimisticUpdate(n uint64) { pr.Next = n + 1 }
  107. // maybeDecrTo returns false if the given to index comes from an out of order message.
  108. // Otherwise it decreases the progress next index to min(rejected, last) and returns true.
  109. func (pr *Progress) maybeDecrTo(rejected, last uint64) bool {
  110. if pr.State == ProgressStateReplicate {
  111. // the rejection must be stale if the progress has matched and "rejected"
  112. // is smaller than "match".
  113. if rejected <= pr.Match {
  114. return false
  115. }
  116. // directly decrease next to match + 1
  117. pr.Next = pr.Match + 1
  118. return true
  119. }
  120. // the rejection must be stale if "rejected" does not match next - 1
  121. if pr.Next-1 != rejected {
  122. return false
  123. }
  124. if pr.Next = min(rejected, last+1); pr.Next < 1 {
  125. pr.Next = 1
  126. }
  127. pr.resume()
  128. return true
  129. }
  130. func (pr *Progress) pause() { pr.Paused = true }
  131. func (pr *Progress) resume() { pr.Paused = false }
  132. // isPaused returns whether progress stops sending message.
  133. func (pr *Progress) isPaused() bool {
  134. switch pr.State {
  135. case ProgressStateProbe:
  136. return pr.Paused
  137. case ProgressStateReplicate:
  138. return pr.ins.full()
  139. case ProgressStateSnapshot:
  140. return true
  141. default:
  142. panic("unexpected state")
  143. }
  144. }
  145. func (pr *Progress) snapshotFailure() { pr.PendingSnapshot = 0 }
  146. // maybeSnapshotAbort unsets pendingSnapshot if Match is equal or higher than
  147. // the pendingSnapshot
  148. func (pr *Progress) maybeSnapshotAbort() bool {
  149. return pr.State == ProgressStateSnapshot && pr.Match >= pr.PendingSnapshot
  150. }
  151. func (pr *Progress) String() string {
  152. return fmt.Sprintf("next = %d, match = %d, state = %s, waiting = %v, pendingSnapshot = %d", pr.Next, pr.Match, pr.State, pr.isPaused(), pr.PendingSnapshot)
  153. }
  154. type inflights struct {
  155. // the starting index in the buffer
  156. start int
  157. // number of inflights in the buffer
  158. count int
  159. // the size of the buffer
  160. size int
  161. buffer []uint64
  162. }
  163. func newInflights(size int) *inflights {
  164. return &inflights{
  165. size: size,
  166. buffer: make([]uint64, size),
  167. }
  168. }
  169. // add adds an inflight into inflights
  170. func (in *inflights) add(inflight uint64) {
  171. if in.full() {
  172. panic("cannot add into a full inflights")
  173. }
  174. next := in.start + in.count
  175. if next >= in.size {
  176. next -= in.size
  177. }
  178. in.buffer[next] = inflight
  179. in.count++
  180. }
  181. // freeTo frees the inflights smaller or equal to the given `to` flight.
  182. func (in *inflights) freeTo(to uint64) {
  183. if in.count == 0 || to < in.buffer[in.start] {
  184. // out of the left side of the window
  185. return
  186. }
  187. i, idx := 0, in.start
  188. for i = 0; i < in.count; i++ {
  189. if to < in.buffer[idx] { // found the first large inflight
  190. break
  191. }
  192. // increase index and maybe rotate
  193. if idx += 1; idx >= in.size {
  194. idx -= in.size
  195. }
  196. }
  197. // free i inflights and set new start index
  198. in.count -= i
  199. in.start = idx
  200. }
  201. func (in *inflights) freeFirstOne() { in.freeTo(in.buffer[in.start]) }
  202. // full returns true if the inflights is full.
  203. func (in *inflights) full() bool {
  204. return in.count == in.size
  205. }
  206. // resets frees all inflights.
  207. func (in *inflights) reset() {
  208. in.count = 0
  209. in.start = 0
  210. }