forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpessimistic_test.go
2842 lines (2538 loc) · 115 KB
/
pessimistic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package session_test
import (
"context"
"fmt"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx/variable"
storeerr "github.com/pingcap/tidb/store/driver/error"
"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/txnkv/transaction"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/deadlockhistory"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
)
var _ = SerialSuites(&testPessimisticSuite{})
func (s *testPessimisticSuite) newAsyncCommitTestKitWithInit(c *C) *testkit.TestKit {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.Se.GetSessionVars().EnableAsyncCommit = true
return tk
}
func (s *testPessimisticSuite) new1PCTestKitWithInit(c *C) *testkit.TestKit {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.Se.GetSessionVars().Enable1PC = true
return tk
}
type testPessimisticSuite struct {
testSessionSuiteBase
}
func (s *testPessimisticSuite) SetUpSuite(c *C) {
s.testSessionSuiteBase.SetUpSuite(c)
// Set it to 300ms for testing lock resolve.
atomic.StoreUint64(&transaction.ManagedLockTTL, 300)
transaction.PrewriteMaxBackoff = 500
}
func (s *testPessimisticSuite) TearDownSuite(c *C) {
s.testSessionSuiteBase.TearDownSuite(c)
transaction.PrewriteMaxBackoff = 20000
}
func (s *testPessimisticSuite) TestPessimisticTxn(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
// Make the name has different indent for easier read.
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists pessimistic")
tk.MustExec("create table pessimistic (k int, v int)")
tk.MustExec("insert into pessimistic values (1, 1)")
// t1 lock, t2 update, t1 update and retry statement.
tk1.MustExec("begin pessimistic")
tk.MustExec("update pessimistic set v = 2 where v = 1")
// Update can see the change, so this statement affects 0 rows.
tk1.MustExec("update pessimistic set v = 3 where v = 1")
c.Assert(tk1.Se.AffectedRows(), Equals, uint64(0))
c.Assert(session.GetHistory(tk1.Se).Count(), Equals, 0)
// select for update can see the change of another transaction.
tk1.MustQuery("select * from pessimistic for update").Check(testkit.Rows("1 2"))
// plain select can not see the change of another transaction.
tk1.MustQuery("select * from pessimistic").Check(testkit.Rows("1 1"))
tk1.MustExec("update pessimistic set v = 3 where v = 2")
c.Assert(tk1.Se.AffectedRows(), Equals, uint64(1))
// pessimistic lock doesn't block read operation of other transactions.
tk.MustQuery("select * from pessimistic").Check(testkit.Rows("1 2"))
tk1.MustExec("commit")
tk1.MustQuery("select * from pessimistic").Check(testkit.Rows("1 3"))
// t1 lock, t1 select for update, t2 wait t1.
tk1.MustExec("begin pessimistic")
tk1.MustExec("select * from pessimistic where k = 1 for update")
finishCh := make(chan struct{})
go func() {
tk.MustExec("update pessimistic set v = 5 where k = 1")
finishCh <- struct{}{}
}()
time.Sleep(time.Millisecond * 10)
tk1.MustExec("update pessimistic set v = 3 where k = 1")
tk1.MustExec("commit")
<-finishCh
tk.MustQuery("select * from pessimistic").Check(testkit.Rows("1 5"))
}
func (s *testPessimisticSuite) TestTxnMode(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tests := []struct {
beginStmt string
txnMode string
isPessimistic bool
}{
{"pessimistic", "pessimistic", true},
{"pessimistic", "optimistic", true},
{"pessimistic", "", true},
{"optimistic", "pessimistic", false},
{"optimistic", "optimistic", false},
{"optimistic", "", false},
{"", "pessimistic", true},
{"", "optimistic", false},
{"", "", false},
}
for _, tt := range tests {
tk.MustExec(fmt.Sprintf("set @@tidb_txn_mode = '%s'", tt.txnMode))
tk.MustExec("begin " + tt.beginStmt)
c.Check(tk.Se.GetSessionVars().TxnCtx.IsPessimistic, Equals, tt.isPessimistic)
tk.MustExec("rollback")
}
tk.MustExec("set @@autocommit = 0")
tk.MustExec("create table if not exists txn_mode (a int)")
tests2 := []struct {
txnMode string
isPessimistic bool
}{
{"pessimistic", true},
{"optimistic", false},
{"", false},
}
for _, tt := range tests2 {
tk.MustExec(fmt.Sprintf("set @@tidb_txn_mode = '%s'", tt.txnMode))
tk.MustExec("rollback")
tk.MustExec("insert txn_mode values (1)")
c.Check(tk.Se.GetSessionVars().TxnCtx.IsPessimistic, Equals, tt.isPessimistic)
tk.MustExec("rollback")
}
tk.MustExec("set @@global.tidb_txn_mode = 'pessimistic'")
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustQuery("select @@tidb_txn_mode").Check(testkit.Rows("pessimistic"))
tk1.MustExec("set @@autocommit = 0")
tk1.MustExec("insert txn_mode values (2)")
c.Check(tk1.Se.GetSessionVars().TxnCtx.IsPessimistic, IsTrue)
tk1.MustExec("set @@tidb_txn_mode = ''")
tk1.MustExec("rollback")
tk1.MustExec("insert txn_mode values (2)")
c.Check(tk1.Se.GetSessionVars().TxnCtx.IsPessimistic, IsFalse)
tk1.MustExec("rollback")
}
func (s *testPessimisticSuite) TestDeadlock(c *C) {
deadlockhistory.GlobalDeadlockHistory.Clear()
deadlockhistory.GlobalDeadlockHistory.Resize(10)
tk1 := testkit.NewTestKitWithInit(c, s.store)
// Use the root user so that the statements can be recorded into statements_summary table, which is necessary
// for fetching
c.Assert(tk1.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil), IsTrue)
tk1.MustExec("drop table if exists deadlock")
tk1.MustExec("create table deadlock (k int primary key, v int)")
tk1.MustExec("insert into deadlock values (1, 1), (2, 1)")
tk1.MustExec("begin pessimistic")
tk1.MustExec("update deadlock set v = v + 1 where k = 1")
ts1, err := strconv.ParseUint(tk1.MustQuery("select @@tidb_current_ts").Rows()[0][0].(string), 10, 64)
c.Assert(err, IsNil)
tk2 := testkit.NewTestKitWithInit(c, s.store)
c.Assert(tk2.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil), IsTrue)
tk2.MustExec("begin pessimistic")
ts2, err := strconv.ParseUint(tk2.MustQuery("select @@tidb_current_ts").Rows()[0][0].(string), 10, 64)
c.Assert(err, IsNil)
syncCh := make(chan error)
go func() {
tk2.MustExec("update deadlock set v = v + 1 where k = 2")
syncCh <- nil
_, err := tk2.Exec("update deadlock set v = v + 1 where k = 1")
syncCh <- err
}()
<-syncCh
_, err1 := tk1.Exec("update deadlock set v = v + 1 where k = 2")
err2 := <-syncCh
// Either err1 or err2 is deadlock error.
if err1 != nil {
c.Assert(err2, IsNil)
err = err1
} else {
err = err2
}
e, ok := errors.Cause(err).(*terror.Error)
c.Assert(ok, IsTrue)
c.Assert(int(e.Code()), Equals, mysql.ErrLockDeadlock)
_, digest := parser.NormalizeDigest("update deadlock set v = v + 1 where k = 1")
expectedDeadlockInfo := []string{
fmt.Sprintf("%v/%v/%v/%v", ts1, ts2, digest, "update `deadlock` set `v` = `v` + ? where `k` = ?"),
fmt.Sprintf("%v/%v/%v/%v", ts2, ts1, digest, "update `deadlock` set `v` = `v` + ? where `k` = ?"),
}
// The last one is the transaction that encountered the deadlock error.
if err1 != nil {
// Swap the two to match the correct order.
expectedDeadlockInfo[0], expectedDeadlockInfo[1] = expectedDeadlockInfo[1], expectedDeadlockInfo[0]
}
res := tk1.MustQuery("select deadlock_id, try_lock_trx_id, trx_holding_lock, current_sql_digest, current_sql_digest_text from information_schema.deadlocks")
res.CheckAt([]int{1, 2, 3, 4}, testutil.RowsWithSep("/", expectedDeadlockInfo...))
c.Assert(res.Rows()[0][0], Equals, res.Rows()[1][0])
}
func (s *testPessimisticSuite) TestSingleStatementRollback(c *C) {
if *withTiKV {
c.Skip("skip with tikv because cluster manipulate is not available")
}
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists pessimistic")
tk.MustExec("create table single_statement (id int primary key, v int)")
tk.MustExec("insert into single_statement values (1, 1), (2, 1), (3, 1), (4, 1)")
tblID := tk.GetTableID("single_statement")
tableStart := tablecodec.GenTableRecordPrefix(tblID)
s.cluster.SplitKeys(tableStart, tableStart.PrefixNext(), 2)
region1Key := codec.EncodeBytes(nil, tablecodec.EncodeRowKeyWithHandle(tblID, kv.IntHandle(1)))
region1, _ := s.cluster.GetRegionByKey(region1Key)
region1ID := region1.Id
region2Key := codec.EncodeBytes(nil, tablecodec.EncodeRowKeyWithHandle(tblID, kv.IntHandle(3)))
region2, _ := s.cluster.GetRegionByKey(region2Key)
region2ID := region2.Id
syncCh := make(chan bool)
c.Assert(failpoint.Enable("tikvclient/SingleStmtDeadLockRetrySleep", "return"), IsNil)
go func() {
tk2.MustExec("begin pessimistic")
<-syncCh
// tk2 will go first, so tk will meet deadlock and retry, tk2 will resolve pessimistic rollback
// lock on key 3 after lock ttl
s.cluster.ScheduleDelay(tk2.Se.GetSessionVars().TxnCtx.StartTS, region2ID, time.Millisecond*3)
tk2.MustExec("update single_statement set v = v + 1")
tk2.MustExec("commit")
<-syncCh
}()
tk.MustExec("begin pessimistic")
syncCh <- true
s.cluster.ScheduleDelay(tk.Se.GetSessionVars().TxnCtx.StartTS, region1ID, time.Millisecond*10)
tk.MustExec("update single_statement set v = v + 1")
tk.MustExec("commit")
c.Assert(failpoint.Disable("tikvclient/SingleStmtDeadLockRetrySleep"), IsNil)
syncCh <- true
}
func (s *testPessimisticSuite) TestFirstStatementFail(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists first")
tk.MustExec("create table first (k int unique)")
tk.MustExec("insert first values (1)")
tk.MustExec("begin pessimistic")
_, err := tk.Exec("insert first values (1)")
c.Assert(err, NotNil)
tk.MustExec("insert first values (2)")
tk.MustExec("commit")
}
func (s *testPessimisticSuite) TestKeyExistsCheck(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists chk")
tk.MustExec("create table chk (k int primary key)")
tk.MustExec("insert chk values (1)")
tk.MustExec("delete from chk where k = 1")
tk.MustExec("begin pessimistic")
tk.MustExec("insert chk values (1)")
tk.MustExec("commit")
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustExec("begin optimistic")
tk1.MustExec("insert chk values (1), (2), (3)")
_, err := tk1.Exec("commit")
c.Assert(err, NotNil)
tk.MustExec("begin pessimistic")
tk.MustExec("insert chk values (2)")
tk.MustExec("commit")
}
func (s *testPessimisticSuite) TestInsertOnDup(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists dup")
tk.MustExec("create table dup (id int primary key, c int)")
tk.MustExec("begin pessimistic")
tk2.MustExec("insert dup values (1, 1)")
tk.MustExec("insert dup values (1, 1) on duplicate key update c = c + 1")
tk.MustExec("commit")
tk.MustQuery("select * from dup").Check(testkit.Rows("1 2"))
}
func (s *testPessimisticSuite) TestPointGetOverflow(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table t(k tinyint, v int, unique key(k))")
tk.MustExec("begin pessimistic")
tk.MustExec("update t set v = 100 where k = -200;")
tk.MustExec("update t set v = 100 where k in (-200, -400);")
}
func (s *testPessimisticSuite) TestPointGetKeyLock(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists point")
tk.MustExec("create table point (id int primary key, u int unique, c int)")
syncCh := make(chan struct{})
tk.MustExec("begin pessimistic")
tk.MustExec("update point set c = c + 1 where id = 1")
tk.MustExec("delete from point where u = 2")
go func() {
tk2.MustExec("begin pessimistic")
_, err1 := tk2.Exec("insert point values (1, 1, 1)")
c.Check(kv.ErrKeyExists.Equal(err1), IsTrue)
_, err1 = tk2.Exec("insert point values (2, 2, 2)")
c.Check(kv.ErrKeyExists.Equal(err1), IsTrue)
tk2.MustExec("rollback")
<-syncCh
}()
time.Sleep(time.Millisecond * 10)
tk.MustExec("insert point values (1, 1, 1)")
tk.MustExec("insert point values (2, 2, 2)")
tk.MustExec("commit")
syncCh <- struct{}{}
tk.MustExec("begin pessimistic")
tk.MustExec("select * from point where id = 3 for update")
tk.MustExec("select * from point where u = 4 for update")
go func() {
tk2.MustExec("begin pessimistic")
_, err1 := tk2.Exec("insert point values (3, 3, 3)")
c.Check(kv.ErrKeyExists.Equal(err1), IsTrue)
_, err1 = tk2.Exec("insert point values (4, 4, 4)")
c.Check(kv.ErrKeyExists.Equal(err1), IsTrue)
tk2.MustExec("rollback")
<-syncCh
}()
time.Sleep(time.Millisecond * 10)
tk.MustExec("insert point values (3, 3, 3)")
tk.MustExec("insert point values (4, 4, 4)")
tk.MustExec("commit")
syncCh <- struct{}{}
}
func (s *testPessimisticSuite) TestBankTransfer(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists accounts")
tk.MustExec("create table accounts (id int primary key, c int)")
tk.MustExec("insert accounts values (1, 100), (2, 100), (3, 100)")
syncCh := make(chan struct{})
tk.MustExec("begin pessimistic")
tk.MustQuery("select * from accounts where id = 1 for update").Check(testkit.Rows("1 100"))
go func() {
tk2.MustExec("begin pessimistic")
tk2.MustExec("select * from accounts where id = 2 for update")
<-syncCh
tk2.MustExec("select * from accounts where id = 3 for update")
tk2.MustExec("update accounts set c = 50 where id = 2")
tk2.MustExec("update accounts set c = 150 where id = 3")
tk2.MustExec("commit")
<-syncCh
}()
syncCh <- struct{}{}
tk.MustQuery("select * from accounts where id = 2 for update").Check(testkit.Rows("2 50"))
tk.MustExec("update accounts set c = 50 where id = 1")
tk.MustExec("update accounts set c = 100 where id = 2")
tk.MustExec("commit")
syncCh <- struct{}{}
tk.MustQuery("select sum(c) from accounts").Check(testkit.Rows("300"))
}
func (s *testPessimisticSuite) TestLockUnchangedRowKey(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists unchanged")
tk.MustExec("create table unchanged (id int primary key, c int)")
tk.MustExec("insert unchanged values (1, 1), (2, 2)")
tk.MustExec("begin pessimistic")
tk.MustExec("update unchanged set c = 1 where id < 2")
tk2.MustExec("begin pessimistic")
err := tk2.ExecToErr("select * from unchanged where id = 1 for update nowait")
c.Assert(err, NotNil)
tk.MustExec("rollback")
tk2.MustQuery("select * from unchanged where id = 1 for update nowait")
tk.MustExec("begin pessimistic")
tk.MustExec("insert unchanged values (2, 2) on duplicate key update c = values(c)")
err = tk2.ExecToErr("select * from unchanged where id = 2 for update nowait")
c.Assert(err, NotNil)
tk.MustExec("commit")
tk2.MustQuery("select * from unchanged where id = 1 for update nowait")
tk2.MustExec("rollback")
}
func (s *testPessimisticSuite) TestOptimisticConflicts(c *C) {
// To avoid the resolve lock request arrives earlier before heartbeat request while lock expires.
atomic.StoreUint64(&transaction.ManagedLockTTL, 1000)
defer func() {
atomic.StoreUint64(&transaction.ManagedLockTTL, 300)
}()
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists conflict")
tk.MustExec("create table conflict (id int primary key, c int)")
tk.MustExec("insert conflict values (1, 1)")
tk.MustExec("begin pessimistic")
tk.MustQuery("select * from conflict where id = 1 for update")
syncCh := make(chan struct{})
go func() {
tk2.MustExec("update conflict set c = 3 where id = 1")
<-syncCh
}()
time.Sleep(time.Millisecond * 10)
tk.MustExec("update conflict set c = 2 where id = 1")
tk.MustExec("commit")
syncCh <- struct{}{}
tk.MustQuery("select c from conflict where id = 1").Check(testkit.Rows("3"))
// Check pessimistic lock is not resolved.
tk.MustExec("begin pessimistic")
tk.MustExec("update conflict set c = 4 where id = 1")
tk2.MustExec("begin optimistic")
tk2.MustExec("update conflict set c = 5 where id = 1")
_, err := tk2.Exec("commit")
c.Check(err, NotNil)
tk.MustExec("rollback")
// Update snapshotTS after a conflict, invalidate snapshot cache.
tk.MustExec("truncate table conflict")
tk.MustExec("insert into conflict values (1, 2)")
tk.MustExec("begin pessimistic")
// This SQL use BatchGet and cache data in the txn snapshot.
// It can be changed to other SQLs that use BatchGet.
tk.MustExec("insert ignore into conflict values (1, 2)")
tk2.MustExec("update conflict set c = c - 1")
// Make the txn update its forUpdateTS.
tk.MustQuery("select * from conflict where id = 1 for update").Check(testkit.Rows("1 1"))
// Cover a bug that the txn snapshot doesn't invalidate cache after ts change.
tk.MustExec("insert into conflict values (1, 999) on duplicate key update c = c + 2")
tk.MustExec("commit")
tk.MustQuery("select * from conflict").Check(testkit.Rows("1 3"))
}
func (s *testPessimisticSuite) TestSelectForUpdateNoWait(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk3 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists tk")
tk.MustExec("create table tk (c1 int primary key, c2 int)")
tk.MustExec("insert into tk values(1,1),(2,2),(3,3),(4,4),(5,5)")
tk.MustExec("set @@autocommit = 0")
tk2.MustExec("set @@autocommit = 0")
tk3.MustExec("set @@autocommit = 0")
// point get with no autocommit
tk.MustExec("begin pessimistic")
tk.MustExec("select * from tk where c1 = 2 for update") // lock succ
tk2.MustExec("begin pessimistic")
_, err := tk2.Exec("select * from tk where c1 = 2 for update nowait")
c.Check(err, NotNil)
tk.MustExec("commit")
tk2.MustExec("select * from tk where c1 = 2 for update nowait") // lock succ
tk3.MustExec("begin pessimistic")
_, err = tk3.Exec("select * from tk where c1 = 2 for update nowait")
c.Check(err, NotNil)
tk2.MustExec("commit")
tk3.MustExec("select * from tk where c1 = 2 for update")
tk3.MustExec("commit")
tk.MustExec("commit")
tk3.MustExec("begin pessimistic")
tk3.MustExec("update tk set c2 = c2 + 1 where c1 = 3")
tk2.MustExec("begin pessimistic")
_, err = tk2.Exec("select * from tk where c1 = 3 for update nowait")
c.Check(err, NotNil)
tk3.MustExec("commit")
tk2.MustExec("select * from tk where c1 = 3 for update nowait")
tk2.MustExec("commit")
tk.MustExec("commit")
tk2.MustExec("commit")
tk3.MustExec("commit")
// scan with no autocommit
tk.MustExec("begin pessimistic")
tk.MustExec("select * from tk where c1 >= 2 for update")
tk2.MustExec("begin pessimistic")
_, err = tk2.Exec("select * from tk where c1 = 2 for update nowait")
c.Check(err, NotNil)
_, err = tk2.Exec("select * from tk where c1 > 3 for update nowait")
c.Check(err, NotNil)
tk2.MustExec("select * from tk where c1 = 1 for update nowait")
tk2.MustExec("commit")
tk.MustQuery("select * from tk where c1 >= 2 for update").Check(testkit.Rows("2 2", "3 4", "4 4", "5 5"))
tk.MustExec("commit")
tk.MustExec("begin pessimistic")
tk.MustExec("update tk set c2 = c2 + 10 where c1 > 3")
tk3.MustExec("begin pessimistic")
_, err = tk3.Exec("select * from tk where c1 = 5 for update nowait")
c.Check(err, NotNil)
tk3.MustExec("select * from tk where c1 = 1 for update nowait")
tk.MustExec("commit")
tk3.MustQuery("select * from tk where c1 > 3 for update nowait").Check(testkit.Rows("4 14", "5 15"))
tk3.MustExec("commit")
// delete
tk3.MustExec("begin pessimistic")
tk3.MustExec("delete from tk where c1 <= 2")
tk.MustExec("begin pessimistic")
_, err = tk.Exec("select * from tk where c1 = 1 for update nowait")
c.Check(err, NotNil)
tk3.MustExec("commit")
tk.MustQuery("select * from tk where c1 > 1 for update nowait").Check(testkit.Rows("3 4", "4 14", "5 15"))
tk.MustExec("update tk set c2 = c2 + 1 where c1 = 5")
tk2.MustExec("begin pessimistic")
_, err = tk2.Exec("select * from tk where c1 = 5 for update nowait")
c.Check(err, NotNil)
tk.MustExec("commit")
tk2.MustQuery("select * from tk where c1 = 5 for update nowait").Check(testkit.Rows("5 16"))
tk2.MustExec("update tk set c2 = c2 + 1 where c1 = 5")
tk2.MustQuery("select * from tk where c1 = 5 for update nowait").Check(testkit.Rows("5 17"))
tk2.MustExec("commit")
}
func (s *testPessimisticSuite) TestAsyncRollBackNoWait(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk3 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists tk")
tk.MustExec("create table tk (c1 int primary key, c2 int)")
tk.MustExec("insert into tk values(1,1),(2,2),(3,3),(4,4),(5,17)")
tk.MustExec("set @@autocommit = 0")
tk2.MustExec("set @@autocommit = 0")
tk3.MustExec("set @@autocommit = 0")
// test get ts failed for handlePessimisticLockError when using nowait
// even though async rollback for pessimistic lock may rollback later locked key if get ts failed from pd
// the txn correctness should be ensured
c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/ExecStmtGetTsError", "return"), IsNil)
c.Assert(failpoint.Enable("tikvclient/beforeAsyncPessimisticRollback", "sleep(100)"), IsNil)
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/ExecStmtGetTsError"), IsNil)
c.Assert(failpoint.Disable("tikvclient/beforeAsyncPessimisticRollback"), IsNil)
}()
tk.MustExec("begin pessimistic")
tk.MustExec("select * from tk where c1 > 0 for update nowait")
tk2.MustExec("begin pessimistic")
// The lock rollback of this statement is delayed by failpoint beforeAsyncPessimisticRollback.
_, err := tk2.Exec("select * from tk where c1 > 0 for update nowait")
c.Check(err, NotNil)
tk.MustExec("commit")
// This statement success for now, but its lock will be rollbacked later by the
// lingering rollback request, as forUpdateTS doesn't change.
tk2.MustQuery("select * from tk where c1 > 0 for update nowait")
tk2.MustQuery("select * from tk where c1 = 5 for update nowait").Check(testkit.Rows("5 17"))
tk3.MustExec("begin pessimistic")
c.Skip("tk3 is blocking because tk2 didn't rollback itself")
// tk3 succ because tk2 rollback itself.
tk3.MustExec("update tk set c2 = 1 where c1 = 5")
// This will not take effect because the lock of tk2 was gone.
tk2.MustExec("update tk set c2 = c2 + 100 where c1 > 0")
_, err = tk2.Exec("commit")
c.Check(err, NotNil) // txn abort because pessimistic lock not found
tk3.MustExec("commit")
tk3.MustExec("begin pessimistic")
tk3.MustQuery("select * from tk where c1 = 5 for update nowait").Check(testkit.Rows("5 1"))
tk3.MustQuery("select * from tk where c1 = 4 for update nowait").Check(testkit.Rows("4 4"))
tk3.MustQuery("select * from tk where c1 = 3 for update nowait").Check(testkit.Rows("3 3"))
tk3.MustExec("commit")
}
func (s *testPessimisticSuite) TestWaitLockKill(c *C) {
// Test kill command works on waiting pessimistic lock.
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists test_kill")
tk.MustExec("create table test_kill (id int primary key, c int)")
tk.MustExec("insert test_kill values (1, 1)")
tk.MustExec("begin pessimistic")
tk2.MustExec("set innodb_lock_wait_timeout = 50")
tk2.MustExec("begin pessimistic")
tk.MustQuery("select * from test_kill where id = 1 for update")
var wg sync.WaitGroup
wg.Add(1)
go func() {
time.Sleep(500 * time.Millisecond)
sessVars := tk2.Se.GetSessionVars()
succ := atomic.CompareAndSwapUint32(&sessVars.Killed, 0, 1)
c.Assert(succ, IsTrue)
wg.Wait()
}()
_, err := tk2.Exec("update test_kill set c = c + 1 where id = 1")
wg.Done()
c.Assert(err, NotNil)
c.Assert(terror.ErrorEqual(err, storeerr.ErrQueryInterrupted), IsTrue)
tk.MustExec("rollback")
}
func (s *testPessimisticSuite) TestKillStopTTLManager(c *C) {
// Test killing an idle pessimistic session stop its ttlManager.
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists test_kill")
tk.MustExec("create table test_kill (id int primary key, c int)")
tk.MustExec("insert test_kill values (1, 1)")
tk.MustExec("begin pessimistic")
tk2.MustExec("begin pessimistic")
tk.MustQuery("select * from test_kill where id = 1 for update")
sessVars := tk.Se.GetSessionVars()
succ := atomic.CompareAndSwapUint32(&sessVars.Killed, 0, 1)
c.Assert(succ, IsTrue)
// This query should success rather than returning a ResolveLock error.
tk2.MustExec("update test_kill set c = c + 1 where id = 1")
}
func (s *testPessimisticSuite) TestConcurrentInsert(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists tk")
tk.MustExec("create table tk (c1 int primary key, c2 int)")
tk.MustExec("insert tk values (1, 1)")
tk.MustExec("create table tk1 (c1 int, c2 int)")
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustExec("begin pessimistic")
tk2 := testkit.NewTestKitWithInit(c, s.store)
forUpdateTsA := tk1.Se.GetSessionVars().TxnCtx.GetForUpdateTS()
tk1.MustQuery("select * from tk where c1 = 1 for update")
forUpdateTsB := tk1.Se.GetSessionVars().TxnCtx.GetForUpdateTS()
c.Assert(forUpdateTsA, Equals, forUpdateTsB)
tk1.MustQuery("select * from tk where c1 > 0 for update")
forUpdateTsC := tk1.Se.GetSessionVars().TxnCtx.GetForUpdateTS()
c.Assert(forUpdateTsC, Greater, forUpdateTsB)
tk2.MustExec("insert tk values (2, 2)")
tk1.MustQuery("select * from tk for update").Check(testkit.Rows("1 1", "2 2"))
tk2.MustExec("insert tk values (3, 3)")
tk1.MustExec("update tk set c2 = c2 + 1")
c.Assert(tk1.Se.AffectedRows(), Equals, uint64(3))
tk2.MustExec("insert tk values (4, 4)")
tk1.MustExec("delete from tk")
c.Assert(tk1.Se.AffectedRows(), Equals, uint64(4))
tk2.MustExec("insert tk values (5, 5)")
tk1.MustExec("insert into tk1 select * from tk")
c.Assert(tk1.Se.AffectedRows(), Equals, uint64(1))
tk2.MustExec("insert tk values (6, 6)")
tk1.MustExec("replace into tk1 select * from tk")
c.Assert(tk1.Se.AffectedRows(), Equals, uint64(2))
tk2.MustExec("insert tk values (7, 7)")
// This test is used to test when the selectPlan is a PointGetPlan, and we didn't update its forUpdateTS.
tk1.MustExec("insert into tk1 select * from tk where c1 = 7")
c.Assert(tk1.Se.AffectedRows(), Equals, uint64(1))
tk1.MustExec("commit")
}
func (s *testPessimisticSuite) TestInnodbLockWaitTimeout(c *C) {
// Increasing the ManagedLockTTL so that the lock may not be resolved testing with TiKV.
atomic.StoreUint64(&transaction.ManagedLockTTL, 5000)
defer func() {
atomic.StoreUint64(&transaction.ManagedLockTTL, 300)
}()
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists tk")
tk.MustExec("create table tk (c1 int primary key, c2 int)")
tk.MustExec("insert into tk values(1,1),(2,2),(3,3),(4,4),(5,5)")
// tk set global
tk.MustExec("set global innodb_lock_wait_timeout = 3")
tk.MustQuery(`show variables like "innodb_lock_wait_timeout"`).Check(testkit.Rows("innodb_lock_wait_timeout 50"))
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk2.MustQuery(`show variables like "innodb_lock_wait_timeout"`).Check(testkit.Rows("innodb_lock_wait_timeout 3"))
tk2.MustExec("set innodb_lock_wait_timeout = 2")
tk2.MustQuery(`show variables like "innodb_lock_wait_timeout"`).Check(testkit.Rows("innodb_lock_wait_timeout 2"))
tk3 := testkit.NewTestKitWithInit(c, s.store)
tk3.MustQuery(`show variables like "innodb_lock_wait_timeout"`).Check(testkit.Rows("innodb_lock_wait_timeout 3"))
tk3.MustExec("set innodb_lock_wait_timeout = 1")
tk3.MustQuery(`show variables like "innodb_lock_wait_timeout"`).Check(testkit.Rows("innodb_lock_wait_timeout 1"))
tk2.MustExec("set @@autocommit = 0")
tk3.MustExec("set @@autocommit = 0")
tk4 := testkit.NewTestKitWithInit(c, s.store)
tk4.MustQuery(`show variables like "innodb_lock_wait_timeout"`).Check(testkit.Rows("innodb_lock_wait_timeout 3"))
tk4.MustExec("set @@autocommit = 0")
// tk2 lock c1 = 1
tk2.MustExec("begin pessimistic")
tk2.MustExec("select * from tk where c1 = 1 for update") // lock succ c1 = 1
// Parallel the blocking tests to accelerate CI.
var wg sync.WaitGroup
wg.Add(2)
timeoutErrCh := make(chan error, 2)
go func() {
defer wg.Done()
// tk3 try lock c1 = 1 timeout 1sec
tk3.MustExec("begin pessimistic")
_, err := tk3.Exec("select * from tk where c1 = 1 for update")
timeoutErrCh <- err
tk3.MustExec("commit")
}()
go func() {
defer wg.Done()
// tk5 try lock c1 = 1 timeout 2sec
tk5 := testkit.NewTestKitWithInit(c, s.store)
tk5.MustExec("set innodb_lock_wait_timeout = 2")
tk5.MustExec("begin pessimistic")
_, err := tk5.Exec("update tk set c2 = c2 - 1 where c1 = 1")
timeoutErrCh <- err
tk5.MustExec("rollback")
}()
timeoutErr := <-timeoutErrCh
c.Assert(timeoutErr, NotNil)
c.Assert(timeoutErr.Error(), Equals, storeerr.ErrLockWaitTimeout.Error())
timeoutErr = <-timeoutErrCh
c.Assert(timeoutErr, NotNil)
c.Assert(timeoutErr.Error(), Equals, storeerr.ErrLockWaitTimeout.Error())
// tk4 lock c1 = 2
tk4.MustExec("begin pessimistic")
tk4.MustExec("update tk set c2 = c2 + 1 where c1 = 2") // lock succ c1 = 2 by update
tk2.MustExec("set innodb_lock_wait_timeout = 1")
tk2.MustQuery(`show variables like "innodb_lock_wait_timeout"`).Check(testkit.Rows("innodb_lock_wait_timeout 1"))
start := time.Now()
_, err := tk2.Exec("delete from tk where c1 = 2")
c.Check(time.Since(start), GreaterEqual, 1000*time.Millisecond)
c.Check(time.Since(start), Less, 3000*time.Millisecond) // unit test diff should not be too big
c.Check(err.Error(), Equals, storeerr.ErrLockWaitTimeout.Error())
tk4.MustExec("commit")
tk.MustQuery(`show variables like "innodb_lock_wait_timeout"`).Check(testkit.Rows("innodb_lock_wait_timeout 50"))
tk.MustQuery(`select * from tk where c1 = 2`).Check(testkit.Rows("2 3")) // tk4 update commit work, tk2 delete should be rollbacked
// test stmtRollBack caused by timeout but not the whole transaction
tk2.MustExec("update tk set c2 = c2 + 2 where c1 = 2") // tk2 lock succ c1 = 2 by update
tk2.MustQuery(`select * from tk where c1 = 2`).Check(testkit.Rows("2 5")) // tk2 update c2 succ
tk3.MustExec("begin pessimistic")
tk3.MustExec("select * from tk where c1 = 3 for update") // tk3 lock c1 = 3 succ
start = time.Now()
_, err = tk2.Exec("delete from tk where c1 = 3") // tk2 tries to lock c1 = 3 fail, this delete should be rollback, but previous update should be keeped
c.Check(time.Since(start), GreaterEqual, 1000*time.Millisecond)
c.Check(time.Since(start), Less, 3000*time.Millisecond) // unit test diff should not be too big
c.Check(err.Error(), Equals, storeerr.ErrLockWaitTimeout.Error())
tk2.MustExec("commit")
tk3.MustExec("commit")
tk.MustQuery(`select * from tk where c1 = 1`).Check(testkit.Rows("1 1"))
tk.MustQuery(`select * from tk where c1 = 2`).Check(testkit.Rows("2 5")) // tk2 update succ
tk.MustQuery(`select * from tk where c1 = 3`).Check(testkit.Rows("3 3")) // tk2 delete should fail
tk.MustQuery(`select * from tk where c1 = 4`).Check(testkit.Rows("4 4"))
tk.MustQuery(`select * from tk where c1 = 5`).Check(testkit.Rows("5 5"))
// clean
tk.MustExec("drop table if exists tk")
tk4.MustExec("commit")
wg.Wait()
}
func (s *testPessimisticSuite) TestPushConditionCheckForPessimisticTxn(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk1 := testkit.NewTestKitWithInit(c, s.store)
defer tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (i int key)")
tk.MustExec("insert into t values (1)")
tk.MustExec("set tidb_txn_mode = 'pessimistic'")
tk.MustExec("begin")
tk1.MustExec("delete from t where i = 1")
tk.MustExec("insert into t values (1) on duplicate key update i = values(i)")
tk.MustExec("commit")
tk.MustQuery("select * from t").Check(testkit.Rows("1"))
}
func (s *testPessimisticSuite) TestInnodbLockWaitTimeoutWaitStart(c *C) {
// Increasing the ManagedLockTTL so that the lock may not be resolved testing with TiKV.
atomic.StoreUint64(&transaction.ManagedLockTTL, 5000)
defer func() {
atomic.StoreUint64(&transaction.ManagedLockTTL, 300)
}()
// prepare work
tk := testkit.NewTestKitWithInit(c, s.store)
defer tk.MustExec("drop table if exists tk")
tk.MustExec("drop table if exists tk")
tk.MustExec("create table tk (c1 int primary key, c2 int)")
tk.MustExec("insert into tk values(1,1),(2,2),(3,3),(4,4),(5,5)")
tk.MustExec("set global innodb_lock_wait_timeout = 1")
// raise pessimistic transaction in tk2 and trigger failpoint returning ErrWriteConflict
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk3 := testkit.NewTestKitWithInit(c, s.store)
tk2.MustQuery(`show variables like "innodb_lock_wait_timeout"`).Check(testkit.Rows("innodb_lock_wait_timeout 1"))
// tk3 gets the pessimistic lock
tk3.MustExec("begin pessimistic")
tk3.MustQuery("select * from tk where c1 = 1 for update")
tk2.MustExec("begin pessimistic")
done := make(chan error)
c.Assert(failpoint.Enable("tikvclient/PessimisticLockErrWriteConflict", "return"), IsNil)
var duration time.Duration
go func() {
var err error
start := time.Now()
defer func() {
duration = time.Since(start)
done <- err
}()
_, err = tk2.Exec("select * from tk where c1 = 1 for update")
}()
time.Sleep(time.Millisecond * 100)
c.Assert(failpoint.Disable("tikvclient/PessimisticLockErrWriteConflict"), IsNil)
waitErr := <-done
c.Assert(waitErr, NotNil)
c.Check(waitErr.Error(), Equals, storeerr.ErrLockWaitTimeout.Error())
c.Check(duration, GreaterEqual, 1000*time.Millisecond)
c.Check(duration, LessEqual, 3000*time.Millisecond)
tk2.MustExec("rollback")
tk3.MustExec("commit")
}
func (s *testPessimisticSuite) TestBatchPointGetWriteConflict(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("use test")
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (i int primary key, c int);")
tk.MustExec("insert t values (1, 1), (2, 2), (3, 3)")
tk1.MustExec("begin pessimistic")
tk1.MustQuery("select * from t where i = 1").Check(testkit.Rows("1 1"))
tk.MustExec("update t set c = c + 1")
tk1.MustQuery("select * from t where i in (1, 3) for update").Check(testkit.Rows("1 2", "3 4"))
tk1.MustExec("commit")
}
func (s *testPessimisticSuite) TestPessimisticSerializable(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("use test")
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustExec("use test")
tk.MustExec("set tidb_txn_mode = 'pessimistic'")
tk1.MustExec("set tidb_txn_mode = 'pessimistic'")
tk.MustExec("drop table if exists test;")
tk.MustExec("create table test (id int not null primary key, value int);")
tk.MustExec("insert into test (id, value) values (1, 10);")
tk.MustExec("insert into test (id, value) values (2, 20);")
tk.MustExec("set tidb_skip_isolation_level_check = 1")
tk1.MustExec("set tidb_skip_isolation_level_check = 1")
tk.MustExec("set tx_isolation = 'SERIALIZABLE'")
tk1.MustExec("set tx_isolation = 'SERIALIZABLE'")
// Predicate-Many-Preceders (PMP)
tk.MustExec("begin")
tk1.MustExec("begin")
tk.MustQuery("select * from test where value = 30;").Check(testkit.Rows())
tk1.MustExec("insert into test (id, value) values(3, 30);")
tk1.MustExec("commit")
tk.MustQuery("select * from test where mod(value, 3) = 0;").Check(testkit.Rows())
tk.MustExec("commit")
tk.MustExec("truncate table test;")
tk.MustExec("insert into test (id, value) values (1, 10);")
tk.MustExec("insert into test (id, value) values (2, 20);")
tk.MustExec("begin;")
tk1.MustExec("begin;")
tk.MustExec("update test set value = value + 10;")
var wg sync.WaitGroup
wg.Add(1)
go func() {
tk1.ExecToErr("delete from test where value = 20;")
wg.Done()
}()
tk.MustExec("commit;")
wg.Wait()
tk1.MustExec("rollback;")
// Lost Update (P4)
tk.MustExec("truncate table test;")
tk.MustExec("insert into test (id, value) values (1, 10);")
tk.MustExec("insert into test (id, value) values (2, 20);")
tk.MustExec("begin;")
tk1.MustExec("begin;")
tk.MustQuery("select * from test where id = 1;").Check(testkit.Rows("1 10"))
tk1.MustQuery("select * from test where id = 1;").Check(testkit.Rows("1 10"))
tk.MustExec("update test set value = 11 where id = 1;")
wg.Add(1)
go func() {
tk1.ExecToErr("update test set value = 11 where id = 1;")
wg.Done()
}()
tk.MustExec("commit;")
wg.Wait()
tk1.MustExec("rollback;")
// Read Skew (G-single)
tk.MustExec("truncate table test;")
tk.MustExec("insert into test (id, value) values (1, 10);")
tk.MustExec("insert into test (id, value) values (2, 20);")
tk.MustExec("begin;")
tk1.MustExec("begin;")
tk.MustQuery("select * from test where id = 1;").Check(testkit.Rows("1 10"))
tk1.MustQuery("select * from test where id = 1;").Check(testkit.Rows("1 10"))
tk1.MustQuery("select * from test where id = 2;").Check(testkit.Rows("2 20"))
tk1.MustExec("update test set value = 12 where id = 1;")
tk1.MustExec("update test set value = 18 where id = 1;")
tk1.MustExec("commit;")
tk.MustQuery("select * from test where id = 2;").Check(testkit.Rows("2 20"))
tk.MustExec("commit;")
tk.MustExec("truncate table test;")
tk.MustExec("insert into test (id, value) values (1, 10);")
tk.MustExec("insert into test (id, value) values (2, 20);")
tk.MustExec("begin;")
tk1.MustExec("begin;")
tk.MustQuery("select * from test where mod(value, 5) = 0;").Check(testkit.Rows("1 10", "2 20"))
tk1.MustExec("update test set value = 12 where value = 10;")
tk1.MustExec("commit;")
tk.MustQuery("select * from test where mod(value, 3) = 0;").Check(testkit.Rows())
tk.MustExec("commit;")
tk.MustExec("truncate table test;")
tk.MustExec("insert into test (id, value) values (1, 10);")
tk.MustExec("insert into test (id, value) values (2, 20);")
tk.MustExec("begin;")
tk1.MustExec("begin;")
tk.MustQuery("select * from test where id = 1;").Check(testkit.Rows("1 10"))
tk1.MustQuery("select * from test;").Check(testkit.Rows("1 10", "2 20"))