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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <string.h>
#include <iostream>
#include <map>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "common/Formatter.h"
#include <common/errno.h>
#include "include/random.h"
#include "cls/rgw/cls_rgw_client.h"
#include "cls/lock/cls_lock_client.h"
#include "rgw_common.h"
#include "rgw_bucket.h"
#include "rgw_lc.h"
#include "rgw_zone.h"
#include "rgw_string.h"
// this seems safe to use, at least for now--arguably, we should
// prefer header-only fmt, in general
#undef FMT_HEADER_ONLY
#define FMT_HEADER_ONLY 1
#include "seastar/fmt/include/fmt/format.h"
#include "services/svc_sys_obj.h"
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rgw
const char* LC_STATUS[] = {
"UNINITIAL",
"PROCESSING",
"FAILED",
"COMPLETE"
};
using namespace librados;
bool LCRule::valid() const
{
if (id.length() > MAX_ID_LEN) {
return false;
}
else if(expiration.empty() && noncur_expiration.empty() && mp_expiration.empty() && !dm_expiration &&
transitions.empty() && noncur_transitions.empty()) {
return false;
}
else if (!expiration.valid() || !noncur_expiration.valid() || !mp_expiration.valid()) {
return false;
}
if (!transitions.empty()) {
bool using_days = expiration.has_days();
bool using_date = expiration.has_date();
for (const auto& elem : transitions) {
if (!elem.second.valid()) {
return false;
}
using_days = using_days || elem.second.has_days();
using_date = using_date || elem.second.has_date();
if (using_days && using_date) {
return false;
}
}
}
for (const auto& elem : noncur_transitions) {
if (!elem.second.valid()) {
return false;
}
}
return true;
}
void LCRule::init_simple_days_rule(std::string_view _id, std::string_view _prefix, int num_days)
{
id = _id;
prefix = _prefix;
char buf[32];
snprintf(buf, sizeof(buf), "%d", num_days);
expiration.set_days(buf);
set_enabled(true);
}
void RGWLifecycleConfiguration::add_rule(const LCRule& rule)
{
auto& id = rule.get_id(); // note that this will return false for groups, but that's ok, we won't search groups
rule_map.insert(pair<string, LCRule>(id, rule));
}
bool RGWLifecycleConfiguration::_add_rule(const LCRule& rule)
{
lc_op op(rule.get_id());
op.status = rule.is_enabled();
if (rule.get_expiration().has_days()) {
op.expiration = rule.get_expiration().get_days();
}
if (rule.get_expiration().has_date()) {
op.expiration_date = ceph::from_iso_8601(rule.get_expiration().get_date());
}
if (rule.get_noncur_expiration().has_days()) {
op.noncur_expiration = rule.get_noncur_expiration().get_days();
}
if (rule.get_mp_expiration().has_days()) {
op.mp_expiration = rule.get_mp_expiration().get_days();
}
op.dm_expiration = rule.get_dm_expiration();
for (const auto &elem : rule.get_transitions()) {
transition_action action;
if (elem.second.has_days()) {
action.days = elem.second.get_days();
} else {
action.date = ceph::from_iso_8601(elem.second.get_date());
}
action.storage_class = rgw_placement_rule::get_canonical_storage_class(elem.first);
op.transitions.emplace(elem.first, std::move(action));
}
for (const auto &elem : rule.get_noncur_transitions()) {
transition_action action;
action.days = elem.second.get_days();
action.date = ceph::from_iso_8601(elem.second.get_date());
action.storage_class = elem.first;
op.noncur_transitions.emplace(elem.first, std::move(action));
}
std::string prefix;
if (rule.get_filter().has_prefix()){
prefix = rule.get_filter().get_prefix();
} else {
prefix = rule.get_prefix();
}
if (rule.get_filter().has_tags()){
op.obj_tags = rule.get_filter().get_tags();
}
prefix_map.emplace(std::move(prefix), std::move(op));
return true;
}
int RGWLifecycleConfiguration::check_and_add_rule(const LCRule& rule)
{
if (!rule.valid()) {
return -EINVAL;
}
auto& id = rule.get_id();
if (rule_map.find(id) != rule_map.end()) { //id shouldn't be the same
return -EINVAL;
}
rule_map.insert(pair<string, LCRule>(id, rule));
if (!_add_rule(rule)) {
return -ERR_INVALID_REQUEST;
}
return 0;
}
bool RGWLifecycleConfiguration::has_same_action(const lc_op& first, const lc_op& second) {
if ((first.expiration > 0 || first.expiration_date != boost::none) &&
(second.expiration > 0 || second.expiration_date != boost::none)) {
return true;
} else if (first.noncur_expiration > 0 && second.noncur_expiration > 0) {
return true;
} else if (first.mp_expiration > 0 && second.mp_expiration > 0) {
return true;
} else if (!first.transitions.empty() && !second.transitions.empty()) {
for (auto &elem : first.transitions) {
if (second.transitions.find(elem.first) != second.transitions.end()) {
return true;
}
}
} else if (!first.noncur_transitions.empty() && !second.noncur_transitions.empty()) {
for (auto &elem : first.noncur_transitions) {
if (second.noncur_transitions.find(elem.first) != second.noncur_transitions.end()) {
return true;
}
}
}
return false;
}
/* Formerly, this method checked for duplicate rules using an invalid
* method (prefix uniqueness). */
bool RGWLifecycleConfiguration::valid()
{
return true;
}
void *RGWLC::LCWorker::entry() {
do {
utime_t start = ceph_clock_now();
if (should_work(start)) {
ldpp_dout(dpp, 2) << "life cycle: start" << dendl;
int r = lc->process();
if (r < 0) {
ldpp_dout(dpp, 0) << "ERROR: do life cycle process() returned error r=" << r << dendl;
}
ldpp_dout(dpp, 2) << "life cycle: stop" << dendl;
}
if (lc->going_down())
break;
utime_t end = ceph_clock_now();
int secs = schedule_next_start_time(start, end);
utime_t next;
next.set_from_double(end + secs);
ldpp_dout(dpp, 5) << "schedule life cycle next start time: " << rgw_to_asctime(next) << dendl;
lock.Lock();
cond.WaitInterval(lock, utime_t(secs, 0));
lock.Unlock();
} while (!lc->going_down());
return NULL;
}
void RGWLC::initialize(CephContext *_cct, RGWRados *_store) {
cct = _cct;
store = _store;
max_objs = cct->_conf->rgw_lc_max_objs;
if (max_objs > HASH_PRIME)
max_objs = HASH_PRIME;
obj_names = new string[max_objs];
for (int i = 0; i < max_objs; i++) {
obj_names[i] = lc_oid_prefix;
char buf[32];
snprintf(buf, 32, ".%d", i);
obj_names[i].append(buf);
}
#define COOKIE_LEN 16
char cookie_buf[COOKIE_LEN + 1];
gen_rand_alphanumeric(cct, cookie_buf, sizeof(cookie_buf) - 1);
cookie = cookie_buf;
}
void RGWLC::finalize()
{
delete[] obj_names;
}
bool RGWLC::if_already_run_today(time_t& start_date)
{
struct tm bdt;
time_t begin_of_day;
utime_t now = ceph_clock_now();
localtime_r(&start_date, &bdt);
if (cct->_conf->rgw_lc_debug_interval > 0) {
if (now - start_date < cct->_conf->rgw_lc_debug_interval)
return true;
else
return false;
}
bdt.tm_hour = 0;
bdt.tm_min = 0;
bdt.tm_sec = 0;
begin_of_day = mktime(&bdt);
if (now - begin_of_day < 24*60*60)
return true;
else
return false;
}
int RGWLC::bucket_lc_prepare(int index)
{
map<string, int > entries;
string marker;
#define MAX_LC_LIST_ENTRIES 100
do {
int ret = cls_rgw_lc_list(store->lc_pool_ctx, obj_names[index], marker, MAX_LC_LIST_ENTRIES, entries);
if (ret < 0)
return ret;
map<string, int>::iterator iter;
for (iter = entries.begin(); iter != entries.end(); ++iter) {
pair<string, int > entry(iter->first, lc_uninitial);
ret = cls_rgw_lc_set_entry(store->lc_pool_ctx, obj_names[index], entry);
if (ret < 0) {
ldpp_dout(this, 0) << "RGWLC::bucket_lc_prepare() failed to set entry on "
<< obj_names[index] << dendl;
return ret;
}
}
if (!entries.empty()) {
marker = std::move(entries.rbegin()->first);
}
} while (!entries.empty());
return 0;
}
static bool obj_has_expired(CephContext *cct, ceph::real_time mtime, int days, ceph::real_time *expire_time = nullptr)
{
double timediff, cmp;
utime_t base_time;
if (cct->_conf->rgw_lc_debug_interval <= 0) {
/* Normal case, run properly */
cmp = double(days)*24*60*60;
base_time = ceph_clock_now().round_to_day();
} else {
/* We're in debug mode; Treat each rgw_lc_debug_interval seconds as a day */
cmp = double(days)*cct->_conf->rgw_lc_debug_interval;
base_time = ceph_clock_now();
}
timediff = base_time - ceph::real_clock::to_time_t(mtime);
if (expire_time) {
*expire_time = mtime + make_timespan(cmp);
}
ldout(cct, 20) << __func__ << "(): mtime=" << mtime << " days=" << days << " base_time=" << base_time << " timediff=" << timediff << " cmp=" << cmp << dendl;
return (timediff >= cmp);
}
static bool pass_object_lock_check(RGWRados *store, RGWBucketInfo& bucket_info, rgw_obj& obj, RGWObjectCtx& ctx)
{
if (!bucket_info.obj_lock_enabled()) {
return true;
}
RGWRados::Object op_target(store, bucket_info, ctx, obj);
RGWRados::Object::Read read_op(&op_target);
map<string, bufferlist> attrs;
read_op.params.attrs = &attrs;
int ret = read_op.prepare();
if (ret < 0) {
if (ret == -ENOENT) {
return true;
} else {
return false;
}
} else {
auto iter = attrs.find(RGW_ATTR_OBJECT_RETENTION);
if (iter != attrs.end()) {
RGWObjectRetention retention;
try {
decode(retention, iter->second);
} catch (buffer::error& err) {
ldout(store->ctx(), 0) << "ERROR: failed to decode RGWObjectRetention" << dendl;
return false;
}
if (ceph::real_clock::to_time_t(retention.get_retain_until_date()) > ceph_clock_now()) {
return false;
}
}
iter = attrs.find(RGW_ATTR_OBJECT_LEGAL_HOLD);
if (iter != attrs.end()) {
RGWObjectLegalHold obj_legal_hold;
try {
decode(obj_legal_hold, iter->second);
} catch (buffer::error& err) {
ldout(store->ctx(), 0) << "ERROR: failed to decode RGWObjectLegalHold" << dendl;
return false;
}
if (obj_legal_hold.is_enabled()) {
return false;
}
}
return true;
}
}
int RGWLC::handle_multipart_expiration(
RGWRados::Bucket *target, const multimap<string, lc_op>& prefix_map)
{
MultipartMetaFilter mp_filter;
vector<rgw_bucket_dir_entry> objs;
RGWMPObj mp_obj;
bool is_truncated;
int ret;
RGWBucketInfo& bucket_info = target->get_bucket_info();
RGWRados::Bucket::List list_op(target);
auto delay_ms = cct->_conf.get_val<int64_t>("rgw_lc_thread_delay");
list_op.params.list_versions = false;
/* lifecycle processing does not depend on total order, so can
* take advantage of unorderd listing optimizations--such as
* operating on one shard at a time */
list_op.params.allow_unordered = true;
list_op.params.ns = RGW_OBJ_NS_MULTIPART;
list_op.params.filter = &mp_filter;
for (auto prefix_iter = prefix_map.begin(); prefix_iter != prefix_map.end(); ++prefix_iter) {
if (!prefix_iter->second.status || prefix_iter->second.mp_expiration <= 0) {
continue;
}
list_op.params.prefix = prefix_iter->first;
do {
objs.clear();
list_op.params.marker = list_op.get_next_marker();
ret = list_op.list_objects(1000, &objs, NULL, &is_truncated);
if (ret < 0) {
if (ret == (-ENOENT))
return 0;
ldpp_dout(this, 0) << "ERROR: store->list_objects():" <<dendl;
return ret;
}
for (auto obj_iter = objs.begin(); obj_iter != objs.end(); ++obj_iter) {
if (obj_has_expired(cct, obj_iter->meta.mtime, prefix_iter->second.mp_expiration)) {
rgw_obj_key key(obj_iter->key);
if (!mp_obj.from_meta(key.name)) {
continue;
}
RGWObjectCtx rctx(store);
ret = abort_multipart_upload(store, cct, &rctx, bucket_info, mp_obj);
if (ret < 0 && ret != -ERR_NO_SUCH_UPLOAD) {
ldpp_dout(this, 0) << "ERROR: abort_multipart_upload failed, ret=" << ret << ", meta:" << obj_iter->key << dendl;
} else if (ret == -ERR_NO_SUCH_UPLOAD) {
ldpp_dout(this, 5) << "ERROR: abort_multipart_upload failed, ret=" << ret << ", meta:" << obj_iter->key << dendl;
}
if (going_down())
return 0;
}
} /* for objs */
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
} while(is_truncated);
}
return 0;
}
static int read_obj_tags(RGWRados *store, RGWBucketInfo& bucket_info, rgw_obj& obj, RGWObjectCtx& ctx, bufferlist& tags_bl)
{
RGWRados::Object op_target(store, bucket_info, ctx, obj);
RGWRados::Object::Read read_op(&op_target);
return read_op.get_attr(RGW_ATTR_TAGS, tags_bl);
}
static bool is_valid_op(const lc_op& op)
{
return (op.status &&
(op.expiration > 0
|| op.expiration_date != boost::none
|| op.noncur_expiration > 0
|| op.dm_expiration
|| !op.transitions.empty()
|| !op.noncur_transitions.empty()));
}
static inline bool has_all_tags(const lc_op& rule_action,
const RGWObjTags& object_tags)
{
if(! rule_action.obj_tags)
return false;
if(object_tags.count() < rule_action.obj_tags->count())
return false;
size_t tag_count = 0;
for (const auto& tag : object_tags.get_tags()) {
const auto& rule_tags = rule_action.obj_tags->get_tags();
const auto& iter = rule_tags.find(tag.first);
if(iter == rule_tags.end())
continue;
if(iter->second == tag.second)
{
tag_count++;
}
/* all tags in the rule appear in obj tags */
}
return tag_count == rule_action.obj_tags->count();
}
class LCObjsLister {
RGWRados *store;
RGWBucketInfo& bucket_info;
RGWRados::Bucket target;
RGWRados::Bucket::List list_op;
bool is_truncated{false};
rgw_obj_key next_marker;
string prefix;
vector<rgw_bucket_dir_entry> objs;
vector<rgw_bucket_dir_entry>::iterator obj_iter;
rgw_bucket_dir_entry pre_obj;
int64_t delay_ms;
public:
LCObjsLister(RGWRados *_store, RGWBucketInfo& _bucket_info) :
store(_store), bucket_info(_bucket_info),
target(store, bucket_info), list_op(&target) {
list_op.params.list_versions = bucket_info.versioned();
list_op.params.allow_unordered = true;
delay_ms = store->ctx()->_conf.get_val<int64_t>("rgw_lc_thread_delay");
}
void set_prefix(const string& p) {
prefix = p;
list_op.params.prefix = prefix;
}
int init() {
return fetch();
}
int fetch() {
int ret = list_op.list_objects(1000, &objs, NULL, &is_truncated);
if (ret < 0) {
return ret;
}
obj_iter = objs.begin();
return 0;
}
void delay() {
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
}
bool get_obj(rgw_bucket_dir_entry *obj) {
if (obj_iter == objs.end()) {
delay();
return false;
}
if (is_truncated && (obj_iter + 1)==objs.end()) {
list_op.params.marker = obj_iter->key;
int ret = fetch();
if (ret < 0) {
ldout(store->ctx(), 0) << "ERROR: list_op returned ret=" << ret << dendl;
return ret;
} else {
obj_iter = objs.begin();
}
delay();
}
*obj = *obj_iter;
return true;
}
rgw_bucket_dir_entry get_prev_obj() {
return pre_obj;
}
void next() {
pre_obj = *obj_iter;
++obj_iter;
}
bool next_has_same_name()
{
if ((obj_iter + 1) == objs.end()) {
/* this should have been called after get_obj() was called, so this should
* only happen if is_truncated is false */
return false;
}
return (obj_iter->key.name.compare((obj_iter + 1)->key.name) == 0);
}
};
struct op_env {
lc_op& op;
RGWRados *store;
RGWLC *lc;
RGWBucketInfo& bucket_info;
LCObjsLister& ol;
op_env(lc_op& _op, RGWRados *_store, RGWLC *_lc, RGWBucketInfo& _bucket_info,
LCObjsLister& _ol) : op(_op), store(_store), lc(_lc), bucket_info(_bucket_info), ol(_ol) {}
};
class LCRuleOp;
struct lc_op_ctx {
CephContext *cct;
op_env& env;
rgw_bucket_dir_entry& o;
RGWRados *store;
RGWBucketInfo& bucket_info;
lc_op& op;
LCObjsLister& ol;
rgw_obj obj;
RGWObjectCtx rctx;
lc_op_ctx(op_env& _env, rgw_bucket_dir_entry& _o) : cct(_env.store->ctx()), env(_env), o(_o),
store(env.store), bucket_info(env.bucket_info), op(env.op), ol(env.ol),
obj(env.bucket_info.bucket, o.key), rctx(env.store) {}
};
static int remove_expired_obj(lc_op_ctx& oc, bool remove_indeed)
{
auto& store = oc.store;
auto& bucket_info = oc.bucket_info;
auto& o = oc.o;
auto obj_key = o.key;
auto& meta = o.meta;
if (!remove_indeed) {
obj_key.instance.clear();
} else if (obj_key.instance.empty()) {
obj_key.instance = "null";
}
rgw_obj obj(bucket_info.bucket, obj_key);
ACLOwner obj_owner;
obj_owner.set_id(rgw_user {meta.owner});
obj_owner.set_name(meta.owner_display_name);
RGWRados::Object del_target(store, bucket_info, oc.rctx, obj);
RGWRados::Object::Delete del_op(&del_target);
del_op.params.bucket_owner = bucket_info.owner;
del_op.params.versioning_status = bucket_info.versioning_status();
del_op.params.obj_owner = obj_owner;
del_op.params.unmod_since = meta.mtime;
return del_op.delete_obj();
}
class LCOpAction {
public:
virtual ~LCOpAction() {}
virtual bool check(lc_op_ctx& oc, ceph::real_time *exp_time) {
return false;
};
/* called after check(). Check should tell us whether this action
* is applicable. If there are multiple actions, we'll end up executing
* the latest applicable action
* For example:
* one action after 10 days, another after 20, third after 40.
* After 10 days, the latest applicable action would be the first one,
* after 20 days it will be the second one. After 21 days it will still be the
* second one. So check() should return true for the second action at that point,
* but should_process() if the action has already been applied. In object removal
* it doesn't matter, but in object transition it does.
*/
virtual bool should_process() {
return true;
}
virtual int process(lc_op_ctx& oc) {
return 0;
}
};
class LCOpFilter {
public:
virtual ~LCOpFilter() {}
virtual bool check(lc_op_ctx& oc) {
return false;
}
};
class LCOpRule {
friend class LCOpAction;
op_env& env;
std::vector<unique_ptr<LCOpFilter> > filters;
std::vector<unique_ptr<LCOpAction> > actions;
public:
LCOpRule(op_env& _env) : env(_env) {}
void build();
int process(rgw_bucket_dir_entry& o);
};
static int check_tags(lc_op_ctx& oc, bool *skip)
{
auto& op = oc.op;
if (op.obj_tags != boost::none) {
*skip = true;
bufferlist tags_bl;
int ret = read_obj_tags(oc.store, oc.bucket_info, oc.obj, oc.rctx, tags_bl);
if (ret < 0) {
if (ret != -ENODATA) {
ldout(oc.cct, 5) << "ERROR: read_obj_tags returned r=" << ret << dendl;
}
return 0;
}
RGWObjTags dest_obj_tags;
try {
auto iter = tags_bl.cbegin();
dest_obj_tags.decode(iter);
} catch (buffer::error& err) {
ldout(oc.cct,0) << "ERROR: caught buffer::error, couldn't decode TagSet" << dendl;
return -EIO;
}
if (! has_all_tags(op, dest_obj_tags)) {
ldout(oc.cct, 20) << __func__ << "() skipping obj " << oc.obj << " as tags do not match in rule: " << op.id << dendl;
return 0;
}
}
*skip = false;
return 0;
}
class LCOpFilter_Tags : public LCOpFilter {
public:
bool check(lc_op_ctx& oc) override {
auto& o = oc.o;
if (o.is_delete_marker()) {
return true;
}
bool skip;
int ret = check_tags(oc, &skip);
if (ret < 0) {
if (ret == -ENOENT) {
return false;
}
ldout(oc.cct, 0) << "ERROR: check_tags on obj=" << oc.obj << " returned ret=" << ret << dendl;
return false;
}
return !skip;
};
};
class LCOpAction_CurrentExpiration : public LCOpAction {
public:
bool check(lc_op_ctx& oc, ceph::real_time *exp_time) override {
auto& o = oc.o;
if (!o.is_current()) {
ldout(oc.cct, 20) << __func__ << "(): key=" << o.key << ": not current, skipping" << dendl;
return false;
}
if (o.is_delete_marker()) {
if (oc.ol.next_has_same_name()) {
return false;
} else {
*exp_time = real_clock::now();
return true;
}
}
auto& mtime = o.meta.mtime;
bool is_expired;
auto& op = oc.op;
if (op.expiration <= 0) {
if (op.expiration_date == boost::none) {
ldout(oc.cct, 20) << __func__ << "(): key=" << o.key << ": no expiration set in rule, skipping" << dendl;
return false;
}
is_expired = ceph_clock_now() >= ceph::real_clock::to_time_t(*op.expiration_date);
*exp_time = *op.expiration_date;
} else {
is_expired = obj_has_expired(oc.cct, mtime, op.expiration, exp_time);
}
ldout(oc.cct, 20) << __func__ << "(): key=" << o.key << ": is_expired=" << (int)is_expired << dendl;
return is_expired;
}
int process(lc_op_ctx& oc) {
auto& o = oc.o;
int r;
if (o.is_delete_marker()) {
r = remove_expired_obj(oc, true);
} else {
r = remove_expired_obj(oc, !oc.bucket_info.versioned());
}
if (r < 0) {
ldout(oc.cct, 0) << "ERROR: remove_expired_obj " << dendl;
return r;
}
ldout(oc.cct, 2) << "DELETED:" << oc.bucket_info.bucket << ":" << o.key << dendl;
return 0;
}
};
class LCOpAction_NonCurrentExpiration : public LCOpAction {
public:
bool check(lc_op_ctx& oc, ceph::real_time *exp_time) override {
auto& o = oc.o;
if (o.is_current()) {
ldout(oc.cct, 20) << __func__ << "(): key=" << o.key << ": current version, skipping" << dendl;
return false;
}
auto mtime = oc.ol.get_prev_obj().meta.mtime;
int expiration = oc.op.noncur_expiration;
bool is_expired = obj_has_expired(oc.cct, mtime, expiration, exp_time);
ldout(oc.cct, 20) << __func__ << "(): key=" << o.key << ": is_expired=" << is_expired << dendl;
return is_expired && pass_object_lock_check(oc.store, oc.bucket_info, oc.obj, oc.rctx);
}
int process(lc_op_ctx& oc) {
auto& o = oc.o;
int r = remove_expired_obj(oc, true);
if (r < 0) {
ldout(oc.cct, 0) << "ERROR: remove_expired_obj " << dendl;
return r;
}
ldout(oc.cct, 2) << "DELETED:" << oc.bucket_info.bucket << ":" << o.key << " (non-current expiration)" << dendl;
return 0;
}
};
class LCOpAction_DMExpiration : public LCOpAction {
public:
bool check(lc_op_ctx& oc, ceph::real_time *exp_time) override {
auto& o = oc.o;
if (!o.is_delete_marker()) {
ldout(oc.cct, 20) << __func__ << "(): key=" << o.key << ": not a delete marker, skipping" << dendl;
return false;
}
if (oc.ol.next_has_same_name()) {
ldout(oc.cct, 20) << __func__ << "(): key=" << o.key << ": next is same object, skipping" << dendl;
return false;
}
*exp_time = real_clock::now();
return true;
}
int process(lc_op_ctx& oc) {
auto& o = oc.o;
int r = remove_expired_obj(oc, true);
if (r < 0) {
ldout(oc.cct, 0) << "ERROR: remove_expired_obj " << dendl;
return r;
}
ldout(oc.cct, 2) << "DELETED:" << oc.bucket_info.bucket << ":" << o.key << " (delete marker expiration)" << dendl;
return 0;
}
};
class LCOpAction_Transition : public LCOpAction {
const transition_action& transition;
bool need_to_process{false};
protected:
virtual bool check_current_state(bool is_current) = 0;
virtual ceph::real_time get_effective_mtime(lc_op_ctx& oc) = 0;
public:
LCOpAction_Transition(const transition_action& _transition) : transition(_transition) {}
bool check(lc_op_ctx& oc, ceph::real_time *exp_time) override {
auto& o = oc.o;
if (o.is_delete_marker()) {
return false;
}
if (!check_current_state(o.is_current())) {
return false;
}
auto mtime = get_effective_mtime(oc);
bool is_expired;
if (transition.days < 0) {
if (transition.date == boost::none) {
ldout(oc.cct, 20) << __func__ << "(): key=" << o.key << ": no transition day/date set in rule, skipping" << dendl;
return false;
}
is_expired = ceph_clock_now() >= ceph::real_clock::to_time_t(*transition.date);
*exp_time = *transition.date;
} else {
is_expired = obj_has_expired(oc.cct, mtime, transition.days, exp_time);
}
ldout(oc.cct, 20) << __func__ << "(): key=" << o.key << ": is_expired=" << is_expired << dendl;
need_to_process = (rgw_placement_rule::get_canonical_storage_class(o.meta.storage_class) != transition.storage_class);
return is_expired;
}
bool should_process() override {
return need_to_process;
}
int process(lc_op_ctx& oc) {
auto& o = oc.o;
rgw_placement_rule target_placement;
target_placement.inherit_from(oc.bucket_info.placement_rule);
target_placement.storage_class = transition.storage_class;
if (!oc.store->svc.zone->get_zone_params().valid_placement(target_placement)) {
ldout(oc.cct, 0) << "ERROR: non existent dest placement: " << target_placement
<< " bucket="<< oc.bucket_info.bucket
<< " rule_id=" << oc.op.id << dendl;
return -EINVAL;
}
int r = oc.store->transition_obj(oc.rctx, oc.bucket_info, oc.obj,
target_placement, o.meta.mtime, o.versioned_epoch);
if (r < 0) {
ldout(oc.cct, 0) << "ERROR: failed to transition obj (r=" << r << ")" << dendl;
return r;
}
ldout(oc.cct, 2) << "TRANSITIONED:" << oc.bucket_info.bucket << ":" << o.key << " -> " << transition.storage_class << dendl;
return 0;
}
};
class LCOpAction_CurrentTransition : public LCOpAction_Transition {
protected:
bool check_current_state(bool is_current) override {
return is_current;
}
ceph::real_time get_effective_mtime(lc_op_ctx& oc) override {
return oc.o.meta.mtime;
}
public:
LCOpAction_CurrentTransition(const transition_action& _transition) : LCOpAction_Transition(_transition) {}
};
class LCOpAction_NonCurrentTransition : public LCOpAction_Transition {
protected:
bool check_current_state(bool is_current) override {
return !is_current;
}
ceph::real_time get_effective_mtime(lc_op_ctx& oc) override {
return oc.ol.get_prev_obj().meta.mtime;
}
public:
LCOpAction_NonCurrentTransition(const transition_action& _transition) : LCOpAction_Transition(_transition) {}
};
void LCOpRule::build()
{
filters.emplace_back(new LCOpFilter_Tags);
auto& op = env.op;
if (op.expiration > 0 ||
op.expiration_date != boost::none) {
actions.emplace_back(new LCOpAction_CurrentExpiration);
}
if (op.dm_expiration) {
actions.emplace_back(new LCOpAction_DMExpiration);
}
if (op.noncur_expiration > 0) {
actions.emplace_back(new LCOpAction_NonCurrentExpiration);
}
for (auto& iter : op.transitions) {
actions.emplace_back(new LCOpAction_CurrentTransition(iter.second));
}
for (auto& iter : op.noncur_transitions) {
actions.emplace_back(new LCOpAction_NonCurrentTransition(iter.second));
}
}
int LCOpRule::process(rgw_bucket_dir_entry& o)
{
lc_op_ctx ctx(env, o);
unique_ptr<LCOpAction> *selected = nullptr;
real_time exp;
for (auto& a : actions) {
real_time action_exp;
if (a->check(ctx, &action_exp)) {
if (action_exp > exp) {
exp = action_exp;
selected = &a;
}
}
}
if (selected &&
(*selected)->should_process()) {
/*
* Calling filter checks after action checks because
* all action checks (as they are implemented now) do
* not access the objects themselves, but return result
* from info from bucket index listing. The current tags filter
* check does access the objects, so we avoid unnecessary rados calls
* having filters check later in the process.
*/
bool cont = false;
for (auto& f : filters) {
if (f->check(ctx)) {
cont = true;
break;
}
}
if (!cont) {
ldout(env.store->ctx(), 20) << __func__ << "(): key=" << o.key << ": no rule match, skipping" << dendl;
return 0;
}
int r = (*selected)->process(ctx);
if (r < 0) {
ldout(ctx.cct, 0) << "ERROR: remove_expired_obj " << dendl;
return r;
}
ldout(ctx.cct, 20) << "processed:" << env.bucket_info.bucket << ":" << o.key << dendl;
}
return 0;
}
int RGWLC::bucket_lc_process(string& shard_id)
{
RGWLifecycleConfiguration config(cct);
RGWBucketInfo bucket_info;
map<string, bufferlist> bucket_attrs;
string no_ns, list_versions;
vector<rgw_bucket_dir_entry> objs;
auto obj_ctx = store->svc.sysobj->init_obj_ctx();
vector<std::string> result;
boost::split(result, shard_id, boost::is_any_of(":"));
string bucket_tenant = result[0];
string bucket_name = result[1];
string bucket_marker = result[2];
int ret = store->get_bucket_info(obj_ctx, bucket_tenant, bucket_name, bucket_info, NULL, &bucket_attrs);
if (ret < 0) {
ldpp_dout(this, 0) << "LC:get_bucket_info for " << bucket_name << " failed" << dendl;
return ret;
}
if (bucket_info.bucket.marker != bucket_marker) {
ldpp_dout(this, 1) << "LC: deleting stale entry found for bucket=" << bucket_tenant
<< ":" << bucket_name << " cur_marker=" << bucket_info.bucket.marker
<< " orig_marker=" << bucket_marker << dendl;
return -ENOENT;
}
RGWRados::Bucket target(store, bucket_info);
map<string, bufferlist>::iterator aiter = bucket_attrs.find(RGW_ATTR_LC);
if (aiter == bucket_attrs.end())
return 0;
bufferlist::const_iterator iter{&aiter->second};
try {
config.decode(iter);
} catch (const buffer::error& e) {
ldpp_dout(this, 0) << __func__ << "() decode life cycle config failed" << dendl;
return -1;
}
multimap<string, lc_op>& prefix_map = config.get_prefix_map();
ldpp_dout(this, 10) << __func__ << "() prefix_map size="
<< prefix_map.size()
<< dendl;
rgw_obj_key pre_marker;
rgw_obj_key next_marker;
for(auto prefix_iter = prefix_map.begin(); prefix_iter != prefix_map.end(); ++prefix_iter) {
auto& op = prefix_iter->second;
if (!is_valid_op(op)) {
continue;
}
ldpp_dout(this, 20) << __func__ << "(): prefix=" << prefix_iter->first << dendl;
if (prefix_iter != prefix_map.begin() &&
(prefix_iter->first.compare(0, prev(prefix_iter)->first.length(), prev(prefix_iter)->first) == 0)) {
next_marker = pre_marker;
} else {
pre_marker = next_marker;
}
LCObjsLister ol(store, bucket_info);
ol.set_prefix(prefix_iter->first);
ret = ol.init();
if (ret < 0) {
if (ret == (-ENOENT))
return 0;
ldpp_dout(this, 0) << "ERROR: store->list_objects():" <<dendl;
return ret;
}
op_env oenv(op, store, this, bucket_info, ol);
LCOpRule orule(oenv);
orule.build();
ceph::real_time mtime;
rgw_bucket_dir_entry o;
for (; ol.get_obj(&o); ol.next()) {
ldpp_dout(this, 20) << __func__ << "(): key=" << o.key << dendl;
int ret = orule.process(o);
if (ret < 0) {
ldpp_dout(this, 20) << "ERROR: orule.process() returned ret="
<< ret
<< dendl;
}
if (going_down()) {
return 0;
}
}
}
ret = handle_multipart_expiration(&target, prefix_map);
return ret;
}
int RGWLC::bucket_lc_post(int index, int max_lock_sec, pair<string, int >& entry, int& result)
{
utime_t lock_duration(cct->_conf->rgw_lc_lock_max_time, 0);
rados::cls::lock::Lock l(lc_index_lock_name);
l.set_cookie(cookie);
l.set_duration(lock_duration);
do {
int ret = l.lock_exclusive(&store->lc_pool_ctx, obj_names[index]);
if (ret == -EBUSY || ret == -EEXIST) { /* already locked by another lc processor */
ldpp_dout(this, 0) << "RGWLC::bucket_lc_post() failed to acquire lock on "
<< obj_names[index] << ", sleep 5, try again" << dendl;
sleep(5);
continue;
}
if (ret < 0)
return 0;
ldpp_dout(this, 20) << "RGWLC::bucket_lc_post() lock " << obj_names[index] << dendl;
if (result == -ENOENT) {
ret = cls_rgw_lc_rm_entry(store->lc_pool_ctx, obj_names[index], entry);
if (ret < 0) {
ldpp_dout(this, 0) << "RGWLC::bucket_lc_post() failed to remove entry "
<< obj_names[index] << dendl;
}
goto clean;
} else if (result < 0) {
entry.second = lc_failed;
} else {
entry.second = lc_complete;
}
ret = cls_rgw_lc_set_entry(store->lc_pool_ctx, obj_names[index], entry);
if (ret < 0) {
ldpp_dout(this, 0) << "RGWLC::process() failed to set entry on "
<< obj_names[index] << dendl;
}
clean:
l.unlock(&store->lc_pool_ctx, obj_names[index]);
ldpp_dout(this, 20) << "RGWLC::bucket_lc_post() unlock " << obj_names[index] << dendl;
return 0;
} while (true);
}
int RGWLC::list_lc_progress(const string& marker, uint32_t max_entries, map<string, int> *progress_map)
{
int index = 0;
progress_map->clear();
for(; index <max_objs; index++) {
map<string, int > entries;
int ret = cls_rgw_lc_list(store->lc_pool_ctx, obj_names[index], marker, max_entries, entries);
if (ret < 0) {
if (ret == -ENOENT) {
ldpp_dout(this, 10) << __func__ << "() ignoring unfound lc object="
<< obj_names[index] << dendl;
continue;
} else {
return ret;
}
}
map<string, int>::iterator iter;
for (iter = entries.begin(); iter != entries.end(); ++iter) {
progress_map->insert(*iter);
}
}
return 0;
}
int RGWLC::process()
{
int max_secs = cct->_conf->rgw_lc_lock_max_time;
const int start = ceph::util::generate_random_number(0, max_objs - 1);
for (int i = 0; i < max_objs; i++) {
int index = (i + start) % max_objs;
int ret = process(index, max_secs);
if (ret < 0)
return ret;
}
return 0;
}
int RGWLC::process(int index, int max_lock_secs)
{
rados::cls::lock::Lock l(lc_index_lock_name);
do {
utime_t now = ceph_clock_now();
pair<string, int > entry;//string = bucket_name:bucket_id ,int = LC_BUCKET_STATUS
if (max_lock_secs <= 0)
return -EAGAIN;
utime_t time(max_lock_secs, 0);
l.set_duration(time);
int ret = l.lock_exclusive(&store->lc_pool_ctx, obj_names[index]);
if (ret == -EBUSY || ret == -EEXIST) { /* already locked by another lc processor */
ldpp_dout(this, 0) << "RGWLC::process() failed to acquire lock on "
<< obj_names[index] << ", sleep 5, try again" << dendl;
sleep(5);
continue;
}
if (ret < 0)
return 0;
cls_rgw_lc_obj_head head;
ret = cls_rgw_lc_get_head(store->lc_pool_ctx, obj_names[index], head);
if (ret < 0) {
ldpp_dout(this, 0) << "RGWLC::process() failed to get obj head "
<< obj_names[index] << ", ret=" << ret << dendl;
goto exit;
}
if(!if_already_run_today(head.start_date)) {
head.start_date = now;
head.marker.clear();
ret = bucket_lc_prepare(index);
if (ret < 0) {
ldpp_dout(this, 0) << "RGWLC::process() failed to update lc object "
<< obj_names[index] << ", ret=" << ret << dendl;
goto exit;
}
}
ret = cls_rgw_lc_get_next_entry(store->lc_pool_ctx, obj_names[index], head.marker, entry);
if (ret < 0) {
ldpp_dout(this, 0) << "RGWLC::process() failed to get obj entry "
<< obj_names[index] << dendl;
goto exit;
}
if (entry.first.empty())
goto exit;
entry.second = lc_processing;
ret = cls_rgw_lc_set_entry(store->lc_pool_ctx, obj_names[index], entry);
if (ret < 0) {
ldpp_dout(this, 0) << "RGWLC::process() failed to set obj entry " << obj_names[index]
<< " (" << entry.first << "," << entry.second << ")" << dendl;
goto exit;
}
head.marker = entry.first;
ret = cls_rgw_lc_put_head(store->lc_pool_ctx, obj_names[index], head);
if (ret < 0) {
ldpp_dout(this, 0) << "RGWLC::process() failed to put head " << obj_names[index] << dendl;
goto exit;
}
l.unlock(&store->lc_pool_ctx, obj_names[index]);
ret = bucket_lc_process(entry.first);
bucket_lc_post(index, max_lock_secs, entry, ret);
}while(1);
exit:
l.unlock(&store->lc_pool_ctx, obj_names[index]);
return 0;
}
void RGWLC::start_processor()
{
worker = new LCWorker(this, cct, this);
worker->create("lifecycle_thr");
}
void RGWLC::stop_processor()
{
down_flag = true;
if (worker) {
worker->stop();
worker->join();
}
delete worker;
worker = NULL;
}
unsigned RGWLC::get_subsys() const
{
return dout_subsys;
}
std::ostream& RGWLC::gen_prefix(std::ostream& out) const
{
return out << "lifecycle: ";
}
void RGWLC::LCWorker::stop()
{
Mutex::Locker l(lock);
cond.Signal();
}
bool RGWLC::going_down()
{
return down_flag;
}
bool RGWLC::LCWorker::should_work(utime_t& now)
{
int start_hour;
int start_minute;
int end_hour;
int end_minute;
string worktime = cct->_conf->rgw_lifecycle_work_time;
sscanf(worktime.c_str(),"%d:%d-%d:%d",&start_hour, &start_minute, &end_hour, &end_minute);
struct tm bdt;
time_t tt = now.sec();
localtime_r(&tt, &bdt);
if (cct->_conf->rgw_lc_debug_interval > 0) {
/* We're debugging, so say we can run */
return true;
} else if ((bdt.tm_hour*60 + bdt.tm_min >= start_hour*60 + start_minute) &&
(bdt.tm_hour*60 + bdt.tm_min <= end_hour*60 + end_minute)) {
return true;
} else {
return false;
}
}
int RGWLC::LCWorker::schedule_next_start_time(utime_t &start, utime_t& now)
{
int secs;
if (cct->_conf->rgw_lc_debug_interval > 0) {
secs = start + cct->_conf->rgw_lc_debug_interval - now;
if (secs < 0)
secs = 0;
return (secs);
}
int start_hour;
int start_minute;
int end_hour;
int end_minute;
string worktime = cct->_conf->rgw_lifecycle_work_time;
sscanf(worktime.c_str(),"%d:%d-%d:%d",&start_hour, &start_minute, &end_hour, &end_minute);
struct tm bdt;
time_t tt = now.sec();
time_t nt;
localtime_r(&tt, &bdt);
bdt.tm_hour = start_hour;
bdt.tm_min = start_minute;
bdt.tm_sec = 0;
nt = mktime(&bdt);
secs = nt - tt;
return secs>0 ? secs : secs+24*60*60;
}
void RGWLifecycleConfiguration::generate_test_instances(list<RGWLifecycleConfiguration*>& o)
{
o.push_back(new RGWLifecycleConfiguration);
}
void get_lc_oid(CephContext *cct, const string& shard_id, string *oid)
{
int max_objs = (cct->_conf->rgw_lc_max_objs > HASH_PRIME ? HASH_PRIME : cct->_conf->rgw_lc_max_objs);
int index = ceph_str_hash_linux(shard_id.c_str(), shard_id.size()) % HASH_PRIME % max_objs;
*oid = lc_oid_prefix;
char buf[32];
snprintf(buf, 32, ".%d", index);
oid->append(buf);
return;
}
static std::string get_lc_shard_name(const rgw_bucket& bucket){
return string_join_reserve(':', bucket.tenant, bucket.name, bucket.marker);
}
template<typename F>
static int guard_lc_modify(RGWRados* store, const rgw_bucket& bucket, const string& cookie, const F& f) {
CephContext *cct = store->ctx();
string shard_id = get_lc_shard_name(bucket);
string oid;
get_lc_oid(cct, shard_id, &oid);
pair<string, int> entry(shard_id, lc_uninitial);
int max_lock_secs = cct->_conf->rgw_lc_lock_max_time;
rados::cls::lock::Lock l(lc_index_lock_name);
utime_t time(max_lock_secs, 0);
l.set_duration(time);
l.set_cookie(cookie);
librados::IoCtx *ctx = store->get_lc_pool_ctx();
int ret;
do {
ret = l.lock_exclusive(ctx, oid);
if (ret == -EBUSY || ret == -EEXIST) {
ldout(cct, 0) << "RGWLC::RGWPutLC() failed to acquire lock on "
<< oid << ", sleep 5, try again" << dendl;
sleep(5); // XXX: return retryable error
continue;
}
if (ret < 0) {
ldout(cct, 0) << "RGWLC::RGWPutLC() failed to acquire lock on "
<< oid << ", ret=" << ret << dendl;
break;
}
ret = f(ctx, oid, entry);
if (ret < 0) {
ldout(cct, 0) << "RGWLC::RGWPutLC() failed to set entry on "
<< oid << ", ret=" << ret << dendl;
}
break;
} while(true);
l.unlock(ctx, oid);
return ret;
}
int RGWLC::set_bucket_config(RGWBucketInfo& bucket_info,
const map<string, bufferlist>& bucket_attrs,
RGWLifecycleConfiguration *config)
{
map<string, bufferlist> attrs = bucket_attrs;
bufferlist lc_bl;
config->encode(lc_bl);
attrs[RGW_ATTR_LC] = std::move(lc_bl);
int ret = rgw_bucket_set_attrs(store, bucket_info, attrs, &bucket_info.objv_tracker);
if (ret < 0)
return ret;
rgw_bucket& bucket = bucket_info.bucket;
ret = guard_lc_modify(store, bucket, cookie, [&](librados::IoCtx *ctx, const string& oid,
const pair<string, int>& entry) {
return cls_rgw_lc_set_entry(*ctx, oid, entry);
});
return ret;
}
int RGWLC::remove_bucket_config(RGWBucketInfo& bucket_info,
const map<string, bufferlist>& bucket_attrs)
{
map<string, bufferlist> attrs = bucket_attrs;
attrs.erase(RGW_ATTR_LC);
int ret = rgw_bucket_set_attrs(store, bucket_info, attrs,
&bucket_info.objv_tracker);
rgw_bucket& bucket = bucket_info.bucket;
if (ret < 0) {
ldout(cct, 0) << "RGWLC::RGWDeleteLC() failed to set attrs on bucket="
<< bucket.name << " returned err=" << ret << dendl;
return ret;
}
ret = guard_lc_modify(store, bucket, cookie, [&](librados::IoCtx *ctx, const string& oid,
const pair<string, int>& entry) {
return cls_rgw_lc_rm_entry(*ctx, oid, entry);
});
return ret;
}
namespace rgw::lc {
int fix_lc_shard_entry(RGWRados* store, const RGWBucketInfo& bucket_info,
const map<std::string,bufferlist>& battrs)
{
if (auto aiter = battrs.find(RGW_ATTR_LC);
aiter == battrs.end()) {
return 0; // No entry, nothing to fix
}
auto shard_name = get_lc_shard_name(bucket_info.bucket);
std::string lc_oid;
get_lc_oid(store->ctx(), shard_name, &lc_oid);
rgw_lc_entry_t entry;
// There are multiple cases we need to encounter here
// 1. entry exists and is already set to marker, happens in plain buckets & newly resharded buckets
// 2. entry doesn't exist, which usually happens when reshard has happened prior to update and next LC process has already dropped the update
// 3. entry exists matching the current bucket id which was after a reshard (needs to be updated to the marker)
// We are not dropping the old marker here as that would be caught by the next LC process update
auto lc_pool_ctx = store->get_lc_pool_ctx();
int ret = cls_rgw_lc_get_entry(*lc_pool_ctx,
lc_oid, shard_name, entry);
if (ret == 0) {
ldout(store->ctx(), 5) << "Entry already exists, nothing to do" << dendl;
return ret; // entry is already existing correctly set to marker
}
ldout(store->ctx(), 5) << "cls_rgw_lc_get_entry errored ret code=" << ret << dendl;
if (ret == -ENOENT) {
ldout(store->ctx(), 1) << "No entry for bucket=" << bucket_info.bucket.name
<< " creating " << dendl;
// TODO: we have too many ppl making cookies like this!
char cookie_buf[COOKIE_LEN + 1];
gen_rand_alphanumeric(store->ctx(), cookie_buf, sizeof(cookie_buf) - 1);
std::string cookie = cookie_buf;
ret = guard_lc_modify(store, bucket_info.bucket, cookie,
[&lc_pool_ctx, &lc_oid](librados::IoCtx *ctx, const string& oid,
const pair<string, int>& entry) {
return cls_rgw_lc_set_entry(*lc_pool_ctx,
lc_oid, entry);
});
}
return ret;
}
std::string s3_expiration_header(
DoutPrefixProvider* dpp,
const rgw_obj_key& obj_key,
const RGWObjTags& obj_tagset,
const ceph::real_time& mtime,
const std::map<std::string, buffer::list>& bucket_attrs)
{
CephContext* cct = dpp->get_cct();
RGWLifecycleConfiguration config(cct);
std::string hdr{""};
const auto& aiter = bucket_attrs.find(RGW_ATTR_LC);
if (aiter == bucket_attrs.end())
return hdr;
bufferlist::const_iterator iter{&aiter->second};
try {
config.decode(iter);
} catch (const buffer::error& e) {
ldpp_dout(dpp, 0) << __func__
<< "() decode life cycle config failed"
<< dendl;
return hdr;
} /* catch */
/* dump tags at debug level 16 */
RGWObjTags::tag_map_t obj_tag_map = obj_tagset.get_tags();
if (cct->_conf->subsys.should_gather(ceph_subsys_rgw, 16)) {
for (const auto& elt : obj_tag_map) {
ldout(cct, 16) << __func__
<< "() key=" << elt.first << " val=" << elt.second
<< dendl;
}
}
boost::optional<ceph::real_time> expiration_date;
boost::optional<std::string> rule_id;
const auto& rule_map = config.get_rule_map();
for (const auto& ri : rule_map) {
const auto& rule = ri.second;
auto& id = rule.get_id();
auto& prefix = rule.get_prefix();
auto& filter = rule.get_filter();
auto& expiration = rule.get_expiration();
auto& noncur_expiration = rule.get_noncur_expiration();
ldpp_dout(dpp, 10) << "rule: " << ri.first
<< " prefix: " << prefix
<< " expiration: "
<< " date: " << expiration.get_date()
<< " days: " << expiration.get_days()
<< " noncur_expiration: "
<< " date: " << noncur_expiration.get_date()
<< " days: " << noncur_expiration.get_days()
<< dendl;
/* skip if rule !enabled
* if rule has prefix, skip iff object !match prefix
* if rule has tags, skip iff object !match tags
* note if object is current or non-current, compare accordingly
* if rule has days, construct date expression and save iff older
* than last saved
* if rule has date, convert date expression and save iff older
* than last saved
* if the date accum has a value, format it into hdr
*/
if (!rule.is_enabled())
continue;
if(!prefix.empty()) {
if (!boost::starts_with(obj_key.name, prefix))
continue;
}
if (filter.has_tags()) {
bool tag_match = false;
const RGWObjTags& rule_tagset = filter.get_tags();
for (auto& tag : rule_tagset.get_tags()) {
/* remember, S3 tags are {key,value} tuples */
tag_match = true;
auto obj_tag = obj_tag_map.find(tag.first);
if (obj_tag == obj_tag_map.end() || obj_tag->second != tag.second) {
ldpp_dout(dpp, 10) << "tag does not match obj_key=" << obj_key
<< " rule_id=" << id
<< " tag=" << tag
<< dendl;
tag_match = false;
break;
}
}
if (! tag_match)
continue;
}
// compute a uniform expiration date
boost::optional<ceph::real_time> rule_expiration_date;
const LCExpiration& rule_expiration =
(obj_key.instance.empty()) ? expiration : noncur_expiration;
if (rule_expiration.has_date()) {
rule_expiration_date =
boost::optional<ceph::real_time>(
ceph::from_iso_8601(rule.get_expiration().get_date()));
rule_id = id;
} else {
if (rule_expiration.has_days()) {
rule_expiration_date =
boost::optional<ceph::real_time>(
mtime + make_timespan(double(rule_expiration.get_days())*24*60*60));
rule_id = id;
}
}
// update earliest expiration
if (rule_expiration_date) {
if ((! expiration_date) ||
(*expiration_date < *rule_expiration_date)) {
expiration_date =
boost::optional<ceph::real_time>(rule_expiration_date);
}
}
}
// cond format header
if (expiration_date && rule_id) {
// Fri, 23 Dec 2012 00:00:00 GMT
char exp_buf[100];
time_t exp = ceph::real_clock::to_time_t(*expiration_date);
if (std::strftime(exp_buf, sizeof(exp_buf),
"%a, %d %b %Y %T %Z", std::gmtime(&exp))) {
hdr = fmt::format("expiry-date=\"{0}\", rule-id=\"{1}\"", exp_buf,
*rule_id);
} else {
ldpp_dout(dpp, 0) << __func__ <<
"() strftime of life cycle expiration header failed"
<< dendl;
}
}
return hdr;
} /* rgwlc_s3_expiration_header */
} /* namespace rgw::lc */
|