Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#41879
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
tangenta authored and ti-chi-bot committed Mar 2, 2023
1 parent ee46b2b commit 5b7be74
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
82 changes: 82 additions & 0 deletions ddl/index_merge_tmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,3 +870,85 @@ func TestAddIndexMultipleDelete(t *testing.T) {
tk.MustQuery("select * from t;").Check(testkit.Rows())
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockDMLExecution"))
}
<<<<<<< HEAD:ddl/index_merge_tmp_test.go
=======

func TestAddIndexDuplicateAndWriteConflict(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(id int primary key, b int);")
tk.MustExec("insert into t values (1, 1);")

tk1 := testkit.NewTestKit(t, store)
tk1.MustExec("use test")

d := dom.DDL()
originalCallback := d.GetHook()
defer d.SetHook(originalCallback)
callback := &callback.TestDDLCallback{}
var runCancel bool
callback.OnJobRunAfterExported = func(job *model.Job) {
if t.Failed() || runCancel {
return
}
switch job.SchemaState {
case model.StateWriteOnly:
_, err := tk1.Exec("insert into t values (2, 1);")
assert.NoError(t, err)
}
if job.State == model.JobStateRollingback {
_, err := tk1.Exec("admin cancel ddl jobs " + strconv.FormatInt(job.ID, 10))
assert.NoError(t, err)
runCancel = true
}
}
d.SetHook(callback)

tk.MustGetErrCode("alter table t add unique index idx(b);", errno.ErrCancelledDDLJob)
tk.MustExec("admin check table t;")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 1", "2 1"))
}

func TestAddIndexUpdateUntouchedValues(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(id int primary key, b int, k int);")
tk.MustExec("insert into t values (1, 1, 1);")

tk1 := testkit.NewTestKit(t, store)
tk1.MustExec("use test")

d := dom.DDL()
originalCallback := d.GetHook()
defer d.SetHook(originalCallback)
callback := &callback.TestDDLCallback{}
var runDML bool
callback.OnJobRunAfterExported = func(job *model.Job) {
if t.Failed() || runDML {
return
}
switch job.SchemaState {
case model.StateWriteReorganization:
_, err := tk1.Exec("begin;")
assert.NoError(t, err)
_, err = tk1.Exec("update t set k=k+1 where id = 1;")
assert.NoError(t, err)
_, err = tk1.Exec("insert into t values (2, 1, 2);")
// Should not report "invalid temp index value".
assert.NoError(t, err)
_, err = tk1.Exec("commit;")
assert.NoError(t, err)
runDML = true
}
}
d.SetHook(callback)

tk.MustGetErrCode("alter table t add unique index idx(b);", errno.ErrDupEntry)
tk.MustExec("admin check table t;")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 1 2", "2 1 2"))
}
>>>>>>> 1c1c388d6e (ddl: never write untouched index values to temp index (#41879)):ddl/indexmergetest/merge_test.go
57 changes: 57 additions & 0 deletions table/tables/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,69 @@ func (c *index) Create(sctx sessionctx.Context, txn kv.Transaction, indexedValue
if err != nil {
return nil, err
}
<<<<<<< HEAD
if len(tempKey) > 0 {
if !opt.Untouched { // Untouched key-values never occur in the storage.
tempVal := tablecodec.TempIndexValueElem{Value: idxVal, KeyVer: keyVer, Distinct: distinct}
val = tempVal.Encode(nil)
}
err = txn.GetMemBuffer().Set(tempKey, val)
=======

opt.IgnoreAssertion = opt.IgnoreAssertion || c.idxInfo.State != model.StatePublic

if !distinct || skipCheck || opt.Untouched {
val := idxVal
if opt.Untouched && (keyIsTempIdxKey || len(tempKey) > 0) {
// Untouched key-values never occur in the storage and the temp index is not public.
// It is unnecessary to write the untouched temp index key-values.
continue
}
if keyIsTempIdxKey {
tempVal := tablecodec.TempIndexValueElem{Value: idxVal, KeyVer: keyVer, Distinct: distinct}
val = tempVal.Encode(nil)
}
err = txn.GetMemBuffer().Set(key, val)
if err != nil {
return nil, err
}
if len(tempKey) > 0 {
tempVal := tablecodec.TempIndexValueElem{Value: idxVal, KeyVer: keyVer, Distinct: distinct}
val = tempVal.Encode(nil)
err = txn.GetMemBuffer().Set(tempKey, val)
if err != nil {
return nil, err
}
}
if !opt.IgnoreAssertion && (!opt.Untouched) {
if sctx.GetSessionVars().LazyCheckKeyNotExists() && !txn.IsPessimistic() {
err = txn.SetAssertion(key, kv.SetAssertUnknown)
} else {
err = txn.SetAssertion(key, kv.SetAssertNotExist)
}
}
if err != nil {
return nil, err
}
continue
}

var value []byte
if c.tblInfo.TempTableType != model.TempTableNone {
// Always check key for temporary table because it does not write to TiKV
value, err = txn.Get(ctx, key)
} else if sctx.GetSessionVars().LazyCheckKeyNotExists() {
value, err = txn.GetMemBuffer().Get(ctx, key)
} else {
value, err = txn.Get(ctx, key)
}
if err != nil && !kv.IsErrNotFound(err) {
return nil, err
}
var tempIdxVal tablecodec.TempIndexValue
if len(value) > 0 && keyIsTempIdxKey {
tempIdxVal, err = tablecodec.DecodeTempIndexValue(value)
>>>>>>> 1c1c388d6e (ddl: never write untouched index values to temp index (#41879))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 5b7be74

Please sign in to comment.