-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Implementation of A68 random_subsetting LB policy. #8650
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/* | ||
* | ||
* Copyright 2025 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 randomsubsetting defines a random subsetting balancer. | ||
// | ||
// To install random subsetting balancer, import this package as: | ||
// | ||
// import _ "google.golang.org/grpc/balancer/randomsubsetting" | ||
package randomsubsetting | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"sort" | ||
"time" | ||
|
||
"github.com/cespare/xxhash/v2" | ||
"google.golang.org/grpc/balancer" | ||
"google.golang.org/grpc/grpclog" | ||
"google.golang.org/grpc/internal/balancer/gracefulswitch" | ||
internalgrpclog "google.golang.org/grpc/internal/grpclog" | ||
iserviceconfig "google.golang.org/grpc/internal/serviceconfig" | ||
"google.golang.org/grpc/resolver" | ||
"google.golang.org/grpc/serviceconfig" | ||
) | ||
|
||
const ( | ||
// Name is the name of the random subsetting load balancer. | ||
Name = "random_subsetting" | ||
) | ||
|
||
var ( | ||
logger = grpclog.Component(Name) | ||
) | ||
|
||
func prefixLogger(p *subsettingBalancer) *internalgrpclog.PrefixLogger { | ||
return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf("[random-subsetting-lb %p] ", p)) | ||
} | ||
|
||
func init() { | ||
balancer.Register(bb{}) | ||
} | ||
|
||
type bb struct{} | ||
|
||
func (bb) Build(cc balancer.ClientConn, bOpts balancer.BuildOptions) balancer.Balancer { | ||
b := &subsettingBalancer{ | ||
cc: cc, | ||
hashf: xxhash.NewWithSeed(uint64(time.Now().UnixNano())), | ||
} | ||
// Create a logger with a prefix specific to this balancer instance. | ||
b.logger = prefixLogger(b) | ||
|
||
b.logger.Infof("Created") | ||
b.child = gracefulswitch.NewBalancer(cc, bOpts) | ||
return b | ||
} | ||
|
||
// LBConfig is the config for the outlier detection balancer. | ||
type LBConfig struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for this type to be exported. I don't see a reason here. So, if there is no reason, please unexport it. The fields can remain exported since you might want that for JSON marshal/unmarshal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
serviceconfig.LoadBalancingConfig `json:"-"` | ||
|
||
SubsetSize uint64 `json:"subset_size,omitempty"` | ||
|
||
ChildPolicy *iserviceconfig.BalancerConfig `json:"child_policy,omitempty"` | ||
Comment on lines
+79
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally use camelCase for the json annotations since the protobuf library uses that when marshalling protobuf messages to JSON. So, please change these to use camelCase. Thanks. |
||
} | ||
|
||
func (bb) ParseConfig(s json.RawMessage) (serviceconfig.LoadBalancingConfig, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to add unit tests for this method. Thanks. |
||
lbCfg := &LBConfig{ | ||
// Default top layer values. | ||
SubsetSize: 10, | ||
Comment on lines
+86
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is this default value specified? |
||
} | ||
|
||
if err := json.Unmarshal(s, lbCfg); err != nil { // Validates child config if present as well. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The gRFC also says that the child policy field needs to specified. So, we probably need a check here after the unmarshal that the field is actually specified and not empty. |
||
return nil, fmt.Errorf("subsetting: unable to unmarshal LBconfig: %s, error: %v", string(s), err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
} | ||
|
||
// if someonw needs subsetSize == 1, he should use pick_first instead | ||
if lbCfg.SubsetSize < 2 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The gRFC only says the following:
Is the condition that the size be at least |
||
return nil, errors.New("subsetting: subsetSize must be >= 2") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: You could use |
||
} | ||
|
||
return lbCfg, nil | ||
} | ||
|
||
func (bb) Name() string { | ||
return Name | ||
} | ||
|
||
type subsettingBalancer struct { | ||
cc balancer.ClientConn | ||
logger *internalgrpclog.PrefixLogger | ||
cfg *LBConfig | ||
hashf *xxhash.Digest | ||
child *gracefulswitch.Balancer | ||
} | ||
|
||
func (b *subsettingBalancer) UpdateClientConnState(s balancer.ClientConnState) error { | ||
lbCfg, ok := s.BalancerConfig.(*LBConfig) | ||
if !ok { | ||
b.logger.Errorf("received config with unexpected type %T: %v", s.BalancerConfig, s.BalancerConfig) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Please add the package name prefix to this error message as well. |
||
return balancer.ErrBadResolverState | ||
} | ||
|
||
// Reject whole config if child policy doesn't exist, don't persist it for | ||
// later. | ||
bb := balancer.Get(lbCfg.ChildPolicy.Name) | ||
if bb == nil { | ||
return fmt.Errorf("subsetting: child balancer %q not registered", lbCfg.ChildPolicy.Name) | ||
} | ||
Comment on lines
+121
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably do this when we parse the config and not here. |
||
|
||
if b.cfg == nil || b.cfg.ChildPolicy.Name != lbCfg.ChildPolicy.Name { | ||
err := b.child.SwitchTo(bb) | ||
if err != nil { | ||
Comment on lines
+129
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: The assignment and the conditional can be on the same line. |
||
return fmt.Errorf("subsetting: error switching to child of type %q: %v", lbCfg.ChildPolicy.Name, err) | ||
} | ||
} | ||
b.cfg = lbCfg | ||
|
||
err := b.child.UpdateClientConnState(balancer.ClientConnState{ | ||
ResolverState: b.prepareChildResolverState(s.ResolverState), | ||
BalancerConfig: b.cfg.ChildPolicy.Config, | ||
}) | ||
|
||
return err | ||
Comment on lines
+136
to
+141
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be shortened as: return b.child.UpdateClientConnState(balancer.ClientConnState{
ResolverState: b.prepareChildResolverState(s.ResolverState),
BalancerConfig: b.cfg.ChildPolicy.Config,
}) |
||
} | ||
|
||
type AddressWithHash struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this type have to be exported? If so, why? |
||
hash uint64 | ||
addr resolver.Address | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to store the endpoint and not just the first address in the endpoint. |
||
} | ||
|
||
// implements the subsetting algorithm, as described in A68: https://github.com/grpc/proposal/pull/423 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: A68 is now merged. So, you can actually link to the doc in the repo instead of linking to the PR. Also, you could do that at the top-level package docstring instead. |
||
func (b *subsettingBalancer) prepareChildResolverState(s resolver.State) resolver.State { | ||
addresses := s.Addresses | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be using the |
||
backendCount := len(addresses) | ||
if backendCount <= int(b.cfg.SubsetSize) { | ||
return s | ||
} | ||
|
||
addressesSet := make([]AddressWithHash, backendCount) | ||
// calculate hash for each endpoint | ||
for i, endpoint := range addresses { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: nix unwanted newline. |
||
b.hashf.Write([]byte(s.Addresses[0].String())) | ||
addressesSet[i] = AddressWithHash{ | ||
hash: b.hashf.Sum64(), | ||
addr: endpoint, | ||
} | ||
} | ||
// sort addresses by hash | ||
sort.Slice(addressesSet, func(i, j int) bool { | ||
return addressesSet[i].hash < addressesSet[j].hash | ||
}) | ||
|
||
b.logger.Infof("resulting subset: %v", addressesSet[:b.cfg.SubsetSize]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please guard this log line with a verbosity check of |
||
|
||
// Convert back to resolver.addresses | ||
addressesSubset := make([]resolver.Address, b.cfg.SubsetSize) | ||
for _, eh := range addressesSet[:b.cfg.SubsetSize] { | ||
addressesSubset = append(addressesSubset, eh.addr) | ||
} | ||
|
||
return resolver.State{ | ||
Addresses: addressesSubset, | ||
ServiceConfig: s.ServiceConfig, | ||
Attributes: s.Attributes, | ||
} | ||
Comment on lines
+174
to
+184
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of this should deal with endpoints and not addresses. |
||
} | ||
|
||
func (b *subsettingBalancer) ResolverError(err error) { | ||
b.child.ResolverError(err) | ||
} | ||
|
||
func (b *subsettingBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) { | ||
b.child.UpdateSubConnState(sc, state) | ||
} | ||
|
||
func (b *subsettingBalancer) Close() { | ||
b.child.Close() | ||
} | ||
|
||
func (b *subsettingBalancer) ExitIdle() { | ||
b.child.ExitIdle() | ||
} | ||
Comment on lines
+187
to
+201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we embed the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment needs to be updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done