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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
#include "rgw_metadata.h"
#include "rgw_mdlog.h"
#define dout_subsys ceph_subsys_rgw
using namespace std;
void LogStatusDump::dump(Formatter *f) const {
string s;
switch (status) {
case MDLOG_STATUS_WRITE:
s = "write";
break;
case MDLOG_STATUS_SETATTRS:
s = "set_attrs";
break;
case MDLOG_STATUS_REMOVE:
s = "remove";
break;
case MDLOG_STATUS_COMPLETE:
s = "complete";
break;
case MDLOG_STATUS_ABORT:
s = "abort";
break;
default:
s = "unknown";
break;
}
encode_json("status", s, f);
}
void encode_json(const char *name, const obj_version& v, Formatter *f)
{
f->open_object_section(name);
f->dump_string("tag", v.tag);
f->dump_unsigned("ver", v.ver);
f->close_section();
}
void decode_json_obj(obj_version& v, JSONObj *obj)
{
JSONDecoder::decode_json("tag", v.tag, obj);
JSONDecoder::decode_json("ver", v.ver, obj);
}
void RGWMetadataLogData::encode(bufferlist& bl) const {
ENCODE_START(1, 1, bl);
encode(read_version, bl);
encode(write_version, bl);
uint32_t s = (uint32_t)status;
encode(s, bl);
ENCODE_FINISH(bl);
}
void RGWMetadataLogData::decode(bufferlist::const_iterator& bl) {
DECODE_START(1, bl);
decode(read_version, bl);
decode(write_version, bl);
uint32_t s;
decode(s, bl);
status = (RGWMDLogStatus)s;
DECODE_FINISH(bl);
}
void RGWMetadataLogData::dump(Formatter *f) const {
encode_json("read_version", read_version, f);
encode_json("write_version", write_version, f);
encode_json("status", LogStatusDump(status), f);
}
void decode_json_obj(RGWMDLogStatus& status, JSONObj *obj) {
string s;
JSONDecoder::decode_json("status", s, obj);
if (s == "complete") {
status = MDLOG_STATUS_COMPLETE;
} else if (s == "write") {
status = MDLOG_STATUS_WRITE;
} else if (s == "remove") {
status = MDLOG_STATUS_REMOVE;
} else if (s == "set_attrs") {
status = MDLOG_STATUS_SETATTRS;
} else if (s == "abort") {
status = MDLOG_STATUS_ABORT;
} else {
status = MDLOG_STATUS_UNKNOWN;
}
}
void RGWMetadataLogData::decode_json(JSONObj *obj) {
JSONDecoder::decode_json("read_version", read_version, obj);
JSONDecoder::decode_json("write_version", write_version, obj);
JSONDecoder::decode_json("status", status, obj);
}
void RGWMetadataLogData::generate_test_instances(std::list<RGWMetadataLogData *>& l) {
l.push_back(new RGWMetadataLogData{});
l.push_back(new RGWMetadataLogData);
l.back()->read_version = obj_version();
l.back()->read_version.tag = "read_tag";
l.back()->write_version = obj_version();
l.back()->write_version.tag = "write_tag";
l.back()->status = MDLOG_STATUS_WRITE;
}
class RGWMetadataTopHandler : public RGWMetadataHandler {
struct iter_data {
set<string> sections;
set<string>::iterator iter;
};
RGWMetadataManager *mgr;
public:
explicit RGWMetadataTopHandler(RGWMetadataManager *_mgr) : mgr(_mgr) {}
string get_type() override { return string(); }
RGWMetadataObject *get_meta_obj(JSONObj *jo, const obj_version& objv, const ceph::real_time& mtime) {
return new RGWMetadataObject;
}
int get(string& entry, RGWMetadataObject **obj, optional_yield y, const DoutPrefixProvider *dpp) override {
return -ENOTSUP;
}
int put(string& entry, RGWMetadataObject *obj, RGWObjVersionTracker& objv_tracker,
optional_yield y, const DoutPrefixProvider *dpp, RGWMDLogSyncType type, bool from_remote_zone) override {
return -ENOTSUP;
}
int remove(string& entry, RGWObjVersionTracker& objv_tracker, optional_yield y, const DoutPrefixProvider *dpp) override {
return -ENOTSUP;
}
int mutate(const string& entry,
const ceph::real_time& mtime,
RGWObjVersionTracker *objv_tracker,
optional_yield y,
const DoutPrefixProvider *dpp,
RGWMDLogStatus op_type,
std::function<int()> f) {
return -ENOTSUP;
}
int list_keys_init(const DoutPrefixProvider *dpp, const string& marker, void **phandle) override {
iter_data *data = new iter_data;
list<string> sections;
mgr->get_sections(sections);
for (auto& s : sections) {
data->sections.insert(s);
}
data->iter = data->sections.lower_bound(marker);
*phandle = data;
return 0;
}
int list_keys_next(const DoutPrefixProvider *dpp, void *handle, int max, list<string>& keys, bool *truncated) override {
iter_data *data = static_cast<iter_data *>(handle);
for (int i = 0; i < max && data->iter != data->sections.end(); ++i, ++(data->iter)) {
keys.push_back(*data->iter);
}
*truncated = (data->iter != data->sections.end());
return 0;
}
void list_keys_complete(void *handle) override {
iter_data *data = static_cast<iter_data *>(handle);
delete data;
}
virtual string get_marker(void *handle) override {
iter_data *data = static_cast<iter_data *>(handle);
if (data->iter != data->sections.end()) {
return *(data->iter);
}
return string();
}
};
int RGWMetadataHandler::attach(RGWMetadataManager *manager)
{
return manager->register_handler(this);
}
RGWMetadataHandler::~RGWMetadataHandler() {}
obj_version& RGWMetadataObject::get_version()
{
return objv;
}
RGWMetadataManager::RGWMetadataManager()
{
md_top_handler.reset(new RGWMetadataTopHandler(this));
}
RGWMetadataManager::~RGWMetadataManager()
{
}
int RGWMetadataManager::register_handler(RGWMetadataHandler *handler)
{
string type = handler->get_type();
if (handlers.find(type) != handlers.end())
return -EEXIST;
handlers[type] = handler;
return 0;
}
RGWMetadataHandler *RGWMetadataManager::get_handler(const string& type)
{
map<string, RGWMetadataHandler *>::iterator iter = handlers.find(type);
if (iter == handlers.end())
return NULL;
return iter->second;
}
void RGWMetadataManager::parse_metadata_key(const string& metadata_key, string& type, string& entry)
{
auto pos = metadata_key.find(':');
if (pos == string::npos) {
type = metadata_key;
} else {
type = metadata_key.substr(0, pos);
entry = metadata_key.substr(pos + 1);
}
}
int RGWMetadataManager::find_handler(const string& metadata_key, RGWMetadataHandler **handler, string& entry)
{
string type;
parse_metadata_key(metadata_key, type, entry);
if (type.empty()) {
*handler = md_top_handler.get();
return 0;
}
map<string, RGWMetadataHandler *>::iterator iter = handlers.find(type);
if (iter == handlers.end())
return -ENOENT;
*handler = iter->second;
return 0;
}
int RGWMetadataManager::get(string& metadata_key, Formatter *f, optional_yield y, const DoutPrefixProvider *dpp)
{
RGWMetadataHandler *handler;
string entry;
int ret = find_handler(metadata_key, &handler, entry);
if (ret < 0) {
return ret;
}
RGWMetadataObject *obj;
ret = handler->get(entry, &obj, y, dpp);
if (ret < 0) {
return ret;
}
f->open_object_section("metadata_info");
encode_json("key", metadata_key, f);
encode_json("ver", obj->get_version(), f);
real_time mtime = obj->get_mtime();
if (!real_clock::is_zero(mtime)) {
utime_t ut(mtime);
encode_json("mtime", ut, f);
}
encode_json("data", *obj, f);
f->close_section();
delete obj;
return 0;
}
int RGWMetadataManager::put(string& metadata_key, bufferlist& bl,
optional_yield y,
const DoutPrefixProvider *dpp,
RGWMDLogSyncType sync_type,
bool from_remote_zone,
obj_version *existing_version)
{
RGWMetadataHandler *handler;
string entry;
int ret = find_handler(metadata_key, &handler, entry);
if (ret < 0) {
return ret;
}
JSONParser parser;
if (!parser.parse(bl.c_str(), bl.length())) {
return -EINVAL;
}
RGWObjVersionTracker objv_tracker;
obj_version *objv = &objv_tracker.write_version;
utime_t mtime;
try {
JSONDecoder::decode_json("key", metadata_key, &parser);
JSONDecoder::decode_json("ver", *objv, &parser);
JSONDecoder::decode_json("mtime", mtime, &parser);
} catch (JSONDecoder::err& e) {
return -EINVAL;
}
JSONObj *jo = parser.find_obj("data");
if (!jo) {
return -EINVAL;
}
RGWMetadataObject *obj = handler->get_meta_obj(jo, *objv, mtime.to_real_time());
if (!obj) {
return -EINVAL;
}
ret = handler->put(entry, obj, objv_tracker, y, dpp, sync_type, from_remote_zone);
if (existing_version) {
*existing_version = objv_tracker.read_version;
}
delete obj;
return ret;
}
int RGWMetadataManager::remove(string& metadata_key, optional_yield y, const DoutPrefixProvider *dpp)
{
RGWMetadataHandler *handler;
string entry;
int ret = find_handler(metadata_key, &handler, entry);
if (ret < 0) {
return ret;
}
RGWMetadataObject *obj;
ret = handler->get(entry, &obj, y, dpp);
if (ret < 0) {
return ret;
}
RGWObjVersionTracker objv_tracker;
objv_tracker.read_version = obj->get_version();
delete obj;
return handler->remove(entry, objv_tracker, y, dpp);
}
int RGWMetadataManager::mutate(const string& metadata_key,
const ceph::real_time& mtime,
RGWObjVersionTracker *objv_tracker,
optional_yield y,
const DoutPrefixProvider *dpp,
RGWMDLogStatus op_type,
std::function<int()> f)
{
RGWMetadataHandler *handler;
string entry;
int ret = find_handler(metadata_key, &handler, entry);
if (ret < 0) {
return ret;
}
return handler->mutate(entry, mtime, objv_tracker, y, dpp, op_type, f);
}
int RGWMetadataManager::get_shard_id(const string& section, const string& entry, int *shard_id)
{
RGWMetadataHandler *handler = get_handler(section);
if (!handler) {
return -EINVAL;
}
return handler->get_shard_id(entry, shard_id);
}
struct list_keys_handle {
void *handle;
RGWMetadataHandler *handler;
};
int RGWMetadataManager::list_keys_init(const DoutPrefixProvider *dpp, const string& section, void **handle)
{
return list_keys_init(dpp, section, string(), handle);
}
int RGWMetadataManager::list_keys_init(const DoutPrefixProvider *dpp, const string& section,
const string& marker, void **handle)
{
string entry;
RGWMetadataHandler *handler;
int ret;
ret = find_handler(section, &handler, entry);
if (ret < 0) {
return -ENOENT;
}
list_keys_handle *h = new list_keys_handle;
h->handler = handler;
ret = handler->list_keys_init(dpp, marker, &h->handle);
if (ret < 0) {
delete h;
return ret;
}
*handle = (void *)h;
return 0;
}
int RGWMetadataManager::list_keys_next(const DoutPrefixProvider *dpp, void *handle, int max, list<string>& keys, bool *truncated)
{
list_keys_handle *h = static_cast<list_keys_handle *>(handle);
RGWMetadataHandler *handler = h->handler;
return handler->list_keys_next(dpp, h->handle, max, keys, truncated);
}
void RGWMetadataManager::list_keys_complete(void *handle)
{
list_keys_handle *h = static_cast<list_keys_handle *>(handle);
RGWMetadataHandler *handler = h->handler;
handler->list_keys_complete(h->handle);
delete h;
}
string RGWMetadataManager::get_marker(void *handle)
{
list_keys_handle *h = static_cast<list_keys_handle *>(handle);
return h->handler->get_marker(h->handle);
}
void RGWMetadataManager::dump_log_entry(cls_log_entry& entry, Formatter *f)
{
f->open_object_section("entry");
f->dump_string("id", entry.id);
f->dump_string("section", entry.section);
f->dump_string("name", entry.name);
entry.timestamp.gmtime_nsec(f->dump_stream("timestamp"));
try {
RGWMetadataLogData log_data;
auto iter = entry.data.cbegin();
decode(log_data, iter);
encode_json("data", log_data, f);
} catch (buffer::error& err) {
}
f->close_section();
}
void RGWMetadataManager::get_sections(list<string>& sections)
{
for (map<string, RGWMetadataHandler *>::iterator iter = handlers.begin(); iter != handlers.end(); ++iter) {
sections.push_back(iter->first);
}
}
|