Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xds: Outlier Detection configuration in Cluster Resolver Balancer #5371

Merged
merged 6 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions xds/internal/balancer/clusterresolver/configbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func buildPriorityConfig(priorities []priorityConfig, xdsLBPolicy *internalservi
retAddrs = append(retAddrs, addrs...)
var odCfg *outlierdetection.LBConfig
if envconfig.XDSOutlierDetection {
odCfg = makeClusterImplOutlierDetectionChild(*config, *p.mechanism.OutlierDetection)
odCfg = makeClusterImplOutlierDetectionChild(*config, p.mechanism.OutlierDetection)
retConfig.Children[name] = &priority.Child{
Config: &internalserviceconfig.BalancerConfig{Name: outlierdetection.Name, Config: odCfg},
// Not ignore re-resolution from DNS children, they will trigger
Expand All @@ -181,15 +181,18 @@ func buildPriorityConfig(priorities []priorityConfig, xdsLBPolicy *internalservi
func convertClusterImplMapToOutlierDetection(ciCfgs map[string]*clusterimpl.LBConfig, odCfg *outlierdetection.LBConfig) map[string]*outlierdetection.LBConfig {
odCfgs := make(map[string]*outlierdetection.LBConfig, len(ciCfgs))
for n, c := range ciCfgs {
odCfgs[n] = makeClusterImplOutlierDetectionChild(*c, *odCfg)
odCfgs[n] = makeClusterImplOutlierDetectionChild(*c, odCfg)
}
return odCfgs
}

func makeClusterImplOutlierDetectionChild(ciCfg clusterimpl.LBConfig, odCfg outlierdetection.LBConfig) *outlierdetection.LBConfig {
odCfgRet := odCfg
odCfgRet.ChildPolicy = &internalserviceconfig.BalancerConfig{Name: clusterimpl.Name, Config: &ciCfg} // This can panic if odCfg is nil. This shouldn't be nil though, as per CDS balancer. I can add check if you want.
return &odCfgRet
func makeClusterImplOutlierDetectionChild(ciCfg clusterimpl.LBConfig, odCfg *outlierdetection.LBConfig) *outlierdetection.LBConfig {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not pass ciCfg by pointer? Generally structs should be passed by pointer unless there's a good reason not to.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: go/go-style/decisions#pass-values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to passing by pointer.

odCfgRet := &outlierdetection.LBConfig{}
if odCfg != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it valid for this to be nil? Why? For the JSON config, can we make the field a value instead of a pointer so it's never nil? Then pass by value to avoid the explicit copy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not valid. This config is prepared by the cluster resolver, so we're guaranteed for this not to be nil, but this handles the nil panic I discussed earlier with you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had the discovery mechanism hold onto a value, and pass it by value around. I kept this function returning a pointer though, as that is the value of internalserviceconfig.BalancerConfig that I populate.

*odCfgRet = *odCfg
}
odCfgRet.ChildPolicy = &internalserviceconfig.BalancerConfig{Name: clusterimpl.Name, Config: &ciCfg}
return odCfgRet
}

func buildClusterImplConfigForDNS(g *nameGenerator, addrStrs []string, mechanism DiscoveryMechanism) (string, *clusterimpl.LBConfig, []resolver.Address) {
Expand Down
8 changes: 7 additions & 1 deletion xds/internal/balancer/outlierdetection/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package outlierdetection

import (
"encoding/json"
"errors"
"fmt"

"google.golang.org/grpc/balancer"
Expand All @@ -43,7 +44,7 @@ func (bb) Build(cc balancer.ClientConn, bOpts balancer.BuildOptions) balancer.Ba

func (bb) ParseConfig(s json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
var lbCfg *LBConfig
if err := json.Unmarshal(s, &lbCfg); err != nil {
if err := json.Unmarshal(s, &lbCfg); err != nil { // Validates child config if present as well.
return nil, fmt.Errorf("xds: unable to unmarshal LBconfig: %s, error: %v", string(s), err)
}

Expand Down Expand Up @@ -90,6 +91,11 @@ func (bb) ParseConfig(s json.RawMessage) (serviceconfig.LoadBalancingConfig, err
if lbCfg.FailurePercentageEjection != nil && lbCfg.FailurePercentageEjection.EnforcementPercentage > 100 {
return nil, fmt.Errorf("LBConfig.FailurePercentageEjection.EnforcementPercentage = %v; must be <= 100", lbCfg.FailurePercentageEjection.EnforcementPercentage)
}

if lbCfg.ChildPolicy == nil {
return nil, errors.New("LBConfig.ChildPolicy needs to be present")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/needs to/must/?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

return lbCfg, nil
}

Expand Down
92 changes: 89 additions & 3 deletions xds/internal/balancer/outlierdetection/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ package outlierdetection

import (
"encoding/json"
"errors"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/internal/grpctest"
internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
"google.golang.org/grpc/serviceconfig"
Expand All @@ -50,9 +52,26 @@ func (s) TestParseConfig(t *testing.T) {
wantErr string
}{
{
name: "noop-lb-config",
input: `{"interval": 9223372036854775807}`,
wantCfg: &LBConfig{Interval: 1<<63 - 1},
name: "noop-lb-config",
input: `{
"interval": 9223372036854775807,
"childPolicy": [
{
"xds_cluster_impl_experimental": {
"cluster": "test_cluster"
}
}
]
}`,
wantCfg: &LBConfig{
Interval: 1<<63 - 1,
ChildPolicy: &internalserviceconfig.BalancerConfig{
Name: "xds_cluster_impl_experimental",
Config: &clusterimpl.LBConfig{
Cluster: "test_cluster",
},
},
},
},
{
name: "good-lb-config",
Expand All @@ -72,7 +91,14 @@ func (s) TestParseConfig(t *testing.T) {
"enforcementPercentage": 5,
"minimumHosts": 5,
"requestVolume": 50
},
"childPolicy": [
{
"xds_cluster_impl_experimental": {
"cluster": "test_cluster"
}
}
]
}`,
wantCfg: &LBConfig{
Interval: 10 * time.Second,
Expand All @@ -91,6 +117,12 @@ func (s) TestParseConfig(t *testing.T) {
MinimumHosts: 5,
RequestVolume: 50,
},
ChildPolicy: &internalserviceconfig.BalancerConfig{
Name: "xds_cluster_impl_experimental",
Config: &clusterimpl.LBConfig{
Cluster: "test_cluster",
},
},
},
},
{
Expand Down Expand Up @@ -140,6 +172,42 @@ func (s) TestParseConfig(t *testing.T) {
}`,
wantErr: "LBConfig.FailurePercentageEjection.EnforcementPercentage = 150; must be <= 100",
},
{
name: "child-policy-not-present",
input: `{
"interval": 10000000000,
"baseEjectionTime": 30000000000,
"maxEjectionTime": 300000000000,
"maxEjectionPercent": 10,
"successRateEjection": {
"stdevFactor": 1900,
"enforcementPercentage": 100,
"minimumHosts": 5,
"requestVolume": 100
},
"failurePercentageEjection": {
"threshold": 85,
"enforcementPercentage": 5,
"minimumHosts": 5,
"requestVolume": 50
}
}`,
wantErr: "LBConfig.ChildPolicy needs to be present",
},
{
name: "child-policy-present-but-parse-error",
input: `{
"interval": 9223372036854775807,
"childPolicy": [
{
"errParseConfigBalancer": {
"cluster": "test_cluster"
}
}
]
}`,
wantErr: "error parsing loadBalancingConfig for policy \"errParseConfigBalancer\"",
},
{
name: "child-policy",
input: `{
Expand Down Expand Up @@ -186,3 +254,21 @@ func (lbc *LBConfig) Equal(lbc2 *LBConfig) bool {
}
return cmp.Equal(lbc.ChildPolicy, lbc2.ChildPolicy)
}

func init() {
easwars marked this conversation as resolved.
Show resolved Hide resolved
balancer.Register(errParseConfigBuilder{})
}

type errParseConfigBuilder struct{}

func (errParseConfigBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
return nil
}

func (errParseConfigBuilder) Name() string {
return "errParseConfigBalancer"
}

func (errParseConfigBuilder) ParseConfig(c json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
return nil, errors.New("some error")
}