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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! LSN-continuity-after-crash tests.
//!
//! These tests verify that when the engine is dropped without `close()`
//! (simulating a crash), the next `Engine::open()` resumes the LSN
//! counter above the maximum LSN found in any persisted data source
//! (WAL replay, frozen memtables, SSTables, manifest).
//!
//! Without correct LSN resumption, post-crash writes could reuse LSNs
//! already assigned to pre-crash data, breaking the "highest-LSN wins"
//! merge invariant and causing data corruption.
//!
//! ## See also
//! - [`tests_lsn_continuity`] — LSN continuity across clean reopens
//! - [`tests_crash_recovery`] — single crash durability
//! - [`tests_multi_crash`] — multiple consecutive crash cycles
#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
use crate::engine::Engine;
use crate::engine::tests::helpers::*;
use tempfile::TempDir;
// ================================================================
// 1. Overwrite after crash shadows old value
// ================================================================
/// # Scenario
/// After a crash (drop without close), a post-recovery overwrite
/// must shadow the pre-crash value because it receives a higher LSN.
///
/// # Actions
/// 1. Put `"k"` = `"old"`, drop (crash).
/// 2. Reopen, put `"k"` = `"new"`, verify.
///
/// # Expected behavior
/// `get("k")` returns `"new"`.
#[test]
fn memtable__overwrite_after_crash_shadows_old() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
{
let engine = Engine::open(path, default_config()).unwrap();
engine.put(b"k".to_vec(), b"old".to_vec()).unwrap();
// Drop — crash.
}
let engine = Engine::open(path, default_config()).unwrap();
engine.put(b"k".to_vec(), b"new".to_vec()).unwrap();
assert_eq!(
engine.get(b"k".to_vec()).unwrap(),
Some(b"new".to_vec()),
"post-crash overwrite must shadow old value"
);
}
// ================================================================
// 2. Delete after crash hides old put
// ================================================================
/// # Scenario
/// A tombstone written after crash recovery hides a pre-crash put.
///
/// # Actions
/// 1. Put `"k"` = `"v"`, drop (crash).
/// 2. Reopen, delete `"k"`, verify.
///
/// # Expected behavior
/// `get("k")` returns `None`.
#[test]
fn memtable__delete_after_crash_hides_old_put() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
{
let engine = Engine::open(path, default_config()).unwrap();
engine.put(b"k".to_vec(), b"v".to_vec()).unwrap();
}
let engine = Engine::open(path, default_config()).unwrap();
engine.delete(b"k".to_vec()).unwrap();
assert_eq!(
engine.get(b"k".to_vec()).unwrap(),
None,
"post-crash delete must hide old put"
);
}
// ================================================================
// 3. Range-delete after crash hides old puts
// ================================================================
/// # Scenario
/// A range-delete written after crash recovery hides multiple
/// pre-crash puts.
///
/// # Actions
/// 1. Write keys 0..10, drop (crash).
/// 2. Reopen, range-delete [key_03, key_07), verify.
///
/// # Expected behavior
/// Keys 3-6 return `None`. Others return original values.
#[test]
fn memtable__range_delete_after_crash_hides_old_puts() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
{
let engine = Engine::open(path, default_config()).unwrap();
for i in 0..10u8 {
engine
.put(
format!("key_{i:02}").into_bytes(),
format!("val_{i:02}").into_bytes(),
)
.unwrap();
}
}
let engine = Engine::open(path, default_config()).unwrap();
engine
.delete_range(b"key_03".to_vec(), b"key_07".to_vec())
.unwrap();
for i in 0..10u8 {
let key = format!("key_{i:02}").into_bytes();
let val = engine.get(key).unwrap();
if (3..7).contains(&i) {
assert_eq!(val, None, "key_{i:02} should be range-deleted after crash");
} else {
assert_eq!(
val,
Some(format!("val_{i:02}").into_bytes()),
"key_{i:02} should survive"
);
}
}
}
// ================================================================
// 4. LSN continuity across multiple crash cycles
// ================================================================
/// # Scenario
/// LSN counter correctly resumes across three crash (drop) cycles,
/// ensuring the latest overwrite always wins.
///
/// # Actions
/// 1. Cycle 1: put `"k"` = `"v1"`, drop.
/// 2. Cycle 2: reopen, put `"k"` = `"v2"`, drop.
/// 3. Cycle 3: reopen, put `"k"` = `"v3"`, drop.
/// 4. Final: reopen, get `"k"`.
///
/// # Expected behavior
/// Returns `"v3"` — each crash cycle gets a higher base LSN.
#[test]
fn memtable__lsn_continuity_across_crash_cycles() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
for i in 1..=3 {
let engine = Engine::open(path, default_config()).unwrap();
engine
.put(b"k".to_vec(), format!("v{i}").into_bytes())
.unwrap();
// Drop — crash.
}
let engine = Engine::open(path, default_config()).unwrap();
assert_eq!(
engine.get(b"k".to_vec()).unwrap(),
Some(b"v3".to_vec()),
"Most recent crash-cycle write must win"
);
}
// ================================================================
// 5. LSN continuity after crash with SSTables
// ================================================================
/// # Scenario
/// After crash with data in SSTables (flushed) + unflushed memtable,
/// a post-recovery overwrite must shadow SSTable values.
///
/// # Actions
/// 1. Write 30 keys with small buffer (creates SSTables).
/// 2. Flush all frozen, write key_0010 = "pre-crash", drop (crash).
/// 3. Reopen, put key_0010 = "post-crash", scan.
///
/// # Expected behavior
/// Scan shows key_0010 = "post-crash" (post-crash LSN > SSTable LSN).
#[test]
fn memtable_sstable__overwrite_after_crash_shadows_sstable() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
{
let engine = Engine::open(path, small_buffer_config()).unwrap();
for i in 0..30u32 {
engine
.put(
format!("key_{i:04}").into_bytes(),
format!("old_{i:04}").into_bytes(),
)
.unwrap();
}
engine.flush_all_frozen().unwrap();
// Write a final value that stays in memtable.
engine
.put(b"key_0010".to_vec(), b"pre-crash".to_vec())
.unwrap();
// Drop — crash.
}
let engine = Engine::open(path, small_buffer_config()).unwrap();
engine
.put(b"key_0010".to_vec(), b"post-crash".to_vec())
.unwrap();
let results = collect_scan(&engine, b"key_", b"key_\xff");
let entry = results.iter().find(|(k, _)| k == b"key_0010").unwrap();
assert_eq!(
entry.1,
b"post-crash".to_vec(),
"post-crash overwrite must shadow SSTable & WAL values"
);
}
// ================================================================
// 6. LSN monotonicity: post-crash writes get strictly higher LSNs
// ================================================================
/// # Scenario
/// Verify that the LSN assigned to the first post-crash value is
/// strictly higher than the max LSN of any pre-crash record,
/// indirectly validated by overwrite semantics on multiple keys.
///
/// # Actions
/// 1. Write keys a, b, c with values v1. Drop (crash).
/// 2. Reopen. Overwrite only b="v2". Read all three.
///
/// # Expected behavior
/// a="v1", b="v2", c="v1". The overwrite of b works only if the
/// post-crash LSN is strictly higher than the recovered LSN of b.
#[test]
fn memtable__post_crash_lsn_strictly_higher() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
{
let engine = Engine::open(path, default_config()).unwrap();
engine.put(b"a".to_vec(), b"v1".to_vec()).unwrap();
engine.put(b"b".to_vec(), b"v1".to_vec()).unwrap();
engine.put(b"c".to_vec(), b"v1".to_vec()).unwrap();
}
let engine = Engine::open(path, default_config()).unwrap();
engine.put(b"b".to_vec(), b"v2".to_vec()).unwrap();
assert_eq!(
engine.get(b"a".to_vec()).unwrap(),
Some(b"v1".to_vec()),
"a untouched"
);
assert_eq!(
engine.get(b"b".to_vec()).unwrap(),
Some(b"v2".to_vec()),
"b must be overwritten — post-crash LSN > recovered LSN"
);
assert_eq!(
engine.get(b"c".to_vec()).unwrap(),
Some(b"v1".to_vec()),
"c untouched"
);
}
// ================================================================
// 7. Multiple crash cycles with flush in between
// ================================================================
/// # Scenario
/// Three crash cycles where data moves to SSTables between crashes.
/// Each cycle's overwrite to the same key must win.
///
/// # Actions
/// 1. Write key="k", value="sstable-1", flush to SSTable. Drop.
/// 2. Reopen. Overwrite key="k", value="sstable-2", flush. Drop.
/// 3. Reopen. Overwrite key="k", value="memtable-3" (no flush). Drop.
/// 4. Final: reopen, get "k".
///
/// # Expected behavior
/// Returns "memtable-3" — the latest overwrite wins across all layers.
#[test]
fn memtable_sstable__lsn_across_crash_cycles_with_flush() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
// Cycle 1: write + flush to SSTable + crash.
{
let engine = Engine::open(path, small_buffer_config()).unwrap();
engine.put(b"k".to_vec(), b"sstable-1".to_vec()).unwrap();
// Write enough to trigger a frozen memtable with small buffer.
for i in 0..20u32 {
engine
.put(format!("pad_{i:04}").into_bytes(), b"x".repeat(64))
.unwrap();
}
engine.flush_all_frozen().unwrap();
}
// Cycle 2: overwrite + flush + crash.
{
let engine = Engine::open(path, small_buffer_config()).unwrap();
engine.put(b"k".to_vec(), b"sstable-2".to_vec()).unwrap();
for i in 20..40u32 {
engine
.put(format!("pad_{i:04}").into_bytes(), b"x".repeat(64))
.unwrap();
}
engine.flush_all_frozen().unwrap();
}
// Cycle 3: overwrite in memtable only + crash.
{
let engine = Engine::open(path, small_buffer_config()).unwrap();
engine.put(b"k".to_vec(), b"memtable-3".to_vec()).unwrap();
}
// Final verification.
let engine = Engine::open(path, small_buffer_config()).unwrap();
assert_eq!(
engine.get(b"k".to_vec()).unwrap(),
Some(b"memtable-3".to_vec()),
"Latest crash-cycle overwrite (memtable) must win over SSTable data"
);
}
}