From 8ed0d4759bb8d39d8d487856b5b8fb8350498e01 Mon Sep 17 00:00:00 2001 From: tangenta Date: Mon, 18 Dec 2023 19:03:22 +0800 Subject: [PATCH] ddl: disable fast reorg and dist task execution for system tables (#49542) close pingcap/tidb#49506 --- pkg/ddl/ddl_api.go | 18 ++++++++++++------ pkg/ddl/multi_schema_change.go | 2 +- pkg/session/BUILD.bazel | 2 ++ pkg/session/bootstrap_test.go | 10 ++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/pkg/ddl/ddl_api.go b/pkg/ddl/ddl_api.go index cdb91ea8ec6e7..0d513fd2c9d6d 100644 --- a/pkg/ddl/ddl_api.go +++ b/pkg/ddl/ddl_api.go @@ -7092,7 +7092,7 @@ func (d *ddl) CreatePrimaryKey(ctx sessionctx.Context, ti ast.Ident, indexName m Args: []interface{}{unique, indexName, indexPartSpecifications, indexOption, sqlMode, nil, global}, Priority: ctx.GetSessionVars().DDLReorgPriority, } - reorgMeta, err := newReorgMetaFromVariables(d, job, ctx) + reorgMeta, err := newReorgMetaFromVariables(job, ctx) if err != nil { return err } @@ -7349,7 +7349,7 @@ func (d *ddl) createIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast.Inde Charset: chs, Collate: coll, } - reorgMeta, err := newReorgMetaFromVariables(d, job, ctx) + reorgMeta, err := newReorgMetaFromVariables(job, ctx) if err != nil { return err } @@ -7365,24 +7365,30 @@ func (d *ddl) createIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast.Inde return errors.Trace(err) } -func newReorgMetaFromVariables(d *ddl, job *model.Job, sctx sessionctx.Context) (*model.DDLReorgMeta, error) { +func newReorgMetaFromVariables(job *model.Job, sctx sessionctx.Context) (*model.DDLReorgMeta, error) { reorgMeta := NewDDLReorgMeta(sctx) reorgMeta.IsDistReorg = variable.EnableDistTask.Load() reorgMeta.IsFastReorg = variable.EnableFastReorg.Load() if reorgMeta.IsDistReorg && !reorgMeta.IsFastReorg { return nil, dbterror.ErrUnsupportedDistTask } - isUpgradingSysDB := d.stateSyncer.IsUpgradingState() && hasSysDB(job) - if isUpgradingSysDB { + if hasSysDB(job) { if reorgMeta.IsDistReorg { - logutil.BgLogger().Info("cannot use distributed task execution because the job on system DB is in upgrade state", + logutil.BgLogger().Info("cannot use distributed task execution on system DB", zap.String("category", "ddl"), zap.Stringer("job", job)) } reorgMeta.IsDistReorg = false + reorgMeta.IsFastReorg = false + failpoint.Inject("reorgMetaRecordFastReorgDisabled", func(_ failpoint.Value) { + LastReorgMetaFastReorgDisabled = true + }) } return reorgMeta, nil } +// LastReorgMetaFastReorgDisabled is used for test. +var LastReorgMetaFastReorgDisabled bool + func buildFKInfo(fkName model.CIStr, keys []*ast.IndexPartSpecification, refer *ast.ReferenceDef, cols []*table.Column) (*model.FKInfo, error) { if len(keys) != len(refer.IndexPartSpecifications) { return nil, infoschema.ErrForeignKeyNotMatch.GenWithStackByArgs(fkName, "Key reference and table reference don't match") diff --git a/pkg/ddl/multi_schema_change.go b/pkg/ddl/multi_schema_change.go index 90cc38721e971..9c2e0162f73e3 100644 --- a/pkg/ddl/multi_schema_change.go +++ b/pkg/ddl/multi_schema_change.go @@ -47,7 +47,7 @@ func (d *ddl) MultiSchemaChange(ctx sessionctx.Context, ti ast.Ident) error { ReorgMeta: nil, } if containsDistTaskSubJob(subJobs) { - job.ReorgMeta, err = newReorgMetaFromVariables(d, job, ctx) + job.ReorgMeta, err = newReorgMetaFromVariables(job, ctx) if err != nil { return err } diff --git a/pkg/session/BUILD.bazel b/pkg/session/BUILD.bazel index b4c484de4123f..c3a961b05d4ff 100644 --- a/pkg/session/BUILD.bazel +++ b/pkg/session/BUILD.bazel @@ -136,6 +136,7 @@ go_test( "//pkg/autoid_service", "//pkg/bindinfo", "//pkg/config", + "//pkg/ddl", "//pkg/domain", "//pkg/executor", "//pkg/expression", @@ -157,6 +158,7 @@ go_test( "//pkg/util/chunk", "//pkg/util/logutil", "//pkg/util/sqlexec", + "@com_github_pingcap_failpoint//:failpoint", "@com_github_pingcap_log//:log", "@com_github_stretchr_testify//require", "@com_github_tikv_client_go_v2//tikv", diff --git a/pkg/session/bootstrap_test.go b/pkg/session/bootstrap_test.go index 3c5b82d7b4e25..2edafb93b0b76 100644 --- a/pkg/session/bootstrap_test.go +++ b/pkg/session/bootstrap_test.go @@ -21,7 +21,9 @@ import ( "testing" "time" + "github.com/pingcap/failpoint" "github.com/pingcap/tidb/pkg/bindinfo" + "github.com/pingcap/tidb/pkg/ddl" "github.com/pingcap/tidb/pkg/domain" "github.com/pingcap/tidb/pkg/meta" "github.com/pingcap/tidb/pkg/parser/auth" @@ -1585,10 +1587,18 @@ func TestTiDBUpgradeToVer136(t *testing.T) { require.NoError(t, err) require.Equal(t, int64(ver135), ver) + MustExec(t, seV135, "ALTER TABLE mysql.tidb_background_subtask DROP INDEX idx_task_key;") + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/ddl/reorgMetaRecordFastReorgDisabled", `return`)) + t.Cleanup(func() { + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/ddl/reorgMetaRecordFastReorgDisabled")) + }) + MustExec(t, seV135, "set global tidb_ddl_enable_fast_reorg = 1") dom, err := BootstrapSession(store) require.NoError(t, err) ver, err = getBootstrapVersion(seV135) require.NoError(t, err) + require.True(t, ddl.LastReorgMetaFastReorgDisabled) + require.Less(t, int64(ver135), ver) dom.Close() }