From f6f9af4ee6945a24ecaa80203ac4307b0660bbe3 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Fri, 4 Jun 2021 15:40:06 -0700 Subject: [PATCH 1/2] [c2p_id_random] c2p: add random number to xDS node ID in google-c2p resolver --- internal/grpcrand/grpcrand.go | 8 ++++++++ xds/googledirectpath/googlec2p.go | 5 ++++- xds/googledirectpath/googlec2p_test.go | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/internal/grpcrand/grpcrand.go b/internal/grpcrand/grpcrand.go index 200b115ca20..8d9c3975dc4 100644 --- a/internal/grpcrand/grpcrand.go +++ b/internal/grpcrand/grpcrand.go @@ -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 +} + // Int63n implements rand.Int63n on the grpcrand global source. func Int63n(n int64) int64 { mu.Lock() diff --git a/xds/googledirectpath/googlec2p.go b/xds/googledirectpath/googlec2p.go index b514f03bfbf..af487ec4a73 100644 --- a/xds/googledirectpath/googlec2p.go +++ b/xds/googledirectpath/googlec2p.go @@ -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. @@ -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}, diff --git a/xds/googledirectpath/googlec2p_test.go b/xds/googledirectpath/googlec2p_test.go index 5b8085ef34c..fb68fa23a1d 100644 --- a/xds/googledirectpath/googlec2p_test.go +++ b/xds/googledirectpath/googlec2p_test.go @@ -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, From ce0f463c32373a56d7dbca5ddb0a7052b0eb3349 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Mon, 7 Jun 2021 16:18:45 -0700 Subject: [PATCH 2/2] [c2p_id_random] c1 --- internal/grpcrand/grpcrand.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/internal/grpcrand/grpcrand.go b/internal/grpcrand/grpcrand.go index 8d9c3975dc4..7bc3583b5fc 100644 --- a/internal/grpcrand/grpcrand.go +++ b/internal/grpcrand/grpcrand.go @@ -34,31 +34,27 @@ var ( // Int implements rand.Int on the grpcrand global source. func Int() int { mu.Lock() - res := r.Int() - mu.Unlock() - return res + defer mu.Unlock() + return r.Int() } // Int63n implements rand.Int63n on the grpcrand global source. func Int63n(n int64) int64 { mu.Lock() - res := r.Int63n(n) - mu.Unlock() - return res + defer mu.Unlock() + return r.Int63n(n) } // Intn implements rand.Intn on the grpcrand global source. func Intn(n int) int { mu.Lock() - res := r.Intn(n) - mu.Unlock() - return res + defer mu.Unlock() + return r.Intn(n) } // Float64 implements rand.Float64 on the grpcrand global source. func Float64() float64 { mu.Lock() - res := r.Float64() - mu.Unlock() - return res + defer mu.Unlock() + return r.Float64() }