Skip to content

Commit

Permalink
add support for local zone affinity to sticky_random
Browse files Browse the repository at this point in the history
Using the zone local attribute inhjected by discovery (if it exists), update
sticky_random so that it biases to only use the local zone connections if there
are any available, otherwise fall back to remote.
  • Loading branch information
demmer committed Jan 23, 2025
1 parent 862b899 commit bde3588
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions go/vt/vtgateproxy/sticky_random_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,39 @@ func init() {

type stickyPickerBuilder struct{}

// Would be nice if this were easier in golang
func boolValue(val interface{}) bool {
switch val := val.(type) {
case bool:
return val
}
return false
}

func (*stickyPickerBuilder) Build(info base.PickerBuildInfo) balancer.Picker {
// log.V(100).Infof("stickyRandomPicker: Build called with info: %v", info)
if len(info.ReadySCs) == 0 {
return base.NewErrPicker(balancer.ErrNoSubConnAvailable)
}

scs := make([]balancer.SubConn, 0, len(info.ReadySCs))
for sc := range info.ReadySCs {
scs = append(scs, sc)

// Where possible filter to only ready conns in the local zone, using the remote
// zone only if there are no local conns available.
for sc, scInfo := range info.ReadySCs {
local := boolValue(scInfo.Address.Attributes.Value(ZoneLocalAttr))
if local {
scs = append(scs, sc)
}
}

// Otherwise use all the ready conns regardless of locality
if len(scs) == 0 {
for sc := range info.ReadySCs {
scs = append(scs, sc)
}
}

return &stickyPicker{
subConns: scs,
}
Expand Down

0 comments on commit bde3588

Please sign in to comment.