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

executor: make the ParallelApply be safe to be called again after returning empty results #24935

Merged
merged 2 commits into from
Jun 1, 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
7 changes: 7 additions & 0 deletions executor/parallel_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type ParallelNestedLoopApplyExec struct {
// fields about concurrency control
concurrency int
started uint32
drained uint32 // drained == true indicates there is no more data
freeChkCh chan *chunk.Chunk
resultChkCh chan result
outerRowCh chan outerRow
Expand Down Expand Up @@ -130,6 +131,11 @@ func (e *ParallelNestedLoopApplyExec) Open(ctx context.Context) error {

// Next implements the Executor interface.
func (e *ParallelNestedLoopApplyExec) Next(ctx context.Context, req *chunk.Chunk) (err error) {
if atomic.LoadUint32(&e.drained) == 1 {
req.Reset()
return nil
}

if atomic.CompareAndSwapUint32(&e.started, 0, 1) {
e.workerWg.Add(1)
go e.outerWorker(ctx)
Expand All @@ -147,6 +153,7 @@ func (e *ParallelNestedLoopApplyExec) Next(ctx context.Context, req *chunk.Chunk
}
if result.chk == nil { // no more data
req.Reset()
atomic.StoreUint32(&e.drained, 1)
return nil
}
req.SwapColumns(result.chk)
Expand Down
11 changes: 11 additions & 0 deletions executor/parallel_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,14 @@ func (s *testSuite) TestApplyGoroutinePanic(c *C) {
c.Assert(failpoint.Disable(panicPath), IsNil)
}
}

func (s *testSuite) TestIssue24930(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("set tidb_enable_parallel_apply=true")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int)")
tk.MustExec("create table t2(a int)")
tk.MustQuery(`select case when t1.a is null
then (select t2.a from t2 where t2.a = t1.a limit 1) else t1.a end a
from t1 where t1.a=1 order by a limit 1`).Check(testkit.Rows()) // can return an empty result instead of hanging forever
}