Skip to main content

tuwunel_database/engine/
db_opts.rs

1use std::cmp;
2
3use rocksdb::{Cache, DBRecoveryMode, Env, LogLevel, Options, statistics::StatsLevel};
4use tuwunel_core::{Config, Result, utils};
5
6use super::{cf_opts::cache_size_f64, events::Events, logger::handle as handle_log};
7use crate::util::map_err;
8
9/// Create database-wide options suitable for opening the database. This also
10/// sets our default column options in case of opening a column with the same
11/// resulting value. Note that we require special per-column options on some
12/// columns, therefor columns should only be opened after passing this result
13/// through cf_options().
14pub(crate) fn db_options(config: &Config, env: &Env, row_cache: &Cache) -> Result<Options> {
15	const DEFAULT_STATS_LEVEL: StatsLevel = if cfg!(debug_assertions) {
16		StatsLevel::ExceptDetailedTimers
17	} else {
18		StatsLevel::DisableAll
19	};
20
21	let mut opts = Options::default();
22
23	// Logging
24	set_logging_defaults(&mut opts, config);
25	opts.add_event_listener(Events::new(config, env));
26
27	// Processing
28	opts.set_max_background_jobs(num_threads::<i32>(config)?);
29	opts.set_max_subcompactions(num_threads::<u32>(config)?);
30	opts.set_avoid_unnecessary_blocking_io(true);
31	opts.set_max_file_opening_threads(0);
32
33	// IO
34	opts.set_manual_wal_flush(true);
35	opts.set_atomic_flush(config.rocksdb.atomic_flush);
36	opts.set_enable_pipelined_write(!config.rocksdb.atomic_flush);
37	if config.rocksdb.direct_io {
38		opts.set_use_direct_reads(true);
39		opts.set_use_direct_io_for_flush_and_compaction(true);
40	}
41
42	if config.rocksdb.optimize_for_spinning_disks {
43		opts.set_skip_stats_update_on_db_open(true);
44		//opts.set_max_file_opening_threads(threads.try_into().unwrap());
45	} else {
46		opts.set_max_file_opening_threads(num_threads(config)?);
47		opts.set_compaction_readahead_size(1024 * 512);
48	}
49
50	// Blocks
51	opts.set_row_cache(row_cache);
52	opts.set_db_write_buffer_size(cache_size_f64(
53		config,
54		config.rocksdb.write_buffer_capacity_mb,
55		1_048_576,
56	));
57
58	// Files
59	opts.set_table_cache_num_shard_bits(7);
60	opts.set_wal_size_limit_mb(1024);
61	opts.set_max_total_wal_size(1024 * 1024 * 512);
62	opts.set_writable_file_max_buffer_size(1024 * 1024 * 2);
63	if !config.rocksdb.allow_fallocate {
64		opts.set_options_from_string("allow_fallocate=false")
65			.map_err(map_err)?;
66	}
67
68	// Misc
69	opts.set_disable_auto_compactions(!config.rocksdb.compaction);
70	opts.create_missing_column_families(true);
71	opts.create_if_missing(true);
72
73	opts.set_statistics_level(match config.rocksdb.stats_level {
74		| 0 => StatsLevel::DisableAll,
75		| 1 => DEFAULT_STATS_LEVEL,
76		| 2 => StatsLevel::ExceptHistogramOrTimers,
77		| 3 => StatsLevel::ExceptTimers,
78		| 4 => StatsLevel::ExceptDetailedTimers,
79		| 5 => StatsLevel::ExceptTimeForMutex,
80		| 6_u8..=u8::MAX => StatsLevel::All,
81	});
82
83	opts.set_report_bg_io_stats(match config.rocksdb.stats_level {
84		| 0..=1 => false,
85		| 2_u8..=u8::MAX => true,
86	});
87
88	// Default: https://github.com/facebook/rocksdb/wiki/WAL-Recovery-Modes#ktoleratecorruptedtailrecords
89	//
90	// Unclean shutdowns of a Matrix homeserver are likely to be fine when
91	// recovered in this manner as it's likely any lost information will be
92	// restored via federation.
93	opts.set_wal_recovery_mode(match config.rocksdb.recovery_mode {
94		| 0 => DBRecoveryMode::AbsoluteConsistency,
95		| 1 => DBRecoveryMode::TolerateCorruptedTailRecords,
96		| 2 => DBRecoveryMode::PointInTime,
97		| 3 => DBRecoveryMode::SkipAnyCorruptedRecord,
98		| 4_u8..=u8::MAX => unimplemented!(),
99	});
100
101	// <https://github.com/facebook/rocksdb/wiki/Track-WAL-in-MANIFEST>
102	// "We recommend to set track_and_verify_wals_in_manifest to true for
103	// production, it has been enabled in production for the entire database cluster
104	// serving the social graph for all Meta apps."
105	opts.set_track_and_verify_wals_in_manifest(true);
106
107	opts.set_paranoid_checks(config.rocksdb.paranoid_file_checks);
108
109	opts.set_env(env);
110
111	Ok(opts)
112}
113
114fn set_logging_defaults(opts: &mut Options, config: &Config) {
115	let log_level = match config.rocksdb.log_level.as_ref() {
116		| "debug" => LogLevel::Debug,
117		| "info" => LogLevel::Info,
118		| "warn" => LogLevel::Warn,
119		| "fatal" => LogLevel::Fatal,
120		| _ => LogLevel::Error,
121	};
122
123	opts.set_log_level(log_level);
124	opts.set_max_log_file_size(config.rocksdb.max_log_file_size);
125	opts.set_log_file_time_to_roll(config.rocksdb.log_time_to_roll);
126	opts.set_keep_log_file_num(config.rocksdb.max_log_files);
127	opts.set_stats_dump_period_sec(0);
128
129	if config.rocksdb.log_stderr {
130		opts.set_stderr_logger(log_level, "rocksdb");
131	} else {
132		opts.set_callback_logger(log_level, handle_log);
133	}
134}
135
136fn num_threads<T: TryFrom<usize>>(config: &Config) -> Result<T> {
137	const MIN_PARALLELISM: usize = 2;
138
139	let requested = if config.rocksdb.parallelism_threads != 0 {
140		config.rocksdb.parallelism_threads
141	} else {
142		utils::available_parallelism()
143	};
144
145	utils::math::try_into::<T, usize>(cmp::max(MIN_PARALLELISM, requested))
146}