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

internal/client: remove the lock for idle connection recycle #278

Merged
merged 4 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,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 @@ -297,6 +295,13 @@ 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() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I confirmed return error will not make TiDB unavailable.
This error will be recognized as a RPC error, and TiDB will refresh the region cache ... and retry the request.

Copy link
Member

Choose a reason for hiding this comment

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

I confirmed return error will not make TiDB unavailable.
This error will be recognized as a RPC error, and TiDB will refresh the region cache ... and retry the request.

This behavior is a magic, it's hard to figure out that tidb will retry the request while reading the source code. Could you add some notes here and file an issue? Also, we should do refactor to make sure we know when should return a "retry" error.

return nil, errors.Errorf("rpcClient is idle")
}

return array, nil
}

Expand Down Expand Up @@ -363,22 +368,18 @@ func (c *RPCClient) SendRequest(ctx context.Context, addr string, req *tikvrpc.R
ctx = opentracing.ContextWithSpan(ctx, span1)
}

start := time.Now()
if atomic.CompareAndSwapUint32(&c.idleNotify, 1, 0) {
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)
}
metrics.TiKVBatchClientRecycle.Observe(time.Since(start).Seconds())

start = time.Now()
start := time.Now()
defer func() {
stmtExec := ctx.Value(util.ExecDetailsKey)
if stmtExec != nil {
Expand Down
6 changes: 4 additions & 2 deletions internal/client/client_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,8 +795,7 @@ func sendBatchRequest(
}

func (c *RPCClient) recycleIdleConnArray() {
c.recycleMu.Lock()
defer c.recycleMu.Unlock()
start := time.Now()

var addrs []string
c.RLock()
Expand All @@ -816,8 +815,11 @@ func (c *RPCClient) recycleIdleConnArray() {
zap.String("target", addr))
}
c.Unlock()

if conn != nil {
conn.Close()
}
}

metrics.TiKVBatchClientRecycle.Observe(time.Since(start).Seconds())
}