use super::*;
pub const KEEP_LABEL: &str = "apoc.keep";
const TERMINAL_LISTING: &str = "('completed', 'failed', 'canceled')";
const ACTIVE_LISTING: &str = "('queued', 'running', 'stalled', 'interrupted')";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RetentionDeleted {
pub executions: usize,
pub events: usize,
pub payloads: usize,
pub process_events: usize,
pub invocations: usize,
pub observations: usize,
pub leases: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExpiredLifetime {
pub id: String,
pub touched_at: u64,
pub lifetime_ms: u64,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SpaceReclaim {
pub auto_vacuum: String,
pub freelist_pages: u64,
pub reclaimed_pages: u64,
}
pub(super) fn migrate(connection: &Connection) -> Result<(), String> {
connection
.execute_batch(
"CREATE TABLE IF NOT EXISTS execution_lifetimes (
id TEXT PRIMARY KEY,
lifetime_ms INTEGER,
touched_at INTEGER NOT NULL
);",
)
.map_err(|error| error.to_string())
}
fn keep_check(alias: &str) -> String {
format!("COALESCE(json_extract({alias}.value, '$.spec.labels.\"{KEEP_LABEL}\"') = 'true', 0)")
}
fn retention_execution_sql() -> String {
let kept_self = keep_check("e");
let kept_parent = keep_check("p");
let kept_child = keep_check("ce");
format!(
"SELECT l.id FROM execution_listing l
JOIN execution_records e ON e.id = l.id
WHERE l.updated_at < ?1 AND l.status IN {TERMINAL_LISTING}
AND NOT ({kept_self})
AND NOT EXISTS (
SELECT 1 FROM execution_records p
WHERE p.id = e.parent_id AND {kept_parent}
)
AND NOT EXISTS (
SELECT 1 FROM execution_listing pl
WHERE pl.id = e.parent_id
AND (pl.status NOT IN {TERMINAL_LISTING} OR pl.updated_at >= ?1)
)
AND NOT EXISTS (
SELECT 1 FROM execution_listing c
JOIN execution_records ce ON ce.id = c.id
WHERE c.parent_id = l.id
AND (c.status NOT IN {TERMINAL_LISTING} OR c.updated_at >= ?1 OR {kept_child})
)
ORDER BY l.updated_at ASC, l.id ASC
LIMIT ?2"
)
}
const RETENTION_INVOCATION_SQL: &str = "SELECT id FROM invocations
WHERE updated_at < ?1 AND status != 'running' AND recording_id IS NULL
AND (execution_id IS NULL OR NOT EXISTS (
SELECT 1 FROM execution_records r WHERE r.id = invocations.execution_id
))
ORDER BY updated_at ASC, id ASC
LIMIT ?2";
const RETENTION_PROCESS_EVENT_SQL: &str = "SELECT id FROM process_events
WHERE at < ?1 AND execution_id IS NULL AND run_id IS NULL
ORDER BY at ASC
LIMIT ?2";
fn ids(
connection: &Connection,
sql: &str,
cutoff: u64,
limit: usize,
) -> Result<Vec<String>, String> {
let mut statement = connection.prepare(sql).map_err(|error| error.to_string())?;
statement
.query_map(
params![as_i64(cutoff), limit.min(i64::MAX as usize) as i64],
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())
}
fn capped_count(
connection: &Connection,
sql: &str,
cutoff: u64,
cap: usize,
) -> Result<(usize, bool), String> {
let count = connection
.query_row(
&format!("SELECT COUNT(*) FROM ({sql})"),
params![
as_i64(cutoff),
cap.saturating_add(1).min(i64::MAX as usize) as i64
],
|row| row.get::<_, i64>(0),
)
.map_err(|error| error.to_string())?
.max(0) as usize;
Ok((count.min(cap), count > cap))
}
fn delete_invocation(transaction: &rusqlite::Transaction<'_>, id: &str) -> Result<usize, String> {
transaction
.execute("DELETE FROM invocations WHERE id = ?1", [id])
.map_err(|error| error.to_string())?;
transaction
.execute(
"DELETE FROM observation_versions WHERE id = ?1 AND deleted = 0",
[format!("invocation:{id}")],
)
.map_err(|error| error.to_string())
}
impl Store {
pub fn register_execution_lifetime(
&self,
id: &str,
lifetime_ms: Option<u64>,
at: u64,
) -> Result<(), String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT OR IGNORE INTO execution_lifetimes(id, lifetime_ms, touched_at)
VALUES (?1, ?2, ?3)",
params![id, lifetime_ms.map(as_i64), as_i64(at)],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn touch_execution(&self, id: &str, at: u64) -> Result<bool, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let tracked = connection
.query_row(
"SELECT EXISTS(SELECT 1 FROM execution_lifetimes WHERE id = ?1)",
[id],
|row| row.get::<_, bool>(0),
)
.map_err(|error| error.to_string())?;
if !tracked {
return Ok(false);
}
connection
.execute(
"UPDATE execution_lifetimes SET touched_at = MAX(touched_at, ?2) WHERE id = ?1",
params![id, as_i64(at)],
)
.map_err(|error| error.to_string())?;
Ok(true)
}
pub fn execution_lifetime(&self, id: &str) -> Result<Option<(Option<u64>, u64)>, String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT lifetime_ms, touched_at FROM execution_lifetimes WHERE id = ?1",
[id],
|row| {
Ok((
row.get::<_, Option<i64>>(0)?
.map(|value| value.max(0) as u64),
row.get::<_, i64>(1)?.max(0) as u64,
))
},
)
.optional()
.map_err(|error| error.to_string())
}
pub fn running_code_execution_count(&self) -> Result<usize, String> {
let count = self
.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT COUNT(*) FROM executions WHERE json_extract(value, '$.status') = 'running'",
[],
|row| row.get::<_, i64>(0),
)
.map_err(|error| error.to_string())?;
Ok(count.max(0) as usize)
}
pub fn expired_lifetimes(
&self,
now: u64,
limit: usize,
cap: usize,
) -> Result<(Vec<ExpiredLifetime>, usize, bool), String> {
let sql = format!(
"SELECT t.id, t.touched_at, t.lifetime_ms FROM execution_lifetimes t
JOIN execution_listing l ON l.id = t.id
WHERE t.lifetime_ms IS NOT NULL AND t.touched_at + t.lifetime_ms <= ?1
AND l.status IN {ACTIVE_LISTING}
ORDER BY t.touched_at ASC, t.id ASC
LIMIT ?2"
);
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let expired = {
let mut statement = connection
.prepare(&sql)
.map_err(|error| error.to_string())?;
statement
.query_map(params![as_i64(now), limit as i64], |row| {
Ok(ExpiredLifetime {
id: row.get(0)?,
touched_at: row.get::<_, i64>(1)?.max(0) as u64,
lifetime_ms: row.get::<_, i64>(2)?.max(0) as u64,
})
})
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
let (count, capped) = capped_count(&connection, &sql, now, cap)?;
Ok((expired, count, capped))
}
pub fn retention_execution_candidates(
&self,
cutoff: u64,
limit: usize,
) -> Result<Vec<String>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
ids(&connection, &retention_execution_sql(), cutoff, limit)
}
pub fn count_retention_executions(
&self,
cutoff: u64,
cap: usize,
) -> Result<(usize, bool), String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
capped_count(&connection, &retention_execution_sql(), cutoff, cap)
}
pub fn retention_invocation_candidates(
&self,
cutoff: u64,
limit: usize,
) -> Result<Vec<String>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
ids(&connection, RETENTION_INVOCATION_SQL, cutoff, limit)
}
pub fn count_retention_invocations(
&self,
cutoff: u64,
cap: usize,
) -> Result<(usize, bool), String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
capped_count(&connection, RETENTION_INVOCATION_SQL, cutoff, cap)
}
pub fn retention_process_event_candidates(
&self,
cutoff: u64,
limit: usize,
) -> Result<Vec<String>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
ids(&connection, RETENTION_PROCESS_EVENT_SQL, cutoff, limit)
}
pub fn count_retention_process_events(
&self,
cutoff: u64,
cap: usize,
) -> Result<(usize, bool), String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
capped_count(&connection, RETENTION_PROCESS_EVENT_SQL, cutoff, cap)
}
pub fn delete_execution_history(&self, ids: &[String]) -> Result<RetentionDeleted, String> {
let mut deleted = RetentionDeleted::default();
if ids.is_empty() {
return Ok(deleted);
}
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
for id in ids {
let status = transaction
.query_row(
"SELECT status FROM execution_listing WHERE id = ?1",
[id],
|row| row.get::<_, Option<String>>(0),
)
.optional()
.map_err(|error| error.to_string())?
.flatten();
if !matches!(status.as_deref(), Some("completed" | "failed" | "canceled")) {
continue;
}
let lease = format!("execution/{id}");
let payloads = {
let mut statement = transaction
.prepare(
"SELECT DISTINCT payload_id FROM causal_events
WHERE execution_id = ?1 AND payload_id IS NOT NULL",
)
.map_err(|error| error.to_string())?;
statement
.query_map([id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
for payload in &payloads {
deleted.payloads += transaction
.execute(
"DELETE FROM causal_payloads WHERE id = ?1 AND pinned = 0",
[payload],
)
.map_err(|error| error.to_string())?;
}
deleted.events += transaction
.execute("DELETE FROM causal_events WHERE execution_id = ?1", [id])
.map_err(|error| error.to_string())?;
deleted.process_events += transaction
.execute(
"DELETE FROM process_events WHERE execution_id = ?1 OR run_id = ?1",
[id],
)
.map_err(|error| error.to_string())?;
let invocations = {
let mut statement = transaction
.prepare(
"SELECT id FROM invocations
WHERE execution_id = ?1 AND recording_id IS NULL",
)
.map_err(|error| error.to_string())?;
statement
.query_map([id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
for invocation in &invocations {
deleted.observations += delete_invocation(&transaction, invocation)?;
}
deleted.invocations += invocations.len();
transaction
.execute("DELETE FROM reservations WHERE lease_id = ?1", [&lease])
.map_err(|error| error.to_string())?;
deleted.leases += transaction
.execute("DELETE FROM leases WHERE id = ?1", [&lease])
.map_err(|error| error.to_string())?;
for sql in [
"DELETE FROM execution_lifetimes WHERE id = ?1",
"DELETE FROM artifacts WHERE execution_id = ?1",
"DELETE FROM executions WHERE id = ?1",
"DELETE FROM runs WHERE id = ?1",
"DELETE FROM execution_idempotency WHERE execution_id = ?1",
"DELETE FROM poll_iterations WHERE poll_id = ?1",
"DELETE FROM telemetry_samples WHERE capture_id IN (
SELECT id FROM telemetry_captures WHERE execution_id = ?1
)",
"DELETE FROM telemetry_captures WHERE execution_id = ?1",
] {
transaction
.execute(sql, [id])
.map_err(|error| error.to_string())?;
}
deleted.executions += transaction
.execute("DELETE FROM execution_records WHERE id = ?1", [id])
.map_err(|error| error.to_string())?;
}
transaction.commit().map_err(|error| error.to_string())?;
Ok(deleted)
}
pub fn delete_invocations(&self, ids: &[String]) -> Result<usize, String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let mut deleted = 0;
for id in ids {
let terminal = transaction
.query_row(
"SELECT status != 'running' FROM invocations WHERE id = ?1",
[id],
|row| row.get::<_, bool>(0),
)
.optional()
.map_err(|error| error.to_string())?
.unwrap_or(false);
if terminal {
delete_invocation(&transaction, id)?;
deleted += 1;
}
}
transaction.commit().map_err(|error| error.to_string())?;
Ok(deleted)
}
pub fn delete_process_events(&self, ids: &[String]) -> Result<usize, String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let mut deleted = 0;
for id in ids {
deleted += transaction
.execute("DELETE FROM process_events WHERE id = ?1", [id])
.map_err(|error| error.to_string())?;
}
transaction.commit().map_err(|error| error.to_string())?;
Ok(deleted)
}
pub fn reclaim_space(&self, max_pages: u64) -> Result<SpaceReclaim, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let pragma = |name: &str| {
connection
.pragma_query_value(None, name, |row| row.get::<_, i64>(0))
.map(|value| value.max(0) as u64)
.map_err(|error| error.to_string())
};
let mode = pragma("auto_vacuum")?;
let before = pragma("freelist_count")?;
if mode == 2 && before > 0 && max_pages > 0 {
connection
.execute_batch(&format!("PRAGMA incremental_vacuum({max_pages});"))
.map_err(|error| error.to_string())?;
}
let after = pragma("freelist_count")?;
Ok(SpaceReclaim {
auto_vacuum: match mode {
1 => "full",
2 => "incremental",
_ => "none",
}
.to_string(),
freelist_pages: after,
reclaimed_pages: before.saturating_sub(after),
})
}
}