Skip to main content

second_brain_api/
cold_pass.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use chrono::Utc;
5use second_brain_core::residency;
6use second_brain_core::sqlite_store::SqliteStore;
7
8pub const PASS_INTERVAL: Duration = Duration::from_secs(3600);
9/// Between segments, so a long first pass never holds other writers off.
10pub const PAUSE_BETWEEN_SEGMENTS: Duration = Duration::from_millis(200);
11
12/// Moves old archived raws into the store's cold tier, which must already be
13/// attached, once at start and then every `interval`.
14pub struct ColdPass {
15    pub store: Arc<SqliteStore>,
16    pub interval: Duration,
17    pub pause: Duration,
18}
19
20impl ColdPass {
21    pub fn spawn(self) {
22        std::thread::Builder::new()
23            .name("cold-tier".to_string())
24            .spawn(move || {
25                loop {
26                    self.run_once();
27                    std::thread::sleep(self.interval);
28                }
29            })
30            .expect("spawning the cold tier thread");
31    }
32
33    fn run_once(&self) {
34        match self.store.run_cold_pass(Utc::now(), self.pause) {
35            Ok(report) => {
36                if report.sealed_rows > 0
37                    || report.retired_segments > 0
38                    || report.rewritten_segments > 0
39                {
40                    tracing::info!(
41                        "cold tier: {} archived raws moved into {} segments ({} bytes), {} segments retired, {} rewritten without dead records",
42                        report.sealed_rows,
43                        report.sealed_segments,
44                        report.sealed_bytes,
45                        report.retired_segments,
46                        report.rewritten_segments
47                    );
48                    residency::release_free_heap();
49                }
50                for segment in &report.unreadable {
51                    tracing::warn!(
52                        "cold segment {} cannot be read: {}",
53                        segment.segment,
54                        segment.error
55                    );
56                }
57            }
58            Err(e) => tracing::warn!("cold tier pass failed: {e:#}"),
59        }
60    }
61}