progress.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // inflights is a sliding window for the inflight messages.
  52. // When inflights is full, no more message should be sent.
  53. // When sends out a message, the index of the last entry should
  54. // be add to inflights. The index MUST be added into inflights
  55. // in order.
  56. // When receives a reply, the previous inflights should be freed
  57. // by calling inflights.freeTo.
  58. ins *inflights
  59. }
  60. func (pr *Progress) resetState(state ProgressStateType) {
  61. pr.Paused = false
  62. pr.PendingSnapshot = 0
  63. pr.State = state
  64. pr.ins.reset()
  65. }
  66. func (pr *Progress) becomeProbe() {
  67. // If the original state is ProgressStateSnapshot, progress knows that
  68. // the pending snapshot has been sent to this peer successfully, then
  69. // probes from pendingSnapshot + 1.
  70. if pr.State == ProgressStateSnapshot {
  71. pendingSnapshot := pr.PendingSnapshot
  72. pr.resetState(ProgressStateProbe)
  73. pr.Next = max(pr.Match+1, pendingSnapshot+1)
  74. } else {
  75. pr.resetState(ProgressStateProbe)
  76. pr.Next = pr.Match + 1
  77. }
  78. }
  79. func (pr *Progress) becomeReplicate() {
  80. pr.resetState(ProgressStateReplicate)
  81. pr.Next = pr.Match + 1
  82. }
  83. func (pr *Progress) becomeSnapshot(snapshoti uint64) {
  84. pr.resetState(ProgressStateSnapshot)
  85. pr.PendingSnapshot = snapshoti
  86. }
  87. // maybeUpdate returns false if the given n index comes from an outdated message.
  88. // Otherwise it updates the progress and returns true.
  89. func (pr *Progress) maybeUpdate(n uint64) bool {
  90. var updated bool
  91. if pr.Match < n {
  92. pr.Match = n
  93. updated = true
  94. pr.resume()
  95. }
  96. if pr.Next < n+1 {
  97. pr.Next = n + 1
  98. }
  99. return updated
  100. }
  101. func (pr *Progress) optimisticUpdate(n uint64) { pr.Next = n + 1 }
  102. // maybeDecrTo returns false if the given to index comes from an out of order message.
  103. // Otherwise it decreases the progress next index to min(rejected, last) and returns true.
  104. func (pr *Progress) maybeDecrTo(rejected, last uint64) bool {
  105. if pr.State == ProgressStateReplicate {
  106. // the rejection must be stale if the progress has matched and "rejected"
  107. // is smaller than "match".
  108. if rejected <= pr.Match {
  109. return false
  110. }
  111. // directly decrease next to match + 1
  112. pr.Next = pr.Match + 1
  113. return true
  114. }
  115. // the rejection must be stale if "rejected" does not match next - 1
  116. if pr.Next-1 != rejected {
  117. return false
  118. }
  119. if pr.Next = min(rejected, last+1); pr.Next < 1 {
  120. pr.Next = 1
  121. }
  122. pr.resume()
  123. return true
  124. }
  125. func (pr *Progress) pause() { pr.Paused = true }
  126. func (pr *Progress) resume() { pr.Paused = false }
  127. // isPaused returns whether progress stops sending message.
  128. func (pr *Progress) isPaused() bool {
  129. switch pr.State {
  130. case ProgressStateProbe:
  131. return pr.Paused
  132. case ProgressStateReplicate:
  133. return pr.ins.full()
  134. case ProgressStateSnapshot:
  135. return true
  136. default:
  137. panic("unexpected state")
  138. }
  139. }
  140. func (pr *Progress) snapshotFailure() { pr.PendingSnapshot = 0 }
  141. // maybeSnapshotAbort unsets pendingSnapshot if Match is equal or higher than
  142. // the pendingSnapshot
  143. func (pr *Progress) maybeSnapshotAbort() bool {
  144. return pr.State == ProgressStateSnapshot && pr.Match >= pr.PendingSnapshot
  145. }
  146. func (pr *Progress) String() string {
  147. return fmt.Sprintf("next = %d, match = %d, state = %s, waiting = %v, pendingSnapshot = %d", pr.Next, pr.Match, pr.State, pr.isPaused(), pr.PendingSnapshot)
  148. }
  149. type inflights struct {
  150. // the starting index in the buffer
  151. start int
  152. // number of inflights in the buffer
  153. count int
  154. // the size of the buffer
  155. size int
  156. buffer []uint64
  157. }
  158. func newInflights(size int) *inflights {
  159. return &inflights{
  160. size: size,
  161. buffer: make([]uint64, size),
  162. }
  163. }
  164. // add adds an inflight into inflights
  165. func (in *inflights) add(inflight uint64) {
  166. if in.full() {
  167. panic("cannot add into a full inflights")
  168. }
  169. next := in.start + in.count
  170. if next >= in.size {
  171. next -= in.size
  172. }
  173. in.buffer[next] = inflight
  174. in.count++
  175. }
  176. // freeTo frees the inflights smaller or equal to the given `to` flight.
  177. func (in *inflights) freeTo(to uint64) {
  178. if in.count == 0 || to < in.buffer[in.start] {
  179. // out of the left side of the window
  180. return
  181. }
  182. i, idx := 0, in.start
  183. for i = 0; i < in.count; i++ {
  184. if to < in.buffer[idx] { // found the first large inflight
  185. break
  186. }
  187. // increase index and maybe rotate
  188. if idx += 1; idx >= in.size {
  189. idx -= in.size
  190. }
  191. }
  192. // free i inflights and set new start index
  193. in.count -= i
  194. in.start = idx
  195. }
  196. func (in *inflights) freeFirstOne() { in.freeTo(in.buffer[in.start]) }
  197. // full returns true if the inflights is full.
  198. func (in *inflights) full() bool {
  199. return in.count == in.size
  200. }
  201. // resets frees all inflights.
  202. func (in *inflights) reset() {
  203. in.count = 0
  204. in.start = 0
  205. }