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

planner: skip MDL when analyzing table (#50928) | tidb-test=pr/2323 #53050

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
2 changes: 1 addition & 1 deletion ddl/metadatalocktest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go_test(
"mdl_test.go",
],
flaky = True,
shard_count = 32,
shard_count = 34,
deps = [
"//config",
"//ddl",
Expand Down
84 changes: 84 additions & 0 deletions ddl/metadatalocktest/mdl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,90 @@ func TestMDLAutoCommitReadOnly(t *testing.T) {
require.Greater(t, ts1, ts2)
}

func TestMDLAnalyze(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
sv := server.CreateMockServer(t, store)

sv.SetDomain(dom)
dom.InfoSyncer().SetSessionManager(sv)
defer sv.Close()

conn1 := server.CreateMockConn(t, sv)
tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session)
conn2 := server.CreateMockConn(t, sv)
tkDDL := testkit.NewTestKitWithSession(t, store, conn2.Context().Session)
tk.MustExec("use test")
tk.MustExec("set global tidb_enable_metadata_lock=1")
tk.MustExec("create table t(a int);")
tk.MustExec("insert into t values(1);")

var wg sync.WaitGroup
wg.Add(2)
var ts2 time.Time
var ts1 time.Time

go func() {
tk.MustExec("begin")
tk.MustExec("analyze table t;")
tk.MustQuery("select sleep(2);")
tk.MustExec("commit")
ts1 = time.Now()
wg.Done()
}()

go func() {
tkDDL.MustExec("alter table test.t add column b int;")
ts2 = time.Now()
wg.Done()
}()

wg.Wait()
require.Greater(t, ts1, ts2)
}

func TestMDLAnalyzePartition(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
sv := server.CreateMockServer(t, store)

sv.SetDomain(dom)
dom.InfoSyncer().SetSessionManager(sv)
defer sv.Close()

conn1 := server.CreateMockConn(t, sv)
tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session)
conn2 := server.CreateMockConn(t, sv)
tkDDL := testkit.NewTestKitWithSession(t, store, conn2.Context().Session)
tk.MustExec("use test")
tk.MustExec("set @@tidb_partition_prune_mode='dynamic'")
tk.MustExec("set global tidb_enable_metadata_lock=1")
tk.MustExec("create table t(a int) partition by range(a) ( PARTITION p0 VALUES LESS THAN (0), PARTITION p1 VALUES LESS THAN (100), PARTITION p2 VALUES LESS THAN MAXVALUE );")
tk.MustExec("insert into t values(1), (2), (3), (4);")

var wg sync.WaitGroup
wg.Add(2)
var ts2 time.Time
var ts1 time.Time

go func() {
tk.MustExec("begin")
tk.MustExec("analyze table t;")
tk.MustExec("analyze table t partition p1;")
tk.MustQuery("select sleep(2);")
tk.MustExec("commit")
ts1 = time.Now()
wg.Done()
}()

go func() {
tkDDL.MustExec("alter table test.t drop partition p2;")
ts2 = time.Now()
wg.Done()
}()

wg.Wait()
require.Greater(t, ts1, ts2)
}

func TestMDLAutoCommitNonReadOnly(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
sv := server.CreateMockServer(t, store)
Expand Down
23 changes: 18 additions & 5 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
return errors.Trace(v.err)
}

type preprocessorFlag uint8
type preprocessorFlag uint64

const (
// inPrepare is set when visiting in prepare statement.
Expand All @@ -157,6 +157,8 @@
inSequenceFunction
// initTxnContextProvider is set when we should init txn context in preprocess
initTxnContextProvider
// inAnalyze is set when visiting an analyze statement.
inAnalyze
)

// Make linter happy.
Expand Down Expand Up @@ -390,6 +392,8 @@
p.sctx.GetSessionVars().StmtCtx.IsStaleness = true
p.IsStaleness = true
}
case *ast.AnalyzeTableStmt:
p.flag |= inAnalyze
default:
p.flag &= ^parentIsJoin
}
Expand Down Expand Up @@ -1573,10 +1577,12 @@
if tn.Schema.String() != "" {
currentDB = tn.Schema.L
}
table, err = tryLockMDLAndUpdateSchemaIfNecessary(p.sctx, model.NewCIStr(currentDB), table, p.ensureInfoSchema())
if err != nil {
p.err = err
return
if !p.skipLockMDL() {
table, err = tryLockMDLAndUpdateSchemaIfNecessary(p.sctx, model.NewCIStr(currentDB), table, p.ensureInfoSchema())
if err != nil {
p.err = err
return
}

Check warning on line 1585 in planner/core/preprocess.go

View check run for this annotation

Codecov / codecov/patch

planner/core/preprocess.go#L1583-L1585

Added lines #L1583 - L1585 were not covered by tests
}

tableInfo := table.Meta()
Expand Down Expand Up @@ -1905,3 +1911,10 @@
}
return tbl, nil
}

// skipLockMDL returns true if the preprocessor should skip the lock of MDL.
func (p *preprocessor) skipLockMDL() bool {
// because it's a batch process and will do both DML and DDL.
// skip lock mdl for ANALYZE statement.
return p.flag&inAnalyze > 0
}
Loading