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
|
/*****************************************************************************
Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2015, 2022, 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, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file trx/trx0trx.cc
The transaction
Created 3/26/1996 Heikki Tuuri
*******************************************************/
#include "trx0trx.h"
#ifdef WITH_WSREP
#include <mysql/service_wsrep.h>
#endif
#include <mysql/service_thd_error_context.h>
#include "btr0sea.h"
#include "lock0lock.h"
#include "log0log.h"
#include "que0que.h"
#include "srv0mon.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "trx0purge.h"
#include "trx0rec.h"
#include "trx0roll.h"
#include "trx0rseg.h"
#include "trx0undo.h"
#include "trx0xa.h"
#include "ut0pool.h"
#include "ut0vec.h"
#include "log.h"
#include <set>
#include <new>
/** The bit pattern corresponding to TRX_ID_MAX */
const byte trx_id_max_bytes[8] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
/** The bit pattern corresponding to max timestamp */
const byte timestamp_max_bytes[7] = {
0x7f, 0xff, 0xff, 0xff, 0x0f, 0x42, 0x3f
};
static const ulint MAX_DETAILED_ERROR_LEN = 256;
/*************************************************************//**
Set detailed error message for the transaction. */
void
trx_set_detailed_error(
/*===================*/
trx_t* trx, /*!< in: transaction struct */
const char* msg) /*!< in: detailed error message */
{
strncpy(trx->detailed_error, msg, MAX_DETAILED_ERROR_LEN - 1);
trx->detailed_error[MAX_DETAILED_ERROR_LEN - 1] = '\0';
}
/*************************************************************//**
Set detailed error message for the transaction from a file. Note that the
file is rewinded before reading from it. */
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, MAX_DETAILED_ERROR_LEN);
}
/********************************************************************//**
Initialize transaction object.
@param trx trx to initialize */
static
void
trx_init(
/*=====*/
trx_t* trx)
{
trx->state = TRX_STATE_NOT_STARTED;
trx->is_recovered = false;
trx->op_info = "";
trx->active_commit_ordered = false;
trx->isolation_level = TRX_ISO_REPEATABLE_READ;
trx->check_foreigns = true;
trx->check_unique_secondary = true;
trx->lock.n_rec_locks = 0;
trx->dict_operation = false;
trx->error_state = DB_SUCCESS;
trx->error_key_num = ULINT_UNDEFINED;
trx->undo_no = 0;
trx->rsegs.m_redo.rseg = NULL;
trx->rsegs.m_noredo.rseg = NULL;
trx->read_only = false;
trx->auto_commit = false;
trx->will_lock = false;
trx->bulk_insert = false;
trx->apply_online_log = false;
ut_d(trx->start_file = 0);
ut_d(trx->start_line = 0);
trx->magic_n = TRX_MAGIC_N;
trx->last_sql_stat_start.least_undo_no = 0;
ut_ad(!trx->read_view.is_open());
trx->lock.rec_cached = 0;
trx->lock.table_cached = 0;
#ifdef WITH_WSREP
ut_ad(!trx->wsrep);
#endif /* WITH_WSREP */
}
/** For managing the life-cycle of the trx_t instance that we get
from the pool. */
struct TrxFactory {
/** 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.
@param trx Transaction instance to initialise */
static void init(trx_t* trx)
{
/* Explicitly call the constructor of the already
allocated object. trx_t objects are allocated by
ut_zalloc_nokey() in Pool::Pool() which would not call
the constructors of the trx_t members. */
new(&trx->mod_tables) trx_mod_tables_t();
new(&trx->lock.table_locks) lock_list();
new(&trx->read_view) ReadView();
trx->rw_trx_hash_pins = 0;
trx_init(trx);
trx->dict_operation_lock_mode = false;
trx->detailed_error = reinterpret_cast<char*>(
ut_zalloc_nokey(MAX_DETAILED_ERROR_LEN));
trx->lock.lock_heap = mem_heap_create_typed(
1024, MEM_HEAP_FOR_LOCK_HEAP);
pthread_cond_init(&trx->lock.cond, nullptr);
UT_LIST_INIT(trx->lock.trx_locks, &lock_t::trx_locks);
UT_LIST_INIT(trx->lock.evicted_tables,
&dict_table_t::table_LRU);
UT_LIST_INIT(
trx->trx_savepoints,
&trx_named_savept_t::trx_savepoints);
trx->mutex_init();
}
/** Release resources held by the transaction object.
@param trx the transaction for which to release resources */
static void destroy(trx_t* trx)
{
#ifdef __SANITIZE_ADDRESS__
/* Unpoison the memory for AddressSanitizer */
MEM_MAKE_ADDRESSABLE(trx, sizeof *trx);
#elif !__has_feature(memory_sanitizer)
/* In Valgrind, we cannot cancel MEM_NOACCESS() without
changing the state of the V bits (which indicate
which bits are initialized).
We will declare the contents as initialized.
We did invoke MEM_CHECK_DEFINED() in trx_t::free(). */
MEM_MAKE_DEFINED(trx, sizeof *trx);
#endif
ut_a(trx->magic_n == TRX_MAGIC_N);
ut_ad(!trx->mysql_thd);
ut_a(trx->lock.wait_lock == NULL);
ut_a(trx->lock.wait_thr == NULL);
ut_a(!trx->dict_operation_lock_mode);
if (trx->lock.lock_heap != NULL) {
mem_heap_free(trx->lock.lock_heap);
trx->lock.lock_heap = NULL;
}
pthread_cond_destroy(&trx->lock.cond);
ut_a(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0);
ut_ad(UT_LIST_GET_LEN(trx->lock.evicted_tables) == 0);
ut_free(trx->detailed_error);
trx->mutex_destroy();
trx->mod_tables.~trx_mod_tables_t();
ut_ad(!trx->read_view.is_open());
trx->lock.table_locks.~lock_list();
trx->read_view.~ReadView();
}
};
/** The lock strategy for TrxPool */
class TrxPoolLock
{
mysql_mutex_t mutex;
public:
/** Create the mutex */
void create()
{
mysql_mutex_init(trx_pool_mutex_key, &mutex, nullptr);
}
/** Acquire the mutex */
void enter() { mysql_mutex_lock(&mutex); }
/** Release the mutex */
void exit() { mysql_mutex_unlock(&mutex); }
/** Free the mutex */
void destroy() { mysql_mutex_destroy(&mutex); }
};
/** The lock strategy for the TrxPoolManager */
class TrxPoolManagerLock
{
mysql_mutex_t mutex;
public:
/** Create the mutex */
void create()
{
mysql_mutex_init(trx_pool_manager_mutex_key, &mutex, nullptr);
}
/** Acquire the mutex */
void enter() { mysql_mutex_lock(&mutex); }
/** Release the mutex */
void exit() { mysql_mutex_unlock(&mutex); }
/** Free the mutex */
void destroy() { mysql_mutex_destroy(&mutex); }
};
/** Use explicit mutexes for the trx_t pool and its manager. */
typedef Pool<trx_t, TrxFactory, TrxPoolLock> trx_pool_t;
typedef PoolManager<trx_pool_t, TrxPoolManagerLock > trx_pools_t;
/** The trx_t pool manager */
static trx_pools_t* trx_pools;
/** Size of on trx_t pool in bytes. */
static const ulint MAX_TRX_BLOCK_SIZE = 1024 * 1024 * 4;
/** Create the trx_t pool */
void
trx_pool_init()
{
trx_pools = UT_NEW_NOKEY(trx_pools_t(MAX_TRX_BLOCK_SIZE));
ut_a(trx_pools != 0);
}
/** Destroy the trx_t pool */
void
trx_pool_close()
{
UT_DELETE(trx_pools);
trx_pools = 0;
}
/** @return an allocated transaction */
trx_t *trx_create()
{
trx_t* trx = trx_pools->get();
#ifdef __SANITIZE_ADDRESS__
/* Unpoison the memory for AddressSanitizer.
It may have been poisoned in trx_t::free().*/
MEM_MAKE_ADDRESSABLE(trx, sizeof *trx);
#elif !__has_feature(memory_sanitizer)
/* In Valgrind, we cannot cancel MEM_NOACCESS() without
changing the state of the V bits (which indicate
which bits are initialized).
We will declare the contents as initialized.
We did invoke MEM_CHECK_DEFINED() in trx_t::free(). */
MEM_MAKE_DEFINED(trx, sizeof *trx);
#endif
trx->assert_freed();
mem_heap_t* heap;
ib_alloc_t* alloc;
/* We just got trx from pool, it should be non locking */
ut_ad(!trx->will_lock);
ut_ad(!trx->rw_trx_hash_pins);
DBUG_LOG("trx", "Create: " << trx);
heap = mem_heap_create(sizeof(ib_vector_t) + sizeof(void*) * 8);
alloc = ib_heap_allocator_create(heap);
trx->autoinc_locks = ib_vector_create(alloc, sizeof(void**), 4);
ut_ad(trx->mod_tables.empty());
ut_ad(trx->lock.n_rec_locks == 0);
ut_ad(trx->lock.table_cached == 0);
ut_ad(trx->lock.rec_cached == 0);
ut_ad(UT_LIST_GET_LEN(trx->lock.evicted_tables) == 0);
trx_sys.register_trx(trx);
return(trx);
}
/** Free the memory to trx_pools */
void trx_t::free()
{
#ifdef HAVE_MEM_CHECK
if (xid.is_null())
MEM_MAKE_DEFINED(&xid, sizeof xid);
else
MEM_MAKE_DEFINED(&xid.data[xid.gtrid_length + xid.bqual_length],
sizeof xid.data - (xid.gtrid_length + xid.bqual_length));
#endif
MEM_CHECK_DEFINED(this, sizeof *this);
ut_ad(!n_mysql_tables_in_use);
ut_ad(!mysql_log_file_name);
ut_ad(!mysql_n_tables_locked);
ut_ad(!will_lock);
ut_ad(error_state == DB_SUCCESS);
ut_ad(magic_n == TRX_MAGIC_N);
ut_ad(!read_only);
ut_ad(!lock.wait_lock);
dict_operation= false;
trx_sys.deregister_trx(this);
check_unique_secondary= true;
check_foreigns= true;
assert_freed();
trx_sys.rw_trx_hash.put_pins(this);
mysql_thd= nullptr;
// FIXME: We need to avoid this heap free/alloc for each commit.
if (autoinc_locks)
{
ut_ad(ib_vector_is_empty(autoinc_locks));
/* We allocated a dedicated heap for the vector. */
ib_vector_free(autoinc_locks);
autoinc_locks= NULL;
}
MEM_NOACCESS(&skip_lock_inheritance_and_n_ref,
sizeof skip_lock_inheritance_and_n_ref);
/* do not poison mutex */
MEM_NOACCESS(&id, sizeof id);
MEM_NOACCESS(&max_inactive_id, sizeof id);
MEM_NOACCESS(&state, sizeof state);
MEM_NOACCESS(&is_recovered, sizeof is_recovered);
#ifdef WITH_WSREP
MEM_NOACCESS(&wsrep, sizeof wsrep);
#endif
read_view.mem_noaccess();
MEM_NOACCESS(&lock, sizeof lock);
MEM_NOACCESS(&op_info, sizeof op_info +
sizeof(unsigned) /* isolation_level, snapshot_isolation,
check_foreigns, check_unique_secondary,
bulk_insert */);
MEM_NOACCESS(&is_registered, sizeof is_registered);
MEM_NOACCESS(&active_commit_ordered, sizeof active_commit_ordered);
MEM_NOACCESS(&flush_log_later, sizeof flush_log_later);
MEM_NOACCESS(&duplicates, sizeof duplicates);
MEM_NOACCESS(&dict_operation, sizeof dict_operation);
MEM_NOACCESS(&dict_operation_lock_mode, sizeof dict_operation_lock_mode);
MEM_NOACCESS(&start_time, sizeof start_time);
MEM_NOACCESS(&start_time_micro, sizeof start_time_micro);
MEM_NOACCESS(&commit_lsn, sizeof commit_lsn);
MEM_NOACCESS(&mysql_thd, sizeof mysql_thd);
MEM_NOACCESS(&mysql_log_file_name, sizeof mysql_log_file_name);
MEM_NOACCESS(&mysql_log_offset, sizeof mysql_log_offset);
MEM_NOACCESS(&n_mysql_tables_in_use, sizeof n_mysql_tables_in_use);
MEM_NOACCESS(&mysql_n_tables_locked, sizeof mysql_n_tables_locked);
MEM_NOACCESS(&error_state, sizeof error_state);
MEM_NOACCESS(&error_info, sizeof error_info);
MEM_NOACCESS(&error_key_num, sizeof error_key_num);
MEM_NOACCESS(&graph, sizeof graph);
MEM_NOACCESS(&trx_savepoints, sizeof trx_savepoints);
MEM_NOACCESS(&undo_no, sizeof undo_no);
MEM_NOACCESS(&last_sql_stat_start, sizeof last_sql_stat_start);
MEM_NOACCESS(&rsegs, sizeof rsegs);
MEM_NOACCESS(&roll_limit, sizeof roll_limit);
MEM_NOACCESS(&in_rollback, sizeof in_rollback);
MEM_NOACCESS(&pages_undone, sizeof pages_undone);
MEM_NOACCESS(&n_autoinc_rows, sizeof n_autoinc_rows);
MEM_NOACCESS(&autoinc_locks, sizeof autoinc_locks);
MEM_NOACCESS(&read_only, sizeof read_only);
MEM_NOACCESS(&auto_commit, sizeof auto_commit);
MEM_NOACCESS(&will_lock, sizeof will_lock);
MEM_NOACCESS(&fts_trx, sizeof fts_trx);
MEM_NOACCESS(&fts_next_doc_id, sizeof fts_next_doc_id);
MEM_NOACCESS(&flush_tables, sizeof flush_tables);
#ifdef UNIV_DEBUG
MEM_NOACCESS(&start_line, sizeof start_line);
MEM_NOACCESS(&start_file, sizeof start_file);
#endif /* UNIV_DEBUG */
MEM_NOACCESS(&xid, sizeof xid);
MEM_NOACCESS(&mod_tables, sizeof mod_tables);
MEM_NOACCESS(&detailed_error, sizeof detailed_error);
MEM_NOACCESS(&magic_n, sizeof magic_n);
MEM_NOACCESS(&apply_online_log, sizeof apply_online_log);
trx_pools->mem_free(this);
}
/** Transition to committed state, to release implicit locks. */
TRANSACTIONAL_INLINE inline void trx_t::commit_state()
{
ut_ad(state == TRX_STATE_PREPARED
|| state == TRX_STATE_PREPARED_RECOVERED
|| state == TRX_STATE_ACTIVE);
/* This makes the transaction committed in memory and makes its
changes to data visible to other transactions. NOTE that there is a
small discrepancy from the strict formal visibility rules here: a
user of the database can see modifications made by another
transaction T even before the necessary redo log segment has been
flushed to the disk. If the database happens to crash before the
flush, the user has seen modifications from T which will never be a
committed transaction. However, any transaction T2 which sees the
modifications of the committing transaction T, and which also itself
makes modifications to the database, will get an lsn larger than the
committing transaction T. In the case where the log flush fails, and
T never gets committed, also T2 will never get committed. */
TMTrxGuard tg{*this};
state= TRX_STATE_COMMITTED_IN_MEMORY;
ut_ad(id || !is_referenced());
}
/** Release any explicit locks of a committing transaction. */
inline void trx_t::release_locks()
{
DEBUG_SYNC_C("trx_t_release_locks_enter");
DBUG_ASSERT(state == TRX_STATE_COMMITTED_IN_MEMORY);
DBUG_ASSERT(!is_referenced());
if (UT_LIST_GET_LEN(lock.trx_locks))
{
lock_release(this);
ut_ad(!lock.n_rec_locks);
ut_ad(UT_LIST_GET_LEN(lock.trx_locks) == 0);
ut_ad(ib_vector_is_empty(autoinc_locks));
mem_heap_empty(lock.lock_heap);
}
lock.table_locks.clear();
reset_skip_lock_inheritance();
id= 0;
while (dict_table_t *table= UT_LIST_GET_FIRST(lock.evicted_tables))
{
UT_LIST_REMOVE(lock.evicted_tables, table);
dict_mem_table_free(table);
}
DEBUG_SYNC_C("after_trx_committed_in_memory");
}
/** At shutdown, frees a transaction object. */
TRANSACTIONAL_TARGET void trx_free_at_shutdown(trx_t *trx)
{
ut_ad(trx->is_recovered);
ut_a(trx_state_eq(trx, TRX_STATE_PREPARED)
|| trx_state_eq(trx, TRX_STATE_PREPARED_RECOVERED)
|| (trx_state_eq(trx, TRX_STATE_ACTIVE)
&& (!srv_was_started
|| srv_operation == SRV_OPERATION_RESTORE
|| srv_operation == SRV_OPERATION_RESTORE_EXPORT
|| srv_read_only_mode
|| srv_force_recovery >= SRV_FORCE_NO_TRX_UNDO
|| (!srv_is_being_started
&& !srv_undo_sources && srv_fast_shutdown))));
ut_a(trx->magic_n == TRX_MAGIC_N);
ut_d(trx->apply_online_log = false);
trx->commit_state();
trx->release_locks();
trx->mod_tables.clear();
trx_undo_free_at_shutdown(trx);
ut_a(!trx->read_only);
DBUG_LOG("trx", "Free prepared: " << trx);
trx->state = TRX_STATE_NOT_STARTED;
ut_ad(!UT_LIST_GET_LEN(trx->lock.trx_locks));
trx->free();
}
/**
Disconnect a prepared transaction from MySQL
@param[in,out] trx transaction
*/
void trx_disconnect_prepared(trx_t *trx)
{
ut_ad(trx_state_eq(trx, TRX_STATE_PREPARED));
ut_ad(trx->mysql_thd);
ut_ad(!trx->mysql_log_file_name);
trx->read_view.close();
trx_sys.trx_list.freeze();
trx->is_recovered= true;
trx->mysql_thd= NULL;
trx_sys.trx_list.unfreeze();
/* todo/fixme: suggest to do it at innodb prepare */
trx->will_lock= false;
trx_sys.rw_trx_hash.put_pins(trx);
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/** Resurrect the table locks for a resurrected transaction. */
static dberr_t trx_resurrect_table_locks(trx_t *trx, const trx_undo_t &undo)
{
ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE) ||
trx_state_eq(trx, TRX_STATE_PREPARED));
ut_ad(undo.rseg == trx->rsegs.m_redo.rseg);
if (undo.empty())
return DB_SUCCESS;
mtr_t mtr;
std::map<table_id_t, bool> tables;
mtr.start();
dberr_t err;
if (buf_block_t *block=
buf_page_get_gen(page_id_t(trx->rsegs.m_redo.rseg->space->id,
undo.top_page_no), 0, RW_S_LATCH, nullptr,
BUF_GET, &mtr, &err))
{
buf_page_make_young_if_needed(&block->page);
buf_block_t *undo_block= block;
const trx_undo_rec_t *undo_rec= block->page.frame + undo.top_offset;
do
{
byte type;
byte cmpl_info;
undo_no_t undo_no;
table_id_t table_id;
bool updated_extern;
if (undo_block != block)
{
mtr.release(*undo_block);
undo_block= block;
}
trx_undo_rec_get_pars(undo_rec, &type, &cmpl_info,
&updated_extern, &undo_no, &table_id);
tables.emplace(table_id, type == TRX_UNDO_EMPTY);
undo_rec= trx_undo_get_prev_rec(block, page_offset(undo_rec),
undo.hdr_page_no, undo.hdr_offset,
true, &mtr);
}
while (undo_rec);
}
mtr.commit();
if (err != DB_SUCCESS)
return err;
for (auto p : tables)
{
if (dict_table_t *table=
dict_table_open_on_id(p.first, FALSE, DICT_TABLE_OP_LOAD_TABLESPACE))
{
if (!table->is_readable())
{
dict_sys.lock(SRW_LOCK_CALL);
table->release();
dict_sys.remove(table);
dict_sys.unlock();
continue;
}
if (trx->state == TRX_STATE_PREPARED)
trx->mod_tables.emplace(table, 0);
lock_table_resurrect(table, trx, p.second ? LOCK_X : LOCK_IX);
DBUG_LOG("ib_trx",
"resurrect " << ib::hex(trx->id) << " lock on " << table->name);
table->release();
}
}
return DB_SUCCESS;
}
MY_ATTRIBUTE((nonnull, warn_unused_result))
/**
Resurrect the transactions that were doing inserts/updates the time of the
crash, they need to be undone.
*/
static dberr_t trx_resurrect(trx_undo_t *undo, trx_rseg_t *rseg,
time_t start_time, ulonglong start_time_micro,
uint64_t *rows_to_undo)
{
trx_state_t state;
ut_ad(rseg->needs_purge >= undo->trx_id);
/*
This is single-threaded startup code, we do not need the
protection of trx->mutex here.
*/
switch (undo->state)
{
case TRX_UNDO_ACTIVE:
state= TRX_STATE_ACTIVE;
break;
case TRX_UNDO_PREPARED:
/*
Prepared transactions are left in the prepared state
waiting for a commit or abort decision from MySQL
*/
state= TRX_STATE_PREPARED;
sql_print_information("InnoDB: Transaction " TRX_ID_FMT
" was in the XA prepared state.", undo->trx_id);
break;
default:
return DB_SUCCESS;
}
rseg->acquire();
trx_t *trx= trx_create();
trx->state= state;
ut_d(trx->start_file= __FILE__);
ut_d(trx->start_line= __LINE__);
trx->rsegs.m_redo.undo= undo;
trx->undo_no= undo->top_undo_no + 1;
trx->rsegs.m_redo.rseg= rseg;
trx->xid= undo->xid;
trx->id= undo->trx_id;
trx->is_recovered= true;
trx->start_time= start_time;
trx->start_time_micro= start_time_micro;
trx->dict_operation= undo->dict_operation;
trx_sys.rw_trx_hash.insert(trx);
trx_sys.rw_trx_hash.put_pins(trx);
if (trx_state_eq(trx, TRX_STATE_ACTIVE))
*rows_to_undo+= trx->undo_no;
return trx_resurrect_table_locks(trx, *undo);
}
/** Initialize (resurrect) transactions at startup. */
dberr_t trx_lists_init_at_db_start()
{
ut_a(srv_is_being_started);
ut_ad(!srv_was_started);
if (srv_operation == SRV_OPERATION_RESTORE) {
/* mariabackup --prepare only deals with
the redo log and the data files, not with
transactions or the data dictionary. */
return trx_rseg_array_init();
}
if (srv_force_recovery >= SRV_FORCE_NO_UNDO_LOG_SCAN) {
return DB_SUCCESS;
}
purge_sys.create();
dberr_t err = trx_rseg_array_init();
if (err != DB_SUCCESS) {
corrupted:
ib::info() << "Retry with innodb_force_recovery=5";
return err;
}
if (trx_sys.is_undo_empty()) {
func_exit:
purge_sys.clone_oldest_view<true>();
return DB_SUCCESS;
}
/* Look from the rollback segments if there exist undo logs for
transactions. */
const time_t start_time = time(NULL);
const ulonglong start_time_micro= microsecond_interval_timer();
uint64_t rows_to_undo = 0;
for (auto& rseg : trx_sys.rseg_array) {
trx_undo_t* undo;
/* Some rollback segment may be unavailable,
especially if the server was previously run with a
non-default value of innodb_undo_logs. */
if (!rseg.space) {
continue;
}
/* Resurrect other transactions. */
for (undo = UT_LIST_GET_FIRST(rseg.undo_list);
undo != NULL;
undo = UT_LIST_GET_NEXT(undo_list, undo)) {
trx_t *trx = trx_sys.find(0, undo->trx_id, false);
if (!trx) {
err = trx_resurrect(undo, &rseg, start_time,
start_time_micro,
&rows_to_undo);
} else {
ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE) ||
trx_state_eq(trx, TRX_STATE_PREPARED));
ut_ad(trx->start_time == start_time);
ut_ad(trx->is_recovered);
ut_ad(trx->rsegs.m_redo.rseg == &rseg);
ut_ad(rseg.is_referenced());
ut_ad(rseg.needs_purge);
trx->rsegs.m_redo.undo = undo;
if (undo->top_undo_no >= trx->undo_no) {
if (trx_state_eq(trx,
TRX_STATE_ACTIVE)) {
rows_to_undo -= trx->undo_no;
rows_to_undo +=
undo->top_undo_no + 1;
}
trx->undo_no = undo->top_undo_no + 1;
}
err = trx_resurrect_table_locks(trx, *undo);
}
if (err != DB_SUCCESS) {
goto corrupted;
}
}
}
if (const auto size = trx_sys.rw_trx_hash.size()) {
ib::info() << size
<< " transaction(s) which must be rolled back or"
" cleaned up in total " << rows_to_undo
<< " row operations to undo";
ib::info() << "Trx id counter is " << trx_sys.get_max_trx_id();
}
goto func_exit;
}
/** Assign a persistent rollback segment in a round-robin fashion,
evenly distributed between 0 and innodb_undo_logs-1
@param trx transaction */
static void trx_assign_rseg_low(trx_t *trx)
{
ut_ad(!trx->rsegs.m_redo.rseg);
ut_ad(srv_available_undo_logs == TRX_SYS_N_RSEGS);
/* The first slot is always assigned to the system tablespace. */
ut_ad(trx_sys.rseg_array[0].space == fil_system.sys_space);
trx_sys.register_rw(trx);
ut_ad(trx->id);
/* Choose a rollback segment evenly distributed between 0 and
innodb_undo_logs-1 in a round-robin fashion, skipping those
undo tablespaces that are scheduled for truncation. */
static Atomic_counter<unsigned> rseg_slot;
unsigned slot = rseg_slot++ % TRX_SYS_N_RSEGS;
ut_d(if (trx_rseg_n_slots_debug) slot = 0);
ut_d(const auto start_scan_slot = slot);
ut_d(bool look_for_rollover = false);
trx_rseg_t* rseg;
bool allocated;
do {
for (;;) {
rseg = &trx_sys.rseg_array[slot];
ut_ad(!look_for_rollover || start_scan_slot != slot);
ut_d(look_for_rollover = true);
ut_d(if (!trx_rseg_n_slots_debug))
slot = (slot + 1) % TRX_SYS_N_RSEGS;
if (!rseg->space) {
continue;
}
ut_ad(rseg->is_persistent());
if (rseg->space != fil_system.sys_space) {
if (rseg->skip_allocation()) {
continue;
}
} else if (const fil_space_t *space =
trx_sys.rseg_array[slot].space) {
if (space != fil_system.sys_space
&& srv_undo_tablespaces > 0) {
/** If dedicated
innodb_undo_tablespaces have
been configured, try to use them
instead of the system tablespace. */
continue;
}
}
break;
}
/* By now we have only selected the rseg but not marked it
allocated. By marking it allocated we are ensuring that it will
never be selected for UNDO truncate purge. */
allocated = rseg->acquire_if_available();
} while (!allocated);
trx->rsegs.m_redo.rseg = rseg;
}
/** Assign a rollback segment for modifying temporary tables.
@return the assigned rollback segment */
trx_rseg_t *trx_t::assign_temp_rseg()
{
ut_ad(!rsegs.m_noredo.rseg);
ut_ad(!is_autocommit_non_locking());
compile_time_assert(ut_is_2pow(TRX_SYS_N_RSEGS));
/* Choose a temporary rollback segment between 0 and 127
in a round-robin fashion. */
static Atomic_counter<unsigned> rseg_slot;
trx_rseg_t* rseg = &trx_sys.temp_rsegs[
rseg_slot++ & (TRX_SYS_N_RSEGS - 1)];
ut_ad(!rseg->is_persistent());
rsegs.m_noredo.rseg = rseg;
if (id == 0) {
trx_sys.register_rw(this);
}
return(rseg);
}
/****************************************************************//**
Starts a transaction. */
static
void
trx_start_low(
/*==========*/
trx_t* trx, /*!< in: transaction */
bool read_write) /*!< in: true if read-write transaction */
{
ut_ad(!trx->in_rollback);
ut_ad(!trx->is_recovered);
ut_ad(trx->start_line != 0);
ut_ad(trx->start_file != 0);
ut_ad(trx->roll_limit == 0);
ut_ad(trx->error_state == DB_SUCCESS);
ut_ad(trx->rsegs.m_redo.rseg == NULL);
ut_ad(trx->rsegs.m_noredo.rseg == NULL);
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 = thd_trx_is_auto_commit(trx->mysql_thd);
trx->read_only = srv_read_only_mode
|| (!trx->dict_operation
&& thd_trx_is_read_only(trx->mysql_thd));
if (!trx->auto_commit) {
trx->will_lock = true;
} else if (!trx->will_lock) {
trx->read_only = true;
}
#ifdef WITH_WSREP
trx->xid.null();
#endif /* WITH_WSREP */
ut_a(ib_vector_is_empty(trx->autoinc_locks));
ut_a(trx->lock.table_locks.empty());
/* No other thread can access this trx object through rw_trx_hash,
still it can be found through trx_sys.trx_list. Sometimes it's
possible to indirectly protect trx_t::state by freezing
trx_sys.trx_list.
For now we update it without mutex protection, because original code
did it this way. It has to be reviewed and fixed properly. */
trx->state = TRX_STATE_ACTIVE;
/* By default all transactions are in the read-only list unless they
are non-locking auto-commit read only transactions or background
(internal) transactions. Note: Transactions marked explicitly as
read only can write to temporary tables, we put those on the RO
list too. */
if (!trx->read_only
&& (!trx->mysql_thd || read_write || trx->dict_operation)) {
/* Temporary rseg is assigned only if the transaction
updates a temporary table */
if (!high_level_read_only) {
trx_assign_rseg_low(trx);
}
} else {
if (!trx->is_autocommit_non_locking()) {
/* If this is a read-only transaction that is writing
to a temporary table then it needs a transaction id
to write to the temporary table. */
if (read_write) {
ut_ad(!srv_read_only_mode);
trx_sys.register_rw(trx);
}
} else {
ut_ad(!read_write);
}
}
trx->start_time = time(NULL);
trx->start_time_micro = trx->mysql_thd
? thd_start_utime(trx->mysql_thd)
: microsecond_interval_timer();
ut_a(trx->error_state == DB_SUCCESS);
}
/** Release an empty undo log that was associated with a transaction. */
ATTRIBUTE_COLD
void trx_t::commit_empty(mtr_t *mtr)
{
trx_rseg_t *rseg= rsegs.m_redo.rseg;
trx_undo_t *&undo= rsegs.m_redo.undo;
ut_ad(undo->state == TRX_UNDO_ACTIVE || undo->state == TRX_UNDO_PREPARED);
if (UNIV_UNLIKELY(undo->size != 1))
{
sql_print_error("InnoDB: Undo log for transaction " TRX_ID_FMT
" is corrupted (" UINT32PF "!=1)", id, undo->size);
ut_ad("corrupted undo log" == 0);
}
if (buf_block_t *u=
buf_page_get(page_id_t(rseg->space->id, undo->hdr_page_no), 0,
RW_X_LATCH, mtr))
{
ut_d(const uint16_t state=
mach_read_from_2(TRX_UNDO_SEG_HDR + TRX_UNDO_STATE + u->page.frame));
ut_ad(state == undo->state || state == TRX_UNDO_ACTIVE);
static_assert(TRX_UNDO_PAGE_START + 2 == TRX_UNDO_PAGE_FREE,
"compatibility");
ut_ad(!memcmp(TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_START + u->page.frame,
TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE + u->page.frame, 2));
ut_ad(mach_read_from_4(TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE + FLST_PREV +
FIL_ADDR_PAGE + u->page.frame) == FIL_NULL);
ut_ad(mach_read_from_2(TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE + FLST_PREV +
FIL_ADDR_BYTE + u->page.frame) == 0);
ut_ad(!memcmp(TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE + FLST_PREV +
u->page.frame,
TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE + FLST_NEXT +
u->page.frame, FIL_ADDR_SIZE));
/* Delete the last undo log header, which must be for this transaction.
An undo segment can be reused (TRX_UNDO_CACHED) only if it
comprises of one page and that single page contains enough space
for the undo log header of a subsequent transaction. See
trx_purge_add_undo_to_history(), which is executed when committing
a nonempty transaction.
If we simply changed the undo page state to TRX_UNDO_CACHED,
then trx_undo_reuse_cached() could run out of space. We will
release the space consumed by our empty undo log to avoid that. */
for (byte *last= &u->page.frame[TRX_UNDO_SEG_HDR + TRX_UNDO_SEG_HDR_SIZE],
*prev= nullptr;;)
{
/* TRX_UNDO_PREV_LOG is only being read in debug assertions, and
written in trx_undo_header_create(). To remain compatible with
possibly corrupted old data files, we will not read the field
TRX_UNDO_PREV_LOG but instead rely on TRX_UNDO_NEXT_LOG. */
ut_ad(mach_read_from_2(TRX_UNDO_PREV_LOG + last) ==
(reinterpret_cast<size_t>(prev) & (srv_page_size - 1)));
if (uint16_t next= mach_read_from_2(TRX_UNDO_NEXT_LOG + last))
{
ut_ad(ulint{next} + TRX_UNDO_LOG_XA_HDR_SIZE < srv_page_size - 100);
ut_ad(&u->page.frame[next] > last);
ut_ad(mach_read_from_2(TRX_UNDO_LOG_START + last) <= next);
prev= last;
last= &u->page.frame[next];
continue;
}
ut_ad(mach_read_from_8(TRX_UNDO_TRX_ID + last) == id);
ut_ad(!mach_read_from_8(TRX_UNDO_TRX_NO + last));
ut_ad(!memcmp(TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_START + u->page.frame,
TRX_UNDO_LOG_START + last, 2));
if (prev)
{
mtr->memcpy(*u, TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_START +
u->page.frame, prev + TRX_UNDO_LOG_START, 2);
const ulint free= page_offset(last);
mtr->write<2>(*u, TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE +
u->page.frame, free);
mtr->write<2>(*u, TRX_UNDO_SEG_HDR + TRX_UNDO_STATE + u->page.frame,
TRX_UNDO_CACHED);
mtr->write<2>(*u, TRX_UNDO_SEG_HDR + TRX_UNDO_LAST_LOG + u->page.frame,
page_offset(prev));
mtr->write<2>(*u, prev + TRX_UNDO_NEXT_LOG, 0U);
mtr->memset(u, free, srv_page_size - FIL_PAGE_DATA_END - free, 0);
/* We may have updated PAGE_MAX_TRX_ID on secondary index pages
to this->id. Ensure that trx_sys.m_max_trx_id will be recovered
correctly, even though we removed our undo log record along
with the TRX_UNDO_TRX_ID above. */
/* Below, we are acquiring rseg_header->page.lock after
u->page.lock (the opposite of trx_purge_add_undo_to_history()).
This is fine, because both functions are holding exclusive
rseg->latch. */
if (mach_read_from_8(prev + TRX_UNDO_TRX_NO) >= id);
else if (buf_block_t *rseg_header= rseg->get(mtr, nullptr))
{
byte *m= TRX_RSEG + TRX_RSEG_MAX_TRX_ID + rseg_header->page.frame;
do
{
if (UNIV_UNLIKELY(mach_read_from_4(TRX_RSEG + TRX_RSEG_FORMAT +
rseg_header->page.frame)))
/* This must have been upgraded from before MariaDB 10.3.5. */
trx_rseg_format_upgrade(rseg_header, mtr);
else if (mach_read_from_8(m) >= id)
continue;
mtr->write<8>(*rseg_header, m, id);
}
while (0);
}
}
else
/* Our undo log header was right after the undo log segment header.
This page should have been created by trx_undo_create(), not
returned by trx_undo_reuse_cached().
We retain the dummy empty log in order to remain compatible with
trx_undo_mem_create_at_db_start(). This page will remain available
to trx_undo_reuse_cached(), and it will eventually be freed by
trx_purge_truncate_rseg_history(). */
mtr->write<2>(*u, TRX_UNDO_SEG_HDR + TRX_UNDO_STATE + u->page.frame,
TRX_UNDO_CACHED);
break;
}
}
else
ut_ad("undo log page was not found" == 0);
UT_LIST_REMOVE(rseg->undo_list, undo);
UT_LIST_ADD_FIRST(rseg->undo_cached, undo);
undo->state= TRX_UNDO_CACHED;
undo= nullptr;
/* We must assign an "end" identifier even though we are not going
to persistently write it anywhere, to make sure that the purge of
history will not be stuck. */
trx_sys.assign_new_trx_no(this);
}
/** Assign the transaction its history serialisation number and write the
UNDO log to the assigned rollback segment.
@param mtr mini-transaction */
inline void trx_t::write_serialisation_history(mtr_t *mtr)
{
ut_ad(!read_only);
trx_rseg_t *rseg= rsegs.m_redo.rseg;
trx_undo_t *&undo= rsegs.m_redo.undo;
if (UNIV_LIKELY(undo != nullptr))
{
MONITOR_INC(MONITOR_TRX_COMMIT_UNDO);
/* We have to hold exclusive rseg->latch because undo log headers have
to be put to the history list in the (serialisation) order of the
UNDO trx number. This is required for purge_sys too. */
rseg->latch.wr_lock(SRW_LOCK_CALL);
ut_ad(undo->rseg == rseg);
/* Assign the transaction serialisation number and add any
undo log to the purge queue. */
if (UNIV_UNLIKELY(!undo_no))
{
/* The transaction was rolled back. */
commit_empty(mtr);
goto done;
}
else if (rseg->last_page_no == FIL_NULL)
{
/* trx_sys.assign_new_trx_no() and
purge_sys.enqueue() must be invoked in the same
critical section protected with purge queue mutex to avoid rseg with
greater last commit number to be pushed to purge queue prior to rseg with
lesser last commit number. In other words pushing to purge queue must be
serialized along with assigning trx_no. Otherwise purge coordinator
thread can also fetch redo log records from rseg with greater last commit
number before rseg with lesser one. */
purge_sys.queue_lock();
trx_sys.assign_new_trx_no(this);
const trx_id_t end{rw_trx_hash_element->no};
rseg->last_page_no= undo->hdr_page_no;
/* end cannot be less than anything in rseg. User threads only
produce events when a rollback segment is empty. */
rseg->set_last_commit(undo->hdr_offset, end);
purge_sys.enqueue(end, *rseg);
purge_sys.queue_unlock();
}
else
trx_sys.assign_new_trx_no(this);
UT_LIST_REMOVE(rseg->undo_list, undo);
/* Change the undo log segment state from TRX_UNDO_ACTIVE, to
define the transaction as committed in the file based domain,
at mtr->commit_lsn() obtained in mtr->commit() below. */
trx_purge_add_undo_to_history(this, undo, mtr);
done:
rseg->release();
rseg->latch.wr_unlock();
}
else
rseg->release();
mtr->commit();
}
/********************************************************************
Finalize a transaction containing updates for a FTS table. */
static
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;
ut_a(fts->add_wq);
mem_heap_t* 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
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;
}
extern "C" MYSQL_THD thd_increment_pending_ops(MYSQL_THD);
extern "C" void thd_decrement_pending_ops(MYSQL_THD);
#include "../log/log0sync.h"
/*
If required, initiates write and optionally flush of the log to
disk
@param lsn LSN up to which logs are to be flushed.
@param trx transaction; if trx->state is PREPARED, the function will
also wait for the flush to complete.
*/
static void trx_flush_log_if_needed(lsn_t lsn, trx_t *trx)
{
ut_ad(srv_flush_log_at_trx_commit);
ut_ad(trx->state != TRX_STATE_PREPARED);
if (log_sys.get_flushed_lsn(std::memory_order_relaxed) >= lsn)
return;
const bool flush=
(srv_file_flush_method != SRV_NOSYNC &&
(srv_flush_log_at_trx_commit & 1));
completion_callback cb;
if (!log_sys.is_pmem() &&
(cb.m_param= thd_increment_pending_ops(trx->mysql_thd)))
{
cb.m_callback = (void (*)(void *)) thd_decrement_pending_ops;
log_write_up_to(lsn, flush, &cb);
}
else
{
trx->op_info= "flushing log";
log_write_up_to(lsn, flush);
trx->op_info= "";
}
}
/** Process tables that were modified by the committing transaction. */
inline void trx_t::commit_tables()
{
if (undo_no && !mod_tables.empty())
{
const trx_id_t max_trx_id= trx_sys.get_max_trx_id();
const auto now= start_time;
for (const auto &p : mod_tables)
{
dict_table_t *table= p.first;
table->update_time= now;
table->query_cache_inv_trx_id= max_trx_id;
}
}
}
/** Evict a table definition due to the rollback of ALTER TABLE.
@param table_id table identifier
@param reset_only whether to only reset dict_table_t::def_trx_id */
void trx_t::evict_table(table_id_t table_id, bool reset_only)
{
ut_ad(in_rollback);
dict_table_t* table = dict_sys.find_table(table_id);
if (!table) {
return;
}
table->def_trx_id = 0;
if (auto ref_count = table->get_ref_count()) {
/* This must be a DDL operation that is being rolled
back in an active connection. */
ut_a(ref_count == 1);
ut_ad(!is_recovered);
ut_ad(mysql_thd);
return;
}
if (reset_only) {
return;
}
/* This table should only be locked by this transaction, if at all. */
ut_ad(UT_LIST_GET_LEN(table->locks) <= 1);
const bool locked = UT_LIST_GET_LEN(table->locks);
ut_ad(!locked || UT_LIST_GET_FIRST(table->locks)->trx == this);
dict_sys.remove(table, true, locked);
if (locked) {
UT_LIST_ADD_FIRST(lock.evicted_tables, table);
}
}
/** Free temporary undo log after commit or rollback.
@param undo temporary undo log */
ATTRIBUTE_NOINLINE static void trx_commit_cleanup(trx_undo_t *&undo)
{
trx_rseg_t *const rseg= undo->rseg;
ut_ad(rseg->space == fil_system.temp_space);
rseg->latch.wr_lock(SRW_LOCK_CALL);
UT_LIST_REMOVE(rseg->undo_list, undo);
ut_ad(undo->state == TRX_UNDO_ACTIVE || undo->state == TRX_UNDO_PREPARED);
ut_ad(undo->id < TRX_RSEG_N_SLOTS);
/* Delete first the undo log segment in the file */
bool finished;
mtr_t mtr;
do
{
mtr.start();
mtr.set_log_mode(MTR_LOG_NO_REDO);
finished= true;
if (buf_block_t *block=
buf_page_get(page_id_t(SRV_TMP_SPACE_ID, undo->hdr_page_no), 0,
RW_X_LATCH, &mtr))
{
fseg_header_t *file_seg= TRX_UNDO_SEG_HDR + TRX_UNDO_FSEG_HEADER +
block->page.frame;
finished= fseg_free_step(file_seg, &mtr);
if (!finished);
else if (buf_block_t *rseg_header= rseg->get(&mtr, nullptr))
{
static_assert(FIL_NULL == 0xffffffff, "compatibility");
memset(rseg_header->page.frame + TRX_RSEG + TRX_RSEG_UNDO_SLOTS +
undo->id * TRX_RSEG_SLOT_SIZE, 0xff, 4);
}
}
mtr.commit();
}
while (!finished);
ut_ad(rseg->curr_size > undo->size);
rseg->curr_size-= undo->size;
rseg->latch.wr_unlock();
ut_free(undo);
undo= nullptr;
}
TRANSACTIONAL_INLINE inline void trx_t::commit_in_memory(const mtr_t *mtr)
{
/* We already detached from rseg in write_serialisation_history() */
ut_ad(!rsegs.m_redo.undo);
read_view.close();
if (is_autocommit_non_locking())
{
ut_ad(id == 0);
ut_ad(read_only);
ut_ad(!will_lock);
ut_a(!is_recovered);
ut_ad(!rsegs.m_redo.rseg);
ut_ad(!rsegs.m_redo.undo);
ut_ad(mysql_thd);
ut_ad(state == TRX_STATE_ACTIVE);
/* Note: We do not have to hold any lock_sys latch here, because
this is a non-locking transaction. */
ut_a(UT_LIST_GET_LEN(lock.trx_locks) == 0);
ut_ad(UT_LIST_GET_LEN(lock.evicted_tables) == 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 freezing of trx_sys.trx_list will protect the trx_t
instance and it cannot be removed from the trx_list and freed
without first unfreezing trx_list. */
state= TRX_STATE_NOT_STARTED;
MONITOR_INC(MONITOR_TRX_NL_RO_COMMIT);
DBUG_LOG("trx", "Autocommit in memory: " << this);
}
else
{
#ifdef UNIV_DEBUG
if (!UT_LIST_GET_LEN(lock.trx_locks))
for (auto l : lock.table_locks)
ut_ad(!l);
#endif /* UNIV_DEBUG */
commit_state();
if (id)
{
trx_sys.deregister_rw(this);
/* Wait for any implicit-to-explicit lock conversions to cease,
so that there will be no race condition in lock_release(). */
while (UNIV_UNLIKELY(is_referenced()))
LF_BACKOFF();
}
else
ut_ad(read_only || !rsegs.m_redo.rseg);
if (read_only || !rsegs.m_redo.rseg)
{
MONITOR_INC(MONITOR_TRX_RO_COMMIT);
}
else
{
commit_tables();
MONITOR_INC(MONITOR_TRX_RW_COMMIT);
is_recovered= false;
}
if (UNIV_LIKELY(!dict_operation))
release_locks();
}
if (trx_undo_t *&undo= rsegs.m_noredo.undo)
{
ut_ad(undo->rseg == rsegs.m_noredo.rseg);
trx_commit_cleanup(undo);
}
if (mtr)
{
/* NOTE that we could possibly make a group commit more efficient
here: call std::this_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_t::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. */
commit_lsn= undo_no || !xid.is_null() ? mtr->commit_lsn() : 0;
if (commit_lsn && !flush_log_later && srv_flush_log_at_trx_commit)
{
trx_flush_log_if_needed(commit_lsn, this);
commit_lsn= 0;
}
}
savepoints_discard();
if (fts_trx)
trx_finalize_for_fts(this, undo_no != 0);
#ifdef WITH_WSREP
/* Serialization history has been written and the transaction is
committed in memory, which makes this commit ordered. Release commit
order critical section. */
if (wsrep)
{
wsrep= false;
wsrep_commit_ordered(mysql_thd);
}
#endif /* WITH_WSREP */
lock.was_chosen_as_deadlock_victim= false;
}
void trx_t::commit_cleanup()
{
ut_ad(!dict_operation);
ut_ad(!was_dict_operation);
if (is_bulk_insert())
for (auto &t : mod_tables)
delete t.second.bulk_store;
mutex.wr_lock();
state= TRX_STATE_NOT_STARTED;
*detailed_error= '\0';
mod_tables.clear();
check_foreigns= true;
check_unique_secondary= true;
assert_freed();
trx_init(this);
mutex.wr_unlock();
ut_a(error_state == DB_SUCCESS);
}
/** Commit the transaction in a mini-transaction.
@param mtr mini-transaction (if there are any persistent modifications) */
TRANSACTIONAL_TARGET void trx_t::commit_low(mtr_t *mtr)
{
ut_ad(!mtr || mtr->is_active());
ut_d(bool aborted= in_rollback && error_state == DB_DEADLOCK);
ut_ad(!mtr == (aborted || !has_logged_persistent()));
ut_ad(!mtr || !aborted);
if (fts_trx && undo_no)
{
ut_a(!is_autocommit_non_locking());
/* MDEV-24088 FIXME: Invoke fts_commit() earlier (before possible
XA PREPARE), so that we will be able to return an error and rollback
the transaction, instead of violating consistency!
The original claim about DB_DUPLICATE KEY was:
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 (ut_d(dberr_t error=) fts_commit(this))
ut_ad(error == DB_DUPLICATE_KEY || error == DB_LOCK_WAIT_TIMEOUT);
}
#ifdef ENABLED_DEBUG_SYNC
const bool debug_sync= mysql_thd && has_logged_persistent();
#endif
if (mtr)
{
if (UNIV_UNLIKELY(apply_online_log))
apply_log();
/* 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 do not necessarily come in
exactly the same order as commit lsn's, if the transactions have
different rollback segments. 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. */
write_serialisation_history(mtr);
}
else if (trx_rseg_t *rseg= rsegs.m_redo.rseg)
{
ut_ad(id);
ut_ad(!rsegs.m_redo.undo);
rseg->release();
}
#ifdef ENABLED_DEBUG_SYNC
if (debug_sync)
DEBUG_SYNC_C("before_trx_state_committed_in_memory");
#endif
commit_in_memory(mtr);
}
void trx_t::commit_persist()
{
mtr_t *mtr= nullptr;
mtr_t local_mtr;
if (has_logged_persistent())
{
mtr= &local_mtr;
local_mtr.start();
}
commit_low(mtr);
}
void trx_t::commit()
{
ut_ad(!was_dict_operation);
ut_d(was_dict_operation= dict_operation);
dict_operation= false;
commit_persist();
#ifdef UNIV_DEBUG
if (!was_dict_operation)
for (const auto &p : mod_tables) ut_ad(!p.second.is_dropped());
#endif /* UNIV_DEBUG */
ut_d(was_dict_operation= false);
commit_cleanup();
}
/****************************************************************//**
Prepares a transaction for commit/rollback. */
void
trx_commit_or_rollback_prepare(
/*===========================*/
trx_t* trx) /*!< in/out: transaction */
{
/* We are reading trx->state without holding trx->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:
trx_start_low(trx, true);
/* fall through */
case TRX_STATE_ACTIVE:
case TRX_STATE_PREPARED:
case TRX_STATE_PREPARED_RECOVERED:
trx->lock.wait_thr = NULL;
return;
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
}
ut_error;
}
/*********************************************************************//**
Creates a commit command node struct.
@return own: commit node struct */
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 */
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);
trx_commit_or_rollback_prepare(trx);
trx->commit();
ut_ad(trx->lock.wait_thr == NULL);
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 */
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. */
switch (trx->state) {
case TRX_STATE_NOT_STARTED:
return DB_SUCCESS;
case TRX_STATE_ACTIVE:
case TRX_STATE_PREPARED:
case TRX_STATE_PREPARED_RECOVERED:
trx->op_info = "committing";
trx->commit();
trx->op_info = "";
return(DB_SUCCESS);
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
}
ut_error;
return(DB_CORRUPTION);
}
/** Durably write log until trx->commit_lsn
(if trx_t::commit_in_memory() was invoked with flush_log_later=true). */
void trx_commit_complete_for_mysql(trx_t *trx)
{
const lsn_t lsn= trx->commit_lsn;
if (!lsn)
return;
switch (srv_flush_log_at_trx_commit) {
case 0:
return;
case 1:
if (trx->active_commit_ordered)
return;
}
trx_flush_log_if_needed(lsn, trx);
}
/**********************************************************************//**
Marks the latest SQL statement ended. */
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_PREPARED_RECOVERED:
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
case TRX_STATE_NOT_STARTED:
trx->undo_no = 0;
/* fall through */
case TRX_STATE_ACTIVE:
if (trx->fts_trx != NULL) {
fts_savepoint_laststmt_refresh(trx);
}
if (trx->is_bulk_insert()) {
/* MDEV-25036 FIXME: we support buffered
insert only for the first insert statement */
trx->error_state = trx->bulk_insert_apply();
/* Allow a subsequent INSERT into an empty table
if !unique_checks && !foreign_key_checks. */
return;
}
trx->last_sql_stat_start.least_undo_no = trx->undo_no;
trx->end_bulk_insert();
return;
}
ut_error;
}
/**********************************************************************//**
Prints info about a transaction. */
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: trx->lock.n_rec_locks */
ulint n_trx_locks,
/*!< in: length of trx->lock.trx_locks */
ulint heap_size)
/*!< in: mem_heap_get_size(trx->lock.lock_heap) */
{
if (const trx_id_t id = trx->id) {
fprintf(f, "TRANSACTION " TRX_ID_FMT, trx->id);
} else {
fprintf(f, "TRANSACTION (%p)", trx);
}
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:
case TRX_STATE_PREPARED_RECOVERED:
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:
const char* op_info = trx->op_info;
if (*op_info) {
putc(' ', f);
fputs(op_info, f);
}
if (trx->is_recovered) {
fputs(" recovered trx", f);
}
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);
}
bool newline = true;
if (trx->in_rollback) { /* dirty read for performance reasons */
fputs("ROLLING BACK ", f);
} else if (trx->lock.wait_lock) {
fputs("LOCK WAIT ", f);
} else {
newline = false;
}
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->undo_no != 0) {
newline = true;
fprintf(f, ", undo log entries " TRX_ID_FMT, trx->undo_no);
}
if (newline) {
putc('\n', f);
}
if (trx->state != TRX_STATE_NOT_STARTED && 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.latch.
When possible, use trx_print() instead. */
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 */
{
lock_sys.assert_locked();
trx_print_low(f, trx, max_query_len,
trx->lock.n_rec_locks,
UT_LIST_GET_LEN(trx->lock.trx_locks),
mem_heap_get_size(trx->lock.lock_heap));
}
/**********************************************************************//**
Prints info about a transaction.
Acquires and releases lock_sys.latch. */
TRANSACTIONAL_TARGET
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, n_trx_locks, heap_size;
{
TMLockMutexGuard g{SRW_LOCK_CALL};
n_rec_locks= trx->lock.n_rec_locks;
n_trx_locks= UT_LIST_GET_LEN(trx->lock.trx_locks);
heap_size= mem_heap_get_size(trx->lock.lock_heap);
}
trx_print_low(f, trx, max_query_len, n_rec_locks, n_trx_locks, heap_size);
}
/** Prepare a transaction.
@return log sequence number that makes the XA PREPARE durable
@retval 0 if no changes needed to be made durable */
static lsn_t trx_prepare_low(trx_t *trx)
{
ut_ad(!trx->is_recovered);
mtr_t mtr;
if (trx_undo_t* undo = trx->rsegs.m_noredo.undo) {
ut_ad(undo->rseg == trx->rsegs.m_noredo.rseg);
mtr.start();
mtr.set_log_mode(MTR_LOG_NO_REDO);
trx_undo_set_state_at_prepare(trx, undo, false, &mtr);
mtr.commit();
}
trx_undo_t* undo = trx->rsegs.m_redo.undo;
if (!undo) {
/* There were no changes to persistent tables. */
return(0);
}
ut_ad(undo->rseg == trx->rsegs.m_redo.rseg);
mtr.start();
/* 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. */
trx_undo_set_state_at_prepare(trx, undo, false, &mtr);
/* Make the XA PREPARE durable. */
mtr.commit();
ut_ad(mtr.commit_lsn() > 0);
return(mtr.commit_lsn());
}
/****************************************************************//**
Prepares a transaction. */
TRANSACTIONAL_TARGET
static
void
trx_prepare(
/*========*/
trx_t* trx) /*!< in/out: transaction */
{
/* Only fresh user transactions can be prepared.
Recovered transactions cannot. */
ut_a(!trx->is_recovered);
lsn_t lsn = trx_prepare_low(trx);
ut_a(trx->state == TRX_STATE_ACTIVE);
{
TMTrxGuard tg{*trx};
trx->state = TRX_STATE_PREPARED;
}
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.
We must not be holding any mutexes or latches here. */
if (auto f = srv_flush_log_at_trx_commit) {
log_write_up_to(lsn, (f & 1) && srv_file_flush_method
!= SRV_NOSYNC);
}
if (!UT_LIST_GET_LEN(trx->lock.trx_locks)
|| trx->isolation_level == TRX_ISO_SERIALIZABLE) {
/* Do not release any locks at the
SERIALIZABLE isolation level. */
} else if (!trx->mysql_thd
|| thd_sql_command(trx->mysql_thd)
!= SQLCOM_XA_PREPARE) {
/* Do not release locks for XA COMMIT ONE PHASE
or for internal distributed transactions
(XID::get_my_xid() would be nonzero). */
} else {
lock_release_on_prepare(trx);
}
}
}
/** XA PREPARE a transaction.
@param[in,out] trx transaction to prepare */
void trx_prepare_for_mysql(trx_t* trx)
{
trx_start_if_not_started_xa(trx, false);
trx->op_info = "preparing";
trx_prepare(trx);
trx->op_info = "";
}
struct trx_recover_for_mysql_callback_arg
{
XID *xid_list;
uint len;
uint count;
};
static my_bool trx_recover_for_mysql_callback(rw_trx_hash_element_t *element,
trx_recover_for_mysql_callback_arg *arg)
{
DBUG_ASSERT(arg->len > 0);
element->mutex.wr_lock();
if (trx_t *trx= element->trx)
{
/*
The state of a read-write transaction can only change from ACTIVE to
PREPARED while we are holding the element->mutex. But since it is
executed at startup no state change should occur.
*/
if (trx_state_eq(trx, TRX_STATE_PREPARED))
{
ut_ad(trx->is_recovered);
ut_ad(trx->id);
if (arg->count == 0)
ib::info() << "Starting recovery for XA transactions...";
XID& xid= arg->xid_list[arg->count];
if (arg->count++ < arg->len)
{
trx->state= TRX_STATE_PREPARED_RECOVERED;
ib::info() << "Transaction " << trx->id
<< " in prepared state after recovery";
ib::info() << "Transaction contains changes to " << trx->undo_no
<< " rows";
xid= trx->xid;
}
}
}
element->mutex.wr_unlock();
/* Do not terminate upon reaching arg->len; count all transactions */
return false;
}
static my_bool trx_recover_reset_callback(rw_trx_hash_element_t *element,
void*)
{
element->mutex.wr_lock();
if (trx_t *trx= element->trx)
{
if (trx_state_eq(trx, TRX_STATE_PREPARED_RECOVERED))
trx->state= TRX_STATE_PREPARED;
}
element->mutex.wr_unlock();
return false;
}
/**
Find prepared transaction objects for recovery.
@param[out] xid_list prepared transactions
@param[in] len number of slots in xid_list
@return number of prepared transactions stored in xid_list
*/
int trx_recover_for_mysql(XID *xid_list, uint len)
{
trx_recover_for_mysql_callback_arg arg= { xid_list, len, 0 };
ut_ad(xid_list);
ut_ad(len);
/* Fill xid_list with PREPARED transactions. */
trx_sys.rw_trx_hash.iterate_no_dups(trx_recover_for_mysql_callback, &arg);
if (arg.count)
{
ib::info() << arg.count
<< " transactions in prepared state after recovery";
/* After returning the full list, reset the state, because
init_server_components() wants to recover the collection of
transactions twice, by first calling tc_log->open() and then
ha_recover() directly. */
if (arg.count <= len)
trx_sys.rw_trx_hash.iterate(trx_recover_reset_callback);
}
return int(std::min(arg.count, len));
}
struct trx_get_trx_by_xid_callback_arg
{
const XID *xid;
trx_t *trx;
};
static my_bool trx_get_trx_by_xid_callback(rw_trx_hash_element_t *element,
trx_get_trx_by_xid_callback_arg *arg)
{
my_bool found= 0;
element->mutex.wr_lock();
if (trx_t *trx= element->trx)
{
trx->mutex_lock();
if (trx->is_recovered &&
(trx_state_eq(trx, TRX_STATE_PREPARED) ||
trx_state_eq(trx, TRX_STATE_PREPARED_RECOVERED)) &&
arg->xid->eq(&trx->xid))
{
#ifdef WITH_WSREP
/* The commit of a prepared recovered Galera
transaction needs a valid trx->xid for
invoking trx_sys_update_wsrep_checkpoint(). */
if (!wsrep_is_wsrep_xid(&trx->xid))
#endif /* WITH_WSREP */
/* Invalidate the XID, so that subsequent calls will not find it. */
trx->xid.null();
arg->trx= trx;
found= 1;
}
trx->mutex_unlock();
}
element->mutex.wr_unlock();
return found;
}
/** Look up an X/Open distributed transaction in XA PREPARE state.
@param[in] xid X/Open XA transaction identifier
@return transaction on match (the trx_t::xid will be invalidated);
note that the trx may have been committed before the caller acquires
trx_t::mutex
@retval NULL if no match */
trx_t* trx_get_trx_by_xid(const XID* xid)
{
trx_get_trx_by_xid_callback_arg arg= { xid, 0 };
if (xid)
trx_sys.rw_trx_hash.iterate(trx_get_trx_by_xid_callback, &arg);
return arg.trx;
}
/*************************************************************//**
Starts the transaction if it is not yet started. */
void
trx_start_if_not_started_xa_low(
/*============================*/
trx_t* trx, /*!< in/out: transaction */
bool read_write) /*!< in: true if read write transaction */
{
switch (trx->state) {
case TRX_STATE_NOT_STARTED:
trx_start_low(trx, read_write);
return;
case TRX_STATE_ACTIVE:
if (trx->id == 0 && read_write) {
/* If the transaction is tagged as read-only then
it can only write to temp tables and for such
transactions we don't want to move them to the
trx_sys_t::rw_trx_hash. */
if (!trx->read_only) {
trx_set_rw_mode(trx);
}
}
return;
case TRX_STATE_PREPARED:
case TRX_STATE_PREPARED_RECOVERED:
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
}
ut_error;
}
/*************************************************************//**
Starts the transaction if it is not yet started. */
void
trx_start_if_not_started_low(
/*==========================*/
trx_t* trx, /*!< in: transaction */
bool read_write) /*!< in: true if read write transaction */
{
switch (trx->state) {
case TRX_STATE_NOT_STARTED:
trx_start_low(trx, read_write);
return;
case TRX_STATE_ACTIVE:
if (read_write && trx->id == 0 && !trx->read_only) {
trx_set_rw_mode(trx);
}
return;
case TRX_STATE_PREPARED:
case TRX_STATE_PREPARED_RECOVERED:
case TRX_STATE_COMMITTED_IN_MEMORY:
break;
}
ut_error;
}
/**
Start a transaction for internal processing.
@param trx transaction
@param read_write whether writes may be performed */
void trx_start_internal_low(trx_t *trx, bool read_write)
{
trx->will_lock= true;
trx_start_low(trx, read_write);
}
/** Start a transaction for a DDL operation.
@param trx transaction */
void trx_start_for_ddl_low(trx_t *trx)
{
/* Flag this transaction as a dictionary operation, so that
the data dictionary will be locked in crash recovery. */
trx->dict_operation= true;
trx_start_internal_low(trx, true);
}
/*************************************************************//**
Set the transaction as a read-write transaction if it is not already
tagged as such. Read-only transactions that are writing to temporary
tables are assigned an ID and a rollback segment but are not added
to the trx read-write list because their updates should not be visible
to other transactions and therefore their changes can be ignored by
by MVCC. */
void
trx_set_rw_mode(
/*============*/
trx_t* trx) /*!< in/out: transaction that is RW */
{
ut_ad(trx->rsegs.m_redo.rseg == 0);
ut_ad(!trx->is_autocommit_non_locking());
ut_ad(!trx->read_only);
ut_ad(trx->id == 0);
if (high_level_read_only) {
return;
}
trx_assign_rseg_low(trx);
/* So that we can see our own changes. */
if (trx->read_view.is_open()) {
trx->read_view.set_creator_trx_id(trx->id);
}
}
|