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

xds/resolver: generate channel ID randomly #5603

Merged
merged 1 commit into from Aug 23, 2022
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions xds/internal/resolver/serviceconfig.go
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
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
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
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