progress.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  52. func (pr *Progress) resetState(state ProgressStateType) {
  53. pr.Paused = false
  54. pr.PendingSnapshot = 0
  55. pr.State = state
  56. }
  57. func (pr *Progress) becomeProbe() {
  58. // If the original state is ProgressStateSnapshot, progress knows that
  59. // the pending snapshot has been sent to this peer successfully, then
  60. // probes from pendingSnapshot + 1.
  61. if pr.State == ProgressStateSnapshot {
  62. pendingSnapshot := pr.PendingSnapshot
  63. pr.resetState(ProgressStateProbe)
  64. pr.Next = max(pr.Match+1, pendingSnapshot+1)
  65. } else {
  66. pr.resetState(ProgressStateProbe)
  67. pr.Next = pr.Match + 1
  68. }
  69. }
  70. func (pr *Progress) becomeReplicate() {
  71. pr.resetState(ProgressStateReplicate)
  72. pr.Next = pr.Match + 1
  73. }
  74. func (pr *Progress) becomeSnapshot(snapshoti uint64) {
  75. pr.resetState(ProgressStateSnapshot)
  76. pr.PendingSnapshot = snapshoti
  77. }
  78. // maybeUpdate returns false if the given n index comes from an outdated message.
  79. // Otherwise it updates the progress and returns true.
  80. func (pr *Progress) maybeUpdate(n uint64) bool {
  81. var updated bool
  82. if pr.Match < n {
  83. pr.Match = n
  84. updated = true
  85. pr.resume()
  86. }
  87. if pr.Next < n+1 {
  88. pr.Next = n + 1
  89. }
  90. return updated
  91. }
  92. func (pr *Progress) optimisticUpdate(n uint64) { pr.Next = n + 1 }
  93. // maybeDecrTo returns false if the given to index comes from an out of order message.
  94. // Otherwise it decreases the progress next index to min(rejected, last) and returns true.
  95. func (pr *Progress) maybeDecrTo(rejected, last uint64) bool {
  96. if pr.State == ProgressStateReplicate {
  97. // the rejection must be stale if the progress has matched and "rejected"
  98. // is smaller than "match".
  99. if rejected <= pr.Match {
  100. return false
  101. }
  102. // directly decrease next to match + 1
  103. pr.Next = pr.Match + 1
  104. return true
  105. }
  106. // the rejection must be stale if "rejected" does not match next - 1
  107. if pr.Next-1 != rejected {
  108. return false
  109. }
  110. if pr.Next = min(rejected, last+1); pr.Next < 1 {
  111. pr.Next = 1
  112. }
  113. pr.resume()
  114. return true
  115. }
  116. func (pr *Progress) pause() { pr.Paused = true }
  117. func (pr *Progress) resume() { pr.Paused = false }
  118. // isPaused returns whether progress stops sending message.
  119. func (pr *Progress) isPaused() bool {
  120. return pr.State == ProgressStateProbe && pr.Paused || pr.State == ProgressStateSnapshot
  121. }
  122. func (pr *Progress) snapshotFailure() { pr.PendingSnapshot = 0 }
  123. // maybeSnapshotAbort unsets pendingSnapshot if Match is equal or higher than
  124. // the pendingSnapshot
  125. func (pr *Progress) maybeSnapshotAbort() bool {
  126. return pr.State == ProgressStateSnapshot && pr.Match >= pr.PendingSnapshot
  127. }
  128. func (pr *Progress) String() string {
  129. return fmt.Sprintf("next = %d, match = %d, state = %s, waiting = %v, pendingSnapshot = %d", pr.Next, pr.Match, pr.State, pr.isPaused(), pr.PendingSnapshot)
  130. }