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

ddl: simplify TestMDLPreparePlanCacheExecuteInsert (#58113) #58133

Closed
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
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.4.1
80 changes: 80 additions & 0 deletions pkg/ddl/tests/metadatalock/mdl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,86 @@ func TestMDLPreparePlanCacheExecute2(t *testing.T) {
tk.MustExec("admin check table t")
}

<<<<<<< HEAD
=======
// TestMDLPreparePlanCacheExecuteInsert makes sure the insert statement handle the schema correctly in plan cache.
func TestMDLPreparePlanCacheExecuteInsert(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
defer ingesttestutil.InjectMockBackendMgr(t, store)()

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 primary key, b int);")
tk.MustExec("create table t2(a int);")
tk.MustExec("insert into t values(1, 1), (2, 2), (3, 3), (4, 4);")

tk.MustExec(`prepare insert_stmt from 'insert into t values (?, ?)'`)
tk.MustExec(`set @a=4, @b=4;`)

ch := make(chan struct{})

first := true
testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onJobUpdated", func(job *model.Job) {
switch job.SchemaState {
case model.StateWriteReorganization:
tbl, _ := dom.InfoSchema().TableByID(context.Background(), job.TableID)
idx := tbl.Meta().FindIndexByName("idx")
switch idx.BackfillState {
case model.BackfillStateRunning:
if first {
// generate plan, cache it, and make some row change to make
// sure backfill state 'merging' is not skipped.
tk.MustExec(`begin`)
tk.MustExec(`delete from t where a = 4;`)
tk.MustExec(`execute insert_stmt using @a, @b;`)
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
tk.MustExec(`commit`)

tk.MustExec("begin")
// Activate txn.
tk.MustExec("select * from t2")
first = false
return
}
}
}
})

ddl.MockDMLExecutionMerging = func() {
tk.MustExec(`delete from t where a = 4;`)
// we must generate a new plan here, because the schema has changed since
// the last plan was generated.
tk.MustExec(`execute insert_stmt using @a, @b;`)
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
tk.MustExec("commit")
}
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/ddl/mockDMLExecutionMerging", "1*return(true)->return(false)"))

var wg sync.WaitGroup
wg.Add(1)
go func() {
<-ch
tkDDL.MustExec("alter table test.t add index idx(a);")
wg.Done()
}()

ch <- struct{}{}
wg.Wait()

tk.MustExec("admin check table t")
}

>>>>>>> 93295c233a3 (ddl: simplify TestMDLPreparePlanCacheExecuteInsert (#58113))
func TestMDLDisable2Enable(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
sv := server.CreateMockServer(t, store)
Expand Down
2 changes: 2 additions & 0 deletions pkg/table/tables/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ func (c *index) create(sctx table.MutateContext, txn kv.Transaction, indexedValu
tempVal := tablecodec.TempIndexValueElem{Value: idxVal, KeyVer: keyVer, Distinct: distinct}
val = tempVal.Encode(nil)
}
// during some step of add-index, such as in write-reorg state, this
// key is THE temp index key.
err = txn.GetMemBuffer().Set(key, val)
if err != nil {
return nil, err
Expand Down