Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raft/tracker: visit Progress in stable order #11004

Merged
merged 2 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this to a common util pkg? no need for two insertionSort.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

@tbg tbg Aug 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally didn't do that (https://www.youtube.com/watch?v=PAAkCSZUG1c&t=9m28s). Just to double check, are you suggesting I make an insertionsortutil package that only includes this method? Because it won't be able to include much more than that because quorum's dependency tree is quite small and that's a good thing (plus, lots of our existing util-like code actually depends on raft).

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say to avoid the double slice, but you're moving this code from somewhere else, so feel free to keep as is.

var ids []uint64
var sl [7]uint64
if n <= len(sl) {
    ids = sl[:n]
} else {
    ids = make([]uint64, n)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed both up (the one here was even wrong!)

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