Skip to content

Commit

Permalink
ringhash: ensure addresses are consistenly hashed across updates (#6066)
Browse files Browse the repository at this point in the history
  • Loading branch information
easwars committed Mar 3, 2023
1 parent 52dcd14 commit ae4a231
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions xds/internal/balancer/ringhash/ring.go
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 {
h := xxhash.Sum64String(scw.sc.addr + "_" + strconv.Itoa(idx))
items = append(items, &ringEntry{hash: h, sc: scw.sc})
idx++
currentHashes++
}
}

Expand Down

0 comments on commit ae4a231

Please sign in to comment.