Skip to content

Commit

Permalink
store/tikv: fix a memory leak in the batchClient for the large transa…
Browse files Browse the repository at this point in the history
…ctions (#14031) (#14032)
  • Loading branch information
sre-bot authored Dec 12, 2019
1 parent 3a10bb8 commit b10d1a5
Showing 1 changed file with 22 additions and 2 deletions.
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

0 comments on commit b10d1a5

Please sign in to comment.