-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyValueStore.cc
2983 lines (2482 loc) · 82.8 KB
/
KeyValueStore.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
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2013 UnitedStack <[email protected]>
*
* Author: Haomai Wang <[email protected]>
*
* 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 "include/int_types.h"
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <errno.h>
#include <dirent.h>
#include <iostream>
#include <map>
#include "include/compat.h"
#include <fstream>
#include <sstream>
#include "KeyValueStore.h"
#include "common/BackTrace.h"
#include "include/types.h"
#include "osd/osd_types.h"
#include "include/color.h"
#include "include/buffer.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/run_cmd.h"
#include "common/safe_io.h"
#include "common/perf_counters.h"
#include "common/sync_filesystem.h"
#include "LevelDBStore.h"
#include "common/ceph_crypto.h"
using ceph::crypto::SHA1;
#include "include/assert.h"
#include "common/config.h"
#define dout_subsys ceph_subsys_keyvaluestore
const string KeyValueStore::OBJECT_STRIP_PREFIX = "_STRIP_";
const string KeyValueStore::OBJECT_XATTR = "__OBJATTR__";
const string KeyValueStore::OBJECT_OMAP = "__OBJOMAP__";
const string KeyValueStore::OBJECT_OMAP_HEADER = "__OBJOMAP_HEADER__";
const string KeyValueStore::OBJECT_OMAP_HEADER_KEY = "__OBJOMAP_HEADER__KEY_";
const string KeyValueStore::COLLECTION = "__COLLECTION__";
const string KeyValueStore::COLLECTION_ATTR = "__COLL_ATTR__";
// ============== StripObjectMap Implementation =================
int StripObjectMap::save_strip_header(StripObjectHeader &strip_header,
KeyValueDB::Transaction t)
{
strip_header.header->data.clear();
::encode(strip_header, strip_header.header->data);
set_header(strip_header.cid, strip_header.oid, *(strip_header.header), t);
return 0;
}
int StripObjectMap::create_strip_header(const coll_t &cid,
const ghobject_t &oid,
StripObjectHeader &strip_header,
KeyValueDB::Transaction t)
{
Header header = generate_new_header(cid, oid, Header(), t);
if (!header)
return -EINVAL;
strip_header.oid = oid;
strip_header.cid = cid;
strip_header.header = header;
return 0;
}
int StripObjectMap::lookup_strip_header(const coll_t &cid,
const ghobject_t &oid,
StripObjectHeader &strip_header)
{
Header header = lookup_header(cid, oid);
if (!header) {
dout(20) << "lookup_strip_header failed to get strip_header "
<< " cid " << cid <<" oid " << oid << dendl;
return -ENOENT;
}
if (header->data.length()) {
bufferlist::iterator bliter = header->data.begin();
::decode(strip_header, bliter);
}
if (strip_header.strip_size == 0)
strip_header.strip_size = default_strip_size;
strip_header.oid = oid;
strip_header.cid = cid;
strip_header.header = header;
dout(10) << "lookup_strip_header done " << " cid " << cid << " oid "
<< oid << dendl;
return 0;
}
int StripObjectMap::file_to_extents(uint64_t offset, size_t len,
uint64_t strip_size,
vector<StripExtent> &extents)
{
if (len == 0)
return 0;
uint64_t start, end, strip_offset, extent_offset, extent_len;
start = offset / strip_size;
end = (offset + len) / strip_size;
strip_offset = start * strip_size;
// "offset" may in the middle of first strip object
if (offset > strip_offset) {
extent_offset = offset - strip_offset;
if (extent_offset + len <= strip_size)
extent_len = len;
else
extent_len = strip_size - extent_offset;
extents.push_back(StripExtent(start, extent_offset, extent_len));
start++;
strip_offset += strip_size;
}
for (; start < end; ++start) {
extents.push_back(StripExtent(start, 0, strip_size));
strip_offset += strip_size;
}
// The end of strip object may be partial
if (offset + len > strip_offset)
extents.push_back(StripExtent(start, 0, offset+len-strip_offset));
assert(extents.size());
dout(10) << "file_to_extents done " << dendl;
return 0;
}
void StripObjectMap::clone_wrap(StripObjectHeader &old_header,
const coll_t &cid, const ghobject_t &oid,
KeyValueDB::Transaction t,
StripObjectHeader *origin_header,
StripObjectHeader *target_header)
{
Header new_origin_header;
if (target_header)
*target_header = old_header;
if (origin_header)
*origin_header = old_header;
clone(old_header.header, cid, oid, t, &new_origin_header,
&target_header->header);
if(origin_header)
origin_header->header = new_origin_header;
if (target_header) {
target_header->oid = oid;
target_header->cid = cid;
}
}
void StripObjectMap::rename_wrap(const coll_t &cid, const ghobject_t &oid,
KeyValueDB::Transaction t,
StripObjectHeader *header)
{
assert(header);
assert(header->header);
rename(header->header, cid, oid, t);
header->oid = oid;
header->cid = cid;
}
int StripObjectMap::get_values_with_header(const StripObjectHeader &header,
const string &prefix,
const set<string> &keys,
map<string, bufferlist> *out)
{
return scan(header.header, prefix, keys, 0, out);
}
int StripObjectMap::get_keys_with_header(const StripObjectHeader &header,
const string &prefix,
set<string> *keys)
{
ObjectMap::ObjectMapIterator iter = _get_iterator(header.header, prefix);
for (; iter->valid(); iter->next()) {
if (iter->status())
return iter->status();
keys->insert(iter->key());
}
return 0;
}
int StripObjectMap::get_with_header(const StripObjectHeader &header,
const string &prefix, map<string, bufferlist> *out)
{
ObjectMap::ObjectMapIterator iter = _get_iterator(header.header, prefix);
for (iter->seek_to_first(); iter->valid(); iter->next()) {
if (iter->status())
return iter->status();
out->insert(make_pair(iter->key(), iter->value()));
}
return 0;
}
// ========= KeyValueStore::BufferTransaction Implementation ============
int KeyValueStore::BufferTransaction::lookup_cached_header(
const coll_t &cid, const ghobject_t &oid,
StripObjectMap::StripObjectHeader **strip_header,
bool create_if_missing)
{
StripObjectMap::StripObjectHeader header;
int r = 0;
StripHeaderMap::iterator it = strip_headers.find(make_pair(cid, oid));
if (it != strip_headers.end()) {
if (!it->second.deleted) {
if (strip_header)
*strip_header = &it->second;
return 0;
} else if (!create_if_missing) {
return -ENOENT;
}
// If (it->second.deleted && create_if_missing) go down
r = -ENOENT;
} else {
r = store->backend->lookup_strip_header(cid, oid, header);
}
if (r == -ENOENT && create_if_missing) {
r = store->backend->create_strip_header(cid, oid, header, t);
}
if (r < 0) {
dout(10) << __func__ << " " << cid << "/" << oid << " "
<< " r = " << r << dendl;
return r;
}
strip_headers[make_pair(cid, oid)] = header;
if (strip_header)
*strip_header = &strip_headers[make_pair(cid, oid)];
return r;
}
int KeyValueStore::BufferTransaction::get_buffer_keys(
StripObjectMap::StripObjectHeader &strip_header, const string &prefix,
const set<string> &keys, map<string, bufferlist> *out)
{
set<string> need_lookup;
for (set<string>::iterator it = keys.begin(); it != keys.end(); ++it) {
map<pair<string, string>, bufferlist>::iterator i =
strip_header.buffers.find(make_pair(prefix, *it));
if (i != strip_header.buffers.end()) {
(*out)[*it].swap(i->second);
} else {
need_lookup.insert(*it);
}
}
if (!need_lookup.empty()) {
int r = store->backend->get_values_with_header(strip_header, prefix,
need_lookup, out);
if (r < 0) {
dout(10) << __func__ << " " << strip_header.cid << "/"
<< strip_header.oid << " " << " r = " << r << dendl;
return r;
}
}
return 0;
}
void KeyValueStore::BufferTransaction::set_buffer_keys(
StripObjectMap::StripObjectHeader &strip_header,
const string &prefix, map<string, bufferlist> &values)
{
store->backend->set_keys(strip_header.header, prefix, values, t);
for (map<string, bufferlist>::iterator iter = values.begin();
iter != values.end(); ++iter) {
strip_header.buffers[make_pair(prefix, iter->first)].swap(iter->second);
}
}
int KeyValueStore::BufferTransaction::remove_buffer_keys(
StripObjectMap::StripObjectHeader &strip_header, const string &prefix,
const set<string> &keys)
{
for (set<string>::iterator iter = keys.begin(); iter != keys.end(); ++iter) {
strip_header.buffers[make_pair(prefix, *iter)] = bufferlist();
}
return store->backend->rm_keys(strip_header.header, prefix, keys, t);
}
void KeyValueStore::BufferTransaction::clear_buffer_keys(
StripObjectMap::StripObjectHeader &strip_header, const string &prefix)
{
for (map<pair<string, string>, bufferlist>::iterator iter = strip_header.buffers.begin();
iter != strip_header.buffers.end(); ++iter) {
if (iter->first.first == prefix)
iter->second = bufferlist();
}
}
int KeyValueStore::BufferTransaction::clear_buffer(
StripObjectMap::StripObjectHeader &strip_header)
{
strip_header.deleted = true;
return store->backend->clear(strip_header.header, t);
}
void KeyValueStore::BufferTransaction::clone_buffer(
StripObjectMap::StripObjectHeader &old_header,
const coll_t &cid, const ghobject_t &oid)
{
// Remove target ahead to avoid dead lock
strip_headers.erase(make_pair(cid, oid));
StripObjectMap::StripObjectHeader new_origin_header, new_target_header;
store->backend->clone_wrap(old_header, cid, oid, t,
&new_origin_header, &new_target_header);
// FIXME: Lacking of lock for origin header(now become parent), it will
// cause other operation can get the origin header while submitting
// transactions
strip_headers[make_pair(cid, old_header.oid)] = new_origin_header;
strip_headers[make_pair(cid, oid)] = new_target_header;
}
void KeyValueStore::BufferTransaction::rename_buffer(
StripObjectMap::StripObjectHeader &old_header,
const coll_t &cid, const ghobject_t &oid)
{
// FIXME: Lacking of lock for origin header, it will cause other operation
// can get the origin header while submitting transactions
store->backend->rename_wrap(cid, oid, t, &old_header);
strip_headers.erase(make_pair(old_header.cid, old_header.oid));
strip_headers[make_pair(cid, oid)] = old_header;
}
int KeyValueStore::BufferTransaction::submit_transaction()
{
int r = 0;
for (StripHeaderMap::iterator header_iter = strip_headers.begin();
header_iter != strip_headers.end(); ++header_iter) {
StripObjectMap::StripObjectHeader header = header_iter->second;
if (header.deleted)
continue;
r = store->backend->save_strip_header(header, t);
if (r < 0) {
dout(10) << __func__ << " save strip header failed " << dendl;
goto out;
}
}
out:
dout(5) << __func__ << " r = " << r << dendl;
return store->backend->submit_transaction(t);
}
// =========== KeyValueStore Intern Helper Implementation ==============
ostream& operator<<(ostream& out, const KeyValueStore::OpSequencer& s)
{
assert(&out);
return out << *s.parent;
}
int KeyValueStore::_create_current()
{
struct stat st;
int ret = ::stat(current_fn.c_str(), &st);
if (ret == 0) {
// current/ exists
if (!S_ISDIR(st.st_mode)) {
dout(0) << "_create_current: current/ exists but is not a directory" << dendl;
ret = -EINVAL;
}
} else {
ret = ::mkdir(current_fn.c_str(), 0755);
if (ret < 0) {
ret = -errno;
dout(0) << "_create_current: mkdir " << current_fn << " failed: "<< cpp_strerror(ret) << dendl;
}
}
return ret;
}
// =========== KeyValueStore API Implementation ==============
KeyValueStore::KeyValueStore(const std::string &base,
const char *name, bool do_update) :
ObjectStore(base),
internal_name(name),
basedir(base),
fsid_fd(-1), current_fd(-1),
kv_type(KV_TYPE_NONE),
backend(NULL),
ondisk_finisher(g_ceph_context),
lock("KeyValueStore::lock"),
default_osr("default"),
op_queue_len(0), op_queue_bytes(0),
op_throttle_lock("KeyValueStore::op_throttle_lock"),
op_finisher(g_ceph_context),
op_tp(g_ceph_context, "KeyValueStore::op_tp",
g_conf->keyvaluestore_op_threads, "keyvaluestore_op_threads"),
op_wq(this, g_conf->keyvaluestore_op_thread_timeout,
g_conf->keyvaluestore_op_thread_suicide_timeout, &op_tp),
perf_logger(NULL),
m_keyvaluestore_queue_max_ops(g_conf->keyvaluestore_queue_max_ops),
m_keyvaluestore_queue_max_bytes(g_conf->keyvaluestore_queue_max_bytes),
m_keyvaluestore_strip_size(g_conf->keyvaluestore_default_strip_size),
m_keyvaluestore_max_expected_write_size(g_conf->keyvaluestore_max_expected_write_size),
do_update(do_update)
{
ostringstream oss;
oss << basedir << "/current";
current_fn = oss.str();
ostringstream sss;
sss << basedir << "/current/commit_op_seq";
current_op_seq_fn = sss.str();
// initialize perf_logger
PerfCountersBuilder plb(g_ceph_context, internal_name, l_os_commit_len, l_os_last);
plb.add_u64(l_os_oq_max_ops, "op_queue_max_ops");
plb.add_u64(l_os_oq_ops, "op_queue_ops");
plb.add_u64_counter(l_os_ops, "ops");
plb.add_u64(l_os_oq_max_bytes, "op_queue_max_bytes");
plb.add_u64(l_os_oq_bytes, "op_queue_bytes");
plb.add_u64_counter(l_os_bytes, "bytes");
plb.add_time_avg(l_os_commit_lat, "commit_latency");
plb.add_time_avg(l_os_apply_lat, "apply_latency");
plb.add_time_avg(l_os_queue_lat, "queue_transaction_latency_avg");
perf_logger = plb.create_perf_counters();
g_ceph_context->get_perfcounters_collection()->add(perf_logger);
g_ceph_context->_conf->add_observer(this);
}
KeyValueStore::~KeyValueStore()
{
g_ceph_context->_conf->remove_observer(this);
g_ceph_context->get_perfcounters_collection()->remove(perf_logger);
delete perf_logger;
}
int KeyValueStore::statfs(struct statfs *buf)
{
if (::statfs(basedir.c_str(), buf) < 0) {
int r = -errno;
return r;
}
return 0;
}
int KeyValueStore::mkfs()
{
int ret = 0;
char fsid_fn[PATH_MAX];
uuid_d old_fsid;
dout(1) << "mkfs in " << basedir << dendl;
// open+lock fsid
snprintf(fsid_fn, sizeof(fsid_fn), "%s/fsid", basedir.c_str());
fsid_fd = ::open(fsid_fn, O_RDWR|O_CREAT, 0644);
if (fsid_fd < 0) {
ret = -errno;
derr << "mkfs: failed to open " << fsid_fn << ": " << cpp_strerror(ret) << dendl;
return ret;
}
if (lock_fsid() < 0) {
ret = -EBUSY;
goto close_fsid_fd;
}
if (read_fsid(fsid_fd, &old_fsid) < 0 || old_fsid.is_zero()) {
if (fsid.is_zero()) {
fsid.generate_random();
dout(1) << "mkfs generated fsid " << fsid << dendl;
} else {
dout(1) << "mkfs using provided fsid " << fsid << dendl;
}
char fsid_str[40];
fsid.print(fsid_str);
strcat(fsid_str, "\n");
ret = ::ftruncate(fsid_fd, 0);
if (ret < 0) {
ret = -errno;
derr << "mkfs: failed to truncate fsid: " << cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
ret = safe_write(fsid_fd, fsid_str, strlen(fsid_str));
if (ret < 0) {
derr << "mkfs: failed to write fsid: " << cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
if (::fsync(fsid_fd) < 0) {
ret = errno;
derr << "mkfs: close failed: can't write fsid: "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
dout(10) << "mkfs fsid is " << fsid << dendl;
} else {
if (!fsid.is_zero() && fsid != old_fsid) {
derr << "mkfs on-disk fsid " << old_fsid << " != provided " << fsid << dendl;
ret = -EINVAL;
goto close_fsid_fd;
}
fsid = old_fsid;
dout(1) << "mkfs fsid is already set to " << fsid << dendl;
}
// version stamp
ret = write_version_stamp();
if (ret < 0) {
derr << "mkfs: write_version_stamp() failed: "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
ret = _create_current();
if (ret < 0) {
derr << "mkfs: failed to create current/ " << cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
if (_detect_backend()) {
derr << "KeyValueStore::mkfs error in _detect_backend" << dendl;
ret = -1;
goto close_fsid_fd;
}
{
KeyValueDB *store;
if (kv_type == KV_TYPE_LEVELDB) {
store = new LevelDBStore(g_ceph_context, current_fn);
} else {
derr << "KeyValueStore::mkfs error: unknown backend type" << kv_type << dendl;
ret = -1;
goto close_fsid_fd;
}
store->init();
stringstream err;
if (store->create_and_open(err)) {
derr << "KeyValueStore::mkfs failed to create keyvaluestore backend: "
<< err.str() << dendl;
ret = -1;
delete store;
goto close_fsid_fd;
} else {
delete store;
dout(1) << "keyvaluestore backend exists/created" << dendl;
}
}
dout(1) << "mkfs done in " << basedir << dendl;
ret = 0;
close_fsid_fd:
VOID_TEMP_FAILURE_RETRY(::close(fsid_fd));
fsid_fd = -1;
return ret;
}
int KeyValueStore::read_fsid(int fd, uuid_d *uuid)
{
char fsid_str[40];
int ret = safe_read(fd, fsid_str, sizeof(fsid_str));
if (ret < 0)
return ret;
if (ret == 8) {
// old 64-bit fsid... mirror it.
*(uint64_t*)&uuid->uuid[0] = *(uint64_t*)fsid_str;
*(uint64_t*)&uuid->uuid[8] = *(uint64_t*)fsid_str;
return 0;
}
if (ret > 36)
fsid_str[36] = 0;
if (!uuid->parse(fsid_str))
return -EINVAL;
return 0;
}
int KeyValueStore::lock_fsid()
{
struct flock l;
memset(&l, 0, sizeof(l));
l.l_type = F_WRLCK;
l.l_whence = SEEK_SET;
l.l_start = 0;
l.l_len = 0;
int r = ::fcntl(fsid_fd, F_SETLK, &l);
if (r < 0) {
int err = errno;
dout(0) << "lock_fsid failed to lock " << basedir
<< "/fsid, is another ceph-osd still running? "
<< cpp_strerror(err) << dendl;
return -err;
}
return 0;
}
bool KeyValueStore::test_mount_in_use()
{
dout(5) << "test_mount basedir " << basedir << dendl;
char fn[PATH_MAX];
snprintf(fn, sizeof(fn), "%s/fsid", basedir.c_str());
// verify fs isn't in use
fsid_fd = ::open(fn, O_RDWR, 0644);
if (fsid_fd < 0)
return 0; // no fsid, ok.
bool inuse = lock_fsid() < 0;
VOID_TEMP_FAILURE_RETRY(::close(fsid_fd));
fsid_fd = -1;
return inuse;
}
int KeyValueStore::update_version_stamp()
{
return write_version_stamp();
}
int KeyValueStore::version_stamp_is_valid(uint32_t *version)
{
bufferptr bp(PATH_MAX);
int ret = safe_read_file(basedir.c_str(), "store_version",
bp.c_str(), bp.length());
if (ret < 0) {
if (ret == -ENOENT)
return 0;
return ret;
}
bufferlist bl;
bl.push_back(bp);
bufferlist::iterator i = bl.begin();
::decode(*version, i);
if (*version == target_version)
return 1;
else
return 0;
}
int KeyValueStore::write_version_stamp()
{
bufferlist bl;
::encode(target_version, bl);
return safe_write_file(basedir.c_str(), "store_version",
bl.c_str(), bl.length());
}
int KeyValueStore::mount()
{
int ret;
char buf[PATH_MAX];
dout(5) << "basedir " << basedir << dendl;
// make sure global base dir exists
if (::access(basedir.c_str(), R_OK | W_OK)) {
ret = -errno;
derr << "KeyValueStore::mount: unable to access basedir '" << basedir
<< "': " << cpp_strerror(ret) << dendl;
goto done;
}
// get fsid
snprintf(buf, sizeof(buf), "%s/fsid", basedir.c_str());
fsid_fd = ::open(buf, O_RDWR, 0644);
if (fsid_fd < 0) {
ret = -errno;
derr << "KeyValueStore::mount: error opening '" << buf << "': "
<< cpp_strerror(ret) << dendl;
goto done;
}
ret = read_fsid(fsid_fd, &fsid);
if (ret < 0) {
derr << "KeyValueStore::mount: error reading fsid_fd: "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
if (lock_fsid() < 0) {
derr << "KeyValueStore::mount: lock_fsid failed" << dendl;
ret = -EBUSY;
goto close_fsid_fd;
}
dout(10) << "mount fsid is " << fsid << dendl;
uint32_t version_stamp;
ret = version_stamp_is_valid(&version_stamp);
if (ret < 0) {
derr << "KeyValueStore::mount : error in version_stamp_is_valid: "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
} else if (ret == 0) {
if (do_update) {
derr << "KeyValueStore::mount : stale version stamp detected: "
<< version_stamp << ". Proceeding, do_update "
<< "is set, performing disk format upgrade." << dendl;
} else {
ret = -EINVAL;
derr << "KeyValueStore::mount : stale version stamp " << version_stamp
<< ". Please run the KeyValueStore update script before starting "
<< "the OSD, or set keyvaluestore_update_to to " << target_version
<< dendl;
goto close_fsid_fd;
}
}
current_fd = ::open(current_fn.c_str(), O_RDONLY);
if (current_fd < 0) {
ret = -errno;
derr << "KeyValueStore::mount: error opening: " << current_fn << ": "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
assert(current_fd >= 0);
if (_detect_backend()) {
derr << "KeyValueStore::mount error in _detect_backend" << dendl;
ret = -1;
goto close_current_fd;
}
{
KeyValueDB *store;
if (kv_type == KV_TYPE_LEVELDB) {
store = new LevelDBStore(g_ceph_context, current_fn);
} else {
derr << "KeyValueStore::mount error: unknown backend type" << kv_type
<< dendl;
ret = -1;
goto close_current_fd;
}
store->init();
stringstream err;
if (store->open(err)) {
derr << "KeyValueStore::mount Error initializing keyvaluestore backend: "
<< err.str() << dendl;
ret = -1;
delete store;
goto close_current_fd;
}
StripObjectMap *dbomap = new StripObjectMap(store);
ret = dbomap->init(do_update);
if (ret < 0) {
delete dbomap;
derr << "Error initializing StripObjectMap: " << ret << dendl;
goto close_current_fd;
}
stringstream err2;
if (g_conf->keyvaluestore_debug_check_backend && !dbomap->check(err2)) {
derr << err2.str() << dendl;
delete dbomap;
ret = -EINVAL;
goto close_current_fd;
}
default_strip_size = m_keyvaluestore_strip_size;
backend.reset(dbomap);
}
op_tp.start();
op_finisher.start();
ondisk_finisher.start();
// all okay.
return 0;
close_current_fd:
VOID_TEMP_FAILURE_RETRY(::close(current_fd));
current_fd = -1;
close_fsid_fd:
VOID_TEMP_FAILURE_RETRY(::close(fsid_fd));
fsid_fd = -1;
done:
return ret;
}
int KeyValueStore::umount()
{
dout(5) << "umount " << basedir << dendl;
op_tp.stop();
op_finisher.stop();
ondisk_finisher.stop();
if (fsid_fd >= 0) {
VOID_TEMP_FAILURE_RETRY(::close(fsid_fd));
fsid_fd = -1;
}
if (current_fd >= 0) {
VOID_TEMP_FAILURE_RETRY(::close(current_fd));
current_fd = -1;
}
backend.reset();
// nothing
return 0;
}
int KeyValueStore::get_max_object_name_length()
{
lock.Lock();
int ret = pathconf(basedir.c_str(), _PC_NAME_MAX);
if (ret < 0) {
int err = errno;
lock.Unlock();
if (err == 0)
return -EDOM;
return -err;
}
lock.Unlock();
return ret;
}
int KeyValueStore::queue_transactions(Sequencer *posr, list<Transaction*> &tls,
TrackedOpRef osd_op,
ThreadPool::TPHandle *handle)
{
Context *onreadable;
Context *ondisk;
Context *onreadable_sync;
ObjectStore::Transaction::collect_contexts(
tls, &onreadable, &ondisk, &onreadable_sync);
// set up the sequencer
OpSequencer *osr;
if (!posr)
posr = &default_osr;
if (posr->p) {
osr = static_cast<OpSequencer *>(posr->p);
dout(5) << "queue_transactions existing " << *osr << "/" << osr->parent
<< dendl; //<< " w/ q " << osr->q << dendl;
} else {
osr = new OpSequencer;
osr->parent = posr;
posr->p = osr;
dout(5) << "queue_transactions new " << *osr << "/" << osr->parent << dendl;
}
Op *o = build_op(tls, ondisk, onreadable, onreadable_sync, osd_op);
op_queue_reserve_throttle(o, handle);
dout(5) << "queue_transactions (trailing journal) " << " " << tls <<dendl;
queue_op(osr, o);
return 0;
}
// ============== KeyValueStore Op Handler =================
KeyValueStore::Op *KeyValueStore::build_op(list<Transaction*>& tls,
Context *ondisk, Context *onreadable, Context *onreadable_sync,
TrackedOpRef osd_op)
{
uint64_t bytes = 0, ops = 0;
for (list<Transaction*>::iterator p = tls.begin();
p != tls.end();
++p) {
bytes += (*p)->get_num_bytes();
ops += (*p)->get_num_ops();
}
Op *o = new Op;
o->start = ceph_clock_now(g_ceph_context);
o->tls.swap(tls);
o->ondisk = ondisk;
o->onreadable = onreadable;
o->onreadable_sync = onreadable_sync;
o->ops = ops;
o->bytes = bytes;
o->osd_op = osd_op;
return o;
}
void KeyValueStore::queue_op(OpSequencer *osr, Op *o)
{
// queue op on sequencer, then queue sequencer for the threadpool,
// so that regardless of which order the threads pick up the
// sequencer, the op order will be preserved.
osr->queue(o);
perf_logger->inc(l_os_ops);
perf_logger->inc(l_os_bytes, o->bytes);
dout(5) << "queue_op " << o << " seq " << o->op << " " << *osr << " "
<< o->bytes << " bytes" << " (queue has " << op_queue_len
<< " ops and " << op_queue_bytes << " bytes)" << dendl;
op_wq.queue(osr);
}
void KeyValueStore::op_queue_reserve_throttle(Op *o, ThreadPool::TPHandle *handle)
{
uint64_t max_ops = m_keyvaluestore_queue_max_ops;
uint64_t max_bytes = m_keyvaluestore_queue_max_bytes;
perf_logger->set(l_os_oq_max_ops, max_ops);
perf_logger->set(l_os_oq_max_bytes, max_bytes);
utime_t start = ceph_clock_now(g_ceph_context);
{
Mutex::Locker l(op_throttle_lock);
while ((max_ops && (op_queue_len + 1) > max_ops) ||
(max_bytes && op_queue_bytes // let single large ops through!
&& (op_queue_bytes + o->bytes) > max_bytes)) {
dout(2) << "waiting " << op_queue_len + 1 << " > " << max_ops
<< " ops || " << op_queue_bytes + o->bytes << " > " << max_bytes
<< dendl;
if (handle)
handle->suspend_tp_timeout();
op_throttle_cond.Wait(op_throttle_lock);
if (handle)
handle->reset_tp_timeout();
}
op_queue_len++;
op_queue_bytes += o->bytes;
}
utime_t end = ceph_clock_now(g_ceph_context);
perf_logger->tinc(l_os_queue_lat, end - start);
perf_logger->set(l_os_oq_ops, op_queue_len);
perf_logger->set(l_os_oq_bytes, op_queue_bytes);
}
void KeyValueStore::op_queue_release_throttle(Op *o)
{
{
Mutex::Locker l(op_throttle_lock);
op_queue_len--;