second_brain_api/
log_floor.rs1use std::sync::Arc;
2use std::time::Duration;
3
4use anyhow::Result;
5use chrono::{DateTime, Utc};
6use second_brain_core::sqlite_store::{FLOOR_TICK_MICROS, FloorPass, SqliteStore, Truncation};
7use tokio::task::JoinHandle;
8use tokio::time::MissedTickBehavior;
9
10use crate::health::Health;
11use crate::write_gate::WriteGate;
12
13pub const TRUNCATION: Truncation = Truncation::On;
14
15pub fn spawn(
18 sqlite: Arc<SqliteStore>,
19 gate: Arc<WriteGate>,
20 health: Arc<Health>,
21) -> JoinHandle<()> {
22 tokio::spawn(async move {
23 let mut interval =
24 tokio::time::interval(Duration::from_micros(FLOOR_TICK_MICROS.unsigned_abs()));
25 interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
26 loop {
27 interval.tick().await;
28 let (sqlite, gate, health) = (sqlite.clone(), gate.clone(), health.clone());
29 let ticked = tokio::task::spawn_blocking(move || {
30 let now = Utc::now();
31 let pass = tick(&sqlite, &gate, now.timestamp_micros())?;
32 report_floor(&sqlite, &health, now)?;
33 Ok::<_, anyhow::Error>(pass)
34 })
35 .await;
36 match ticked {
37 Ok(Ok(Some(pass))) => tracing::info!(
38 floor = pass.floor,
39 dropped = pass.dropped,
40 log_floor = pass.log_floor,
41 "log floor pass finished"
42 ),
43 Ok(Ok(None)) => {}
44 Ok(Err(e)) => tracing::warn!("log floor tick failed: {e:#}"),
45 Err(e) => tracing::warn!("log floor tick panicked: {e}"),
46 }
47 }
48 })
49}
50
51fn report_floor(sqlite: &SqliteStore, health: &Health, now: DateTime<Utc>) -> Result<()> {
52 let floor = sqlite.floor_health(now.timestamp_micros())?;
53 let failed = sqlite.failed_record_counts()?;
54 health.floor_checked(floor, failed, now);
55 Ok(())
56}
57
58pub fn tick(sqlite: &SqliteStore, gate: &WriteGate, now_micros: i64) -> Result<Option<FloorPass>> {
61 sqlite.advance_floor_uptime(now_micros)?;
62 if !sqlite.floor_pass_due(now_micros)? {
63 return Ok(None);
64 }
65 let Some(_slot) = gate.try_enter() else {
66 return Ok(None);
67 };
68 sqlite.run_floor_pass(TRUNCATION, now_micros).map(Some)
69}
70
71#[cfg(test)]
72mod tests {
73 use second_brain_core::schema::{Memory, MemoryType};
74 use second_brain_core::sqlite_store::SqliteStore;
75 use second_brain_core::store::{Store, StoreAdmin};
76
77 use super::tick;
78 use crate::write_gate::WriteGate;
79
80 const NOW: i64 = 1_790_000_000_000_000;
81 const DAY: i64 = 86_400_000_000;
82
83 fn store_with_log() -> SqliteStore {
84 let store =
85 SqliteStore::in_memory("00000000-0000-4000-8000-000000000000".to_string()).unwrap();
86 for _ in 0..3 {
87 store
88 .store_memory(&Memory::new(
89 "floor tick".to_string(),
90 MemoryType::Semantic,
91 "test".to_string(),
92 String::new(),
93 ))
94 .unwrap();
95 }
96 store
97 }
98
99 #[test]
100 fn the_daily_pass_frees_log_rows_up_to_the_floor() {
101 let store = store_with_log();
102 let gate = WriteGate::default();
103 let head = store.log_head().unwrap();
104 let before = store.sync_log_since(0).unwrap().len();
105
106 let pass = tick(&store, &gate, NOW)
107 .unwrap()
108 .expect("the first tick runs the pass");
109
110 assert_eq!(pass.floor, head, "no neighbour: F is the head");
111 assert_eq!(pass.dropped as usize, before);
112 assert_eq!(pass.log_floor, head);
113 assert_eq!(store.log_floor().unwrap(), head);
114 assert!(store.sync_log_since(0).unwrap().is_empty());
115
116 store
117 .store_memory(&Memory::new(
118 "after the pass".to_string(),
119 MemoryType::Semantic,
120 "test".to_string(),
121 String::new(),
122 ))
123 .unwrap();
124 assert!(!store.sync_log_since(head).unwrap().is_empty());
125 }
126
127 #[test]
128 fn the_pass_runs_once_a_day() {
129 let store = store_with_log();
130 let gate = WriteGate::default();
131
132 assert!(tick(&store, &gate, NOW).unwrap().is_some());
133 assert!(tick(&store, &gate, NOW + DAY / 2).unwrap().is_none());
134 assert!(tick(&store, &gate, NOW + DAY).unwrap().is_some());
135 }
136
137 #[test]
138 fn store_maintenance_defers_the_pass_to_a_later_tick() {
139 let store = store_with_log();
140 let gate = WriteGate::default();
141 let closed = gate.close();
142
143 assert!(tick(&store, &gate, NOW).unwrap().is_none());
144 drop(closed);
145 assert!(tick(&store, &gate, NOW + 1).unwrap().is_some());
146 }
147
148 #[test]
149 fn each_tick_reports_the_floor_to_health() {
150 let store = store_with_log();
151 let gate = WriteGate::default();
152 let health = crate::health::Health::new(chrono::Utc::now(), None);
153 assert!(health.report().sync.floor.is_none());
154
155 let pass = tick(&store, &gate, NOW).unwrap().unwrap();
156 super::report_floor(&store, &health, chrono::Utc::now()).unwrap();
157
158 let floor = health.report().sync.floor.expect("the floor is reported");
159 assert_eq!(floor.log_floor, pass.log_floor);
160 assert_eq!(floor.floor, pass.floor);
161 }
162}