-
Notifications
You must be signed in to change notification settings - Fork 172
/
forestdb.cc
6914 lines (6149 loc) · 257 KB
/
forestdb.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: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2010 Couchbase, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#if !defined(WIN32) && !defined(_WIN32)
#include <sys/time.h>
#endif
#include "libforestdb/forestdb.h"
#include "fdb_internal.h"
#include "filemgr.h"
#include "hbtrie.h"
#include "list.h"
#include "btree.h"
#include "btree_kv.h"
#include "btree_var_kv_ops.h"
#include "docio.h"
#include "btreeblock.h"
#include "common.h"
#include "wal.h"
#include "snapshot.h"
#include "filemgr_ops.h"
#include "configuration.h"
#include "internal_types.h"
#include "bgflusher.h"
#include "compactor.h"
#include "memleak.h"
#include "time_utils.h"
#include "system_resource_stats.h"
#ifdef __DEBUG
#ifndef __DEBUG_FDB
#undef DBG
#undef DBGCMD
#undef DBGSW
#define DBG(...)
#define DBGCMD(...)
#define DBGSW(n, ...)
#endif
#endif
#ifdef _TRACE_HANDLES
struct avl_tree open_handles;
static spin_t open_handle_lock;
static int _fdb_handle_cmp(struct avl_node *a, struct avl_node *b, void *aux)
{
struct _fdb_kvs_handle *aa, *bb;
aa = _get_entry(a, struct _fdb_kvs_handle, avl_trace);
bb = _get_entry(b, struct _fdb_kvs_handle, avl_trace);
return (aa > bb) ? 1 : -1;
}
#endif
static volatile uint8_t fdb_initialized = 0;
static volatile uint8_t fdb_open_inprog = 0;
#ifdef SPIN_INITIALIZER
static spin_t initial_lock = SPIN_INITIALIZER;
#else
static volatile unsigned int initial_lock_status = 0;
static spin_t initial_lock;
#endif
static fdb_status _fdb_wal_snapshot_func(void *handle, fdb_doc *doc,
uint64_t offset);
INLINE int _cmp_uint64_t_endian_safe(void *key1, void *key2, void *aux)
{
(void) aux;
uint64_t a,b;
a = *(uint64_t*)key1;
b = *(uint64_t*)key2;
a = _endian_decode(a);
b = _endian_decode(b);
return _CMP_U64(a, b);
}
size_t _fdb_readkey_wrap(void *handle, uint64_t offset, void *buf)
{
keylen_t keylen;
offset = _endian_decode(offset);
docio_read_doc_key((struct docio_handle *)handle, offset, &keylen, buf);
return keylen;
}
size_t _fdb_readseq_wrap(void *handle, uint64_t offset, void *buf)
{
int size_id, size_seq, size_chunk;
fdb_seqnum_t _seqnum;
struct docio_object doc;
struct docio_handle *dhandle = (struct docio_handle *)handle;
size_id = sizeof(fdb_kvs_id_t);
size_seq = sizeof(fdb_seqnum_t);
size_chunk = dhandle->file->config->chunksize;
memset(&doc, 0, sizeof(struct docio_object));
offset = _endian_decode(offset);
docio_read_doc_key_meta((struct docio_handle *)handle, offset, &doc,
true);
buf2buf(size_chunk, doc.key, size_id, buf);
_seqnum = _endian_encode(doc.seqnum);
memcpy((uint8_t*)buf + size_id, &_seqnum, size_seq);
free(doc.key);
free(doc.meta);
return size_id + size_seq;
}
int _fdb_custom_cmp_wrap(void *key1, void *key2, void *aux)
{
int is_key1_inf, is_key2_inf;
uint8_t *keystr1 = alca(uint8_t, FDB_MAX_KEYLEN_INTERNAL);
uint8_t *keystr2 = alca(uint8_t, FDB_MAX_KEYLEN_INTERNAL);
size_t keylen1, keylen2;
btree_cmp_args *args = (btree_cmp_args *)aux;
fdb_custom_cmp_variable cmp = (fdb_custom_cmp_variable)args->aux;
is_key1_inf = _is_inf_key(key1);
is_key2_inf = _is_inf_key(key2);
if (is_key1_inf && is_key2_inf) { // both are infinite
return 0;
} else if (!is_key1_inf && is_key2_inf) { // key2 is infinite
return -1;
} else if (is_key1_inf && !is_key2_inf) { // key1 is infinite
return 1;
}
_get_var_key(key1, (void*)keystr1, &keylen1);
_get_var_key(key2, (void*)keystr2, &keylen2);
if (keylen1 == 0 && keylen2 == 0) {
return 0;
} else if (keylen1 ==0 && keylen2 > 0) {
return -1;
} else if (keylen1 > 0 && keylen2 == 0) {
return 1;
}
return cmp(keystr1, keylen1, keystr2, keylen2);
}
void fdb_fetch_header(void *header_buf,
bid_t *trie_root_bid,
bid_t *seq_root_bid,
uint64_t *ndocs,
uint64_t *nlivenodes,
uint64_t *datasize,
uint64_t *last_wal_flush_hdr_bid,
uint64_t *kv_info_offset,
uint64_t *header_flags,
char **new_filename,
char **old_filename)
{
size_t offset = 0;
uint16_t new_filename_len;
uint16_t old_filename_len;
seq_memcpy(trie_root_bid, (uint8_t *)header_buf + offset,
sizeof(bid_t), offset);
*trie_root_bid = _endian_decode(*trie_root_bid);
seq_memcpy(seq_root_bid, (uint8_t *)header_buf + offset,
sizeof(bid_t), offset);
*seq_root_bid = _endian_decode(*seq_root_bid);
seq_memcpy(ndocs, (uint8_t *)header_buf + offset,
sizeof(uint64_t), offset);
*ndocs = _endian_decode(*ndocs);
seq_memcpy(nlivenodes, (uint8_t *)header_buf + offset,
sizeof(uint64_t), offset);
*nlivenodes = _endian_decode(*nlivenodes);
seq_memcpy(datasize, (uint8_t *)header_buf + offset,
sizeof(uint64_t), offset);
*datasize = _endian_decode(*datasize);
seq_memcpy(last_wal_flush_hdr_bid, (uint8_t *)header_buf + offset,
sizeof(uint64_t), offset);
*last_wal_flush_hdr_bid = _endian_decode(*last_wal_flush_hdr_bid);
seq_memcpy(kv_info_offset, (uint8_t *)header_buf + offset,
sizeof(uint64_t), offset);
*kv_info_offset = _endian_decode(*kv_info_offset);
seq_memcpy(header_flags, (uint8_t *)header_buf + offset,
sizeof(uint64_t), offset);
*header_flags = _endian_decode(*header_flags);
seq_memcpy(&new_filename_len, (uint8_t *)header_buf + offset,
sizeof(new_filename_len), offset);
new_filename_len = _endian_decode(new_filename_len);
seq_memcpy(&old_filename_len, (uint8_t *)header_buf + offset,
sizeof(old_filename_len), offset);
old_filename_len = _endian_decode(old_filename_len);
if (new_filename_len) {
*new_filename = (char*)((uint8_t *)header_buf + offset);
} else {
*new_filename = NULL;
}
offset += new_filename_len;
if (old_filename && old_filename_len) {
*old_filename = (char *) malloc(old_filename_len);
seq_memcpy(*old_filename,
(uint8_t *)header_buf + offset,
old_filename_len, offset);
}
}
INLINE void _fdb_restore_wal(fdb_kvs_handle *handle,
fdb_restore_mode_t mode,
bid_t hdr_bid,
fdb_kvs_id_t kv_id_req)
{
struct filemgr *file = handle->file;
uint32_t blocksize = handle->file->blocksize;
uint64_t last_wal_flush_hdr_bid = handle->last_wal_flush_hdr_bid;
uint64_t hdr_off = hdr_bid * FDB_BLOCKSIZE;
uint64_t offset = 0; //assume everything from first block needs restoration
err_log_callback *log_callback;
if (!hdr_off) { // Nothing to do if we don't have a header block offset
return;
}
if (last_wal_flush_hdr_bid != BLK_NOT_FOUND) {
offset = (last_wal_flush_hdr_bid + 1) * blocksize;
}
// If a valid last header was retrieved and it matches the current header
// OR if WAL already had entries populated, then no crash recovery needed
if (hdr_off <= offset ||
(!handle->shandle && wal_get_size(file) &&
mode != FDB_RESTORE_KV_INS)) {
return;
}
// Temporarily disable the error logging callback as there are false positive
// checksum errors in docio_read_doc.
// TODO: Need to adapt docio_read_doc to separate false checksum errors.
log_callback = handle->dhandle->log_callback;
handle->dhandle->log_callback = NULL;
if (!handle->shandle) {
filemgr_mutex_lock(file);
}
for (; offset < hdr_off;
offset = ((offset / blocksize) + 1) * blocksize) { // next block's off
if (!docio_check_buffer(handle->dhandle, offset / blocksize)) {
continue;
} else {
do {
struct docio_object doc;
uint64_t _offset;
uint64_t doc_offset;
memset(&doc, 0, sizeof(doc));
_offset = docio_read_doc(handle->dhandle, offset, &doc, true);
if (_offset == offset) { // reached unreadable doc, skip block
break;
}
if (doc.key || (doc.length.flag & DOCIO_TXN_COMMITTED)) {
// check if the doc is transactional or not, and
// also check if the doc contains system info
if (!(doc.length.flag & DOCIO_TXN_DIRTY) &&
!(doc.length.flag & DOCIO_SYSTEM)) {
if (doc.length.flag & DOCIO_TXN_COMMITTED) {
// commit mark .. read doc offset
doc_offset = doc.doc_offset;
// read the previously skipped doc
docio_read_doc(handle->dhandle, doc_offset, &doc, true);
if (doc.key == NULL) { // doc read error
free(doc.meta);
free(doc.body);
offset = _offset;
continue;
}
} else {
doc_offset = offset;
}
// If say a snapshot is taken on a db handle after
// rollback, then skip WAL items after rollback point
if (handle->config.seqtree_opt == FDB_SEQTREE_USE &&
(mode == FDB_RESTORE_KV_INS || !handle->kvs) &&
doc.seqnum > handle->seqnum) {
free(doc.key);
free(doc.meta);
free(doc.body);
offset = _offset;
continue;
}
// restore document
fdb_doc wal_doc;
wal_doc.keylen = doc.length.keylen;
wal_doc.bodylen = doc.length.bodylen;
wal_doc.key = doc.key;
wal_doc.seqnum = doc.seqnum;
wal_doc.deleted = doc.length.flag & DOCIO_DELETED;
if (!handle->shandle) {
wal_doc.metalen = doc.length.metalen;
wal_doc.meta = doc.meta;
wal_doc.size_ondisk = _fdb_get_docsize(doc.length);
if (handle->kvs) {
// check seqnum before insert
fdb_kvs_id_t kv_id;
fdb_seqnum_t kv_seqnum;
buf2kvid(handle->config.chunksize,
wal_doc.key, &kv_id);
if (handle->config.seqtree_opt == FDB_SEQTREE_USE) {
kv_seqnum = fdb_kvs_get_seqnum(handle->file, kv_id);
} else {
kv_seqnum = SEQNUM_NOT_USED;
}
if (doc.seqnum <= kv_seqnum &&
((mode == FDB_RESTORE_KV_INS &&
kv_id == kv_id_req) ||
(mode == FDB_RESTORE_NORMAL)) ) {
// if mode is NORMAL, restore all items
// if mode is KV_INS, restore items matching ID
wal_insert(&file->global_txn, file,
&wal_doc, doc_offset,
WAL_INS_WRITER);
}
} else {
wal_insert(&file->global_txn, file,
&wal_doc, doc_offset,
WAL_INS_WRITER);
}
if (doc.key) free(doc.key);
} else {
// snapshot
if (handle->kvs) {
fdb_kvs_id_t kv_id;
buf2kvid(handle->config.chunksize,
wal_doc.key, &kv_id);
if (kv_id == handle->kvs->id) {
// snapshot: insert ID matched documents only
snap_insert(handle->shandle,
&wal_doc, doc_offset);
} else {
free(doc.key);
}
} else {
snap_insert(handle->shandle, &wal_doc, doc_offset);
}
}
free(doc.meta);
free(doc.body);
offset = _offset;
} else {
// skip transactional document or system document
free(doc.key);
free(doc.meta);
free(doc.body);
offset = _offset;
// do not break.. read next doc
}
} else {
free(doc.key);
free(doc.meta);
free(doc.body);
offset = _offset;
break;
}
} while (offset + sizeof(struct docio_length) < hdr_off);
}
}
// wal commit
if (!handle->shandle) {
wal_commit(&file->global_txn, file, NULL, &handle->log_callback);
filemgr_mutex_unlock(file);
}
handle->dhandle->log_callback = log_callback;
}
INLINE fdb_status _fdb_recover_compaction(fdb_kvs_handle *handle,
const char *new_filename)
{
fdb_kvs_handle new_db;
fdb_config config = handle->config;
struct filemgr *new_file;
memset(&new_db, 0, sizeof(new_db));
new_db.log_callback.callback = handle->log_callback.callback;
new_db.log_callback.ctx_data = handle->log_callback.ctx_data;
config.flags |= FDB_OPEN_FLAG_RDONLY;
new_db.fhandle = handle->fhandle;
new_db.kvs_config = handle->kvs_config;
fdb_status status = _fdb_open(&new_db, new_filename,
FDB_AFILENAME, &config);
if (status != FDB_RESULT_SUCCESS) {
return fdb_log(&handle->log_callback, status,
"Error in opening a partially compacted file '%s' for recovery.",
new_filename);
}
new_file = new_db.file;
if (new_file->old_filename &&
!strncmp(new_file->old_filename, handle->file->filename,
FDB_MAX_FILENAME_LEN)) {
struct filemgr *old_file = handle->file;
// If new file has a recorded old_filename then it means that
// compaction has completed successfully. Mark self for deletion
filemgr_mutex_lock(new_file);
status = btreeblk_end(handle->bhandle);
if (status != FDB_RESULT_SUCCESS) {
filemgr_mutex_unlock(new_file);
_fdb_close(&new_db);
return status;
}
btreeblk_free(handle->bhandle);
free(handle->bhandle);
handle->bhandle = new_db.bhandle;
docio_free(handle->dhandle);
free(handle->dhandle);
handle->dhandle = new_db.dhandle;
hbtrie_free(handle->trie);
free(handle->trie);
handle->trie = new_db.trie;
wal_shutdown(handle->file);
handle->file = new_file;
if (handle->config.seqtree_opt == FDB_SEQTREE_USE) {
if (handle->kvs) {
// multi KV instance mode
hbtrie_free(handle->seqtrie);
free(handle->seqtrie);
if (new_db.config.seqtree_opt == FDB_SEQTREE_USE) {
handle->seqtrie = new_db.seqtrie;
}
} else {
free(handle->seqtree->kv_ops);
free(handle->seqtree);
if (new_db.config.seqtree_opt == FDB_SEQTREE_USE) {
handle->seqtree = new_db.seqtree;
}
}
}
filemgr_mutex_unlock(new_file);
if (new_db.kvs) {
fdb_kvs_info_free(&new_db);
}
// remove self: WARNING must not close this handle if snapshots
// are yet to open this file
filemgr_remove_pending(old_file, new_db.file, &new_db.log_callback);
filemgr_close(old_file, 0, handle->filename, &handle->log_callback);
free(new_db.filename);
return FDB_RESULT_FAIL_BY_COMPACTION;
}
// As the new file is partially compacted, it should be removed upon close.
// Just in-case the new file gets opened before removal, point it to the old
// file to ensure availability of data.
filemgr_remove_pending(new_db.file, handle->file, &handle->log_callback);
_fdb_close(&new_db);
return FDB_RESULT_SUCCESS;
}
LIBFDB_API
fdb_status fdb_init(fdb_config *config)
{
fdb_config _config;
compactor_config c_config;
bgflusher_config bgf_config;
struct filemgr_config f_config;
if (config) {
if (validate_fdb_config(config)) {
_config = *config;
} else {
return FDB_RESULT_INVALID_CONFIG;
}
} else {
_config = get_default_config();
}
// global initialization
// initialized only once at first time
if (!fdb_initialized) {
#ifdef _TRACE_HANDLES
spin_init(&open_handle_lock);
avl_init(&open_handles, NULL);
#endif
#ifndef SPIN_INITIALIZER
// Note that only Windows passes through this routine
if (InterlockedCompareExchange(&initial_lock_status, 1, 0) == 0) {
// atomically initialize spin lock only once
spin_init(&initial_lock);
initial_lock_status = 2;
} else {
// the others .. wait until initializing 'initial_lock' is done
while (initial_lock_status != 2) {
Sleep(1);
}
}
#endif
}
spin_lock(&initial_lock);
if (!fdb_initialized) {
#if !defined(_ANDROID_) && !defined(__ANDROID__)
// Some Android devices (e.g., Nexus 6) return incorrect RAM size.
// We temporarily disable validity checking of block cache size
// on Android platform at this time.
double ram_size = (double) get_memory_size();
if (ram_size * BCACHE_MEMORY_THRESHOLD < (double) _config.buffercache_size) {
spin_unlock(&initial_lock);
return FDB_RESULT_TOO_BIG_BUFFER_CACHE;
}
#endif
// initialize file manager and block cache
f_config.blocksize = _config.blocksize;
f_config.ncacheblock = _config.buffercache_size / _config.blocksize;
filemgr_init(&f_config);
filemgr_set_lazy_file_deletion(true,
compactor_register_file_removing,
compactor_is_file_removed);
// initialize compaction daemon
c_config.sleep_duration = _config.compactor_sleep_duration;
c_config.num_threads = _config.num_compactor_threads;
compactor_init(&c_config);
// initialize background flusher daemon
bgf_config.num_threads = _config.num_bgflusher_threads;
bgflusher_init(&bgf_config);
fdb_initialized = 1;
}
fdb_open_inprog++;
spin_unlock(&initial_lock);
return FDB_RESULT_SUCCESS;
}
LIBFDB_API
fdb_config fdb_get_default_config(void) {
return get_default_config();
}
LIBFDB_API
fdb_kvs_config fdb_get_default_kvs_config(void) {
return get_default_kvs_config();
}
LIBFDB_API
fdb_status fdb_open(fdb_file_handle **ptr_fhandle,
const char *filename,
fdb_config *fconfig)
{
#ifdef _MEMPOOL
mempool_init();
#endif
fdb_config config;
fdb_file_handle *fhandle;
fdb_kvs_handle *handle;
if (fconfig) {
if (validate_fdb_config(fconfig)) {
config = *fconfig;
} else {
return FDB_RESULT_INVALID_CONFIG;
}
} else {
config = get_default_config();
}
fhandle = (fdb_file_handle*)calloc(1, sizeof(fdb_file_handle));
if (!fhandle) { // LCOV_EXCL_START
return FDB_RESULT_ALLOC_FAIL;
} // LCOV_EXCL_STOP
handle = (fdb_kvs_handle *) calloc(1, sizeof(fdb_kvs_handle));
if (!handle) { // LCOV_EXCL_START
free(fhandle);
return FDB_RESULT_ALLOC_FAIL;
} // LCOV_EXCL_STOP
atomic_init_uint8_t(&handle->handle_busy, 0);
handle->shandle = NULL;
handle->kvs_config = get_default_kvs_config();
fdb_status fs = fdb_init(fconfig);
if (fs != FDB_RESULT_SUCCESS) {
free(handle);
free(fhandle);
return fs;
}
fdb_file_handle_init(fhandle, handle);
fs = _fdb_open(handle, filename, FDB_VFILENAME, &config);
if (fs == FDB_RESULT_SUCCESS) {
*ptr_fhandle = fhandle;
} else {
*ptr_fhandle = NULL;
free(handle);
fdb_file_handle_free(fhandle);
}
spin_lock(&initial_lock);
fdb_open_inprog--;
spin_unlock(&initial_lock);
return fs;
}
LIBFDB_API
fdb_status fdb_open_custom_cmp(fdb_file_handle **ptr_fhandle,
const char *filename,
fdb_config *fconfig,
size_t num_functions,
char **kvs_names,
fdb_custom_cmp_variable *functions)
{
#ifdef _MEMPOOL
mempool_init();
#endif
fdb_config config;
fdb_file_handle *fhandle;
fdb_kvs_handle *handle;
if (fconfig) {
if (validate_fdb_config(fconfig)) {
config = *fconfig;
} else {
return FDB_RESULT_INVALID_CONFIG;
}
} else {
config = get_default_config();
}
if (config.multi_kv_instances == false) {
// single KV instance mode does not support customized cmp function
return FDB_RESULT_INVALID_CONFIG;
}
fhandle = (fdb_file_handle*)calloc(1, sizeof(fdb_file_handle));
if (!fhandle) { // LCOV_EXCL_START
return FDB_RESULT_ALLOC_FAIL;
} // LCOV_EXCL_STOP
handle = (fdb_kvs_handle *) calloc(1, sizeof(fdb_kvs_handle));
if (!handle) { // LCOV_EXCL_START
free(fhandle);
return FDB_RESULT_ALLOC_FAIL;
} // LCOV_EXCL_STOP
atomic_init_uint8_t(&handle->handle_busy, 0);
handle->shandle = NULL;
handle->kvs_config = get_default_kvs_config();
fdb_status fs = fdb_init(fconfig);
if (fs != FDB_RESULT_SUCCESS) {
free(handle);
free(fhandle);
return fs;
}
fdb_file_handle_init(fhandle, handle);
// insert kvs_names and functions into fhandle's list
fdb_file_handle_parse_cmp_func(fhandle, num_functions,
kvs_names, functions);
fs = _fdb_open(handle, filename, FDB_VFILENAME, &config);
if (fs == FDB_RESULT_SUCCESS) {
*ptr_fhandle = fhandle;
} else {
*ptr_fhandle = NULL;
free(handle);
fdb_file_handle_free(fhandle);
}
spin_lock(&initial_lock);
fdb_open_inprog--;
spin_unlock(&initial_lock);
return fs;
}
fdb_status fdb_open_for_compactor(fdb_file_handle **ptr_fhandle,
const char *filename,
fdb_config *fconfig,
struct list *cmp_func_list)
{
#ifdef _MEMPOOL
mempool_init();
#endif
fdb_file_handle *fhandle;
fdb_kvs_handle *handle;
fhandle = (fdb_file_handle*)calloc(1, sizeof(fdb_file_handle));
if (!fhandle) { // LCOV_EXCL_START
return FDB_RESULT_ALLOC_FAIL;
} // LCOV_EXCL_STOP
handle = (fdb_kvs_handle *) calloc(1, sizeof(fdb_kvs_handle));
if (!handle) { // LCOV_EXCL_START
free(fhandle);
return FDB_RESULT_ALLOC_FAIL;
} // LCOV_EXCL_STOP
atomic_init_uint8_t(&handle->handle_busy, 0);
handle->shandle = NULL;
fdb_file_handle_init(fhandle, handle);
if (cmp_func_list && list_begin(cmp_func_list)) {
fdb_file_handle_clone_cmp_func_list(fhandle, cmp_func_list);
}
fdb_status fs = _fdb_open(handle, filename, FDB_VFILENAME, fconfig);
if (fs == FDB_RESULT_SUCCESS) {
*ptr_fhandle = fhandle;
} else {
*ptr_fhandle = NULL;
free(handle);
fdb_file_handle_free(fhandle);
}
return fs;
}
LIBFDB_API
fdb_status fdb_snapshot_open(fdb_kvs_handle *handle_in,
fdb_kvs_handle **ptr_handle, fdb_seqnum_t seqnum)
{
#ifdef _MEMPOOL
mempool_init();
#endif
fdb_config config = handle_in->config;
fdb_kvs_config kvs_config = handle_in->kvs_config;
fdb_kvs_handle *handle;
fdb_status fs;
filemgr *file;
file_status_t fstatus = FILE_NORMAL;
if (!handle_in || !ptr_handle) {
return FDB_RESULT_INVALID_ARGS;
}
// Sequence trees are a must for snapshot creation
if (handle_in->config.seqtree_opt != FDB_SEQTREE_USE) {
return FDB_RESULT_INVALID_CONFIG;
}
fdb_snapshot_open_start:
if (!handle_in->shandle) {
fdb_check_file_reopen(handle_in, &fstatus);
fdb_sync_db_header(handle_in);
file = handle_in->file;
if (handle_in->kvs && handle_in->kvs->type == KVS_SUB) {
handle_in->seqnum = fdb_kvs_get_seqnum(file,
handle_in->kvs->id);
} else {
handle_in->seqnum = filemgr_get_seqnum(file);
}
} else {
file = handle_in->file;
}
// if the max sequence number seen by this handle is lower than the
// requested snapshot marker, it means the snapshot is not yet visible
// even via the current fdb_kvs_handle
if (seqnum != FDB_SNAPSHOT_INMEM && seqnum > handle_in->seqnum) {
return FDB_RESULT_NO_DB_INSTANCE;
}
handle = (fdb_kvs_handle *) calloc(1, sizeof(fdb_kvs_handle));
if (!handle) { // LCOV_EXCL_START
return FDB_RESULT_ALLOC_FAIL;
} // LCOV_EXCL_STOP
atomic_init_uint8_t(&handle->handle_busy, 0);
handle->log_callback = handle_in->log_callback;
handle->max_seqnum = seqnum;
handle->fhandle = handle_in->fhandle;
config.flags |= FDB_OPEN_FLAG_RDONLY;
// do not perform compaction for snapshot
config.compaction_mode = FDB_COMPACTION_MANUAL;
// If cloning an existing snapshot handle, then rewind indexes
// to its last DB header and point its avl tree to existing snapshot's tree
bool clone_snapshot = false;
if (handle_in->shandle) {
handle->last_hdr_bid = handle_in->last_hdr_bid; // do fast rewind
if (snap_clone(handle_in->shandle, handle_in->max_seqnum,
&handle->shandle, seqnum) == FDB_RESULT_SUCCESS) {
handle->max_seqnum = FDB_SNAPSHOT_INMEM; // temp value to skip WAL
clone_snapshot = true;
}
}
if (!handle->shandle) {
handle->shandle = (struct snap_handle *) calloc(1, sizeof(snap_handle));
if (!handle->shandle) { // LCOV_EXCL_START
free(handle);
return FDB_RESULT_ALLOC_FAIL;
} // LCOV_EXCL_STOP
snap_init(handle->shandle, handle_in);
}
if (handle_in->kvs) {
// sub-handle in multi KV instance mode
if (clone_snapshot) {
fs = _fdb_kvs_clone_snapshot(handle_in, handle);
} else {
fs = _fdb_kvs_open(handle_in->kvs->root,
&config, &kvs_config, file,
file->filename,
_fdb_kvs_get_name(handle_in, file),
handle);
}
} else {
if (clone_snapshot) {
fs = _fdb_clone_snapshot(handle_in, handle);
} else {
fs = _fdb_open(handle, file->filename, FDB_AFILENAME, &config);
}
}
if (fs == FDB_RESULT_SUCCESS) {
if (seqnum == FDB_SNAPSHOT_INMEM &&
!handle_in->shandle) {
fdb_seqnum_t upto_seq = seqnum;
// In-memory snapshot
wal_snapshot(handle->file, (void *)handle->shandle,
handle_in->txn, &upto_seq, _fdb_wal_snapshot_func);
// set seqnum based on handle type (multikv or default)
if (handle_in->kvs && handle_in->kvs->id > 0) {
handle->max_seqnum =
_fdb_kvs_get_seqnum(handle->file->kv_header,
handle_in->kvs->id);
} else {
handle->max_seqnum = filemgr_get_seqnum(handle->file);
}
// synchronize dirty root nodes if exist
if (filemgr_dirty_root_exist(handle->file)) {
bid_t dirty_idtree_root, dirty_seqtree_root;
filemgr_mutex_lock(handle->file);
filemgr_get_dirty_root(handle->file,
&dirty_idtree_root, &dirty_seqtree_root);
if (dirty_idtree_root != BLK_NOT_FOUND) {
handle->trie->root_bid = dirty_idtree_root;
}
if (handle->config.seqtree_opt == FDB_SEQTREE_USE) {
if (dirty_seqtree_root != BLK_NOT_FOUND) {
if (handle->kvs) {
handle->seqtrie->root_bid = dirty_seqtree_root;
} else {
btree_init_from_bid(handle->seqtree,
handle->seqtree->blk_handle,
handle->seqtree->blk_ops,
handle->seqtree->kv_ops,
handle->seqtree->blksize,
dirty_seqtree_root);
}
}
}
btreeblk_discard_blocks(handle->bhandle);
btreeblk_create_dirty_snapshot(handle->bhandle);
filemgr_mutex_unlock(handle->file);
}
} else if (clone_snapshot) {
// Snapshot is created on the other snapshot handle
handle->max_seqnum = handle_in->seqnum;
if (seqnum == FDB_SNAPSHOT_INMEM) {
// in-memory snapshot
// Clone dirty root nodes from the source snapshot by incrementing
// their ref counters
handle->trie->root_bid = handle_in->trie->root_bid;
if (handle->config.seqtree_opt == FDB_SEQTREE_USE) {
if (handle->kvs) {
handle->seqtrie->root_bid = handle_in->seqtrie->root_bid;
} else {
handle->seqtree->root_bid = handle_in->seqtree->root_bid;
}
}
btreeblk_discard_blocks(handle->bhandle);
btreeblk_clone_dirty_snapshot(handle->bhandle,
handle_in->bhandle);
}
}
*ptr_handle = handle;
} else {
*ptr_handle = NULL;
snap_close(handle->shandle);
free(handle);
// If compactor thread had finished compaction just before this routine
// calls _fdb_open, then it is possible that the snapshot's DB header
// is only present in the new_file. So we must retry the snapshot
// open attempt IFF _fdb_open indicates FDB_RESULT_NO_DB_INSTANCE..
if (fs == FDB_RESULT_NO_DB_INSTANCE && fstatus == FILE_COMPACT_OLD) {
if (filemgr_get_file_status(file) == FILE_REMOVED_PENDING) {
goto fdb_snapshot_open_start;
}
}
}
return fs;
}
static fdb_status _fdb_reset(fdb_kvs_handle *handle, fdb_kvs_handle *handle_in);
LIBFDB_API
fdb_status fdb_rollback(fdb_kvs_handle **handle_ptr, fdb_seqnum_t seqnum)
{
#ifdef _MEMPOOL
mempool_init();
#endif
fdb_config config;
fdb_kvs_handle *handle_in, *handle;
fdb_status fs;
fdb_seqnum_t old_seqnum;
if (!handle_ptr) {
return FDB_RESULT_INVALID_ARGS;
}
handle_in = *handle_ptr;
config = handle_in->config;
if (handle_in->kvs) {
return fdb_kvs_rollback(handle_ptr, seqnum);
}
// Sequence trees are a must for rollback
if (handle_in->config.seqtree_opt != FDB_SEQTREE_USE) {
return FDB_RESULT_INVALID_CONFIG;
}
if (handle_in->config.flags & FDB_OPEN_FLAG_RDONLY) {
return fdb_log(&handle_in->log_callback, FDB_RESULT_RONLY_VIOLATION,
"Warning: Rollback is not allowed on the read-only DB file '%s'.",
handle_in->file->filename);
}
if (!atomic_cas_uint8_t(&handle_in->handle_busy, 0, 1)) {
return FDB_RESULT_HANDLE_BUSY;
}
filemgr_mutex_lock(handle_in->file);
filemgr_set_rollback(handle_in->file, 1); // disallow writes operations
// All transactions should be closed before rollback
if (wal_txn_exists(handle_in->file)) {
filemgr_set_rollback(handle_in->file, 0);
filemgr_mutex_unlock(handle_in->file);
atomic_cas_uint8_t(&handle_in->handle_busy, 1, 0);
return FDB_RESULT_FAIL_BY_TRANSACTION;
}
// If compaction is running, wait until it is aborted.
// TODO: Find a better way of waiting for the compaction abortion.
unsigned int sleep_time = 10000; // 10 ms.
file_status_t fstatus = filemgr_get_file_status(handle_in->file);
while (fstatus == FILE_COMPACT_OLD) {
filemgr_mutex_unlock(handle_in->file);
decaying_usleep(&sleep_time, 1000000);
filemgr_mutex_lock(handle_in->file);
fstatus = filemgr_get_file_status(handle_in->file);
}
if (fstatus == FILE_REMOVED_PENDING) {
filemgr_mutex_unlock(handle_in->file);
fdb_check_file_reopen(handle_in, NULL);
} else {
filemgr_mutex_unlock(handle_in->file);
}
fdb_sync_db_header(handle_in);