Skip to content

Commit

Permalink
balancergroup: add method to exitIdle a sub-balancer (#4994)
Browse files Browse the repository at this point in the history
This is required for the RLS LB policy. At pick time, if the RLS picker
finds one of its child policies in IDLE, it needs to be able to ask it
to exit idle.
  • Loading branch information
easwars committed Nov 29, 2021
1 parent 6f8796b commit 58beff1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/balancergroup/balancergroup.go
Expand Up @@ -506,3 +506,13 @@ func (bg *BalancerGroup) ExitIdle() {
}
bg.outgoingMu.Unlock()
}

// ExitIdleOne instructs the sub-balancer `id` to exit IDLE state, if
// appropriate and possible.
func (bg *BalancerGroup) ExitIdleOne(id string) {
bg.outgoingMu.Lock()
if config := bg.idToBalancerConfig[id]; config != nil {
config.exitIdle()
}
bg.outgoingMu.Unlock()
}
26 changes: 26 additions & 0 deletions internal/balancergroup/balancergroup_test.go
Expand Up @@ -509,3 +509,29 @@ func (s) TestBalancerGroupBuildOptions(t *testing.T) {
t.Fatal(err)
}
}

func (s) TestBalancerExitIdleOne(t *testing.T) {
const balancerName = "stub-balancer-test-balancergroup-exit-idle-one"
exitIdleCh := make(chan struct{}, 1)
stub.Register(balancerName, stub.BalancerFuncs{
ExitIdle: func(*stub.BalancerData) {
exitIdleCh <- struct{}{}
},
})
cc := testutils.NewTestClientConn(t)
bg := New(cc, balancer.BuildOptions{}, nil, nil)
bg.Start()
defer bg.Close()

// Add the stub balancer build above as a child policy.
builder := balancer.Get(balancerName)
bg.Add(testBalancerIDs[0], builder)

// Call ExitIdle on the child policy.
bg.ExitIdleOne(testBalancerIDs[0])
select {
case <-time.After(time.Second):
t.Fatal("Timeout when waiting for ExitIdle to be invoked on child policy")
case <-exitIdleCh:
}
}

0 comments on commit 58beff1

Please sign in to comment.