Skip to content

Commit

Permalink
balancer: add a warning for balancer names that contain upper case le…
Browse files Browse the repository at this point in the history
…tters (#6647)
  • Loading branch information
easwars authored Sep 25, 2023
1 parent 4ced601 commit 147bd85
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
15 changes: 15 additions & 0 deletions balancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"google.golang.org/grpc/channelz"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/internal"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/resolver"
Expand All @@ -39,6 +40,8 @@ import (
var (
// m is a map from name to balancer builder.
m = make(map[string]Builder)

logger = grpclog.Component("balancer")
)

// Register registers the balancer builder to the balancer map. b.Name
Expand All @@ -51,6 +54,12 @@ var (
// an init() function), and is not thread-safe. If multiple Balancers are
// registered with the same name, the one registered last will take effect.
func Register(b Builder) {
if strings.ToLower(b.Name()) != b.Name() {
// TODO: Skip the use of strings.ToLower() to index the map after v1.59
// is released to switch to case sensitive balancer registry. Also,
// remove this warning and update the docstrings for Register and Get.
logger.Warningf("Balancer registered with name %q. grpc-go will be switching to case sensitive balancer registries soon", b.Name())
}
m[strings.ToLower(b.Name())] = b
}

Expand All @@ -70,6 +79,12 @@ func init() {
// Note that the compare is done in a case-insensitive fashion.
// If no builder is register with the name, nil will be returned.
func Get(name string) Builder {
if strings.ToLower(name) != name {
// TODO: Skip the use of strings.ToLower() to index the map after v1.59
// is released to switch to case sensitive balancer registry. Also,
// remove this warning and update the docstrings for Register and Get.
logger.Warningf("Balancer retrieved for name %q. grpc-go will be switching to case sensitive balancer registries soon", name)
}
if b, ok := m[strings.ToLower(name)]; ok {
return b
}
Expand Down
17 changes: 9 additions & 8 deletions xds/internal/balancer/clusterresolver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/roundrobin"
iserviceconfig "google.golang.org/grpc/internal/serviceconfig"
"google.golang.org/grpc/xds/internal/balancer/outlierdetection"
"google.golang.org/grpc/xds/internal/balancer/ringhash"
Expand Down Expand Up @@ -107,7 +108,7 @@ const (
"edsServiceName": "test-eds-service-name",
"outlierDetection": {}
}],
"xdsLbPolicy":[{"ROUND_ROBIN":{}}]
"xdsLbPolicy":[{"round_robin":{}}]
}`
testJSONConfig2 = `{
"discoveryMechanisms": [{
Expand All @@ -124,7 +125,7 @@ const (
"type": "LOGICAL_DNS",
"outlierDetection": {}
}],
"xdsLbPolicy":[{"ROUND_ROBIN":{}}]
"xdsLbPolicy":[{"round_robin":{}}]
}`
testJSONConfig3 = `{
"discoveryMechanisms": [{
Expand All @@ -138,7 +139,7 @@ const (
"edsServiceName": "test-eds-service-name",
"outlierDetection": {}
}],
"xdsLbPolicy":[{"ROUND_ROBIN":{}}]
"xdsLbPolicy":[{"round_robin":{}}]
}`
testJSONConfig4 = `{
"discoveryMechanisms": [{
Expand Down Expand Up @@ -166,7 +167,7 @@ const (
"edsServiceName": "test-eds-service-name",
"outlierDetection": {}
}],
"xdsLbPolicy":[{"ROUND_ROBIN":{}}]
"xdsLbPolicy":[{"round_robin":{}}]
}`
)

Expand Down Expand Up @@ -211,7 +212,7 @@ func TestParseConfig(t *testing.T) {
},
},
xdsLBPolicy: iserviceconfig.BalancerConfig{ // do we want to make this not pointer
Name: "ROUND_ROBIN",
Name: roundrobin.Name,
Config: nil,
},
},
Expand Down Expand Up @@ -248,7 +249,7 @@ func TestParseConfig(t *testing.T) {
},
},
xdsLBPolicy: iserviceconfig.BalancerConfig{
Name: "ROUND_ROBIN",
Name: roundrobin.Name,
Config: nil,
},
},
Expand All @@ -275,7 +276,7 @@ func TestParseConfig(t *testing.T) {
},
},
xdsLBPolicy: iserviceconfig.BalancerConfig{
Name: "ROUND_ROBIN",
Name: roundrobin.Name,
Config: nil,
},
},
Expand Down Expand Up @@ -329,7 +330,7 @@ func TestParseConfig(t *testing.T) {
},
},
xdsLBPolicy: iserviceconfig.BalancerConfig{
Name: "ROUND_ROBIN",
Name: roundrobin.Name,
Config: nil,
},
},
Expand Down

0 comments on commit 147bd85

Please sign in to comment.