Skip to content

Commit

Permalink
tidb: enable to forbid to retry when commit, add isolation test. (pin…
Browse files Browse the repository at this point in the history
  • Loading branch information
wentaoxu authored and tiancaiamao committed Mar 16, 2018
1 parent f65ece4 commit c2d6f91
Show file tree
Hide file tree
Showing 2 changed files with 264 additions and 1 deletion.
263 changes: 263 additions & 0 deletions isolation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package tidb_test

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
)

var _ = Suite(&testIsolationSuite{})

type testIsolationSuite struct {
cluster *mocktikv.Cluster
mvccStore *mocktikv.MvccStore
store kv.Storage
dom *domain.Domain
}

func (s *testIsolationSuite) SetUpSuite(c *C) {
testleak.BeforeTest()
s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
s.mvccStore = mocktikv.NewMvccStore()
store, err := mockstore.NewMockTikvStore(
mockstore.WithCluster(s.cluster),
mockstore.WithMVCCStore(s.mvccStore),
)
c.Assert(err, IsNil)
s.store = store
tidb.SetSchemaLease(0)
tidb.SetStatsLease(0)
s.dom, err = tidb.BootstrapSession(s.store)
c.Assert(err, IsNil)
tidb.SetCommitRetryLimit(0)
}

func (s *testIsolationSuite) TearDownSuite(c *C) {
tidb.SetCommitRetryLimit(10)
s.dom.Close()
s.store.Close()
testleak.AfterTest(c)()
}

/*
These test cases come from the paper <A Critique of ANSI SQL Isolation Levels>.
The sign 'P0', 'P1'.... can be found in the paper. These cases will run under snapshot isolation.
*/
func (s *testIsolationSuite) TestP0DirtyWrite(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
session1.MustExec("insert into x values(1, 1);")

session1.MustExec("begin;")
session1.MustExec("update x set c = c+1 where id = 1;")
session2.MustExec("begin;")
session2.MustExec("update x set c = c+1 where id = 1;")
session1.MustExec("commit;")
_, err := session2.Exec("commit;")
c.Assert(err, NotNil)
}

func (s *testIsolationSuite) TestP1DirtyRead(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
session1.MustExec("insert into x values(1, 1);")

session1.MustExec("begin;")
session1.MustExec("update x set c = c+1 where id = 1;")
session2.MustExec("begin;")
session2.MustQuery("select c from x where id = 1;").Check(testkit.Rows("1"))
session1.MustExec("commit;")
session2.MustExec("commit;")
}

func (s *testIsolationSuite) TestP2NonRepeatableRead(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)

session1.MustExec("drop table if exists x;")
session1.MustExec("drop table if exists y;")
session1.MustExec("create table x (id int primary key, c int);")
session1.MustExec("insert into x values(1, 1);")
session1.MustExec("create table y (id int primary key, c int);")
session1.MustExec("insert into y values(1, 1);")

session1.MustExec("begin;")
session2.MustExec("begin;")
session1.MustQuery("select c from x where id = 1;").Check(testkit.Rows("1"))
session2.MustQuery("select c from x where id = 1;").Check(testkit.Rows("1"))
session2.MustExec("update x set c = c+1 where id = 1;")
session2.MustQuery("select c from y where id = 1;").Check(testkit.Rows("1"))
session2.MustExec("update y set c = c+1 where id = 1;")
session2.MustExec("commit;")
session1.MustQuery("select c from y where id = 1;").Check(testkit.Rows("1"))
session1.MustExec("commit;")
}

func (s *testIsolationSuite) TestP3Phantom(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)

session1.MustExec("drop table if exists x;")
session1.MustExec("drop table if exists z;")
session1.MustExec("create table x (id int primary key, c int);")
session1.MustExec("insert into x values(1, 1);")
session1.MustExec("create table z (id int primary key, c int);")
session1.MustExec("insert into z values(1, 1);")

session1.MustExec("begin;")
session2.MustExec("begin;")
session1.MustQuery("select c from x where id < 5;").Check(testkit.Rows("1"))
session2.MustExec("insert into x values(2, 1);")
session2.MustQuery("select c from z where id = 1;").Check(testkit.Rows("1"))
session2.MustExec("update z set c = c+1 where id = 1;")
session2.MustExec("commit;")
session1.MustQuery("select c from z where id = 1;").Check(testkit.Rows("1"))
session1.MustExec("commit;")
}

func (s *testIsolationSuite) TestP4LostUpdate(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
session1.MustExec("insert into x values(1, 1);")

session1.MustExec("begin;")
session1.MustQuery("select c from x where id = 1;").Check(testkit.Rows("1"))
session2.MustExec("begin;")
session2.MustQuery("select c from x where id = 1;").Check(testkit.Rows("1"))
session2.MustExec("update x set c = c+1 where id = 1;")
session2.MustExec("commit;")
session1.MustExec("update x set c = c+1 where id = 1;")
_, err := session1.Exec("commit;")
c.Assert(err, NotNil)
}

// cursor is not supported
func (s *testIsolationSuite) TestP4CLostUpdate(c *C) {}

func (s *testIsolationSuite) TestA3Phantom(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
session1.MustExec("insert into x values(1, 1);")

session1.MustExec("begin;")
session2.MustExec("begin;")
session2.MustQuery("select c from x where id < 5;").Check(testkit.Rows("1"))
session1.MustExec("insert into x values(2, 1);")
session1.MustExec("commit;")
session2.MustQuery("select c from x where id < 5;").Check(testkit.Rows("1"))
session2.MustExec("commit;")
}

func (s *testIsolationSuite) TestA5AReadSkew(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)

session1.MustExec("drop table if exists x;")
session1.MustExec("drop table if exists y;")
session1.MustExec("create table x (id int primary key, c int);")
session1.MustExec("insert into x values(1, 1);")
session1.MustExec("create table y (id int primary key, c int);")
session1.MustExec("insert into y values(1, 1);")

session1.MustExec("begin;")
session2.MustExec("begin;")
session1.MustQuery("select c from x where id = 1;").Check(testkit.Rows("1"))
session2.MustExec("update x set c = c+1 where id = 1;")
session2.MustExec("update y set c = c+1 where id = 1;")
session2.MustExec("commit;")
session1.MustQuery("select c from y where id = 1;").Check(testkit.Rows("1"))
session1.MustExec("commit;")
}

func (s *testIsolationSuite) TestA5BWriteSkew(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)

session1.MustExec("drop table if exists x;")
session1.MustExec("drop table if exists y;")
session1.MustExec("create table x (id int primary key, c int);")
session1.MustExec("insert into x values(1, 1);")
session1.MustExec("create table y (id int primary key, c int);")
session1.MustExec("insert into y values(1, 1);")

session1.MustExec("begin;")
session2.MustExec("begin;")
session1.MustQuery("select c from x where id = 1;").Check(testkit.Rows("1"))
session2.MustQuery("select c from y where id = 1;").Check(testkit.Rows("1"))
session1.MustExec("update y set c = c+1 where id = 1;")
session2.MustExec("update x set c = c+1 where id = 1;")
session2.MustExec("commit;")
session1.MustExec("commit;")
}

/*
These test cases come from the paper <Highly Available Transactions: Virtues and Limitations>
for tidb, we support read-after-write on cluster level.
*/
func (s *testIsolationSuite) TestReadAfterWrite(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
session1.MustExec("insert into x values(1, 1);")

session1.MustExec("begin;")
session1.MustExec("update x set c = c+1 where id = 1;")
session1.MustExec("commit;")
session1.MustExec("begin;")
session2.MustQuery("select c from x where id = 1;").Check(testkit.Rows("2"))
session2.MustExec("commit;")
}

/*
This case will do harm in Innodb, even if in snapshot isolation, but harmless in tidb.
*/
func (s *testIsolationSuite) TestPhantomReadInInnodb(c *C) {
session1 := testkit.NewTestKitWithInit(c, s.store)
session2 := testkit.NewTestKitWithInit(c, s.store)

session1.MustExec("drop table if exists x;")
session1.MustExec("create table x (id int primary key, c int);")
session1.MustExec("insert into x values(1, 1);")

session1.MustExec("begin;")
session1.MustQuery("select c from x where id < 5;").Check(testkit.Rows("1"))
session2.MustExec("begin;")
session2.MustExec("insert into x values(2, 1);")
session2.MustExec("commit;")
session1.MustExec("update x set c = c+1 where id < 5;")
session1.MustQuery("select c from x where id < 5;").Check(testkit.Rows("2"))
session1.MustExec("commit;")
}
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (s *session) doCommitWithRetry(ctx context.Context) error {
// Don't retry in BatchInsert mode. As a counter-example, insert into t1 select * from t2,
// BatchInsert already commit the first batch 1000 rows, then it commit 1000-2000 and retry the statement,
// Finally t1 will have more data than t2, with no errors return to user!
if s.isRetryableError(err) && !s.sessionVars.BatchInsert {
if s.isRetryableError(err) && !s.sessionVars.BatchInsert && commitRetryLimit != 0 {
log.Warnf("[%d] retryable error: %v, txn: %v", s.sessionVars.ConnectionID, err, s.txn)
// Transactions will retry 2 ~ commitRetryLimit times.
// We make larger transactions retry less times to prevent cluster resource outage.
Expand Down

0 comments on commit c2d6f91

Please sign in to comment.