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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <random>
#include <boost/iterator/counting_iterator.hpp>
#include "test/crimson/gtest_seastar.h"
#include "test/crimson/seastore/transaction_manager_test_state.h"
#include "crimson/os/seastore/cache.h"
#include "crimson/os/seastore/transaction_manager.h"
#include "crimson/os/seastore/segment_manager/ephemeral.h"
#include "crimson/os/seastore/segment_manager.h"
#include "test/crimson/seastore/test_block.h"
using namespace crimson;
using namespace crimson::os;
using namespace crimson::os::seastore;
namespace {
[[maybe_unused]] seastar::logger& logger() {
return crimson::get_logger(ceph_subsys_test);
}
}
struct test_extent_record_t {
test_extent_desc_t desc;
unsigned refcount = 0;
test_extent_record_t() = default;
test_extent_record_t(
const test_extent_desc_t &desc,
unsigned refcount) : desc(desc), refcount(refcount) {}
void update(const test_extent_desc_t &to) {
desc = to;
}
bool operator==(const test_extent_desc_t &rhs) const {
return desc == rhs;
}
bool operator!=(const test_extent_desc_t &rhs) const {
return desc != rhs;
}
};
template<>
struct fmt::formatter<test_extent_record_t> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(const test_extent_record_t& r, FormatContext& ctx) const {
return fmt::format_to(ctx.out(), "test_extent_record_t({}, refcount={})",
r.desc, r.refcount);
}
};
struct transaction_manager_test_t :
public seastar_test_suite_t,
TMTestState {
std::random_device rd;
std::mt19937 gen;
transaction_manager_test_t(std::size_t num_main_devices, std::size_t num_cold_devices)
: TMTestState(num_main_devices, num_cold_devices), gen(rd()) {
}
laddr_t get_random_laddr(size_t block_size, laddr_t limit) {
return block_size *
std::uniform_int_distribution<>(0, (limit / block_size) - 1)(gen);
}
char get_random_contents() {
return static_cast<char>(std::uniform_int_distribution<>(0, 255)(gen));
}
seastar::future<> set_up_fut() final {
return tm_setup();
}
seastar::future<> tear_down_fut() final {
return tm_teardown();
}
struct test_extents_t : std::map<laddr_t, test_extent_record_t> {
using delta_t = std::map<laddr_t, std::optional<test_extent_record_t>>;
std::map<laddr_t, uint64_t> laddr_write_seq;
struct delta_overlay_t {
const test_extents_t &extents;
const delta_t δ
delta_overlay_t(
const test_extents_t &extents,
const delta_t &delta)
: extents(extents), delta(delta) {}
class iterator {
friend class test_extents_t;
const delta_overlay_t &parent;
test_extents_t::const_iterator biter;
delta_t::const_iterator oiter;
std::optional<std::pair<laddr_t, test_extent_record_t>> cur;
iterator(
const delta_overlay_t &parent,
test_extents_t::const_iterator biter,
delta_t::const_iterator oiter)
: parent(parent), biter(biter), oiter(oiter) {}
laddr_t get_bkey() {
return biter == parent.extents.end() ? L_ADDR_MAX : biter->first;
}
laddr_t get_okey() {
return oiter == parent.delta.end() ? L_ADDR_MAX : oiter->first;
}
bool is_end() {
return oiter == parent.delta.end() && biter == parent.extents.end();
}
bool is_valid() {
return is_end() ||
((get_okey() < get_bkey()) && (oiter->second)) ||
(get_okey() > get_bkey());
}
auto get_pair() {
assert(is_valid());
assert(!is_end());
auto okey = get_okey();
auto bkey = get_bkey();
return (
bkey < okey ?
std::pair<laddr_t, test_extent_record_t>(*biter) :
std::make_pair(okey, *(oiter->second)));
}
void adjust() {
while (!is_valid()) {
if (get_okey() < get_bkey()) {
assert(!oiter->second);
++oiter;
} else {
assert(get_okey() == get_bkey());
++biter;
}
}
assert(is_valid());
if (!is_end()) {
cur = get_pair();
} else {
cur = std::nullopt;
}
}
public:
iterator(const iterator &) = default;
iterator(iterator &&) = default;
iterator &operator++() {
assert(is_valid());
assert(!is_end());
if (get_bkey() < get_okey()) {
++biter;
} else {
++oiter;
}
adjust();
return *this;
}
bool operator==(const iterator &o) const {
return o.biter == biter && o.oiter == oiter;
}
bool operator!=(const iterator &o) const {
return !(*this == o);
}
auto operator*() {
assert(!is_end());
return *cur;
}
auto operator->() {
assert(!is_end());
return &*cur;
}
};
iterator begin() {
auto ret = iterator{*this, extents.begin(), delta.begin()};
ret.adjust();
return ret;
}
iterator end() {
auto ret = iterator{*this, extents.end(), delta.end()};
// adjust unnecessary
return ret;
}
iterator lower_bound(laddr_t l) {
auto ret = iterator{*this, extents.lower_bound(l), delta.lower_bound(l)};
ret.adjust();
return ret;
}
iterator upper_bound(laddr_t l) {
auto ret = iterator{*this, extents.upper_bound(l), delta.upper_bound(l)};
ret.adjust();
return ret;
}
iterator find(laddr_t l) {
auto ret = lower_bound(l);
if (ret == end() || ret->first != l) {
return end();
} else {
return ret;
}
}
};
private:
void check_available(
laddr_t addr, extent_len_t len, const delta_t &delta
) const {
delta_overlay_t overlay(*this, delta);
for (const auto &i: overlay) {
if (i.first < addr) {
EXPECT_FALSE(i.first + i.second.desc.len > addr);
} else {
EXPECT_FALSE(addr + len > i.first);
}
}
}
void check_hint(
laddr_t hint,
laddr_t addr,
extent_len_t len,
delta_t &delta) const {
delta_overlay_t overlay(*this, delta);
auto iter = overlay.lower_bound(hint);
laddr_t last = hint;
while (true) {
if (iter == overlay.end() || iter->first > addr) {
EXPECT_EQ(addr, last);
break;
}
EXPECT_FALSE(iter->first - last > len);
last = iter->first + iter->second.desc.len;
++iter;
}
}
std::optional<test_extent_record_t> &populate_delta(
laddr_t addr, delta_t &delta, const test_extent_desc_t *desc) const {
auto diter = delta.find(addr);
if (diter != delta.end())
return diter->second;
auto iter = find(addr);
if (iter == end()) {
assert(desc);
auto ret = delta.emplace(
std::make_pair(addr, test_extent_record_t{*desc, 0}));
assert(ret.second);
return ret.first->second;
} else {
auto ret = delta.emplace(*iter);
assert(ret.second);
return ret.first->second;
}
}
public:
delta_overlay_t get_overlay(const delta_t &delta) const {
return delta_overlay_t{*this, delta};
}
void insert(TestBlock &extent, delta_t &delta) const {
check_available(extent.get_laddr(), extent.get_length(), delta);
delta[extent.get_laddr()] =
test_extent_record_t{extent.get_desc(), 1};
}
void alloced(laddr_t hint, TestBlock &extent, delta_t &delta) const {
check_hint(hint, extent.get_laddr(), extent.get_length(), delta);
insert(extent, delta);
}
bool contains(laddr_t addr, const delta_t &delta) const {
delta_overlay_t overlay(*this, delta);
return overlay.find(addr) != overlay.end();
}
test_extent_record_t get(laddr_t addr, const delta_t &delta) const {
delta_overlay_t overlay(*this, delta);
auto iter = overlay.find(addr);
assert(iter != overlay.end());
return iter->second;
}
void update(
laddr_t addr,
const test_extent_desc_t &desc,
delta_t &delta) const {
auto &rec = populate_delta(addr, delta, &desc);
assert(rec);
rec->desc = desc;
}
int inc_ref(
laddr_t addr,
delta_t &delta) const {
auto &rec = populate_delta(addr, delta, nullptr);
assert(rec);
return ++rec->refcount;
}
int dec_ref(
laddr_t addr,
delta_t &delta) const {
auto &rec = populate_delta(addr, delta, nullptr);
assert(rec);
assert(rec->refcount > 0);
rec->refcount--;
if (rec->refcount == 0) {
delta[addr] = std::nullopt;
return 0;
} else {
return rec->refcount;
}
}
void consume(const delta_t &delta, const uint64_t write_seq = 0) {
for (const auto &i : delta) {
if (i.second) {
if (laddr_write_seq.find(i.first) == laddr_write_seq.end() ||
laddr_write_seq[i.first] <= write_seq) {
(*this)[i.first] = *i.second;
laddr_write_seq[i.first] = write_seq;
}
} else {
erase(i.first);
}
}
}
} test_mappings;
struct test_transaction_t {
TransactionRef t;
test_extents_t::delta_t mapping_delta;
};
test_transaction_t create_transaction() {
return { create_mutate_transaction(), {} };
}
test_transaction_t create_read_test_transaction() {
return {create_read_transaction(), {} };
}
test_transaction_t create_weak_test_transaction() {
return { create_weak_transaction(), {} };
}
TestBlockRef alloc_extent(
test_transaction_t &t,
laddr_t hint,
extent_len_t len,
char contents) {
auto extent = with_trans_intr(*(t.t), [&](auto& trans) {
return tm->alloc_extent<TestBlock>(trans, hint, len);
}).unsafe_get0();
extent->set_contents(contents);
EXPECT_FALSE(test_mappings.contains(extent->get_laddr(), t.mapping_delta));
EXPECT_EQ(len, extent->get_length());
test_mappings.alloced(hint, *extent, t.mapping_delta);
return extent;
}
TestBlockRef alloc_extent(
test_transaction_t &t,
laddr_t hint,
extent_len_t len) {
return alloc_extent(
t,
hint,
len,
get_random_contents());
}
bool check_usage() {
return epm->check_usage();
}
void replay() {
EXPECT_TRUE(check_usage());
restart();
}
void check() {
check_mappings();
check_usage();
}
void check_mappings() {
auto t = create_weak_test_transaction();
check_mappings(t);
}
TestBlockRef get_extent(
test_transaction_t &t,
laddr_t addr,
extent_len_t len) {
ceph_assert(test_mappings.contains(addr, t.mapping_delta));
ceph_assert(test_mappings.get(addr, t.mapping_delta).desc.len == len);
auto ext = with_trans_intr(*(t.t), [&](auto& trans) {
return tm->read_extent<TestBlock>(trans, addr, len);
}).unsafe_get0();
EXPECT_EQ(addr, ext->get_laddr());
return ext;
}
TestBlockRef try_get_extent(
test_transaction_t &t,
laddr_t addr) {
ceph_assert(test_mappings.contains(addr, t.mapping_delta));
using ertr = with_trans_ertr<TransactionManager::read_extent_iertr>;
using ret = ertr::future<TestBlockRef>;
auto ext = with_trans_intr(*(t.t), [&](auto& trans) {
return tm->read_extent<TestBlock>(trans, addr);
}).safe_then([](auto ext) -> ret {
return ertr::make_ready_future<TestBlockRef>(ext);
}).handle_error(
[](const crimson::ct_error::eagain &e) {
return seastar::make_ready_future<TestBlockRef>();
},
crimson::ct_error::assert_all{
"get_extent got invalid error"
}
).get0();
if (ext) {
EXPECT_EQ(addr, ext->get_laddr());
}
return ext;
}
TestBlockRef try_get_extent(
test_transaction_t &t,
laddr_t addr,
extent_len_t len) {
ceph_assert(test_mappings.contains(addr, t.mapping_delta));
ceph_assert(test_mappings.get(addr, t.mapping_delta).desc.len == len);
using ertr = with_trans_ertr<TransactionManager::read_extent_iertr>;
using ret = ertr::future<TestBlockRef>;
auto ext = with_trans_intr(*(t.t), [&](auto& trans) {
return tm->read_extent<TestBlock>(trans, addr, len);
}).safe_then([](auto ext) -> ret {
return ertr::make_ready_future<TestBlockRef>(ext);
}).handle_error(
[](const crimson::ct_error::eagain &e) {
return seastar::make_ready_future<TestBlockRef>();
},
crimson::ct_error::assert_all{
"get_extent got invalid error"
}
).get0();
if (ext) {
EXPECT_EQ(addr, ext->get_laddr());
}
return ext;
}
TestBlockRef try_read_pin(
test_transaction_t &t,
LBAMappingRef &&pin) {
using ertr = with_trans_ertr<TransactionManager::base_iertr>;
using ret = ertr::future<TestBlockRef>;
auto addr = pin->get_key();
auto ext = with_trans_intr(*(t.t), [&](auto& trans) {
return tm->read_pin<TestBlock>(trans, std::move(pin));
}).safe_then([](auto ext) -> ret {
return ertr::make_ready_future<TestBlockRef>(ext);
}).handle_error(
[](const crimson::ct_error::eagain &e) {
return seastar::make_ready_future<TestBlockRef>();
},
crimson::ct_error::assert_all{
"read_pin got invalid error"
}
).get0();
if (ext) {
EXPECT_EQ(addr, ext->get_laddr());
}
if (t.t->is_conflicted()) {
return nullptr;
}
return ext;
}
test_block_mutator_t mutator;
TestBlockRef mutate_extent(
test_transaction_t &t,
TestBlockRef ref) {
ceph_assert(test_mappings.contains(ref->get_laddr(), t.mapping_delta));
ceph_assert(
test_mappings.get(ref->get_laddr(), t.mapping_delta).desc.len ==
ref->get_length());
auto ext = tm->get_mutable_extent(*t.t, ref)->cast<TestBlock>();
EXPECT_EQ(ext->get_laddr(), ref->get_laddr());
EXPECT_EQ(ext->get_desc(), ref->get_desc());
mutator.mutate(*ext, gen);
test_mappings.update(ext->get_laddr(), ext->get_desc(), t.mapping_delta);
return ext;
}
TestBlockRef mutate_addr(
test_transaction_t &t,
laddr_t offset,
size_t length) {
auto ext = get_extent(t, offset, length);
mutate_extent(t, ext);
return ext;
}
LBAMappingRef get_pin(
test_transaction_t &t,
laddr_t offset) {
ceph_assert(test_mappings.contains(offset, t.mapping_delta));
auto pin = with_trans_intr(*(t.t), [&](auto& trans) {
return tm->get_pin(trans, offset);
}).unsafe_get0();
EXPECT_EQ(offset, pin->get_key());
return pin;
}
LBAMappingRef try_get_pin(
test_transaction_t &t,
laddr_t offset) {
ceph_assert(test_mappings.contains(offset, t.mapping_delta));
using ertr = with_trans_ertr<TransactionManager::get_pin_iertr>;
using ret = ertr::future<LBAMappingRef>;
auto pin = with_trans_intr(*(t.t), [&](auto& trans) {
return tm->get_pin(trans, offset);
}).safe_then([](auto pin) -> ret {
return ertr::make_ready_future<LBAMappingRef>(std::move(pin));
}).handle_error(
[](const crimson::ct_error::eagain &e) {
return seastar::make_ready_future<LBAMappingRef>();
},
crimson::ct_error::assert_all{
"get_extent got invalid error"
}
).get0();
if (pin) {
EXPECT_EQ(offset, pin->get_key());
}
return pin;
}
void inc_ref(test_transaction_t &t, laddr_t offset) {
ceph_assert(test_mappings.contains(offset, t.mapping_delta));
ceph_assert(test_mappings.get(offset, t.mapping_delta).refcount > 0);
auto refcnt = with_trans_intr(*(t.t), [&](auto& trans) {
return tm->inc_ref(trans, offset);
}).unsafe_get0();
auto check_refcnt = test_mappings.inc_ref(offset, t.mapping_delta);
EXPECT_EQ(refcnt, check_refcnt);
}
void dec_ref(test_transaction_t &t, laddr_t offset) {
ceph_assert(test_mappings.contains(offset, t.mapping_delta));
ceph_assert(test_mappings.get(offset, t.mapping_delta).refcount > 0);
auto refcnt = with_trans_intr(*(t.t), [&](auto& trans) {
return tm->dec_ref(trans, offset);
}).unsafe_get0();
auto check_refcnt = test_mappings.dec_ref(offset, t.mapping_delta);
EXPECT_EQ(refcnt, check_refcnt);
if (refcnt == 0)
logger().debug("dec_ref: {} at refcount 0", offset);
}
void check_mappings(test_transaction_t &t) {
auto overlay = test_mappings.get_overlay(t.mapping_delta);
for (const auto &i: overlay) {
logger().debug("check_mappings: {}->{}", i.first, i.second);
auto ext = get_extent(t, i.first, i.second.desc.len);
EXPECT_EQ(i.second, ext->get_desc());
}
with_trans_intr(
*t.t,
[this, &overlay](auto &t) {
return lba_manager->scan_mappings(
t,
0,
L_ADDR_MAX,
[iter=overlay.begin(), &overlay](auto l, auto p, auto len) mutable {
EXPECT_NE(iter, overlay.end());
logger().debug(
"check_mappings: scan {}",
l);
EXPECT_EQ(l, iter->first);
++iter;
});
}).unsafe_get0();
(void)with_trans_intr(
*t.t,
[=, this](auto &t) {
return lba_manager->check_child_trackers(t);
}).unsafe_get0();
}
bool try_submit_transaction(test_transaction_t t) {
using ertr = with_trans_ertr<TransactionManager::submit_transaction_iertr>;
using ret = ertr::future<bool>;
uint64_t write_seq = 0;
bool success = submit_transaction_fut_with_seq(*t.t
).safe_then([&write_seq](auto seq) -> ret {
write_seq = seq;
return ertr::make_ready_future<bool>(true);
}).handle_error(
[](const crimson::ct_error::eagain &e) {
return seastar::make_ready_future<bool>(false);
},
crimson::ct_error::assert_all{
"try_submit_transaction hit invalid error"
}
).then([this](auto ret) {
return epm->run_background_work_until_halt(
).then([ret] { return ret; });
}).get0();
if (success) {
test_mappings.consume(t.mapping_delta, write_seq);
}
return success;
}
void submit_transaction(test_transaction_t &&t) {
bool success = try_submit_transaction(std::move(t));
EXPECT_TRUE(success);
}
void submit_transaction_expect_conflict(test_transaction_t &&t) {
bool success = try_submit_transaction(std::move(t));
EXPECT_FALSE(success);
}
auto allocate_sequentially(const size_t size, const int num, bool run_clean = true) {
return repeat_eagain([this, size, num] {
return seastar::do_with(
create_transaction(),
[this, size, num](auto &t) {
return with_trans_intr(
*t.t,
[&t, this, size, num](auto &) {
return trans_intr::do_for_each(
boost::make_counting_iterator(0),
boost::make_counting_iterator(num),
[&t, this, size](auto) {
return tm->alloc_extent<TestBlock>(
*(t.t), L_ADDR_MIN, size
).si_then([&t, this, size](auto extent) {
extent->set_contents(get_random_contents());
EXPECT_FALSE(
test_mappings.contains(extent->get_laddr(), t.mapping_delta));
EXPECT_EQ(size, extent->get_length());
test_mappings.alloced(extent->get_laddr(), *extent, t.mapping_delta);
return seastar::now();
});
}).si_then([&t, this] {
return tm->submit_transaction(*t.t);
});
}).safe_then([&t, this] {
test_mappings.consume(t.mapping_delta);
});
});
}).safe_then([this, run_clean]() {
if (run_clean) {
return epm->run_background_work_until_halt();
} else {
return epm->background_process.trimmer->trim();
}
}).handle_error(
crimson::ct_error::assert_all{
"Invalid error in SeaStore::list_collections"
}
);
}
void test_parallel_extent_read() {
constexpr size_t TOTAL = 4<<20;
constexpr size_t BSIZE = 4<<10;
constexpr size_t BLOCKS = TOTAL / BSIZE;
run_async([this] {
for (unsigned i = 0; i < BLOCKS; ++i) {
auto t = create_transaction();
auto extent = alloc_extent(
t,
i * BSIZE,
BSIZE);
ASSERT_EQ(i * BSIZE, extent->get_laddr());
submit_transaction(std::move(t));
}
seastar::do_with(
create_read_test_transaction(),
[this](auto &t) {
return with_trans_intr(*(t.t), [this](auto &t) {
return trans_intr::parallel_for_each(
boost::make_counting_iterator(0lu),
boost::make_counting_iterator(BLOCKS),
[this, &t](auto i) {
return tm->read_extent<TestBlock>(t, i * BSIZE, BSIZE
).si_then([](auto) {
return seastar::now();
});
});
});
}).unsafe_get0();
});
}
void test_random_writes_concurrent() {
constexpr unsigned WRITE_STREAMS = 256;
constexpr size_t TOTAL = 4<<20;
constexpr size_t BSIZE = 4<<10;
constexpr size_t BLOCKS = TOTAL / BSIZE;
run_async([this] {
std::for_each(
boost::make_counting_iterator(0u),
boost::make_counting_iterator(WRITE_STREAMS),
[&](auto idx) {
for (unsigned i = idx; i < BLOCKS; i += WRITE_STREAMS) {
while (true) {
auto t = create_transaction();
auto extent = alloc_extent(
t,
i * BSIZE,
BSIZE);
ASSERT_EQ(i * BSIZE, extent->get_laddr());
if (try_submit_transaction(std::move(t)))
break;
}
}
});
int writes = 0;
unsigned failures = 0;
seastar::parallel_for_each(
boost::make_counting_iterator(0u),
boost::make_counting_iterator(WRITE_STREAMS),
[&](auto) {
return seastar::async([&] {
while (writes < 300) {
auto t = create_transaction();
auto ext = try_get_extent(
t,
get_random_laddr(BSIZE, TOTAL),
BSIZE);
if (!ext){
failures++;
continue;
}
auto mut = mutate_extent(t, ext);
auto success = try_submit_transaction(std::move(t));
writes += success;
failures += !success;
}
});
}).get0();
replay();
logger().info("random_writes_concurrent: checking");
check();
logger().info(
"random_writes_concurrent: {} suceeded, {} failed",
writes,
failures
);
});
}
void test_evict() {
// only support segmented backend currently
ASSERT_EQ(epm->get_main_backend_type(), backend_type_t::SEGMENTED);
ASSERT_TRUE(epm->background_process.has_cold_tier());
constexpr size_t device_size =
segment_manager::DEFAULT_TEST_EPHEMERAL.size;
constexpr size_t block_size =
segment_manager::DEFAULT_TEST_EPHEMERAL.block_size;
constexpr size_t segment_size =
segment_manager::DEFAULT_TEST_EPHEMERAL.segment_size;
ASSERT_GE(segment_size, block_size * 20);
run_async([this] {
// indicates there is no available segments to reclaim
double stop_ratio = (double)segment_size / (double)device_size / 2;
// 1 segment
double default_ratio = stop_ratio * 2;
// 1.25 segment
double fast_ratio = stop_ratio * 2.5;
epm->background_process
.eviction_state
.init(stop_ratio, default_ratio, fast_ratio);
// these variables are described in
// EPM::BackgroundProcess::eviction_state_t::maybe_update_eviction_mode
size_t ratio_A_size = segment_size / 2 - block_size * 10;
size_t ratio_B_size = segment_size / 2 + block_size * 10;
size_t ratio_C_size = segment_size + block_size;
size_t ratio_D_size = segment_size * 1.25 + block_size;
auto run_until = [this](size_t size) -> seastar::future<> {
return seastar::repeat([this, size] {
size_t current_size = epm->background_process
.main_cleaner->get_stat().data_stored;
if (current_size >= size) {
return seastar::futurize_invoke([] {
return seastar::stop_iteration::yes;
});
} else {
int num = (size - current_size) / block_size;
return seastar::do_for_each(
boost::make_counting_iterator(0),
boost::make_counting_iterator(num),
[this](auto) {
// don't start background process to test the behavior
// of generation changes during alloc new extents
return allocate_sequentially(block_size, 1, false);
}).then([] {
return seastar::stop_iteration::no;
});
}
});
};
std::vector<extent_types_t> all_extent_types{
extent_types_t::ROOT,
extent_types_t::LADDR_INTERNAL,
extent_types_t::LADDR_LEAF,
extent_types_t::OMAP_INNER,
extent_types_t::OMAP_LEAF,
extent_types_t::ONODE_BLOCK_STAGED,
extent_types_t::COLL_BLOCK,
extent_types_t::OBJECT_DATA_BLOCK,
extent_types_t::RETIRED_PLACEHOLDER,
extent_types_t::ALLOC_INFO,
extent_types_t::JOURNAL_TAIL,
extent_types_t::TEST_BLOCK,
extent_types_t::TEST_BLOCK_PHYSICAL,
extent_types_t::BACKREF_INTERNAL,
extent_types_t::BACKREF_LEAF
};
std::vector<rewrite_gen_t> all_generations;
for (auto i = INIT_GENERATION; i < REWRITE_GENERATIONS; i++) {
all_generations.push_back(i);
}
// input target-generation -> expected generation after the adjustment
using generation_mapping_t = std::map<rewrite_gen_t, rewrite_gen_t>;
std::map<extent_types_t, generation_mapping_t> expected_generations;
// this loop should be consistent with EPM::adjust_generation
for (auto t : all_extent_types) {
expected_generations[t] = {};
if (!is_logical_type(t)) {
for (auto gen : all_generations) {
expected_generations[t][gen] = INLINE_GENERATION;
}
} else {
if (get_extent_category(t) == data_category_t::METADATA) {
expected_generations[t][INIT_GENERATION] = INLINE_GENERATION;
} else {
expected_generations[t][INIT_GENERATION] = OOL_GENERATION;
}
for (auto i = INIT_GENERATION + 1; i < REWRITE_GENERATIONS; i++) {
expected_generations[t][i] = i;
}
}
}
auto update_data_gen_mapping = [&](std::function<rewrite_gen_t(rewrite_gen_t)> func) {
for (auto t : all_extent_types) {
if (!is_logical_type(t)) {
continue;
}
for (auto i = INIT_GENERATION + 1; i < REWRITE_GENERATIONS; i++) {
expected_generations[t][i] = func(i);
}
}
// since background process didn't start in allocate_sequentially
// we update eviction mode manually.
epm->background_process.maybe_update_eviction_mode();
};
auto test_gen = [&](const char *caller) {
for (auto t : all_extent_types) {
for (auto gen : all_generations) {
auto epm_gen = epm->adjust_generation(
get_extent_category(t),
t,
placement_hint_t::HOT,
gen);
if (expected_generations[t][gen] != epm_gen) {
logger().error("caller: {}, extent type: {}, input generation: {}, "
"expected generation : {}, adjust result from EPM: {}",
caller, t, gen, expected_generations[t][gen], epm_gen);
}
EXPECT_EQ(expected_generations[t][gen], epm_gen);
}
}
};
// verify that no data should go to the cold tier
update_data_gen_mapping([](rewrite_gen_t gen) -> rewrite_gen_t {
if (gen == MIN_COLD_GENERATION) {
return MIN_COLD_GENERATION - 1;
} else {
return gen;
}
});
test_gen("init");
run_until(ratio_A_size).get();
EXPECT_TRUE(epm->background_process.eviction_state.is_stop_mode());
test_gen("exceed ratio A");
epm->run_background_work_until_halt().get();
run_until(ratio_B_size).get();
EXPECT_TRUE(epm->background_process.eviction_state.is_stop_mode());
test_gen("exceed ratio B");
epm->run_background_work_until_halt().get();
// verify that data may go to the cold tier
run_until(ratio_C_size).get();
update_data_gen_mapping([](rewrite_gen_t gen) { return gen; });
EXPECT_TRUE(epm->background_process.eviction_state.is_default_mode());
test_gen("exceed ratio C");
epm->run_background_work_until_halt().get();
// verify that data must go to the cold tier
run_until(ratio_D_size).get();
update_data_gen_mapping([](rewrite_gen_t gen) {
if (gen >= MIN_REWRITE_GENERATION && gen < MIN_COLD_GENERATION) {
return MIN_COLD_GENERATION;
} else {
return gen;
}
});
EXPECT_TRUE(epm->background_process.eviction_state.is_fast_mode());
test_gen("exceed ratio D");
auto main_size = epm->background_process.main_cleaner->get_stat().data_stored;
auto cold_size = epm->background_process.cold_cleaner->get_stat().data_stored;
EXPECT_EQ(cold_size, 0);
epm->run_background_work_until_halt().get();
auto new_main_size = epm->background_process.main_cleaner->get_stat().data_stored;
auto new_cold_size = epm->background_process.cold_cleaner->get_stat().data_stored;
EXPECT_GE(main_size, new_main_size);
EXPECT_NE(new_cold_size, 0);
update_data_gen_mapping([](rewrite_gen_t gen) { return gen; });
EXPECT_TRUE(epm->background_process.eviction_state.is_default_mode());
test_gen("finish evict");
});
}
using remap_entry = TransactionManager::remap_entry;
LBAMappingRef remap_pin(
test_transaction_t &t,
LBAMappingRef &&opin,
extent_len_t new_offset,
extent_len_t new_len) {
if (t.t->is_conflicted()) {
return nullptr;
}
auto o_laddr = opin->get_key();
auto pin = with_trans_intr(*(t.t), [&](auto& trans) {
return tm->remap_pin<TestBlock>(
trans, std::move(opin), std::array{
remap_entry(new_offset, new_len)}
).si_then([](auto ret) {
return std::move(ret[0]);
});
}).handle_error(crimson::ct_error::eagain::handle([] {
LBAMappingRef t = nullptr;
return t;
}), crimson::ct_error::pass_further_all{}).unsafe_get0();
if (t.t->is_conflicted()) {
return nullptr;
}
test_mappings.dec_ref(o_laddr, t.mapping_delta);
EXPECT_FALSE(test_mappings.contains(o_laddr, t.mapping_delta));
EXPECT_TRUE(pin);
EXPECT_EQ(pin->get_length(), new_len);
EXPECT_EQ(pin->get_key(), o_laddr + new_offset);
auto extent = try_read_pin(t, pin->duplicate());
if (extent) {
test_mappings.alloced(pin->get_key(), *extent, t.mapping_delta);
EXPECT_TRUE(extent->is_exist_clean());
} else {
ceph_assert(t.t->is_conflicted());
return nullptr;
}
return pin;
}
using _overwrite_pin_iertr = TransactionManager::get_pin_iertr;
using _overwrite_pin_ret = _overwrite_pin_iertr::future<
std::tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>>;
_overwrite_pin_ret _overwrite_pin(
Transaction &t,
LBAMappingRef &&opin,
extent_len_t new_offset,
extent_len_t new_len,
ceph::bufferlist &bl) {
auto o_laddr = opin->get_key();
auto o_len = opin->get_length();
if (new_offset != 0 && o_len != new_offset + new_len) {
return tm->remap_pin<TestBlock, 2>(
t,
std::move(opin),
std::array{
remap_entry(
0,
new_offset),
remap_entry(
new_offset + new_len,
o_len - new_offset - new_len)
}
).si_then([this, new_offset, new_len, o_laddr, &t, &bl](auto ret) {
return tm->alloc_extent<TestBlock>(t, o_laddr + new_offset, new_len
).si_then([this, ret = std::move(ret), new_len,
new_offset, o_laddr, &t, &bl](auto ext) mutable {
ceph_assert(ret.size() == 2);
auto iter = bl.cbegin();
iter.copy(new_len, ext->get_bptr().c_str());
auto r_laddr = o_laddr + new_offset + new_len;
// old pins expired after alloc new extent, need to get it.
return tm->get_pin(t, o_laddr
).si_then([this, &t, ext = std::move(ext), r_laddr](auto lpin) mutable {
return tm->get_pin(t, r_laddr
).si_then([lpin = std::move(lpin), ext = std::move(ext)]
(auto rpin) mutable {
return _overwrite_pin_iertr::make_ready_future<
std::tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>>(
std::make_tuple(
std::move(lpin), std::move(ext), std::move(rpin)));
});
});
});
});
} else if (new_offset == 0 && o_len != new_offset + new_len) {
return tm->remap_pin<TestBlock, 1>(
t,
std::move(opin),
std::array{
remap_entry(
new_offset + new_len,
o_len - new_offset - new_len)
}
).si_then([this, new_offset, new_len, o_laddr, &t, &bl](auto ret) {
return tm->alloc_extent<TestBlock>(t, o_laddr + new_offset, new_len
).si_then([this, ret = std::move(ret), new_offset, new_len,
o_laddr, &t, &bl](auto ext) mutable {
ceph_assert(ret.size() == 1);
auto iter = bl.cbegin();
iter.copy(new_len, ext->get_bptr().c_str());
auto r_laddr = o_laddr + new_offset + new_len;
return tm->get_pin(t, r_laddr
).si_then([ext = std::move(ext)](auto rpin) mutable {
return _overwrite_pin_iertr::make_ready_future<
std::tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>>(
std::make_tuple(
nullptr, std::move(ext), std::move(rpin)));
});
});
});
} else if (new_offset != 0 && o_len == new_offset + new_len) {
return tm->remap_pin<TestBlock, 1>(
t,
std::move(opin),
std::array{
remap_entry(
0,
new_offset)
}
).si_then([this, new_offset, new_len, o_laddr, &t, &bl](auto ret) {
return tm->alloc_extent<TestBlock>(t, o_laddr + new_offset, new_len
).si_then([this, ret = std::move(ret), new_len, o_laddr, &t, &bl]
(auto ext) mutable {
ceph_assert(ret.size() == 1);
auto iter = bl.cbegin();
iter.copy(new_len, ext->get_bptr().c_str());
return tm->get_pin(t, o_laddr
).si_then([ext = std::move(ext)](auto lpin) mutable {
return _overwrite_pin_iertr::make_ready_future<
std::tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>>(
std::make_tuple(
std::move(lpin), std::move(ext), nullptr));
});
});
});
} else {
ceph_abort("impossible");
return _overwrite_pin_iertr::make_ready_future<
std::tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>>(
std::make_tuple(nullptr, nullptr, nullptr));
}
}
using overwrite_pin_ret = std::tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>;
overwrite_pin_ret overwrite_pin(
test_transaction_t &t,
LBAMappingRef &&opin,
extent_len_t new_offset,
extent_len_t new_len,
ceph::bufferlist &bl) {
if (t.t->is_conflicted()) {
return std::make_tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>(
nullptr, nullptr, nullptr);
}
auto o_laddr = opin->get_key();
auto o_paddr = opin->get_val();
auto o_len = opin->get_length();
auto res = with_trans_intr(*(t.t), [&](auto& trans) {
return _overwrite_pin(
trans, std::move(opin), new_offset, new_len, bl);
}).handle_error(crimson::ct_error::eagain::handle([] {
return std::make_tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>(
nullptr, nullptr, nullptr);
}), crimson::ct_error::pass_further_all{}).unsafe_get0();
if (t.t->is_conflicted()) {
return std::make_tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>(
nullptr, nullptr, nullptr);
}
test_mappings.dec_ref(o_laddr, t.mapping_delta);
EXPECT_FALSE(test_mappings.contains(o_laddr, t.mapping_delta));
auto &[lpin, ext, rpin] = res;
EXPECT_TRUE(ext);
EXPECT_TRUE(lpin || rpin);
EXPECT_TRUE(o_len > ext->get_length());
if (lpin) {
EXPECT_EQ(lpin->get_key(), o_laddr);
EXPECT_EQ(lpin->get_val(), o_paddr);
EXPECT_EQ(lpin->get_length(), new_offset);
auto lext = try_read_pin(t, lpin->duplicate());
if (lext) {
test_mappings.alloced(lpin->get_key(), *lext, t.mapping_delta);
EXPECT_TRUE(lext->is_exist_clean());
} else {
ceph_assert(t.t->is_conflicted());
return std::make_tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>(
nullptr, nullptr, nullptr);
}
}
EXPECT_EQ(ext->get_laddr(), o_laddr + new_offset);
EXPECT_EQ(ext->get_length(), new_len);
test_mappings.alloced(ext->get_laddr(), *ext, t.mapping_delta);
if (rpin) {
EXPECT_EQ(rpin->get_key(), o_laddr + new_offset + new_len);
EXPECT_EQ(rpin->get_val(), o_paddr.add_offset(new_offset)
.add_offset(new_len));
EXPECT_EQ(rpin->get_length(), o_len - new_offset - new_len);
auto rext = try_read_pin(t, rpin->duplicate());
if (rext) {
test_mappings.alloced(rpin->get_key(), *rext, t.mapping_delta);
EXPECT_TRUE(rext->is_exist_clean());
} else {
ceph_assert(t.t->is_conflicted());
return std::make_tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>(
nullptr, nullptr, nullptr);
}
}
return std::make_tuple<LBAMappingRef, TestBlockRef, LBAMappingRef>(
std::move(lpin), std::move(ext), std::move(rpin));
}
void test_remap_pin() {
run_async([this] {
constexpr size_t l_offset = 32 << 10;
constexpr size_t l_len = 32 << 10;
constexpr size_t r_offset = 64 << 10;
constexpr size_t r_len = 32 << 10;
{
auto t = create_transaction();
auto lext = alloc_extent(t, l_offset, l_len);
lext->set_contents('l', 0, 16 << 10);
auto rext = alloc_extent(t, r_offset, r_len);
rext->set_contents('r', 16 << 10, 16 << 10);
submit_transaction(std::move(t));
}
{
auto t = create_transaction();
auto lpin = get_pin(t, l_offset);
auto rpin = get_pin(t, r_offset);
//split left
auto pin1 = remap_pin(t, std::move(lpin), 0, 16 << 10);
ASSERT_TRUE(pin1);
auto pin2 = remap_pin(t, std::move(pin1), 0, 8 << 10);
ASSERT_TRUE(pin2);
auto pin3 = remap_pin(t, std::move(pin2), 0, 4 << 10);
ASSERT_TRUE(pin3);
auto lext = get_extent(t, pin3->get_key(), pin3->get_length());
EXPECT_EQ('l', lext->get_bptr().c_str()[0]);
auto mlext = mutate_extent(t, lext);
ASSERT_TRUE(mlext->is_exist_mutation_pending());
ASSERT_TRUE(mlext.get() == lext.get());
//split right
auto pin4 = remap_pin(t, std::move(rpin), 16 << 10, 16 << 10);
ASSERT_TRUE(pin4);
auto pin5 = remap_pin(t, std::move(pin4), 8 << 10, 8 << 10);
ASSERT_TRUE(pin5);
auto pin6 = remap_pin(t, std::move(pin5), 4 << 10, 4 << 10);
ASSERT_TRUE(pin6);
auto rext = get_extent(t, pin6->get_key(), pin6->get_length());
EXPECT_EQ('r', rext->get_bptr().c_str()[0]);
auto mrext = mutate_extent(t, rext);
ASSERT_TRUE(mrext->is_exist_mutation_pending());
ASSERT_TRUE(mrext.get() == rext.get());
submit_transaction(std::move(t));
check();
}
replay();
check();
});
}
void test_overwrite_pin() {
run_async([this] {
constexpr size_t m_offset = 8 << 10;
constexpr size_t m_len = 56 << 10;
constexpr size_t l_offset = 64 << 10;
constexpr size_t l_len = 64 << 10;
constexpr size_t r_offset = 128 << 10;
constexpr size_t r_len = 64 << 10;
{
auto t = create_transaction();
auto m_ext = alloc_extent(t, m_offset, m_len);
m_ext->set_contents('a', 0 << 10, 8 << 10);
m_ext->set_contents('b', 16 << 10, 4 << 10);
m_ext->set_contents('c', 36 << 10, 4 << 10);
m_ext->set_contents('d', 52 << 10, 4 << 10);
auto l_ext = alloc_extent(t, l_offset, l_len);
auto r_ext = alloc_extent(t, r_offset, r_len);
submit_transaction(std::move(t));
}
{
auto t = create_transaction();
auto mpin = get_pin(t, m_offset);
auto lpin = get_pin(t, l_offset);
auto rpin = get_pin(t, r_offset);
bufferlist mbl1, mbl2, mbl3;
mbl1.append(ceph::bufferptr(ceph::buffer::create(8 << 10, 0)));
mbl2.append(ceph::bufferptr(ceph::buffer::create(16 << 10, 0)));
mbl3.append(ceph::bufferptr(ceph::buffer::create(12 << 10, 0)));
auto [mlp1, mext1, mrp1] = overwrite_pin(
t, std::move(mpin), 8 << 10 , 8 << 10, mbl1);
auto [mlp2, mext2, mrp2] = overwrite_pin(
t, std::move(mrp1), 4 << 10 , 16 << 10, mbl2);
auto [mlpin3, me3, mrpin3] = overwrite_pin(
t, std::move(mrp2), 4 << 10 , 12 << 10, mbl3);
auto mlext1 = get_extent(t, mlp1->get_key(), mlp1->get_length());
auto mlext2 = get_extent(t, mlp2->get_key(), mlp2->get_length());
auto mlext3 = get_extent(t, mlpin3->get_key(), mlpin3->get_length());
auto mrext3 = get_extent(t, mrpin3->get_key(), mrpin3->get_length());
EXPECT_EQ('a', mlext1->get_bptr().c_str()[0]);
EXPECT_EQ('b', mlext2->get_bptr().c_str()[0]);
EXPECT_EQ('c', mlext3->get_bptr().c_str()[0]);
EXPECT_EQ('d', mrext3->get_bptr().c_str()[0]);
auto mutate_mlext1 = mutate_extent(t, mlext1);
auto mutate_mlext2 = mutate_extent(t, mlext2);
auto mutate_mlext3 = mutate_extent(t, mlext3);
auto mutate_mrext3 = mutate_extent(t, mrext3);
ASSERT_TRUE(mutate_mlext1->is_exist_mutation_pending());
ASSERT_TRUE(mutate_mlext2->is_exist_mutation_pending());
ASSERT_TRUE(mutate_mlext3->is_exist_mutation_pending());
ASSERT_TRUE(mutate_mrext3->is_exist_mutation_pending());
ASSERT_TRUE(mutate_mlext1.get() == mlext1.get());
ASSERT_TRUE(mutate_mlext2.get() == mlext2.get());
ASSERT_TRUE(mutate_mlext3.get() == mlext3.get());
ASSERT_TRUE(mutate_mrext3.get() == mrext3.get());
bufferlist lbl1, rbl1;
lbl1.append(ceph::bufferptr(ceph::buffer::create(32 << 10, 0)));
auto [llp1, lext1, lrp1] = overwrite_pin(
t, std::move(lpin), 0 , 32 << 10, lbl1);
EXPECT_FALSE(llp1);
EXPECT_TRUE(lrp1);
EXPECT_TRUE(lext1);
rbl1.append(ceph::bufferptr(ceph::buffer::create(32 << 10, 0)));
auto [rlp1, rext1, rrp1] = overwrite_pin(
t, std::move(rpin), 32 << 10 , 32 << 10, rbl1);
EXPECT_TRUE(rlp1);
EXPECT_TRUE(rext1);
EXPECT_FALSE(rrp1);
submit_transaction(std::move(t));
check();
}
replay();
check();
});
}
void test_remap_pin_concurrent() {
run_async([this] {
constexpr unsigned REMAP_NUM = 32;
constexpr size_t offset = 0;
constexpr size_t length = 256 << 10;
{
auto t = create_transaction();
auto extent = alloc_extent(t, offset, length);
ASSERT_EQ(length, extent->get_length());
submit_transaction(std::move(t));
}
int success = 0;
int early_exit = 0;
int conflicted = 0;
seastar::parallel_for_each(
boost::make_counting_iterator(0u),
boost::make_counting_iterator(REMAP_NUM),
[&](auto) {
return seastar::async([&] {
uint32_t pieces = std::uniform_int_distribution<>(6, 31)(gen);
std::set<uint32_t> split_points;
for (uint32_t i = 0; i < pieces; i++) {
auto p = std::uniform_int_distribution<>(1, 256)(gen);
split_points.insert(p - p % 4);
}
auto t = create_transaction();
auto pin0 = try_get_pin(t, offset);
if (!pin0 || pin0->get_length() != length) {
early_exit++;
return;
}
auto last_pin = pin0->duplicate();
ASSERT_TRUE(!split_points.empty());
for (auto off : split_points) {
if (off == 0 || off >= 255) {
continue;
}
auto new_off = (off << 10) - last_pin->get_key();
auto new_len = last_pin->get_length() - new_off;
//always remap right extent at new split_point
auto pin = remap_pin(t, std::move(last_pin), new_off, new_len);
if (!pin) {
conflicted++;
return;
}
last_pin = pin->duplicate();
}
auto last_ext = try_get_extent(t, last_pin->get_key());
if (last_ext) {
auto last_ext1 = mutate_extent(t, last_ext);
ASSERT_TRUE(last_ext1->is_exist_mutation_pending());
} else {
conflicted++;
return;
}
if (try_submit_transaction(std::move(t))) {
success++;
logger().info("transaction {} submit the transction",
static_cast<void*>(t.t.get()));
} else {
conflicted++;
}
});
}).handle_exception([](std::exception_ptr e) {
logger().info("{}", e);
}).get0();
logger().info("test_remap_pin_concurrent: "
"early_exit {} conflicted {} success {}",
early_exit, conflicted, success);
ASSERT_TRUE(success == 1);
ASSERT_EQ(success + conflicted + early_exit, REMAP_NUM);
replay();
check();
});
}
void test_overwrite_pin_concurrent() {
run_async([this] {
constexpr unsigned REMAP_NUM = 32;
constexpr size_t offset = 0;
constexpr size_t length = 256 << 10;
{
auto t = create_transaction();
auto extent = alloc_extent(t, offset, length);
ASSERT_EQ(length, extent->get_length());
submit_transaction(std::move(t));
}
int success = 0;
int early_exit = 0;
int conflicted = 0;
seastar::parallel_for_each(
boost::make_counting_iterator(0u),
boost::make_counting_iterator(REMAP_NUM),
[&](auto) {
return seastar::async([&] {
uint32_t pieces = std::uniform_int_distribution<>(6, 31)(gen);
if (pieces % 2 == 1) {
pieces++;
}
std::list<uint32_t> split_points;
for (uint32_t i = 0; i < pieces; i++) {
auto p = std::uniform_int_distribution<>(1, 120)(gen);
split_points.push_back(p - p % 4);
}
split_points.sort();
auto t = create_transaction();
auto pin0 = try_get_pin(t, offset);
if (!pin0 || pin0->get_length() != length) {
early_exit++;
return;
}
auto empty_transaction = true;
auto last_rpin = pin0->duplicate();
ASSERT_TRUE(!split_points.empty());
while(!split_points.empty()) {
// new overwrite area: start_off ~ end_off
auto start_off = split_points.front();
split_points.pop_front();
auto end_off = split_points.front();
split_points.pop_front();
ASSERT_TRUE(start_off <= end_off);
if (((end_off << 10) == pin0->get_key() + pin0->get_length())
|| (start_off == end_off)) {
if (split_points.empty() && empty_transaction) {
early_exit++;
return;
}
continue;
}
empty_transaction = false;
auto new_off = (start_off << 10) - last_rpin->get_key();
auto new_len = (end_off - start_off) << 10;
bufferlist bl;
bl.append(ceph::bufferptr(ceph::buffer::create(new_len, 0)));
auto [lpin, ext, rpin] = overwrite_pin(
t, last_rpin->duplicate(), new_off, new_len, bl);
if (!ext) {
conflicted++;
return;
}
// lpin is nullptr might not cause by confliction,
// it might just not exist.
if (lpin) {
auto lext = try_get_extent(t, lpin->get_key());
if (!lext) {
conflicted++;
return;
}
if (get_random_contents() % 2 == 0) {
auto lext1 = mutate_extent(t, lext);
ASSERT_TRUE(lext1->is_exist_mutation_pending());
}
}
ASSERT_TRUE(rpin);
last_rpin = rpin->duplicate();
}
auto last_rext = try_get_extent(t, last_rpin->get_key());
if (!last_rext) {
conflicted++;
return;
}
if (get_random_contents() % 2 == 0) {
auto last_rext1 = mutate_extent(t, last_rext);
ASSERT_TRUE(last_rext1->is_exist_mutation_pending());
}
if (try_submit_transaction(std::move(t))) {
success++;
logger().info("transaction {} submit the transction",
static_cast<void*>(t.t.get()));
} else {
conflicted++;
}
});
}).handle_exception([](std::exception_ptr e) {
logger().info("{}", e);
}).get0();
logger().info("test_overwrite_pin_concurrent: "
"early_exit {} conflicted {} success {}",
early_exit, conflicted, success);
ASSERT_TRUE(success == 1 || early_exit == REMAP_NUM);
ASSERT_EQ(success + conflicted + early_exit, REMAP_NUM);
replay();
check();
});
}
};
struct tm_single_device_test_t :
public transaction_manager_test_t {
tm_single_device_test_t() : transaction_manager_test_t(1, 0) {}
};
struct tm_multi_device_test_t :
public transaction_manager_test_t {
tm_multi_device_test_t() : transaction_manager_test_t(3, 0) {}
};
struct tm_multi_tier_device_test_t :
public transaction_manager_test_t {
tm_multi_tier_device_test_t() : transaction_manager_test_t(1, 2) {}
};
TEST_P(tm_single_device_test_t, basic)
{
constexpr laddr_t SIZE = 4096;
run_async([this] {
constexpr laddr_t ADDR = 0xFF * SIZE;
{
auto t = create_transaction();
auto extent = alloc_extent(
t,
ADDR,
SIZE,
'a');
ASSERT_EQ(ADDR, extent->get_laddr());
check_mappings(t);
check();
submit_transaction(std::move(t));
check();
}
});
}
TEST_P(tm_single_device_test_t, mutate)
{
constexpr laddr_t SIZE = 4096;
run_async([this] {
constexpr laddr_t ADDR = 0xFF * SIZE;
{
auto t = create_transaction();
auto extent = alloc_extent(
t,
ADDR,
SIZE,
'a');
ASSERT_EQ(ADDR, extent->get_laddr());
check_mappings(t);
check();
submit_transaction(std::move(t));
check();
}
ASSERT_TRUE(check_usage());
replay();
{
auto t = create_transaction();
auto ext = get_extent(
t,
ADDR,
SIZE);
auto mut = mutate_extent(t, ext);
check_mappings(t);
check();
submit_transaction(std::move(t));
check();
}
ASSERT_TRUE(check_usage());
replay();
check();
});
}
TEST_P(tm_single_device_test_t, allocate_lba_conflict)
{
constexpr laddr_t SIZE = 4096;
run_async([this] {
constexpr laddr_t ADDR = 0xFF * SIZE;
constexpr laddr_t ADDR2 = 0xFE * SIZE;
auto t = create_transaction();
auto t2 = create_transaction();
// These should conflict as they should both modify the lba root
auto extent = alloc_extent(
t,
ADDR,
SIZE,
'a');
ASSERT_EQ(ADDR, extent->get_laddr());
check_mappings(t);
check();
auto extent2 = alloc_extent(
t2,
ADDR2,
SIZE,
'a');
ASSERT_EQ(ADDR2, extent2->get_laddr());
check_mappings(t2);
extent2.reset();
submit_transaction(std::move(t2));
submit_transaction_expect_conflict(std::move(t));
});
}
TEST_P(tm_single_device_test_t, mutate_lba_conflict)
{
constexpr laddr_t SIZE = 4096;
run_async([this] {
{
auto t = create_transaction();
for (unsigned i = 0; i < 300; ++i) {
auto extent = alloc_extent(
t,
laddr_t(i * SIZE),
SIZE);
}
check_mappings(t);
submit_transaction(std::move(t));
check();
}
constexpr laddr_t ADDR = 150 * SIZE;
{
auto t = create_transaction();
auto t2 = create_transaction();
mutate_addr(t, ADDR, SIZE);
mutate_addr(t2, ADDR, SIZE);
submit_transaction(std::move(t));
submit_transaction_expect_conflict(std::move(t2));
}
check();
{
auto t = create_transaction();
mutate_addr(t, ADDR, SIZE);
submit_transaction(std::move(t));
}
check();
});
}
TEST_P(tm_single_device_test_t, concurrent_mutate_lba_no_conflict)
{
constexpr laddr_t SIZE = 4096;
constexpr size_t NUM = 500;
constexpr laddr_t addr = 0;
constexpr laddr_t addr2 = SIZE * (NUM - 1);
run_async([this] {
{
auto t = create_transaction();
for (unsigned i = 0; i < NUM; ++i) {
auto extent = alloc_extent(
t,
laddr_t(i * SIZE),
SIZE);
}
submit_transaction(std::move(t));
}
{
auto t = create_transaction();
auto t2 = create_transaction();
mutate_addr(t, addr, SIZE);
mutate_addr(t2, addr2, SIZE);
submit_transaction(std::move(t));
submit_transaction(std::move(t2));
}
check();
});
}
TEST_P(tm_single_device_test_t, create_remove_same_transaction)
{
constexpr laddr_t SIZE = 4096;
run_async([this] {
constexpr laddr_t ADDR = 0xFF * SIZE;
{
auto t = create_transaction();
auto extent = alloc_extent(
t,
ADDR,
SIZE,
'a');
ASSERT_EQ(ADDR, extent->get_laddr());
check_mappings(t);
dec_ref(t, ADDR);
check_mappings(t);
extent = alloc_extent(
t,
ADDR,
SIZE,
'a');
submit_transaction(std::move(t));
check();
}
replay();
check();
});
}
TEST_P(tm_single_device_test_t, split_merge_read_same_transaction)
{
constexpr laddr_t SIZE = 4096;
run_async([this] {
{
auto t = create_transaction();
for (unsigned i = 0; i < 300; ++i) {
auto extent = alloc_extent(
t,
laddr_t(i * SIZE),
SIZE);
}
check_mappings(t);
submit_transaction(std::move(t));
check();
}
{
auto t = create_transaction();
for (unsigned i = 0; i < 240; ++i) {
dec_ref(
t,
laddr_t(i * SIZE));
}
check_mappings(t);
submit_transaction(std::move(t));
check();
}
});
}
TEST_P(tm_single_device_test_t, inc_dec_ref)
{
constexpr laddr_t SIZE = 4096;
run_async([this] {
constexpr laddr_t ADDR = 0xFF * SIZE;
{
auto t = create_transaction();
auto extent = alloc_extent(
t,
ADDR,
SIZE,
'a');
ASSERT_EQ(ADDR, extent->get_laddr());
check_mappings(t);
check();
submit_transaction(std::move(t));
check();
}
replay();
{
auto t = create_transaction();
inc_ref(t, ADDR);
check_mappings(t);
check();
submit_transaction(std::move(t));
check();
}
{
auto t = create_transaction();
dec_ref(t, ADDR);
check_mappings(t);
check();
submit_transaction(std::move(t));
check();
}
replay();
{
auto t = create_transaction();
dec_ref(t, ADDR);
check_mappings(t);
check();
submit_transaction(std::move(t));
check();
}
});
}
TEST_P(tm_single_device_test_t, cause_lba_split)
{
constexpr laddr_t SIZE = 4096;
run_async([this] {
for (unsigned i = 0; i < 200; ++i) {
auto t = create_transaction();
auto extent = alloc_extent(
t,
i * SIZE,
SIZE,
(char)(i & 0xFF));
ASSERT_EQ(i * SIZE, extent->get_laddr());
submit_transaction(std::move(t));
}
check();
});
}
TEST_P(tm_single_device_test_t, random_writes)
{
constexpr size_t TOTAL = 4<<20;
constexpr size_t BSIZE = 4<<10;
constexpr size_t PADDING_SIZE = 256<<10;
constexpr size_t BLOCKS = TOTAL / BSIZE;
run_async([this] {
for (unsigned i = 0; i < BLOCKS; ++i) {
auto t = create_transaction();
auto extent = alloc_extent(
t,
i * BSIZE,
BSIZE);
ASSERT_EQ(i * BSIZE, extent->get_laddr());
submit_transaction(std::move(t));
}
for (unsigned i = 0; i < 4; ++i) {
for (unsigned j = 0; j < 65; ++j) {
auto t = create_transaction();
for (unsigned k = 0; k < 2; ++k) {
auto ext = get_extent(
t,
get_random_laddr(BSIZE, TOTAL),
BSIZE);
auto mut = mutate_extent(t, ext);
// pad out transaction
auto padding = alloc_extent(
t,
TOTAL + (k * PADDING_SIZE),
PADDING_SIZE);
dec_ref(t, padding->get_laddr());
}
submit_transaction(std::move(t));
}
replay();
logger().info("random_writes: {} checking", i);
check();
logger().info("random_writes: {} done replaying/checking", i);
}
});
}
TEST_P(tm_single_device_test_t, find_hole_assert_trigger)
{
constexpr unsigned max = 10;
constexpr size_t BSIZE = 4<<10;
int num = 40;
run([&, this] {
return seastar::parallel_for_each(
boost::make_counting_iterator(0u),
boost::make_counting_iterator(max),
[&, this](auto idx) {
return allocate_sequentially(BSIZE, num);
});
});
}
TEST_P(tm_single_device_test_t, remap_lazy_read)
{
constexpr laddr_t offset = 0;
constexpr size_t length = 256 << 10;
run_async([this, offset] {
{
auto t = create_transaction();
auto extent = alloc_extent(
t,
offset,
length,
'a');
ASSERT_EQ(offset, extent->get_laddr());
check_mappings(t);
submit_transaction(std::move(t));
check();
}
replay();
{
auto t = create_transaction();
auto pin = get_pin(t, offset);
auto rpin = remap_pin(t, std::move(pin), 0, 128 << 10);
check_mappings(t);
submit_transaction(std::move(t));
check();
}
replay();
{
auto t = create_transaction();
auto pin = get_pin(t, offset);
bufferlist bl;
bl.append(ceph::bufferptr(ceph::buffer::create(64 << 10, 0)));
auto [lpin, ext, rpin] = overwrite_pin(
t, std::move(pin), 4 << 10 , 64 << 10, bl);
check_mappings(t);
submit_transaction(std::move(t));
check();
}
replay();
});
}
TEST_P(tm_single_device_test_t, random_writes_concurrent)
{
test_random_writes_concurrent();
}
TEST_P(tm_multi_device_test_t, random_writes_concurrent)
{
test_random_writes_concurrent();
}
TEST_P(tm_multi_tier_device_test_t, evict)
{
test_evict();
}
TEST_P(tm_single_device_test_t, parallel_extent_read)
{
test_parallel_extent_read();
}
TEST_P(tm_single_device_test_t, test_remap_pin)
{
test_remap_pin();
}
TEST_P(tm_single_device_test_t, test_overwrite_pin)
{
test_overwrite_pin();
}
TEST_P(tm_single_device_test_t, test_remap_pin_concurrent)
{
test_remap_pin_concurrent();
}
TEST_P(tm_single_device_test_t, test_overwrite_pin_concurrent)
{
test_overwrite_pin_concurrent();
}
INSTANTIATE_TEST_SUITE_P(
transaction_manager_test,
tm_single_device_test_t,
::testing::Values (
"segmented",
"circularbounded"
)
);
INSTANTIATE_TEST_SUITE_P(
transaction_manager_test,
tm_multi_device_test_t,
::testing::Values (
"segmented"
)
);
INSTANTIATE_TEST_SUITE_P(
transaction_manager_test,
tm_multi_tier_device_test_t,
::testing::Values (
"segmented"
)
);
|