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

SendBatch: Launch one fewer goroutine #230

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
44 changes: 26 additions & 18 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"math"
"strconv"
"sync"
"sync/atomic"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -243,28 +244,35 @@ func (c *client) SendBatch(ctx context.Context, batch []hrpc.Call) (

// Send each group of RPCs to region client to be executed.
var (
wg sync.WaitGroup

mu sync.Mutex
fail bool
wg sync.WaitGroup
fail uint32 // set atomically to 1 if one of the clients fail
)
wg.Add(len(rpcByClient))
clientCount := len(rpcByClient)
wg.Add(clientCount)

sendBlocking := func(client hrpc.RegionClient, rpcs []hrpc.Call) {
defer wg.Done()
client.QueueBatch(ctx, rpcs)
ctx, sp := observability.StartSpan(ctx, "waitForResult")
defer sp.End()
ok := c.waitForCompletion(ctx, client, rpcs, res, rpcToRes)
if !ok {
atomic.StoreUint32(&fail, 1)
}
}

i := 0
for client, rpcs := range rpcByClient {
go func(client hrpc.RegionClient, rpcs []hrpc.Call) {
defer wg.Done()
client.QueueBatch(ctx, rpcs)
ctx, sp := observability.StartSpan(ctx, "waitForResult")
defer sp.End()
ok := c.waitForCompletion(ctx, client, rpcs, res, rpcToRes)
if !ok {
mu.Lock()
fail = true
mu.Unlock()
}
}(client, rpcs)
if i++; i < clientCount {
go sendBlocking(client, rpcs)
} else {
// Small optimization: don't launch a goroutine for the
// last client
sendBlocking(client, rpcs)
}
}
wg.Wait()
allOK = !fail
aaronbee marked this conversation as resolved.
Show resolved Hide resolved
allOK = fail == 0

return res, allOK
}
Expand Down