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

rewrite shuffleTablets to be clearer and more efficient #15716

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 15 additions & 40 deletions go/vt/vtgate/tabletgateway.go
Expand Up @@ -377,50 +377,25 @@ func (gw *TabletGateway) getStatsAggregator(target *querypb.Target) *TabletStatu
}

func (gw *TabletGateway) shuffleTablets(cell string, tablets []*discovery.TabletHealth) {
sameCell, diffCell, sameCellMax := 0, 0, -1
length := len(tablets)

// move all same cell tablets to the front, this is O(n)
for {
sameCellMax = diffCell - 1
sameCell = gw.nextTablet(cell, tablets, sameCell, length, true)
diffCell = gw.nextTablet(cell, tablets, diffCell, length, false)
// either no more diffs or no more same cells should stop the iteration
if sameCell < 0 || diffCell < 0 {
break
}

if sameCell < diffCell {
// fast forward the `sameCell` lookup to `diffCell + 1`, `diffCell` unchanged
sameCell = diffCell + 1
// Randomly shuffle the list of tablets, putting the same-cell hosts at the front
Copy link
Member

Choose a reason for hiding this comment

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

nit: I'd always use tablet/tablets instead of host/hosts. Those are the Vitess terms.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok. edited.

// of the list and the other-cell hosts at the back
//
// Only need to do n-1 swaps since the last host is always in the right place.
n := len(tablets)
head := 0
tail := n - 1
for i := 0; i < n-1; i++ {
j := head + rand.IntN(tail-head+1)

if tablets[j].Tablet.Alias.Cell == cell {
tablets[head], tablets[j] = tablets[j], tablets[head]
head++
} else {
// sameCell > diffCell, swap needed
tablets[sameCell], tablets[diffCell] = tablets[diffCell], tablets[sameCell]
sameCell++
diffCell++
}
}

// shuffle in same cell tablets
for i := sameCellMax; i > 0; i-- {
swap := rand.IntN(i + 1)
tablets[i], tablets[swap] = tablets[swap], tablets[i]
}

// shuffle in diff cell tablets
for i, diffCellMin := length-1, sameCellMax+1; i > diffCellMin; i-- {
swap := rand.IntN(i-sameCellMax) + diffCellMin
tablets[i], tablets[swap] = tablets[swap], tablets[i]
}
}

func (gw *TabletGateway) nextTablet(cell string, tablets []*discovery.TabletHealth, offset, length int, sameCell bool) int {
for ; offset < length; offset++ {
if (tablets[offset].Tablet.Alias.Cell == cell) == sameCell {
return offset
tablets[tail], tablets[j] = tablets[j], tablets[tail]
tail--
}
}
return -1
}

// TabletsCacheStatus returns a displayable version of the health check cache.
Expand Down