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

ringhash: ensure addresses are consistenly hashed across updates #6066

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Changes from all commits
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
19 changes: 11 additions & 8 deletions xds/internal/balancer/ringhash/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ func newRing(subConns *resolver.AddressMap, minRingSize, maxRingSize uint64, log
//
// A hash is generated for each item, and later the results will be sorted
// based on the hash.
var (
idx int
targetIdx float64
)
var currentHashes, targetHashes float64
for _, scw := range normalizedWeights {
targetIdx += scale * scw.weight
for float64(idx) < targetIdx {
h := xxhash.Sum64String(scw.sc.addr + strconv.Itoa(idx))
items = append(items, &ringEntry{idx: idx, hash: h, sc: scw.sc})
targetHashes += scale * scw.weight
// This index ensures that ring entries corresponding to the same
// address hash to different values. And since this index is
// per-address, these entries hash to the same value across address
// updates.
idx := 0
for currentHashes < targetHashes {
Copy link
Member

Choose a reason for hiding this comment

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

Why are we accumulating targetHashes and currentHashes instead of resetting them? e.g.

for _, scw := range normalizedWeights {
	targetHashes := scale * scw.weight
	for idx := 0; idx < targetHashes; idx++ {
		h := xxhash.Sum...
		items = append...
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since scw.weight is not a whole number, targetHashes is not a whole number either. In order to ensure that we end up with the correct number of items in the ring, we need to keep a running sum.

I based this change on C-core and Java's implementation. Looks like envoy also does the same: https://github.com/envoyproxy/envoy/blob/main/source/common/upstream/ring_hash_lb.cc#L159.

h := xxhash.Sum64String(scw.sc.addr + "_" + strconv.Itoa(idx))
items = append(items, &ringEntry{hash: h, sc: scw.sc})
idx++
currentHashes++
}
}

Expand Down