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

store/tikv: fix data race on idle (#12164) (#12824) #12846

Merged
merged 1 commit into from
Oct 21, 2019
Merged
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
16 changes: 12 additions & 4 deletions store/tikv/client_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import (
)

type batchConn struct {
index uint32
// An atomic flag indicates whether the batch is idle or not.
// 0 for busy, others for idle.
idle uint32

// batchCommandsCh used for batch commands.
batchCommandsCh chan *batchCommandsEntry
batchCommandsClients []*batchCommandsClient
Expand All @@ -44,10 +47,11 @@ type batchConn struct {

// Notify rpcClient to check the idle flag
idleNotify *uint32
idle bool
idleDetect *time.Timer

pendingRequests prometheus.Gauge

index uint32
}

func newBatchConn(connCount, maxBatchSize uint, idleNotify *uint32) *batchConn {
Expand All @@ -62,6 +66,10 @@ func newBatchConn(connCount, maxBatchSize uint, idleNotify *uint32) *batchConn {
}
}

func (a *batchConn) isIdle() bool {
return atomic.LoadUint32(&a.idle) != 0
}

// fetchAllPendingRequests fetches all pending requests from the channel.
func (a *batchConn) fetchAllPendingRequests(
maxBatchSize int,
Expand All @@ -78,7 +86,7 @@ func (a *batchConn) fetchAllPendingRequests(
a.idleDetect.Reset(idleTimeout)
case <-a.idleDetect.C:
a.idleDetect.Reset(idleTimeout)
a.idle = true
atomic.AddUint32(&a.idle, 1)
atomic.CompareAndSwapUint32(a.idleNotify, 0, 1)
// This batchConn to be recycled
return
Expand Down Expand Up @@ -540,7 +548,7 @@ func (c *rpcClient) recycleIdleConnArray() {
var addrs []string
c.RLock()
for _, conn := range c.conns {
if conn.idle {
if conn.isIdle() {
addrs = append(addrs, conn.target)
}
}
Expand Down