File: lock_impl_to_expl.test

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (1050 lines) | stat: -rw-r--r-- 30,083 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
--source include/have_debug_sync.inc

--echo # Bug #27491839  INNODB: ASSERTION FAILURE:
--echo #                LOCK0LOCK.CC:NNN:!LOCK_REC_OTHER_TRX_HOLDS_EXPL( LOCK

# Save the original settings, to be restored at the end of test
  SET @innodb_lock_wait_timeout_saved = @@global.innodb_lock_wait_timeout;

# Make sure that transactions will not finish prematurely
  SET @@global.innodb_lock_wait_timeout = 100000;

--echo # Scenario 1

--source suite/innodb/include/prepare_secondary_index_on_virtual.inc

# This scenario reproduces the original problem, which was that a transaction
# was considered to hold implicit lock on a secondary index entry, even if
# it has not modified any column affecting this secondary index.
# 1. C1 obtains S lock on secondary index only for id=1
# 2. C2 modifies row with id=1, by changing c2, which does not affect
#    secondary index, and thus it should not be considered to hold an
#    implicit lock on secondary index. Also, this does not require C2
#    to pay any attention to C1's lock on secondary index.
# 3. C1 tries to get X lock on the same row, which as one of the steps
#    checks if C2 has implicit lock (and the bug causes it to believe
#    that C2 holds it) and if so, then if there is some trx holding an
#    explicit lock (and it turns out that C1 already has the S lock).

--connect (C1, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE v1=1 FOR SHARE;

--connect (C2, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=1;

--connection C1
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE v1=1 FOR UPDATE;

--connection C2
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  ROLLBACK;

--connection C1
  --reap
  ROLLBACK;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc


--echo # Scenario 2

--source suite/innodb/include/prepare_secondary_index_on_virtual.inc

# This scenario is dual to Scenario 1 in some sense, as it deals with delete
# marked entries in secondary index.
# It is similar to Scenario 1 in that a bad implementation could wrongly
# conclude that modifying an unrelated field makes the transaction an implicit
# lock owner.
# This scenario proves that the bug in the original code was not simply in
# negating the result of row_vers_non_vc_index_entry_match.
# 1. `default` modifies row with id=1, by setting c1 to 55,
#    so that that entry with v1=1 becomes delete marked
# 2. C1 obtains S lock on secondary index only for v1=1
# 3. C2 modifies row with id=1, by changing c2, which does not affect
#    secondary index, and thus it should not be considered to hold an
#    implicit lock on secondary index. Also, this does not require C2
#    to pay any attention to C1's lock on secondary index.
# 4. C1 tries to get X lock on the same row, which as one of the steps
#    checks if C2 has implicit lock (and the bug causes it to believe
#    that C2 holds it) and if so, then if there is some trx holding an
#    explicit lock (and it turns out that C1 already has the S lock).

  UPDATE t1 SET c1=55 WHERE id=1;

--connect (C1, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE v1=1 FOR SHARE;

--connect (C2, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=1;

--connection C1
  SELECT 1 FROM t1 WHERE v1=1 FOR UPDATE;
  ROLLBACK;

--connection C2
  ROLLBACK;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 3

--source suite/innodb/include/prepare_secondary_index_on_virtual.inc

# This is not a very difficult scenario, but one needed to verify that a
# DELETE operation is logged to undo log without UPD_NODE_NO_ORD_CHANGE flag
# and thus virtual column fields can be retrieved from undo log.
# This is one of the core assumptions for the fix to this bug.
# 1. C1 DELETEs record with id=1, and thus should hold implicit lock
#       on secondary index for v1=1.
# 2. C2 performs a SELECT 1 FROM t1 WHERE v1=1 FOR SHARE, and should wait
# 3. C1 commits
# 4. C2 should see empty result

--connect (C1, localhost, root,,)
  BEGIN;
  DELETE FROM t1 WHERE id=1;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE v1=1 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4

# In this scenario we check the last possible path for missing virtual
# columns information: the one where secondary row is not delete marked,
# and previous version of primary index row is also not delete marked in
# which case we should withold with deciding if there is an implicit lock
# until we check trx_id.
# There are many subscenarios.

--echo # Scenario 4a

--source suite/innodb/include/prepare_secondary_index_on_virtual.inc

# In 4a we have a case where the previous version is made by active trx,
# and there are no older versions, because it started with INSERT.
# In such case it should own implicit lock.
# 1. C1 INSERTS new row with id=2 v1=2
# 2. C1 UPDATES unrelated column c2, so that previous_version is INSERT
# 3. C2 does SELECT 1 FROM t1 WHERE v1=2 FOR SHARE and has to wait
# 4. C1 commits
# 5. C2 sees the new row

--connect (C1, localhost, root,,)
  BEGIN;
  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE v1=2 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap


--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4b

--source suite/innodb/include/prepare_secondary_index_on_virtual.inc

# In 4b, again the previous version is made by active trx, and there exists
# an even older version, which was not made by active trx and also matches
# so in this case there is no implicit lock.
# 1. default INSERTs id=1,v1=2
# 2. C1 UPDATEs unrelated field c2
# 3. C1 UPDATEs unrelated field c2 again
# 4. C2 does SELECT 1 FROM t1 WHERE v1=2 FOR SHARE and does not have to wait
# 5. C2 COMMITS
# 6. C1 COMMITS

  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);

--connect (C1, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=2;
  UPDATE t1 SET c2=42 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE v1=2 FOR SHARE;
  COMMIT;

--connection C1
  COMMIT;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4c

--source suite/innodb/include/prepare_secondary_index_on_virtual.inc

# In 4c, the previous version is made by active trx, there is an older version,
# also made by trx_id but does not match the secondary index, thus
# there is an implicit lock
# 1. C1 INSERTs id=2 v1=2
# 2. C1 UPDATEs c1 (and thus v1) to 10 for id=2
# 3. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE v1=10 FOR SHARE and has to wait
# 5. C1 COMMITS
# 6. C2 sees the new row

--connect (C1, localhost, root,,)
  BEGIN;
  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);
  UPDATE t1 SET c1=10 WHERE id=2;
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE v1=10 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4d

--source suite/innodb/include/prepare_secondary_index_on_virtual.inc

# In 4d, the previous version is made by active trx, there is an older version,
# which was not made by trx_id, and does not match secondary index, thus
# there is an implicit lock.
# 1. default INSERTs id=2 v1=2
# 2. C1 UPDATEs c1 (and thus v1) to 10 for id=2
# 3. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE v1=10 FOR SHARE and has to wait
# 5. C1 COMMITS
# 6. C2 sees the new row

  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);

--connect (C1, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c1=10 WHERE id=2;
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE v1=10 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4e

--source suite/innodb/include/prepare_secondary_index_on_virtual.inc

# In 4e, the previous version is not made by trx_id, so we conclude, that
# there is no implicit lock
# 1. default INSERTs id=2,v1=2
# 2. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE v1=2 FOR SHARE and does not have to wait
# 5. C2 COMMITS
# 6. C1 COMMITS

  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);

--connect (C1, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE v1=2 FOR SHARE;
  COMMIT;

--connection C1
  COMMIT;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc



#
# We will now run through same scenarios as above, but this time we will not use
# any virtual columns: we will use c1 in place of v1, just to see if the code
# which handles regular secondary columns has same observable behavior as the
# code which handles virtual columns
#



--echo # Scenario 1

--source suite/innodb/include/prepare_secondary_index.inc

# This scenario reproduces the original problem, which was that a transaction
# was considered to hold implicit lock on a secondary index entry, even if
# it has not modified any column affecting this secondary index.
# 1. C1 obtains S lock on secondary index only for id=1
# 2. C2 modifies row with id=1, by changing c2, which does not affect
#    secondary index, and thus it should not be considered to hold an
#    implicit lock on secondary index. Also, this does not require C2
#    to pay any attention to C1's lock on secondary index.
# 3. C1 tries to get X lock on the same row, which as one of the steps
#    checks if C2 has implicit lock (and the bug causes it to believe
#    that C2 holds it) and if so, then if there is some trx holding an
#    explicit lock (and it turns out that C1 already has the S lock).

--connect (C1, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE c1=1 FOR SHARE;

--connect (C2, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=1;

--connection C1
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE c1=1 FOR UPDATE;

--connection C2
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  ROLLBACK;

--connection C1
  --reap
  ROLLBACK;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc


--echo # Scenario 2

--source suite/innodb/include/prepare_secondary_index.inc

# This scenario is dual to Scenario 1 in some sense, as it deals with delete
# marked entries in secondary index.
# It is similar to Scenario 1 in that a bad implementation could wrongly
# conclude that modifying an unrelated field makes the transaction an implicit
# lock owner.
# This scenario proves that the bug in the original code was not simply in
# negating the result of row_vers_non_vc_index_entry_match.
# 1. `default` modifies row with id=1, by setting c1 to 55,
#    so that that entry with c1=1 becomes delete marked
# 2. C1 obtains S lock on secondary index only for c1=1
# 3. C2 modifies row with id=1, by changing c2, which does not affect
#    secondary index, and thus it should not be considered to hold an
#    implicit lock on secondary index. Also, this does not require C2
#    to pay any attention to C1's lock on secondary index.
# 4. C1 tries to get X lock on the same row, which as one of the steps
#    checks if C2 has implicit lock (and the bug causes it to believe
#    that C2 holds it) and if so, then if there is some trx holding an
#    explicit lock (and it turns out that C1 already has the S lock).

  UPDATE t1 SET c1=55 WHERE id=1;

--connect (C1, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE c1=1 FOR SHARE;

--connect (C2, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=1;

--connection C1
  SELECT 1 FROM t1 WHERE c1=1 FOR UPDATE;
  ROLLBACK;

--connection C2
  ROLLBACK;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 3

--source suite/innodb/include/prepare_secondary_index.inc

# This is not a very difficult scenario, and it makes more sense
# for virtual colum version of this test which can be found above.
# 1. C1 DELETEs record with id=1, and thus should hold implicit lock
#       on secondary index for c1=1.
# 2. C2 performs a SELECT 1 FROM t1 WHERE c1=1 FOR SHARE, and should wait
# 3. C1 commits
# 4. C2 should see empty result

--connect (C1, localhost, root,,)
  BEGIN;
  DELETE FROM t1 WHERE id=1;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE c1=1 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4

# In this scenario we check the last possible path - the one where secondary
# row is not delete marked, and previous version of primary index row is also
# not delete marked in which case we should withold with deciding if there is an
# implicit lock until we check trx_id.
# There are many subscenarios.

--echo # Scenario 4a

--source suite/innodb/include/prepare_secondary_index.inc

# In 4a we have a case where the previous version is made by active trx,
# and there are no older versions, because it started with INSERT.
# In such case it should own implicit lock.
# 1. C1 INSERTS new row with id=2 c1=2
# 2. C1 UPDATES unrelated column c2, so that previous_version is INSERT
# 3. C2 does SELECT 1 FROM t1 WHERE c1=2 FOR SHARE and has to wait
# 4. C1 commits
# 5. C2 sees the new row

--connect (C1, localhost, root,,)
  BEGIN;
  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE c1=2 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap


--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4b

--source suite/innodb/include/prepare_secondary_index.inc

# In 4b, again the previous version is made by active trx, and there exists
# an even older version, which was not made by active trx and also matches
# so in this case there is no implicit lock.
# 1. default INSERTs id=1,c1=2
# 2. C1 UPDATEs unrelated field c2
# 3. C1 UPDATEs unrelated field c2 again
# 4. C2 does SELECT 1 FROM t1 WHERE c1=2 FOR SHARE and does not have to wait
# 5. C2 COMMITS
# 6. C1 COMMITS

  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);

--connect (C1, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=2;
  UPDATE t1 SET c2=42 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE c1=2 FOR SHARE;
  COMMIT;

--connection C1
  COMMIT;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4c

--source suite/innodb/include/prepare_secondary_index.inc

# In 4c, the previous version is made by active trx, there is an older version,
# also made by trx_id but does not match the secondary index, thus
# there is an implicit lock
# 1. C1 INSERTs id=2 c1=2
# 2. C1 UPDATEs c1 to 10 for id=2
# 3. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE c1=10 FOR SHARE and has to wait
# 5. C1 COMMITS
# 6. C2 sees the new row

--connect (C1, localhost, root,,)
  BEGIN;
  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);
  UPDATE t1 SET c1=10 WHERE id=2;
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE c1=10 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4d

--source suite/innodb/include/prepare_secondary_index.inc

# In 4d, the previous version is made by active trx, there is an older version,
# which was not made by trx_id, and does not match secondary index, thus
# there is an implicit lock.
# 1. default INSERTs id=2 c1=2
# 2. C1 UPDATEs c1 to 10 for id=2
# 3. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE c1=10 FOR SHARE and has to wait
# 5. C1 COMMITS
# 6. C2 sees the new row

  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);

--connect (C1, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c1=10 WHERE id=2;
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE c1=10 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4e

--source suite/innodb/include/prepare_secondary_index.inc

# In 4e, the previous version is not made by trx_id, so we conclude, that
# there is no implicit lock
# 1. default INSERTs id=2,c1=2
# 2. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE c1=2 FOR SHARE and does not have to wait
# 5. C2 COMMITS
# 6. C1 COMMITS

  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);

--connect (C1, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE c1=2 FOR SHARE;
  COMMIT;

--connection C1
  COMMIT;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc



#
# We go through all scenarios one more time, but this time, there exists a
# separate secondary index on c2, so changes to c2 cause undo logging of columns
# - in particular virtual columns.
# As we have separate cases in code for the lack of virtual columns information
# in the undo log, this is an interesting case.
# In case of scenario 4b, we now have two separate versions of it,
# where 4b' is the same as 4b except that one of the updates changes column
# c3, which does not take part in any secondary index at all.
#

--echo # Scenario 1

--source suite/innodb/include/prepare_secondary_indexs_on_virtual_and_normal.inc

# This scenario reproduces the original problem, which was that a transaction
# was considered to hold implicit lock on a secondary index entry, even if
# it has not modified any column affecting this secondary index.
# 1. C1 obtains S lock on secondary index only for id=1
# 2. C2 modifies row with id=1, by changing c2, which does not affect
#    secondary index, and thus it should not be considered to hold an
#    implicit lock on secondary index. Also, this does not require C2
#    to pay any attention to C1's lock on secondary index.
# 3. C1 tries to get X lock on the same row, which as one of the steps
#    checks if C2 has implicit lock (and the bug causes it to believe
#    that C2 holds it) and if so, then if there is some trx holding an
#    explicit lock (and it turns out that C1 already has the S lock).

--connect (C1, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE v1=1 FOR SHARE;

--connect (C2, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=1;

--connection C1
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE v1=1 FOR UPDATE;

--connection C2
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  ROLLBACK;

--connection C1
  --reap
  ROLLBACK;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc


--echo # Scenario 2

--source suite/innodb/include/prepare_secondary_indexs_on_virtual_and_normal.inc

# This scenario is dual to Scenario 1 in some sense, as it deals with delete
# marked entries in secondary index.
# It is similar to Scenario 1 in that a bad implementation could wrongly
# conclude that modifying an unrelated field makes the transaction an implicit
# lock owner.
# This scenario proves that the bug in the original code was not simply in
# negating the result of row_vers_non_vc_index_entry_match.
# 1. `default` modifies row with id=1, by setting c1 to 55,
#    so that that entry with v1=1 becomes delete marked
# 2. C1 obtains S lock on secondary index only for v1=1
# 3. C2 modifies row with id=1, by changing c2, which does not affect
#    secondary index, and thus it should not be considered to hold an
#    implicit lock on secondary index. Also, this does not require C2
#    to pay any attention to C1's lock on secondary index.
# 4. C1 tries to get X lock on the same row, which as one of the steps
#    checks if C2 has implicit lock (and the bug causes it to believe
#    that C2 holds it) and if so, then if there is some trx holding an
#    explicit lock (and it turns out that C1 already has the S lock).

  UPDATE t1 SET c1=55 WHERE id=1;

--connect (C1, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE v1=1 FOR SHARE;

--connect (C2, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=1;

--connection C1
  SELECT 1 FROM t1 WHERE v1=1 FOR UPDATE;
  ROLLBACK;

--connection C2
  ROLLBACK;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 2b

--source suite/innodb/include/prepare_secondary_indexs_on_virtual_and_normal.inc

# This scenario is similar to 2, but we perform not one, but two UPDATES
# in C2, one of them on a totally unrelated column c3, just to check if the
# algorithm correctly handles missing vrow information

  UPDATE t1 SET c1=55 WHERE id=1;

--connect (C1, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE v1=1 FOR SHARE;

--connect (C2, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c3=13 WHERE id=1;
  UPDATE t1 SET c2=13 WHERE id=1;

--connection C1
  SELECT 1 FROM t1 WHERE v1=1 FOR UPDATE;
  ROLLBACK;

--connection C2
  ROLLBACK;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 3

--source suite/innodb/include/prepare_secondary_indexs_on_virtual_and_normal.inc

# This is not a very difficult scenario, but one needed to verify that a
# DELETE operation is logged to undo log without UPD_NODE_NO_ORD_CHANGE flag
# and thus virtual column fields can be retrieved from undo log.
# This is one of the core assumptions for the fix to this bug.
# 1. C1 DELETEs record with id=1, and thus should hold implicit lock
#       on secondary index for v1=1.
# 2. C2 performs a SELECT 1 FROM t1 WHERE v1=1 FOR SHARE, and should wait
# 3. C1 commits
# 4. C2 should see empty result

--connect (C1, localhost, root,,)
  BEGIN;
  DELETE FROM t1 WHERE id=1;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE v1=1 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4

# In this scenario we check the last possible path for missing virtual
# columns information: the one where secondary row is not delete marked,
# and previous version of primary index row is also not delete marked in
# which case we should withold with deciding if there is an implicit lock
# until we check trx_id.
# There are many subscenarios.

--echo # Scenario 4a

--source suite/innodb/include/prepare_secondary_indexs_on_virtual_and_normal.inc

# In 4a we have a case where the previous version is made by active trx,
# and there are no older versions, because it started with INSERT.
# In such case it should own implicit lock.
# 1. C1 INSERTS new row with id=2 v1=2
# 2. C1 UPDATES unrelated column c2, so that previous_version is INSERT
# 3. C2 does SELECT 1 FROM t1 WHERE v1=2 FOR SHARE and has to wait
# 4. C1 commits
# 5. C2 sees the new row

--connect (C1, localhost, root,,)
  BEGIN;
  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE v1=2 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap


--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4b

--source suite/innodb/include/prepare_secondary_indexs_on_virtual_and_normal.inc

# In 4b, again the previous version is made by active trx, and there exists
# an even older version, which was not made by active trx and also matches
# so in this case there is no implicit lock.
# 1. default INSERTs id=1,v1=2
# 2. C1 UPDATEs unrelated field c2
# 3. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE v1=2 FOR SHARE and does not have to wait
# 5. C2 COMMITS
# 6. C1 COMMITS

  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);

--connect (C1, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=2;
  UPDATE t1 SET c2=42 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE v1=2 FOR SHARE;
  COMMIT;

--connection C1
  COMMIT;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4b-prime

--source suite/innodb/include/prepare_secondary_indexs_on_virtual_and_normal.inc

# In 4b, again the previous version is made by active trx, and there exists
# an even older version, which was not made by active trx and also matches
# so in this case there is no implicit lock.
# 1. default INSERTs id=1,v1=2
# 2. C1 UPDATEs unrelated field c3
# 3. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE v1=2 FOR SHARE and does not have to wait
# 5. C2 COMMITS
# 6. C1 COMMITS

  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);

--connect (C1, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c3=13 WHERE id=2;
  UPDATE t1 SET c2=42 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE v1=2 FOR SHARE;
  COMMIT;

--connection C1
  COMMIT;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4c

--source suite/innodb/include/prepare_secondary_indexs_on_virtual_and_normal.inc

# In 4c, the previous version is made by active trx, there is an older version,
# also made by trx_id but does not match the secondary index, thus
# there is an implicit lock
# 1. C1 INSERTs id=2 v1=2
# 2. C1 UPDATEs c1 (and thus v1) to 10 for id=2
# 3. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE v1=10 FOR SHARE and has to wait
# 5. C1 COMMITS
# 6. C2 sees the new row

--connect (C1, localhost, root,,)
  BEGIN;
  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);
  UPDATE t1 SET c1=10 WHERE id=2;
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE v1=10 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4d

--source suite/innodb/include/prepare_secondary_indexs_on_virtual_and_normal.inc

# In 4d, the previous version is made by active trx, there is an older version,
# which was not made by trx_id, and does not match secondary index, thus
# there is an implicit lock.
# 1. default INSERTs id=2 v1=2
# 2. C1 UPDATEs c1 (and thus v1) to 10 for id=2
# 3. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE v1=10 FOR SHARE and has to wait
# 5. C1 COMMITS
# 6. C2 sees the new row

  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);

--connect (C1, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c1=10 WHERE id=2;
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SET DEBUG_SYNC='lock_wait_will_wait SIGNAL c1_will_wait';
  --send SELECT 1 FROM t1 WHERE v1=10 FOR SHARE

--connection C1
  SET DEBUG_SYNC='now WAIT_FOR c1_will_wait';
  COMMIT;

--connection C2
  --reap

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc

--echo # Scenario 4e

--source suite/innodb/include/prepare_secondary_indexs_on_virtual_and_normal.inc

# In 4e, the previous version is not made by trx_id, so we conclude, that
# there is no implicit lock
# 1. default INSERTs id=2,v1=2
# 2. C1 UPDATEs unrelated field c2
# 4. C2 does SELECT 1 FROM t1 WHERE v1=2 FOR SHARE and does not have to wait
# 5. C2 COMMITS
# 6. C1 COMMITS

  INSERT INTO t1 (id,c1,c2) VALUES (2,2,2);

--connect (C1, localhost, root,,)
  BEGIN;
  UPDATE t1 SET c2=13 WHERE id=2;

--connect (C2, localhost, root,,)
  BEGIN;
  SELECT 1 FROM t1 WHERE v1=2 FOR SHARE;
  COMMIT;

--connection C1
  COMMIT;

--connection default
--disconnect C1
--disconnect C2

--source suite/innodb/include/cleanup_secondary_index.inc



SET @@global.innodb_lock_wait_timeout = @innodb_lock_wait_timeout_saved;