Skip to content

Commit

Permalink
raft/tracker: visit Progress in stable order
Browse files Browse the repository at this point in the history
This is helpful for upcoming testing work which allows datadriven
testing of the interaction of multiple nodes. This testing requires
determinism to work correctly.
  • Loading branch information
tbg committed Aug 7, 2019
1 parent 9553994 commit 5feebe1
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions raft/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,34 @@ func (p *ProgressTracker) Committed() uint64 {
return uint64(p.Voters.CommittedIndex(matchAckIndexer(p.Progress)))
}

// Visit invokes the supplied closure for all tracked progresses.
func insertionSort(sl []uint64) {
a, b := 0, len(sl)
for i := a + 1; i < b; i++ {
for j := i; j > a && sl[j] < sl[j-1]; j-- {
sl[j], sl[j-1] = sl[j-1], sl[j]
}
}
}

// Visit invokes the supplied closure for all tracked progresses in stable order.
func (p *ProgressTracker) Visit(f func(id uint64, pr *Progress)) {
for id, pr := range p.Progress {
f(id, pr)
n := len(p.Progress)
// We need to sort the IDs and don't want to allocate since this is hot code.
// The optimization here mirrors that in `(MajorityConfig).CommittedIndex`,
// see there for details.
var sl [7]uint64
ids := sl[:]
if n > len(sl) {
ids = make([]uint64, n)
}
ids = sl[:n]
for id := range p.Progress {
n--
ids[n] = id
}
insertionSort(ids)
for _, id := range ids {
f(id, p.Progress[id])
}
}

Expand Down

0 comments on commit 5feebe1

Please sign in to comment.