Skip to content

Commit

Permalink
ringhash: add logs to surface information about ring creation (#5832)
Browse files Browse the repository at this point in the history
Fixes #5781
  • Loading branch information
easwars committed Dec 6, 2022
1 parent f7c110a commit 1949035
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion xds/internal/balancer/ringhash/ring.go
Expand Up @@ -24,6 +24,7 @@ import (
"strconv"

xxhash "github.com/cespare/xxhash/v2"
"google.golang.org/grpc/internal/grpclog"
"google.golang.org/grpc/resolver"
)

Expand Down Expand Up @@ -65,9 +66,12 @@ type ringEntry struct {
// and first item with hash >= given hash will be returned.
//
// Must be called with a non-empty subConns map.
func newRing(subConns *resolver.AddressMap, minRingSize, maxRingSize uint64) *ring {
func newRing(subConns *resolver.AddressMap, minRingSize, maxRingSize uint64, logger *grpclog.PrefixLogger) *ring {
logger.Debugf("newRing: number of subConns is %d, minRingSize is %d, maxRingSize is %d", subConns.Len(), minRingSize, maxRingSize)

// https://github.com/envoyproxy/envoy/blob/765c970f06a4c962961a0e03a467e165b276d50f/source/common/upstream/ring_hash_lb.cc#L114
normalizedWeights, minWeight := normalizeWeights(subConns)
logger.Debugf("newRing: normalized subConn weights is %v", normalizedWeights)

// Normalized weights for {3,3,4} is {0.3,0.3,0.4}.

Expand All @@ -78,6 +82,7 @@ func newRing(subConns *resolver.AddressMap, minRingSize, maxRingSize uint64) *ri
scale := math.Min(math.Ceil(minWeight*float64(minRingSize))/minWeight, float64(maxRingSize))
ringSize := math.Ceil(scale)
items := make([]*ringEntry, 0, int(ringSize))
logger.Debugf("newRing: creating new ring of size %v", ringSize)

// For each entry, scale*weight nodes are generated in the ring.
//
Expand Down
6 changes: 3 additions & 3 deletions xds/internal/balancer/ringhash/ring_test.go
Expand Up @@ -52,7 +52,7 @@ func (s) TestRingNew(t *testing.T) {
for _, min := range []uint64{3, 4, 6, 8} {
for _, max := range []uint64{20, 8} {
t.Run(fmt.Sprintf("size-min-%v-max-%v", min, max), func(t *testing.T) {
r := newRing(testSubConnMap, min, max)
r := newRing(testSubConnMap, min, max, nil)
totalCount := len(r.items)
if totalCount < int(min) || totalCount > int(max) {
t.Fatalf("unexpect size %v, want min %v, max %v", totalCount, min, max)
Expand Down Expand Up @@ -82,7 +82,7 @@ func equalApproximately(x, y float64) bool {
}

func (s) TestRingPick(t *testing.T) {
r := newRing(testSubConnMap, 10, 20)
r := newRing(testSubConnMap, 10, 20, nil)
for _, h := range []uint64{xxhash.Sum64String("1"), xxhash.Sum64String("2"), xxhash.Sum64String("3"), xxhash.Sum64String("4")} {
t.Run(fmt.Sprintf("picking-hash-%v", h), func(t *testing.T) {
e := r.pick(h)
Expand All @@ -100,7 +100,7 @@ func (s) TestRingPick(t *testing.T) {
}

func (s) TestRingNext(t *testing.T) {
r := newRing(testSubConnMap, 10, 20)
r := newRing(testSubConnMap, 10, 20, nil)

for _, e := range r.items {
ne := r.next(e)
Expand Down
2 changes: 1 addition & 1 deletion xds/internal/balancer/ringhash/ringhash.go
Expand Up @@ -293,7 +293,7 @@ func (b *ringhashBalancer) UpdateClientConnState(s balancer.ClientConnState) err
if regenerateRing {
// Ring creation is guaranteed to not fail because we call newRing()
// with a non-empty subConns map.
b.ring = newRing(b.subConns, b.config.MinRingSize, b.config.MaxRingSize)
b.ring = newRing(b.subConns, b.config.MinRingSize, b.config.MaxRingSize, b.logger)
b.regeneratePicker()
b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker})
}
Expand Down

0 comments on commit 1949035

Please sign in to comment.