Skip to content

fix(data_plane): advise view segments behind the newest sealed one cold - #858

Merged
zzylol merged 1 commit into
feat/tiered-view-storagefrom
fix/cold-view-pages
Nov 23, 2026
Merged

zzylol merged 1 commit into
feat/tiered-view-storagefrom
fix/cold-view-pages

Conversation

@zzylol

@zzylol zzylol commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Problem: pages of view segments stay on the active LRU list long after any query reads them. Without memory pressure the kernel never ages them, so they inflate the cgroup working set (memory.current − inactive_file). On a live 10k-series demo run (64 GiB limit, no pressure), the working set was 6.0 GiB against VictoriaMetrics' 4.9 GiB, although ASAP's anon memory was only 1.45 GiB. memory.stat showed active_file 4.47 GB and file_mapped 3.19 GB.
  • Change: once a refresh group has a newer sealed segment, its older sealed segments are advised cold, once each: madvise(MADV_COLD) on the mapping and posix_fadvise(POSIX_FADV_DONTNEED) on the file. This happens on roll, on seal, and after indexing at startup. A new counter, asap_view_segments_marked_cold_total, counts the marked segments.
  • Effect: in a 110-min run of the same workload (10k series, 8 users), the working set at minute 105 was 1.19 GB, down from 2.27 GB, and active_file was 0.37 GB, down from 1.28 GB. Anon memory and answers are unchanged.

Review guide

  • Base: feat/tiered-view-storage. The per-view retention PR is stacked on this one.
  • Read first: Segment::mark_cold in data_plane/src/segment.rs (the advice) and GroupFiles::cool in data_plane/src/persist.rs (which segments are marked, and when).
  • Mechanical: GroupFiles::seal_active (it factors out two copies of the existing seal code), the metric plumbing in metrics.rs and engine.rs, and the module doc.

Behavior: before → after

Setup: the asap-demo workload (quantile_over_time(0.99, lat[1h]), max_over_time(queue_depth[6h]), …, 15 s step over 1 h ranges), 10k series, 8 simulated dashboard users each refreshing every query every 15 s, and the window cache off (--view-cache-max-bytes 0, as in the demo).

  • Before: while a segment is active, head queries read its windows with pread. The first write marks those pages referenced, and the reads activate them. After the segment is sealed, only a range's far edge reads its windows again, through the mapping. The pages stay active anyway. Live demo, with clear_refs and then 90 s: every group-0 segment older than about 70 min still had about 107 MB of 172 MB resident and 0 MB referenced.
  • After: a segment behind the newest sealed one is deactivated: its mapped pages move to the inactive list and stay cached. Its unmapped pages, which were read with pread while it was active and are clean because the segment is synced, are dropped. A far-edge read maps them again. In the spare-host run, active_file levels off at 0.37 GB after minute 45, while the baseline grows linearly to 1.28 GB.

This serves the MVP comparison against VictoriaMetrics: the working set now counts what the data plane reads, not every page it ever read.

Interfaces

data_plane/src/segment.rs

 pub struct Segment {
     path: PathBuf,
     file: File,
     len: AtomicU64,
     sealed: AtomicBool,
+    /// Whether its pages were advised cold (`Segment::mark_cold`); false at creation and open.
+    cold: AtomicBool,
     mapping: OnceLock<Result<Arc<Mapping>, String>>,
     texts: RwLock<Vec<Arc<str>>>,
     dictionary: Mutex<Option<ArrayRef>>,
     max_end_ms: AtomicI64,
 }
 impl Segment {
+    /// Whether its pages were advised cold.
+    pub fn is_cold(&self) -> bool;
+    /// Advises a sealed segment's pages cold, once: MADV_COLD on the mapping (if mapped),
+    /// POSIX_FADV_DONTNEED on the file. Returns whether this call marked it.
+    pub(crate) fn mark_cold(&self) -> bool;
 }
 impl WindowLocation {
+    /// Whether its segment's pages were advised cold.
+    pub fn is_cold(&self) -> bool;
 }

data_plane/src/persist.rs

 pub struct Written {
     pub bytes: u64,
     pub sync_ns: u64,
     pub not_persisted: u64,
     pub locations: Vec<(PhysicalASAPNodeId, WindowLocation)>,
+    /// Segments this append marked cold (on a roll); 0 otherwise.
+    pub segments_marked_cold: u64,
 }

data_plane/src/metrics.rs

 pub struct Metrics {
     ...
+    /// View segments whose pages were advised cold; exported as `asap_view_segments_marked_cold_total`.
+    pub view_segments_marked_cold: AtomicU64,
 }

(The other Metrics fields are unchanged; the struct has about 40 of them.)

Usage: data_plane --plan plan.json --view-dir views/ ... → the persist thread calls ViewFiles::append(group, (start, end), views) for each refreshed window. When the window starts a new 15-min segment, append seals the active segment, calls cool(), and every sealed segment but the newest gets mark_cold(). Written.segments_marked_cold is added to asap_view_segments_marked_cold_total. After 105 min of the run above, the counter read 20 and the cgroup's active_file read 0.37 GB.

Algorithm changes

Which segments are cold.

  • Problem: the kernel deactivates page cache only under memory pressure, so the active list held about 1.1 GB of segment pages that nothing read (baseline, minute 105).
  • Before / after: before, no advice. After, GroupFiles::cool runs on every roll and seal and after indexing. It calls Segment::mark_cold on each sealed segment except the newest. mark_cold is a no-op on an active or already-cold segment.
  • Rule chosen: a segment is cold once it is behind the newest sealed one, not once it is past "the longest reader range". The reason is that a cover of range R reads narrow windows at both edges: the head (every step) and T − R (each window once, as it leaves). The live data shows exactly this: only the newest segments and the one about 60–70 min old were referenced. The windows in between are not read at all. A retention-based rule would mark a segment only just before it is deleted (and, with the per-view retention PR stacked on this one, at the moment of deletion), so it would never reduce the working set.
  • Correctness: both advice calls only move or drop clean page cache. A later read maps the pages again or reads them from disk, so answers cannot change. The unit test checks that a cold segment reads back the same rows, and the parity suite passes.
  • Complexity: O(sealed segments of the group) per roll, which happens once per 15 min. One madvise and one fadvise per segment, once.
  • Limits: dropping the unmapped pages means a far-edge read of a cold segment can be a disk read: about one window's fragment per view per step, with MADV_WILLNEED readahead. The p50 query latency in the run did not go up (see Verification). A segment never mapped has only its unmapped pages dropped; there is nothing to deactivate.

Decisions and open questions

Needs reviewer decision

  • The "cold" point: behind the newest sealed segment (this PR), or a configurable age? With the default 15-min segments, a window becomes cold 15–30 min after it is written.

Decided (2026-11-23)

  • madvise(MADV_COLD) plus POSIX_FADV_DONTNEED, not munmap with re-mapping on demand. Unmapping does not touch the LRU lists: the pages stay cached and active, so the working set would not drop, and re-mapping adds a lock and a remap path to every read. MADV_COLD alone reaches only mapped pages. The pages read with pread while the segment was active are not mapped, and fadvise(DONTNEED) is the only call that removes them from the active list (5.15 leaves mapped pages alone in fadvise).

Verification

Scope Result Newly failing Notes
cargo test -p data_plane --lib 49 passed (with the stacked PR) none includes the new test
--test planner_executor_parity 4 passed none
--test view_persistence, view_tiering, view_cold_allocations, view_memory 7 / 9 / 1 / 1 passed none run on the stacked branch, which contains this commit
cargo clippy -p data_plane --all-targets no new warnings

New test segments_behind_the_newest_sealed_one_are_marked_cold: it checks that only segments behind the newest sealed one are marked, each once (counted through Written::segments_marked_cold), that sealing advances the cold point, and that a cold segment reads back the same rows. It fails when cool() marks nothing.

Measured on a spare 40-thread host (same plan, feeder and query load as the live demo; 10k series, 8 users, data plane in a 12-core cgroup with 64 GiB memory.max, window cache off, 110 min; kernel 6.8 with MGLRU turned off for the run to match the demo hosts' classic LRU). Values are GB from the cgroup's memory.stat:

minute arm working set active_file inactive_file file_mapped anon view files
60 before 1.38 0.73 0.17 0.13 0.61 0.90
60 after 1.04 0.37 0.18 0.12 0.65 0.90
90 before 2.03 1.10 0.24 0.41 0.89 1.34
90 after 1.18 0.37 0.45 0.40 0.79 1.34
105 before 2.27 1.28 0.28 0.54 0.94 1.56
105 after 1.19 0.37 0.58 0.53 0.79 1.56

Queries: 28,508 answered OK before and 28,548 after, with no errors. p50 / p99 latency after minute 75: 0.186 / 4.22 s before, 0.154 / 4.27 s after.

Follow-ups

  • The stacked PR deletes each view's windows at its own retention. That removes the 5 h of a 1 h view's bytes that a 6 h view in the same refresh group keeps alive today.
  • Write path: freshly written segment pages do become active, because the write marks them referenced and the head's pread activates them. They are the hot head, though, and this PR releases them once the segment falls behind. Dropping them right after persist (sync_file_range + fadvise(DONTNEED)) would turn every head read into a disk read, so it is left out.

@zzylol zzylol added bug Something isn't working area: data plane Runtime ingestion, storage, and query-serving data paths labels Nov 23, 2026
The data plane maps every retained view segment. Pages read while a segment
was young (pread while it was the active segment, then the mapping while it
was the newest sealed one) stay on the active LRU list: without memory
pressure the kernel never ages them. On a live run, segments of one refresh
group older than about 70 min each kept about 107 MB of 172 MB resident with
none of it referenced, so the cgroup's working set (memory.current minus
inactive_file) counted gigabytes of page cache no query reads.

Queries read a view's newest windows on every evaluation, and an older
window only once more, when it leaves a range's far edge. So once a newer
sealed segment exists, a refresh group's older sealed segments are marked
cold, once each, on roll, on seal and after indexing at startup:

- madvise(MADV_COLD) over the segment's mapping deactivates the pages it
  maps and keeps them cached;
- posix_fadvise(POSIX_FADV_DONTNEED) over the file drops the pages that are
  cached but not mapped (read with pread while the segment was active),
  which MADV_COLD cannot reach. They are clean: a sealed segment is synced.
  A later read maps or reads them again.

Answers cannot change: the advice only moves or drops clean page cache.

Why advice and not munmap with re-mapping on demand: unmapping does not
touch the LRU lists. The pages stay cached and active, so the working set
would not drop, and re-mapping adds a lock and a remap path to every read.

New counter asap_view_segments_marked_cold_total. A unit test checks that
only segments behind the newest sealed one are marked, each once, and that
a cold segment reads back the same rows.
@zzylol
zzylol force-pushed the fix/cold-view-pages branch from 82174ba to 2feadb0 Compare November 23, 2026 14:33
@zzylol
zzylol marked this pull request as ready for review November 23, 2026 14:33
@zzylol
zzylol merged commit 5f67a45 into feat/tiered-view-storage Nov 23, 2026
1 check 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