Skip to content

Commit

Permalink
xds/resolver: generate channel ID randomly (#5603)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley authored Aug 23, 2022
1 parent 92cee34 commit 8e5a84e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
5 changes: 2 additions & 3 deletions xds/internal/resolver/serviceconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,8 @@ func (cs *configSelector) generateHash(rpcInfo iresolver.RPCInfo, hashPolicies [
generatedHash = true
generatedPolicyHash = true
case xdsresource.HashPolicyTypeChannelID:
// Hash the ClientConn pointer which logically uniquely
// identifies the client.
policyHash = xxhash.Sum64String(fmt.Sprintf("%p", &cs.r.cc))
// Use the static channel ID as the hash for this policy.
policyHash = cs.r.channelID
generatedHash = true
generatedPolicyHash = true
}
Expand Down
7 changes: 4 additions & 3 deletions xds/internal/resolver/serviceconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package resolver

import (
"context"
"fmt"
"regexp"
"testing"

Expand Down Expand Up @@ -50,9 +49,11 @@ func (s) TestPruneActiveClusters(t *testing.T) {
}

func (s) TestGenerateRequestHash(t *testing.T) {
const channelID = 12378921
cs := &configSelector{
r: &xdsResolver{
cc: &testClientConn{},
cc: &testClientConn{},
channelID: channelID,
},
}
tests := []struct {
Expand Down Expand Up @@ -85,7 +86,7 @@ func (s) TestGenerateRequestHash(t *testing.T) {
hashPolicies: []*xdsresource.HashPolicy{{
HashPolicyType: xdsresource.HashPolicyTypeChannelID,
}},
requestHashWant: xxhash.Sum64String(fmt.Sprintf("%p", &cs.r.cc)),
requestHashWant: channelID,
rpcInfo: iresolver.RPCInfo{},
},
// TestGenerateRequestHashEmptyString tests generating request hashes
Expand Down
6 changes: 6 additions & 0 deletions xds/internal/resolver/xds_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/internal"
"google.golang.org/grpc/internal/grpclog"
"google.golang.org/grpc/internal/grpcrand"
"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/internal/pretty"
iresolver "google.golang.org/grpc/internal/resolver"
Expand Down Expand Up @@ -71,6 +72,7 @@ func (b *xdsResolverBuilder) Build(target resolver.Target, cc resolver.ClientCon
closed: grpcsync.NewEvent(),
updateCh: make(chan suWithError, 1),
activeClusters: make(map[string]*clusterInfo),
channelID: grpcrand.Uint64(),
}
defer func() {
if retErr != nil {
Expand Down Expand Up @@ -184,6 +186,10 @@ type xdsResolver struct {
activeClusters map[string]*clusterInfo

curConfigSelector *configSelector

// A random number which uniquely identifies the channel which owns this
// resolver.
channelID uint64
}

// sendNewServiceConfig prunes active clusters, generates a new service config
Expand Down
2 changes: 1 addition & 1 deletion xds/internal/xdsclient/xdsresource/type_rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const (
// HashPolicyTypeHeader specifies to hash a Header in the incoming request.
HashPolicyTypeHeader HashPolicyType = iota
// HashPolicyTypeChannelID specifies to hash a unique Identifier of the
// Channel. In grpc-go, this will be done using the ClientConn pointer.
// Channel. This is a 64-bit random int computed at initialization time.
HashPolicyTypeChannelID
)

Expand Down

0 comments on commit 8e5a84e

Please sign in to comment.