1
- // Copyright 2021 PingCAP, Inc.
1
+ // Copyright 2025 PingCAP, Inc.
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- package executor_test
15
+ package txntest
16
16
17
17
import (
18
18
"bytes"
19
19
"context"
20
20
"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"
21
29
"strconv"
22
30
"testing"
23
31
"time"
24
32
25
- "github.com/docker/go-units"
26
- "github.com/pingcap/errors"
27
- "github.com/pingcap/failpoint"
28
33
"github.com/pingcap/kvproto/pkg/errorpb"
29
34
"github.com/pingcap/kvproto/pkg/kvrpcpb"
30
35
"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"
34
36
"github.com/pingcap/tidb/pkg/testkit"
35
- "github.com/pingcap/tidb/pkg/types "
37
+ "github.com/pingcap/tidb/tests/realtikvtest "
36
38
"github.com/stretchr/testify/require"
37
- "github.com/tikv/client-go/v2/oracle"
38
39
"github.com/tikv/client-go/v2/tikvrpc"
39
40
"github.com/tikv/client-go/v2/tikvrpc/interceptor"
40
41
)
41
42
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
+
42
77
func TestExactStalenessTransaction (t * testing.T ) {
43
78
testcases := []struct {
44
79
name string
@@ -79,7 +114,7 @@ func TestExactStalenessTransaction(t *testing.T) {
79
114
zone : "" ,
80
115
},
81
116
}
82
- store := testkit . CreateMockStore (t )
117
+ store := realtikvtest . CreateMockStoreAndSetup (t )
83
118
tk := testkit .NewTestKit (t , store )
84
119
tk .MustExec ("use test" )
85
120
for _ , testcase := range testcases {
@@ -102,17 +137,19 @@ func TestExactStalenessTransaction(t *testing.T) {
102
137
}
103
138
104
139
func TestSelectAsOf (t * testing.T ) {
105
- store := testkit . CreateMockStore (t )
140
+ store := realtikvtest . CreateMockStoreAndSetup (t )
106
141
tk := testkit .NewTestKit (t , store )
107
142
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')
113
149
ON DUPLICATE KEY
114
150
UPDATE variable_value = '%[2]s', comment = '%[3]s'` , safePointName , safePointValue , safePointComment )
115
- tk .MustExec (updateSafePoint )
151
+ tk .MustExec (updateSafePoint )
152
+ }
116
153
tk .MustExec ("drop table if exists t" )
117
154
tk .MustExec (`drop table if exists b` )
118
155
tk .MustExec ("create table t (id int primary key);" )
@@ -256,7 +293,7 @@ func TestSelectAsOf(t *testing.T) {
256
293
}
257
294
258
295
func TestStaleReadKVRequest (t * testing.T ) {
259
- store := testkit . CreateMockStore (t )
296
+ store := realtikvtest . CreateMockStoreAndSetup (t )
260
297
tk := testkit .NewTestKit (t , store )
261
298
safePointName := "tikv_gc_safe_point"
262
299
safePointValue := "20160102-15:04:05 -0700"
@@ -350,7 +387,7 @@ func TestStaleReadKVRequest(t *testing.T) {
350
387
}
351
388
352
389
func TestStalenessAndHistoryRead (t * testing.T ) {
353
- store := testkit . CreateMockStore (t )
390
+ store := realtikvtest . CreateMockStoreAndSetup (t )
354
391
tk := testkit .NewTestKit (t , store )
355
392
tk .MustExec ("use test" )
356
393
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
@@ -433,7 +470,7 @@ func TestStalenessAndHistoryRead(t *testing.T) {
433
470
}
434
471
435
472
func TestTimeBoundedStalenessTxn (t * testing.T ) {
436
- store := testkit . CreateMockStore (t )
473
+ store := realtikvtest . CreateMockStoreAndSetup (t )
437
474
tk := testkit .NewTestKit (t , store )
438
475
tk .MustExec ("use test" )
439
476
tk .MustExec ("drop table if exists t" )
@@ -498,7 +535,7 @@ func TestTimeBoundedStalenessTxn(t *testing.T) {
498
535
}
499
536
500
537
func TestStalenessTransactionSchemaVer (t * testing.T ) {
501
- store := testkit . CreateMockStore (t )
538
+ store := realtikvtest . CreateMockStoreAndSetup (t )
502
539
tk := testkit .NewTestKit (t , store )
503
540
tk .MustExec ("use test" )
504
541
tk .MustExec ("drop table if exists t" )
@@ -529,7 +566,7 @@ func TestStalenessTransactionSchemaVer(t *testing.T) {
529
566
func TestSetTransactionReadOnlyAsOf (t * testing.T ) {
530
567
t1 , err := time .Parse (types .TimeFormat , "2016-09-21 09:53:04" )
531
568
require .NoError (t , err )
532
- store := testkit . CreateMockStore (t )
569
+ store := realtikvtest . CreateMockStoreAndSetup (t )
533
570
tk := testkit .NewTestKit (t , store )
534
571
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
535
572
safePointName := "tikv_gc_safe_point"
@@ -738,7 +775,7 @@ func TestValidateReadOnlyInStalenessTransaction(t *testing.T) {
738
775
errMsg : errMsg1 ,
739
776
},
740
777
}
741
- store := testkit . CreateMockStore (t )
778
+ store := realtikvtest . CreateMockStoreAndSetup (t )
742
779
tk := testkit .NewTestKit (t , store )
743
780
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
744
781
safePointName := "tikv_gc_safe_point"
@@ -779,7 +816,7 @@ func TestValidateReadOnlyInStalenessTransaction(t *testing.T) {
779
816
}
780
817
781
818
func TestSpecialSQLInStalenessTxn (t * testing.T ) {
782
- store := testkit . CreateMockStore (t )
819
+ store := realtikvtest . CreateMockStoreAndSetup (t )
783
820
tk := testkit .NewTestKit (t , store )
784
821
tk .MustExec ("use test" )
785
822
testcases := []struct {
@@ -833,7 +870,7 @@ func TestSpecialSQLInStalenessTxn(t *testing.T) {
833
870
}
834
871
835
872
func TestAsOfTimestampCompatibility (t * testing.T ) {
836
- store := testkit . CreateMockStore (t )
873
+ store := realtikvtest . CreateMockStoreAndSetup (t )
837
874
tk := testkit .NewTestKit (t , store )
838
875
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
839
876
safePointName := "tikv_gc_safe_point"
@@ -889,7 +926,7 @@ func TestAsOfTimestampCompatibility(t *testing.T) {
889
926
}
890
927
891
928
func TestSetTransactionInfoSchema (t * testing.T ) {
892
- store := testkit . CreateMockStore (t )
929
+ store := realtikvtest . CreateMockStoreAndSetup (t )
893
930
tk := testkit .NewTestKit (t , store )
894
931
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
895
932
safePointName := "tikv_gc_safe_point"
@@ -937,7 +974,7 @@ func testSetTransactionInfoSchema(t *testing.T, tk *testkit.TestKit) {
937
974
}
938
975
939
976
func TestStaleSelect (t * testing.T ) {
940
- store := testkit . CreateMockStore (t )
977
+ store := realtikvtest . CreateMockStoreAndSetup (t )
941
978
tk := testkit .NewTestKit (t , store )
942
979
tk .MustExec ("use test" )
943
980
tk .MustExec ("drop table if exists t" )
@@ -999,7 +1036,7 @@ func TestStaleSelect(t *testing.T) {
999
1036
}
1000
1037
1001
1038
func TestStaleReadFutureTime (t * testing.T ) {
1002
- store := testkit . CreateMockStore (t )
1039
+ store := realtikvtest . CreateMockStoreAndSetup (t )
1003
1040
tk := testkit .NewTestKit (t , store )
1004
1041
tk .MustExec ("use test" )
1005
1042
tk .MustExec ("create table t (id int)" )
@@ -1019,7 +1056,7 @@ func TestStaleReadFutureTime(t *testing.T) {
1019
1056
}
1020
1057
1021
1058
func TestStaleReadPrepare (t * testing.T ) {
1022
- store := testkit . CreateMockStore (t )
1059
+ store := realtikvtest . CreateMockStoreAndSetup (t )
1023
1060
tk := testkit .NewTestKit (t , store )
1024
1061
tk .MustExec ("use test" )
1025
1062
tk .MustExec ("drop table if exists t" )
@@ -1077,7 +1114,7 @@ func TestStaleReadPrepare(t *testing.T) {
1077
1114
}
1078
1115
1079
1116
func TestStmtCtxStaleFlag (t * testing.T ) {
1080
- store := testkit . CreateMockStore (t )
1117
+ store := realtikvtest . CreateMockStoreAndSetup (t )
1081
1118
tk := testkit .NewTestKit (t , store )
1082
1119
tk .MustExec ("use test" )
1083
1120
tk .MustExec ("drop table if exists t" )
@@ -1171,7 +1208,7 @@ func TestStmtCtxStaleFlag(t *testing.T) {
1171
1208
}
1172
1209
1173
1210
func TestStaleSessionQuery (t * testing.T ) {
1174
- store := testkit . CreateMockStore (t )
1211
+ store := realtikvtest . CreateMockStoreAndSetup (t )
1175
1212
tk := testkit .NewTestKit (t , store )
1176
1213
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
1177
1214
safePointName := "tikv_gc_safe_point"
@@ -1210,7 +1247,7 @@ func TestStaleSessionQuery(t *testing.T) {
1210
1247
}
1211
1248
1212
1249
func TestStaleReadCompatibility (t * testing.T ) {
1213
- store := testkit . CreateMockStore (t )
1250
+ store := realtikvtest . CreateMockStoreAndSetup (t )
1214
1251
tk := testkit .NewTestKit (t , store )
1215
1252
tk .MustExec ("use test" )
1216
1253
tk .MustExec ("drop table if exists t" )
@@ -1255,7 +1292,7 @@ func TestStaleReadCompatibility(t *testing.T) {
1255
1292
}
1256
1293
1257
1294
func TestStaleReadNoExtraTSORequest (t * testing.T ) {
1258
- store := testkit . CreateMockStore (t )
1295
+ store := realtikvtest . CreateMockStoreAndSetup (t )
1259
1296
tk := testkit .NewTestKit (t , store )
1260
1297
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
1261
1298
safePointName := "tikv_gc_safe_point"
@@ -1303,7 +1340,7 @@ func TestStaleReadNoExtraTSORequest(t *testing.T) {
1303
1340
}
1304
1341
1305
1342
func TestPlanCacheWithStaleReadByBinaryProto (t * testing.T ) {
1306
- store := testkit . CreateMockStore (t )
1343
+ store := realtikvtest . CreateMockStoreAndSetup (t )
1307
1344
1308
1345
tk := testkit .NewTestKit (t , store )
1309
1346
tk .MustExec ("use test" )
@@ -1339,7 +1376,7 @@ func TestPlanCacheWithStaleReadByBinaryProto(t *testing.T) {
1339
1376
}
1340
1377
1341
1378
func TestStalePrepare (t * testing.T ) {
1342
- store := testkit . CreateMockStore (t )
1379
+ store := realtikvtest . CreateMockStoreAndSetup (t )
1343
1380
tk := testkit .NewTestKit (t , store )
1344
1381
tk .MustExec ("use test" )
1345
1382
tk .MustExec ("drop table if exists t" )
@@ -1365,7 +1402,7 @@ func TestStalePrepare(t *testing.T) {
1365
1402
}
1366
1403
1367
1404
func TestStaleTSO (t * testing.T ) {
1368
- store := testkit . CreateMockStore (t )
1405
+ store := realtikvtest . CreateMockStoreAndSetup (t )
1369
1406
tk := testkit .NewTestKit (t , store )
1370
1407
tk .MustExec ("use test" )
1371
1408
tk .MustExec ("drop table if exists t" )
@@ -1411,7 +1448,7 @@ func TestStaleReadNoBackoff(t *testing.T) {
1411
1448
config .StoreGlobalConfig (cfg )
1412
1449
require .Equal (t , "us-east-1a" , config .GetGlobalConfig ().GetTiKVConfig ().TxnScope )
1413
1450
1414
- store := testkit . CreateMockStore (t )
1451
+ store := realtikvtest . CreateMockStoreAndSetup (t )
1415
1452
1416
1453
tk := testkit .NewTestKit (t , store )
1417
1454
tk .MustExec ("use test" )
0 commit comments