Skip to content

Commit

Permalink
store/tikv/client: remove the lock for idle connection recycle (#27678)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Sep 16, 2021
1 parent 0241716 commit 2a6f643
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions store/tikv/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ 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 @@ -272,6 +270,12 @@ func (c *RPCClient) getConnArray(addr string, enableBatch bool, opt ...func(cfg
return nil, err
}
}

// An idle connArray will not change to active again, this avoid the race condition
// that recycling idle connection close an active connection unexpectedly (idle -> active).
if array.batchConn != nil && array.isIdle() {
return nil, errors.Errorf("rpcClient is idle")
}
return array, nil
}

Expand Down Expand Up @@ -349,15 +353,11 @@ 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()
go c.recycleIdleConnArray()
}

// TiDB will not send batch commands to TiFlash, to resolve the conflict with Batch Cop Request.
enableBatch := req.StoreTp != tikvrpc.TiDB && req.StoreTp != tikvrpc.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 2a6f643

Please sign in to comment.