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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <iostream>
#include "common/ceph_json.h"
#include "common/Formatter.h"
#include "rgw_common.h"
#include "rgw_rados.h"
#include "rgw_zone.h"
#ifndef CEPH_TEST_RGW_COMMON_H
#define CEPH_TEST_RGW_COMMON_H
struct old_rgw_bucket {
std::string tenant;
std::string name;
std::string data_pool;
std::string data_extra_pool; /* if not set, then we should use data_pool instead */
std::string index_pool;
std::string marker;
std::string bucket_id;
std::string oid; /*
* runtime in-memory only info. If not empty, points to the bucket instance object
*/
old_rgw_bucket() { }
// cppcheck-suppress noExplicitConstructor
old_rgw_bucket(const std::string& s) : name(s) {
data_pool = index_pool = s;
marker = "";
}
explicit old_rgw_bucket(const char *n) : name(n) {
data_pool = index_pool = n;
marker = "";
}
old_rgw_bucket(const char *t, const char *n, const char *dp, const char *ip, const char *m, const char *id, const char *h) :
tenant(t), name(n), data_pool(dp), index_pool(ip), marker(m), bucket_id(id) {}
void encode(bufferlist& bl) const {
ENCODE_START(8, 3, bl);
encode(name, bl);
encode(data_pool, bl);
encode(marker, bl);
encode(bucket_id, bl);
encode(index_pool, bl);
encode(data_extra_pool, bl);
encode(tenant, bl);
ENCODE_FINISH(bl);
}
void decode(bufferlist::const_iterator& bl) {
DECODE_START_LEGACY_COMPAT_LEN(8, 3, 3, bl);
decode(name, bl);
decode(data_pool, bl);
if (struct_v >= 2) {
decode(marker, bl);
if (struct_v <= 3) {
uint64_t id;
decode(id, bl);
char buf[16];
snprintf(buf, sizeof(buf), "%llu", (long long)id);
bucket_id = buf;
} else {
decode(bucket_id, bl);
}
}
if (struct_v >= 5) {
decode(index_pool, bl);
} else {
index_pool = data_pool;
}
if (struct_v >= 7) {
decode(data_extra_pool, bl);
}
if (struct_v >= 8) {
decode(tenant, bl);
}
DECODE_FINISH(bl);
}
// format a key for the bucket/instance. pass delim=0 to skip a field
std::string get_key(char tenant_delim = '/',
char id_delim = ':') const;
const std::string& get_data_extra_pool() {
if (data_extra_pool.empty()) {
return data_pool;
}
return data_extra_pool;
}
void dump(Formatter *f) const;
void decode_json(JSONObj *obj);
static void generate_test_instances(std::list<old_rgw_bucket*>& o);
bool operator<(const old_rgw_bucket& b) const {
return name.compare(b.name) < 0;
}
};
WRITE_CLASS_ENCODER(old_rgw_bucket)
class old_rgw_obj {
std::string orig_obj;
std::string loc;
std::string object;
std::string instance;
public:
const std::string& get_object() const { return object; }
const std::string& get_orig_obj() const { return orig_obj; }
const std::string& get_loc() const { return loc; }
const std::string& get_instance() const { return instance; }
old_rgw_bucket bucket;
std::string ns;
bool in_extra_data; /* in-memory only member, does not serialize */
// Represents the hash index source for this object once it is set (non-empty)
std::string index_hash_source;
old_rgw_obj() : in_extra_data(false) {}
old_rgw_obj(old_rgw_bucket& b, const std::string& o) : in_extra_data(false) {
init(b, o);
}
old_rgw_obj(old_rgw_bucket& b, const rgw_obj_key& k) : in_extra_data(false) {
from_index_key(b, k);
}
void init(old_rgw_bucket& b, const std::string& o) {
bucket = b;
set_obj(o);
reset_loc();
}
void init_ns(old_rgw_bucket& b, const std::string& o, const std::string& n) {
bucket = b;
set_ns(n);
set_obj(o);
reset_loc();
}
int set_ns(const char *n) {
if (!n)
return -EINVAL;
std::string ns_str(n);
return set_ns(ns_str);
}
int set_ns(const std::string& n) {
if (n[0] == '_')
return -EINVAL;
ns = n;
set_obj(orig_obj);
return 0;
}
int set_instance(const std::string& i) {
if (i[0] == '_')
return -EINVAL;
instance = i;
set_obj(orig_obj);
return 0;
}
int clear_instance() {
return set_instance(std::string());
}
void set_loc(const std::string& k) {
loc = k;
}
void reset_loc() {
loc.clear();
/*
* For backward compatibility. Older versions used to have object locator on all objects,
* however, the orig_obj was the effective object locator. This had the same effect as not
* having object locator at all for most objects but the ones that started with underscore as
* these were escaped.
*/
if (orig_obj[0] == '_' && ns.empty()) {
loc = orig_obj;
}
}
bool have_null_instance() {
return instance == "null";
}
bool have_instance() {
return !instance.empty();
}
bool need_to_encode_instance() {
return have_instance() && !have_null_instance();
}
void set_obj(const std::string& o) {
object.reserve(128);
orig_obj = o;
if (ns.empty() && !need_to_encode_instance()) {
if (o.empty()) {
return;
}
if (o.size() < 1 || o[0] != '_') {
object = o;
return;
}
object = "_";
object.append(o);
} else {
object = "_";
object.append(ns);
if (need_to_encode_instance()) {
object.append(std::string(":") + instance);
}
object.append("_");
object.append(o);
}
reset_loc();
}
/*
* get the object's key name as being referred to by the bucket index.
*/
std::string get_index_key_name() const {
if (ns.empty()) {
if (orig_obj.size() < 1 || orig_obj[0] != '_') {
return orig_obj;
}
return std::string("_") + orig_obj;
};
char buf[ns.size() + 16];
snprintf(buf, sizeof(buf), "_%s_", ns.c_str());
return std::string(buf) + orig_obj;
};
void from_index_key(old_rgw_bucket& b, const rgw_obj_key& key) {
if (key.name[0] != '_') {
init(b, key.name);
set_instance(key.instance);
return;
}
if (key.name[1] == '_') {
init(b, key.name.substr(1));
set_instance(key.instance);
return;
}
ssize_t pos = key.name.find('_', 1);
if (pos < 0) {
/* shouldn't happen, just use key */
init(b, key.name);
set_instance(key.instance);
return;
}
init_ns(b, key.name.substr(pos + 1), key.name.substr(1, pos -1));
set_instance(key.instance);
}
void get_index_key(rgw_obj_key *key) const {
key->name = get_index_key_name();
key->instance = instance;
}
static void parse_ns_field(std::string& ns, std::string& instance) {
int pos = ns.find(':');
if (pos >= 0) {
instance = ns.substr(pos + 1);
ns = ns.substr(0, pos);
} else {
instance.clear();
}
}
std::string& get_hash_object() {
return index_hash_source.empty() ? orig_obj : index_hash_source;
}
/**
* Translate a namespace-mangled object name to the user-facing name
* existing in the given namespace.
*
* If the object is part of the given namespace, it returns true
* and cuts down the name to the unmangled version. If it is not
* part of the given namespace, it returns false.
*/
static bool translate_raw_obj_to_obj_in_ns(std::string& obj, std::string& instance, std::string& ns) {
if (obj[0] != '_') {
if (ns.empty()) {
return true;
}
return false;
}
std::string obj_ns;
bool ret = parse_raw_oid(obj, &obj, &instance, &obj_ns);
if (!ret) {
return ret;
}
return (ns == obj_ns);
}
static bool parse_raw_oid(const std::string& oid, std::string *obj_name, std::string *obj_instance, std::string *obj_ns) {
obj_instance->clear();
obj_ns->clear();
if (oid[0] != '_') {
*obj_name = oid;
return true;
}
if (oid.size() >= 2 && oid[1] == '_') {
*obj_name = oid.substr(1);
return true;
}
if (oid[0] != '_' || oid.size() < 3) // for namespace, min size would be 3: _x_
return false;
int pos = oid.find('_', 1);
if (pos <= 1) // if it starts with __, it's not in our namespace
return false;
*obj_ns = oid.substr(1, pos - 1);
parse_ns_field(*obj_ns, *obj_instance);
*obj_name = oid.substr(pos + 1);
return true;
}
/**
* Given a mangled object name and an empty namespace string, this
* function extracts the namespace into the string and sets the object
* name to be the unmangled version.
*
* It returns true after successfully doing so, or
* false if it fails.
*/
static bool strip_namespace_from_object(std::string& obj, std::string& ns, std::string& instance) {
ns.clear();
instance.clear();
if (obj[0] != '_') {
return true;
}
size_t pos = obj.find('_', 1);
if (pos == std::string::npos) {
return false;
}
if (obj[1] == '_') {
obj = obj.substr(1);
return true;
}
size_t period_pos = obj.find('.');
if (period_pos < pos) {
return false;
}
ns = obj.substr(1, pos-1);
obj = obj.substr(pos+1, std::string::npos);
parse_ns_field(ns, instance);
return true;
}
void set_in_extra_data(bool val) {
in_extra_data = val;
}
bool is_in_extra_data() const {
return in_extra_data;
}
void encode(bufferlist& bl) const {
ENCODE_START(5, 3, bl);
encode(bucket.name, bl);
encode(loc, bl);
encode(ns, bl);
encode(object, bl);
encode(bucket, bl);
encode(instance, bl);
if (!ns.empty() || !instance.empty()) {
encode(orig_obj, bl);
}
ENCODE_FINISH(bl);
}
void decode(bufferlist::const_iterator& bl) {
DECODE_START_LEGACY_COMPAT_LEN(5, 3, 3, bl);
decode(bucket.name, bl);
decode(loc, bl);
decode(ns, bl);
decode(object, bl);
if (struct_v >= 2)
decode(bucket, bl);
if (struct_v >= 4)
decode(instance, bl);
if (ns.empty() && instance.empty()) {
if (object[0] != '_') {
orig_obj = object;
} else {
orig_obj = object.substr(1);
}
} else {
if (struct_v >= 5) {
decode(orig_obj, bl);
} else {
ssize_t pos = object.find('_', 1);
if (pos < 0) {
throw buffer::malformed_input();
}
orig_obj = object.substr(pos);
}
}
DECODE_FINISH(bl);
}
bool operator==(const old_rgw_obj& o) const {
return (object.compare(o.object) == 0) &&
(bucket.name.compare(o.bucket.name) == 0) &&
(ns.compare(o.ns) == 0) &&
(instance.compare(o.instance) == 0);
}
bool operator<(const old_rgw_obj& o) const {
int r = bucket.name.compare(o.bucket.name);
if (r == 0) {
r = bucket.bucket_id.compare(o.bucket.bucket_id);
if (r == 0) {
r = object.compare(o.object);
if (r == 0) {
r = ns.compare(o.ns);
if (r == 0) {
r = instance.compare(o.instance);
}
}
}
}
return (r < 0);
}
};
WRITE_CLASS_ENCODER(old_rgw_obj)
static inline void prepend_old_bucket_marker(const old_rgw_bucket& bucket, const std::string& orig_oid, std::string& oid)
{
if (bucket.marker.empty() || orig_oid.empty()) {
oid = orig_oid;
} else {
oid = bucket.marker;
oid.append("_");
oid.append(orig_oid);
}
}
void test_rgw_init_env(RGWZoneGroup *zonegroup, RGWZoneParams *zone_params);
struct test_rgw_env {
RGWZoneGroup zonegroup;
RGWZoneParams zone_params;
rgw_data_placement_target default_placement;
test_rgw_env() {
test_rgw_init_env(&zonegroup, &zone_params);
default_placement.data_pool = rgw_pool(zone_params.placement_pools[zonegroup.default_placement.name].get_standard_data_pool());
default_placement.data_extra_pool = rgw_pool(zone_params.placement_pools[zonegroup.default_placement.name].data_extra_pool);
}
rgw_data_placement_target get_placement(const std::string& placement_id) {
const RGWZonePlacementInfo& pi = zone_params.placement_pools[placement_id];
rgw_data_placement_target pt;
pt.index_pool = pi.index_pool;
pt.data_pool = pi.get_standard_data_pool();
pt.data_extra_pool = pi.data_extra_pool;
return pt;
}
rgw_raw_obj get_raw(const rgw_obj& obj) {
rgw_obj_select s(obj);
return s.get_raw_obj(zonegroup, zone_params);
}
rgw_raw_obj get_raw(const rgw_obj_select& os) {
return os.get_raw_obj(zonegroup, zone_params);
}
};
void test_rgw_add_placement(RGWZoneGroup *zonegroup, RGWZoneParams *zone_params, const std::string& name, bool is_default);
void test_rgw_populate_explicit_placement_bucket(rgw_bucket *b, const char *t, const char *n, const char *dp, const char *ip, const char *m, const char *id);
void test_rgw_populate_old_bucket(old_rgw_bucket *b, const char *t, const char *n, const char *dp, const char *ip, const char *m, const char *id);
std::string test_rgw_get_obj_oid(const rgw_obj& obj);
void test_rgw_init_explicit_placement_bucket(rgw_bucket *bucket, const char *name);
void test_rgw_init_old_bucket(old_rgw_bucket *bucket, const char *name);
void test_rgw_populate_bucket(rgw_bucket *b, const char *t, const char *n, const char *m, const char *id);
void test_rgw_init_bucket(rgw_bucket *bucket, const char *name);
rgw_obj test_rgw_create_obj(const rgw_bucket& bucket, const std::string& name, const std::string& instance, const std::string& ns);
#endif
|