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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
// Copyright © SurrealDB Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! This module stores the database persistence logic.
#![cfg(not(target_arch = "wasm32"))]
use crate::compression::CompressedReader;
use crate::compression::CompressedWriter;
use crate::compression::CompressionMode;
use crate::err::PersistenceError;
use crate::inner::Inner;
use crate::version::Version;
use crate::versions::Versions;
use bincode::config;
use bytes::Bytes;
use crossbeam_deque::{Injector, Steal};
use parking_lot::RwLock;
use std::collections::BTreeMap;
use std::fs::{self, File, OpenOptions};
use std::io::Write;
use std::io::{BufReader, BufWriter, Seek, SeekFrom};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
use web_time::{Duration, Instant};
/// Represents a pending asynchronous append operation
#[derive(Debug, Clone)]
pub(crate) struct AsyncAppendOperation {
pub version: u64,
pub writeset: BTreeMap<Bytes, Option<Bytes>>,
}
/// Configuration for AOL (Append-Only Log) behavior
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum AolMode {
/// Never use AOL
#[default]
Never,
/// Write immediatelyto AOL on every commit
SynchronousOnCommit,
/// Write asynchronously to AOL on every commit
AsynchronousAfterCommit,
}
/// Configuration for snapshot behavior
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum SnapshotMode {
/// Never use snapshots
#[default]
Never,
/// Periodically snapshot at the given interval
Interval(Duration),
}
/// Configuration for fsync behavior
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum FsyncMode {
/// Never call fsync (fastest, least durable)
#[default]
Never,
/// Call fsync after every append operation (slowest, most durable)
EveryAppend,
/// Call fsync at most once per interval
Interval(Duration),
}
/// Configuration options for persistence
#[derive(Debug, Clone)]
pub struct PersistenceOptions {
/// Base path for persistence files
pub base_path: PathBuf,
/// AOL (append-only log) behavior mode
pub aol_mode: AolMode,
/// Snapshot behavior mode
pub snapshot_mode: SnapshotMode,
/// Configuration for fsync behavior
pub fsync_mode: FsyncMode,
/// Path to the append-only log file (relative to base path or absolute)
pub aol_path: Option<PathBuf>,
/// Path to the snapshot file (relative to base path or absolute)
pub snapshot_path: Option<PathBuf>,
/// Compression mode for snapshots
pub compression_mode: CompressionMode,
}
impl Default for PersistenceOptions {
fn default() -> Self {
Self {
base_path: PathBuf::from("./data"),
aol_mode: AolMode::default(),
snapshot_mode: SnapshotMode::default(),
fsync_mode: FsyncMode::default(),
aol_path: None,
snapshot_path: None,
compression_mode: CompressionMode::default(),
}
}
}
impl PersistenceOptions {
/// Create new persistence options with the given base path
pub fn new<P: Into<PathBuf>>(base_path: P) -> Self {
Self {
base_path: base_path.into(),
..Self::default()
}
}
/// Set the base path for persistence files
pub fn with_base_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.base_path = path.into();
self
}
/// Set the AOL (append-only log) behavior mode
pub fn with_aol_mode(mut self, mode: AolMode) -> Self {
self.aol_mode = mode;
self
}
/// Set the snapshot behavior mode
pub fn with_snapshot_mode(mut self, mode: SnapshotMode) -> Self {
self.snapshot_mode = mode;
self
}
/// Set the fsync mode
pub fn with_fsync_mode(mut self, mode: FsyncMode) -> Self {
self.fsync_mode = mode;
self
}
/// Set a custom AOL file path
pub fn with_aol_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.aol_path = Some(path.into());
self
}
/// Set a custom snapshot file path
pub fn with_snapshot_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.snapshot_path = Some(path.into());
self
}
/// Set the compression mode for snapshots
pub fn with_compression(mut self, mode: CompressionMode) -> Self {
self.compression_mode = mode;
self
}
}
/// A persistence layer for storing and loading database state
///
/// This struct handles the persistence of database state through:
/// - Append-only log (AOL) for recording changes
/// - Periodic snapshots for efficient recovery
/// - Background worker for automatic snapshot creation
#[derive(Clone)]
pub struct Persistence {
/// Reference to the inner database state
pub(crate) inner: Arc<Inner>,
/// File handle for the append-only log (None if AOL is disabled)
pub(crate) aol: Option<Arc<Mutex<File>>>,
/// Path to the append-only log file (None if AOL is disabled)
pub(crate) aol_path: PathBuf,
/// Path to the snapshot file
pub(crate) snapshot_path: PathBuf,
/// AOL (append-only log) behavior mode
pub(crate) aol_mode: AolMode,
/// Snapshot behavior mode
pub(crate) snapshot_mode: SnapshotMode,
/// Fsync configuration mode
pub(crate) fsync_mode: FsyncMode,
/// Compression mode for snapshots
pub(crate) compression_mode: CompressionMode,
/// Specifies whether background worker threads are enabled
pub(crate) background_threads_enabled: Arc<AtomicBool>,
/// Handle to the background fsync worker thread (for interval mode)
pub(crate) fsync_handle: Arc<RwLock<Option<JoinHandle<()>>>>,
/// Handle to the background snapshot worker thread
pub(crate) snapshot_handle: Arc<RwLock<Option<JoinHandle<()>>>>,
/// Handle to the background async append worker thread
pub(crate) appender_handle: Arc<RwLock<Option<JoinHandle<()>>>>,
/// Last fsync timestamp for interval mode
pub(crate) last_fsync: Arc<Mutex<Instant>>,
/// Counter for AOL appends since last fsync
pub(crate) pending_syncs: Arc<AtomicU64>,
/// Queue for asynchronous append operations
pub(crate) async_append_injector: Arc<Injector<AsyncAppendOperation>>,
}
impl Persistence {
/// Creates a new persistence layer with custom options
///
/// # Arguments
/// * `options` - Configuration options for persistence
/// * `inner` - Reference to the database state
///
/// # Returns
/// * `Result<Self, PersistenceError>` - The created persistence layer or an
/// error
pub(crate) fn new_with_options(
options: PersistenceOptions,
inner: Arc<Inner>,
) -> Result<Self, PersistenceError> {
// Get the base path from options
let base_path = &options.base_path;
// Ensure the directory exists
fs::create_dir_all(base_path)?;
// Determine the specified AOL file path
let aol_path = if let Some(path) = options.aol_path {
if path.is_absolute() {
path
} else {
base_path.join(path)
}
} else {
base_path.join("aol.bin")
};
// Determine the specified snapshot file path
let snapshot_path = if let Some(path) = options.snapshot_path {
if path.is_absolute() {
path
} else {
base_path.join(path)
}
} else {
base_path.join("snapshot.bin")
};
// Initialize AOL components if enabled
let aol = if !matches!(options.aol_mode, AolMode::Never) {
// Ensure parent directories exist for AOL path
if let Some(parent) = aol_path.parent() {
fs::create_dir_all(parent)?;
}
// Open the AOL file with append mode
let file = OpenOptions::new().create(true).append(true).read(true).open(&aol_path)?;
Some(Arc::new(Mutex::new(file)))
} else {
None
};
// Ensure parent directories exist for snapshot path
if let Some(parent) = snapshot_path.parent() {
fs::create_dir_all(parent)?;
}
// Create the persistence instance
let this = Self {
inner,
aol,
aol_path,
snapshot_path,
aol_mode: options.aol_mode,
snapshot_mode: options.snapshot_mode,
fsync_mode: options.fsync_mode,
compression_mode: options.compression_mode,
background_threads_enabled: Arc::new(AtomicBool::new(true)),
fsync_handle: Arc::new(RwLock::new(None)),
snapshot_handle: Arc::new(RwLock::new(None)),
appender_handle: Arc::new(RwLock::new(None)),
last_fsync: Arc::new(Mutex::new(Instant::now())),
pending_syncs: Arc::new(AtomicU64::new(0)),
async_append_injector: Arc::new(Injector::new()),
};
// Load existing data from disk
this.load()?;
// Start the background snapshot worker if snapshots are enabled
this.spawn_snapshot_worker();
// Start the fsync worker if needed (only when AOL is enabled)
this.spawn_fsync_worker();
// Start the async append worker if asynchronous mode is enabled
this.spawn_appender_worker();
// Return the persistence layer
Ok(this)
}
/// Creates a new snapshot of the current database state
///
/// This function:
/// 1. Captures the current AOL file position as a cutoff point
/// 2. Creates a new snapshot file atomically using a temporary file
/// 3. Streams data to reduce memory usage
/// 4. Truncates AOL only up to the cutoff position, preserving newer
/// entries
///
/// # Returns
/// * `Result<(), PersistenceError>` - Success or an error
pub fn snapshot(&self) -> Result<(), PersistenceError> {
// Create temporary file for atomic swap
let temp_path = self.snapshot_path.with_extension("tmp");
// Execute snapshot operation in closure for clean error handling
let result = (|| -> Result<(), PersistenceError> {
// Create temporary file
let file = File::create(&temp_path)?;
// Create compressed writer (handles buffering internally)
let mut writer = CompressedWriter::new(file, self.compression_mode)?;
// Get the current position in the AOL file (if AOL is enabled)
let aol_cutoff_position = if let Some(ref aol) = self.aol {
aol.lock()?.metadata()?.len()
} else {
0
};
// Stream write each key-value pair to reduce memory usage
let mut iter = self.inner.datastore.raw_iter();
iter.seek_to_first();
while let Some((key, versions)) = iter.next() {
// Get all versions for this key
let versions = versions.all_versions();
// Ensure that there are version entries
if !versions.is_empty() {
// Serialize and write this single entry
bincode::serde::encode_into_std_write(
&(key.clone(), versions),
&mut writer,
config::standard(),
)?;
}
}
// Flush the compressed writer
writer.flush()?;
// Finish compression (finalizes LZ4 stream)
writer.finish()?;
// Atomically rename temporary file to actual snapshot
fs::rename(&temp_path, &self.snapshot_path)?;
// Sync the renamed file to disk for durability
{
let final_file = File::open(&self.snapshot_path)?;
final_file.sync_all()?;
}
// Truncate AOL only up to the cutoff position
Self::truncate(&self.aol, aol_cutoff_position, &self.pending_syncs)?;
// All ok
Ok(())
})();
// Clean up temporary file if operation failed
if result.is_err() {
// Ignore removal errors
let _ = fs::remove_file(&temp_path);
}
// Return the operation result
result
}
/// Loads the database state from disk
///
/// This function:
/// 1. Loads the latest snapshot if it exists
/// 2. Applies any changes from the append-only log
fn load(&self) -> Result<(), PersistenceError> {
// Check if snapshot file exists
if self.snapshot_path.exists() {
// Read and deserialize the snapshot data
let file = File::open(&self.snapshot_path)?;
// Get the metadata of the snapshot file
let metadata = file.metadata()?;
// Check if the snapshot file is empty
if metadata.len() > 0 {
// Create compressed reader that auto-detects compression mode
let mut reader = CompressedReader::new(file)?;
// Initialize counters for tracking loaded entries
let mut count = 0;
// Stream reading the snapshot to reduce memory usage
loop {
// Increment the counter
count += 1;
// Trace the loading of the snapshot entry
tracing::trace!("Loading snapshot entry: {count}");
// Type alias for the entry
type Entry = (Bytes, Vec<(u64, Option<Bytes>)>);
// Attempt to decode the next entry, handling EOF gracefully
let result: Result<Entry, _> =
bincode::serde::decode_from_std_read(&mut reader, config::standard());
// Detech any end of file errors
match result {
Ok((k, versions)) => {
// Ensure that there are version entries
if !versions.is_empty() {
// Create a new versions entry
let mut entries = Versions::new();
// Add all of the version entries
for (version, value) in versions.into_iter() {
entries.push(Version {
version,
value,
});
}
// Insert the entry into the datastore
self.inner.datastore.insert(k, entries);
}
}
Err(e) => match e {
// Handle bincode decode errors that indicate EOF
bincode::error::DecodeError::Io {
inner,
..
} if inner.kind() == std::io::ErrorKind::UnexpectedEof => {
break;
}
e => return Err(PersistenceError::Deserialization(e)),
},
}
}
}
}
// Check if append-only file exists
if self.aol_path.exists() {
// Open and read the AOL file
let file = File::open(&self.aol_path)?;
// Get the metadata of the append-only file
let metadata = file.metadata()?;
// Check if the append-only file is empty
if metadata.len() > 0 {
// Create buffered reader for efficient reading
let mut reader = BufReader::new(file);
// Initialize counters for tracking loaded entries
let mut count = 0;
// Read and apply each change from the AOL
loop {
// Increment the counter
count += 1;
// Trace the loading of the append-only entry
tracing::trace!("Loading AOL entry: {count}");
// Type alias for the entry
type Entry = (Bytes, u64, Option<Bytes>);
// Explicitly type the result to help type inference
let result: Result<Entry, _> =
bincode::serde::decode_from_std_read(&mut reader, config::standard());
// Detech any end of file errors
match result {
Ok((k, version, val)) => {
// Update existing key, or insert a new one
let mut iter = self.inner.datastore.raw_iter_mut();
if iter.seek_exact(&k) {
let (_, versions) = iter.next().expect("seek_exact returned true");
versions.push(Version {
version,
value: val,
});
} else {
iter.insert_here(
k.clone(),
Versions::from(Version {
version,
value: val,
}),
);
}
}
Err(e) => match e {
// Handle bincode decode errors that indicate EOF
bincode::error::DecodeError::Io {
inner,
..
} if inner.kind() == std::io::ErrorKind::UnexpectedEof => {
break;
}
e => return Err(PersistenceError::Deserialization(e)),
},
}
}
}
}
// Return success
Ok(())
}
/// Truncate the AOL file up to the specified position, preserving any data
/// after.
fn truncate(
aol: &Option<Arc<Mutex<File>>>,
position: u64,
pending_syncs: &Arc<AtomicU64>,
) -> Result<(), PersistenceError> {
// Check that we have a AOL file handle
if let Some(ref aol) = aol {
// Lock the AOL file
let mut file = aol.lock()?;
// Get the current file length
let file_len = file.metadata()?.len();
// Check if there is remaining data
if file_len > position {
// Generate a unique name for the temporary file
let name = format!("aol_truncate_{}.tmp", std::process::id());
// Generate the path for the temporary file
let path = std::env::temp_dir().join(name);
// Execute truncation in a closure for clean error handling
let result = (|| -> Result<(), PersistenceError> {
// Create temporary file and copy remaining data
{
file.seek(SeekFrom::Start(position))?;
// Create the temporary file
let mut temp = File::create(&path)?;
// Copy the remaining data to the temporary file
std::io::copy(&mut *file, &mut temp)?;
// Sync the temporary file
temp.sync_all()?;
}
// Go to the beginning of the file
file.seek(SeekFrom::Start(0))?;
// Truncate the AOL file
file.set_len(0)?;
// Copy data from temporary file
{
let mut temp = File::open(&path)?;
std::io::copy(&mut temp, &mut *file)?;
}
// Flush the file contents
file.flush()?;
// All ok
Ok(())
})();
// Delete the temporary file
let _ = fs::remove_file(&path);
// Return the result
result?;
} else {
// Truncate the AOL file
file.set_len(0)?;
// Flush the file contents
file.flush()?;
}
// Reset pending syncs if we truncated to beginning
if position == 0 {
pending_syncs.store(0, Ordering::Release);
}
}
// All ok
Ok(())
}
/// Spawns a background worker thread for periodic fsync
fn spawn_fsync_worker(&self) {
// Check if AOL is enabled
if self.aol_mode == AolMode::Never {
return;
}
// Get the specified fsync interval
let FsyncMode::Interval(interval) = self.fsync_mode else {
return;
};
// Check if AOL is enabled
if let Some(ref aol) = self.aol {
// Check if a background thread is already running
if self.fsync_handle.read().is_none() {
// Clone necessary fields for the worker thread
let aol = aol.clone();
let pending_syncs = self.pending_syncs.clone();
let enabled = self.background_threads_enabled.clone();
// Spawn the background worker thread
let handle = thread::spawn(move || {
// Check whether the persistence process is enabled
while enabled.load(Ordering::Acquire) {
// Sleep for the configured interval
thread::park_timeout(interval);
// Check shutdown flag again after waking
if !enabled.load(Ordering::Acquire) {
break;
}
// Check if there are pending syncs
if pending_syncs.load(Ordering::Acquire) > 0 {
if let Ok(file) = aol.lock() {
if let Err(e) = file.sync_all() {
tracing::error!("Fsync worker error: {e}");
} else {
pending_syncs.store(0, Ordering::Release);
}
}
}
}
});
// Store and track the thread handle
*self.fsync_handle.write() = Some(handle);
}
}
}
/// Spawns a background worker thread for periodic snapshots
///
/// The worker thread:
/// 1. Sleeps for the configured interval
/// 2. Captures the current AOL file position
/// 3. Creates a new snapshot
/// 4. Truncates AOL up to the cutoff, preserving newer entries
fn spawn_snapshot_worker(&self) {
// Check if snapshots are enabled
if self.snapshot_mode == SnapshotMode::Never {
return;
}
// Only spawn if snapshot interval is configured
let SnapshotMode::Interval(interval) = self.snapshot_mode else {
return;
};
// Check if a background thread is already running
if self.snapshot_handle.read().is_none() {
// Clone necessary fields for the worker thread
let db = self.inner.clone();
let aol = self.aol.clone();
let snapshot_path = self.snapshot_path.clone();
let pending_syncs = self.pending_syncs.clone();
let enabled = self.background_threads_enabled.clone();
let compression = self.compression_mode;
// Spawn the background worker thread
let handle = thread::spawn(move || {
// Check whether the persistence process is enabled
while enabled.load(Ordering::Acquire) {
// Sleep for the configured interval
thread::park_timeout(interval);
// Check shutdown flag again after waking
if !enabled.load(Ordering::Acquire) {
break;
}
// Create temporary file for atomic swap
let temp_path = snapshot_path.with_extension("tmp");
// Ensure clean error handling in closure
let result = (|| -> Result<(), PersistenceError> {
// Create temporary file
let file = File::create(&temp_path)?;
// Create compressed writer (handles buffering internally)
let mut writer = CompressedWriter::new(file, compression)?;
// Get the current position in the AOL file before snapshotting (if AOL
// enabled)
let aol_cutoff_position = if let Some(ref aol) = aol {
aol.lock()?.metadata()?.len()
} else {
0
};
// Stream write each entry to reduce memory usage
let mut iter = db.datastore.raw_iter();
iter.seek_to_first();
while let Some((key, versions)) = iter.next() {
// Get all versions for this key
let versions = versions.all_versions();
// Ensure that there are version entries
if !versions.is_empty() {
// Serialize and write this single entry
bincode::serde::encode_into_std_write(
&(key.clone(), versions),
&mut writer,
config::standard(),
)?;
}
}
// Flush the compressed writer
writer.flush()?;
// Finish compression (finalizes LZ4 stream)
writer.finish()?;
// Atomically rename temporary file
fs::rename(&temp_path, &snapshot_path)?;
// Sync the renamed file to disk for durability
{
let final_file = File::open(&snapshot_path)?;
final_file.sync_all()?;
}
// Truncate AOL to the cutoff position
Self::truncate(&aol, aol_cutoff_position, &pending_syncs)?;
// All ok
Ok(())
})();
// Check if the snapshot operation failed
if let Err(e) = result {
// Trace the snapshot worker error
tracing::error!("Snapshot worker error: {e}");
// Clean up temporary file if it exists
let _ = fs::remove_file(&temp_path);
}
}
});
// Store the worker thread handle
*self.snapshot_handle.write() = Some(handle);
}
}
/// Spawn the background worker thread for processing async append
/// operations
fn spawn_appender_worker(&self) {
// Check if asynchronous append mode is enabled
if self.aol_mode != AolMode::AsynchronousAfterCommit {
return;
}
// Check if AOL is enabled
if let Some(ref aol) = self.aol {
// Check if a background thread is already running
if self.appender_handle.read().is_none() {
// Clone necessary fields for the worker thread
let injector = self.async_append_injector.clone();
let aol = aol.clone();
let fsync_mode = self.fsync_mode;
let enabled = self.background_threads_enabled.clone();
let pending_syncs = self.pending_syncs.clone();
let last_fsync = self.last_fsync.clone();
// Spawn the background worker thread
let handle = thread::spawn(move || {
// Set the batch size and timeout
const BATCH_SIZE: usize = 100;
const TIMEOUT_MS: u64 = 10;
// Initialize the batch vector
let mut batch = Vec::with_capacity(BATCH_SIZE);
// Check whether the persistence process is enabled
while enabled.load(Ordering::Acquire) {
// Check shutdown flag again after waking
if !enabled.load(Ordering::Acquire) {
break;
}
// Clear the batch
batch.clear();
// Collect operations into a batch
loop {
// Check shutdown flag in the inner loop
if !enabled.load(Ordering::Acquire) {
break;
}
match injector.steal() {
Steal::Retry => {
std::thread::yield_now();
continue;
}
Steal::Success(op) => {
batch.push(op);
if batch.len() == BATCH_SIZE {
break;
}
}
Steal::Empty => {
// If we have items to append, break
if !batch.is_empty() {
break;
}
// Park the thread to wait for work
thread::park_timeout(Duration::from_millis(TIMEOUT_MS));
}
}
}
// Process the batch if we have operations
if !batch.is_empty() {
// Ensure clean error handling in closure
let result = (|| -> Result<(), PersistenceError> {
// Lock the AOL file for writing
if let Ok(mut file) = aol.lock() {
// Create a new buffer for the AOL file
let mut writer = BufWriter::new(&mut *file);
// Write all operations in the batch
for op in &batch {
for (k, v) in &op.writeset {
bincode::serde::encode_into_std_write(
(k, op.version, v),
&mut writer,
config::standard(),
)?;
}
}
// Flush the buffer to the file on the operating system
writer.flush()?;
// Drop the writer to release the mutable borrow
drop(writer);
// Handle fsync based on mode
match fsync_mode {
// Let the operating system handle syncing to disk
FsyncMode::Never => {
// No fsync, just increment pending counter
pending_syncs.fetch_add(1, Ordering::Release);
}
// Sync immediately to diskafter every append
FsyncMode::EveryAppend => {
// Sync immediately
file.sync_all()?;
}
// Force sync to disk at a specified interval
FsyncMode::Interval(duration) => {
// Check if we should sync based on time
let now = Instant::now();
// Check if we should sync based on time
let should_sync = {
// Get the last fsync time
let mut last_fsync = last_fsync.lock()?;
// Check if the last fsync time is greater than the
// duration
if now.duration_since(*last_fsync) >= duration {
// Update the last fsync time
*last_fsync = now;
true
} else {
false
}
};
// Check if we should sync
if should_sync {
// Force sync the AOL file to disk
file.sync_all()?;
// Reset the pending syncs counter
pending_syncs.store(0, Ordering::Release);
} else {
// Increment the pending syncs counter
pending_syncs.fetch_add(1, Ordering::Release);
}
}
}
}
// All ok
Ok(())
})();
// Check if the async append operation failed
if let Err(e) = result {
// Trace the snapshot worker error
tracing::error!("Async append worker error: {e}");
}
}
}
});
// Store the thread handle
*self.appender_handle.write() = Some(handle);
}
}
}
/// Appends a set of changes to the append-only log
///
/// # Arguments
/// * `version` - The version (timestamp) for these changes
/// * `writeset` - Map of key-value changes to append
///
/// # Returns
/// * `Result<(), PersistenceError>` - Success or an error
pub(crate) fn append(
&self,
version: u64,
writeset: &BTreeMap<Bytes, Option<Bytes>>,
) -> Result<(), PersistenceError> {
// Skip AOL writing if AOL is disabled
if self.aol_mode == AolMode::Never {
return Ok(());
}
// AOL is enabled, proceed with append logic
if let Some(ref aol) = self.aol {
// Handle asynchronous AOL mode by queuing the operation
if self.aol_mode == AolMode::AsynchronousAfterCommit {
// Queue the append operation
self.async_append_injector.push(AsyncAppendOperation {
version,
writeset: writeset.clone(),
});
// Wake up the async append worker if available
if let Some(handle) = self.appender_handle.read().as_ref() {
handle.thread().unpark();
}
}
if self.aol_mode == AolMode::SynchronousOnCommit {
// Lock the AOL file for writing
let mut file = aol.lock()?;
// Create a new buffer for the AOL file
let mut writer = BufWriter::new(&mut *file);
// Serialize and write each change with version
for (k, v) in writeset {
bincode::serde::encode_into_std_write(
(k, version, v),
&mut writer,
config::standard(),
)?;
}
// Flush the buffer to the file on the operating system
writer.flush()?;
// Drop the writer to release the mutable borrow
drop(writer);
// Handle fsync based on mode
match self.fsync_mode {
// Let the operating system handle syncing to disk
FsyncMode::Never => {
// No fsync, just increment pending counter
self.pending_syncs.fetch_add(1, Ordering::Release);
}
// Sync immediately to diskafter every append
FsyncMode::EveryAppend => {
// Sync immediately
file.sync_all()?;
}
// Force sync to disk at a specified interval
FsyncMode::Interval(duration) => {
// Check if we should sync based on time
let now = Instant::now();
// Check if we should sync based on time
let should_sync = {
// Get the last fsync time
let mut last_fsync = self.last_fsync.lock()?;
// Check if the last fsync time is greater than the duration
if now.duration_since(*last_fsync) >= duration {
// Update the last fsync time
*last_fsync = now;
true
} else {
false
}
};
// Check if we should sync
if should_sync {
// Force sync the AOL file to disk
file.sync_all()?;
// Reset the pending syncs counter
self.pending_syncs.store(0, Ordering::Release);
} else {
// Increment the pending syncs counter
self.pending_syncs.fetch_add(1, Ordering::Release);
}
}
}
}
}
// All ok
Ok(())
}
}
impl Drop for Persistence {
/// Cleans up resources when the persistence layer is dropped
fn drop(&mut self) {
// Signal shutdown to the worker threads
self.background_threads_enabled.store(false, Ordering::Release);
// Stop the fsync worker if it exists
if let Some(handle) = self.fsync_handle.write().take() {
handle.thread().unpark();
let _ = handle.join();
}
// Stop the snapshot worker if it exists
if let Some(handle) = self.snapshot_handle.write().take() {
handle.thread().unpark();
let _ = handle.join();
}
// Stop the async append worker if it exists
if let Some(handle) = self.appender_handle.write().take() {
handle.thread().unpark();
let _ = handle.join();
}
// Perform final fsync if there are pending syncs
if self.aol_mode != AolMode::Never && self.pending_syncs.load(Ordering::Acquire) > 0 {
// Try to acquire lock on AOL file
if let Some(ref aol) = self.aol {
// Lock the AOL file
if let Ok(file) = aol.lock() {
// Sync file contents to disk
let _ = file.sync_all();
}
}
}
}
}