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

*: fix lost index bug of insert on duplicate key update #16672

Merged
merged 7 commits into from Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions executor/union_scan_test.go
Expand Up @@ -290,3 +290,31 @@ func (s *testSuite7) TestUnionScanForMemBufferReader(c *C) {
tk.MustQuery("select * from t1 use index(idx2);").Check(testkit.Rows("1 2 1"))
tk.MustExec("admin check table t1;")
}

func (s *testSuite7) TestForUpdateUntouchedIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")

checkFunc := func() {
tk.MustExec("begin")
tk.MustExec("insert into t values ('a', 1), ('b', 3), ('a', 2) on duplicate key update b = b + 1;")
tk.MustExec("commit")
tk.MustExec("admin check table t")

// Test for autocommit
tk.MustExec("set autocommit=0")
tk.MustExec("insert into t values ('a', 1), ('b', 3), ('a', 2) on duplicate key update b = b + 1;")
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
tk.MustExec("set autocommit=1")
tk.MustExec("admin check table t")
}

// Test for primary key.
tk.MustExec("create table t (a varchar(10) primary key,b int)")
checkFunc()

// Test for unique key.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a varchar(10),b int, unique index(a))")
checkFunc()
}
5 changes: 5 additions & 0 deletions kv/kv.go
Expand Up @@ -207,6 +207,11 @@ type Transaction interface {
IsPessimistic() bool
}

// MemBufferRetriever is the interface for the Get data only from memory-buffer.
type MemBufferRetriever interface {
GetFromMemBuffer(ctx context.Context, k Key) ([]byte, error)
}

// LockCtx contains information for LockKeys method.
type LockCtx struct {
Killed *uint32
Expand Down
18 changes: 18 additions & 0 deletions session/txn.go
Expand Up @@ -307,6 +307,24 @@ func (st *TxnState) Get(ctx context.Context, k kv.Key) ([]byte, error) {
return val, nil
}

// GetFromMemBuffer implements the MemBufferRetriever interface.
func (st *TxnState) GetFromMemBuffer(ctx context.Context, k kv.Key) ([]byte, error) {
val, err := st.stmtBufGet(ctx, k)
if kv.IsErrNotFound(err) {
_, err = st.Transaction.GetMemBuffer().Get(ctx, k)
if kv.IsErrNotFound(err) {
return nil, err
}
}
if err != nil {
return nil, err
}
if len(val) == 0 {
return nil, kv.ErrNotExist
}
return val, nil
}

// BatchGet overrides the Transaction interface.
func (st *TxnState) BatchGet(ctx context.Context, keys []kv.Key) (map[string][]byte, error) {
bufferValues := make([][]byte, len(keys))
Expand Down
6 changes: 5 additions & 1 deletion table/tables/index.go
Expand Up @@ -293,7 +293,11 @@ func (c *index) Create(sctx sessionctx.Context, rm kv.RetrieverMutator, indexedV
// If the index kv was untouched(unchanged), and the key/value already exists in mem-buffer,
// should not overwrite the key with un-commit flag.
// So if the key exists, just do nothing and return.
_, err = txn.GetMemBuffer().Get(ctx, key)
if memTxn, ok := txn.(kv.MemBufferRetriever); ok {
_, err = memTxn.GetFromMemBuffer(ctx, key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to change TxnState.GetMembuffer implementation, so we will not misuse it later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buffer level is too complex to get things right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great

} else {
_, err = txn.GetMemBuffer().Get(ctx, key)
}
if err == nil {
return 0, nil
}
Expand Down