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

exeuctor: fix coprocessor goroutine leak for IndexMerge #41610

Merged
merged 10 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
26 changes: 26 additions & 0 deletions executor/index_merge_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ func (e *IndexMergeReaderExecutor) startPartialIndexWorker(ctx context.Context,
SetFromInfoSchema(e.ctx.GetInfoSchema()).
SetClosestReplicaReadAdjuster(newClosestReadAdjuster(e.ctx, &builder.Request, e.partialNetDataSizes[workID]))

selectResults := make([]distsql.SelectResult, 0, len(keyRanges))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why use a slice? only one element?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

defer func() {
// To make sure SelectResult.Close() is called even got panic in fetchHandles().
for _, s := range selectResults {
terror.Call(s.Close)
}
}()
for parTblIdx, keyRange := range keyRanges {
// check if this executor is closed
select {
Expand All @@ -384,6 +391,11 @@ func (e *IndexMergeReaderExecutor) startPartialIndexWorker(ctx context.Context,
syncErr(ctx, e.finished, fetchCh, err)
return
}
selectResults = append(selectResults, result)
failpoint.Inject("testIndexMergePartialIndexWorkerCoprLeak", func(v failpoint.Value) {
time.Sleep(time.Duration(v.(int)))
panic("testIndexMergePartialIndexWorkerCoprLeak")
})
worker.batchSize = e.maxChunkSize
if worker.batchSize > worker.maxBatchSize {
worker.batchSize = worker.maxBatchSize
Expand All @@ -401,6 +413,7 @@ func (e *IndexMergeReaderExecutor) startPartialIndexWorker(ctx context.Context,
if err := result.Close(); err != nil {
logutil.Logger(ctx).Error("close Select result failed:", zap.Error(err))
}
selectResults = selectResults[:len(selectResults)-1]
cancel()
e.ctx.StoreQueryFeedback(e.feedbacks[workID])
if fetchErr != nil {
Expand Down Expand Up @@ -475,6 +488,13 @@ func (e *IndexMergeReaderExecutor) startPartialTableWorker(ctx context.Context,
partialTableReader.dagPB = e.dagPBs[workID]
}

var tableReaderClosed bool
defer func() {
// To make sure SelectResult.Close() is called even got panic in fetchHandles().
if !tableReaderClosed {
terror.Call(worker.tableReader.Close)
}
}()
for parTblIdx, tbl := range tbls {
// check if this executor is closed
select {
Expand All @@ -490,6 +510,11 @@ func (e *IndexMergeReaderExecutor) startPartialTableWorker(ctx context.Context,
syncErr(ctx, e.finished, fetchCh, err)
break
}
failpoint.Inject("testIndexMergePartialTableWorkerCoprLeak", func(v failpoint.Value) {
time.Sleep(time.Duration(v.(int)))
panic("testIndexMergePartialTableWorkerCoprLeak")
})
tableReaderClosed = false
worker.batchSize = e.maxChunkSize
if worker.batchSize > worker.maxBatchSize {
worker.batchSize = worker.maxBatchSize
Expand All @@ -510,6 +535,7 @@ func (e *IndexMergeReaderExecutor) startPartialTableWorker(ctx context.Context,
if err = worker.tableReader.Close(); err != nil {
logutil.Logger(ctx).Error("close Select result failed:", zap.Error(err))
}
tableReaderClosed = true
Copy link
Contributor

Choose a reason for hiding this comment

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

If worker.tableReader.Close() return a error, it will be called again in defer()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will fix in #41647

e.ctx.StoreQueryFeedback(e.feedbacks[workID])
if fetchErr != nil {
break
Expand Down
32 changes: 32 additions & 0 deletions executor/index_merge_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,35 @@ func TestIndexMergePanic(t *testing.T) {
require.NoError(t, failpoint.Disable(fp))
}
}

func TestIndexMergeCoprGoroutinesLeak(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(c1 int, c2 bigint, c3 bigint, primary key(c1), key(c2), key(c3));")
insertStr := "insert into t1 values(0, 0, 0)"
for i := 1; i < 1000; i++ {
insertStr += fmt.Sprintf(", (%d, %d, %d)", i, i, i)
}
tk.MustExec(insertStr)
tk.MustExec("analyze table t1;")
tk.MustExec("set tidb_partition_prune_mode = 'dynamic'")

var err error
sql := "select /*+ use_index_merge(t1) */ c1 from t1 where c1 < 900 or c2 < 1000;"
res := tk.MustQuery("explain " + sql).Rows()
require.Contains(t, res[1][0], "IndexMerge")

// If got goroutines leak in coprocessor, ci will fail.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testIndexMergePartialTableWorkerCoprLeak", "return(3)"))
err = tk.QueryToErr(sql)
require.Contains(t, err.Error(), "testIndexMergePartialTableWorkerCoprLeak")
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testIndexMergePartialTableWorkerCoprLeak"))

require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testIndexMergePartialIndexWorkerCoprLeak", "return(3)"))
err = tk.QueryToErr(sql)
require.Contains(t, err.Error(), "testIndexMergePartialIndexWorkerCoprLeak")
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testIndexMergePartialIndexWorkerCoprLeak"))
}