Skip to content

Commit

Permalink
Guard childState and inhibitPickerUpdates access with mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
aranjans committed Oct 3, 2024
1 parent e67b0d9 commit f687883
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions xds/internal/balancer/clusterimpl/clusterimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ type clusterImplBalancer struct {
// childState/drops/requestCounter keeps the state used by the most recently
// generated picker.
childState balancer.State
mu sync.Mutex
dropCategories []DropConfig // The categories for drops.
drops []*dropper
requestCounterCluster string // The cluster name for the request counter.
Expand Down Expand Up @@ -254,9 +255,8 @@ func (b *clusterImplBalancer) updateClientConnState(s balancer.ClientConnState)

b.config = newConfig

b.mu.Lock()
b.inhibitPickerUpdates = true
defer func() { b.inhibitPickerUpdates = false }()

b.telemetryLabels = newConfig.TelemetryLabels
dc := b.handleDropAndRequestCount(newConfig)
if dc != nil && b.childState.Picker != nil {
Expand All @@ -265,6 +265,8 @@ func (b *clusterImplBalancer) updateClientConnState(s balancer.ClientConnState)
Picker: b.newPicker(dc),
})
}
b.inhibitPickerUpdates = false
b.mu.Unlock()

// Addresses and sub-balancer config are sent to sub-balancer.
return b.child.UpdateClientConnState(balancer.ClientConnState{
Expand Down Expand Up @@ -318,8 +320,9 @@ func (b *clusterImplBalancer) UpdateSubConnState(sc balancer.SubConn, s balancer
func (b *clusterImplBalancer) Close() {
b.serializer.TrySchedule(func(_ context.Context) {
b.child.Close()
b.mu.Lock()
b.childState = balancer.State{}

b.mu.Unlock()
if b.cancelLoadReport != nil {
b.cancelLoadReport()
b.cancelLoadReport = nil
Expand All @@ -339,18 +342,20 @@ func (b *clusterImplBalancer) ExitIdle() {
// Override methods to accept updates from the child LB.

func (b *clusterImplBalancer) UpdateState(state balancer.State) {
b.serializer.TrySchedule(func(context.Context) {
b.childState = state
if !b.inhibitPickerUpdates {
b.ClientConn.UpdateState(balancer.State{
ConnectivityState: b.childState.ConnectivityState,
Picker: b.newPicker(&dropConfigs{
drops: b.drops,
requestCounter: b.requestCounter,
requestCountMax: b.requestCountMax,
}),
})
}
b.mu.Lock()
b.childState = state
if b.inhibitPickerUpdates {
b.mu.Unlock()
return

Check warning on line 349 in xds/internal/balancer/clusterimpl/clusterimpl.go

View check run for this annotation

Codecov / codecov/patch

xds/internal/balancer/clusterimpl/clusterimpl.go#L348-L349

Added lines #L348 - L349 were not covered by tests
}
b.mu.Unlock()
b.ClientConn.UpdateState(balancer.State{
ConnectivityState: state.ConnectivityState,
Picker: b.newPicker(&dropConfigs{
drops: b.drops,
requestCounter: b.requestCounter,
requestCountMax: b.requestCountMax,
}),
})
}

Expand Down

0 comments on commit f687883

Please sign in to comment.