Skip to content

Commit 2ed1798

Browse files
authored
*: Use strict validation for stale read ts & flashback ts (pingcap#57050) (pingcap#58409)
close pingcap#56809
1 parent 7c3ca2e commit 2ed1798

File tree

16 files changed

+79
-97
lines changed

16 files changed

+79
-97
lines changed

DEPS.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3603,8 +3603,8 @@ def go_deps():
36033603
name = "com_github_tikv_client_go_v2",
36043604
build_file_proto_mode = "disable_global",
36053605
importpath = "github.com/tikv/client-go/v2",
3606-
sum = "h1:0YcirnuxtXC9eQRb231im1M5w/n7JFuOo0IgE/K9ffM=",
3607-
version = "v2.0.4-0.20241125064444-5f59e4e34c62",
3606+
sum = "h1:P6bhZG2yFFuKYvOpfltUbt89sbHohq4BAv2P4GB3fL8=",
3607+
version = "v2.0.4-0.20250109055446-ccec7efbf0f7",
36083608
)
36093609
go_repository(
36103610
name = "com_github_tikv_pd_client",

ddl/cluster.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/pingcap/tidb/infoschema"
3131
"github.com/pingcap/tidb/kv"
3232
"github.com/pingcap/tidb/meta"
33-
"github.com/pingcap/tidb/metrics"
3433
"github.com/pingcap/tidb/parser/model"
3534
"github.com/pingcap/tidb/sessionctx"
3635
"github.com/pingcap/tidb/sessionctx/variable"
@@ -108,16 +107,12 @@ func getStoreGlobalMinSafeTS(s kv.Storage) time.Time {
108107

109108
// ValidateFlashbackTS validates that flashBackTS in range [gcSafePoint, currentTS).
110109
func ValidateFlashbackTS(ctx context.Context, sctx sessionctx.Context, flashBackTS uint64) error {
111-
currentTS, err := sctx.GetStore().GetOracle().GetStaleTimestamp(ctx, oracle.GlobalTxnScope, 0)
112-
// If we fail to calculate currentTS from local time, fallback to get a timestamp from PD.
110+
currentVer, err := sctx.GetStore().CurrentVersion(oracle.GlobalTxnScope)
113111
if err != nil {
114-
metrics.ValidateReadTSFromPDCount.Inc()
115-
currentVer, err := sctx.GetStore().CurrentVersion(oracle.GlobalTxnScope)
116-
if err != nil {
117-
return errors.Errorf("fail to validate flashback timestamp: %v", err)
118-
}
119-
currentTS = currentVer.Ver
112+
return errors.Errorf("fail to validate flashback timestamp: %v", err)
120113
}
114+
currentTS := currentVer.Ver
115+
121116
oracleFlashbackTS := oracle.GetTimeFromTS(flashBackTS)
122117
if oracleFlashbackTS.After(oracle.GetTimeFromTS(currentTS)) {
123118
return errors.Errorf("cannot set flashback timestamp to future time")

executor/executor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4776,7 +4776,7 @@ func TestStaleReadAtFutureTime(t *testing.T) {
47764776

47774777
tk := testkit.NewTestKit(t, store)
47784778
// Setting tx_read_ts to a time in the future will fail. (One day before the 2038 problem)
4779-
tk.MustGetErrMsg("set @@tx_read_ts = '2038-01-18 03:14:07'", "cannot set read timestamp to a future time")
4779+
tk.MustContainErrMsg("set @@tx_read_ts = '2038-01-18 03:14:07'", "cannot set read timestamp to a future time")
47804780
// TxnReadTS Is not updated if check failed.
47814781
require.Zero(t, tk.Session().GetSessionVars().TxnReadTS.PeakTxnReadTS())
47824782
}

executor/set.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,8 @@ func (e *SetExecutor) setSysVariable(ctx context.Context, name string, v *expres
197197
newSnapshotTS := getSnapshotTSByName()
198198
newSnapshotIsSet := newSnapshotTS > 0 && newSnapshotTS != oldSnapshotTS
199199
if newSnapshotIsSet {
200-
if name == variable.TiDBTxnReadTS {
201-
err = sessionctx.ValidateStaleReadTS(ctx, e.ctx, newSnapshotTS)
202-
} else {
203-
err = sessionctx.ValidateSnapshotReadTS(ctx, e.ctx, newSnapshotTS)
200+
err = sessionctx.ValidateSnapshotReadTS(ctx, e.ctx.GetStore(), newSnapshotTS)
201+
if name != variable.TiDBTxnReadTS {
204202
// Also check gc safe point for snapshot read.
205203
// We don't check snapshot with gc safe point for read_ts
206204
// Client-go will automatically check the snapshotTS with gc safe point. It's unnecessary to check gc safe point during set executor.

executor/stale_txn_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package executor_test
1717
import (
1818
"context"
1919
"fmt"
20+
"strconv"
2021
"testing"
2122
"time"
2223

@@ -1406,14 +1407,30 @@ func TestStaleTSO(t *testing.T) {
14061407
tk.MustExec("create table t (id int)")
14071408

14081409
tk.MustExec("insert into t values(1)")
1410+
ts1, err := strconv.ParseUint(tk.MustQuery("select json_extract(@@tidb_last_txn_info, '$.commit_ts')").Rows()[0][0].(string), 10, 64)
1411+
require.NoError(t, err)
1412+
1413+
// Wait until the physical advances for 1s
1414+
var currentTS uint64
1415+
for {
1416+
tk.MustExec("begin")
1417+
currentTS, err = strconv.ParseUint(tk.MustQuery("select @@tidb_current_ts").Rows()[0][0].(string), 10, 64)
1418+
require.NoError(t, err)
1419+
tk.MustExec("rollback")
1420+
if oracle.GetTimeFromTS(currentTS).After(oracle.GetTimeFromTS(ts1).Add(time.Second)) {
1421+
break
1422+
}
1423+
time.Sleep(time.Millisecond * 100)
1424+
}
14091425

14101426
asOfExprs := []string{
1411-
"now(3) - interval 1 second",
1412-
"current_time() - interval 1 second",
1413-
"curtime() - interval 1 second",
1427+
"now(3) - interval 10 second",
1428+
"current_time() - interval 10 second",
1429+
"curtime() - interval 10 second",
14141430
}
14151431

1416-
nextTSO := oracle.GoTimeToTS(time.Now().Add(2 * time.Second))
1432+
nextPhysical := oracle.GetPhysical(oracle.GetTimeFromTS(currentTS).Add(10 * time.Second))
1433+
nextTSO := oracle.ComposeTS(nextPhysical, oracle.ExtractLogical(currentTS))
14171434
require.Nil(t, failpoint.Enable("github.com/pingcap/tidb/sessiontxn/staleread/mockStaleReadTSO", fmt.Sprintf("return(%d)", nextTSO)))
14181435
defer failpoint.Disable("github.com/pingcap/tidb/sessiontxn/staleread/mockStaleReadTSO")
14191436
for _, expr := range asOfExprs {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ require (
9090
github.com/stretchr/testify v1.8.4
9191
github.com/tdakkota/asciicheck v0.1.1
9292
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2
93-
github.com/tikv/client-go/v2 v2.0.4-0.20241125064444-5f59e4e34c62
93+
github.com/tikv/client-go/v2 v2.0.4-0.20250109055446-ccec7efbf0f7
9494
github.com/tikv/pd/client v0.0.0-20230904040343-947701a32c05
9595
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
9696
github.com/twmb/murmur3 v1.1.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,8 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpR
948948
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY=
949949
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 h1:mbAskLJ0oJfDRtkanvQPiooDH8HvJ2FBh+iKT/OmiQQ=
950950
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfKggNGDuadAa0LElHrByyrz4JPZ9fFx6Gs7nx7ZZU=
951-
github.com/tikv/client-go/v2 v2.0.4-0.20241125064444-5f59e4e34c62 h1:0YcirnuxtXC9eQRb231im1M5w/n7JFuOo0IgE/K9ffM=
952-
github.com/tikv/client-go/v2 v2.0.4-0.20241125064444-5f59e4e34c62/go.mod h1:mmVCLP2OqWvQJPOIevQPZvGphzh/oq9vv8J5LDfpadQ=
951+
github.com/tikv/client-go/v2 v2.0.4-0.20250109055446-ccec7efbf0f7 h1:P6bhZG2yFFuKYvOpfltUbt89sbHohq4BAv2P4GB3fL8=
952+
github.com/tikv/client-go/v2 v2.0.4-0.20250109055446-ccec7efbf0f7/go.mod h1:mmVCLP2OqWvQJPOIevQPZvGphzh/oq9vv8J5LDfpadQ=
953953
github.com/tikv/pd/client v0.0.0-20230904040343-947701a32c05 h1:e4hLUKfgfPeJPZwOfU+/I/03G0sn6IZqVcbX/5o+hvM=
954954
github.com/tikv/pd/client v0.0.0-20230904040343-947701a32c05/go.mod h1:MLIl+d2WbOF4A3U88WKtyXrQQW417wZDDvBcq2IW9bQ=
955955
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 h1:kl4KhGNsJIbDHS9/4U9yQo1UcPQM0kOMJHn29EoH/Ro=

pkg/sessionctx/BUILD.bazel

Whitespace-only changes.

planner/core/plan_cache_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ type PlanCacheStmt struct {
515515
SQLDigest *parser.Digest
516516
PlanDigest *parser.Digest
517517
ForUpdateRead bool
518-
SnapshotTSEvaluator func(sessionctx.Context) (uint64, error)
518+
SnapshotTSEvaluator func(context.Context, sessionctx.Context) (uint64, error)
519519
NormalizedSQL4PC string
520520
SQLDigest4PC string
521521

planner/core/planbuilder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,7 +3399,7 @@ func (b *PlanBuilder) buildSimple(ctx context.Context, node ast.StmtNode) (Plan,
33993399
if err != nil {
34003400
return nil, err
34013401
}
3402-
if err := sessionctx.ValidateStaleReadTS(ctx, b.ctx, startTS); err != nil {
3402+
if err := sessionctx.ValidateSnapshotReadTS(ctx, b.ctx.GetStore(), startTS); err != nil {
34033403
return nil, err
34043404
}
34053405
p.StaleTxnStartTS = startTS
@@ -3413,7 +3413,7 @@ func (b *PlanBuilder) buildSimple(ctx context.Context, node ast.StmtNode) (Plan,
34133413
if err != nil {
34143414
return nil, err
34153415
}
3416-
if err := sessionctx.ValidateStaleReadTS(ctx, b.ctx, startTS); err != nil {
3416+
if err := sessionctx.ValidateSnapshotReadTS(ctx, b.ctx.GetStore(), startTS); err != nil {
34173417
return nil, err
34183418
}
34193419
p.StaleTxnStartTS = startTS

0 commit comments

Comments
 (0)