/
rgw_data_sync.cc
2566 lines (2192 loc) · 92.1 KB
/
rgw_data_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
#include "common/ceph_json.h"
#include "common/RWLock.h"
#include "common/RefCountedObj.h"
#include "common/WorkQueue.h"
#include "common/Throttle.h"
#include "common/errno.h"
#include "rgw_common.h"
#include "rgw_rados.h"
#include "rgw_sync.h"
#include "rgw_data_sync.h"
#include "rgw_rest_conn.h"
#include "rgw_cr_rados.h"
#include "rgw_cr_rest.h"
#include "rgw_http_client.h"
#include "rgw_bucket.h"
#include "rgw_metadata.h"
#include "rgw_boost_asio_yield.h"
#include "cls/lock/cls_lock_client.h"
#define dout_subsys ceph_subsys_rgw
static string datalog_sync_status_oid_prefix = "datalog.sync-status";
static string datalog_sync_status_shard_prefix = "datalog.sync-status.shard";
static string datalog_sync_full_sync_index_prefix = "data.full-sync.index";
static string bucket_status_oid_prefix = "bucket.sync-status";
void rgw_datalog_info::decode_json(JSONObj *obj) {
JSONDecoder::decode_json("num_objects", num_shards, obj);
}
void rgw_datalog_entry::decode_json(JSONObj *obj) {
JSONDecoder::decode_json("key", key, obj);
utime_t ut;
JSONDecoder::decode_json("timestamp", ut, obj);
timestamp = ut.to_real_time();
}
void rgw_datalog_shard_data::decode_json(JSONObj *obj) {
JSONDecoder::decode_json("marker", marker, obj);
JSONDecoder::decode_json("truncated", truncated, obj);
JSONDecoder::decode_json("entries", entries, obj);
};
class RGWReadDataSyncStatusCoroutine : public RGWSimpleRadosReadCR<rgw_data_sync_info> {
RGWDataSyncEnv *sync_env;
rgw_data_sync_status *sync_status;
public:
RGWReadDataSyncStatusCoroutine(RGWDataSyncEnv *_sync_env,
rgw_data_sync_status *_status) : RGWSimpleRadosReadCR(_sync_env->async_rados, _sync_env->store,
_sync_env->store->get_zone_params().log_pool,
RGWDataSyncStatusManager::sync_status_oid(_sync_env->source_zone),
&_status->sync_info),
sync_env(_sync_env),
sync_status(_status) {}
int handle_data(rgw_data_sync_info& data);
};
int RGWReadDataSyncStatusCoroutine::handle_data(rgw_data_sync_info& data)
{
if (retcode == -ENOENT) {
return retcode;
}
map<uint32_t, rgw_data_sync_marker>& markers = sync_status->sync_markers;
RGWRados *store = sync_env->store;
for (int i = 0; i < (int)data.num_shards; i++) {
spawn(new RGWSimpleRadosReadCR<rgw_data_sync_marker>(sync_env->async_rados, store, store->get_zone_params().log_pool,
RGWDataSyncStatusManager::shard_obj_name(sync_env->source_zone, i), &markers[i]), true);
}
return 0;
}
class RGWReadRemoteDataLogShardInfoCR : public RGWCoroutine {
RGWDataSyncEnv *sync_env;
RGWRESTReadResource *http_op;
int shard_id;
RGWDataChangesLogInfo *shard_info;
public:
RGWReadRemoteDataLogShardInfoCR(RGWDataSyncEnv *_sync_env,
int _shard_id, RGWDataChangesLogInfo *_shard_info) : RGWCoroutine(_sync_env->cct),
sync_env(_sync_env),
http_op(NULL),
shard_id(_shard_id),
shard_info(_shard_info) {
}
~RGWReadRemoteDataLogShardInfoCR() {
if (http_op) {
http_op->put();
}
}
int operate() {
reenter(this) {
yield {
char buf[16];
snprintf(buf, sizeof(buf), "%d", shard_id);
rgw_http_param_pair pairs[] = { { "type" , "data" },
{ "id", buf },
{ "info" , NULL },
{ NULL, NULL } };
string p = "/admin/log/";
http_op = new RGWRESTReadResource(sync_env->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 read from " << p << dendl;
log_error() << "failed to send http operation: " << http_op->to_str() << " ret=" << ret << std::endl;
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;
}
};
struct read_remote_data_log_response {
string marker;
bool truncated;
list<rgw_data_change_log_entry> entries;
read_remote_data_log_response() : truncated(false) {}
void decode_json(JSONObj *obj) {
JSONDecoder::decode_json("marker", marker, obj);
JSONDecoder::decode_json("truncated", truncated, obj);
JSONDecoder::decode_json("entries", entries, obj);
};
};
class RGWReadRemoteDataLogShardCR : public RGWCoroutine {
RGWDataSyncEnv *sync_env;
RGWRESTReadResource *http_op;
int shard_id;
string *pmarker;
list<rgw_data_change_log_entry> *entries;
bool *truncated;
read_remote_data_log_response response;
public:
RGWReadRemoteDataLogShardCR(RGWDataSyncEnv *_sync_env,
int _shard_id, string *_pmarker, list<rgw_data_change_log_entry> *_entries, bool *_truncated) : RGWCoroutine(_sync_env->cct),
sync_env(_sync_env),
http_op(NULL),
shard_id(_shard_id),
pmarker(_pmarker),
entries(_entries),
truncated(_truncated) {
if (http_op) {
http_op->put();
}
}
int operate() {
reenter(this) {
yield {
char buf[16];
snprintf(buf, sizeof(buf), "%d", shard_id);
rgw_http_param_pair pairs[] = { { "type" , "data" },
{ "id", buf },
{ "marker", pmarker->c_str() },
{ "extra-info", "true" },
{ NULL, NULL } };
string p = "/admin/log/";
http_op = new RGWRESTReadResource(sync_env->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 read from " << p << dendl;
log_error() << "failed to send http operation: " << http_op->to_str() << " ret=" << ret << std::endl;
return set_cr_error(ret);
}
return io_block(0);
}
yield {
int ret = http_op->wait(&response);
if (ret < 0) {
return set_cr_error(ret);
}
entries->clear();
entries->swap(response.entries);
*pmarker = response.marker;
*truncated = response.truncated;
return set_cr_done();
}
}
return 0;
}
};
class RGWReadRemoteDataLogInfoCR : public RGWShardCollectCR {
RGWDataSyncEnv *sync_env;
int num_shards;
map<int, RGWDataChangesLogInfo> *datalog_info;
int shard_id;
#define READ_DATALOG_MAX_CONCURRENT 10
public:
RGWReadRemoteDataLogInfoCR(RGWDataSyncEnv *_sync_env,
int _num_shards,
map<int, RGWDataChangesLogInfo> *_datalog_info) : RGWShardCollectCR(_sync_env->cct, READ_DATALOG_MAX_CONCURRENT),
sync_env(_sync_env), num_shards(_num_shards),
datalog_info(_datalog_info), shard_id(0) {}
bool spawn_next();
};
bool RGWReadRemoteDataLogInfoCR::spawn_next() {
if (shard_id >= num_shards) {
return false;
}
spawn(new RGWReadRemoteDataLogShardInfoCR(sync_env, shard_id, &(*datalog_info)[shard_id]), false);
shard_id++;
return true;
}
class RGWListRemoteDataLogShardCR : public RGWSimpleCoroutine {
RGWDataSyncEnv *sync_env;
RGWRESTReadResource *http_op;
int shard_id;
string marker;
uint32_t max_entries;
rgw_datalog_shard_data *result;
public:
RGWListRemoteDataLogShardCR(RGWDataSyncEnv *env, int _shard_id,
const string& _marker, uint32_t _max_entries,
rgw_datalog_shard_data *_result)
: RGWSimpleCoroutine(env->store->ctx()), sync_env(env), http_op(NULL),
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", "data" },
{ "id", buf },
{ "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 datalog shard, ret=" << ret << dendl;
return ret;
}
return 0;
}
};
class RGWListRemoteDataLogCR : public RGWShardCollectCR {
RGWDataSyncEnv *sync_env;
map<int, string> shards;
int max_entries_per_shard;
map<int, rgw_datalog_shard_data> *result;
map<int, string>::iterator iter;
#define READ_DATALOG_MAX_CONCURRENT 10
public:
RGWListRemoteDataLogCR(RGWDataSyncEnv *_sync_env,
map<int, string>& _shards,
int _max_entries_per_shard,
map<int, rgw_datalog_shard_data> *_result) : RGWShardCollectCR(_sync_env->cct, READ_DATALOG_MAX_CONCURRENT),
sync_env(_sync_env), max_entries_per_shard(_max_entries_per_shard),
result(_result) {
shards.swap(_shards);
iter = shards.begin();
}
bool spawn_next();
};
bool RGWListRemoteDataLogCR::spawn_next() {
if (iter == shards.end()) {
return false;
}
spawn(new RGWListRemoteDataLogShardCR(sync_env, iter->first, iter->second, max_entries_per_shard, &(*result)[iter->first]), false);
++iter;
return true;
}
class RGWInitDataSyncStatusCoroutine : public RGWCoroutine {
RGWDataSyncEnv *sync_env;
RGWRados *store;
string sync_status_oid;
string lock_name;
string cookie;
rgw_data_sync_info status;
map<int, RGWDataChangesLogInfo> shards_info;
public:
RGWInitDataSyncStatusCoroutine(RGWDataSyncEnv *_sync_env,
uint32_t _num_shards) : RGWCoroutine(_sync_env->cct),
sync_env(_sync_env), store(sync_env->store) {
lock_name = "sync_lock";
status.num_shards = _num_shards;
#define COOKIE_LEN 16
char buf[COOKIE_LEN + 1];
gen_rand_alphanumeric(cct, buf, sizeof(buf) - 1);
string cookie = buf;
sync_status_oid = RGWDataSyncStatusManager::sync_status_oid(sync_env->source_zone);
}
int operate() {
int ret;
reenter(this) {
yield {
uint32_t lock_duration = 30;
call(new RGWSimpleRadosLockCR(sync_env->async_rados, store, store->get_zone_params().log_pool, sync_status_oid,
lock_name, cookie, lock_duration));
if (retcode < 0) {
ldout(cct, 0) << "ERROR: failed to take a lock on " << sync_status_oid << dendl;
return set_cr_error(retcode);
}
}
yield {
call(new RGWSimpleRadosWriteCR<rgw_data_sync_info>(sync_env->async_rados, store, store->get_zone_params().log_pool,
sync_status_oid, status));
}
yield { /* take lock again, we just recreated the object */
uint32_t lock_duration = 30;
call(new RGWSimpleRadosLockCR(sync_env->async_rados, store, store->get_zone_params().log_pool, sync_status_oid,
lock_name, cookie, lock_duration));
if (retcode < 0) {
ldout(cct, 0) << "ERROR: failed to take a lock on " << sync_status_oid << dendl;
return set_cr_error(retcode);
}
}
/* fetch current position in logs */
yield {
RGWRESTConn *conn = store->get_zone_conn_by_id(sync_env->source_zone);
if (!conn) {
ldout(cct, 0) << "ERROR: connection to zone " << sync_env->source_zone << " does not exist!" << dendl;
return set_cr_error(-EIO);
}
for (int i = 0; i < (int)status.num_shards; i++) {
spawn(new RGWReadRemoteDataLogShardInfoCR(sync_env, i, &shards_info[i]), true);
}
}
while (collect(&ret)) {
if (ret < 0) {
return set_state(RGWCoroutine_Error);
}
yield;
}
yield {
for (int i = 0; i < (int)status.num_shards; i++) {
rgw_data_sync_marker marker;
RGWDataChangesLogInfo& info = shards_info[i];
marker.next_step_marker = info.marker;
marker.timestamp = info.last_update;
spawn(new RGWSimpleRadosWriteCR<rgw_data_sync_marker>(sync_env->async_rados, store, store->get_zone_params().log_pool,
RGWDataSyncStatusManager::shard_obj_name(sync_env->source_zone, i), marker), true);
}
}
yield {
status.state = rgw_data_sync_info::StateBuildingFullSyncMaps;
call(new RGWSimpleRadosWriteCR<rgw_data_sync_info>(sync_env->async_rados, store, store->get_zone_params().log_pool,
sync_status_oid, status));
}
yield { /* unlock */
call(new RGWSimpleRadosUnlockCR(sync_env->async_rados, store, store->get_zone_params().log_pool, sync_status_oid,
lock_name, cookie));
}
while (collect(&ret)) {
if (ret < 0) {
return set_state(RGWCoroutine_Error);
}
yield;
}
drain_all();
return set_cr_done();
}
return 0;
}
};
int RGWRemoteDataLog::read_log_info(rgw_datalog_info *log_info)
{
rgw_http_param_pair pairs[] = { { "type", "data" },
{ NULL, NULL } };
int ret = sync_env.conn->get_json_resource("/admin/log", pairs, *log_info);
if (ret < 0) {
ldout(store->ctx(), 0) << "ERROR: failed to fetch datalog info" << dendl;
return ret;
}
ldout(store->ctx(), 20) << "remote datalog, num_shards=" << log_info->num_shards << dendl;
return 0;
}
int RGWRemoteDataLog::read_source_log_shards_info(map<int, RGWDataChangesLogInfo> *shards_info)
{
rgw_datalog_info log_info;
int ret = read_log_info(&log_info);
if (ret < 0) {
return ret;
}
return run(new RGWReadRemoteDataLogInfoCR(&sync_env, log_info.num_shards, shards_info));
}
int RGWRemoteDataLog::read_source_log_shards_next(map<int, string> shard_markers, map<int, rgw_datalog_shard_data> *result)
{
if (store->is_meta_master()) {
return 0;
}
return run(new RGWListRemoteDataLogCR(&sync_env, shard_markers, 1, result));
}
int RGWRemoteDataLog::init(const string& _source_zone, RGWRESTConn *_conn, RGWSyncErrorLogger *_error_logger)
{
if (initialized) {
return 0;
}
sync_env.init(store->ctx(), store, _conn, async_rados, &http_manager, _error_logger, _source_zone);
int ret = http_manager.set_threaded();
if (ret < 0) {
ldout(store->ctx(), 0) << "failed in http_manager.set_threaded() ret=" << ret << dendl;
return ret;
}
initialized = true;
return 0;
}
void RGWRemoteDataLog::finish()
{
stop();
}
int RGWRemoteDataLog::get_shard_info(int shard_id)
{
char buf[32];
snprintf(buf, sizeof(buf), "%d", shard_id);
rgw_http_param_pair pairs[] = { { "type", "data" },
{ "id", buf },
{ "info", NULL },
{ NULL, NULL } };
RGWDataChangesLogInfo info;
int ret = sync_env.conn->get_json_resource("/admin/log", pairs, info);
if (ret < 0) {
ldout(store->ctx(), 0) << "ERROR: failed to fetch datalog info" << dendl;
return ret;
}
ldout(store->ctx(), 20) << "remote datalog, shard_id=" << shard_id << " marker=" << info.marker << dendl;
return 0;
}
int RGWRemoteDataLog::read_sync_status(rgw_data_sync_status *sync_status)
{
int r = run(new RGWReadDataSyncStatusCoroutine(&sync_env, sync_status));
if (r == -ENOENT) {
r = 0;
}
return r;
}
int RGWRemoteDataLog::init_sync_status(int num_shards)
{
return run(new RGWInitDataSyncStatusCoroutine(&sync_env, num_shards));
}
static string full_data_sync_index_shard_oid(const string& source_zone, int shard_id)
{
char buf[datalog_sync_full_sync_index_prefix.size() + 1 + source_zone.size() + 1 + 16];
snprintf(buf, sizeof(buf), "%s.%s.%d", datalog_sync_full_sync_index_prefix.c_str(), source_zone.c_str(), shard_id);
return string(buf);
}
struct bucket_instance_meta_info {
string key;
obj_version ver;
utime_t mtime;
RGWBucketInstanceMetadataObject data;
bucket_instance_meta_info() {}
void decode_json(JSONObj *obj) {
JSONDecoder::decode_json("key", key, obj);
JSONDecoder::decode_json("ver", ver, obj);
JSONDecoder::decode_json("mtime", mtime, obj);
JSONDecoder::decode_json("data", data, obj);
}
};
class RGWListBucketIndexesCR : public RGWCoroutine {
RGWDataSyncEnv *sync_env;
RGWRados *store;
rgw_data_sync_status *sync_status;
int num_shards;
int req_ret;
int ret;
list<string> result;
list<string>::iterator iter;
RGWShardedOmapCRManager *entries_index;
string oid_prefix;
string path;
bucket_instance_meta_info meta_info;
string key;
string s;
int i;
bool failed;
public:
RGWListBucketIndexesCR(RGWDataSyncEnv *_sync_env,
rgw_data_sync_status *_sync_status) : RGWCoroutine(_sync_env->cct), sync_env(_sync_env),
store(sync_env->store), sync_status(_sync_status),
req_ret(0), ret(0), entries_index(NULL), i(0), failed(false) {
oid_prefix = datalog_sync_full_sync_index_prefix + "." + sync_env->source_zone;
path = "/admin/metadata/bucket.instance";
num_shards = sync_status->sync_info.num_shards;
}
~RGWListBucketIndexesCR() {
delete entries_index;
}
int operate() {
reenter(this) {
entries_index = new RGWShardedOmapCRManager(sync_env->async_rados, store, this, num_shards,
store->get_zone_params().log_pool,
oid_prefix);
yield {
string entrypoint = string("/admin/metadata/bucket.instance");
/* FIXME: need a better scaling solution here, requires streaming output */
call(new RGWReadRESTResourceCR<list<string> >(store->ctx(), sync_env->conn, sync_env->http_manager,
entrypoint, NULL, &result));
}
if (get_ret_status() < 0) {
ldout(sync_env->cct, 0) << "ERROR: failed to fetch metadata for section bucket.index" << dendl;
return set_state(RGWCoroutine_Error);
}
for (iter = result.begin(); iter != result.end(); ++iter) {
ldout(sync_env->cct, 20) << "list metadata: section=bucket.index key=" << *iter << dendl;
key = *iter;
yield {
rgw_http_param_pair pairs[] = { { "key", key.c_str() },
{ NULL, NULL } };
call(new RGWReadRESTResourceCR<bucket_instance_meta_info>(store->ctx(), sync_env->conn, sync_env->http_manager, path, pairs, &meta_info));
}
num_shards = meta_info.data.get_bucket_info().num_shards;
if (num_shards > 0) {
for (i = 0; i < num_shards; i++) {
char buf[16];
snprintf(buf, sizeof(buf), ":%d", i);
s = key + buf;
yield entries_index->append(s, store->data_log->get_log_shard_id(meta_info.data.get_bucket_info().bucket, i));
}
} else {
yield entries_index->append(key, store->data_log->get_log_shard_id(meta_info.data.get_bucket_info().bucket, -1));
}
}
yield {
if (!entries_index->finish()) {
failed = true;
}
}
if (!failed) {
for (map<uint32_t, rgw_data_sync_marker>::iterator iter = sync_status->sync_markers.begin(); iter != sync_status->sync_markers.end(); ++iter) {
int shard_id = (int)iter->first;
rgw_data_sync_marker& marker = iter->second;
marker.total_entries = entries_index->get_total_entries(shard_id);
spawn(new RGWSimpleRadosWriteCR<rgw_data_sync_marker>(sync_env->async_rados, store, store->get_zone_params().log_pool,
RGWDataSyncStatusManager::shard_obj_name(sync_env->source_zone, shard_id), marker), true);
}
} else {
yield call(sync_env->error_logger->log_error_cr(sync_env->conn->get_remote_id(), "data.init", "",
EIO, string("failed to build bucket instances map")));
}
while (collect(&ret)) {
if (ret < 0) {
yield call(sync_env->error_logger->log_error_cr(sync_env->conn->get_remote_id(), "data.init", "",
-ret, string("failed to store sync status: ") + cpp_strerror(-ret)));
req_ret = ret;
}
yield;
}
drain_all();
if (req_ret < 0) {
yield return set_cr_error(req_ret);
}
yield return set_cr_done();
}
return 0;
}
};
#define DATA_SYNC_UPDATE_MARKER_WINDOW 1
class RGWDataSyncShardMarkerTrack : public RGWSyncShardMarkerTrack<string, string> {
RGWDataSyncEnv *sync_env;
string marker_oid;
rgw_data_sync_marker sync_marker;
map<string, string> key_to_marker;
map<string, string> marker_to_key;
void handle_finish(const string& marker) {
map<string, string>::iterator iter = marker_to_key.find(marker);
if (iter == marker_to_key.end()) {
return;
}
key_to_marker.erase(iter->second);
reset_need_retry(iter->second);
marker_to_key.erase(iter);
}
public:
RGWDataSyncShardMarkerTrack(RGWDataSyncEnv *_sync_env,
const string& _marker_oid,
const rgw_data_sync_marker& _marker) : RGWSyncShardMarkerTrack(DATA_SYNC_UPDATE_MARKER_WINDOW),
sync_env(_sync_env),
marker_oid(_marker_oid),
sync_marker(_marker) {}
RGWCoroutine *store_marker(const string& new_marker, uint64_t index_pos, const real_time& timestamp) {
sync_marker.marker = new_marker;
sync_marker.pos = index_pos;
ldout(sync_env->cct, 20) << __func__ << "(): updating marker marker_oid=" << marker_oid << " marker=" << new_marker << dendl;
RGWRados *store = sync_env->store;
return new RGWSimpleRadosWriteCR<rgw_data_sync_marker>(sync_env->async_rados, store, store->get_zone_params().log_pool,
marker_oid, sync_marker);
}
/*
* create index from key -> marker, and from marker -> key
* this is useful so that we can insure that we only have one
* entry for any key that is used. This is needed when doing
* incremenatl sync of data, and we don't want to run multiple
* concurrent sync operations for the same bucket shard
*/
bool index_key_to_marker(const string& key, const string& marker) {
if (key_to_marker.find(key) != key_to_marker.end()) {
set_need_retry(key);
return false;
}
key_to_marker[key] = marker;
marker_to_key[marker] = key;
return true;
}
};
class RGWRunBucketSyncCoroutine : public RGWCoroutine {
RGWDataSyncEnv *sync_env;
string bucket_name;
string bucket_id;
RGWBucketInfo bucket_info;
int shard_id;
rgw_bucket_shard_sync_info sync_status;
RGWMetaSyncEnv meta_sync_env;
public:
RGWRunBucketSyncCoroutine(RGWDataSyncEnv *_sync_env,
const string& _bucket_name, const string _bucket_id, int _shard_id) : RGWCoroutine(_sync_env->cct),
sync_env(_sync_env),
bucket_name(_bucket_name),
bucket_id(_bucket_id), shard_id(_shard_id) {}
int operate();
};
static int parse_bucket_shard(CephContext *cct, const string& raw_key, string *bucket_name, string *bucket_instance, int *shard_id)
{
ssize_t pos = raw_key.find(':');
*bucket_name = raw_key.substr(0, pos);
*bucket_instance = raw_key.substr(pos + 1);
pos = bucket_instance->find(':');
*shard_id = -1;
if (pos >= 0) {
string err;
string s = bucket_instance->substr(pos + 1);
*shard_id = strict_strtol(s.c_str(), 10, &err);
if (!err.empty()) {
ldout(cct, 0) << "ERROR: failed to parse bucket instance key: " << *bucket_instance << dendl;
return -EINVAL;
}
*bucket_instance = bucket_instance->substr(0, pos);
}
return 0;
}
class RGWDataSyncSingleEntryCR : public RGWCoroutine {
RGWDataSyncEnv *sync_env;
string raw_key;
string entry_marker;
string bucket_name;
string bucket_instance;
int sync_status;
bufferlist md_bl;
RGWDataSyncShardMarkerTrack *marker_tracker;
public:
RGWDataSyncSingleEntryCR(RGWDataSyncEnv *_sync_env,
const string& _raw_key, const string& _entry_marker, RGWDataSyncShardMarkerTrack *_marker_tracker) : RGWCoroutine(_sync_env->cct),
sync_env(_sync_env),
raw_key(_raw_key), entry_marker(_entry_marker),
sync_status(0),
marker_tracker(_marker_tracker) {
set_description() << "data sync single entry (source_zone=" << sync_env->source_zone << ") key=" <<_raw_key << " entry=" << entry_marker;
}
int operate() {
reenter(this) {
do {
yield {
int shard_id;
int ret = parse_bucket_shard(sync_env->cct, raw_key, &bucket_name, &bucket_instance, &shard_id);
if (ret < 0) {
return set_cr_error(-EIO);
}
marker_tracker->reset_need_retry(raw_key);
call(new RGWRunBucketSyncCoroutine(sync_env, bucket_name, bucket_instance, shard_id));
}
} while (marker_tracker->need_retry(raw_key));
sync_status = retcode;
if (sync_status < 0) {
yield call(sync_env->error_logger->log_error_cr(sync_env->conn->get_remote_id(), "data", bucket_name + ":" + bucket_instance,
-sync_status, string("failed to sync bucket instance: ") + cpp_strerror(-sync_status)));
}
/* FIXME: what do do in case of error */
if (!entry_marker.empty()) {
/* update marker */
yield call(marker_tracker->finish(entry_marker));
}
if (sync_status == 0) {
sync_status = retcode;
}
if (sync_status < 0) {
return set_cr_error(sync_status);
}
return set_cr_done();
}
return 0;
}
};
#define BUCKET_SHARD_SYNC_SPAWN_WINDOW 20
class RGWDataSyncShardCR : public RGWCoroutine {
RGWDataSyncEnv *sync_env;
rgw_bucket pool;
uint32_t shard_id;
rgw_data_sync_marker sync_marker;
map<string, bufferlist> entries;
map<string, bufferlist>::iterator iter;
string oid;
RGWDataSyncShardMarkerTrack *marker_tracker;
list<rgw_data_change_log_entry> log_entries;
list<rgw_data_change_log_entry>::iterator log_iter;
bool truncated;
RGWDataChangesLogInfo shard_info;
string datalog_marker;
Mutex inc_lock;
Cond inc_cond;
boost::asio::coroutine incremental_cr;
boost::asio::coroutine full_cr;
set<string> modified_shards;
set<string> current_modified;
set<string>::iterator modified_iter;
int total_entries;
int spawn_window;
bool *reset_backoff;
set<string> spawned_keys;
RGWContinuousLeaseCR *lease_cr;
string status_oid;
public:
RGWDataSyncShardCR(RGWDataSyncEnv *_sync_env,
rgw_bucket& _pool,
uint32_t _shard_id, rgw_data_sync_marker& _marker, bool *_reset_backoff) : RGWCoroutine(_sync_env->cct),
sync_env(_sync_env),
pool(_pool),
shard_id(_shard_id),
sync_marker(_marker),
marker_tracker(NULL), truncated(false), inc_lock("RGWDataSyncShardCR::inc_lock"),
total_entries(0), spawn_window(BUCKET_SHARD_SYNC_SPAWN_WINDOW), reset_backoff(NULL),
lease_cr(NULL) {
set_description() << "data sync shard source_zone=" << sync_env->source_zone << " shard_id=" << shard_id;
status_oid = RGWDataSyncStatusManager::shard_obj_name(sync_env->source_zone, shard_id);
}
~RGWDataSyncShardCR() {
delete marker_tracker;
if (lease_cr) {
lease_cr->abort();
lease_cr->put();
}
}
void append_modified_shards(set<string>& keys) {
Mutex::Locker l(inc_lock);
modified_shards.insert(keys.begin(), keys.end());
}
void set_marker_tracker(RGWDataSyncShardMarkerTrack *mt) {
delete marker_tracker;
marker_tracker = mt;
}
int operate() {
int r;
while (true) {
switch (sync_marker.state) {
case rgw_data_sync_marker::FullSync:
r = full_sync();
if (r < 0) {
ldout(cct, 10) << "sync: full_sync: shard_id=" << shard_id << " r=" << r << dendl;
return set_cr_error(r);
}
return 0;
case rgw_data_sync_marker::IncrementalSync:
r = incremental_sync();
if (r < 0) {
ldout(cct, 10) << "sync: incremental_sync: shard_id=" << shard_id << " r=" << r << dendl;
return set_cr_error(r);
}
return 0;
default:
return set_cr_error(-EIO);
}
}
return 0;
}
void init_lease_cr() {
set_status("acquiring sync lock");
uint32_t lock_duration = cct->_conf->rgw_sync_lease_period;
string lock_name = "sync_lock";
if (lease_cr) {
lease_cr->abort();
lease_cr->put();
}
RGWRados *store = sync_env->store;
lease_cr = new RGWContinuousLeaseCR(sync_env->async_rados, store, store->get_zone_params().log_pool, status_oid,
lock_name, lock_duration, this);
lease_cr->get();
spawn(lease_cr, false);
}
int full_sync() {
#define OMAP_GET_MAX_ENTRIES 100
int max_entries = OMAP_GET_MAX_ENTRIES;
reenter(&full_cr) {
yield init_lease_cr();
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;
}
oid = full_data_sync_index_shard_oid(sync_env->source_zone, shard_id);
set_marker_tracker(new RGWDataSyncShardMarkerTrack(sync_env, status_oid, sync_marker));
total_entries = sync_marker.pos;
do {
yield call(new RGWRadosGetOmapKeysCR(sync_env->store, pool, oid, sync_marker.marker, &entries, max_entries));
if (retcode < 0) {
ldout(sync_env->cct, 0) << "ERROR: " << __func__ << "(): RGWRadosGetOmapKeysCR() returned ret=" << retcode << dendl;
lease_cr->go_down();
drain_all();
return set_cr_error(retcode);
}
iter = entries.begin();
for (; iter != entries.end(); ++iter) {
ldout(sync_env->cct, 20) << __func__ << ": full sync: " << iter->first << dendl;
total_entries++;
if (!marker_tracker->start(iter->first, total_entries, real_time())) {
ldout(sync_env->cct, 0) << "ERROR: cannot start syncing " << iter->first << ". Duplicate entry?" << dendl;
} else {
// fetch remote and write locally
yield spawn(new RGWDataSyncSingleEntryCR(sync_env, iter->first, iter->first, marker_tracker), false);
if (retcode < 0) {
lease_cr->go_down();
drain_all();
return set_cr_error(retcode);
}
}
sync_marker.marker = iter->first;
}
} while ((int)entries.size() == max_entries);
lease_cr->go_down();
drain_all();
yield {
/* update marker to reflect we're done with full sync */