Skip to main content

reddb_server/runtime/
impl_materialized_view.rs

1//! Runtime materialized-view refresh and retention sweep.
2//!
3//! Extracted verbatim from `impl_core.rs` (impl_core slice 6/10, issue #1627).
4//! Houses the background-tick surface for scheduled view refresh and retention
5//! sweeps:
6//!
7//! - `materialized_view_metadata` — snapshot of every registered view's
8//!   runtime state (feeds `red.materialized_views`).
9//! - `retention_sweeper_snapshot` — snapshot of every sweeper's state
10//!   (feeds `red.retention`).
11//! - `sweep_retention_tick` — one tick of the retention sweeper.
12//! - `refresh_due_materialized_views` — claim and refresh views due for
13//!   a scheduled refresh.
14use super::*;
15
16impl RedDBRuntime {
17    /// Snapshot of every registered materialized view's runtime
18    /// state — feeds the `red.materialized_views` virtual table.
19    /// Issue #583 slice 10.
20    pub fn materialized_view_metadata(
21        &self,
22    ) -> Vec<crate::storage::cache::result::MaterializedViewMetadata> {
23        // Issue #595 slice 9c — `current_row_count` is now scraped
24        // live from the backing collection rather than read from the
25        // cache slot. Mirrors the slice-10 invariant on
26        // `queue_pending_gauge` in #527: the live store is the source
27        // of truth, the cache slot only carries last-refresh telemetry
28        // (timing, error, refresh cadence).
29        let store = self.inner.db.store();
30        let mut entries = self.inner.materialized_views.read().metadata();
31        for entry in &mut entries {
32            if let Some(manager) = store.get_collection(&entry.name) {
33                entry.current_row_count = manager.count() as u64;
34            }
35        }
36        entries
37    }
38
39    /// Drive scheduled refreshes for materialized views with a
40    /// `REFRESH EVERY <duration>` clause. Called from the background
41    /// scheduler thread (and from unit tests with a fake clock via
42    /// `claim_due_at`). Each invocation atomically claims the set of
43    /// due views (so two concurrent ticks never double-fire the same
44    /// view) and runs each refresh through the standard execution
45    /// path — failures are captured in `last_error` and the prior
46    /// content stays intact. Issue #583 slice 10.
47    /// Snapshot of every tracked retention sweeper state — feeds the
48    /// three extra columns on `red.retention`. Issue #584 slice 12.
49    pub(crate) fn retention_sweeper_snapshot(
50        &self,
51    ) -> Vec<(String, crate::runtime::retention_sweeper::SweeperState)> {
52        self.inner.retention_sweeper.read().snapshot()
53    }
54
55    /// Drive one tick of the retention sweeper. Iterates collections
56    /// with a retention policy set, physically deletes at most
57    /// `batch_size` expired rows per collection, and records the
58    /// `last_sweep_at_ms` / `rows_swept_total` / pending estimate that
59    /// `red.retention` exposes. Called from the background sweeper
60    /// thread; safe to invoke directly from tests with a small batch
61    /// size to drain rows deterministically. Issue #584 slice 12.
62    ///
63    /// Deletes are issued as `DELETE FROM <collection> WHERE
64    /// <ts_column> < <cutoff>` through the standard `execute_query`
65    /// chokepoint so WAL participation and snapshot guards apply
66    /// exactly as for a user-issued DELETE — replicas replay the
67    /// sweeper's deletes via the same WAL stream with no special
68    /// handling on the replication side.
69    ///
70    /// Batching is enforced by tightening the cutoff: if more than
71    /// `batch_size` rows are expired, the cutoff is dropped to the
72    /// `batch_size`-th oldest expired timestamp + 1 so the predicate
73    /// matches roughly `batch_size` rows; the remainder is reported
74    /// as `current_rows_pending_sweep_estimate` and drained on the
75    /// next tick.
76    pub fn sweep_retention_tick(&self, batch_size: usize) {
77        if batch_size == 0 {
78            return;
79        }
80        let now_ms = std::time::SystemTime::now()
81            .duration_since(std::time::UNIX_EPOCH)
82            .map(|d| d.as_millis() as u64)
83            .unwrap_or(0);
84
85        let store = self.inner.db.store();
86        let collections = store.list_collections();
87        for name in collections {
88            let Some(contract) = self.inner.db.collection_contract(&name) else {
89                continue;
90            };
91            let Some(retention_ms) = contract.retention_duration_ms else {
92                continue;
93            };
94            let Some(ts_column) =
95                crate::runtime::retention_filter::resolve_timestamp_column(&contract)
96            else {
97                continue;
98            };
99            let Some(manager) = store.get_collection(&name) else {
100                continue;
101            };
102            let cutoff = (now_ms as i64).saturating_sub(retention_ms as i64);
103
104            // Single pass: collect expired timestamps. We keep the
105            // full Vec rather than a bounded heap because the partial
106            // sort below is the simplest correct way to find the
107            // batch-th oldest; for the slice's "1000-row default
108            // batch" target this is bounded enough for production
109            // operation, and the alternative (in-place heap of size
110            // batch+1) is a follow-up optimisation.
111            let mut expired_ts: Vec<i64> = Vec::new();
112            manager.for_each_entity(|entity| {
113                let ts = match ts_column.as_str() {
114                    "created_at" => Some(entity.created_at as i64),
115                    "updated_at" => Some(entity.updated_at as i64),
116                    other => entity
117                        .data
118                        .as_row()
119                        .and_then(|row| row.get_field(other))
120                        .and_then(|v| match v {
121                            crate::storage::schema::Value::TimestampMs(t) => Some(*t),
122                            crate::storage::schema::Value::Timestamp(t) => {
123                                Some(t.saturating_mul(1_000))
124                            }
125                            crate::storage::schema::Value::BigInt(t) => Some(*t),
126                            crate::storage::schema::Value::UnsignedInteger(t) => {
127                                i64::try_from(*t).ok()
128                            }
129                            crate::storage::schema::Value::Integer(t) => Some(*t),
130                            _ => None,
131                        }),
132                };
133                if let Some(t) = ts {
134                    if t < cutoff {
135                        expired_ts.push(t);
136                    }
137                }
138                true
139            });
140
141            let total_expired = expired_ts.len() as u64;
142            if total_expired == 0 {
143                self.inner
144                    .retention_sweeper
145                    .write()
146                    .record_tick(&name, 0, 0, now_ms);
147                continue;
148            }
149
150            let (effective_cutoff, pending) = if (total_expired as usize) <= batch_size {
151                (cutoff, 0u64)
152            } else {
153                // Tighten the cutoff to the (batch_size)-th oldest
154                // expired timestamp + 1 so DELETE matches roughly
155                // `batch_size` rows.
156                expired_ts.sort_unstable();
157                let nth = expired_ts[batch_size - 1];
158                (
159                    nth.saturating_add(1),
160                    total_expired.saturating_sub(batch_size as u64),
161                )
162            };
163
164            let stmt = format!(
165                "DELETE FROM {} WHERE {} < {}",
166                name, ts_column, effective_cutoff
167            );
168            let deleted = match self.execute_query(&stmt) {
169                Ok(r) => r.affected_rows,
170                Err(_) => 0,
171            };
172
173            self.inner
174                .retention_sweeper
175                .write()
176                .record_tick(&name, deleted, pending, now_ms);
177        }
178    }
179
180    pub fn refresh_due_materialized_views(&self) {
181        let due = {
182            let mut cache = self.inner.materialized_views.write();
183            cache.claim_due_at(std::time::Instant::now())
184        };
185        for name in due {
186            // Round-trip through `execute_query` (rather than the
187            // prepared-statement `execute_query_expr` fast path, which
188            // explicitly rejects DDL/maintenance statements). Failures
189            // are captured inside the RefreshMaterializedView handler
190            // via `record_refresh_failure`; the scheduler ignores the
191            // Result so one bad view doesn't halt the loop.
192            let stmt = format!("REFRESH MATERIALIZED VIEW {}", name);
193            let _ = self.execute_query(&stmt);
194        }
195    }
196}