1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026-present, Dmitry Prudnikov
//! Closing a tree, and learning when it no longer touches its directory.
//!
//! Everything of a tree that owns a file of its directory or works in it holds
//! the tree's [`Life`]: the tree handles, the table file pool (joined refills
//! included), the background deleter until it drained its queue, and, through
//! the deletion pause they all share, every table, blob file and pin. A tree
//! is closed once the last of them drops; [`TreeClose`] reports that moment.
use crate::{AnyTree, BlobTree, Tree, path::PathBuf, tree::inner::TreeInner};
use alloc::{boxed::Box, sync::Arc, sync::Weak, vec::Vec};
use core::sync::atomic::{AtomicBool, Ordering};
// no-std: spin::Mutex (parking_lot is std-only); waiting needs std.
#[cfg(feature = "std")]
use parking_lot::Mutex;
#[cfg(not(feature = "std"))]
use spin::Mutex;
/// A callback waiting for a tree to close.
type OnClosed = Box<dyn FnOnce() + Send>;
/// What a tree's closing signal shares with its [`TreeClose`] handles.
struct Signal {
closed: AtomicBool,
/// The callbacks to run once closed; emptied by the closing.
waiting: Mutex<Vec<OnClosed>>,
/// Woken once closed, for the callers that block.
#[cfg(feature = "std")]
settled: parking_lot::Condvar,
}
impl Signal {
fn fire(&self) {
let callbacks = {
let mut waiting = self.waiting.lock();
self.closed.store(true, Ordering::Release);
#[cfg(feature = "std")]
self.settled.notify_all();
core::mem::take(&mut *waiting)
};
// Outside the lock: a callback may register another, or wait.
for callback in callbacks {
run(callback);
}
}
}
/// Runs a callback of a closing tree. It runs inside a drop of the tree's own
/// parts, possibly while a panic unwinds, or at registration on the caller's
/// thread; either way its panic is contained, so it aborts no process and
/// keeps no other callback from running.
fn run(callback: impl FnOnce()) {
#[cfg(feature = "std")]
if std::panic::catch_unwind(core::panic::AssertUnwindSafe(callback)).is_err() {
log::error!("a TreeClose callback panicked; the others still run");
}
#[cfg(not(feature = "std"))]
callback();
}
/// A tree's liveness: held, as an `Arc`, by everything of the tree that owns
/// a file of its directory. Its last drop closes the tree.
pub struct Life {
signal: Arc<Signal>,
}
impl Life {
/// The liveness of a tree being opened.
pub fn new() -> Arc<Self> {
Arc::new(Self {
signal: Arc::new(Signal {
closed: AtomicBool::new(false),
waiting: Mutex::new(Vec::new()),
#[cfg(feature = "std")]
settled: parking_lot::Condvar::new(),
}),
})
}
/// A handle on the signal, holding no liveness itself.
pub fn signal(&self) -> TreeClose {
TreeClose {
signal: Arc::clone(&self.signal),
}
}
}
impl Drop for Life {
fn drop(&mut self) {
self.signal.fire();
}
}
/// The signal of a tree being closed, returned by
/// [`Tree::close`](crate::Tree::close) and its counterparts.
///
/// A tree is closed once nothing of it owns a file of its directory or works
/// in it any more: no tree handle, iterator, snapshot or pin holds it, its
/// table file pool made its last file and the background deleter removed
/// the last file it was handed. From then on the tree writes nothing into its
/// directory, and removing the directory is safe.
///
/// The handle keeps nothing open itself, nor do the callbacks registered on
/// it, so it can be held for as long as wanted.
#[derive(Clone)]
#[must_use = "the signal is the only way to learn when the tree is closed"]
pub struct TreeClose {
signal: Arc<Signal>,
}
impl TreeClose {
/// Runs `f` once the tree is closed, on the thread that finished closing
/// it, or at once on this thread if it is closed already. Nothing waits or
/// polls meanwhile: an async caller completes its own oneshot from it.
///
/// `f` runs inside the drop that closed the tree, so it should be short.
/// With `std` a panic in `f` is caught and logged, and the other
/// callbacks still run.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// use std::sync::mpsc;
///
/// let tree = lsm_tree::Config::new(
/// folder.path(),
/// lsm_tree::SequenceNumberCounter::default(),
/// lsm_tree::SequenceNumberCounter::default(),
/// )
/// .open()?;
///
/// let (closed, on_closed) = mpsc::channel();
/// // The receiver may be gone by then: a failed send is nothing to report.
/// tree.close().on_closed(move || {
/// let _ = closed.send(());
/// });
/// on_closed.recv()?;
/// std::fs::remove_dir_all(folder.path())?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn on_closed(&self, f: impl FnOnce() + Send + 'static) {
let mut waiting = self.signal.waiting.lock();
if self.signal.closed.load(Ordering::Acquire) {
drop(waiting);
run(f);
} else {
waiting.push(Box::new(f));
}
}
/// Whether the tree is closed.
#[must_use]
pub fn is_closed(&self) -> bool {
self.signal.closed.load(Ordering::Acquire)
}
/// Blocks until the tree is closed.
///
/// Blocks for good if the caller itself still holds a tree handle, an
/// iterator, a snapshot or a pin of the tree: drop them first, or use
/// [`on_closed`](Self::on_closed).
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// use lsm_tree::AbstractTree;
///
/// let tree = lsm_tree::Config::new(
/// folder.path(),
/// lsm_tree::SequenceNumberCounter::default(),
/// lsm_tree::SequenceNumberCounter::default(),
/// )
/// .open()?;
/// tree.insert("a", "1", 0);
/// tree.flush_active_memtable(0)?;
///
/// tree.close().wait();
/// std::fs::remove_dir_all(folder.path())?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[cfg(feature = "std")]
pub fn wait(&self) {
let mut waiting = self.signal.waiting.lock();
while !self.signal.closed.load(Ordering::Acquire) {
self.signal.settled.wait(&mut waiting);
}
}
/// Blocks until the tree is closed or `timeout` passes; whether it is
/// closed.
///
/// Returns `false` at the timeout while anything of the tree is held, the
/// caller's own handles, iterators, snapshots and pins included.
#[cfg(feature = "std")]
#[must_use]
pub fn wait_timeout(&self, timeout: core::time::Duration) -> bool {
let mut waiting = self.signal.waiting.lock();
let _ = self.signal.settled.wait_while_for(
&mut waiting,
|_| !self.signal.closed.load(Ordering::Acquire),
timeout,
);
self.signal.closed.load(Ordering::Acquire)
}
}
impl core::fmt::Debug for TreeClose {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("TreeClose")
.field("closed", &self.is_closed())
.finish()
}
}
/// A handle on a tree that does not keep it open, made by
/// [`Tree::downgrade`](crate::Tree::downgrade) and its counterparts.
///
/// For a holder that only acts on a live tree, such as a compaction monitor
/// or a queued flush request: it lets a dropped or closing tree close without
/// waiting for the holder.
#[derive(Clone)]
pub struct WeakTree {
inner: Weak<TreeInner>,
/// The blob folder of a key-value-separated tree.
blobs_folder: Option<Arc<PathBuf>>,
}
impl WeakTree {
pub(crate) fn of(tree: &Tree, blobs_folder: Option<Arc<PathBuf>>) -> Self {
Self {
inner: Arc::downgrade(&tree.0),
blobs_folder,
}
}
/// A strong handle on the tree, or `None` once every strong handle is
/// gone or the tree is being closed.
///
/// A tree being closed is not raised again, even while strong handles
/// taken earlier still finish their work, so a holder woken in between
/// starts no new work the closing would then wait for.
#[must_use]
pub fn upgrade(&self) -> Option<AnyTree> {
let inner = self.inner.upgrade()?;
#[cfg(test)]
crate::tree::inner::TestHooks::fire(&inner.test_hooks.upgrade);
// The one point `close` and `upgrade` meet: read after the strong
// handle is taken, so a flag read clear means `close` comes after,
// and the closing waits for this handle like any other.
if inner.closing.load(Ordering::SeqCst) {
return None;
}
let index = Tree(inner);
Some(match &self.blobs_folder {
None => AnyTree::Standard(index),
Some(blobs_folder) => AnyTree::Blob(BlobTree {
index,
blobs_folder: Arc::clone(blobs_folder),
}),
})
}
}
impl core::fmt::Debug for WeakTree {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("WeakTree")
.field("blob", &self.blobs_folder.is_some())
.finish_non_exhaustive()
}
}
/// Gives up `tree`'s handle and returns its closing signal: from here no weak
/// handle raises the tree again.
pub fn close(tree: Tree) -> TreeClose {
let signal = tree.life.signal();
tree.closing.store(true, Ordering::SeqCst);
drop(tree);
signal
}
#[cfg(test)]
#[expect(clippy::unwrap_used, clippy::expect_used, reason = "test code")]
mod tests;