1 // Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 
6 #include "options/db_options.h"
7 
8 #include <cinttypes>
9 
10 #include "db/version_edit.h"
11 #include "logging/logging.h"
12 #include "port/port.h"
13 #include "rocksdb/cache.h"
14 #include "rocksdb/env.h"
15 #include "rocksdb/file_system.h"
16 #include "rocksdb/sst_file_manager.h"
17 #include "rocksdb/wal_filter.h"
18 
19 namespace ROCKSDB_NAMESPACE {
20 
ImmutableDBOptions()21 ImmutableDBOptions::ImmutableDBOptions() : ImmutableDBOptions(Options()) {}
22 
ImmutableDBOptions(const DBOptions & options)23 ImmutableDBOptions::ImmutableDBOptions(const DBOptions& options)
24     : create_if_missing(options.create_if_missing),
25       create_missing_column_families(options.create_missing_column_families),
26       error_if_exists(options.error_if_exists),
27       paranoid_checks(options.paranoid_checks),
28       env(options.env),
29       fs(options.env->GetFileSystem()),
30       rate_limiter(options.rate_limiter),
31       sst_file_manager(options.sst_file_manager),
32       info_log(options.info_log),
33       info_log_level(options.info_log_level),
34       max_file_opening_threads(options.max_file_opening_threads),
35       statistics(options.statistics),
36       use_fsync(options.use_fsync),
37       db_paths(options.db_paths),
38       db_log_dir(options.db_log_dir),
39       wal_dir(options.wal_dir),
40       max_subcompactions(options.max_subcompactions),
41       max_background_flushes(options.max_background_flushes),
42       max_log_file_size(options.max_log_file_size),
43       log_file_time_to_roll(options.log_file_time_to_roll),
44       keep_log_file_num(options.keep_log_file_num),
45       recycle_log_file_num(options.recycle_log_file_num),
46       max_manifest_file_size(options.max_manifest_file_size),
47       table_cache_numshardbits(options.table_cache_numshardbits),
48       wal_ttl_seconds(options.WAL_ttl_seconds),
49       wal_size_limit_mb(options.WAL_size_limit_MB),
50       max_write_batch_group_size_bytes(
51           options.max_write_batch_group_size_bytes),
52       manifest_preallocation_size(options.manifest_preallocation_size),
53       allow_mmap_reads(options.allow_mmap_reads),
54       allow_mmap_writes(options.allow_mmap_writes),
55       use_direct_reads(options.use_direct_reads),
56       use_direct_io_for_flush_and_compaction(
57           options.use_direct_io_for_flush_and_compaction),
58       allow_fallocate(options.allow_fallocate),
59       is_fd_close_on_exec(options.is_fd_close_on_exec),
60       advise_random_on_open(options.advise_random_on_open),
61       db_write_buffer_size(options.db_write_buffer_size),
62       write_buffer_manager(options.write_buffer_manager),
63       access_hint_on_compaction_start(options.access_hint_on_compaction_start),
64       new_table_reader_for_compaction_inputs(
65           options.new_table_reader_for_compaction_inputs),
66       random_access_max_buffer_size(options.random_access_max_buffer_size),
67       use_adaptive_mutex(options.use_adaptive_mutex),
68       listeners(options.listeners),
69       enable_thread_tracking(options.enable_thread_tracking),
70       enable_pipelined_write(options.enable_pipelined_write),
71       unordered_write(options.unordered_write),
72       allow_concurrent_memtable_write(options.allow_concurrent_memtable_write),
73       enable_write_thread_adaptive_yield(
74           options.enable_write_thread_adaptive_yield),
75       write_thread_max_yield_usec(options.write_thread_max_yield_usec),
76       write_thread_slow_yield_usec(options.write_thread_slow_yield_usec),
77       skip_stats_update_on_db_open(options.skip_stats_update_on_db_open),
78       skip_checking_sst_file_sizes_on_db_open(
79           options.skip_checking_sst_file_sizes_on_db_open),
80       wal_recovery_mode(options.wal_recovery_mode),
81       allow_2pc(options.allow_2pc),
82       row_cache(options.row_cache),
83 #ifndef ROCKSDB_LITE
84       wal_filter(options.wal_filter),
85 #endif  // ROCKSDB_LITE
86       fail_if_options_file_error(options.fail_if_options_file_error),
87       dump_malloc_stats(options.dump_malloc_stats),
88       avoid_flush_during_recovery(options.avoid_flush_during_recovery),
89       allow_ingest_behind(options.allow_ingest_behind),
90       preserve_deletes(options.preserve_deletes),
91       two_write_queues(options.two_write_queues),
92       manual_wal_flush(options.manual_wal_flush),
93       atomic_flush(options.atomic_flush),
94       avoid_unnecessary_blocking_io(options.avoid_unnecessary_blocking_io),
95       persist_stats_to_disk(options.persist_stats_to_disk),
96       write_dbid_to_manifest(options.write_dbid_to_manifest),
97       log_readahead_size(options.log_readahead_size),
98       file_checksum_gen_factory(options.file_checksum_gen_factory),
99       best_efforts_recovery(options.best_efforts_recovery) {
100 }
101 
Dump(Logger * log) const102 void ImmutableDBOptions::Dump(Logger* log) const {
103   ROCKS_LOG_HEADER(log, "                        Options.error_if_exists: %d",
104                    error_if_exists);
105   ROCKS_LOG_HEADER(log, "                      Options.create_if_missing: %d",
106                    create_if_missing);
107   ROCKS_LOG_HEADER(log, "                        Options.paranoid_checks: %d",
108                    paranoid_checks);
109   ROCKS_LOG_HEADER(log, "                                    Options.env: %p",
110                    env);
111   ROCKS_LOG_HEADER(log, "                                     Options.fs: %s",
112                    fs->Name());
113   ROCKS_LOG_HEADER(log, "                               Options.info_log: %p",
114                    info_log.get());
115   ROCKS_LOG_HEADER(log, "               Options.max_file_opening_threads: %d",
116                    max_file_opening_threads);
117   ROCKS_LOG_HEADER(log, "                             Options.statistics: %p",
118                    statistics.get());
119   ROCKS_LOG_HEADER(log, "                              Options.use_fsync: %d",
120                    use_fsync);
121   ROCKS_LOG_HEADER(
122       log, "                      Options.max_log_file_size: %" ROCKSDB_PRIszt,
123       max_log_file_size);
124   ROCKS_LOG_HEADER(log,
125                    "                 Options.max_manifest_file_size: %" PRIu64,
126                    max_manifest_file_size);
127   ROCKS_LOG_HEADER(
128       log, "                  Options.log_file_time_to_roll: %" ROCKSDB_PRIszt,
129       log_file_time_to_roll);
130   ROCKS_LOG_HEADER(
131       log, "                      Options.keep_log_file_num: %" ROCKSDB_PRIszt,
132       keep_log_file_num);
133   ROCKS_LOG_HEADER(
134       log, "                   Options.recycle_log_file_num: %" ROCKSDB_PRIszt,
135       recycle_log_file_num);
136   ROCKS_LOG_HEADER(log, "                        Options.allow_fallocate: %d",
137                    allow_fallocate);
138   ROCKS_LOG_HEADER(log, "                       Options.allow_mmap_reads: %d",
139                    allow_mmap_reads);
140   ROCKS_LOG_HEADER(log, "                      Options.allow_mmap_writes: %d",
141                    allow_mmap_writes);
142   ROCKS_LOG_HEADER(log, "                       Options.use_direct_reads: %d",
143                    use_direct_reads);
144   ROCKS_LOG_HEADER(log,
145                    "                       "
146                    "Options.use_direct_io_for_flush_and_compaction: %d",
147                    use_direct_io_for_flush_and_compaction);
148   ROCKS_LOG_HEADER(log, "         Options.create_missing_column_families: %d",
149                    create_missing_column_families);
150   ROCKS_LOG_HEADER(log, "                             Options.db_log_dir: %s",
151                    db_log_dir.c_str());
152   ROCKS_LOG_HEADER(log, "                                Options.wal_dir: %s",
153                    wal_dir.c_str());
154   ROCKS_LOG_HEADER(log, "               Options.table_cache_numshardbits: %d",
155                    table_cache_numshardbits);
156   ROCKS_LOG_HEADER(log,
157                    "                     Options.max_subcompactions: %" PRIu32,
158                    max_subcompactions);
159   ROCKS_LOG_HEADER(log, "                 Options.max_background_flushes: %d",
160                    max_background_flushes);
161   ROCKS_LOG_HEADER(log,
162                    "                        Options.WAL_ttl_seconds: %" PRIu64,
163                    wal_ttl_seconds);
164   ROCKS_LOG_HEADER(log,
165                    "                      Options.WAL_size_limit_MB: %" PRIu64,
166                    wal_size_limit_mb);
167   ROCKS_LOG_HEADER(log,
168                    "                       "
169                    "Options.max_write_batch_group_size_bytes: %" PRIu64,
170                    max_write_batch_group_size_bytes);
171   ROCKS_LOG_HEADER(
172       log, "            Options.manifest_preallocation_size: %" ROCKSDB_PRIszt,
173       manifest_preallocation_size);
174   ROCKS_LOG_HEADER(log, "                    Options.is_fd_close_on_exec: %d",
175                    is_fd_close_on_exec);
176   ROCKS_LOG_HEADER(log, "                  Options.advise_random_on_open: %d",
177                    advise_random_on_open);
178   ROCKS_LOG_HEADER(
179       log, "                   Options.db_write_buffer_size: %" ROCKSDB_PRIszt,
180       db_write_buffer_size);
181   ROCKS_LOG_HEADER(log, "                   Options.write_buffer_manager: %p",
182                    write_buffer_manager.get());
183   ROCKS_LOG_HEADER(log, "        Options.access_hint_on_compaction_start: %d",
184                    static_cast<int>(access_hint_on_compaction_start));
185   ROCKS_LOG_HEADER(log, " Options.new_table_reader_for_compaction_inputs: %d",
186                    new_table_reader_for_compaction_inputs);
187   ROCKS_LOG_HEADER(
188       log, "          Options.random_access_max_buffer_size: %" ROCKSDB_PRIszt,
189       random_access_max_buffer_size);
190   ROCKS_LOG_HEADER(log, "                     Options.use_adaptive_mutex: %d",
191                    use_adaptive_mutex);
192   ROCKS_LOG_HEADER(log, "                           Options.rate_limiter: %p",
193                    rate_limiter.get());
194   Header(
195       log, "    Options.sst_file_manager.rate_bytes_per_sec: %" PRIi64,
196       sst_file_manager ? sst_file_manager->GetDeleteRateBytesPerSecond() : 0);
197   ROCKS_LOG_HEADER(log, "                      Options.wal_recovery_mode: %d",
198                    static_cast<int>(wal_recovery_mode));
199   ROCKS_LOG_HEADER(log, "                 Options.enable_thread_tracking: %d",
200                    enable_thread_tracking);
201   ROCKS_LOG_HEADER(log, "                 Options.enable_pipelined_write: %d",
202                    enable_pipelined_write);
203   ROCKS_LOG_HEADER(log, "                 Options.unordered_write: %d",
204                    unordered_write);
205   ROCKS_LOG_HEADER(log, "        Options.allow_concurrent_memtable_write: %d",
206                    allow_concurrent_memtable_write);
207   ROCKS_LOG_HEADER(log, "     Options.enable_write_thread_adaptive_yield: %d",
208                    enable_write_thread_adaptive_yield);
209   ROCKS_LOG_HEADER(log,
210                    "            Options.write_thread_max_yield_usec: %" PRIu64,
211                    write_thread_max_yield_usec);
212   ROCKS_LOG_HEADER(log,
213                    "           Options.write_thread_slow_yield_usec: %" PRIu64,
214                    write_thread_slow_yield_usec);
215   if (row_cache) {
216     ROCKS_LOG_HEADER(
217         log,
218         "                              Options.row_cache: %" ROCKSDB_PRIszt,
219         row_cache->GetCapacity());
220   } else {
221     ROCKS_LOG_HEADER(log,
222                      "                              Options.row_cache: None");
223   }
224 #ifndef ROCKSDB_LITE
225   ROCKS_LOG_HEADER(log, "                             Options.wal_filter: %s",
226                    wal_filter ? wal_filter->Name() : "None");
227 #endif  // ROCKDB_LITE
228 
229   ROCKS_LOG_HEADER(log, "            Options.avoid_flush_during_recovery: %d",
230                    avoid_flush_during_recovery);
231   ROCKS_LOG_HEADER(log, "            Options.allow_ingest_behind: %d",
232                    allow_ingest_behind);
233   ROCKS_LOG_HEADER(log, "            Options.preserve_deletes: %d",
234                    preserve_deletes);
235   ROCKS_LOG_HEADER(log, "            Options.two_write_queues: %d",
236                    two_write_queues);
237   ROCKS_LOG_HEADER(log, "            Options.manual_wal_flush: %d",
238                    manual_wal_flush);
239   ROCKS_LOG_HEADER(log, "            Options.atomic_flush: %d", atomic_flush);
240   ROCKS_LOG_HEADER(log,
241                    "            Options.avoid_unnecessary_blocking_io: %d",
242                    avoid_unnecessary_blocking_io);
243   ROCKS_LOG_HEADER(log, "                Options.persist_stats_to_disk: %u",
244                    persist_stats_to_disk);
245   ROCKS_LOG_HEADER(log, "                Options.write_dbid_to_manifest: %d",
246                    write_dbid_to_manifest);
247   ROCKS_LOG_HEADER(
248       log, "                Options.log_readahead_size: %" ROCKSDB_PRIszt,
249       log_readahead_size);
250   ROCKS_LOG_HEADER(log, "                Options.file_checksum_gen_factory: %s",
251                    file_checksum_gen_factory
252                        ? file_checksum_gen_factory->Name()
253                        : kUnknownFileChecksumFuncName.c_str());
254   ROCKS_LOG_HEADER(log, "                Options.best_efforts_recovery: %d",
255                    static_cast<int>(best_efforts_recovery));
256 }
257 
MutableDBOptions()258 MutableDBOptions::MutableDBOptions()
259     : max_background_jobs(2),
260       base_background_compactions(-1),
261       max_background_compactions(-1),
262       avoid_flush_during_shutdown(false),
263       writable_file_max_buffer_size(1024 * 1024),
264       delayed_write_rate(2 * 1024U * 1024U),
265       max_total_wal_size(0),
266       delete_obsolete_files_period_micros(6ULL * 60 * 60 * 1000000),
267       stats_dump_period_sec(600),
268       stats_persist_period_sec(600),
269       stats_history_buffer_size(1024 * 1024),
270       max_open_files(-1),
271       bytes_per_sync(0),
272       wal_bytes_per_sync(0),
273       strict_bytes_per_sync(false),
274       compaction_readahead_size(0) {}
275 
MutableDBOptions(const DBOptions & options)276 MutableDBOptions::MutableDBOptions(const DBOptions& options)
277     : max_background_jobs(options.max_background_jobs),
278       base_background_compactions(options.base_background_compactions),
279       max_background_compactions(options.max_background_compactions),
280       avoid_flush_during_shutdown(options.avoid_flush_during_shutdown),
281       writable_file_max_buffer_size(options.writable_file_max_buffer_size),
282       delayed_write_rate(options.delayed_write_rate),
283       max_total_wal_size(options.max_total_wal_size),
284       delete_obsolete_files_period_micros(
285           options.delete_obsolete_files_period_micros),
286       stats_dump_period_sec(options.stats_dump_period_sec),
287       stats_persist_period_sec(options.stats_persist_period_sec),
288       stats_history_buffer_size(options.stats_history_buffer_size),
289       max_open_files(options.max_open_files),
290       bytes_per_sync(options.bytes_per_sync),
291       wal_bytes_per_sync(options.wal_bytes_per_sync),
292       strict_bytes_per_sync(options.strict_bytes_per_sync),
293       compaction_readahead_size(options.compaction_readahead_size) {}
294 
Dump(Logger * log) const295 void MutableDBOptions::Dump(Logger* log) const {
296   ROCKS_LOG_HEADER(log, "            Options.max_background_jobs: %d",
297                    max_background_jobs);
298   ROCKS_LOG_HEADER(log, "            Options.max_background_compactions: %d",
299                    max_background_compactions);
300   ROCKS_LOG_HEADER(log, "            Options.avoid_flush_during_shutdown: %d",
301                    avoid_flush_during_shutdown);
302   ROCKS_LOG_HEADER(
303       log, "          Options.writable_file_max_buffer_size: %" ROCKSDB_PRIszt,
304       writable_file_max_buffer_size);
305   ROCKS_LOG_HEADER(log, "            Options.delayed_write_rate : %" PRIu64,
306                    delayed_write_rate);
307   ROCKS_LOG_HEADER(log, "            Options.max_total_wal_size: %" PRIu64,
308                    max_total_wal_size);
309   ROCKS_LOG_HEADER(
310       log, "            Options.delete_obsolete_files_period_micros: %" PRIu64,
311       delete_obsolete_files_period_micros);
312   ROCKS_LOG_HEADER(log, "                  Options.stats_dump_period_sec: %u",
313                    stats_dump_period_sec);
314   ROCKS_LOG_HEADER(log, "                Options.stats_persist_period_sec: %d",
315                    stats_persist_period_sec);
316   ROCKS_LOG_HEADER(
317       log,
318       "                Options.stats_history_buffer_size: %" ROCKSDB_PRIszt,
319       stats_history_buffer_size);
320   ROCKS_LOG_HEADER(log, "                         Options.max_open_files: %d",
321                    max_open_files);
322   ROCKS_LOG_HEADER(log,
323                    "                         Options.bytes_per_sync: %" PRIu64,
324                    bytes_per_sync);
325   ROCKS_LOG_HEADER(log,
326                    "                     Options.wal_bytes_per_sync: %" PRIu64,
327                    wal_bytes_per_sync);
328   ROCKS_LOG_HEADER(log,
329                    "                  Options.strict_bytes_per_sync: %d",
330                    strict_bytes_per_sync);
331   ROCKS_LOG_HEADER(log,
332                    "      Options.compaction_readahead_size: %" ROCKSDB_PRIszt,
333                    compaction_readahead_size);
334 }
335 
336 }  // namespace ROCKSDB_NAMESPACE
337