From 8e5a84e6b2dec2a96d0b780eeb93cbfd4928086e Mon Sep 17 00:00:00 2001 From: Doug Fawley Date: Tue, 23 Aug 2022 17:26:11 +0000 Subject: [PATCH] xds/resolver: generate channel ID randomly (#5603) --- xds/internal/resolver/serviceconfig.go | 5 ++--- xds/internal/resolver/serviceconfig_test.go | 7 ++++--- xds/internal/resolver/xds_resolver.go | 6 ++++++ xds/internal/xdsclient/xdsresource/type_rds.go | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/xds/internal/resolver/serviceconfig.go b/xds/internal/resolver/serviceconfig.go index fd75af21045..d1dd79354ae 100644 --- a/xds/internal/resolver/serviceconfig.go +++ b/xds/internal/resolver/serviceconfig.go @@ -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 } diff --git a/xds/internal/resolver/serviceconfig_test.go b/xds/internal/resolver/serviceconfig_test.go index 98d633a9e19..786b003c015 100644 --- a/xds/internal/resolver/serviceconfig_test.go +++ b/xds/internal/resolver/serviceconfig_test.go @@ -20,7 +20,6 @@ package resolver import ( "context" - "fmt" "regexp" "testing" @@ -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 { @@ -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 diff --git a/xds/internal/resolver/xds_resolver.go b/xds/internal/resolver/xds_resolver.go index 4f31d9c44c3..f473fcbaa73 100644 --- a/xds/internal/resolver/xds_resolver.go +++ b/xds/internal/resolver/xds_resolver.go @@ -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" @@ -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 { @@ -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 diff --git a/xds/internal/xdsclient/xdsresource/type_rds.go b/xds/internal/xdsclient/xdsresource/type_rds.go index decffd4ae76..0504346c399 100644 --- a/xds/internal/xdsclient/xdsresource/type_rds.go +++ b/xds/internal/xdsclient/xdsresource/type_rds.go @@ -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 )