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
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
|
/*****************************************************************************
Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2015, 2018, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file trx/trx0trx.cc
The transaction
Created 3/26/1996 Heikki Tuuri
*******************************************************/
#include "btr0types.h"
#include "trx0trx.h"
#ifdef UNIV_NONINL
#include "trx0trx.ic"
#endif
#include <mysql/service_wsrep.h>
#include "trx0undo.h"
#include "trx0rseg.h"
#include "log0log.h"
#include "que0que.h"
#include "lock0lock.h"
#include "trx0roll.h"
#include "usr0sess.h"
#include "read0read.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "btr0sea.h"
#include "os0proc.h"
#include "trx0xa.h"
#include "trx0rec.h"
#include "trx0purge.h"
#include "ha_prototypes.h"
#include "srv0mon.h"
#include "ut0vec.h"
#include<set>
extern "C"
int thd_deadlock_victim_preference(const MYSQL_THD thd1, const MYSQL_THD thd2);
/** Set of table_id */
typedef std::set<table_id_t> table_id_set;
/** Dummy session used currently in MySQL interface */
UNIV_INTERN sess_t* trx_dummy_sess = NULL;
#ifdef UNIV_PFS_MUTEX
/* Key to register the mutex with performance schema */
UNIV_INTERN mysql_pfs_key_t trx_mutex_key;
/* Key to register the mutex with performance schema */
UNIV_INTERN mysql_pfs_key_t trx_undo_mutex_key;
#endif /* UNIV_PFS_MUTEX */
/*************************************************************//**
Set detailed error message for the transaction. */
UNIV_INTERN
void
trx_set_detailed_error(
/*===================*/
trx_t* trx, /*!< in: transaction struct */
const char* msg) /*!< in: detailed error message */
{
ut_strlcpy(trx->detailed_error, msg, sizeof(trx->detailed_error));
}
/*************************************************************//**
Set detailed error message for the transaction from a file. Note that the
file is rewinded before reading from it. */
UNIV_INTERN
void
trx_set_detailed_error_from_file(
/*=============================*/
trx_t* trx, /*!< in: transaction struct */
FILE* file) /*!< in: file to read message from */
{
os_file_read_string(file, trx->detailed_error,
sizeof(trx->detailed_error));
}
/*************************************************************//**
Callback function for trx_find_descriptor() to compare trx IDs. */
UNIV_INTERN
int
trx_descr_cmp(
/*==========*/
const void *a, /*!< in: pointer to first comparison argument */
const void *b) /*!< in: pointer to second comparison argument */
{
const trx_id_t* da = (const trx_id_t*) a;
const trx_id_t* db = (const trx_id_t*) b;
if (*da < *db) {
return -1;
} else if (*da > *db) {
return 1;
}
return 0;
}
/*************************************************************//**
Reserve a slot for a given trx in the global descriptors array. */
UNIV_INLINE
void
trx_reserve_descriptor(
/*===================*/
const trx_t* trx) /*!< in: trx pointer */
{
ulint n_used;
ulint n_max;
trx_id_t* descr;
ut_ad(mutex_own(&trx_sys->mutex) || srv_is_being_started);
ut_ad(srv_is_being_started ||
!trx_find_descriptor(trx_sys->descriptors,
trx_sys->descr_n_used,
trx->id));
n_used = trx_sys->descr_n_used + 1;
n_max = trx_sys->descr_n_max;
if (UNIV_UNLIKELY(n_used > n_max)) {
n_max = n_max * 2;
trx_sys->descriptors = static_cast<trx_id_t*>(
ut_realloc(trx_sys->descriptors,
n_max * sizeof(trx_id_t)));
trx_sys->descr_n_max = n_max;
srv_descriptors_memory = n_max * sizeof(trx_id_t);
}
descr = trx_sys->descriptors + n_used - 1;
if (UNIV_UNLIKELY(n_used > 1 && trx->id < descr[-1])) {
/* Find the slot where it should be inserted. We could use a
binary search, but in reality linear search should be faster,
because the slot we are looking for is near the array end. */
trx_id_t* tdescr;
for (tdescr = descr - 1;
tdescr >= trx_sys->descriptors && *tdescr > trx->id;
tdescr--) {
}
tdescr++;
ut_memmove(tdescr + 1, tdescr, (descr - tdescr) *
sizeof(trx_id_t));
descr = tdescr;
}
*descr = trx->id;
trx_sys->descr_n_used = n_used;
}
/*************************************************************//**
Release a slot for a given trx in the global descriptors array. */
UNIV_INTERN
void
trx_release_descriptor(
/*===================*/
trx_t* trx) /*!< in: trx pointer */
{
ulint size;
trx_id_t* descr;
ut_ad(mutex_own(&trx_sys->mutex));
if (UNIV_LIKELY(trx->in_trx_serial_list)) {
UT_LIST_REMOVE(trx_serial_list, trx_sys->trx_serial_list,
trx);
trx->in_trx_serial_list = false;
}
descr = trx_find_descriptor(trx_sys->descriptors,
trx_sys->descr_n_used,
trx->id);
if (UNIV_UNLIKELY(descr == NULL)) {
return;
}
size = (trx_sys->descriptors + trx_sys->descr_n_used - 1 - descr) *
sizeof(trx_id_t);
if (UNIV_LIKELY(size > 0)) {
ut_memmove(descr, descr + 1, size);
}
trx_sys->descr_n_used--;
}
/****************************************************************//**
Creates and initializes a transaction object. It must be explicitly
started with trx_start_if_not_started() before using it. The default
isolation level is TRX_ISO_REPEATABLE_READ.
@return transaction instance, should never be NULL */
static
trx_t*
trx_create(void)
/*============*/
{
trx_t* trx;
mem_heap_t* heap;
ib_alloc_t* heap_alloc;
trx = static_cast<trx_t*>(mem_zalloc(sizeof(*trx)));
mutex_create(trx_mutex_key, &trx->mutex, SYNC_TRX);
trx->magic_n = TRX_MAGIC_N;
trx->active_commit_ordered = 0;
trx->state = TRX_STATE_NOT_STARTED;
trx->isolation_level = TRX_ISO_REPEATABLE_READ;
trx->no = TRX_ID_MAX;
trx->in_trx_serial_list = false;
trx->support_xa = TRUE;
trx->fake_changes = FALSE;
trx->check_foreigns = TRUE;
trx->check_unique_secondary = TRUE;
trx->dict_operation = TRX_DICT_OP_NONE;
trx->idle_start = 0;
trx->last_stmt_start = 0;
mutex_create(trx_undo_mutex_key, &trx->undo_mutex, SYNC_TRX_UNDO);
trx->error_state = DB_SUCCESS;
trx->lock.que_state = TRX_QUE_RUNNING;
trx->lock.lock_heap = mem_heap_create_typed(
256, MEM_HEAP_FOR_LOCK_HEAP);
trx->search_latch_timeout = BTR_SEA_TIMEOUT;
trx->io_reads = 0;
trx->io_read = 0;
trx->io_reads_wait_timer = 0;
trx->lock_que_wait_timer = 0;
trx->innodb_que_wait_timer = 0;
trx->distinct_page_access = 0;
trx->distinct_page_access_hash = NULL;
trx->take_stats = FALSE;
trx->xid.null();
trx->op_info = "";
trx->api_trx = false;
trx->api_auto_commit = false;
trx->read_write = true;
heap = mem_heap_create(sizeof(ib_vector_t) + sizeof(void*) * 8);
heap_alloc = ib_heap_allocator_create(heap);
/* Remember to free the vector explicitly in trx_free(). */
trx->autoinc_locks = ib_vector_create(heap_alloc, sizeof(void**), 4);
/* Remember to free the vector explicitly in trx_free(). */
heap = mem_heap_create(sizeof(ib_vector_t) + sizeof(void*) * 128);
heap_alloc = ib_heap_allocator_create(heap);
trx->lock.table_locks = ib_vector_create(
heap_alloc, sizeof(void**), 32);
#ifdef WITH_WSREP
trx->wsrep_event = NULL;
#endif /* WITH_WSREP */
return(trx);
}
/********************************************************************//**
Creates a transaction object for background operations by the master thread.
@return own: transaction object */
UNIV_INTERN
trx_t*
trx_allocate_for_background(void)
/*=============================*/
{
trx_t* trx;
trx = trx_create();
trx->sess = trx_dummy_sess;
return(trx);
}
/********************************************************************//**
Creates a transaction object for MySQL.
@return own: transaction object */
UNIV_INTERN
trx_t*
trx_allocate_for_mysql(void)
/*========================*/
{
trx_t* trx;
trx = trx_allocate_for_background();
mutex_enter(&trx_sys->mutex);
ut_d(trx->in_mysql_trx_list = TRUE);
UT_LIST_ADD_FIRST(mysql_trx_list, trx_sys->mysql_trx_list, trx);
mutex_exit(&trx_sys->mutex);
if (UNIV_UNLIKELY(trx->take_stats)) {
trx->distinct_page_access_hash
= static_cast<byte *>(mem_alloc(DPAH_SIZE));
memset(trx->distinct_page_access_hash, 0, DPAH_SIZE);
}
return(trx);
}
/********************************************************************//**
Frees a transaction object without releasing the corresponding descriptor.
Should be used by callers that already own trx_sys->mutex. */
static
void
trx_free_low(
/*=========*/
trx_t* trx) /*!< in, own: trx object */
{
ut_a(trx->magic_n == TRX_MAGIC_N);
ut_ad(!trx->in_ro_trx_list);
ut_ad(!trx->in_rw_trx_list);
ut_ad(!trx->in_mysql_trx_list);
mutex_free(&trx->undo_mutex);
if (trx->undo_no_arr != NULL) {
trx_undo_arr_free(trx->undo_no_arr);
}
ut_a(trx->lock.wait_lock == NULL);
ut_a(trx->lock.wait_thr == NULL);
ut_a(!trx->has_search_latch);
#ifdef UNIV_SYNC_DEBUG
ut_ad(!btr_search_own_any());
#endif
ut_a(trx->dict_operation_lock_mode == 0);
if (trx->lock.lock_heap) {
mem_heap_free(trx->lock.lock_heap);
}
ut_a(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0);
ut_a(ib_vector_is_empty(trx->autoinc_locks));
/* We allocated a dedicated heap for the vector. */
ib_vector_free(trx->autoinc_locks);
if (trx->lock.table_locks != NULL) {
/* We allocated a dedicated heap for the vector. */
ib_vector_free(trx->lock.table_locks);
}
mutex_free(&trx->mutex);
read_view_free(trx->prebuilt_view);
mem_free(trx);
}
/********************************************************************//**
Frees a transaction object. */
static
void
trx_free(
/*=========*/
trx_t* trx) /*!< in, own: trx object */
{
mutex_enter(&trx_sys->mutex);
trx_release_descriptor(trx);
mutex_exit(&trx_sys->mutex);
trx_free_low(trx);
}
/********************************************************************//**
Frees a transaction object of a background operation of the master thread. */
UNIV_INTERN
void
trx_free_for_background(
/*====================*/
trx_t* trx) /*!< in, own: trx object */
{
if (trx->distinct_page_access_hash)
{
mem_free(trx->distinct_page_access_hash);
trx->distinct_page_access_hash= NULL;
}
if (trx->declared_to_be_inside_innodb) {
ib_logf(IB_LOG_LEVEL_ERROR,
"Freeing a trx (%p, " TRX_ID_FMT ") which is declared "
"to be processing inside InnoDB", trx, trx->id);
trx_print(stderr, trx, 600);
putc('\n', stderr);
/* This is an error but not a fatal error. We must keep
the counters like srv_conc_n_threads accurate. */
srv_conc_force_exit_innodb(trx);
}
if (trx->n_mysql_tables_in_use != 0
|| trx->mysql_n_tables_locked != 0) {
ib_logf(IB_LOG_LEVEL_ERROR,
"MySQL is freeing a thd though "
"trx->n_mysql_tables_in_use is %lu and "
"trx->mysql_n_tables_locked is %lu.",
(ulong) trx->n_mysql_tables_in_use,
(ulong) trx->mysql_n_tables_locked);
trx_print(stderr, trx, 600);
ut_print_buf(stderr, trx, sizeof(trx_t));
putc('\n', stderr);
}
ut_a(trx->state == TRX_STATE_NOT_STARTED);
ut_a(trx->insert_undo == NULL);
ut_a(trx->update_undo == NULL);
ut_a(trx->read_view == NULL);
trx_free(trx);
}
/********************************************************************//**
At shutdown, frees a transaction object that is in the PREPARED state. */
UNIV_INTERN
void
trx_free_prepared(
/*==============*/
trx_t* trx) /*!< in, own: trx object */
{
ut_a(trx_state_eq(trx, TRX_STATE_PREPARED)
|| (trx->is_recovered
&& (trx_state_eq(trx, TRX_STATE_ACTIVE)
|| trx_state_eq(trx, TRX_STATE_COMMITTED_IN_MEMORY))
&& (srv_read_only_mode
|| srv_apply_log_only
|| srv_force_recovery >= SRV_FORCE_NO_TRX_UNDO)));
ut_a(trx->magic_n == TRX_MAGIC_N);
lock_trx_release_locks(trx);
trx_undo_free_prepared(trx);
assert_trx_in_rw_list(trx);
ut_a(!trx->read_only);
UT_LIST_REMOVE(trx_list, trx_sys->rw_trx_list, trx);
ut_d(trx->in_rw_trx_list = FALSE);
mutex_enter(&trx_sys->mutex);
trx_release_descriptor(trx);
mutex_exit(&trx_sys->mutex);
/* Undo trx_resurrect_table_locks(). */
UT_LIST_INIT(trx->lock.trx_locks);
trx_free_low(trx);
ut_ad(trx_sys->descr_n_used <= UT_LIST_GET_LEN(trx_sys->rw_trx_list));
}
/********************************************************************//**
Frees a transaction object for MySQL. */
UNIV_INTERN
void
trx_free_for_mysql(
/*===============*/
trx_t* trx) /*!< in, own: trx object */
{
if (trx->distinct_page_access_hash)
{
mem_free(trx->distinct_page_access_hash);
trx->distinct_page_access_hash= NULL;
}
mutex_enter(&trx_sys->mutex);
ut_ad(trx->in_mysql_trx_list);
ut_d(trx->in_mysql_trx_list = FALSE);
UT_LIST_REMOVE(mysql_trx_list, trx_sys->mysql_trx_list, trx);
ut_ad(trx_sys_validate_trx_list());
mutex_exit(&trx_sys->mutex);
trx_free_for_background(trx);
}
/****************************************************************//**
Inserts the trx handle in the trx system trx list in the right position.
The list is sorted on the trx id so that the biggest id is at the list
start. This function is used at the database startup to insert incomplete
transactions to the list. */
static
void
trx_list_rw_insert_ordered(
/*=======================*/
trx_t* trx) /*!< in: trx handle */
{
trx_t* trx2;
ut_ad(!trx->read_only);
ut_d(trx->start_file = __FILE__);
ut_d(trx->start_line = __LINE__);
ut_a(srv_is_being_started);
ut_ad(!trx->in_ro_trx_list);
ut_ad(!trx->in_rw_trx_list);
ut_ad(trx->state != TRX_STATE_NOT_STARTED);
ut_ad(trx->is_recovered);
for (trx2 = UT_LIST_GET_FIRST(trx_sys->rw_trx_list);
trx2 != NULL;
trx2 = UT_LIST_GET_NEXT(trx_list, trx2)) {
assert_trx_in_rw_list(trx2);
if (trx->id >= trx2->id) {
ut_ad(trx->id > trx2->id);
break;
}
}
if (trx2 != NULL) {
trx2 = UT_LIST_GET_PREV(trx_list, trx2);
if (trx2 == NULL) {
UT_LIST_ADD_FIRST(trx_list, trx_sys->rw_trx_list, trx);
} else {
UT_LIST_INSERT_AFTER(
trx_list, trx_sys->rw_trx_list, trx2, trx);
}
} else {
UT_LIST_ADD_LAST(trx_list, trx_sys->rw_trx_list, trx);
}
#ifdef UNIV_DEBUG
if (trx->id > trx_sys->rw_max_trx_id) {
trx_sys->rw_max_trx_id = trx->id;
}
#endif /* UNIV_DEBUG */
ut_ad(!trx->in_rw_trx_list);
ut_d(trx->in_rw_trx_list = TRUE);
}
/****************************************************************//**
Resurrect the table locks for a resurrected transaction. */
static
void
trx_resurrect_table_locks(
/*======================*/
trx_t* trx, /*!< in/out: transaction */
const trx_undo_t* undo) /*!< in: undo log */
{
mtr_t mtr;
page_t* undo_page;
trx_undo_rec_t* undo_rec;
table_id_set tables;
ut_ad(undo == trx->insert_undo || undo == trx->update_undo);
if (trx_state_eq(trx, TRX_STATE_COMMITTED_IN_MEMORY)
|| undo->empty) {
return;
}
mtr_start(&mtr);
/* trx_rseg_mem_create() may have acquired an X-latch on this
page, so we cannot acquire an S-latch. */
undo_page = trx_undo_page_get(
undo->space, undo->zip_size, undo->top_page_no, &mtr);
undo_rec = undo_page + undo->top_offset;
do {
ulint type;
ulint cmpl_info;
bool updated_extern;
undo_no_t undo_no;
table_id_t table_id;
page_t* undo_rec_page = page_align(undo_rec);
if (undo_rec_page != undo_page) {
if (!mtr_memo_release(&mtr,
buf_block_align(undo_page),
MTR_MEMO_PAGE_X_FIX)) {
/* The page of the previous undo_rec
should have been latched by
trx_undo_page_get() or
trx_undo_get_prev_rec(). */
ut_ad(0);
}
undo_page = undo_rec_page;
}
trx_undo_rec_get_pars(
undo_rec, &type, &cmpl_info,
&updated_extern, &undo_no, &table_id);
tables.insert(table_id);
undo_rec = trx_undo_get_prev_rec(
undo_rec, undo->hdr_page_no,
undo->hdr_offset, false, &mtr);
} while (undo_rec);
mtr_commit(&mtr);
for (table_id_set::const_iterator i = tables.begin();
i != tables.end(); i++) {
if (dict_table_t* table = dict_table_open_on_id(
*i, FALSE, DICT_TABLE_OP_LOAD_TABLESPACE)) {
if (table->file_unreadable
|| dict_table_is_temporary(table)) {
mutex_enter(&dict_sys->mutex);
dict_table_close(table, TRUE, FALSE);
dict_table_remove_from_cache(table);
mutex_exit(&dict_sys->mutex);
continue;
}
lock_table_ix_resurrect(table, trx);
DBUG_PRINT("ib_trx",
("resurrect" TRX_ID_FMT
" table '%s' IX lock from %s undo",
trx->id, table->name,
undo == trx->insert_undo
? "insert" : "update"));
dict_table_close(table, FALSE, FALSE);
}
}
}
/****************************************************************//**
Resurrect the transactions that were doing inserts the time of the
crash, they need to be undone.
@return trx_t instance */
static
trx_t*
trx_resurrect_insert(
/*=================*/
trx_undo_t* undo, /*!< in: entry to UNDO */
trx_rseg_t* rseg) /*!< in: rollback segment */
{
trx_t* trx;
trx = trx_allocate_for_background();
trx->rseg = rseg;
trx->xid = undo->xid;
trx->id = undo->trx_id;
trx->insert_undo = undo;
trx->is_recovered = TRUE;
/* This is single-threaded startup code, we do not need the
protection of trx->mutex or trx_sys->mutex here. */
if (undo->state != TRX_UNDO_ACTIVE) {
/* Prepared transactions are left in the prepared state
waiting for a commit or abort decision from MySQL */
if (undo->state == TRX_UNDO_PREPARED) {
fprintf(stderr,
"InnoDB: Transaction " TRX_ID_FMT " was in the"
" XA prepared state.\n", trx->id);
trx->state = TRX_STATE_PREPARED;
trx_sys->n_prepared_trx++;
trx_sys->n_prepared_recovered_trx++;
} else {
trx->state = TRX_STATE_COMMITTED_IN_MEMORY;
}
/* We give a dummy value for the trx no; this should have no
relevance since purge is not interested in committed
transaction numbers, unless they are in the history
list, in which case it looks the number from the disk based
undo log structure */
trx->no = trx->id;
} else {
trx->state = TRX_STATE_ACTIVE;
/* A running transaction always has the number
field inited to TRX_ID_MAX */
trx->no = TRX_ID_MAX;
}
/* trx_start_low() is not called with resurrect, so need to initialize
start time here.*/
if (trx->state == TRX_STATE_ACTIVE
|| trx->state == TRX_STATE_PREPARED) {
trx->start_time = ut_time();
}
if (undo->dict_operation) {
trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
trx->table_id = undo->table_id;
}
if (!undo->empty) {
trx->undo_no = undo->top_undo_no + 1;
}
return(trx);
}
/****************************************************************//**
Prepared transactions are left in the prepared state waiting for a
commit or abort decision from MySQL */
static
void
trx_resurrect_update_in_prepared_state(
/*===================================*/
trx_t* trx, /*!< in,out: transaction */
const trx_undo_t* undo) /*!< in: update UNDO record */
{
/* This is single-threaded startup code, we do not need the
protection of trx->mutex or trx_sys->mutex here. */
if (undo->state == TRX_UNDO_PREPARED) {
fprintf(stderr,
"InnoDB: Transaction " TRX_ID_FMT
" was in the XA prepared state.\n", trx->id);
if (trx_state_eq(trx, TRX_STATE_NOT_STARTED)) {
trx_sys->n_prepared_trx++;
trx_sys->n_prepared_recovered_trx++;
} else {
ut_ad(trx_state_eq(trx, TRX_STATE_PREPARED));
}
trx->state = TRX_STATE_PREPARED;
} else {
trx->state = TRX_STATE_COMMITTED_IN_MEMORY;
}
}
/****************************************************************//**
Resurrect the transactions that were doing updates the time of the
crash, they need to be undone. */
static
void
trx_resurrect_update(
/*=================*/
trx_t* trx, /*!< in/out: transaction */
trx_undo_t* undo, /*!< in/out: update UNDO record */
trx_rseg_t* rseg) /*!< in/out: rollback segment */
{
trx->rseg = rseg;
trx->xid = undo->xid;
trx->id = undo->trx_id;
trx->update_undo = undo;
trx->is_recovered = TRUE;
/* This is single-threaded startup code, we do not need the
protection of trx->mutex or trx_sys->mutex here. */
if (undo->state != TRX_UNDO_ACTIVE) {
trx_resurrect_update_in_prepared_state(trx, undo);
/* We give a dummy value for the trx number */
trx->no = trx->id;
} else {
trx->state = TRX_STATE_ACTIVE;
/* A running transaction always has the number field inited to
TRX_ID_MAX */
trx->no = TRX_ID_MAX;
}
/* trx_start_low() is not called with resurrect, so need to initialize
start time here.*/
if (trx->state == TRX_STATE_ACTIVE
|| trx->state == TRX_STATE_PREPARED) {
trx->start_time = ut_time();
}
if (undo->dict_operation) {
trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
trx->table_id = undo->table_id;
}
if (!undo->empty && undo->top_undo_no >= trx->undo_no) {
trx->undo_no = undo->top_undo_no + 1;
}
}
/****************************************************************//**
Creates trx objects for transactions and initializes the trx list of
trx_sys at database start. Rollback segment and undo log lists must
already exist when this function is called, because the lists of
transactions to be rolled back or cleaned up are built based on the
undo log lists. */
UNIV_INTERN
void
trx_lists_init_at_db_start(void)
/*============================*/
{
ulint i;
ut_a(srv_is_being_started);
UT_LIST_INIT(trx_sys->ro_trx_list);
UT_LIST_INIT(trx_sys->rw_trx_list);
UT_LIST_INIT(trx_sys->trx_serial_list);
/* Look from the rollback segments if there exist undo logs for
transactions */
for (i = 0; i < TRX_SYS_N_RSEGS; ++i) {
trx_undo_t* undo;
trx_rseg_t* rseg;
rseg = trx_sys->rseg_array[i];
if (rseg == NULL) {
continue;
}
/* Resurrect transactions that were doing inserts. */
for (undo = UT_LIST_GET_FIRST(rseg->insert_undo_list);
undo != NULL;
undo = UT_LIST_GET_NEXT(undo_list, undo)) {
trx_t* trx;
trx = trx_resurrect_insert(undo, rseg);
if (trx->state == TRX_STATE_ACTIVE ||
trx->state == TRX_STATE_PREPARED) {
trx_reserve_descriptor(trx);
}
trx_list_rw_insert_ordered(trx);
trx_resurrect_table_locks(trx, undo);
}
/* Ressurrect transactions that were doing updates. */
for (undo = UT_LIST_GET_FIRST(rseg->update_undo_list);
undo != NULL;
undo = UT_LIST_GET_NEXT(undo_list, undo)) {
trx_t* trx;
ibool trx_created;
/* Check the trx_sys->rw_trx_list first. */
mutex_enter(&trx_sys->mutex);
trx = trx_get_rw_trx_by_id(undo->trx_id);
mutex_exit(&trx_sys->mutex);
if (trx == NULL) {
trx = trx_allocate_for_background();
trx_created = TRUE;
} else {
trx_created = FALSE;
}
trx_resurrect_update(trx, undo, rseg);
if (trx_created) {
if (trx->state == TRX_STATE_ACTIVE ||
trx->state == TRX_STATE_PREPARED) {
trx_reserve_descriptor(trx);
}
trx_list_rw_insert_ordered(trx);
}
trx_resurrect_table_locks(trx, undo);
}
}
}
/******************************************************************//**
Assigns a rollback segment to a transaction in a round-robin fashion.
@return assigned rollback segment instance */
static
trx_rseg_t*
trx_assign_rseg_low(
/*================*/
ulong max_undo_logs, /*!< in: maximum number of UNDO logs to use */
ulint n_tablespaces) /*!< in: number of rollback tablespaces */
{
ulint i;
trx_rseg_t* rseg;
static ulint latest_rseg = 0;
if (srv_read_only_mode) {
ut_a(max_undo_logs == ULONG_UNDEFINED);
return(NULL);
}
/* This breaks true round robin but that should be OK. */
ut_a(max_undo_logs > 0 && max_undo_logs <= TRX_SYS_N_RSEGS);
i = latest_rseg++;
i %= max_undo_logs;
/* Note: The assumption here is that there can't be any gaps in
the array. Once we implement more flexible rollback segment
management this may not hold. The assertion checks for that case. */
if (trx_sys->rseg_array[0] == NULL) {
return(NULL);
}
/* Skip the system tablespace if we have more than one tablespace
defined for rollback segments. We want all UNDO records to be in
the non-system tablespaces. */
do {
rseg = trx_sys->rseg_array[i];
ut_a(rseg == NULL || i == rseg->id);
i = (rseg == NULL) ? 0 : i + 1;
} while (rseg == NULL
|| (rseg->space == 0
&& n_tablespaces > 0
&& trx_sys->rseg_array[1] != NULL));
return(rseg);
}
/****************************************************************//**
Assign a read-only transaction a rollback-segment, if it is attempting
to write to a TEMPORARY table. */
UNIV_INTERN
void
trx_assign_rseg(
/*============*/
trx_t* trx) /*!< A read-only transaction that
needs to be assigned a RBS. */
{
ut_a(trx->rseg == 0);
ut_a(trx->read_only);
ut_a(!srv_read_only_mode);
ut_a(!trx_is_autocommit_non_locking(trx));
trx->rseg = trx_assign_rseg_low(srv_undo_logs, srv_undo_tablespaces);
}
/****************************************************************//**
Starts a transaction. */
static
void
trx_start_low(
/*==========*/
trx_t* trx) /*!< in: transaction */
{
ut_ad(trx->rseg == NULL);
ut_ad(trx->start_file != 0);
ut_ad(trx->start_line != 0);
ut_ad(!trx->is_recovered);
ut_ad(trx_state_eq(trx, TRX_STATE_NOT_STARTED));
ut_ad(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0);
/* Check whether it is an AUTOCOMMIT SELECT */
trx->auto_commit = (trx->api_trx && trx->api_auto_commit)
|| thd_trx_is_auto_commit(trx->mysql_thd);
trx->read_only =
(trx->api_trx && !trx->read_write)
|| (!trx->ddl && thd_trx_is_read_only(trx->mysql_thd))
|| srv_read_only_mode;
if (!trx->auto_commit) {
++trx->will_lock;
} else if (trx->will_lock == 0) {
trx->read_only = TRUE;
}
if (!trx->read_only) {
trx->rseg = trx_assign_rseg_low(
srv_undo_logs, srv_undo_tablespaces);
}
#ifdef WITH_WSREP
trx->xid.null();
#endif /* WITH_WSREP */
/* The initial value for trx->no: TRX_ID_MAX is used in
read_view_open_now: */
trx->no = TRX_ID_MAX;
ut_a(ib_vector_is_empty(trx->autoinc_locks));
ut_a(ib_vector_is_empty(trx->lock.table_locks));
mutex_enter(&trx_sys->mutex);
/* If this transaction came from trx_allocate_for_mysql(),
trx->in_mysql_trx_list would hold. In that case, the trx->state
change must be protected by the trx_sys->mutex, so that
lock_print_info_all_transactions() will have a consistent view. */
trx->state = TRX_STATE_ACTIVE;
trx->id = trx_sys_get_new_trx_id();
/* Cache the state of fake_changes that transaction will use for
lifetime. Any change in session/global fake_changes configuration during
lifetime of transaction will not be honored by already started
transaction. */
trx->fake_changes = thd_fake_changes(trx->mysql_thd);
ut_ad(!trx->in_rw_trx_list);
ut_ad(!trx->in_ro_trx_list);
if (trx->read_only) {
/* Note: The trx_sys_t::ro_trx_list doesn't really need to
be ordered, we should exploit this using a list type that
doesn't need a list wide lock to increase concurrency. */
if (!trx_is_autocommit_non_locking(trx)) {
UT_LIST_ADD_FIRST(trx_list, trx_sys->ro_trx_list, trx);
ut_d(trx->in_ro_trx_list = TRUE);
}
} else {
ut_ad(trx->rseg != NULL
|| srv_force_recovery >= SRV_FORCE_NO_TRX_UNDO);
ut_ad(!trx_is_autocommit_non_locking(trx));
UT_LIST_ADD_FIRST(trx_list, trx_sys->rw_trx_list, trx);
ut_d(trx->in_rw_trx_list = TRUE);
#ifdef UNIV_DEBUG
if (trx->id > trx_sys->rw_max_trx_id) {
trx_sys->rw_max_trx_id = trx->id;
}
#endif /* UNIV_DEBUG */
trx_reserve_descriptor(trx);
}
ut_ad(trx_sys_validate_trx_list());
mutex_exit(&trx_sys->mutex);
trx->start_time = ut_time();
trx->start_time_micro =
trx->mysql_thd ? thd_query_start_micro(trx->mysql_thd) : 0;
MONITOR_INC(MONITOR_TRX_ACTIVE);
}
/****************************************************************//**
Set the transaction serialisation number. */
static
void
trx_serialisation_number_get(
/*=========================*/
trx_t* trx) /*!< in: transaction */
{
trx_rseg_t* rseg;
rseg = trx->rseg;
ut_ad(mutex_own(&rseg->mutex));
mutex_enter(&trx_sys->mutex);
trx->no = trx_sys_get_new_trx_id();
if (UNIV_LIKELY(!trx->in_trx_serial_list)) {
UT_LIST_ADD_LAST(trx_serial_list, trx_sys->trx_serial_list,
trx);
trx->in_trx_serial_list = true;
}
/* If the rollack segment is not empty then the
new trx_t::no can't be less than any trx_t::no
already in the rollback segment. User threads only
produce events when a rollback segment is empty. */
if (rseg->last_page_no == FIL_NULL) {
void* ptr;
rseg_queue_t rseg_queue;
rseg_queue.rseg = rseg;
rseg_queue.trx_no = trx->no;
mutex_enter(&purge_sys->bh_mutex);
/* This is to reduce the pressure on the trx_sys_t::mutex
though in reality it should make very little (read no)
difference because this code path is only taken when the
rbs is empty. */
mutex_exit(&trx_sys->mutex);
ptr = ib_bh_push(purge_sys->ib_bh, &rseg_queue);
ut_a(ptr);
mutex_exit(&purge_sys->bh_mutex);
} else {
mutex_exit(&trx_sys->mutex);
}
}
/****************************************************************//**
Assign the transaction its history serialisation number and write the
update UNDO log record to the assigned rollback segment. */
static MY_ATTRIBUTE((nonnull))
void
trx_write_serialisation_history(
/*============================*/
trx_t* trx, /*!< in/out: transaction */
mtr_t* mtr) /*!< in/out: mini-transaction */
{
#ifdef WITH_WSREP
trx_sysf_t* sys_header;
#endif /* WITH_WSREP */
trx_rseg_t* rseg;
rseg = trx->rseg;
/* Change the undo log segment states from TRX_UNDO_ACTIVE
to some other state: these modifications to the file data
structure define the transaction as committed in the file
based domain, at the serialization point of the log sequence
number lsn obtained below. */
if (trx->update_undo != NULL) {
page_t* undo_hdr_page;
trx_undo_t* undo = trx->update_undo;
/* We have to hold the rseg mutex because update
log headers have to be put to the history list in the
(serialisation) order of the UNDO trx number. This is
required for the purge in-memory data structures too. */
mutex_enter(&rseg->mutex);
/* Assign the transaction serialisation number and also
update the purge min binary heap if this is the first
UNDO log being written to the assigned rollback segment. */
trx_serialisation_number_get(trx);
/* It is not necessary to obtain trx->undo_mutex here
because only a single OS thread is allowed to do the
transaction commit for this transaction. */
undo_hdr_page = trx_undo_set_state_at_finish(undo, mtr);
trx_undo_update_cleanup(trx, undo_hdr_page, mtr);
} else {
mutex_enter(&rseg->mutex);
}
if (trx->insert_undo != NULL) {
trx_undo_set_state_at_finish(trx->insert_undo, mtr);
}
mutex_exit(&rseg->mutex);
MONITOR_INC(MONITOR_TRX_COMMIT_UNDO);
#ifdef WITH_WSREP
sys_header = trx_sysf_get(mtr);
/* Update latest MySQL wsrep XID in trx sys header. */
if (wsrep_is_wsrep_xid(&trx->xid))
{
trx_sys_update_wsrep_checkpoint(&trx->xid, sys_header, mtr);
}
#endif /* WITH_WSREP */
/* Update the latest MySQL binlog name and offset info
in trx sys header if MySQL binlogging is on or the database
server is a MySQL replication slave */
if (trx->mysql_log_file_name
&& trx->mysql_log_file_name[0] != '\0') {
trx_sys_update_mysql_binlog_offset(
trx->mysql_log_file_name,
trx->mysql_log_offset,
TRX_SYS_MYSQL_LOG_INFO,
#ifdef WITH_WSREP
sys_header,
#endif /* WITH_WSREP */
mtr);
trx->mysql_log_file_name = NULL;
}
}
/********************************************************************
Finalize a transaction containing updates for a FTS table. */
static MY_ATTRIBUTE((nonnull))
void
trx_finalize_for_fts_table(
/*=======================*/
fts_trx_table_t* ftt) /* in: FTS trx table */
{
fts_t* fts = ftt->table->fts;
fts_doc_ids_t* doc_ids = ftt->added_doc_ids;
mutex_enter(&fts->bg_threads_mutex);
if (fts->fts_status & BG_THREAD_STOP) {
/* The table is about to be dropped, no use
adding anything to its work queue. */
mutex_exit(&fts->bg_threads_mutex);
} else {
mem_heap_t* heap;
mutex_exit(&fts->bg_threads_mutex);
ut_a(fts->add_wq);
heap = static_cast<mem_heap_t*>(doc_ids->self_heap->arg);
ib_wqueue_add(fts->add_wq, doc_ids, heap);
/* fts_trx_table_t no longer owns the list. */
ftt->added_doc_ids = NULL;
}
}
/******************************************************************//**
Finalize a transaction containing updates to FTS tables. */
static MY_ATTRIBUTE((nonnull))
void
trx_finalize_for_fts(
/*=================*/
trx_t* trx, /*!< in/out: transaction */
bool is_commit) /*!< in: true if the transaction was
committed, false if it was rolled back. */
{
if (is_commit) {
const ib_rbt_node_t* node;
ib_rbt_t* tables;
fts_savepoint_t* savepoint;
savepoint = static_cast<fts_savepoint_t*>(
ib_vector_last(trx->fts_trx->savepoints));
tables = savepoint->tables;
for (node = rbt_first(tables);
node;
node = rbt_next(tables, node)) {
fts_trx_table_t** ftt;
ftt = rbt_value(fts_trx_table_t*, node);
if ((*ftt)->added_doc_ids) {
trx_finalize_for_fts_table(*ftt);
}
}
}
fts_trx_free(trx->fts_trx);
trx->fts_trx = NULL;
}
/**********************************************************************//**
If required, flushes the log to disk based on the value of
innodb_flush_log_at_trx_commit. */
static
void
trx_flush_log_if_needed_low(
/*========================*/
lsn_t lsn, /*!< in: lsn up to which logs are to be
flushed. */
trx_t* trx) /*!< in: transaction */
{
ulint flush_log_at_trx_commit;
flush_log_at_trx_commit = srv_use_global_flush_log_at_trx_commit
? thd_flush_log_at_trx_commit(NULL)
: thd_flush_log_at_trx_commit(trx->mysql_thd);
switch (flush_log_at_trx_commit) {
case 0:
/* Do nothing */
break;
case 1:
case 3:
/* Write the log and optionally flush it to disk */
log_write_up_to(lsn, LOG_WAIT_ONE_GROUP,
srv_unix_file_flush_method != SRV_UNIX_NOSYNC);
break;
case 2:
/* Write the log but do not flush it to disk */
log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, FALSE);
break;
default:
ut_error;
}
}
/**********************************************************************//**
If required, flushes the log to disk based on the value of
innodb_flush_log_at_trx_commit. */
static MY_ATTRIBUTE((nonnull))
void
trx_flush_log_if_needed(
/*====================*/
lsn_t lsn, /*!< in: lsn up to which logs are to be
flushed. */
trx_t* trx) /*!< in/out: transaction */
{
trx->op_info = "flushing log";
trx_flush_log_if_needed_low(lsn, trx);
trx->op_info = "";
}
/****************************************************************//**
Commits a transaction in memory. */
static MY_ATTRIBUTE((nonnull))
void
trx_commit_in_memory(
/*=================*/
trx_t* trx, /*!< in/out: transaction */
lsn_t lsn) /*!< in: log sequence number of the mini-transaction
commit of trx_write_serialisation_history(), or 0
if the transaction did not modify anything */
{
trx->must_flush_log_later = FALSE;
if (trx_is_autocommit_non_locking(trx)) {
ut_ad(trx->read_only);
ut_a(!trx->is_recovered);
ut_ad(trx->rseg == NULL);
ut_ad(!trx->in_ro_trx_list);
ut_ad(!trx->in_rw_trx_list);
/* Note: We are asserting without holding the lock mutex. But
that is OK because this transaction is not waiting and cannot
be rolled back and no new locks can (or should not) be added
becuase it is flagged as a non-locking read-only transaction. */
ut_a(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0);
/* This state change is not protected by any mutex, therefore
there is an inherent race here around state transition during
printouts. We ignore this race for the sake of efficiency.
However, the trx_sys_t::mutex will protect the trx_t instance
and it cannot be removed from the mysql_trx_list and freed
without first acquiring the trx_sys_t::mutex. */
ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE));
trx->state = TRX_STATE_NOT_STARTED;
read_view_remove(trx->global_read_view, false);
MONITOR_INC(MONITOR_TRX_NL_RO_COMMIT);
} else {
lock_trx_release_locks(trx);
/* Remove the transaction from the list of active
transactions now that it no longer holds any user locks. */
ut_ad(trx_state_eq(trx, TRX_STATE_COMMITTED_IN_MEMORY));
mutex_enter(&trx_sys->mutex);
assert_trx_in_list(trx);
if (trx->read_only) {
UT_LIST_REMOVE(trx_list, trx_sys->ro_trx_list, trx);
ut_d(trx->in_ro_trx_list = FALSE);
MONITOR_INC(MONITOR_TRX_RO_COMMIT);
} else {
UT_LIST_REMOVE(trx_list, trx_sys->rw_trx_list, trx);
ut_d(trx->in_rw_trx_list = FALSE);
ut_ad(trx_sys->descr_n_used <=
UT_LIST_GET_LEN(trx_sys->rw_trx_list));
MONITOR_INC(MONITOR_TRX_RW_COMMIT);
}
/* If this transaction came from trx_allocate_for_mysql(),
trx->in_mysql_trx_list would hold. In that case, the
trx->state change must be protected by trx_sys->mutex, so that
lock_print_info_all_transactions() will have a consistent
view. */
trx->state = TRX_STATE_NOT_STARTED;
/* We already own the trx_sys_t::mutex, by doing it here we
avoid a potential context switch later. */
read_view_remove(trx->global_read_view, true);
ut_ad(trx_sys_validate_trx_list());
mutex_exit(&trx_sys->mutex);
}
if (trx->global_read_view != NULL) {
trx->global_read_view = NULL;
}
trx->read_view = NULL;
if (lsn) {
ulint flush_log_at_trx_commit;
DEBUG_SYNC_C("after_trx_committed_in_memory");
if (trx->insert_undo != NULL) {
trx_undo_insert_cleanup(trx);
}
if (srv_use_global_flush_log_at_trx_commit) {
flush_log_at_trx_commit = thd_flush_log_at_trx_commit(NULL);
} else {
flush_log_at_trx_commit = thd_flush_log_at_trx_commit(trx->mysql_thd);
}
/* NOTE that we could possibly make a group commit more
efficient here: call os_thread_yield here to allow also other
trxs to come to commit! */
/*-------------------------------------*/
/* Depending on the my.cnf options, we may now write the log
buffer to the log files, making the transaction durable if
the OS does not crash. We may also flush the log files to
disk, making the transaction durable also at an OS crash or a
power outage.
The idea in InnoDB's group commit is that a group of
transactions gather behind a trx doing a physical disk write
to log files, and when that physical write has been completed,
one of those transactions does a write which commits the whole
group. Note that this group commit will only bring benefit if
there are > 2 users in the database. Then at least 2 users can
gather behind one doing the physical log write to disk.
If we are calling trx_commit() under prepare_commit_mutex, we
will delay possible log write and flush to a separate function
trx_commit_complete_for_mysql(), which is only called when the
thread has released the mutex. This is to make the
group commit algorithm to work. Otherwise, the prepare_commit
mutex would serialize all commits and prevent a group of
transactions from gathering. */
if (trx->flush_log_later) {
/* Do nothing yet */
trx->must_flush_log_later = TRUE;
} else if (flush_log_at_trx_commit == 0
|| thd_requested_durability(trx->mysql_thd)
== HA_IGNORE_DURABILITY) {
/* Do nothing */
} else {
trx_flush_log_if_needed(lsn, trx);
}
trx->commit_lsn = lsn;
/* Tell server some activity has happened, since the trx
does changes something. Background utility threads like
master thread, purge thread or page_cleaner thread might
have some work to do. */
srv_active_wake_master_thread();
}
/* undo_no is non-zero if we're doing the final commit. */
bool not_rollback = trx->undo_no != 0;
/* Free all savepoints, starting from the first. */
trx_named_savept_t* savep = UT_LIST_GET_FIRST(trx->trx_savepoints);
trx_roll_savepoints_free(trx, savep);
trx->rseg = NULL;
trx->undo_no = 0;
trx->last_sql_stat_start.least_undo_no = 0;
trx->ddl = false;
#ifdef UNIV_DEBUG
ut_ad(trx->start_file != 0);
ut_ad(trx->start_line != 0);
trx->start_file = 0;
trx->start_line = 0;
#endif /* UNIV_DEBUG */
trx->will_lock = 0;
trx->read_only = FALSE;
trx->auto_commit = FALSE;
if (trx->fts_trx) {
trx_finalize_for_fts(trx, not_rollback);
}
ut_ad(trx->lock.wait_thr == NULL);
ut_ad(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0);
ut_ad(!trx->in_ro_trx_list);
ut_ad(!trx->in_rw_trx_list);
#ifdef WITH_WSREP
if (trx->mysql_thd && wsrep_on(trx->mysql_thd)) {
trx->lock.was_chosen_as_deadlock_victim = FALSE;
}
#endif
trx->dict_operation = TRX_DICT_OP_NONE;
trx->error_state = DB_SUCCESS;
/* trx->in_mysql_trx_list would hold between
trx_allocate_for_mysql() and trx_free_for_mysql(). It does not
hold for recovered transactions or system transactions. */
}
/****************************************************************//**
Commits a transaction and a mini-transaction. */
UNIV_INTERN
void
trx_commit_low(
/*===========*/
trx_t* trx, /*!< in/out: transaction */
mtr_t* mtr) /*!< in/out: mini-transaction (will be committed),
or NULL if trx made no modifications */
{
lsn_t lsn;
assert_trx_nonlocking_or_in_list(trx);
ut_ad(!trx_state_eq(trx, TRX_STATE_COMMITTED_IN_MEMORY));
ut_ad(!mtr || mtr->state == MTR_ACTIVE);
ut_ad(!mtr == !(trx->insert_undo || trx->update_undo));
/* undo_no is non-zero if we're doing the final commit. */
if (trx->fts_trx && trx->undo_no != 0) {
dberr_t error;
ut_a(!trx_is_autocommit_non_locking(trx));
error = fts_commit(trx);
/* FTS-FIXME: Temporarily tolerate DB_DUPLICATE_KEY
instead of dying. This is a possible scenario if there
is a crash between insert to DELETED table committing
and transaction committing. The fix would be able to
return error from this function */
if (error != DB_SUCCESS && error != DB_DUPLICATE_KEY) {
/* FTS-FIXME: once we can return values from this
function, we should do so and signal an error
instead of just dying. */
ut_error;
}
}
if (mtr) {
trx_write_serialisation_history(trx, mtr);
/* The following call commits the mini-transaction, making the
whole transaction committed in the file-based world, at this
log sequence number. The transaction becomes 'durable' when
we write the log to disk, but in the logical sense the commit
in the file-based data structures (undo logs etc.) happens
here.
NOTE that transaction numbers, which are assigned only to
transactions with an update undo log, do not necessarily come
in exactly the same order as commit lsn's, if the transactions
have different rollback segments. To get exactly the same
order we should hold the kernel mutex up to this point,
adding to the contention of the kernel mutex. However, if
a transaction T2 is able to see modifications made by
a transaction T1, T2 will always get a bigger transaction
number and a bigger commit lsn than T1. */
/*--------------*/
mtr_commit(mtr);
/*--------------*/
lsn = mtr->end_lsn;
} else {
lsn = 0;
}
trx_commit_in_memory(trx, lsn);
}
/****************************************************************//**
Commits a transaction. */
UNIV_INTERN
void
trx_commit(
/*=======*/
trx_t* trx) /*!< in/out: transaction */
{
mtr_t local_mtr;
mtr_t* mtr;
if (trx->insert_undo || trx->update_undo) {
mtr = &local_mtr;
mtr_start(mtr);
} else {
mtr = NULL;
}
trx_commit_low(trx, mtr);
}
/****************************************************************//**
Cleans up a transaction at database startup. The cleanup is needed if
the transaction already got to the middle of a commit when the database
crashed, and we cannot roll it back. */
UNIV_INTERN
void
trx_cleanup_at_db_startup(
/*======================*/
trx_t* trx) /*!< in: transaction */
{
ut_ad(trx->is_recovered);
if (trx->insert_undo != NULL) {
trx_undo_insert_cleanup(trx);
}
trx->rseg = NULL;
trx->undo_no = 0;
trx->last_sql_stat_start.least_undo_no = 0;
mutex_enter(&trx_sys->mutex);
ut_a(!trx->read_only);
UT_LIST_REMOVE(trx_list, trx_sys->rw_trx_list, trx);
ut_ad(trx_sys->descr_n_used <= UT_LIST_GET_LEN(trx_sys->rw_trx_list));
assert_trx_in_rw_list(trx);
ut_d(trx->in_rw_trx_list = FALSE);
trx->state = TRX_STATE_NOT_STARTED;
trx_release_descriptor(trx);
mutex_exit(&trx_sys->mutex);
/* Change the transaction state without mutex protection, now
that it no longer is in the trx_list. Recovered transactions
are never placed in the mysql_trx_list. */
ut_ad(trx->is_recovered);
ut_ad(!trx->in_ro_trx_list);
ut_ad(!trx->in_rw_trx_list);
ut_ad(!trx->in_mysql_trx_list);
}
/********************************************************************//**
Assigns a read view for a consistent read query. All the consistent reads
within the same transaction will get the same read view, which is created
when this function is first called for a new started transaction.
@return consistent read view */
UNIV_INTERN
read_view_t*
trx_assign_read_view(
/*=================*/
trx_t* trx) /*!< in: active transaction */
{
ut_ad(trx->state == TRX_STATE_ACTIVE);
if (trx->read_view != NULL) {
return(trx->read_view);
}
trx->read_view = read_view_open_now(trx->id, trx->prebuilt_view);
trx->global_read_view = trx->read_view;
return(trx->read_view);
}
/********************************************************************//**
Clones the read view from another transaction. All consistent reads within
the receiver transaction will get the same read view as the donor transaction
@return read view clone */
UNIV_INTERN
read_view_t*
trx_clone_read_view(
/*================*/
trx_t* trx, /*!< in: receiver transaction */
trx_t* from_trx) /*!< in: donor transaction */
{
ut_ad(lock_mutex_own());
ut_ad(mutex_own(&trx_sys->mutex));
ut_ad(trx_mutex_own(from_trx));
ut_ad(trx->read_view == NULL);
if (from_trx->state != TRX_STATE_ACTIVE ||
from_trx->read_view == NULL) {
return(NULL);
}
trx->read_view = read_view_clone(from_trx->read_view,
trx->prebuilt_view);
read_view_add(trx->read_view);
trx->global_read_view = trx->read_view;
return(trx->read_view);
}
/****************************************************************//**
Prepares a transaction for commit/rollback. */
UNIV_INTERN
void
trx_commit_or_rollback_prepare(
/*===========================*/
trx_t* trx) /*!< in/out: transaction */
{
/* We are reading trx->state without holding trx_sys->mutex
here, because the commit or rollback should be invoked for a
running (or recovered prepared) transaction that is associated
with the current thread. */
switch (trx->state) {
case TRX_STATE_NOT_STARTED:
#ifdef WITH_WSREP
ut_d(trx->start_file = __FILE__);
ut_d(trx->start_line = __LINE__);
#endif /* WITH_WSREP */
trx_start_low(trx);
/* fall through */
case TRX_STATE_ACTIVE:
case TRX_STATE_PREPARED:
/* If the trx is in a lock wait state, moves the waiting
query thread to the suspended state */
if (trx->lock.que_state == TRX_QUE_LOCK_WAIT) {
ulint sec;
ulint ms;
ib_uint64_t now;
ut_a(trx->lock.wait_thr != NULL);
trx->lock.wait_thr->state = QUE_THR_SUSPENDED;
trx->lock.wait_thr = NULL;
if (UNIV_UNLIKELY(trx->take_stats)) {
ut_usectime(&sec, &ms);
now = (ib_uint64_t)sec * 1000000 + ms;
trx->lock_que_wait_timer
+= (ulint)
(now - trx->lock_que_wait_ustarted);
}
trx->lock.que_state = TRX_QUE_RUNNING;
}
ut_a(trx->lock.n_active_thrs == 1);
return;
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
}
ut_error;
}
/*********************************************************************//**
Creates a commit command node struct.
@return own: commit node struct */
UNIV_INTERN
commit_node_t*
trx_commit_node_create(
/*===================*/
mem_heap_t* heap) /*!< in: mem heap where created */
{
commit_node_t* node;
node = static_cast<commit_node_t*>(mem_heap_alloc(heap, sizeof(*node)));
node->common.type = QUE_NODE_COMMIT;
node->state = COMMIT_NODE_SEND;
return(node);
}
/***********************************************************//**
Performs an execution step for a commit type node in a query graph.
@return query thread to run next, or NULL */
UNIV_INTERN
que_thr_t*
trx_commit_step(
/*============*/
que_thr_t* thr) /*!< in: query thread */
{
commit_node_t* node;
node = static_cast<commit_node_t*>(thr->run_node);
ut_ad(que_node_get_type(node) == QUE_NODE_COMMIT);
if (thr->prev_node == que_node_get_parent(node)) {
node->state = COMMIT_NODE_SEND;
}
if (node->state == COMMIT_NODE_SEND) {
trx_t* trx;
node->state = COMMIT_NODE_WAIT;
trx = thr_get_trx(thr);
ut_a(trx->lock.wait_thr == NULL);
ut_a(trx->lock.que_state != TRX_QUE_LOCK_WAIT);
trx_commit_or_rollback_prepare(trx);
trx->lock.que_state = TRX_QUE_COMMITTING;
trx_commit(trx);
ut_ad(trx->lock.wait_thr == NULL);
trx->lock.que_state = TRX_QUE_RUNNING;
thr = NULL;
} else {
ut_ad(node->state == COMMIT_NODE_WAIT);
node->state = COMMIT_NODE_SEND;
thr->run_node = que_node_get_parent(node);
}
return(thr);
}
/**********************************************************************//**
Does the transaction commit for MySQL.
@return DB_SUCCESS or error number */
UNIV_INTERN
dberr_t
trx_commit_for_mysql(
/*=================*/
trx_t* trx) /*!< in/out: transaction */
{
/* Because we do not do the commit by sending an Innobase
sig to the transaction, we must here make sure that trx has been
started. */
ut_a(trx);
switch (trx->state) {
case TRX_STATE_NOT_STARTED:
/* Update the info whether we should skip XA steps that eat
CPU time.
For the duration of the transaction trx->support_xa is
not reread from thd so any changes in the value take
effect in the next transaction. This is to avoid a
scenario where some undo log records generated by a
transaction contain XA information and other undo log
records, generated by the same transaction do not. */
trx->support_xa = thd_supports_xa(trx->mysql_thd);
ut_d(trx->start_file = __FILE__);
ut_d(trx->start_line = __LINE__);
trx_start_low(trx);
/* fall through */
case TRX_STATE_ACTIVE:
case TRX_STATE_PREPARED:
trx->op_info = "committing";
trx_commit(trx);
MONITOR_DEC(MONITOR_TRX_ACTIVE);
trx->op_info = "";
return(DB_SUCCESS);
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
}
ut_error;
return(DB_CORRUPTION);
}
/**********************************************************************//**
If required, flushes the log to disk if we called trx_commit_for_mysql()
with trx->flush_log_later == TRUE. */
UNIV_INTERN
void
trx_commit_complete_for_mysql(
/*==========================*/
trx_t* trx) /*!< in/out: transaction */
{
ut_a(trx);
if (!trx->must_flush_log_later
|| thd_requested_durability(trx->mysql_thd)
== HA_IGNORE_DURABILITY) {
return;
}
ulint flush_log_at_trx_commit;
flush_log_at_trx_commit = srv_use_global_flush_log_at_trx_commit
? thd_flush_log_at_trx_commit(NULL)
: thd_flush_log_at_trx_commit(trx->mysql_thd);
if (flush_log_at_trx_commit == 1 && trx->active_commit_ordered) {
return;
}
trx_flush_log_if_needed(trx->commit_lsn, trx);
trx->must_flush_log_later = FALSE;
}
/**********************************************************************//**
Marks the latest SQL statement ended. */
UNIV_INTERN
void
trx_mark_sql_stat_end(
/*==================*/
trx_t* trx) /*!< in: trx handle */
{
ut_a(trx);
switch (trx->state) {
case TRX_STATE_PREPARED:
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
case TRX_STATE_NOT_STARTED:
trx->undo_no = 0;
/* fall through */
case TRX_STATE_ACTIVE:
trx->last_sql_stat_start.least_undo_no = trx->undo_no;
if (trx->fts_trx) {
fts_savepoint_laststmt_refresh(trx);
}
return;
}
ut_error;
}
/**********************************************************************//**
Prints info about a transaction.
Caller must hold trx_sys->mutex. */
UNIV_INTERN
void
trx_print_low(
/*==========*/
FILE* f,
/*!< in: output stream */
const trx_t* trx,
/*!< in: transaction */
ulint max_query_len,
/*!< in: max query length to print,
or 0 to use the default max length */
ulint n_rec_locks,
/*!< in: lock_number_of_rows_locked(&trx->lock) */
ulint n_trx_locks,
/*!< in: length of trx->lock.trx_locks */
ulint heap_size)
/*!< in: mem_heap_get_size(trx->lock.lock_heap) */
{
ibool newline;
const char* op_info;
ut_ad(mutex_own(&trx_sys->mutex));
fprintf(f, "TRANSACTION " TRX_ID_FMT, trx->id);
/* trx->state cannot change from or to NOT_STARTED while we
are holding the trx_sys->mutex. It may change from ACTIVE to
PREPARED or COMMITTED. */
switch (trx->state) {
case TRX_STATE_NOT_STARTED:
fputs(", not started", f);
goto state_ok;
case TRX_STATE_ACTIVE:
fprintf(f, ", ACTIVE %lu sec",
(ulong) difftime(time(NULL), trx->start_time));
goto state_ok;
case TRX_STATE_PREPARED:
fprintf(f, ", ACTIVE (PREPARED) %lu sec",
(ulong) difftime(time(NULL), trx->start_time));
goto state_ok;
case TRX_STATE_COMMITTED_IN_MEMORY:
fputs(", COMMITTED IN MEMORY", f);
goto state_ok;
}
fprintf(f, ", state %lu", (ulong) trx->state);
ut_ad(0);
state_ok:
/* prevent a race condition */
op_info = trx->op_info;
if (*op_info) {
putc(' ', f);
fputs(op_info, f);
}
if (trx->is_recovered) {
fputs(" recovered trx", f);
}
if (trx->declared_to_be_inside_innodb) {
fprintf(f, ", thread declared inside InnoDB %lu",
(ulong) trx->n_tickets_to_enter_innodb);
}
putc('\n', f);
if (trx->n_mysql_tables_in_use > 0 || trx->mysql_n_tables_locked > 0) {
fprintf(f, "mysql tables in use %lu, locked %lu\n",
(ulong) trx->n_mysql_tables_in_use,
(ulong) trx->mysql_n_tables_locked);
}
newline = TRUE;
/* trx->lock.que_state of an ACTIVE transaction may change
while we are not holding trx->mutex. We perform a dirty read
for performance reasons. */
switch (trx->lock.que_state) {
case TRX_QUE_RUNNING:
newline = FALSE; break;
case TRX_QUE_LOCK_WAIT:
fputs("LOCK WAIT ", f); break;
case TRX_QUE_ROLLING_BACK:
fputs("ROLLING BACK ", f); break;
case TRX_QUE_COMMITTING:
fputs("COMMITTING ", f); break;
default:
fprintf(f, "que state %lu ", (ulong) trx->lock.que_state);
}
if (n_trx_locks > 0 || heap_size > 400) {
newline = TRUE;
fprintf(f, "%lu lock struct(s), heap size %lu,"
" %lu row lock(s)",
(ulong) n_trx_locks,
(ulong) heap_size,
(ulong) n_rec_locks);
}
if (trx->has_search_latch) {
newline = TRUE;
fputs(", holds adaptive hash latch", f);
}
if (trx->undo_no != 0) {
newline = TRUE;
fprintf(f, ", undo log entries " TRX_ID_FMT, trx->undo_no);
}
if (newline) {
putc('\n', f);
}
if (trx->mysql_thd != NULL) {
innobase_mysql_print_thd(
f, trx->mysql_thd, static_cast<uint>(max_query_len));
}
}
/**********************************************************************//**
Prints info about a transaction.
The caller must hold lock_sys->mutex and trx_sys->mutex.
When possible, use trx_print() instead. */
UNIV_INTERN
void
trx_print_latched(
/*==============*/
FILE* f, /*!< in: output stream */
const trx_t* trx, /*!< in: transaction */
ulint max_query_len) /*!< in: max query length to print,
or 0 to use the default max length */
{
ut_ad(lock_mutex_own());
ut_ad(mutex_own(&trx_sys->mutex));
trx_print_low(f, trx, max_query_len,
lock_number_of_rows_locked(&trx->lock),
UT_LIST_GET_LEN(trx->lock.trx_locks),
mem_heap_get_size(trx->lock.lock_heap));
}
#ifdef WITH_WSREP
/**********************************************************************//**
Prints info about a transaction.
Transaction information may be retrieved without having trx_sys->mutex acquired
so it may not be completely accurate. The caller must own lock_sys->mutex
and the trx must have some locks to make sure that it does not escape
without locking lock_sys->mutex. */
UNIV_INTERN
void
wsrep_trx_print_locking(
/*==========*/
FILE* f,
/*!< in: output stream */
const trx_t* trx,
/*!< in: transaction */
ulint max_query_len)
/*!< in: max query length to print,
or 0 to use the default max length */
{
ibool newline;
const char* op_info;
ut_ad(lock_mutex_own());
ut_ad(trx->lock.trx_locks.count > 0);
fprintf(f, "TRANSACTION " TRX_ID_FMT, trx->id);
/* trx->state may change since trx_sys->mutex is not required */
switch (trx->state) {
case TRX_STATE_NOT_STARTED:
fputs(", not started", f);
goto state_ok;
case TRX_STATE_ACTIVE:
fprintf(f, ", ACTIVE %lu sec",
(ulong) difftime(time(NULL), trx->start_time));
goto state_ok;
case TRX_STATE_PREPARED:
fprintf(f, ", ACTIVE (PREPARED) %lu sec",
(ulong) difftime(time(NULL), trx->start_time));
goto state_ok;
case TRX_STATE_COMMITTED_IN_MEMORY:
fputs(", COMMITTED IN MEMORY", f);
goto state_ok;
}
fprintf(f, ", state %lu", (ulong) trx->state);
ut_ad(0);
state_ok:
/* prevent a race condition */
op_info = trx->op_info;
if (*op_info) {
putc(' ', f);
fputs(op_info, f);
}
if (trx->is_recovered) {
fputs(" recovered trx", f);
}
if (trx->declared_to_be_inside_innodb) {
fprintf(f, ", thread declared inside InnoDB %lu",
(ulong) trx->n_tickets_to_enter_innodb);
}
putc('\n', f);
if (trx->n_mysql_tables_in_use > 0 || trx->mysql_n_tables_locked > 0) {
fprintf(f, "mysql tables in use %lu, locked %lu\n",
(ulong) trx->n_mysql_tables_in_use,
(ulong) trx->mysql_n_tables_locked);
}
newline = TRUE;
/* trx->lock.que_state of an ACTIVE transaction may change
while we are not holding trx->mutex. We perform a dirty read
for performance reasons. */
switch (trx->lock.que_state) {
case TRX_QUE_RUNNING:
newline = FALSE; break;
case TRX_QUE_LOCK_WAIT:
fputs("LOCK WAIT ", f); break;
case TRX_QUE_ROLLING_BACK:
fputs("ROLLING BACK ", f); break;
case TRX_QUE_COMMITTING:
fputs("COMMITTING ", f); break;
default:
fprintf(f, "que state %lu ", (ulong) trx->lock.que_state);
}
if (trx->has_search_latch) {
newline = TRUE;
fputs(", holds adaptive hash latch", f);
}
if (trx->undo_no != 0) {
newline = TRUE;
fprintf(f, ", undo log entries " TRX_ID_FMT, trx->undo_no);
}
if (newline) {
putc('\n', f);
}
if (trx->mysql_thd != NULL) {
innobase_mysql_print_thd(
f, trx->mysql_thd, static_cast<uint>(max_query_len));
}
}
#endif /* WITH_WSREP */
/**********************************************************************//**
Prints info about a transaction.
Acquires and releases lock_sys->mutex and trx_sys->mutex. */
UNIV_INTERN
void
trx_print(
/*======*/
FILE* f, /*!< in: output stream */
const trx_t* trx, /*!< in: transaction */
ulint max_query_len) /*!< in: max query length to print,
or 0 to use the default max length */
{
ulint n_rec_locks;
ulint n_trx_locks;
ulint heap_size;
lock_mutex_enter();
n_rec_locks = lock_number_of_rows_locked(&trx->lock);
n_trx_locks = UT_LIST_GET_LEN(trx->lock.trx_locks);
heap_size = mem_heap_get_size(trx->lock.lock_heap);
lock_mutex_exit();
mutex_enter(&trx_sys->mutex);
trx_print_low(f, trx, max_query_len,
n_rec_locks, n_trx_locks, heap_size);
mutex_exit(&trx_sys->mutex);
}
#ifdef UNIV_DEBUG
/**********************************************************************//**
Asserts that a transaction has been started.
The caller must hold trx_sys->mutex.
@return TRUE if started */
UNIV_INTERN
ibool
trx_assert_started(
/*===============*/
const trx_t* trx) /*!< in: transaction */
{
ut_ad(mutex_own(&trx_sys->mutex));
/* Non-locking autocommits should not hold any locks and this
function is only called from the locking code. */
assert_trx_in_list(trx);
/* trx->state can change from or to NOT_STARTED while we are holding
trx_sys->mutex for non-locking autocommit selects but not for other
types of transactions. It may change from ACTIVE to PREPARED. Unless
we are holding lock_sys->mutex, it may also change to COMMITTED. */
switch (trx->state) {
case TRX_STATE_PREPARED:
return(TRUE);
case TRX_STATE_ACTIVE:
case TRX_STATE_COMMITTED_IN_MEMORY:
return(TRUE);
case TRX_STATE_NOT_STARTED:
break;
}
ut_error;
return(FALSE);
}
#endif /* UNIV_DEBUG */
/*******************************************************************//**
Compares the "weight" (or size) of two transactions. The heavier the weight,
the more reluctant we will be to choose the transaction as a deadlock victim.
@return TRUE if weight(a) >= weight(b) */
UNIV_INTERN
ibool
trx_weight_ge(
/*==========*/
const trx_t* a, /*!< in: the first transaction to be compared */
const trx_t* b) /*!< in: the second transaction to be compared */
{
int pref;
/* First ask the upper server layer if it has any preference for which
to prefer as a deadlock victim. */
pref= thd_deadlock_victim_preference(a->mysql_thd, b->mysql_thd);
if (pref < 0) {
return FALSE;
} else if (pref > 0) {
return TRUE;
}
/* Upper server layer had no preference, we fall back to comparing the
number of altered/locked rows. */
#if 0
fprintf(stderr,
"%s TRX_WEIGHT(a): %lld+%lu, TRX_WEIGHT(b): %lld+%lu\n",
__func__,
a->undo_no, UT_LIST_GET_LEN(a->lock.trx_locks),
b->undo_no, UT_LIST_GET_LEN(b->lock.trx_locks));
#endif
return(TRX_WEIGHT(a) >= TRX_WEIGHT(b));
}
/****************************************************************//**
Prepares a transaction. */
static
void
trx_prepare(
/*========*/
trx_t* trx) /*!< in/out: transaction */
{
trx_rseg_t* rseg;
lsn_t lsn;
mtr_t mtr;
rseg = trx->rseg;
/* Only fresh user transactions can be prepared.
Recovered transactions cannot. */
ut_a(!trx->is_recovered);
if (trx->insert_undo != NULL || trx->update_undo != NULL) {
mtr_start(&mtr);
/* Change the undo log segment states from TRX_UNDO_ACTIVE
to TRX_UNDO_PREPARED: these modifications to the file data
structure define the transaction as prepared in the
file-based world, at the serialization point of lsn. */
mutex_enter(&rseg->mutex);
if (trx->insert_undo != NULL) {
/* It is not necessary to obtain trx->undo_mutex here
because only a single OS thread is allowed to do the
transaction prepare for this transaction. */
trx_undo_set_state_at_prepare(trx, trx->insert_undo,
&mtr);
}
if (trx->update_undo) {
trx_undo_set_state_at_prepare(
trx, trx->update_undo, &mtr);
}
mutex_exit(&rseg->mutex);
/*--------------*/
mtr_commit(&mtr); /* This mtr commit makes the
transaction prepared in the file-based
world */
/*--------------*/
lsn = mtr.end_lsn;
ut_ad(lsn);
} else {
lsn = 0;
}
/*--------------------------------------*/
ut_a(trx->state == TRX_STATE_ACTIVE);
mutex_enter(&trx_sys->mutex);
trx->state = TRX_STATE_PREPARED;
trx_sys->n_prepared_trx++;
mutex_exit(&trx_sys->mutex);
/*--------------------------------------*/
if (lsn) {
/* Depending on the my.cnf options, we may now write the log
buffer to the log files, making the prepared state of the
transaction durable if the OS does not crash. We may also
flush the log files to disk, making the prepared state of the
transaction durable also at an OS crash or a power outage.
The idea in InnoDB's group prepare is that a group of
transactions gather behind a trx doing a physical disk write
to log files, and when that physical write has been completed,
one of those transactions does a write which prepares the whole
group. Note that this group prepare will only bring benefit if
there are > 2 users in the database. Then at least 2 users can
gather behind one doing the physical log write to disk.
TODO: find out if MySQL holds some mutex when calling this.
That would spoil our group prepare algorithm. */
trx_flush_log_if_needed(lsn, trx);
}
}
/**********************************************************************//**
Does the transaction prepare for MySQL. */
UNIV_INTERN
void
trx_prepare_for_mysql(
/*==================*/
trx_t* trx) /*!< in/out: trx handle */
{
trx_start_if_not_started_xa(trx);
trx->op_info = "preparing";
trx_prepare(trx);
trx->op_info = "";
}
/**********************************************************************//**
This function is used to find number of prepared transactions and
their transaction objects for a recovery.
@return number of prepared transactions stored in xid_list */
UNIV_INTERN
int
trx_recover_for_mysql(
/*==================*/
XID* xid_list, /*!< in/out: prepared transactions */
ulint len) /*!< in: number of slots in xid_list */
{
const trx_t* trx;
ulint count = 0;
ut_ad(xid_list);
ut_ad(len);
/* We should set those transactions which are in the prepared state
to the xid_list */
mutex_enter(&trx_sys->mutex);
for (trx = UT_LIST_GET_FIRST(trx_sys->rw_trx_list);
trx != NULL;
trx = UT_LIST_GET_NEXT(trx_list, trx)) {
assert_trx_in_rw_list(trx);
/* The state of a read-write transaction cannot change
from or to NOT_STARTED while we are holding the
trx_sys->mutex. It may change to PREPARED, but not if
trx->is_recovered. It may also change to COMMITTED. */
if (trx_state_eq(trx, TRX_STATE_PREPARED)) {
xid_list[count] = trx->xid;
if (count == 0) {
ut_print_timestamp(stderr);
fprintf(stderr,
" InnoDB: Starting recovery for"
" XA transactions...\n");
}
ut_print_timestamp(stderr);
fprintf(stderr,
" InnoDB: Transaction " TRX_ID_FMT " in"
" prepared state after recovery\n",
trx->id);
ut_print_timestamp(stderr);
fprintf(stderr,
" InnoDB: Transaction contains changes"
" to " TRX_ID_FMT " rows\n",
trx->undo_no);
count++;
if (count == len) {
break;
}
}
}
mutex_exit(&trx_sys->mutex);
if (count > 0){
ut_print_timestamp(stderr);
fprintf(stderr,
" InnoDB: %d transactions in prepared state"
" after recovery\n",
int (count));
}
return(int (count));
}
/*******************************************************************//**
This function is used to find one X/Open XA distributed transaction
which is in the prepared state
@return trx on match, the trx->xid will be invalidated;
note that the trx may have been committed, unless the caller is
holding lock_sys->mutex */
static MY_ATTRIBUTE((nonnull, warn_unused_result))
trx_t*
trx_get_trx_by_xid_low(
/*===================*/
const XID* xid) /*!< in: X/Open XA transaction
identifier */
{
trx_t* trx;
ut_ad(mutex_own(&trx_sys->mutex));
for (trx = UT_LIST_GET_FIRST(trx_sys->rw_trx_list);
trx != NULL;
trx = UT_LIST_GET_NEXT(trx_list, trx)) {
assert_trx_in_rw_list(trx);
/* Compare two X/Open XA transaction id's: their
length should be the same and binary comparison
of gtrid_length+bqual_length bytes should be
the same */
if (trx->is_recovered
&& trx_state_eq(trx, TRX_STATE_PREPARED)
&& !trx->xid.is_null()
&& xid->gtrid_length == trx->xid.gtrid_length
&& xid->bqual_length == trx->xid.bqual_length
&& memcmp(xid->data, trx->xid.data,
xid->gtrid_length + xid->bqual_length) == 0) {
/* Invalidate the XID, so that subsequent calls
will not find it. */
trx->xid.null();
break;
}
}
return(trx);
}
/*******************************************************************//**
This function is used to find one X/Open XA distributed transaction
which is in the prepared state
@return trx or NULL; on match, the trx->xid will be invalidated;
note that the trx may have been committed, unless the caller is
holding lock_sys->mutex */
UNIV_INTERN
trx_t*
trx_get_trx_by_xid(
/*===============*/
const XID* xid) /*!< in: X/Open XA transaction identifier */
{
trx_t* trx;
if (xid == NULL) {
return(NULL);
}
mutex_enter(&trx_sys->mutex);
/* Recovered/Resurrected transactions are always only on the
trx_sys_t::rw_trx_list. */
trx = trx_get_trx_by_xid_low(xid);
mutex_exit(&trx_sys->mutex);
return(trx);
}
/*************************************************************//**
Starts the transaction if it is not yet started. */
UNIV_INTERN
void
trx_start_if_not_started_xa_low(
/*============================*/
trx_t* trx) /*!< in: transaction */
{
switch (trx->state) {
case TRX_STATE_NOT_STARTED:
/* Update the info whether we should skip XA steps
that eat CPU time.
For the duration of the transaction trx->support_xa is
not reread from thd so any changes in the value take
effect in the next transaction. This is to avoid a
scenario where some undo generated by a transaction,
has XA stuff, and other undo, generated by the same
transaction, doesn't. */
trx->support_xa = thd_supports_xa(trx->mysql_thd);
trx_start_low(trx);
/* fall through */
case TRX_STATE_ACTIVE:
return;
case TRX_STATE_PREPARED:
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
}
ut_error;
}
/*************************************************************//**
Starts the transaction if it is not yet started. */
UNIV_INTERN
void
trx_start_if_not_started_low(
/*=========================*/
trx_t* trx) /*!< in: transaction */
{
switch (trx->state) {
case TRX_STATE_NOT_STARTED:
#ifdef WITH_WSREP
ut_d(trx->start_file = __FILE__);
ut_d(trx->start_line = __LINE__);
#endif /* WITH_WSREP */
trx_start_low(trx);
/* fall through */
case TRX_STATE_ACTIVE:
return;
case TRX_STATE_PREPARED:
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
}
ut_error;
}
/*************************************************************//**
Starts the transaction for a DDL operation. */
UNIV_INTERN
void
trx_start_for_ddl_low(
/*==================*/
trx_t* trx, /*!< in/out: transaction */
trx_dict_op_t op) /*!< in: dictionary operation type */
{
switch (trx->state) {
case TRX_STATE_NOT_STARTED:
/* Flag this transaction as a dictionary operation, so that
the data dictionary will be locked in crash recovery. */
trx_set_dict_operation(trx, op);
/* Ensure it is not flagged as an auto-commit-non-locking
transation. */
trx->will_lock = 1;
trx->ddl = true;
#ifdef WITH_WSREP
ut_d(trx->start_file = __FILE__);
ut_d(trx->start_line = __LINE__);
#endif /* WITH_WSREP */
trx_start_low(trx);
return;
case TRX_STATE_ACTIVE:
/* We have this start if not started idiom, therefore we
can't add stronger checks here. */
trx->ddl = true;
ut_ad(trx->dict_operation != TRX_DICT_OP_NONE);
ut_ad(trx->will_lock > 0);
return;
case TRX_STATE_PREPARED:
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
}
ut_error;
}
|