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
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
|
/*
Copyright (c) 2012, Monty Program Ab
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation // gcc: Class implementation
#endif
#define MYSQL_SERVER 1
/* For use of 'PRIu64': */
#define __STDC_FORMAT_MACROS
#include <my_global.h>
#include <inttypes.h>
/* The C++ file's header */
#include "./ha_rocksdb.h"
/* C++ standard header files */
#include <algorithm>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
/* MySQL includes */
#include "./debug_sync.h"
#include "./my_bit.h"
#include "./my_stacktrace.h"
#include "./my_sys.h"
#include "./sql_audit.h"
#include "./sql_table.h"
#include "./sql_hset.h"
#include <mysql/psi/mysql_table.h>
#ifdef MARIAROCKS_NOT_YET
#include <mysql/thread_pool_priv.h>
#endif
#include <mysys_err.h>
// Both MySQL and RocksDB define the same constant. To avoid compilation errors
// till we make the fix in RocksDB, we'll temporary undefine it here.
#undef CACHE_LINE_SIZE
/* RocksDB includes */
#include "monitoring/histogram.h"
#include "rocksdb/compaction_filter.h"
#include "rocksdb/env.h"
#include "rocksdb/persistent_cache.h"
#include "rocksdb/rate_limiter.h"
#include "rocksdb/slice_transform.h"
#include "rocksdb/thread_status.h"
#include "rocksdb/utilities/checkpoint.h"
#include "rocksdb/utilities/convenience.h"
#include "rocksdb/utilities/memory_util.h"
#include "rocksdb/utilities/sim_cache.h"
#include "util/stop_watch.h"
/* MyRocks includes */
#include "./event_listener.h"
#include "./ha_rocksdb_proto.h"
#include "./logger.h"
#include "./rdb_cf_manager.h"
#include "./rdb_cf_options.h"
#include "./rdb_datadic.h"
#include "./rdb_i_s.h"
#include "./rdb_index_merge.h"
#include "./rdb_mutex_wrapper.h"
#include "./rdb_psi.h"
#include "./rdb_threads.h"
#include "./rdb_mariadb_server_port.h"
// Internal MySQL APIs not exposed in any header.
extern "C" {
/**
Mark transaction to rollback and mark error as fatal to a sub-statement.
@param thd Thread handle
@param all TRUE <=> rollback main transaction.
*/
void thd_mark_transaction_to_rollback(MYSQL_THD thd, bool all);
/**
* Get the user thread's binary logging format
* @param thd user thread
* @return Value to be used as index into the binlog_format_names array
*/
int thd_binlog_format(const MYSQL_THD thd);
/**
* Check if binary logging is filtered for thread's current db.
* @param thd Thread handle
* @retval 1 the query is not filtered, 0 otherwise.
*/
bool thd_binlog_filter_ok(const MYSQL_THD thd);
}
MYSQL_PLUGIN_IMPORT bool my_disable_leak_check;
namespace myrocks {
static st_global_stats global_stats;
static st_export_stats export_stats;
static st_memory_stats memory_stats;
static st_io_stall_stats io_stall_stats;
const std::string DEFAULT_CF_NAME("default");
const std::string DEFAULT_SYSTEM_CF_NAME("__system__");
const std::string PER_INDEX_CF_NAME("$per_index_cf");
/**
Updates row counters based on the table type and operation type.
*/
void ha_rocksdb::update_row_stats(const operation_type &type) {
DBUG_ASSERT(type < ROWS_MAX);
// Find if we are modifying system databases.
if (table->s && m_tbl_def->m_is_mysql_system_table)
global_stats.system_rows[type].inc();
else
global_stats.rows[type].inc();
}
void dbug_dump_database(rocksdb::DB *db);
static handler *rocksdb_create_handler(my_core::handlerton *hton,
my_core::TABLE_SHARE *table_arg,
my_core::MEM_ROOT *mem_root);
bool can_use_bloom_filter(THD *thd, const Rdb_key_def &kd,
const rocksdb::Slice &eq_cond,
const bool use_all_keys);
static rocksdb::CompactRangeOptions getCompactRangeOptions() {
rocksdb::CompactRangeOptions compact_range_options;
compact_range_options.bottommost_level_compaction =
rocksdb::BottommostLevelCompaction::kForce;
compact_range_options.exclusive_manual_compaction = false;
return compact_range_options;
}
///////////////////////////////////////////////////////////
// Parameters and settings
///////////////////////////////////////////////////////////
static char *rocksdb_default_cf_options = nullptr;
static char *rocksdb_override_cf_options = nullptr;
static char *rocksdb_update_cf_options = nullptr;
///////////////////////////////////////////////////////////
// Globals
///////////////////////////////////////////////////////////
handlerton *rocksdb_hton;
rocksdb::TransactionDB *rdb = nullptr;
rocksdb::HistogramImpl *commit_latency_stats = nullptr;
static std::shared_ptr<rocksdb::Statistics> rocksdb_stats;
static std::unique_ptr<rocksdb::Env> flashcache_aware_env;
static std::shared_ptr<Rdb_tbl_prop_coll_factory> properties_collector_factory;
Rdb_dict_manager dict_manager;
Rdb_cf_manager cf_manager;
Rdb_ddl_manager ddl_manager;
Rdb_binlog_manager binlog_manager;
#if !defined(_WIN32) && !defined(__APPLE__)
Rdb_io_watchdog *io_watchdog = nullptr;
#endif
/**
MyRocks background thread control
N.B. This is besides RocksDB's own background threads
(@see rocksdb::CancelAllBackgroundWork())
*/
static Rdb_background_thread rdb_bg_thread;
// List of table names (using regex) that are exceptions to the strict
// collation check requirement.
Regex_list_handler *rdb_collation_exceptions;
static const char **rdb_get_error_messages(int nr);
static void rocksdb_flush_all_memtables() {
const Rdb_cf_manager &cf_manager = rdb_get_cf_manager();
for (const auto &cf_handle : cf_manager.get_all_cf()) {
rdb->Flush(rocksdb::FlushOptions(), cf_handle);
}
}
static void rocksdb_compact_column_family_stub(
THD *const thd, struct st_mysql_sys_var *const var, void *const var_ptr,
const void *const save) {}
static int rocksdb_compact_column_family(THD *const thd,
struct st_mysql_sys_var *const var,
void *const var_ptr,
struct st_mysql_value *const value) {
char buff[STRING_BUFFER_USUAL_SIZE];
int len = sizeof(buff);
DBUG_ASSERT(value != nullptr);
if (const char *const cf = value->val_str(value, buff, &len)) {
auto cfh = cf_manager.get_cf(cf);
if (cfh != nullptr && rdb != nullptr) {
sql_print_information("RocksDB: Manual compaction of column family: %s\n",
cf);
rdb->CompactRange(getCompactRangeOptions(), cfh, nullptr, nullptr);
}
}
return HA_EXIT_SUCCESS;
}
///////////////////////////////////////////////////////////
// Hash map: table name => open table handler
///////////////////////////////////////////////////////////
namespace // anonymous namespace = not visible outside this source file
{
const ulong TABLE_HASH_SIZE = 32;
typedef Hash_set<Rdb_table_handler> Rdb_table_set;
struct Rdb_open_tables_map {
/* Hash table used to track the handlers of open tables */
Rdb_table_set m_hash;
/* The mutex used to protect the hash table */
mutable mysql_mutex_t m_mutex;
static uchar *get_hash_key(const Rdb_table_handler *const table_handler,
size_t *const length,
my_bool not_used MY_ATTRIBUTE((__unused__)));
Rdb_table_handler *get_table_handler(const char *const table_name);
void release_table_handler(Rdb_table_handler *const table_handler);
Rdb_open_tables_map() : m_hash(get_hash_key, system_charset_info) { }
std::vector<std::string> get_table_names(void) const;
};
} // anonymous namespace
static Rdb_open_tables_map rdb_open_tables;
static std::string rdb_normalize_dir(std::string dir) {
while (dir.size() > 0 && dir.back() == '/') {
dir.resize(dir.size() - 1);
}
return dir;
}
static int rocksdb_create_checkpoint(
THD *const thd MY_ATTRIBUTE((__unused__)),
struct st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const save MY_ATTRIBUTE((__unused__)),
struct st_mysql_value *const value) {
char buf[FN_REFLEN];
int len = sizeof(buf);
const char *const checkpoint_dir_raw = value->val_str(value, buf, &len);
if (checkpoint_dir_raw) {
if (rdb != nullptr) {
std::string checkpoint_dir = rdb_normalize_dir(checkpoint_dir_raw);
// NO_LINT_DEBUG
sql_print_information("RocksDB: creating checkpoint in directory : %s\n",
checkpoint_dir.c_str());
rocksdb::Checkpoint *checkpoint;
auto status = rocksdb::Checkpoint::Create(rdb, &checkpoint);
// We can only return HA_EXIT_FAILURE/HA_EXIT_SUCCESS here which is why
// the return code is ignored, but by calling into rdb_error_to_mysql,
// it will call my_error for us, which will propogate up to the client.
int rc __attribute__((__unused__));
if (status.ok()) {
status = checkpoint->CreateCheckpoint(checkpoint_dir.c_str());
delete checkpoint;
if (status.ok()) {
sql_print_information(
"RocksDB: created checkpoint in directory : %s\n",
checkpoint_dir.c_str());
return HA_EXIT_SUCCESS;
} else {
rc = ha_rocksdb::rdb_error_to_mysql(status);
}
} else {
rc = ha_rocksdb::rdb_error_to_mysql(status);
}
}
}
return HA_EXIT_FAILURE;
}
/* This method is needed to indicate that the
ROCKSDB_CREATE_CHECKPOINT command is not read-only */
static void rocksdb_create_checkpoint_stub(THD *const thd,
struct st_mysql_sys_var *const var,
void *const var_ptr,
const void *const save) {}
static void rocksdb_force_flush_memtable_now_stub(
THD *const thd, struct st_mysql_sys_var *const var, void *const var_ptr,
const void *const save) {}
static int rocksdb_force_flush_memtable_now(
THD *const thd, struct st_mysql_sys_var *const var, void *const var_ptr,
struct st_mysql_value *const value) {
sql_print_information("RocksDB: Manual memtable flush.");
rocksdb_flush_all_memtables();
return HA_EXIT_SUCCESS;
}
static void rocksdb_force_flush_memtable_and_lzero_now_stub(
THD *const thd, struct st_mysql_sys_var *const var, void *const var_ptr,
const void *const save) {}
static int rocksdb_force_flush_memtable_and_lzero_now(
THD *const thd, struct st_mysql_sys_var *const var, void *const var_ptr,
struct st_mysql_value *const value) {
sql_print_information("RocksDB: Manual memtable and L0 flush.");
rocksdb_flush_all_memtables();
const Rdb_cf_manager &cf_manager = rdb_get_cf_manager();
rocksdb::CompactionOptions c_options = rocksdb::CompactionOptions();
rocksdb::ColumnFamilyMetaData metadata;
rocksdb::ColumnFamilyDescriptor cf_descr;
for (const auto &cf_handle : cf_manager.get_all_cf()) {
rdb->GetColumnFamilyMetaData(cf_handle, &metadata);
cf_handle->GetDescriptor(&cf_descr);
c_options.output_file_size_limit = cf_descr.options.target_file_size_base;
DBUG_ASSERT(metadata.levels[0].level == 0);
std::vector<std::string> file_names;
for (auto &file : metadata.levels[0].files) {
file_names.emplace_back(file.db_path + file.name);
}
if (!file_names.empty()) {
rocksdb::Status s;
s = rdb->CompactFiles(c_options, cf_handle, file_names, 1);
if (!s.ok() && !s.IsAborted()) {
rdb_handle_io_error(s, RDB_IO_ERROR_GENERAL);
return HA_EXIT_FAILURE;
}
}
}
return HA_EXIT_SUCCESS;
}
static void rocksdb_drop_index_wakeup_thread(
my_core::THD *const thd MY_ATTRIBUTE((__unused__)),
struct st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr MY_ATTRIBUTE((__unused__)), const void *const save);
static my_bool rocksdb_pause_background_work = 0;
static mysql_mutex_t rdb_sysvars_mutex;
static void rocksdb_set_pause_background_work(
my_core::THD *const thd MY_ATTRIBUTE((__unused__)),
struct st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr MY_ATTRIBUTE((__unused__)), const void *const save) {
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
const bool pause_requested = *static_cast<const bool *>(save);
if (rocksdb_pause_background_work != pause_requested) {
if (pause_requested) {
rdb->PauseBackgroundWork();
} else {
rdb->ContinueBackgroundWork();
}
rocksdb_pause_background_work = pause_requested;
}
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
static void rocksdb_set_compaction_options(THD *thd,
struct st_mysql_sys_var *var,
void *var_ptr, const void *save);
static void rocksdb_set_table_stats_sampling_pct(THD *thd,
struct st_mysql_sys_var *var,
void *var_ptr,
const void *save);
static void rocksdb_set_rate_limiter_bytes_per_sec(THD *thd,
struct st_mysql_sys_var *var,
void *var_ptr,
const void *save);
static void rocksdb_set_sst_mgr_rate_bytes_per_sec(THD *thd,
struct st_mysql_sys_var *var,
void *var_ptr,
const void *save);
static void rocksdb_set_delayed_write_rate(THD *thd,
struct st_mysql_sys_var *var,
void *var_ptr, const void *save);
static void rocksdb_set_max_latest_deadlocks(THD *thd,
struct st_mysql_sys_var *var,
void *var_ptr, const void *save);
static void rdb_set_collation_exception_list(const char *exception_list);
static void rocksdb_set_collation_exception_list(THD *thd,
struct st_mysql_sys_var *var,
void *var_ptr,
const void *save);
void rocksdb_set_update_cf_options(THD *thd,
struct st_mysql_sys_var *var,
void *var_ptr,
const void *save);
static void
rocksdb_set_bulk_load(THD *thd,
struct st_mysql_sys_var *var MY_ATTRIBUTE((__unused__)),
void *var_ptr, const void *save);
static void rocksdb_set_bulk_load_allow_unsorted(
THD *thd, struct st_mysql_sys_var *var MY_ATTRIBUTE((__unused__)),
void *var_ptr, const void *save);
static void rocksdb_set_max_background_jobs(THD *thd,
struct st_mysql_sys_var *const var,
void *const var_ptr,
const void *const save);
//////////////////////////////////////////////////////////////////////////////
// Options definitions
//////////////////////////////////////////////////////////////////////////////
static long long rocksdb_block_cache_size;
static long long rocksdb_sim_cache_size;
static my_bool rocksdb_use_clock_cache;
/* Use unsigned long long instead of uint64_t because of MySQL compatibility */
static unsigned long long // NOLINT(runtime/int)
rocksdb_rate_limiter_bytes_per_sec;
static unsigned long long // NOLINT(runtime/int)
rocksdb_sst_mgr_rate_bytes_per_sec;
static unsigned long long rocksdb_delayed_write_rate;
static uint32_t rocksdb_max_latest_deadlocks;
static unsigned long // NOLINT(runtime/int)
rocksdb_persistent_cache_size_mb;
static ulong rocksdb_info_log_level;
static char *rocksdb_wal_dir;
static char *rocksdb_persistent_cache_path;
static ulong rocksdb_index_type;
static uint32_t rocksdb_flush_log_at_trx_commit;
static uint32_t rocksdb_debug_optimizer_n_rows;
static my_bool rocksdb_force_compute_memtable_stats;
static uint32_t rocksdb_force_compute_memtable_stats_cachetime;
static my_bool rocksdb_debug_optimizer_no_zero_cardinality;
static uint32_t rocksdb_wal_recovery_mode;
static uint32_t rocksdb_access_hint_on_compaction_start;
static char *rocksdb_compact_cf_name;
static char *rocksdb_checkpoint_name;
static my_bool rocksdb_signal_drop_index_thread;
static my_bool rocksdb_strict_collation_check = 1;
static my_bool rocksdb_enable_2pc = 0;
static char *rocksdb_strict_collation_exceptions;
static my_bool rocksdb_collect_sst_properties = 1;
static my_bool rocksdb_force_flush_memtable_now_var = 0;
static my_bool rocksdb_force_flush_memtable_and_lzero_now_var = 0;
static my_bool rocksdb_enable_ttl = 1;
static my_bool rocksdb_enable_ttl_read_filtering = 1;
static int rocksdb_debug_ttl_rec_ts = 0;
static int rocksdb_debug_ttl_snapshot_ts = 0;
static int rocksdb_debug_ttl_read_filter_ts = 0;
static my_bool rocksdb_debug_ttl_ignore_pk = 0;
static my_bool rocksdb_reset_stats = 0;
static uint32_t rocksdb_io_write_timeout_secs = 0;
static uint64_t rocksdb_number_stat_computes = 0;
static uint32_t rocksdb_seconds_between_stat_computes = 3600;
static long long rocksdb_compaction_sequential_deletes = 0l;
static long long rocksdb_compaction_sequential_deletes_window = 0l;
static long long rocksdb_compaction_sequential_deletes_file_size = 0l;
static uint32_t rocksdb_validate_tables = 1;
static char *rocksdb_datadir;
static uint32_t rocksdb_table_stats_sampling_pct;
static my_bool rocksdb_enable_bulk_load_api = 1;
static my_bool rocksdb_print_snapshot_conflict_queries = 0;
static my_bool rocksdb_large_prefix = 0;
char *compression_types_val=
const_cast<char*>(get_rocksdb_supported_compression_types());
std::atomic<uint64_t> rocksdb_snapshot_conflict_errors(0);
std::atomic<uint64_t> rocksdb_wal_group_syncs(0);
static std::unique_ptr<rocksdb::DBOptions> rdb_init_rocksdb_db_options(void) {
auto o = std::unique_ptr<rocksdb::DBOptions>(new rocksdb::DBOptions());
o->create_if_missing = true;
o->listeners.push_back(std::make_shared<Rdb_event_listener>(&ddl_manager));
o->info_log_level = rocksdb::InfoLogLevel::INFO_LEVEL;
o->max_subcompactions = DEFAULT_SUBCOMPACTIONS;
o->concurrent_prepare = true;
o->manual_wal_flush = true;
return o;
}
/* DBOptions contains Statistics and needs to be destructed last */
static std::unique_ptr<rocksdb::BlockBasedTableOptions> rocksdb_tbl_options =
std::unique_ptr<rocksdb::BlockBasedTableOptions>(
new rocksdb::BlockBasedTableOptions());
static std::unique_ptr<rocksdb::DBOptions> rocksdb_db_options =
rdb_init_rocksdb_db_options();
static std::shared_ptr<rocksdb::RateLimiter> rocksdb_rate_limiter;
/* This enum needs to be kept up to date with rocksdb::InfoLogLevel */
static const char *info_log_level_names[] = {"debug_level", "info_level",
"warn_level", "error_level",
"fatal_level", NullS};
static TYPELIB info_log_level_typelib = {
array_elements(info_log_level_names) - 1, "info_log_level_typelib",
info_log_level_names, nullptr};
static void rocksdb_set_rocksdb_info_log_level(
THD *const thd, struct st_mysql_sys_var *const var, void *const var_ptr,
const void *const save) {
DBUG_ASSERT(save != nullptr);
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
rocksdb_info_log_level = *static_cast<const uint64_t *>(save);
rocksdb_db_options->info_log->SetInfoLogLevel(
static_cast<const rocksdb::InfoLogLevel>(rocksdb_info_log_level));
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
static void rocksdb_set_reset_stats(
my_core::THD *const /* unused */,
my_core::st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr, const void *const save) {
DBUG_ASSERT(save != nullptr);
DBUG_ASSERT(rdb != nullptr);
DBUG_ASSERT(rocksdb_stats != nullptr);
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
*static_cast<bool *>(var_ptr) = *static_cast<const bool *>(save);
if (rocksdb_reset_stats) {
rocksdb::Status s = rdb->ResetStats();
// RocksDB will always return success. Let's document this assumption here
// as well so that we'll get immediately notified when contract changes.
DBUG_ASSERT(s == rocksdb::Status::OK());
s = rocksdb_stats->Reset();
DBUG_ASSERT(s == rocksdb::Status::OK());
}
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
static void rocksdb_set_io_write_timeout(
my_core::THD *const thd MY_ATTRIBUTE((__unused__)),
my_core::st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr MY_ATTRIBUTE((__unused__)), const void *const save) {
DBUG_ASSERT(save != nullptr);
DBUG_ASSERT(rdb != nullptr);
#if !defined(_WIN32) && !defined(__APPLE__)
DBUG_ASSERT(io_watchdog != nullptr);
#endif
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
const uint32_t new_val = *static_cast<const uint32_t *>(save);
rocksdb_io_write_timeout_secs = new_val;
#if !defined(_WIN32) && !defined(__APPLE__)
io_watchdog->reset_timeout(rocksdb_io_write_timeout_secs);
#endif
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
static const char *index_type_names[] = {"kBinarySearch", "kHashSearch", NullS};
static TYPELIB index_type_typelib = {array_elements(index_type_names) - 1,
"index_type_typelib", index_type_names,
nullptr};
const ulong RDB_MAX_LOCK_WAIT_SECONDS = 1024 * 1024 * 1024;
const ulong RDB_MAX_ROW_LOCKS = 1024 * 1024 * 1024;
const ulong RDB_DEFAULT_BULK_LOAD_SIZE = 1000;
const ulong RDB_MAX_BULK_LOAD_SIZE = 1024 * 1024 * 1024;
const size_t RDB_DEFAULT_MERGE_BUF_SIZE = 64 * 1024 * 1024;
const size_t RDB_MIN_MERGE_BUF_SIZE = 100;
const size_t RDB_DEFAULT_MERGE_COMBINE_READ_SIZE = 1024 * 1024 * 1024;
const size_t RDB_MIN_MERGE_COMBINE_READ_SIZE = 100;
const size_t RDB_DEFAULT_MERGE_TMP_FILE_REMOVAL_DELAY = 0;
const size_t RDB_MIN_MERGE_TMP_FILE_REMOVAL_DELAY = 0;
const int64 RDB_DEFAULT_BLOCK_CACHE_SIZE = 512 * 1024 * 1024;
const int64 RDB_MIN_BLOCK_CACHE_SIZE = 1024;
const int RDB_MAX_CHECKSUMS_PCT = 100;
const ulong RDB_DEADLOCK_DETECT_DEPTH = 50;
// TODO: 0 means don't wait at all, and we don't support it yet?
static MYSQL_THDVAR_ULONG(lock_wait_timeout, PLUGIN_VAR_RQCMDARG,
"Number of seconds to wait for lock", nullptr,
nullptr, /*default*/ 1, /*min*/ 1,
/*max*/ RDB_MAX_LOCK_WAIT_SECONDS, 0);
static MYSQL_THDVAR_BOOL(deadlock_detect, PLUGIN_VAR_RQCMDARG,
"Enables deadlock detection", nullptr, nullptr, FALSE);
static MYSQL_THDVAR_ULONG(deadlock_detect_depth, PLUGIN_VAR_RQCMDARG,
"Number of transactions deadlock detection will "
"traverse through before assuming deadlock",
nullptr, nullptr,
/*default*/ RDB_DEADLOCK_DETECT_DEPTH,
/*min*/ 2,
/*max*/ ULONG_MAX, 0);
static MYSQL_THDVAR_BOOL(
trace_sst_api, PLUGIN_VAR_RQCMDARG,
"Generate trace output in the log for each call to the SstFileWriter",
nullptr, nullptr, FALSE);
static MYSQL_THDVAR_BOOL(
bulk_load, PLUGIN_VAR_RQCMDARG,
"Use bulk-load mode for inserts. This disables "
"unique_checks and enables rocksdb_commit_in_the_middle.",
nullptr, rocksdb_set_bulk_load, FALSE);
static MYSQL_THDVAR_BOOL(bulk_load_allow_unsorted, PLUGIN_VAR_RQCMDARG,
"Allow unsorted input during bulk-load. "
"Can be changed only when bulk load is disabled.",
nullptr, rocksdb_set_bulk_load_allow_unsorted, FALSE);
static MYSQL_SYSVAR_BOOL(enable_bulk_load_api, rocksdb_enable_bulk_load_api,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Enables using SstFileWriter for bulk loading",
nullptr, nullptr, rocksdb_enable_bulk_load_api);
static MYSQL_THDVAR_STR(tmpdir, PLUGIN_VAR_OPCMDARG | PLUGIN_VAR_MEMALLOC,
"Directory for temporary files during DDL operations.",
nullptr, nullptr, "");
static MYSQL_THDVAR_STR(
skip_unique_check_tables, PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
"Skip unique constraint checking for the specified tables", nullptr,
nullptr, ".*");
static MYSQL_THDVAR_BOOL(
commit_in_the_middle, PLUGIN_VAR_RQCMDARG,
"Commit rows implicitly every rocksdb_bulk_load_size, on bulk load/insert, "
"update and delete",
nullptr, nullptr, FALSE);
static MYSQL_THDVAR_BOOL(
blind_delete_primary_key, PLUGIN_VAR_RQCMDARG,
"Deleting rows by primary key lookup, without reading rows (Blind Deletes)."
" Blind delete is disabled if the table has secondary key",
nullptr, nullptr, FALSE);
static MYSQL_THDVAR_STR(
read_free_rpl_tables, PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
"List of tables that will use read-free replication on the slave "
"(i.e. not lookup a row during replication)",
nullptr, nullptr, "");
static MYSQL_THDVAR_BOOL(skip_bloom_filter_on_read, PLUGIN_VAR_RQCMDARG,
"Skip using bloom filter for reads", nullptr, nullptr,
FALSE);
static MYSQL_THDVAR_ULONG(max_row_locks, PLUGIN_VAR_RQCMDARG,
"Maximum number of locks a transaction can have",
nullptr, nullptr,
/*default*/ RDB_MAX_ROW_LOCKS,
/*min*/ 1,
/*max*/ RDB_MAX_ROW_LOCKS, 0);
static MYSQL_THDVAR_ULONGLONG(
write_batch_max_bytes, PLUGIN_VAR_RQCMDARG,
"Maximum size of write batch in bytes. 0 means no limit.", nullptr, nullptr,
/* default */ 0, /* min */ 0, /* max */ SIZE_T_MAX, 1);
static MYSQL_THDVAR_BOOL(
lock_scanned_rows, PLUGIN_VAR_RQCMDARG,
"Take and hold locks on rows that are scanned but not updated", nullptr,
nullptr, FALSE);
static MYSQL_THDVAR_ULONG(bulk_load_size, PLUGIN_VAR_RQCMDARG,
"Max #records in a batch for bulk-load mode", nullptr,
nullptr,
/*default*/ RDB_DEFAULT_BULK_LOAD_SIZE,
/*min*/ 1,
/*max*/ RDB_MAX_BULK_LOAD_SIZE, 0);
static MYSQL_THDVAR_ULONGLONG(
merge_buf_size, PLUGIN_VAR_RQCMDARG,
"Size to allocate for merge sort buffers written out to disk "
"during inplace index creation.",
nullptr, nullptr,
/* default (64MB) */ RDB_DEFAULT_MERGE_BUF_SIZE,
/* min (100B) */ RDB_MIN_MERGE_BUF_SIZE,
/* max */ SIZE_T_MAX, 1);
static MYSQL_THDVAR_ULONGLONG(
merge_combine_read_size, PLUGIN_VAR_RQCMDARG,
"Size that we have to work with during combine (reading from disk) phase "
"of "
"external sort during fast index creation.",
nullptr, nullptr,
/* default (1GB) */ RDB_DEFAULT_MERGE_COMBINE_READ_SIZE,
/* min (100B) */ RDB_MIN_MERGE_COMBINE_READ_SIZE,
/* max */ SIZE_T_MAX, 1);
static MYSQL_THDVAR_ULONGLONG(
merge_tmp_file_removal_delay_ms, PLUGIN_VAR_RQCMDARG,
"Fast index creation creates a large tmp file on disk during index "
"creation. Removing this large file all at once when index creation is "
"complete can cause trim stalls on Flash. This variable specifies a "
"duration to sleep (in milliseconds) between calling chsize() to truncate "
"the file in chunks. The chunk size is the same as merge_buf_size.",
nullptr, nullptr,
/* default (0ms) */ RDB_DEFAULT_MERGE_TMP_FILE_REMOVAL_DELAY,
/* min (0ms) */ RDB_MIN_MERGE_TMP_FILE_REMOVAL_DELAY,
/* max */ SIZE_T_MAX, 1);
static MYSQL_SYSVAR_BOOL(
create_if_missing,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->create_if_missing),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::create_if_missing for RocksDB", nullptr, nullptr,
rocksdb_db_options->create_if_missing);
static MYSQL_SYSVAR_BOOL(
concurrent_prepare,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->concurrent_prepare),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::concurrent_prepare for RocksDB", nullptr, nullptr,
rocksdb_db_options->concurrent_prepare);
static MYSQL_SYSVAR_BOOL(
manual_wal_flush,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->manual_wal_flush),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::manual_wal_flush for RocksDB", nullptr, nullptr,
rocksdb_db_options->manual_wal_flush);
static MYSQL_SYSVAR_BOOL(
create_missing_column_families,
*reinterpret_cast<my_bool *>(
&rocksdb_db_options->create_missing_column_families),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::create_missing_column_families for RocksDB", nullptr, nullptr,
rocksdb_db_options->create_missing_column_families);
static MYSQL_SYSVAR_BOOL(
error_if_exists,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->error_if_exists),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::error_if_exists for RocksDB", nullptr, nullptr,
rocksdb_db_options->error_if_exists);
static MYSQL_SYSVAR_BOOL(
paranoid_checks,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->paranoid_checks),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::paranoid_checks for RocksDB", nullptr, nullptr,
rocksdb_db_options->paranoid_checks);
static MYSQL_SYSVAR_ULONGLONG(
rate_limiter_bytes_per_sec, rocksdb_rate_limiter_bytes_per_sec,
PLUGIN_VAR_RQCMDARG, "DBOptions::rate_limiter bytes_per_sec for RocksDB",
nullptr, rocksdb_set_rate_limiter_bytes_per_sec, /* default */ 0L,
/* min */ 0L, /* max */ MAX_RATE_LIMITER_BYTES_PER_SEC, 0);
static MYSQL_SYSVAR_ULONGLONG(
sst_mgr_rate_bytes_per_sec, rocksdb_sst_mgr_rate_bytes_per_sec,
PLUGIN_VAR_RQCMDARG,
"DBOptions::sst_file_manager rate_bytes_per_sec for RocksDB", nullptr,
rocksdb_set_sst_mgr_rate_bytes_per_sec,
/* default */ DEFAULT_SST_MGR_RATE_BYTES_PER_SEC,
/* min */ 0L, /* max */ UINT64_MAX, 0);
static MYSQL_SYSVAR_ULONGLONG(delayed_write_rate, rocksdb_delayed_write_rate,
PLUGIN_VAR_RQCMDARG,
"DBOptions::delayed_write_rate", nullptr,
rocksdb_set_delayed_write_rate,
rocksdb_db_options->delayed_write_rate, 0,
UINT64_MAX, 0);
static MYSQL_SYSVAR_UINT(max_latest_deadlocks, rocksdb_max_latest_deadlocks,
PLUGIN_VAR_RQCMDARG,
"Maximum number of recent "
"deadlocks to store",
nullptr, rocksdb_set_max_latest_deadlocks,
rocksdb::kInitialMaxDeadlocks, 0, UINT32_MAX, 0);
static MYSQL_SYSVAR_ENUM(
info_log_level, rocksdb_info_log_level, PLUGIN_VAR_RQCMDARG,
"Filter level for info logs to be written mysqld error log. "
"Valid values include 'debug_level', 'info_level', 'warn_level'"
"'error_level' and 'fatal_level'.",
nullptr, rocksdb_set_rocksdb_info_log_level,
rocksdb::InfoLogLevel::ERROR_LEVEL, &info_log_level_typelib);
static MYSQL_THDVAR_INT(
perf_context_level, PLUGIN_VAR_RQCMDARG,
"Perf Context Level for rocksdb internal timer stat collection", nullptr,
nullptr,
/* default */ rocksdb::PerfLevel::kUninitialized,
/* min */ rocksdb::PerfLevel::kUninitialized,
/* max */ rocksdb::PerfLevel::kOutOfBounds - 1, 0);
static MYSQL_SYSVAR_UINT(
wal_recovery_mode, rocksdb_wal_recovery_mode, PLUGIN_VAR_RQCMDARG,
"DBOptions::wal_recovery_mode for RocksDB. Default is kAbsoluteConsistency",
nullptr, nullptr,
/* default */ (uint)rocksdb::WALRecoveryMode::kAbsoluteConsistency,
/* min */ (uint)rocksdb::WALRecoveryMode::kTolerateCorruptedTailRecords,
/* max */ (uint)rocksdb::WALRecoveryMode::kSkipAnyCorruptedRecords, 0);
static MYSQL_SYSVAR_SIZE_T(compaction_readahead_size,
rocksdb_db_options->compaction_readahead_size,
PLUGIN_VAR_RQCMDARG,
"DBOptions::compaction_readahead_size for RocksDB",
nullptr, nullptr,
rocksdb_db_options->compaction_readahead_size,
/* min */ 0L, /* max */ SIZE_T_MAX, 0);
static MYSQL_SYSVAR_BOOL(
new_table_reader_for_compaction_inputs,
*reinterpret_cast<my_bool *>(
&rocksdb_db_options->new_table_reader_for_compaction_inputs),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::new_table_reader_for_compaction_inputs for RocksDB", nullptr,
nullptr, rocksdb_db_options->new_table_reader_for_compaction_inputs);
static MYSQL_SYSVAR_UINT(
access_hint_on_compaction_start, rocksdb_access_hint_on_compaction_start,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::access_hint_on_compaction_start for RocksDB", nullptr, nullptr,
/* default */ (uint)rocksdb::Options::AccessHint::NORMAL,
/* min */ (uint)rocksdb::Options::AccessHint::NONE,
/* max */ (uint)rocksdb::Options::AccessHint::WILLNEED, 0);
static MYSQL_SYSVAR_BOOL(
allow_concurrent_memtable_write,
*reinterpret_cast<my_bool *>(
&rocksdb_db_options->allow_concurrent_memtable_write),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::allow_concurrent_memtable_write for RocksDB", nullptr, nullptr,
false);
static MYSQL_SYSVAR_BOOL(
enable_write_thread_adaptive_yield,
*reinterpret_cast<my_bool *>(
&rocksdb_db_options->enable_write_thread_adaptive_yield),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::enable_write_thread_adaptive_yield for RocksDB", nullptr,
nullptr, false);
static MYSQL_SYSVAR_INT(max_open_files, rocksdb_db_options->max_open_files,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_open_files for RocksDB", nullptr,
nullptr, rocksdb_db_options->max_open_files,
/* min */ -1, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_UINT64_T(max_total_wal_size,
rocksdb_db_options->max_total_wal_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_total_wal_size for RocksDB", nullptr,
nullptr, rocksdb_db_options->max_total_wal_size,
/* min */ 0, /* max */ LONGLONG_MAX, 0);
static MYSQL_SYSVAR_BOOL(
use_fsync, *reinterpret_cast<my_bool *>(&rocksdb_db_options->use_fsync),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::use_fsync for RocksDB", nullptr, nullptr,
rocksdb_db_options->use_fsync);
static MYSQL_SYSVAR_STR(wal_dir, rocksdb_wal_dir,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::wal_dir for RocksDB", nullptr, nullptr,
rocksdb_db_options->wal_dir.c_str());
static MYSQL_SYSVAR_STR(
persistent_cache_path, rocksdb_persistent_cache_path,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Path for BlockBasedTableOptions::persistent_cache for RocksDB", nullptr,
nullptr, "");
static MYSQL_SYSVAR_ULONG(
persistent_cache_size_mb, rocksdb_persistent_cache_size_mb,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Size of cache in MB for BlockBasedTableOptions::persistent_cache "
"for RocksDB", nullptr, nullptr, rocksdb_persistent_cache_size_mb,
/* min */ 0L, /* max */ ULONG_MAX, 0);
static MYSQL_SYSVAR_UINT64_T(
delete_obsolete_files_period_micros,
rocksdb_db_options->delete_obsolete_files_period_micros,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::delete_obsolete_files_period_micros for RocksDB", nullptr,
nullptr, rocksdb_db_options->delete_obsolete_files_period_micros,
/* min */ 0, /* max */ LONGLONG_MAX, 0);
static MYSQL_SYSVAR_INT(max_background_jobs,
rocksdb_db_options->max_background_jobs,
PLUGIN_VAR_RQCMDARG,
"DBOptions::max_background_jobs for RocksDB", nullptr,
rocksdb_set_max_background_jobs,
rocksdb_db_options->max_background_jobs,
/* min */ -1, /* max */ MAX_BACKGROUND_JOBS, 0);
static MYSQL_SYSVAR_UINT(max_subcompactions,
rocksdb_db_options->max_subcompactions,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_subcompactions for RocksDB", nullptr,
nullptr, rocksdb_db_options->max_subcompactions,
/* min */ 1, /* max */ MAX_SUBCOMPACTIONS, 0);
static MYSQL_SYSVAR_SIZE_T(max_log_file_size,
rocksdb_db_options->max_log_file_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_log_file_size for RocksDB", nullptr,
nullptr, rocksdb_db_options->max_log_file_size,
/* min */ 0L, /* max */ SIZE_T_MAX, 0);
static MYSQL_SYSVAR_SIZE_T(log_file_time_to_roll,
rocksdb_db_options->log_file_time_to_roll,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::log_file_time_to_roll for RocksDB",
nullptr, nullptr,
rocksdb_db_options->log_file_time_to_roll,
/* min */ 0L, /* max */ SIZE_T_MAX, 0);
static MYSQL_SYSVAR_SIZE_T(keep_log_file_num,
rocksdb_db_options->keep_log_file_num,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::keep_log_file_num for RocksDB", nullptr,
nullptr, rocksdb_db_options->keep_log_file_num,
/* min */ 0L, /* max */ SIZE_T_MAX, 0);
static MYSQL_SYSVAR_UINT64_T(max_manifest_file_size,
rocksdb_db_options->max_manifest_file_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_manifest_file_size for RocksDB",
nullptr, nullptr,
rocksdb_db_options->max_manifest_file_size,
/* min */ 0L, /* max */ ULONGLONG_MAX, 0);
static MYSQL_SYSVAR_INT(table_cache_numshardbits,
rocksdb_db_options->table_cache_numshardbits,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::table_cache_numshardbits for RocksDB",
nullptr, nullptr,
rocksdb_db_options->table_cache_numshardbits,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_UINT64_T(wal_ttl_seconds, rocksdb_db_options->WAL_ttl_seconds,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::WAL_ttl_seconds for RocksDB", nullptr,
nullptr, rocksdb_db_options->WAL_ttl_seconds,
/* min */ 0L, /* max */ LONGLONG_MAX, 0);
static MYSQL_SYSVAR_UINT64_T(wal_size_limit_mb,
rocksdb_db_options->WAL_size_limit_MB,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::WAL_size_limit_MB for RocksDB", nullptr,
nullptr, rocksdb_db_options->WAL_size_limit_MB,
/* min */ 0L, /* max */ LONGLONG_MAX, 0);
static MYSQL_SYSVAR_SIZE_T(manifest_preallocation_size,
rocksdb_db_options->manifest_preallocation_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::manifest_preallocation_size for RocksDB",
nullptr, nullptr,
rocksdb_db_options->manifest_preallocation_size,
/* min */ 0L, /* max */ SIZE_T_MAX, 0);
static MYSQL_SYSVAR_BOOL(
use_direct_reads,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->use_direct_reads),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::use_direct_reads for RocksDB", nullptr, nullptr,
rocksdb_db_options->use_direct_reads);
static MYSQL_SYSVAR_BOOL(
use_direct_io_for_flush_and_compaction,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->use_direct_io_for_flush_and_compaction),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::use_direct_io_for_flush_and_compaction for RocksDB", nullptr, nullptr,
rocksdb_db_options->use_direct_io_for_flush_and_compaction);
static MYSQL_SYSVAR_BOOL(
allow_mmap_reads,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->allow_mmap_reads),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::allow_mmap_reads for RocksDB", nullptr, nullptr,
rocksdb_db_options->allow_mmap_reads);
static MYSQL_SYSVAR_BOOL(
allow_mmap_writes,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->allow_mmap_writes),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::allow_mmap_writes for RocksDB", nullptr, nullptr,
rocksdb_db_options->allow_mmap_writes);
static MYSQL_SYSVAR_BOOL(
is_fd_close_on_exec,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->is_fd_close_on_exec),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::is_fd_close_on_exec for RocksDB", nullptr, nullptr,
rocksdb_db_options->is_fd_close_on_exec);
static MYSQL_SYSVAR_UINT(stats_dump_period_sec,
rocksdb_db_options->stats_dump_period_sec,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::stats_dump_period_sec for RocksDB",
nullptr, nullptr,
rocksdb_db_options->stats_dump_period_sec,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_BOOL(
advise_random_on_open,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->advise_random_on_open),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::advise_random_on_open for RocksDB", nullptr, nullptr,
rocksdb_db_options->advise_random_on_open);
static MYSQL_SYSVAR_SIZE_T(db_write_buffer_size,
rocksdb_db_options->db_write_buffer_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::db_write_buffer_size for RocksDB",
nullptr, nullptr,
rocksdb_db_options->db_write_buffer_size,
/* min */ 0L, /* max */ SIZE_T_MAX, 0);
static MYSQL_SYSVAR_BOOL(
use_adaptive_mutex,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->use_adaptive_mutex),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::use_adaptive_mutex for RocksDB", nullptr, nullptr,
rocksdb_db_options->use_adaptive_mutex);
static MYSQL_SYSVAR_UINT64_T(bytes_per_sync, rocksdb_db_options->bytes_per_sync,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::bytes_per_sync for RocksDB", nullptr,
nullptr, rocksdb_db_options->bytes_per_sync,
/* min */ 0L, /* max */ ULONGLONG_MAX, 0);
static MYSQL_SYSVAR_UINT64_T(wal_bytes_per_sync,
rocksdb_db_options->wal_bytes_per_sync,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::wal_bytes_per_sync for RocksDB", nullptr,
nullptr, rocksdb_db_options->wal_bytes_per_sync,
/* min */ 0L, /* max */ ULONGLONG_MAX, 0);
static MYSQL_SYSVAR_BOOL(
enable_thread_tracking,
*reinterpret_cast<my_bool *>(&rocksdb_db_options->enable_thread_tracking),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::enable_thread_tracking for RocksDB", nullptr, nullptr, true);
static MYSQL_SYSVAR_LONGLONG(block_cache_size, rocksdb_block_cache_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"block_cache size for RocksDB", nullptr, nullptr,
/* default */ RDB_DEFAULT_BLOCK_CACHE_SIZE,
/* min */ RDB_MIN_BLOCK_CACHE_SIZE,
/* max */ LONGLONG_MAX,
/* Block size */ RDB_MIN_BLOCK_CACHE_SIZE);
static MYSQL_SYSVAR_LONGLONG(sim_cache_size, rocksdb_sim_cache_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Simulated cache size for RocksDB", nullptr,
nullptr,
/* default */ 0,
/* min */ 0,
/* max */ LONGLONG_MAX,
/* Block size */ 0);
static MYSQL_SYSVAR_BOOL(
use_clock_cache,
rocksdb_use_clock_cache,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Use ClockCache instead of default LRUCache for RocksDB",
nullptr, nullptr, false);
static MYSQL_SYSVAR_BOOL(
cache_index_and_filter_blocks,
*reinterpret_cast<my_bool *>(
&rocksdb_tbl_options->cache_index_and_filter_blocks),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::cache_index_and_filter_blocks for RocksDB",
nullptr, nullptr, true);
// When pin_l0_filter_and_index_blocks_in_cache is true, RocksDB will use the
// LRU cache, but will always keep the filter & idndex block's handle checked
// out (=won't call ShardedLRUCache::Release), plus the parsed out objects
// the LRU cache will never push flush them out, hence they're pinned.
//
// This fixes the mutex contention between :ShardedLRUCache::Lookup and
// ShardedLRUCache::Release which reduced the QPS ratio (QPS using secondary
// index / QPS using PK).
static MYSQL_SYSVAR_BOOL(
pin_l0_filter_and_index_blocks_in_cache,
*reinterpret_cast<my_bool *>(
&rocksdb_tbl_options->pin_l0_filter_and_index_blocks_in_cache),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"pin_l0_filter_and_index_blocks_in_cache for RocksDB", nullptr, nullptr,
true);
static MYSQL_SYSVAR_ENUM(index_type, rocksdb_index_type,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::index_type for RocksDB",
nullptr, nullptr,
(ulong)rocksdb_tbl_options->index_type,
&index_type_typelib);
static MYSQL_SYSVAR_BOOL(
hash_index_allow_collision,
*reinterpret_cast<my_bool *>(
&rocksdb_tbl_options->hash_index_allow_collision),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::hash_index_allow_collision for RocksDB", nullptr,
nullptr, rocksdb_tbl_options->hash_index_allow_collision);
static MYSQL_SYSVAR_BOOL(
no_block_cache,
*reinterpret_cast<my_bool *>(&rocksdb_tbl_options->no_block_cache),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::no_block_cache for RocksDB", nullptr, nullptr,
rocksdb_tbl_options->no_block_cache);
static MYSQL_SYSVAR_SIZE_T(block_size, rocksdb_tbl_options->block_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::block_size for RocksDB",
nullptr, nullptr, rocksdb_tbl_options->block_size,
/* min */ 1L, /* max */ SIZE_T_MAX, 0);
static MYSQL_SYSVAR_INT(
block_size_deviation, rocksdb_tbl_options->block_size_deviation,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::block_size_deviation for RocksDB", nullptr,
nullptr, rocksdb_tbl_options->block_size_deviation,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_INT(
block_restart_interval, rocksdb_tbl_options->block_restart_interval,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::block_restart_interval for RocksDB", nullptr,
nullptr, rocksdb_tbl_options->block_restart_interval,
/* min */ 1, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_BOOL(
whole_key_filtering,
*reinterpret_cast<my_bool *>(&rocksdb_tbl_options->whole_key_filtering),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::whole_key_filtering for RocksDB", nullptr, nullptr,
rocksdb_tbl_options->whole_key_filtering);
static MYSQL_SYSVAR_STR(default_cf_options, rocksdb_default_cf_options,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"default cf options for RocksDB", nullptr, nullptr, "");
static MYSQL_SYSVAR_STR(override_cf_options, rocksdb_override_cf_options,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"option overrides per cf for RocksDB", nullptr, nullptr,
"");
static MYSQL_SYSVAR_STR(update_cf_options, rocksdb_update_cf_options,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC
/* psergey-merge: need this? : PLUGIN_VAR_ALLOCATED*/,
"Option updates per column family for RocksDB", nullptr,
rocksdb_set_update_cf_options, nullptr);
enum rocksdb_flush_log_at_trx_commit_type : unsigned int {
FLUSH_LOG_NEVER = 0,
FLUSH_LOG_SYNC,
FLUSH_LOG_BACKGROUND,
FLUSH_LOG_MAX /* must be last */
};
static MYSQL_SYSVAR_UINT(flush_log_at_trx_commit,
rocksdb_flush_log_at_trx_commit, PLUGIN_VAR_RQCMDARG,
"Sync on transaction commit. Similar to "
"innodb_flush_log_at_trx_commit. 1: sync on commit, "
"0,2: not sync on commit",
nullptr, nullptr, /* default */ FLUSH_LOG_SYNC,
/* min */ FLUSH_LOG_NEVER,
/* max */ FLUSH_LOG_BACKGROUND, 0);
static MYSQL_THDVAR_BOOL(write_disable_wal, PLUGIN_VAR_RQCMDARG,
"WriteOptions::disableWAL for RocksDB", nullptr,
nullptr, rocksdb::WriteOptions().disableWAL);
static MYSQL_THDVAR_BOOL(
write_ignore_missing_column_families, PLUGIN_VAR_RQCMDARG,
"WriteOptions::ignore_missing_column_families for RocksDB", nullptr,
nullptr, rocksdb::WriteOptions().ignore_missing_column_families);
static MYSQL_THDVAR_BOOL(skip_fill_cache, PLUGIN_VAR_RQCMDARG,
"Skip filling block cache on read requests", nullptr,
nullptr, FALSE);
static MYSQL_THDVAR_BOOL(
unsafe_for_binlog, PLUGIN_VAR_RQCMDARG,
"Allowing statement based binary logging which may break consistency",
nullptr, nullptr, FALSE);
static MYSQL_THDVAR_UINT(records_in_range, PLUGIN_VAR_RQCMDARG,
"Used to override the result of records_in_range(). "
"Set to a positive number to override",
nullptr, nullptr, 0,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_THDVAR_UINT(force_index_records_in_range, PLUGIN_VAR_RQCMDARG,
"Used to override the result of records_in_range() "
"when FORCE INDEX is used.",
nullptr, nullptr, 0,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_UINT(
debug_optimizer_n_rows, rocksdb_debug_optimizer_n_rows,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY | PLUGIN_VAR_NOSYSVAR,
"Test only to override rocksdb estimates of table size in a memtable",
nullptr, nullptr, 0, /* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_BOOL(force_compute_memtable_stats,
rocksdb_force_compute_memtable_stats,
PLUGIN_VAR_RQCMDARG,
"Force to always compute memtable stats",
nullptr, nullptr, TRUE);
static MYSQL_SYSVAR_UINT(force_compute_memtable_stats_cachetime,
rocksdb_force_compute_memtable_stats_cachetime,
PLUGIN_VAR_RQCMDARG,
"Time in usecs to cache memtable estimates", nullptr,
nullptr, /* default */ 60 * 1000 * 1000,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_BOOL(
debug_optimizer_no_zero_cardinality,
rocksdb_debug_optimizer_no_zero_cardinality, PLUGIN_VAR_RQCMDARG,
"In case if cardinality is zero, overrides it with some value", nullptr,
nullptr, TRUE);
static MYSQL_SYSVAR_STR(compact_cf, rocksdb_compact_cf_name,
PLUGIN_VAR_RQCMDARG, "Compact column family",
rocksdb_compact_column_family,
rocksdb_compact_column_family_stub, "");
static MYSQL_SYSVAR_STR(create_checkpoint, rocksdb_checkpoint_name,
PLUGIN_VAR_RQCMDARG, "Checkpoint directory",
rocksdb_create_checkpoint,
rocksdb_create_checkpoint_stub, "");
static MYSQL_SYSVAR_BOOL(signal_drop_index_thread,
rocksdb_signal_drop_index_thread, PLUGIN_VAR_RQCMDARG,
"Wake up drop index thread", nullptr,
rocksdb_drop_index_wakeup_thread, FALSE);
static MYSQL_SYSVAR_BOOL(pause_background_work, rocksdb_pause_background_work,
PLUGIN_VAR_RQCMDARG,
"Disable all rocksdb background operations", nullptr,
rocksdb_set_pause_background_work, FALSE);
static MYSQL_SYSVAR_BOOL(
enable_ttl, rocksdb_enable_ttl, PLUGIN_VAR_RQCMDARG,
"Enable expired TTL records to be dropped during compaction.", nullptr,
nullptr, TRUE);
static MYSQL_SYSVAR_BOOL(
enable_ttl_read_filtering, rocksdb_enable_ttl_read_filtering,
PLUGIN_VAR_RQCMDARG,
"For tables with TTL, expired records are skipped/filtered out during "
"processing and in query results. Disabling this will allow these records "
"to be seen, but as a result rows may disappear in the middle of "
"transactions as they are dropped during compaction. Use with caution.",
nullptr, nullptr, TRUE);
static MYSQL_SYSVAR_INT(
debug_ttl_rec_ts, rocksdb_debug_ttl_rec_ts, PLUGIN_VAR_RQCMDARG,
"For debugging purposes only. Overrides the TTL of records to "
"now() + debug_ttl_rec_ts. The value can be +/- to simulate "
"a record inserted in the past vs a record inserted in the 'future'. "
"A value of 0 denotes that the variable is not set. This variable is a "
"no-op in non-debug builds.",
nullptr, nullptr, 0, /* min */ -3600, /* max */ 3600, 0);
static MYSQL_SYSVAR_INT(
debug_ttl_snapshot_ts, rocksdb_debug_ttl_snapshot_ts, PLUGIN_VAR_RQCMDARG,
"For debugging purposes only. Sets the snapshot during compaction to "
"now() + debug_set_ttl_snapshot_ts. The value can be +/- to simulate "
"a snapshot in the past vs a snapshot created in the 'future'. "
"A value of 0 denotes that the variable is not set. This variable is a "
"no-op in non-debug builds.",
nullptr, nullptr, 0, /* min */ -3600, /* max */ 3600, 0);
static MYSQL_SYSVAR_INT(
debug_ttl_read_filter_ts, rocksdb_debug_ttl_read_filter_ts,
PLUGIN_VAR_RQCMDARG,
"For debugging purposes only. Overrides the TTL read filtering time to "
"time + debug_ttl_read_filter_ts. A value of 0 denotes that the variable "
"is not set. This variable is a no-op in non-debug builds.",
nullptr, nullptr, 0, /* min */ -3600, /* max */ 3600, 0);
static MYSQL_SYSVAR_BOOL(
debug_ttl_ignore_pk, rocksdb_debug_ttl_ignore_pk, PLUGIN_VAR_RQCMDARG,
"For debugging purposes only. If true, compaction filtering will not occur "
"on PK TTL data. This variable is a no-op in non-debug builds.",
nullptr, nullptr, FALSE);
static MYSQL_SYSVAR_BOOL(
reset_stats, rocksdb_reset_stats, PLUGIN_VAR_RQCMDARG,
"Reset the RocksDB internal statistics without restarting the DB.", nullptr,
rocksdb_set_reset_stats, FALSE);
static MYSQL_SYSVAR_UINT(io_write_timeout, rocksdb_io_write_timeout_secs,
PLUGIN_VAR_RQCMDARG,
"Timeout for experimental I/O watchdog.", nullptr,
rocksdb_set_io_write_timeout, /* default */ 0,
/* min */ 0L,
/* max */ UINT_MAX, 0);
static MYSQL_SYSVAR_BOOL(enable_2pc, rocksdb_enable_2pc, PLUGIN_VAR_RQCMDARG,
"Enable two phase commit for MyRocks", nullptr,
nullptr, TRUE);
static MYSQL_SYSVAR_BOOL(strict_collation_check, rocksdb_strict_collation_check,
PLUGIN_VAR_RQCMDARG,
"Enforce case sensitive collation for MyRocks indexes",
nullptr, nullptr, TRUE);
static MYSQL_SYSVAR_STR(strict_collation_exceptions,
rocksdb_strict_collation_exceptions,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
"List of tables (using regex) that are excluded "
"from the case sensitive collation enforcement",
nullptr, rocksdb_set_collation_exception_list, "");
static MYSQL_SYSVAR_BOOL(collect_sst_properties, rocksdb_collect_sst_properties,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Enables collecting SST file properties on each flush",
nullptr, nullptr, rocksdb_collect_sst_properties);
static MYSQL_SYSVAR_BOOL(
force_flush_memtable_now, rocksdb_force_flush_memtable_now_var,
PLUGIN_VAR_RQCMDARG,
"Forces memstore flush which may block all write requests so be careful",
rocksdb_force_flush_memtable_now, rocksdb_force_flush_memtable_now_stub,
FALSE);
static MYSQL_SYSVAR_BOOL(
force_flush_memtable_and_lzero_now,
rocksdb_force_flush_memtable_and_lzero_now_var, PLUGIN_VAR_RQCMDARG,
"Acts similar to force_flush_memtable_now, but also compacts all L0 files.",
rocksdb_force_flush_memtable_and_lzero_now,
rocksdb_force_flush_memtable_and_lzero_now_stub, FALSE);
static MYSQL_THDVAR_BOOL(
flush_memtable_on_analyze, PLUGIN_VAR_RQCMDARG,
"Forces memtable flush on ANALZYE table to get accurate cardinality",
nullptr, nullptr, true);
static MYSQL_SYSVAR_UINT(
seconds_between_stat_computes, rocksdb_seconds_between_stat_computes,
PLUGIN_VAR_RQCMDARG,
"Sets a number of seconds to wait between optimizer stats recomputation. "
"Only changed indexes will be refreshed.",
nullptr, nullptr, rocksdb_seconds_between_stat_computes,
/* min */ 0L, /* max */ UINT_MAX, 0);
static MYSQL_SYSVAR_LONGLONG(compaction_sequential_deletes,
rocksdb_compaction_sequential_deletes,
PLUGIN_VAR_RQCMDARG,
"RocksDB will trigger compaction for the file if "
"it has more than this number sequential deletes "
"per window",
nullptr, rocksdb_set_compaction_options,
DEFAULT_COMPACTION_SEQUENTIAL_DELETES,
/* min */ 0L,
/* max */ MAX_COMPACTION_SEQUENTIAL_DELETES, 0);
static MYSQL_SYSVAR_LONGLONG(
compaction_sequential_deletes_window,
rocksdb_compaction_sequential_deletes_window, PLUGIN_VAR_RQCMDARG,
"Size of the window for counting rocksdb_compaction_sequential_deletes",
nullptr, rocksdb_set_compaction_options,
DEFAULT_COMPACTION_SEQUENTIAL_DELETES_WINDOW,
/* min */ 0L, /* max */ MAX_COMPACTION_SEQUENTIAL_DELETES_WINDOW, 0);
static MYSQL_SYSVAR_LONGLONG(
compaction_sequential_deletes_file_size,
rocksdb_compaction_sequential_deletes_file_size, PLUGIN_VAR_RQCMDARG,
"Minimum file size required for compaction_sequential_deletes", nullptr,
rocksdb_set_compaction_options, 0L,
/* min */ -1L, /* max */ LONGLONG_MAX, 0);
static MYSQL_SYSVAR_BOOL(
compaction_sequential_deletes_count_sd,
rocksdb_compaction_sequential_deletes_count_sd, PLUGIN_VAR_RQCMDARG,
"Counting SingleDelete as rocksdb_compaction_sequential_deletes", nullptr,
nullptr, rocksdb_compaction_sequential_deletes_count_sd);
static MYSQL_SYSVAR_BOOL(
print_snapshot_conflict_queries, rocksdb_print_snapshot_conflict_queries,
PLUGIN_VAR_RQCMDARG,
"Logging queries that got snapshot conflict errors into *.err log", nullptr,
nullptr, rocksdb_print_snapshot_conflict_queries);
static MYSQL_THDVAR_INT(checksums_pct, PLUGIN_VAR_RQCMDARG,
"How many percentages of rows to be checksummed",
nullptr, nullptr, RDB_MAX_CHECKSUMS_PCT,
/* min */ 0, /* max */ RDB_MAX_CHECKSUMS_PCT, 0);
static MYSQL_THDVAR_BOOL(store_row_debug_checksums, PLUGIN_VAR_RQCMDARG,
"Include checksums when writing index/table records",
nullptr, nullptr, false /* default value */);
static MYSQL_THDVAR_BOOL(verify_row_debug_checksums, PLUGIN_VAR_RQCMDARG,
"Verify checksums when reading index/table records",
nullptr, nullptr, false /* default value */);
static MYSQL_THDVAR_BOOL(master_skip_tx_api, PLUGIN_VAR_RQCMDARG,
"Skipping holding any lock on row access. "
"Not effective on slave.",
nullptr, nullptr, false);
static MYSQL_SYSVAR_UINT(
validate_tables, rocksdb_validate_tables,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Verify all .frm files match all RocksDB tables (0 means no verification, "
"1 means verify and fail on error, and 2 means verify but continue",
nullptr, nullptr, 1 /* default value */, 0 /* min value */,
2 /* max value */, 0);
static MYSQL_SYSVAR_STR(datadir, rocksdb_datadir,
PLUGIN_VAR_OPCMDARG | PLUGIN_VAR_READONLY,
"RocksDB data directory", nullptr, nullptr,
"./.rocksdb");
static MYSQL_SYSVAR_STR(supported_compression_types,
compression_types_val,
PLUGIN_VAR_NOCMDOPT | PLUGIN_VAR_READONLY,
"Compression algorithms supported by RocksDB",
nullptr, nullptr,
compression_types_val);
static MYSQL_SYSVAR_UINT(
table_stats_sampling_pct, rocksdb_table_stats_sampling_pct,
PLUGIN_VAR_RQCMDARG,
"Percentage of entries to sample when collecting statistics about table "
"properties. Specify either 0 to sample everything or percentage "
"[" STRINGIFY_ARG(RDB_TBL_STATS_SAMPLE_PCT_MIN) ".." STRINGIFY_ARG(
RDB_TBL_STATS_SAMPLE_PCT_MAX) "]. "
"By default " STRINGIFY_ARG(
RDB_DEFAULT_TBL_STATS_SAMPLE_PCT) "% "
"of"
" e"
"nt"
"ri"
"es"
" a"
"re"
" "
"sa"
"mp"
"le"
"d"
".",
nullptr, rocksdb_set_table_stats_sampling_pct, /* default */
RDB_DEFAULT_TBL_STATS_SAMPLE_PCT, /* everything */ 0,
/* max */ RDB_TBL_STATS_SAMPLE_PCT_MAX, 0);
static MYSQL_SYSVAR_BOOL(
large_prefix, rocksdb_large_prefix, PLUGIN_VAR_RQCMDARG,
"Support large index prefix length of 3072 bytes. If off, the maximum "
"index prefix length is 767.",
nullptr, nullptr, FALSE);
static const int ROCKSDB_ASSUMED_KEY_VALUE_DISK_SIZE = 100;
static struct st_mysql_sys_var *rocksdb_system_variables[] = {
MYSQL_SYSVAR(lock_wait_timeout),
MYSQL_SYSVAR(deadlock_detect),
MYSQL_SYSVAR(deadlock_detect_depth),
MYSQL_SYSVAR(max_row_locks),
MYSQL_SYSVAR(write_batch_max_bytes),
MYSQL_SYSVAR(lock_scanned_rows),
MYSQL_SYSVAR(bulk_load),
MYSQL_SYSVAR(bulk_load_allow_unsorted),
MYSQL_SYSVAR(skip_unique_check_tables),
MYSQL_SYSVAR(trace_sst_api),
MYSQL_SYSVAR(commit_in_the_middle),
MYSQL_SYSVAR(blind_delete_primary_key),
MYSQL_SYSVAR(read_free_rpl_tables),
MYSQL_SYSVAR(bulk_load_size),
MYSQL_SYSVAR(merge_buf_size),
MYSQL_SYSVAR(enable_bulk_load_api),
MYSQL_SYSVAR(tmpdir),
MYSQL_SYSVAR(merge_combine_read_size),
MYSQL_SYSVAR(merge_tmp_file_removal_delay_ms),
MYSQL_SYSVAR(skip_bloom_filter_on_read),
MYSQL_SYSVAR(create_if_missing),
MYSQL_SYSVAR(concurrent_prepare),
MYSQL_SYSVAR(manual_wal_flush),
MYSQL_SYSVAR(create_missing_column_families),
MYSQL_SYSVAR(error_if_exists),
MYSQL_SYSVAR(paranoid_checks),
MYSQL_SYSVAR(rate_limiter_bytes_per_sec),
MYSQL_SYSVAR(sst_mgr_rate_bytes_per_sec),
MYSQL_SYSVAR(delayed_write_rate),
MYSQL_SYSVAR(max_latest_deadlocks),
MYSQL_SYSVAR(info_log_level),
MYSQL_SYSVAR(max_open_files),
MYSQL_SYSVAR(max_total_wal_size),
MYSQL_SYSVAR(use_fsync),
MYSQL_SYSVAR(wal_dir),
MYSQL_SYSVAR(persistent_cache_path),
MYSQL_SYSVAR(persistent_cache_size_mb),
MYSQL_SYSVAR(delete_obsolete_files_period_micros),
MYSQL_SYSVAR(max_background_jobs),
MYSQL_SYSVAR(max_log_file_size),
MYSQL_SYSVAR(max_subcompactions),
MYSQL_SYSVAR(log_file_time_to_roll),
MYSQL_SYSVAR(keep_log_file_num),
MYSQL_SYSVAR(max_manifest_file_size),
MYSQL_SYSVAR(table_cache_numshardbits),
MYSQL_SYSVAR(wal_ttl_seconds),
MYSQL_SYSVAR(wal_size_limit_mb),
MYSQL_SYSVAR(manifest_preallocation_size),
MYSQL_SYSVAR(use_direct_reads),
MYSQL_SYSVAR(use_direct_io_for_flush_and_compaction),
MYSQL_SYSVAR(allow_mmap_reads),
MYSQL_SYSVAR(allow_mmap_writes),
MYSQL_SYSVAR(is_fd_close_on_exec),
MYSQL_SYSVAR(stats_dump_period_sec),
MYSQL_SYSVAR(advise_random_on_open),
MYSQL_SYSVAR(db_write_buffer_size),
MYSQL_SYSVAR(use_adaptive_mutex),
MYSQL_SYSVAR(bytes_per_sync),
MYSQL_SYSVAR(wal_bytes_per_sync),
MYSQL_SYSVAR(enable_thread_tracking),
MYSQL_SYSVAR(perf_context_level),
MYSQL_SYSVAR(wal_recovery_mode),
MYSQL_SYSVAR(access_hint_on_compaction_start),
MYSQL_SYSVAR(new_table_reader_for_compaction_inputs),
MYSQL_SYSVAR(compaction_readahead_size),
MYSQL_SYSVAR(allow_concurrent_memtable_write),
MYSQL_SYSVAR(enable_write_thread_adaptive_yield),
MYSQL_SYSVAR(block_cache_size),
MYSQL_SYSVAR(sim_cache_size),
MYSQL_SYSVAR(use_clock_cache),
MYSQL_SYSVAR(cache_index_and_filter_blocks),
MYSQL_SYSVAR(pin_l0_filter_and_index_blocks_in_cache),
MYSQL_SYSVAR(index_type),
MYSQL_SYSVAR(hash_index_allow_collision),
MYSQL_SYSVAR(no_block_cache),
MYSQL_SYSVAR(block_size),
MYSQL_SYSVAR(block_size_deviation),
MYSQL_SYSVAR(block_restart_interval),
MYSQL_SYSVAR(whole_key_filtering),
MYSQL_SYSVAR(default_cf_options),
MYSQL_SYSVAR(override_cf_options),
MYSQL_SYSVAR(update_cf_options),
MYSQL_SYSVAR(flush_log_at_trx_commit),
MYSQL_SYSVAR(write_disable_wal),
MYSQL_SYSVAR(write_ignore_missing_column_families),
MYSQL_SYSVAR(skip_fill_cache),
MYSQL_SYSVAR(unsafe_for_binlog),
MYSQL_SYSVAR(records_in_range),
MYSQL_SYSVAR(force_index_records_in_range),
MYSQL_SYSVAR(debug_optimizer_n_rows),
MYSQL_SYSVAR(force_compute_memtable_stats),
MYSQL_SYSVAR(force_compute_memtable_stats_cachetime),
MYSQL_SYSVAR(debug_optimizer_no_zero_cardinality),
MYSQL_SYSVAR(compact_cf),
MYSQL_SYSVAR(signal_drop_index_thread),
MYSQL_SYSVAR(pause_background_work),
MYSQL_SYSVAR(enable_2pc),
MYSQL_SYSVAR(strict_collation_check),
MYSQL_SYSVAR(strict_collation_exceptions),
MYSQL_SYSVAR(collect_sst_properties),
MYSQL_SYSVAR(force_flush_memtable_now),
MYSQL_SYSVAR(force_flush_memtable_and_lzero_now),
MYSQL_SYSVAR(enable_ttl),
MYSQL_SYSVAR(enable_ttl_read_filtering),
MYSQL_SYSVAR(debug_ttl_rec_ts),
MYSQL_SYSVAR(debug_ttl_snapshot_ts),
MYSQL_SYSVAR(debug_ttl_read_filter_ts),
MYSQL_SYSVAR(debug_ttl_ignore_pk),
MYSQL_SYSVAR(reset_stats),
MYSQL_SYSVAR(io_write_timeout),
MYSQL_SYSVAR(flush_memtable_on_analyze),
MYSQL_SYSVAR(seconds_between_stat_computes),
MYSQL_SYSVAR(compaction_sequential_deletes),
MYSQL_SYSVAR(compaction_sequential_deletes_window),
MYSQL_SYSVAR(compaction_sequential_deletes_file_size),
MYSQL_SYSVAR(compaction_sequential_deletes_count_sd),
MYSQL_SYSVAR(print_snapshot_conflict_queries),
MYSQL_SYSVAR(datadir),
MYSQL_SYSVAR(supported_compression_types),
MYSQL_SYSVAR(create_checkpoint),
MYSQL_SYSVAR(checksums_pct),
MYSQL_SYSVAR(store_row_debug_checksums),
MYSQL_SYSVAR(verify_row_debug_checksums),
MYSQL_SYSVAR(master_skip_tx_api),
MYSQL_SYSVAR(validate_tables),
MYSQL_SYSVAR(table_stats_sampling_pct),
MYSQL_SYSVAR(large_prefix),
nullptr};
static rocksdb::WriteOptions
rdb_get_rocksdb_write_options(my_core::THD *const thd) {
rocksdb::WriteOptions opt;
opt.sync = (rocksdb_flush_log_at_trx_commit == FLUSH_LOG_SYNC);
opt.disableWAL = THDVAR(thd, write_disable_wal);
opt.ignore_missing_column_families =
THDVAR(thd, write_ignore_missing_column_families);
return opt;
}
///////////////////////////////////////////////////////////////////////////////////////////
/**
@brief
Function we use in the creation of our hash to get key.
*/
uchar *
Rdb_open_tables_map::get_hash_key(const Rdb_table_handler *const table_handler,
size_t *const length,
my_bool not_used MY_ATTRIBUTE((__unused__))) {
*length = table_handler->m_table_name_length;
return reinterpret_cast<uchar *>(table_handler->m_table_name);
}
/*
Drop index thread's control
*/
static Rdb_drop_index_thread rdb_drop_idx_thread;
static void rocksdb_drop_index_wakeup_thread(
my_core::THD *const thd MY_ATTRIBUTE((__unused__)),
struct st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr MY_ATTRIBUTE((__unused__)), const void *const save) {
if (*static_cast<const bool *>(save)) {
rdb_drop_idx_thread.signal();
}
}
static inline uint32_t rocksdb_perf_context_level(THD *const thd) {
DBUG_ASSERT(thd != nullptr);
const int session_perf_context_level = THDVAR(thd, perf_context_level);
if (session_perf_context_level > rocksdb::PerfLevel::kUninitialized) {
return session_perf_context_level;
}
/*
Fallback to global thdvar, if session specific one was not set to a valid
value.
*/
const int global_perf_context_level = THDVAR(nullptr, perf_context_level);
if (global_perf_context_level > rocksdb::PerfLevel::kUninitialized) {
return global_perf_context_level;
}
return rocksdb::PerfLevel::kDisable;
}
/*
Very short (functor-like) interface to be passed to
Rdb_transaction::walk_tx_list()
*/
interface Rdb_tx_list_walker {
virtual ~Rdb_tx_list_walker() {}
virtual void process_tran(const Rdb_transaction *const) = 0;
};
/*
This is a helper class that is passed to RocksDB to get notifications when
a snapshot gets created.
*/
class Rdb_snapshot_notifier : public rocksdb::TransactionNotifier {
Rdb_transaction *m_owning_tx;
void SnapshotCreated(const rocksdb::Snapshot *snapshot) override;
public:
Rdb_snapshot_notifier(const Rdb_snapshot_notifier &) = delete;
Rdb_snapshot_notifier &operator=(const Rdb_snapshot_notifier &) = delete;
explicit Rdb_snapshot_notifier(Rdb_transaction *const owning_tx)
: m_owning_tx(owning_tx) {}
// If the owning Rdb_transaction gets destructed we need to not reference
// it anymore.
void detach() { m_owning_tx = nullptr; }
};
#ifdef MARIAROCKS_NOT_YET
// ER_LOCK_WAIT_TIMEOUT error also has a reason in facebook/mysql-5.6
#endif
String timeout_message(const char *command, const char *name1,
const char *name2)
{
String msg;
msg.append("Timeout on ");
msg.append(command);
msg.append(": ");
msg.append(name1);
if (name2 && name2[0])
{
msg.append(".");
msg.append(name2);
}
return msg;
}
/* This is the base class for transactions when interacting with rocksdb.
*/
class Rdb_transaction {
protected:
ulonglong m_write_count = 0;
ulonglong m_insert_count = 0;
ulonglong m_update_count = 0;
ulonglong m_delete_count = 0;
ulonglong m_lock_count = 0;
bool m_is_delayed_snapshot = false;
bool m_is_two_phase = false;
THD *m_thd = nullptr;
rocksdb::ReadOptions m_read_opts;
static std::multiset<Rdb_transaction *> s_tx_list;
static mysql_mutex_t s_tx_list_mutex;
Rdb_io_perf *m_tbl_io_perf;
bool m_tx_read_only = false;
int m_timeout_sec; /* Cached value of @@rocksdb_lock_wait_timeout */
/* Maximum number of locks the transaction can have */
ulonglong m_max_row_locks;
bool m_is_tx_failed = false;
bool m_rollback_only = false;
std::shared_ptr<Rdb_snapshot_notifier> m_notifier;
// This should be used only when updating binlog information.
virtual rocksdb::WriteBatchBase *get_write_batch() = 0;
virtual bool commit_no_binlog() = 0;
virtual rocksdb::Iterator *
get_iterator(const rocksdb::ReadOptions &options,
rocksdb::ColumnFamilyHandle *column_family) = 0;
public:
const char *m_mysql_log_file_name;
my_off_t m_mysql_log_offset;
#ifdef MARIAROCKS_NOT_YET
// TODO: MariaDB probably doesn't need these at all:
const char *m_mysql_gtid;
const char *m_mysql_max_gtid;
#endif
String m_detailed_error;
int64_t m_snapshot_timestamp = 0;
bool m_ddl_transaction;
/*
Tracks the number of tables in use through external_lock.
This should not be reset during start_tx().
*/
int64_t m_n_mysql_tables_in_use = 0;
/*
MariaDB's group commit:
*/
bool commit_ordered_done;
bool commit_ordered_res;
/*
for distinction between rdb_transaction_impl and rdb_writebatch_impl
when using walk tx list
*/
virtual bool is_writebatch_trx() const = 0;
static void init_mutex() {
mysql_mutex_init(key_mutex_tx_list, &s_tx_list_mutex, MY_MUTEX_INIT_FAST);
}
static void term_mutex() {
DBUG_ASSERT(s_tx_list.size() == 0);
mysql_mutex_destroy(&s_tx_list_mutex);
}
static void walk_tx_list(Rdb_tx_list_walker *walker) {
DBUG_ASSERT(walker != nullptr);
RDB_MUTEX_LOCK_CHECK(s_tx_list_mutex);
for (auto it : s_tx_list)
walker->process_tran(it);
RDB_MUTEX_UNLOCK_CHECK(s_tx_list_mutex);
}
int set_status_error(THD *const thd, const rocksdb::Status &s,
const Rdb_key_def &kd, Rdb_tbl_def *const tbl_def,
Rdb_table_handler *const table_handler) {
DBUG_ASSERT(!s.ok());
DBUG_ASSERT(tbl_def != nullptr);
if (s.IsTimedOut()) {
/*
SQL layer has weird expectations. If we return an error when
doing a read in DELETE IGNORE, it will ignore the error ("because it's
an IGNORE command!) but then will fail an assert, because "error code
was returned, but no error happened". Do what InnoDB's
convert_error_code_to_mysql() does: force a statement
rollback before returning HA_ERR_LOCK_WAIT_TIMEOUT:
*/
my_core::thd_mark_transaction_to_rollback(thd, false /*just statement*/);
m_detailed_error.copy(timeout_message(
"index", tbl_def->full_tablename().c_str(), kd.get_name().c_str()));
table_handler->m_lock_wait_timeout_counter.inc();
return HA_ERR_LOCK_WAIT_TIMEOUT;
}
if (s.IsDeadlock()) {
my_core::thd_mark_transaction_to_rollback(thd,
false /* just statement */);
m_detailed_error = String();
table_handler->m_deadlock_counter.inc();
return HA_ERR_LOCK_DEADLOCK;
} else if (s.IsBusy()) {
rocksdb_snapshot_conflict_errors++;
if (rocksdb_print_snapshot_conflict_queries) {
char user_host_buff[MAX_USER_HOST_SIZE + 1];
make_user_name(thd, user_host_buff);
// NO_LINT_DEBUG
sql_print_warning("Got snapshot conflict errors: User: %s "
"Query: %s",
user_host_buff, thd->query());
}
m_detailed_error = String(" (snapshot conflict)", system_charset_info);
table_handler->m_deadlock_counter.inc();
return HA_ERR_LOCK_DEADLOCK;
}
if (s.IsIOError() || s.IsCorruption()) {
rdb_handle_io_error(s, RDB_IO_ERROR_GENERAL);
}
return ha_rocksdb::rdb_error_to_mysql(s);
}
THD *get_thd() const { return m_thd; }
/* Used for tracking io_perf counters */
void io_perf_start(Rdb_io_perf *const io_perf) {
/*
Since perf_context is tracked per thread, it is difficult and expensive
to maintain perf_context on a per table basis. Therefore, roll all
perf_context data into the first table used in a query. This works well
for single table queries and is probably good enough for queries that hit
multiple tables.
perf_context stats gathering is started when the table lock is acquired
or when ha_rocksdb::start_stmt is called in case of LOCK TABLES. They
are recorded when the table lock is released, or when commit/rollback
is called on the transaction, whichever comes first. Table lock release
and commit/rollback can happen in different orders. In the case where
the lock is released before commit/rollback is called, an extra step to
gather stats during commit/rollback is needed.
*/
if (m_tbl_io_perf == nullptr &&
io_perf->start(rocksdb_perf_context_level(m_thd))) {
m_tbl_io_perf = io_perf;
}
}
void io_perf_end_and_record(void) {
if (m_tbl_io_perf != nullptr) {
m_tbl_io_perf->end_and_record(rocksdb_perf_context_level(m_thd));
m_tbl_io_perf = nullptr;
}
}
void io_perf_end_and_record(Rdb_io_perf *const io_perf) {
if (m_tbl_io_perf == io_perf) {
io_perf_end_and_record();
}
}
void update_bytes_written(ulonglong bytes_written) {
if (m_tbl_io_perf != nullptr) {
m_tbl_io_perf->update_bytes_written(rocksdb_perf_context_level(m_thd),
bytes_written);
}
}
void set_params(int timeout_sec_arg, int max_row_locks_arg) {
m_timeout_sec = timeout_sec_arg;
m_max_row_locks = max_row_locks_arg;
set_lock_timeout(timeout_sec_arg);
}
virtual void set_lock_timeout(int timeout_sec_arg) = 0;
ulonglong get_write_count() const { return m_write_count; }
ulonglong get_insert_count() const { return m_insert_count; }
ulonglong get_update_count() const { return m_update_count; }
ulonglong get_delete_count() const { return m_delete_count; }
void incr_insert_count() { ++m_insert_count; }
void incr_update_count() { ++m_update_count; }
void incr_delete_count() { ++m_delete_count; }
int get_timeout_sec() const { return m_timeout_sec; }
ulonglong get_lock_count() const { return m_lock_count; }
virtual void set_sync(bool sync) = 0;
virtual void release_lock(rocksdb::ColumnFamilyHandle *const column_family,
const std::string &rowkey) = 0;
virtual bool prepare(const rocksdb::TransactionName &name) = 0;
bool commit_or_rollback() {
bool res;
if (m_is_tx_failed) {
rollback();
res = false;
} else
res = commit();
return res;
}
bool commit() {
if (get_write_count() == 0) {
rollback();
return false;
} else if (m_rollback_only) {
/*
Transactions marked as rollback_only are expected to be rolled back at
prepare(). But there are some exceptions like below that prepare() is
never called and commit() is called instead.
1. Binlog is disabled
2. No modification exists in binlog cache for the transaction (#195)
In both cases, rolling back transaction is safe. Nothing is written to
binlog.
*/
my_error(ER_ROLLBACK_ONLY, MYF(0));
rollback();
return true;
} else {
mysql_bin_log_commit_pos(m_thd, &m_mysql_log_offset,
&m_mysql_log_file_name);
binlog_manager.update(m_mysql_log_file_name, m_mysql_log_offset,
get_write_batch());
return commit_no_binlog();
}
}
virtual void rollback() = 0;
void snapshot_created(const rocksdb::Snapshot *const snapshot) {
DBUG_ASSERT(snapshot != nullptr);
m_read_opts.snapshot = snapshot;
rdb->GetEnv()->GetCurrentTime(&m_snapshot_timestamp);
m_is_delayed_snapshot = false;
}
virtual void acquire_snapshot(bool acquire_now) = 0;
virtual void release_snapshot() = 0;
bool has_snapshot() const { return m_read_opts.snapshot != nullptr; }
private:
// The tables we are currently loading. In a partitioned table this can
// have more than one entry
std::vector<ha_rocksdb *> m_curr_bulk_load;
public:
int finish_bulk_load() {
int rc = 0;
std::vector<ha_rocksdb *>::iterator it;
while ((it = m_curr_bulk_load.begin()) != m_curr_bulk_load.end()) {
int rc2 = (*it)->finalize_bulk_load();
if (rc2 != 0 && rc == 0) {
rc = rc2;
}
}
DBUG_ASSERT(m_curr_bulk_load.size() == 0);
return rc;
}
void start_bulk_load(ha_rocksdb *const bulk_load) {
/*
If we already have an open bulk load of a table and the name doesn't
match the current one, close out the currently running one. This allows
multiple bulk loads to occur on a partitioned table, but then closes
them all out when we switch to another table.
*/
DBUG_ASSERT(bulk_load != nullptr);
if (!m_curr_bulk_load.empty() &&
!bulk_load->same_table(*m_curr_bulk_load[0])) {
const auto res = finish_bulk_load();
SHIP_ASSERT(res == 0);
}
m_curr_bulk_load.push_back(bulk_load);
}
void end_bulk_load(ha_rocksdb *const bulk_load) {
for (auto it = m_curr_bulk_load.begin(); it != m_curr_bulk_load.end();
it++) {
if (*it == bulk_load) {
m_curr_bulk_load.erase(it);
return;
}
}
// Should not reach here
SHIP_ASSERT(0);
}
int num_ongoing_bulk_load() const { return m_curr_bulk_load.size(); }
/*
Flush the data accumulated so far. This assumes we're doing a bulk insert.
@detail
This should work like transaction commit, except that we don't
synchronize with the binlog (there is no API that would allow to have
binlog flush the changes accumulated so far and return its current
position)
@todo
Add test coverage for what happens when somebody attempts to do bulk
inserts while inside a multi-statement transaction.
*/
bool flush_batch() {
if (get_write_count() == 0)
return false;
/* Commit the current transaction */
if (commit_no_binlog())
return true;
/* Start another one */
start_tx();
return false;
}
virtual rocksdb::Status put(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key,
const rocksdb::Slice &value) = 0;
virtual rocksdb::Status
delete_key(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key) = 0;
virtual rocksdb::Status
single_delete(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key) = 0;
virtual bool has_modifications() const = 0;
virtual rocksdb::WriteBatchBase *get_indexed_write_batch() = 0;
/*
Return a WriteBatch that one can write to. The writes will skip any
transaction locking. The writes will NOT be visible to the transaction.
*/
rocksdb::WriteBatchBase *get_blind_write_batch() {
return get_indexed_write_batch()->GetWriteBatch();
}
virtual rocksdb::Status get(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key,
std::string *value) const = 0;
virtual rocksdb::Status
get_for_update(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key, std::string *const value,
bool exclusive) = 0;
rocksdb::Iterator *
get_iterator(rocksdb::ColumnFamilyHandle *const column_family,
bool skip_bloom_filter, bool fill_cache,
bool read_current = false, bool create_snapshot = true) {
// Make sure we are not doing both read_current (which implies we don't
// want a snapshot) and create_snapshot which makes sure we create
// a snapshot
DBUG_ASSERT(column_family != nullptr);
DBUG_ASSERT(!read_current || !create_snapshot);
if (create_snapshot)
acquire_snapshot(true);
rocksdb::ReadOptions options = m_read_opts;
if (skip_bloom_filter) {
options.total_order_seek = true;
} else {
// With this option, Iterator::Valid() returns false if key
// is outside of the prefix bloom filter range set at Seek().
// Must not be set to true if not using bloom filter.
options.prefix_same_as_start = true;
}
options.fill_cache = fill_cache;
if (read_current) {
options.snapshot = nullptr;
}
return get_iterator(options, column_family);
}
virtual bool is_tx_started() const = 0;
virtual void start_tx() = 0;
virtual void start_stmt() = 0;
virtual void rollback_stmt() = 0;
void set_tx_failed(bool failed_arg) { m_is_tx_failed = failed_arg; }
bool can_prepare() const {
if (m_rollback_only) {
my_error(ER_ROLLBACK_ONLY, MYF(0));
return false;
}
return true;
}
int rollback_to_savepoint(void *const savepoint) {
if (has_modifications()) {
my_error(ER_ROLLBACK_TO_SAVEPOINT, MYF(0));
m_rollback_only = true;
return HA_EXIT_FAILURE;
}
return HA_EXIT_SUCCESS;
}
/*
This is used by transactions started with "START TRANSACTION WITH "
"CONSISTENT [ROCKSDB] SNAPSHOT". When tx_read_only is turned on,
snapshot has to be created via DB::GetSnapshot(), not via Transaction
API.
*/
bool is_tx_read_only() const { return m_tx_read_only; }
bool is_two_phase() const { return m_is_two_phase; }
void set_tx_read_only(bool val) { m_tx_read_only = val; }
explicit Rdb_transaction(THD *const thd)
: m_thd(thd), m_tbl_io_perf(nullptr) {
RDB_MUTEX_LOCK_CHECK(s_tx_list_mutex);
s_tx_list.insert(this);
RDB_MUTEX_UNLOCK_CHECK(s_tx_list_mutex);
}
virtual ~Rdb_transaction() {
RDB_MUTEX_LOCK_CHECK(s_tx_list_mutex);
s_tx_list.erase(this);
RDB_MUTEX_UNLOCK_CHECK(s_tx_list_mutex);
}
};
/*
This is a rocksdb transaction. Its members represent the current transaction,
which consists of:
- the snapshot
- the changes we've made but are not seeing yet.
The changes are made to individual tables, which store them here and then
this object commits them on commit.
*/
class Rdb_transaction_impl : public Rdb_transaction {
rocksdb::Transaction *m_rocksdb_tx = nullptr;
rocksdb::Transaction *m_rocksdb_reuse_tx = nullptr;
public:
void set_lock_timeout(int timeout_sec_arg) override {
if (m_rocksdb_tx)
m_rocksdb_tx->SetLockTimeout(rdb_convert_sec_to_ms(m_timeout_sec));
}
void set_sync(bool sync) override {
m_rocksdb_tx->GetWriteOptions()->sync = sync;
}
void release_lock(rocksdb::ColumnFamilyHandle *const column_family,
const std::string &rowkey) override {
if (!THDVAR(m_thd, lock_scanned_rows)) {
m_rocksdb_tx->UndoGetForUpdate(column_family, rocksdb::Slice(rowkey));
}
}
virtual bool is_writebatch_trx() const override { return false; }
private:
void release_tx(void) {
// We are done with the current active transaction object. Preserve it
// for later reuse.
DBUG_ASSERT(m_rocksdb_reuse_tx == nullptr);
m_rocksdb_reuse_tx = m_rocksdb_tx;
m_rocksdb_tx = nullptr;
}
bool prepare(const rocksdb::TransactionName &name) override {
rocksdb::Status s;
s = m_rocksdb_tx->SetName(name);
if (!s.ok()) {
rdb_handle_io_error(s, RDB_IO_ERROR_TX_COMMIT);
return false;
}
s = m_rocksdb_tx->Prepare();
if (!s.ok()) {
rdb_handle_io_error(s, RDB_IO_ERROR_TX_COMMIT);
return false;
}
return true;
}
bool commit_no_binlog() override {
bool res = false;
release_snapshot();
const rocksdb::Status s = m_rocksdb_tx->Commit();
if (!s.ok()) {
rdb_handle_io_error(s, RDB_IO_ERROR_TX_COMMIT);
res = true;
}
/* Save the transaction object to be reused */
release_tx();
m_write_count = 0;
m_insert_count = 0;
m_update_count = 0;
m_delete_count = 0;
m_lock_count = 0;
set_tx_read_only(false);
m_rollback_only = false;
return res;
}
public:
void rollback() override {
m_write_count = 0;
m_insert_count = 0;
m_update_count = 0;
m_delete_count = 0;
m_lock_count = 0;
m_ddl_transaction = false;
if (m_rocksdb_tx) {
release_snapshot();
/* This will also release all of the locks: */
m_rocksdb_tx->Rollback();
/* Save the transaction object to be reused */
release_tx();
set_tx_read_only(false);
m_rollback_only = false;
}
}
void acquire_snapshot(bool acquire_now) override {
if (m_read_opts.snapshot == nullptr) {
if (is_tx_read_only()) {
snapshot_created(rdb->GetSnapshot());
} else if (acquire_now) {
m_rocksdb_tx->SetSnapshot();
snapshot_created(m_rocksdb_tx->GetSnapshot());
} else if (!m_is_delayed_snapshot) {
m_rocksdb_tx->SetSnapshotOnNextOperation(m_notifier);
m_is_delayed_snapshot = true;
}
}
}
void release_snapshot() override {
bool need_clear = m_is_delayed_snapshot;
if (m_read_opts.snapshot != nullptr) {
m_snapshot_timestamp = 0;
if (is_tx_read_only()) {
rdb->ReleaseSnapshot(m_read_opts.snapshot);
need_clear = false;
} else {
need_clear = true;
}
m_read_opts.snapshot = nullptr;
}
if (need_clear && m_rocksdb_tx != nullptr)
m_rocksdb_tx->ClearSnapshot();
}
bool has_snapshot() { return m_read_opts.snapshot != nullptr; }
rocksdb::Status put(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key,
const rocksdb::Slice &value) override {
++m_write_count;
++m_lock_count;
if (m_write_count > m_max_row_locks || m_lock_count > m_max_row_locks)
return rocksdb::Status::Aborted(rocksdb::Status::kLockLimit);
return m_rocksdb_tx->Put(column_family, key, value);
}
rocksdb::Status delete_key(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key) override {
++m_write_count;
++m_lock_count;
if (m_write_count > m_max_row_locks || m_lock_count > m_max_row_locks)
return rocksdb::Status::Aborted(rocksdb::Status::kLockLimit);
return m_rocksdb_tx->Delete(column_family, key);
}
rocksdb::Status
single_delete(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key) override {
++m_write_count;
++m_lock_count;
if (m_write_count > m_max_row_locks || m_lock_count > m_max_row_locks)
return rocksdb::Status::Aborted(rocksdb::Status::kLockLimit);
return m_rocksdb_tx->SingleDelete(column_family, key);
}
bool has_modifications() const override {
return m_rocksdb_tx->GetWriteBatch() &&
m_rocksdb_tx->GetWriteBatch()->GetWriteBatch() &&
m_rocksdb_tx->GetWriteBatch()->GetWriteBatch()->Count() > 0;
}
rocksdb::WriteBatchBase *get_write_batch() override {
if (is_two_phase()) {
return m_rocksdb_tx->GetCommitTimeWriteBatch();
}
return m_rocksdb_tx->GetWriteBatch()->GetWriteBatch();
}
/*
Return a WriteBatch that one can write to. The writes will skip any
transaction locking. The writes WILL be visible to the transaction.
*/
rocksdb::WriteBatchBase *get_indexed_write_batch() override {
++m_write_count;
return m_rocksdb_tx->GetWriteBatch();
}
rocksdb::Status get(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key,
std::string *value) const override {
global_stats.queries[QUERIES_POINT].inc();
return m_rocksdb_tx->Get(m_read_opts, column_family, key, value);
}
rocksdb::Status
get_for_update(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key, std::string *const value,
bool exclusive) override {
if (++m_lock_count > m_max_row_locks)
return rocksdb::Status::Aborted(rocksdb::Status::kLockLimit);
return m_rocksdb_tx->GetForUpdate(m_read_opts, column_family, key, value,
exclusive);
}
rocksdb::Iterator *
get_iterator(const rocksdb::ReadOptions &options,
rocksdb::ColumnFamilyHandle *const column_family) override {
global_stats.queries[QUERIES_RANGE].inc();
return m_rocksdb_tx->GetIterator(options, column_family);
}
const rocksdb::Transaction *get_rdb_trx() const { return m_rocksdb_tx; }
bool is_tx_started() const override { return (m_rocksdb_tx != nullptr); }
void start_tx() override {
rocksdb::TransactionOptions tx_opts;
rocksdb::WriteOptions write_opts;
tx_opts.set_snapshot = false;
tx_opts.lock_timeout = rdb_convert_sec_to_ms(m_timeout_sec);
tx_opts.deadlock_detect = THDVAR(m_thd, deadlock_detect);
tx_opts.deadlock_detect_depth = THDVAR(m_thd, deadlock_detect_depth);
tx_opts.max_write_batch_size = THDVAR(m_thd, write_batch_max_bytes);
write_opts.sync = (rocksdb_flush_log_at_trx_commit == FLUSH_LOG_SYNC);
write_opts.disableWAL = THDVAR(m_thd, write_disable_wal);
write_opts.ignore_missing_column_families =
THDVAR(m_thd, write_ignore_missing_column_families);
m_is_two_phase = rocksdb_enable_2pc;
commit_ordered_done= false;
/*
If m_rocksdb_reuse_tx is null this will create a new transaction object.
Otherwise it will reuse the existing one.
*/
m_rocksdb_tx =
rdb->BeginTransaction(write_opts, tx_opts, m_rocksdb_reuse_tx);
m_rocksdb_reuse_tx = nullptr;
m_read_opts = rocksdb::ReadOptions();
m_ddl_transaction = false;
}
/*
Start a statement inside a multi-statement transaction.
@todo: are we sure this is called once (and not several times) per
statement start?
For hooking to start of statement that is its own transaction, see
ha_rocksdb::external_lock().
*/
void start_stmt() override {
// Set the snapshot to delayed acquisition (SetSnapshotOnNextOperation)
acquire_snapshot(false);
m_rocksdb_tx->SetSavePoint();
}
/*
This must be called when last statement is rolled back, but the transaction
continues
*/
void rollback_stmt() override {
/* TODO: here we must release the locks taken since the start_stmt() call */
if (m_rocksdb_tx) {
const rocksdb::Snapshot *const org_snapshot = m_rocksdb_tx->GetSnapshot();
m_rocksdb_tx->RollbackToSavePoint();
const rocksdb::Snapshot *const cur_snapshot = m_rocksdb_tx->GetSnapshot();
if (org_snapshot != cur_snapshot) {
if (org_snapshot != nullptr)
m_snapshot_timestamp = 0;
m_read_opts.snapshot = cur_snapshot;
if (cur_snapshot != nullptr)
rdb->GetEnv()->GetCurrentTime(&m_snapshot_timestamp);
else
m_is_delayed_snapshot = true;
}
}
}
explicit Rdb_transaction_impl(THD *const thd)
: Rdb_transaction(thd), m_rocksdb_tx(nullptr) {
// Create a notifier that can be called when a snapshot gets generated.
m_notifier = std::make_shared<Rdb_snapshot_notifier>(this);
}
virtual ~Rdb_transaction_impl() {
rollback();
// Theoretically the notifier could outlive the Rdb_transaction_impl
// (because of the shared_ptr), so let it know it can't reference
// the transaction anymore.
m_notifier->detach();
// Free any transaction memory that is still hanging around.
delete m_rocksdb_reuse_tx;
DBUG_ASSERT(m_rocksdb_tx == nullptr);
}
};
/* This is a rocksdb write batch. This class doesn't hold or wait on any
transaction locks (skips rocksdb transaction API) thus giving better
performance. The commit is done through rdb->GetBaseDB()->Commit().
Currently this is only used for replication threads which are guaranteed
to be non-conflicting. Any further usage of this class should completely
be thought thoroughly.
*/
class Rdb_writebatch_impl : public Rdb_transaction {
rocksdb::WriteBatchWithIndex *m_batch;
rocksdb::WriteOptions write_opts;
// Called after commit/rollback.
void reset() {
m_batch->Clear();
m_read_opts = rocksdb::ReadOptions();
m_ddl_transaction = false;
}
private:
bool prepare(const rocksdb::TransactionName &name) override { return true; }
bool commit_no_binlog() override {
bool res = false;
release_snapshot();
const rocksdb::Status s =
rdb->GetBaseDB()->Write(write_opts, m_batch->GetWriteBatch());
if (!s.ok()) {
rdb_handle_io_error(s, RDB_IO_ERROR_TX_COMMIT);
res = true;
}
reset();
m_write_count = 0;
m_insert_count = 0;
m_update_count = 0;
m_delete_count = 0;
set_tx_read_only(false);
m_rollback_only = false;
return res;
}
public:
bool is_writebatch_trx() const override { return true; }
void set_lock_timeout(int timeout_sec_arg) override {
// Nothing to do here.
}
void set_sync(bool sync) override { write_opts.sync = sync; }
void release_lock(rocksdb::ColumnFamilyHandle *const column_family,
const std::string &rowkey) override {
// Nothing to do here since we don't hold any row locks.
}
void rollback() override {
m_write_count = 0;
m_insert_count = 0;
m_update_count = 0;
m_delete_count = 0;
m_lock_count = 0;
release_snapshot();
reset();
set_tx_read_only(false);
m_rollback_only = false;
}
void acquire_snapshot(bool acquire_now) override {
if (m_read_opts.snapshot == nullptr)
snapshot_created(rdb->GetSnapshot());
}
void release_snapshot() override {
if (m_read_opts.snapshot != nullptr) {
rdb->ReleaseSnapshot(m_read_opts.snapshot);
m_read_opts.snapshot = nullptr;
}
}
rocksdb::Status put(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key,
const rocksdb::Slice &value) override {
++m_write_count;
m_batch->Put(column_family, key, value);
// Note Put/Delete in write batch doesn't return any error code. We simply
// return OK here.
return rocksdb::Status::OK();
}
rocksdb::Status delete_key(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key) override {
++m_write_count;
m_batch->Delete(column_family, key);
return rocksdb::Status::OK();
}
rocksdb::Status
single_delete(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key) override {
++m_write_count;
m_batch->SingleDelete(column_family, key);
return rocksdb::Status::OK();
}
bool has_modifications() const override {
return m_batch->GetWriteBatch()->Count() > 0;
}
rocksdb::WriteBatchBase *get_write_batch() override { return m_batch; }
rocksdb::WriteBatchBase *get_indexed_write_batch() override {
++m_write_count;
return m_batch;
}
rocksdb::Status get(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key,
std::string *const value) const override {
return m_batch->GetFromBatchAndDB(rdb, m_read_opts, column_family, key,
value);
}
rocksdb::Status
get_for_update(rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key, std::string *const value,
bool exclusive) override {
return get(column_family, key, value);
}
rocksdb::Iterator *
get_iterator(const rocksdb::ReadOptions &options,
rocksdb::ColumnFamilyHandle *const column_family) override {
const auto it = rdb->NewIterator(options);
return m_batch->NewIteratorWithBase(it);
}
bool is_tx_started() const override { return (m_batch != nullptr); }
void start_tx() override {
commit_ordered_done= false; // Do we need this here?
reset();
write_opts.sync = (rocksdb_flush_log_at_trx_commit == FLUSH_LOG_SYNC);
write_opts.disableWAL = THDVAR(m_thd, write_disable_wal);
write_opts.ignore_missing_column_families =
THDVAR(m_thd, write_ignore_missing_column_families);
}
void start_stmt() override { m_batch->SetSavePoint(); }
void rollback_stmt() override {
if (m_batch)
m_batch->RollbackToSavePoint();
}
explicit Rdb_writebatch_impl(THD *const thd)
: Rdb_transaction(thd), m_batch(nullptr) {
m_batch = new rocksdb::WriteBatchWithIndex(rocksdb::BytewiseComparator(), 0,
true);
}
virtual ~Rdb_writebatch_impl() {
rollback();
delete m_batch;
}
};
void Rdb_snapshot_notifier::SnapshotCreated(
const rocksdb::Snapshot *const snapshot) {
if (m_owning_tx != nullptr) {
m_owning_tx->snapshot_created(snapshot);
}
}
std::multiset<Rdb_transaction *> Rdb_transaction::s_tx_list;
mysql_mutex_t Rdb_transaction::s_tx_list_mutex;
static Rdb_transaction *&get_tx_from_thd(THD *const thd) {
return *reinterpret_cast<Rdb_transaction **>(
my_core::thd_ha_data(thd, rocksdb_hton));
}
namespace {
class Rdb_perf_context_guard {
Rdb_io_perf m_io_perf;
Rdb_io_perf *m_io_perf_ptr;
Rdb_transaction *m_tx;
uint m_level;
public:
Rdb_perf_context_guard(const Rdb_perf_context_guard &) = delete;
Rdb_perf_context_guard &operator=(const Rdb_perf_context_guard &) = delete;
explicit Rdb_perf_context_guard(Rdb_io_perf *io_perf, uint level)
: m_io_perf_ptr(io_perf), m_tx(nullptr), m_level(level) {
m_io_perf_ptr->start(m_level);
}
explicit Rdb_perf_context_guard(Rdb_transaction *tx, uint level)
: m_io_perf_ptr(nullptr), m_tx(tx), m_level(level) {
/*
if perf_context information is already being recorded, this becomes a
no-op
*/
if (tx != nullptr) {
tx->io_perf_start(&m_io_perf);
}
}
~Rdb_perf_context_guard() {
if (m_tx != nullptr) {
m_tx->io_perf_end_and_record();
} else if (m_io_perf_ptr != nullptr) {
m_io_perf_ptr->end_and_record(m_level);
}
}
};
} // anonymous namespace
/*
TODO: maybe, call this in external_lock() and store in ha_rocksdb..
*/
static Rdb_transaction *get_or_create_tx(THD *const thd) {
Rdb_transaction *&tx = get_tx_from_thd(thd);
// TODO: this is called too many times.. O(#rows)
if (tx == nullptr) {
bool rpl_skip_tx_api= false; // MARIAROCKS_NOT_YET.
if ((rpl_skip_tx_api && thd->rgi_slave) ||
false /* MARIAROCKS_NOT_YET: THDVAR(thd, master_skip_tx_api) && !thd->rgi_slave)*/)
{
tx = new Rdb_writebatch_impl(thd);
}
else
{
tx = new Rdb_transaction_impl(thd);
}
tx->set_params(THDVAR(thd, lock_wait_timeout), THDVAR(thd, max_row_locks));
tx->start_tx();
} else {
tx->set_params(THDVAR(thd, lock_wait_timeout), THDVAR(thd, max_row_locks));
if (!tx->is_tx_started()) {
tx->start_tx();
}
}
return tx;
}
static int rocksdb_close_connection(handlerton *const hton, THD *const thd) {
Rdb_transaction *&tx = get_tx_from_thd(thd);
if (tx != nullptr) {
int rc = tx->finish_bulk_load();
if (rc != 0) {
// NO_LINT_DEBUG
sql_print_error("RocksDB: Error %d finalizing last SST file while "
"disconnecting",
rc);
abort_with_stack_traces();
}
delete tx;
tx = nullptr;
}
return HA_EXIT_SUCCESS;
}
/*
* Serializes an xid to a string so that it can
* be used as a rocksdb transaction name
*/
static std::string rdb_xid_to_string(const XID &src) {
DBUG_ASSERT(src.gtrid_length >= 0 && src.gtrid_length <= MAXGTRIDSIZE);
DBUG_ASSERT(src.bqual_length >= 0 && src.bqual_length <= MAXBQUALSIZE);
std::string buf;
buf.reserve(RDB_XIDHDR_LEN + src.gtrid_length + src.bqual_length);
/*
* expand formatID to fill 8 bytes if it doesn't already
* then reinterpret bit pattern as unsigned and store in network order
*/
uchar fidbuf[RDB_FORMATID_SZ];
int64 signed_fid8 = src.formatID;
const uint64 raw_fid8 = *reinterpret_cast<uint64 *>(&signed_fid8);
rdb_netbuf_store_uint64(fidbuf, raw_fid8);
buf.append(reinterpret_cast<const char *>(fidbuf), RDB_FORMATID_SZ);
buf.push_back(src.gtrid_length);
buf.push_back(src.bqual_length);
buf.append(src.data, (src.gtrid_length) + (src.bqual_length));
return buf;
}
#if 0
// MARIAROCKS: MariaDB doesn't have flush_wal method
/**
Called by hton->flush_logs after MySQL group commit prepares a set of
transactions.
*/
static bool rocksdb_flush_wal(handlerton* hton __attribute__((__unused__)))
DBUG_ASSERT(rdb != nullptr);
rocksdb::Status s;
/*
target_lsn is set to 0 when MySQL wants to sync the wal files
*/
if (target_lsn == 0 || rocksdb_flush_log_at_trx_commit != FLUSH_LOG_NEVER) {
rocksdb_wal_group_syncs++;
s = rdb->FlushWAL(target_lsn == 0 ||
rocksdb_flush_log_at_trx_commit == FLUSH_LOG_SYNC);
}
if (!s.ok()) {
rdb_log_status_error(s);
return HA_EXIT_FAILURE;
}
return HA_EXIT_SUCCESS;
}
#endif
/**
For a slave, prepare() updates the slave_gtid_info table which tracks the
replication progress.
*/
static int rocksdb_prepare(handlerton* hton, THD* thd, bool prepare_tx)
{
bool async=false; // This is "ASYNC_COMMIT" feature which is only present in webscalesql
Rdb_transaction *&tx = get_tx_from_thd(thd);
if (!tx->can_prepare()) {
return HA_EXIT_FAILURE;
}
if (prepare_tx ||
(!my_core::thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))) {
/* We were instructed to prepare the whole transaction, or
this is an SQL statement end and autocommit is on */
#ifdef MARIAROCKS_NOT_YET // Crash-safe slave does not work yet
std::vector<st_slave_gtid_info> slave_gtid_info;
my_core::thd_slave_gtid_info(thd, &slave_gtid_info);
for (const auto &it : slave_gtid_info) {
rocksdb::WriteBatchBase *const write_batch = tx->get_blind_write_batch();
binlog_manager.update_slave_gtid_info(it.id, it.db, it.gtid, write_batch);
}
#endif
if (tx->is_two_phase()) {
/*
MariaDB: the following branch is never taken.
We always flush at Prepare and rely on RocksDB's internal Group Commit
to do some grouping.
*/
if (thd->durability_property == HA_IGNORE_DURABILITY || async) {
tx->set_sync(false);
}
/*
MariaDB: do not flush logs if we are running in a non-crash-safe mode.
*/
if (!rocksdb_flush_log_at_trx_commit)
tx->set_sync(false);
XID xid;
thd_get_xid(thd, reinterpret_cast<MYSQL_XID *>(&xid));
if (!tx->prepare(rdb_xid_to_string(xid))) {
return HA_EXIT_FAILURE;
}
/*
MariaDB: our Group Commit implementation does not use the
hton->flush_logs call (at least currently) so the following is not
needed (TODO: will we need this for binlog rotation?)
*/
#ifdef MARIAROCKS_NOT_YET
if (thd->durability_property == HA_IGNORE_DURABILITY )
(rocksdb_flush_log_at_trx_commit != FLUSH_LOG_NEVER))
&&
THDVAR(thd, flush_log_at_trx_commit))
#endif
#ifdef MARIAROCKS_NOT_YET
{
// MariaRocks: disable the
// "write/sync redo log before flushing binlog cache to file"
// feature. See a869c56d361bb44f46c0efeb11a8f03561676247
/**
we set the log sequence as '1' just to trigger hton->flush_logs
*/
thd_store_lsn(thd, 1, DB_TYPE_ROCKSDB);
}
#endif
}
DEBUG_SYNC(thd, "rocksdb.prepared");
}
return HA_EXIT_SUCCESS;
}
/**
do nothing for prepare/commit by xid
this is needed to avoid crashes in XA scenarios
*/
static int rocksdb_commit_by_xid(handlerton *const hton, XID *const xid) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(hton != nullptr);
DBUG_ASSERT(xid != nullptr);
DBUG_ASSERT(commit_latency_stats != nullptr);
rocksdb::StopWatchNano timer(rocksdb::Env::Default(), true);
const auto name = rdb_xid_to_string(*xid);
DBUG_ASSERT(!name.empty());
rocksdb::Transaction *const trx = rdb->GetTransactionByName(name);
if (trx == nullptr) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
const rocksdb::Status s = trx->Commit();
if (!s.ok()) {
rdb_log_status_error(s);
DBUG_RETURN(HA_EXIT_FAILURE);
}
delete trx;
// `Add()` is implemented in a thread-safe manner.
commit_latency_stats->Add(timer.ElapsedNanos() / 1000);
DBUG_RETURN(HA_EXIT_SUCCESS);
}
static int
rocksdb_rollback_by_xid(handlerton *const hton MY_ATTRIBUTE((__unused__)),
XID *const xid) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(hton != nullptr);
DBUG_ASSERT(xid != nullptr);
DBUG_ASSERT(rdb != nullptr);
const auto name = rdb_xid_to_string(*xid);
rocksdb::Transaction *const trx = rdb->GetTransactionByName(name);
if (trx == nullptr) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
const rocksdb::Status s = trx->Rollback();
if (!s.ok()) {
rdb_log_status_error(s);
DBUG_RETURN(HA_EXIT_FAILURE);
}
delete trx;
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/**
Rebuilds an XID from a serialized version stored in a string.
*/
static void rdb_xid_from_string(const std::string &src, XID *const dst) {
DBUG_ASSERT(dst != nullptr);
uint offset = 0;
uint64 raw_fid8 =
rdb_netbuf_to_uint64(reinterpret_cast<const uchar *>(src.data()));
const int64 signed_fid8 = *reinterpret_cast<int64 *>(&raw_fid8);
dst->formatID = signed_fid8;
offset += RDB_FORMATID_SZ;
dst->gtrid_length = src.at(offset);
offset += RDB_GTRID_SZ;
dst->bqual_length = src.at(offset);
offset += RDB_BQUAL_SZ;
DBUG_ASSERT(dst->gtrid_length >= 0 && dst->gtrid_length <= MAXGTRIDSIZE);
DBUG_ASSERT(dst->bqual_length >= 0 && dst->bqual_length <= MAXBQUALSIZE);
src.copy(dst->data, (dst->gtrid_length) + (dst->bqual_length),
RDB_XIDHDR_LEN);
}
/**
Reading last committed binary log info from RocksDB system row.
The info is needed for crash safe slave/master to work.
*/
static int rocksdb_recover(handlerton* hton, XID* xid_list, uint len)
#ifdef MARIAROCKS_NOT_YET
char* const binlog_file,
my_off_t *const binlog_pos,
Gtid *const binlog_max_gtid) {
#endif
{
#ifdef MARIAROCKS_NOT_YET
if (binlog_file && binlog_pos) {
char file_buf[FN_REFLEN + 1] = {0};
my_off_t pos;
char gtid_buf[FN_REFLEN + 1] = {0};
if (binlog_manager.read(file_buf, &pos, gtid_buf)) {
if (is_binlog_advanced(binlog_file, *binlog_pos, file_buf, pos)) {
memcpy(binlog_file, file_buf, FN_REFLEN + 1);
*binlog_pos = pos;
fprintf(stderr, "RocksDB: Last binlog file position %llu,"
" file name %s\n",
pos, file_buf);
if (*gtid_buf) {
global_sid_lock->rdlock();
binlog_max_gtid->parse(global_sid_map, gtid_buf);
global_sid_lock->unlock();
fprintf(stderr, "RocksDB: Last MySQL Gtid %s\n", gtid_buf);
}
}
}
}
#endif
if (len == 0 || xid_list == nullptr) {
return HA_EXIT_SUCCESS;
}
std::vector<rocksdb::Transaction *> trans_list;
rdb->GetAllPreparedTransactions(&trans_list);
uint count = 0;
for (auto &trans : trans_list) {
if (count >= len) {
break;
}
auto name = trans->GetName();
rdb_xid_from_string(name, &xid_list[count]);
count++;
}
return count;
}
/*
Handle a commit checkpoint request from server layer.
InnoDB does this:
We put the request in a queue, so that we can notify upper layer about
checkpoint complete when we have flushed the redo log.
If we have already flushed all relevant redo log, we notify immediately.
MariaRocks just flushes everything right away ATM
*/
static void rocksdb_checkpoint_request(handlerton *hton,
void *cookie)
{
const rocksdb::Status s= rdb->SyncWAL();
//TODO: what to do on error?
if (s.ok())
{
rocksdb_wal_group_syncs++;
commit_checkpoint_notify_ha(hton, cookie);
}
}
/*
@param all: TRUE - commit the transaction
FALSE - SQL statement ended
*/
static void rocksdb_commit_ordered(handlerton *hton, THD* thd, bool all)
{
// Same assert as InnoDB has
DBUG_ASSERT(all || (!thd_test_options(thd, OPTION_NOT_AUTOCOMMIT |
OPTION_BEGIN)));
Rdb_transaction *&tx = get_tx_from_thd(thd);
if (!tx->is_two_phase()) {
/*
ordered_commit is supposedly slower as it is done sequentially
in order to preserve commit order.
if we are not required do 2-phase commit with the binlog, do not do
anything here.
*/
return;
}
tx->set_sync(false);
/* This will note the master position also */
tx->commit_ordered_res= tx->commit();
tx->commit_ordered_done= true;
}
static int rocksdb_commit(handlerton* hton, THD* thd, bool commit_tx)
{
DBUG_ENTER_FUNC();
DBUG_ASSERT(hton != nullptr);
DBUG_ASSERT(thd != nullptr);
DBUG_ASSERT(commit_latency_stats != nullptr);
rocksdb::StopWatchNano timer(rocksdb::Env::Default(), true);
/* note: h->external_lock(F_UNLCK) is called after this function is called) */
Rdb_transaction *&tx = get_tx_from_thd(thd);
/* this will trigger saving of perf_context information */
Rdb_perf_context_guard guard(tx, rocksdb_perf_context_level(thd));
if (tx != nullptr) {
if (commit_tx || (!my_core::thd_test_options(thd, OPTION_NOT_AUTOCOMMIT |
OPTION_BEGIN))) {
/*
This will not add anything to commit_latency_stats, and this is correct
right?
*/
if (tx->commit_ordered_done)
{
thd_wakeup_subsequent_commits(thd, 0);
DBUG_RETURN((tx->commit_ordered_res? HA_ERR_INTERNAL_ERROR: 0));
}
/*
We get here
- For a COMMIT statement that finishes a multi-statement transaction
- For a statement that has its own transaction
*/
if (tx->commit()) {
DBUG_RETURN(HA_ERR_ROCKSDB_COMMIT_FAILED);
}
thd_wakeup_subsequent_commits(thd, 0);
} else {
/*
We get here when committing a statement within a transaction.
We don't need to do anything here. tx->start_stmt() will notify
Rdb_transaction_impl that another statement has started.
*/
tx->set_tx_failed(false);
}
if (my_core::thd_tx_isolation(thd) <= ISO_READ_COMMITTED) {
// For READ_COMMITTED, we release any existing snapshot so that we will
// see any changes that occurred since the last statement.
tx->release_snapshot();
}
}
// `Add()` is implemented in a thread-safe manner.
commit_latency_stats->Add(timer.ElapsedNanos() / 1000);
DBUG_RETURN(HA_EXIT_SUCCESS);
}
static int rocksdb_rollback(handlerton *const hton, THD *const thd,
bool rollback_tx) {
Rdb_transaction *&tx = get_tx_from_thd(thd);
Rdb_perf_context_guard guard(tx, rocksdb_perf_context_level(thd));
if (tx != nullptr) {
if (rollback_tx) {
/*
We get here, when
- ROLLBACK statement is issued.
Discard the changes made by the transaction
*/
tx->rollback();
} else {
/*
We get here when
- a statement with AUTOCOMMIT=1 is being rolled back (because of some
error)
- a statement inside a transaction is rolled back
*/
tx->rollback_stmt();
tx->set_tx_failed(true);
}
if (my_core::thd_tx_isolation(thd) <= ISO_READ_COMMITTED) {
// For READ_COMMITTED, we release any existing snapshot so that we will
// see any changes that occurred since the last statement.
tx->release_snapshot();
}
}
return HA_EXIT_SUCCESS;
}
static bool print_stats(THD *const thd, std::string const &type,
std::string const &name, std::string const &status,
stat_print_fn *stat_print) {
return stat_print(thd, type.c_str(), type.size(), name.c_str(), name.size(),
status.c_str(), status.size());
}
static std::string format_string(const char *const format, ...) {
std::string res;
va_list args;
va_list args_copy;
char static_buff[256];
DBUG_ASSERT(format != nullptr);
va_start(args, format);
va_copy(args_copy, args);
// Calculate how much space we will need
int len = vsnprintf(nullptr, 0, format, args);
va_end(args);
if (len < 0) {
res = std::string("<format error>");
} else if (len == 0) {
// Shortcut for an empty string
res = std::string("");
} else {
// For short enough output use a static buffer
char *buff = static_buff;
std::unique_ptr<char[]> dynamic_buff = nullptr;
len++; // Add one for null terminator
// for longer output use an allocated buffer
if (static_cast<uint>(len) > sizeof(static_buff)) {
dynamic_buff.reset(new char[len]);
buff = dynamic_buff.get();
}
// Now re-do the vsnprintf with the buffer which is now large enough
(void)vsnprintf(buff, len, format, args_copy);
// Convert to a std::string. Note we could have created a std::string
// large enough and then converted the buffer to a 'char*' and created
// the output in place. This would probably work but feels like a hack.
// Since this isn't code that needs to be super-performant we are going
// with this 'safer' method.
res = std::string(buff);
}
va_end(args_copy);
return res;
}
class Rdb_snapshot_status : public Rdb_tx_list_walker {
private:
std::string m_data;
static std::string current_timestamp(void) {
static const char *const format = "%d-%02d-%02d %02d:%02d:%02d";
time_t currtime;
struct tm currtm;
time(&currtime);
localtime_r(&currtime, &currtm);
return format_string(format, currtm.tm_year + 1900, currtm.tm_mon + 1,
currtm.tm_mday, currtm.tm_hour, currtm.tm_min,
currtm.tm_sec);
}
static std::string get_header(void) {
return "\n============================================================\n" +
current_timestamp() +
" ROCKSDB TRANSACTION MONITOR OUTPUT\n"
"============================================================\n"
"---------\n"
"SNAPSHOTS\n"
"---------\n"
"LIST OF SNAPSHOTS FOR EACH SESSION:\n";
}
static std::string get_footer(void) {
return "-----------------------------------------\n"
"END OF ROCKSDB TRANSACTION MONITOR OUTPUT\n"
"=========================================\n";
}
static std::string get_dlock_txn_info(const rocksdb::DeadlockInfo &txn,
const GL_INDEX_ID &gl_index_id,
bool is_last_path = false) {
std::string txn_data;
/* extract table name and index names using the index id */
std::string table_name = ddl_manager.safe_get_table_name(gl_index_id);
if (table_name.empty()) {
table_name =
"NOT FOUND; INDEX_ID: " + std::to_string(gl_index_id.index_id);
}
auto kd = ddl_manager.safe_find(gl_index_id);
std::string idx_name =
(kd) ? kd->get_name()
: "NOT FOUND; INDEX_ID: " + std::to_string(gl_index_id.index_id);
/* get the name of the column family */
rocksdb::ColumnFamilyHandle *cfh = cf_manager.get_cf(txn.m_cf_id);
std::string cf_name = cfh->GetName();
txn_data += format_string(
"TRANSACTIONID: %u\n"
"COLUMN FAMILY NAME: %s\n"
"WAITING KEY: %s\n"
"LOCK TYPE: %s\n"
"INDEX NAME: %s\n"
"TABLE NAME: %s\n",
txn.m_txn_id, cf_name.c_str(),
rdb_hexdump(txn.m_waiting_key.c_str(), txn.m_waiting_key.length())
.c_str(),
txn.m_exclusive ? "EXCLUSIVE" : "SHARED", idx_name.c_str(),
table_name.c_str());
if (!is_last_path) {
txn_data += "---------------WAITING FOR---------------\n";
}
return txn_data;
}
static std::string
get_dlock_path_info(const rocksdb::DeadlockPath &path_entry) {
std::string path_data;
if (path_entry.limit_exceeded) {
path_data += "\n-------DEADLOCK EXCEEDED MAX DEPTH-------\n";
} else {
path_data += "\n*** DEADLOCK PATH\n"
"=========================================\n";
for (auto it = path_entry.path.begin(); it != path_entry.path.end();
it++) {
auto txn = *it;
const GL_INDEX_ID gl_index_id = {
txn.m_cf_id, rdb_netbuf_to_uint32(reinterpret_cast<const uchar *>(
txn.m_waiting_key.c_str()))};
path_data += get_dlock_txn_info(txn, gl_index_id);
}
DBUG_ASSERT_IFF(path_entry.limit_exceeded, path_entry.path.empty());
/* print the first txn in the path to display the full deadlock cycle */
if (!path_entry.path.empty() && !path_entry.limit_exceeded) {
auto txn = path_entry.path[0];
const GL_INDEX_ID gl_index_id = {
txn.m_cf_id, rdb_netbuf_to_uint32(reinterpret_cast<const uchar *>(
txn.m_waiting_key.c_str()))};
path_data += get_dlock_txn_info(txn, gl_index_id, true);
/* prints the txn id of the transaction that caused the deadlock */
auto deadlocking_txn = *(path_entry.path.end() - 1);
path_data +=
format_string("\n--------TRANSACTIONID: %u GOT DEADLOCK---------\n",
deadlocking_txn.m_txn_id);
}
}
return path_data;
}
public:
Rdb_snapshot_status() : m_data(get_header()) {}
std::string getResult() { return m_data + get_footer(); }
/* Implement Rdb_transaction interface */
/* Create one row in the snapshot status table */
void process_tran(const Rdb_transaction *const tx) override {
DBUG_ASSERT(tx != nullptr);
/* Calculate the duration the snapshot has existed */
int64_t snapshot_timestamp = tx->m_snapshot_timestamp;
if (snapshot_timestamp != 0) {
int64_t curr_time;
rdb->GetEnv()->GetCurrentTime(&curr_time);
char buffer[1024];
#ifdef MARIAROCKS_NOT_YET
thd_security_context(tx->get_thd(), buffer, sizeof buffer, 0);
#endif
m_data += format_string(
"---SNAPSHOT, ACTIVE %lld sec\n"
"%s\n"
"lock count %llu, write count %llu\n"
"insert count %llu, update count %llu, delete count %llu\n",
(longlong)(curr_time - snapshot_timestamp), buffer, tx->get_lock_count(),
tx->get_write_count(), tx->get_insert_count(), tx->get_update_count(),
tx->get_delete_count());
}
}
void populate_deadlock_buffer() {
auto dlock_buffer = rdb->GetDeadlockInfoBuffer();
m_data += "----------LATEST DETECTED DEADLOCKS----------\n";
for (auto path_entry : dlock_buffer) {
m_data += get_dlock_path_info(path_entry);
}
}
};
/**
* @brief
* walks through all non-replication transactions and copies
* out relevant information for information_schema.rocksdb_trx
*/
class Rdb_trx_info_aggregator : public Rdb_tx_list_walker {
private:
std::vector<Rdb_trx_info> *m_trx_info;
public:
explicit Rdb_trx_info_aggregator(std::vector<Rdb_trx_info> *const trx_info)
: m_trx_info(trx_info) {}
void process_tran(const Rdb_transaction *const tx) override {
static const std::map<int, std::string> state_map = {
{rocksdb::Transaction::STARTED, "STARTED"},
{rocksdb::Transaction::AWAITING_PREPARE, "AWAITING_PREPARE"},
{rocksdb::Transaction::PREPARED, "PREPARED"},
{rocksdb::Transaction::AWAITING_COMMIT, "AWAITING_COMMIT"},
{rocksdb::Transaction::COMMITED, "COMMITED"},
{rocksdb::Transaction::AWAITING_ROLLBACK, "AWAITING_ROLLBACK"},
{rocksdb::Transaction::ROLLEDBACK, "ROLLEDBACK"},
};
DBUG_ASSERT(tx != nullptr);
THD *const thd = tx->get_thd();
ulong thread_id = thd_get_thread_id(thd);
if (tx->is_writebatch_trx()) {
const auto wb_impl = static_cast<const Rdb_writebatch_impl *>(tx);
DBUG_ASSERT(wb_impl);
m_trx_info->push_back(
{"", /* name */
0, /* trx_id */
wb_impl->get_write_count(), 0, /* lock_count */
0, /* timeout_sec */
"", /* state */
"", /* waiting_key */
0, /* waiting_cf_id */
1, /*is_replication */
1, /* skip_trx_api */
wb_impl->is_tx_read_only(), 0, /* deadlock detection */
wb_impl->num_ongoing_bulk_load(), thread_id, "" /* query string */});
} else {
const auto tx_impl = static_cast<const Rdb_transaction_impl *>(tx);
DBUG_ASSERT(tx_impl);
const rocksdb::Transaction *rdb_trx = tx_impl->get_rdb_trx();
if (rdb_trx == nullptr) {
return;
}
char query_buf[NAME_LEN+1];
thd_query_safe(thd, query_buf, sizeof(query_buf));
std::string query_str(query_buf);
const auto state_it = state_map.find(rdb_trx->GetState());
DBUG_ASSERT(state_it != state_map.end());
const int is_replication = (thd->rgi_slave != nullptr);
uint32_t waiting_cf_id;
std::string waiting_key;
rdb_trx->GetWaitingTxns(&waiting_cf_id, &waiting_key),
m_trx_info->push_back(
{rdb_trx->GetName(), rdb_trx->GetID(), tx_impl->get_write_count(),
tx_impl->get_lock_count(), tx_impl->get_timeout_sec(),
state_it->second, waiting_key, waiting_cf_id, is_replication,
0, /* skip_trx_api */
tx_impl->is_tx_read_only(), rdb_trx->IsDeadlockDetect(),
tx_impl->num_ongoing_bulk_load(), thread_id, query_str});
}
}
};
/*
returns a vector of info for all non-replication threads
for use by information_schema.rocksdb_trx
*/
std::vector<Rdb_trx_info> rdb_get_all_trx_info() {
std::vector<Rdb_trx_info> trx_info;
Rdb_trx_info_aggregator trx_info_agg(&trx_info);
Rdb_transaction::walk_tx_list(&trx_info_agg);
return trx_info;
}
#ifdef MARIAROCKS_NOT_YET
/* Generate the snapshot status table */
static bool rocksdb_show_snapshot_status(handlerton *const hton, THD *const thd,
stat_print_fn *const stat_print) {
Rdb_snapshot_status showStatus;
Rdb_transaction::walk_tx_list(&showStatus);
showStatus.populate_deadlock_buffer();
/* Send the result data back to MySQL */
return print_stats(thd, "rocksdb", "", showStatus.getResult(), stat_print);
}
#endif
/*
This is called for SHOW ENGINE ROCKSDB STATUS | LOGS | etc.
For now, produce info about live files (which gives an imprecise idea about
what column families are there).
*/
static bool rocksdb_show_status(handlerton *const hton, THD *const thd,
stat_print_fn *const stat_print,
enum ha_stat_type stat_type) {
DBUG_ASSERT(hton != nullptr);
DBUG_ASSERT(thd != nullptr);
DBUG_ASSERT(stat_print != nullptr);
bool res = false;
char buf[100] = {'\0'};
if (stat_type == HA_ENGINE_STATUS) {
DBUG_ASSERT(rdb != nullptr);
std::string str;
/* Global DB Statistics */
if (rocksdb_stats) {
str = rocksdb_stats->ToString();
// Use the same format as internal RocksDB statistics entries to make
// sure that output will look unified.
DBUG_ASSERT(commit_latency_stats != nullptr);
snprintf(buf, sizeof(buf), "rocksdb.commit_latency statistics "
"Percentiles :=> 50 : %.2f 95 : %.2f "
"99 : %.2f 100 : %.2f\n",
commit_latency_stats->Percentile(50),
commit_latency_stats->Percentile(95),
commit_latency_stats->Percentile(99),
commit_latency_stats->Percentile(100));
str.append(buf);
uint64_t v = 0;
// Retrieve additional stalling related numbers from RocksDB and append
// them to the buffer meant for displaying detailed statistics. The intent
// here is to avoid adding another row to the query output because of
// just two numbers.
//
// NB! We're replacing hyphens with underscores in output to better match
// the existing naming convention.
if (rdb->GetIntProperty("rocksdb.is-write-stopped", &v)) {
snprintf(buf, sizeof(buf), "rocksdb.is_write_stopped COUNT : %llu\n", (ulonglong)v);
str.append(buf);
}
if (rdb->GetIntProperty("rocksdb.actual-delayed-write-rate", &v)) {
snprintf(buf, sizeof(buf), "rocksdb.actual_delayed_write_rate "
"COUNT : %llu\n",
(ulonglong)v);
str.append(buf);
}
res |= print_stats(thd, "STATISTICS", "rocksdb", str, stat_print);
}
/* Per DB stats */
if (rdb->GetProperty("rocksdb.dbstats", &str)) {
res |= print_stats(thd, "DBSTATS", "rocksdb", str, stat_print);
}
/* Per column family stats */
for (const auto &cf_name : cf_manager.get_cf_names()) {
rocksdb::ColumnFamilyHandle *cfh = cf_manager.get_cf(cf_name);
if (cfh == nullptr) {
continue;
}
if (!rdb->GetProperty(cfh, "rocksdb.cfstats", &str)) {
continue;
}
res |= print_stats(thd, "CF_COMPACTION", cf_name, str, stat_print);
}
/* Memory Statistics */
std::vector<rocksdb::DB *> dbs;
std::unordered_set<const rocksdb::Cache *> cache_set;
size_t internal_cache_count = 0;
size_t kDefaultInternalCacheSize = 8 * 1024 * 1024;
dbs.push_back(rdb);
cache_set.insert(rocksdb_tbl_options->block_cache.get());
for (const auto &cf_handle : cf_manager.get_all_cf()) {
rocksdb::ColumnFamilyDescriptor cf_desc;
cf_handle->GetDescriptor(&cf_desc);
auto *const table_factory = cf_desc.options.table_factory.get();
if (table_factory != nullptr) {
std::string tf_name = table_factory->Name();
if (tf_name.find("BlockBasedTable") != std::string::npos) {
const rocksdb::BlockBasedTableOptions *const bbt_opt =
reinterpret_cast<rocksdb::BlockBasedTableOptions *>(
table_factory->GetOptions());
if (bbt_opt != nullptr) {
if (bbt_opt->block_cache.get() != nullptr) {
cache_set.insert(bbt_opt->block_cache.get());
} else {
internal_cache_count++;
}
cache_set.insert(bbt_opt->block_cache_compressed.get());
}
}
}
}
std::map<rocksdb::MemoryUtil::UsageType, uint64_t> temp_usage_by_type;
str.clear();
rocksdb::MemoryUtil::GetApproximateMemoryUsageByType(dbs, cache_set,
&temp_usage_by_type);
snprintf(buf, sizeof(buf), "\nMemTable Total: %llu",
(ulonglong)temp_usage_by_type[rocksdb::MemoryUtil::kMemTableTotal]);
str.append(buf);
snprintf(buf, sizeof(buf), "\nMemTable Unflushed: %llu",
(ulonglong)temp_usage_by_type[rocksdb::MemoryUtil::kMemTableUnFlushed]);
str.append(buf);
snprintf(buf, sizeof(buf), "\nTable Readers Total: %llu",
(ulonglong)temp_usage_by_type[rocksdb::MemoryUtil::kTableReadersTotal]);
str.append(buf);
snprintf(buf, sizeof(buf), "\nCache Total: %llu",
(ulonglong)temp_usage_by_type[rocksdb::MemoryUtil::kCacheTotal]);
str.append(buf);
snprintf(buf, sizeof(buf), "\nDefault Cache Capacity: %llu",
(ulonglong)internal_cache_count * kDefaultInternalCacheSize);
str.append(buf);
res |= print_stats(thd, "MEMORY_STATS", "rocksdb", str, stat_print);
#ifdef MARIAROCKS_NOT_YET
/* Show the background thread status */
std::vector<rocksdb::ThreadStatus> thread_list;
rocksdb::Status s = rdb->GetEnv()->GetThreadList(&thread_list);
if (!s.ok()) {
sql_print_error("RocksDB: Returned error (%s) from GetThreadList.\n",
s.ToString().c_str());
res |= true;
} else {
/* For each background thread retrieved, print out its information */
for (auto &it : thread_list) {
/* Only look at background threads. Ignore user threads, if any. */
if (it.thread_type > rocksdb::ThreadStatus::LOW_PRIORITY) {
continue;
}
str = "\nthread_type: " + it.GetThreadTypeName(it.thread_type) +
"\ncf_name: " + it.cf_name +
"\noperation_type: " + it.GetOperationName(it.operation_type) +
"\noperation_stage: " +
it.GetOperationStageName(it.operation_stage) +
"\nelapsed_time_ms: " +
it.MicrosToString(it.op_elapsed_micros);
for (auto &it_props :
it.InterpretOperationProperties(it.operation_type,
it.op_properties)) {
str += "\n" + it_props.first + ": " + std::to_string(it_props.second);
}
str += "\nstate_type: " + it.GetStateName(it.state_type);
res |= print_stats(thd, "BG_THREADS", std::to_string(it.thread_id),
str, stat_print);
}
}
#endif
#ifdef MARIAROCKS_NOT_YET
} else if (stat_type == HA_ENGINE_TRX) {
/* Handle the SHOW ENGINE ROCKSDB TRANSACTION STATUS command */
res |= rocksdb_show_snapshot_status(hton, thd, stat_print);
#endif
}
return res;
}
static inline void rocksdb_register_tx(handlerton *const hton, THD *const thd,
Rdb_transaction *const tx) {
DBUG_ASSERT(tx != nullptr);
trans_register_ha(thd, FALSE, rocksdb_hton);
if (my_core::thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) {
tx->start_stmt();
trans_register_ha(thd, TRUE, rocksdb_hton);
}
}
static const char *ha_rocksdb_exts[] = {NullS};
/*
Supporting START TRANSACTION WITH CONSISTENT [ROCKSDB] SNAPSHOT
Features:
1. Supporting START TRANSACTION WITH CONSISTENT SNAPSHOT
2. Getting current binlog position in addition to #1.
The second feature is done by START TRANSACTION WITH
CONSISTENT ROCKSDB SNAPSHOT. This is Facebook's extension, and
it works like existing START TRANSACTION WITH CONSISTENT INNODB SNAPSHOT.
- When not setting engine, START TRANSACTION WITH CONSISTENT SNAPSHOT
takes both InnoDB and RocksDB snapshots, and both InnoDB and RocksDB
participate in transaction. When executing COMMIT, both InnoDB and
RocksDB modifications are committed. Remember that XA is not supported yet,
so mixing engines is not recommended anyway.
- When setting engine, START TRANSACTION WITH CONSISTENT.. takes
snapshot for the specified engine only. But it starts both
InnoDB and RocksDB transactions.
*/
static int rocksdb_start_tx_and_assign_read_view(
handlerton *const hton, /*!< in: RocksDB handlerton */
THD* thd) /*!< in: MySQL thread handle of the
user for whom the transaction should
be committed */
{
ulong const tx_isolation = my_core::thd_tx_isolation(thd);
if (tx_isolation != ISO_REPEATABLE_READ) {
my_error(ER_ISOLATION_LEVEL_WITH_CONSISTENT_SNAPSHOT, MYF(0));
return HA_EXIT_FAILURE;
}
/*
MariaDB: there is no need to call mysql_bin_log_lock_commits and then
unlock back.
SQL layer calls start_consistent_snapshot() for all engines, including the
binlog under LOCK_commit_ordered mutex.
The mutex prevents binlog commits from happening (right?) while the storage
engine(s) allocate read snapshots. That way, each storage engine is
synchronized with current binlog position.
*/
mysql_mutex_assert_owner(&LOCK_commit_ordered);
Rdb_transaction *const tx = get_or_create_tx(thd);
Rdb_perf_context_guard guard(tx, rocksdb_perf_context_level(thd));
DBUG_ASSERT(!tx->has_snapshot());
tx->set_tx_read_only(true);
rocksdb_register_tx(hton, thd, tx);
tx->acquire_snapshot(true);
return HA_EXIT_SUCCESS;
}
/* Dummy SAVEPOINT support. This is needed for long running transactions
* like mysqldump (https://bugs.mysql.com/bug.php?id=71017).
* Current SAVEPOINT does not correctly handle ROLLBACK and does not return
* errors. This needs to be addressed in future versions (Issue#96).
*/
static int rocksdb_savepoint(handlerton *const hton, THD *const thd,
void *const savepoint) {
return HA_EXIT_SUCCESS;
}
static int rocksdb_rollback_to_savepoint(handlerton *const hton, THD *const thd,
void *const savepoint) {
Rdb_transaction *&tx = get_tx_from_thd(thd);
return tx->rollback_to_savepoint(savepoint);
}
static bool
rocksdb_rollback_to_savepoint_can_release_mdl(handlerton *const hton,
THD *const thd) {
return true;
}
#ifdef MARIAROCKS_NOT_YET
/*
This is called for INFORMATION_SCHEMA
*/
static void rocksdb_update_table_stats(
/* per-table stats callback */
void (*cb)(const char *db, const char *tbl, bool is_partition,
my_io_perf_t *r, my_io_perf_t *w, my_io_perf_t *r_blob,
my_io_perf_t *r_primary, my_io_perf_t *r_secondary,
page_stats_t *page_stats, comp_stats_t *comp_stats,
int n_lock_wait, int n_lock_wait_timeout, int n_lock_deadlock,
const char *engine)) {
my_io_perf_t io_perf_read;
my_io_perf_t io_perf_write;
my_io_perf_t io_perf;
page_stats_t page_stats;
comp_stats_t comp_stats;
uint lock_wait_timeout_stats;
uint deadlock_stats;
std::vector<std::string> tablenames;
/*
Most of these are for innodb, so setting them to 0.
TODO: possibly separate out primary vs. secondary index reads
*/
memset(&io_perf, 0, sizeof(io_perf));
memset(&page_stats, 0, sizeof(page_stats));
memset(&comp_stats, 0, sizeof(comp_stats));
memset(&io_perf_write, 0, sizeof(io_perf_write));
tablenames = rdb_open_tables.get_table_names();
for (const auto &it : tablenames) {
Rdb_table_handler *table_handler;
std::string str, dbname, tablename, partname;
char dbname_sys[NAME_LEN + 1];
char tablename_sys[NAME_LEN + 1];
bool is_partition;
if (rdb_normalize_tablename(it, &str) != HA_EXIT_SUCCESS) {
/* Function needs to return void because of the interface and we've
* detected an error which shouldn't happen. There's no way to let
* caller know that something failed.
*/
SHIP_ASSERT(false);
return;
}
if (rdb_split_normalized_tablename(str, &dbname, &tablename, &partname)) {
continue;
}
is_partition = (partname.size() != 0);
table_handler = rdb_open_tables.get_table_handler(it.c_str());
if (table_handler == nullptr) {
continue;
}
io_perf_read.bytes = table_handler->m_io_perf_read.bytes.load();
io_perf_read.requests = table_handler->m_io_perf_read.requests.load();
io_perf_write.bytes = table_handler->m_io_perf_write.bytes.load();
io_perf_write.requests = table_handler->m_io_perf_write.requests.load();
lock_wait_timeout_stats = table_handler->m_lock_wait_timeout_counter.load();
deadlock_stats = table_handler->m_deadlock_counter.load();
/*
Convert from rocksdb timer to mysql timer. RocksDB values are
in nanoseconds, but table statistics expect the value to be
in my_timer format.
*/
io_perf_read.svc_time = my_core::microseconds_to_my_timer(
table_handler->m_io_perf_read.svc_time.load() / 1000);
io_perf_read.svc_time_max = my_core::microseconds_to_my_timer(
table_handler->m_io_perf_read.svc_time_max.load() / 1000);
io_perf_read.wait_time = my_core::microseconds_to_my_timer(
table_handler->m_io_perf_read.wait_time.load() / 1000);
io_perf_read.wait_time_max = my_core::microseconds_to_my_timer(
table_handler->m_io_perf_read.wait_time_max.load() / 1000);
io_perf_read.slow_ios = table_handler->m_io_perf_read.slow_ios.load();
rdb_open_tables.release_table_handler(table_handler);
/*
Table stats expects our database and table name to be in system encoding,
not filename format. Convert before calling callback.
*/
my_core::filename_to_tablename(dbname.c_str(), dbname_sys,
sizeof(dbname_sys));
my_core::filename_to_tablename(tablename.c_str(), tablename_sys,
sizeof(tablename_sys));
(*cb)(dbname_sys, tablename_sys, is_partition, &io_perf_read,
&io_perf_write, &io_perf, &io_perf, &io_perf, &page_stats,
&comp_stats, 0, lock_wait_timeout_stats, deadlock_stats,
rocksdb_hton_name);
}
}
#endif
static rocksdb::Status check_rocksdb_options_compatibility(
const char *const dbpath, const rocksdb::Options &main_opts,
const std::vector<rocksdb::ColumnFamilyDescriptor> &cf_descr) {
DBUG_ASSERT(rocksdb_datadir != nullptr);
rocksdb::DBOptions loaded_db_opt;
std::vector<rocksdb::ColumnFamilyDescriptor> loaded_cf_descs;
rocksdb::Status status = LoadLatestOptions(dbpath, rocksdb::Env::Default(),
&loaded_db_opt, &loaded_cf_descs);
// If we're starting from scratch and there are no options saved yet then this
// is a valid case. Therefore we can't compare the current set of options to
// anything.
if (status.IsNotFound()) {
return rocksdb::Status::OK();
}
if (!status.ok()) {
return status;
}
if (loaded_cf_descs.size() != cf_descr.size()) {
return rocksdb::Status::NotSupported("Mismatched size of column family "
"descriptors.");
}
// Please see RocksDB documentation for more context about why we need to set
// user-defined functions and pointer-typed options manually.
for (size_t i = 0; i < loaded_cf_descs.size(); i++) {
loaded_cf_descs[i].options.compaction_filter =
cf_descr[i].options.compaction_filter;
loaded_cf_descs[i].options.compaction_filter_factory =
cf_descr[i].options.compaction_filter_factory;
loaded_cf_descs[i].options.comparator = cf_descr[i].options.comparator;
loaded_cf_descs[i].options.memtable_factory =
cf_descr[i].options.memtable_factory;
loaded_cf_descs[i].options.merge_operator =
cf_descr[i].options.merge_operator;
loaded_cf_descs[i].options.prefix_extractor =
cf_descr[i].options.prefix_extractor;
loaded_cf_descs[i].options.table_factory =
cf_descr[i].options.table_factory;
}
// This is the essence of the function - determine if it's safe to open the
// database or not.
status = CheckOptionsCompatibility(dbpath, rocksdb::Env::Default(), main_opts,
loaded_cf_descs);
return status;
}
/*
Storage Engine initialization function, invoked when plugin is loaded.
*/
static int rocksdb_init_func(void *const p) {
DBUG_ENTER_FUNC();
// Validate the assumption about the size of ROCKSDB_SIZEOF_HIDDEN_PK_COLUMN.
static_assert(sizeof(longlong) == 8, "Assuming that longlong is 8 bytes.");
init_rocksdb_psi_keys();
rocksdb_hton = (handlerton *)p;
mysql_mutex_init(rdb_psi_open_tbls_mutex_key, &rdb_open_tables.m_mutex,
MY_MUTEX_INIT_FAST);
#ifdef HAVE_PSI_INTERFACE
rdb_bg_thread.init(rdb_signal_bg_psi_mutex_key, rdb_signal_bg_psi_cond_key);
rdb_drop_idx_thread.init(rdb_signal_drop_idx_psi_mutex_key,
rdb_signal_drop_idx_psi_cond_key);
#else
rdb_bg_thread.init();
rdb_drop_idx_thread.init();
#endif
mysql_mutex_init(rdb_collation_data_mutex_key, &rdb_collation_data_mutex,
MY_MUTEX_INIT_FAST);
mysql_mutex_init(rdb_mem_cmp_space_mutex_key, &rdb_mem_cmp_space_mutex,
MY_MUTEX_INIT_FAST);
#if defined(HAVE_PSI_INTERFACE)
rdb_collation_exceptions =
new Regex_list_handler(key_rwlock_collation_exception_list);
#else
rdb_collation_exceptions = new Regex_list_handler();
#endif
mysql_mutex_init(rdb_sysvars_psi_mutex_key, &rdb_sysvars_mutex,
MY_MUTEX_INIT_FAST);
Rdb_transaction::init_mutex();
rocksdb_hton->state = SHOW_OPTION_YES;
rocksdb_hton->create = rocksdb_create_handler;
rocksdb_hton->close_connection = rocksdb_close_connection;
rocksdb_hton->prepare = rocksdb_prepare;
rocksdb_hton->prepare_ordered = NULL; // Do not need it
rocksdb_hton->commit_by_xid = rocksdb_commit_by_xid;
rocksdb_hton->rollback_by_xid = rocksdb_rollback_by_xid;
rocksdb_hton->recover = rocksdb_recover;
rocksdb_hton->commit_ordered= rocksdb_commit_ordered;
rocksdb_hton->commit = rocksdb_commit;
rocksdb_hton->commit_checkpoint_request= rocksdb_checkpoint_request;
rocksdb_hton->rollback = rocksdb_rollback;
rocksdb_hton->show_status = rocksdb_show_status;
rocksdb_hton->start_consistent_snapshot =
rocksdb_start_tx_and_assign_read_view;
rocksdb_hton->savepoint_set = rocksdb_savepoint;
rocksdb_hton->savepoint_rollback = rocksdb_rollback_to_savepoint;
rocksdb_hton->savepoint_rollback_can_release_mdl =
rocksdb_rollback_to_savepoint_can_release_mdl;
#ifdef MARIAROCKS_NOT_YET
rocksdb_hton->update_table_stats = rocksdb_update_table_stats;
#endif // MARIAROCKS_NOT_YET
/*
Not needed in MariaDB:
rocksdb_hton->flush_logs = rocksdb_flush_wal;
*/
rocksdb_hton->flags = HTON_TEMPORARY_NOT_SUPPORTED |
HTON_SUPPORTS_EXTENDED_KEYS | HTON_CAN_RECREATE;
rocksdb_hton->tablefile_extensions= ha_rocksdb_exts;
DBUG_ASSERT(!mysqld_embedded);
rocksdb_stats = rocksdb::CreateDBStatistics();
rocksdb_db_options->statistics = rocksdb_stats;
if (rocksdb_rate_limiter_bytes_per_sec != 0) {
rocksdb_rate_limiter.reset(
rocksdb::NewGenericRateLimiter(rocksdb_rate_limiter_bytes_per_sec));
rocksdb_db_options->rate_limiter = rocksdb_rate_limiter;
}
rocksdb_db_options->delayed_write_rate = rocksdb_delayed_write_rate;
std::shared_ptr<Rdb_logger> myrocks_logger = std::make_shared<Rdb_logger>();
rocksdb::Status s = rocksdb::CreateLoggerFromOptions(
rocksdb_datadir, *rocksdb_db_options, &rocksdb_db_options->info_log);
if (s.ok()) {
myrocks_logger->SetRocksDBLogger(rocksdb_db_options->info_log);
}
rocksdb_db_options->info_log = myrocks_logger;
myrocks_logger->SetInfoLogLevel(
static_cast<rocksdb::InfoLogLevel>(rocksdb_info_log_level));
rocksdb_db_options->wal_dir = rocksdb_wal_dir;
rocksdb_db_options->wal_recovery_mode =
static_cast<rocksdb::WALRecoveryMode>(rocksdb_wal_recovery_mode);
rocksdb_db_options->access_hint_on_compaction_start =
static_cast<rocksdb::Options::AccessHint>(
rocksdb_access_hint_on_compaction_start);
if (rocksdb_db_options->allow_mmap_reads &&
rocksdb_db_options->use_direct_reads) {
// allow_mmap_reads implies !use_direct_reads and RocksDB will not open if
// mmap_reads and direct_reads are both on. (NO_LINT_DEBUG)
sql_print_error("RocksDB: Can't enable both use_direct_reads "
"and allow_mmap_reads\n");
DBUG_RETURN(HA_EXIT_FAILURE);
}
if (rocksdb_db_options->allow_mmap_writes &&
rocksdb_db_options->use_direct_io_for_flush_and_compaction) {
// See above comment for allow_mmap_reads. (NO_LINT_DEBUG)
sql_print_error("RocksDB: Can't enable both "
"use_direct_io_for_flush_and_compaction and "
"allow_mmap_writes\n");
DBUG_RETURN(HA_EXIT_FAILURE);
}
// sst_file_manager will move deleted rocksdb sst files to trash_dir
// to be deleted in a background thread.
std::string trash_dir = std::string(rocksdb_datadir) + "/trash";
rocksdb_db_options->sst_file_manager.reset(
NewSstFileManager(rocksdb_db_options->env, myrocks_logger, trash_dir));
rocksdb_db_options->sst_file_manager->SetDeleteRateBytesPerSecond(
rocksdb_sst_mgr_rate_bytes_per_sec);
std::vector<std::string> cf_names;
rocksdb::Status status;
status = rocksdb::DB::ListColumnFamilies(*rocksdb_db_options, rocksdb_datadir,
&cf_names);
if (!status.ok()) {
/*
When we start on an empty datadir, ListColumnFamilies returns IOError,
and RocksDB doesn't provide any way to check what kind of error it was.
Checking system errno happens to work right now.
*/
if (status.IsIOError()
#ifndef _WIN32
&& errno == ENOENT
#endif
) {
sql_print_information("RocksDB: Got ENOENT when listing column families");
sql_print_information(
"RocksDB: assuming that we're creating a new database");
} else {
rdb_log_status_error(status, "Error listing column families");
DBUG_RETURN(HA_EXIT_FAILURE);
}
} else
sql_print_information("RocksDB: %ld column families found",
cf_names.size());
std::vector<rocksdb::ColumnFamilyDescriptor> cf_descr;
std::vector<rocksdb::ColumnFamilyHandle *> cf_handles;
rocksdb_tbl_options->index_type =
(rocksdb::BlockBasedTableOptions::IndexType)rocksdb_index_type;
if (!rocksdb_tbl_options->no_block_cache) {
std::shared_ptr<rocksdb::Cache> block_cache = rocksdb_use_clock_cache
? rocksdb::NewClockCache(rocksdb_block_cache_size)
: rocksdb::NewLRUCache(rocksdb_block_cache_size);
if (rocksdb_sim_cache_size > 0) {
// Simulated cache enabled
// Wrap block cache inside a simulated cache and pass it to RocksDB
rocksdb_tbl_options->block_cache =
rocksdb::NewSimCache(block_cache, rocksdb_sim_cache_size, 6);
} else {
// Pass block cache to RocksDB
rocksdb_tbl_options->block_cache = block_cache;
}
}
// Using newer BlockBasedTable format version for better compression
// and better memory allocation.
// See:
// https://github.com/facebook/rocksdb/commit/9ab5adfc59a621d12357580c94451d9f7320c2dd
rocksdb_tbl_options->format_version = 2;
if (rocksdb_collect_sst_properties) {
properties_collector_factory =
std::make_shared<Rdb_tbl_prop_coll_factory>(&ddl_manager);
rocksdb_set_compaction_options(nullptr, nullptr, nullptr, nullptr);
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
DBUG_ASSERT(rocksdb_table_stats_sampling_pct <=
RDB_TBL_STATS_SAMPLE_PCT_MAX);
properties_collector_factory->SetTableStatsSamplingPct(
rocksdb_table_stats_sampling_pct);
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
if (rocksdb_persistent_cache_size_mb > 0) {
std::shared_ptr<rocksdb::PersistentCache> pcache;
uint64_t cache_size_bytes= rocksdb_persistent_cache_size_mb * 1024 * 1024;
rocksdb::NewPersistentCache(
rocksdb::Env::Default(), std::string(rocksdb_persistent_cache_path),
cache_size_bytes, myrocks_logger, true, &pcache);
rocksdb_tbl_options->persistent_cache = pcache;
} else if (strlen(rocksdb_persistent_cache_path)) {
sql_print_error("RocksDB: Must specify rocksdb_persistent_cache_size_mb");
DBUG_RETURN(HA_EXIT_FAILURE);
}
std::unique_ptr<Rdb_cf_options> cf_options_map(new Rdb_cf_options());
if (!cf_options_map->init(*rocksdb_tbl_options, properties_collector_factory,
rocksdb_default_cf_options,
rocksdb_override_cf_options)) {
// NO_LINT_DEBUG
sql_print_error("RocksDB: Failed to initialize CF options map.");
DBUG_RETURN(HA_EXIT_FAILURE);
}
/*
If there are no column families, we're creating the new database.
Create one column family named "default".
*/
if (cf_names.size() == 0)
cf_names.push_back(DEFAULT_CF_NAME);
std::vector<int> compaction_enabled_cf_indices;
sql_print_information("RocksDB: Column Families at start:");
for (size_t i = 0; i < cf_names.size(); ++i) {
rocksdb::ColumnFamilyOptions opts;
cf_options_map->get_cf_options(cf_names[i], &opts);
sql_print_information(" cf=%s", cf_names[i].c_str());
sql_print_information(" write_buffer_size=%ld", opts.write_buffer_size);
sql_print_information(" target_file_size_base=%" PRIu64,
opts.target_file_size_base);
/*
Temporarily disable compactions to prevent a race condition where
compaction starts before compaction filter is ready.
*/
if (!opts.disable_auto_compactions) {
compaction_enabled_cf_indices.push_back(i);
opts.disable_auto_compactions = true;
}
cf_descr.push_back(rocksdb::ColumnFamilyDescriptor(cf_names[i], opts));
}
rocksdb::Options main_opts(*rocksdb_db_options,
cf_options_map->get_defaults());
rocksdb::TransactionDBOptions tx_db_options;
tx_db_options.transaction_lock_timeout = 2; // 2 seconds
tx_db_options.custom_mutex_factory = std::make_shared<Rdb_mutex_factory>();
status =
check_rocksdb_options_compatibility(rocksdb_datadir, main_opts, cf_descr);
// We won't start if we'll determine that there's a chance of data corruption
// because of incompatible options.
if (!status.ok()) {
rdb_log_status_error(
status, "Compatibility check against existing database options failed");
DBUG_RETURN(HA_EXIT_FAILURE);
}
status = rocksdb::TransactionDB::Open(
main_opts, tx_db_options, rocksdb_datadir, cf_descr, &cf_handles, &rdb);
if (!status.ok()) {
rdb_log_status_error(status, "Error opening instance");
DBUG_RETURN(HA_EXIT_FAILURE);
}
cf_manager.init(std::move(cf_options_map), &cf_handles);
if (dict_manager.init(rdb->GetBaseDB(), &cf_manager)) {
// NO_LINT_DEBUG
sql_print_error("RocksDB: Failed to initialize data dictionary.");
DBUG_RETURN(HA_EXIT_FAILURE);
}
if (binlog_manager.init(&dict_manager)) {
// NO_LINT_DEBUG
sql_print_error("RocksDB: Failed to initialize binlog manager.");
DBUG_RETURN(HA_EXIT_FAILURE);
}
if (ddl_manager.init(&dict_manager, &cf_manager, rocksdb_validate_tables)) {
// NO_LINT_DEBUG
sql_print_error("RocksDB: Failed to initialize DDL manager.");
DBUG_RETURN(HA_EXIT_FAILURE);
}
Rdb_sst_info::init(rdb);
/*
Enable auto compaction, things needed for compaction filter are finished
initializing
*/
std::vector<rocksdb::ColumnFamilyHandle *> compaction_enabled_cf_handles;
compaction_enabled_cf_handles.reserve(compaction_enabled_cf_indices.size());
for (const auto &index : compaction_enabled_cf_indices) {
compaction_enabled_cf_handles.push_back(cf_handles[index]);
}
status = rdb->EnableAutoCompaction(compaction_enabled_cf_handles);
if (!status.ok()) {
rdb_log_status_error(status, "Error enabling compaction");
DBUG_RETURN(HA_EXIT_FAILURE);
}
auto err = rdb_bg_thread.create_thread(BG_THREAD_NAME
#ifdef HAVE_PSI_INTERFACE
,
rdb_background_psi_thread_key
#endif
);
if (err != 0) {
sql_print_error("RocksDB: Couldn't start the background thread: (errno=%d)",
err);
DBUG_RETURN(HA_EXIT_FAILURE);
}
err = rdb_drop_idx_thread.create_thread(INDEX_THREAD_NAME
#ifdef HAVE_PSI_INTERFACE
,
rdb_drop_idx_psi_thread_key
#endif
);
if (err != 0) {
sql_print_error("RocksDB: Couldn't start the drop index thread: (errno=%d)",
err);
DBUG_RETURN(HA_EXIT_FAILURE);
}
rdb_set_collation_exception_list(rocksdb_strict_collation_exceptions);
if (rocksdb_pause_background_work) {
rdb->PauseBackgroundWork();
}
// NO_LINT_DEBUG
sql_print_information("RocksDB: global statistics using %s indexer",
STRINGIFY_ARG(RDB_INDEXER));
#if defined(HAVE_SCHED_GETCPU)
if (sched_getcpu() == -1) {
// NO_LINT_DEBUG
sql_print_information(
"RocksDB: sched_getcpu() failed - "
"global statistics will use thread_id_indexer_t instead");
}
#endif
/**
Rocksdb does not always shutdown its threads, when
plugin is shut down. Disable server's leak check
at exit to avoid crash.
*/
my_disable_leak_check = true;
err = my_error_register(rdb_get_error_messages, HA_ERR_ROCKSDB_FIRST,
HA_ERR_ROCKSDB_LAST);
if (err != 0) {
// NO_LINT_DEBUG
sql_print_error("RocksDB: Couldn't initialize error messages");
rdb_open_tables.m_hash.~Rdb_table_set();
DBUG_RETURN(HA_EXIT_FAILURE);
}
// Creating an instance of HistogramImpl should only happen after RocksDB
// has been successfully initialized.
commit_latency_stats = new rocksdb::HistogramImpl();
// Construct a list of directories which will be monitored by I/O watchdog
// to make sure that we won't lose write access to them.
std::vector<std::string> directories;
// 1. Data directory.
directories.push_back(mysql_real_data_home);
// 2. Transaction logs.
if (myrocks::rocksdb_wal_dir && *myrocks::rocksdb_wal_dir) {
directories.push_back(myrocks::rocksdb_wal_dir);
}
#if !defined(_WIN32) && !defined(__APPLE__)
io_watchdog = new Rdb_io_watchdog(directories);
io_watchdog->reset_timeout(rocksdb_io_write_timeout_secs);
#endif
// NO_LINT_DEBUG
sql_print_information("MyRocks storage engine plugin has been successfully "
"initialized.");
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/*
Storage Engine deinitialization function, invoked when plugin is unloaded.
*/
static int rocksdb_done_func(void *const p) {
DBUG_ENTER_FUNC();
int error = 0;
// signal the drop index thread to stop
rdb_drop_idx_thread.signal(true);
// Flush all memtables for not losing data, even if WAL is disabled.
rocksdb_flush_all_memtables();
// Stop all rocksdb background work
CancelAllBackgroundWork(rdb->GetBaseDB(), true);
// Signal the background thread to stop and to persist all stats collected
// from background flushes and compactions. This will add more keys to a new
// memtable, but since the memtables were just flushed, it should not trigger
// a flush that can stall due to background threads being stopped. As long
// as these keys are stored in a WAL file, they can be retrieved on restart.
rdb_bg_thread.signal(true);
// Wait for the background thread to finish.
auto err = rdb_bg_thread.join();
if (err != 0) {
// We'll log the message and continue because we're shutting down and
// continuation is the optimal strategy.
// NO_LINT_DEBUG
sql_print_error("RocksDB: Couldn't stop the background thread: (errno=%d)",
err);
}
// Wait for the drop index thread to finish.
err = rdb_drop_idx_thread.join();
if (err != 0) {
// NO_LINT_DEBUG
sql_print_error("RocksDB: Couldn't stop the index thread: (errno=%d)", err);
}
if (rdb_open_tables.m_hash.size()) {
// Looks like we are getting unloaded and yet we have some open tables
// left behind.
error = 1;
}
/*
destructors for static objects can be called at _exit(),
but we want to free the memory at dlclose()
*/
rdb_open_tables.m_hash.~Rdb_table_set();
mysql_mutex_destroy(&rdb_open_tables.m_mutex);
mysql_mutex_destroy(&rdb_sysvars_mutex);
delete rdb_collation_exceptions;
mysql_mutex_destroy(&rdb_collation_data_mutex);
mysql_mutex_destroy(&rdb_mem_cmp_space_mutex);
Rdb_transaction::term_mutex();
for (auto &it : rdb_collation_data) {
delete it;
it = nullptr;
}
ddl_manager.cleanup();
binlog_manager.cleanup();
dict_manager.cleanup();
cf_manager.cleanup();
delete rdb;
rdb = nullptr;
delete commit_latency_stats;
commit_latency_stats = nullptr;
#if !defined(_WIN32) && !defined(__APPLE__)
delete io_watchdog;
io_watchdog = nullptr;
#endif
// Disown the cache data since we're shutting down.
// This results in memory leaks but it improved the shutdown time.
// Don't disown when running under valgrind
#ifndef HAVE_purify
if (rocksdb_tbl_options->block_cache) {
rocksdb_tbl_options->block_cache->DisownData();
}
#endif /* HAVE_purify */
rocksdb_db_options = nullptr;
rocksdb_tbl_options = nullptr;
rocksdb_stats = nullptr;
my_error_unregister(HA_ERR_ROCKSDB_FIRST, HA_ERR_ROCKSDB_LAST);
DBUG_RETURN(error);
}
static inline void rocksdb_smart_seek(bool seek_backward,
rocksdb::Iterator *const iter,
const rocksdb::Slice &key_slice) {
if (seek_backward) {
iter->SeekForPrev(key_slice);
} else {
iter->Seek(key_slice);
}
}
static inline void rocksdb_smart_next(bool seek_backward,
rocksdb::Iterator *const iter) {
if (seek_backward) {
iter->Prev();
} else {
iter->Next();
}
}
/**
@brief
Example of simple lock controls. The "table_handler" it creates is a
structure we will pass to each ha_rocksdb handler. Do you have to have
one of these? Well, you have pieces that are used for locking, and
they are needed to function.
*/
Rdb_table_handler *
Rdb_open_tables_map::get_table_handler(const char *const table_name) {
Rdb_table_handler *table_handler;
uint length;
char *tmp_name;
DBUG_ASSERT(table_name != nullptr);
length = (uint)strlen(table_name);
// First, look up the table in the hash map.
RDB_MUTEX_LOCK_CHECK(m_mutex);
if (!m_hash.size() || !(table_handler = m_hash.find(table_name, length))) {
// Since we did not find it in the hash map, attempt to create and add it
// to the hash map.
if (!(table_handler = reinterpret_cast<Rdb_table_handler *>(my_multi_malloc(
MYF(MY_WME | MY_ZEROFILL), &table_handler, sizeof(*table_handler),
&tmp_name, length + 1, NullS)))) {
// Allocating a new Rdb_table_handler and a new table name failed.
RDB_MUTEX_UNLOCK_CHECK(m_mutex);
return nullptr;
}
table_handler->m_ref_count = 0;
table_handler->m_table_name_length = length;
table_handler->m_table_name = tmp_name;
strmov(table_handler->m_table_name, table_name);
if (m_hash.insert(table_handler)) {
// Inserting into the hash map failed.
RDB_MUTEX_UNLOCK_CHECK(m_mutex);
my_free(table_handler);
return nullptr;
}
thr_lock_init(&table_handler->m_thr_lock);
#ifdef MARIAROCKS_NOT_YET
table_handler->m_io_perf_read.init();
table_handler->m_io_perf_write.init();
#endif
}
DBUG_ASSERT(table_handler->m_ref_count >= 0);
table_handler->m_ref_count++;
RDB_MUTEX_UNLOCK_CHECK(m_mutex);
return table_handler;
}
std::vector<std::string> rdb_get_open_table_names(void) {
return rdb_open_tables.get_table_names();
}
std::vector<std::string> Rdb_open_tables_map::get_table_names(void) const {
size_t i;
const Rdb_table_handler *table_handler;
std::vector<std::string> names;
RDB_MUTEX_LOCK_CHECK(m_mutex);
for (i = 0; (table_handler = m_hash.at(i)); i++) {
DBUG_ASSERT(table_handler != nullptr);
names.push_back(table_handler->m_table_name);
}
DBUG_ASSERT(i == m_hash.size());
RDB_MUTEX_UNLOCK_CHECK(m_mutex);
return names;
}
void ha_rocksdb::load_auto_incr_value() {
const int save_active_index = active_index;
active_index = table->s->next_number_index;
const uint8 save_table_status = table->status;
/*
load_auto_incr_value() may be called by statements that
do not execute implicit commits (i.e. SHOW CREATE TABLE).
index_last() creates a snapshot. When a snapshot is created
here, it has to be released as well. (GitHub issue#189)
*/
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
const bool is_new_snapshot = !tx->has_snapshot();
// Do a lookup. We only need index column, so it should be index-only.
// (another reason to make it index-only is that table->read_set is
// not set appropriately and non-index-only lookup will not read the value)
const bool save_keyread_only = m_keyread_only;
m_keyread_only = true;
if (!index_last(table->record[0]))
update_auto_incr_val();
m_keyread_only = save_keyread_only;
if (is_new_snapshot) {
tx->release_snapshot();
}
table->status = save_table_status;
active_index = save_active_index;
/*
Do what ha_rocksdb::index_end() does.
(Why don't we use index_init/index_end? class handler defines index_init
as private, for some reason).
*/
release_scan_iterator();
}
/* Get PK value from table->record[0]. */
/*
TODO(alexyang): No existing support for auto_increment on non-pk columns, see
end of ha_rocksdb::create. Also see opened issue here:
https://github.com/facebook/mysql-5.6/issues/153
*/
void ha_rocksdb::update_auto_incr_val() {
Field *field;
longlong new_val;
field = table->key_info[table->s->next_number_index].key_part[0].field;
my_bitmap_map *const old_map =
dbug_tmp_use_all_columns(table, table->read_set);
new_val = field->val_int();
// don't increment if we would wrap around
if (new_val != std::numeric_limits<longlong>::max()) {
new_val++;
}
dbug_tmp_restore_column_map(table->read_set, old_map);
longlong auto_incr_val = m_tbl_def->m_auto_incr_val;
while (auto_incr_val < new_val &&
!m_tbl_def->m_auto_incr_val.compare_exchange_weak(auto_incr_val,
new_val)) {
// Do nothing - just loop until auto_incr_val is >= new_val or
// we successfully set it
}
}
int ha_rocksdb::load_hidden_pk_value() {
const int save_active_index = active_index;
active_index = m_tbl_def->m_key_count - 1;
const uint8 save_table_status = table->status;
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
const bool is_new_snapshot = !tx->has_snapshot();
// Do a lookup.
if (!index_last(table->record[0])) {
/*
Decode PK field from the key
*/
longlong hidden_pk_id = 0;
auto err = read_hidden_pk_id_from_rowkey(&hidden_pk_id);
if (err) {
if (is_new_snapshot) {
tx->release_snapshot();
}
return err;
}
hidden_pk_id++;
longlong old = m_tbl_def->m_hidden_pk_val;
while (
old < hidden_pk_id &&
!m_tbl_def->m_hidden_pk_val.compare_exchange_weak(old, hidden_pk_id)) {
}
}
if (is_new_snapshot) {
tx->release_snapshot();
}
table->status = save_table_status;
active_index = save_active_index;
release_scan_iterator();
return HA_EXIT_SUCCESS;
}
/* Get PK value from m_tbl_def->m_hidden_pk_info. */
longlong ha_rocksdb::update_hidden_pk_val() {
DBUG_ASSERT(has_hidden_pk(table));
const longlong new_val = m_tbl_def->m_hidden_pk_val++;
return new_val;
}
/* Get the id of the hidden pk id from m_last_rowkey */
int ha_rocksdb::read_hidden_pk_id_from_rowkey(longlong *const hidden_pk_id) {
DBUG_ASSERT(hidden_pk_id != nullptr);
DBUG_ASSERT(table != nullptr);
DBUG_ASSERT(has_hidden_pk(table));
rocksdb::Slice rowkey_slice(m_last_rowkey.ptr(), m_last_rowkey.length());
// Get hidden primary key from old key slice
Rdb_string_reader reader(&rowkey_slice);
if ((!reader.read(Rdb_key_def::INDEX_NUMBER_SIZE)))
return HA_ERR_ROCKSDB_CORRUPT_DATA;
const int length= 8; /* was Field_longlong::PACK_LENGTH in FB MySQL tree */
const uchar *from = reinterpret_cast<const uchar *>(reader.read(length));
if (from == nullptr) {
/* Mem-comparable image doesn't have enough bytes */
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
*hidden_pk_id = rdb_netbuf_read_uint64(&from);
return HA_EXIT_SUCCESS;
}
/**
@brief
Free lock controls. We call this whenever we close a table. If the table had
the last reference to the table_handler, then we free the memory associated
with it.
*/
void Rdb_open_tables_map::release_table_handler(
Rdb_table_handler *const table_handler) {
RDB_MUTEX_LOCK_CHECK(m_mutex);
DBUG_ASSERT(table_handler != nullptr);
DBUG_ASSERT(table_handler->m_ref_count > 0);
if (!--table_handler->m_ref_count) {
// Last reference was released. Tear down the hash entry.
const auto ret MY_ATTRIBUTE((__unused__)) = m_hash.remove(table_handler);
DBUG_ASSERT(!ret); // the hash entry must actually be found and deleted
my_core::thr_lock_delete(&table_handler->m_thr_lock);
my_free(table_handler);
}
RDB_MUTEX_UNLOCK_CHECK(m_mutex);
}
static handler *rocksdb_create_handler(my_core::handlerton *const hton,
my_core::TABLE_SHARE *const table_arg,
my_core::MEM_ROOT *const mem_root) {
return new (mem_root) ha_rocksdb(hton, table_arg);
}
ha_rocksdb::ha_rocksdb(my_core::handlerton *const hton,
my_core::TABLE_SHARE *const table_arg)
: handler(hton, table_arg), m_table_handler(nullptr), m_scan_it(nullptr),
m_scan_it_skips_bloom(false), m_scan_it_snapshot(nullptr),
m_tbl_def(nullptr), m_pk_descr(nullptr), m_key_descr_arr(nullptr),
m_pk_can_be_decoded(false), m_maybe_unpack_info(false),
m_pk_tuple(nullptr), m_pk_packed_tuple(nullptr),
m_sk_packed_tuple(nullptr), m_end_key_packed_tuple(nullptr),
m_sk_match_prefix(nullptr), m_sk_match_prefix_buf(nullptr),
m_sk_packed_tuple_old(nullptr), m_dup_sk_packed_tuple(nullptr),
m_dup_sk_packed_tuple_old(nullptr), m_pack_buffer(nullptr),
m_lock_rows(RDB_LOCK_NONE), m_keyread_only(FALSE),
m_bulk_load_tx(nullptr), m_encoder_arr(nullptr),
m_row_checksums_checked(0), m_in_rpl_delete_rows(false),
m_in_rpl_update_rows(false), m_force_skip_unique_check(false) {
// TODO(alexyang): create a valid PSI_mutex_key for this mutex
mysql_mutex_init(0, &m_bulk_load_mutex, MY_MUTEX_INIT_FAST);
}
bool ha_rocksdb::same_table(const ha_rocksdb &other) const {
return m_tbl_def->base_tablename() == other.m_tbl_def->base_tablename();
}
/**
@return
false OK
other Error inpacking the data
*/
bool ha_rocksdb::init_with_fields() {
DBUG_ENTER_FUNC();
const uint pk = table_share->primary_key;
if (pk != MAX_KEY) {
const uint key_parts = table_share->key_info[pk].user_defined_key_parts;
check_keyread_allowed(pk /*PK*/, key_parts - 1, true);
} else
m_pk_can_be_decoded = false;
cached_table_flags = table_flags();
DBUG_RETURN(false); /* Ok */
}
/*
If the key is a TTL key, we may need to filter it out.
The purpose of read filtering for tables with TTL is to ensure that
during a transaction a key which has expired already but not removed by
compaction yet is not returned to the user.
Without this the user might be hit with problems such as disappearing
rows within a transaction, etc, because the compaction filter ignores
snapshots when filtering keys.
*/
bool ha_rocksdb::should_hide_ttl_rec(const Rdb_key_def &kd,
const rocksdb::Slice &ttl_rec_val,
const int64_t curr_ts) {
DBUG_ASSERT(kd.has_ttl());
DBUG_ASSERT(kd.m_ttl_rec_offset != UINT_MAX);
/*
Curr_ts can only be 0 if there are no snapshots open.
should_hide_ttl_rec can only be called when there is >=1 snapshots, unless
we are filtering on the write path (single INSERT/UPDATE) in which case
we are passed in the current time as curr_ts.
In the event curr_ts is 0, we always decide not to filter the record. We
also log a warning and increment a diagnostic counter.
*/
if (curr_ts == 0) {
update_row_stats(ROWS_HIDDEN_NO_SNAPSHOT);
return false;
}
if (!rdb_is_ttl_read_filtering_enabled() || !rdb_is_ttl_enabled()) {
return false;
}
Rdb_string_reader reader(&ttl_rec_val);
/*
Find where the 8-byte ttl is for each record in this index.
*/
uint64 ts;
if (!reader.read(kd.m_ttl_rec_offset) || reader.read_uint64(&ts)) {
/*
This condition should never be reached since all TTL records have an
8 byte ttl field in front. Don't filter the record out, and log an error.
*/
std::string buf;
buf = rdb_hexdump(ttl_rec_val.data(), ttl_rec_val.size(),
RDB_MAX_HEXDUMP_LEN);
const GL_INDEX_ID gl_index_id = kd.get_gl_index_id();
// NO_LINT_DEBUG
sql_print_error("Decoding ttl from PK value failed, "
"for index (%u,%u), val: %s",
gl_index_id.cf_id, gl_index_id.index_id, buf.c_str());
DBUG_ASSERT(0);
return false;
}
/* Hide record if it has expired before the current snapshot time. */
uint64 read_filter_ts = 0;
#ifndef NDEBUG
read_filter_ts += rdb_dbug_set_ttl_read_filter_ts();
#endif
return ts + kd.m_ttl_duration + read_filter_ts <=
static_cast<uint64>(curr_ts);
}
void ha_rocksdb::rocksdb_skip_expired_records(const Rdb_key_def &kd,
rocksdb::Iterator *const iter,
bool seek_backward) {
if (kd.has_ttl()) {
while (iter->Valid() &&
should_hide_ttl_rec(
kd, iter->value(),
get_or_create_tx(table->in_use)->m_snapshot_timestamp)) {
rocksdb_smart_next(seek_backward, iter);
}
}
}
/**
Convert record from table->record[0] form into a form that can be written
into rocksdb.
@param pk_packed_slice Packed PK tuple. We need it in order to compute
and store its CRC.
@param packed_rec OUT Data slice with record data.
*/
int ha_rocksdb::convert_record_to_storage_format(
const struct update_row_info &row_info, rocksdb::Slice *const packed_rec) {
DBUG_ASSERT_IMP(m_maybe_unpack_info, row_info.new_pk_unpack_info);
DBUG_ASSERT(m_pk_descr != nullptr);
const rocksdb::Slice &pk_packed_slice = row_info.new_pk_slice;
Rdb_string_writer *const pk_unpack_info = row_info.new_pk_unpack_info;
bool has_ttl = m_pk_descr->has_ttl();
bool has_ttl_column = !m_pk_descr->m_ttl_column.empty();
bool ttl_in_pk = has_ttl_column && (row_info.ttl_pk_offset != UINT_MAX);
m_storage_record.length(0);
if (has_ttl) {
/* If it's a TTL record, reserve space for 8 byte TTL value in front. */
m_storage_record.fill(ROCKSDB_SIZEOF_TTL_RECORD + m_null_bytes_in_rec, 0);
m_ttl_bytes_updated = false;
/*
If the TTL is contained within the key, we use the offset to find the
TTL value and place it in the beginning of the value record.
*/
if (ttl_in_pk) {
Rdb_string_reader reader(&pk_packed_slice);
const char *ts;
if (!reader.read(row_info.ttl_pk_offset) ||
!(ts = reader.read(ROCKSDB_SIZEOF_TTL_RECORD))) {
std::string buf;
buf = rdb_hexdump(pk_packed_slice.data(), pk_packed_slice.size(),
RDB_MAX_HEXDUMP_LEN);
const GL_INDEX_ID gl_index_id = m_pk_descr->get_gl_index_id();
// NO_LINT_DEBUG
sql_print_error("Decoding ttl from PK failed during insert, "
"for index (%u,%u), key: %s",
gl_index_id.cf_id, gl_index_id.index_id, buf.c_str());
return HA_EXIT_FAILURE;
}
char *const data = const_cast<char *>(m_storage_record.ptr());
memcpy(data, ts, ROCKSDB_SIZEOF_TTL_RECORD);
#ifndef NDEBUG
// Adjust for test case if needed
rdb_netbuf_store_uint64(
reinterpret_cast<uchar *>(data),
rdb_netbuf_to_uint64(reinterpret_cast<const uchar *>(data)) +
rdb_dbug_set_ttl_rec_ts());
#endif
// Also store in m_ttl_bytes to propagate to update_sk
memcpy(m_ttl_bytes, data, ROCKSDB_SIZEOF_TTL_RECORD);
} else if (!has_ttl_column) {
/*
For implicitly generated TTL records we need to copy over the old
TTL value from the old record in the event of an update. It was stored
in m_ttl_bytes.
Otherwise, generate a timestamp using the current time.
*/
if (!row_info.old_pk_slice.empty()) {
char *const data = const_cast<char *>(m_storage_record.ptr());
memcpy(data, m_ttl_bytes, sizeof(uint64));
} else {
uint64 ts = static_cast<uint64>(std::time(nullptr));
#ifndef NDEBUG
ts += rdb_dbug_set_ttl_rec_ts();
#endif
char *const data = const_cast<char *>(m_storage_record.ptr());
rdb_netbuf_store_uint64(reinterpret_cast<uchar *>(data), ts);
// Also store in m_ttl_bytes to propagate to update_sk
memcpy(m_ttl_bytes, data, ROCKSDB_SIZEOF_TTL_RECORD);
}
}
} else {
/* All NULL bits are initially 0 */
m_storage_record.fill(m_null_bytes_in_rec, 0);
}
// If a primary key may have non-empty unpack_info for certain values,
// (m_maybe_unpack_info=TRUE), we write the unpack_info block. The block
// itself was prepared in Rdb_key_def::pack_record.
if (m_maybe_unpack_info) {
m_storage_record.append(reinterpret_cast<char *>(pk_unpack_info->ptr()),
pk_unpack_info->get_current_pos());
}
for (uint i = 0; i < table->s->fields; i++) {
/* Don't pack decodable PK key parts */
if (m_encoder_arr[i].m_storage_type != Rdb_field_encoder::STORE_ALL) {
continue;
}
Field *const field = table->field[i];
if (m_encoder_arr[i].maybe_null()) {
char *data = const_cast<char *>(m_storage_record.ptr());
if (has_ttl) {
data += ROCKSDB_SIZEOF_TTL_RECORD;
}
if (field->is_null()) {
data[m_encoder_arr[i].m_null_offset] |= m_encoder_arr[i].m_null_mask;
/* Don't write anything for NULL values */
continue;
}
}
if (m_encoder_arr[i].m_field_type == MYSQL_TYPE_BLOB) {
my_core::Field_blob *blob = (my_core::Field_blob *)field;
/* Get the number of bytes needed to store length*/
const uint length_bytes = blob->pack_length() - portable_sizeof_char_ptr;
/* Store the length of the value */
m_storage_record.append(reinterpret_cast<char *>(blob->ptr),
length_bytes);
/* Store the blob value itself */
char *data_ptr;
memcpy(&data_ptr, blob->ptr + length_bytes, sizeof(uchar **));
m_storage_record.append(data_ptr, blob->get_length());
} else if (m_encoder_arr[i].m_field_type == MYSQL_TYPE_VARCHAR) {
Field_varstring *const field_var = (Field_varstring *)field;
uint data_len;
/* field_var->length_bytes is 1 or 2 */
if (field_var->length_bytes == 1) {
data_len = field_var->ptr[0];
} else {
DBUG_ASSERT(field_var->length_bytes == 2);
data_len = uint2korr(field_var->ptr);
}
m_storage_record.append(reinterpret_cast<char *>(field_var->ptr),
field_var->length_bytes + data_len);
} else {
/* Copy the field data */
const uint len = field->pack_length_in_rec();
m_storage_record.append(reinterpret_cast<char *>(field->ptr), len);
/*
Check if this is the TTL field within the table, if so store the TTL
in the front of the record as well here.
*/
if (has_ttl && has_ttl_column &&
i == m_pk_descr->get_ttl_field_offset()) {
DBUG_ASSERT(len == ROCKSDB_SIZEOF_TTL_RECORD);
DBUG_ASSERT(field->real_type() == MYSQL_TYPE_LONGLONG);
DBUG_ASSERT(m_pk_descr->get_ttl_field_offset() != UINT_MAX);
char *const data = const_cast<char *>(m_storage_record.ptr());
uint64 ts = uint8korr(field->ptr);
#ifndef NDEBUG
ts += rdb_dbug_set_ttl_rec_ts();
#endif
rdb_netbuf_store_uint64(reinterpret_cast<uchar *>(data), ts);
// If this is an update and the timestamp has been updated, take note
// so we can avoid updating SKs unnecessarily.
if (!row_info.old_pk_slice.empty()) {
m_ttl_bytes_updated =
memcmp(m_ttl_bytes, data, ROCKSDB_SIZEOF_TTL_RECORD);
}
// Store timestamp in m_ttl_bytes to propagate to update_sk
memcpy(m_ttl_bytes, data, ROCKSDB_SIZEOF_TTL_RECORD);
}
}
}
if (should_store_row_debug_checksums()) {
const uint32_t key_crc32 = my_core::crc32(
0, rdb_slice_to_uchar_ptr(&pk_packed_slice), pk_packed_slice.size());
const uint32_t val_crc32 =
my_core::crc32(0, rdb_mysql_str_to_uchar_str(&m_storage_record),
m_storage_record.length());
uchar key_crc_buf[RDB_CHECKSUM_SIZE];
uchar val_crc_buf[RDB_CHECKSUM_SIZE];
rdb_netbuf_store_uint32(key_crc_buf, key_crc32);
rdb_netbuf_store_uint32(val_crc_buf, val_crc32);
m_storage_record.append((const char *)&RDB_CHECKSUM_DATA_TAG, 1);
m_storage_record.append((const char *)key_crc_buf, RDB_CHECKSUM_SIZE);
m_storage_record.append((const char *)val_crc_buf, RDB_CHECKSUM_SIZE);
}
*packed_rec =
rocksdb::Slice(m_storage_record.ptr(), m_storage_record.length());
return HA_EXIT_SUCCESS;
}
/*
@brief
Setup which fields will be unpacked when reading rows
@detail
Two special cases when we still unpack all fields:
- When this table is being updated (m_lock_rows==RDB_LOCK_WRITE).
- When @@rocksdb_verify_row_debug_checksums is ON (In this mode, we need to
read all
fields to find whether there is a row checksum at the end. We could skip
the fields instead of decoding them, but currently we do decoding.)
@seealso
ha_rocksdb::setup_field_converters()
ha_rocksdb::convert_record_from_storage_format()
*/
void ha_rocksdb::setup_read_decoders() {
m_decoders_vect.clear();
int last_useful = 0;
int skip_size = 0;
for (uint i = 0; i < table->s->fields; i++) {
// We only need the decoder if the whole record is stored.
if (m_encoder_arr[i].m_storage_type != Rdb_field_encoder::STORE_ALL) {
continue;
}
// bitmap is cleared on index merge, but it still needs to decode columns
if (m_lock_rows == RDB_LOCK_WRITE || m_verify_row_debug_checksums ||
bitmap_is_clear_all(table->read_set) ||
bitmap_is_set(table->read_set, table->field[i]->field_index)) {
// We will need to decode this field
m_decoders_vect.push_back({&m_encoder_arr[i], true, skip_size});
last_useful = m_decoders_vect.size();
skip_size = 0;
} else {
if (m_encoder_arr[i].uses_variable_len_encoding() ||
m_encoder_arr[i].maybe_null()) {
// For variable-length field, we need to read the data and skip it
m_decoders_vect.push_back({&m_encoder_arr[i], false, skip_size});
skip_size = 0;
} else {
// Fixed-width field can be skipped without looking at it.
// Add appropriate skip_size to the next field.
skip_size += m_encoder_arr[i].m_pack_length_in_rec;
}
}
}
// It could be that the last few elements are varchars that just do
// skipping. Remove them.
m_decoders_vect.erase(m_decoders_vect.begin() + last_useful,
m_decoders_vect.end());
}
#ifndef NDEBUG
void dbug_append_garbage_at_end(std::string &on_disk_rec) {
on_disk_rec.append("abc");
}
void dbug_truncate_record(std::string &on_disk_rec) { on_disk_rec.resize(0); }
void dbug_modify_rec_varchar12(std::string &on_disk_rec) {
std::string res;
// The record is NULL-byte followed by VARCHAR(10).
// Put the NULL-byte
res.append("\0", 1);
// Then, add a valid VARCHAR(12) value.
res.append("\xC", 1);
res.append("123456789ab", 12);
on_disk_rec.assign(res);
}
void dbug_modify_key_varchar8(String &on_disk_rec) {
std::string res;
// The key starts with index number
res.append(on_disk_rec.ptr(), Rdb_key_def::INDEX_NUMBER_SIZE);
// Then, a mem-comparable form of a varchar(8) value.
res.append("ABCDE\0\0\0\xFC", 9);
on_disk_rec.length(0);
on_disk_rec.append(res.data(), res.size());
}
void dbug_create_err_inplace_alter() {
my_printf_error(ER_UNKNOWN_ERROR,
"Intentional failure in inplace alter occurred.", MYF(0));
}
#endif
int ha_rocksdb::convert_record_from_storage_format(
const rocksdb::Slice *const key, uchar *const buf) {
DBUG_EXECUTE_IF("myrocks_simulate_bad_row_read1",
dbug_append_garbage_at_end(m_retrieved_record););
DBUG_EXECUTE_IF("myrocks_simulate_bad_row_read2",
dbug_truncate_record(m_retrieved_record););
DBUG_EXECUTE_IF("myrocks_simulate_bad_row_read3",
dbug_modify_rec_varchar12(m_retrieved_record););
const rocksdb::Slice retrieved_rec_slice(&m_retrieved_record.front(),
m_retrieved_record.size());
return convert_record_from_storage_format(key, &retrieved_rec_slice, buf);
}
int ha_rocksdb::convert_blob_from_storage_format(
my_core::Field_blob *const blob,
Rdb_string_reader *const reader,
bool decode)
{
/* Get the number of bytes needed to store length*/
const uint length_bytes = blob->pack_length() - portable_sizeof_char_ptr;
const char *data_len_str;
if (!(data_len_str = reader->read(length_bytes))) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
memcpy(blob->ptr, data_len_str, length_bytes);
const uint32 data_len = blob->get_length(
reinterpret_cast<const uchar*>(data_len_str), length_bytes);
const char *blob_ptr;
if (!(blob_ptr = reader->read(data_len))) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
if (decode) {
// set 8-byte pointer to 0, like innodb does (relevant for 32-bit
// platforms)
memset(blob->ptr + length_bytes, 0, 8);
memcpy(blob->ptr + length_bytes, &blob_ptr, sizeof(uchar **));
}
return HA_EXIT_SUCCESS;
}
int ha_rocksdb::convert_varchar_from_storage_format(
my_core::Field_varstring *const field_var,
Rdb_string_reader *const reader,
bool decode)
{
const char *data_len_str;
if (!(data_len_str = reader->read(field_var->length_bytes)))
return HA_ERR_ROCKSDB_CORRUPT_DATA;
uint data_len;
/* field_var->length_bytes is 1 or 2 */
if (field_var->length_bytes == 1) {
data_len = (uchar)data_len_str[0];
} else {
DBUG_ASSERT(field_var->length_bytes == 2);
data_len = uint2korr(data_len_str);
}
if (data_len > field_var->field_length) {
/* The data on disk is longer than table DDL allows? */
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
if (!reader->read(data_len)) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
if (decode) {
memcpy(field_var->ptr, data_len_str, field_var->length_bytes + data_len);
}
return HA_EXIT_SUCCESS;
}
int ha_rocksdb::convert_field_from_storage_format(
my_core::Field *const field,
Rdb_string_reader *const reader,
bool decode,
uint len)
{
const char *data_bytes;
if (len > 0) {
if ((data_bytes = reader->read(len)) == nullptr) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
if (decode)
memcpy(field->ptr, data_bytes, len);
}
return HA_EXIT_SUCCESS;
}
/*
@brief
Unpack the record in this->m_retrieved_record and this->m_last_rowkey from
storage format into buf (which can be table->record[0] or table->record[1]).
@param key Table record's key in mem-comparable form.
@param buf Store record in table->record[0] format here
@detail
If the table has blobs, the unpacked data in buf may keep pointers to the
data in this->m_retrieved_record.
The key is only needed to check its checksum value (the checksum is in
m_retrieved_record).
@seealso
ha_rocksdb::setup_read_decoders() Sets up data structures which tell which
columns to decode.
@return
0 OK
other Error inpacking the data
*/
int ha_rocksdb::convert_record_from_storage_format(
const rocksdb::Slice *const key, const rocksdb::Slice *const value,
uchar *const buf) {
DBUG_ASSERT(key != nullptr);
DBUG_ASSERT(buf != nullptr);
Rdb_string_reader reader(value);
/*
Decode PK fields from the key
*/
DBUG_EXECUTE_IF("myrocks_simulate_bad_pk_read1",
dbug_modify_key_varchar8(m_last_rowkey););
const rocksdb::Slice rowkey_slice(m_last_rowkey.ptr(),
m_last_rowkey.length());
const char *unpack_info = nullptr;
uint16 unpack_info_len = 0;
rocksdb::Slice unpack_slice;
/* If it's a TTL record, skip the 8 byte TTL value */
const char *ttl_bytes;
if (m_pk_descr->has_ttl()) {
if ((ttl_bytes = reader.read(ROCKSDB_SIZEOF_TTL_RECORD))) {
memcpy(m_ttl_bytes, ttl_bytes, ROCKSDB_SIZEOF_TTL_RECORD);
} else {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
}
/* Other fields are decoded from the value */
const char *null_bytes = nullptr;
if (m_null_bytes_in_rec && !(null_bytes = reader.read(m_null_bytes_in_rec))) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
if (m_maybe_unpack_info) {
unpack_info = reader.get_current_ptr();
if (!unpack_info || !Rdb_key_def::is_unpack_data_tag(unpack_info[0]) ||
!reader.read(Rdb_key_def::get_unpack_header_size(unpack_info[0]))) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
unpack_info_len =
rdb_netbuf_to_uint16(reinterpret_cast<const uchar *>(unpack_info + 1));
unpack_slice = rocksdb::Slice(unpack_info, unpack_info_len);
reader.read(unpack_info_len -
Rdb_key_def::get_unpack_header_size(unpack_info[0]));
}
int err = m_pk_descr->unpack_record(table, buf, &rowkey_slice,
unpack_info ? &unpack_slice : nullptr,
false /* verify_checksum */);
if (err != HA_EXIT_SUCCESS) {
return err;
}
for (auto it = m_decoders_vect.begin(); it != m_decoders_vect.end(); it++) {
const Rdb_field_encoder *const field_dec = it->m_field_enc;
const bool decode = it->m_decode;
const bool isNull =
field_dec->maybe_null() &&
((null_bytes[field_dec->m_null_offset] & field_dec->m_null_mask) != 0);
Field *const field = table->field[field_dec->m_field_index];
/* Skip the bytes we need to skip */
if (it->m_skip && !reader.read(it->m_skip)) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
uint field_offset = field->ptr - table->record[0];
uint null_offset = field->null_offset();
bool maybe_null = field->real_maybe_null();
field->move_field(buf + field_offset,
maybe_null ? buf + null_offset : nullptr,
field->null_bit);
// WARNING! - Don't return before restoring field->ptr and field->null_ptr!
if (isNull) {
if (decode) {
/* This sets the NULL-bit of this record */
field->set_null();
/*
Besides that, set the field value to default value. CHECKSUM TABLE
depends on this.
*/
memcpy(field->ptr, table->s->default_values + field_offset,
field->pack_length());
}
} else {
if (decode) {
field->set_notnull();
}
if (field_dec->m_field_type == MYSQL_TYPE_BLOB) {
err = convert_blob_from_storage_format(
(my_core::Field_blob *) field, &reader, decode);
} else if (field_dec->m_field_type == MYSQL_TYPE_VARCHAR) {
err = convert_varchar_from_storage_format(
(my_core::Field_varstring *) field, &reader, decode);
} else {
err = convert_field_from_storage_format(
field, &reader, decode, field_dec->m_pack_length_in_rec);
}
}
// Restore field->ptr and field->null_ptr
field->move_field(table->record[0] + field_offset,
maybe_null ? table->record[0] + null_offset : nullptr,
field->null_bit);
if (err != HA_EXIT_SUCCESS) {
return err;
}
}
if (m_verify_row_debug_checksums) {
if (reader.remaining_bytes() == RDB_CHECKSUM_CHUNK_SIZE &&
reader.read(1)[0] == RDB_CHECKSUM_DATA_TAG) {
uint32_t stored_key_chksum =
rdb_netbuf_to_uint32((const uchar *)reader.read(RDB_CHECKSUM_SIZE));
uint32_t stored_val_chksum =
rdb_netbuf_to_uint32((const uchar *)reader.read(RDB_CHECKSUM_SIZE));
const uint32_t computed_key_chksum =
my_core::crc32(0, rdb_slice_to_uchar_ptr(key), key->size());
const uint32_t computed_val_chksum =
my_core::crc32(0, rdb_slice_to_uchar_ptr(value),
value->size() - RDB_CHECKSUM_CHUNK_SIZE);
DBUG_EXECUTE_IF("myrocks_simulate_bad_pk_checksum1",
stored_key_chksum++;);
if (stored_key_chksum != computed_key_chksum) {
m_pk_descr->report_checksum_mismatch(true, key->data(), key->size());
return HA_ERR_ROCKSDB_CHECKSUM_MISMATCH;
}
DBUG_EXECUTE_IF("myrocks_simulate_bad_pk_checksum2",
stored_val_chksum++;);
if (stored_val_chksum != computed_val_chksum) {
m_pk_descr->report_checksum_mismatch(false, value->data(),
value->size());
return HA_ERR_ROCKSDB_CHECKSUM_MISMATCH;
}
m_row_checksums_checked++;
}
if (reader.remaining_bytes())
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
return HA_EXIT_SUCCESS;
}
void ha_rocksdb::get_storage_type(Rdb_field_encoder *const encoder,
const uint &kp) {
// STORE_SOME uses unpack_info.
if (m_pk_descr->has_unpack_info(kp)) {
DBUG_ASSERT(m_pk_descr->can_unpack(kp));
encoder->m_storage_type = Rdb_field_encoder::STORE_SOME;
m_maybe_unpack_info = true;
} else if (m_pk_descr->can_unpack(kp)) {
encoder->m_storage_type = Rdb_field_encoder::STORE_NONE;
}
}
/*
Setup data needed to convert table->record[] to and from record storage
format.
@seealso
ha_rocksdb::convert_record_to_storage_format,
ha_rocksdb::convert_record_from_storage_format
*/
void ha_rocksdb::setup_field_converters() {
uint i;
uint null_bytes = 0;
uchar cur_null_mask = 0x1;
DBUG_ASSERT(m_encoder_arr == nullptr);
m_encoder_arr = static_cast<Rdb_field_encoder *>(
my_malloc(table->s->fields * sizeof(Rdb_field_encoder), MYF(0)));
if (m_encoder_arr == nullptr) {
return;
}
for (i = 0; i < table->s->fields; i++) {
Field *const field = table->field[i];
m_encoder_arr[i].m_storage_type = Rdb_field_encoder::STORE_ALL;
/*
Check if this field is
- a part of primary key, and
- it can be decoded back from its key image.
If both hold, we don't need to store this field in the value part of
RocksDB's key-value pair.
If hidden pk exists, we skip this check since the field will never be
part of the hidden pk.
*/
if (!has_hidden_pk(table)) {
KEY *const pk_info = &table->key_info[table->s->primary_key];
for (uint kp = 0; kp < pk_info->user_defined_key_parts; kp++) {
/* key_part->fieldnr is counted from 1 */
if (field->field_index + 1 == pk_info->key_part[kp].fieldnr) {
get_storage_type(&m_encoder_arr[i], kp);
break;
}
}
}
m_encoder_arr[i].m_field_type = field->real_type();
m_encoder_arr[i].m_field_index = i;
m_encoder_arr[i].m_pack_length_in_rec = field->pack_length_in_rec();
if (field->real_maybe_null()) {
m_encoder_arr[i].m_null_mask = cur_null_mask;
m_encoder_arr[i].m_null_offset = null_bytes;
if (cur_null_mask == 0x80) {
cur_null_mask = 0x1;
null_bytes++;
} else
cur_null_mask = cur_null_mask << 1;
} else {
m_encoder_arr[i].m_null_mask = 0;
}
}
/* Count the last, unfinished NULL-bits byte */
if (cur_null_mask != 0x1)
null_bytes++;
m_null_bytes_in_rec = null_bytes;
}
int ha_rocksdb::alloc_key_buffers(const TABLE *const table_arg,
const Rdb_tbl_def *const tbl_def_arg,
bool alloc_alter_buffers) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(m_pk_tuple == nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
std::shared_ptr<Rdb_key_def> *const kd_arr = tbl_def_arg->m_key_descr_arr;
uint key_len = 0;
uint max_packed_sk_len = 0;
uint pack_key_len = 0;
m_pk_descr = kd_arr[pk_index(table_arg, tbl_def_arg)];
if (has_hidden_pk(table_arg)) {
m_pk_key_parts = 1;
} else {
m_pk_key_parts =
table->key_info[table->s->primary_key].user_defined_key_parts;
key_len = table->key_info[table->s->primary_key].key_length;
}
// move this into get_table_handler() ??
m_pk_descr->setup(table_arg, tbl_def_arg);
m_pk_tuple = reinterpret_cast<uchar *>(my_malloc(key_len, MYF(0)));
pack_key_len = m_pk_descr->max_storage_fmt_length();
m_pk_packed_tuple =
reinterpret_cast<uchar *>(my_malloc(pack_key_len, MYF(0)));
/* Sometimes, we may use m_sk_packed_tuple for storing packed PK */
max_packed_sk_len = pack_key_len;
for (uint i = 0; i < table_arg->s->keys; i++) {
if (i == table_arg->s->primary_key) /* Primary key was processed above */
continue;
// TODO: move this into get_table_handler() ??
kd_arr[i]->setup(table_arg, tbl_def_arg);
const uint packed_len = kd_arr[i]->max_storage_fmt_length();
if (packed_len > max_packed_sk_len) {
max_packed_sk_len = packed_len;
}
}
m_sk_packed_tuple =
reinterpret_cast<uchar *>(my_malloc(max_packed_sk_len, MYF(0)));
m_sk_match_prefix_buf =
reinterpret_cast<uchar *>(my_malloc(max_packed_sk_len, MYF(0)));
m_sk_packed_tuple_old =
reinterpret_cast<uchar *>(my_malloc(max_packed_sk_len, MYF(0)));
m_end_key_packed_tuple =
reinterpret_cast<uchar *>(my_malloc(max_packed_sk_len, MYF(0)));
m_pack_buffer =
reinterpret_cast<uchar *>(my_malloc(max_packed_sk_len, MYF(0)));
/*
If inplace alter is happening, allocate special buffers for unique
secondary index duplicate checking.
*/
if (alloc_alter_buffers) {
m_dup_sk_packed_tuple =
reinterpret_cast<uchar *>(my_malloc(max_packed_sk_len, MYF(0)));
m_dup_sk_packed_tuple_old =
reinterpret_cast<uchar *>(my_malloc(max_packed_sk_len, MYF(0)));
}
if (m_pk_tuple == nullptr || m_pk_packed_tuple == nullptr ||
m_sk_packed_tuple == nullptr || m_sk_packed_tuple_old == nullptr ||
m_end_key_packed_tuple == nullptr || m_pack_buffer == nullptr ||
(alloc_alter_buffers && (m_dup_sk_packed_tuple == nullptr ||
m_dup_sk_packed_tuple_old == nullptr))) {
// One or more of the above allocations failed. Clean up and exit
free_key_buffers();
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
void ha_rocksdb::free_key_buffers() {
my_free(m_pk_tuple);
m_pk_tuple = nullptr;
my_free(m_pk_packed_tuple);
m_pk_packed_tuple = nullptr;
my_free(m_sk_packed_tuple);
m_sk_packed_tuple = nullptr;
my_free(m_sk_match_prefix_buf);
m_sk_match_prefix_buf = nullptr;
my_free(m_sk_packed_tuple_old);
m_sk_packed_tuple_old = nullptr;
my_free(m_end_key_packed_tuple);
m_end_key_packed_tuple = nullptr;
my_free(m_pack_buffer);
m_pack_buffer = nullptr;
my_free(m_dup_sk_packed_tuple);
m_dup_sk_packed_tuple = nullptr;
my_free(m_dup_sk_packed_tuple_old);
m_dup_sk_packed_tuple_old = nullptr;
}
#ifdef MARIAROCKS_NOT_YET
void ha_rocksdb::set_use_read_free_rpl(const char *const whitelist) {
DBUG_ASSERT(whitelist != nullptr);
#if defined(HAVE_PSI_INTERFACE)
Regex_list_handler regex_handler(key_rwlock_read_free_rpl_tables);
#else
Regex_list_handler regex_handler;
#endif
if (!regex_handler.set_patterns(whitelist)) {
warn_about_bad_patterns(®ex_handler, "read_free_rpl_tables");
}
m_use_read_free_rpl = regex_handler.matches(m_tbl_def->base_tablename());
}
#endif
void ha_rocksdb::set_skip_unique_check_tables(const char *const whitelist) {
DBUG_ASSERT(whitelist != nullptr);
#if defined(HAVE_PSI_INTERFACE)
Regex_list_handler regex_handler(key_rwlock_skip_unique_check_tables);
#else
Regex_list_handler regex_handler;
#endif
if (!regex_handler.set_patterns(whitelist)) {
warn_about_bad_patterns(®ex_handler, "skip_unique_check_tables");
}
m_skip_unique_check = regex_handler.matches(m_tbl_def->base_tablename());
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::open(const char *const name, int mode, uint test_if_locked) {
DBUG_ENTER_FUNC();
int err = close();
if (err) {
DBUG_RETURN(err);
}
m_table_handler = rdb_open_tables.get_table_handler(name);
if (m_table_handler == nullptr) {
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
}
my_core::thr_lock_data_init(&m_table_handler->m_thr_lock, &m_db_lock,
nullptr);
m_io_perf.init(&m_table_handler->m_table_perf_context,
&m_table_handler->m_io_perf_read,
&m_table_handler->m_io_perf_write, &stats);
Rdb_perf_context_guard guard(&m_io_perf,
rocksdb_perf_context_level(ha_thd()));
std::string fullname;
err = rdb_normalize_tablename(name, &fullname);
if (err != HA_EXIT_SUCCESS) {
DBUG_RETURN(err);
}
m_tbl_def = ddl_manager.find(fullname);
if (m_tbl_def == nullptr) {
my_error(ER_INTERNAL_ERROR, MYF(0),
"Attempt to open a table that is not present in RocksDB-SE data "
"dictionary");
DBUG_RETURN(HA_ERR_ROCKSDB_INVALID_TABLE);
}
m_lock_rows = RDB_LOCK_NONE;
m_key_descr_arr = m_tbl_def->m_key_descr_arr;
/*
Full table scan actually uses primary key
(UPDATE needs to know this, otherwise it will go into infinite loop on
queries like "UPDATE tbl SET pk=pk+100")
*/
key_used_on_scan = table->s->primary_key;
// close() above has already called free_key_buffers(). No need to do it here.
err = alloc_key_buffers(table, m_tbl_def);
if (err) {
DBUG_RETURN(err);
}
/*
init_with_fields() is used to initialize table flags based on the field
definitions in table->field[].
It is called by open_binary_frm(), but that function calls the method for
a temporary ha_rocksdb object which is later destroyed.
If we are here in ::open(), then init_with_fields() has not been called
for this object. Call it ourselves, we want all member variables to be
properly initialized.
*/
init_with_fields();
setup_field_converters();
info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);
/*
The following load_XXX code calls row decode functions, and they do
that without having done ::external_lock() or index_init()/rnd_init().
(Note: this also means we're doing a read when there was no
setup_field_converters() call)
Initialize the necessary variables for them:
*/
m_verify_row_debug_checksums = false;
/* TODO: move the following to where TABLE_SHARE is opened: */
if (table->found_next_number_field)
load_auto_incr_value();
if (has_hidden_pk(table) &&
(err = load_hidden_pk_value()) != HA_EXIT_SUCCESS) {
free_key_buffers();
DBUG_RETURN(err);
}
/* Index block size in MyRocks: used by MySQL in query optimization */
stats.block_size = rocksdb_tbl_options->block_size;
#ifdef MARIAROCKS_NOT_YET // MDEV-10976
/* Determine at open whether we can use Read Free Replication or not */
set_use_read_free_rpl(THDVAR(ha_thd(), read_free_rpl_tables));
#endif
/* Determine at open whether we should skip unique checks for this table */
set_skip_unique_check_tables(THDVAR(ha_thd(), skip_unique_check_tables));
DBUG_RETURN(HA_EXIT_SUCCESS);
}
int ha_rocksdb::close(void) {
DBUG_ENTER_FUNC();
m_pk_descr = nullptr;
m_key_descr_arr = nullptr;
free_key_buffers();
my_free(m_encoder_arr);
m_encoder_arr = nullptr;
if (m_table_handler != nullptr) {
rdb_open_tables.release_table_handler(m_table_handler);
m_table_handler = nullptr;
}
// These are needed to suppress valgrind errors in rocksdb.partition
m_storage_record.free();
m_last_rowkey.free();
m_sk_tails.free();
m_sk_tails_old.free();
m_pk_unpack_info.free();
DBUG_RETURN(HA_EXIT_SUCCESS);
}
static const char *rdb_error_messages[] = {
"Table must have a PRIMARY KEY.",
"Specifying DATA DIRECTORY for an individual table is not supported.",
"Specifying INDEX DIRECTORY for an individual table is not supported.",
"RocksDB commit failed.",
"Failure during bulk load operation.",
"Found data corruption.",
"CRC checksum mismatch.",
"Invalid table.",
"Could not access RocksDB properties.",
"File I/O error during merge/sort operation.",
"RocksDB status: not found.",
"RocksDB status: corruption.",
"RocksDB status: invalid argument.",
"RocksDB status: io error.",
"RocksDB status: no space.",
"RocksDB status: merge in progress.",
"RocksDB status: incomplete.",
"RocksDB status: shutdown in progress.",
"RocksDB status: timed out.",
"RocksDB status: aborted.",
"RocksDB status: lock limit reached.",
"RocksDB status: busy.",
"RocksDB status: deadlock.",
"RocksDB status: expired.",
"RocksDB status: try again.",
};
static_assert((sizeof(rdb_error_messages) / sizeof(rdb_error_messages[0])) ==
((HA_ERR_ROCKSDB_LAST - HA_ERR_ROCKSDB_FIRST) + 1),
"Number of error messages doesn't match number of error codes");
//psergey-merge: do we need this in MariaDB: we have get_error_messages
//below...
#if 0
static const char *rdb_get_error_message(int nr) {
return rdb_error_messages[nr - HA_ERR_ROCKSDB_FIRST];
}
#endif
static const char **rdb_get_error_messages(int nr) { return rdb_error_messages; }
bool ha_rocksdb::get_error_message(const int error, String *const buf) {
DBUG_ENTER_FUNC();
static_assert(HA_ERR_ROCKSDB_LAST > HA_ERR_FIRST,
"HA_ERR_ROCKSDB_LAST > HA_ERR_FIRST");
static_assert(HA_ERR_ROCKSDB_LAST > HA_ERR_LAST,
"HA_ERR_ROCKSDB_LAST > HA_ERR_LAST");
DBUG_ASSERT(buf != nullptr);
if (error == HA_ERR_LOCK_WAIT_TIMEOUT || error == HA_ERR_LOCK_DEADLOCK) {
Rdb_transaction *const tx = get_tx_from_thd(ha_thd());
DBUG_ASSERT(tx != nullptr);
buf->append(tx->m_detailed_error);
DBUG_RETURN(true);
}
if (error >= HA_ERR_ROCKSDB_FIRST && error <= HA_ERR_ROCKSDB_LAST) {
buf->append(rdb_error_messages[error - HA_ERR_ROCKSDB_FIRST]);
}
// We can be called with the values which are < HA_ERR_FIRST because most
// MySQL internal functions will just return HA_EXIT_FAILURE in case of
// an error.
DBUG_RETURN(false);
}
/*
Generalized way to convert RocksDB status errors into MySQL error code, and
print error message.
Each error code below maps to a RocksDB status code found in:
rocksdb/include/rocksdb/status.h
*/
int ha_rocksdb::rdb_error_to_mysql(const rocksdb::Status &s,
const char *opt_msg) {
DBUG_ASSERT(!s.ok());
int err;
switch (s.code()) {
case rocksdb::Status::Code::kOk:
err = HA_EXIT_SUCCESS;
break;
case rocksdb::Status::Code::kNotFound:
err = HA_ERR_ROCKSDB_STATUS_NOT_FOUND;
break;
case rocksdb::Status::Code::kCorruption:
err = HA_ERR_ROCKSDB_STATUS_CORRUPTION;
break;
case rocksdb::Status::Code::kNotSupported:
err = HA_ERR_ROCKSDB_STATUS_NOT_SUPPORTED;
break;
case rocksdb::Status::Code::kInvalidArgument:
err = HA_ERR_ROCKSDB_STATUS_INVALID_ARGUMENT;
break;
case rocksdb::Status::Code::kIOError:
err = (s.IsNoSpace()) ? HA_ERR_ROCKSDB_STATUS_NO_SPACE
: HA_ERR_ROCKSDB_STATUS_IO_ERROR;
break;
case rocksdb::Status::Code::kMergeInProgress:
err = HA_ERR_ROCKSDB_STATUS_MERGE_IN_PROGRESS;
break;
case rocksdb::Status::Code::kIncomplete:
err = HA_ERR_ROCKSDB_STATUS_INCOMPLETE;
break;
case rocksdb::Status::Code::kShutdownInProgress:
err = HA_ERR_ROCKSDB_STATUS_SHUTDOWN_IN_PROGRESS;
break;
case rocksdb::Status::Code::kTimedOut:
err = HA_ERR_ROCKSDB_STATUS_TIMED_OUT;
break;
case rocksdb::Status::Code::kAborted:
err = (s.IsLockLimit()) ? HA_ERR_ROCKSDB_STATUS_LOCK_LIMIT
: HA_ERR_ROCKSDB_STATUS_ABORTED;
break;
case rocksdb::Status::Code::kBusy:
err = (s.IsDeadlock()) ? HA_ERR_ROCKSDB_STATUS_DEADLOCK
: HA_ERR_ROCKSDB_STATUS_BUSY;
break;
case rocksdb::Status::Code::kExpired:
err = HA_ERR_ROCKSDB_STATUS_EXPIRED;
break;
case rocksdb::Status::Code::kTryAgain:
err = HA_ERR_ROCKSDB_STATUS_TRY_AGAIN;
break;
default:
DBUG_ASSERT(0);
return -1;
}
if (opt_msg) {
my_error(ER_RDB_STATUS_MSG, MYF(0), opt_msg, s.code(),
s.ToString().c_str());
} else {
my_error(ER_RDB_STATUS_GENERAL, MYF(0), s.code(), s.ToString().c_str());
}
return err;
}
/* MyRocks supports only the following collations for indexed columns */
static const std::set<uint> RDB_INDEX_COLLATIONS = {
COLLATION_BINARY, COLLATION_UTF8_BIN, COLLATION_LATIN1_BIN};
static bool
rdb_is_index_collation_supported(const my_core::Field *const field) {
const my_core::enum_field_types type = field->real_type();
/* Handle [VAR](CHAR|BINARY) or TEXT|BLOB */
if (type == MYSQL_TYPE_VARCHAR || type == MYSQL_TYPE_STRING ||
type == MYSQL_TYPE_BLOB) {
return RDB_INDEX_COLLATIONS.find(field->charset()->number) !=
RDB_INDEX_COLLATIONS.end();
}
return true;
}
/*
Create structures needed for storing data in rocksdb. This is called when the
table is created. The structures will be shared by all TABLE* objects.
@param
table_arg Table with definition
db_table "dbname.tablename"
len strlen of the above
tbl_def_arg tbl_def whose key_descr is being created/populated
old_tbl_def_arg tbl_def from which keys are being copied over from
(for use during inplace alter)
@return
0 - Ok
other - error, either given table ddl is not supported by rocksdb or OOM.
*/
int ha_rocksdb::create_key_defs(
const TABLE *const table_arg, Rdb_tbl_def *const tbl_def_arg,
const TABLE *const old_table_arg /* = nullptr */,
const Rdb_tbl_def *const old_tbl_def_arg
/* = nullptr */) const {
DBUG_ENTER_FUNC();
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(table_arg->s != nullptr);
uint i;
/*
These need to be one greater than MAX_INDEXES since the user can create
MAX_INDEXES secondary keys and no primary key which would cause us
to generate a hidden one.
*/
std::array<key_def_cf_info, MAX_INDEXES + 1> cfs;
/*
NOTE: All new column families must be created before new index numbers are
allocated to each key definition. See below for more details.
http://github.com/MySQLOnRocksDB/mysql-5.6/issues/86#issuecomment-138515501
*/
if (create_cfs(table_arg, tbl_def_arg, &cfs)) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
if (!old_tbl_def_arg) {
/*
old_tbl_def doesn't exist. this means we are in the process of creating
a new table.
Get the index numbers (this will update the next_index_number)
and create Rdb_key_def structures.
*/
for (i = 0; i < tbl_def_arg->m_key_count; i++) {
if (create_key_def(table_arg, i, tbl_def_arg, &m_key_descr_arr[i],
cfs[i])) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
}
} else {
/*
old_tbl_def exists. This means we are creating a new tbl_def as part of
in-place alter table. Copy over existing keys from the old_tbl_def and
generate the necessary new key definitions if any.
*/
if (create_inplace_key_defs(table_arg, tbl_def_arg, old_table_arg,
old_tbl_def_arg, cfs)) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/*
Checks index parameters and creates column families needed for storing data
in rocksdb if necessary.
@param in
table_arg Table with definition
db_table Table name
tbl_def_arg Table def structure being populated
@param out
cfs CF info for each key definition in 'key_info' order
@return
0 - Ok
other - error
*/
int ha_rocksdb::create_cfs(
const TABLE *const table_arg, Rdb_tbl_def *const tbl_def_arg,
std::array<struct key_def_cf_info, MAX_INDEXES + 1> *const cfs) const {
DBUG_ENTER_FUNC();
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(table_arg->s != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
char tablename_sys[NAME_LEN + 1];
bool tsys_set= false;
/*
The first loop checks the index parameters and creates
column families if necessary.
*/
for (uint i = 0; i < tbl_def_arg->m_key_count; i++) {
rocksdb::ColumnFamilyHandle *cf_handle;
if (rocksdb_strict_collation_check &&
!is_hidden_pk(i, table_arg, tbl_def_arg) &&
tbl_def_arg->base_tablename().find(tmp_file_prefix) != 0) {
if (!tsys_set)
{
tsys_set= true;
my_core::filename_to_tablename(tbl_def_arg->base_tablename().c_str(),
tablename_sys, sizeof(tablename_sys));
}
for (uint part = 0; part < table_arg->key_info[i].ext_key_parts;
part++)
{
if (!rdb_is_index_collation_supported(
table_arg->key_info[i].key_part[part].field) &&
!rdb_collation_exceptions->matches(tablename_sys)) {
std::string collation_err;
for (const auto &coll : RDB_INDEX_COLLATIONS) {
if (collation_err != "") {
collation_err += ", ";
}
collation_err += get_charset_name(coll);
}
my_error(ER_UNSUPPORTED_COLLATION, MYF(0),
tbl_def_arg->full_tablename().c_str(),
table_arg->key_info[i].key_part[part].field->field_name.str,
collation_err.c_str());
DBUG_RETURN(HA_EXIT_FAILURE);
}
}
}
// Internal consistency check to make sure that data in TABLE and
// Rdb_tbl_def structures matches. Either both are missing or both are
// specified. Yes, this is critical enough to make it into SHIP_ASSERT.
SHIP_ASSERT(!table_arg->part_info == tbl_def_arg->base_partition().empty());
// Generate the name for the column family to use.
bool per_part_match_found = false;
std::string cf_name = generate_cf_name(i, table_arg, tbl_def_arg,
&per_part_match_found);
// Prevent create from using the system column family.
if (cf_name == DEFAULT_SYSTEM_CF_NAME) {
my_error(ER_WRONG_ARGUMENTS, MYF(0),
"column family not valid for storing index data.");
DBUG_RETURN(HA_EXIT_FAILURE);
}
// Here's how `get_or_create_cf` will use the input parameters:
//
// `cf_name` - will be used as a CF name.
cf_handle = cf_manager.get_or_create_cf(rdb, cf_name);
if (!cf_handle) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
auto &cf = (*cfs)[i];
cf.cf_handle = cf_handle;
cf.is_reverse_cf = Rdb_cf_manager::is_cf_name_reverse(cf_name.c_str());
cf.is_per_partition_cf = per_part_match_found;
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/*
Create key definition needed for storing data in rocksdb during ADD index
inplace operations.
@param in
table_arg Table with definition
tbl_def_arg New table def structure being populated
old_tbl_def_arg Old(current) table def structure
cfs Struct array which contains column family information
@return
0 - Ok
other - error, either given table ddl is not supported by rocksdb or OOM.
*/
int ha_rocksdb::create_inplace_key_defs(
const TABLE *const table_arg, Rdb_tbl_def *const tbl_def_arg,
const TABLE *const old_table_arg, const Rdb_tbl_def *const old_tbl_def_arg,
const std::array<key_def_cf_info, MAX_INDEXES + 1> &cfs) const {
DBUG_ENTER_FUNC();
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
DBUG_ASSERT(old_tbl_def_arg != nullptr);
std::shared_ptr<Rdb_key_def> *const old_key_descr =
old_tbl_def_arg->m_key_descr_arr;
std::shared_ptr<Rdb_key_def> *const new_key_descr =
tbl_def_arg->m_key_descr_arr;
const std::unordered_map<std::string, uint> old_key_pos =
get_old_key_positions(table_arg, tbl_def_arg, old_table_arg,
old_tbl_def_arg);
uint i;
for (i = 0; i < tbl_def_arg->m_key_count; i++) {
const auto &it = old_key_pos.find(get_key_name(i, table_arg, tbl_def_arg));
if (it != old_key_pos.end()) {
/*
Found matching index in old table definition, so copy it over to the
new one created.
*/
const Rdb_key_def &okd = *old_key_descr[it->second];
const GL_INDEX_ID gl_index_id = okd.get_gl_index_id();
struct Rdb_index_info index_info;
if (!dict_manager.get_index_info(gl_index_id, &index_info)) {
// NO_LINT_DEBUG
sql_print_error("RocksDB: Could not get index information "
"for Index Number (%u,%u), table %s",
gl_index_id.cf_id, gl_index_id.index_id,
old_tbl_def_arg->full_tablename().c_str());
DBUG_RETURN(HA_EXIT_FAILURE);
}
uint32 ttl_rec_offset =
Rdb_key_def::has_index_flag(index_info.m_index_flags,
Rdb_key_def::TTL_FLAG)
? Rdb_key_def::calculate_index_flag_offset(
index_info.m_index_flags, Rdb_key_def::TTL_FLAG)
: UINT_MAX;
/*
We can't use the copy constructor because we need to update the
keynr within the pack_info for each field and the keyno of the keydef
itself.
*/
new_key_descr[i] = std::make_shared<Rdb_key_def>(
okd.get_index_number(), i, okd.get_cf(),
index_info.m_index_dict_version, index_info.m_index_type,
index_info.m_kv_version, okd.m_is_reverse_cf,
okd.m_is_per_partition_cf, okd.m_name.c_str(),
dict_manager.get_stats(gl_index_id), index_info.m_index_flags,
ttl_rec_offset, index_info.m_ttl_duration);
} else if (create_key_def(table_arg, i, tbl_def_arg, &new_key_descr[i],
cfs[i])) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
DBUG_ASSERT(new_key_descr[i] != nullptr);
new_key_descr[i]->setup(table_arg, tbl_def_arg);
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
std::unordered_map<std::string, uint> ha_rocksdb::get_old_key_positions(
const TABLE *const table_arg, const Rdb_tbl_def *const tbl_def_arg,
const TABLE *const old_table_arg,
const Rdb_tbl_def *const old_tbl_def_arg) const {
DBUG_ENTER_FUNC();
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(old_table_arg != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
DBUG_ASSERT(old_tbl_def_arg != nullptr);
std::shared_ptr<Rdb_key_def> *const old_key_descr =
old_tbl_def_arg->m_key_descr_arr;
std::unordered_map<std::string, uint> old_key_pos;
std::unordered_map<std::string, uint> new_key_pos;
uint i;
for (i = 0; i < tbl_def_arg->m_key_count; i++) {
new_key_pos[get_key_name(i, table_arg, tbl_def_arg)] = i;
}
for (i = 0; i < old_tbl_def_arg->m_key_count; i++) {
if (is_hidden_pk(i, old_table_arg, old_tbl_def_arg)) {
old_key_pos[old_key_descr[i]->m_name] = i;
continue;
}
/*
In case of matching key name, need to check key parts of keys as well,
in case a simultaneous drop + add is performed, where the key name is the
same but the key parts are different.
Example:
CREATE TABLE t1 (a INT, b INT, KEY ka(a)) ENGINE=RocksDB;
ALTER TABLE t1 DROP INDEX ka, ADD INDEX ka(b), ALGORITHM=INPLACE;
*/
const KEY *const old_key = &old_table_arg->key_info[i];
const auto &it = new_key_pos.find(old_key->name.str);
if (it == new_key_pos.end()) {
continue;
}
KEY *const new_key = &table_arg->key_info[it->second];
/*
Check that the key is identical between old and new tables.
If not, we still need to create a new index.
The exception is if there is an index changed from unique to non-unique,
in these cases we don't need to rebuild as they are stored the same way in
RocksDB.
*/
bool unique_to_non_unique =
((old_key->flags ^ new_key->flags) == HA_NOSAME) &&
(old_key->flags & HA_NOSAME);
if (compare_keys(old_key, new_key) && !unique_to_non_unique) {
continue;
}
/* Check to make sure key parts match. */
if (compare_key_parts(old_key, new_key)) {
continue;
}
old_key_pos[old_key->name.str] = i;
}
DBUG_RETURN(old_key_pos);
}
/* Check to see if two keys are identical. */
int ha_rocksdb::compare_keys(const KEY *const old_key,
const KEY *const new_key) const {
DBUG_ENTER_FUNC();
DBUG_ASSERT(old_key != nullptr);
DBUG_ASSERT(new_key != nullptr);
/* Check index name. */
if (strcmp(old_key->name.str, new_key->name.str) != 0) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
/* If index algorithms are different then keys are different. */
if (old_key->algorithm != new_key->algorithm) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
/* Check that the key is identical between old and new tables. */
if ((old_key->flags ^ new_key->flags) & HA_KEYFLAG_MASK) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
/* Check index comment. (for column family changes) */
std::string old_comment(old_key->comment.str, old_key->comment.length);
std::string new_comment(new_key->comment.str, new_key->comment.length);
if (old_comment.compare(new_comment) != 0) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/* Check two keys to ensure that key parts within keys match */
int ha_rocksdb::compare_key_parts(const KEY *const old_key,
const KEY *const new_key) const {
DBUG_ENTER_FUNC();
DBUG_ASSERT(old_key != nullptr);
DBUG_ASSERT(new_key != nullptr);
/* Skip if key parts do not match, as it is a different key */
if (new_key->user_defined_key_parts != old_key->user_defined_key_parts) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
/* Check to see that key parts themselves match */
for (uint i = 0; i < old_key->user_defined_key_parts; i++) {
if (strcmp(old_key->key_part[i].field->field_name.str,
new_key->key_part[i].field->field_name.str) != 0) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
/* Check if prefix index key part length has changed */
if (old_key->key_part[i].length != new_key->key_part[i].length) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/*
Create key definition needed for storing data in rocksdb.
This can be called either during CREATE table or doing ADD index operations.
@param in
table_arg Table with definition
i Position of index being created inside table_arg->key_info
tbl_def_arg Table def structure being populated
cf_info Struct which contains column family information
@param out
new_key_def Newly created index definition.
@return
0 - Ok
other - error, either given table ddl is not supported by rocksdb or OOM.
*/
int ha_rocksdb::create_key_def(const TABLE *const table_arg, const uint &i,
const Rdb_tbl_def *const tbl_def_arg,
std::shared_ptr<Rdb_key_def> *const new_key_def,
const struct key_def_cf_info &cf_info) const {
DBUG_ENTER_FUNC();
DBUG_ASSERT(new_key_def != nullptr);
DBUG_ASSERT(*new_key_def == nullptr);
uint64 ttl_duration = 0;
std::string ttl_column;
uint ttl_field_offset;
uint err;
if ((err = Rdb_key_def::extract_ttl_duration(table_arg, tbl_def_arg,
&ttl_duration))) {
DBUG_RETURN(err);
}
if ((err = Rdb_key_def::extract_ttl_col(table_arg, tbl_def_arg, &ttl_column,
&ttl_field_offset))) {
DBUG_RETURN(err);
}
/* We don't currently support TTL on tables with hidden primary keys. */
if (ttl_duration > 0 && is_hidden_pk(i, table_arg, tbl_def_arg)) {
my_error(ER_RDB_TTL_UNSUPPORTED, MYF(0));
DBUG_RETURN(HA_EXIT_FAILURE);
}
/*
If TTL duration is not specified but TTL column was specified, throw an
error because TTL column requires duration.
*/
if (ttl_duration == 0 && !ttl_column.empty()) {
my_error(ER_RDB_TTL_COL_FORMAT, MYF(0), ttl_column.c_str());
DBUG_RETURN(HA_EXIT_FAILURE);
}
const uint index_id = ddl_manager.get_and_update_next_number(&dict_manager);
const uint16_t index_dict_version = Rdb_key_def::INDEX_INFO_VERSION_LATEST;
uchar index_type;
uint16_t kv_version;
if (is_hidden_pk(i, table_arg, tbl_def_arg)) {
index_type = Rdb_key_def::INDEX_TYPE_HIDDEN_PRIMARY;
kv_version = Rdb_key_def::PRIMARY_FORMAT_VERSION_LATEST;
} else if (i == table_arg->s->primary_key) {
index_type = Rdb_key_def::INDEX_TYPE_PRIMARY;
uint16 pk_latest_version = Rdb_key_def::PRIMARY_FORMAT_VERSION_LATEST;
kv_version = pk_latest_version;
} else {
index_type = Rdb_key_def::INDEX_TYPE_SECONDARY;
uint16 sk_latest_version = Rdb_key_def::SECONDARY_FORMAT_VERSION_LATEST;
kv_version = sk_latest_version;
}
// Use PRIMARY_FORMAT_VERSION_UPDATE1 here since it is the same value as
// SECONDARY_FORMAT_VERSION_UPDATE1 so it doesn't matter if this is a
// primary key or secondary key.
DBUG_EXECUTE_IF("MYROCKS_LEGACY_VARBINARY_FORMAT", {
kv_version = Rdb_key_def::PRIMARY_FORMAT_VERSION_UPDATE1;
});
DBUG_EXECUTE_IF("MYROCKS_NO_COVERED_BITMAP_FORMAT", {
if (index_type == Rdb_key_def::INDEX_TYPE_SECONDARY) {
kv_version = Rdb_key_def::SECONDARY_FORMAT_VERSION_UPDATE2;
}
});
uint32 index_flags = (ttl_duration > 0 ? Rdb_key_def::TTL_FLAG : 0);
uint32 ttl_rec_offset =
Rdb_key_def::has_index_flag(index_flags, Rdb_key_def::TTL_FLAG)
? Rdb_key_def::calculate_index_flag_offset(index_flags,
Rdb_key_def::TTL_FLAG)
: UINT_MAX;
const char *const key_name = get_key_name(i, table_arg, m_tbl_def);
*new_key_def = std::make_shared<Rdb_key_def>(
index_id, i, cf_info.cf_handle, index_dict_version, index_type,
kv_version, cf_info.is_reverse_cf, cf_info.is_per_partition_cf, key_name,
Rdb_index_stats(), index_flags, ttl_rec_offset, ttl_duration);
if (!ttl_column.empty()) {
(*new_key_def)->m_ttl_column = ttl_column;
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
int rdb_normalize_tablename(const std::string &tablename,
std::string *const strbuf) {
DBUG_ASSERT(strbuf != nullptr);
if (tablename.size() < 2 || tablename[0] != '.' || tablename[1] != FN_LIBCHAR) {
DBUG_ASSERT(0); // We were not passed table name?
return HA_ERR_ROCKSDB_INVALID_TABLE;
}
size_t pos = tablename.find_first_of(FN_LIBCHAR, 2);
if (pos == std::string::npos) {
DBUG_ASSERT(0); // We were not passed table name?
return HA_ERR_ROCKSDB_INVALID_TABLE;
}
*strbuf = tablename.substr(2, pos - 2) + "." + tablename.substr(pos + 1);
return HA_EXIT_SUCCESS;
}
/*
Check to see if the user's original statement includes foreign key
references
*/
bool ha_rocksdb::contains_foreign_key(THD *const thd) {
bool success;
const char *str = thd_query_string(thd)->str;
DBUG_ASSERT(str != nullptr);
while (*str != '\0') {
// Scan from our current pos looking for 'FOREIGN'
str = rdb_find_in_string(str, "FOREIGN", &success);
if (!success) {
return false;
}
// Skip past the found "FOREIGN'
str = rdb_check_next_token(&my_charset_bin, str, "FOREIGN", &success);
DBUG_ASSERT(success);
if (!my_isspace(&my_charset_bin, *str)) {
return false;
}
// See if the next token is 'KEY'
str = rdb_check_next_token(&my_charset_bin, str, "KEY", &success);
if (!success) {
continue;
}
// See if the next token is '('
str = rdb_check_next_token(&my_charset_bin, str, "(", &success);
if (!success) {
// There is an optional index id after 'FOREIGN KEY', skip it
str = rdb_skip_id(&my_charset_bin, str);
// Now check for '(' again
str = rdb_check_next_token(&my_charset_bin, str, "(", &success);
}
// If we have found 'FOREIGN KEY [<word>] (' we can be confident we have
// a foreign key clause.
return success;
}
// We never found a valid foreign key clause
return false;
}
/**
@brief
splits the normalized table name of <dbname>.<tablename>#P#<part_no> into
the <dbname>, <tablename> and <part_no> components.
@param dbbuf returns database name/table_schema
@param tablebuf returns tablename
@param partitionbuf returns partition suffix if there is one
@return HA_EXIT_SUCCESS on success, non-zero on failure to split
*/
int rdb_split_normalized_tablename(const std::string &fullname,
std::string *const db,
std::string *const table,
std::string *const partition) {
DBUG_ASSERT(!fullname.empty());
#define RDB_PARTITION_STR "#P#"
/* Normalize returns dbname.tablename. */
size_t dotpos = fullname.find('.');
/* Invalid table name? */
if (dotpos == std::string::npos) {
return HA_ERR_ROCKSDB_INVALID_TABLE;
}
// Table must have a database name associated with it.
DBUG_ASSERT(dotpos > 0);
if (db != nullptr) {
*db = fullname.substr(0, dotpos);
}
dotpos++;
const size_t partpos =
fullname.find(RDB_PARTITION_STR, dotpos, strlen(RDB_PARTITION_STR));
if (partpos != std::string::npos) {
DBUG_ASSERT(partpos >= dotpos);
if (table != nullptr) {
*table = fullname.substr(dotpos, partpos - dotpos);
}
if (partition != nullptr) {
*partition = fullname.substr(partpos + strlen(RDB_PARTITION_STR));
}
} else if (table != nullptr) {
*table = fullname.substr(dotpos);
}
return HA_EXIT_SUCCESS;
}
/**
@brief
create() is called to create a database. The variable name will have the name
of the table.
@details
When create() is called you do not need to worry about
opening the table. Also, the .frm file will have already been
created so adjusting create_info is not necessary. You can overwrite
the .frm file at this point if you wish to change the table
definition, but there are no methods currently provided for doing
so.
Called from handle.cc by ha_create_table().
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
@see
ha_create_table() in handle.cc
*/
int ha_rocksdb::create(const char *const name, TABLE *const table_arg,
HA_CREATE_INFO *const create_info) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(create_info != nullptr);
if (create_info->data_file_name) {
// DATA DIRECTORY is used to create tables under a specific location
// outside the MySQL data directory. We don't support this for MyRocks.
// The `rocksdb_datadir` setting should be used to configure RocksDB data
// directory.
print_error(HA_ERR_ROCKSDB_TABLE_DATA_DIRECTORY_NOT_SUPPORTED,
MYF(ME_JUST_WARNING));
DBUG_RETURN(HA_WRONG_CREATE_OPTION);
}
if (create_info->index_file_name) {
// Similar check for INDEX DIRECTORY as well.
print_error(HA_ERR_ROCKSDB_TABLE_INDEX_DIRECTORY_NOT_SUPPORTED,
MYF(ME_JUST_WARNING));
DBUG_RETURN(HA_WRONG_CREATE_OPTION);
}
std::string str;
int err;
THD *const thd = my_core::thd_get_current_thd();
if (get_table_if_exists(name)) {
if (thd->lex->sql_command == SQLCOM_TRUNCATE) {
err = delete_table(name);
if (err != HA_EXIT_SUCCESS) {
DBUG_RETURN(err);
}
} else {
err = rdb_normalize_tablename(name, &str);
if (err != HA_EXIT_SUCCESS) {
DBUG_RETURN(err);
}
my_error(ER_METADATA_INCONSISTENCY, MYF(0), str.c_str(), name);
DBUG_RETURN(HA_ERR_ROCKSDB_CORRUPT_DATA);
}
}
/*
Construct dbname.tablename ourselves, because parititioning
passes strings like "./test/t14#P#p0" for individual partitions,
while table_arg->s->table_name has none of that.
*/
err = rdb_normalize_tablename(name, &str);
if (err != HA_EXIT_SUCCESS) {
DBUG_RETURN(err);
}
if (contains_foreign_key(thd)) {
my_error(ER_NOT_SUPPORTED_YET, MYF(0),
"FOREIGN KEY for the RocksDB storage engine");
DBUG_RETURN(HA_ERR_UNSUPPORTED);
}
const std::unique_ptr<rocksdb::WriteBatch> wb = dict_manager.begin();
rocksdb::WriteBatch *const batch = wb.get();
/* Create table/key descriptions and put them into the data dictionary */
m_tbl_def = new Rdb_tbl_def(str);
uint n_keys = table_arg->s->keys;
/*
If no primary key found, create a hidden PK and place it inside table
definition
*/
if (has_hidden_pk(table_arg)) {
n_keys += 1;
}
m_key_descr_arr = new std::shared_ptr<Rdb_key_def>[n_keys];
m_tbl_def->m_key_count = n_keys;
m_tbl_def->m_key_descr_arr = m_key_descr_arr;
err = create_key_defs(table_arg, m_tbl_def);
if (err != HA_EXIT_SUCCESS) {
goto error;
}
m_pk_descr = m_key_descr_arr[pk_index(table_arg, m_tbl_def)];
dict_manager.lock();
err = ddl_manager.put_and_write(m_tbl_def, batch);
if (err != HA_EXIT_SUCCESS) {
dict_manager.unlock();
goto error;
}
err = dict_manager.commit(batch);
if (err != HA_EXIT_SUCCESS) {
dict_manager.unlock();
goto error;
}
dict_manager.unlock();
if (create_info->auto_increment_value)
m_tbl_def->m_auto_incr_val = create_info->auto_increment_value;
/*
We only support auto_increment at start of the PRIMARY KEY.
*/
// Field *field;
// if ((field= table_arg->next_number_field))
/* TODO mdcallag: disable this for now to let UNIQUE indexes kind of work
if ((field= table_arg->found_next_number_field))
{
int pk= table_arg->s->primary_key;
Field *pk_field= table_arg->key_info[pk].key_part[0].field;
if (field->field_index != pk_field->field_index)
DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
}
*/
DBUG_RETURN(HA_EXIT_SUCCESS);
error:
/* Delete what we have allocated so far */
delete m_tbl_def;
m_tbl_def = nullptr;
m_key_descr_arr = nullptr;
DBUG_RETURN(err);
}
/**
@note
This function is used only when the table has not yet been opened, and
keyread_allowed bitmap doesn't have the correct values yet.
See comment in ha_rocksdb::index_flags() for details.
*/
bool ha_rocksdb::check_keyread_allowed(uint inx, uint part,
bool all_parts) const {
bool res = true;
KEY *const key_info = &table_share->key_info[inx];
Rdb_field_packing dummy1;
res = dummy1.setup(nullptr, key_info->key_part[part].field, inx, part,
key_info->key_part[part].length);
if (res && all_parts) {
for (uint i = 0; i < part; i++) {
Field *field;
if ((field = key_info->key_part[i].field)) {
Rdb_field_packing dummy;
if (!dummy.setup(nullptr, field, inx, i,
key_info->key_part[i].length)) {
/* Cannot do index-only reads for this column */
res = false;
break;
}
}
}
}
const uint pk = table_share->primary_key;
if (inx == pk && all_parts &&
part + 1 == table_share->key_info[pk].user_defined_key_parts) {
m_pk_can_be_decoded = res;
}
return res;
}
int ha_rocksdb::read_key_exact(const Rdb_key_def &kd,
rocksdb::Iterator *const iter,
const bool &full_key_match,
const rocksdb::Slice &key_slice,
const int64_t ttl_filter_ts) {
DBUG_ASSERT(iter != nullptr);
/*
We are looking for the first record such that
index_tuple= lookup_tuple.
lookup_tuple may be a prefix of the index.
*/
rocksdb_smart_seek(kd.m_is_reverse_cf, iter, key_slice);
while (iter->Valid() && kd.value_matches_prefix(iter->key(), key_slice)) {
/*
If TTL is enabled we need to check if the given key has already expired
from the POV of the current transaction. If it has, try going to the next
key.
*/
if (kd.has_ttl() && should_hide_ttl_rec(kd, iter->value(), ttl_filter_ts)) {
rocksdb_smart_next(kd.m_is_reverse_cf, iter);
continue;
}
return HA_EXIT_SUCCESS;
}
/*
Got a record that is not equal to the lookup value, or even a record
from another table.index.
*/
return HA_ERR_KEY_NOT_FOUND;
}
int ha_rocksdb::read_before_key(const Rdb_key_def &kd,
const bool &full_key_match,
const rocksdb::Slice &key_slice,
const int64_t ttl_filter_ts) {
/*
We are looking for record with the biggest t.key such that
t.key < lookup_tuple.
*/
rocksdb_smart_seek(!kd.m_is_reverse_cf, m_scan_it, key_slice);
while (m_scan_it->Valid()) {
/*
We are using full key and we've hit an exact match, or...
If TTL is enabled we need to check if the given key has already expired
from the POV of the current transaction. If it has, try going to the next
key.
*/
if ((full_key_match &&
kd.value_matches_prefix(m_scan_it->key(), key_slice)) ||
(kd.has_ttl() &&
should_hide_ttl_rec(kd, m_scan_it->value(), ttl_filter_ts))) {
rocksdb_smart_next(!kd.m_is_reverse_cf, m_scan_it);
continue;
}
return HA_EXIT_SUCCESS;
}
return HA_ERR_KEY_NOT_FOUND;
}
int ha_rocksdb::read_after_key(const Rdb_key_def &kd,
const rocksdb::Slice &key_slice,
const int64_t ttl_filter_ts) {
/*
We are looking for the first record such that
index_tuple $GT lookup_tuple
with HA_READ_AFTER_KEY, $GT = '>',
with HA_READ_KEY_OR_NEXT, $GT = '>='
*/
rocksdb_smart_seek(kd.m_is_reverse_cf, m_scan_it, key_slice);
/*
If TTL is enabled we need to check if the given key has already expired
from the POV of the current transaction. If it has, try going to the next
key.
*/
while (m_scan_it->Valid() && kd.has_ttl() &&
should_hide_ttl_rec(kd, m_scan_it->value(), ttl_filter_ts)) {
rocksdb_smart_next(kd.m_is_reverse_cf, m_scan_it);
}
return m_scan_it->Valid() ? HA_EXIT_SUCCESS : HA_ERR_KEY_NOT_FOUND;
}
int ha_rocksdb::position_to_correct_key(
const Rdb_key_def &kd, const enum ha_rkey_function &find_flag,
const bool &full_key_match, const uchar *const key,
const key_part_map &keypart_map, const rocksdb::Slice &key_slice,
bool *const move_forward, const int64_t ttl_filter_ts) {
int rc = 0;
*move_forward = true;
switch (find_flag) {
case HA_READ_KEY_EXACT:
rc =
read_key_exact(kd, m_scan_it, full_key_match, key_slice, ttl_filter_ts);
break;
case HA_READ_BEFORE_KEY:
*move_forward = false;
rc = read_before_key(kd, full_key_match, key_slice, ttl_filter_ts);
if (rc == 0 && !kd.covers_key(m_scan_it->key())) {
/* The record we've got is not from this index */
rc = HA_ERR_KEY_NOT_FOUND;
}
break;
case HA_READ_AFTER_KEY:
case HA_READ_KEY_OR_NEXT:
rc = read_after_key(kd, key_slice, ttl_filter_ts);
if (rc == 0 && !kd.covers_key(m_scan_it->key())) {
/* The record we've got is not from this index */
rc = HA_ERR_KEY_NOT_FOUND;
}
break;
case HA_READ_KEY_OR_PREV:
case HA_READ_PREFIX:
/* This flag is not used by the SQL layer, so we don't support it yet. */
rc = HA_ERR_UNSUPPORTED;
break;
case HA_READ_PREFIX_LAST:
case HA_READ_PREFIX_LAST_OR_PREV:
*move_forward = false;
/*
Find the last record with the specified index prefix lookup.
- HA_READ_PREFIX_LAST requires that the record has the
prefix=lookup (if there are no such records,
HA_ERR_KEY_NOT_FOUND should be returned).
- HA_READ_PREFIX_LAST_OR_PREV has no such requirement. If there are no
records with prefix=lookup, we should return the last record
before that.
*/
rc = read_before_key(kd, full_key_match, key_slice, ttl_filter_ts);
if (rc == 0) {
const rocksdb::Slice &rkey = m_scan_it->key();
if (!kd.covers_key(rkey)) {
/* The record we've got is not from this index */
rc = HA_ERR_KEY_NOT_FOUND;
} else if (find_flag == HA_READ_PREFIX_LAST) {
uint size = kd.pack_index_tuple(table, m_pack_buffer, m_sk_packed_tuple,
key, keypart_map);
rocksdb::Slice lookup_tuple(reinterpret_cast<char *>(m_sk_packed_tuple),
size);
// We need to compare the key we've got with the original search prefix.
if (!kd.value_matches_prefix(rkey, lookup_tuple)) {
rc = HA_ERR_KEY_NOT_FOUND;
}
}
}
break;
default:
DBUG_ASSERT(0);
break;
}
return rc;
}
int ha_rocksdb::calc_eq_cond_len(const Rdb_key_def &kd,
const enum ha_rkey_function &find_flag,
const rocksdb::Slice &slice,
const int &bytes_changed_by_succ,
const key_range *const end_key,
uint *const end_key_packed_size) {
if (find_flag == HA_READ_KEY_EXACT)
return slice.size();
if (find_flag == HA_READ_PREFIX_LAST) {
/*
We have made the kd.successor(m_sk_packed_tuple) call above.
The slice is at least Rdb_key_def::INDEX_NUMBER_SIZE bytes long.
*/
return slice.size() - bytes_changed_by_succ;
}
if (end_key) {
*end_key_packed_size =
kd.pack_index_tuple(table, m_pack_buffer, m_end_key_packed_tuple,
end_key->key, end_key->keypart_map);
/*
Calculating length of the equal conditions here. 4 byte index id is
included.
Example1: id1 BIGINT, id2 INT, id3 BIGINT, PRIMARY KEY (id1, id2, id3)
WHERE id1=1 AND id2=1 AND id3>=2 => eq_cond_len= 4+8+4= 16
WHERE id1=1 AND id2>=1 AND id3>=2 => eq_cond_len= 4+8= 12
Example2: id1 VARCHAR(30), id2 INT, PRIMARY KEY (id1, id2)
WHERE id1 = 'AAA' and id2 < 3; => eq_cond_len=13 (varchar used 9 bytes)
*/
rocksdb::Slice end_slice(reinterpret_cast<char *>(m_end_key_packed_tuple),
*end_key_packed_size);
return slice.difference_offset(end_slice);
}
/*
On range scan without any end key condition, there is no
eq cond, and eq cond length is the same as index_id size (4 bytes).
Example1: id1 BIGINT, id2 INT, id3 BIGINT, PRIMARY KEY (id1, id2, id3)
WHERE id1>=1 AND id2 >= 2 and id2 <= 5 => eq_cond_len= 4
*/
return Rdb_key_def::INDEX_NUMBER_SIZE;
}
int ha_rocksdb::read_row_from_primary_key(uchar *const buf) {
DBUG_ASSERT(buf != nullptr);
int rc;
const rocksdb::Slice &rkey = m_scan_it->key();
const uint pk_size = rkey.size();
const char *pk_data = rkey.data();
memcpy(m_pk_packed_tuple, pk_data, pk_size);
m_last_rowkey.copy(pk_data, pk_size, &my_charset_bin);
if (m_lock_rows != RDB_LOCK_NONE) {
/* We need to put a lock and re-read */
rc = get_row_by_rowid(buf, m_pk_packed_tuple, pk_size);
} else {
/* Unpack from the row we've read */
const rocksdb::Slice &value = m_scan_it->value();
rc = convert_record_from_storage_format(&rkey, &value, buf);
}
return rc;
}
int ha_rocksdb::read_row_from_secondary_key(uchar *const buf,
const Rdb_key_def &kd,
bool move_forward) {
DBUG_ASSERT(buf != nullptr);
int rc = 0;
uint pk_size;
/* Get the key columns and primary key value */
const rocksdb::Slice &rkey = m_scan_it->key();
const rocksdb::Slice &value = m_scan_it->value();
bool covered_lookup =
m_keyread_only || kd.covers_lookup(table, &value, &m_lookup_bitmap);
if (covered_lookup && m_lock_rows == RDB_LOCK_NONE && !has_hidden_pk(table)) {
pk_size =
kd.get_primary_key_tuple(table, *m_pk_descr, &rkey, m_pk_packed_tuple);
if (pk_size == RDB_INVALID_KEY_LEN) {
rc = HA_ERR_ROCKSDB_CORRUPT_DATA;
} else {
rc = kd.unpack_record(table, buf, &rkey, &value,
m_verify_row_debug_checksums);
global_stats.covered_secondary_key_lookups.inc();
}
} else {
if (kd.m_is_reverse_cf)
move_forward = !move_forward;
rc = find_icp_matching_index_rec(move_forward, buf);
if (!rc) {
const rocksdb::Slice &rkey = m_scan_it->key();
pk_size = kd.get_primary_key_tuple(table, *m_pk_descr, &rkey,
m_pk_packed_tuple);
if (pk_size == RDB_INVALID_KEY_LEN) {
rc = HA_ERR_ROCKSDB_CORRUPT_DATA;
} else {
rc = get_row_by_rowid(buf, m_pk_packed_tuple, pk_size);
}
}
}
if (!rc) {
m_last_rowkey.copy((const char *)m_pk_packed_tuple, pk_size,
&my_charset_bin);
}
return rc;
}
/**
@note
The problem with this function is that SQL layer calls it, when
- the table has not been yet opened (no ::open() call done)
- this->table_share already exists, but it is in the process of being
filled, so some of fields are still NULL.
- In particular, table_share->key_info[inx].key_part[] is filled only up
to part #part. Subsequent key parts are not yet filled.
To complicate things further, SQL layer will call index_flags() with
all_parts=TRUE. Essentially, we're asked to provide flags for reading
keyparts whose datatype is not yet known.
We walk around this problem by using check_keyread_allowed(), which uses
table_share object and is careful not to step on unitialized data.
When we get a call with all_parts=TRUE, we try to analyze all parts but
ignore those that have key_part->field==nullptr (these are not initialized
yet).
*/
ulong ha_rocksdb::index_flags(uint inx, uint part, bool all_parts) const {
DBUG_ENTER_FUNC();
ulong base_flags = HA_READ_NEXT | // doesn't seem to be used
HA_READ_ORDER | HA_READ_RANGE | HA_READ_PREV;
if (check_keyread_allowed(inx, part, all_parts))
base_flags |= HA_KEYREAD_ONLY;
if (inx == table_share->primary_key) {
/*
Index-only reads on primary key are the same as table scan for us. Still,
we need to explicitly "allow" them, otherwise SQL layer will miss some
plans.
*/
base_flags |= HA_KEYREAD_ONLY | HA_CLUSTERED_INDEX;
} else {
/*
We can Index Condition Pushdown any key except the primary. With primary
key, we get (pk, record) pair immediately, there is no place to put the
ICP check.
*/
base_flags |= HA_DO_INDEX_COND_PUSHDOWN;
}
DBUG_RETURN(base_flags);
}
/**
@brief
Read next index tuple through the secondary index.
@details
m_scan_it points at the index key-value pair that we should read the (pk,row)
pair for.
*/
int ha_rocksdb::secondary_index_read(const int keyno, uchar *const buf) {
DBUG_ASSERT(buf != nullptr);
DBUG_ASSERT(table != nullptr);
#ifdef MARIAROCKS_NOT_YET
stats.rows_requested++;
#endif
/* Use STATUS_NOT_FOUND when record not found or some error occurred */
table->status = STATUS_NOT_FOUND;
if (m_scan_it->Valid()) {
rocksdb::Slice key = m_scan_it->key();
/* Check if we've ran out of records of this index */
if (m_key_descr_arr[keyno]->covers_key(key)) {
int rc = 0;
// TODO: We could here check if we have ran out of range we're scanning
const uint size = m_key_descr_arr[keyno]->get_primary_key_tuple(
table, *m_pk_descr, &key, m_pk_packed_tuple);
if (size == RDB_INVALID_KEY_LEN) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
m_last_rowkey.copy((const char *)m_pk_packed_tuple, size,
&my_charset_bin);
rocksdb::Slice value = m_scan_it->value();
bool covered_lookup =
m_keyread_only || m_key_descr_arr[keyno]->covers_lookup(
table, &value, &m_lookup_bitmap);
if (covered_lookup && m_lock_rows == RDB_LOCK_NONE &&
!has_hidden_pk(table)) {
rc = m_key_descr_arr[keyno]->unpack_record(
table, buf, &key, &value, m_verify_row_debug_checksums);
global_stats.covered_secondary_key_lookups.inc();
} else {
rc = get_row_by_rowid(buf, m_pk_packed_tuple, size);
}
if (!rc) {
table->status = 0;
#ifdef MARIAROCKS_NOT_YET
stats.rows_read++;
stats.rows_index_next++;
#endif
update_row_stats(ROWS_READ);
}
return rc;
}
}
return HA_ERR_END_OF_FILE;
}
bool ha_rocksdb::is_using_full_key(key_part_map keypart_map,
uint actual_key_parts)
{
return (keypart_map == HA_WHOLE_KEY) ||
(keypart_map == ((key_part_map(1) << actual_key_parts)
- 1));
}
/*
ha_rocksdb::read_range_first overrides handler::read_range_first.
The only difference from handler::read_range_first is that
ha_rocksdb::read_range_first passes end_key to
ha_rocksdb::index_read_map_impl function.
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::read_range_first(const key_range *const start_key,
const key_range *const end_key,
bool eq_range_arg, bool sorted) {
DBUG_ENTER_FUNC();
int result;
eq_range = eq_range_arg;
/* MariaDB: Pass RANGE_SCAN_ASC when we support ICP on backwards scans */
set_end_range(end_key);
range_key_part = table->key_info[active_index].key_part;
if (!start_key) // Read first record
result = ha_index_first(table->record[0]);
else {
#ifdef MARIAROCKS_NOT_YET
if (is_using_prohibited_gap_locks(
is_using_full_unique_key(active_index, start_key->keypart_map,
start_key->flag))) {
DBUG_RETURN(HA_ERR_LOCK_DEADLOCK);
}
#endif
increment_statistics(&SSV::ha_read_key_count);
MYSQL_TABLE_IO_WAIT(m_psi, PSI_TABLE_FETCH_ROW, active_index, 0, {
result =
index_read_map_impl(table->record[0], start_key->key,
start_key->keypart_map, start_key->flag, end_key);
})
}
if (result)
DBUG_RETURN((result == HA_ERR_KEY_NOT_FOUND) ? HA_ERR_END_OF_FILE : result);
if (compare_key(end_range) <= 0) {
DBUG_RETURN(HA_EXIT_SUCCESS);
} else {
/*
The last read row does not fall in the range. So request
storage engine to release row lock if possible.
*/
unlock_row();
DBUG_RETURN(HA_ERR_END_OF_FILE);
}
}
int ha_rocksdb::prepare_index_scan()
{
range_key_part= table->key_info[active_index].key_part;
set_end_range(NULL);
return 0;
}
int ha_rocksdb::prepare_range_scan(const key_range *start_key,
const key_range *end_key)
{
range_key_part= table->key_info[active_index].key_part;
set_end_range(end_key);
return 0;
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::index_read_map(uchar *const buf, const uchar *const key,
key_part_map keypart_map,
enum ha_rkey_function find_flag) {
DBUG_ENTER_FUNC();
DBUG_RETURN(index_read_map_impl(buf, key, keypart_map, find_flag, nullptr));
}
/*
See storage/rocksdb/rocksdb-range-access.txt for description of how MySQL
index navigation commands are converted into RocksDB lookup commands.
This function takes end_key as an argument, and it is set on range scan.
MyRocks needs to decide whether prefix bloom filter can be used or not.
To decide to use prefix bloom filter or not, calculating equal condition
length
is needed. On equal lookups (find_flag == HA_READ_KEY_EXACT), equal
condition length is the same as rocksdb::Slice.size() of the start key.
On range scan, equal condition length is MIN(start_key, end_key) of the
rocksdb::Slice expression.
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::index_read_map_impl(uchar *const buf, const uchar *const key,
key_part_map keypart_map,
enum ha_rkey_function find_flag,
const key_range *end_key) {
DBUG_ENTER_FUNC();
int rc = 0;
const Rdb_key_def &kd = *m_key_descr_arr[active_index];
const uint actual_key_parts = kd.get_key_parts();
bool using_full_key = is_using_full_key(keypart_map, actual_key_parts);
if (!end_key)
end_key = end_range;
/* By default, we don't need the retrieved records to match the prefix */
m_sk_match_prefix = nullptr;
#ifdef MARIAROCKS_NOT_YET
stats.rows_requested++;
#endif
if (active_index == table->s->primary_key && find_flag == HA_READ_KEY_EXACT &&
using_full_key) {
/*
Equality lookup over primary key, using full tuple.
This is a special case, use DB::Get.
*/
const uint size = kd.pack_index_tuple(table, m_pack_buffer,
m_pk_packed_tuple, key, keypart_map);
bool skip_lookup = is_blind_delete_enabled();
rc = get_row_by_rowid(buf, m_pk_packed_tuple, size,
skip_lookup, false);
if (!rc && !skip_lookup) {
#ifdef MARIAROCKS_NOT_YET
stats.rows_read++;
stats.rows_index_first++;
#endif
update_row_stats(ROWS_READ);
}
DBUG_RETURN(rc);
}
/*
Unique secondary index performs lookups without the extended key fields
*/
uint packed_size;
if (active_index != table->s->primary_key &&
table->key_info[active_index].flags & HA_NOSAME &&
find_flag == HA_READ_KEY_EXACT && using_full_key) {
key_part_map tmp_map = (key_part_map(1) << table->key_info[active_index]
.user_defined_key_parts) -
1;
packed_size = kd.pack_index_tuple(table, m_pack_buffer, m_sk_packed_tuple,
key, tmp_map);
if (table->key_info[active_index].user_defined_key_parts !=
kd.get_key_parts())
using_full_key = false;
} else {
packed_size = kd.pack_index_tuple(table, m_pack_buffer, m_sk_packed_tuple,
key, keypart_map);
}
if ((pushed_idx_cond && pushed_idx_cond_keyno == active_index) &&
(find_flag == HA_READ_KEY_EXACT || find_flag == HA_READ_PREFIX_LAST)) {
/*
We are doing a point index lookup, and ICP is enabled. It is possible
that this call will be followed by ha_rocksdb->index_next_same() call.
Do what InnoDB does: save the lookup tuple now. We will need it in
index_next_same/find_icp_matching_index_rec in order to stop scanning
as soon as index record doesn't match the lookup tuple.
When not using ICP, handler::index_next_same() will make sure that rows
that don't match the lookup prefix are not returned.
row matches the lookup prefix.
*/
m_sk_match_prefix = m_sk_match_prefix_buf;
m_sk_match_length = packed_size;
memcpy(m_sk_match_prefix, m_sk_packed_tuple, packed_size);
}
int bytes_changed_by_succ = 0;
if (find_flag == HA_READ_PREFIX_LAST_OR_PREV ||
find_flag == HA_READ_PREFIX_LAST || find_flag == HA_READ_AFTER_KEY) {
/* See below */
bytes_changed_by_succ = kd.successor(m_sk_packed_tuple, packed_size);
}
rocksdb::Slice slice(reinterpret_cast<const char *>(m_sk_packed_tuple),
packed_size);
uint end_key_packed_size = 0;
const uint eq_cond_len =
calc_eq_cond_len(kd, find_flag, slice, bytes_changed_by_succ, end_key,
&end_key_packed_size);
bool use_all_keys = false;
if (find_flag == HA_READ_KEY_EXACT &&
my_count_bits(keypart_map) == kd.get_key_parts())
use_all_keys = true;
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
const bool is_new_snapshot = !tx->has_snapshot();
// Loop as long as we get a deadlock error AND we end up creating the
// snapshot here (i.e. it did not exist prior to this)
for (;;) {
/*
This will open the iterator and position it at a record that's equal or
greater than the lookup tuple.
*/
setup_scan_iterator(kd, &slice, use_all_keys, eq_cond_len);
/*
Once we are positioned on from above, move to the position we really
want: See storage/rocksdb/rocksdb-range-access.txt
*/
bool move_forward;
rc =
position_to_correct_key(kd, find_flag, using_full_key, key, keypart_map,
slice, &move_forward, tx->m_snapshot_timestamp);
if (rc) {
/* This status is returned on any error */
table->status = STATUS_NOT_FOUND;
DBUG_RETURN(rc);
}
m_skip_scan_it_next_call = false;
/*
Now get the data for the row into 'buf'. If we were using a primary key
then we have all the rows we need. For a secondary key we now need to
lookup the primary key.
*/
if (active_index == table->s->primary_key)
rc = read_row_from_primary_key(buf);
else
rc = read_row_from_secondary_key(buf, kd, move_forward);
if (rc != HA_ERR_LOCK_DEADLOCK || !is_new_snapshot)
break; /* Exit the loop */
// release the snapshot and iterator so they will be regenerated
tx->release_snapshot();
release_scan_iterator();
}
if (rc) {
/* the only possible error condition is record-not-found */
table->status = STATUS_NOT_FOUND;
} else {
table->status = 0;
#ifdef MARIAROCKS_NOT_YET
stats.rows_read++;
stats.rows_index_first++;
#endif
update_row_stats(ROWS_READ);
}
DBUG_RETURN(rc);
}
/*
@brief
Scan the secondary index until we find an index record that satisfies ICP
@param move_forward TRUE <=> move m_scan_it forward
FALSE <=> move m_scan_it backward
@param buf Record buffer (must be the same buffer that
pushed index condition points to, in practice
it is table->record[0])
@detail
Move the current iterator m_scan_it until we get an index tuple that
satisfies the pushed Index Condition.
(if there is no pushed index condition, return right away)
@return
0 - Index tuple satisfies ICP, can do index read.
other - error code
*/
int ha_rocksdb::find_icp_matching_index_rec(const bool &move_forward,
uchar *const buf) {
DBUG_ASSERT(buf != nullptr);
if (pushed_idx_cond && pushed_idx_cond_keyno == active_index) {
const Rdb_key_def &kd = *m_key_descr_arr[active_index];
while (1) {
rocksdb_skip_expired_records(kd, m_scan_it, !move_forward);
if (!m_scan_it->Valid()) {
table->status = STATUS_NOT_FOUND;
return HA_ERR_END_OF_FILE;
}
const rocksdb::Slice rkey = m_scan_it->key();
if (!kd.covers_key(rkey)) {
table->status = STATUS_NOT_FOUND;
return HA_ERR_END_OF_FILE;
}
if (m_sk_match_prefix) {
const rocksdb::Slice prefix((const char *)m_sk_match_prefix,
m_sk_match_length);
if (!kd.value_matches_prefix(rkey, prefix)) {
table->status = STATUS_NOT_FOUND;
return HA_ERR_END_OF_FILE;
}
}
const rocksdb::Slice value = m_scan_it->value();
int err = kd.unpack_record(table, buf, &rkey, &value,
m_verify_row_debug_checksums);
if (err != HA_EXIT_SUCCESS) {
return err;
}
const enum icp_result icp_status= handler_index_cond_check(this);
if (icp_status == ICP_NO_MATCH) {
rocksdb_smart_next(!move_forward, m_scan_it);
continue; /* Get the next (or prev) index tuple */
}
else if (icp_status == ICP_OUT_OF_RANGE || icp_status == ICP_ABORTED_BY_USER) {
/* We have walked out of range we are scanning */
table->status = STATUS_NOT_FOUND;
return HA_ERR_END_OF_FILE;
}
else /* icp_status == ICP_MATCH */
{
/* Index Condition is satisfied. We have rc==0, proceed to fetch the
* row. */
break;
}
/*
TODO: should we have this here, or RockDB handles this internally?
if (my_core::thd_killed(current_thd))
{
rc= HA_ERR_INTERNAL_ERROR; // doesn't matter
break;
}
*/
}
}
return HA_EXIT_SUCCESS;
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
/**
@return
HA_ADMIN_OK OK
other HA_ADMIN error code
*/
int ha_rocksdb::check(THD *const thd, HA_CHECK_OPT *const check_opt) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(thd != nullptr);
DBUG_ASSERT(check_opt != nullptr);
const uint pk = pk_index(table, m_tbl_def);
String rowkey_copy;
String sec_key_copy;
const char *const table_name = table->s->table_name.str;
// Only when debugging: don't use snapshot when reading
// Rdb_transaction *tx= get_or_create_tx(table->in_use);
// tx->snapshot= nullptr;
const bool save_verify_row_debug_checksums = m_verify_row_debug_checksums;
m_verify_row_debug_checksums = true;
/* For each secondary index, check that we can get a PK value from it */
// NO_LINT_DEBUG
sql_print_information("CHECKTABLE %s: Checking table %s", table_name,
table_name);
ha_rows UNINIT_VAR(row_checksums_at_start); // set/used iff first_index==true
ha_rows row_checksums = ha_rows(-1);
bool first_index = true;
for (uint keyno = 0; keyno < table->s->keys; keyno++) {
if (keyno != pk) {
extra(HA_EXTRA_KEYREAD);
ha_index_init(keyno, true);
ha_rows rows = 0;
ha_rows checksums = 0;
if (first_index)
row_checksums_at_start = m_row_checksums_checked;
int res;
// NO_LINT_DEBUG
sql_print_information("CHECKTABLE %s: Checking index %s", table_name,
table->key_info[keyno].name);
while (1) {
if (!rows)
res = index_first(table->record[0]);
else
res = index_next(table->record[0]);
if (res == HA_ERR_END_OF_FILE)
break;
if (res) {
// error
// NO_LINT_DEBUG
sql_print_error("CHECKTABLE %s: .. row %lld: index scan error %d",
table_name, rows, res);
goto error;
}
rocksdb::Slice key = m_scan_it->key();
sec_key_copy.copy(key.data(), key.size(), &my_charset_bin);
rowkey_copy.copy(m_last_rowkey.ptr(), m_last_rowkey.length(),
&my_charset_bin);
if (m_key_descr_arr[keyno]->unpack_info_has_checksum(
m_scan_it->value())) {
checksums++;
}
if ((res = get_row_by_rowid(table->record[0], rowkey_copy.ptr(),
rowkey_copy.length()))) {
// NO_LINT_DEBUG
sql_print_error("CHECKTABLE %s: .. row %lld: "
"failed to fetch row by rowid",
table_name, rows);
goto error;
}
longlong hidden_pk_id = 0;
if (has_hidden_pk(table) &&
read_hidden_pk_id_from_rowkey(&hidden_pk_id))
goto error;
/* Check if we get the same PK value */
uint packed_size = m_pk_descr->pack_record(
table, m_pack_buffer, table->record[0], m_pk_packed_tuple, nullptr,
false, hidden_pk_id);
if (packed_size != rowkey_copy.length() ||
memcmp(m_pk_packed_tuple, rowkey_copy.ptr(), packed_size)) {
// NO_LINT_DEBUG
sql_print_error("CHECKTABLE %s: .. row %lld: PK value mismatch",
table_name, rows);
goto print_and_error;
}
/* Check if we get the same secondary key value */
packed_size = m_key_descr_arr[keyno]->pack_record(
table, m_pack_buffer, table->record[0], m_sk_packed_tuple,
&m_sk_tails, false, hidden_pk_id);
if (packed_size != sec_key_copy.length() ||
memcmp(m_sk_packed_tuple, sec_key_copy.ptr(), packed_size)) {
// NO_LINT_DEBUG
sql_print_error("CHECKTABLE %s: .. row %lld: "
"secondary index value mismatch",
table_name, rows);
goto print_and_error;
}
rows++;
continue;
print_and_error : {
std::string buf;
buf = rdb_hexdump(rowkey_copy.ptr(), rowkey_copy.length(),
RDB_MAX_HEXDUMP_LEN);
// NO_LINT_DEBUG
sql_print_error("CHECKTABLE %s: rowkey: %s", table_name, buf.c_str());
buf = rdb_hexdump(m_retrieved_record.data(), m_retrieved_record.size(),
RDB_MAX_HEXDUMP_LEN);
// NO_LINT_DEBUG
sql_print_error("CHECKTABLE %s: record: %s", table_name, buf.c_str());
buf = rdb_hexdump(sec_key_copy.ptr(), sec_key_copy.length(),
RDB_MAX_HEXDUMP_LEN);
// NO_LINT_DEBUG
sql_print_error("CHECKTABLE %s: index: %s", table_name, buf.c_str());
goto error;
}
}
// NO_LINT_DEBUG
sql_print_information("CHECKTABLE %s: ... %lld index entries checked "
"(%lld had checksums)",
table_name, rows, checksums);
if (first_index) {
row_checksums = m_row_checksums_checked - row_checksums_at_start;
first_index = false;
}
ha_index_end();
}
}
if (row_checksums != ha_rows(-1)) {
// NO_LINT_DEBUG
sql_print_information("CHECKTABLE %s: %lld table records had checksums",
table_name, row_checksums);
}
extra(HA_EXTRA_NO_KEYREAD);
m_verify_row_debug_checksums = save_verify_row_debug_checksums;
/*
TODO: we should check also for PK records that are missing in the secondary
indexes.
For that, need to walk through the PK and check that every PK record has a
proper counterpart in each secondary index.
*/
DBUG_RETURN(HA_ADMIN_OK);
error:
m_verify_row_debug_checksums = save_verify_row_debug_checksums;
ha_index_or_rnd_end();
extra(HA_EXTRA_NO_KEYREAD);
DBUG_RETURN(HA_ADMIN_CORRUPT);
}
static void dbug_dump_str(FILE *const out, const char *const str, int len) {
fprintf(out, "\"");
for (int i = 0; i < len; i++) {
if (str[i] > 32)
fprintf(out, "%c", str[i]);
else
fprintf(out, "\\%d", str[i]);
}
fprintf(out, "\"");
}
/*
Debugging help: dump the whole database into a human-readable file.
Usage:
dbug_dump_database(rdb);
*/
void dbug_dump_database(rocksdb::DB *const db) {
FILE *const out = fopen("/tmp/rocksdb.dump", "wt");
if (!out)
return;
rocksdb::Iterator *it = db->NewIterator(rocksdb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) {
rocksdb::Slice key = it->key();
rocksdb::Slice val = it->value();
dbug_dump_str(out, key.data(), key.size());
fprintf(out, " -> ");
dbug_dump_str(out, val.data(), val.size());
fprintf(out, "\n");
}
delete it;
fclose(out);
}
rocksdb::Status ha_rocksdb::get_for_update(
Rdb_transaction *const tx, rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key, std::string *const value) const {
DBUG_ASSERT(m_lock_rows != RDB_LOCK_NONE);
const bool exclusive = m_lock_rows != RDB_LOCK_READ;
rocksdb::Status s = tx->get_for_update(column_family, key, value, exclusive);
// If we have a lock conflict and we are running in READ COMMITTTED mode
// release and reacquire the snapshot and then retry the get_for_update().
if (s.IsBusy() && !s.IsDeadlock() &&
my_core::thd_tx_isolation(ha_thd()) == ISO_READ_COMMITTED) {
tx->release_snapshot();
tx->acquire_snapshot(false);
s = tx->get_for_update(column_family, key, value, exclusive);
}
return s;
}
bool ha_rocksdb::is_blind_delete_enabled()
{
THD *thd = ha_thd();
return (THDVAR(thd, blind_delete_primary_key) &&
thd->lex->sql_command == SQLCOM_DELETE &&
thd->lex->table_count == 1 &&
table->s->keys == 1 &&
!has_hidden_pk(table) &&
!thd->rgi_slave);
}
/*
Given a rowid (i.e. packed PK) as a parameter, get the record.
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::get_row_by_rowid(uchar *const buf, const char *const rowid,
const uint rowid_size, const bool skip_lookup,
const bool skip_ttl_check) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(buf != nullptr);
DBUG_ASSERT(rowid != nullptr);
DBUG_ASSERT(table != nullptr);
int rc;
rocksdb::Slice key_slice(rowid, rowid_size);
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
DBUG_ASSERT(tx != nullptr);
DEBUG_SYNC(ha_thd(), "rocksdb.get_row_by_rowid");
DBUG_EXECUTE_IF("dbug.rocksdb.get_row_by_rowid", {
THD *thd = ha_thd();
const char act[] = "now signal Reached "
"wait_for signal.rocksdb.get_row_by_rowid_let_running";
DBUG_ASSERT(opt_debug_sync_timeout > 0);
DBUG_ASSERT(!debug_sync_set_action(thd, STRING_WITH_LEN(act)));
};);
bool found;
rocksdb::Status s;
/* Pretend row found without looking up */
if (skip_lookup)
{
#ifdef MARIAROCKS_NOT_YET
stats.rows_deleted_blind++;
#endif
update_row_stats(ROWS_DELETED_BLIND);
m_last_rowkey.copy((const char *)rowid, rowid_size, &my_charset_bin);
table->status = 0;
DBUG_RETURN(0);
}
if (m_lock_rows == RDB_LOCK_NONE) {
tx->acquire_snapshot(true);
s = tx->get(m_pk_descr->get_cf(), key_slice, &m_retrieved_record);
} else {
s = get_for_update(tx, m_pk_descr->get_cf(), key_slice,
&m_retrieved_record);
}
if (!s.IsNotFound() && !s.ok()) {
DBUG_RETURN(tx->set_status_error(table->in_use, s, *m_pk_descr, m_tbl_def,
m_table_handler));
}
found = !s.IsNotFound();
table->status = STATUS_NOT_FOUND;
if (found) {
/* If we found the record, but it's expired, pretend we didn't find it. */
if (!skip_ttl_check && m_pk_descr->has_ttl() &&
should_hide_ttl_rec(*m_pk_descr,
rocksdb::Slice(&m_retrieved_record.front(),
m_retrieved_record.size()),
tx->m_snapshot_timestamp)) {
DBUG_RETURN(HA_ERR_KEY_NOT_FOUND);
}
m_last_rowkey.copy((const char *)rowid, rowid_size, &my_charset_bin);
rc = convert_record_from_storage_format(&key_slice, buf);
if (!rc) {
table->status = 0;
}
} else {
/*
Note: we don't need to unlock the row. It is intentional that we keep
locks on rows that don't exist.
*/
rc = HA_ERR_KEY_NOT_FOUND;
}
DBUG_RETURN(rc);
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::index_next(uchar *const buf) {
DBUG_ENTER_FUNC();
bool moves_forward = true;
if (m_key_descr_arr[active_index]->m_is_reverse_cf) {
moves_forward = false;
}
int rc = index_next_with_direction(buf, moves_forward);
if (rc == HA_ERR_KEY_NOT_FOUND)
rc = HA_ERR_END_OF_FILE;
DBUG_RETURN(rc);
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::index_prev(uchar *const buf) {
DBUG_ENTER_FUNC();
bool moves_forward = false;
if (m_key_descr_arr[active_index]->m_is_reverse_cf) {
moves_forward = true;
}
int rc = index_next_with_direction(buf, moves_forward);
if (rc == HA_ERR_KEY_NOT_FOUND)
rc = HA_ERR_END_OF_FILE;
DBUG_RETURN(rc);
}
int ha_rocksdb::index_next_with_direction(uchar *const buf, bool move_forward) {
DBUG_ENTER_FUNC();
int rc;
if (active_index == pk_index(table, m_tbl_def)) {
rc = rnd_next_with_direction(buf, move_forward);
} else {
if (m_skip_scan_it_next_call) {
m_skip_scan_it_next_call = false;
} else {
if (move_forward)
m_scan_it->Next(); /* this call cannot fail */
else
m_scan_it->Prev();
}
rocksdb_skip_expired_records(*m_key_descr_arr[active_index], m_scan_it,
!move_forward);
rc = find_icp_matching_index_rec(move_forward, buf);
if (!rc)
rc = secondary_index_read(active_index, buf);
}
DBUG_RETURN(rc);
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::index_first(uchar *const buf) {
DBUG_ENTER_FUNC();
m_sk_match_prefix = nullptr;
int rc = m_key_descr_arr[active_index]->m_is_reverse_cf
? index_last_intern(buf)
: index_first_intern(buf);
if (rc == HA_ERR_KEY_NOT_FOUND)
rc = HA_ERR_END_OF_FILE;
DBUG_RETURN(rc);
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::index_last(uchar *const buf) {
DBUG_ENTER_FUNC();
m_sk_match_prefix = nullptr;
int rc = m_key_descr_arr[active_index]->m_is_reverse_cf
? index_first_intern(buf)
: index_last_intern(buf);
if (rc == HA_ERR_KEY_NOT_FOUND)
rc = HA_ERR_END_OF_FILE;
DBUG_RETURN(rc);
}
/*
Start scanning from the "first" value.
The 'first' here means "the first from start of the key space".
For reverse-ordered key spaces, the first value will be the biggest, 'zzzz'.
An picture of a forward-ordered keyspace (remember, the keys have form
'indexnr-keyval'. Suppose the index we are at has number n)
(n-1) - ...
( n ) <--- 1. (n) doesn't exist in the db but it would be here.
( n ) - aaa <--- 2. Seek("n") will put us here on the first index
( n ) - bbb record.
( n ) - cc
So, need to do: Seek(n);
A backward-ordered keyspace:
(n+1) - bbb
(n+1) - aaa
(n+1) <--- (n+1) doesn't exist in the db but would be here.
( n ) - ccc <--- 1. We need to be here.
( n ) - bbb
( n ) - aaa
( n )
So, need to: Seek(n+1);
*/
int ha_rocksdb::index_first_intern(uchar *const buf) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(buf != nullptr);
uchar *key;
uint key_size;
int rc;
if (is_pk(active_index, table, m_tbl_def)) {
key = m_pk_packed_tuple;
} else {
key = m_sk_packed_tuple;
}
DBUG_ASSERT(key != nullptr);
const Rdb_key_def &kd = *m_key_descr_arr[active_index];
if (kd.m_is_reverse_cf) {
kd.get_supremum_key(key, &key_size);
} else {
kd.get_infimum_key(key, &key_size);
}
rocksdb::Slice index_key((const char *)key, key_size);
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
DBUG_ASSERT(tx != nullptr);
const bool is_new_snapshot = !tx->has_snapshot();
// Loop as long as we get a deadlock error AND we end up creating the
// snapshot here (i.e. it did not exist prior to this)
for (;;) {
setup_scan_iterator(kd, &index_key, false, Rdb_key_def::INDEX_NUMBER_SIZE);
m_scan_it->Seek(index_key);
m_skip_scan_it_next_call = true;
rc = index_next_with_direction(buf, true);
if (rc != HA_ERR_LOCK_DEADLOCK || !is_new_snapshot)
break; // exit the loop
// release the snapshot and iterator so they will be regenerated
tx->release_snapshot();
release_scan_iterator();
}
if (!rc) {
/*
index_next is always incremented on success, so decrement if it is
index_first instead
*/
#ifdef MARIAROCKS_NOT_YET
stats.rows_index_first++;
stats.rows_index_next--;
#endif
}
DBUG_RETURN(rc);
}
/**
@details
Start scanning from the "last" value
The 'last' here means "the last from start of the key space".
For reverse-ordered key spaces, we will actually read the smallest value.
An picture of a forward-ordered keyspace (remember, the keys have form
'indexnr-keyval'. Suppose the we are at a key that has number n)
(n-1)-something
( n )-aaa
( n )-bbb
( n )-ccc <----------- Need to seek to here.
(n+1) <---- Doesn't exist, but would be here.
(n+1)-smth, or no value at all
RocksDB's Iterator::SeekForPrev($val) seeks to "at $val or last value that's
smaller". We can't seek to "(n)-ccc" directly, because we don't know what
is the value of 'ccc' (the biggest record with prefix (n)). Instead, we seek
to "(n+1)", which is the least possible value that's greater than any value
in index #n.
So, need to: it->SeekForPrev(n+1)
A backward-ordered keyspace:
(n+1)-something
( n ) - ccc
( n ) - bbb
( n ) - aaa <---------------- (*) Need to seek here.
( n ) <--- Doesn't exist, but would be here.
(n-1)-smth, or no value at all
So, need to: it->SeekForPrev(n)
*/
int ha_rocksdb::index_last_intern(uchar *const buf) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(buf != nullptr);
uchar *key;
uint key_size;
int rc;
if (is_pk(active_index, table, m_tbl_def)) {
key = m_pk_packed_tuple;
} else {
key = m_sk_packed_tuple;
}
DBUG_ASSERT(key != nullptr);
const Rdb_key_def &kd = *m_key_descr_arr[active_index];
if (kd.m_is_reverse_cf) {
kd.get_infimum_key(key, &key_size);
} else {
kd.get_supremum_key(key, &key_size);
}
rocksdb::Slice index_key((const char *)key, key_size);
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
DBUG_ASSERT(tx != nullptr);
bool is_new_snapshot = !tx->has_snapshot();
// Loop as long as we get a deadlock error AND we end up creating the
// snapshot here (i.e. it did not exist prior to this)
for (;;) {
setup_scan_iterator(kd, &index_key, false, Rdb_key_def::INDEX_NUMBER_SIZE);
m_scan_it->SeekForPrev(index_key);
m_skip_scan_it_next_call = false;
if (is_pk(active_index, table, m_tbl_def)) {
m_skip_scan_it_next_call = true;
rc = rnd_next_with_direction(buf, false);
} else {
rc = find_icp_matching_index_rec(false /*move_forward*/, buf);
if (!rc)
rc = secondary_index_read(active_index, buf);
}
if (rc != HA_ERR_LOCK_DEADLOCK || !is_new_snapshot)
break; /* exit the loop */
// release the snapshot and iterator so they will be regenerated
tx->release_snapshot();
release_scan_iterator();
}
if (!rc) {
/*
index_next is always incremented on success, so decrement if it is
index_first instead
*/
#ifdef MARIAROCKS_NOT_YET
stats.rows_index_first++;
stats.rows_index_next--;
#endif
}
DBUG_RETURN(rc);
}
void ha_rocksdb::unlock_row() {
DBUG_ENTER_FUNC();
if (m_lock_rows != RDB_LOCK_NONE) {
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
tx->release_lock(m_pk_descr->get_cf(),
std::string(m_last_rowkey.ptr(), m_last_rowkey.length()));
}
DBUG_VOID_RETURN;
}
/*
Returning true if SingleDelete can be used.
- Secondary Indexes can always use SingleDelete.
- If the index is PRIMARY KEY, and if all of the columns of the table
are covered by the PRIMARY KEY, SingleDelete can be used.
*/
bool ha_rocksdb::can_use_single_delete(const uint &index) const {
return (index != pk_index(table, m_tbl_def) ||
(!has_hidden_pk(table) &&
table->key_info[index].ext_key_parts == table->s->fields));
}
bool ha_rocksdb::skip_unique_check() const {
/*
We want to skip unique checks if:
1) bulk_load is on
2) this table is in the whitelist of tables to skip and the replication
lag has reached a large enough value (see unique_check_lag_threshold
and unique_check_lage_reset_threshold)
3) the user set unique_checks option to 0, and the table does not have
any indexes. If the table has secondary keys, then those might becomes
inconsisted/corrupted
*/
return THDVAR(table->in_use, bulk_load) ||
(m_force_skip_unique_check && m_skip_unique_check) ||
(my_core::thd_test_options(table->in_use,
OPTION_RELAXED_UNIQUE_CHECKS) &&
m_tbl_def->m_key_count == 1);
}
#ifdef MARIAROCKS_NOT_YET // MDEV-10975
void ha_rocksdb::set_force_skip_unique_check(bool skip) {
DBUG_ENTER_FUNC();
m_force_skip_unique_check = skip;
DBUG_VOID_RETURN;
}
#endif
bool ha_rocksdb::commit_in_the_middle() {
return THDVAR(table->in_use, bulk_load) ||
THDVAR(table->in_use, commit_in_the_middle);
}
/*
Executing bulk commit if it should.
@retval true if bulk commit failed
@retval false if bulk commit was skipped or succeeded
*/
bool ha_rocksdb::do_bulk_commit(Rdb_transaction *const tx) {
DBUG_ASSERT(tx != nullptr);
return commit_in_the_middle() &&
tx->get_write_count() >= THDVAR(table->in_use, bulk_load_size) &&
tx->flush_batch();
}
/*
If table was created without primary key, SQL layer represents the primary
key number as MAX_INDEXES. Hence, this function returns true if the table
does not contain a primary key. (In which case we generate a hidden
'auto-incremented' pk.)
*/
bool ha_rocksdb::has_hidden_pk(const TABLE *const table) const {
DBUG_ASSERT(table != nullptr);
return Rdb_key_def::table_has_hidden_pk(table);
}
/*
Returns true if given index number is a hidden_pk.
- This is used when a table is created with no primary key.
*/
bool ha_rocksdb::is_hidden_pk(const uint index, const TABLE *const table_arg,
const Rdb_tbl_def *const tbl_def_arg) {
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(table_arg->s != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
return (table_arg->s->primary_key == MAX_INDEXES &&
index == tbl_def_arg->m_key_count - 1);
}
/* Returns index of primary key */
uint ha_rocksdb::pk_index(const TABLE *const table_arg,
const Rdb_tbl_def *const tbl_def_arg) {
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(table_arg->s != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
return table_arg->s->primary_key == MAX_INDEXES ? tbl_def_arg->m_key_count - 1
: table_arg->s->primary_key;
}
/* Returns true if given index number is a primary key */
bool ha_rocksdb::is_pk(const uint index, const TABLE *const table_arg,
const Rdb_tbl_def *const tbl_def_arg) {
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(table_arg->s != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
return index == table_arg->s->primary_key ||
is_hidden_pk(index, table_arg, tbl_def_arg);
}
uint ha_rocksdb::max_supported_key_part_length() const {
DBUG_ENTER_FUNC();
DBUG_RETURN(rocksdb_large_prefix ? MAX_INDEX_COL_LEN_LARGE
: MAX_INDEX_COL_LEN_SMALL);
}
const char *ha_rocksdb::get_key_name(const uint index,
const TABLE *const table_arg,
const Rdb_tbl_def *const tbl_def_arg) {
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
if (is_hidden_pk(index, table_arg, tbl_def_arg)) {
return HIDDEN_PK_NAME;
}
DBUG_ASSERT(table_arg->key_info != nullptr);
DBUG_ASSERT(table_arg->key_info[index].name.str != nullptr);
return table_arg->key_info[index].name.str;
}
const char *ha_rocksdb::get_key_comment(const uint index,
const TABLE *const table_arg,
const Rdb_tbl_def *const tbl_def_arg) {
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
if (is_hidden_pk(index, table_arg, tbl_def_arg)) {
return nullptr;
}
DBUG_ASSERT(table_arg->key_info != nullptr);
return table_arg->key_info[index].comment.str;
}
const std::string ha_rocksdb::generate_cf_name(const uint index,
const TABLE *const table_arg,
const Rdb_tbl_def *const tbl_def_arg,
bool *per_part_match_found) {
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
DBUG_ASSERT(per_part_match_found != nullptr);
// When creating CF-s the caller needs to know if there was a custom CF name
// specified for a given paritition.
*per_part_match_found = false;
// Index comment is used to define the column family name specification(s).
// If there was no comment, we get an emptry string, and it means "use the
// default column family".
const char *const comment = get_key_comment(index, table_arg, tbl_def_arg);
// `get_key_comment` can return `nullptr`, that's why this.
std::string key_comment = comment ? comment : "";
std::string cf_name = Rdb_key_def::parse_comment_for_qualifier(
key_comment, table_arg, tbl_def_arg, per_part_match_found,
RDB_CF_NAME_QUALIFIER);
if (table_arg->part_info != nullptr && !*per_part_match_found) {
// At this point we tried to search for a custom CF name for a partition,
// but none was specified. Therefore default one will be used.
return "";
}
// If we didn't find any partitioned/non-partitioned qualifiers, return the
// comment itself. NOTE: this currently handles returning the cf name
// specified in the index comment in the case of no partitions, which doesn't
// use any qualifiers at the moment. (aka its a special case)
if (cf_name.empty() && !key_comment.empty()) {
return key_comment;
}
return cf_name;
}
const std::string ha_rocksdb::get_table_comment(const TABLE *const table_arg) {
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(table_arg->s != nullptr);
return table_arg->s->comment.str;
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::write_row(uchar *const buf) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(buf != nullptr);
DBUG_ASSERT(buf == table->record[0]);
DBUG_ASSERT(m_lock_rows == RDB_LOCK_WRITE);
/*
Note: "buf == table->record[0]" is copied from innodb. I am not aware of
any use cases where this condition is not true.
*/
if (table->next_number_field && buf == table->record[0]) {
int err;
if ((err = update_auto_increment())) {
DBUG_RETURN(err);
}
}
const int rv = update_write_row(nullptr, buf, skip_unique_check());
if (rv == 0) {
#ifdef MARIAROCKS_NOT_YET
stats.rows_inserted++;
#endif
update_row_stats(ROWS_INSERTED);
}
DBUG_RETURN(rv);
}
/**
Constructing m_last_rowkey (MyRocks key expression) from
before_update|delete image (MySQL row expression).
m_last_rowkey is normally set during lookup phase, such as
rnd_next_with_direction() and rnd_pos(). With Read Free Replication,
these read functions are skipped and update_rows(), delete_rows() are
called without setting m_last_rowkey. This function sets m_last_rowkey
for Read Free Replication.
*/
void ha_rocksdb::set_last_rowkey(const uchar *const old_data) {
#ifdef MARIAROCKS_NOT_YET
if (old_data && use_read_free_rpl()) {
const int old_pk_size = m_pk_descr->pack_record(
table, m_pack_buffer, old_data, m_pk_packed_tuple, nullptr, false);
m_last_rowkey.copy((const char *)m_pk_packed_tuple, old_pk_size,
&my_charset_bin);
}
#endif
}
int ha_rocksdb::get_pk_for_update(struct update_row_info *const row_info) {
int size;
/*
Get new row key for any insert, and any update where the pk is not hidden.
Row key for updates with hidden pk is handled below.
*/
if (!has_hidden_pk(table)) {
row_info->hidden_pk_id = 0;
row_info->new_pk_unpack_info = &m_pk_unpack_info;
size =
m_pk_descr->pack_record(table, m_pack_buffer, row_info->new_data,
m_pk_packed_tuple, row_info->new_pk_unpack_info,
false, 0, 0, nullptr, &row_info->ttl_pk_offset);
} else if (row_info->old_data == nullptr) {
row_info->hidden_pk_id = update_hidden_pk_val();
size =
m_pk_descr->pack_hidden_pk(row_info->hidden_pk_id, m_pk_packed_tuple);
} else {
/*
If hidden primary key, rowkey for new record will always be the same as
before
*/
size = row_info->old_pk_slice.size();
memcpy(m_pk_packed_tuple, row_info->old_pk_slice.data(), size);
int err = read_hidden_pk_id_from_rowkey(&row_info->hidden_pk_id);
if (err) {
return err;
}
}
row_info->new_pk_slice =
rocksdb::Slice((const char *)m_pk_packed_tuple, size);
return HA_EXIT_SUCCESS;
}
int ha_rocksdb::check_and_lock_unique_pk(const uint &key_id,
const struct update_row_info &row_info,
bool *const found,
bool *const pk_changed) {
DBUG_ASSERT(found != nullptr);
DBUG_ASSERT(pk_changed != nullptr);
*pk_changed = false;
/*
For UPDATEs, if the key has changed, we need to obtain a lock. INSERTs
always require locking.
*/
if (row_info.old_pk_slice.size() > 0) {
/*
If the keys are the same, then no lock is needed
*/
if (!Rdb_pk_comparator::bytewise_compare(row_info.new_pk_slice,
row_info.old_pk_slice)) {
*found = false;
return HA_EXIT_SUCCESS;
}
*pk_changed = true;
}
/*
Perform a read to determine if a duplicate entry exists. For primary
keys, a point lookup will be sufficient.
note: we intentionally don't set options.snapshot here. We want to read
the latest committed data.
*/
/*
To prevent race conditions like below, it is necessary to
take a lock for a target row. get_for_update() holds a gap lock if
target key does not exist, so below conditions should never
happen.
1) T1 Get(empty) -> T2 Get(empty) -> T1 Put(insert) -> T1 commit
-> T2 Put(overwrite) -> T2 commit
2) T1 Get(empty) -> T1 Put(insert, not committed yet) -> T2 Get(empty)
-> T2 Put(insert, blocked) -> T1 commit -> T2 commit(overwrite)
*/
const rocksdb::Status s =
get_for_update(row_info.tx, m_pk_descr->get_cf(), row_info.new_pk_slice,
&m_retrieved_record);
if (!s.ok() && !s.IsNotFound()) {
return row_info.tx->set_status_error(
table->in_use, s, *m_key_descr_arr[key_id], m_tbl_def, m_table_handler);
}
*found = !s.IsNotFound();
return HA_EXIT_SUCCESS;
}
int ha_rocksdb::check_and_lock_sk(const uint &key_id,
const struct update_row_info &row_info,
bool *const found) {
DBUG_ASSERT(found != nullptr);
*found = false;
/*
Can skip checking this key if none of the key fields have changed.
*/
if (row_info.old_data != nullptr && !m_update_scope.is_set(key_id)) {
return HA_EXIT_SUCCESS;
}
KEY *key_info = nullptr;
uint n_null_fields = 0;
uint user_defined_key_parts = 1;
key_info = &table->key_info[key_id];
user_defined_key_parts = key_info->user_defined_key_parts;
/*
If there are no uniqueness requirements, there's no need to obtain a
lock for this key.
*/
if (!(key_info->flags & HA_NOSAME)) {
return HA_EXIT_SUCCESS;
}
const Rdb_key_def &kd = *m_key_descr_arr[key_id];
/*
Calculate the new key for obtaining the lock
For unique secondary indexes, the key used for locking does not
include the extended fields.
*/
int size =
kd.pack_record(table, m_pack_buffer, row_info.new_data, m_sk_packed_tuple,
nullptr, false, 0, user_defined_key_parts, &n_null_fields);
if (n_null_fields > 0) {
/*
If any fields are marked as NULL this will never match another row as
to NULL never matches anything else including another NULL.
*/
return HA_EXIT_SUCCESS;
}
const rocksdb::Slice new_slice =
rocksdb::Slice((const char *)m_sk_packed_tuple, size);
/*
For UPDATEs, if the key has changed, we need to obtain a lock. INSERTs
always require locking.
*/
if (row_info.old_data != nullptr) {
size = kd.pack_record(table, m_pack_buffer, row_info.old_data,
m_sk_packed_tuple_old, nullptr, false, 0,
user_defined_key_parts);
const rocksdb::Slice old_slice =
rocksdb::Slice((const char *)m_sk_packed_tuple_old, size);
/*
For updates, if the keys are the same, then no lock is needed
Also check to see if the key has any fields set to NULL. If it does, then
this key is unique since NULL is not equal to each other, so no lock is
needed.
*/
if (!Rdb_pk_comparator::bytewise_compare(new_slice, old_slice)) {
return HA_EXIT_SUCCESS;
}
}
/*
Perform a read to determine if a duplicate entry exists - since this is
a secondary indexes a range scan is needed.
note: we intentionally don't set options.snapshot here. We want to read
the latest committed data.
*/
const bool all_parts_used = (user_defined_key_parts == kd.get_key_parts());
/*
This iterator seems expensive since we need to allocate and free
memory for each unique index.
If this needs to be optimized, for keys without NULL fields, the
extended primary key fields can be migrated to the value portion of the
key. This enables using Get() instead of Seek() as in the primary key
case.
The bloom filter may need to be disabled for this lookup.
*/
const bool total_order_seek = !can_use_bloom_filter(
ha_thd(), kd, new_slice, all_parts_used);
const bool fill_cache = !THDVAR(ha_thd(), skip_fill_cache);
/*
psergey-todo: we just need to take lock, lookups not needed:
*/
std::string dummy_value;
const rocksdb::Status s =
get_for_update(row_info.tx, kd.get_cf(), new_slice, &dummy_value);
if (!s.ok() && !s.IsNotFound()) {
return row_info.tx->set_status_error(table->in_use, s, kd, m_tbl_def,
m_table_handler);
}
rocksdb::Iterator *const iter = row_info.tx->get_iterator(
kd.get_cf(), total_order_seek, fill_cache, true /* read current data */,
false /* acquire snapshot */);
/*
Need to scan the transaction to see if there is a duplicate key.
Also need to scan RocksDB and verify the key has not been deleted
in the transaction.
*/
iter->Seek(new_slice);
*found = !read_key_exact(kd, iter, all_parts_used, new_slice,
row_info.tx->m_snapshot_timestamp);
delete iter;
return HA_EXIT_SUCCESS;
}
int ha_rocksdb::check_uniqueness_and_lock(
const struct update_row_info &row_info, bool *const pk_changed) {
/*
Go through each index and determine if the index has uniqueness
requirements. If it does, then try to obtain a row lock on the new values.
Once all locks have been obtained, then perform the changes needed to
update/insert the row.
*/
for (uint key_id = 0; key_id < m_tbl_def->m_key_count; key_id++) {
bool found;
int rc;
if (is_pk(key_id, table, m_tbl_def)) {
rc = check_and_lock_unique_pk(key_id, row_info, &found, pk_changed);
} else {
rc = check_and_lock_sk(key_id, row_info, &found);
}
if (rc != HA_EXIT_SUCCESS) {
return rc;
}
/*
If the pk key has ttl, we may need to pretend the row wasn't
found if it is already expired. The pk record is read into
m_retrieved_record by check_and_lock_unique_pk().
*/
if (is_pk(key_id, table, m_tbl_def) && found && m_pk_descr->has_ttl() &&
should_hide_ttl_rec(*m_pk_descr,
rocksdb::Slice(&m_retrieved_record.front(),
m_retrieved_record.size()),
(row_info.tx->m_snapshot_timestamp
? row_info.tx->m_snapshot_timestamp
: static_cast<int64_t>(std::time(nullptr))))) {
found = false;
}
if (found) {
/* There is a row with this key already, so error out. */
errkey = key_id;
m_dupp_errkey = errkey;
return HA_ERR_FOUND_DUPP_KEY;
}
}
return HA_EXIT_SUCCESS;
}
int ha_rocksdb::check_duplicate_sk(const TABLE *table_arg,
const Rdb_key_def &index,
const rocksdb::Slice *key,
struct unique_sk_buf_info *sk_info) {
uint n_null_fields = 0;
const rocksdb::Comparator *index_comp = index.get_cf()->GetComparator();
/* Get proper SK buffer. */
uchar *sk_buf = sk_info->swap_and_get_sk_buf();
/* Get memcmp form of sk without extended pk tail */
uint sk_memcmp_size =
index.get_memcmp_sk_parts(table_arg, *key, sk_buf, &n_null_fields);
sk_info->sk_memcmp_key =
rocksdb::Slice(reinterpret_cast<char *>(sk_buf), sk_memcmp_size);
if (sk_info->sk_memcmp_key_old.size() > 0 && n_null_fields == 0 &&
index_comp->Compare(sk_info->sk_memcmp_key, sk_info->sk_memcmp_key_old) ==
0) {
return 1;
}
sk_info->sk_memcmp_key_old = sk_info->sk_memcmp_key;
return 0;
}
int ha_rocksdb::bulk_load_key(Rdb_transaction *const tx, const Rdb_key_def &kd,
const rocksdb::Slice &key,
const rocksdb::Slice &value, bool sort) {
DBUG_ENTER_FUNC();
rocksdb::ColumnFamilyHandle *cf = kd.get_cf();
DBUG_ASSERT(cf != nullptr);
int res = HA_EXIT_SUCCESS;
if (sort) {
GL_INDEX_ID kd_gl_id = kd.get_gl_index_id();
auto it = m_key_merge.find(kd_gl_id);
if (it == m_key_merge.end()) {
m_key_merge.emplace(
std::piecewise_construct, std::make_tuple(kd_gl_id),
std::make_tuple(
thd_rocksdb_tmpdir(), THDVAR(ha_thd(), merge_buf_size),
THDVAR(ha_thd(), merge_combine_read_size),
THDVAR(ha_thd(), merge_tmp_file_removal_delay_ms), cf));
it = m_key_merge.find(kd_gl_id);
if ((res = it->second.init()) != 0) {
DBUG_RETURN(res);
}
if (m_bulk_load_tx == nullptr) {
tx->start_bulk_load(this);
m_bulk_load_tx = tx;
}
}
res = it->second.add(key, value);
} else {
if (!m_sst_info) {
m_sst_info.reset(new Rdb_sst_info(rdb, m_table_handler->m_table_name,
kd.get_name(), cf, *rocksdb_db_options,
THDVAR(ha_thd(), trace_sst_api)));
tx->start_bulk_load(this);
m_bulk_load_tx = tx;
}
DBUG_ASSERT(m_sst_info);
res = m_sst_info->put(key, value);
}
DBUG_RETURN(res);
}
int ha_rocksdb::finalize_bulk_load() {
DBUG_ENTER_FUNC();
DBUG_ASSERT_IMP(!m_key_merge.empty() || m_sst_info,
m_bulk_load_tx != nullptr);
/* Skip if there are no possible ongoing bulk loads */
if (m_key_merge.empty() && !m_sst_info && m_bulk_load_tx == nullptr) {
DBUG_RETURN(HA_EXIT_SUCCESS);
}
int res = HA_EXIT_SUCCESS;
RDB_MUTEX_LOCK_CHECK(m_bulk_load_mutex);
if (m_sst_info) {
res = m_sst_info->commit();
m_sst_info.reset();
}
if (!m_key_merge.empty()) {
rocksdb::Slice merge_key;
rocksdb::Slice merge_val;
for (auto it = m_key_merge.begin(); it != m_key_merge.end(); it++) {
const std::string &index_name =
ddl_manager.safe_find(it->first)->get_name();
Rdb_index_merge &rdb_merge = it->second;
Rdb_sst_info sst_info(rdb, m_table_handler->m_table_name, index_name,
rdb_merge.get_cf(), *rocksdb_db_options,
THDVAR(ha_thd(), trace_sst_api));
while ((res = rdb_merge.next(&merge_key, &merge_val)) == 0) {
if ((res = sst_info.put(merge_key, merge_val)) != 0) {
break;
}
}
// res == -1 => finished ok; res > 0 => error
if (res <= 0) {
if ((res = sst_info.commit()) != 0) {
break;
}
}
}
m_key_merge.clear();
/*
Explicitly tell jemalloc to clean up any unused dirty pages at this point.
See https://reviews.facebook.net/D63723 for more details.
*/
purge_all_jemalloc_arenas();
}
if (m_bulk_load_tx != nullptr) {
m_bulk_load_tx->end_bulk_load(this);
m_bulk_load_tx = nullptr;
}
RDB_MUTEX_UNLOCK_CHECK(m_bulk_load_mutex);
DBUG_RETURN(res);
}
int ha_rocksdb::update_pk(const Rdb_key_def &kd,
const struct update_row_info &row_info,
const bool &pk_changed) {
const uint key_id = kd.get_keyno();
const bool hidden_pk = is_hidden_pk(key_id, table, m_tbl_def);
ulonglong bytes_written = 0;
/*
If the PK has changed, or if this PK uses single deletes and this is an
update, the old key needs to be deleted. In the single delete case, it
might be possible to have this sequence of keys: PUT(X), PUT(X), SD(X),
resulting in the first PUT(X) showing up.
*/
if (!hidden_pk && (pk_changed || ((row_info.old_pk_slice.size() > 0) &&
can_use_single_delete(key_id)))) {
const rocksdb::Status s = delete_or_singledelete(
key_id, row_info.tx, kd.get_cf(), row_info.old_pk_slice);
if (!s.ok()) {
return row_info.tx->set_status_error(table->in_use, s, kd, m_tbl_def,
m_table_handler);
} else {
bytes_written = row_info.old_pk_slice.size();
}
}
if (table->next_number_field) {
update_auto_incr_val();
}
int rc = HA_EXIT_SUCCESS;
rocksdb::Slice value_slice;
/* Prepare the new record to be written into RocksDB */
if ((rc = convert_record_to_storage_format(row_info, &value_slice))) {
return rc;
}
const auto cf = m_pk_descr->get_cf();
if (rocksdb_enable_bulk_load_api && THDVAR(table->in_use, bulk_load) &&
!hidden_pk) {
/*
Write the primary key directly to an SST file using an SstFileWriter
*/
rc = bulk_load_key(row_info.tx, kd, row_info.new_pk_slice, value_slice,
THDVAR(table->in_use, bulk_load_allow_unsorted));
} else if (row_info.skip_unique_check || row_info.tx->m_ddl_transaction) {
/*
It is responsibility of the user to make sure that the data being
inserted doesn't violate any unique keys.
*/
row_info.tx->get_indexed_write_batch()->Put(cf, row_info.new_pk_slice,
value_slice);
} else {
const auto s = row_info.tx->put(cf, row_info.new_pk_slice, value_slice);
if (!s.ok()) {
if (s.IsBusy()) {
errkey = table->s->primary_key;
m_dupp_errkey = errkey;
rc = HA_ERR_FOUND_DUPP_KEY;
} else {
rc = row_info.tx->set_status_error(table->in_use, s, *m_pk_descr,
m_tbl_def, m_table_handler);
}
}
}
if (rc == HA_EXIT_SUCCESS) {
row_info.tx->update_bytes_written(
bytes_written + row_info.new_pk_slice.size() + value_slice.size());
}
return rc;
}
int ha_rocksdb::update_sk(const TABLE *const table_arg, const Rdb_key_def &kd,
const struct update_row_info &row_info) {
int new_packed_size;
int old_packed_size;
rocksdb::Slice new_key_slice;
rocksdb::Slice new_value_slice;
rocksdb::Slice old_key_slice;
const uint key_id = kd.get_keyno();
ulonglong bytes_written = 0;
/*
Can skip updating this key if none of the key fields have changed and, if
this table has TTL, the TTL timestamp has not changed.
*/
if (row_info.old_data != nullptr && !m_update_scope.is_set(key_id) &&
(!kd.has_ttl() || !m_ttl_bytes_updated)) {
return HA_EXIT_SUCCESS;
}
const bool store_row_debug_checksums = should_store_row_debug_checksums();
new_packed_size =
kd.pack_record(table_arg, m_pack_buffer, row_info.new_data,
m_sk_packed_tuple, &m_sk_tails, store_row_debug_checksums,
row_info.hidden_pk_id, 0, nullptr, nullptr, m_ttl_bytes);
if (row_info.old_data != nullptr) {
// The old value
old_packed_size = kd.pack_record(
table_arg, m_pack_buffer, row_info.old_data, m_sk_packed_tuple_old,
&m_sk_tails_old, store_row_debug_checksums, row_info.hidden_pk_id, 0,
nullptr, nullptr, m_ttl_bytes);
/*
Check if we are going to write the same value. This can happen when
one does
UPDATE tbl SET col='foo'
and we are looking at the row that already has col='foo'.
We also need to compare the unpack info. Suppose, the collation is
case-insensitive, and unpack info contains information about whether
the letters were uppercase and lowercase. Then, both 'foo' and 'FOO'
will have the same key value, but different data in unpack_info.
(note: anyone changing bytewise_compare should take this code into
account)
*/
if (old_packed_size == new_packed_size &&
m_sk_tails_old.get_current_pos() == m_sk_tails.get_current_pos() &&
!(kd.has_ttl() && m_ttl_bytes_updated) &&
memcmp(m_sk_packed_tuple_old, m_sk_packed_tuple, old_packed_size) ==
0 &&
memcmp(m_sk_tails_old.ptr(), m_sk_tails.ptr(),
m_sk_tails.get_current_pos()) == 0) {
return HA_EXIT_SUCCESS;
}
/*
Deleting entries from secondary index should skip locking, but
be visible to the transaction.
(also note that DDL statements do not delete rows, so this is not a DDL
statement)
*/
old_key_slice = rocksdb::Slice(
reinterpret_cast<const char *>(m_sk_packed_tuple_old), old_packed_size);
row_info.tx->get_indexed_write_batch()->SingleDelete(kd.get_cf(),
old_key_slice);
bytes_written = old_key_slice.size();
}
new_key_slice = rocksdb::Slice(
reinterpret_cast<const char *>(m_sk_packed_tuple), new_packed_size);
new_value_slice =
rocksdb::Slice(reinterpret_cast<const char *>(m_sk_tails.ptr()),
m_sk_tails.get_current_pos());
row_info.tx->get_indexed_write_batch()->Put(kd.get_cf(), new_key_slice,
new_value_slice);
row_info.tx->update_bytes_written(bytes_written + new_key_slice.size() +
new_value_slice.size());
return HA_EXIT_SUCCESS;
}
int ha_rocksdb::update_indexes(const struct update_row_info &row_info,
const bool &pk_changed) {
int rc;
// The PK must be updated first to pull out the TTL value.
rc = update_pk(*m_pk_descr, row_info, pk_changed);
if (rc != HA_EXIT_SUCCESS) {
return rc;
}
// Update the remaining indexes.
for (uint key_id = 0; key_id < m_tbl_def->m_key_count; key_id++) {
if (is_pk(key_id, table, m_tbl_def)) {
continue;
}
rc = update_sk(table, *m_key_descr_arr[key_id], row_info);
if (rc != HA_EXIT_SUCCESS) {
return rc;
}
}
return HA_EXIT_SUCCESS;
}
int ha_rocksdb::update_write_row(const uchar *const old_data,
const uchar *const new_data,
const bool skip_unique_check) {
DBUG_ENTER_FUNC();
bool pk_changed = false;
struct update_row_info row_info;
row_info.old_data = old_data;
row_info.new_data = new_data;
row_info.skip_unique_check = skip_unique_check;
row_info.new_pk_unpack_info = nullptr;
set_last_rowkey(old_data);
row_info.tx = get_or_create_tx(table->in_use);
if (old_data != nullptr) {
row_info.old_pk_slice =
rocksdb::Slice(m_last_rowkey.ptr(), m_last_rowkey.length());
/* Determine which indexes need updating. */
calc_updated_indexes();
}
/*
Get the new row key into row_info.new_pk_slice
*/
int rc = get_pk_for_update(&row_info);
if (rc != HA_EXIT_SUCCESS) {
DBUG_RETURN(rc);
}
if (!skip_unique_check) {
/*
Check to see if we are going to have failures because of unique
keys. Also lock the appropriate key values.
*/
rc = check_uniqueness_and_lock(row_info, &pk_changed);
if (rc != HA_EXIT_SUCCESS) {
DBUG_RETURN(rc);
}
}
DEBUG_SYNC(ha_thd(), "rocksdb.update_write_row_after_unique_check");
/*
At this point, all locks have been obtained, and all checks for duplicate
keys have been performed. No further errors can be allowed to occur from
here because updates to the transaction will be made and those updates
cannot be easily removed without rolling back the entire transaction.
*/
rc = update_indexes(row_info, pk_changed);
if (rc != HA_EXIT_SUCCESS) {
DBUG_RETURN(rc);
}
if (old_data != nullptr) {
row_info.tx->incr_update_count();
} else {
row_info.tx->incr_insert_count();
}
if (do_bulk_commit(row_info.tx)) {
DBUG_RETURN(HA_ERR_ROCKSDB_BULK_LOAD);
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/*
Open a cursor
*/
void ha_rocksdb::setup_scan_iterator(const Rdb_key_def &kd,
rocksdb::Slice *const slice,
const bool use_all_keys,
const uint eq_cond_len) {
DBUG_ASSERT(slice != nullptr);
DBUG_ASSERT(slice->size() >= eq_cond_len);
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
bool skip_bloom = true;
const rocksdb::Slice eq_cond(slice->data(), eq_cond_len);
if (can_use_bloom_filter(ha_thd(), kd, eq_cond, use_all_keys)) {
skip_bloom = false;
}
/*
In some cases, setup_scan_iterator() is called multiple times from
the same query but bloom filter can not always be used.
Suppose the following query example. id2 is VARCHAR(30) and PRIMARY KEY
(id1, id2).
select count(*) from t2 WHERE id1=100 and id2 IN ('00000000000000000000',
'100');
In this case, setup_scan_iterator() is called twice, the first time is for
(id1, id2)=(100, '00000000000000000000') and the second time is for (100,
'100').
If prefix bloom filter length is 24 bytes, prefix bloom filter can be used
for the
first condition but not for the second condition.
If bloom filter condition is changed, currently it is necessary to destroy
and
re-create Iterator.
*/
if (m_scan_it_skips_bloom != skip_bloom) {
release_scan_iterator();
}
/*
SQL layer can call rnd_init() multiple times in a row.
In that case, re-use the iterator, but re-position it at the table start.
*/
if (!m_scan_it) {
const bool fill_cache = !THDVAR(ha_thd(), skip_fill_cache);
if (commit_in_the_middle()) {
DBUG_ASSERT(m_scan_it_snapshot == nullptr);
m_scan_it_snapshot = rdb->GetSnapshot();
auto read_opts = rocksdb::ReadOptions();
read_opts.total_order_seek = true; // TODO: set based on WHERE conditions
read_opts.snapshot = m_scan_it_snapshot;
m_scan_it = rdb->NewIterator(read_opts, kd.get_cf());
} else {
m_scan_it = tx->get_iterator(kd.get_cf(), skip_bloom, fill_cache);
}
m_scan_it_skips_bloom = skip_bloom;
}
}
void ha_rocksdb::release_scan_iterator() {
delete m_scan_it;
m_scan_it = nullptr;
if (m_scan_it_snapshot) {
rdb->ReleaseSnapshot(m_scan_it_snapshot);
m_scan_it_snapshot = nullptr;
}
}
void ha_rocksdb::setup_iterator_for_rnd_scan() {
uint key_size;
if (m_pk_descr->m_is_reverse_cf)
m_pk_descr->get_supremum_key(m_pk_packed_tuple, &key_size);
else
m_pk_descr->get_infimum_key(m_pk_packed_tuple, &key_size);
rocksdb::Slice table_key((const char *)m_pk_packed_tuple, key_size);
setup_scan_iterator(*m_pk_descr, &table_key);
m_scan_it->Seek(table_key);
m_skip_scan_it_next_call = true;
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::rnd_init(bool scan) {
DBUG_ENTER_FUNC();
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
setup_read_decoders();
if (scan) {
m_rnd_scan_is_new_snapshot = !tx->has_snapshot();
setup_iterator_for_rnd_scan();
} else {
/* We don't need any preparations for rnd_pos() calls. */
}
// If m_lock_rows is on then we will be doing a get_for_update when accessing
// the index, so don't acquire the snapshot right away. Otherwise acquire
// the snapshot immediately.
tx->acquire_snapshot(m_lock_rows == RDB_LOCK_NONE);
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::rnd_next(uchar *const buf) {
DBUG_ENTER_FUNC();
int rc;
for (;;) {
rc = rnd_next_with_direction(buf, true);
if (rc != HA_ERR_LOCK_DEADLOCK || !m_rnd_scan_is_new_snapshot)
break; /* exit the loop */
// release the snapshot and iterator and then regenerate them
Rdb_transaction *tx = get_or_create_tx(table->in_use);
tx->release_snapshot();
release_scan_iterator();
setup_iterator_for_rnd_scan();
}
m_rnd_scan_is_new_snapshot = false;
if (rc == HA_ERR_KEY_NOT_FOUND)
rc = HA_ERR_END_OF_FILE;
DBUG_RETURN(rc);
}
/*
See also secondary_index_read().
*/
int ha_rocksdb::rnd_next_with_direction(uchar *const buf, bool move_forward) {
DBUG_ENTER_FUNC();
int rc;
table->status = STATUS_NOT_FOUND;
#ifdef MARIAROCKS_NOT_YET
stats.rows_requested++;
#endif
if (!m_scan_it || !m_scan_it->Valid()) {
/*
We can get here when SQL layer has called
h->index_init(PRIMARY);
h->index_read_map(full index tuple, HA_READ_KEY_EXACT);
In this case, we should return EOF.
*/
DBUG_RETURN(HA_ERR_END_OF_FILE);
}
for (;;) {
if (m_skip_scan_it_next_call) {
m_skip_scan_it_next_call = false;
} else {
if (move_forward)
m_scan_it->Next(); /* this call cannot fail */
else
m_scan_it->Prev(); /* this call cannot fail */
}
if (!m_scan_it->Valid()) {
rc = HA_ERR_END_OF_FILE;
break;
}
/* check if we're out of this table */
const rocksdb::Slice key = m_scan_it->key();
if (!m_pk_descr->covers_key(key)) {
rc = HA_ERR_END_OF_FILE;
break;
}
if (m_lock_rows != RDB_LOCK_NONE) {
/*
Lock the row we've just read.
Now we call get_for_update which will 1) Take a lock and 2) Will fail
if the row was deleted since the snapshot was taken.
*/
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
DEBUG_SYNC(ha_thd(), "rocksdb_concurrent_delete");
if (m_pk_descr->has_ttl() &&
should_hide_ttl_rec(*m_pk_descr, m_scan_it->value(),
tx->m_snapshot_timestamp)) {
continue;
}
const rocksdb::Status s =
get_for_update(tx, m_pk_descr->get_cf(), key, &m_retrieved_record);
if (s.IsNotFound() &&
my_core::thd_tx_isolation(ha_thd()) == ISO_READ_COMMITTED) {
// This occurs if we accessed a row, tried to lock it, failed,
// released and reacquired the snapshot (because of READ COMMITTED
// mode) and the row was deleted by someone else in the meantime.
// If so, we just want to move on to the next row.
continue;
}
if (!s.ok()) {
DBUG_RETURN(tx->set_status_error(table->in_use, s, *m_pk_descr,
m_tbl_def, m_table_handler));
}
// If we called get_for_update() use the value from that call not from
// the iterator as it may be stale since we don't have a snapshot
// when m_lock_rows is not RDB_LOCK_NONE.
m_last_rowkey.copy(key.data(), key.size(), &my_charset_bin);
rc = convert_record_from_storage_format(&key, buf);
} else {
// Use the value from the iterator
rocksdb::Slice value = m_scan_it->value();
if (m_pk_descr->has_ttl() &&
should_hide_ttl_rec(
*m_pk_descr, value,
get_or_create_tx(table->in_use)->m_snapshot_timestamp)) {
continue;
}
m_last_rowkey.copy(key.data(), key.size(), &my_charset_bin);
rc = convert_record_from_storage_format(&key, &value, buf);
}
table->status = 0;
break;
}
if (!rc) {
#ifdef MARIAROCKS_NOT_YET
stats.rows_read++;
stats.rows_index_next++;
#endif
update_row_stats(ROWS_READ);
}
DBUG_RETURN(rc);
}
int ha_rocksdb::rnd_end() {
DBUG_ENTER_FUNC();
release_scan_iterator();
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::index_init(uint idx, bool sorted) {
DBUG_ENTER_FUNC();
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
DBUG_ASSERT(tx != nullptr);
setup_read_decoders();
if (!m_keyread_only) {
m_key_descr_arr[idx]->get_lookup_bitmap(table, &m_lookup_bitmap);
}
// If m_lock_rows is not RDB_LOCK_NONE then we will be doing a get_for_update
// when accessing the index, so don't acquire the snapshot right away.
// Otherwise acquire the snapshot immediately.
tx->acquire_snapshot(m_lock_rows == RDB_LOCK_NONE);
active_index = idx;
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/**
@return
HA_EXIT_SUCCESS OK
*/
int ha_rocksdb::index_end() {
DBUG_ENTER_FUNC();
release_scan_iterator();
bitmap_free(&m_lookup_bitmap);
active_index = MAX_KEY;
in_range_check_pushed_down = FALSE;
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::truncate() {
DBUG_ENTER_FUNC();
DBUG_ASSERT(m_tbl_def != nullptr);
m_tbl_def->m_auto_incr_val = 1;
m_tbl_def->m_hidden_pk_val = 1;
DBUG_RETURN(remove_rows(m_tbl_def));
}
/*
Delete the row we've last read. The row is also passed as parameter.
@detail
The caller guarantees table buf points to the row that was just read.
The row is either table->record[0] or table->record[1].
(Check out InnoDB: row_update_for_mysql() has "UT_NOT_USED(mysql_rec)"
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::delete_row(const uchar *const buf) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(buf != nullptr);
set_last_rowkey(buf);
rocksdb::Slice key_slice(m_last_rowkey.ptr(), m_last_rowkey.length());
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
ulonglong bytes_written = 0;
const uint index = pk_index(table, m_tbl_def);
rocksdb::Status s =
delete_or_singledelete(index, tx, m_pk_descr->get_cf(), key_slice);
if (!s.ok()) {
DBUG_RETURN(tx->set_status_error(table->in_use, s, *m_pk_descr, m_tbl_def,
m_table_handler));
} else {
bytes_written = key_slice.size();
}
longlong hidden_pk_id = 0;
if (m_tbl_def->m_key_count > 1 && has_hidden_pk(table)) {
int err = read_hidden_pk_id_from_rowkey(&hidden_pk_id);
if (err) {
DBUG_RETURN(err);
}
}
// Delete the record for every secondary index
for (uint i = 0; i < m_tbl_def->m_key_count; i++) {
if (!is_pk(i, table, m_tbl_def)) {
int packed_size;
const Rdb_key_def &kd = *m_key_descr_arr[i];
packed_size = kd.pack_record(table, m_pack_buffer, buf, m_sk_packed_tuple,
nullptr, false, hidden_pk_id);
rocksdb::Slice secondary_key_slice(
reinterpret_cast<const char *>(m_sk_packed_tuple), packed_size);
/* Deleting on secondary key doesn't need any locks: */
tx->get_indexed_write_batch()->SingleDelete(kd.get_cf(),
secondary_key_slice);
bytes_written += secondary_key_slice.size();
}
}
tx->incr_delete_count();
if (do_bulk_commit(tx)) {
DBUG_RETURN(HA_ERR_ROCKSDB_BULK_LOAD);
}
#ifdef MARIAROCKS_NOT_YET
stats.rows_deleted++;
#endif
update_row_stats(ROWS_DELETED);
tx->update_bytes_written(bytes_written);
DBUG_RETURN(HA_EXIT_SUCCESS);
}
rocksdb::Status ha_rocksdb::delete_or_singledelete(
uint index, Rdb_transaction *const tx,
rocksdb::ColumnFamilyHandle *const column_family,
const rocksdb::Slice &key) {
if (can_use_single_delete(index))
return tx->single_delete(column_family, key);
return tx->delete_key(column_family, key);
}
void ha_rocksdb::update_stats(void) {
DBUG_ENTER_FUNC();
stats.records = 0;
stats.index_file_length = 0ul;
stats.data_file_length = 0ul;
stats.mean_rec_length = 0;
for (uint i = 0; i < m_tbl_def->m_key_count; i++) {
if (is_pk(i, table, m_tbl_def)) {
stats.data_file_length = m_pk_descr->m_stats.m_actual_disk_size;
stats.records = m_pk_descr->m_stats.m_rows;
} else {
stats.index_file_length += m_key_descr_arr[i]->m_stats.m_actual_disk_size;
}
}
DBUG_VOID_RETURN;
}
/**
@return
HA_EXIT_SUCCESS OK
HA_EXIT_FAILURE Error
*/
int ha_rocksdb::info(uint flag) {
DBUG_ENTER_FUNC();
if (!table)
DBUG_RETURN(HA_EXIT_FAILURE);
if (flag & HA_STATUS_VARIABLE) {
/*
Test only to simulate corrupted stats
*/
DBUG_EXECUTE_IF("myrocks_simulate_negative_stats",
m_pk_descr->m_stats.m_actual_disk_size =
-m_pk_descr->m_stats.m_actual_disk_size;);
update_stats();
/*
If any stats are negative due to bad cached stats, re-run analyze table
and re-retrieve the stats.
*/
if (static_cast<longlong>(stats.data_file_length) < 0 ||
static_cast<longlong>(stats.index_file_length) < 0 ||
static_cast<longlong>(stats.records) < 0) {
if (analyze(nullptr, nullptr)) {
DBUG_RETURN(HA_EXIT_FAILURE);
}
update_stats();
}
// if number of records is hardcoded, we do not want to force computation
// of memtable cardinalities
if (stats.records == 0 ||
(rocksdb_force_compute_memtable_stats &&
rocksdb_debug_optimizer_n_rows == 0))
{
// First, compute SST files stats
uchar buf[Rdb_key_def::INDEX_NUMBER_SIZE * 2];
auto r = get_range(pk_index(table, m_tbl_def), buf);
uint64_t sz = 0;
uint8_t include_flags = rocksdb::DB::INCLUDE_FILES;
// recompute SST files stats only if records count is 0
if (stats.records == 0) {
rdb->GetApproximateSizes(m_pk_descr->get_cf(), &r, 1, &sz,
include_flags);
stats.records+= sz/ROCKSDB_ASSUMED_KEY_VALUE_DISK_SIZE;
stats.data_file_length+= sz;
}
// Second, compute memtable stats. This call is expensive, so cache
// values computed for some time.
uint64_t cachetime = rocksdb_force_compute_memtable_stats_cachetime;
uint64_t time = (cachetime == 0) ? 0 : my_interval_timer() / 1000;
if (cachetime == 0 ||
time > m_table_handler->m_mtcache_last_update + cachetime) {
uint64_t memtableCount;
uint64_t memtableSize;
rdb->GetApproximateMemTableStats(m_pk_descr->get_cf(), r,
&memtableCount, &memtableSize);
// Atomically update all of these fields at the same time
if (cachetime > 0) {
if (m_table_handler->m_mtcache_lock.fetch_add(
1, std::memory_order_acquire) == 0) {
m_table_handler->m_mtcache_count = memtableCount;
m_table_handler->m_mtcache_size = memtableSize;
m_table_handler->m_mtcache_last_update = time;
}
m_table_handler->m_mtcache_lock.fetch_sub(1,
std::memory_order_release);
}
stats.records += memtableCount;
stats.data_file_length += memtableSize;
} else {
// Cached data is still valid, so use it instead
stats.records += m_table_handler->m_mtcache_count;
stats.data_file_length += m_table_handler->m_mtcache_size;
}
if (rocksdb_debug_optimizer_n_rows > 0)
stats.records = rocksdb_debug_optimizer_n_rows;
}
if (stats.records != 0)
stats.mean_rec_length = stats.data_file_length / stats.records;
}
if (flag & HA_STATUS_CONST) {
ref_length = m_pk_descr->max_storage_fmt_length();
// TODO: Needs to reimplement after having real index statistics
for (uint i = 0; i < m_tbl_def->m_key_count; i++) {
if (is_hidden_pk(i, table, m_tbl_def)) {
continue;
}
KEY *const k = &table->key_info[i];
for (uint j = 0; j < k->ext_key_parts; j++) {
const Rdb_index_stats &k_stats = m_key_descr_arr[i]->m_stats;
uint x = k_stats.m_distinct_keys_per_prefix.size() > j &&
k_stats.m_distinct_keys_per_prefix[j] > 0
? k_stats.m_rows / k_stats.m_distinct_keys_per_prefix[j]
: 0;
if (x > stats.records)
x = stats.records;
if ((x == 0 && rocksdb_debug_optimizer_no_zero_cardinality) ||
rocksdb_debug_optimizer_n_rows > 0) {
// Fake cardinality implementation. For example, (idx1, idx2, idx3)
// index
/*
Make MariaRocks behave the same way as MyRocks does:
1. SQL layer thinks that unique secondary indexes are not extended
with PK columns (both in MySQL and MariaDB)
2. MariaDB also thinks that indexes with partially-covered columns
are not extended with PK columns. Use the same number of
keyparts that MyRocks would use.
*/
uint ext_key_parts2;
if (k->flags & HA_NOSAME)
ext_key_parts2= k->ext_key_parts; // This is #1
else
ext_key_parts2= m_key_descr_arr[i]->get_key_parts(); // This is #2.
// will have rec_per_key for (idx1)=4, (idx1,2)=2, and (idx1,2,3)=1.
// rec_per_key for the whole index is 1, and multiplied by 2^n if
// n suffix columns of the index are not used.
x = 1 << (ext_key_parts2 - j - 1);
}
k->rec_per_key[j] = x;
}
}
}
if (flag & HA_STATUS_ERRKEY) {
/*
Currently we support only primary keys so we know which key had a
uniqueness violation.
*/
errkey = m_dupp_errkey;
dup_ref = m_pk_tuple; // TODO(?): this should store packed PK.
}
if (flag & HA_STATUS_AUTO) {
stats.auto_increment_value = m_tbl_def->m_auto_incr_val;
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
void ha_rocksdb::position(const uchar *const record) {
DBUG_ENTER_FUNC();
longlong hidden_pk_id = 0;
if (has_hidden_pk(table) && read_hidden_pk_id_from_rowkey(&hidden_pk_id))
DBUG_ASSERT(false); // should never reach here
/*
Get packed primary key value from the record.
(Note: m_last_rowkey has the packed PK of last-read-row, which allows to
handle most cases, but there is an exception to this: when slave applies
RBR events, it fills the record and then calls position(); rnd_pos())
Also note that we *can* rely on PK column values being available. This is
because table_flags() includes HA_PRIMARY_KEY_REQUIRED_FOR_POSITION bit.
When that is true, table->prepare_for_position() adds PK columns into the
read set (this may potentially disable index-only access when PK column
cannot be restored from its mem-comparable form in the secondary indexes).
*/
const uint packed_size = m_pk_descr->pack_record(
table, m_pack_buffer, record, ref, nullptr, false, hidden_pk_id);
/*
It could be that mem-comparable form of PK occupies less than ref_length
bytes. Fill the remainder with zeros.
*/
if (ref_length > packed_size)
memset(ref + packed_size, 0, ref_length - packed_size);
DBUG_VOID_RETURN;
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::rnd_pos(uchar *const buf, uchar *const pos) {
DBUG_ENTER_FUNC();
int rc;
size_t len;
#ifdef MARIAROCKS_NOT_YET
stats.rows_requested++;
#endif
len = m_pk_descr->key_length(table,
rocksdb::Slice((const char *)pos, ref_length));
if (len == size_t(-1)) {
DBUG_RETURN(HA_ERR_ROCKSDB_CORRUPT_DATA); /* Data corruption? */
}
rc = get_row_by_rowid(buf, pos, len);
if (!rc) {
#ifdef MARIAROCKS_NOT_YET
stats.rows_read++;
#endif
update_row_stats(ROWS_READ);
}
DBUG_RETURN(rc);
}
/*
@brief
Calculate (if needed) the bitmap of indexes that are modified by the
current query
@detail
The calculation done by checking index definitions against the
table->write_set
column bitmap.
*/
void ha_rocksdb::calc_updated_indexes() {
if (!m_update_scope_is_valid) {
m_update_scope_is_valid = true;
m_update_scope.clear_all();
for (uint keynr = 0; keynr < table->s->keys; keynr++) {
const Rdb_key_def &kd = *m_key_descr_arr[keynr];
/* Walk over all key parts, including the "extended key" suffix */
const uint key_parts = kd.get_key_parts();
for (uint kp = 0; kp < key_parts; kp++) {
if (has_hidden_pk(table) && kp + 1 == key_parts)
break;
Field *const field = kd.get_table_field_for_part_no(table, kp);
if (bitmap_is_set(table->write_set, field->field_index)) {
m_update_scope.set_bit(keynr);
break;
}
}
}
}
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::update_row(const uchar *const old_data,
const uchar *const new_data) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(old_data != nullptr);
DBUG_ASSERT(new_data != nullptr);
DBUG_ASSERT(m_lock_rows == RDB_LOCK_WRITE);
/*
old_data points to record we're updating. It is the same as the record
we've just read (for multi-table UPDATE, too, because SQL layer will make
an rnd_pos() call to re-read the record before calling update_row())
*/
DBUG_ASSERT(new_data == table->record[0]);
const int rv = update_write_row(old_data, new_data, false);
if (rv == 0) {
#ifdef MARIAROCKS_NOT_YET
stats.rows_updated++;
#endif
update_row_stats(ROWS_UPDATED);
}
DBUG_RETURN(rv);
}
/*
MariaDB's temporary: MyRocks has this function in sql/handler.cc:
*/
bool can_hold_read_locks_on_select(THD *thd, thr_lock_type lock_type)
{
return (lock_type == TL_READ_WITH_SHARED_LOCKS
|| lock_type == TL_READ_NO_INSERT
|| (lock_type != TL_IGNORE
&& thd->lex->sql_command != SQLCOM_SELECT));
}
/* The following function was copied from ha_blackhole::store_lock: */
THR_LOCK_DATA **ha_rocksdb::store_lock(THD *const thd, THR_LOCK_DATA **to,
enum thr_lock_type lock_type) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(thd != nullptr);
DBUG_ASSERT(to != nullptr);
bool in_lock_tables = my_core::thd_in_lock_tables(thd);
/* First, make a decision about MyRocks's internal locking */
if (lock_type >= TL_WRITE_ALLOW_WRITE) {
m_lock_rows = RDB_LOCK_WRITE;
} else if (lock_type == TL_READ_WITH_SHARED_LOCKS) {
m_lock_rows = RDB_LOCK_READ;
} else {
m_lock_rows = RDB_LOCK_NONE;
if (THDVAR(thd, lock_scanned_rows)) {
/*
The following logic was copied directly from
ha_innobase::store_lock_with_x_type() in
storage/innobase/handler/ha_innodb.cc and causes MyRocks to leave
locks in place on rows that are in a table that is not being updated.
*/
const uint sql_command = my_core::thd_sql_command(thd);
if ((lock_type == TL_READ && in_lock_tables) ||
(lock_type == TL_READ_HIGH_PRIORITY && in_lock_tables) ||
can_hold_read_locks_on_select(thd, lock_type)) {
ulong tx_isolation = my_core::thd_tx_isolation(thd);
if (sql_command != SQLCOM_CHECKSUM &&
((my_core::thd_test_options(thd, OPTION_BIN_LOG) &&
tx_isolation > ISO_READ_COMMITTED) ||
tx_isolation == ISO_SERIALIZABLE ||
(lock_type != TL_READ && lock_type != TL_READ_NO_INSERT) ||
(sql_command != SQLCOM_INSERT_SELECT &&
sql_command != SQLCOM_REPLACE_SELECT &&
sql_command != SQLCOM_UPDATE && sql_command != SQLCOM_DELETE &&
sql_command != SQLCOM_CREATE_TABLE))) {
m_lock_rows = RDB_LOCK_READ;
}
}
}
}
/* Then, tell the SQL layer what kind of locking it should use: */
if (lock_type != TL_IGNORE && m_db_lock.type == TL_UNLOCK) {
/*
Here is where we get into the guts of a row level lock.
If TL_UNLOCK is set
If we are not doing a LOCK TABLE or DISCARD/IMPORT
TABLESPACE, then allow multiple writers
*/
if ((lock_type >= TL_WRITE_CONCURRENT_INSERT && lock_type <= TL_WRITE) &&
!in_lock_tables && !my_core::thd_tablespace_op(thd)) {
lock_type = TL_WRITE_ALLOW_WRITE;
}
/*
In queries of type INSERT INTO t1 SELECT ... FROM t2 ...
MySQL would use the lock TL_READ_NO_INSERT on t2, and that
would conflict with TL_WRITE_ALLOW_WRITE, blocking all inserts
to t2. Convert the lock to a normal read lock to allow
concurrent inserts to t2.
*/
if (lock_type == TL_READ_NO_INSERT && !in_lock_tables) {
lock_type = TL_READ;
}
m_db_lock.type = lock_type;
}
*to++ = &m_db_lock;
DBUG_RETURN(to);
}
void ha_rocksdb::read_thd_vars(THD *const thd) {
m_store_row_debug_checksums = THDVAR(thd, store_row_debug_checksums);
m_verify_row_debug_checksums = THDVAR(thd, verify_row_debug_checksums);
m_checksums_pct = THDVAR(thd, checksums_pct);
}
const char *ha_rocksdb::thd_rocksdb_tmpdir() {
const char *tmp_dir = THDVAR(ha_thd(), tmpdir);
/*
We want to treat an empty string as nullptr, in these cases DDL operations
will use the default --tmpdir passed to mysql instead.
*/
if (tmp_dir != nullptr && *tmp_dir == '\0') {
tmp_dir = nullptr;
}
return (tmp_dir);
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (cannot be SE-specific)
*/
int ha_rocksdb::external_lock(THD *const thd, int lock_type) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(thd != nullptr);
int res = HA_EXIT_SUCCESS;
int binlog_format = my_core::thd_binlog_format(thd);
bool unsafe_for_binlog = THDVAR(ha_thd(), unsafe_for_binlog);
/*
A note about (*) below: In fb/mysql, LOCK TABLE myrocks_table WRITE
- produces an error when @@binlog_format='ROW'.
- does not produce an error with @@binlog_format='STATEMENT'
We cannot easily provide this behavior in MariaDB, because LOCK ROWS
changes @@binlog_format for the duration of the statement.
Instead we will make the check in ha_rocksdb::start_stmt and will produce
an error in the first DML statement done under LOCK TABLE
(**) - The same happens for other "DDL-type" commands, so allow them here
as well.
*/
if (lock_type == F_WRLCK && !thd->rgi_slave && !unsafe_for_binlog &&
binlog_format != BINLOG_FORMAT_ROW &&
binlog_format != BINLOG_FORMAT_UNSPEC &&
thd->lex->sql_command != SQLCOM_LOCK_TABLES && // (*)
thd->lex->sql_command != SQLCOM_ANALYZE && // (**)
thd->lex->sql_command != SQLCOM_OPTIMIZE && // (**)
my_core::thd_binlog_filter_ok(thd)) {
my_error(ER_REQUIRE_ROW_BINLOG_FORMAT, MYF(0));
DBUG_RETURN(HA_ERR_UNSUPPORTED);
}
if (lock_type == F_UNLCK) {
Rdb_transaction *const tx = get_or_create_tx(thd);
tx->io_perf_end_and_record(&m_io_perf);
tx->m_n_mysql_tables_in_use--;
if (tx->m_n_mysql_tables_in_use == 0 &&
!my_core::thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) {
/*
Do like InnoDB: when we get here, it's time to commit a
single-statement transaction.
If the statement involved multiple tables, this code will be executed
for each of them, but that's ok because non-first tx->commit() calls
will be no-ops.
*/
if (tx->commit_or_rollback()) {
res = HA_ERR_INTERNAL_ERROR;
}
}
} else {
if (my_core::thd_tx_isolation(thd) < ISO_READ_COMMITTED ||
my_core::thd_tx_isolation(thd) > ISO_REPEATABLE_READ) {
my_error(ER_ISOLATION_MODE_NOT_SUPPORTED, MYF(0),
tx_isolation_names[my_core::thd_tx_isolation(thd)]);
DBUG_RETURN(HA_ERR_UNSUPPORTED);
}
/*
It's nice to do the following on start of every statement. The problem
is, handler->start_stmt() is not called for INSERTs.
So, we put this code here.
*/
Rdb_transaction *const tx = get_or_create_tx(thd);
read_thd_vars(thd);
m_update_scope_is_valid = false;
if (skip_unique_check()) {
if ((thd->lex->sql_command == SQLCOM_INSERT ||
thd->lex->sql_command == SQLCOM_LOAD ||
thd->lex->sql_command == SQLCOM_REPLACE) &&
(thd->lex->duplicates == DUP_REPLACE ||
thd->lex->duplicates == DUP_UPDATE)) {
my_error(ER_ON_DUPLICATE_DISABLED, MYF(0), thd->query());
DBUG_RETURN(HA_ERR_UNSUPPORTED);
}
}
if (lock_type == F_WRLCK) {
if (tx->is_tx_read_only()) {
my_error(ER_UPDATES_WITH_CONSISTENT_SNAPSHOT, MYF(0));
DBUG_RETURN(HA_ERR_UNSUPPORTED);
}
/*
SQL layer signals us to take a write lock. It does so when starting DML
statement. We should put locks on the rows we're reading.
Note: sometimes, external_lock() can be called without a prior
::store_lock call. That's why we need to set lock_* members here, too.
*/
m_lock_rows = RDB_LOCK_WRITE;
if (thd->lex->sql_command == SQLCOM_CREATE_INDEX ||
thd->lex->sql_command == SQLCOM_DROP_INDEX ||
thd->lex->sql_command == SQLCOM_ALTER_TABLE) {
tx->m_ddl_transaction = true;
}
}
tx->m_n_mysql_tables_in_use++;
rocksdb_register_tx(rocksdb_hton, thd, tx);
tx->io_perf_start(&m_io_perf);
}
DBUG_RETURN(res);
}
/**
@note
A quote from ha_innobase::start_stmt():
<quote>
MySQL calls this function at the start of each SQL statement inside LOCK
TABLES. Inside LOCK TABLES the ::external_lock method does not work to
mark SQL statement borders.
</quote>
@return
HA_EXIT_SUCCESS OK
*/
int ha_rocksdb::start_stmt(THD *const thd, thr_lock_type lock_type) {
DBUG_ENTER_FUNC();
/*
MariaDB: the following is a copy of the check in ha_rocksdb::external_lock:
*/
int binlog_format = my_core::thd_binlog_format(thd);
bool unsafe_for_binlog = THDVAR(ha_thd(), unsafe_for_binlog);
if (lock_type >= TL_WRITE_ALLOW_WRITE &&
!thd->rgi_slave && !unsafe_for_binlog &&
binlog_format != BINLOG_FORMAT_ROW &&
binlog_format != BINLOG_FORMAT_UNSPEC &&
my_core::thd_binlog_filter_ok(thd)) {
my_error(ER_REQUIRE_ROW_BINLOG_FORMAT, MYF(0));
DBUG_RETURN(HA_ERR_UNSUPPORTED);
}
DBUG_ASSERT(thd != nullptr);
Rdb_transaction *const tx = get_or_create_tx(thd);
read_thd_vars(thd);
rocksdb_register_tx(ht, thd, tx);
tx->io_perf_start(&m_io_perf);
DBUG_RETURN(HA_EXIT_SUCCESS);
}
rocksdb::Range get_range(uint32_t i,
uchar buf[Rdb_key_def::INDEX_NUMBER_SIZE * 2],
int offset1, int offset2) {
uchar *buf_begin = buf;
uchar *buf_end = buf + Rdb_key_def::INDEX_NUMBER_SIZE;
rdb_netbuf_store_index(buf_begin, i + offset1);
rdb_netbuf_store_index(buf_end, i + offset2);
return rocksdb::Range(
rocksdb::Slice((const char *)buf_begin, Rdb_key_def::INDEX_NUMBER_SIZE),
rocksdb::Slice((const char *)buf_end, Rdb_key_def::INDEX_NUMBER_SIZE));
}
static rocksdb::Range get_range(const Rdb_key_def &kd,
uchar buf[Rdb_key_def::INDEX_NUMBER_SIZE * 2],
int offset1, int offset2) {
return get_range(kd.get_index_number(), buf, offset1, offset2);
}
rocksdb::Range get_range(const Rdb_key_def &kd,
uchar buf[Rdb_key_def::INDEX_NUMBER_SIZE * 2]) {
if (kd.m_is_reverse_cf) {
return myrocks::get_range(kd, buf, 1, 0);
} else {
return myrocks::get_range(kd, buf, 0, 1);
}
}
rocksdb::Range
ha_rocksdb::get_range(const int &i,
uchar buf[Rdb_key_def::INDEX_NUMBER_SIZE * 2]) const {
return myrocks::get_range(*m_key_descr_arr[i], buf);
}
static bool is_myrocks_index_empty(
rocksdb::ColumnFamilyHandle *cfh, const bool is_reverse_cf,
const rocksdb::ReadOptions &read_opts,
const uint index_id)
{
bool index_removed = false;
uchar key_buf[Rdb_key_def::INDEX_NUMBER_SIZE] = {0};
rdb_netbuf_store_uint32(key_buf, index_id);
const rocksdb::Slice key =
rocksdb::Slice(reinterpret_cast<char *>(key_buf), sizeof(key_buf));
std::unique_ptr<rocksdb::Iterator> it(rdb->NewIterator(read_opts, cfh));
rocksdb_smart_seek(is_reverse_cf, it.get(), key);
if (!it->Valid()) {
index_removed = true;
} else {
if (memcmp(it->key().data(), key_buf,
Rdb_key_def::INDEX_NUMBER_SIZE)) {
// Key does not have same prefix
index_removed = true;
}
}
return index_removed;
}
/*
Drop index thread's main logic
*/
void Rdb_drop_index_thread::run() {
RDB_MUTEX_LOCK_CHECK(m_signal_mutex);
for (;;) {
// The stop flag might be set by shutdown command
// after drop_index_thread releases signal_mutex
// (i.e. while executing expensive Seek()). To prevent drop_index_thread
// from entering long cond_timedwait, checking if stop flag
// is true or not is needed, with drop_index_interrupt_mutex held.
if (m_stop) {
break;
}
timespec ts;
int sec= dict_manager.is_drop_index_empty()
? 24 * 60 * 60 // no filtering
: 60; // filtering
set_timespec(ts,sec);
const auto ret MY_ATTRIBUTE((__unused__)) =
mysql_cond_timedwait(&m_signal_cond, &m_signal_mutex, &ts);
if (m_stop) {
break;
}
// make sure, no program error is returned
DBUG_ASSERT(ret == 0 || ret == ETIMEDOUT);
RDB_MUTEX_UNLOCK_CHECK(m_signal_mutex);
std::unordered_set<GL_INDEX_ID> indices;
dict_manager.get_ongoing_drop_indexes(&indices);
if (!indices.empty()) {
std::unordered_set<GL_INDEX_ID> finished;
rocksdb::ReadOptions read_opts;
read_opts.total_order_seek = true; // disable bloom filter
for (const auto d : indices) {
uint32 cf_flags = 0;
if (!dict_manager.get_cf_flags(d.cf_id, &cf_flags)) {
sql_print_error("RocksDB: Failed to get column family flags "
"from cf id %u. MyRocks data dictionary may "
"get corrupted.",
d.cf_id);
abort_with_stack_traces();
}
rocksdb::ColumnFamilyHandle *cfh = cf_manager.get_cf(d.cf_id);
DBUG_ASSERT(cfh);
const bool is_reverse_cf = cf_flags & Rdb_key_def::REVERSE_CF_FLAG;
if (is_myrocks_index_empty(cfh, is_reverse_cf, read_opts, d.index_id))
{
finished.insert(d);
continue;
}
uchar buf[Rdb_key_def::INDEX_NUMBER_SIZE * 2];
rocksdb::Range range = get_range(d.index_id, buf, is_reverse_cf ? 1 : 0,
is_reverse_cf ? 0 : 1);
rocksdb::Status status = DeleteFilesInRange(rdb->GetBaseDB(), cfh,
&range.start, &range.limit);
if (!status.ok()) {
if (status.IsShutdownInProgress()) {
break;
}
rdb_handle_io_error(status, RDB_IO_ERROR_BG_THREAD);
}
status = rdb->CompactRange(getCompactRangeOptions(), cfh, &range.start,
&range.limit);
if (!status.ok()) {
if (status.IsShutdownInProgress()) {
break;
}
rdb_handle_io_error(status, RDB_IO_ERROR_BG_THREAD);
}
if (is_myrocks_index_empty(cfh, is_reverse_cf, read_opts, d.index_id))
{
finished.insert(d);
}
}
if (!finished.empty()) {
dict_manager.finish_drop_indexes(finished);
}
}
RDB_MUTEX_LOCK_CHECK(m_signal_mutex);
}
RDB_MUTEX_UNLOCK_CHECK(m_signal_mutex);
}
Rdb_tbl_def *ha_rocksdb::get_table_if_exists(const char *const tablename) {
DBUG_ASSERT(tablename != nullptr);
std::string str;
if (rdb_normalize_tablename(tablename, &str) != HA_EXIT_SUCCESS) {
// We were not passed table name?
DBUG_ASSERT(0);
return nullptr;
}
return ddl_manager.find(str);
}
/*
Note: the following function is called when the table is not open. That is,
this->table==nullptr, pk_key_descr==nullptr, etc.
tablename points to line in form "./dbname/tablename".
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (can be SE-specific)
*/
int ha_rocksdb::delete_table(const char *const tablename) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(tablename != nullptr);
const std::unique_ptr<rocksdb::WriteBatch> wb = dict_manager.begin();
rocksdb::WriteBatch *const batch = wb.get();
/* Find the table in the hash */
Rdb_tbl_def *const tbl = get_table_if_exists(tablename);
if (!tbl)
DBUG_RETURN(HA_ERR_NO_SUCH_TABLE);
dict_manager.add_drop_table(tbl->m_key_descr_arr, tbl->m_key_count, batch);
/*
Remove the table entry in data dictionary (this will also remove it from
the persistent data dictionary).
*/
ddl_manager.remove(tbl, batch, true);
int err = dict_manager.commit(batch);
if (err) {
DBUG_RETURN(err);
}
rdb_drop_idx_thread.signal();
DBUG_RETURN(HA_EXIT_SUCCESS);
}
int ha_rocksdb::remove_rows(Rdb_tbl_def *const tbl) {
const rocksdb::WriteOptions wo =
rdb_get_rocksdb_write_options(handler::ha_thd());
rocksdb::ReadOptions opts;
opts.total_order_seek = true;
Rdb_transaction *const tx = get_or_create_tx(table->in_use);
char key_buf[MAX_KEY_LENGTH];
uint key_len;
ulonglong bytes_written = 0;
/*
Remove all records in each index.
(This is is not crash-safe, but it doesn't matter, because bulk row
deletion will be handled on rocksdb side)
*/
for (uint i = 0; i < tbl->m_key_count; i++) {
const Rdb_key_def &kd = *tbl->m_key_descr_arr[i];
kd.get_infimum_key(reinterpret_cast<uchar *>(key_buf), &key_len);
rocksdb::ColumnFamilyHandle *cf = kd.get_cf();
std::unique_ptr<rocksdb::Iterator> it(rdb->NewIterator(opts, cf));
const rocksdb::Slice table_key(key_buf, key_len);
it->Seek(table_key);
while (it->Valid()) {
const rocksdb::Slice key = it->key();
if (!kd.covers_key(key)) {
break;
}
rocksdb::Status s;
if (can_use_single_delete(i)) {
s = rdb->SingleDelete(wo, cf, key);
} else {
s = rdb->Delete(wo, cf, key);
}
if (!s.ok()) {
return tx->set_status_error(table->in_use, s, *m_pk_descr, m_tbl_def,
m_table_handler);
}
bytes_written += key.size();
it->Next();
}
}
tx->update_bytes_written(bytes_written);
return HA_EXIT_SUCCESS;
}
/**
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code (cannot be SE-specific)
*/
int ha_rocksdb::rename_table(const char *const from, const char *const to) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(from != nullptr);
DBUG_ASSERT(to != nullptr);
std::string from_str;
std::string to_str;
std::string from_db;
std::string to_db;
int rc = rdb_normalize_tablename(from, &from_str);
if (rc != HA_EXIT_SUCCESS) {
DBUG_RETURN(rc);
}
rc = rdb_split_normalized_tablename(from_str, &from_db);
if (rc != HA_EXIT_SUCCESS) {
DBUG_RETURN(rc);
}
rc = rdb_normalize_tablename(to, &to_str);
if (rc != HA_EXIT_SUCCESS) {
DBUG_RETURN(rc);
}
rc = rdb_split_normalized_tablename(to_str, &to_db);
if (rc != HA_EXIT_SUCCESS) {
DBUG_RETURN(rc);
}
// If the user changed the database part of the name then validate that the
// 'to' database exists.
if (from_db != to_db && !rdb_database_exists(to_db)) {
// If we return a RocksDB specific error code here we get
// "error: 206 - Unknown error 206". InnoDB gets
// "error -1 - Unknown error -1" so let's match them.
DBUG_RETURN(-1);
}
DBUG_EXECUTE_IF("gen_sql_table_name", to_str = to_str + "#sql-test";);
const std::unique_ptr<rocksdb::WriteBatch> wb = dict_manager.begin();
rocksdb::WriteBatch *const batch = wb.get();
dict_manager.lock();
if (ddl_manager.rename(from_str, to_str, batch)) {
rc = HA_ERR_NO_SUCH_TABLE;
} else {
rc = dict_manager.commit(batch);
}
dict_manager.unlock();
DBUG_RETURN(rc);
}
/**
check_if_incompatible_data() called if ALTER TABLE can't detect otherwise
if new and old definition are compatible
@details If there are no other explicit signs like changed number of
fields this function will be called by compare_tables()
(sql/sql_tables.cc) to decide should we rewrite whole table or only .frm
file.
*/
bool ha_rocksdb::check_if_incompatible_data(HA_CREATE_INFO *const info,
uint table_changes) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(info != nullptr);
// this function is needed only for online alter-table
DBUG_RETURN(COMPATIBLE_DATA_NO);
}
/**
@return
HA_EXIT_SUCCESS OK
*/
int ha_rocksdb::extra(enum ha_extra_function operation) {
DBUG_ENTER_FUNC();
switch (operation) {
case HA_EXTRA_KEYREAD:
m_keyread_only = true;
break;
case HA_EXTRA_NO_KEYREAD:
m_keyread_only = false;
break;
case HA_EXTRA_FLUSH:
/*
If the table has blobs, then they are part of m_retrieved_record.
This call invalidates them.
*/
m_retrieved_record.clear();
break;
default:
break;
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/*
Given a starting key and an ending key, estimate the number of rows that
will exist between the two keys.
*/
ha_rows ha_rocksdb::records_in_range(uint inx, key_range *const min_key,
key_range *const max_key) {
DBUG_ENTER_FUNC();
ha_rows ret = THDVAR(ha_thd(), records_in_range);
if (ret) {
DBUG_RETURN(ret);
}
if (table->force_index) {
const ha_rows force_rows = THDVAR(ha_thd(), force_index_records_in_range);
if (force_rows) {
DBUG_RETURN(force_rows);
}
}
const Rdb_key_def &kd = *m_key_descr_arr[inx];
uint size1 = 0;
if (min_key) {
size1 = kd.pack_index_tuple(table, m_pack_buffer, m_sk_packed_tuple,
min_key->key, min_key->keypart_map);
if (min_key->flag == HA_READ_PREFIX_LAST_OR_PREV ||
min_key->flag == HA_READ_PREFIX_LAST ||
min_key->flag == HA_READ_AFTER_KEY) {
kd.successor(m_sk_packed_tuple, size1);
}
} else {
kd.get_infimum_key(m_sk_packed_tuple, &size1);
}
uint size2 = 0;
if (max_key) {
size2 = kd.pack_index_tuple(table, m_pack_buffer, m_sk_packed_tuple_old,
max_key->key, max_key->keypart_map);
if (max_key->flag == HA_READ_PREFIX_LAST_OR_PREV ||
max_key->flag == HA_READ_PREFIX_LAST ||
max_key->flag == HA_READ_AFTER_KEY) {
kd.successor(m_sk_packed_tuple_old, size2);
}
// pad the upper key with FFFFs to make sure it is more than the lower
if (size1 > size2) {
memset(m_sk_packed_tuple_old + size2, 0xff, size1 - size2);
size2 = size1;
}
} else {
kd.get_supremum_key(m_sk_packed_tuple_old, &size2);
}
const rocksdb::Slice slice1((const char *)m_sk_packed_tuple, size1);
const rocksdb::Slice slice2((const char *)m_sk_packed_tuple_old, size2);
// slice1 >= slice2 means no row will match
if (slice1.compare(slice2) >= 0) {
DBUG_RETURN(HA_EXIT_SUCCESS);
}
rocksdb::Range r(kd.m_is_reverse_cf ? slice2 : slice1,
kd.m_is_reverse_cf ? slice1 : slice2);
uint64_t sz = 0;
auto disk_size = kd.m_stats.m_actual_disk_size;
if (disk_size == 0)
disk_size = kd.m_stats.m_data_size;
auto rows = kd.m_stats.m_rows;
if (rows == 0 || disk_size == 0) {
rows = 1;
disk_size = ROCKSDB_ASSUMED_KEY_VALUE_DISK_SIZE;
}
// Getting statistics, including from Memtables
uint8_t include_flags = rocksdb::DB::INCLUDE_FILES;
rdb->GetApproximateSizes(kd.get_cf(), &r, 1, &sz, include_flags);
ret = rows * sz / disk_size;
uint64_t memTableCount;
rdb->GetApproximateMemTableStats(kd.get_cf(), r, &memTableCount, &sz);
ret += memTableCount;
/*
GetApproximateSizes() gives estimates so ret might exceed stats.records.
MySQL then decides to use full index scan rather than range scan, which
is not efficient for most cases.
To prevent this, changing estimated records slightly smaller than
stats.records.
*/
if (ret >= stats.records) {
ret = stats.records * 0.99;
}
if (rocksdb_debug_optimizer_n_rows > 0) {
ret = rocksdb_debug_optimizer_n_rows;
} else if (ret == 0) {
ret = 1;
}
DBUG_RETURN(ret);
}
void ha_rocksdb::update_create_info(HA_CREATE_INFO *const create_info) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(create_info != nullptr);
if (!(create_info->used_fields & HA_CREATE_USED_AUTO)) {
create_info->auto_increment_value = m_tbl_def->m_auto_incr_val;
}
DBUG_VOID_RETURN;
}
/**
@brief
Doing manual compaction on OPTIMIZE TABLE in RocksDB.
Compaction itself is executed by background thread in RocksDB, but
CompactRange() waits until compaction completes so this function
may take a long time.
Since RocksDB dataset is allocated per index id, OPTIMIZE TABLE
triggers manual compaction for all indexes of the table.
@details
Compaction range is from the beginning of the index id to
the first row of the next index id. When using reverse order
column family, the first row of the next index id should be
the last row of the previous index id.
@return
HA_ADMIN_OK OK
other HA_ADMIN error code
*/
int ha_rocksdb::optimize(THD *const thd, HA_CHECK_OPT *const check_opt) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(thd != nullptr);
DBUG_ASSERT(check_opt != nullptr);
for (uint i = 0; i < table->s->keys; i++) {
uchar buf[Rdb_key_def::INDEX_NUMBER_SIZE * 2];
auto range = get_range(i, buf);
const rocksdb::Status s = rdb->CompactRange(getCompactRangeOptions(),
m_key_descr_arr[i]->get_cf(),
&range.start, &range.limit);
if (!s.ok()) {
DBUG_RETURN(rdb_error_to_mysql(s));
}
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
int ha_rocksdb::calculate_stats(const TABLE *const table_arg, THD *const thd,
HA_CHECK_OPT *const check_opt) {
DBUG_ENTER_FUNC();
// find per column family key ranges which need to be queried
std::unordered_map<rocksdb::ColumnFamilyHandle *, std::vector<rocksdb::Range>>
ranges;
std::unordered_set<GL_INDEX_ID> ids_to_check;
std::unordered_map<GL_INDEX_ID, uint> ids_to_keyparts;
std::vector<uchar> buf(table_arg->s->keys * 2 *
Rdb_key_def::INDEX_NUMBER_SIZE);
for (uint i = 0; i < table_arg->s->keys; i++) {
const auto bufp = &buf[i * 2 * Rdb_key_def::INDEX_NUMBER_SIZE];
const Rdb_key_def &kd = *m_key_descr_arr[i];
ranges[kd.get_cf()].push_back(get_range(i, bufp));
ids_to_check.insert(kd.get_gl_index_id());
ids_to_keyparts[kd.get_gl_index_id()] = kd.get_key_parts();
}
// for analyze statements, force flush on memtable to get accurate cardinality
Rdb_cf_manager &cf_manager = rdb_get_cf_manager();
if (thd != nullptr && THDVAR(thd, flush_memtable_on_analyze) &&
!rocksdb_pause_background_work) {
for (auto it : ids_to_check) {
rdb->Flush(rocksdb::FlushOptions(), cf_manager.get_cf(it.cf_id));
}
}
// get RocksDB table properties for these ranges
rocksdb::TablePropertiesCollection props;
for (auto it : ranges) {
const auto old_size MY_ATTRIBUTE((__unused__)) = props.size();
const auto status = rdb->GetPropertiesOfTablesInRange(
it.first, &it.second[0], it.second.size(), &props);
DBUG_ASSERT(props.size() >= old_size);
if (!status.ok()) {
DBUG_RETURN(
rdb_error_to_mysql(status, "Could not access RocksDB properties"));
}
}
int num_sst = 0;
// group stats per index id
std::unordered_map<GL_INDEX_ID, Rdb_index_stats> stats;
for (const auto &it : ids_to_check) {
// Initialize the stats to 0. If there are no files that contain
// this gl_index_id, then 0 should be stored for the cached stats.
stats[it] = Rdb_index_stats(it);
DBUG_ASSERT(ids_to_keyparts.count(it) > 0);
stats[it].m_distinct_keys_per_prefix.resize(ids_to_keyparts[it]);
}
for (const auto &it : props) {
std::vector<Rdb_index_stats> sst_stats;
Rdb_tbl_prop_coll::read_stats_from_tbl_props(it.second, &sst_stats);
/*
sst_stats is a list of index statistics for indexes that have entries
in the current SST file.
*/
for (const auto &it1 : sst_stats) {
/*
Only update statistics for indexes that belong to this SQL table.
The reason is: We are walking through all SST files that have
entries from this table (and so can compute good statistics). For
other SQL tables, it can be that we're only seeing a small fraction
of table's entries (and so we can't update statistics based on that).
*/
if (ids_to_check.find(it1.m_gl_index_id) == ids_to_check.end())
continue;
auto kd = ddl_manager.safe_find(it1.m_gl_index_id);
DBUG_ASSERT(kd != nullptr);
stats[it1.m_gl_index_id].merge(it1, true, kd->max_storage_fmt_length());
}
num_sst++;
}
// set and persist new stats
ddl_manager.set_stats(stats);
ddl_manager.persist_stats(true);
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/*
@return
HA_ADMIN_OK OK
other HA_ADMIN error code
*/
int ha_rocksdb::analyze(THD *const thd, HA_CHECK_OPT *const check_opt) {
DBUG_ENTER_FUNC();
if (table && calculate_stats(table, thd, check_opt) != HA_EXIT_SUCCESS) {
DBUG_RETURN(HA_ADMIN_FAILED);
}
DBUG_RETURN(HA_ADMIN_OK);
}
void ha_rocksdb::get_auto_increment(ulonglong off, ulonglong inc,
ulonglong nb_desired_values,
ulonglong *const first_value,
ulonglong *const nb_reserved_values) {
/*
MySQL has a somewhat complicated way of handling the auto-increment value.
The first time get_auto_increment is called for a statement,
nb_desired_values is the estimate for how many values will be needed. The
engine can then reserve some values, and those will be automatically used
by MySQL, until a hard-coded value shows up in the insert statement, after
which MySQL again calls this function to reset its starting value.
*
For simplicity we will just ignore nb_desired_values - we aren't going to
reserve any extra values for a multi-insert statement. Each row will
simply acquire the next value as needed and we will always tell MySQL that
we only reserved 1 value. Since we are using an atomic value for
m_auto_incr_val this should be safe - if we had to grab a mutex, doing
an actual reserve of some values might be a better solution.
*/
DEBUG_SYNC(ha_thd(), "rocksdb.autoinc_vars");
if (off > inc) {
off = 1;
}
longlong new_val;
// Local variable reference to simplify code below
std::atomic<longlong> &auto_incr = m_tbl_def->m_auto_incr_val;
if (inc == 1 && off == 1) {
// Optimization for the standard case where we are always simply
// incrementing from the last position
// Use CAS operation in a loop to make sure automically get the next auto
// increment value while ensuring tha we don't wrap around to a negative
// number.
new_val = auto_incr;
while (new_val != std::numeric_limits<longlong>::max()) {
if (auto_incr.compare_exchange_weak(new_val, new_val + 1)) {
break;
}
}
} else {
// The next value can be more complicated if either `inc` or 'off' is not 1
longlong last_val = auto_incr;
// Loop until we can correctly update the atomic value
do {
if (((last_val - off) / inc) ==
(std::numeric_limits<longlong>::max() - off) / inc) {
// The 'last_val' value is already equal to or larger than the largest
// value in the sequence. Continuing would wrap around (technically
// the behavior would be undefined). What should we do?
// We could:
// 1) set the new value to the last possible number in our sequence
// as described above. The problem with this is that this
// number could be smaller than a value in an existing row.
// 2) set the new value to the largest possible number. This number
// may not be in our sequence, but it is guaranteed to be equal
// to or larger than any other value already inserted.
//
// For now I'm going to take option @2.
new_val = std::numeric_limits<longlong>::max();
auto_incr = new_val; // Store the largest value into auto_incr
break;
}
// Calculate the next value in the auto increment series:
// offset + N * increment
// where N is 0, 1, 2, ...
//
// For further information please visit:
// http://dev.mysql.com/doc/refman/5.7/en/replication-options-master.html
//
// The following is confusing so here is an explanation:
// To get the next number in the sequence above you subtract out
// the offset, calculate the next sequence (N * increment) and then add
// the offset back in.
new_val = (((last_val - off) + (inc - 1)) / inc) * inc + off;
// Attempt to store the new value (plus 1 since m_auto_incr_val contains
// the next available value) into the atomic value. If the current
// value no longer matches what we have in 'last_val' this will fail and
// we will repeat the loop (`last_val` will automatically get updated
// with the current value).
} while (!auto_incr.compare_exchange_weak(last_val, new_val + 1));
}
*first_value = new_val;
*nb_reserved_values = 1;
}
#ifndef DBUG_OFF
/* Debugger help function */
static char dbug_item_print_buf[512];
const char *dbug_print_item(Item *const item) {
char *const buf = dbug_item_print_buf;
String str(buf, sizeof(dbug_item_print_buf), &my_charset_bin);
str.length(0);
if (!item)
return "(Item*)nullptr";
item->print(&str, QT_ORDINARY);
if (str.c_ptr() == buf)
return buf;
else
return "Couldn't fit into buffer";
}
#endif /*DBUG_OFF*/
/**
SQL layer calls this function to push an index condition.
@details
The condition is for index keyno (only one condition can be pushed at a
time).
The caller guarantees that condition refers only to index fields; besides
that, fields must have
$field->part_of_key.set_bit(keyno)
which means that
(handler->index_flags(keyno, $keypart, 0) & HA_KEYREAD_ONLY) == 1
which means that field value can be restored from the index tuple.
@return
Part of condition we couldn't check (always nullptr).
*/
class Item *ha_rocksdb::idx_cond_push(uint keyno, class Item *const idx_cond) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(keyno != MAX_KEY);
DBUG_ASSERT(idx_cond != nullptr);
pushed_idx_cond = idx_cond;
pushed_idx_cond_keyno = keyno;
in_range_check_pushed_down = TRUE;
/* We will check the whole condition */
DBUG_RETURN(nullptr);
}
/*
Checks if inplace alter is supported for a given operation.
*/
my_core::enum_alter_inplace_result ha_rocksdb::check_if_supported_inplace_alter(
TABLE *altered_table, my_core::Alter_inplace_info *const ha_alter_info) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(ha_alter_info != nullptr);
if (ha_alter_info->handler_flags &
~(my_core::Alter_inplace_info::DROP_INDEX |
my_core::Alter_inplace_info::DROP_UNIQUE_INDEX |
my_core::Alter_inplace_info::ADD_INDEX |
my_core::Alter_inplace_info::ALTER_PARTITIONED |
my_core::Alter_inplace_info::ADD_UNIQUE_INDEX)) {
DBUG_RETURN(my_core::HA_ALTER_INPLACE_NOT_SUPPORTED);
}
/* We don't support unique keys on table w/ no primary keys */
if ((ha_alter_info->handler_flags &
my_core::Alter_inplace_info::ADD_UNIQUE_INDEX) &&
has_hidden_pk(altered_table)) {
DBUG_RETURN(my_core::HA_ALTER_INPLACE_NOT_SUPPORTED);
}
DBUG_RETURN(my_core::HA_ALTER_INPLACE_SHARED_LOCK_AFTER_PREPARE);
}
/**
Allows the storage engine to update internal structures with concurrent
writes blocked. If check_if_supported_inplace_alter() returns
HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE or
HA_ALTER_INPLACE_SHARED_AFTER_PREPARE, this function is called with
exclusive lock otherwise the same level of locking as for
inplace_alter_table() will be used.
@note Storage engines are responsible for reporting any errors by
calling my_error()/print_error()
@note If this function reports error, commit_inplace_alter_table()
will be called with commit= false.
@note For partitioning, failing to prepare one partition, means that
commit_inplace_alter_table() will be called to roll back changes for
all partitions. This means that commit_inplace_alter_table() might be
called without prepare_inplace_alter_table() having been called first
for a given partition.
@param altered_table TABLE object for new version of table.
@param ha_alter_info Structure describing changes to be done
by ALTER TABLE and holding data used
during in-place alter.
@retval true Error
@retval false Success
*/
bool ha_rocksdb::prepare_inplace_alter_table(
TABLE *const altered_table,
my_core::Alter_inplace_info *const ha_alter_info) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(altered_table != nullptr);
DBUG_ASSERT(ha_alter_info != nullptr);
const uint old_n_keys = m_tbl_def->m_key_count;
uint new_n_keys = altered_table->s->keys;
if (has_hidden_pk(altered_table)) {
new_n_keys += 1;
}
const TABLE *const old_table = table;
std::shared_ptr<Rdb_key_def> *const old_key_descr =
m_tbl_def->m_key_descr_arr;
std::shared_ptr<Rdb_key_def> *const new_key_descr =
new std::shared_ptr<Rdb_key_def>[new_n_keys];
Rdb_tbl_def *const new_tdef = new Rdb_tbl_def(m_tbl_def->full_tablename());
new_tdef->m_key_descr_arr = new_key_descr;
new_tdef->m_key_count = new_n_keys;
new_tdef->m_auto_incr_val =
m_tbl_def->m_auto_incr_val.load(std::memory_order_relaxed);
new_tdef->m_hidden_pk_val =
m_tbl_def->m_hidden_pk_val.load(std::memory_order_relaxed);
if (ha_alter_info->handler_flags &
(my_core::Alter_inplace_info::DROP_INDEX |
my_core::Alter_inplace_info::DROP_UNIQUE_INDEX |
my_core::Alter_inplace_info::ADD_INDEX |
my_core::Alter_inplace_info::ADD_UNIQUE_INDEX) &&
create_key_defs(altered_table, new_tdef, table, m_tbl_def)) {
/* Delete the new key descriptors */
delete[] new_key_descr;
/*
Explicitly mark as nullptr so we don't accidentally remove entries
from data dictionary on cleanup (or cause double delete[]).
*/
new_tdef->m_key_descr_arr = nullptr;
delete new_tdef;
my_error(ER_KEY_CREATE_DURING_ALTER, MYF(0));
DBUG_RETURN(HA_EXIT_FAILURE);
}
std::unordered_set<std::shared_ptr<Rdb_key_def>> added_indexes;
std::unordered_set<GL_INDEX_ID> dropped_index_ids;
uint i;
uint j;
/* Determine which(if any) key definition(s) need to be dropped */
for (i = 0; i < ha_alter_info->index_drop_count; i++) {
const KEY *const dropped_key = ha_alter_info->index_drop_buffer[i];
for (j = 0; j < old_n_keys; j++) {
const KEY *const old_key =
&old_table->key_info[old_key_descr[j]->get_keyno()];
if (!compare_keys(old_key, dropped_key)) {
dropped_index_ids.insert(old_key_descr[j]->get_gl_index_id());
break;
}
}
}
/* Determine which(if any) key definitions(s) need to be added */
int identical_indexes_found = 0;
for (i = 0; i < ha_alter_info->index_add_count; i++) {
const KEY *const added_key =
&ha_alter_info->key_info_buffer[ha_alter_info->index_add_buffer[i]];
for (j = 0; j < new_n_keys; j++) {
const KEY *const new_key =
&altered_table->key_info[new_key_descr[j]->get_keyno()];
if (!compare_keys(new_key, added_key)) {
/*
Check for cases where an 'identical' index is being dropped and
re-added in a single ALTER statement. Turn this into a no-op as the
index has not changed.
E.G. Unique index -> non-unique index requires no change
Note that cases where the index name remains the same but the
key-parts are changed is already handled in create_inplace_key_defs.
In these cases the index needs to be rebuilt.
*/
if (dropped_index_ids.count(new_key_descr[j]->get_gl_index_id())) {
dropped_index_ids.erase(new_key_descr[j]->get_gl_index_id());
identical_indexes_found++;
} else {
added_indexes.insert(new_key_descr[j]);
}
break;
}
}
}
const uint n_dropped_keys =
ha_alter_info->index_drop_count - identical_indexes_found;
const uint n_added_keys =
ha_alter_info->index_add_count - identical_indexes_found;
DBUG_ASSERT(dropped_index_ids.size() == n_dropped_keys);
DBUG_ASSERT(added_indexes.size() == n_added_keys);
DBUG_ASSERT(new_n_keys == (old_n_keys - n_dropped_keys + n_added_keys));
ha_alter_info->handler_ctx = new Rdb_inplace_alter_ctx(
new_tdef, old_key_descr, new_key_descr, old_n_keys, new_n_keys,
added_indexes, dropped_index_ids, n_added_keys, n_dropped_keys);
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/**
Alter the table structure in-place with operations specified using
HA_ALTER_FLAGS and Alter_inplace_info. The level of concurrency allowed
during this operation depends on the return value from
check_if_supported_inplace_alter().
@note Storage engines are responsible for reporting any errors by
calling my_error()/print_error()
@note If this function reports error, commit_inplace_alter_table()
will be called with commit= false.
@param altered_table TABLE object for new version of table.
@param ha_alter_info Structure describing changes to be done
by ALTER TABLE and holding data used
during in-place alter.
@retval true Error
@retval false Success
*/
bool ha_rocksdb::inplace_alter_table(
TABLE *const altered_table,
my_core::Alter_inplace_info *const ha_alter_info) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(altered_table != nullptr);
DBUG_ASSERT(ha_alter_info != nullptr);
DBUG_ASSERT(ha_alter_info->handler_ctx != nullptr);
Rdb_inplace_alter_ctx *const ctx =
static_cast<Rdb_inplace_alter_ctx *>(ha_alter_info->handler_ctx);
if (ha_alter_info->handler_flags &
(my_core::Alter_inplace_info::ADD_INDEX |
my_core::Alter_inplace_info::ADD_UNIQUE_INDEX)) {
/*
Buffers need to be set up again to account for new, possibly longer
secondary keys.
*/
free_key_buffers();
DBUG_ASSERT(ctx != nullptr);
/*
If adding unique index, allocate special buffers for duplicate checking.
*/
int err;
if ((err = alloc_key_buffers(
altered_table, ctx->m_new_tdef,
ha_alter_info->handler_flags &
my_core::Alter_inplace_info::ADD_UNIQUE_INDEX))) {
my_error(ER_OUT_OF_RESOURCES, MYF(0));
DBUG_RETURN(err);
}
/* Populate all new secondary keys by scanning the primary key. */
if ((err = inplace_populate_sk(altered_table, ctx->m_added_indexes))) {
my_error(ER_SK_POPULATE_DURING_ALTER, MYF(0));
DBUG_RETURN(HA_EXIT_FAILURE);
}
}
DBUG_EXECUTE_IF("myrocks_simulate_index_create_rollback", {
dbug_create_err_inplace_alter();
DBUG_RETURN(HA_EXIT_FAILURE);
};);
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/**
Scan the Primary Key index entries and populate the new secondary keys.
*/
int ha_rocksdb::inplace_populate_sk(
TABLE *const new_table_arg,
const std::unordered_set<std::shared_ptr<Rdb_key_def>> &indexes) {
DBUG_ENTER_FUNC();
int res = HA_EXIT_SUCCESS;
const std::unique_ptr<rocksdb::WriteBatch> wb = dict_manager.begin();
rocksdb::WriteBatch *const batch = wb.get();
/* Update the data dictionary */
std::unordered_set<GL_INDEX_ID> create_index_ids;
for (const auto &index : indexes) {
create_index_ids.insert(index->get_gl_index_id());
}
dict_manager.add_create_index(create_index_ids, batch);
res = dict_manager.commit(batch);
if (res != HA_EXIT_SUCCESS) {
return res;
}
/*
Add uncommitted key definitons to ddl_manager. We need to do this
so that the property collector can find this keydef when it needs to
update stats. The property collector looks for the keydef in the
data dictionary, but it won't be there yet since this key definition
is still in the creation process.
*/
ddl_manager.add_uncommitted_keydefs(indexes);
const bool hidden_pk_exists = has_hidden_pk(table);
Rdb_transaction *tx = get_or_create_tx(table->in_use);
/*
There is one specific scenario where m_sst_info may not be nullptr. This
happens if the handler we're using happens to be the handler where the PK
bulk load was done on. The sequence of events that lead to this is as
follows (T1 is PK bulk load, T2 is SK alter table):
T1: Execute last INSERT statement
T1: Return TABLE and handler object back to Table_cache_manager
T1: Close connection
T2: Execute ALTER statement
T2: Take same TABLE/handler from Table_cache_manager
T2: Call closefrm which will call finalize_bulk_load on every other open
table/handler *except* the one it's on.
T2: Acquire stale snapshot of PK
T1: Call finalize_bulk_load
This is rare because usually, closefrm will call the destructor (and thus
finalize_bulk_load) on the handler where PK bulk load is done. However, if
the thread ids of the bulk load thread and the alter thread differ by a
multiple of table_cache_instances (8 by default), then they hash to the
same bucket in Table_cache_manager and the alter thread will not not call
the destructor on the handler it is holding. Thus, its m_sst_info will not
be nullptr.
At this point, it is safe to refresh the snapshot because we know all other
open handlers have been closed at this point, and the one we're on is the
only one left.
*/
if (m_sst_info) {
if ((res = finalize_bulk_load())) {
DBUG_RETURN(res);
}
tx->commit();
}
const ulonglong rdb_merge_buf_size = THDVAR(ha_thd(), merge_buf_size);
const ulonglong rdb_merge_combine_read_size =
THDVAR(ha_thd(), merge_combine_read_size);
const ulonglong rdb_merge_tmp_file_removal_delay =
THDVAR(ha_thd(), merge_tmp_file_removal_delay_ms);
for (const auto &index : indexes) {
bool is_unique_index =
new_table_arg->key_info[index->get_keyno()].flags & HA_NOSAME;
Rdb_index_merge rdb_merge(
thd_rocksdb_tmpdir(), rdb_merge_buf_size, rdb_merge_combine_read_size,
rdb_merge_tmp_file_removal_delay, index->get_cf());
if ((res = rdb_merge.init())) {
DBUG_RETURN(res);
}
/*
Note: We pass in the currently existing table + tbl_def object here,
as the pk index position may have changed in the case of hidden primary
keys.
*/
const uint pk = pk_index(table, m_tbl_def);
ha_index_init(pk, true);
/* Scan each record in the primary key in order */
for (res = index_first(table->record[0]); res == 0;
res = index_next(table->record[0])) {
longlong hidden_pk_id = 0;
if (hidden_pk_exists &&
(res = read_hidden_pk_id_from_rowkey(&hidden_pk_id))) {
// NO_LINT_DEBUG
sql_print_error("Error retrieving hidden pk id.");
ha_index_end();
DBUG_RETURN(res);
}
/* Create new secondary index entry */
const int new_packed_size = index->pack_record(
new_table_arg, m_pack_buffer, table->record[0], m_sk_packed_tuple,
&m_sk_tails, should_store_row_debug_checksums(), hidden_pk_id, 0,
nullptr, nullptr, m_ttl_bytes);
const rocksdb::Slice key = rocksdb::Slice(
reinterpret_cast<const char *>(m_sk_packed_tuple), new_packed_size);
const rocksdb::Slice val =
rocksdb::Slice(reinterpret_cast<const char *>(m_sk_tails.ptr()),
m_sk_tails.get_current_pos());
/*
Add record to offset tree in preparation for writing out to
disk in sorted chunks.
*/
if ((res = rdb_merge.add(key, val))) {
ha_index_end();
DBUG_RETURN(res);
}
}
if (res != HA_ERR_END_OF_FILE) {
// NO_LINT_DEBUG
sql_print_error("Error retrieving index entry from primary key.");
ha_index_end();
DBUG_RETURN(res);
}
ha_index_end();
/*
Perform an n-way merge of n sorted buffers on disk, then writes all
results to RocksDB via SSTFileWriter API.
*/
rocksdb::Slice merge_key;
rocksdb::Slice merge_val;
struct unique_sk_buf_info sk_info;
sk_info.dup_sk_buf = m_dup_sk_packed_tuple;
sk_info.dup_sk_buf_old = m_dup_sk_packed_tuple_old;
while ((res = rdb_merge.next(&merge_key, &merge_val)) == 0) {
/* Perform uniqueness check if needed */
if (is_unique_index) {
if (check_duplicate_sk(new_table_arg, *index, &merge_key, &sk_info)) {
/*
Duplicate entry found when trying to create unique secondary key.
We need to unpack the record into new_table_arg->record[0] as it
is used inside print_keydup_error so that the error message shows
the duplicate record.
*/
if (index->unpack_record(new_table_arg, new_table_arg->record[0],
&merge_key, nullptr,
m_verify_row_debug_checksums)) {
/* Should never reach here */
DBUG_ASSERT(0);
}
print_keydup_error(new_table_arg,
&new_table_arg->key_info[index->get_keyno()],
MYF(0));
DBUG_RETURN(ER_DUP_ENTRY);
}
}
/*
Insert key and slice to SST via SSTFileWriter API.
*/
if ((res = bulk_load_key(tx, *index, merge_key, merge_val, false))) {
break;
}
}
/*
Here, res == -1 means that we are finished, while > 0 means an error
occurred.
*/
if (res > 0) {
// NO_LINT_DEBUG
sql_print_error("Error while bulk loading keys in external merge sort.");
DBUG_RETURN(res);
}
if ((res = tx->finish_bulk_load())) {
// NO_LINT_DEBUG
sql_print_error("Error finishing bulk load.");
DBUG_RETURN(res);
}
}
/*
Explicitly tell jemalloc to clean up any unused dirty pages at this point.
See https://reviews.facebook.net/D63723 for more details.
*/
purge_all_jemalloc_arenas();
DBUG_EXECUTE_IF("crash_during_online_index_creation", DBUG_SUICIDE(););
DBUG_RETURN(res);
}
/**
Commit or rollback the changes made during prepare_inplace_alter_table()
and inplace_alter_table() inside the storage engine.
Note that in case of rollback the allowed level of concurrency during
this operation will be the same as for inplace_alter_table() and thus
might be higher than during prepare_inplace_alter_table(). (For example,
concurrent writes were blocked during prepare, but might not be during
rollback).
@note Storage engines are responsible for reporting any errors by
calling my_error()/print_error()
@note If this function with commit= true reports error, it will be called
again with commit= false.
@note In case of partitioning, this function might be called for rollback
without prepare_inplace_alter_table() having been called first.
Also partitioned tables sets ha_alter_info->group_commit_ctx to a NULL
terminated array of the partitions handlers and if all of them are
committed as one, then group_commit_ctx should be set to NULL to indicate
to the partitioning handler that all partitions handlers are committed.
@see prepare_inplace_alter_table().
@param altered_table TABLE object for new version of table.
@param ha_alter_info Structure describing changes to be done
by ALTER TABLE and holding data used
during in-place alter.
@param commit True => Commit, False => Rollback.
@retval true Error
@retval false Success
*/
bool ha_rocksdb::commit_inplace_alter_table(
my_core::TABLE *const altered_table,
my_core::Alter_inplace_info *const ha_alter_info, bool commit) {
DBUG_ENTER_FUNC();
DBUG_ASSERT(altered_table != nullptr);
DBUG_ASSERT(ha_alter_info != nullptr);
Rdb_inplace_alter_ctx *const ctx0 =
static_cast<Rdb_inplace_alter_ctx *>(ha_alter_info->handler_ctx);
DEBUG_SYNC(ha_thd(), "rocksdb.commit_in_place_alter_table");
/*
IMPORTANT: When rollback is requested, mysql will abort with
an assertion failure. That means every failed commit during inplace alter
table will result in a fatal error on the server. Indexes ongoing creation
will be detected when the server restarts, and dropped.
For partitioned tables, a rollback call to this function (commit == false)
is done for each partition. A successful commit call only executes once
for all partitions.
*/
if (!commit) {
/* If ctx has not been created yet, nothing to do here */
if (!ctx0) {
DBUG_RETURN(HA_EXIT_SUCCESS);
}
/*
Cannot call destructor for Rdb_tbl_def directly because we don't want to
erase the mappings inside the ddl_manager, as the old_key_descr is still
using them.
*/
if (ctx0->m_new_key_descr) {
/* Delete the new key descriptors */
for (uint i = 0; i < ctx0->m_new_tdef->m_key_count; i++) {
ctx0->m_new_key_descr[i] = nullptr;
}
delete[] ctx0->m_new_key_descr;
ctx0->m_new_key_descr = nullptr;
ctx0->m_new_tdef->m_key_descr_arr = nullptr;
delete ctx0->m_new_tdef;
}
/* Remove uncommitted key definitons from ddl_manager */
ddl_manager.remove_uncommitted_keydefs(ctx0->m_added_indexes);
/* Rollback any partially created indexes */
dict_manager.rollback_ongoing_index_creation();
DBUG_RETURN(HA_EXIT_SUCCESS);
}
DBUG_ASSERT(ctx0);
/*
For partitioned tables, we need to commit all changes to all tables at
once, unlike in the other inplace alter API methods.
*/
inplace_alter_handler_ctx **ctx_array;
inplace_alter_handler_ctx *ctx_single[2];
if (ha_alter_info->group_commit_ctx) {
DBUG_EXECUTE_IF("crash_during_index_creation_partition", DBUG_SUICIDE(););
ctx_array = ha_alter_info->group_commit_ctx;
} else {
ctx_single[0] = ctx0;
ctx_single[1] = nullptr;
ctx_array = ctx_single;
}
DBUG_ASSERT(ctx0 == ctx_array[0]);
ha_alter_info->group_commit_ctx = nullptr;
if (ha_alter_info->handler_flags &
(my_core::Alter_inplace_info::DROP_INDEX |
my_core::Alter_inplace_info::DROP_UNIQUE_INDEX |
my_core::Alter_inplace_info::ADD_INDEX |
my_core::Alter_inplace_info::ADD_UNIQUE_INDEX)) {
const std::unique_ptr<rocksdb::WriteBatch> wb = dict_manager.begin();
rocksdb::WriteBatch *const batch = wb.get();
std::unordered_set<GL_INDEX_ID> create_index_ids;
m_tbl_def = ctx0->m_new_tdef;
m_key_descr_arr = m_tbl_def->m_key_descr_arr;
m_pk_descr = m_key_descr_arr[pk_index(altered_table, m_tbl_def)];
dict_manager.lock();
for (inplace_alter_handler_ctx **pctx = ctx_array; *pctx; pctx++) {
Rdb_inplace_alter_ctx *const ctx =
static_cast<Rdb_inplace_alter_ctx *>(*pctx);
/* Mark indexes to be dropped */
dict_manager.add_drop_index(ctx->m_dropped_index_ids, batch);
for (const auto &index : ctx->m_added_indexes) {
create_index_ids.insert(index->get_gl_index_id());
}
if (ddl_manager.put_and_write(ctx->m_new_tdef, batch)) {
/*
Failed to write new entry into data dictionary, this should never
happen.
*/
DBUG_ASSERT(0);
}
/*
Remove uncommitted key definitons from ddl_manager, as they are now
committed into the data dictionary.
*/
ddl_manager.remove_uncommitted_keydefs(ctx->m_added_indexes);
}
if (dict_manager.commit(batch)) {
/*
Should never reach here. We assume MyRocks will abort if commit fails.
*/
DBUG_ASSERT(0);
}
dict_manager.unlock();
/* Mark ongoing create indexes as finished/remove from data dictionary */
dict_manager.finish_indexes_operation(
create_index_ids, Rdb_key_def::DDL_CREATE_INDEX_ONGOING);
/*
We need to recalculate the index stats here manually. The reason is that
the secondary index does not exist inside
m_index_num_to_keydef until it is committed to the data dictionary, which
prevents us from updating the stats normally as the ddl_manager cannot
find the proper gl_index_ids yet during adjust_stats calls.
*/
if (calculate_stats(altered_table, nullptr, nullptr)) {
/* Failed to update index statistics, should never happen */
DBUG_ASSERT(0);
}
rdb_drop_idx_thread.signal();
}
DBUG_RETURN(HA_EXIT_SUCCESS);
}
#define SHOW_FNAME(name) rocksdb_show_##name
#define DEF_SHOW_FUNC(name, key) \
static int SHOW_FNAME(name)(MYSQL_THD thd, SHOW_VAR * var, char *buff) { \
rocksdb_status_counters.name = \
rocksdb_stats->getTickerCount(rocksdb::key); \
var->type = SHOW_LONGLONG; \
var->value = (char *)&rocksdb_status_counters.name; \
return HA_EXIT_SUCCESS; \
}
#define DEF_STATUS_VAR(name) \
{ "rocksdb_" #name, (char *)&SHOW_FNAME(name), SHOW_FUNC }
#define DEF_STATUS_VAR_PTR(name, ptr, option) \
{ "rocksdb_" name, (char *)ptr, option }
#define DEF_STATUS_VAR_FUNC(name, ptr, option) \
{ name, reinterpret_cast<char *>(ptr), option }
struct rocksdb_status_counters_t {
uint64_t block_cache_miss;
uint64_t block_cache_hit;
uint64_t block_cache_add;
uint64_t block_cache_index_miss;
uint64_t block_cache_index_hit;
uint64_t block_cache_filter_miss;
uint64_t block_cache_filter_hit;
uint64_t block_cache_data_miss;
uint64_t block_cache_data_hit;
uint64_t bloom_filter_useful;
uint64_t memtable_hit;
uint64_t memtable_miss;
uint64_t compaction_key_drop_new;
uint64_t compaction_key_drop_obsolete;
uint64_t compaction_key_drop_user;
uint64_t number_keys_written;
uint64_t number_keys_read;
uint64_t number_keys_updated;
uint64_t bytes_written;
uint64_t bytes_read;
uint64_t no_file_closes;
uint64_t no_file_opens;
uint64_t no_file_errors;
uint64_t stall_micros;
uint64_t rate_limit_delay_millis;
uint64_t num_iterators;
uint64_t number_multiget_get;
uint64_t number_multiget_keys_read;
uint64_t number_multiget_bytes_read;
uint64_t number_deletes_filtered;
uint64_t number_merge_failures;
uint64_t bloom_filter_prefix_checked;
uint64_t bloom_filter_prefix_useful;
uint64_t number_reseeks_iteration;
uint64_t getupdatessince_calls;
uint64_t block_cachecompressed_miss;
uint64_t block_cachecompressed_hit;
uint64_t wal_synced;
uint64_t wal_bytes;
uint64_t write_self;
uint64_t write_other;
uint64_t write_timedout;
uint64_t write_wal;
uint64_t flush_write_bytes;
uint64_t compact_read_bytes;
uint64_t compact_write_bytes;
uint64_t number_superversion_acquires;
uint64_t number_superversion_releases;
uint64_t number_superversion_cleanups;
uint64_t number_block_not_compressed;
};
static rocksdb_status_counters_t rocksdb_status_counters;
DEF_SHOW_FUNC(block_cache_miss, BLOCK_CACHE_MISS)
DEF_SHOW_FUNC(block_cache_hit, BLOCK_CACHE_HIT)
DEF_SHOW_FUNC(block_cache_add, BLOCK_CACHE_ADD)
DEF_SHOW_FUNC(block_cache_index_miss, BLOCK_CACHE_INDEX_MISS)
DEF_SHOW_FUNC(block_cache_index_hit, BLOCK_CACHE_INDEX_HIT)
DEF_SHOW_FUNC(block_cache_filter_miss, BLOCK_CACHE_FILTER_MISS)
DEF_SHOW_FUNC(block_cache_filter_hit, BLOCK_CACHE_FILTER_HIT)
DEF_SHOW_FUNC(block_cache_data_miss, BLOCK_CACHE_DATA_MISS)
DEF_SHOW_FUNC(block_cache_data_hit, BLOCK_CACHE_DATA_HIT)
DEF_SHOW_FUNC(bloom_filter_useful, BLOOM_FILTER_USEFUL)
DEF_SHOW_FUNC(memtable_hit, MEMTABLE_HIT)
DEF_SHOW_FUNC(memtable_miss, MEMTABLE_MISS)
DEF_SHOW_FUNC(compaction_key_drop_new, COMPACTION_KEY_DROP_NEWER_ENTRY)
DEF_SHOW_FUNC(compaction_key_drop_obsolete, COMPACTION_KEY_DROP_OBSOLETE)
DEF_SHOW_FUNC(compaction_key_drop_user, COMPACTION_KEY_DROP_USER)
DEF_SHOW_FUNC(number_keys_written, NUMBER_KEYS_WRITTEN)
DEF_SHOW_FUNC(number_keys_read, NUMBER_KEYS_READ)
DEF_SHOW_FUNC(number_keys_updated, NUMBER_KEYS_UPDATED)
DEF_SHOW_FUNC(bytes_written, BYTES_WRITTEN)
DEF_SHOW_FUNC(bytes_read, BYTES_READ)
DEF_SHOW_FUNC(no_file_closes, NO_FILE_CLOSES)
DEF_SHOW_FUNC(no_file_opens, NO_FILE_OPENS)
DEF_SHOW_FUNC(no_file_errors, NO_FILE_ERRORS)
DEF_SHOW_FUNC(stall_micros, STALL_MICROS)
DEF_SHOW_FUNC(rate_limit_delay_millis, RATE_LIMIT_DELAY_MILLIS)
DEF_SHOW_FUNC(num_iterators, NO_ITERATORS)
DEF_SHOW_FUNC(number_multiget_get, NUMBER_MULTIGET_CALLS)
DEF_SHOW_FUNC(number_multiget_keys_read, NUMBER_MULTIGET_KEYS_READ)
DEF_SHOW_FUNC(number_multiget_bytes_read, NUMBER_MULTIGET_BYTES_READ)
DEF_SHOW_FUNC(number_deletes_filtered, NUMBER_FILTERED_DELETES)
DEF_SHOW_FUNC(number_merge_failures, NUMBER_MERGE_FAILURES)
DEF_SHOW_FUNC(bloom_filter_prefix_checked, BLOOM_FILTER_PREFIX_CHECKED)
DEF_SHOW_FUNC(bloom_filter_prefix_useful, BLOOM_FILTER_PREFIX_USEFUL)
DEF_SHOW_FUNC(number_reseeks_iteration, NUMBER_OF_RESEEKS_IN_ITERATION)
DEF_SHOW_FUNC(getupdatessince_calls, GET_UPDATES_SINCE_CALLS)
DEF_SHOW_FUNC(block_cachecompressed_miss, BLOCK_CACHE_COMPRESSED_MISS)
DEF_SHOW_FUNC(block_cachecompressed_hit, BLOCK_CACHE_COMPRESSED_HIT)
DEF_SHOW_FUNC(wal_synced, WAL_FILE_SYNCED)
DEF_SHOW_FUNC(wal_bytes, WAL_FILE_BYTES)
DEF_SHOW_FUNC(write_self, WRITE_DONE_BY_SELF)
DEF_SHOW_FUNC(write_other, WRITE_DONE_BY_OTHER)
DEF_SHOW_FUNC(write_timedout, WRITE_TIMEDOUT)
DEF_SHOW_FUNC(write_wal, WRITE_WITH_WAL)
DEF_SHOW_FUNC(flush_write_bytes, FLUSH_WRITE_BYTES)
DEF_SHOW_FUNC(compact_read_bytes, COMPACT_READ_BYTES)
DEF_SHOW_FUNC(compact_write_bytes, COMPACT_WRITE_BYTES)
DEF_SHOW_FUNC(number_superversion_acquires, NUMBER_SUPERVERSION_ACQUIRES)
DEF_SHOW_FUNC(number_superversion_releases, NUMBER_SUPERVERSION_RELEASES)
DEF_SHOW_FUNC(number_superversion_cleanups, NUMBER_SUPERVERSION_CLEANUPS)
DEF_SHOW_FUNC(number_block_not_compressed, NUMBER_BLOCK_NOT_COMPRESSED)
static void myrocks_update_status() {
export_stats.rows_deleted = global_stats.rows[ROWS_DELETED];
export_stats.rows_inserted = global_stats.rows[ROWS_INSERTED];
export_stats.rows_read = global_stats.rows[ROWS_READ];
export_stats.rows_updated = global_stats.rows[ROWS_UPDATED];
export_stats.rows_deleted_blind = global_stats.rows[ROWS_DELETED_BLIND];
export_stats.rows_expired = global_stats.rows[ROWS_EXPIRED];
export_stats.system_rows_deleted = global_stats.system_rows[ROWS_DELETED];
export_stats.system_rows_inserted = global_stats.system_rows[ROWS_INSERTED];
export_stats.system_rows_read = global_stats.system_rows[ROWS_READ];
export_stats.system_rows_updated = global_stats.system_rows[ROWS_UPDATED];
export_stats.queries_point = global_stats.queries[QUERIES_POINT];
export_stats.queries_range = global_stats.queries[QUERIES_RANGE];
export_stats.covered_secondary_key_lookups =
global_stats.covered_secondary_key_lookups;
}
static void myrocks_update_memory_status() {
std::vector<rocksdb::DB *> dbs;
std::unordered_set<const rocksdb::Cache *> cache_set;
dbs.push_back(rdb);
std::map<rocksdb::MemoryUtil::UsageType, uint64_t> temp_usage_by_type;
rocksdb::MemoryUtil::GetApproximateMemoryUsageByType(dbs, cache_set,
&temp_usage_by_type);
memory_stats.memtable_total =
temp_usage_by_type[rocksdb::MemoryUtil::kMemTableTotal];
memory_stats.memtable_unflushed =
temp_usage_by_type[rocksdb::MemoryUtil::kMemTableUnFlushed];
}
static SHOW_VAR myrocks_status_variables[] = {
DEF_STATUS_VAR_FUNC("rows_deleted", &export_stats.rows_deleted,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("rows_inserted", &export_stats.rows_inserted,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("rows_read", &export_stats.rows_read, SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("rows_updated", &export_stats.rows_updated,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("rows_deleted_blind", &export_stats.rows_deleted_blind,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("rows_expired", &export_stats.rows_expired,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("system_rows_deleted",
&export_stats.system_rows_deleted, SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("system_rows_inserted",
&export_stats.system_rows_inserted, SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("system_rows_read", &export_stats.system_rows_read,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("system_rows_updated",
&export_stats.system_rows_updated, SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("memtable_total", &memory_stats.memtable_total,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("memtable_unflushed", &memory_stats.memtable_unflushed,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("queries_point", &export_stats.queries_point,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("queries_range", &export_stats.queries_range,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("covered_secondary_key_lookups",
&export_stats.covered_secondary_key_lookups,
SHOW_LONGLONG),
{NullS, NullS, SHOW_LONG}};
static void show_myrocks_vars(THD *thd, SHOW_VAR *var, char *buff) {
myrocks_update_status();
myrocks_update_memory_status();
var->type = SHOW_ARRAY;
var->value = reinterpret_cast<char *>(&myrocks_status_variables);
}
static ulonglong
io_stall_prop_value(const std::map<std::string, std::string> &props,
const std::string &key) {
std::map<std::string, std::string>::const_iterator iter =
props.find("io_stalls." + key);
if (iter != props.end()) {
return std::stoull(iter->second);
} else {
DBUG_PRINT("warning",
("RocksDB GetMapPropery hasn't returned key=%s", key.c_str()));
DBUG_ASSERT(0);
return 0;
}
}
static void update_rocksdb_stall_status() {
st_io_stall_stats local_io_stall_stats;
for (const auto &cf_name : cf_manager.get_cf_names()) {
rocksdb::ColumnFamilyHandle *cfh = cf_manager.get_cf(cf_name);
if (cfh == nullptr) {
continue;
}
std::map<std::string, std::string> props;
if (!rdb->GetMapProperty(cfh, "rocksdb.cfstats", &props)) {
continue;
}
local_io_stall_stats.level0_slowdown +=
io_stall_prop_value(props, "level0_slowdown");
local_io_stall_stats.level0_slowdown_with_compaction +=
io_stall_prop_value(props, "level0_slowdown_with_compaction");
local_io_stall_stats.level0_numfiles +=
io_stall_prop_value(props, "level0_numfiles");
local_io_stall_stats.level0_numfiles_with_compaction +=
io_stall_prop_value(props, "level0_numfiles_with_compaction");
local_io_stall_stats.stop_for_pending_compaction_bytes +=
io_stall_prop_value(props, "stop_for_pending_compaction_bytes");
local_io_stall_stats.slowdown_for_pending_compaction_bytes +=
io_stall_prop_value(props, "slowdown_for_pending_compaction_bytes");
local_io_stall_stats.memtable_compaction +=
io_stall_prop_value(props, "memtable_compaction");
local_io_stall_stats.memtable_slowdown +=
io_stall_prop_value(props, "memtable_slowdown");
local_io_stall_stats.total_stop += io_stall_prop_value(props, "total_stop");
local_io_stall_stats.total_slowdown +=
io_stall_prop_value(props, "total_slowdown");
}
io_stall_stats = local_io_stall_stats;
}
static SHOW_VAR rocksdb_stall_status_variables[] = {
DEF_STATUS_VAR_FUNC("l0_file_count_limit_slowdowns",
&io_stall_stats.level0_slowdown, SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("locked_l0_file_count_limit_slowdowns",
&io_stall_stats.level0_slowdown_with_compaction,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("l0_file_count_limit_stops",
&io_stall_stats.level0_numfiles, SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("locked_l0_file_count_limit_stops",
&io_stall_stats.level0_numfiles_with_compaction,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("pending_compaction_limit_stops",
&io_stall_stats.stop_for_pending_compaction_bytes,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("pending_compaction_limit_slowdowns",
&io_stall_stats.slowdown_for_pending_compaction_bytes,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("memtable_limit_stops",
&io_stall_stats.memtable_compaction, SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("memtable_limit_slowdowns",
&io_stall_stats.memtable_slowdown, SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("total_stops", &io_stall_stats.total_stop,
SHOW_LONGLONG),
DEF_STATUS_VAR_FUNC("total_slowdowns", &io_stall_stats.total_slowdown,
SHOW_LONGLONG),
// end of the array marker
{NullS, NullS, SHOW_LONG}};
static void show_rocksdb_stall_vars(THD *thd, SHOW_VAR *var, char *buff) {
update_rocksdb_stall_status();
var->type = SHOW_ARRAY;
var->value = reinterpret_cast<char *>(&rocksdb_stall_status_variables);
}
static SHOW_VAR rocksdb_status_vars[] = {
DEF_STATUS_VAR(block_cache_miss),
DEF_STATUS_VAR(block_cache_hit),
DEF_STATUS_VAR(block_cache_add),
DEF_STATUS_VAR(block_cache_index_miss),
DEF_STATUS_VAR(block_cache_index_hit),
DEF_STATUS_VAR(block_cache_filter_miss),
DEF_STATUS_VAR(block_cache_filter_hit),
DEF_STATUS_VAR(block_cache_data_miss),
DEF_STATUS_VAR(block_cache_data_hit),
DEF_STATUS_VAR(bloom_filter_useful),
DEF_STATUS_VAR(memtable_hit),
DEF_STATUS_VAR(memtable_miss),
DEF_STATUS_VAR(compaction_key_drop_new),
DEF_STATUS_VAR(compaction_key_drop_obsolete),
DEF_STATUS_VAR(compaction_key_drop_user),
DEF_STATUS_VAR(number_keys_written),
DEF_STATUS_VAR(number_keys_read),
DEF_STATUS_VAR(number_keys_updated),
DEF_STATUS_VAR(bytes_written),
DEF_STATUS_VAR(bytes_read),
DEF_STATUS_VAR(no_file_closes),
DEF_STATUS_VAR(no_file_opens),
DEF_STATUS_VAR(no_file_errors),
DEF_STATUS_VAR(stall_micros),
DEF_STATUS_VAR(rate_limit_delay_millis),
DEF_STATUS_VAR(num_iterators),
DEF_STATUS_VAR(number_multiget_get),
DEF_STATUS_VAR(number_multiget_keys_read),
DEF_STATUS_VAR(number_multiget_bytes_read),
DEF_STATUS_VAR(number_deletes_filtered),
DEF_STATUS_VAR(number_merge_failures),
DEF_STATUS_VAR(bloom_filter_prefix_checked),
DEF_STATUS_VAR(bloom_filter_prefix_useful),
DEF_STATUS_VAR(number_reseeks_iteration),
DEF_STATUS_VAR(getupdatessince_calls),
DEF_STATUS_VAR(block_cachecompressed_miss),
DEF_STATUS_VAR(block_cachecompressed_hit),
DEF_STATUS_VAR(wal_synced),
DEF_STATUS_VAR(wal_bytes),
DEF_STATUS_VAR(write_self),
DEF_STATUS_VAR(write_other),
DEF_STATUS_VAR(write_timedout),
DEF_STATUS_VAR(write_wal),
DEF_STATUS_VAR(flush_write_bytes),
DEF_STATUS_VAR(compact_read_bytes),
DEF_STATUS_VAR(compact_write_bytes),
DEF_STATUS_VAR(number_superversion_acquires),
DEF_STATUS_VAR(number_superversion_releases),
DEF_STATUS_VAR(number_superversion_cleanups),
DEF_STATUS_VAR(number_block_not_compressed),
DEF_STATUS_VAR_PTR("snapshot_conflict_errors",
&rocksdb_snapshot_conflict_errors, SHOW_LONGLONG),
DEF_STATUS_VAR_PTR("wal_group_syncs", &rocksdb_wal_group_syncs,
SHOW_LONGLONG),
DEF_STATUS_VAR_PTR("number_stat_computes", &rocksdb_number_stat_computes,
SHOW_LONGLONG),
DEF_STATUS_VAR_PTR("number_sst_entry_put", &rocksdb_num_sst_entry_put,
SHOW_LONGLONG),
DEF_STATUS_VAR_PTR("number_sst_entry_delete", &rocksdb_num_sst_entry_delete,
SHOW_LONGLONG),
DEF_STATUS_VAR_PTR("number_sst_entry_singledelete",
&rocksdb_num_sst_entry_singledelete, SHOW_LONGLONG),
DEF_STATUS_VAR_PTR("number_sst_entry_merge", &rocksdb_num_sst_entry_merge,
SHOW_LONGLONG),
DEF_STATUS_VAR_PTR("number_sst_entry_other", &rocksdb_num_sst_entry_other,
SHOW_LONGLONG),
// the variables generated by SHOW_FUNC are sorted only by prefix (first
// arg in the tuple below), so make sure it is unique to make sorting
// deterministic as quick sort is not stable
{"rocksdb", reinterpret_cast<char *>(&show_myrocks_vars), SHOW_FUNC},
{"rocksdb_stall", reinterpret_cast<char *>(&show_rocksdb_stall_vars),
SHOW_FUNC},
{NullS, NullS, SHOW_LONG}};
/*
Background thread's main logic
*/
void Rdb_background_thread::run() {
// How many seconds to wait till flushing the WAL next time.
const int WAKE_UP_INTERVAL = 1;
timespec ts_next_sync;
set_timespec(ts_next_sync, WAKE_UP_INTERVAL);
for (;;) {
// Wait until the next timeout or until we receive a signal to stop the
// thread. Request to stop the thread should only be triggered when the
// storage engine is being unloaded.
RDB_MUTEX_LOCK_CHECK(m_signal_mutex);
const auto ret MY_ATTRIBUTE((__unused__)) =
mysql_cond_timedwait(&m_signal_cond, &m_signal_mutex, &ts_next_sync);
// Check that we receive only the expected error codes.
DBUG_ASSERT(ret == 0 || ret == ETIMEDOUT);
const bool local_stop = m_stop;
const bool local_save_stats = m_save_stats;
reset();
RDB_MUTEX_UNLOCK_CHECK(m_signal_mutex);
if (local_stop) {
// If we're here then that's because condition variable was signaled by
// another thread and we're shutting down. Break out the loop to make
// sure that shutdown thread can proceed.
break;
}
// This path should be taken only when the timer expired.
DBUG_ASSERT(ret == ETIMEDOUT);
if (local_save_stats) {
ddl_manager.persist_stats();
}
// Set the next timestamp for mysql_cond_timedwait() (which ends up calling
// pthread_cond_timedwait()) to wait on.
set_timespec(ts_next_sync, WAKE_UP_INTERVAL);
// Flush the WAL. Sync it for both background and never modes to copy
// InnoDB's behavior. For mode never, the wal file isn't even written,
// whereas background writes to the wal file, but issues the syncs in a
// background thread.
if (rdb && (rocksdb_flush_log_at_trx_commit != FLUSH_LOG_SYNC)) {
DBUG_ASSERT(!rocksdb_db_options->allow_mmap_writes);
const rocksdb::Status s = rdb->FlushWAL(true);
if (!s.ok()) {
rdb_handle_io_error(s, RDB_IO_ERROR_BG_THREAD);
}
}
}
// save remaining stats which might've left unsaved
ddl_manager.persist_stats();
}
/**
Deciding if it is possible to use bloom filter or not.
@detail
Even if bloom filter exists, it is not always possible
to use bloom filter. If using bloom filter when you shouldn't,
false negative may happen -- fewer rows than expected may be returned.
It is users' responsibility to use bloom filter correctly.
If bloom filter does not exist, return value does not matter because
RocksDB does not use bloom filter internally.
@param kd
@param eq_cond Equal condition part of the key. This always includes
system index id (4 bytes).
@param use_all_keys True if all key parts are set with equal conditions.
This is aware of extended keys.
*/
bool can_use_bloom_filter(THD *thd, const Rdb_key_def &kd,
const rocksdb::Slice &eq_cond,
const bool use_all_keys) {
bool can_use = false;
if (THDVAR(thd, skip_bloom_filter_on_read)) {
return can_use;
}
const rocksdb::SliceTransform *prefix_extractor = kd.get_extractor();
if (prefix_extractor) {
/*
This is an optimized use case for CappedPrefixTransform.
If eq_cond length >= prefix extractor length and if
all keys are used for equal lookup, it is
always possible to use bloom filter.
Prefix bloom filter can't be used on descending scan with
prefix lookup (i.e. WHERE id1=1 ORDER BY id2 DESC), because of
RocksDB's limitation. On ascending (or not sorting) scan,
keys longer than the capped prefix length will be truncated down
to the capped length and the resulting key is added to the bloom filter.
Keys shorter than the capped prefix length will be added to
the bloom filter. When keys are looked up, key conditionals
longer than the capped length can be used; key conditionals
shorter require all parts of the key to be available
for the short key match.
*/
if ((use_all_keys && prefix_extractor->InRange(eq_cond))
|| prefix_extractor->SameResultWhenAppended(eq_cond))
can_use = true;
else
can_use = false;
} else {
/*
if prefix extractor is not defined, all key parts have to be
used by eq_cond.
*/
if (use_all_keys)
can_use = true;
else
can_use = false;
}
return can_use;
}
/* For modules that need access to the global data structures */
rocksdb::TransactionDB *rdb_get_rocksdb_db() { return rdb; }
Rdb_cf_manager &rdb_get_cf_manager() { return cf_manager; }
const rocksdb::BlockBasedTableOptions &rdb_get_table_options() {
return *rocksdb_tbl_options;
}
bool rdb_is_ttl_enabled() { return rocksdb_enable_ttl; }
bool rdb_is_ttl_read_filtering_enabled() {
return rocksdb_enable_ttl_read_filtering;
}
#ifndef NDEBUG
int rdb_dbug_set_ttl_rec_ts() { return rocksdb_debug_ttl_rec_ts; }
int rdb_dbug_set_ttl_snapshot_ts() { return rocksdb_debug_ttl_snapshot_ts; }
int rdb_dbug_set_ttl_read_filter_ts() {
return rocksdb_debug_ttl_read_filter_ts;
}
bool rdb_dbug_set_ttl_ignore_pk() { return rocksdb_debug_ttl_ignore_pk; }
#endif
void rdb_update_global_stats(const operation_type &type, uint count,
bool is_system_table) {
DBUG_ASSERT(type < ROWS_MAX);
if (count == 0) {
return;
}
if (is_system_table) {
global_stats.system_rows[type].add(count);
} else {
global_stats.rows[type].add(count);
}
}
int rdb_get_table_perf_counters(const char *const tablename,
Rdb_perf_counters *const counters) {
DBUG_ASSERT(counters != nullptr);
DBUG_ASSERT(tablename != nullptr);
Rdb_table_handler *table_handler;
table_handler = rdb_open_tables.get_table_handler(tablename);
if (table_handler == nullptr) {
return HA_ERR_ROCKSDB_INVALID_TABLE;
}
counters->load(table_handler->m_table_perf_context);
rdb_open_tables.release_table_handler(table_handler);
return HA_EXIT_SUCCESS;
}
const char *get_rdb_io_error_string(const RDB_IO_ERROR_TYPE err_type) {
// If this assertion fails then this means that a member has been either added
// to or removed from RDB_IO_ERROR_TYPE enum and this function needs to be
// changed to return the appropriate value.
static_assert(RDB_IO_ERROR_LAST == 4, "Please handle all the error types.");
switch (err_type) {
case RDB_IO_ERROR_TYPE::RDB_IO_ERROR_TX_COMMIT:
return "RDB_IO_ERROR_TX_COMMIT";
case RDB_IO_ERROR_TYPE::RDB_IO_ERROR_DICT_COMMIT:
return "RDB_IO_ERROR_DICT_COMMIT";
case RDB_IO_ERROR_TYPE::RDB_IO_ERROR_BG_THREAD:
return "RDB_IO_ERROR_BG_THREAD";
case RDB_IO_ERROR_TYPE::RDB_IO_ERROR_GENERAL:
return "RDB_IO_ERROR_GENERAL";
default:
DBUG_ASSERT(false);
return "(unknown)";
}
}
// In case of core dump generation we want this function NOT to be optimized
// so that we can capture as much data as possible to debug the root cause
// more efficiently.
#ifdef __GNUC__
#pragma GCC push_options
#pragma GCC optimize("O0")
#endif
void rdb_handle_io_error(const rocksdb::Status status,
const RDB_IO_ERROR_TYPE err_type) {
if (status.IsIOError()) {
switch (err_type) {
case RDB_IO_ERROR_TX_COMMIT:
case RDB_IO_ERROR_DICT_COMMIT: {
rdb_log_status_error(status, "failed to write to WAL");
/* NO_LINT_DEBUG */
sql_print_error("MyRocks: aborting on WAL write error.");
abort_with_stack_traces();
break;
}
case RDB_IO_ERROR_BG_THREAD: {
rdb_log_status_error(status, "BG thread failed to write to RocksDB");
break;
}
case RDB_IO_ERROR_GENERAL: {
rdb_log_status_error(status, "failed on I/O");
/* NO_LINT_DEBUG */
sql_print_error("MyRocks: aborting on I/O error.");
abort_with_stack_traces();
break;
}
default:
DBUG_ASSERT(0);
break;
}
} else if (status.IsCorruption()) {
rdb_log_status_error(status, "data corruption detected!");
/* NO_LINT_DEBUG */
sql_print_error("MyRocks: aborting because of data corruption.");
abort_with_stack_traces();
} else if (!status.ok()) {
switch (err_type) {
case RDB_IO_ERROR_DICT_COMMIT: {
rdb_log_status_error(status, "Failed to write to WAL (dictionary)");
/* NO_LINT_DEBUG */
sql_print_error("MyRocks: aborting on WAL write error.");
abort_with_stack_traces();
break;
}
default:
rdb_log_status_error(status, "Failed to read/write in RocksDB");
break;
}
}
}
#ifdef __GNUC__
#pragma GCC pop_options
#endif
Rdb_dict_manager *rdb_get_dict_manager(void) { return &dict_manager; }
Rdb_ddl_manager *rdb_get_ddl_manager(void) { return &ddl_manager; }
Rdb_binlog_manager *rdb_get_binlog_manager(void) { return &binlog_manager; }
void rocksdb_set_compaction_options(
my_core::THD *const thd MY_ATTRIBUTE((__unused__)),
my_core::st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr, const void *const save) {
if (var_ptr && save) {
*(uint64_t *)var_ptr = *(const uint64_t *)save;
}
const Rdb_compact_params params = {
(uint64_t)rocksdb_compaction_sequential_deletes,
(uint64_t)rocksdb_compaction_sequential_deletes_window,
(uint64_t)rocksdb_compaction_sequential_deletes_file_size};
if (properties_collector_factory) {
properties_collector_factory->SetCompactionParams(params);
}
}
void rocksdb_set_table_stats_sampling_pct(
my_core::THD *const thd MY_ATTRIBUTE((__unused__)),
my_core::st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr MY_ATTRIBUTE((__unused__)), const void *const save) {
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
const uint32_t new_val = *static_cast<const uint32_t *>(save);
if (new_val != rocksdb_table_stats_sampling_pct) {
rocksdb_table_stats_sampling_pct = new_val;
if (properties_collector_factory) {
properties_collector_factory->SetTableStatsSamplingPct(
rocksdb_table_stats_sampling_pct);
}
}
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
/*
This function allows setting the rate limiter's bytes per second value
but only if the rate limiter is turned on which has to be done at startup.
If the rate is already 0 (turned off) or we are changing it to 0 (trying
to turn it off) this function will push a warning to the client and do
nothing.
This is similar to the code in innodb_doublewrite_update (found in
storage/innobase/handler/ha_innodb.cc).
*/
void rocksdb_set_rate_limiter_bytes_per_sec(
my_core::THD *const thd,
my_core::st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr MY_ATTRIBUTE((__unused__)), const void *const save) {
const uint64_t new_val = *static_cast<const uint64_t *>(save);
if (new_val == 0 || rocksdb_rate_limiter_bytes_per_sec == 0) {
/*
If a rate_limiter was not enabled at startup we can't change it nor
can we disable it if one was created at startup
*/
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, ER_WRONG_ARGUMENTS,
"RocksDB: rocksdb_rate_limiter_bytes_per_sec cannot "
"be dynamically changed to or from 0. Do a clean "
"shutdown if you want to change it from or to 0.");
} else if (new_val != rocksdb_rate_limiter_bytes_per_sec) {
/* Apply the new value to the rate limiter and store it locally */
DBUG_ASSERT(rocksdb_rate_limiter != nullptr);
rocksdb_rate_limiter_bytes_per_sec = new_val;
rocksdb_rate_limiter->SetBytesPerSecond(new_val);
}
}
void rocksdb_set_sst_mgr_rate_bytes_per_sec(
my_core::THD *const thd,
my_core::st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr MY_ATTRIBUTE((__unused__)), const void *const save) {
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
const uint64_t new_val = *static_cast<const uint64_t *>(save);
if (new_val != rocksdb_sst_mgr_rate_bytes_per_sec) {
rocksdb_sst_mgr_rate_bytes_per_sec = new_val;
rocksdb_db_options->sst_file_manager->SetDeleteRateBytesPerSecond(
rocksdb_sst_mgr_rate_bytes_per_sec);
}
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
void rocksdb_set_delayed_write_rate(THD *thd, struct st_mysql_sys_var *var,
void *var_ptr, const void *save) {
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
const uint64_t new_val = *static_cast<const uint64_t *>(save);
if (rocksdb_delayed_write_rate != new_val) {
rocksdb_delayed_write_rate = new_val;
rocksdb::Status s =
rdb->SetDBOptions({{"delayed_write_rate", std::to_string(new_val)}});
if (!s.ok()) {
/* NO_LINT_DEBUG */
sql_print_warning("MyRocks: failed to update delayed_write_rate. "
"status code = %d, status = %s",
s.code(), s.ToString().c_str());
}
}
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
void rocksdb_set_max_latest_deadlocks(THD *thd, struct st_mysql_sys_var *var,
void *var_ptr, const void *save) {
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
const uint32_t new_val = *static_cast<const uint32_t *>(save);
if (rocksdb_max_latest_deadlocks != new_val) {
rocksdb_max_latest_deadlocks = new_val;
rdb->SetDeadlockInfoBufferSize(rocksdb_max_latest_deadlocks);
}
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
void rdb_set_collation_exception_list(const char *const exception_list) {
DBUG_ASSERT(rdb_collation_exceptions != nullptr);
if (!rdb_collation_exceptions->set_patterns(exception_list)) {
my_core::warn_about_bad_patterns(rdb_collation_exceptions,
"strict_collation_exceptions");
}
}
void rocksdb_set_collation_exception_list(THD *const thd,
struct st_mysql_sys_var *const var,
void *const var_ptr,
const void *const save) {
const char *const val = *static_cast<const char *const *>(save);
rdb_set_collation_exception_list(val == nullptr ? "" : val);
//psergey-todo: what is the purpose of the below??
const char *val_copy= val? my_strdup(val, MYF(0)): nullptr;
my_free(*static_cast<char**>(var_ptr));
*static_cast<const char**>(var_ptr) = val_copy;
}
void rocksdb_set_bulk_load(THD *const thd, struct st_mysql_sys_var *const var
MY_ATTRIBUTE((__unused__)),
void *const var_ptr, const void *const save) {
Rdb_transaction *&tx = get_tx_from_thd(thd);
if (tx != nullptr) {
const int rc = tx->finish_bulk_load();
if (rc != 0) {
// NO_LINT_DEBUG
sql_print_error("RocksDB: Error %d finalizing last SST file while "
"setting bulk loading variable",
rc);
/*
MariaDB doesn't do the following:
abort_with_stack_traces();
because it doesn't seem a good idea to crash a server when a user makes
a mistake.
Instead, we return an error to the user. The error has already been
produced inside ha_rocksdb::finalize_bulk_load().
*/
}
}
*static_cast<bool *>(var_ptr) = *static_cast<const bool *>(save);
}
void rocksdb_set_bulk_load_allow_unsorted(
THD *const thd,
struct st_mysql_sys_var *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr, const void *const save) {
if (THDVAR(thd, bulk_load)) {
my_error(ER_ERROR_WHEN_EXECUTING_COMMAND, MYF(0), "SET",
"Cannot change this setting while bulk load is enabled");
} else {
*static_cast<bool *>(var_ptr) = *static_cast<const bool *>(save);
}
}
static void rocksdb_set_max_background_jobs(THD *thd,
struct st_mysql_sys_var *const var,
void *const var_ptr,
const void *const save) {
DBUG_ASSERT(save != nullptr);
DBUG_ASSERT(rocksdb_db_options != nullptr);
DBUG_ASSERT(rocksdb_db_options->env != nullptr);
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
const int new_val = *static_cast<const int *>(save);
if (rocksdb_db_options->max_background_jobs != new_val) {
rocksdb_db_options->max_background_jobs = new_val;
rocksdb::Status s =
rdb->SetDBOptions({{"max_background_jobs", std::to_string(new_val)}});
if (!s.ok()) {
/* NO_LINT_DEBUG */
sql_print_warning("MyRocks: failed to update max_background_jobs. "
"Status code = %d, status = %s.",
s.code(), s.ToString().c_str());
}
}
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
void rocksdb_set_update_cf_options(THD *const /* unused */,
struct st_mysql_sys_var *const /* unused */,
void *const var_ptr,
const void *const save) {
const char *const val = *static_cast<const char *const *>(save);
if (!val) {
// NO_LINT_DEBUG
sql_print_warning("MyRocks: NULL is not a valid option for updates to "
"column family settings.");
return;
}
RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
DBUG_ASSERT(val != nullptr);
// Do the real work of applying the changes.
Rdb_cf_options::Name_to_config_t option_map;
// Basic sanity checking and parsing the options into a map. If this fails
// then there's no point to proceed.
if (!Rdb_cf_options::parse_cf_options(val, &option_map)) {
*reinterpret_cast<char**>(var_ptr) = nullptr;
// NO_LINT_DEBUG
sql_print_warning("MyRocks: failed to parse the updated column family "
"options = '%s'.", val);
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
return;
}
// For each CF we have, see if we need to update any settings.
for (const auto &cf_name : cf_manager.get_cf_names()) {
DBUG_ASSERT(!cf_name.empty());
rocksdb::ColumnFamilyHandle *cfh = cf_manager.get_cf(cf_name);
DBUG_ASSERT(cfh != nullptr);
const auto it = option_map.find(cf_name);
std::string per_cf_options = (it != option_map.end()) ? it->second : "";
if (!per_cf_options.empty()) {
Rdb_cf_options::Name_to_config_t opt_map;
rocksdb::Status s = rocksdb::StringToMap(per_cf_options, &opt_map);
if (s != rocksdb::Status::OK()) {
// NO_LINT_DEBUG
sql_print_warning("MyRocks: failed to convert the options for column "
"family '%s' to a map. %s", cf_name.c_str(),
s.ToString().c_str());
} else {
DBUG_ASSERT(rdb != nullptr);
// Finally we can apply the options.
s = rdb->SetOptions(cfh, opt_map);
if (s != rocksdb::Status::OK()) {
// NO_LINT_DEBUG
sql_print_warning("MyRocks: failed to apply the options for column "
"family '%s'. %s", cf_name.c_str(),
s.ToString().c_str());
} else {
// NO_LINT_DEBUG
sql_print_information("MyRocks: options for column family '%s' "
"have been successfully updated.",
cf_name.c_str());
// Make sure that data is internally consistent as well and update
// the CF options. This is necessary also to make sure that the CF
// options will be correctly reflected in the relevant table:
// ROCKSDB_CF_OPTIONS in INFORMATION_SCHEMA.
rocksdb::ColumnFamilyOptions cf_options = rdb->GetOptions(cfh);
std::string updated_options;
s = rocksdb::GetStringFromColumnFamilyOptions(&updated_options,
cf_options);
DBUG_ASSERT(s == rocksdb::Status::OK());
DBUG_ASSERT(!updated_options.empty());
cf_manager.update_options_map(cf_name, updated_options);
}
}
}
}
// Reset the pointers regardless of how much success we had with updating
// the CF options. This will results in consistent behavior and avoids
// dealing with cases when only a subset of CF-s was successfully updated.
if (val) {
*reinterpret_cast<char**>(var_ptr) = my_strdup(val, MYF(0));
} else {
*reinterpret_cast<char**>(var_ptr) = nullptr;
}
// Our caller (`plugin_var_memalloc_global_update`) will call `my_free` to
// free up resources used before.
RDB_MUTEX_UNLOCK_CHECK(rdb_sysvars_mutex);
}
void rdb_queue_save_stats_request() { rdb_bg_thread.request_save_stats(); }
#ifdef MARIAROCKS_NOT_YET // MDEV-10976
void ha_rocksdb::rpl_before_delete_rows() {
DBUG_ENTER_FUNC();
m_in_rpl_delete_rows = true;
DBUG_VOID_RETURN;
}
void ha_rocksdb::rpl_after_delete_rows() {
DBUG_ENTER_FUNC();
m_in_rpl_delete_rows = false;
DBUG_VOID_RETURN;
}
void ha_rocksdb::rpl_before_update_rows() {
DBUG_ENTER_FUNC();
m_in_rpl_update_rows = true;
DBUG_VOID_RETURN;
}
void ha_rocksdb::rpl_after_update_rows() {
DBUG_ENTER_FUNC();
m_in_rpl_update_rows = false;
DBUG_VOID_RETURN;
}
/**
@brief
Read Free Replication can be used or not. Returning False means
Read Free Replication can be used. Read Free Replication can be used
on UPDATE or DELETE row events, and table must have user defined
primary key.
*/
bool ha_rocksdb::use_read_free_rpl() {
DBUG_ENTER_FUNC();
DBUG_RETURN((m_in_rpl_delete_rows || m_in_rpl_update_rows) &&
!has_hidden_pk(table) && m_use_read_free_rpl);
}
#endif // MARIAROCKS_NOT_YET
double ha_rocksdb::read_time(uint index, uint ranges, ha_rows rows) {
DBUG_ENTER_FUNC();
if (index != table->s->primary_key) {
/* Non covering index range scan */
DBUG_RETURN(handler::read_time(index, ranges, rows));
}
DBUG_RETURN((rows / 20.0) + 1);
}
} // namespace myrocks
/**
Construct and emit duplicate key error message using information
from table's record buffer.
@sa print_keydup_error(table, key, msg, errflag, thd, org_table_name).
*/
void print_keydup_error(TABLE *table, KEY *key, myf errflag,
const THD *thd, const char *org_table_name)
{
print_keydup_error(table, key, ER(ER_DUP_ENTRY_WITH_KEY_NAME), errflag);
}
/*
Register the storage engine plugin outside of myrocks namespace
so that mysql_declare_plugin does not get confused when it does
its name generation.
*/
struct st_mysql_storage_engine rocksdb_storage_engine = {
MYSQL_HANDLERTON_INTERFACE_VERSION};
maria_declare_plugin(rocksdb_se){
MYSQL_STORAGE_ENGINE_PLUGIN, /* Plugin Type */
&rocksdb_storage_engine, /* Plugin Descriptor */
"ROCKSDB", /* Plugin Name */
"Monty Program Ab", /* Plugin Author */
"RocksDB storage engine", /* Plugin Description */
PLUGIN_LICENSE_GPL, /* Plugin Licence */
myrocks::rocksdb_init_func, /* Plugin Entry Point */
myrocks::rocksdb_done_func, /* Plugin Deinitializer */
0x0001, /* version number (0.1) */
myrocks::rocksdb_status_vars, /* status variables */
myrocks::rocksdb_system_variables, /* system variables */
"1.0", /* string version */
MariaDB_PLUGIN_MATURITY_ALPHA /* maturity */
},
myrocks::rdb_i_s_cfstats, myrocks::rdb_i_s_dbstats,
myrocks::rdb_i_s_perf_context, myrocks::rdb_i_s_perf_context_global,
myrocks::rdb_i_s_cfoptions, myrocks::rdb_i_s_compact_stats,
myrocks::rdb_i_s_global_info, myrocks::rdb_i_s_ddl,
myrocks::rdb_i_s_index_file_map, myrocks::rdb_i_s_lock_info,
myrocks::rdb_i_s_trx_info
maria_declare_plugin_end;
|