-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xds/internal/xdsclient: Custom LB xDS Client Changes (#6165)
- Loading branch information
Showing
13 changed files
with
1,452 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* | ||
* 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 wrrlocality provides an implementation of the wrr locality LB policy, | ||
// as defined in [A52 - xDS Custom LB Policies]. | ||
// | ||
// [A52 - xDS Custom LB Policies]: https://github.com/grpc/proposal/blob/master/A52-xds-custom-lb-policies.md | ||
package wrrlocality | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"google.golang.org/grpc/balancer" | ||
internalserviceconfig "google.golang.org/grpc/internal/serviceconfig" | ||
"google.golang.org/grpc/serviceconfig" | ||
) | ||
|
||
// Name is the name of wrr_locality balancer. | ||
const Name = "xds_wrr_locality_experimental" | ||
|
||
func init() { | ||
balancer.Register(bb{}) | ||
} | ||
|
||
type bb struct{} | ||
|
||
func (bb) Name() string { | ||
return Name | ||
} | ||
|
||
func (bb) Build(cc balancer.ClientConn, bOpts balancer.BuildOptions) balancer.Balancer { | ||
return nil | ||
} | ||
|
||
// LBConfig is the config for the wrr locality balancer. | ||
type LBConfig struct { | ||
serviceconfig.LoadBalancingConfig | ||
// ChildPolicy is the config for the child policy. | ||
ChildPolicy *internalserviceconfig.BalancerConfig `json:"childPolicy,omitempty"` | ||
} | ||
|
||
func (bb) ParseConfig(s json.RawMessage) (serviceconfig.LoadBalancingConfig, error) { | ||
var lbCfg *LBConfig | ||
if err := json.Unmarshal(s, &lbCfg); err != nil { | ||
return nil, fmt.Errorf("xds: invalid LBConfig for wrrlocality: %s, error: %v", string(s), err) | ||
} | ||
if lbCfg == nil || lbCfg.ChildPolicy == nil { | ||
return nil, errors.New("xds: invalidw LBConfig for wrrlocality: child policy field must be set") | ||
} | ||
return lbCfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* | ||
* 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 wrrlocality | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
"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" | ||
internalserviceconfig "google.golang.org/grpc/internal/serviceconfig" | ||
"google.golang.org/grpc/serviceconfig" | ||
) | ||
|
||
type s struct { | ||
grpctest.Tester | ||
} | ||
|
||
func Test(t *testing.T) { | ||
grpctest.RunSubTests(t, s{}) | ||
} | ||
|
||
func (s) TestParseConfig(t *testing.T) { | ||
const errParseConfigName = "errParseConfigBalancer" | ||
stub.Register(errParseConfigName, stub.BalancerFuncs{ | ||
ParseConfig: func(json.RawMessage) (serviceconfig.LoadBalancingConfig, error) { | ||
return nil, errors.New("some error") | ||
}, | ||
}) | ||
|
||
parser := bb{} | ||
tests := []struct { | ||
name string | ||
input string | ||
wantCfg serviceconfig.LoadBalancingConfig | ||
wantErr string | ||
}{ | ||
{ | ||
name: "happy-case-round robin-child", | ||
input: `{"childPolicy": [{"round_robin": {}}]}`, | ||
wantCfg: &LBConfig{ | ||
ChildPolicy: &internalserviceconfig.BalancerConfig{ | ||
Name: roundrobin.Name, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "invalid-json", | ||
input: "{{invalidjson{{", | ||
wantErr: "invalid character", | ||
}, | ||
|
||
{ | ||
name: "child-policy-field-isn't-set", | ||
input: `{}`, | ||
wantErr: "child policy field must be set", | ||
}, | ||
{ | ||
name: "child-policy-type-is-empty", | ||
input: `{"childPolicy": []}`, | ||
wantErr: "invalid loadBalancingConfig: no supported policies found in []", | ||
}, | ||
{ | ||
name: "child-policy-empty-config", | ||
input: `{"childPolicy": [{"": {}}]}`, | ||
wantErr: "invalid loadBalancingConfig: no supported policies found in []", | ||
}, | ||
{ | ||
name: "child-policy-type-isn't-registered", | ||
input: `{"childPolicy": [{"doesNotExistBalancer": {"cluster": "test_cluster"}}]}`, | ||
wantErr: "invalid loadBalancingConfig: no supported policies found in [doesNotExistBalancer]", | ||
}, | ||
{ | ||
name: "child-policy-config-is-invalid", | ||
input: `{"childPolicy": [{"errParseConfigBalancer": {"cluster": "test_cluster"}}]}`, | ||
wantErr: "error parsing loadBalancingConfig for policy \"errParseConfigBalancer\"", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
gotCfg, gotErr := parser.ParseConfig(json.RawMessage(test.input)) | ||
// Substring match makes this very tightly coupled to the | ||
// internalserviceconfig.BalancerConfig error strings. However, it | ||
// is important to distinguish the different types of error messages | ||
// possible as the parser has a few defined buckets of ways it can | ||
// error out. | ||
if (gotErr != nil) != (test.wantErr != "") { | ||
t.Fatalf("ParseConfig(%v) = %v, wantErr %v", test.input, gotErr, test.wantErr) | ||
} | ||
if gotErr != nil && !strings.Contains(gotErr.Error(), test.wantErr) { | ||
t.Fatalf("ParseConfig(%v) = %v, wantErr %v", test.input, gotErr, test.wantErr) | ||
} | ||
if test.wantErr != "" { | ||
return | ||
} | ||
if diff := cmp.Diff(gotCfg, test.wantCfg); diff != "" { | ||
t.Fatalf("ParseConfig(%v) got unexpected output, diff (-got +want): %v", test.input, diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.