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

kv: merge batch get results to fix insert ignore in a transaction block #6215

Merged
merged 6 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions executor/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func (e *RecoverIndexExec) batchMarkDup(txn kv.Transaction, rows []recoverRows)
distinctFlags[i] = distinct
}

values, err := txn.GetSnapshot().BatchGet(e.batchKeys)
values, err := kv.BatchGetValues(txn, e.batchKeys)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -485,7 +485,7 @@ func (e *CleanupIndexExec) batchGetRecord(txn kv.Transaction) (map[string][]byte
for handle := range e.idxValues {
e.batchKeys = append(e.batchKeys, e.table.RecordKey(handle))
}
values, err := txn.GetSnapshot().BatchGet(e.batchKeys)
values, err := kv.BatchGetValues(txn, e.batchKeys)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ func batchMarkDupRows(ctx sessionctx.Context, t table.Table, rows [][]types.Datu
batchKeys = append(batchKeys, k.key)
}
}
values, err := ctx.Txn().GetSnapshot().BatchGet(batchKeys)
values, err := kv.BatchGetValues(ctx.Txn(), batchKeys)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
34 changes: 34 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,40 @@ func (s *testSuite) TestInsertIgnore(c *C) {
c.Assert(err, IsNil)
r = tk.MustQuery("SHOW WARNINGS")
r.Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'PRIMARY'"))

testSQL = `drop table if exists test;
create table test (i int primary key, j int unique);
begin;
insert into test values (1,1);
insert ignore into test values (2,1);
commit;`
tk.MustExec(testSQL)
testSQL = `select * from test;`
r = tk.MustQuery(testSQL)
r.Check(testkit.Rows("1 1"))

testSQL = `delete from test;
insert into test values (1, 1);
begin;
delete from test where i = 1;
insert ignore into test values (2, 1);
commit;`
tk.MustExec(testSQL)
testSQL = `select * from test;`
r = tk.MustQuery(testSQL)
r.Check(testkit.Rows("2 1"))

testSQL = `delete from test;
insert into test values (1, 1);
begin;
update test set i = 2, j = 2 where i = 1;
insert ignore into test values (1, 3);
insert ignore into test values (2, 4);
commit;`
tk.MustExec(testSQL)
testSQL = `select * from test order by i;`
r = tk.MustQuery(testSQL)
r.Check(testkit.Rows("1 3", "2 2"))
}

func (s *testSuite) TestReplace(c *C) {
Expand Down
28 changes: 28 additions & 0 deletions kv/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,31 @@ func BackOff(attempts uint) int {
time.Sleep(sleep)
return int(sleep)
}

// BatchGetValues gets values in batch.
// The values from buffer in transaction and the values from the storage node are merged together.
func BatchGetValues(txn Transaction, keys []Key) (map[string][]byte, error) {
bufferValues := make(map[string][]byte, len(keys))
Copy link
Member

Choose a reason for hiding this comment

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

Using a keys slice and a values slice has a lower cost.

shrinkKeys := make([]Key, 0, len(keys))
for _, key := range keys {
val, err := txn.GetMemBuffer().Get(key)
if IsErrNotFound(err) {
shrinkKeys = append(shrinkKeys, key)
continue
}
if err != nil {
return nil, errors.Trace(err)
}
if len(val) != 0 {
bufferValues[string(key)] = val
}
}
storageValues, err := txn.GetSnapshot().BatchGet(shrinkKeys)
if err != nil {
return nil, errors.Trace(err)
}
for key, val := range bufferValues {
storageValues[string(key)] = val
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to do CheckVisibility(txn.startTS) ?

Copy link
Member Author

Choose a reason for hiding this comment

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

In (s *tikvSnapshot) BatchGet , it is already been checked.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ignore it.
We do it in BatchGet.

return storageValues, nil
}