Skip to content

Commit c88a003

Browse files
committed
outlierdetection: fix unconditional calls of child UpdateSubConnState
1 parent 01cbdd3 commit c88a003

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

xds/internal/balancer/outlierdetection/balancer.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ func (bb) Name() string {
142142
type scUpdate struct {
143143
scw *subConnWrapper
144144
state balancer.SubConnState
145-
cb func(balancer.SubConnState)
146145
}
147146

148147
type ejectionUpdate struct {
@@ -346,7 +345,7 @@ func (b *outlierDetectionBalancer) ResolverError(err error) {
346345
b.child.ResolverError(err)
347346
}
348347

349-
func (b *outlierDetectionBalancer) updateSubConnState(sc balancer.SubConn, state balancer.SubConnState, cb func(balancer.SubConnState)) {
348+
func (b *outlierDetectionBalancer) updateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
350349
b.mu.Lock()
351350
defer b.mu.Unlock()
352351
scw, ok := b.scWrappers[sc]
@@ -362,12 +361,11 @@ func (b *outlierDetectionBalancer) updateSubConnState(sc balancer.SubConn, state
362361
b.scUpdateCh.Put(&scUpdate{
363362
scw: scw,
364363
state: state,
365-
cb: cb,
366364
})
367365
}
368366

369367
func (b *outlierDetectionBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
370-
b.updateSubConnState(sc, state, nil)
368+
b.logger.Errorf("UpdateSubConnState(%v, %+v) called unexpectedly", sc, state)
371369
}
372370

373371
func (b *outlierDetectionBalancer) Close() {
@@ -474,7 +472,7 @@ func (b *outlierDetectionBalancer) UpdateState(s balancer.State) {
474472
func (b *outlierDetectionBalancer) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
475473
var sc balancer.SubConn
476474
oldListener := opts.StateListener
477-
opts.StateListener = func(state balancer.SubConnState) { b.updateSubConnState(sc, state, oldListener) }
475+
opts.StateListener = func(state balancer.SubConnState) { b.updateSubConnState(sc, state) }
478476
sc, err := b.cc.NewSubConn(addrs, opts)
479477
if err != nil {
480478
return nil, err
@@ -483,6 +481,7 @@ func (b *outlierDetectionBalancer) NewSubConn(addrs []resolver.Address, opts bal
483481
SubConn: sc,
484482
addresses: addrs,
485483
scUpdateCh: b.scUpdateCh,
484+
listener: oldListener,
486485
}
487486
b.mu.Lock()
488487
defer b.mu.Unlock()
@@ -617,8 +616,8 @@ func (b *outlierDetectionBalancer) handleSubConnUpdate(u *scUpdate) {
617616
scw.latestState = u.state
618617
if !scw.ejected {
619618
b.childMu.Lock()
620-
if u.cb != nil {
621-
u.cb(u.state)
619+
if scw.listener != nil {
620+
scw.listener(u.state)
622621
} else {
623622
b.child.UpdateSubConnState(scw, u.state)
624623
}
@@ -640,7 +639,11 @@ func (b *outlierDetectionBalancer) handleEjectedUpdate(u *ejectionUpdate) {
640639
}
641640
}
642641
b.childMu.Lock()
643-
b.child.UpdateSubConnState(scw, stateToUpdate)
642+
if scw.listener != nil {
643+
scw.listener(stateToUpdate)
644+
} else {
645+
b.child.UpdateSubConnState(scw, stateToUpdate)
646+
}
644647
b.childMu.Unlock()
645648
}
646649

xds/internal/balancer/outlierdetection/balancer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ func (s) TestEjectUnejectSuccessRate(t *testing.T) {
10851085

10861086
// Since no addresses are ejected, a SubConn update should forward down
10871087
// to the child.
1088-
od.UpdateSubConnState(scw1.(*subConnWrapper).SubConn, balancer.SubConnState{
1088+
od.updateSubConnState(scw1.(*subConnWrapper).SubConn, balancer.SubConnState{
10891089
ConnectivityState: connectivity.Connecting,
10901090
})
10911091

@@ -1147,7 +1147,7 @@ func (s) TestEjectUnejectSuccessRate(t *testing.T) {
11471147
// that address should not be forwarded downward. These SubConn updates
11481148
// will be cached to update the child sometime in the future when the
11491149
// address gets unejected.
1150-
od.UpdateSubConnState(pi.SubConn, balancer.SubConnState{
1150+
od.updateSubConnState(pi.SubConn, balancer.SubConnState{
11511151
ConnectivityState: connectivity.Connecting,
11521152
})
11531153
sCtx, cancel = context.WithTimeout(context.Background(), defaultTestShortTimeout)
@@ -1564,7 +1564,7 @@ func (s) TestConcurrentOperations(t *testing.T) {
15641564

15651565
// Call balancer.Balancers synchronously in this goroutine, upholding the
15661566
// balancer.Balancer API guarantee.
1567-
od.UpdateSubConnState(scw1.(*subConnWrapper).SubConn, balancer.SubConnState{
1567+
od.updateSubConnState(scw1.(*subConnWrapper).SubConn, balancer.SubConnState{
15681568
ConnectivityState: connectivity.Connecting,
15691569
})
15701570
od.ResolverError(errors.New("some error"))

xds/internal/balancer/outlierdetection/subconn_wrapper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
// whether or not this SubConn is ejected.
3232
type subConnWrapper struct {
3333
balancer.SubConn
34+
listener func(balancer.SubConnState)
3435

3536
// addressInfo is a pointer to the subConnWrapper's corresponding address
3637
// map entry, if the map entry exists.

0 commit comments

Comments
 (0)