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
|
/*
* OSD classes for the key value store
*
* Created on: Aug 10, 2012
* Author: Eleanor Cawthon
*/
#include "include/compat.h"
#include "objclass/objclass.h"
#include <errno.h>
#include "key_value_store/kvs_arg_types.h"
#include "include/types.h"
#include <iostream>
#include <climits>
using std::string;
using std::map;
using std::set;
/**
* finds the index_data where a key belongs.
*
* @param key: the key to search for
* @param idata: the index_data for the first index value such that idata.key
* is greater than key.
* @param next_idata: the index_data for the next index entry after idata
* @pre: key is not encoded
* @post: idata contains complete information
* stored
*/
static int get_idata_from_key(cls_method_context_t hctx, const string &key,
index_data &idata, index_data &next_idata) {
bufferlist raw_val;
int r = 0;
std::map<std::string, bufferlist> kvmap;
bool more;
r = cls_cxx_map_get_vals(hctx, key_data(key).encoded(), "", 2, &kvmap, &more);
if (r < 0) {
CLS_LOG(20, "error reading index for range %s: %d", key.c_str(), r);
return r;
}
r = cls_cxx_map_get_val(hctx, key_data(key).encoded(), &raw_val);
if (r == 0){
CLS_LOG(20, "%s is already in the index: %d", key.c_str(), r);
auto b = raw_val.cbegin();
idata.decode(b);
if (!kvmap.empty()) {
auto b = kvmap.begin()->second.cbegin();
next_idata.decode(b);
}
return r;
} else if (r == -ENOENT || r == -ENODATA) {
auto b = kvmap.begin()->second.cbegin();
idata.decode(b);
if (idata.kdata.prefix != "1") {
auto nb = (++kvmap.begin())->second.cbegin();
next_idata.decode(nb);
}
r = 0;
} else if (r < 0) {
CLS_LOG(20, "error reading index for duplicates %s: %d", key.c_str(), r);
return r;
}
CLS_LOG(20, "idata is %s", idata.str().c_str());
return r;
}
static int get_idata_from_key_op(cls_method_context_t hctx,
bufferlist *in, bufferlist *out) {
CLS_LOG(20, "get_idata_from_key_op");
idata_from_key_args op;
auto it = in->cbegin();
try {
decode(op, it);
} catch (buffer::error& err) {
CLS_LOG(20, "error decoding idata_from_key_args.");
return -EINVAL;
}
int r = get_idata_from_key(hctx, op.key, op.idata, op.next_idata);
if (r < 0) {
return r;
} else {
encode(op, *out);
return 0;
}
}
/**
* finds the object in the index with the lowest key value that is greater
* than idata.key. If idata.key is the max key, returns -EOVERFLOW. If
* idata has a prefix and has timed out, cleans up.
*
* @param idata: idata for the object to search for.
* @param out_data: the idata for the next object.
*
* @pre: idata must contain a key.
* @post: out_data contains complete information
*/
static int get_next_idata(cls_method_context_t hctx, const index_data &idata,
index_data &out_data) {
int r = 0;
std::map<std::string, bufferlist> kvs;
bool more;
r = cls_cxx_map_get_vals(hctx, idata.kdata.encoded(), "", 1, &kvs, &more);
if (r < 0){
CLS_LOG(20, "getting kvs failed with error %d", r);
return r;
}
if (!kvs.empty()) {
out_data.kdata.parse(kvs.begin()->first);
auto b = kvs.begin()->second.cbegin();
out_data.decode(b);
} else {
r = -EOVERFLOW;
}
return r;
}
static int get_next_idata_op(cls_method_context_t hctx,
bufferlist *in, bufferlist *out) {
CLS_LOG(20, "get_next_idata_op");
idata_from_idata_args op;
auto it = in->cbegin();
try {
decode(op, it);
} catch (buffer::error& err) {
return -EINVAL;
}
int r = get_next_idata(hctx, op.idata, op.next_idata);
if (r < 0) {
return r;
} else {
op.encode(*out);
return 0;
}
}
/**
* finds the object in the index with the highest key value that is less
* than idata.key. If idata.key is the lowest key, returns -ERANGE If
* idata has a prefix and has timed out, cleans up.
*
* @param idata: idata for the object to search for.
* @param out_data: the idata for the next object.
*
* @pre: idata must contain a key.
* @ost: out_data contains complete information
*/
static int get_prev_idata(cls_method_context_t hctx, const index_data &idata,
index_data &out_data) {
int r = 0;
std::map<std::string, bufferlist> kvs;
bool more;
r = cls_cxx_map_get_vals(hctx, "", "", LONG_MAX, &kvs, &more);
if (r < 0){
CLS_LOG(20, "getting kvs failed with error %d", r);
return r;
}
std::map<std::string, bufferlist>::iterator it =
kvs.lower_bound(idata.kdata.encoded());
if (it->first != idata.kdata.encoded()) {
CLS_LOG(20, "object %s not found in the index (expected %s, found %s)",
idata.str().c_str(), idata.kdata.encoded().c_str(),
it->first.c_str());
return -ENODATA;
}
if (it == kvs.begin()) {
//it is the first object, there is no previous.
return -ERANGE;
} else {
--it;
}
out_data.kdata.parse(it->first);
auto b = it->second.cbegin();
out_data.decode(b);
return 0;
}
static int get_prev_idata_op(cls_method_context_t hctx,
bufferlist *in, bufferlist *out) {
CLS_LOG(20, "get_next_idata_op");
idata_from_idata_args op;
auto it = in->cbegin();
try {
decode(op, it);
} catch (buffer::error& err) {
return -EINVAL;
}
int r = get_prev_idata(hctx, op.idata, op.next_idata);
if (r < 0) {
return r;
} else {
op.encode(*out);
return 0;
}
}
/**
* Read all of the index entries where any keys in the map go
*/
static int read_many(cls_method_context_t hctx, const set<string> &keys,
map<string, bufferlist> * out) {
int r = 0;
bool more;
CLS_ERR("reading from a map of size %d, first key encoded is %s",
(int)keys.size(), key_data(*keys.begin()).encoded().c_str());
r = cls_cxx_map_get_vals(hctx, key_data(*keys.begin()).encoded().c_str(),
"", LONG_MAX, out, &more);
if (r < 0) {
CLS_ERR("getting omap vals failed with error %d", r);
}
CLS_ERR("got map of size %d ", (int)out->size());
if (out->size() > 1) {
out->erase(out->upper_bound(key_data(*keys.rbegin()).encoded().c_str()),
out->end());
}
CLS_ERR("returning map of size %d", (int)out->size());
return r;
}
static int read_many_op(cls_method_context_t hctx, bufferlist *in,
bufferlist *out) {
CLS_LOG(20, "read_many_op");
set<string> op;
map<string, bufferlist> outmap;
auto it = in->cbegin();
try {
decode(op, it);
} catch (buffer::error & err) {
return -EINVAL;
}
int r = read_many(hctx, op, &outmap);
if (r < 0) {
return r;
} else {
encode(outmap, *out);
return 0;
}
}
/**
* Checks the unwritable xattr. If it is "1" (i.e., it is unwritable), returns
* -EACCES. otherwise, returns 0.
*/
static int check_writable(cls_method_context_t hctx) {
bufferlist bl;
int r = cls_cxx_getxattr(hctx, "unwritable", &bl);
if (r < 0) {
CLS_LOG(20, "error reading xattr %s: %d", "unwritable", r);
return r;
}
if (string(bl.c_str(), bl.length()) == "1") {
return -EACCES;
} else{
return 0;
}
}
static int check_writable_op(cls_method_context_t hctx,
bufferlist *in, bufferlist *out) {
CLS_LOG(20, "check_writable_op");
return check_writable(hctx);
}
/**
* returns -EKEYREJECTED if size is outside of bound, according to comparator.
*
* @bound: the limit to test
* @comparator: should be CEPH_OSD_CMPXATTR_OP_[EQ|GT|LT]
*/
static int assert_size_in_bound(cls_method_context_t hctx, int bound,
int comparator) {
//determine size
bufferlist size_bl;
int r = cls_cxx_getxattr(hctx, "size", &size_bl);
if (r < 0) {
CLS_LOG(20, "error reading xattr %s: %d", "size", r);
return r;
}
int size = atoi(string(size_bl.c_str(), size_bl.length()).c_str());
CLS_LOG(20, "size is %d, bound is %d", size, bound);
//compare size to comparator
switch (comparator) {
case CEPH_OSD_CMPXATTR_OP_EQ:
if (size != bound) {
return -EKEYREJECTED;
}
break;
case CEPH_OSD_CMPXATTR_OP_LT:
if (size >= bound) {
return -EKEYREJECTED;
}
break;
case CEPH_OSD_CMPXATTR_OP_GT:
if (size <= bound) {
return -EKEYREJECTED;
}
break;
default:
CLS_LOG(20, "invalid argument passed to assert_size_in_bound: %d",
comparator);
return -EINVAL;
}
return 0;
}
static int assert_size_in_bound_op(cls_method_context_t hctx,
bufferlist *in, bufferlist *out) {
CLS_LOG(20, "assert_size_in_bound_op");
assert_size_args op;
auto it = in->cbegin();
try {
decode(op, it);
} catch (buffer::error& err) {
return -EINVAL;
}
return assert_size_in_bound(hctx, op.bound, op.comparator);
}
/**
* Attempts to insert omap into this object's omap.
*
* @return:
* if unwritable, returns -EACCES.
* if size > bound and key doesn't already exist in the omap, returns -EBALANCE.
* if exclusive is true, returns -EEXIST if any keys already exist.
*
* @post: object has omap entries inserted, and size xattr is updated
*/
static int omap_insert(cls_method_context_t hctx,
const map<string, bufferlist> &omap, int bound, bool exclusive) {
uint64_t size;
time_t time;
int r = cls_cxx_stat(hctx, &size, &time);
if (r < 0) {
return r;
}
CLS_LOG(20, "inserting %s", omap.begin()->first.c_str());
r = check_writable(hctx);
if (r < 0) {
CLS_LOG(20, "omap_insert: this object is unwritable: %d", r);
return r;
}
int assert_bound = bound;
//if this is an exclusive insert, make sure the key doesn't already exist.
for (map<string, bufferlist>::const_iterator it = omap.begin();
it != omap.end(); ++it) {
bufferlist bl;
r = cls_cxx_map_get_val(hctx, it->first, &bl);
if (r == 0 && string(bl.c_str(), bl.length()) != ""){
if (exclusive) {
CLS_LOG(20, "error: this is an exclusive insert and %s exists.",
it->first.c_str());
return -EEXIST;
}
assert_bound++;
CLS_LOG(20, "increased assert_bound to %d", assert_bound);
} else if (r != -ENODATA && r != -ENOENT) {
CLS_LOG(20, "error reading omap val for %s: %d", it->first.c_str(), r);
return r;
}
}
bufferlist old_size;
r = cls_cxx_getxattr(hctx, "size", &old_size);
if (r < 0) {
CLS_LOG(20, "error reading xattr %s: %d", "size", r);
return r;
}
int old_size_int = atoi(string(old_size.c_str(), old_size.length()).c_str());
CLS_LOG(20, "asserting size is less than %d (bound is %d)", assert_bound, bound);
if (old_size_int >= assert_bound) {
return -EKEYREJECTED;
}
int new_size_int = old_size_int + omap.size() - (assert_bound - bound);
CLS_LOG(20, "old size is %d, new size is %d", old_size_int, new_size_int);
bufferlist new_size;
std::stringstream s;
s << new_size_int;
new_size.append(s.str());
r = cls_cxx_map_set_vals(hctx, &omap);
if (r < 0) {
CLS_LOG(20, "error setting omap: %d", r);
return r;
}
r = cls_cxx_setxattr(hctx, "size", &new_size);
if (r < 0) {
CLS_LOG(20, "error setting xattr %s: %d", "size", r);
return r;
}
CLS_LOG(20, "successfully inserted %s", omap.begin()->first.c_str());
return 0;
}
static int omap_insert_op(cls_method_context_t hctx,
bufferlist *in, bufferlist *out) {
CLS_LOG(20, "omap_insert");
omap_set_args op;
auto it = in->cbegin();
try {
decode(op, it);
} catch (buffer::error& err) {
return -EINVAL;
}
return omap_insert(hctx, op.omap, op.bound, op.exclusive);
}
static int create_with_omap(cls_method_context_t hctx,
const map<string, bufferlist> &omap) {
CLS_LOG(20, "creating with omap: %s", omap.begin()->first.c_str());
//first make sure the object is writable
int r = cls_cxx_create(hctx, true);
if (r < 0) {
CLS_LOG(20, "omap create: creating failed: %d", r);
return r;
}
int new_size_int = omap.size();
CLS_LOG(20, "omap insert: new size is %d", new_size_int);
bufferlist new_size;
std::stringstream s;
s << new_size_int;
new_size.append(s.str());
r = cls_cxx_map_set_vals(hctx, &omap);
if (r < 0) {
CLS_LOG(20, "omap create: error setting omap: %d", r);
return r;
}
r = cls_cxx_setxattr(hctx, "size", &new_size);
if (r < 0) {
CLS_LOG(20, "omap create: error setting xattr %s: %d", "size", r);
return r;
}
bufferlist u;
u.append("0");
r = cls_cxx_setxattr(hctx, "unwritable", &u);
if (r < 0) {
CLS_LOG(20, "omap create: error setting xattr %s: %d", "unwritable", r);
return r;
}
CLS_LOG(20, "successfully created %s", omap.begin()->first.c_str());
return 0;
}
static int create_with_omap_op(cls_method_context_t hctx,
bufferlist *in, bufferlist *out) {
CLS_LOG(20, "omap_insert");
map<string, bufferlist> omap;
auto it = in->cbegin();
try {
decode(omap, it);
} catch (buffer::error& err) {
return -EINVAL;
}
return create_with_omap(hctx, omap);
}
/**
* Attempts to remove omap from this object's omap.
*
* @return:
* if unwritable, returns -EACCES.
* if size < bound and key doesn't already exist in the omap, returns -EBALANCE.
* if any of the keys are not in this object, returns -ENODATA.
*
* @post: object has omap entries removed, and size xattr is updated
*/
static int omap_remove(cls_method_context_t hctx,
const std::set<string> &omap, int bound) {
int r;
uint64_t size;
time_t time;
r = cls_cxx_stat(hctx, &size, &time);
if (r < 0) {
return r;
}
//first make sure the object is writable
r = check_writable(hctx);
if (r < 0) {
return r;
}
//check for existance of the key first
for (set<string>::const_iterator it = omap.begin();
it != omap.end(); ++it) {
bufferlist bl;
r = cls_cxx_map_get_val(hctx, *it, &bl);
if (r == -ENOENT || r == -ENODATA
|| string(bl.c_str(), bl.length()) == ""){
return -ENODATA;
} else if (r < 0) {
CLS_LOG(20, "error reading omap val for %s: %d", it->c_str(), r);
return r;
}
}
//fail if removing from an object with only bound entries.
bufferlist old_size;
r = cls_cxx_getxattr(hctx, "size", &old_size);
if (r < 0) {
CLS_LOG(20, "error reading xattr %s: %d", "size", r);
return r;
}
int old_size_int = atoi(string(old_size.c_str(), old_size.length()).c_str());
CLS_LOG(20, "asserting size is greater than %d", bound);
if (old_size_int <= bound) {
return -EKEYREJECTED;
}
int new_size_int = old_size_int - omap.size();
CLS_LOG(20, "old size is %d, new size is %d", old_size_int, new_size_int);
bufferlist new_size;
std::stringstream s;
s << new_size_int;
new_size.append(s.str());
r = cls_cxx_setxattr(hctx, "size", &new_size);
if (r < 0) {
CLS_LOG(20, "error setting xattr %s: %d", "unwritable", r);
return r;
}
for (std::set<string>::const_iterator it = omap.begin();
it != omap.end(); ++it) {
r = cls_cxx_map_remove_key(hctx, *it);
if (r < 0) {
CLS_LOG(20, "error removing omap: %d", r);
return r;
}
}
return 0;
}
static int omap_remove_op(cls_method_context_t hctx,
bufferlist *in, bufferlist *out) {
CLS_LOG(20, "omap_remove");
omap_rm_args op;
auto it = in->cbegin();
try {
decode(op, it);
} catch (buffer::error& err) {
return -EINVAL;
}
return omap_remove(hctx, op.omap, op.bound);
}
/**
* checks to see if this object needs to be split or rebalanced. if so, reads
* information about it.
*
* @post: if assert_size_in_bound(hctx, bound, comparator) succeeds,
* odata contains the size, omap, and unwritable attributes for this object.
* Otherwise, odata contains the size and unwritable attribute.
*/
static int maybe_read_for_balance(cls_method_context_t hctx,
object_data &odata, int bound, int comparator) {
CLS_LOG(20, "rebalance reading");
//if unwritable, return
int r = check_writable(hctx);
if (r < 0) {
odata.unwritable = true;
CLS_LOG(20, "rebalance read: error getting xattr %s: %d", "unwritable", r);
return r;
} else {
odata.unwritable = false;
}
//get the size attribute
bufferlist size;
r = cls_cxx_getxattr(hctx, "size", &size);
if (r < 0) {
CLS_LOG(20, "rebalance read: error getting xattr %s: %d", "size", r);
return r;
}
odata.size = atoi(string(size.c_str(), size.length()).c_str());
//check if it needs to be balanced
r = assert_size_in_bound(hctx, bound, comparator);
if (r < 0) {
CLS_LOG(20, "rebalance read: error on asserting size: %d", r);
return -EBALANCE;
}
//if the assert succeeded, it needs to be balanced
bool more;
r = cls_cxx_map_get_vals(hctx, "", "", LONG_MAX, &odata.omap, &more);
if (r < 0){
CLS_LOG(20, "rebalance read: getting kvs failed with error %d", r);
return r;
}
CLS_LOG(20, "rebalance read: size xattr is %llu, omap size is %llu",
(unsigned long long)odata.size,
(unsigned long long)odata.omap.size());
return 0;
}
static int maybe_read_for_balance_op(cls_method_context_t hctx,
bufferlist *in, bufferlist *out) {
CLS_LOG(20, "maybe_read_for_balance");
rebalance_args op;
auto it = in->cbegin();
try {
decode(op, it);
} catch (buffer::error& err) {
return -EINVAL;
}
int r = maybe_read_for_balance(hctx, op.odata, op.bound, op.comparator);
if (r < 0) {
return r;
} else {
op.encode(*out);
return 0;
}
}
CLS_INIT(kvs)
{
CLS_LOG(20, "Loaded assert condition class!");
cls_handle_t h_class;
cls_method_handle_t h_get_idata_from_key;
cls_method_handle_t h_get_next_idata;
cls_method_handle_t h_get_prev_idata;
cls_method_handle_t h_read_many;
cls_method_handle_t h_check_writable;
cls_method_handle_t h_assert_size_in_bound;
cls_method_handle_t h_omap_insert;
cls_method_handle_t h_create_with_omap;
cls_method_handle_t h_omap_remove;
cls_method_handle_t h_maybe_read_for_balance;
cls_register("kvs", &h_class);
cls_register_cxx_method(h_class, "get_idata_from_key",
CLS_METHOD_RD,
get_idata_from_key_op, &h_get_idata_from_key);
cls_register_cxx_method(h_class, "get_next_idata",
CLS_METHOD_RD,
get_next_idata_op, &h_get_next_idata);
cls_register_cxx_method(h_class, "get_prev_idata",
CLS_METHOD_RD,
get_prev_idata_op, &h_get_prev_idata);
cls_register_cxx_method(h_class, "read_many",
CLS_METHOD_RD,
read_many_op, &h_read_many);
cls_register_cxx_method(h_class, "check_writable",
CLS_METHOD_RD | CLS_METHOD_WR,
check_writable_op, &h_check_writable);
cls_register_cxx_method(h_class, "assert_size_in_bound",
CLS_METHOD_WR,
assert_size_in_bound_op, &h_assert_size_in_bound);
cls_register_cxx_method(h_class, "omap_insert",
CLS_METHOD_WR,
omap_insert_op, &h_omap_insert);
cls_register_cxx_method(h_class, "create_with_omap",
CLS_METHOD_WR,
create_with_omap_op, &h_create_with_omap);
cls_register_cxx_method(h_class, "omap_remove",
CLS_METHOD_WR,
omap_remove_op, &h_omap_remove);
cls_register_cxx_method(h_class, "maybe_read_for_balance",
CLS_METHOD_RD,
maybe_read_for_balance_op, &h_maybe_read_for_balance);
return;
}
|