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 a memory leak in the batchClient for the large transactions (#14031) #14032

Merged
merged 1 commit into from
Dec 12, 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
24 changes: 22 additions & 2 deletions store/tikv/client_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,22 @@ func (b *batchCommandsEntry) isCanceled() bool {

const idleTimeout = 3 * time.Minute

func resetEntries(entries []*batchCommandsEntry) []*batchCommandsEntry {
for i := 0; i < len(entries); i++ {
entries[i] = nil
}
entries = entries[:0]
return entries
}

func resetRequests(requests []*tikvpb.BatchCommandsRequest_Request) []*tikvpb.BatchCommandsRequest_Request {
for i := 0; i < len(requests); i++ {
requests[i] = nil
}
requests = requests[:0]
return requests
}

func (a *batchConn) batchSendLoop(cfg config.TiKVClient) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -416,8 +432,12 @@ func (a *batchConn) batchSendLoop(cfg config.TiKVClient) {

var bestBatchWaitSize = cfg.BatchWaitSize
for {
entries = entries[:0]
requests = requests[:0]
// NOTE: We can't simply set entries = entries[:0] here.
// The data in the cap part of the slice would reference the prewrite keys whose
// underlying memory is borrowed from memdb. The reference cause GC can't release
// the memdb, leading to serious memory leak problems in the large transaction case.
entries = resetEntries(entries)
requests = resetRequests(requests)
requestIDs = requestIDs[:0]

a.pendingRequests.Set(float64(len(a.batchCommandsCh)))
Expand Down