Skip to content

Commit

Permalink
store/tikv: fix a concurrency bug that may cause the batchClient time…
Browse files Browse the repository at this point in the history
…out (#22239) (#22336)

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ti-srebot authored Jan 11, 2021
1 parent aaa4262 commit 1edefab
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions store/tikv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ type rpcClient struct {
security config.Security

idleNotify uint32
// recycleMu protect the conns from being modified during a connArray is taken out and used.
// That means recycleIdleConnArray() will wait until nobody doing sendBatchRequest()
recycleMu sync.RWMutex
// Periodically check whether there is any connection that is idle and then close and remove these connections.
// Implement background cleanup.
isClosed bool
Expand Down Expand Up @@ -336,11 +339,15 @@ func (c *rpcClient) SendRequest(ctx context.Context, addr string, req *tikvrpc.R
}()

if atomic.CompareAndSwapUint32(&c.idleNotify, 1, 0) {
c.recycleMu.Lock()
c.recycleIdleConnArray()
c.recycleMu.Unlock()
}

// TiDB will not send batch commands to TiFlash, to resolve the conflict with Batch Cop Request.
enableBatch := req.StoreTp != kv.TiDB && req.StoreTp != kv.TiFlash
c.recycleMu.RLock()
defer c.recycleMu.RUnlock()
connArray, err := c.getConnArray(addr, enableBatch)
if err != nil {
return nil, errors.Trace(err)
Expand Down

0 comments on commit 1edefab

Please sign in to comment.