linkrs-storage 0.1.0

Storage engine with CSR and memory-mapped containers
Documentation
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
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use parking_lot::{Condvar, Mutex};

use linkrs_core::StorageResult;
use linkrs_metrics::{CheckpointTriggerReason, StatsManager};

use crate::engine::persistence_coordinator::{
    CheckpointStats, PersistenceCoordinator, PersistenceStateGuard,
};
use crate::thread_pool::{BackgroundTaskHandle, StorageThreadPool};

#[derive(Debug, Clone, Copy)]
struct CheckpointRequest {
    requested_at: Instant,
    reason: CheckpointTriggerReason,
}

/// Background checkpoint scheduler.
///
/// Monitors WAL size and time since last checkpoint, triggering
/// non-blocking checkpoint operations on the shared thread pool.
pub struct CheckpointScheduler {
    coordinator: Arc<parking_lot::RwLock<PersistenceCoordinator>>,
    thread_pool: Arc<StorageThreadPool>,
    stats: Arc<Mutex<Option<Arc<StatsManager>>>>,
    pending: Arc<AtomicBool>,
    pending_request: Arc<Mutex<Option<CheckpointRequest>>>,
    /// In-flight checkpoint tasks spawned onto the pool. `stop` drains this
    /// count so shutdown cannot leave a checkpoint writing to the storage
    /// directory after the owning handle is gone.
    active: Arc<(Mutex<usize>, Condvar)>,
    handle: Option<BackgroundTaskHandle>,
    poll_interval: Duration,
    enabled: bool,
    executor: Arc<
        dyn Fn(PersistenceStateGuard, CheckpointTriggerReason) -> StorageResult<CheckpointStats>
            + Send
            + Sync,
    >,
}

impl CheckpointScheduler {
    pub fn new(
        coordinator: Arc<parking_lot::RwLock<PersistenceCoordinator>>,
        thread_pool: Arc<StorageThreadPool>,
        stats: Option<Arc<StatsManager>>,
        poll_interval: Duration,
        enabled: bool,
        executor: Arc<
            dyn Fn(PersistenceStateGuard, CheckpointTriggerReason) -> StorageResult<CheckpointStats>
                + Send
                + Sync,
        >,
    ) -> Self {
        Self {
            coordinator,
            thread_pool,
            stats: Arc::new(Mutex::new(stats)),
            pending: Arc::new(AtomicBool::new(false)),
            pending_request: Arc::new(Mutex::new(None)),
            active: Arc::new((Mutex::new(0), Condvar::new())),
            handle: None,
            poll_interval,
            enabled,
            executor,
        }
    }

    pub fn set_stats(&self, stats: Option<Arc<StatsManager>>) {
        *self.stats.lock() = stats;
    }

    /// Request a checkpoint with the given reason.
    /// Non-blocking and deduplicated via AtomicBool.
    pub fn request_checkpoint(&self, reason: CheckpointTriggerReason) {
        if !self.enabled {
            return;
        }
        if self
            .pending
            .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
            .is_ok()
        {
            let mut guard = self.pending_request.lock();
            *guard = Some(CheckpointRequest {
                requested_at: Instant::now(),
                reason,
            });
        } else if let Some(stats) = self.stats.lock().as_ref() {
            stats.record_checkpoint_deduplicated();
        }
    }

    /// Start the periodic background polling task.
    pub fn start(&mut self) {
        if !self.enabled || self.handle.is_some() {
            return;
        }
        let coordinator = self.coordinator.clone();
        let stats = self.stats.clone();
        let pending = self.pending.clone();
        let pending_request = self.pending_request.clone();
        let executor = self.executor.clone();
        let active = self.active.clone();
        let thread_pool_clone = self.thread_pool.clone();

        let running = Arc::new(AtomicBool::new(false));
        let poll_interval = self.poll_interval;
        let handle = self.thread_pool.spawn_periodic(
            running,
            poll_interval,
            Duration::from_millis(10),
            move || {
                // Passive polling: check if checkpoint is needed
                let should = {
                    let coord = coordinator.read();
                    coord.should_checkpoint()
                };
                if should {
                    let reason = {
                        let coord = coordinator.read();
                        coord
                            .checkpoint_trigger_reason()
                            .unwrap_or(CheckpointTriggerReason::TimeSinceLastCheckpoint)
                    };
                    if pending
                        .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
                        .is_ok()
                    {
                        let mut guard = pending_request.lock();
                        *guard = Some(CheckpointRequest {
                            requested_at: Instant::now(),
                            reason,
                        });
                    } else if let Some(s) = stats.lock().as_ref() {
                        s.record_checkpoint_deduplicated();
                    }
                }

                // Try to execute pending request
                let request_opt = {
                    let mut guard = pending_request.lock();
                    guard.take()
                };
                if let Some(req) = request_opt {
                    pending.store(false, Ordering::Release);
                    let age = req.requested_at.elapsed();
                    if let Some(s) = stats.lock().as_ref().cloned() {
                        s.record_checkpoint_trigger(req.reason, age);
                    }
                    match coordinator.read().try_enter_checkpoint() {
                        Ok(guard) => {
                            let stats_clone = stats.clone();
                            let exec = executor.clone();
                            let reason = req.reason;
                            let pool = thread_pool_clone.clone();
                            let active_task = active.clone();
                            *active_task.0.lock() += 1;
                            // Spawn checkpoint on the shared pool so the periodic
                            // poll loop is never blocked by a long-running checkpoint.
                            pool.spawn(move || {
                                match exec(guard, reason) {
                                    Ok(cs) => {
                                        if let Some(s) = stats_clone.lock().as_ref().cloned() {
                                            s.record_checkpoint_success(
                                                cs.duration,
                                                cs.bytes_flushed,
                                                cs.wal_files_truncated as u64,
                                            );
                                        }
                                        log::info!(
                                            "Async checkpoint completed: id={} reason={:?}",
                                            cs.checkpoint_id,
                                            reason
                                        );
                                    }
                                    Err(e) => {
                                        if let Some(s) = stats_clone.lock().as_ref().cloned() {
                                            s.record_checkpoint_failure();
                                        }
                                        log::warn!(
                                            "Async checkpoint failed (reason={:?}): {}",
                                            reason,
                                            e
                                        );
                                    }
                                }
                                let (lock, cv) = &*active_task;
                                let mut count = lock.lock();
                                *count = count.saturating_sub(1);
                                if *count == 0 {
                                    cv.notify_all();
                                }
                            });
                        }
                        Err(_) => {
                            if let Some(s) = stats.lock().as_ref().cloned() {
                                s.record_checkpoint_blocked();
                            }
                        }
                    }
                }
            },
        );
        self.handle = Some(handle);
    }

    pub fn stop(&mut self) {
        if let Some(handle) = self.handle.take() {
            handle.stop();
            handle.join();
        }
        self.pending.store(false, Ordering::Release);
        *self.pending_request.lock() = None;
        // Drain in-flight checkpoint tasks so a dropped handle cannot leave a
        // checkpoint writing to the directory a subsequent open recovers.
        let (lock, cv) = &*self.active;
        let mut count = lock.lock();
        while *count > 0 {
            cv.wait(&mut count);
        }
    }

    pub fn is_running(&self) -> bool {
        self.handle
            .as_ref()
            .map(|h| h.is_running())
            .unwrap_or(false)
    }

    pub fn pending(&self) -> bool {
        self.pending.load(Ordering::Acquire)
    }

    /// Monotonic checkpoint epoch hint for table commit manifests. Reads the
    /// published checkpoint sequence (the scheduler's single-flight order),
    /// so per-table commits share one global order without a new clock.
    pub fn epoch_hint(&self) -> u64 {
        self.coordinator
            .read()
            .manifest_manager
            .load_latest()
            .ok()
            .flatten()
            .map(|m| m.checkpoint_id.saturating_add(1))
            .unwrap_or(1)
            .max(1)
    }
}

impl Drop for CheckpointScheduler {
    fn drop(&mut self) {
        self.stop();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::AtomicUsize;

    #[test]
    fn test_deduplication() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let config = crate::engine::PersistenceConfig::for_work_dir(temp_dir.path());
        let coordinator = Arc::new(parking_lot::RwLock::new(
            PersistenceCoordinator::new(config).unwrap(),
        ));
        let pool = Arc::new(StorageThreadPool::new().unwrap());
        let stats = Arc::new(StatsManager::new());
        let call_count = Arc::new(AtomicUsize::new(0));
        let cc = call_count.clone();
        let executor: Arc<
            dyn Fn(PersistenceStateGuard, CheckpointTriggerReason) -> StorageResult<CheckpointStats>
                + Send
                + Sync,
        > = Arc::new(move |_g, _| {
            cc.fetch_add(1, Ordering::Relaxed);
            Err(linkrs_core::StorageError::db_error("injected"))
        });
        let scheduler = CheckpointScheduler::new(
            coordinator,
            pool,
            Some(stats.clone()),
            Duration::from_secs(1),
            true,
            executor,
        );
        // First request should be pending
        scheduler.request_checkpoint(CheckpointTriggerReason::WalSizeExceeded);
        assert!(scheduler.pending());
        // Second request should be deduplicated
        scheduler.request_checkpoint(CheckpointTriggerReason::Explicit);
        assert_eq!(
            stats.get_value(linkrs_metrics::MetricType::CheckpointRequestsDeduplicated),
            Some(1)
        );
        // Still pending with original reason
        assert!(scheduler.pending());
    }

    #[test]
    fn test_disabled_scheduler_does_not_accept_requests() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let config = crate::engine::PersistenceConfig::for_work_dir(temp_dir.path());
        let coordinator = Arc::new(parking_lot::RwLock::new(
            PersistenceCoordinator::new(config).unwrap(),
        ));
        let pool = Arc::new(StorageThreadPool::new().unwrap());
        let stats = Arc::new(StatsManager::new());
        let executor: Arc<
            dyn Fn(PersistenceStateGuard, CheckpointTriggerReason) -> StorageResult<CheckpointStats>
                + Send
                + Sync,
        > = Arc::new(|_, _| Err(linkrs_core::StorageError::db_error("injected")));
        let scheduler = CheckpointScheduler::new(
            coordinator,
            pool,
            Some(stats),
            Duration::from_secs(1),
            false,
            executor,
        );
        scheduler.request_checkpoint(CheckpointTriggerReason::Explicit);
        assert!(!scheduler.pending());
    }

    #[test]
    fn test_blocked_checkpoint_records_metric() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let config = crate::engine::PersistenceConfig::for_work_dir(temp_dir.path());
        let coordinator = Arc::new(parking_lot::RwLock::new(
            PersistenceCoordinator::new(config).unwrap(),
        ));
        let pool = Arc::new(StorageThreadPool::new().unwrap());
        let stats = Arc::new(StatsManager::new());
        // Hold the checkpoint lock externally so scheduler's try_enter fails.
        let _guard = coordinator.read().try_enter_checkpoint().unwrap();
        let executor: Arc<
            dyn Fn(PersistenceStateGuard, CheckpointTriggerReason) -> StorageResult<CheckpointStats>
                + Send
                + Sync,
        > = Arc::new(|_, _| {
            panic!("executor should not be called when checkpoint is already active");
        });
        let mut scheduler = CheckpointScheduler::new(
            coordinator.clone(),
            pool,
            Some(stats.clone()),
            Duration::from_millis(20),
            true,
            executor,
        );
        scheduler.request_checkpoint(CheckpointTriggerReason::Explicit);
        scheduler.start();
        std::thread::sleep(Duration::from_millis(100));
        scheduler.stop();
        assert_eq!(
            stats.get_value(linkrs_metrics::MetricType::CheckpointRequestsBlocked),
            Some(1)
        );
    }

    #[test]
    fn test_async_checkpoint_triggered_by_wal_size() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let mut config = crate::engine::PersistenceConfig::for_work_dir(temp_dir.path());
        config.checkpoint_threshold = 10;
        config.max_wal_size = 100;
        config.auto_checkpoint_interval = Duration::from_secs(3600);
        config.async_checkpoint_poll_interval = Duration::from_millis(20);
        let coordinator = Arc::new(parking_lot::RwLock::new(
            PersistenceCoordinator::new(config).unwrap(),
        ));
        {
            let wal = coordinator.read().wal_manager().unwrap();
            wal.write()
                .append_entry(
                    linkrs_core::wal::types::WalOpType::InsertVertex,
                    0 as linkrs_core::types::Timestamp,
                    &[],
                )
                .unwrap();
        }
        let pool = Arc::new(StorageThreadPool::new().unwrap());
        let stats = Arc::new(StatsManager::new());
        let executed = Arc::new(AtomicUsize::new(0));
        let exec_cnt = executed.clone();
        let coord_for_exec = coordinator.clone();
        let executor: Arc<
            dyn Fn(PersistenceStateGuard, CheckpointTriggerReason) -> StorageResult<CheckpointStats>
                + Send
                + Sync,
        > = Arc::new(move |_guard, reason| {
            assert_eq!(reason, CheckpointTriggerReason::WalSizeExceeded);
            exec_cnt.fetch_add(1, Ordering::Relaxed);
            // Simulate a real checkpoint advancing the baseline so the next
            // poll sees should_checkpoint() == false.
            coord_for_exec
                .read()
                .mark_checkpointed(linkrs_transaction::wal::Lsn::new(40));
            Ok(CheckpointStats {
                checkpoint_id: 1,
                data_flushed: 100,
                wal_truncated: 10,
                duration: Duration::from_millis(5),
                snapshot_created: false,
                checkpoint_seq: 1,
                data_files_created: 1,
                bytes_flushed: 100,
                wal_files_truncated: 1,
                trigger_reason: reason,
                snapshot_timestamp: 0,
            })
        });
        let mut scheduler = CheckpointScheduler::new(
            coordinator.clone(),
            pool,
            Some(stats.clone()),
            Duration::from_millis(20),
            true,
            executor,
        );
        scheduler.start();
        // Wait for the periodic poll to detect should_checkpoint and execute
        for _ in 0..30 {
            if executed.load(Ordering::Relaxed) > 0 {
                break;
            }
            std::thread::sleep(Duration::from_millis(20));
        }
        // Give the scheduler a brief grace period then shut down; at most one
        // successful checkpoint should have been recorded because the mock
        // executor advances last_checkpoint_lsn.
        std::thread::sleep(Duration::from_millis(40));
        scheduler.stop();
        assert!(
            executed.load(Ordering::Relaxed) >= 1,
            "checkpoint should have been triggered by WAL size"
        );
        assert_eq!(
            stats.get_value(linkrs_metrics::MetricType::CheckpointSuccessCount),
            Some(1)
        );
        assert_eq!(
            stats.get_value(linkrs_metrics::MetricType::CheckpointTriggeredByWalSize),
            Some(1)
        );
    }

    #[test]
    fn test_async_checkpoint_failure_does_not_block_commits() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let config = crate::engine::PersistenceConfig::for_work_dir(temp_dir.path());
        let coordinator = Arc::new(parking_lot::RwLock::new(
            PersistenceCoordinator::new(config).unwrap(),
        ));
        let pool = Arc::new(StorageThreadPool::new().unwrap());
        let stats = Arc::new(StatsManager::new());
        let call_count = Arc::new(AtomicUsize::new(0));
        let cc = call_count.clone();
        let executor: Arc<
            dyn Fn(PersistenceStateGuard, CheckpointTriggerReason) -> StorageResult<CheckpointStats>
                + Send
                + Sync,
        > = Arc::new(move |_g, _| {
            let c = cc.fetch_add(1, Ordering::Relaxed);
            if c == 0 {
                Err(linkrs_core::StorageError::db_error("injected failure"))
            } else {
                Ok(CheckpointStats {
                    checkpoint_id: 1,
                    data_flushed: 10,
                    wal_truncated: 10,
                    duration: Duration::from_millis(1),
                    snapshot_created: false,
                    checkpoint_seq: 1,
                    data_files_created: 1,
                    bytes_flushed: 10,
                    wal_files_truncated: 1,
                    trigger_reason: CheckpointTriggerReason::Explicit,
                    snapshot_timestamp: 0,
                })
            }
        });
        let mut scheduler = CheckpointScheduler::new(
            coordinator.clone(),
            pool,
            Some(stats.clone()),
            Duration::from_millis(20),
            true,
            executor,
        );
        scheduler.request_checkpoint(CheckpointTriggerReason::Explicit);
        scheduler.start();
        std::thread::sleep(Duration::from_millis(80));
        // First execution should have failed
        assert_eq!(
            stats.get_value(linkrs_metrics::MetricType::CheckpointFailureCount),
            Some(1)
        );
        // Request again after failure
        scheduler.request_checkpoint(CheckpointTriggerReason::Explicit);
        std::thread::sleep(Duration::from_millis(80));
        scheduler.stop();
        assert_eq!(
            stats.get_value(linkrs_metrics::MetricType::CheckpointSuccessCount),
            Some(1)
        );
        // Verify dedup didn't block second attempt
        assert_eq!(call_count.load(Ordering::Relaxed), 2);
    }
}