Skip to content

Commit fbff83e

Browse files
committed
test: migrate stale_txn tests to real tikv tests
Signed-off-by: ekexium <[email protected]>
1 parent 1596775 commit fbff83e

File tree

1 file changed

+75
-39
lines changed

1 file changed

+75
-39
lines changed

pkg/executor/stale_txn_test.go renamed to tests/realtikvtest/txntest/stale_read_test.go

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 PingCAP, Inc.
1+
// Copyright 2025 PingCAP, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,33 +12,68 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package executor_test
15+
package txntest
1616

1717
import (
1818
"bytes"
1919
"context"
2020
"fmt"
21+
"github.com/docker/go-units"
22+
"github.com/pingcap/errors"
23+
"github.com/pingcap/failpoint"
24+
"github.com/pingcap/tidb/pkg/ddl/placement"
25+
"github.com/pingcap/tidb/pkg/sessiontxn"
26+
"github.com/pingcap/tidb/pkg/sessiontxn/staleread"
27+
"github.com/pingcap/tidb/pkg/types"
28+
"github.com/tikv/client-go/v2/oracle"
2129
"strconv"
2230
"testing"
2331
"time"
2432

25-
"github.com/docker/go-units"
26-
"github.com/pingcap/errors"
27-
"github.com/pingcap/failpoint"
2833
"github.com/pingcap/kvproto/pkg/errorpb"
2934
"github.com/pingcap/kvproto/pkg/kvrpcpb"
3035
"github.com/pingcap/tidb/pkg/config"
31-
"github.com/pingcap/tidb/pkg/ddl/placement"
32-
"github.com/pingcap/tidb/pkg/sessiontxn"
33-
"github.com/pingcap/tidb/pkg/sessiontxn/staleread"
3436
"github.com/pingcap/tidb/pkg/testkit"
35-
"github.com/pingcap/tidb/pkg/types"
37+
"github.com/pingcap/tidb/tests/realtikvtest"
3638
"github.com/stretchr/testify/require"
37-
"github.com/tikv/client-go/v2/oracle"
3839
"github.com/tikv/client-go/v2/tikvrpc"
3940
"github.com/tikv/client-go/v2/tikvrpc/interceptor"
4041
)
4142

43+
func TestTxnScopeAndValidateReadTs(t *testing.T) {
44+
defer config.RestoreFunc()()
45+
config.UpdateGlobal(func(conf *config.Config) {
46+
conf.Labels = map[string]string{
47+
"zone": "bj",
48+
}
49+
})
50+
51+
store := realtikvtest.CreateMockStoreAndSetup(t)
52+
tk := testkit.NewTestKit(t, store)
53+
tk.MustExec("use test")
54+
tk.MustExec("create table t1 (id int primary key);")
55+
time.Sleep(time.Second)
56+
57+
// stale read
58+
tk.MustQuery("select * from t1 AS OF TIMESTAMP NOW() where id = 1;").Check(testkit.Rows())
59+
60+
// replica read
61+
tk.MustExec("set @@tidb_replica_read = 'closest-replicas';")
62+
tk.MustExec("begin")
63+
tk.MustQuery("select * from t1 where id = 1;").Check(testkit.Rows())
64+
tk.MustExec("commit")
65+
66+
tk.MustExec("set @@tidb_replica_read = 'follower';")
67+
tk.MustExec("begin")
68+
tk.MustQuery("select * from t1 where id = 1;").Check(testkit.Rows())
69+
tk.MustExec("commit")
70+
71+
tk.MustExec("set @@tidb_replica_read = 'closest-adaptive';")
72+
tk.MustExec("begin")
73+
tk.MustQuery("select * from t1 where id = 1;").Check(testkit.Rows())
74+
tk.MustExec("commit")
75+
}
76+
4277
func TestExactStalenessTransaction(t *testing.T) {
4378
testcases := []struct {
4479
name string
@@ -79,7 +114,7 @@ func TestExactStalenessTransaction(t *testing.T) {
79114
zone: "",
80115
},
81116
}
82-
store := testkit.CreateMockStore(t)
117+
store := realtikvtest.CreateMockStoreAndSetup(t)
83118
tk := testkit.NewTestKit(t, store)
84119
tk.MustExec("use test")
85120
for _, testcase := range testcases {
@@ -102,17 +137,19 @@ func TestExactStalenessTransaction(t *testing.T) {
102137
}
103138

104139
func TestSelectAsOf(t *testing.T) {
105-
store := testkit.CreateMockStore(t)
140+
store := realtikvtest.CreateMockStoreAndSetup(t)
106141
tk := testkit.NewTestKit(t, store)
107142
tk.MustExec("use test")
108-
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
109-
safePointName := "tikv_gc_safe_point"
110-
safePointValue := "20160102-15:04:05 -0700"
111-
safePointComment := "All versions after safe point can be accessed. (DO NOT EDIT)"
112-
updateSafePoint := fmt.Sprintf(`INSERT INTO mysql.tidb VALUES ('%[1]s', '%[2]s', '%[3]s')
143+
if !*realtikvtest.WithRealTiKV {
144+
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
145+
safePointName := "tikv_gc_safe_point"
146+
safePointValue := "20160102-15:04:05 -0700"
147+
safePointComment := "All versions after safe point can be accessed. (DO NOT EDIT)"
148+
updateSafePoint := fmt.Sprintf(`INSERT INTO mysql.tidb VALUES ('%[1]s', '%[2]s', '%[3]s')
113149
ON DUPLICATE KEY
114150
UPDATE variable_value = '%[2]s', comment = '%[3]s'`, safePointName, safePointValue, safePointComment)
115-
tk.MustExec(updateSafePoint)
151+
tk.MustExec(updateSafePoint)
152+
}
116153
tk.MustExec("drop table if exists t")
117154
tk.MustExec(`drop table if exists b`)
118155
tk.MustExec("create table t (id int primary key);")
@@ -256,7 +293,7 @@ func TestSelectAsOf(t *testing.T) {
256293
}
257294

258295
func TestStaleReadKVRequest(t *testing.T) {
259-
store := testkit.CreateMockStore(t)
296+
store := realtikvtest.CreateMockStoreAndSetup(t)
260297
tk := testkit.NewTestKit(t, store)
261298
safePointName := "tikv_gc_safe_point"
262299
safePointValue := "20160102-15:04:05 -0700"
@@ -350,7 +387,7 @@ func TestStaleReadKVRequest(t *testing.T) {
350387
}
351388

352389
func TestStalenessAndHistoryRead(t *testing.T) {
353-
store := testkit.CreateMockStore(t)
390+
store := realtikvtest.CreateMockStoreAndSetup(t)
354391
tk := testkit.NewTestKit(t, store)
355392
tk.MustExec("use test")
356393
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
@@ -433,7 +470,7 @@ func TestStalenessAndHistoryRead(t *testing.T) {
433470
}
434471

435472
func TestTimeBoundedStalenessTxn(t *testing.T) {
436-
store := testkit.CreateMockStore(t)
473+
store := realtikvtest.CreateMockStoreAndSetup(t)
437474
tk := testkit.NewTestKit(t, store)
438475
tk.MustExec("use test")
439476
tk.MustExec("drop table if exists t")
@@ -498,7 +535,7 @@ func TestTimeBoundedStalenessTxn(t *testing.T) {
498535
}
499536

500537
func TestStalenessTransactionSchemaVer(t *testing.T) {
501-
store := testkit.CreateMockStore(t)
538+
store := realtikvtest.CreateMockStoreAndSetup(t)
502539
tk := testkit.NewTestKit(t, store)
503540
tk.MustExec("use test")
504541
tk.MustExec("drop table if exists t")
@@ -529,7 +566,7 @@ func TestStalenessTransactionSchemaVer(t *testing.T) {
529566
func TestSetTransactionReadOnlyAsOf(t *testing.T) {
530567
t1, err := time.Parse(types.TimeFormat, "2016-09-21 09:53:04")
531568
require.NoError(t, err)
532-
store := testkit.CreateMockStore(t)
569+
store := realtikvtest.CreateMockStoreAndSetup(t)
533570
tk := testkit.NewTestKit(t, store)
534571
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
535572
safePointName := "tikv_gc_safe_point"
@@ -738,7 +775,7 @@ func TestValidateReadOnlyInStalenessTransaction(t *testing.T) {
738775
errMsg: errMsg1,
739776
},
740777
}
741-
store := testkit.CreateMockStore(t)
778+
store := realtikvtest.CreateMockStoreAndSetup(t)
742779
tk := testkit.NewTestKit(t, store)
743780
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
744781
safePointName := "tikv_gc_safe_point"
@@ -779,7 +816,7 @@ func TestValidateReadOnlyInStalenessTransaction(t *testing.T) {
779816
}
780817

781818
func TestSpecialSQLInStalenessTxn(t *testing.T) {
782-
store := testkit.CreateMockStore(t)
819+
store := realtikvtest.CreateMockStoreAndSetup(t)
783820
tk := testkit.NewTestKit(t, store)
784821
tk.MustExec("use test")
785822
testcases := []struct {
@@ -833,7 +870,7 @@ func TestSpecialSQLInStalenessTxn(t *testing.T) {
833870
}
834871

835872
func TestAsOfTimestampCompatibility(t *testing.T) {
836-
store := testkit.CreateMockStore(t)
873+
store := realtikvtest.CreateMockStoreAndSetup(t)
837874
tk := testkit.NewTestKit(t, store)
838875
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
839876
safePointName := "tikv_gc_safe_point"
@@ -889,7 +926,7 @@ func TestAsOfTimestampCompatibility(t *testing.T) {
889926
}
890927

891928
func TestSetTransactionInfoSchema(t *testing.T) {
892-
store := testkit.CreateMockStore(t)
929+
store := realtikvtest.CreateMockStoreAndSetup(t)
893930
tk := testkit.NewTestKit(t, store)
894931
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
895932
safePointName := "tikv_gc_safe_point"
@@ -937,7 +974,7 @@ func testSetTransactionInfoSchema(t *testing.T, tk *testkit.TestKit) {
937974
}
938975

939976
func TestStaleSelect(t *testing.T) {
940-
store := testkit.CreateMockStore(t)
977+
store := realtikvtest.CreateMockStoreAndSetup(t)
941978
tk := testkit.NewTestKit(t, store)
942979
tk.MustExec("use test")
943980
tk.MustExec("drop table if exists t")
@@ -999,7 +1036,7 @@ func TestStaleSelect(t *testing.T) {
9991036
}
10001037

10011038
func TestStaleReadFutureTime(t *testing.T) {
1002-
store := testkit.CreateMockStore(t)
1039+
store := realtikvtest.CreateMockStoreAndSetup(t)
10031040
tk := testkit.NewTestKit(t, store)
10041041
tk.MustExec("use test")
10051042
tk.MustExec("create table t (id int)")
@@ -1019,7 +1056,7 @@ func TestStaleReadFutureTime(t *testing.T) {
10191056
}
10201057

10211058
func TestStaleReadPrepare(t *testing.T) {
1022-
store := testkit.CreateMockStore(t)
1059+
store := realtikvtest.CreateMockStoreAndSetup(t)
10231060
tk := testkit.NewTestKit(t, store)
10241061
tk.MustExec("use test")
10251062
tk.MustExec("drop table if exists t")
@@ -1077,7 +1114,7 @@ func TestStaleReadPrepare(t *testing.T) {
10771114
}
10781115

10791116
func TestStmtCtxStaleFlag(t *testing.T) {
1080-
store := testkit.CreateMockStore(t)
1117+
store := realtikvtest.CreateMockStoreAndSetup(t)
10811118
tk := testkit.NewTestKit(t, store)
10821119
tk.MustExec("use test")
10831120
tk.MustExec("drop table if exists t")
@@ -1171,7 +1208,7 @@ func TestStmtCtxStaleFlag(t *testing.T) {
11711208
}
11721209

11731210
func TestStaleSessionQuery(t *testing.T) {
1174-
store := testkit.CreateMockStore(t)
1211+
store := realtikvtest.CreateMockStoreAndSetup(t)
11751212
tk := testkit.NewTestKit(t, store)
11761213
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
11771214
safePointName := "tikv_gc_safe_point"
@@ -1210,7 +1247,7 @@ func TestStaleSessionQuery(t *testing.T) {
12101247
}
12111248

12121249
func TestStaleReadCompatibility(t *testing.T) {
1213-
store := testkit.CreateMockStore(t)
1250+
store := realtikvtest.CreateMockStoreAndSetup(t)
12141251
tk := testkit.NewTestKit(t, store)
12151252
tk.MustExec("use test")
12161253
tk.MustExec("drop table if exists t")
@@ -1255,7 +1292,7 @@ func TestStaleReadCompatibility(t *testing.T) {
12551292
}
12561293

12571294
func TestStaleReadNoExtraTSORequest(t *testing.T) {
1258-
store := testkit.CreateMockStore(t)
1295+
store := realtikvtest.CreateMockStoreAndSetup(t)
12591296
tk := testkit.NewTestKit(t, store)
12601297
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
12611298
safePointName := "tikv_gc_safe_point"
@@ -1303,7 +1340,7 @@ func TestStaleReadNoExtraTSORequest(t *testing.T) {
13031340
}
13041341

13051342
func TestPlanCacheWithStaleReadByBinaryProto(t *testing.T) {
1306-
store := testkit.CreateMockStore(t)
1343+
store := realtikvtest.CreateMockStoreAndSetup(t)
13071344

13081345
tk := testkit.NewTestKit(t, store)
13091346
tk.MustExec("use test")
@@ -1339,7 +1376,7 @@ func TestPlanCacheWithStaleReadByBinaryProto(t *testing.T) {
13391376
}
13401377

13411378
func TestStalePrepare(t *testing.T) {
1342-
store := testkit.CreateMockStore(t)
1379+
store := realtikvtest.CreateMockStoreAndSetup(t)
13431380
tk := testkit.NewTestKit(t, store)
13441381
tk.MustExec("use test")
13451382
tk.MustExec("drop table if exists t")
@@ -1365,7 +1402,7 @@ func TestStalePrepare(t *testing.T) {
13651402
}
13661403

13671404
func TestStaleTSO(t *testing.T) {
1368-
store := testkit.CreateMockStore(t)
1405+
store := realtikvtest.CreateMockStoreAndSetup(t)
13691406
tk := testkit.NewTestKit(t, store)
13701407
tk.MustExec("use test")
13711408
tk.MustExec("drop table if exists t")
@@ -1411,7 +1448,7 @@ func TestStaleReadNoBackoff(t *testing.T) {
14111448
config.StoreGlobalConfig(cfg)
14121449
require.Equal(t, "us-east-1a", config.GetGlobalConfig().GetTiKVConfig().TxnScope)
14131450

1414-
store := testkit.CreateMockStore(t)
1451+
store := realtikvtest.CreateMockStoreAndSetup(t)
14151452

14161453
tk := testkit.NewTestKit(t, store)
14171454
tk.MustExec("use test")
@@ -1426,7 +1463,6 @@ func TestStaleReadNoBackoff(t *testing.T) {
14261463

14271464
failStaleReadCtx := interceptor.WithRPCInterceptor(context.Background(), interceptor.NewRPCInterceptor("fail-stale-read", func(next interceptor.RPCInterceptorFunc) interceptor.RPCInterceptorFunc {
14281465
return func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
1429-
tikvrpc.AttachContext(req, req.Context)
14301466
if getRequest, ok := req.Req.(*kvrpcpb.GetRequest); ok {
14311467
if ctx := getRequest.GetContext(); ctx != nil && ctx.StaleRead && !ctx.IsRetryRequest {
14321468
return &tikvrpc.Response{Resp: &kvrpcpb.GetResponse{RegionError: &errorpb.Error{

0 commit comments

Comments
 (0)