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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=2 sw=2 smarttab
#include "./pg_scrubber.h" // the '.' notation used to affect clang-format order
#include <iostream>
#include <vector>
#include "debug.h"
#include "common/errno.h"
#include "messages/MOSDOp.h"
#include "messages/MOSDRepScrub.h"
#include "messages/MOSDRepScrubMap.h"
#include "messages/MOSDScrub.h"
#include "messages/MOSDScrubReserve.h"
#include "OSD.h"
#include "ScrubStore.h"
#include "scrub_machine.h"
using namespace Scrub;
using namespace std::chrono;
using namespace std::chrono_literals;
#define dout_context (m_osds->cct)
#define dout_subsys ceph_subsys_osd
#undef dout_prefix
#define dout_prefix _prefix(_dout, this)
template <class T>
static ostream& _prefix(std::ostream* _dout, T* t)
{
return t->gen_prefix(*_dout);
}
ostream& operator<<(ostream& out, const scrub_flags_t& sf)
{
if (sf.auto_repair)
out << " AUTO_REPAIR";
if (sf.check_repair)
out << " CHECK_REPAIR";
if (sf.deep_scrub_on_error)
out << " DEEP_SCRUB_ON_ERROR";
if (sf.required)
out << " REQ_SCRUB";
return out;
}
ostream& operator<<(ostream& out, const requested_scrub_t& sf)
{
if (sf.must_repair)
out << " MUST_REPAIR";
if (sf.auto_repair)
out << " planned AUTO_REPAIR";
if (sf.check_repair)
out << " planned CHECK_REPAIR";
if (sf.deep_scrub_on_error)
out << " planned DEEP_SCRUB_ON_ERROR";
if (sf.must_deep_scrub)
out << " MUST_DEEP_SCRUB";
if (sf.must_scrub)
out << " MUST_SCRUB";
if (sf.time_for_deep)
out << " TIME_FOR_DEEP";
if (sf.need_auto)
out << " NEED_AUTO";
if (sf.req_scrub)
out << " planned REQ_SCRUB";
return out;
}
/*
* if the incoming message is from a previous interval, it must mean
* PrimaryLogPG::on_change() was called when that interval ended. We can safely discard
* the stale message.
*/
bool PgScrubber::check_interval(epoch_t epoch_to_verify)
{
return epoch_to_verify >= m_pg->get_same_interval_since();
}
bool PgScrubber::is_message_relevant(epoch_t epoch_to_verify)
{
if (!m_active) {
// not scrubbing. We can assume that the scrub was already terminated, and we
// can silently discard the incoming event.
return false;
}
// is this a message from before we started this scrub?
if (epoch_to_verify < m_epoch_start) {
return false;
}
// has a new interval started?
if (!check_interval(epoch_to_verify)) {
// if this is a new interval, on_change() has already terminated that
// old scrub.
return false;
}
ceph_assert(is_primary());
// were we instructed to abort?
return verify_against_abort(epoch_to_verify);
}
bool PgScrubber::verify_against_abort(epoch_t epoch_to_verify)
{
if (!should_abort()) {
return true;
}
dout(10) << __func__ << " aborting. incoming epoch: " << epoch_to_verify
<< " vs last-aborted: " << m_last_aborted << dendl;
// if we were not aware of the abort before - kill the scrub.
if (epoch_to_verify >= m_last_aborted) {
scrub_clear_state();
m_last_aborted = std::max(epoch_to_verify, m_epoch_start);
}
return false;
}
bool PgScrubber::should_abort() const
{
if (m_flags.required) {
return false; // not stopping 'required' scrubs for configuration changes
}
if (m_is_deep) {
if (get_osdmap()->test_flag(CEPH_OSDMAP_NODEEP_SCRUB) ||
m_pg->pool.info.has_flag(pg_pool_t::FLAG_NODEEP_SCRUB)) {
dout(10) << "nodeep_scrub set, aborting" << dendl;
return true;
}
} else if (get_osdmap()->test_flag(CEPH_OSDMAP_NOSCRUB) ||
m_pg->pool.info.has_flag(pg_pool_t::FLAG_NOSCRUB)) {
dout(10) << "noscrub set, aborting" << dendl;
return true;
}
return false;
}
// initiating state-machine events --------------------------------
/*
* a note re the checks performed before sending scrub-initiating messages:
*
* For those ('StartScrub', 'AfterRepairScrub') scrub-initiation messages that
* possibly were in the queue while the PG changed state and became unavailable for
* scrubbing:
*
* The check_interval() catches all major changes to the PG. As for the other conditions
* we may check (and see is_message_relevant() above):
*
* - we are not 'active' yet, so must not check against is_active(), and:
*
* - the 'abort' flags were just verified (when the triggering message was queued). As
* those are only modified in human speeds - they need not be queried again.
*
* Some of the considerations above are also relevant to the replica-side initiation
* ('StartReplica' & 'StartReplicaNoWait').
*/
void PgScrubber::initiate_regular_scrub(epoch_t epoch_queued)
{
dout(15) << __func__ << " epoch: " << epoch_queued << dendl;
// we may have lost our Primary status while the message languished in the queue
if (check_interval(epoch_queued)) {
dout(10) << "scrubber event -->> StartScrub epoch: " << epoch_queued << dendl;
reset_epoch(epoch_queued);
m_fsm->process_event(StartScrub{});
dout(10) << "scrubber event --<< StartScrub" << dendl;
} else {
// and just in case snap trimming was blocked by the aborted scrub
m_pg->snap_trimmer_scrub_complete();
clear_queued_or_active();
}
}
void PgScrubber::initiate_scrub_after_repair(epoch_t epoch_queued)
{
dout(15) << __func__ << " epoch: " << epoch_queued << dendl;
// we may have lost our Primary status while the message languished in the queue
if (check_interval(epoch_queued)) {
dout(10) << "scrubber event -->> AfterRepairScrub epoch: " << epoch_queued << dendl;
reset_epoch(epoch_queued);
m_fsm->process_event(AfterRepairScrub{});
dout(10) << "scrubber event --<< AfterRepairScrub" << dendl;
} else {
m_pg->snap_trimmer_scrub_complete();
clear_queued_or_active();
}
}
void PgScrubber::send_scrub_unblock(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (is_message_relevant(epoch_queued)) {
m_fsm->process_event(Unblocked{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_scrub_resched(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (is_message_relevant(epoch_queued)) {
m_fsm->process_event(InternalSchedScrub{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_start_replica(epoch_t epoch_queued, Scrub::act_token_t token)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued
<< " token: " << token << dendl;
if (is_primary()) {
// shouldn't happen. Ignore
dout(1) << "got a replica scrub request while Primary!" << dendl;
return;
}
if (check_interval(epoch_queued) && is_token_current(token)) {
// save us some time by not waiting for updates if there are none
// to wait for. Affects the transition from NotActive into either
// ReplicaWaitUpdates or ActiveReplica.
if (pending_active_pushes())
m_fsm->process_event(StartReplica{});
else
m_fsm->process_event(StartReplicaNoWait{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_sched_replica(epoch_t epoch_queued, Scrub::act_token_t token)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued
<< " token: " << token << dendl;
if (check_interval(epoch_queued) && is_token_current(token)) {
m_fsm->process_event(SchedReplica{}); // retest for map availability
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::active_pushes_notification(epoch_t epoch_queued)
{
// note: Primary only
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (is_message_relevant(epoch_queued)) {
m_fsm->process_event(ActivePushesUpd{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::update_applied_notification(epoch_t epoch_queued)
{
// note: Primary only
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (is_message_relevant(epoch_queued)) {
m_fsm->process_event(UpdatesApplied{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::digest_update_notification(epoch_t epoch_queued)
{
// note: Primary only
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (is_message_relevant(epoch_queued)) {
m_fsm->process_event(DigestUpdate{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_local_map_done(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (is_message_relevant(epoch_queued)) {
m_fsm->process_event(Scrub::IntLocalMapDone{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_replica_maps_ready(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (is_message_relevant(epoch_queued)) {
m_fsm->process_event(GotReplicas{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_replica_pushes_upd(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (check_interval(epoch_queued)) {
m_fsm->process_event(ReplicaPushesUpd{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_remotes_reserved(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
// note: scrub is not active yet
if (check_interval(epoch_queued)) {
m_fsm->process_event(RemotesReserved{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_reservation_failure(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (check_interval(epoch_queued)) { // do not check for 'active'!
m_fsm->process_event(ReservationFailure{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_full_reset(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
m_fsm->process_event(Scrub::FullReset{});
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_chunk_free(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (check_interval(epoch_queued)) {
m_fsm->process_event(Scrub::SelectedChunkFree{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_chunk_busy(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (check_interval(epoch_queued)) {
m_fsm->process_event(Scrub::ChunkIsBusy{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_get_next_chunk(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
if (is_message_relevant(epoch_queued)) {
m_fsm->process_event(Scrub::NextChunk{});
}
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_scrub_is_finished(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
// can't check for "active"
m_fsm->process_event(Scrub::ScrubFinished{});
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
void PgScrubber::send_maps_compared(epoch_t epoch_queued)
{
dout(10) << "scrubber event -->> " << __func__ << " epoch: " << epoch_queued << dendl;
m_fsm->process_event(Scrub::MapsCompared{});
dout(10) << "scrubber event --<< " << __func__ << dendl;
}
// -----------------
bool PgScrubber::is_reserving() const
{
return m_fsm->is_reserving();
}
void PgScrubber::reset_epoch(epoch_t epoch_queued)
{
dout(10) << __func__ << " state deep? " << state_test(PG_STATE_DEEP_SCRUB) << dendl;
m_fsm->assert_not_active();
m_epoch_start = epoch_queued;
m_needs_sleep = true;
m_is_deep = state_test(PG_STATE_DEEP_SCRUB);
update_op_mode_text();
}
unsigned int PgScrubber::scrub_requeue_priority(Scrub::scrub_prio_t with_priority) const
{
unsigned int qu_priority = m_flags.priority;
if (with_priority == Scrub::scrub_prio_t::high_priority) {
qu_priority =
std::max(qu_priority, (unsigned int)m_pg->get_cct()->_conf->osd_client_op_priority);
}
return qu_priority;
}
unsigned int PgScrubber::scrub_requeue_priority(Scrub::scrub_prio_t with_priority,
unsigned int suggested_priority) const
{
if (with_priority == Scrub::scrub_prio_t::high_priority) {
suggested_priority = std::max(suggested_priority,
(unsigned int)m_pg->cct->_conf->osd_client_op_priority);
}
return suggested_priority;
}
// ///////////////////////////////////////////////////////////////////// //
// scrub-op registration handling
bool PgScrubber::is_scrub_registered() const
{
return !m_scrub_reg_stamp.is_zero();
}
void PgScrubber::reg_next_scrub(const requested_scrub_t& request_flags)
{
if (!is_primary()) {
// normal. No warning is required.
return;
}
dout(10) << __func__ << " planned: must? " << request_flags.must_scrub << " need-auto? "
<< request_flags.need_auto << " stamp: " << m_pg->info.history.last_scrub_stamp
<< dendl;
ceph_assert(!is_scrub_registered());
utime_t reg_stamp;
bool must = false;
if (request_flags.must_scrub || request_flags.need_auto) {
// Set the smallest time that isn't utime_t()
reg_stamp = PgScrubber::scrub_must_stamp();
must = true;
} else if (m_pg->info.stats.stats_invalid &&
m_pg->cct->_conf->osd_scrub_invalid_stats) {
reg_stamp = ceph_clock_now();
must = true;
} else {
reg_stamp = m_pg->info.history.last_scrub_stamp;
}
dout(15) << __func__ << " pg(" << m_pg_id << ") must: " << must
<< " required:" << m_flags.required << " flags: " << request_flags
<< " stamp: " << reg_stamp << dendl;
const double scrub_min_interval =
m_pg->pool.info.opts.value_or(pool_opts_t::SCRUB_MIN_INTERVAL, 0.0);
const double scrub_max_interval =
m_pg->pool.info.opts.value_or(pool_opts_t::SCRUB_MAX_INTERVAL, 0.0);
// note the sched_time, so we can locate this scrub, and remove it later
m_scrub_reg_stamp = m_osds->reg_pg_scrub(m_pg->info.pgid, reg_stamp, scrub_min_interval,
scrub_max_interval, must);
dout(15) << __func__ << " pg(" << m_pg_id << ") register next scrub, scrub time "
<< m_scrub_reg_stamp << ", must = " << (int)must << dendl;
}
void PgScrubber::unreg_next_scrub()
{
if (is_scrub_registered()) {
dout(15) << __func__ << " existing-" << m_scrub_reg_stamp << dendl;
m_osds->unreg_pg_scrub(m_pg->info.pgid, m_scrub_reg_stamp);
m_scrub_reg_stamp = utime_t{};
}
}
void PgScrubber::scrub_requested(scrub_level_t scrub_level,
scrub_type_t scrub_type,
requested_scrub_t& req_flags)
{
dout(10) << __func__ << (scrub_level == scrub_level_t::deep ? " deep " : " shallow ")
<< (scrub_type == scrub_type_t::do_repair ? " repair-scrub " : " not-repair ")
<< " prev stamp: " << m_scrub_reg_stamp << " " << is_scrub_registered()
<< dendl;
unreg_next_scrub();
req_flags.must_scrub = true;
req_flags.must_deep_scrub =
(scrub_level == scrub_level_t::deep) || (scrub_type == scrub_type_t::do_repair);
req_flags.must_repair = (scrub_type == scrub_type_t::do_repair);
// User might intervene, so clear this
req_flags.need_auto = false;
req_flags.req_scrub = true;
dout(20) << __func__ << " pg(" << m_pg_id << ") planned:" << req_flags << dendl;
reg_next_scrub(req_flags);
}
void PgScrubber::request_rescrubbing(requested_scrub_t& req_flags)
{
dout(10) << __func__ << " existing-" << m_scrub_reg_stamp << ". was registered? "
<< is_scrub_registered() << dendl;
unreg_next_scrub();
req_flags.need_auto = true;
reg_next_scrub(req_flags);
}
bool PgScrubber::reserve_local()
{
// try to create the reservation object (which translates into asking the
// OSD for the local scrub resource). If failing - undo it immediately
m_local_osd_resource.emplace(m_osds);
if (m_local_osd_resource->is_reserved()) {
dout(15) << __func__ << ": local resources reserved" << dendl;
return true;
}
dout(10) << __func__ << ": failed to reserve local scrub resources" << dendl;
m_local_osd_resource.reset();
return false;
}
// ----------------------------------------------------------------------------
bool PgScrubber::has_pg_marked_new_updates() const
{
auto last_applied = m_pg->recovery_state.get_last_update_applied();
dout(10) << __func__ << " recovery last: " << last_applied
<< " vs. scrub's: " << m_subset_last_update << dendl;
return last_applied >= m_subset_last_update;
}
void PgScrubber::set_subset_last_update(eversion_t e)
{
m_subset_last_update = e;
dout(15) << __func__ << " last-update: " << e << dendl;
}
void PgScrubber::on_applied_when_primary(const eversion_t& applied_version)
{
// we are only interested in updates if we are the Primary, and in state
// WaitLastUpdate
if (m_fsm->is_accepting_updates() && (applied_version >= m_subset_last_update)) {
m_osds->queue_scrub_applied_update(m_pg, m_pg->is_scrub_blocking_ops());
dout(15) << __func__ << " update: " << applied_version
<< " vs. required: " << m_subset_last_update << dendl;
}
}
/*
* The selected range is set directly into 'm_start' and 'm_end'
* setting:
* - m_subset_last_update
* - m_max_end
* - end
* - start
*/
bool PgScrubber::select_range()
{
m_primary_scrubmap = ScrubMap{};
m_received_maps.clear();
/* get the start and end of our scrub chunk
*
* Our scrub chunk has an important restriction we're going to need to
* respect. We can't let head be start or end.
* Using a half-open interval means that if end == head,
* we'd scrub/lock head and the clone right next to head in different
* chunks which would allow us to miss clones created between
* scrubbing that chunk and scrubbing the chunk including head.
* This isn't true for any of the other clones since clones can
* only be created "just to the left of" head. There is one exception
* to this: promotion of clones which always happens to the left of the
* left-most clone, but promote_object checks the scrubber in that
* case, so it should be ok. Also, it's ok to "miss" clones at the
* left end of the range if we are a tier because they may legitimately
* not exist (see _scrub).
*/
int min_idx = std::max<int64_t>(
3, m_pg->get_cct()->_conf->osd_scrub_chunk_min / preemption_data.chunk_divisor());
int max_idx = std::max<int64_t>(min_idx, m_pg->get_cct()->_conf->osd_scrub_chunk_max /
preemption_data.chunk_divisor());
dout(10) << __func__ << " Min: " << min_idx << " Max: " << max_idx
<< " Div: " << preemption_data.chunk_divisor() << dendl;
hobject_t start = m_start;
hobject_t candidate_end;
std::vector<hobject_t> objects;
int ret = m_pg->get_pgbackend()->objects_list_partial(start, min_idx, max_idx, &objects,
&candidate_end);
ceph_assert(ret >= 0);
if (!objects.empty()) {
hobject_t back = objects.back();
while (candidate_end.is_head() && candidate_end == back.get_head()) {
candidate_end = back;
objects.pop_back();
if (objects.empty()) {
ceph_assert(0 ==
"Somehow we got more than 2 objects which"
"have the same head but are not clones");
}
back = objects.back();
}
if (candidate_end.is_head()) {
ceph_assert(candidate_end != back.get_head());
candidate_end = candidate_end.get_object_boundary();
}
} else {
ceph_assert(candidate_end.is_max());
}
// is that range free for us? if not - we will be rescheduled later by whoever
// triggered us this time
if (!m_pg->_range_available_for_scrub(m_start, candidate_end)) {
// we'll be requeued by whatever made us unavailable for scrub
dout(10) << __func__ << ": scrub blocked somewhere in range "
<< "[" << m_start << ", " << candidate_end << ")" << dendl;
return false;
}
m_end = candidate_end;
if (m_end > m_max_end)
m_max_end = m_end;
dout(15) << __func__ << " range selected: " << m_start << " //// " << m_end << " //// "
<< m_max_end << dendl;
return true;
}
void PgScrubber::select_range_n_notify()
{
if (select_range()) {
// the next chunk to handle is not blocked
dout(20) << __func__ << ": selection OK" << dendl;
m_osds->queue_scrub_chunk_free(m_pg, Scrub::scrub_prio_t::low_priority);
} else {
// we will wait for the objects range to become available for scrubbing
dout(10) << __func__ << ": selected chunk is busy" << dendl;
m_osds->queue_scrub_chunk_busy(m_pg, Scrub::scrub_prio_t::low_priority);
}
}
bool PgScrubber::write_blocked_by_scrub(const hobject_t& soid)
{
if (soid < m_start || soid >= m_end) {
return false;
}
dout(20) << __func__ << " " << soid << " can preempt? "
<< preemption_data.is_preemptable() << " already preempted? "
<< preemption_data.was_preempted() << dendl;
if (preemption_data.was_preempted()) {
// otherwise - write requests arriving while 'already preempted' is set
// but 'preemptable' is not - will not be allowed to continue, and will
// not be requeued on time.
return false;
}
if (preemption_data.is_preemptable()) {
dout(10) << __func__ << " " << soid << " preempted" << dendl;
// signal the preemption
preemption_data.do_preempt();
m_end = m_start; // free the range we were scrubbing
return false;
}
return true;
}
bool PgScrubber::range_intersects_scrub(const hobject_t& start, const hobject_t& end)
{
// does [start, end] intersect [scrubber.start, scrubber.m_max_end)
return (start < m_max_end && end >= m_start);
}
/**
* if we are required to sleep:
* arrange a callback sometimes later.
* be sure to be able to identify a stale callback.
* Otherwise: perform a requeue (i.e. - rescheduling thru the OSD queue)
* anyway.
*/
void PgScrubber::add_delayed_scheduling()
{
m_end = m_start; // not blocking any range now
milliseconds sleep_time{0ms};
if (m_needs_sleep) {
double scrub_sleep = 1000.0 * m_osds->osd->scrub_sleep_time(m_flags.required);
sleep_time = milliseconds{long(scrub_sleep)};
}
dout(15) << __func__ << " sleep: " << sleep_time.count() << "ms. needed? "
<< m_needs_sleep << dendl;
if (sleep_time.count()) {
// schedule a transition for some 'sleep_time' ms in the future
m_needs_sleep = false;
m_sleep_started_at = ceph_clock_now();
// the following log line is used by osd-scrub-test.sh
dout(20) << __func__ << " scrub state is PendingTimer, sleeping" << dendl;
// the 'delayer' for crimson is different. Will be factored out.
spg_t pgid = m_pg->get_pgid();
auto callbk = new LambdaContext([osds = m_osds, pgid,
scrbr = this]([[maybe_unused]] int r) mutable {
PGRef pg = osds->osd->lookup_lock_pg(pgid);
if (!pg) {
lgeneric_subdout(g_ceph_context, osd, 10)
<< "scrub_requeue_callback: Could not find "
<< "PG " << pgid << " can't complete scrub requeue after sleep" << dendl;
return;
}
scrbr->m_needs_sleep = true;
lgeneric_dout(scrbr->get_pg_cct(), 7)
<< "scrub_requeue_callback: slept for "
<< ceph_clock_now() - scrbr->m_sleep_started_at << ", re-queuing scrub" << dendl;
scrbr->m_sleep_started_at = utime_t{};
osds->queue_for_scrub_resched(&(*pg), Scrub::scrub_prio_t::low_priority);
pg->unlock();
});
std::lock_guard l(m_osds->sleep_lock);
m_osds->sleep_timer.add_event_after(sleep_time.count() / 1000.0f, callbk);
} else {
// just a requeue
m_osds->queue_for_scrub_resched(m_pg, Scrub::scrub_prio_t::high_priority);
}
}
eversion_t PgScrubber::search_log_for_updates() const
{
auto& projected = m_pg->projected_log.log;
auto pi = find_if(
projected.crbegin(), projected.crend(),
[this](const auto& e) -> bool { return e.soid >= m_start && e.soid < m_end; });
if (pi != projected.crend())
return pi->version;
// there was no relevant update entry in the log
auto& log = m_pg->recovery_state.get_pg_log().get_log().log;
auto p = find_if(log.crbegin(), log.crend(), [this](const auto& e) -> bool {
return e.soid >= m_start && e.soid < m_end;
});
if (p == log.crend())
return eversion_t{};
else
return p->version;
}
void PgScrubber::get_replicas_maps(bool replica_can_preempt)
{
dout(10) << __func__ << " started in epoch/interval: " << m_epoch_start << "/"
<< m_interval_start
<< " pg same_interval_since: " << m_pg->info.history.same_interval_since
<< dendl;
m_primary_scrubmap_pos.reset();
// ask replicas to scan and send maps
for (const auto& i : m_pg->get_actingset()) {
if (i == m_pg_whoami)
continue;
m_maps_status.mark_replica_map_request(i);
_request_scrub_map(i, m_subset_last_update, m_start, m_end, m_is_deep,
replica_can_preempt);
}
dout(10) << __func__ << " awaiting" << m_maps_status << dendl;
}
bool PgScrubber::was_epoch_changed() const
{
// for crimson we have m_pg->get_info().history.same_interval_since
dout(10) << __func__ << " epoch_start: " << m_interval_start
<< " from pg: " << m_pg->get_history().same_interval_since << dendl;
return m_interval_start < m_pg->get_history().same_interval_since;
}
void PgScrubber::mark_local_map_ready()
{
m_maps_status.mark_local_map_ready();
}
bool PgScrubber::are_all_maps_available() const
{
return m_maps_status.are_all_maps_available();
}
std::string PgScrubber::dump_awaited_maps() const
{
return m_maps_status.dump();
}
void PgScrubber::update_op_mode_text()
{
auto visible_repair = state_test(PG_STATE_REPAIR);
m_mode_desc = (visible_repair ? "repair"sv : (m_is_deep ? "deep-scrub"sv : "scrub"sv));
dout(10) << __func__ << ": repair: visible: " << (visible_repair ? "true" : "false")
<< ", internal: " << (m_is_repair ? "true" : "false")
<< ". Displayed: " << m_mode_desc << dendl;
}
void PgScrubber::_request_scrub_map(pg_shard_t replica,
eversion_t version,
hobject_t start,
hobject_t end,
bool deep,
bool allow_preemption)
{
ceph_assert(replica != m_pg_whoami);
dout(10) << __func__ << " scrubmap from osd." << replica
<< (deep ? " deep" : " shallow") << dendl;
auto repscrubop =
new MOSDRepScrub(spg_t(m_pg->info.pgid.pgid, replica.shard), version,
get_osdmap_epoch(), m_pg->get_last_peering_reset(), start, end, deep,
allow_preemption, m_flags.priority, m_pg->ops_blocked_by_scrub());
// default priority. We want the replica-scrub processed prior to any recovery
// or client io messages (we are holding a lock!)
m_osds->send_message_osd_cluster(replica.osd, repscrubop, get_osdmap_epoch());
}
void PgScrubber::cleanup_store(ObjectStore::Transaction* t)
{
if (!m_store)
return;
struct OnComplete : Context {
std::unique_ptr<Scrub::Store> store;
explicit OnComplete(std::unique_ptr<Scrub::Store>&& store) : store(std::move(store))
{}
void finish(int) override {}
};
m_store->cleanup(t);
t->register_on_complete(new OnComplete(std::move(m_store)));
ceph_assert(!m_store);
}
void PgScrubber::on_init()
{
// going upwards from 'inactive'
ceph_assert(!is_scrub_active());
preemption_data.reset();
m_pg->publish_stats_to_osd();
m_interval_start = m_pg->get_history().same_interval_since;
dout(10) << __func__ << " start same_interval:" << m_interval_start << dendl;
// create a new store
{
ObjectStore::Transaction t;
cleanup_store(&t);
m_store.reset(
Scrub::Store::create(m_pg->osd->store, &t, m_pg->info.pgid, m_pg->coll));
m_pg->osd->store->queue_transaction(m_pg->ch, std::move(t), nullptr);
}
m_start = m_pg->info.pgid.pgid.get_hobj_start();
m_active = true;
}
void PgScrubber::on_replica_init()
{
m_active = true;
}
void PgScrubber::_scan_snaps(ScrubMap& smap)
{
hobject_t head;
SnapSet snapset;
// Test qa/standalone/scrub/osd-scrub-snaps.sh greps for the strings
// in this function
dout(15) << "_scan_snaps starts" << dendl;
for (auto i = smap.objects.rbegin(); i != smap.objects.rend(); ++i) {
const hobject_t& hoid = i->first;
ScrubMap::object& o = i->second;
dout(20) << __func__ << " " << hoid << dendl;
ceph_assert(!hoid.is_snapdir());
if (hoid.is_head()) {
// parse the SnapSet
bufferlist bl;
if (o.attrs.find(SS_ATTR) == o.attrs.end()) {
continue;
}
bl.push_back(o.attrs[SS_ATTR]);
auto p = bl.cbegin();
try {
decode(snapset, p);
} catch (...) {
continue;
}
head = hoid.get_head();
continue;
}
if (hoid.snap < CEPH_MAXSNAP) {
// check and if necessary fix snap_mapper
if (hoid.get_head() != head) {
derr << __func__ << " no head for " << hoid << " (have " << head << ")" << dendl;
continue;
}
set<snapid_t> obj_snaps;
auto p = snapset.clone_snaps.find(hoid.snap);
if (p == snapset.clone_snaps.end()) {
derr << __func__ << " no clone_snaps for " << hoid << " in " << snapset << dendl;
continue;
}
obj_snaps.insert(p->second.begin(), p->second.end());
set<snapid_t> cur_snaps;
int r = m_pg->snap_mapper.get_snaps(hoid, &cur_snaps);
if (r != 0 && r != -ENOENT) {
derr << __func__ << ": get_snaps returned " << cpp_strerror(r) << dendl;
ceph_abort();
}
if (r == -ENOENT || cur_snaps != obj_snaps) {
ObjectStore::Transaction t;
OSDriver::OSTransaction _t(m_pg->osdriver.get_transaction(&t));
if (r == 0) {
r = m_pg->snap_mapper.remove_oid(hoid, &_t);
if (r != 0) {
derr << __func__ << ": remove_oid returned " << cpp_strerror(r) << dendl;
ceph_abort();
}
m_pg->osd->clog->error()
<< "osd." << m_pg->osd->whoami << " found snap mapper error on pg "
<< m_pg->info.pgid << " oid " << hoid << " snaps in mapper: " << cur_snaps
<< ", oi: " << obj_snaps << "...repaired";
} else {
m_pg->osd->clog->error()
<< "osd." << m_pg->osd->whoami << " found snap mapper error on pg "
<< m_pg->info.pgid << " oid " << hoid << " snaps missing in mapper"
<< ", should be: " << obj_snaps << " was " << cur_snaps << " r " << r
<< "...repaired";
}
m_pg->snap_mapper.add_oid(hoid, obj_snaps, &_t);
// wait for repair to apply to avoid confusing other bits of the system.
{
dout(15) << __func__ << " wait on repair!" << dendl;
ceph::condition_variable my_cond;
ceph::mutex my_lock = ceph::make_mutex("PG::_scan_snaps my_lock");
int e = 0;
bool done;
t.register_on_applied_sync(new C_SafeCond(my_lock, my_cond, &done, &e));
e = m_pg->osd->store->queue_transaction(m_pg->ch, std::move(t));
if (e != 0) {
derr << __func__ << ": queue_transaction got " << cpp_strerror(e) << dendl;
} else {
std::unique_lock l{my_lock};
my_cond.wait(l, [&done] { return done; });
}
}
}
}
}
}
int PgScrubber::build_primary_map_chunk()
{
epoch_t map_building_since = m_pg->get_osdmap_epoch();
dout(20) << __func__ << ": initiated at epoch " << map_building_since << dendl;
auto ret = build_scrub_map_chunk(m_primary_scrubmap, m_primary_scrubmap_pos, m_start,
m_end, m_is_deep);
if (ret == -EINPROGRESS) {
// reschedule another round of asking the backend to collect the scrub data
m_osds->queue_for_scrub_resched(m_pg, Scrub::scrub_prio_t::low_priority);
}
return ret;
}
int PgScrubber::build_replica_map_chunk()
{
dout(10) << __func__ << " interval start: " << m_interval_start
<< " current token: " << m_current_token << " epoch: " << m_epoch_start
<< " deep: " << m_is_deep << dendl;
auto ret = build_scrub_map_chunk(replica_scrubmap, replica_scrubmap_pos, m_start, m_end,
m_is_deep);
switch (ret) {
case -EINPROGRESS:
// must wait for the backend to finish. No external event source.
// (note: previous version used low priority here. Now switched to using the
// priority of the original message)
m_osds->queue_for_rep_scrub_resched(m_pg, m_replica_request_priority,
m_flags.priority, m_current_token);
break;
case 0: {
// finished!
m_cleaned_meta_map.clear_from(m_start);
m_cleaned_meta_map.insert(replica_scrubmap);
auto for_meta_scrub = clean_meta_map();
_scan_snaps(for_meta_scrub);
// the local map has been created. Send it to the primary.
// Note: once the message reaches the Primary, it may ask us for another
// chunk - and we better be done with the current scrub. Thus - the preparation of
// the reply message is separate, and we clear the scrub state before actually
// sending it.
auto reply = prep_replica_map_msg(PreemptionNoted::no_preemption);
replica_handling_done();
dout(15) << __func__ << " chunk map sent " << dendl;
send_replica_map(reply);
} break;
default:
// negative retval: build_scrub_map_chunk() signalled an error
// Pre-Pacific code ignored this option, treating it as a success.
// \todo Add an error flag in the returning message.
dout(1) << "Error! Aborting. ActiveReplica::react(SchedReplica) Ret: " << ret
<< dendl;
replica_handling_done();
// only in debug mode for now:
assert(false && "backend error");
break;
};
return ret;
}
int PgScrubber::build_scrub_map_chunk(
ScrubMap& map, ScrubMapBuilder& pos, hobject_t start, hobject_t end, bool deep)
{
dout(10) << __func__ << " [" << start << "," << end << ") "
<< " pos " << pos << " Deep: " << deep << dendl;
// start
while (pos.empty()) {
pos.deep = deep;
map.valid_through = m_pg->info.last_update;
// objects
vector<ghobject_t> rollback_obs;
pos.ret =
m_pg->get_pgbackend()->objects_list_range(start, end, &pos.ls, &rollback_obs);
dout(10) << __func__ << " while pos empty " << pos.ret << dendl;
if (pos.ret < 0) {
dout(5) << "objects_list_range error: " << pos.ret << dendl;
return pos.ret;
}
dout(10) << __func__ << " pos.ls.empty()? " << (pos.ls.empty() ? "+" : "-") << dendl;
if (pos.ls.empty()) {
break;
}
m_pg->_scan_rollback_obs(rollback_obs);
pos.pos = 0;
return -EINPROGRESS;
}
// scan objects
while (!pos.done()) {
int r = m_pg->get_pgbackend()->be_scan_list(map, pos);
if (r == -EINPROGRESS) {
dout(20) << __func__ << " in progress" << dendl;
return r;
}
}
// finish
dout(20) << __func__ << " finishing" << dendl;
ceph_assert(pos.done());
m_pg->_repair_oinfo_oid(map);
dout(20) << __func__ << " done, got " << map.objects.size() << " items" << dendl;
return 0;
}
/*
* Process:
* Building a map of objects suitable for snapshot validation.
* The data in m_cleaned_meta_map is the left over partial items that need to
* be completed before they can be processed.
*
* Snapshots in maps precede the head object, which is why we are scanning backwards.
*/
ScrubMap PgScrubber::clean_meta_map()
{
ScrubMap for_meta_scrub;
if (m_end.is_max() || m_cleaned_meta_map.objects.empty()) {
m_cleaned_meta_map.swap(for_meta_scrub);
} else {
auto iter = m_cleaned_meta_map.objects.end();
--iter; // not empty, see 'if' clause
auto begin = m_cleaned_meta_map.objects.begin();
if (iter->first.has_snapset()) {
++iter;
} else {
while (iter != begin) {
auto next = iter--;
if (next->first.get_head() != iter->first.get_head()) {
++iter;
break;
}
}
}
for_meta_scrub.objects.insert(begin, iter);
m_cleaned_meta_map.objects.erase(begin, iter);
}
return for_meta_scrub;
}
void PgScrubber::run_callbacks()
{
std::list<Context*> to_run;
to_run.swap(m_callbacks);
for (auto& tr : to_run) {
tr->complete(0);
}
}
void PgScrubber::maps_compare_n_cleanup()
{
scrub_compare_maps();
m_start = m_end;
run_callbacks();
requeue_waiting();
m_osds->queue_scrub_maps_compared(m_pg, Scrub::scrub_prio_t::low_priority);
}
Scrub::preemption_t& PgScrubber::get_preemptor()
{
return preemption_data;
}
/*
* Process note: called for the arriving "give me your map, replica!" request. Unlike
* the original implementation, we do not requeue the Op waiting for
* updates. Instead - we trigger the FSM.
*/
void PgScrubber::replica_scrub_op(OpRequestRef op)
{
op->mark_started();
auto msg = op->get_req<MOSDRepScrub>();
dout(10) << __func__ << " pg:" << m_pg->pg_id << " Msg: map_epoch:" << msg->map_epoch
<< " min_epoch:" << msg->min_epoch << " deep?" << msg->deep << dendl;
// are we still processing a previous scrub-map request without noticing that the
// interval changed? won't see it here, but rather at the reservation stage.
if (msg->map_epoch < m_pg->info.history.same_interval_since) {
dout(10) << "replica_scrub_op discarding old replica_scrub from " << msg->map_epoch
<< " < " << m_pg->info.history.same_interval_since << dendl;
// is there a general sync issue? are we holding a stale reservation?
// not checking now - assuming we will actively react to interval change.
return;
}
if (is_queued_or_active()) {
// this is bug!
// Somehow, we have received a new scrub request from our Primary, before
// having finished with the previous one. Did we go through an interval
// change without reseting the FSM? Possible responses:
// - crashing (the original assert_not_active() implemented that one), or
// - trying to recover:
// - (logging enough information to debug this scenario)
// - reset the FSM.
m_osds->clog->warn()
<< __func__
<< ": error: a second scrub-op received while handling the previous one";
scrub_clear_state();
m_osds->clog->warn() << __func__
<< ": after a reset. Now handling the new OP";
}
// make sure the FSM is at NotActive
m_fsm->assert_not_active();
replica_scrubmap = ScrubMap{};
replica_scrubmap_pos = ScrubMapBuilder{};
m_replica_min_epoch = msg->min_epoch;
m_start = msg->start;
m_end = msg->end;
m_max_end = msg->end;
m_is_deep = msg->deep;
m_interval_start = m_pg->info.history.same_interval_since;
m_replica_request_priority = msg->high_priority ? Scrub::scrub_prio_t::high_priority
: Scrub::scrub_prio_t::low_priority;
m_flags.priority = msg->priority ? msg->priority : m_pg->get_scrub_priority();
preemption_data.reset();
preemption_data.force_preemptability(msg->allow_preemption);
replica_scrubmap_pos.reset();
set_queued_or_active();
m_osds->queue_for_rep_scrub(m_pg, m_replica_request_priority,
m_flags.priority, m_current_token);
}
void PgScrubber::set_op_parameters(requested_scrub_t& request)
{
dout(10) << __func__ << " input: " << request << dendl;
// write down the epoch of starting a new scrub. Will be used
// to discard stale messages from previous aborted scrubs.
m_epoch_start = m_pg->get_osdmap_epoch();
m_flags.check_repair = request.check_repair;
m_flags.auto_repair = request.auto_repair || request.need_auto;
m_flags.required = request.req_scrub || request.must_scrub;
m_flags.priority = (request.must_scrub || request.need_auto)
? get_pg_cct()->_conf->osd_requested_scrub_priority
: m_pg->get_scrub_priority();
state_set(PG_STATE_SCRUBBING);
// will we be deep-scrubbing?
if (request.must_deep_scrub || request.need_auto || request.time_for_deep) {
state_set(PG_STATE_DEEP_SCRUB);
}
// m_is_repair is set for either 'must_repair' or 'repair-on-the-go' (i.e.
// deep-scrub with the auto_repair configuration flag set). m_is_repair value
// determines the scrubber behavior.
// PG_STATE_REPAIR, on the other hand, is only used for status reports (inc. the
// PG status as appearing in the logs).
m_is_repair = request.must_repair || m_flags.auto_repair;
if (request.must_repair) {
state_set(PG_STATE_REPAIR);
// not calling update_op_mode_text() yet, as m_is_deep not set yet
}
// the publishing here seems to be required for tests synchronization
m_pg->publish_stats_to_osd();
m_flags.deep_scrub_on_error = request.deep_scrub_on_error;
}
void PgScrubber::scrub_compare_maps()
{
dout(10) << __func__ << " has maps, analyzing" << dendl;
// construct authoritative scrub map for type-specific scrubbing
m_cleaned_meta_map.insert(m_primary_scrubmap);
map<hobject_t, pair<std::optional<uint32_t>, std::optional<uint32_t>>> missing_digest;
map<pg_shard_t, ScrubMap*> maps;
maps[m_pg_whoami] = &m_primary_scrubmap;
for (const auto& i : m_pg->get_actingset()) {
if (i == m_pg_whoami)
continue;
dout(2) << __func__ << " replica " << i << " has "
<< m_received_maps[i].objects.size() << " items" << dendl;
maps[i] = &m_received_maps[i];
}
set<hobject_t> master_set;
// Construct master set
for (const auto& map : maps) {
for (const auto& i : map.second->objects) {
master_set.insert(i.first);
}
}
stringstream ss;
m_pg->get_pgbackend()->be_omap_checks(maps, master_set, m_omap_stats, ss);
if (!ss.str().empty()) {
m_osds->clog->warn(ss);
}
if (m_pg->recovery_state.get_actingset().size() > 1) {
dout(10) << __func__ << " comparing replica scrub maps" << dendl;
// Map from object with errors to good peer
map<hobject_t, list<pg_shard_t>> authoritative;
dout(2) << __func__ << ": primary (" << m_pg->get_primary() << ") has "
<< m_primary_scrubmap.objects.size() << " items" << dendl;
ss.str("");
ss.clear();
m_pg->get_pgbackend()->be_compare_scrubmaps(
maps, master_set, m_is_repair, m_missing, m_inconsistent,
authoritative, missing_digest, m_shallow_errors, m_deep_errors, m_store.get(),
m_pg->info.pgid, m_pg->recovery_state.get_acting(), ss);
if (!ss.str().empty()) {
m_osds->clog->error(ss);
}
for (auto& i : authoritative) {
list<pair<ScrubMap::object, pg_shard_t>> good_peers;
for (list<pg_shard_t>::const_iterator j = i.second.begin(); j != i.second.end();
++j) {
good_peers.emplace_back(maps[*j]->objects[i.first], *j);
}
m_authoritative.emplace(i.first, good_peers);
}
for (auto i = authoritative.begin(); i != authoritative.end(); ++i) {
m_cleaned_meta_map.objects.erase(i->first);
m_cleaned_meta_map.objects.insert(
*(maps[i->second.back()]->objects.find(i->first)));
}
}
auto for_meta_scrub = clean_meta_map();
// ok, do the pg-type specific scrubbing
// (Validates consistency of the object info and snap sets)
scrub_snapshot_metadata(for_meta_scrub, missing_digest);
// Called here on the primary can use an authoritative map if it isn't the primary
_scan_snaps(for_meta_scrub);
if (!m_store->empty()) {
if (m_is_repair) {
dout(10) << __func__ << ": discarding scrub results" << dendl;
m_store->flush(nullptr);
} else {
dout(10) << __func__ << ": updating scrub object" << dendl;
ObjectStore::Transaction t;
m_store->flush(&t);
m_pg->osd->store->queue_transaction(m_pg->ch, std::move(t), nullptr);
}
}
}
ScrubMachineListener::MsgAndEpoch PgScrubber::prep_replica_map_msg(
PreemptionNoted was_preempted)
{
dout(10) << __func__ << " min epoch:" << m_replica_min_epoch << dendl;
auto reply =
make_message<MOSDRepScrubMap>(spg_t(m_pg->info.pgid.pgid, m_pg->get_primary().shard),
m_replica_min_epoch, m_pg_whoami);
reply->preempted = (was_preempted == PreemptionNoted::preempted);
::encode(replica_scrubmap, reply->get_data());
return ScrubMachineListener::MsgAndEpoch{reply, m_replica_min_epoch};
}
void PgScrubber::send_replica_map(const MsgAndEpoch& preprepared)
{
m_pg->send_cluster_message(m_pg->get_primary().osd, preprepared.m_msg,
preprepared.m_epoch, false);
}
void PgScrubber::send_preempted_replica()
{
auto reply =
make_message<MOSDRepScrubMap>(spg_t{m_pg->info.pgid.pgid, m_pg->get_primary().shard},
m_replica_min_epoch, m_pg_whoami);
reply->preempted = true;
::encode(replica_scrubmap, reply->get_data()); // must not skip this
m_pg->send_cluster_message(m_pg->get_primary().osd, reply, m_replica_min_epoch, false);
}
/*
* - if the replica lets us know it was interrupted, we mark the chunk as interrupted.
* The state-machine will react to that when all replica maps are received.
* - when all maps are received, we signal the FSM with the GotReplicas event (see
* scrub_send_replmaps_ready()). Note that due to the no-reentrancy limitations of the
* FSM, we do not 'process' the event directly. Instead - it is queued for the OSD to
* handle.
*/
void PgScrubber::map_from_replica(OpRequestRef op)
{
auto m = op->get_req<MOSDRepScrubMap>();
dout(15) << __func__ << " " << *m << dendl;
if (m->map_epoch < m_pg->info.history.same_interval_since) {
dout(10) << __func__ << " discarding old from " << m->map_epoch << " < "
<< m_pg->info.history.same_interval_since << dendl;
return;
}
auto p = const_cast<bufferlist&>(m->get_data()).cbegin();
m_received_maps[m->from].decode(p, m_pg->info.pgid.pool());
dout(15) << "map version is " << m_received_maps[m->from].valid_through << dendl;
auto [is_ok, err_txt] = m_maps_status.mark_arriving_map(m->from);
if (!is_ok) {
// previously an unexpected map was triggering an assert. Now, as scrubs can be
// aborted at any time, the chances of this happening have increased, and aborting is
// not justified
dout(1) << __func__ << err_txt << " from OSD " << m->from << dendl;
return;
}
if (m->preempted) {
dout(10) << __func__ << " replica was preempted, setting flag" << dendl;
preemption_data.do_preempt();
}
if (m_maps_status.are_all_maps_available()) {
dout(15) << __func__ << " all repl-maps available" << dendl;
m_osds->queue_scrub_got_repl_maps(m_pg, m_pg->is_scrub_blocking_ops());
}
}
void PgScrubber::handle_scrub_reserve_request(OpRequestRef op)
{
dout(10) << __func__ << " " << *op->get_req() << dendl;
op->mark_started();
auto request_ep = op->get_req<MOSDScrubReserve>()->get_map_epoch();
/*
* if we are currently holding a reservation, then:
* either (1) we, the scrubber, did not yet notice an interval change. The remembered
* reservation epoch is from before our interval, and we can silently discard the
* reservation (no message is required).
* or:
* (2) the interval hasn't changed, but the same Primary that (we think) holds the
* lock just sent us a new request. Note that we know it's the same Primary, as
* otherwise the interval would have changed.
* Ostensibly we can discard & redo the reservation. But then we
* will be temporarily releasing the OSD resource - and might not be able to grab it
* again. Thus, we simply treat this as a successful new request
* (but mark the fact that if there is a previous request from the primary to
* scrub a specific chunk - that request is now defunct).
*/
if (m_remote_osd_resource.has_value() && m_remote_osd_resource->is_stale()) {
// we are holding a stale reservation from a past epoch
m_remote_osd_resource.reset();
dout(10) << __func__ << " cleared existing stale reservation" << dendl;
}
if (request_ep < m_pg->get_same_interval_since()) {
// will not ack stale requests
return;
}
bool granted{false};
if (m_remote_osd_resource.has_value()) {
dout(10) << __func__ << " already reserved." << dendl;
/*
* it might well be that we did not yet finish handling the latest scrub-op from
* our primary. This happens, for example, if 'noscrub' was set via a command, then
* reset. The primary in this scenario will remain in the same interval, but we do need
* to reset our internal state (otherwise - the first renewed 'give me your scrub map'
* from the primary will see us in active state, crashing the OSD).
*/
advance_token();
granted = true;
} else if (m_pg->cct->_conf->osd_scrub_during_recovery ||
!m_osds->is_recovery_active()) {
m_remote_osd_resource.emplace(this, m_pg, m_osds, request_ep);
// OSD resources allocated?
granted = m_remote_osd_resource->is_reserved();
if (!granted) {
// just forget it
m_remote_osd_resource.reset();
dout(20) << __func__ << ": failed to reserve remotely" << dendl;
}
}
dout(10) << __func__ << " reserved? " << (granted ? "yes" : "no") << dendl;
Message* reply = new MOSDScrubReserve(
spg_t(m_pg->info.pgid.pgid, m_pg->get_primary().shard), request_ep,
granted ? MOSDScrubReserve::GRANT : MOSDScrubReserve::REJECT, m_pg_whoami);
m_osds->send_message_osd_cluster(reply, op->get_req()->get_connection());
}
void PgScrubber::handle_scrub_reserve_grant(OpRequestRef op, pg_shard_t from)
{
dout(10) << __func__ << " " << *op->get_req() << dendl;
op->mark_started();
if (m_reservations.has_value()) {
m_reservations->handle_reserve_grant(op, from);
} else {
dout(20) << __func__ << ": late/unsolicited reservation grant from osd "
<< from << " (" << op << ")" << dendl;
}
}
void PgScrubber::handle_scrub_reserve_reject(OpRequestRef op, pg_shard_t from)
{
dout(10) << __func__ << " " << *op->get_req() << dendl;
op->mark_started();
if (m_reservations.has_value()) {
// there is an active reservation process. No action is required otherwise.
m_reservations->handle_reserve_reject(op, from);
}
}
void PgScrubber::handle_scrub_reserve_release(OpRequestRef op)
{
dout(10) << __func__ << " " << *op->get_req() << dendl;
op->mark_started();
/*
* this specific scrub session has terminated. All incoming events carrying the old
* tag will be discarded.
*/
advance_token();
m_remote_osd_resource.reset();
}
void PgScrubber::discard_replica_reservations()
{
dout(10) << __func__ << dendl;
if (m_reservations.has_value()) {
m_reservations->discard_all();
}
}
void PgScrubber::clear_scrub_reservations()
{
dout(10) << __func__ << dendl;
m_reservations.reset(); // the remote reservations
m_local_osd_resource.reset(); // the local reservation
m_remote_osd_resource.reset(); // we as replica reserved for a Primary
}
void PgScrubber::message_all_replicas(int32_t opcode, std::string_view op_text)
{
ceph_assert(m_pg->recovery_state.get_backfill_targets().empty());
std::vector<pair<int, Message*>> messages;
messages.reserve(m_pg->get_actingset().size());
epoch_t epch = get_osdmap_epoch();
for (auto& p : m_pg->get_actingset()) {
if (p == m_pg_whoami)
continue;
dout(10) << "scrub requesting " << op_text << " from osd." << p << " Epoch: " << epch
<< dendl;
Message* m = new MOSDScrubReserve(spg_t(m_pg->info.pgid.pgid, p.shard), epch, opcode,
m_pg_whoami);
messages.push_back(std::make_pair(p.osd, m));
}
if (!messages.empty()) {
m_osds->send_message_osd_cluster(messages, epch);
}
}
void PgScrubber::unreserve_replicas()
{
dout(10) << __func__ << dendl;
m_reservations.reset();
}
void PgScrubber::set_queued_or_active()
{
m_queued_or_active = true;
}
void PgScrubber::clear_queued_or_active()
{
m_queued_or_active = false;
}
bool PgScrubber::is_queued_or_active() const
{
return m_queued_or_active;
}
[[nodiscard]] bool PgScrubber::scrub_process_inconsistent()
{
dout(10) << __func__ << ": checking authoritative (mode="
<< m_mode_desc << ", auth remaining #: " << m_authoritative.size()
<< ")" << dendl;
// authoritative only store objects which are missing or inconsistent.
if (!m_authoritative.empty()) {
stringstream ss;
ss << m_pg->info.pgid << " " << m_mode_desc << " " << m_missing.size() << " missing, "
<< m_inconsistent.size() << " inconsistent objects";
dout(2) << ss.str() << dendl;
m_osds->clog->error(ss);
if (m_is_repair) {
state_clear(PG_STATE_CLEAN);
// we know we have a problem, so it's OK to set the user-visible flag
// even if we only reached here via auto-repair
state_set(PG_STATE_REPAIR);
update_op_mode_text();
for (const auto& [hobj, shrd_list] : m_authoritative) {
auto missing_entry = m_missing.find(hobj);
if (missing_entry != m_missing.end()) {
m_pg->repair_object(hobj, shrd_list, missing_entry->second);
m_fixed_count += missing_entry->second.size();
}
if (m_inconsistent.count(hobj)) {
m_pg->repair_object(hobj, shrd_list, m_inconsistent[hobj]);
m_fixed_count += m_inconsistent[hobj].size();
}
}
}
}
return (!m_authoritative.empty() && m_is_repair);
}
/*
* note: only called for the Primary.
*/
void PgScrubber::scrub_finish()
{
dout(10) << __func__ << " before flags: " << m_flags
<< ". repair state: " << (state_test(PG_STATE_REPAIR) ? "repair" : "no-repair")
<< ". deep_scrub_on_error: " << m_flags.deep_scrub_on_error << dendl;
ceph_assert(m_pg->is_locked());
ceph_assert(is_queued_or_active());
m_pg->m_planned_scrub = requested_scrub_t{};
// if the repair request comes from auto-repair and large number of errors,
// we would like to cancel auto-repair
if (m_is_repair && m_flags.auto_repair &&
m_authoritative.size() > m_pg->cct->_conf->osd_scrub_auto_repair_num_errors) {
dout(10) << __func__ << " undoing the repair" << dendl;
state_clear(PG_STATE_REPAIR); // not expected to be set, anyway
m_is_repair = false;
update_op_mode_text();
}
bool do_auto_scrub = false;
// if a regular scrub had errors within the limit, do a deep scrub to auto repair
if (m_flags.deep_scrub_on_error && !m_authoritative.empty() &&
m_authoritative.size() <= m_pg->cct->_conf->osd_scrub_auto_repair_num_errors) {
ceph_assert(!m_is_deep);
do_auto_scrub = true;
dout(15) << __func__ << " Try to auto repair after scrub errors" << dendl;
}
m_flags.deep_scrub_on_error = false;
// type-specific finish (can tally more errors)
_scrub_finish();
bool has_error = scrub_process_inconsistent();
{
stringstream oss;
oss << m_pg->info.pgid.pgid << " " << m_mode_desc << " ";
int total_errors = m_shallow_errors + m_deep_errors;
if (total_errors)
oss << total_errors << " errors";
else
oss << "ok";
if (!m_is_deep && m_pg->info.stats.stats.sum.num_deep_scrub_errors)
oss << " ( " << m_pg->info.stats.stats.sum.num_deep_scrub_errors
<< " remaining deep scrub error details lost)";
if (m_is_repair)
oss << ", " << m_fixed_count << " fixed";
if (total_errors)
m_osds->clog->error(oss);
else
m_osds->clog->debug(oss);
}
// Since we don't know which errors were fixed, we can only clear them
// when every one has been fixed.
if (m_is_repair) {
if (m_fixed_count == m_shallow_errors + m_deep_errors) {
ceph_assert(m_is_deep);
m_shallow_errors = 0;
m_deep_errors = 0;
dout(20) << __func__ << " All may be fixed" << dendl;
} else if (has_error) {
// Deep scrub in order to get corrected error counts
m_pg->scrub_after_recovery = true;
m_pg->m_planned_scrub.req_scrub =
m_pg->m_planned_scrub.req_scrub || m_flags.required;
dout(20) << __func__ << " Current 'required': " << m_flags.required
<< " Planned 'req_scrub': " << m_pg->m_planned_scrub.req_scrub << dendl;
} else if (m_shallow_errors || m_deep_errors) {
// We have errors but nothing can be fixed, so there is no repair
// possible.
state_set(PG_STATE_FAILED_REPAIR);
dout(10) << __func__ << " " << (m_shallow_errors + m_deep_errors)
<< " error(s) present with no repair possible" << dendl;
}
}
{
// finish up
ObjectStore::Transaction t;
m_pg->recovery_state.update_stats(
[this](auto& history, auto& stats) {
dout(10) << "m_pg->recovery_state.update_stats()" << dendl;
utime_t now = ceph_clock_now();
history.last_scrub = m_pg->recovery_state.get_info().last_update;
history.last_scrub_stamp = now;
if (m_is_deep) {
history.last_deep_scrub = m_pg->recovery_state.get_info().last_update;
history.last_deep_scrub_stamp = now;
}
if (m_is_deep) {
if ((m_shallow_errors == 0) && (m_deep_errors == 0))
history.last_clean_scrub_stamp = now;
stats.stats.sum.num_shallow_scrub_errors = m_shallow_errors;
stats.stats.sum.num_deep_scrub_errors = m_deep_errors;
stats.stats.sum.num_large_omap_objects = m_omap_stats.large_omap_objects;
stats.stats.sum.num_omap_bytes = m_omap_stats.omap_bytes;
stats.stats.sum.num_omap_keys = m_omap_stats.omap_keys;
dout(25) << "scrub_finish shard " << m_pg_whoami
<< " num_omap_bytes = " << stats.stats.sum.num_omap_bytes
<< " num_omap_keys = " << stats.stats.sum.num_omap_keys << dendl;
} else {
stats.stats.sum.num_shallow_scrub_errors = m_shallow_errors;
// XXX: last_clean_scrub_stamp doesn't mean the pg is not inconsistent
// because of deep-scrub errors
if (m_shallow_errors == 0)
history.last_clean_scrub_stamp = now;
}
stats.stats.sum.num_scrub_errors = stats.stats.sum.num_shallow_scrub_errors +
stats.stats.sum.num_deep_scrub_errors;
if (m_flags.check_repair) {
m_flags.check_repair = false;
if (m_pg->info.stats.stats.sum.num_scrub_errors) {
state_set(PG_STATE_FAILED_REPAIR);
dout(10) << "scrub_finish " << m_pg->info.stats.stats.sum.num_scrub_errors
<< " error(s) still present after re-scrub" << dendl;
}
}
return true;
},
&t);
int tr = m_osds->store->queue_transaction(m_pg->ch, std::move(t), nullptr);
ceph_assert(tr == 0);
}
if (has_error) {
m_pg->queue_peering_event(PGPeeringEventRef(std::make_shared<PGPeeringEvent>(
get_osdmap_epoch(), get_osdmap_epoch(), PeeringState::DoRecovery())));
} else {
m_is_repair = false;
state_clear(PG_STATE_REPAIR);
update_op_mode_text();
}
cleanup_on_finish();
if (do_auto_scrub) {
request_rescrubbing(m_pg->m_planned_scrub);
}
if (m_pg->is_active() && m_pg->is_primary()) {
m_pg->recovery_state.share_pg_info();
}
// we may have blocked the snap trimmer
m_pg->snap_trimmer_scrub_complete();
}
void PgScrubber::on_digest_updates()
{
dout(10) << __func__ << " #pending: " << num_digest_updates_pending
<< (m_end.is_max() ? " <last chunk>" : " <mid chunk>")
<< (is_queued_or_active() ? "" : " ** not marked as scrubbing **")
<< dendl;
if (num_digest_updates_pending > 0) {
// do nothing for now. We will be called again when new updates arrive
return;
}
// got all updates, and finished with this chunk. Any more?
if (m_end.is_max()) {
m_osds->queue_scrub_is_finished(m_pg);
} else {
// go get a new chunk (via "requeue")
preemption_data.reset();
m_osds->queue_scrub_next_chunk(m_pg, m_pg->is_scrub_blocking_ops());
}
}
/*
* note that the flags-set fetched from the PG (m_pg->m_planned_scrub)
* is cleared once scrubbing starts; Some of the values dumped here are
* thus transitory.
*/
void PgScrubber::dump(ceph::Formatter* f) const
{
f->open_object_section("scrubber");
f->dump_stream("epoch_start") << m_interval_start;
f->dump_bool("active", m_active);
if (m_active) {
f->dump_stream("start") << m_start;
f->dump_stream("end") << m_end;
f->dump_stream("m_max_end") << m_max_end;
f->dump_stream("subset_last_update") << m_subset_last_update;
f->dump_bool("deep", m_is_deep);
f->dump_bool("must_scrub", (m_pg->m_planned_scrub.must_scrub || m_flags.required));
f->dump_bool("must_deep_scrub", m_pg->m_planned_scrub.must_deep_scrub);
f->dump_bool("must_repair", m_pg->m_planned_scrub.must_repair);
f->dump_bool("need_auto", m_pg->m_planned_scrub.need_auto);
f->dump_bool("req_scrub", m_flags.required);
f->dump_bool("time_for_deep", m_pg->m_planned_scrub.time_for_deep);
f->dump_bool("auto_repair", m_flags.auto_repair);
f->dump_bool("check_repair", m_flags.check_repair);
f->dump_bool("deep_scrub_on_error", m_flags.deep_scrub_on_error);
f->dump_stream("scrub_reg_stamp") << m_scrub_reg_stamp; // utime_t
f->dump_unsigned("priority", m_flags.priority);
f->dump_int("shallow_errors", m_shallow_errors);
f->dump_int("deep_errors", m_deep_errors);
f->dump_int("fixed", m_fixed_count);
{
f->open_array_section("waiting_on_whom");
for (const auto& p : m_maps_status.get_awaited()) {
f->dump_stream("shard") << p;
}
f->close_section();
}
}
f->close_section();
}
void PgScrubber::handle_query_state(ceph::Formatter* f)
{
dout(10) << __func__ << dendl;
f->open_object_section("scrub");
f->dump_stream("scrubber.epoch_start") << m_interval_start;
f->dump_bool("scrubber.active", m_active);
f->dump_stream("scrubber.start") << m_start;
f->dump_stream("scrubber.end") << m_end;
f->dump_stream("scrubber.m_max_end") << m_max_end;
f->dump_stream("scrubber.m_subset_last_update") << m_subset_last_update;
f->dump_bool("scrubber.deep", m_is_deep);
{
f->open_array_section("scrubber.waiting_on_whom");
for (const auto& p : m_maps_status.get_awaited()) {
f->dump_stream("shard") << p;
}
f->close_section();
}
f->dump_string("comment", "DEPRECATED - may be removed in the next release");
f->close_section();
}
PgScrubber::~PgScrubber() = default;
PgScrubber::PgScrubber(PG* pg)
: m_pg{pg}
, m_pg_id{pg->pg_id}
, m_osds{m_pg->osd}
, m_pg_whoami{pg->pg_whoami}
, preemption_data{pg}
{
m_fsm = std::make_unique<ScrubMachine>(m_pg, this);
m_fsm->initiate();
}
void PgScrubber::scrub_begin()
{
stringstream ss;
ss << m_pg->info.pgid.pgid << " " << m_mode_desc << " starts";
dout(2) << ss.str() << dendl;
m_osds->clog->debug(ss);
}
void PgScrubber::reserve_replicas()
{
dout(10) << __func__ << dendl;
m_reservations.emplace(m_pg, m_pg_whoami);
}
void PgScrubber::cleanup_on_finish()
{
dout(10) << __func__ << dendl;
ceph_assert(m_pg->is_locked());
state_clear(PG_STATE_SCRUBBING);
state_clear(PG_STATE_DEEP_SCRUB);
m_pg->publish_stats_to_osd();
clear_scrub_reservations();
m_pg->publish_stats_to_osd();
requeue_waiting();
reset_internal_state();
m_flags = scrub_flags_t{};
// type-specific state clear
_scrub_clear_state();
}
// uses process_event(), so must be invoked externally
void PgScrubber::scrub_clear_state()
{
dout(10) << __func__ << dendl;
clear_pgscrub_state();
m_fsm->process_event(FullReset{});
}
/*
* note: does not access the state-machine
*/
void PgScrubber::clear_pgscrub_state()
{
dout(10) << __func__ << dendl;
ceph_assert(m_pg->is_locked());
state_clear(PG_STATE_SCRUBBING);
state_clear(PG_STATE_DEEP_SCRUB);
state_clear(PG_STATE_REPAIR);
clear_scrub_reservations();
m_pg->publish_stats_to_osd();
requeue_waiting();
reset_internal_state();
m_flags = scrub_flags_t{};
// type-specific state clear
_scrub_clear_state();
}
void PgScrubber::replica_handling_done()
{
dout(10) << __func__ << dendl;
state_clear(PG_STATE_SCRUBBING);
state_clear(PG_STATE_DEEP_SCRUB);
reset_internal_state();
m_pg->publish_stats_to_osd();
}
/*
* note: performs run_callbacks()
* note: reservations-related variables are not reset here
*/
void PgScrubber::reset_internal_state()
{
dout(10) << __func__ << dendl;
preemption_data.reset();
m_maps_status.reset();
m_received_maps.clear();
m_start = hobject_t{};
m_end = hobject_t{};
m_max_end = hobject_t{};
m_subset_last_update = eversion_t{};
m_shallow_errors = 0;
m_deep_errors = 0;
m_fixed_count = 0;
m_omap_stats = (const struct omap_stat_t){0};
run_callbacks();
m_inconsistent.clear();
m_missing.clear();
m_authoritative.clear();
num_digest_updates_pending = 0;
m_primary_scrubmap = ScrubMap{};
m_primary_scrubmap_pos.reset();
replica_scrubmap = ScrubMap{};
replica_scrubmap_pos.reset();
m_cleaned_meta_map = ScrubMap{};
m_needs_sleep = true;
m_sleep_started_at = utime_t{};
m_active = false;
clear_queued_or_active();
}
// note that only applicable to the Replica:
void PgScrubber::advance_token()
{
dout(10) << __func__ << " was: " << m_current_token << dendl;
m_current_token++;
// when advance_token() is called, it is assumed that no scrubbing takes place.
// We will, though, verify that. And if we are actually still handling a stale request -
// both our internal state and the FSM state will be cleared.
replica_handling_done();
m_fsm->process_event(FullReset{});
}
bool PgScrubber::is_token_current(Scrub::act_token_t received_token)
{
if (received_token == 0 || received_token == m_current_token) {
return true;
}
dout(5) << __func__ << " obsolete token (" << received_token
<< " vs current " << m_current_token << dendl;
return false;
}
const OSDMapRef& PgScrubber::get_osdmap() const
{
return m_pg->get_osdmap();
}
ostream& operator<<(ostream& out, const PgScrubber& scrubber)
{
return out << scrubber.m_flags;
}
std::ostream& PgScrubber::gen_prefix(std::ostream& out) const
{
const auto fsm_state = m_fsm ? m_fsm->current_states_desc() : "- :";
if (m_pg) {
return m_pg->gen_prefix(out) << "scrubber " << fsm_state << ": ";
} else {
return out << " scrubber [~] " << fsm_state << ": ";
}
}
ostream& PgScrubber::show(ostream& out) const
{
return out << " [ " << m_pg_id << ": " << m_flags << " ] ";
}
// ///////////////////// preemption_data_t //////////////////////////////////
PgScrubber::preemption_data_t::preemption_data_t(PG* pg) : m_pg{pg}
{
m_left = static_cast<int>(
m_pg->get_cct()->_conf.get_val<uint64_t>("osd_scrub_max_preemptions"));
}
void PgScrubber::preemption_data_t::reset()
{
std::lock_guard<std::mutex> lk{m_preemption_lock};
m_preemptable = false;
m_preempted = false;
m_left =
static_cast<int>(m_pg->cct->_conf.get_val<uint64_t>("osd_scrub_max_preemptions"));
m_size_divisor = 1;
}
// ///////////////////// ReplicaReservations //////////////////////////////////
namespace Scrub {
void ReplicaReservations::release_replica(pg_shard_t peer, epoch_t epoch)
{
auto m = new MOSDScrubReserve(spg_t(m_pg->info.pgid.pgid, peer.shard), epoch,
MOSDScrubReserve::RELEASE, m_pg->pg_whoami);
m_osds->send_message_osd_cluster(peer.osd, m, epoch);
}
ReplicaReservations::ReplicaReservations(PG* pg, pg_shard_t whoami)
: m_pg{pg}
, m_acting_set{pg->get_actingset()}
, m_osds{m_pg->osd}
, m_pending{static_cast<int>(m_acting_set.size()) - 1}
{
epoch_t epoch = m_pg->get_osdmap_epoch();
{
std::stringstream prefix;
prefix << "osd." << m_osds->whoami << " ep: " << epoch
<< " scrubber::ReplicaReservations pg[" << pg->pg_id << "]: ";
m_log_msg_prefix = prefix.str();
}
// handle the special case of no replicas
if (m_pending <= 0) {
// just signal the scrub state-machine to continue
send_all_done();
} else {
for (auto p : m_acting_set) {
if (p == whoami)
continue;
auto m = new MOSDScrubReserve(spg_t(m_pg->info.pgid.pgid, p.shard), epoch,
MOSDScrubReserve::REQUEST, m_pg->pg_whoami);
m_osds->send_message_osd_cluster(p.osd, m, epoch);
m_waited_for_peers.push_back(p);
dout(10) << __func__ << ": reserve " << p.osd << dendl;
}
}
}
void ReplicaReservations::send_all_done()
{
m_osds->queue_for_scrub_granted(m_pg, scrub_prio_t::low_priority);
}
void ReplicaReservations::send_reject()
{
m_osds->queue_for_scrub_denied(m_pg, scrub_prio_t::low_priority);
}
void ReplicaReservations::discard_all()
{
dout(10) << __func__ << ": " << m_reserved_peers << dendl;
m_had_rejections = true; // preventing late-coming responses from triggering events
m_reserved_peers.clear();
m_waited_for_peers.clear();
}
ReplicaReservations::~ReplicaReservations()
{
m_had_rejections = true; // preventing late-coming responses from triggering events
// send un-reserve messages to all reserved replicas. We do not wait for answer (there
// wouldn't be one). Other incoming messages will be discarded on the way, by our
// owner.
epoch_t epoch = m_pg->get_osdmap_epoch();
for (auto& p : m_reserved_peers) {
release_replica(p, epoch);
}
m_reserved_peers.clear();
// note: the release will follow on the heels of the request. When tried otherwise,
// grants that followed a reject arrived after the whole scrub machine-state was
// reset, causing leaked reservations.
for (auto& p : m_waited_for_peers) {
release_replica(p, epoch);
}
m_waited_for_peers.clear();
}
/**
* @ATTN we would not reach here if the ReplicaReservation object managed by the
* scrubber was reset.
*/
void ReplicaReservations::handle_reserve_grant(OpRequestRef op, pg_shard_t from)
{
dout(10) << __func__ << ": granted by " << from << dendl;
op->mark_started();
{
// reduce the amount of extra release messages. Not a must, but the log is cleaner
auto w = find(m_waited_for_peers.begin(), m_waited_for_peers.end(), from);
if (w != m_waited_for_peers.end())
m_waited_for_peers.erase(w);
}
// are we forced to reject the reservation?
if (m_had_rejections) {
dout(10) << __func__ << ": rejecting late-coming reservation from "
<< from << dendl;
release_replica(from, m_pg->get_osdmap_epoch());
} else if (std::find(m_reserved_peers.begin(), m_reserved_peers.end(), from) !=
m_reserved_peers.end()) {
dout(10) << __func__ << ": already had osd." << from << " reserved" << dendl;
} else {
dout(10) << __func__ << ": osd." << from << " scrub reserve = success"
<< dendl;
m_reserved_peers.push_back(from);
if (--m_pending == 0) {
send_all_done();
}
}
}
void ReplicaReservations::handle_reserve_reject(OpRequestRef op, pg_shard_t from)
{
dout(10) << __func__ << ": rejected by " << from << dendl;
dout(15) << __func__ << ": " << *op->get_req() << dendl;
op->mark_started();
{
// reduce the amount of extra release messages. Not a must, but the log is cleaner
auto w = find(m_waited_for_peers.begin(), m_waited_for_peers.end(), from);
if (w != m_waited_for_peers.end())
m_waited_for_peers.erase(w);
}
if (m_had_rejections) {
// our failure was already handled when the first rejection arrived
dout(15) << __func__ << ": ignoring late-coming rejection from "
<< from << dendl;
} else if (std::find(m_reserved_peers.begin(), m_reserved_peers.end(), from) !=
m_reserved_peers.end()) {
dout(10) << __func__ << ": already had osd." << from << " reserved" << dendl;
} else {
dout(10) << __func__ << ": osd." << from << " scrub reserve = fail" << dendl;
m_had_rejections = true; // preventing any additional notifications
send_reject();
}
}
std::ostream& ReplicaReservations::gen_prefix(std::ostream& out) const
{
return out << m_log_msg_prefix;
}
// ///////////////////// LocalReservation //////////////////////////////////
// note: no dout()s in LocalReservation functions. Client logs interactions.
LocalReservation::LocalReservation(OSDService* osds)
: m_osds{osds}
{
if (m_osds->inc_scrubs_local()) {
// the failure is signalled by not having m_holding_local_reservation set
m_holding_local_reservation = true;
}
}
LocalReservation::~LocalReservation()
{
if (m_holding_local_reservation) {
m_holding_local_reservation = false;
m_osds->dec_scrubs_local();
}
}
// ///////////////////// ReservedByRemotePrimary ///////////////////////////////
ReservedByRemotePrimary::ReservedByRemotePrimary(const PgScrubber* scrubber,
PG* pg,
OSDService* osds,
epoch_t epoch)
: m_scrubber{scrubber}
, m_pg{pg}
, m_osds{osds}
, m_reserved_at{epoch}
{
if (!m_osds->inc_scrubs_remote()) {
dout(10) << __func__ << ": failed to reserve at Primary request" << dendl;
// the failure is signalled by not having m_reserved_by_remote_primary set
return;
}
dout(20) << __func__ << ": scrub resources reserved at Primary request" << dendl;
m_reserved_by_remote_primary = true;
}
bool ReservedByRemotePrimary::is_stale() const
{
return m_reserved_at < m_pg->get_same_interval_since();
}
ReservedByRemotePrimary::~ReservedByRemotePrimary()
{
if (m_reserved_by_remote_primary) {
m_reserved_by_remote_primary = false;
m_osds->dec_scrubs_remote();
}
}
std::ostream& ReservedByRemotePrimary::gen_prefix(std::ostream& out) const
{
return m_scrubber->gen_prefix(out);
}
// ///////////////////// MapsCollectionStatus ////////////////////////////////
auto MapsCollectionStatus::mark_arriving_map(pg_shard_t from)
-> std::tuple<bool, std::string_view>
{
auto fe = std::find(m_maps_awaited_for.begin(), m_maps_awaited_for.end(), from);
if (fe != m_maps_awaited_for.end()) {
// we are indeed waiting for a map from this replica
m_maps_awaited_for.erase(fe);
return std::tuple{true, ""sv};
} else {
return std::tuple{false, " unsolicited scrub-map"sv};
}
}
void MapsCollectionStatus::reset()
{
*this = MapsCollectionStatus{};
}
std::string MapsCollectionStatus::dump() const
{
std::string all;
for (const auto& rp : m_maps_awaited_for) {
all.append(rp.get_osd() + " "s);
}
return all;
}
ostream& operator<<(ostream& out, const MapsCollectionStatus& sf)
{
out << " [ ";
for (const auto& rp : sf.m_maps_awaited_for) {
out << rp.get_osd() << " ";
}
if (!sf.m_local_map_ready) {
out << " local ";
}
return out << " ] ";
}
} // namespace Scrub
|