use std::sync::Arc;
use std::time::Duration;
use chrono::Utc;
use second_brain_core::residency;
use second_brain_core::sqlite_store::SqliteStore;
pub const PASS_INTERVAL: Duration = Duration::from_secs(3600);
pub const PAUSE_BETWEEN_SEGMENTS: Duration = Duration::from_millis(200);
pub struct ColdPass {
pub store: Arc<SqliteStore>,
pub interval: Duration,
pub pause: Duration,
}
impl ColdPass {
pub fn spawn(self) {
std::thread::Builder::new()
.name("cold-tier".to_string())
.spawn(move || {
loop {
self.run_once();
std::thread::sleep(self.interval);
}
})
.expect("spawning the cold tier thread");
}
fn run_once(&self) {
match self.store.run_cold_pass(Utc::now(), self.pause) {
Ok(report) => {
if report.sealed_rows > 0
|| report.retired_segments > 0
|| report.rewritten_segments > 0
{
tracing::info!(
"cold tier: {} archived raws moved into {} segments ({} bytes), {} segments retired, {} rewritten without dead records",
report.sealed_rows,
report.sealed_segments,
report.sealed_bytes,
report.retired_segments,
report.rewritten_segments
);
residency::release_free_heap();
}
for segment in &report.unreadable {
tracing::warn!(
"cold segment {} cannot be read: {}",
segment.segment,
segment.error
);
}
}
Err(e) => tracing::warn!("cold tier pass failed: {e:#}"),
}
}
}