snapshot of https://github.com/grpc/grpc-go.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / vet-proto (push) Failing after 1s
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
Stale bot / stale (push) Failing after 0s

This commit is contained in:
upstream-mirror
2026-09-15 20:40:15 +05:00
commit e241b33f26
1365 changed files with 339570 additions and 0 deletions

View File

@@ -0,0 +1,252 @@
/*
*
* Copyright 2023 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Package converter provides converters to convert proto load balancing
// configuration, defined by the xDS API spec, to JSON load balancing
// configuration. These converters are registered by proto type in a registry,
// which gets pulled from based off proto type passed in.
package converter
import (
"encoding/json"
"fmt"
"strings"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/leastrequest"
"google.golang.org/grpc/balancer/pickfirst"
"google.golang.org/grpc/balancer/ringhash"
"google.golang.org/grpc/balancer/roundrobin"
"google.golang.org/grpc/balancer/weightedroundrobin"
iringhash "google.golang.org/grpc/internal/ringhash"
internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
"google.golang.org/grpc/internal/xds/balancer/wrrlocality"
"google.golang.org/grpc/internal/xds/xdsclient/xdslbregistry"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
v1xdsudpatypepb "github.com/cncf/xds/go/udpa/type/v1"
v3xdsxdstypepb "github.com/cncf/xds/go/xds/type/v3"
v3clientsideweightedroundrobinpb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/client_side_weighted_round_robin/v3"
v3leastrequestpb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/least_request/v3"
v3pickfirstpb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/pick_first/v3"
v3ringhashpb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/ring_hash/v3"
v3wrrlocalitypb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/wrr_locality/v3"
)
func init() {
xdslbregistry.Register("type.googleapis.com/envoy.extensions.load_balancing_policies.client_side_weighted_round_robin.v3.ClientSideWeightedRoundRobin", convertWeightedRoundRobinProtoToServiceConfig)
xdslbregistry.Register("type.googleapis.com/envoy.extensions.load_balancing_policies.ring_hash.v3.RingHash", convertRingHashProtoToServiceConfig)
xdslbregistry.Register("type.googleapis.com/envoy.extensions.load_balancing_policies.pick_first.v3.PickFirst", convertPickFirstProtoToServiceConfig)
xdslbregistry.Register("type.googleapis.com/envoy.extensions.load_balancing_policies.round_robin.v3.RoundRobin", convertRoundRobinProtoToServiceConfig)
xdslbregistry.Register("type.googleapis.com/envoy.extensions.load_balancing_policies.wrr_locality.v3.WrrLocality", convertWRRLocalityProtoToServiceConfig)
xdslbregistry.Register("type.googleapis.com/envoy.extensions.load_balancing_policies.least_request.v3.LeastRequest", convertLeastRequestProtoToServiceConfig)
xdslbregistry.Register("type.googleapis.com/udpa.type.v1.TypedStruct", convertV1TypedStructToServiceConfig)
xdslbregistry.Register("type.googleapis.com/xds.type.v3.TypedStruct", convertV3TypedStructToServiceConfig)
}
const (
defaultRingHashMinSize = 1024
defaultRingHashMaxSize = 8 * 1024 * 1024 // 8M
defaultLeastRequestChoiceCount = 2
)
func convertRingHashProtoToServiceConfig(rawProto []byte, _ int) (json.RawMessage, error) {
rhProto := &v3ringhashpb.RingHash{}
if err := proto.Unmarshal(rawProto, rhProto); err != nil {
return nil, fmt.Errorf("failed to unmarshal resource: %v", err)
}
if rhProto.GetHashFunction() != v3ringhashpb.RingHash_XX_HASH {
return nil, fmt.Errorf("unsupported ring_hash hash function %v", rhProto.GetHashFunction())
}
var minSize, maxSize uint64 = defaultRingHashMinSize, defaultRingHashMaxSize
if min := rhProto.GetMinimumRingSize(); min != nil {
minSize = min.GetValue()
}
if max := rhProto.GetMaximumRingSize(); max != nil {
maxSize = max.GetValue()
}
rhCfg := &iringhash.LBConfig{
MinRingSize: minSize,
MaxRingSize: maxSize,
}
rhCfgJSON, err := json.Marshal(rhCfg)
if err != nil {
return nil, fmt.Errorf("error marshaling JSON for type %T: %v", rhCfg, err)
}
return makeBalancerConfigJSON(ringhash.Name, rhCfgJSON), nil
}
type pfConfig struct {
ShuffleAddressList bool `json:"shuffleAddressList"`
}
func convertPickFirstProtoToServiceConfig(rawProto []byte, _ int) (json.RawMessage, error) {
pfProto := &v3pickfirstpb.PickFirst{}
if err := proto.Unmarshal(rawProto, pfProto); err != nil {
return nil, fmt.Errorf("failed to unmarshal resource: %v", err)
}
pfCfg := &pfConfig{ShuffleAddressList: pfProto.GetShuffleAddressList()}
js, err := json.Marshal(pfCfg)
if err != nil {
return nil, fmt.Errorf("error marshaling JSON for type %T: %v", pfCfg, err)
}
return makeBalancerConfigJSON(pickfirst.Name, js), nil
}
func convertRoundRobinProtoToServiceConfig([]byte, int) (json.RawMessage, error) {
return makeBalancerConfigJSON(roundrobin.Name, json.RawMessage("{}")), nil
}
type wrrLocalityLBConfig struct {
ChildPolicy json.RawMessage `json:"childPolicy,omitempty"`
}
func convertWRRLocalityProtoToServiceConfig(rawProto []byte, depth int) (json.RawMessage, error) {
wrrlProto := &v3wrrlocalitypb.WrrLocality{}
if err := proto.Unmarshal(rawProto, wrrlProto); err != nil {
return nil, fmt.Errorf("failed to unmarshal resource: %v", err)
}
epJSON, err := xdslbregistry.ConvertToServiceConfig(wrrlProto.GetEndpointPickingPolicy(), depth+1)
if err != nil {
return nil, fmt.Errorf("error converting endpoint picking policy: %v for %+v", err, wrrlProto)
}
wrrLCfg := wrrLocalityLBConfig{
ChildPolicy: epJSON,
}
lbCfgJSON, err := json.Marshal(wrrLCfg)
if err != nil {
return nil, fmt.Errorf("error marshaling JSON for type %T: %v", wrrLCfg, err)
}
return makeBalancerConfigJSON(wrrlocality.Name, lbCfgJSON), nil
}
func convertWeightedRoundRobinProtoToServiceConfig(rawProto []byte, _ int) (json.RawMessage, error) {
cswrrProto := &v3clientsideweightedroundrobinpb.ClientSideWeightedRoundRobin{}
if err := proto.Unmarshal(rawProto, cswrrProto); err != nil {
return nil, fmt.Errorf("failed to unmarshal resource: %v", err)
}
wrrLBCfg := &wrrLBConfig{}
// Only set fields if specified in proto. If not set, ParseConfig of the WRR
// will populate the config with defaults.
if enableOOBLoadReportCfg := cswrrProto.GetEnableOobLoadReport(); enableOOBLoadReportCfg != nil {
wrrLBCfg.EnableOOBLoadReport = enableOOBLoadReportCfg.GetValue()
}
if oobReportingPeriodCfg := cswrrProto.GetOobReportingPeriod(); oobReportingPeriodCfg != nil {
wrrLBCfg.OOBReportingPeriod = internalserviceconfig.Duration(oobReportingPeriodCfg.AsDuration())
}
if blackoutPeriodCfg := cswrrProto.GetBlackoutPeriod(); blackoutPeriodCfg != nil {
wrrLBCfg.BlackoutPeriod = internalserviceconfig.Duration(blackoutPeriodCfg.AsDuration())
}
if weightExpirationPeriodCfg := cswrrProto.GetWeightExpirationPeriod(); weightExpirationPeriodCfg != nil {
wrrLBCfg.WeightExpirationPeriod = internalserviceconfig.Duration(weightExpirationPeriodCfg.AsDuration())
}
if weightUpdatePeriodCfg := cswrrProto.GetWeightUpdatePeriod(); weightUpdatePeriodCfg != nil {
wrrLBCfg.WeightUpdatePeriod = internalserviceconfig.Duration(weightUpdatePeriodCfg.AsDuration())
}
if errorUtilizationPenaltyCfg := cswrrProto.GetErrorUtilizationPenalty(); errorUtilizationPenaltyCfg != nil {
wrrLBCfg.ErrorUtilizationPenalty = float64(errorUtilizationPenaltyCfg.GetValue())
}
lbCfgJSON, err := json.Marshal(wrrLBCfg)
if err != nil {
return nil, fmt.Errorf("error marshaling JSON for type %T: %v", wrrLBCfg, err)
}
return makeBalancerConfigJSON(weightedroundrobin.Name, lbCfgJSON), nil
}
func convertLeastRequestProtoToServiceConfig(rawProto []byte, _ int) (json.RawMessage, error) {
lrProto := &v3leastrequestpb.LeastRequest{}
if err := proto.Unmarshal(rawProto, lrProto); err != nil {
return nil, fmt.Errorf("failed to unmarshal resource: %v", err)
}
// "The configuration for the Least Request LB policy is the
// least_request_lb_config field. The field is optional; if not present,
// defaults will be assumed for all of its values." - A48
choiceCount := uint32(defaultLeastRequestChoiceCount)
if cc := lrProto.GetChoiceCount(); cc != nil {
choiceCount = cc.GetValue()
}
lrCfg := &leastrequest.LBConfig{ChoiceCount: choiceCount}
js, err := json.Marshal(lrCfg)
if err != nil {
return nil, fmt.Errorf("error marshaling JSON for type %T: %v", lrCfg, err)
}
return makeBalancerConfigJSON(leastrequest.Name, js), nil
}
func convertV1TypedStructToServiceConfig(rawProto []byte, _ int) (json.RawMessage, error) {
tsProto := &v1xdsudpatypepb.TypedStruct{}
if err := proto.Unmarshal(rawProto, tsProto); err != nil {
return nil, fmt.Errorf("failed to unmarshal resource: %v", err)
}
return convertCustomPolicy(tsProto.GetTypeUrl(), tsProto.GetValue())
}
func convertV3TypedStructToServiceConfig(rawProto []byte, _ int) (json.RawMessage, error) {
tsProto := &v3xdsxdstypepb.TypedStruct{}
if err := proto.Unmarshal(rawProto, tsProto); err != nil {
return nil, fmt.Errorf("failed to unmarshal resource: %v", err)
}
return convertCustomPolicy(tsProto.GetTypeUrl(), tsProto.GetValue())
}
// convertCustomPolicy attempts to prepare json configuration for a custom lb
// proto, which specifies the gRPC balancer type and configuration. Returns the
// converted json and an error which should cause caller to error if error
// converting. If both json and error returned are nil, it means the gRPC
// Balancer registry does not contain that balancer type, and the caller should
// continue to the next policy.
func convertCustomPolicy(typeURL string, s *structpb.Struct) (json.RawMessage, error) {
// The gRPC policy name will be the "type name" part of the value of the
// type_url field in the TypedStruct. We get this by using the part after
// the last / character. Can assume a valid type_url from the control plane.
pos := strings.LastIndex(typeURL, "/")
name := typeURL[pos+1:]
if balancer.Get(name) == nil {
return nil, nil
}
rawJSON, err := json.Marshal(s)
if err != nil {
return nil, fmt.Errorf("error converting custom lb policy %v: %v for %+v", err, typeURL, s)
}
// The Struct contained in the TypedStruct will be returned as-is as the
// configuration JSON object.
return makeBalancerConfigJSON(name, rawJSON), nil
}
type wrrLBConfig struct {
EnableOOBLoadReport bool `json:"enableOobLoadReport,omitempty"`
OOBReportingPeriod internalserviceconfig.Duration `json:"oobReportingPeriod,omitempty"`
BlackoutPeriod internalserviceconfig.Duration `json:"blackoutPeriod,omitempty"`
WeightExpirationPeriod internalserviceconfig.Duration `json:"weightExpirationPeriod,omitempty"`
WeightUpdatePeriod internalserviceconfig.Duration `json:"weightUpdatePeriod,omitempty"`
ErrorUtilizationPenalty float64 `json:"errorUtilizationPenalty,omitempty"`
}
func makeBalancerConfigJSON(name string, value json.RawMessage) []byte {
return []byte(fmt.Sprintf(`[{%q: %s}]`, name, value))
}

View File

@@ -0,0 +1,82 @@
/*
*
* Copyright 2023 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Package xdslbregistry provides a registry of converters that convert proto
// from load balancing configuration, defined by the xDS API spec, to JSON load
// balancing configuration.
package xdslbregistry
import (
"encoding/json"
"fmt"
v3clusterpb "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
)
var (
// m is a map from proto type to Converter.
m = make(map[string]Converter)
)
// Register registers the converter to the map keyed on a proto type. Must be
// called at init time. Not thread safe.
func Register(protoType string, c Converter) {
m[protoType] = c
}
// SetRegistry sets the xDS LB registry. Must be called at init time. Not thread
// safe.
func SetRegistry(registry map[string]Converter) {
m = registry
}
// Converter converts raw proto bytes into the internal Go JSON representation
// of the proto passed. Returns the json message, and an error. If both
// returned are nil, it represents continuing to the next proto.
type Converter func([]byte, int) (json.RawMessage, error)
// ConvertToServiceConfig converts a proto Load Balancing Policy configuration
// into a json string. Returns an error if:
// - no supported policy found
// - there is more than 16 layers of recursion in the configuration
// - a failure occurs when converting the policy
func ConvertToServiceConfig(lbPolicy *v3clusterpb.LoadBalancingPolicy, depth int) (json.RawMessage, error) {
// "Configurations that require more than 16 levels of recursion are
// considered invalid and should result in a NACK response." - A51
if depth > 15 {
return nil, fmt.Errorf("lb policy %v exceeds max depth supported: 16 layers", lbPolicy)
}
// "This function iterate over the list of policy messages in
// LoadBalancingPolicy, attempting to convert each one to gRPC form,
// stopping at the first supported policy." - A52
for _, policy := range lbPolicy.GetPolicies() {
converter := m[policy.GetTypedExtensionConfig().GetTypedConfig().GetTypeUrl()]
// "Any entry not in the above list is unsupported and will be skipped."
// - A52
if converter == nil {
continue
}
json, err := converter(policy.GetTypedExtensionConfig().GetTypedConfig().GetValue(), depth)
if json == nil && err == nil {
continue
}
return json, err
}
return nil, fmt.Errorf("no supported policy found in policy list +%v", lbPolicy)
}

View File

@@ -0,0 +1,459 @@
/*
*
* Copyright 2023 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Package xdslbregistry_test contains test cases for the xDS LB Policy Registry.
package xdslbregistry_test
import (
"encoding/json"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
_ "google.golang.org/grpc/balancer/roundrobin"
"google.golang.org/grpc/internal/balancer/stub"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/internal/pretty"
internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/xds/balancer/wrrlocality"
"google.golang.org/grpc/internal/xds/xdsclient/xdslbregistry"
_ "google.golang.org/grpc/xds" // Register the xDS LB Registry Converters.
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/wrapperspb"
v1xdsudpatypepb "github.com/cncf/xds/go/udpa/type/v1"
v3xdsxdstypepb "github.com/cncf/xds/go/xds/type/v3"
v3clusterpb "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
v3clientsideweightedroundrobinpb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/client_side_weighted_round_robin/v3"
v3leastrequestpb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/least_request/v3"
v3maglevpb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/maglev/v3"
v3pickfirstpb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/pick_first/v3"
v3ringhashpb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/ring_hash/v3"
v3roundrobinpb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/round_robin/v3"
v3wrrlocalitypb "github.com/envoyproxy/go-control-plane/envoy/extensions/load_balancing_policies/wrr_locality/v3"
)
type s struct {
grpctest.Tester
}
func Test(t *testing.T) {
grpctest.RunSubTests(t, s{})
}
func wrrLocalityBalancerConfig(childPolicy *internalserviceconfig.BalancerConfig) *internalserviceconfig.BalancerConfig {
return &internalserviceconfig.BalancerConfig{
Name: wrrlocality.Name,
Config: &wrrlocality.LBConfig{
ChildPolicy: childPolicy,
},
}
}
func (s) TestConvertToServiceConfigSuccess(t *testing.T) {
const customLBPolicyName = "myorg.MyCustomLeastRequestPolicy"
stub.Register(customLBPolicyName, stub.BalancerFuncs{})
tests := []struct {
name string
policy *v3clusterpb.LoadBalancingPolicy
wantConfig string // JSON config
lrEnabled bool
}{
{
name: "ring_hash",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3ringhashpb.RingHash{
HashFunction: v3ringhashpb.RingHash_XX_HASH,
MinimumRingSize: wrapperspb.UInt64(10),
MaximumRingSize: wrapperspb.UInt64(100),
}),
},
},
},
},
wantConfig: `[{"ring_hash_experimental": { "minRingSize": 10, "maxRingSize": 100 }}]`,
},
{
name: "least_request",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3leastrequestpb.LeastRequest{
ChoiceCount: wrapperspb.UInt32(3),
}),
},
},
},
},
wantConfig: `[{"least_request_experimental": { "choiceCount": 3 }}]`,
lrEnabled: true,
},
{
name: "pick_first_shuffle",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3pickfirstpb.PickFirst{
ShuffleAddressList: true,
}),
},
},
},
},
wantConfig: `[{"pick_first": { "shuffleAddressList": true }}]`,
},
{
name: "pick_first",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3pickfirstpb.PickFirst{}),
},
},
},
},
wantConfig: `[{"pick_first": { "shuffleAddressList": false }}]`,
},
{
name: "round_robin",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3roundrobinpb.RoundRobin{}),
},
},
},
},
wantConfig: `[{"round_robin": {}}]`,
},
{
name: "weighted_round_robin",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3clientsideweightedroundrobinpb.ClientSideWeightedRoundRobin{}),
},
},
},
},
wantConfig: `[{"weighted_round_robin": {}}]`,
},
{
name: "weighted_round_robin_populated",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3clientsideweightedroundrobinpb.ClientSideWeightedRoundRobin{
EnableOobLoadReport: wrapperspb.Bool(true),
OobReportingPeriod: durationpb.New(10 * time.Second),
BlackoutPeriod: durationpb.New(5 * time.Second),
WeightExpirationPeriod: durationpb.New(3 * time.Minute),
WeightUpdatePeriod: durationpb.New(1 * time.Second),
ErrorUtilizationPenalty: wrapperspb.Float(1.5),
}),
},
},
},
},
wantConfig: `[{"weighted_round_robin": {
"enableOobLoadReport": true,
"oobReportingPeriod": "10s",
"blackoutPeriod": "5s",
"weightExpirationPeriod": "180s",
"weightUpdatePeriod": "1s",
"errorUtilizationPenalty": 1.5
}}]`,
},
{
name: "round_robin_ring_hash_use_first_supported",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3roundrobinpb.RoundRobin{}),
},
},
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3ringhashpb.RingHash{
HashFunction: v3ringhashpb.RingHash_XX_HASH,
MinimumRingSize: wrapperspb.UInt64(10),
MaximumRingSize: wrapperspb.UInt64(100),
}),
},
},
},
},
wantConfig: `[{"round_robin": {}}]`,
},
{
name: "pf_rr_use_pick_first",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3pickfirstpb.PickFirst{
ShuffleAddressList: true,
}),
},
},
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3roundrobinpb.RoundRobin{}),
},
},
},
},
wantConfig: `[{"pick_first": { "shuffleAddressList": true }}]`,
},
{
name: "custom_lb_type_v3_struct",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
// The type not registered in gRPC Policy registry.
// Should fallback to next policy in list.
TypedConfig: testutils.MarshalAny(t, &v3xdsxdstypepb.TypedStruct{
TypeUrl: "type.googleapis.com/myorg.ThisTypeDoesNotExist",
Value: &structpb.Struct{},
}),
},
},
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3xdsxdstypepb.TypedStruct{
TypeUrl: "type.googleapis.com/myorg.MyCustomLeastRequestPolicy",
Value: &structpb.Struct{},
}),
},
},
},
},
wantConfig: `[{"myorg.MyCustomLeastRequestPolicy": {}}]`,
},
{
name: "custom_lb_type_v1_struct",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v1xdsudpatypepb.TypedStruct{
TypeUrl: "type.googleapis.com/myorg.MyCustomLeastRequestPolicy",
Value: &structpb.Struct{},
}),
},
},
},
},
wantConfig: `[{"myorg.MyCustomLeastRequestPolicy": {}}]`,
},
{
name: "wrr_locality_child_round_robin",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: wrrLocalityAny(t, &v3roundrobinpb.RoundRobin{}),
},
},
},
},
wantConfig: `[{"xds_wrr_locality_experimental": { "childPolicy": [{"round_robin": {}}] }}]`,
},
{
name: "wrr_locality_child_custom_lb_type_v3_struct",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: wrrLocalityAny(t, &v3xdsxdstypepb.TypedStruct{
TypeUrl: "type.googleapis.com/myorg.MyCustomLeastRequestPolicy",
Value: &structpb.Struct{},
}),
},
},
},
},
wantConfig: `[{"xds_wrr_locality_experimental": { "childPolicy": [{"myorg.MyCustomLeastRequestPolicy": {}}] }}]`,
},
{
name: "on-the-boundary-of-recursive-limit",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: wrrLocalityAny(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, &v3roundrobinpb.RoundRobin{}))))))))))))))),
},
},
},
},
wantConfig: jsonMarshal(t, wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(wrrLocalityBalancerConfig(&internalserviceconfig.BalancerConfig{
Name: "round_robin",
})))))))))))))))),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
rawJSON, err := xdslbregistry.ConvertToServiceConfig(test.policy, 0)
if err != nil {
t.Fatalf("ConvertToServiceConfig(%s) failed: %v", pretty.ToJSON(test.policy), err)
}
// got and want must be unmarshalled since JSON strings shouldn't
// generally be directly compared.
var got []map[string]any
if err := json.Unmarshal(rawJSON, &got); err != nil {
t.Fatalf("Error unmarshalling rawJSON (%q): %v", rawJSON, err)
}
var want []map[string]any
if err := json.Unmarshal(json.RawMessage(test.wantConfig), &want); err != nil {
t.Fatalf("Error unmarshalling wantConfig (%q): %v", test.wantConfig, err)
}
if diff := cmp.Diff(got, want); diff != "" {
t.Fatalf("ConvertToServiceConfig() got unexpected output, diff (-got +want): %v", diff)
}
})
}
}
func jsonMarshal(t *testing.T, x any) string {
t.Helper()
js, err := json.Marshal(x)
if err != nil {
t.Fatalf("Error marshalling to JSON (%+v): %v", x, err)
}
return string(js)
}
// TestConvertToServiceConfigFailure tests failure cases of the xDS LB registry
// of converting proto configuration to JSON configuration.
func (s) TestConvertToServiceConfigFailure(t *testing.T) {
tests := []struct {
name string
policy *v3clusterpb.LoadBalancingPolicy
wantErr string
}{
{
name: "not xx_hash function",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, &v3ringhashpb.RingHash{
HashFunction: v3ringhashpb.RingHash_MURMUR_HASH_2,
MinimumRingSize: wrapperspb.UInt64(10),
MaximumRingSize: wrapperspb.UInt64(100),
}),
},
},
},
},
wantErr: "unsupported ring_hash hash function",
},
{
name: "no-supported-policy",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
// The type not registered in gRPC Policy registry.
TypedConfig: testutils.MarshalAny(t, &v3xdsxdstypepb.TypedStruct{
TypeUrl: "type.googleapis.com/myorg.ThisTypeDoesNotExist",
Value: &structpb.Struct{},
}),
},
},
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
// Maglev is not yet supported by gRPC.
TypedConfig: testutils.MarshalAny(t, &v3maglevpb.Maglev{}),
},
},
},
},
wantErr: "no supported policy found in policy list",
},
{
name: "exceeds-boundary-of-recursive-limit-by-1",
policy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: wrrLocalityAny(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, wrrLocality(t, &v3roundrobinpb.RoundRobin{})))))))))))))))),
},
},
},
},
wantErr: "exceeds max depth",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, gotErr := xdslbregistry.ConvertToServiceConfig(test.policy, 0)
// Test the error substring to test the different root causes of
// errors. This is more brittle over time, but it's important to
// test the root cause of the errors emitted from the
// ConvertToServiceConfig function call. Also, this package owns the
// error strings so breakages won't come unexpectedly.
if gotErr == nil || !strings.Contains(gotErr.Error(), test.wantErr) {
t.Fatalf("ConvertToServiceConfig() = %v, wantErr %v", gotErr, test.wantErr)
}
})
}
}
// wrrLocality is a helper that takes a proto message and returns a
// WrrLocalityProto with the proto message marshaled into a proto.Any as a
// child.
func wrrLocality(t *testing.T, m proto.Message) *v3wrrlocalitypb.WrrLocality {
return &v3wrrlocalitypb.WrrLocality{
EndpointPickingPolicy: &v3clusterpb.LoadBalancingPolicy{
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{
{
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
TypedConfig: testutils.MarshalAny(t, m),
},
},
},
},
}
}
// wrrLocalityAny takes a proto message and returns a wrr locality proto
// marshaled as an any with an any child set to the marshaled proto message.
func wrrLocalityAny(t *testing.T, m proto.Message) *anypb.Any {
return testutils.MarshalAny(t, wrrLocality(t, m))
}