tracker.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. "sort"
  18. "go.etcd.io/etcd/raft/quorum"
  19. )
  20. // ProgressTracker tracks the currently active configuration and the information
  21. // known about the nodes and learners in it. In particular, it tracks the match
  22. // index for each peer which in turn allows reasoning about the committed index.
  23. type ProgressTracker struct {
  24. Voters quorum.JointConfig
  25. Learners map[uint64]struct{}
  26. Progress map[uint64]*Progress
  27. Votes map[uint64]bool
  28. MaxInflight int
  29. }
  30. // MakeProgressTracker initializes a ProgressTracker.
  31. func MakeProgressTracker(maxInflight int) ProgressTracker {
  32. p := ProgressTracker{
  33. MaxInflight: maxInflight,
  34. Voters: quorum.JointConfig{
  35. quorum.MajorityConfig{},
  36. quorum.MajorityConfig{},
  37. },
  38. Learners: map[uint64]struct{}{},
  39. Votes: map[uint64]bool{},
  40. Progress: map[uint64]*Progress{},
  41. }
  42. return p
  43. }
  44. // IsSingleton returns true if (and only if) there is only one voting member
  45. // (i.e. the leader) in the current configuration.
  46. func (p *ProgressTracker) IsSingleton() bool {
  47. return len(p.Voters[0]) == 1 && len(p.Voters[1]) == 0
  48. }
  49. type matchAckIndexer map[uint64]*Progress
  50. var _ quorum.AckedIndexer = matchAckIndexer(nil)
  51. // AckedIndex implements IndexLookuper.
  52. func (l matchAckIndexer) AckedIndex(id uint64) (quorum.Index, bool) {
  53. pr, ok := l[id]
  54. if !ok {
  55. return 0, false
  56. }
  57. return quorum.Index(pr.Match), true
  58. }
  59. // Committed returns the largest log index known to be committed based on what
  60. // the voting members of the group have acknowledged.
  61. func (p *ProgressTracker) Committed() uint64 {
  62. return uint64(p.Voters.CommittedIndex(matchAckIndexer(p.Progress)))
  63. }
  64. // RemoveAny removes this peer, which *must* be tracked as a voter or learner,
  65. // from the tracker.
  66. func (p *ProgressTracker) RemoveAny(id uint64) {
  67. _, okPR := p.Progress[id]
  68. _, okV1 := p.Voters[0][id]
  69. _, okV2 := p.Voters[1][id]
  70. _, okL := p.Learners[id]
  71. okV := okV1 || okV2
  72. if !okPR {
  73. panic("attempting to remove unknown peer %x")
  74. } else if !okV && !okL {
  75. panic("attempting to remove unknown peer %x")
  76. } else if okV && okL {
  77. panic(fmt.Sprintf("peer %x is both voter and learner", id))
  78. }
  79. delete(p.Voters[0], id)
  80. delete(p.Voters[1], id)
  81. delete(p.Learners, id)
  82. delete(p.Progress, id)
  83. }
  84. // InitProgress initializes a new progress for the given node or learner. The
  85. // node may not exist yet in either form or a panic will ensue.
  86. func (p *ProgressTracker) InitProgress(id, match, next uint64, isLearner bool) {
  87. if pr := p.Progress[id]; pr != nil {
  88. panic(fmt.Sprintf("peer %x already tracked as node %v", id, pr))
  89. }
  90. if !isLearner {
  91. p.Voters[0][id] = struct{}{}
  92. } else {
  93. p.Learners[id] = struct{}{}
  94. }
  95. p.Progress[id] = &Progress{Next: next, Match: match, Inflights: NewInflights(p.MaxInflight), IsLearner: isLearner}
  96. }
  97. // Visit invokes the supplied closure for all tracked progresses.
  98. func (p *ProgressTracker) Visit(f func(id uint64, pr *Progress)) {
  99. for id, pr := range p.Progress {
  100. f(id, pr)
  101. }
  102. }
  103. // QuorumActive returns true if the quorum is active from the view of the local
  104. // raft state machine. Otherwise, it returns false.
  105. func (p *ProgressTracker) QuorumActive() bool {
  106. votes := map[uint64]bool{}
  107. p.Visit(func(id uint64, pr *Progress) {
  108. if pr.IsLearner {
  109. return
  110. }
  111. votes[id] = pr.RecentActive
  112. })
  113. return p.Voters.VoteResult(votes) == quorum.VoteWon
  114. }
  115. // VoterNodes returns a sorted slice of voters.
  116. func (p *ProgressTracker) VoterNodes() []uint64 {
  117. m := p.Voters.IDs()
  118. nodes := make([]uint64, 0, len(m))
  119. for id := range m {
  120. nodes = append(nodes, id)
  121. }
  122. sort.Slice(nodes, func(i, j int) bool { return nodes[i] < nodes[j] })
  123. return nodes
  124. }
  125. // LearnerNodes returns a sorted slice of learners.
  126. func (p *ProgressTracker) LearnerNodes() []uint64 {
  127. nodes := make([]uint64, 0, len(p.Learners))
  128. for id := range p.Learners {
  129. nodes = append(nodes, id)
  130. }
  131. sort.Slice(nodes, func(i, j int) bool { return nodes[i] < nodes[j] })
  132. return nodes
  133. }
  134. // ResetVotes prepares for a new round of vote counting via recordVote.
  135. func (p *ProgressTracker) ResetVotes() {
  136. p.Votes = map[uint64]bool{}
  137. }
  138. // RecordVote records that the node with the given id voted for this Raft
  139. // instance if v == true (and declined it otherwise).
  140. func (p *ProgressTracker) RecordVote(id uint64, v bool) {
  141. _, ok := p.Votes[id]
  142. if !ok {
  143. p.Votes[id] = v
  144. }
  145. }
  146. // TallyVotes returns the number of granted and rejected Votes, and whether the
  147. // election outcome is known.
  148. func (p *ProgressTracker) TallyVotes() (granted int, rejected int, _ quorum.VoteResult) {
  149. // Make sure to populate granted/rejected correctly even if the Votes slice
  150. // contains members no longer part of the configuration. This doesn't really
  151. // matter in the way the numbers are used (they're informational), but might
  152. // as well get it right.
  153. for id, pr := range p.Progress {
  154. if pr.IsLearner {
  155. continue
  156. }
  157. v, voted := p.Votes[id]
  158. if !voted {
  159. continue
  160. }
  161. if v {
  162. granted++
  163. } else {
  164. rejected++
  165. }
  166. }
  167. result := p.Voters.VoteResult(p.Votes)
  168. return granted, rejected, result
  169. }