Repository navigation
fix(data_plane): advise view segments behind the newest sealed one cold - #858
Merged
Merged
Conversation
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
force-pushed
the
fix/cold-view-pages
branch
from
November 23, 2026 14:33
82174ba to
2feadb0
Compare
zzylol
marked this pull request as ready for review
November 23, 2026 14:33
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
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.statshowedactive_file4.47 GB andfile_mapped3.19 GB.madvise(MADV_COLD)on the mapping andposix_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.active_filewas 0.37 GB, down from 1.28 GB. Anon memory and answers are unchanged.Review guide
feat/tiered-view-storage. The per-view retention PR is stacked on this one.Segment::mark_coldindata_plane/src/segment.rs(the advice) andGroupFiles::coolindata_plane/src/persist.rs(which segments are marked, and when).GroupFiles::seal_active(it factors out two copies of the existing seal code), the metric plumbing inmetrics.rsandengine.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).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, withclear_refsand 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.preadwhile 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_filelevels 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.rspub 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.rspub 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.rspub 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
Metricsfields are unchanged; the struct has about 40 of them.)Usage:
data_plane --plan plan.json --view-dir views/ ...→ the persist thread callsViewFiles::append(group, (start, end), views)for each refreshed window. When the window starts a new 15-min segment,appendseals the active segment, callscool(), and every sealed segment but the newest getsmark_cold().Written.segments_marked_coldis added toasap_view_segments_marked_cold_total. After 105 min of the run above, the counter read 20 and the cgroup'sactive_fileread 0.37 GB.Algorithm changes
Which segments are cold.
GroupFiles::coolruns on every roll and seal and after indexing. It callsSegment::mark_coldon each sealed segment except the newest.mark_coldis a no-op on an active or already-cold segment.madviseand onefadviseper segment, once.MADV_WILLNEEDreadahead. 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
Decided (2026-11-23)
madvise(MADV_COLD)plusPOSIX_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_COLDalone reaches only mapped pages. The pages read withpreadwhile the segment was active are not mapped, andfadvise(DONTNEED)is the only call that removes them from the active list (5.15 leaves mapped pages alone infadvise).Verification
cargo test -p data_plane --lib--test planner_executor_parity--test view_persistence,view_tiering,view_cold_allocations,view_memorycargo clippy -p data_plane --all-targetsNew 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 throughWritten::segments_marked_cold), that sealing advances the cold point, and that a cold segment reads back the same rows. It fails whencool()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'smemory.stat: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
preadactivates 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.