xref: /6.0.3/forestdb/src/configuration.cc (revision c1a4cfaa)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2010 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#include <stdint.h>
19#include <string.h>
20
21#include "configuration.h"
22#include "system_resource_stats.h"
23
24static ssize_t prime_size_table[] = {
25    11, 31, 47, 73, 97, 109, 211, 313, 419, -1
26};
27
28fdb_config get_default_config(void) {
29    fdb_config fconfig;
30
31    fconfig.chunksize = sizeof(uint64_t);
32    // 4KB by default.
33    fconfig.blocksize = FDB_BLOCKSIZE;
34    // 128MB by default.
35    fconfig.buffercache_size = 134217728;
36    // 4K WAL entries by default.
37    fconfig.wal_threshold = 4096;
38    fconfig.wal_flush_before_commit = true;
39    fconfig.auto_commit = false;
40    // 0 second by default.
41    fconfig.purging_interval = 0;
42    // Sequnce trees are disabled by default.
43    fconfig.seqtree_opt = FDB_SEQTREE_NOT_USE;
44    // Use a synchronous commit by default.
45    fconfig.durability_opt = FDB_DRB_NONE;
46    fconfig.flags = FDB_OPEN_FLAG_CREATE;
47    // 4MB by default.
48    fconfig.compaction_buf_maxsize = FDB_COMP_BUF_MINSIZE;
49    // Clean up cache entries when a file is closed.
50    fconfig.cleanup_cache_onclose = true;
51    // Compress the body of documents using snappy. Disabled by default.
52    fconfig.compress_document_body = false;
53    // Auto compaction is disabled by default
54    fconfig.compaction_mode = FDB_COMPACTION_MANUAL;
55    // Compaction threshold, 30% by default
56    fconfig.compaction_threshold = FDB_DEFAULT_COMPACTION_THRESHOLD;
57    fconfig.compaction_minimum_filesize = 1048576; // 1MB by default
58    // 8 hours by default
59    fconfig.compactor_sleep_duration = FDB_COMPACTOR_SLEEP_DURATION;
60    // Multi KV Instance mode is enabled by default
61    fconfig.multi_kv_instances = true;
62    // TODO: Re-enable this after prefetch ThreadSanitizer fixes are in..
63    fconfig.prefetch_duration = 0;
64
65    // Determine the number of WAL and buffer cache partitions by considering the
66    // number of cores available in the host environment.
67    int i = 0;
68    ssize_t num_cores = (ssize_t) get_num_cores();
69    for (; prime_size_table[i] > 0 && prime_size_table[i] < num_cores; ++i) {
70        // Finding the smallest prime number that is greater than the number of cores.
71    }
72    if (prime_size_table[i] == -1) {
73        fconfig.num_wal_partitions = prime_size_table[i-1];
74        fconfig.num_bcache_partitions = prime_size_table[i-1];
75    } else {
76        fconfig.num_wal_partitions = prime_size_table[i];
77        // For bcache partitions pick a higher value for smaller avl trees
78        fconfig.num_bcache_partitions = prime_size_table[i];
79    }
80
81    // No compaction callback function by default
82    fconfig.compaction_cb = NULL;
83    fconfig.compaction_cb_mask = 0x0;
84    fconfig.compaction_cb_ctx = NULL;
85    fconfig.max_writer_lock_prob = 100;
86    // 4 daemon compactor threads by default
87    fconfig.num_compactor_threads = DEFAULT_NUM_COMPACTOR_THREADS;
88    fconfig.num_bgflusher_threads = DEFAULT_NUM_BGFLUSHER_THREADS;
89    // Block reusing threshold, 65% by default (i.e., almost 3x space amplification)
90    fconfig.block_reusing_threshold = 65;
91    // Keep at most 5 recent committed database snapshots
92    fconfig.num_keeping_headers = 5;
93
94    fconfig.encryption_key.algorithm = FDB_ENCRYPTION_NONE;
95    memset(fconfig.encryption_key.bytes, 0, sizeof(fconfig.encryption_key.bytes));
96
97    // Breakpad minidump directory, set to current working dir
98    fconfig.breakpad_minidump_dir = ".";
99
100    return fconfig;
101}
102
103fdb_kvs_config get_default_kvs_config(void) {
104    fdb_kvs_config kvs_config;
105
106    // create an empty KV store if it doesn't exist.
107    kvs_config.create_if_missing = true;
108    // lexicographical key order by default
109    kvs_config.custom_cmp = NULL;
110
111    return kvs_config;
112}
113
114bool validate_fdb_config(fdb_config *fconfig) {
115    assert(fconfig);
116
117    if (fconfig->chunksize < 4 || fconfig->chunksize > 64) {
118        // Chunk size should be set between 4 and 64 bytes.
119        return false;
120    }
121    if (fconfig->chunksize < sizeof(void *)) {
122        // Chunk size should be equal to or greater than the address bus size
123        return false;
124    }
125    if (fconfig->blocksize < 1024 || fconfig->blocksize > 131072) {
126        // Block size should be set between 1KB and 128KB
127        return false;
128    }
129    if (fconfig->seqtree_opt != FDB_SEQTREE_NOT_USE &&
130        fconfig->seqtree_opt != FDB_SEQTREE_USE) {
131        return false;
132    }
133    if (fconfig->durability_opt != FDB_DRB_NONE &&
134        fconfig->durability_opt != FDB_DRB_ODIRECT &&
135        fconfig->durability_opt != FDB_DRB_ASYNC &&
136        fconfig->durability_opt != FDB_DRB_ODIRECT_ASYNC) {
137        return false;
138    }
139    if ((fconfig->flags & FDB_OPEN_FLAG_CREATE) &&
140        (fconfig->flags & FDB_OPEN_FLAG_RDONLY)) {
141        return false;
142    }
143    if (fconfig->compaction_threshold > 100) {
144        // Compaction threshold should be equal or less then 100 (%).
145        return false;
146    }
147    if (fconfig->compactor_sleep_duration == 0) {
148        // Sleep duration should be larger than zero
149        return false;
150    }
151    if (!fconfig->num_wal_partitions ||
152        (fconfig->num_wal_partitions > MAX_NUM_WAL_PARTITIONS)) {
153        return false;
154    }
155    if (!fconfig->num_bcache_partitions ||
156        (fconfig->num_bcache_partitions > MAX_NUM_BCACHE_PARTITIONS)) {
157        return false;
158    }
159    if (fconfig->max_writer_lock_prob < 20 ||
160        fconfig->max_writer_lock_prob > 100) {
161        return false;
162    }
163    if (fconfig->num_compactor_threads < 1 ||
164        fconfig->num_compactor_threads > MAX_NUM_COMPACTOR_THREADS) {
165        return false;
166    }
167    if (fconfig->num_bgflusher_threads > MAX_NUM_BGFLUSHER_THREADS) {
168        return false;
169    }
170
171    return true;
172}
173
174bool validate_fdb_kvs_config(fdb_kvs_config *kvs_config) {
175    return true;
176}
177
178