Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#39950
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
zyguan authored and ti-chi-bot committed Dec 16, 2022
1 parent 5ccc10b commit 0d5cf76
Show file tree
Hide file tree
Showing 13 changed files with 1,542 additions and 0 deletions.
297 changes: 297 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6995,8 +6995,305 @@ func (s *testSerialDBSuite) TestColumnTypeChangeGenUniqueChangingName(c *C) {
tk.MustExec("drop table if exists t")
}

<<<<<<< HEAD
func (s *testSerialDBSuite) TestModifyColumnTypeWithWarnings(c *C) {
tk := testkit.NewTestKit(c, s.store)
=======
func TestTxnSavepointWithDDL(t *testing.T) {
store, _ := testkit.CreateMockStoreAndDomainWithSchemaLease(t, dbTestLease)
tk := testkit.NewTestKit(t, store)
tk2 := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("set global tidb_enable_metadata_lock=0")
tk2.MustExec("use test;")

prepareFn := func() {
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1 (c1 int primary key, c2 int)")
tk.MustExec("create table t2 (c1 int primary key, c2 int)")
}
prepareFn()

tk.MustExec("begin pessimistic")
tk.MustExec("savepoint s1")
tk.MustExec("insert t1 values (1, 11)")
tk.MustExec("rollback to s1")
tk2.MustExec("alter table t1 add index idx2(c2)")
tk.MustExec("commit")
tk.MustQuery("select * from t1").Check(testkit.Rows())
tk.MustExec("admin check table t1")

tk.MustExec("begin pessimistic")
tk.MustExec("savepoint s1")
tk.MustExec("insert t1 values (1, 11)")
tk.MustExec("savepoint s2")
tk.MustExec("insert t2 values (1, 11)")
tk.MustExec("rollback to s2")
tk2.MustExec("alter table t2 add index idx2(c2)")
tk.MustExec("commit")
tk.MustQuery("select * from t2").Check(testkit.Rows())
tk.MustExec("admin check table t1, t2")

prepareFn()
tk.MustExec("truncate table t1")
tk.MustExec("begin pessimistic")
tk.MustExec("savepoint s1")
tk.MustExec("insert t1 values (1, 11)")
tk.MustExec("savepoint s2")
tk.MustExec("insert t2 values (1, 11)")
tk.MustExec("rollback to s2")
tk2.MustExec("alter table t1 add index idx2(c2)")
tk2.MustExec("alter table t2 add index idx2(c2)")
err := tk.ExecToErr("commit")
require.Error(t, err)
require.Regexp(t, ".*8028.*Information schema is changed during the execution of the statement.*", err.Error())
tk.MustQuery("select * from t1").Check(testkit.Rows())
tk.MustExec("admin check table t1, t2")
}

func TestAmendTxnSavepointWithDDL(t *testing.T) {
store, _ := testkit.CreateMockStoreAndDomainWithSchemaLease(t, dbTestLease)
tk := testkit.NewTestKit(t, store)
tk2 := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("set global tidb_enable_metadata_lock=0")
tk2.MustExec("use test;")
tk.MustExec("set global tidb_ddl_enable_fast_reorg = 0;")
tk.MustExec("set tidb_enable_amend_pessimistic_txn = 1;")

prepareFn := func() {
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1 (c1 int primary key, c2 int)")
tk.MustExec("create table t2 (c1 int primary key, c2 int)")
}

prepareFn()
tk.MustExec("truncate table t1")
tk.MustExec("begin pessimistic")
tk.MustExec("savepoint s1")
tk.MustExec("insert t1 values (1, 11)")
tk.MustExec("savepoint s2")
tk.MustExec("insert t2 values (1, 11)")
tk.MustExec("rollback to s2")
tk2.MustExec("alter table t1 add index idx2(c2)")
tk2.MustExec("alter table t2 add index idx2(c2)")
tk.MustExec("commit")
tk.MustQuery("select * from t1").Check(testkit.Rows("1 11"))
tk.MustQuery("select * from t2").Check(testkit.Rows())
tk.MustExec("admin check table t1, t2")

prepareFn()
tk.MustExec("truncate table t1")
tk.MustExec("begin pessimistic")
tk.MustExec("savepoint s1")
tk.MustExec("insert t1 values (1, 11)")
tk.MustExec("savepoint s2")
tk.MustExec("insert t2 values (1, 11)")
tk.MustExec("savepoint s3")
tk.MustExec("insert t2 values (2, 22)")
tk.MustExec("rollback to s3")
tk2.MustExec("alter table t1 add index idx2(c2)")
tk2.MustExec("alter table t2 add index idx2(c2)")
tk.MustExec("commit")
tk.MustQuery("select * from t1").Check(testkit.Rows("1 11"))
tk.MustQuery("select * from t2").Check(testkit.Rows("1 11"))
tk.MustExec("admin check table t1, t2")
}

func TestSnapshotVersion(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomainWithSchemaLease(t, dbTestLease)

tk := testkit.NewTestKit(t, store)

dd := dom.DDL()
ddl.DisableTiFlashPoll(dd)
require.Equal(t, dbTestLease, dd.GetLease())

snapTS := oracle.GoTimeToTS(time.Now())
tk.MustExec("create database test2")
tk.MustExec("use test2")
tk.MustExec("create table t(a int)")

is := dom.InfoSchema()
require.NotNil(t, is)

// For updating the self schema version.
goCtx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
err := dd.SchemaSyncer().OwnerCheckAllVersions(goCtx, 0, is.SchemaMetaVersion())
cancel()
require.NoError(t, err)

snapIs, err := dom.GetSnapshotInfoSchema(snapTS)
require.NotNil(t, snapIs)
require.NoError(t, err)

// Make sure that the self schema version doesn't be changed.
goCtx, cancel = context.WithTimeout(context.Background(), 100*time.Millisecond)
err = dd.SchemaSyncer().OwnerCheckAllVersions(goCtx, 0, is.SchemaMetaVersion())
cancel()
require.NoError(t, err)

// for GetSnapshotInfoSchema
currSnapTS := oracle.GoTimeToTS(time.Now())
currSnapIs, err := dom.GetSnapshotInfoSchema(currSnapTS)
require.NoError(t, err)
require.NotNil(t, currSnapTS)
require.Equal(t, is.SchemaMetaVersion(), currSnapIs.SchemaMetaVersion())

// for GetSnapshotMeta
dbInfo, ok := currSnapIs.SchemaByName(model.NewCIStr("test2"))
require.True(t, ok)

tbl, err := currSnapIs.TableByName(model.NewCIStr("test2"), model.NewCIStr("t"))
require.NoError(t, err)

m, err := dom.GetSnapshotMeta(snapTS)
require.NoError(t, err)

tblInfo1, err := m.GetTable(dbInfo.ID, tbl.Meta().ID)
require.True(t, meta.ErrDBNotExists.Equal(err))
require.Nil(t, tblInfo1)

m, err = dom.GetSnapshotMeta(currSnapTS)
require.NoError(t, err)

tblInfo2, err := m.GetTable(dbInfo.ID, tbl.Meta().ID)
require.NoError(t, err)
require.Equal(t, tblInfo2, tbl.Meta())
}

func TestSchemaValidator(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomainWithSchemaLease(t, dbTestLease)

tk := testkit.NewTestKit(t, store)

dd := dom.DDL()
ddl.DisableTiFlashPoll(dd)
require.Equal(t, dbTestLease, dd.GetLease())

tk.MustExec("create table test.t(a int)")

err := dom.Reload()
require.NoError(t, err)
schemaVer := dom.InfoSchema().SchemaMetaVersion()
ver, err := store.CurrentVersion(kv.GlobalTxnScope)
require.NoError(t, err)

ts := ver.Ver
_, res := dom.SchemaValidator.Check(ts, schemaVer, nil, true)
require.Equal(t, domain.ResultSucc, res)

require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/domain/ErrorMockReloadFailed", `return(true)`))

err = dom.Reload()
require.Error(t, err)
_, res = dom.SchemaValidator.Check(ts, schemaVer, nil, true)
require.Equal(t, domain.ResultSucc, res)
time.Sleep(dbTestLease)

ver, err = store.CurrentVersion(kv.GlobalTxnScope)
require.NoError(t, err)
ts = ver.Ver
_, res = dom.SchemaValidator.Check(ts, schemaVer, nil, true)
require.Equal(t, domain.ResultUnknown, res)

require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/domain/ErrorMockReloadFailed"))
err = dom.Reload()
require.NoError(t, err)

_, res = dom.SchemaValidator.Check(ts, schemaVer, nil, true)
require.Equal(t, domain.ResultSucc, res)

// For schema check, it tests for getting the result of "ResultUnknown".
is := dom.InfoSchema()
schemaChecker := domain.NewSchemaChecker(dom, is.SchemaMetaVersion(), nil, true)
// Make sure it will retry one time and doesn't take a long time.
domain.SchemaOutOfDateRetryTimes.Store(1)
domain.SchemaOutOfDateRetryInterval.Store(time.Millisecond * 1)
dom.SchemaValidator.Stop()
_, err = schemaChecker.Check(uint64(123456))
require.EqualError(t, err, domain.ErrInfoSchemaExpired.Error())
}

func TestLogAndShowSlowLog(t *testing.T) {
_, dom := testkit.CreateMockStoreAndDomainWithSchemaLease(t, dbTestLease)

dom.LogSlowQuery(&domain.SlowQueryInfo{SQL: "aaa", Duration: time.Second, Internal: true})
dom.LogSlowQuery(&domain.SlowQueryInfo{SQL: "bbb", Duration: 3 * time.Second})
dom.LogSlowQuery(&domain.SlowQueryInfo{SQL: "ccc", Duration: 2 * time.Second})
// Collecting slow queries is asynchronous, wait a while to ensure it's done.
time.Sleep(5 * time.Millisecond)

result := dom.ShowSlowQuery(&ast.ShowSlow{Tp: ast.ShowSlowTop, Count: 2})
require.Len(t, result, 2)
require.Equal(t, "bbb", result[0].SQL)
require.Equal(t, 3*time.Second, result[0].Duration)
require.Equal(t, "ccc", result[1].SQL)
require.Equal(t, 2*time.Second, result[1].Duration)

result = dom.ShowSlowQuery(&ast.ShowSlow{Tp: ast.ShowSlowTop, Count: 2, Kind: ast.ShowSlowKindInternal})
require.Len(t, result, 1)
require.Equal(t, "aaa", result[0].SQL)
require.Equal(t, time.Second, result[0].Duration)
require.True(t, result[0].Internal)

result = dom.ShowSlowQuery(&ast.ShowSlow{Tp: ast.ShowSlowTop, Count: 4, Kind: ast.ShowSlowKindAll})
require.Len(t, result, 3)
require.Equal(t, "bbb", result[0].SQL)
require.Equal(t, 3*time.Second, result[0].Duration)
require.Equal(t, "ccc", result[1].SQL)
require.Equal(t, 2*time.Second, result[1].Duration)
require.Equal(t, "aaa", result[2].SQL)
require.Equal(t, time.Second, result[2].Duration)
require.True(t, result[2].Internal)

result = dom.ShowSlowQuery(&ast.ShowSlow{Tp: ast.ShowSlowRecent, Count: 2})
require.Len(t, result, 2)
require.Equal(t, "ccc", result[0].SQL)
require.Equal(t, 2*time.Second, result[0].Duration)
require.Equal(t, "bbb", result[1].SQL)
require.Equal(t, 3*time.Second, result[1].Duration)
}

func TestReportingMinStartTimestamp(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomainWithSchemaLease(t, dbTestLease)
tk := testkit.NewTestKit(t, store)
se := tk.Session()

infoSyncer := dom.InfoSyncer()
beforeTS := oracle.GoTimeToTS(time.Now())
infoSyncer.ReportMinStartTS(dom.Store())
afterTS := oracle.GoTimeToTS(time.Now())
require.False(t, infoSyncer.GetMinStartTS() > beforeTS && infoSyncer.GetMinStartTS() < afterTS)

now := time.Now()
validTS := oracle.GoTimeToLowerLimitStartTS(now.Add(time.Minute), tikv.MaxTxnTimeUse)
lowerLimit := oracle.GoTimeToLowerLimitStartTS(now, tikv.MaxTxnTimeUse)
sm := se.GetSessionManager().(*testkit.MockSessionManager)
sm.PS = []*util.ProcessInfo{
{CurTxnStartTS: 0, ProtectedTSList: &se.GetSessionVars().ProtectedTSList},
{CurTxnStartTS: math.MaxUint64, ProtectedTSList: &se.GetSessionVars().ProtectedTSList},
{CurTxnStartTS: lowerLimit, ProtectedTSList: &se.GetSessionVars().ProtectedTSList},
{CurTxnStartTS: validTS, ProtectedTSList: &se.GetSessionVars().ProtectedTSList},
}
infoSyncer.ReportMinStartTS(dom.Store())
require.Equal(t, validTS, infoSyncer.GetMinStartTS())

unhold := se.GetSessionVars().ProtectedTSList.HoldTS(validTS - 1)
infoSyncer.ReportMinStartTS(dom.Store())
require.Equal(t, validTS-1, infoSyncer.GetMinStartTS())

unhold()
infoSyncer.ReportMinStartTS(dom.Store())
require.Equal(t, validTS, infoSyncer.GetMinStartTS())
}

// for issue #34931
func TestBuildMaxLengthIndexWithNonRestrictedSqlMode(t *testing.T) {
store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
>>>>>>> 0fe61bd41a (*: prevent cursor read from being cancelled by GC (#39950))
tk.MustExec("use test")

// Test normal warnings.
Expand Down
14 changes: 14 additions & 0 deletions domain/infosync/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,10 @@ func (is *InfoSyncer) ReportMinStartTS(store kv.Storage) {
if sm == nil {
return
}
<<<<<<< HEAD
pl := sm.ShowProcessList()
=======
>>>>>>> 0fe61bd41a (*: prevent cursor read from being cancelled by GC (#39950))

// Calculate the lower limit of the start timestamp to avoid extremely old transaction delaying GC.
currentVer, err := store.CurrentVersion(kv.GlobalTxnScope)
Expand All @@ -612,13 +615,24 @@ func (is *InfoSyncer) ReportMinStartTS(store kv.Storage) {
startTSLowerLimit := oracle.GoTimeToLowerLimitStartTS(now, tikv.MaxTxnTimeUse)

minStartTS := oracle.GoTimeToTS(now)
<<<<<<< HEAD
for _, info := range pl {
if info.CurTxnStartTS > startTSLowerLimit && info.CurTxnStartTS < minStartTS {
minStartTS = info.CurTxnStartTS
}
}

is.minStartTS = minStartTS
=======
logutil.BgLogger().Debug("ReportMinStartTS", zap.Uint64("initial minStartTS", minStartTS),
zap.Uint64("StartTSLowerLimit", startTSLowerLimit))
if ts := sm.GetMinStartTS(startTSLowerLimit); ts > startTSLowerLimit && ts < minStartTS {
minStartTS = ts
}

is.minStartTS = kv.GetMinInnerTxnStartTS(now, startTSLowerLimit, minStartTS)

>>>>>>> 0fe61bd41a (*: prevent cursor read from being cancelled by GC (#39950))
err = is.storeMinStartTS(context.Background())
if err != nil {
logutil.BgLogger().Error("update minStartTS failed", zap.Error(err))
Expand Down
Loading

0 comments on commit 0d5cf76

Please sign in to comment.