use std::sync::Arc;
use std::sync::mpsc::Sender;
use std::time::Duration;
use anyhow::{Result, anyhow};
use chrono::Utc;
use second_brain_core::sqlite_store::{SqliteStore, VacuumSpace};
use crate::health::{Health, MaintenanceState};
use crate::maintenance::DrainRequest;
use crate::write_gate::WriteGate;
pub const SPACE_RETRY: Duration = Duration::from_secs(30 * 60);
const FIRST_RETRY: Duration = Duration::from_secs(30);
const MAX_RETRY: Duration = Duration::from_secs(3600);
pub struct Compaction {
pub store: Arc<SqliteStore>,
pub gate: Arc<WriteGate>,
pub indexes: Option<Sender<DrainRequest>>,
pub health: Arc<Health>,
pub space: fn(&SqliteStore) -> Result<VacuumSpace>,
}
#[derive(Debug, PartialEq, Eq)]
enum Step {
Done,
NoSpace,
IndexesUnavailable,
}
impl Compaction {
pub fn new(
store: Arc<SqliteStore>,
gate: Arc<WriteGate>,
indexes: Option<Sender<DrainRequest>>,
health: Arc<Health>,
) -> Self {
Self {
store,
gate,
indexes,
health,
space: SqliteStore::vacuum_space,
}
}
pub fn run_until_done(&self, mut sleep: impl FnMut(Duration)) {
let mut delay = FIRST_RETRY;
loop {
match self.attempt() {
Ok(Step::Done | Step::IndexesUnavailable) => return,
Ok(Step::NoSpace) => sleep(SPACE_RETRY),
Err(e) => {
tracing::error!(
"store compaction failed, retrying in {}s: {e:#}",
delay.as_secs()
);
self.health
.maintenance_failed(&format!("{e:#}"), Utc::now());
sleep(delay);
delay = (delay * 2).min(MAX_RETRY);
}
}
}
}
fn attempt(&self) -> Result<Step> {
if self.store.stored_redaction_compacted()? {
self.health
.maintenance(MaintenanceState::Done, None, Utc::now());
return Ok(Step::Done);
}
let space = (self.space)(&self.store)?;
if !space.fits() {
tracing::warn!(
"store compaction waits for disk space: needs {} bytes free at {}, has {}",
space.needed_bytes,
space.path.display(),
space.free_bytes
);
self.health
.maintenance(MaintenanceState::WaitingForSpace, Some(space), Utc::now());
return Ok(Step::NoSpace);
}
if let Some(drain) = &self.indexes {
self.health
.maintenance(MaintenanceState::MergingIndex, Some(space), Utc::now());
let (reply, done) = std::sync::mpsc::channel();
drain
.send(DrainRequest::CompactIndexes(reply))
.map_err(|_| anyhow!("the index drain thread is not running"))?;
done.recv()
.map_err(|_| anyhow!("the index drain thread stopped"))??;
}
let (before, after) = vacuum(&self.store, &self.gate, &self.health)?;
if self.indexes.is_none() {
self.health.maintenance_failed(
"the search indexes are unavailable, so their old segments were not merged; \
compaction runs again at the next start",
Utc::now(),
);
return Ok(Step::IndexesUnavailable);
}
self.store.mark_stored_redaction_compacted()?;
self.health
.maintenance(MaintenanceState::Done, None, Utc::now());
tracing::info!("store compacted after redaction: {before} bytes before, {after} after");
Ok(Step::Done)
}
}
pub fn vacuum(store: &SqliteStore, gate: &WriteGate, health: &Health) -> Result<(u64, u64)> {
let _closed = gate.close();
let previous = health.report().maintenance.state;
health.maintenance(MaintenanceState::Vacuuming, None, Utc::now());
let result = store.vacuum_clearing_wal();
health.maintenance(previous, None, Utc::now());
result
}
#[cfg(test)]
mod tests {
use super::*;
fn drain_stub() -> Sender<DrainRequest> {
let (tx, requests) = std::sync::mpsc::channel();
std::thread::spawn(move || {
for request in requests {
if let DrainRequest::CompactIndexes(reply) = request {
let _ = reply.send(Ok(()));
}
}
});
tx
}
fn compaction(dir: &std::path::Path) -> Compaction {
let store = SqliteStore::open(&dir.join("brain.db"), "m".to_string()).unwrap();
Compaction::new(
Arc::new(store),
Arc::new(WriteGate::default()),
Some(drain_stub()),
Arc::new(Health::new(Utc::now(), None)),
)
}
static SPACE_CHECKS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
fn short_of_space_once(store: &SqliteStore) -> Result<VacuumSpace> {
if SPACE_CHECKS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) > 0 {
return store.vacuum_space();
}
Ok(VacuumSpace {
path: std::path::PathBuf::from("/data"),
needed_bytes: 2_000_000,
free_bytes: 1_000_000,
})
}
#[test]
fn a_compaction_runs_once_and_is_recorded() {
let dir = tempfile::tempdir().unwrap();
let c = compaction(dir.path());
c.run_until_done(|_| panic!("nothing to wait for"));
assert!(c.store.stored_redaction_compacted().unwrap());
assert_eq!(c.health.report().maintenance.state, MaintenanceState::Done);
assert!(!c.gate.is_closed());
}
#[test]
fn without_indexes_it_vacuums_but_leaves_the_compaction_unrecorded() {
let dir = tempfile::tempdir().unwrap();
let mut c = compaction(dir.path());
c.indexes = None;
c.run_until_done(|_| panic!("retrying cannot bring the indexes back"));
assert!(!c.store.stored_redaction_compacted().unwrap());
let report = c.health.report();
assert!(report.maintenance.last_error.is_some());
assert!(!c.gate.is_closed());
}
#[test]
fn without_room_it_reports_and_waits_instead_of_failing() {
let dir = tempfile::tempdir().unwrap();
let mut c = compaction(dir.path());
c.space = short_of_space_once;
let health = c.health.clone();
let store = c.store.clone();
let mut waits = Vec::new();
c.run_until_done(|d| {
let report = health.report();
assert_eq!(report.maintenance.state, MaintenanceState::WaitingForSpace);
assert!(
report
.reason
.as_deref()
.is_some_and(|r| r.contains("waits for disk space")),
"{report:?}"
);
assert!(!store.stored_redaction_compacted().unwrap());
waits.push(d);
});
assert_eq!(waits, [SPACE_RETRY]);
assert!(c.store.stored_redaction_compacted().unwrap());
assert_eq!(c.health.report().maintenance.state, MaintenanceState::Done);
assert!(c.health.report().reason.is_none());
}
}