-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
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).