-
Notifications
You must be signed in to change notification settings - Fork 4
/
rgw_sync.cc
2176 lines (1861 loc) · 68.7 KB
/
rgw_sync.cc
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "common/ceph_json.h"
#include "common/RWLock.h"
#include "common/RefCountedObj.h"
#include "common/WorkQueue.h"
#include "common/Throttle.h"
#include "common/admin_socket.h"
#include "common/errno.h"
#include "rgw_common.h"
#include "rgw_rados.h"
#include "rgw_sync.h"
#include "rgw_metadata.h"
#include "rgw_rest_conn.h"
#include "rgw_tools.h"
#include "rgw_cr_rados.h"
#include "rgw_cr_rest.h"
#include "rgw_http_client.h"
#include "rgw_boost_asio_yield.h"
#include "cls/lock/cls_lock_client.h"
#define dout_subsys ceph_subsys_rgw
#undef dout_prefix
#define dout_prefix (*_dout << "rgw meta sync: ")
static string mdlog_sync_status_oid = "mdlog.sync-status";
static string mdlog_sync_status_shard_prefix = "mdlog.sync-status.shard";
static string mdlog_sync_full_sync_index_prefix = "meta.full-sync.index";
RGWSyncErrorLogger::RGWSyncErrorLogger(RGWRados *_store, const string oid_prefix, int _num_shards) : store(_store), num_shards(_num_shards) {
for (int i = 0; i < num_shards; i++) {
oids.push_back(get_shard_oid(oid_prefix, i));
}
}
string RGWSyncErrorLogger::get_shard_oid(const string& oid_prefix, int shard_id) {
char buf[oid_prefix.size() + 16];
snprintf(buf, sizeof(buf), "%s.%d", oid_prefix.c_str(), shard_id);
return string(buf);
}
RGWCoroutine *RGWSyncErrorLogger::log_error_cr(const string& source_zone, const string& section, const string& name, uint32_t error_code, const string& message) {
cls_log_entry entry;
rgw_sync_error_info info(source_zone, error_code, message);
bufferlist bl;
::encode(info, bl);
store->time_log_prepare_entry(entry, real_clock::now(), section, name, bl);
uint32_t shard_id = counter.inc() % num_shards;
return new RGWRadosTimelogAddCR(store, oids[shard_id], entry);
}
void RGWSyncBackoff::update_wait_time()
{
if (cur_wait == 0) {
cur_wait = 1;
} else {
cur_wait = (cur_wait << 1);
}
if (cur_wait >= max_secs) {
cur_wait = max_secs;
}
}
void RGWSyncBackoff::backoff_sleep()
{
update_wait_time();
sleep(cur_wait);
}
void RGWSyncBackoff::backoff(RGWCoroutine *op)
{
update_wait_time();
op->wait(utime_t(cur_wait, 0));
}
int RGWBackoffControlCR::operate() {
RGWCoroutine *finisher_cr;
reenter(this) {
while (true) {
yield {
Mutex::Locker l(lock);
cr = alloc_cr();
cr->get();
call(cr);
}
{
Mutex::Locker l(lock);
cr->put();
cr = NULL;
}
if (retcode < 0 && retcode != -EBUSY && retcode != -EAGAIN) {
ldout(cct, 0) << "ERROR: RGWBackoffControlCR called coroutine returned " << retcode << dendl;
if (exit_on_error) {
return set_cr_error(retcode);
}
}
if (reset_backoff) {
backoff.reset();
}
yield backoff.backoff(this);
finisher_cr = alloc_finisher_cr();
if (finisher_cr) {
yield call(finisher_cr);
if (retcode < 0) {
ldout(cct, 0) << "ERROR: call to finisher_cr() failed: retcode=" << retcode << dendl;
if (exit_on_error) {
return set_cr_error(retcode);
}
}
}
}
}
return 0;
}
void rgw_mdlog_info::decode_json(JSONObj *obj) {
JSONDecoder::decode_json("num_objects", num_shards, obj);
JSONDecoder::decode_json("period", period, obj);
JSONDecoder::decode_json("realm_epoch", realm_epoch, obj);
}
void rgw_mdlog_entry::decode_json(JSONObj *obj) {
JSONDecoder::decode_json("id", id, obj);
JSONDecoder::decode_json("section", section, obj);
JSONDecoder::decode_json("name", name, obj);
utime_t ut;
JSONDecoder::decode_json("timestamp", ut, obj);
timestamp = ut.to_real_time();
JSONDecoder::decode_json("data", log_data, obj);
}
void rgw_mdlog_shard_data::decode_json(JSONObj *obj) {
JSONDecoder::decode_json("marker", marker, obj);
JSONDecoder::decode_json("truncated", truncated, obj);
JSONDecoder::decode_json("entries", entries, obj);
};
int RGWShardCollectCR::operate() {
reenter(this) {
while (spawn_next()) {
current_running++;
while (current_running >= max_concurrent) {
int child_ret;
yield wait_for_child();
if (collect_next(&child_ret)) {
current_running--;
if (child_ret < 0 && child_ret != -ENOENT) {
ldout(cct, 10) << __func__ << ": failed to fetch log status, ret=" << child_ret << dendl;
status = child_ret;
}
}
}
}
while (current_running > 0) {
int child_ret;
yield wait_for_child();
if (collect_next(&child_ret)) {
current_running--;
if (child_ret < 0 && child_ret != -ENOENT) {
ldout(cct, 10) << __func__ << ": failed to fetch log status, ret=" << child_ret << dendl;
status = child_ret;
}
}
}
if (status < 0) {
return set_cr_error(status);
}
return set_cr_done();
}
return 0;
}
class RGWReadRemoteMDLogInfoCR : public RGWShardCollectCR {
RGWMetaSyncEnv *sync_env;
const std::string& period;
int num_shards;
map<int, RGWMetadataLogInfo> *mdlog_info;
int shard_id;
#define READ_MDLOG_MAX_CONCURRENT 10
public:
RGWReadRemoteMDLogInfoCR(RGWMetaSyncEnv *_sync_env,
const std::string& period, int _num_shards,
map<int, RGWMetadataLogInfo> *_mdlog_info) : RGWShardCollectCR(_sync_env->cct, READ_MDLOG_MAX_CONCURRENT),
sync_env(_sync_env),
period(period), num_shards(_num_shards),
mdlog_info(_mdlog_info), shard_id(0) {}
bool spawn_next();
};
class RGWListRemoteMDLogCR : public RGWShardCollectCR {
RGWMetaSyncEnv *sync_env;
const std::string& period;
map<int, string> shards;
int max_entries_per_shard;
map<int, rgw_mdlog_shard_data> *result;
map<int, string>::iterator iter;
#define READ_MDLOG_MAX_CONCURRENT 10
public:
RGWListRemoteMDLogCR(RGWMetaSyncEnv *_sync_env,
const std::string& period, map<int, string>& _shards,
int _max_entries_per_shard,
map<int, rgw_mdlog_shard_data> *_result) : RGWShardCollectCR(_sync_env->cct, READ_MDLOG_MAX_CONCURRENT),
sync_env(_sync_env), period(period),
max_entries_per_shard(_max_entries_per_shard),
result(_result) {
shards.swap(_shards);
iter = shards.begin();
}
bool spawn_next();
};
RGWRemoteMetaLog::~RGWRemoteMetaLog()
{
delete error_logger;
}
int RGWRemoteMetaLog::read_log_info(rgw_mdlog_info *log_info)
{
rgw_http_param_pair pairs[] = { { "type", "metadata" },
{ NULL, NULL } };
int ret = conn->get_json_resource("/admin/log", pairs, *log_info);
if (ret < 0) {
ldout(store->ctx(), 0) << "ERROR: failed to fetch mdlog info" << dendl;
return ret;
}
ldout(store->ctx(), 20) << "remote mdlog, num_shards=" << log_info->num_shards << dendl;
return 0;
}
int RGWRemoteMetaLog::read_master_log_shards_info(string *master_period, map<int, RGWMetadataLogInfo> *shards_info)
{
if (store->is_meta_master()) {
return 0;
}
rgw_mdlog_info log_info;
int ret = read_log_info(&log_info);
if (ret < 0) {
return ret;
}
*master_period = log_info.period;
return run(new RGWReadRemoteMDLogInfoCR(&sync_env, log_info.period, log_info.num_shards, shards_info));
}
int RGWRemoteMetaLog::read_master_log_shards_next(const string& period, map<int, string> shard_markers, map<int, rgw_mdlog_shard_data> *result)
{
if (store->is_meta_master()) {
return 0;
}
return run(new RGWListRemoteMDLogCR(&sync_env, period, shard_markers, 1, result));
}
int RGWRemoteMetaLog::init()
{
conn = store->rest_master_conn;
int ret = http_manager.set_threaded();
if (ret < 0) {
ldout(store->ctx(), 0) << "failed in http_manager.set_threaded() ret=" << ret << dendl;
return ret;
}
error_logger = new RGWSyncErrorLogger(store, RGW_SYNC_ERROR_LOG_SHARD_PREFIX, ERROR_LOGGER_SHARDS);
init_sync_env(&sync_env);
return 0;
}
void RGWRemoteMetaLog::finish()
{
going_down.set(1);
stop();
}
#define CLONE_MAX_ENTRIES 100
int RGWMetaSyncStatusManager::init()
{
if (store->is_meta_master()) {
return 0;
}
if (!store->rest_master_conn) {
lderr(store->ctx()) << "no REST connection to master zone" << dendl;
return -EIO;
}
const char *log_pool = store->get_zone_params().log_pool.name.c_str();
librados::Rados *rados = store->get_rados_handle();
int r = rados->ioctx_create(log_pool, ioctx);
if (r < 0) {
lderr(store->ctx()) << "ERROR: failed to open log pool (" << store->get_zone_params().log_pool.name << " ret=" << r << dendl;
return r;
}
r = master_log.init();
if (r < 0) {
lderr(store->ctx()) << "ERROR: failed to init remote log, r=" << r << dendl;
return r;
}
RGWMetaSyncEnv& sync_env = master_log.get_sync_env();
r = read_sync_status();
if (r < 0 && r != -ENOENT) {
lderr(store->ctx()) << "ERROR: failed to read sync status, r=" << r << dendl;
return r;
}
int num_shards = master_log.get_sync_status().sync_info.num_shards;
for (int i = 0; i < num_shards; i++) {
shard_objs[i] = rgw_obj(store->get_zone_params().log_pool, sync_env.shard_obj_name(i));
}
RWLock::WLocker wl(ts_to_shard_lock);
for (int i = 0; i < num_shards; i++) {
clone_markers.push_back(string());
utime_shard ut;
ut.shard_id = i;
ts_to_shard[ut] = i;
}
return 0;
}
void RGWMetaSyncEnv::init(CephContext *_cct, RGWRados *_store, RGWRESTConn *_conn,
RGWAsyncRadosProcessor *_async_rados, RGWHTTPManager *_http_manager,
RGWSyncErrorLogger *_error_logger) {
cct = _cct;
store = _store;
conn = _conn;
async_rados = _async_rados;
http_manager = _http_manager;
error_logger = _error_logger;
}
string RGWMetaSyncEnv::status_oid()
{
return mdlog_sync_status_oid;
}
string RGWMetaSyncEnv::shard_obj_name(int shard_id)
{
char buf[mdlog_sync_status_shard_prefix.size() + 16];
snprintf(buf, sizeof(buf), "%s.%d", mdlog_sync_status_shard_prefix.c_str(), shard_id);
return string(buf);
}
class RGWAsyncReadMDLogEntries : public RGWAsyncRadosRequest {
RGWRados *store;
RGWMetadataLog *mdlog;
int shard_id;
string *marker;
int max_entries;
list<cls_log_entry> *entries;
bool *truncated;
protected:
int _send_request() {
real_time from_time;
real_time end_time;
void *handle;
mdlog->init_list_entries(shard_id, from_time, end_time, *marker, &handle);
int ret = mdlog->list_entries(handle, max_entries, *entries, marker, truncated);
mdlog->complete_list_entries(handle);
return ret;
}
public:
RGWAsyncReadMDLogEntries(RGWCoroutine *caller, RGWAioCompletionNotifier *cn, RGWRados *_store,
RGWMetadataLog* mdlog, int _shard_id,
string* _marker, int _max_entries,
list<cls_log_entry> *_entries, bool *_truncated)
: RGWAsyncRadosRequest(caller, cn), store(_store), mdlog(mdlog),
shard_id(_shard_id), marker(_marker), max_entries(_max_entries),
entries(_entries), truncated(_truncated) {}
};
class RGWReadMDLogEntriesCR : public RGWSimpleCoroutine {
RGWMetaSyncEnv *sync_env;
RGWMetadataLog *const mdlog;
int shard_id;
string marker;
string *pmarker;
int max_entries;
list<cls_log_entry> *entries;
bool *truncated;
RGWAsyncReadMDLogEntries *req;
public:
RGWReadMDLogEntriesCR(RGWMetaSyncEnv *_sync_env, RGWMetadataLog* mdlog,
int _shard_id, string*_marker, int _max_entries,
list<cls_log_entry> *_entries, bool *_truncated)
: RGWSimpleCoroutine(_sync_env->cct), sync_env(_sync_env), mdlog(mdlog),
shard_id(_shard_id), pmarker(_marker), max_entries(_max_entries),
entries(_entries), truncated(_truncated) {}
~RGWReadMDLogEntriesCR() {
if (req) {
req->finish();
}
}
int send_request() {
marker = *pmarker;
req = new RGWAsyncReadMDLogEntries(this, stack->create_completion_notifier(),
sync_env->store, mdlog, shard_id, &marker,
max_entries, entries, truncated);
sync_env->async_rados->queue(req);
return 0;
}
int request_complete() {
int ret = req->get_ret_status();
if (ret >= 0 && !entries->empty()) {
*pmarker = marker;
}
return req->get_ret_status();
}
};
class RGWReadRemoteMDLogShardInfoCR : public RGWCoroutine {
RGWMetaSyncEnv *env;
RGWRESTReadResource *http_op;
const std::string& period;
int shard_id;
RGWMetadataLogInfo *shard_info;
public:
RGWReadRemoteMDLogShardInfoCR(RGWMetaSyncEnv *env, const std::string& period,
int _shard_id, RGWMetadataLogInfo *_shard_info)
: RGWCoroutine(env->store->ctx()), env(env), http_op(NULL),
period(period), shard_id(_shard_id), shard_info(_shard_info) {}
int operate() {
auto store = env->store;
RGWRESTConn *conn = store->rest_master_conn;
reenter(this) {
yield {
char buf[16];
snprintf(buf, sizeof(buf), "%d", shard_id);
rgw_http_param_pair pairs[] = { { "type" , "metadata" },
{ "id", buf },
{ "period", period.c_str() },
{ "info" , NULL },
{ NULL, NULL } };
string p = "/admin/log/";
http_op = new RGWRESTReadResource(conn, p, pairs, NULL,
env->http_manager);
http_op->set_user_info((void *)stack);
int ret = http_op->aio_read();
if (ret < 0) {
ldout(store->ctx(), 0) << "ERROR: failed to read from " << p << dendl;
log_error() << "failed to send http operation: " << http_op->to_str() << " ret=" << ret << std::endl;
http_op->put();
return set_cr_error(ret);
}
return io_block(0);
}
yield {
int ret = http_op->wait(shard_info);
if (ret < 0) {
return set_cr_error(ret);
}
return set_cr_done();
}
}
return 0;
}
};
class RGWListRemoteMDLogShardCR : public RGWSimpleCoroutine {
RGWMetaSyncEnv *sync_env;
RGWRESTReadResource *http_op;
const std::string& period;
int shard_id;
string marker;
uint32_t max_entries;
rgw_mdlog_shard_data *result;
public:
RGWListRemoteMDLogShardCR(RGWMetaSyncEnv *env, const std::string& period,
int _shard_id, const string& _marker, uint32_t _max_entries,
rgw_mdlog_shard_data *_result)
: RGWSimpleCoroutine(env->store->ctx()), sync_env(env), http_op(NULL),
period(period), shard_id(_shard_id), marker(_marker), max_entries(_max_entries), result(_result) {}
int send_request() {
RGWRESTConn *conn = sync_env->conn;
RGWRados *store = sync_env->store;
char buf[32];
snprintf(buf, sizeof(buf), "%d", shard_id);
char max_entries_buf[32];
snprintf(max_entries_buf, sizeof(max_entries_buf), "%d", (int)max_entries);
const char *marker_key = (marker.empty() ? "" : "marker");
rgw_http_param_pair pairs[] = { { "type", "metadata" },
{ "id", buf },
{ "period", period.c_str() },
{ "max-entries", max_entries_buf },
{ marker_key, marker.c_str() },
{ NULL, NULL } };
string p = "/admin/log/";
http_op = new RGWRESTReadResource(conn, p, pairs, NULL, sync_env->http_manager);
http_op->set_user_info((void *)stack);
int ret = http_op->aio_read();
if (ret < 0) {
ldout(store->ctx(), 0) << "ERROR: failed to read from " << p << dendl;
log_error() << "failed to send http operation: " << http_op->to_str() << " ret=" << ret << std::endl;
http_op->put();
return ret;
}
return 0;
}
int request_complete() {
int ret = http_op->wait(result);
if (ret < 0 && ret != -ENOENT) {
ldout(sync_env->store->ctx(), 0) << "ERROR: failed to list remote mdlog shard, ret=" << ret << dendl;
return ret;
}
return 0;
}
};
bool RGWReadRemoteMDLogInfoCR::spawn_next() {
if (shard_id >= num_shards) {
return false;
}
spawn(new RGWReadRemoteMDLogShardInfoCR(sync_env, period, shard_id, &(*mdlog_info)[shard_id]), false);
shard_id++;
return true;
}
bool RGWListRemoteMDLogCR::spawn_next() {
if (iter == shards.end()) {
return false;
}
spawn(new RGWListRemoteMDLogShardCR(sync_env, period, iter->first, iter->second, max_entries_per_shard, &(*result)[iter->first]), false);
++iter;
return true;
}
class RGWInitSyncStatusCoroutine : public RGWCoroutine {
RGWMetaSyncEnv *sync_env;
RGWObjectCtx& obj_ctx;
rgw_meta_sync_info status;
vector<RGWMetadataLogInfo> shards_info;
RGWContinuousLeaseCR *lease_cr;
public:
RGWInitSyncStatusCoroutine(RGWMetaSyncEnv *_sync_env,
RGWObjectCtx& _obj_ctx,
const rgw_meta_sync_info &status)
: RGWCoroutine(_sync_env->store->ctx()), sync_env(_sync_env),
obj_ctx(_obj_ctx), status(status), shards_info(status.num_shards),
lease_cr(NULL) {}
~RGWInitSyncStatusCoroutine() {
if (lease_cr) {
lease_cr->abort();
lease_cr->put();
}
}
int operate() {
int ret;
reenter(this) {
yield {
set_status("acquiring sync lock");
uint32_t lock_duration = cct->_conf->rgw_sync_lease_period;
string lock_name = "sync_lock";
RGWRados *store = sync_env->store;
lease_cr = new RGWContinuousLeaseCR(sync_env->async_rados, store, store->get_zone_params().log_pool, sync_env->status_oid(),
lock_name, lock_duration, this);
lease_cr->get();
spawn(lease_cr, false);
}
while (!lease_cr->is_locked()) {
if (lease_cr->is_done()) {
ldout(cct, 0) << "ERROR: lease cr failed, done early " << dendl;
set_status("lease lock failed, early abort");
return set_cr_error(lease_cr->get_ret_status());
}
set_sleeping(true);
yield;
}
yield {
set_status("writing sync status");
RGWRados *store = sync_env->store;
call(new RGWSimpleRadosWriteCR<rgw_meta_sync_info>(sync_env->async_rados, store, store->get_zone_params().log_pool,
sync_env->status_oid(), status));
}
if (retcode < 0) {
set_status("failed to write sync status");
ldout(cct, 0) << "ERROR: failed to write sync status, retcode=" << retcode << dendl;
yield lease_cr->go_down();
return set_cr_error(retcode);
}
/* fetch current position in logs */
set_status("fetching remote log position");
yield {
for (int i = 0; i < (int)status.num_shards; i++) {
spawn(new RGWReadRemoteMDLogShardInfoCR(sync_env, status.period, i,
&shards_info[i]), false);
}
}
drain_all_but(1); /* the lease cr still needs to run */
yield {
set_status("updating sync status");
for (int i = 0; i < (int)status.num_shards; i++) {
rgw_meta_sync_marker marker;
RGWMetadataLogInfo& info = shards_info[i];
marker.next_step_marker = info.marker;
marker.timestamp = info.last_update;
RGWRados *store = sync_env->store;
spawn(new RGWSimpleRadosWriteCR<rgw_meta_sync_marker>(sync_env->async_rados, store, store->get_zone_params().log_pool,
sync_env->shard_obj_name(i), marker), true);
}
}
yield {
set_status("changing sync state: build full sync maps");
status.state = rgw_meta_sync_info::StateBuildingFullSyncMaps;
RGWRados *store = sync_env->store;
call(new RGWSimpleRadosWriteCR<rgw_meta_sync_info>(sync_env->async_rados, store, store->get_zone_params().log_pool,
sync_env->status_oid(), status));
}
set_status("drop lock lease");
yield lease_cr->go_down();
while (collect(&ret)) {
if (ret < 0) {
return set_cr_error(ret);
}
yield;
}
drain_all();
return set_cr_done();
}
return 0;
}
};
class RGWReadSyncStatusCoroutine : public RGWSimpleRadosReadCR<rgw_meta_sync_info> {
RGWMetaSyncEnv *sync_env;
RGWObjectCtx& obj_ctx;
rgw_meta_sync_status *sync_status;
public:
RGWReadSyncStatusCoroutine(RGWMetaSyncEnv *_sync_env,
RGWObjectCtx& _obj_ctx,
rgw_meta_sync_status *_status) : RGWSimpleRadosReadCR(_sync_env->async_rados, _sync_env->store, _obj_ctx,
_sync_env->store->get_zone_params().log_pool,
_sync_env->status_oid(),
&_status->sync_info),
sync_env(_sync_env),
obj_ctx(_obj_ctx),
sync_status(_status) {
}
int handle_data(rgw_meta_sync_info& data);
};
int RGWReadSyncStatusCoroutine::handle_data(rgw_meta_sync_info& data)
{
if (retcode == -ENOENT) {
return 0;
}
RGWRados *store = sync_env->store;
map<uint32_t, rgw_meta_sync_marker>& markers = sync_status->sync_markers;
for (int i = 0; i < (int)data.num_shards; i++) {
spawn(new RGWSimpleRadosReadCR<rgw_meta_sync_marker>(sync_env->async_rados, store, obj_ctx, store->get_zone_params().log_pool,
sync_env->shard_obj_name(i), &markers[i]), true);
}
return 0;
}
class RGWFetchAllMetaCR : public RGWCoroutine {
RGWMetaSyncEnv *sync_env;
int num_shards;
int ret_status;
list<string> sections;
list<string>::iterator sections_iter;
list<string> result;
list<string>::iterator iter;
RGWShardedOmapCRManager *entries_index;
RGWContinuousLeaseCR *lease_cr;
bool lost_lock;
bool failed;
map<uint32_t, rgw_meta_sync_marker>& markers;
public:
RGWFetchAllMetaCR(RGWMetaSyncEnv *_sync_env, int _num_shards,
map<uint32_t, rgw_meta_sync_marker>& _markers) : RGWCoroutine(_sync_env->cct), sync_env(_sync_env),
num_shards(_num_shards),
ret_status(0), entries_index(NULL), lease_cr(NULL), lost_lock(false), failed(false), markers(_markers) {
}
~RGWFetchAllMetaCR() {
if (lease_cr) {
lease_cr->put();
}
}
void append_section_from_set(set<string>& all_sections, const string& name) {
set<string>::iterator iter = all_sections.find(name);
if (iter != all_sections.end()) {
sections.emplace_back(std::move(*iter));
all_sections.erase(iter);
}
}
/*
* meta sync should go in the following order: user, bucket.instance, bucket
* then whatever other sections exist (if any)
*/
void rearrange_sections() {
set<string> all_sections;
std::move(sections.begin(), sections.end(),
std::inserter(all_sections, all_sections.end()));
sections.clear();
append_section_from_set(all_sections, "user");
append_section_from_set(all_sections, "bucket.instance");
append_section_from_set(all_sections, "bucket");
std::move(all_sections.begin(), all_sections.end(),
std::back_inserter(sections));
}
int operate() {
RGWRESTConn *conn = sync_env->conn;
reenter(this) {
yield {
set_status(string("acquiring lock (") + sync_env->status_oid() + ")");
uint32_t lock_duration = cct->_conf->rgw_sync_lease_period;
string lock_name = "sync_lock";
lease_cr = new RGWContinuousLeaseCR(sync_env->async_rados, sync_env->store, sync_env->store->get_zone_params().log_pool, sync_env->status_oid(),
lock_name, lock_duration, this);
lease_cr->get();
spawn(lease_cr, false);
}
while (!lease_cr->is_locked()) {
if (lease_cr->is_done()) {
ldout(cct, 0) << "ERROR: lease cr failed, done early " << dendl;
set_status("failed acquiring lock");
return set_cr_error(lease_cr->get_ret_status());
}
set_sleeping(true);
yield;
}
entries_index = new RGWShardedOmapCRManager(sync_env->async_rados, sync_env->store, this, num_shards,
sync_env->store->get_zone_params().log_pool,
mdlog_sync_full_sync_index_prefix);
yield {
call(new RGWReadRESTResourceCR<list<string> >(cct, conn, sync_env->http_manager,
"/admin/metadata", NULL, §ions));
}
if (get_ret_status() < 0) {
ldout(cct, 0) << "ERROR: failed to fetch metadata sections" << dendl;
yield lease_cr->go_down();
drain_all();
return set_cr_error(get_ret_status());
}
rearrange_sections();
sections_iter = sections.begin();
for (; sections_iter != sections.end(); ++sections_iter) {
yield {
string entrypoint = string("/admin/metadata/") + *sections_iter;
/* FIXME: need a better scaling solution here, requires streaming output */
call(new RGWReadRESTResourceCR<list<string> >(cct, conn, sync_env->http_manager,
entrypoint, NULL, &result));
}
if (get_ret_status() < 0) {
ldout(cct, 0) << "ERROR: failed to fetch metadata section: " << *sections_iter << dendl;
yield lease_cr->go_down();
drain_all();
return set_cr_error(get_ret_status());
}
iter = result.begin();
for (; iter != result.end(); ++iter) {
RGWRados *store;
int ret;
yield {
if (!lease_cr->is_locked()) {
lost_lock = true;
break;
}
ldout(cct, 20) << "list metadata: section=" << *sections_iter << " key=" << *iter << dendl;
string s = *sections_iter + ":" + *iter;
int shard_id;
store = sync_env->store;
ret = store->meta_mgr->get_log_shard_id(*sections_iter, *iter, &shard_id);
if (ret < 0) {
ldout(cct, 0) << "ERROR: could not determine shard id for " << *sections_iter << ":" << *iter << dendl;
ret_status = ret;
break;
}
if (!entries_index->append(s, shard_id)) {
break;
}
}
}
}
yield {
if (!entries_index->finish()) {
failed = true;
}
}
if (!failed) {
for (map<uint32_t, rgw_meta_sync_marker>::iterator iter = markers.begin(); iter != markers.end(); ++iter) {
int shard_id = (int)iter->first;
rgw_meta_sync_marker& marker = iter->second;
marker.total_entries = entries_index->get_total_entries(shard_id);
spawn(new RGWSimpleRadosWriteCR<rgw_meta_sync_marker>(sync_env->async_rados, sync_env->store, sync_env->store->get_zone_params().log_pool,
sync_env->shard_obj_name(shard_id), marker), true);
}
}
drain_all_but(1); /* the lease cr still needs to run */
yield lease_cr->go_down();
int ret;
while (collect(&ret)) {
if (ret < 0) {
return set_cr_error(ret);
}
yield;
}
drain_all();
if (failed) {
yield return set_cr_error(-EIO);
}
if (lost_lock) {
yield return set_cr_error(-EBUSY);
}
if (ret_status < 0) {
yield return set_cr_error(ret_status);
}
yield return set_cr_done();
}
return 0;
}
};
static string full_sync_index_shard_oid(int shard_id)
{
char buf[mdlog_sync_full_sync_index_prefix.size() + 16];
snprintf(buf, sizeof(buf), "%s.%d", mdlog_sync_full_sync_index_prefix.c_str(), shard_id);
return string(buf);
}
class RGWReadRemoteMetadataCR : public RGWCoroutine {
RGWMetaSyncEnv *sync_env;
RGWRESTReadResource *http_op;
string section;
string key;
bufferlist *pbl;
public:
RGWReadRemoteMetadataCR(RGWMetaSyncEnv *_sync_env,
const string& _section, const string& _key, bufferlist *_pbl) : RGWCoroutine(_sync_env->cct), sync_env(_sync_env),
http_op(NULL),
section(_section),
key(_key),
pbl(_pbl) {
}
int operate() {
RGWRESTConn *conn = sync_env->conn;
reenter(this) {
yield {
rgw_http_param_pair pairs[] = { { "key" , key.c_str()},
{ NULL, NULL } };
string p = string("/admin/metadata/") + section + "/" + key;
http_op = new RGWRESTReadResource(conn, p, pairs, NULL, sync_env->http_manager);
http_op->set_user_info((void *)stack);
int ret = http_op->aio_read();
if (ret < 0) {
ldout(sync_env->cct, 0) << "ERROR: failed to fetch mdlog data" << dendl;
log_error() << "failed to send http operation: " << http_op->to_str() << " ret=" << ret << std::endl;
http_op->put();
return set_cr_error(ret);
}
return io_block(0);
}
yield {
int ret = http_op->wait_bl(pbl);
if (ret < 0) {
return set_cr_error(ret);
}
return set_cr_done();
}
}
return 0;
}
};
class RGWAsyncMetaStoreEntry : public RGWAsyncRadosRequest {
RGWRados *store;
string raw_key;
bufferlist bl;
protected:
int _send_request() {
int ret = store->meta_mgr->put(raw_key, bl, RGWMetadataHandler::APPLY_ALWAYS);
if (ret < 0) {
ldout(store->ctx(), 0) << "ERROR: can't store key: " << raw_key << " ret=" << ret << dendl;
return ret;
}
return 0;
}
public:
RGWAsyncMetaStoreEntry(RGWCoroutine *caller, RGWAioCompletionNotifier *cn, RGWRados *_store,
const string& _raw_key,
bufferlist& _bl) : RGWAsyncRadosRequest(caller, cn), store(_store),
raw_key(_raw_key), bl(_bl) {}
};
class RGWMetaStoreEntryCR : public RGWSimpleCoroutine {
RGWMetaSyncEnv *sync_env;
string raw_key;
bufferlist bl;
RGWAsyncMetaStoreEntry *req;
public:
RGWMetaStoreEntryCR(RGWMetaSyncEnv *_sync_env,
const string& _raw_key,
bufferlist& _bl) : RGWSimpleCoroutine(_sync_env->cct), sync_env(_sync_env),
raw_key(_raw_key), bl(_bl), req(NULL) {
}