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 4b777b4 commit 8bb84a7
Show file tree
Hide file tree
Showing 13 changed files with 1,537 additions and 0 deletions.
222 changes: 222 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7386,6 +7386,7 @@ func testDropIndexes(c *C, store kv.Storage, lease time.Duration, createSQL, dro
defer resetHook()
testddlutil.SessionExecInGoroutine(store, dropIdxSQL, done)

<<<<<<< HEAD
ticker := time.NewTicker(lease / 2)
defer ticker.Stop()
LOOP:
Expand All @@ -7410,6 +7411,227 @@ LOOP:
for _, idxID := range idxIDs {
checkDelRangeAdded(tk, jobIDExt.jobID, idxID)
}
=======
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())
>>>>>>> 0fe61bd41a (*: prevent cursor read from being cancelled by GC (#39950))
}

func testCancelDropIndexes(c *C, store kv.Storage, d ddl.DDL) {
Expand Down
14 changes: 14 additions & 0 deletions domain/infosync/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,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 @@ -610,13 +613,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 8bb84a7

Please sign in to comment.