rust_rocksdb/checkpoint.rs
1// Copyright 2018 Eugene P.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16//! Implementation of bindings to RocksDB Checkpoint[1] API
17//!
18//! [1]: https://github.com/facebook/rocksdb/wiki/Checkpoints
19
20use crate::db::{DBInner, ExportImportFilesMetaData};
21use crate::{
22 AsColumnFamilyRef, DBCommon, Error, ThreadMode, TransactionDB, ffi, ffi_util::to_cpath,
23};
24use std::{marker::PhantomData, path::Path};
25
26/// Default value for the `log_size_for_flush` parameter passed to
27/// `ffi::rocksdb_checkpoint_create`.
28///
29/// A value of `0` forces RocksDB to flush memtables as needed before creating
30/// the checkpoint. This helps ensure the checkpoint includes the most recent
31/// writes (which may still be in memtables at the time of checkpoint creation).
32///
33/// Forcing a flush can create new SST file(s), potentially very small L0 SSTs
34/// if little data has been written since the last flush.
35const DEFAULT_LOG_SIZE_FOR_FLUSH: u64 = 0_u64;
36
37/// Database's checkpoint object.
38/// Used to create checkpoints of the specified DB from time to time.
39///
40/// A `Checkpoint` must not outlive the `DB` it was created from:
41///
42/// ```compile_fail,E0597
43/// use rust_rocksdb::{checkpoint::Checkpoint, DB};
44///
45/// let _checkpoint = {
46/// let db = DB::open_default("foo").unwrap();
47/// Checkpoint::new(&db)
48/// };
49/// ```
50pub struct Checkpoint<'db> {
51 inner: *mut ffi::rocksdb_checkpoint_t,
52 _db: PhantomData<&'db ()>,
53}
54
55impl<'db> Checkpoint<'db> {
56 /// Creates new checkpoint object for specific DB.
57 ///
58 /// Does not actually produce checkpoints, call `.create_checkpoint()` method to produce
59 /// a DB checkpoint.
60 pub fn new<T: ThreadMode, I: DBInner>(db: &'db DBCommon<T, I>) -> Result<Self, Error> {
61 let checkpoint: *mut ffi::rocksdb_checkpoint_t;
62
63 unsafe {
64 checkpoint = ffi_try!(ffi::rocksdb_checkpoint_object_create(db.inner.inner()));
65 }
66
67 if checkpoint.is_null() {
68 return Err(Error::new("Could not create checkpoint object.".to_owned()));
69 }
70
71 Ok(Self {
72 inner: checkpoint,
73 _db: PhantomData,
74 })
75 }
76
77 /// Creates a new physical RocksDB checkpoint in the directory specified by `path`.
78 ///
79 /// A checkpoint is a consistent, read-only view of the database at a specific
80 /// point in time. Internally, RocksDB creates a new MANIFEST and metadata files
81 /// and hard-links the relevant SST files, making the checkpoint efficient to
82 /// create and safe to keep for long-lived reads.
83 ///
84 /// This method uses the default `log_size_for_flush` value (`0`), which instructs
85 /// RocksDB to flush memtables as needed before creating the checkpoint. Forcing
86 /// a flush ensures that the checkpoint includes the most recent writes that may
87 /// still reside in memtables at the time of checkpoint creation.
88 ///
89 /// Forcing a flush may create new SST file(s), including very small L0 SSTs if
90 /// little data has been written since the last flush. Applications that create
91 /// checkpoints frequently or during periods of low write volume may wish to
92 /// control this behavior by using an API that allows specifying
93 /// `log_size_for_flush`.
94 ///
95 /// Note:
96 /// - Checkpoints are always SST-based and never depend on WAL files or live
97 /// memtables when opened.
98 /// - If writes are performed with WAL disabled, forcing a flush is required to
99 /// ensure those writes appear in the checkpoint.
100 /// - When using RocksDB TransactionDB with two-phase commit (2PC), RocksDB will
101 /// always flush regardless of the `log_size_for_flush` setting.
102 pub fn create_checkpoint<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
103 let c_path = to_cpath(path)?;
104 unsafe {
105 ffi_try!(ffi::rocksdb_checkpoint_create(
106 self.inner,
107 c_path.as_ptr(),
108 DEFAULT_LOG_SIZE_FOR_FLUSH,
109 ));
110 }
111 Ok(())
112 }
113
114 /// Creates a new physical DB checkpoint in `path`, allowing the caller to
115 /// control `log_size_for_flush`.
116 ///
117 /// `log_size_for_flush` is forwarded to RocksDB's Checkpoint API:
118 /// - `0` forces a flush as needed before checkpoint creation, which helps the
119 /// checkpoint include the latest writes; this may create new SST file(s).
120 /// - A non-zero value:
121 /// - **Expected behavior** (once RocksDB bug is fixed): Only forces a flush
122 /// if the total WAL size exceeds the specified threshold. When a flush is
123 /// not forced and WAL writing is enabled, RocksDB includes WAL files in
124 /// the checkpoint that are replayed on open to reconstruct recent writes.
125 /// This avoids creating small SST files during periods of low write volume,
126 /// at the cost of additional checkpoint storage space for the copied WAL.
127 /// - **Current behavior** (RocksDB bug): Never flushes, regardless of WAL
128 /// size. The checkpoint will always include WAL files instead of flushing
129 /// to SST. See: <https://github.com/facebook/rocksdb/pull/14193>
130 ///
131 /// In practice, using a non-zero value means checkpoints may represent an
132 /// *older, fully materialized database state* rather than the instantaneous
133 /// state at the time the checkpoint is created.
134 ///
135 /// Note:
136 /// - If writes are performed with WAL disabled, using a non-zero
137 /// `log_size_for_flush` may cause those writes to be absent from
138 /// the checkpoint.
139 /// - When using RocksDB TransactionDB with two-phase commit (2PC),
140 /// RocksDB will always flush regardless of `log_size_for_flush`.
141 pub fn create_checkpoint_with_log_size<P: AsRef<Path>>(
142 &self,
143 path: P,
144 log_size_for_flush: u64,
145 ) -> Result<(), Error> {
146 let c_path = to_cpath(path)?;
147 unsafe {
148 ffi_try!(ffi::rocksdb_checkpoint_create(
149 self.inner,
150 c_path.as_ptr(),
151 log_size_for_flush,
152 ));
153 }
154 Ok(())
155 }
156
157 /// Export a specified Column Family
158 ///
159 /// Creates copies of the live SST files at the specified export path.
160 ///
161 /// - SST files will be created as hard links when the directory specified
162 /// is in the same partition as the db directory, copied otherwise.
163 /// - the path must not yet exist - a new directory will be created as part of the export.
164 /// - Always triggers a flush.
165 ///
166 /// # Examples
167 ///
168 /// ```rust
169 /// use rust_rocksdb::{DB, checkpoint::Checkpoint};
170 ///
171 /// fn export_column_family(db: &DB, column_family_name: &str, export_path: &str) {
172 /// let cp = Checkpoint::new(&db).unwrap();
173 /// let cf = db.cf_handle(column_family_name).unwrap();
174 ///
175 /// let export_metadata = cp.export_column_family(&cf, export_path).unwrap();
176 ///
177 /// assert!(export_metadata.get_files().len() > 0);
178 /// }
179 /// ```
180 ///
181 /// See also: [`DB::create_column_family_with_import`](crate::DB::create_column_family_with_import).
182 pub fn export_column_family<P: AsRef<Path>>(
183 &self,
184 column_family: &impl AsColumnFamilyRef,
185 path: P,
186 ) -> Result<ExportImportFilesMetaData, Error> {
187 let c_path = to_cpath(path)?;
188 let column_family_handle = column_family.inner();
189 let metadata = unsafe {
190 ffi_try!(ffi::rocksdb_checkpoint_export_column_family(
191 self.inner,
192 column_family_handle,
193 c_path.as_ptr(),
194 ))
195 };
196 Ok(ExportImportFilesMetaData { inner: metadata })
197 }
198}
199
200impl Drop for Checkpoint<'_> {
201 fn drop(&mut self) {
202 unsafe {
203 ffi::rocksdb_checkpoint_object_destroy(self.inner);
204 }
205 }
206}
207
208/// TransactionDB's checkpoint object.
209/// Used to create checkpoints of the specified TransactionDB from time to time.
210///
211/// A `TransactionDBCheckpoint` must not outlive the `TransactionDB` it was created from:
212///
213/// ```compile_fail,E0597
214/// use rust_rocksdb::{checkpoint::TransactionDBCheckpoint, SingleThreaded, TransactionDB};
215///
216/// let _checkpoint = {
217/// let db = TransactionDB::<SingleThreaded>::open_default("foo").unwrap();
218/// TransactionDBCheckpoint::new(&db)
219/// };
220/// ```
221///
222/// `TransactionDBCheckpoint` does not expose `create_checkpoint_with_log_size`
223/// because RocksDB TransactionDB checkpoints are expected to flush regardless
224/// of that setting:
225///
226/// ```compile_fail,E0599
227/// use rust_rocksdb::{checkpoint::TransactionDBCheckpoint, TransactionDB};
228///
229/// let db: TransactionDB = TransactionDB::open_default("foo").unwrap();
230/// let checkpoint = TransactionDBCheckpoint::new(&db).unwrap();
231/// checkpoint
232/// .create_checkpoint_with_log_size("foo-checkpoint", u64::MAX)
233/// .unwrap();
234/// ```
235pub struct TransactionDBCheckpoint<'db> {
236 inner: *mut ffi::rocksdb_checkpoint_t,
237 _db: PhantomData<&'db ()>,
238}
239
240impl<'db> TransactionDBCheckpoint<'db> {
241 /// Creates new checkpoint object for a specific TransactionDB.
242 ///
243 /// Does not actually produce checkpoints, call `.create_checkpoint()` to produce
244 /// a TransactionDB checkpoint.
245 pub fn new<T: ThreadMode>(db: &'db TransactionDB<T>) -> Result<Self, Error> {
246 let checkpoint: *mut ffi::rocksdb_checkpoint_t;
247
248 unsafe {
249 checkpoint = ffi_try!(ffi::rocksdb_transactiondb_checkpoint_object_create(
250 db.inner
251 ));
252 }
253
254 if checkpoint.is_null() {
255 return Err(Error::new("Could not create checkpoint object.".to_owned()));
256 }
257
258 Ok(Self {
259 inner: checkpoint,
260 _db: PhantomData,
261 })
262 }
263
264 /// Creates a new physical RocksDB checkpoint in the directory specified by `path`.
265 ///
266 /// This method uses the default `log_size_for_flush` value (`0`), which instructs
267 /// RocksDB to flush memtables as needed before creating the checkpoint.
268 pub fn create_checkpoint<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
269 let c_path = to_cpath(path)?;
270 unsafe {
271 ffi_try!(ffi::rocksdb_checkpoint_create(
272 self.inner,
273 c_path.as_ptr(),
274 DEFAULT_LOG_SIZE_FOR_FLUSH,
275 ));
276 }
277 Ok(())
278 }
279
280 /// Export a specified Column Family.
281 ///
282 /// Creates copies of the live SST files at the specified export path.
283 ///
284 /// - SST files will be created as hard links when the directory specified
285 /// is in the same partition as the db directory, copied otherwise.
286 /// - the path must not yet exist - a new directory will be created as part of the export.
287 /// - Always triggers a flush.
288 ///
289 /// See also: [`DB::create_column_family_with_import`](crate::DB::create_column_family_with_import).
290 pub fn export_column_family<P: AsRef<Path>>(
291 &self,
292 column_family: &impl AsColumnFamilyRef,
293 path: P,
294 ) -> Result<ExportImportFilesMetaData, Error> {
295 let c_path = to_cpath(path)?;
296 let column_family_handle = column_family.inner();
297 let metadata = unsafe {
298 ffi_try!(ffi::rocksdb_checkpoint_export_column_family(
299 self.inner,
300 column_family_handle,
301 c_path.as_ptr(),
302 ))
303 };
304 Ok(ExportImportFilesMetaData { inner: metadata })
305 }
306}
307
308impl Drop for TransactionDBCheckpoint<'_> {
309 fn drop(&mut self) {
310 unsafe {
311 ffi::rocksdb_checkpoint_object_destroy(self.inner);
312 }
313 }
314}