Skip to content

Commit

Permalink
xds: move balancergroup and weightedtarget our of xds directory (#4966)
Browse files Browse the repository at this point in the history
  • Loading branch information
easwars committed Nov 9, 2021
1 parent 14ebd91 commit 714ba8d
Show file tree
Hide file tree
Showing 34 changed files with 1,867 additions and 1,372 deletions.
File renamed without changes.
Expand Up @@ -17,21 +17,23 @@
*/

// Package weightedtarget implements the weighted_target balancer.
//
// All APIs in this package are experimental.
package weightedtarget

import (
"encoding/json"
"fmt"

"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/weightedtarget/weightedaggregator"
"google.golang.org/grpc/internal/balancergroup"
"google.golang.org/grpc/internal/grpclog"
"google.golang.org/grpc/internal/hierarchy"
"google.golang.org/grpc/internal/pretty"
"google.golang.org/grpc/internal/wrr"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/serviceconfig"
"google.golang.org/grpc/xds/internal/balancer/balancergroup"
"google.golang.org/grpc/xds/internal/balancer/weightedtarget/weightedaggregator"
)

// Name is the name of the weighted_target balancer.
Expand Down Expand Up @@ -69,11 +71,6 @@ func (bb) ParseConfig(c json.RawMessage) (serviceconfig.LoadBalancingConfig, err
type weightedTargetBalancer struct {
logger *grpclog.PrefixLogger

// TODO: Make this package not dependent on any xds specific code.
// BalancerGroup uses xdsinternal.LocalityID as the key in the map of child
// policies that it maintains and reports load using LRS. Once these two
// dependencies are removed from the balancerGroup, this package will not
// have any dependencies on xds code.
bg *balancergroup.BalancerGroup
stateAggregator *weightedaggregator.Aggregator

Expand Down
Expand Up @@ -23,34 +23,38 @@ import (

"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/balancer"
_ "google.golang.org/grpc/balancer/grpclb"
"google.golang.org/grpc/balancer/roundrobin"
internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
"google.golang.org/grpc/xds/internal/balancer/priority"
)

const (
testJSONConfig = `{
"targets": {
"cluster_1" : {
"weight":75,
"childPolicy":[{"priority_experimental":{"priorities": ["child-1"], "children": {"child-1": {"config": [{"round_robin":{}}]}}}}]
"cluster_1": {
"weight": 75,
"childPolicy": [{
"grpclb": {
"childPolicy": [{"pick_first":{}}],
"targetName": "foo-service"
}
}]
},
"cluster_2" : {
"weight":25,
"childPolicy":[{"priority_experimental":{"priorities": ["child-2"], "children": {"child-2": {"config": [{"round_robin":{}}]}}}}]
"cluster_2": {
"weight": 25,
"childPolicy": [{"round_robin": ""}]
}
}
}`
)

var (
testConfigParser = balancer.Get(priority.Name).(balancer.ConfigParser)
testConfigJSON1 = `{"priorities": ["child-1"], "children": {"child-1": {"config": [{"round_robin":{}}]}}}`
testConfig1, _ = testConfigParser.ParseConfig([]byte(testConfigJSON1))
testConfigJSON2 = `{"priorities": ["child-2"], "children": {"child-2": {"config": [{"round_robin":{}}]}}}`
testConfig2, _ = testConfigParser.ParseConfig([]byte(testConfigJSON2))
grpclbConfigParser = balancer.Get("grpclb").(balancer.ConfigParser)
grpclbConfigJSON = `{"childPolicy": [{"pick_first":{}}], "targetName": "foo-service"}`
grpclbConfig, _ = grpclbConfigParser.ParseConfig([]byte(grpclbConfigJSON))
)

func Test_parseConfig(t *testing.T) {
func (s) TestParseConfig(t *testing.T) {
tests := []struct {
name string
js string
Expand All @@ -71,15 +75,14 @@ func Test_parseConfig(t *testing.T) {
"cluster_1": {
Weight: 75,
ChildPolicy: &internalserviceconfig.BalancerConfig{
Name: priority.Name,
Config: testConfig1,
Name: "grpclb",
Config: grpclbConfig,
},
},
"cluster_2": {
Weight: 25,
ChildPolicy: &internalserviceconfig.BalancerConfig{
Name: priority.Name,
Config: testConfig2,
Name: roundrobin.Name,
},
},
},
Expand All @@ -91,8 +94,7 @@ func Test_parseConfig(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := parseConfig([]byte(tt.js))
if (err != nil) != tt.wantErr {
t.Errorf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
return
t.Fatalf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
}
if !cmp.Equal(got, tt.want) {
t.Errorf("parseConfig() got unexpected result, diff: %v", cmp.Diff(got, tt.want))
Expand Down

0 comments on commit 714ba8d

Please sign in to comment.