-
Notifications
You must be signed in to change notification settings - Fork 6.1k
executor: fix analyze cannot be killed #65249
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
base: master
Are you sure you want to change the base?
Changes from all commits
f871275
4812821
64f3305
64e72d8
d686344
30fd1df
2dc0b32
011aa31
4ccfa54
90b5fa5
224081b
6f83757
643191f
e9c35aa
da96e11
60610cf
ee89249
4cb00b4
ff0aec8
cae7fa4
e4f2a12
a7247c6
f250aad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -46,6 +46,7 @@ import ( | |||||||
| handleutil "github.com/pingcap/tidb/pkg/statistics/handle/util" | ||||||||
| "github.com/pingcap/tidb/pkg/util" | ||||||||
| "github.com/pingcap/tidb/pkg/util/chunk" | ||||||||
| "github.com/pingcap/tidb/pkg/util/dbterror/exeerrors" | ||||||||
| "github.com/pingcap/tidb/pkg/util/intest" | ||||||||
| "github.com/pingcap/tidb/pkg/util/sqlescape" | ||||||||
| "github.com/pingcap/tipb/go-tipb" | ||||||||
|
|
@@ -101,6 +102,8 @@ func (e *AnalyzeExec) Next(ctx context.Context, _ *chunk.Chunk) (err error) { | |||||||
| statsHandle := domain.GetDomain(e.Ctx()).StatsHandle() | ||||||||
| infoSchema := sessiontxn.GetTxnManager(e.Ctx()).GetTxnInfoSchema() | ||||||||
| sessionVars := e.Ctx().GetSessionVars() | ||||||||
| ctx, stop := e.buildAnalyzeKillCtx(ctx) | ||||||||
| defer stop() | ||||||||
|
|
||||||||
| // Filter the locked tables. | ||||||||
| tasks, needAnalyzeTableCnt, skippedTables, err := filterAndCollectTasks(e.tasks, statsHandle, infoSchema) | ||||||||
|
|
@@ -132,7 +135,7 @@ func (e *AnalyzeExec) Next(ctx context.Context, _ *chunk.Chunk) (err error) { | |||||||
| taskCh := make(chan *analyzeTask, buildStatsConcurrency) | ||||||||
| resultsCh := make(chan *statistics.AnalyzeResults, 1) | ||||||||
| for range buildStatsConcurrency { | ||||||||
| e.wg.Run(func() { e.analyzeWorker(taskCh, resultsCh) }) | ||||||||
| e.wg.Run(func() { e.analyzeWorker(ctx, taskCh, resultsCh) }) | ||||||||
| } | ||||||||
| pruneMode := variable.PartitionPruneMode(sessionVars.PartitionPruneMode.Load()) | ||||||||
| // needGlobalStats used to indicate whether we should merge the partition-level stats to global-level stats. | ||||||||
|
|
@@ -152,10 +155,12 @@ func (e *AnalyzeExec) Next(ctx context.Context, _ *chunk.Chunk) (err error) { | |||||||
| dom.SysProcTracker().KillSysProcess(id) | ||||||||
| } | ||||||||
| }) | ||||||||
| sentTasks := 0 | ||||||||
| TASKLOOP: | ||||||||
| for _, task := range tasks { | ||||||||
| select { | ||||||||
| case taskCh <- task: | ||||||||
| sentTasks++ | ||||||||
| case <-e.errExitCh: | ||||||||
| break TASKLOOP | ||||||||
| case <-gctx.Done(): | ||||||||
|
|
@@ -173,6 +178,17 @@ TASKLOOP: | |||||||
|
|
||||||||
| err = e.waitFinish(ctx, g, resultsCh) | ||||||||
| if err != nil { | ||||||||
| if stderrors.Is(err, context.Canceled) { | ||||||||
| if cause := context.Cause(ctx); cause != nil { | ||||||||
| err = cause | ||||||||
| } | ||||||||
| } | ||||||||
hawkingrei marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| for task := range taskCh { | ||||||||
| finishJobWithLog(statsHandle, task.job, err) | ||||||||
| } | ||||||||
| for i := sentTasks; i < len(tasks); i++ { | ||||||||
| finishJobWithLog(statsHandle, tasks[i].job, err) | ||||||||
| } | ||||||||
| return err | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -469,6 +485,28 @@ func (e *AnalyzeExec) handleResultsErrorWithConcurrency( | |||||||
| break | ||||||||
| } | ||||||||
| if results.Err != nil { | ||||||||
| if intest.InTest && stderrors.Is(results.Err, context.Canceled) { | ||||||||
| jobInfo := "" | ||||||||
| dbName := "" | ||||||||
| tableName := "" | ||||||||
| partitionName := "" | ||||||||
| if results.Job != nil { | ||||||||
| jobInfo = results.Job.JobInfo | ||||||||
| dbName = results.Job.DBName | ||||||||
| tableName = results.Job.TableName | ||||||||
| partitionName = results.Job.PartitionName | ||||||||
| } | ||||||||
| statslogutil.StatsLogger().Info("analyze result canceled", | ||||||||
| zap.Uint32("killSignal", e.Ctx().GetSessionVars().SQLKiller.GetKillSignal()), | ||||||||
| zap.Uint64("connID", e.Ctx().GetSessionVars().ConnectionID), | ||||||||
| zap.String("jobInfo", jobInfo), | ||||||||
| zap.String("dbName", dbName), | ||||||||
| zap.String("tableName", tableName), | ||||||||
| zap.String("partitionName", partitionName), | ||||||||
| zap.Error(results.Err), | ||||||||
| zap.Stack("stack"), | ||||||||
| ) | ||||||||
| } | ||||||||
| err = results.Err | ||||||||
| if isAnalyzeWorkerPanic(err) { | ||||||||
| panicCnt++ | ||||||||
|
|
@@ -503,7 +541,77 @@ func (e *AnalyzeExec) handleResultsErrorWithConcurrency( | |||||||
| return err | ||||||||
| } | ||||||||
|
|
||||||||
| func (e *AnalyzeExec) analyzeWorker(taskCh <-chan *analyzeTask, resultsCh chan<- *statistics.AnalyzeResults) { | ||||||||
| func (e *AnalyzeExec) buildAnalyzeKillCtx(parent context.Context) (context.Context, func()) { | ||||||||
| ctx, cancel := context.WithCancelCause(parent) | ||||||||
| killer := &e.Ctx().GetSessionVars().SQLKiller | ||||||||
| killCh := killer.GetKillEventChan() | ||||||||
| stopCh := make(chan struct{}) | ||||||||
| go func() { | ||||||||
| for { | ||||||||
| select { | ||||||||
| case <-ctx.Done(): | ||||||||
| return | ||||||||
| case <-stopCh: | ||||||||
| return | ||||||||
| case <-killCh: | ||||||||
| status := killer.GetKillSignal() | ||||||||
| if status == 0 { | ||||||||
| return | ||||||||
| } | ||||||||
| err := killer.HandleSignal() | ||||||||
| if err == nil { | ||||||||
| err = exeerrors.ErrQueryInterrupted | ||||||||
| } | ||||||||
| cancel(err) | ||||||||
| return | ||||||||
| } | ||||||||
| } | ||||||||
| }() | ||||||||
| return ctx, func() { | ||||||||
|
||||||||
| return ctx, func() { | |
| return ctx, func() { | |
| cancel(context.Canceled) |
Uh oh!
There was an error while loading. Please reload this page.