tuwunel_database/
engine.rs1mod backup;
11mod cf_opts;
12pub(crate) mod context;
13mod db_opts;
14pub(crate) mod descriptor;
15mod env;
16mod events;
17mod files;
18mod logger;
19mod memory_usage;
20mod open;
21mod repair;
22#[cfg(test)]
23mod tests;
24
25use std::{
26 collections::BTreeMap,
27 ffi::CStr,
28 path::Path,
29 sync::{
30 Arc, OnceLock, Weak,
31 atomic::{AtomicU32, Ordering},
32 },
33};
34
35use rocksdb::{
36 AsColumnFamilyRef, BoundColumnFamily, DBCommon, DBWithThreadMode, FlushOptions,
37 MultiThreaded, WaitForCompactOptions, WriteOptions, checkpoint::Checkpoint,
38};
39use tuwunel_core::{Err, Result, debug, err, implement, info, warn};
40
41use crate::{
42 Context, Map,
43 pool::Pool,
44 util::{map_err, result},
45};
46
47pub(crate) type CfIndex = BTreeMap<u32, Weak<Map>>;
48
49pub struct Engine {
53 pub(crate) db: Db,
55
56 pub(crate) pool: Arc<Pool>,
59
60 pub(crate) ctx: Arc<Context>,
63
64 pub(super) read_only: bool,
66
67 pub(super) secondary: bool,
69
70 pub(crate) checksums: bool,
72
73 pub(crate) write_options: WriteOptions,
75
76 cf_index: OnceLock<CfIndex>,
79
80 corks: AtomicU32,
82}
83
84pub(crate) type Db = DBWithThreadMode<MultiThreaded>;
86
87impl Engine {
88 #[tracing::instrument(
93 level = "info",
94 skip_all,
95 fields(
96 sequence = ?self.current_sequence(),
97 ),
98 )]
99 pub fn wait_compactions_blocking(&self) -> Result {
100 let mut opts = WaitForCompactOptions::default();
101 opts.set_abort_on_pause(true);
102 opts.set_flush(false);
103 opts.set_timeout(0);
104
105 self.db.wait_for_compact(&opts).map_err(map_err)
106 }
107
108 #[tracing::instrument(
121 level = "info",
122 skip_all,
123 fields(
124 sequence = ?self.current_sequence(),
125 ),
126 )]
127 pub fn sort(&self) -> Result {
128 assert!(!self.is_read_only(), "memtables cannot be flushed on a read-only database");
129
130 let cfs: Vec<_> = self
131 .db
132 .cf_names()
133 .iter()
134 .filter_map(|name| self.db.cf_handle(name))
135 .collect();
136
137 let cfs: Vec<&_> = cfs.iter().collect();
138 let opts = FlushOptions::default();
139
140 result(DBCommon::flush_cfs_opt(&self.db, &cfs, &opts))
141 }
142
143 #[tracing::instrument(level = "info", skip(self))]
148 pub fn checkpoint(&self, path: &Path, log_size: u64) -> Result {
149 let checkpoint = Checkpoint::new(&self.db).map_err(map_err)?;
150
151 checkpoint
152 .create_checkpoint_with_log_size(path, log_size)
153 .map_err(map_err)
154 }
155
156 #[tracing::instrument(
161 level = "debug",
162 skip_all,
163 fields(
164 sequence = ?self.current_sequence(),
165 ),
166 )]
167 pub fn update(&self) -> Result {
168 self.db
169 .try_catch_up_with_primary()
170 .map_err(map_err)
171 }
172
173 #[tracing::instrument(level = "info", skip_all)]
178 pub fn sync(&self) -> Result { result(DBCommon::flush_wal(&self.db, true)) }
179
180 #[tracing::instrument(level = "debug", skip_all)]
185 pub fn flush(&self) -> Result { result(DBCommon::flush_wal(&self.db, false)) }
186
187 #[inline]
189 pub(crate) fn cork(&self) { self.corks.fetch_add(1, Ordering::Relaxed); }
190
191 #[inline]
193 pub(crate) fn uncork(&self) { self.corks.fetch_sub(1, Ordering::Relaxed); }
194
195 #[inline]
204 pub fn corked(&self) -> bool { self.corks.load(Ordering::Relaxed) > 0 }
205
206 pub(crate) fn property_integer(
210 &self,
211 cf: &impl AsColumnFamilyRef,
212 name: &CStr,
213 ) -> Result<u64> {
214 result(self.db.property_int_value_cf(cf, name))
215 .and_then(|val| val.map_or_else(|| Err!("Property {name:?} not found."), Ok))
216 }
217
218 pub(crate) fn property(&self, cf: &impl AsColumnFamilyRef, name: &str) -> Result<String> {
220 result(self.db.property_value_cf(cf, name))
221 .and_then(|val| val.map_or_else(|| Err!("Property {name:?} not found."), Ok))
222 }
223
224 pub(crate) fn cf(&self, name: &str) -> Arc<BoundColumnFamily<'_>> {
233 self.db
234 .cf_handle(name)
235 .expect("column must be described prior to database open")
236 }
237
238 #[inline]
243 #[must_use]
244 pub fn has_cf(&self, name: &str) -> bool { self.db.cf_handle(name).is_some() }
245
246 #[inline]
251 pub fn column_names(&self) -> impl Iterator<Item = String> + Send + use<> {
252 self.db.cf_names().into_iter()
253 }
254
255 pub fn column_size(&self, name: &str) -> Result<u64> {
259 let cf = self
260 .db
261 .cf_handle(name)
262 .ok_or_else(|| err!(Database("column family {name:?} is not open")))?;
263
264 self.property_integer(&cf, c"rocksdb.total-sst-files-size")
265 }
266
267 #[inline]
273 #[must_use]
274 #[tracing::instrument(
275 name = "sequence",
276 level = "debug",
277 skip_all,
278 fields(sequence)
279 )]
280 pub fn current_sequence(&self) -> u64 {
281 let sequence = self.db.latest_sequence_number();
282
283 #[cfg(debug_assertions)]
284 tracing::Span::current().record("sequence", sequence);
285
286 sequence
287 }
288
289 #[inline]
294 #[must_use]
295 pub fn is_read_only(&self) -> bool { self.secondary || self.read_only }
296
297 #[inline]
302 #[must_use]
303 pub fn is_secondary(&self) -> bool { self.secondary }
304}
305
306#[implement(Engine)]
307pub(crate) fn set_cf_index(&self, index: CfIndex) {
308 self.cf_index
309 .set(index)
310 .expect("cf_index initialized twice");
311}
312
313#[implement(Engine)]
314#[inline]
315pub(crate) fn map_by_cf_id(&self, cf_id: u32) -> Option<Arc<Map>> {
316 self.cf_index
317 .get()
318 .expect("cf_index initialized before writes")
319 .get(&cf_id)
320 .and_then(Weak::upgrade)
321}
322
323impl Drop for Engine {
324 #[cold]
325 fn drop(&mut self) {
326 const BLOCKING: bool = true;
327
328 debug!("Waiting for background tasks to finish...");
329 self.db.cancel_all_background_work(BLOCKING);
330
331 info!(
332 sequence = %self.current_sequence(),
333 "Closing database..."
334 );
335 }
336}