Skip to content

fix(data_plane): delete each view's windows at its own retention - #859

Merged
zzylol merged 2 commits into
feat/tiered-view-storagefrom
fix/per-view-segment-retention
Nov 23, 2026
Merged

zzylol merged 2 commits into
feat/tiered-view-storagefrom
fix/per-view-segment-retention

Conversation

@zzylol

@zzylol zzylol commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: view segment files are per refresh group, and a segment is deleted only once every view in it is past retention, which means at the group's longest retention. On the live asap-demo run, group 0 holds all the 15 s views: DDSketch for q1 (1 h), Max for q3 (30 min), Count (5 min), two Counter chains (1 h), and Max over queue_depth for q7 (6 h). Each 15-min segment therefore lives for 6 h. Its DDSketch part, about 107 MB of each 172 MB segment, stays on disk and in the page cache 5 h past its 1 h retention, and the view files take 5.4 GB on disk.
  • Change: segments are now per refresh group and retention, under group-<g>/retention-<ms>/. Each window's record is written once per retention, and delete_expired applies each retention to the newest window end.
  • Effect: on the same spare-host run at minute 105 (10k series, 8 users), the view files take 0.87 GB instead of 1.56 GB and stay flat after minute 75, while before they grow linearly toward the 6 h retention. file_mapped is 0.19 GB instead of 0.54 GB.

Review guide

  • Base: feat/tiered-view-storage (rebased after fix(data_plane): advise view segments behind the newest sealed one cold #858 merged).
  • Read first: ViewFiles::append/append_record (how records are split by retention), ViewFiles::delete_expired, and the directory scan in ViewFiles::index, all in data_plane/src/persist.rs.
  • Mechanical: the GroupFiles → RetentionFiles rename, the files map now keyed by (group, retention_ms), the removal of the per-group retention vector from engine.rs, the test helpers in tests/view_tiering.rs and tests/view_persistence.rs that now look one directory deeper, and the docs.

Behavior: before → after

The asap-demo plan: q1 quantile_over_time(0.99, lat[1h]) and q7 max by (job) (max_over_time(queue_depth[6h])) are both answered from 15 s chains in refresh group 0.

before after
files views/group-0/<end>.seg (all views) views/group-0/retention-3675000/<end>.seg (q1's DDSketch, the Counter chains), retention-21675000/ (q7), retention-1875000/ (q3), retention-375000/ (Count)
q1 window, 2 h old on disk and in the page cache until the q7 view expires (6 h) deleted with its segment, at 1 h + margin
view files after 105 min 1.56 GB, growing 0.87 GB, flat since minute 75

Answers are unchanged: the store drops a view's windows at its retention already. Only the file lifetime changes. This serves the MVP disk and memory comparison.

Interfaces

data_plane/src/persist.rs

-/// One refresh group's segments.
-struct GroupFiles {
+/// The segments of one refresh group's views of one retention.
+struct RetentionFiles {
     active: Option<ActiveSegment>,
     sealed: Vec<Arc<Segment>>,
 }
 pub struct ViewFiles {
     dir: PathBuf,
     plan_hash: u64,
     segment_ms: i64,
-    groups: BTreeMap<usize, GroupFiles>,
+    /// Each view's retention in ms; a view not listed is never deleted by time. Set by `index` or `with_view_retention_ms`.
+    view_retention_ms: BTreeMap<PhysicalASAPNodeId, i64>,
+    /// The segments by (refresh group, retention ms).
+    files: BTreeMap<(usize, i64), RetentionFiles>,
     sync_interval: Duration,
     last_sync: Instant,
-    unsynced: BTreeSet<usize>,
+    unsynced: BTreeSet<(usize, i64)>,
 }
 impl ViewFiles {
+    /// Writes and deletes each view's windows by its retention (ms past the newest window); for tests and benches without `index`.
+    pub fn with_view_retention_ms(self, view_retention_ms: BTreeMap<PhysicalASAPNodeId, i64>) -> Self;
-    /// Deletes `group`'s sealed segments whose every record ends at or before `oldest_kept_end_ms`.
-    pub fn delete_expired(&mut self, group: usize, oldest_kept_end_ms: i64) -> std::io::Result<usize>;
+    /// Deletes `group`'s sealed segments whose every record is past its views' retention of `newest_end_ms`.
+    pub fn delete_expired(&mut self, group: usize, newest_end_ms: i64) -> std::io::Result<usize>;
     /// Indexes the files; now also records each view's retention and reads `retention-<ms>/` directories.
     pub fn index(&mut self, groups: &[Vec<(MaterializedView, SchemaRef)>]) -> std::io::Result<ViewIndex>;
 }

On-disk layout: group-<g>/<end>.seg becomes group-<g>/retention-<ms>/<end>.seg. The segment format is unchanged (still version 2). A segment found directly in group-<g>/, written before this PR, is loaded and deleted at the group's longest retention, as before.

Usage: data_plane --plan plan.json --view-dir views/. Engine::spawn calls ViewFiles::index(&precompute.views()), which records each view's retention including the store margin (q1: 3,675,000 ms; q7: 21,675,000 ms). For each refreshed group-0 window, the persist thread calls append(0, (start, end), views), which writes four records, one per retention directory. It then calls delete_expired(0, end), which removes q1's segments whose windows all ended before end − 3,675,000 and keeps q7's for 21,675,000 ms. In the 105-min run this held the view files at 0.87 GB.

Algorithm changes

Segment expiry.

  • Problem: with one segment per group, the group's longest retention decides when every view's bytes are deleted.
  • Before / after: before, append wrote one record per window to group-<g>/, and delete_expired(g, end − max retention of g) deleted whole segments. After, append groups the views by retention_of(view) and append_record writes one record per retention to that retention's active segment, which rolls on its own. delete_expired(g, end) deletes, for each retention r, the sealed segments with max_end_ms ≤ end − r.
  • Correctness: a view's windows are deleted only past that view's own retention plus the margin, which is when the store drops them. A reader still holding a deleted segment keeps reading it (unlinked files stay open), as before. On an unlink error, the rest of the sealed list is now kept; before, it was dropped.
  • Complexity: one record per retention per window instead of one per window. With 4 retentions in group 0, that is 4 small frame headers, and each record carries its own texts and grids, because texts are per segment. Disk per window is about the same; the live text sets are repeated per retention.
  • Limits: views of the same retention still share segments, which is fine because they expire together.

Decisions and open questions

Needs reviewer decision: None.

Decided (2026-11-23): one directory per (group, retention), option (a). Rejected: (b) fallocate(PUNCH_HOLE) inside shared segments, which needs page-aligned per-view extents tracked, keeps apparent file sizes, and makes "a segment is deleted whole" no longer true; (c) compaction, which rewrites live data.

Verification

Scope Result Newly failing Notes
cargo test -p data_plane --lib 49 passed none
--test planner_executor_parity 4 passed none
--test view_persistence / view_tiering / view_cold_allocations / view_memory 7 / 9 / 1 / 1 passed none helpers look in retention-*/
cargo clippy -p data_plane --all-targets no new warnings

New test each_view_expires_at_its_own_retention: one group holds a view with 20 ms retention and a view with 1000 ms retention. After windows up to 80, the short view's three sealed segments are deleted, the long view's segments stay, its oldest window reads back the same rows, and bytes() drops. segments_roll_and_expire_whole is updated to the new delete_expired(group, newest_end_ms).

Added in review: old_layout_segments_load_and_expire_at_the_longest_retention (segments directly in group-<g>/ are indexed, read back, kept past the short view's retention and deleted past the group's longest) and a_failed_unlink_keeps_the_sealed_list (with the retention directory read-only delete_expired fails; after restoring it, the next call deletes all four expired segments). Rebased onto feat/tiered-view-storage after #858: data_plane lib 53, planner_executor_parity 4, view_persistence 7, view_tiering 9, view_cold_allocations 1, view_memory 1, all passed.

Measured on a spare host (same setup as #858: asap-demo, 10k series, 8 users, 12-core cgroup with a 64 GiB limit, window cache off, 110 min). "Before" is the base branch; "after" is this PR, which includes #858. Values are GB:

minute arm view files on disk working set active_file file_mapped anon
60 before 0.90 1.38 0.73 0.13 0.61
60 after 0.71 0.95 0.31 0.07 0.62
90 before 1.34 2.03 1.10 0.41 0.89
90 after 0.86 1.20 0.31 0.19 0.86
105 before 1.56 2.27 1.28 0.54 0.94
105 after 0.87 1.24 0.31 0.19 0.90

memory.current at minute 105: 2.56 GB before, 1.77 GB with #858 alone, 1.49 GB with this PR. Queries: 28,548 answered OK, no errors. p50 / p99 latency after minute 75: 0.086 / 4.09 s (before: 0.186 / 4.22 s). Before this PR the disk keeps growing until 6 h, so the live demo's 5.4 GB of view files should drop to roughly the 1 h views' share plus the small 6 h and 30 min views. That figure is not measured here; the run was 110 min.

Follow-ups

  • The on-disk layout changes. A data plane restarted on an old view directory loads the old segments and ages them out at the group's longest retention.

@zzylol zzylol added bug Something isn't working area: data plane Runtime ingestion, storage, and query-serving data paths labels Nov 23, 2026
@zzylol
zzylol force-pushed the fix/cold-view-pages branch from 82174ba to 2feadb0 Compare November 23, 2026 14:33
@zzylol
zzylol force-pushed the fix/per-view-segment-retention branch from e5a63ca to fd867e7 Compare November 23, 2026 14:33
View segments were per refresh group, and a group's segment was deleted
only once every view in it was past retention: so at the group's longest
retention. On a live run, one group held all the 15 s views: a quantile
sketch view read back 1 h, other views 30 min, 5 min and 1 h, and a max
view read back 6 h. Each 15-minute segment lived for 6 h, and the sketch
view's part, about 107 MB of each 172 MB segment, stayed on disk and in the
page cache 5 h past its 1 h retention (5.4 GB of view files).

Segments are now per refresh group and retention: a group's views of one
retention share a directory group-<g>/retention-<ms>/, and each window's
record is written once per retention (its own texts and grids). A view's
windows are deleted when every window in a segment of its retention is past
that retention of the newest window. Indexing records each view's
retention; delete_expired takes the newest window end and applies each
retention. A segment written before the split, directly in group-<g>/, is
loaded and deleted at the group's longest retention, as before.

Chosen over punching holes (fallocate PUNCH_HOLE) in shared segments,
which keeps the file sizes and needs per-view extents tracked and page-
aligned, and over compaction, which rewrites live data; separate files need
no new on-disk structure and keep "a segment is deleted whole".

Also fixes delete_expired dropping the rest of a group's sealed list when
one unlink failed.

A unit test writes a 20 ms and a 1000 ms view of one group and checks that
the short view's old segments are gone after its retention while the long
view's stay and read back.
…etention; an unlink failure keeps the sealed list

Two unit tests for the per-retention split: segments written directly in
group-<g>/ before it are indexed, read back, and deleted only past the
group's longest retention; and a failed unlink no longer drops the rest of
a retention's sealed list, so a later call deletes them.
@zzylol
zzylol force-pushed the fix/per-view-segment-retention branch from fd867e7 to 510b4ae Compare November 23, 2026 14:34
@zzylol
zzylol changed the base branch from fix/cold-view-pages to feat/tiered-view-storage November 23, 2026 14:34
@zzylol
zzylol marked this pull request as ready for review November 23, 2026 14:34
@zzylol
zzylol merged commit 414fdad into feat/tiered-view-storage Nov 23, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: data plane Runtime ingestion, storage, and query-serving data paths bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant