Skip to content

Commit

Permalink
Merge pull request #11004 from tbg/interaction/unused-type
Browse files Browse the repository at this point in the history
raft/tracker: visit Progress in stable order
  • Loading branch information
tbg authored Aug 9, 2019
2 parents 5ce1856 + 1b3e082 commit 7948f39
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
12 changes: 5 additions & 7 deletions raft/quorum/majority.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ func (c MajorityConfig) Slice() []uint64 {
return sl
}

type uint64Slice []uint64

func insertionSort(sl uint64Slice) {
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-- {
Expand All @@ -141,12 +139,12 @@ func (c MajorityConfig) CommittedIndex(l AckedIndexer) Index {
// performance is a lesser concern (additionally the performance
// implications of an allocation here are far from drastic).
var stk [7]uint64
srt := uint64Slice(stk[:])

if cap(srt) < n {
var srt []uint64
if len(stk) >= n {
srt = stk[:n]
} else {
srt = make([]uint64, n)
}
srt = srt[:n]

{
// Fill the slice with the indexes observed. Any unused slots will be
Expand Down
31 changes: 28 additions & 3 deletions raft/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,35 @@ 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 len(sl) >= n {
ids = sl[:n]
} else {
ids = make([]uint64, n)
}
for id := range p.Progress {
n--
ids[n] = id
}
insertionSort(ids)
for _, id := range ids {
f(id, p.Progress[id])
}
}

Expand Down
7 changes: 0 additions & 7 deletions raft/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ func (st StateType) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", st.String())), nil
}

// uint64Slice implements sort interface
type uint64Slice []uint64

func (p uint64Slice) Len() int { return len(p) }
func (p uint64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

func min(a, b uint64) uint64 {
if a > b {
return b
Expand Down

0 comments on commit 7948f39

Please sign in to comment.