Skip to main content

reddb_server/storage/wal/
checkpoint.rs

1//! Checkpoint Manager
2//!
3//! Responsible for transferring committed transactions from the WAL to the main
4//! database file. Checkpointing ensures durability and allows WAL truncation.
5//!
6//! # Algorithm
7//!
8//! 1. Read all WAL records sequentially
9//! 2. Track transaction states (Begin, Commit, Rollback)
10//! 3. For committed transactions, collect PageWrite records
11//! 4. Apply committed pages to the Pager in LSN order
12//! 5. Sync Pager to disk
13//! 6. Update checkpoint LSN in database header
14//! 7. Truncate WAL
15//!
16//! # References
17//!
18//! - Turso `core/storage/wal.rs:checkpoint()` - Checkpoint logic
19//! - SQLite WAL documentation
20
21use std::collections::{HashMap, HashSet};
22use std::io;
23use std::path::Path;
24
25use super::reader::WalReader;
26use super::record::WalRecord;
27use super::writer::WalWriter;
28use crate::storage::engine::{Page, Pager, PAGE_SIZE};
29
30/// Checkpoint mode
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum CheckpointMode {
33    /// Passive: Only checkpoint if no active writers
34    Passive,
35    /// Full: Wait for active writers to finish, then checkpoint all
36    Full,
37    /// Restart: Like Full, but also truncates the WAL
38    Restart,
39    /// Truncate: Checkpoint all and truncate WAL
40    Truncate,
41}
42
43/// Checkpoint result statistics
44#[derive(Debug, Clone, Default)]
45pub struct CheckpointResult {
46    /// Number of transactions processed
47    pub transactions_processed: u64,
48    /// Number of pages checkpointed
49    pub pages_checkpointed: u64,
50    /// Number of records processed
51    pub records_processed: u64,
52    /// Final LSN after checkpoint
53    pub checkpoint_lsn: u64,
54    /// Whether WAL was truncated
55    pub wal_truncated: bool,
56}
57
58/// Checkpoint error types
59#[derive(Debug)]
60pub enum CheckpointError {
61    /// I/O error
62    Io(io::Error),
63    /// Pager error
64    Pager(String),
65    /// WAL is corrupted
66    CorruptedWal(String),
67    /// No WAL file found
68    NoWal,
69}
70
71impl std::fmt::Display for CheckpointError {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        match self {
74            Self::Io(e) => write!(f, "I/O error: {}", e),
75            Self::Pager(msg) => write!(f, "Pager error: {}", msg),
76            Self::CorruptedWal(msg) => write!(f, "Corrupted WAL: {}", msg),
77            Self::NoWal => write!(f, "No WAL file found"),
78        }
79    }
80}
81
82impl std::error::Error for CheckpointError {}
83
84impl From<io::Error> for CheckpointError {
85    fn from(e: io::Error) -> Self {
86        Self::Io(e)
87    }
88}
89
90/// Transaction state during checkpoint
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92enum TxState {
93    Active,
94    Committed,
95    Aborted,
96}
97
98/// Pending page write from a transaction
99#[derive(Debug)]
100struct PendingWrite {
101    tx_id: u64,
102    page_id: u32,
103    data: Vec<u8>,
104    lsn: u64,
105}
106
107/// Checkpoint manager
108///
109/// Responsible for transferring committed transactions from the WAL to the main database file.
110pub struct Checkpointer {
111    /// Checkpoint mode
112    mode: CheckpointMode,
113}
114
115impl Checkpointer {
116    /// Create a new checkpointer with the given mode
117    pub fn new(mode: CheckpointMode) -> Self {
118        Self { mode }
119    }
120
121    /// Create a checkpointer with default mode (Full)
122    pub fn default_mode() -> Self {
123        Self::new(CheckpointMode::Full)
124    }
125
126    /// Perform a checkpoint
127    ///
128    /// Reads all records from the WAL and applies committed changes to the database.
129    ///
130    /// # Arguments
131    ///
132    /// * `pager` - The Pager to write committed pages to
133    /// * `wal_path` - Path to the WAL file
134    ///
135    /// # Returns
136    ///
137    /// Checkpoint statistics or error
138    pub fn checkpoint(
139        &self,
140        pager: &Pager,
141        wal_path: &Path,
142    ) -> Result<CheckpointResult, CheckpointError> {
143        // Open WAL for reading
144        let wal_reader = match WalReader::open(wal_path) {
145            Ok(r) => r,
146            Err(e) if e.kind() == io::ErrorKind::NotFound => {
147                // No WAL file - nothing to checkpoint
148                return Ok(CheckpointResult::default());
149            }
150            Err(e) => return Err(CheckpointError::Io(e)),
151        };
152
153        // Phase 1: Read and categorize all records
154        let mut tx_states: HashMap<u64, TxState> = HashMap::new();
155        let mut pending_writes: Vec<PendingWrite> = Vec::new();
156        let mut records_processed: u64 = 0;
157        let mut last_lsn: u64 = 0;
158
159        for record_result in wal_reader.iter() {
160            let (lsn, record) = record_result.map_err(CheckpointError::Io)?;
161            records_processed += 1;
162            last_lsn = lsn;
163
164            match record {
165                WalRecord::Begin { tx_id } => {
166                    tx_states.insert(tx_id, TxState::Active);
167                }
168                WalRecord::Commit { tx_id } => {
169                    tx_states.insert(tx_id, TxState::Committed);
170                }
171                WalRecord::Rollback { tx_id } => {
172                    tx_states.insert(tx_id, TxState::Aborted);
173                }
174                WalRecord::PageWrite {
175                    tx_id,
176                    page_id,
177                    data,
178                } => {
179                    pending_writes.push(PendingWrite {
180                        tx_id,
181                        page_id,
182                        data,
183                        lsn,
184                    });
185                }
186                WalRecord::Checkpoint {
187                    lsn: _checkpoint_lsn,
188                } => {
189                    // Checkpoint marker - we can skip records before this LSN
190                    // For now, we process everything
191                }
192                WalRecord::TxCommitBatch { .. } => {
193                    // Store-level logical commit batches are replayed by
194                    // UnifiedStore, not by the pager page checkpoint path.
195                }
196            }
197        }
198
199        // Phase 2: Filter for committed transactions only
200        let committed_txs: HashSet<u64> = tx_states
201            .iter()
202            .filter(|(_, state)| **state == TxState::Committed)
203            .map(|(tx_id, _)| *tx_id)
204            .collect();
205
206        // Phase 3: Collect pages from committed transactions
207        // Keep only the latest write for each page (from committed txs)
208        let mut latest_writes: HashMap<u32, Vec<u8>> = HashMap::new();
209
210        for write in pending_writes {
211            if committed_txs.contains(&write.tx_id) {
212                // Always overwrite with later writes (they have higher LSN)
213                latest_writes.insert(write.page_id, write.data);
214            }
215        }
216
217        // Phase 4 (PREPARE): Mark checkpoint in progress in header
218        if !latest_writes.is_empty() {
219            pager
220                .set_checkpoint_in_progress(true, last_lsn)
221                .map_err(|e| CheckpointError::Pager(e.to_string()))?;
222        }
223
224        // Phase 5 (APPLY): Write committed pages to Pager
225        let mut pages_checkpointed: u64 = 0;
226
227        for (page_id, data) in &latest_writes {
228            // Reconstruct page from WAL data
229            if data.len() != PAGE_SIZE {
230                return Err(CheckpointError::CorruptedWal(format!(
231                    "Page {} has wrong size: {} (expected {})",
232                    page_id,
233                    data.len(),
234                    PAGE_SIZE
235                )));
236            }
237
238            let mut page_data = [0u8; PAGE_SIZE];
239            page_data.copy_from_slice(data);
240            let page = Page::from_bytes(page_data);
241
242            // Write to pager
243            pager
244                .write_page(*page_id, page)
245                .map_err(|e| CheckpointError::Pager(e.to_string()))?;
246
247            pages_checkpointed += 1;
248        }
249
250        // Phase 6: Sync Pager to disk
251        pager
252            .sync()
253            .map_err(|e| CheckpointError::Pager(e.to_string()))?;
254
255        // Phase 7 (COMPLETE): Clear in-progress flag and update checkpoint LSN
256        if !latest_writes.is_empty() {
257            pager
258                .complete_checkpoint(last_lsn)
259                .map_err(|e| CheckpointError::Pager(e.to_string()))?;
260        }
261
262        // Phase 8: Truncate WAL if requested
263        let wal_truncated = matches!(
264            self.mode,
265            CheckpointMode::Restart | CheckpointMode::Truncate
266        );
267
268        if wal_truncated {
269            let mut wal_writer = WalWriter::open(wal_path)?;
270            wal_writer.truncate()?;
271
272            // Write checkpoint marker with current LSN
273            let checkpoint_record = WalRecord::Checkpoint { lsn: last_lsn };
274            wal_writer.append(&checkpoint_record)?;
275            wal_writer.sync()?;
276        }
277
278        Ok(CheckpointResult {
279            transactions_processed: committed_txs.len() as u64,
280            pages_checkpointed,
281            records_processed,
282            checkpoint_lsn: last_lsn,
283            wal_truncated,
284        })
285    }
286
287    /// Perform crash recovery
288    ///
289    /// Called on database open to apply any committed transactions from the WAL
290    /// that weren't checkpointed before the crash. If a checkpoint was interrupted
291    /// (checkpoint_in_progress flag set), re-applies all WAL records from scratch.
292    ///
293    /// # Arguments
294    ///
295    /// * `pager` - The Pager to recover into
296    /// * `wal_path` - Path to the WAL file
297    ///
298    /// # Returns
299    ///
300    /// Recovery statistics or error
301    pub fn recover(pager: &Pager, wal_path: &Path) -> Result<CheckpointResult, CheckpointError> {
302        // Check if a checkpoint was interrupted
303        if let Ok(header) = pager.header() {
304            if header.checkpoint_in_progress {
305                // Previous checkpoint was interrupted — re-apply everything from WAL
306                // This is safe because page writes are idempotent (last-write-wins)
307                let _ = pager.set_checkpoint_in_progress(false, 0);
308            }
309        }
310        let checkpointer = Self::new(CheckpointMode::Truncate);
311        checkpointer.checkpoint(pager, wal_path)
312    }
313}
314
315#[cfg(test)]
316mod tests {
317    use super::*;
318    use crate::storage::engine::PageType;
319    use std::fs;
320    use std::time::{SystemTime, UNIX_EPOCH};
321
322    fn temp_dir() -> std::path::PathBuf {
323        let timestamp = SystemTime::now()
324            .duration_since(UNIX_EPOCH)
325            .unwrap()
326            .as_nanos();
327        std::env::temp_dir().join(format!("reddb_checkpoint_test_{}", timestamp))
328    }
329
330    fn cleanup(dir: &Path) {
331        let _ = fs::remove_dir_all(dir);
332    }
333
334    #[test]
335    fn test_checkpoint_empty_wal() {
336        let dir = temp_dir();
337        let _ = fs::create_dir_all(&dir);
338        let db_path = dir.join("test.db");
339        let wal_path = dir.join("test.wal");
340
341        // Create pager
342        let pager = Pager::open_default(&db_path).unwrap();
343
344        // No WAL file - should succeed with empty result
345        let checkpointer = Checkpointer::default_mode();
346        let result = checkpointer.checkpoint(&pager, &wal_path).unwrap();
347
348        assert_eq!(result.transactions_processed, 0);
349        assert_eq!(result.pages_checkpointed, 0);
350
351        cleanup(&dir);
352    }
353
354    #[test]
355    fn test_checkpoint_committed_transaction() {
356        let dir = temp_dir();
357        let _ = fs::create_dir_all(&dir);
358        let db_path = dir.join("test.db");
359        let wal_path = dir.join("test.wal");
360
361        // Create pager
362        let pager = Pager::open_default(&db_path).unwrap();
363
364        // Allocate a page to get its ID
365        let page = pager.allocate_page(PageType::BTreeLeaf).unwrap();
366        let page_id = page.page_id();
367
368        // Create WAL with a committed transaction
369        {
370            let mut wal_writer = WalWriter::open(&wal_path).unwrap();
371
372            // Begin transaction
373            wal_writer.append(&WalRecord::Begin { tx_id: 1 }).unwrap();
374
375            // Write a page
376            let mut page_data = [0u8; PAGE_SIZE];
377            page_data[0] = 0x42; // Mark with test byte
378            wal_writer
379                .append(&WalRecord::PageWrite {
380                    tx_id: 1,
381                    page_id,
382                    data: page_data.to_vec(),
383                })
384                .unwrap();
385
386            // Commit transaction
387            wal_writer.append(&WalRecord::Commit { tx_id: 1 }).unwrap();
388
389            wal_writer.sync().unwrap();
390        }
391
392        // Checkpoint
393        let checkpointer = Checkpointer::new(CheckpointMode::Full);
394        let result = checkpointer.checkpoint(&pager, &wal_path).unwrap();
395
396        assert_eq!(result.transactions_processed, 1);
397        assert_eq!(result.pages_checkpointed, 1);
398        assert_eq!(result.records_processed, 3);
399
400        // Verify page was written
401        let read_page = pager.read_page(page_id).unwrap();
402        assert_eq!(read_page.as_bytes()[0], 0x42);
403
404        cleanup(&dir);
405    }
406
407    #[test]
408    fn test_checkpoint_aborted_transaction() {
409        let dir = temp_dir();
410        let _ = fs::create_dir_all(&dir);
411        let db_path = dir.join("test.db");
412        let wal_path = dir.join("test.wal");
413
414        // Create pager
415        let pager = Pager::open_default(&db_path).unwrap();
416
417        // Allocate a page to get its ID
418        let page = pager.allocate_page(PageType::BTreeLeaf).unwrap();
419        let page_id = page.page_id();
420
421        // Create WAL with an aborted transaction
422        {
423            let mut wal_writer = WalWriter::open(&wal_path).unwrap();
424
425            // Begin transaction
426            wal_writer.append(&WalRecord::Begin { tx_id: 1 }).unwrap();
427
428            // Write a page
429            let mut page_data = [0u8; PAGE_SIZE];
430            page_data[0] = 0x42;
431            wal_writer
432                .append(&WalRecord::PageWrite {
433                    tx_id: 1,
434                    page_id,
435                    data: page_data.to_vec(),
436                })
437                .unwrap();
438
439            // Rollback transaction
440            wal_writer
441                .append(&WalRecord::Rollback { tx_id: 1 })
442                .unwrap();
443
444            wal_writer.sync().unwrap();
445        }
446
447        // Checkpoint
448        let checkpointer = Checkpointer::new(CheckpointMode::Full);
449        let result = checkpointer.checkpoint(&pager, &wal_path).unwrap();
450
451        // Aborted transaction should not be checkpointed
452        assert_eq!(result.transactions_processed, 0);
453        assert_eq!(result.pages_checkpointed, 0);
454
455        // Verify page was NOT written (should still be zeros)
456        let read_page = pager.read_page(page_id).unwrap();
457        assert_ne!(read_page.as_bytes()[0], 0x42);
458
459        cleanup(&dir);
460    }
461
462    #[test]
463    fn test_checkpoint_mixed_transactions() {
464        let dir = temp_dir();
465        let _ = fs::create_dir_all(&dir);
466        let db_path = dir.join("test.db");
467        let wal_path = dir.join("test.wal");
468
469        // Create pager
470        let pager = Pager::open_default(&db_path).unwrap();
471
472        // Allocate pages
473        let page1 = pager.allocate_page(PageType::BTreeLeaf).unwrap();
474        let page2 = pager.allocate_page(PageType::BTreeLeaf).unwrap();
475        let page1_id = page1.page_id();
476        let page2_id = page2.page_id();
477
478        // Create WAL with mixed transactions
479        {
480            let mut wal_writer = WalWriter::open(&wal_path).unwrap();
481
482            // Transaction 1: Committed
483            wal_writer.append(&WalRecord::Begin { tx_id: 1 }).unwrap();
484            let mut page_data1 = [0u8; PAGE_SIZE];
485            page_data1[0] = 0x11;
486            wal_writer
487                .append(&WalRecord::PageWrite {
488                    tx_id: 1,
489                    page_id: page1_id,
490                    data: page_data1.to_vec(),
491                })
492                .unwrap();
493            wal_writer.append(&WalRecord::Commit { tx_id: 1 }).unwrap();
494
495            // Transaction 2: Aborted
496            wal_writer.append(&WalRecord::Begin { tx_id: 2 }).unwrap();
497            let mut page_data2 = [0u8; PAGE_SIZE];
498            page_data2[0] = 0x22;
499            wal_writer
500                .append(&WalRecord::PageWrite {
501                    tx_id: 2,
502                    page_id: page2_id,
503                    data: page_data2.to_vec(),
504                })
505                .unwrap();
506            wal_writer
507                .append(&WalRecord::Rollback { tx_id: 2 })
508                .unwrap();
509
510            // Transaction 3: Committed
511            wal_writer.append(&WalRecord::Begin { tx_id: 3 }).unwrap();
512            let mut page_data3 = [0u8; PAGE_SIZE];
513            page_data3[0] = 0x33;
514            wal_writer
515                .append(&WalRecord::PageWrite {
516                    tx_id: 3,
517                    page_id: page2_id,
518                    data: page_data3.to_vec(),
519                })
520                .unwrap();
521            wal_writer.append(&WalRecord::Commit { tx_id: 3 }).unwrap();
522
523            wal_writer.sync().unwrap();
524        }
525
526        // Checkpoint
527        let checkpointer = Checkpointer::new(CheckpointMode::Full);
528        let result = checkpointer.checkpoint(&pager, &wal_path).unwrap();
529
530        // Only committed transactions (1 and 3) should be processed
531        assert_eq!(result.transactions_processed, 2);
532        assert_eq!(result.pages_checkpointed, 2);
533
534        // Verify pages
535        let read_page1 = pager.read_page(page1_id).unwrap();
536        assert_eq!(read_page1.as_bytes()[0], 0x11);
537
538        let read_page2 = pager.read_page(page2_id).unwrap();
539        assert_eq!(read_page2.as_bytes()[0], 0x33); // From tx 3, not tx 2
540
541        cleanup(&dir);
542    }
543
544    #[test]
545    fn test_checkpoint_truncate() {
546        let dir = temp_dir();
547        let _ = fs::create_dir_all(&dir);
548        let db_path = dir.join("test.db");
549        let wal_path = dir.join("test.wal");
550
551        // Create pager
552        let pager = Pager::open_default(&db_path).unwrap();
553
554        // Create WAL with a committed transaction
555        {
556            let mut wal_writer = WalWriter::open(&wal_path).unwrap();
557            wal_writer.append(&WalRecord::Begin { tx_id: 1 }).unwrap();
558            wal_writer.append(&WalRecord::Commit { tx_id: 1 }).unwrap();
559            wal_writer.sync().unwrap();
560        }
561
562        // Checkpoint with truncate
563        let checkpointer = Checkpointer::new(CheckpointMode::Truncate);
564        let result = checkpointer.checkpoint(&pager, &wal_path).unwrap();
565
566        assert!(result.wal_truncated);
567
568        // WAL should be truncated (only header + checkpoint marker)
569        let wal_size = fs::metadata(&wal_path).unwrap().len();
570        // Header (8 bytes) + Checkpoint record (1 + 8 + 4 = 13 bytes)
571        assert!(
572            wal_size < 50,
573            "WAL should be truncated, but size is {}",
574            wal_size
575        );
576
577        cleanup(&dir);
578    }
579}