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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
#include <vector>
#include "common/debug.h"
#include "common/containers.h"
#include "common/errno.h"
#include "common/error_code.h"
#include "common/async/blocked_completion.h"
#include "common/async/librados_completion.h"
#include "cls/fifo/cls_fifo_types.h"
#include "cls/log/cls_log_client.h"
#include "cls_fifo_legacy.h"
#include "rgw_datalog.h"
#include "rgw_log_backing.h"
#include "rgw_tools.h"
#define dout_context g_ceph_context
static constexpr auto dout_subsys = ceph_subsys_rgw;
namespace bs = boost::system;
namespace lr = librados;
using ceph::containers::tiny_vector;
void rgw_data_change::dump(ceph::Formatter *f) const
{
std::string type;
switch (entity_type) {
case ENTITY_TYPE_BUCKET:
type = "bucket";
break;
default:
type = "unknown";
}
encode_json("entity_type", type, f);
encode_json("key", key, f);
utime_t ut(timestamp);
encode_json("timestamp", ut, f);
}
void rgw_data_change::decode_json(JSONObj *obj) {
std::string s;
JSONDecoder::decode_json("entity_type", s, obj);
if (s == "bucket") {
entity_type = ENTITY_TYPE_BUCKET;
} else {
entity_type = ENTITY_TYPE_UNKNOWN;
}
JSONDecoder::decode_json("key", key, obj);
utime_t ut;
JSONDecoder::decode_json("timestamp", ut, obj);
timestamp = ut.to_real_time();
}
void rgw_data_change_log_entry::dump(Formatter *f) const
{
encode_json("log_id", log_id, f);
utime_t ut(log_timestamp);
encode_json("log_timestamp", ut, f);
encode_json("entry", entry, f);
}
void rgw_data_change_log_entry::decode_json(JSONObj *obj) {
JSONDecoder::decode_json("log_id", log_id, obj);
utime_t ut;
JSONDecoder::decode_json("log_timestamp", ut, obj);
log_timestamp = ut.to_real_time();
JSONDecoder::decode_json("entry", entry, obj);
}
class RGWDataChangesOmap final : public RGWDataChangesBE {
using centries = std::list<cls_log_entry>;
std::vector<std::string> oids;
public:
RGWDataChangesOmap(lr::IoCtx& ioctx,
RGWDataChangesLog& datalog,
uint64_t gen_id,
int num_shards)
: RGWDataChangesBE(ioctx, datalog, gen_id) {
oids.reserve(num_shards);
for (auto i = 0; i < num_shards; ++i) {
oids.push_back(get_oid(i));
}
}
~RGWDataChangesOmap() override = default;
void prepare(ceph::real_time ut, const std::string& key,
ceph::buffer::list&& entry, entries& out) override {
if (!std::holds_alternative<centries>(out)) {
ceph_assert(std::visit([](const auto& v) { return std::empty(v); }, out));
out = centries();
}
cls_log_entry e;
cls_log_add_prepare_entry(e, utime_t(ut), {}, key, entry);
std::get<centries>(out).push_back(std::move(e));
}
int push(const DoutPrefixProvider *dpp, int index, entries&& items) override {
lr::ObjectWriteOperation op;
cls_log_add(op, std::get<centries>(items), true);
auto r = rgw_rados_operate(dpp, ioctx, oids[index], &op, null_yield);
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": failed to push to " << oids[index] << cpp_strerror(-r)
<< dendl;
}
return r;
}
int push(const DoutPrefixProvider *dpp, int index, ceph::real_time now,
const std::string& key,
ceph::buffer::list&& bl) override {
lr::ObjectWriteOperation op;
cls_log_add(op, utime_t(now), {}, key, bl);
auto r = rgw_rados_operate(dpp, ioctx, oids[index], &op, null_yield);
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": failed to push to " << oids[index]
<< cpp_strerror(-r) << dendl;
}
return r;
}
int list(const DoutPrefixProvider *dpp, int index, int max_entries,
std::vector<rgw_data_change_log_entry>& entries,
std::optional<std::string_view> marker,
std::string* out_marker, bool* truncated) override {
std::list<cls_log_entry> log_entries;
lr::ObjectReadOperation op;
cls_log_list(op, {}, {}, std::string(marker.value_or("")),
max_entries, log_entries, out_marker, truncated);
auto r = rgw_rados_operate(dpp, ioctx, oids[index], &op, nullptr, null_yield);
if (r == -ENOENT) {
*truncated = false;
return 0;
}
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": failed to list " << oids[index]
<< cpp_strerror(-r) << dendl;
return r;
}
for (auto iter = log_entries.begin(); iter != log_entries.end(); ++iter) {
rgw_data_change_log_entry log_entry;
log_entry.log_id = iter->id;
auto rt = iter->timestamp.to_real_time();
log_entry.log_timestamp = rt;
auto liter = iter->data.cbegin();
try {
decode(log_entry.entry, liter);
} catch (ceph::buffer::error& err) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": failed to decode data changes log entry: "
<< err.what() << dendl;
return -EIO;
}
entries.push_back(log_entry);
}
return 0;
}
int get_info(const DoutPrefixProvider *dpp, int index, RGWDataChangesLogInfo *info) override {
cls_log_header header;
lr::ObjectReadOperation op;
cls_log_info(op, &header);
auto r = rgw_rados_operate(dpp, ioctx, oids[index], &op, nullptr, null_yield);
if (r == -ENOENT) r = 0;
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": failed to get info from " << oids[index]
<< cpp_strerror(-r) << dendl;
} else {
info->marker = header.max_marker;
info->last_update = header.max_time.to_real_time();
}
return r;
}
int trim(const DoutPrefixProvider *dpp, int index, std::string_view marker) override {
lr::ObjectWriteOperation op;
cls_log_trim(op, {}, {}, {}, std::string(marker));
auto r = rgw_rados_operate(dpp, ioctx, oids[index], &op, null_yield);
if (r == -ENOENT) r = -ENODATA;
if (r < 0 && r != -ENODATA) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": failed to get info from " << oids[index]
<< cpp_strerror(-r) << dendl;
}
return r;
}
int trim(const DoutPrefixProvider *dpp, int index, std::string_view marker,
lr::AioCompletion* c) override {
lr::ObjectWriteOperation op;
cls_log_trim(op, {}, {}, {}, std::string(marker));
auto r = ioctx.aio_operate(oids[index], c, &op, 0);
if (r == -ENOENT) r = -ENODATA;
if (r < 0) {
lderr(cct) << __PRETTY_FUNCTION__
<< ": failed to get info from " << oids[index]
<< cpp_strerror(-r) << dendl;
}
return r;
}
std::string_view max_marker() const override {
return "99999999"sv;
}
int is_empty(const DoutPrefixProvider *dpp) override {
for (auto shard = 0u; shard < oids.size(); ++shard) {
std::list<cls_log_entry> log_entries;
lr::ObjectReadOperation op;
std::string out_marker;
bool truncated;
cls_log_list(op, {}, {}, {}, 1, log_entries, &out_marker, &truncated);
auto r = rgw_rados_operate(dpp, ioctx, oids[shard], &op, nullptr, null_yield);
if (r == -ENOENT) {
continue;
}
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": failed to list " << oids[shard]
<< cpp_strerror(-r) << dendl;
return r;
}
if (!log_entries.empty()) {
return 0;
}
}
return 1;
}
};
class RGWDataChangesFIFO final : public RGWDataChangesBE {
using centries = std::vector<ceph::buffer::list>;
tiny_vector<LazyFIFO> fifos;
public:
RGWDataChangesFIFO(lr::IoCtx& ioctx,
RGWDataChangesLog& datalog,
uint64_t gen_id, int shards)
: RGWDataChangesBE(ioctx, datalog, gen_id),
fifos(shards, [&ioctx, this](std::size_t i, auto emplacer) {
emplacer.emplace(ioctx, get_oid(i));
}) {}
~RGWDataChangesFIFO() override = default;
void prepare(ceph::real_time, const std::string&,
ceph::buffer::list&& entry, entries& out) override {
if (!std::holds_alternative<centries>(out)) {
ceph_assert(std::visit([](auto& v) { return std::empty(v); }, out));
out = centries();
}
std::get<centries>(out).push_back(std::move(entry));
}
int push(const DoutPrefixProvider *dpp, int index, entries&& items) override {
auto r = fifos[index].push(dpp, std::get<centries>(items), null_yield);
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": unable to push to FIFO: " << get_oid(index)
<< ": " << cpp_strerror(-r) << dendl;
}
return r;
}
int push(const DoutPrefixProvider *dpp, int index, ceph::real_time,
const std::string&,
ceph::buffer::list&& bl) override {
auto r = fifos[index].push(dpp, std::move(bl), null_yield);
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": unable to push to FIFO: " << get_oid(index)
<< ": " << cpp_strerror(-r) << dendl;
}
return r;
}
int list(const DoutPrefixProvider *dpp, int index, int max_entries,
std::vector<rgw_data_change_log_entry>& entries,
std::optional<std::string_view> marker,
std::string* out_marker, bool* truncated) override {
std::vector<rgw::cls::fifo::list_entry> log_entries;
bool more = false;
auto r = fifos[index].list(dpp, max_entries, marker, &log_entries, &more,
null_yield);
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": unable to list FIFO: " << get_oid(index)
<< ": " << cpp_strerror(-r) << dendl;
return r;
}
for (const auto& entry : log_entries) {
rgw_data_change_log_entry log_entry;
log_entry.log_id = entry.marker;
log_entry.log_timestamp = entry.mtime;
auto liter = entry.data.cbegin();
try {
decode(log_entry.entry, liter);
} catch (const buffer::error& err) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": failed to decode data changes log entry: "
<< err.what() << dendl;
return -EIO;
}
entries.push_back(std::move(log_entry));
}
if (truncated)
*truncated = more;
if (out_marker && !log_entries.empty()) {
*out_marker = log_entries.back().marker;
}
return 0;
}
int get_info(const DoutPrefixProvider *dpp, int index, RGWDataChangesLogInfo *info) override {
auto& fifo = fifos[index];
auto r = fifo.read_meta(dpp, null_yield);
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": unable to get FIFO metadata: " << get_oid(index)
<< ": " << cpp_strerror(-r) << dendl;
return r;
}
rados::cls::fifo::info m;
fifo.meta(dpp, m, null_yield);
auto p = m.head_part_num;
if (p < 0) {
info->marker = ""s;
info->last_update = ceph::real_clock::zero();
return 0;
}
rgw::cls::fifo::part_info h;
r = fifo.get_part_info(dpp, p, &h, null_yield);
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": unable to get part info: " << get_oid(index) << "/" << p
<< ": " << cpp_strerror(-r) << dendl;
return r;
}
info->marker = rgw::cls::fifo::marker{p, h.last_ofs}.to_string();
info->last_update = h.max_time;
return 0;
}
int trim(const DoutPrefixProvider *dpp, int index, std::string_view marker) override {
auto r = fifos[index].trim(dpp, marker, false, null_yield);
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": unable to trim FIFO: " << get_oid(index)
<< ": " << cpp_strerror(-r) << dendl;
}
return r;
}
int trim(const DoutPrefixProvider *dpp, int index, std::string_view marker,
librados::AioCompletion* c) override {
int r = 0;
if (marker == rgw::cls::fifo::marker(0, 0).to_string()) {
rgw_complete_aio_completion(c, -ENODATA);
} else {
fifos[index].trim(dpp, marker, false, c, null_yield);
}
return r;
}
std::string_view max_marker() const override {
static const std::string mm =
rgw::cls::fifo::marker::max().to_string();
return std::string_view(mm);
}
int is_empty(const DoutPrefixProvider *dpp) override {
std::vector<rgw::cls::fifo::list_entry> log_entries;
bool more = false;
for (auto shard = 0u; shard < fifos.size(); ++shard) {
auto r = fifos[shard].list(dpp, 1, {}, &log_entries, &more,
null_yield);
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": unable to list FIFO: " << get_oid(shard)
<< ": " << cpp_strerror(-r) << dendl;
return r;
}
if (!log_entries.empty()) {
return 0;
}
}
return 1;
}
};
RGWDataChangesLog::RGWDataChangesLog(CephContext* cct)
: cct(cct),
num_shards(cct->_conf->rgw_data_log_num_shards),
prefix(get_prefix()),
changes(cct->_conf->rgw_data_log_changes_size) {}
bs::error_code DataLogBackends::handle_init(entries_t e) noexcept {
std::unique_lock l(m);
for (const auto& [gen_id, gen] : e) {
if (gen.pruned) {
lderr(datalog.cct)
<< __PRETTY_FUNCTION__ << ":" << __LINE__
<< ": ERROR: given empty generation: gen_id=" << gen_id << dendl;
}
if (count(gen_id) != 0) {
lderr(datalog.cct)
<< __PRETTY_FUNCTION__ << ":" << __LINE__
<< ": ERROR: generation already exists: gen_id=" << gen_id << dendl;
}
try {
switch (gen.type) {
case log_type::omap:
emplace(gen_id, new RGWDataChangesOmap(ioctx, datalog, gen_id, shards));
break;
case log_type::fifo:
emplace(gen_id, new RGWDataChangesFIFO(ioctx, datalog, gen_id, shards));
break;
default:
lderr(datalog.cct)
<< __PRETTY_FUNCTION__ << ":" << __LINE__
<< ": IMPOSSIBLE: invalid log type: gen_id=" << gen_id
<< ", type" << gen.type << dendl;
return bs::error_code(EFAULT, bs::system_category());
}
} catch (const bs::system_error& err) {
lderr(datalog.cct)
<< __PRETTY_FUNCTION__ << ":" << __LINE__
<< ": error setting up backend: gen_id=" << gen_id
<< ", err=" << err.what() << dendl;
return err.code();
}
}
return {};
}
bs::error_code DataLogBackends::handle_new_gens(entries_t e) noexcept {
return handle_init(std::move(e));
}
bs::error_code DataLogBackends::handle_empty_to(uint64_t new_tail) noexcept {
std::unique_lock l(m);
auto i = cbegin();
if (i->first < new_tail) {
return {};
}
if (new_tail >= (cend() - 1)->first) {
lderr(datalog.cct)
<< __PRETTY_FUNCTION__ << ":" << __LINE__
<< ": ERROR: attempt to trim head: new_tail=" << new_tail << dendl;
return bs::error_code(EFAULT, bs::system_category());
}
erase(i, upper_bound(new_tail));
return {};
}
int RGWDataChangesLog::start(const DoutPrefixProvider *dpp,
const RGWZone* _zone,
const RGWZoneParams& zoneparams,
librados::Rados* lr)
{
zone = _zone;
ceph_assert(zone);
auto defbacking = to_log_type(
cct->_conf.get_val<std::string>("rgw_default_data_log_backing"));
// Should be guaranteed by `set_enum_allowed`
ceph_assert(defbacking);
auto log_pool = zoneparams.log_pool;
auto r = rgw_init_ioctx(dpp, lr, log_pool, ioctx, true, false);
if (r < 0) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": Failed to initialized ioctx, r=" << r
<< ", pool=" << log_pool << dendl;
return -r;
}
auto besr = logback_generations::init<DataLogBackends>(
dpp, ioctx, metadata_log_oid(), [this](uint64_t gen_id, int shard) {
return get_oid(gen_id, shard);
},
num_shards, *defbacking, null_yield, *this);
if (!besr) {
ldpp_dout(dpp, -1) << __PRETTY_FUNCTION__
<< ": Error initializing backends: "
<< besr.error().message() << dendl;
return ceph::from_error_code(besr.error());
}
bes = std::move(*besr);
renew_thread = make_named_thread("rgw_dt_lg_renew",
&RGWDataChangesLog::renew_run, this);
return 0;
}
int RGWDataChangesLog::choose_oid(const rgw_bucket_shard& bs) {
const auto& name = bs.bucket.name;
auto shard_shift = (bs.shard_id > 0 ? bs.shard_id : 0);
auto r = (ceph_str_hash_linux(name.data(), name.size()) +
shard_shift) % num_shards;
return static_cast<int>(r);
}
int RGWDataChangesLog::renew_entries(const DoutPrefixProvider *dpp)
{
if (!zone->log_data)
return 0;
/* we can't keep the bucket name as part of the cls_log_entry, and we need
* it later, so we keep two lists under the map */
bc::flat_map<int, std::pair<std::vector<rgw_bucket_shard>,
RGWDataChangesBE::entries>> m;
std::unique_lock l(lock);
decltype(cur_cycle) entries;
entries.swap(cur_cycle);
l.unlock();
auto ut = real_clock::now();
auto be = bes->head();
for (const auto& bs : entries) {
auto index = choose_oid(bs);
rgw_data_change change;
bufferlist bl;
change.entity_type = ENTITY_TYPE_BUCKET;
change.key = bs.get_key();
change.timestamp = ut;
encode(change, bl);
m[index].first.push_back(bs);
be->prepare(ut, change.key, std::move(bl), m[index].second);
}
for (auto& [index, p] : m) {
auto& [buckets, entries] = p;
auto now = real_clock::now();
auto ret = be->push(dpp, index, std::move(entries));
if (ret < 0) {
/* we don't really need to have a special handling for failed cases here,
* as this is just an optimization. */
ldpp_dout(dpp, -1) << "ERROR: svc.cls->timelog.add() returned " << ret << dendl;
return ret;
}
auto expiration = now;
expiration += ceph::make_timespan(cct->_conf->rgw_data_log_window);
for (auto& bs : buckets) {
update_renewed(bs, expiration);
}
}
return 0;
}
void RGWDataChangesLog::_get_change(const rgw_bucket_shard& bs,
ChangeStatusPtr& status)
{
ceph_assert(ceph_mutex_is_locked(lock));
if (!changes.find(bs, status)) {
status = ChangeStatusPtr(new ChangeStatus);
changes.add(bs, status);
}
}
void RGWDataChangesLog::register_renew(const rgw_bucket_shard& bs)
{
std::scoped_lock l{lock};
cur_cycle.insert(bs);
}
void RGWDataChangesLog::update_renewed(const rgw_bucket_shard& bs,
real_time expiration)
{
std::unique_lock l{lock};
ChangeStatusPtr status;
_get_change(bs, status);
l.unlock();
ldout(cct, 20) << "RGWDataChangesLog::update_renewd() bucket_name="
<< bs.bucket.name << " shard_id=" << bs.shard_id
<< " expiration=" << expiration << dendl;
std::unique_lock sl(status->lock);
status->cur_expiration = expiration;
}
int RGWDataChangesLog::get_log_shard_id(rgw_bucket& bucket, int shard_id) {
rgw_bucket_shard bs(bucket, shard_id);
return choose_oid(bs);
}
bool RGWDataChangesLog::filter_bucket(const DoutPrefixProvider *dpp,
const rgw_bucket& bucket,
optional_yield y) const
{
if (!bucket_filter) {
return true;
}
return bucket_filter(bucket, y, dpp);
}
std::string RGWDataChangesLog::get_oid(uint64_t gen_id, int i) const {
return (gen_id > 0 ?
fmt::format("{}@G{}.{}", prefix, gen_id, i) :
fmt::format("{}.{}", prefix, i));
}
int RGWDataChangesLog::add_entry(const DoutPrefixProvider *dpp, const RGWBucketInfo& bucket_info, int shard_id) {
auto& bucket = bucket_info.bucket;
if (!filter_bucket(dpp, bucket, null_yield)) {
return 0;
}
if (observer) {
observer->on_bucket_changed(bucket.get_key());
}
rgw_bucket_shard bs(bucket, shard_id);
int index = choose_oid(bs);
mark_modified(index, bs);
std::unique_lock l(lock);
ChangeStatusPtr status;
_get_change(bs, status);
l.unlock();
auto now = real_clock::now();
std::unique_lock sl(status->lock);
ldpp_dout(dpp, 20) << "RGWDataChangesLog::add_entry() bucket.name=" << bucket.name
<< " shard_id=" << shard_id << " now=" << now
<< " cur_expiration=" << status->cur_expiration << dendl;
if (now < status->cur_expiration) {
/* no need to send, recently completed */
sl.unlock();
register_renew(bs);
return 0;
}
RefCountedCond* cond;
if (status->pending) {
cond = status->cond;
ceph_assert(cond);
status->cond->get();
sl.unlock();
int ret = cond->wait();
cond->put();
if (!ret) {
register_renew(bs);
}
return ret;
}
status->cond = new RefCountedCond;
status->pending = true;
ceph::real_time expiration;
int ret;
do {
status->cur_sent = now;
expiration = now;
expiration += ceph::make_timespan(cct->_conf->rgw_data_log_window);
sl.unlock();
ceph::buffer::list bl;
rgw_data_change change;
change.entity_type = ENTITY_TYPE_BUCKET;
change.key = bs.get_key();
change.timestamp = now;
encode(change, bl);
ldpp_dout(dpp, 20) << "RGWDataChangesLog::add_entry() sending update with now=" << now << " cur_expiration=" << expiration << dendl;
auto be = bes->head();
ret = be->push(dpp, index, now, change.key, std::move(bl));
now = real_clock::now();
sl.lock();
} while (!ret && real_clock::now() > expiration);
cond = status->cond;
status->pending = false;
/* time of when operation started, not completed */
status->cur_expiration = status->cur_sent;
status->cur_expiration += make_timespan(cct->_conf->rgw_data_log_window);
status->cond = nullptr;
sl.unlock();
cond->done(ret);
cond->put();
return ret;
}
int DataLogBackends::list(const DoutPrefixProvider *dpp, int shard, int max_entries,
std::vector<rgw_data_change_log_entry>& entries,
std::string_view marker,
std::string* out_marker,
bool* truncated)
{
const auto [start_id, start_cursor] = cursorgen(marker);
auto gen_id = start_id;
std::string out_cursor;
while (max_entries > 0) {
std::vector<rgw_data_change_log_entry> gentries;
std::unique_lock l(m);
auto i = lower_bound(gen_id);
if (i == end()) return 0;
auto be = i->second;
l.unlock();
gen_id = be->gen_id;
auto r = be->list(dpp, shard, max_entries, gentries,
gen_id == start_id ? start_cursor : std::string{},
&out_cursor, truncated);
if (r < 0)
return r;
if (out_marker && !out_cursor.empty()) {
*out_marker = gencursor(gen_id, out_cursor);
}
for (auto& g : gentries) {
g.log_id = gencursor(gen_id, g.log_id);
}
if (int s = gentries.size(); s < 0 || s > max_entries)
max_entries = 0;
else
max_entries -= gentries.size();
std::move(gentries.begin(), gentries.end(),
std::back_inserter(entries));
++gen_id;
}
return 0;
}
int RGWDataChangesLog::list_entries(const DoutPrefixProvider *dpp, int shard, int max_entries,
std::vector<rgw_data_change_log_entry>& entries,
std::string_view marker,
std::string* out_marker, bool* truncated)
{
assert(shard < num_shards);
return bes->list(dpp, shard, max_entries, entries, marker, out_marker, truncated);
}
int RGWDataChangesLog::list_entries(const DoutPrefixProvider *dpp, int max_entries,
std::vector<rgw_data_change_log_entry>& entries,
LogMarker& marker, bool *ptruncated)
{
bool truncated;
entries.clear();
for (; marker.shard < num_shards && int(entries.size()) < max_entries;
marker.shard++, marker.marker.clear()) {
int ret = list_entries(dpp, marker.shard, max_entries - entries.size(),
entries, marker.marker, NULL, &truncated);
if (ret == -ENOENT) {
continue;
}
if (ret < 0) {
return ret;
}
if (!truncated) {
*ptruncated = false;
return 0;
}
}
*ptruncated = (marker.shard < num_shards);
return 0;
}
int RGWDataChangesLog::get_info(const DoutPrefixProvider *dpp, int shard_id, RGWDataChangesLogInfo *info)
{
assert(shard_id < num_shards);
auto be = bes->head();
auto r = be->get_info(dpp, shard_id, info);
if (!info->marker.empty()) {
info->marker = gencursor(be->gen_id, info->marker);
}
return r;
}
int DataLogBackends::trim_entries(const DoutPrefixProvider *dpp, int shard_id, std::string_view marker)
{
auto [target_gen, cursor] = cursorgen(marker);
std::unique_lock l(m);
const auto head_gen = (end() - 1)->second->gen_id;
const auto tail_gen = begin()->first;
if (target_gen < tail_gen) return 0;
auto r = 0;
for (auto be = lower_bound(0)->second;
be->gen_id <= target_gen && be->gen_id <= head_gen && r >= 0;
be = upper_bound(be->gen_id)->second) {
l.unlock();
auto c = be->gen_id == target_gen ? cursor : be->max_marker();
r = be->trim(dpp, shard_id, c);
if (r == -ENOENT)
r = -ENODATA;
if (r == -ENODATA && be->gen_id < target_gen)
r = 0;
if (be->gen_id == target_gen)
break;
l.lock();
};
return r;
}
int RGWDataChangesLog::trim_entries(const DoutPrefixProvider *dpp, int shard_id, std::string_view marker)
{
assert(shard_id < num_shards);
return bes->trim_entries(dpp, shard_id, marker);
}
class GenTrim : public rgw::cls::fifo::Completion<GenTrim> {
public:
DataLogBackends* const bes;
const int shard_id;
const uint64_t target_gen;
const std::string cursor;
const uint64_t head_gen;
const uint64_t tail_gen;
boost::intrusive_ptr<RGWDataChangesBE> be;
GenTrim(const DoutPrefixProvider *dpp, DataLogBackends* bes, int shard_id, uint64_t target_gen,
std::string cursor, uint64_t head_gen, uint64_t tail_gen,
boost::intrusive_ptr<RGWDataChangesBE> be,
lr::AioCompletion* super)
: Completion(dpp, super), bes(bes), shard_id(shard_id), target_gen(target_gen),
cursor(std::move(cursor)), head_gen(head_gen), tail_gen(tail_gen),
be(std::move(be)) {}
void handle(const DoutPrefixProvider *dpp, Ptr&& p, int r) {
auto gen_id = be->gen_id;
be.reset();
if (r == -ENOENT)
r = -ENODATA;
if (r == -ENODATA && gen_id < target_gen)
r = 0;
if (r < 0) {
complete(std::move(p), r);
return;
}
{
std::unique_lock l(bes->m);
auto i = bes->upper_bound(gen_id);
if (i == bes->end() || i->first > target_gen || i->first > head_gen) {
l.unlock();
complete(std::move(p), -ENODATA);
return;
}
be = i->second;
}
auto c = be->gen_id == target_gen ? cursor : be->max_marker();
be->trim(dpp, shard_id, c, call(std::move(p)));
}
};
void DataLogBackends::trim_entries(const DoutPrefixProvider *dpp, int shard_id, std::string_view marker,
librados::AioCompletion* c)
{
auto [target_gen, cursor] = cursorgen(marker);
std::unique_lock l(m);
const auto head_gen = (end() - 1)->second->gen_id;
const auto tail_gen = begin()->first;
if (target_gen < tail_gen) {
l.unlock();
rgw_complete_aio_completion(c, -ENODATA);
return;
}
auto be = begin()->second;
l.unlock();
auto gt = std::make_unique<GenTrim>(dpp, this, shard_id, target_gen,
std::string(cursor), head_gen, tail_gen,
be, c);
auto cc = be->gen_id == target_gen ? cursor : be->max_marker();
be->trim(dpp, shard_id, cc, GenTrim::call(std::move(gt)));
}
int DataLogBackends::trim_generations(const DoutPrefixProvider *dpp, std::optional<uint64_t>& through) {
if (size() != 1) {
std::vector<mapped_type> candidates;
{
std::scoped_lock l(m);
auto e = cend() - 1;
for (auto i = cbegin(); i < e; ++i) {
candidates.push_back(i->second);
}
}
std::optional<uint64_t> highest;
for (auto& be : candidates) {
auto r = be->is_empty(dpp);
if (r < 0) {
return r;
} else if (r == 1) {
highest = be->gen_id;
} else {
break;
}
}
through = highest;
if (!highest) {
return 0;
}
auto ec = empty_to(dpp, *highest, null_yield);
if (ec) {
return ceph::from_error_code(ec);
}
}
return ceph::from_error_code(remove_empty(dpp, null_yield));
}
int RGWDataChangesLog::trim_entries(const DoutPrefixProvider *dpp, int shard_id, std::string_view marker,
librados::AioCompletion* c)
{
assert(shard_id < num_shards);
bes->trim_entries(dpp, shard_id, marker, c);
return 0;
}
bool RGWDataChangesLog::going_down() const
{
return down_flag;
}
RGWDataChangesLog::~RGWDataChangesLog() {
down_flag = true;
if (renew_thread.joinable()) {
renew_stop();
renew_thread.join();
}
}
void RGWDataChangesLog::renew_run() {
static constexpr auto runs_per_prune = 150;
auto run = 0;
for (;;) {
const DoutPrefix dp(cct, dout_subsys, "rgw data changes log: ");
ldpp_dout(&dp, 2) << "RGWDataChangesLog::ChangesRenewThread: start" << dendl;
int r = renew_entries(&dp);
if (r < 0) {
ldpp_dout(&dp, 0) << "ERROR: RGWDataChangesLog::renew_entries returned error r=" << r << dendl;
}
if (going_down())
break;
if (run == runs_per_prune) {
std::optional<uint64_t> through;
ldpp_dout(&dp, 2) << "RGWDataChangesLog::ChangesRenewThread: pruning old generations" << dendl;
trim_generations(&dp, through);
if (r < 0) {
derr << "RGWDataChangesLog::ChangesRenewThread: failed pruning r="
<< r << dendl;
} else if (through) {
ldpp_dout(&dp, 2) << "RGWDataChangesLog::ChangesRenewThread: pruned generations "
<< "through " << *through << "." << dendl;
} else {
ldpp_dout(&dp, 2) << "RGWDataChangesLog::ChangesRenewThread: nothing to prune."
<< dendl;
}
run = 0;
} else {
++run;
}
int interval = cct->_conf->rgw_data_log_window * 3 / 4;
std::unique_lock locker{renew_lock};
renew_cond.wait_for(locker, std::chrono::seconds(interval));
}
}
void RGWDataChangesLog::renew_stop()
{
std::lock_guard l{renew_lock};
renew_cond.notify_all();
}
void RGWDataChangesLog::mark_modified(int shard_id, const rgw_bucket_shard& bs)
{
auto key = bs.get_key();
{
std::shared_lock rl{modified_lock}; // read lock to check for existence
auto shard = modified_shards.find(shard_id);
if (shard != modified_shards.end() && shard->second.count(key)) {
return;
}
}
std::unique_lock wl{modified_lock}; // write lock for insertion
modified_shards[shard_id].insert(key);
}
std::string RGWDataChangesLog::max_marker() const {
return gencursor(std::numeric_limits<uint64_t>::max(),
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
int RGWDataChangesLog::change_format(const DoutPrefixProvider *dpp, log_type type, optional_yield y) {
return ceph::from_error_code(bes->new_backing(dpp, type, y));
}
int RGWDataChangesLog::trim_generations(const DoutPrefixProvider *dpp, std::optional<uint64_t>& through) {
return bes->trim_generations(dpp, through);
}
|