Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

balancer/leastrequest: Cache atomic load and also add concurrent rpc test #6602

Merged
merged 3 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions balancer/leastrequest/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -455,3 +456,55 @@ func (s) TestLeastRequestPersistsCounts(t *testing.T) {
t.Fatalf("addr count (-got:, +want): %v", diff)
}
}

// TestConcurrentRPCs tests concurrent RPCs on the least request balancer. It
// configures a channel with a least request balancer as the top level balancer,
// and makes 100 RPCs asynchronously. This makes sure no race conditions happen
// in this scenario.
func (s) TestConcurrentRPCs(t *testing.T) {
addresses := setupBackends(t)

mr := manual.NewBuilderWithScheme("lr-e2e")
defer mr.Close()

// Configure least request as top level balancer of channel.
lrscJSON := `
{
"loadBalancingConfig": [
{
"least_request_experimental": {
"choiceCount": 2
}
}
]
}`
sc := internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(lrscJSON)
firstTwoAddresses := []resolver.Address{
{Addr: addresses[0]},
{Addr: addresses[1]},
}
mr.InitialState(resolver.State{
Addresses: firstTwoAddresses,
ServiceConfig: sc,
})

cc, err := grpc.Dial(mr.Scheme()+":///", grpc.WithResolvers(mr), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
testServiceClient := testgrpc.NewTestServiceClient(cc)

var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
testServiceClient.EmptyCall(ctx, &testpb.Empty{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these do several calls each, maybe, to make concurrent pick calls more likely? Just make sure it doesn't run too long.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Made each goroutine perform 5 calls.

}()
}
wg.Wait()

}
5 changes: 4 additions & 1 deletion balancer/leastrequest/leastrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,18 @@ type picker struct {

func (p *picker) Pick(balancer.PickInfo) (balancer.PickResult, error) {
var pickedSC *scWithRPCCount
var pickedSCNumRPCs int32
for i := 0; i < int(p.choiceCount); i++ {
index := grpcranduint32() % uint32(len(p.subConns))
sc := p.subConns[index]
if pickedSC == nil {
pickedSC = &sc
pickedSCNumRPCs = pickedSC.numRPCs.Load()
continue
}
if sc.numRPCs.Load() < pickedSC.numRPCs.Load() {
if sc.numRPCs.Load() < pickedSCNumRPCs {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should sc.numRPCS.Load() be cached too? to save one more Load

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good point. Switched.

pickedSC = &sc
pickedSCNumRPCs = pickedSC.numRPCs.Load()
}
}
// "The counter for a subchannel should be atomically incremented by one
Expand Down