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

c2p: add random number to xDS node ID in google-c2p resolver #4519

Merged
merged 2 commits into from Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions internal/grpcrand/grpcrand.go
Expand Up @@ -31,6 +31,14 @@ var (
mu sync.Mutex
)

// Int implements rand.Int on the grpcrand global source.
func Int() int {
mu.Lock()
res := r.Int()
mu.Unlock()
return res
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, if you want to do a cleanup below:

mu.Lock()
defer mu.Unlock()
return r.Int()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

// Int63n implements rand.Int63n on the grpcrand global source.
func Int63n(n int64) int64 {
mu.Lock()
Expand Down
5 changes: 4 additions & 1 deletion xds/googledirectpath/googlec2p.go
Expand Up @@ -35,6 +35,7 @@ import (
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/internal/googlecloud"
internalgrpclog "google.golang.org/grpc/internal/grpclog"
"google.golang.org/grpc/internal/grpcrand"
"google.golang.org/grpc/internal/xds/env"
"google.golang.org/grpc/resolver"
_ "google.golang.org/grpc/xds" // To register xds resolvers and balancers.
Expand Down Expand Up @@ -152,13 +153,15 @@ var ipv6EnabledMetadata = &structpb.Struct{
},
}

var id = fmt.Sprintf("C2P-%d", grpcrand.Int())

// newNode makes a copy of defaultNode, and populate it's Metadata and
// Locality fields.
func newNode(zone string, ipv6Capable bool) *v3corepb.Node {
ret := &v3corepb.Node{
// Not all required fields are set in defaultNote. Metadata will be set
// if ipv6 is enabled. Locality will be set to the value from metadata.
Id: "C2P",
Id: id,
UserAgentName: gRPCUserAgentName,
UserAgentVersionType: &v3corepb.Node_UserAgentVersion{UserAgentVersion: grpc.Version},
ClientFeatures: []string{clientFeatureNoOverprovisioning},
Expand Down
2 changes: 1 addition & 1 deletion xds/googledirectpath/googlec2p_test.go
Expand Up @@ -194,7 +194,7 @@ func TestBuildXDS(t *testing.T) {
}

wantNode := &v3corepb.Node{
Id: "C2P",
Id: id,
Metadata: nil,
Locality: &v3corepb.Locality{Zone: testZone},
UserAgentName: gRPCUserAgentName,
Expand Down