Skip to content

Commit

Permalink
balancer/weightedroundrobin: Switch Weighted Round Robin to use pick …
Browse files Browse the repository at this point in the history
…first instead of SubConns (#7826)
  • Loading branch information
zasweq authored Nov 23, 2024
1 parent 93f1cc1 commit 13d5a16
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 305 deletions.
27 changes: 19 additions & 8 deletions balancer/endpointsharding/endpointsharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,33 @@ package endpointsharding
import (
"encoding/json"
"errors"
"fmt"
rand "math/rand/v2"
"sync"
"sync/atomic"

rand "math/rand/v2"

"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/base"
"google.golang.org/grpc/balancer/pickfirst"
"google.golang.org/grpc/balancer/pickfirst/pickfirstleaf"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/internal/balancer/gracefulswitch"
"google.golang.org/grpc/internal/envconfig"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/serviceconfig"
)

// PickFirstConfig is a pick first config without shuffling enabled.
var PickFirstConfig string

func init() {
name := pickfirst.Name
if !envconfig.NewPickFirstEnabled {
name = pickfirstleaf.Name
}
PickFirstConfig = fmt.Sprintf("[{%q: {}}]", name)
}

// ChildState is the balancer state of a child along with the endpoint which
// identifies the child balancer.
type ChildState struct {
Expand Down Expand Up @@ -100,9 +114,6 @@ func (es *endpointSharding) UpdateClientConnState(state balancer.ClientConnState

// Update/Create new children.
for _, endpoint := range state.ResolverState.Endpoints {
if len(endpoint.Addresses) == 0 {
continue
}
if _, ok := newChildren.Get(endpoint); ok {
// Endpoint child was already created, continue to avoid duplicate
// update.
Expand Down Expand Up @@ -143,6 +154,9 @@ func (es *endpointSharding) UpdateClientConnState(state balancer.ClientConnState
}
}
es.children.Store(newChildren)
if newChildren.Len() == 0 {
return balancer.ErrBadResolverState
}
return ret
}

Expand Down Expand Up @@ -306,6 +320,3 @@ func (bw *balancerWrapper) UpdateState(state balancer.State) {
func ParseConfig(cfg json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
return gracefulswitch.ParseConfig(cfg)
}

// PickFirstConfig is a pick first config without shuffling enabled.
const PickFirstConfig = "[{\"pick_first\": {}}]"
Loading

0 comments on commit 13d5a16

Please sign in to comment.