Skip to content

feat: execute ASAPPlanner main's InstallablePlan through asap-executor - #852

Merged
zzylol merged 72 commits into
mainfrom
sync/planner-main-1760a69f
Nov 23, 2026
Merged

zzylol merged 72 commits into
mainfrom
sync/planner-main-1760a69f

Conversation

@zzylol

@zzylol zzylol commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Design: docs/design/data-plane-demo.md (the data plane for the comparison with VictoriaMetrics: threads, columnar panes, shared query-time results, range queries, view persistence, metrics, evaluation design).

Supersedes #845. #845 bumps the ASAPPlanner pin only and does not compile: the Planner's plan file, executor API and planning inputs changed under it (66 compile errors). This PR pins ASAPPlanner main 846a70a and runs the plan through the Planner's executor.

What changes for a user

  • control_plane writes plan.json (wire version 15) and, with --report report.json, Stage 3's SelectedPlanReport (cost, predicted usage per node and per query). --plan-selection-settings is replaced by --cost-inputs. --measured-operation-costs and --ignore-accuracy-guarantees are kept.
  • data_plane keeps the raw samples it receives by Remote Write. Planned queries no longer read raw samples from Prometheus. New flags: --run-memory-limit-bytes and --view-retention-margin-ms.
  • /api/v1/status/asap reports ingestion counters (samples, and windows refreshed, failed or skipped) in place of precompute and query_run.
  • The backend declares query_time_results_retained: false, so the Planner selects no view refreshed on first read. The data plane does not refresh such views yet.
  • Planned range queries are answered (/api/v1/query_range), one evaluation per step.
  • Views persist across restarts (--view-dir, default asap-views; --no-view-persistence): one file per refresh group, one record per window, synced every --view-sync-interval-ms (default 5 s) on --persist-threads threads, tied to the plan by a hash; sketch states as asap_sketchlib's ASAPv1 bytes.
  • GET /metrics exports a per-phase CPU breakdown and the data plane's counters in the Prometheus text format.
  • New flags: --query-threads, --precompute-threads, --max-precompute-backlog, --no-shared-results, --query-time-results-retention-ms, --query-time-results-max-bytes, --view-dir, --no-view-persistence, --result-cache-bytes, --no-result-cache, --ingest-threads, --persist-threads, --max-persist-backlog-bytes, --view-sync-interval-ms.
  • A Grafana range panel refreshed every 15 s computes only its new steps: final answers are kept in a result cache (below).

How the data plane runs a plan

Step Planner API used
Read and check the plan InstallablePlan::from_json, validate, materialized_views, ingestion_refresh_groups
Store one DataStore: raw samples per scanned metric as (ts, value, $promql_series_identity, labels...), columnar by pane; view rows in a copy-on-write view store with the executor's tiling read
Ingestion per refresh group (W, phase p), one execute_ingestion run per window (e - W, e] once the watermark reaches e; rows appended with the range they cover
Query one compiled query run per root (compile with the views it reads, Scope::Query), rows turned into a PromQL vector; a query-time OneEvaluation node several queries read is compiled on its own and bound as a view slot

The plan's retention counts back from the newest evaluation, but ingestion refreshes windows ahead of the queries it answers. Views and query-time raw samples are therefore kept --view-retention-margin-ms (default 60 s) past their retention. The end-to-end run found this: without the margin, a query at a time 20 s behind ingestion found its oldest windows already dropped.

Threads (before → after)

Before: one engine thread ran Remote Write appends, every closed window's ingestion run and every query in turn. While a window's views were computed, Remote Write queued (503 when full) and queries waited.

After:

Thread Does Publishes
HTTP (tokio) decodes Remote Write, interns series —
ingest-0..N (--ingest-threads, default a quarter of the cores) route and append the samples of the series hashed to each shard, into the shard's own raw data the shard's raw data, in a report after every batch
ingest (coordinator) keeps the watermark over the shards behind a flush, closes windows, has the shards evict panes raw data snapshot (every shard's) after every pass
precompute (committer), precompute-0..N (--precompute-threads, default a quarter of the cores, 1 to 4) and precompute-queue one execute_ingestion run per closed window on a worker, or one per worker over a share of the series; the committer merges the parts and stores each group's windows in window order view snapshot after every pass
persist-0..N (--persist-threads, a quarter of the cores, 1 to 4) encode, write and sync view files —
query-0..N (--query-threads, default one per core) one query run to completion each, over the newest snapshots —

A run's output stream is not Send, so a run never moves between threads; what crosses threads (QueryEngine, RunStore, RawData, ViewStore, Precompute) is checked Send/Sync at compile time. At most --max-precompute-backlog (256) windows wait for precompute; when full, the coordinator waits, the shards' queues fill and Remote Write gets 503 (see below). A query does not wait for queued writes; the event-time barrier does (it answers once its windows are refreshed).

Ingest shards: correctness and backpressure (6d40b3d..8682b64)

With several ingest shards, parity lost 16–20 answers per run (sums and top-k low): windows closed before every shard had appended its samples. The rule is now: the watermark moves only behind a flush, and every pass that took a report or a barrier sends one. A flush means every shard has run what was admitted before it.

  • Reports after a flush are held. A shard that has answered a flush may already run later requests. Its reports are held for the next pass, so they move neither the watermark nor the raw data its windows read. A request reaches its shards whole between two flushes (an admission lock), never split by one.
  • The clock cap is the time the flush was sent. Samples may be stamped ahead of the wall clock, as the feeder does, and the watermark is then capped at the clock. The cap used to come from each shard's report time: a shard working through a backlog reports the current time while it still runs old requests, so the watermark passed samples of requests admitted after the flush. A request admitted later holds samples stamped later. A barrier no longer moves the clock.
  • A window closes once event time is past its end, as in the ASAPQuery precompute engine's window manager. The watermark from samples is one millisecond before the newest sample less the allowed lateness, so samples at a window's end that come in a later request still join it. A barrier counts whole.
  • Barriers. A barrier that comes while a flush is waited for goes to a queue for the next pass, behind its own flush. A single slot dropped a second barrier (HTTP 500), and joining the current pass closed windows without the writes acknowledged before it.
  • A dead thread makes the flush wait stop and fail the waiting barriers instead of hanging.
  • Backpressure. The coordinator counts a shard's samples out of its queue when it applies the shard's report, not when the shard sends it. While the coordinator waits for precompute, the queues fill to --max-queued-samples, Remote Write gets 503, and the reports waiting stay bounded. Before, the shards kept draining into an unbounded channel: no 503, no eviction, unbounded memory.

Borrowed from the ASAPQuery precompute engine (asap-query-engine/src/precompute_engine/): routing by series hash, a cross-worker watermark where workers with no samples do not hold it back, a periodic tick, the close-past-the-end rule, and dropping and counting late samples. Its watermark, unstarted-worker, boundary and late-data tests are ported at the engine level. ASAPQuery closes each worker's own partial windows; here one coordinator closes a window over every shard's raw data, so the cross-shard minimum is reached by the flush.

 struct ShardReport {
     shard: usize,
     raw_data: Arc<RawData>,
     newest_ms: Option<i64>,
     first_seen: Vec<(Arc<str>, i64)>,
-    now_ms: i64,
+    /// Samples of the batch, counted out of the queues when applied.
+    samples: u64,
 }
 enum Command {
     Report(ShardReport),
-    Flushed,
+    /// The shard numbered here has run every message sent before the flush.
+    Flushed(usize),
     AdvanceWatermark { barrier_ms: i64, reply: oneshot::Sender<i64> },
     Stop,
 }
-fn flush(shards: &[Sender<ShardMessage>], shared: &Shared)
+/// Sends a flush under the admission lock; returns the time it was sent.
+fn flush(shards: &[Sender<ShardMessage>], shared: &Shared) -> i64
 impl Ingestion {
-    pub fn observe(&mut self, first_seen: &[(Arc<str>, i64)], now_ms: i64)
+    pub fn observe(&mut self, first_seen: &[(Arc<str>, i64)])
+    /// Sets the clock the watermark is capped at: the last flush's time.
+    pub fn advance_clock(&mut self, now_ms: i64)
 }
 pub struct EngineOptions {
+    /// For tests: every shard but the first waits this long per batch.
+    #[doc(hidden)]
+    pub shard_delay_ms: u64,
 }

The boundary between the coordinator and the precompute is unchanged: PrecomputeJob goes over the bounded jobs channel, and the coordinator blocks on it.

Regression tests (data_plane/tests/ingest_shards.rs, one engine against one shard or several). Each was shown to fail on the old behaviour:

Test Before the fix
a_shard_running_ahead_does_not_close_windows_early (8 shards, 7 slowed by 20 ms) wrong answer at 540 s
a_watermark_capped_at_the_clock_moves_only_behind_a_flush (samples up to 1 s ahead of the clock, 4 shards, 3 slowed by 250 ms) 7–11 samples dropped as late per run
samples_at_a_window_end_in_a_later_request_join_the_window (1 and 8 shards) 32 samples dropped over 8 shards
a_late_sample_is_dropped_on_any_shard_count (from ASAPQuery's test_late_data_drop) — (new coverage)
a_precompute_that_cannot_keep_up_reaches_remote_write 40 of 40 requests admitted, no 503
a_barrier_during_another_barriers_flush_covers_writes_before_it sums low
barriers_during_one_flush_all_answer HTTP 500 for one barrier

Parity (PARITY_INGEST_THREADS=n), on 8682b64's code: 148/148 queries and 162/162 range steps, 3 runs each at 1, 4 and 8 shards. ingest_shards: 9/9, 3 runs.

Precompute task pool and windows split by series

Before: one precompute thread computed every closed window in turn. Groups that closed together queued behind each other, and one big group's window took as long as one core needed. With the 60 s, 10 Hz dashboard (10k series of data and of data_total; one refresh group of twelve per-series views, 12M samples per window), each window took 23.8 s on one thread and was ready 24.7 s after its end. Every query at a 3 s or 6 s delay was refused.

After: --precompute-threads (default a quarter of the cores, 1 to 4; 1 is the old behaviour).

  • Windows as tasks. Each closed window is a task. Windows of different groups, and of one group, compute in parallel on precompute-N. A per-group reorder buffer commits each group's windows in window order. The committer (precompute) is the view store's only writer and publishes once per pass. A group that reads another group's views starts a window only after the windows it reads are committed (RefreshTaskGraph, built at install). A group holding an operator that may carry state across windows runs serially.

  • Windows split by series. A group can be split when every node computes each series alone: time-series Scans that keep the series identity, row filters, and summaries with a PerEntity reduction. If it also reads no other group's views, each window runs as one task per thread, and part i reads the series whose id is i mod n. The committer merges the parts' rows into the executor's group key order, one batch per view, so the stored views are byte-identical to one run's. Any other group runs whole. The install log says which groups are split, and /metrics exports asap_precompute_window_parts. Merging partial states of a node that combines series would change float sums in the last bits, so it is not done.

  • Why a task can run on its own. A task is a pure function of immutable inputs:

    • the raw-data snapshot taken when the window closed (every shard flushed; Arc panes never changed in place);
    • the committed views it reads.

    Its output stays invisible until committed, and each execute_ingestion starts fresh.

  • Backlog and metrics. The backlog bound is unchanged (plus up to two windows per worker in flight), and backpressure still reaches Remote Write. New metrics, per group: queue wait, commit wait, and window-ready time (window end → committed). For the pool: busy seconds and thread count.

examples/ingest_bench, paced (live rate), views plan, 16 pinned cores of a shared 56-core host, allowed lateness 1 s, 420 s, one run each. Window-ready time is from the window's end to its commit, the 1 s allowed lateness included:

Workload Threads Ready mean (max) Precompute CPU, 7 windows Process CPU
60 s dashboard, 10 Hz, 20k series (200k samples/s) 1 24.7 s (25.7 s) 167 s 303 s
8, split by series 4.9 s (5.3 s) 193 s 331 s
12, split by series 3.5 s (3.8 s) 181 s 319 s

At 12 threads every window is ready within 4 s of its end, so a 6 s query delay is served. A split run costs about 8–15% more CPU than one run: each part reads and cuts every chunk of the window. Before chunks were cut once per run, the extra cost was 70%.

Windows in parallel only, random-mixed-seed-862-01 (three groups, 10k series per metric, 300 s):

Run Threads Refreshed throughput Mean queue wait by group Precompute CPU
unpaced, views in memory 1 116,425 samples/s 12.4 / 11.9 / 12.8 s 25.6 s
4 280,964 samples/s 3.7 / 3.5 / 3.9 s 39.4 s
paced, 10 Hz (100k samples/s) 1 ready 14.6 / 15.1 / 15.5 s (15 s lateness included) 0.00 / 0.25 / 1.15 s 97 s
4 ready 14.7 / 14.9 / 14.6 s 0.00 / 0.03 / 0.12 s 93 s

The mixed-workload rows were measured before the series split. In those runs, the 4-thread CPU includes the cost of cutting chunks per scan, which is now fixed.

Columnar raw store

Raw samples are held per pane (width: gcd of the refresh groups' widths and phases and 60 s). Each Remote Write request's samples of a pane are one immutable chunk: time and value arrays and a per-row series code into the chunk's series, whose identity JSON is computed once when the series is interned. A pane the watermark has passed is compacted into one chunk. Eviction drops whole panes, and a read never returns a sample at or before the eviction cutoff, so answers match the row store exactly.

Open pane, chosen for query-time Scans: the ingest thread publishes the raw data after every request (copy on write: per-pane chunk lists are copied, chunks are shared), and a read returns one batch per chunk of every pane overlapping the range, the open pane's chunks so far included; only a chunk straddling the range is filtered. This is the simplest option that needs no lock and no copy of the open pane per read; its cost is many small batches when many small requests fall in one open pane.

Shared query-time results (the plan decides)

A OneEvaluation query-time node that several queries' runs reach is computed once per evaluation time T: the first run computes it while concurrent runs wait (single flight), later runs read it from the view store's query-time partition, keyed by (T, node), kept --query-time-results-retention-ms (60 s) behind the newest T, bounded by --query-time-results-max-bytes. Any other node two queries' runs would compute makes the plan refused at install. No executor change: the node is bound as an on-first-read view slot.

Result cache

Before: a range request refreshed 15 s later recomputed every step. After: each planned query's answer at evaluation time T is kept, keyed by (query root, T), once it is final: the run succeeded (every view window it read is stored, and stored windows never change) and, if the root reads raw data at query time, T ≤ the watermark its snapshot was published with. A range request runs only the missing steps; an instant query at T reads a range step at T. Eviction: T more than the plan's longest retention plus --view-retention-margin-ms behind the watermark, then LRU beyond --result-cache-bytes (256 MiB). A run now reads the watermark before the snapshot, which also closes that race for shared query-time results. The steps of one range request read one snapshot, and raw chunks read whole are converted once per request.

examples/range_bench, dashboard workload, 1k series per metric, four 1 h panels at 15 s steps (964 steps), 56-core x86 server:

Request Cache on Cache off
first (cold), views plan 5.0–5.1 s, 176–187 s CPU 5.1 s, 177–180 s CPU
15 s later, views plan 1.2–1.6 s, 1.2–1.6 s CPU (960/964 steps cached) 4.8–5.1 s, 172–175 s CPU
15 s later, raw plan 1.1–1.3 s, 1.1–1.3 s CPU 4.5–4.9 s, 152–172 s CPU

The rest of a cached refresh is JSON encoding of the two 1,000-series matrices (0.4–0.5 s each). Reading one snapshot per request changes cold CPU by about 3 % (within noise): the cold cost is DDSketch merges, not reads. The Planner's cost model does not know about the cache (follow-up in the design doc).

/metrics CPU breakdown

asap_phase_cpu_seconds_total / asap_phase_wall_seconds_total / asap_phase_runs_total by phase: remote_write_decode, append, precompute (per refresh group), query (per query text); thread CPU from CLOCK_THREAD_CPUTIME_ID. Also asap_query_latency_seconds (histogram per query), asap_samples_total, asap_windows_total, asap_samples_queued, asap_precompute_backlog_windows, asap_watermark_lag_seconds, asap_raw_data_bytes, asap_view_bytes, asap_query_time_result_*, asap_view_persisted_bytes_total, asap_view_sync_seconds_total, asap_view_windows_not_persisted_total, process_resident_memory_bytes, and asap_thread_cpu_seconds_total{thread}. scripts/profile.sh writes a flamegraph of a running data plane.

Ingest throughput, before and after

cargo run --release -p data_plane --example ingest_bench -- SERIES 300 PLAN [REQUESTS] [memory]: in process, 300 s of 1 s scrapes sent by Remote Write as fast as accepted, then a barrier at the last sample. Workload random-mixed-seed-862-01; raw keeps raw samples (4 views, 25 windows), views keeps none (7 views, 325 windows). "Refreshed" is samples/s until every window is refreshed; "acknowledged" until every write is accepted. Views are persisted (the default). One run each on a shared host, so the numbers are indicative.

Plan, series Before this PR: refreshed Sync per window: refreshed (acknowledged) Now: refreshed (acknowledged) Now: CPU ingest / precompute; sync time
raw, 1k 387,550 22,953 (473,208) 1,244,680 (2,146,467) 0.12 / 0.21 s; 0 s
raw, 10k 362,043 188,600 (468,328) 413,992 (2,217,223) 0.92 / 2.66 s; 4.0 s
views, 1k 67,883 1,916 (484,228) 25,283 (2,243,669) 0.08 / 5.34 s; 12.0 s
views, 10k 54,173 14,509 (464,644) 11,020 (2,271,512) 0.95 / 61.98 s; 197.9 s
views, 10k, 100 requests per scrape — 20,132 (450,296) 10,570 (2,004,919) 4.15 / 63.22 s; 209.2 s

"Now" includes jemalloc, the in-place Remote Write decode, the interval sync (5 s) and the per-file text dictionary. Writes are acknowledged at 2.0–2.3M samples/s. View-heavy plans are limited by the precompute thread and, at 10k series, by the disk: the views plan writes 6.5 MB per window (2.1 GB per 5 minutes) and syncing it took about 200 s on this shared, nearly full disk.

Ingest threads (same bench, one client, 16 cores). Acknowledged throughput stays at about 2.2–2.3M samples/s at 10k series and 1.7–1.8M at 100k with 1, 2, 4 or 8 shards: the single bench client, which decodes and sends every request itself, is the limit, not the shards. The table is in the design doc's Measurements section.

Remote Write decode (decode_bench, µs per sample): one series per sample (10,000 × 1, the feeder's shape) 1.80 (system allocator) → 1.51 (jemalloc) → 0.38 (in-place decode); 100 samples per series (Prometheus' shape) 0.083 → 0.083 → 0.089.

Bytes per window written (views plan, 1k series): 0.93 MB with a sync per window and label text per row → 0.64 MB with the per-file text dictionary; raw plan 224 KB → 123 KB; views plan, 10k series, 9.4 MB → 6.5 MB.

View persistence

Every sketch kernel is persisted through the executor's sketch() and restore_from_disk (ASAPPlanner #1435): DDSketch, KLL, Count-Min, KMV, HyperLogLog, weighted frequency (Count-Min or Count Sketch, with or without a heap), UnivMon, Hydra-KLL and Hydra-Count-Min (one grid per window, shared by its groups), as asap_sketchlib's ASAPv1 bytes; exact states by their serde form. A UnivMon whose heaps mix key types has no ASAPv1 encoding (an asap_sketchlib limitation) and is counted in asap_view_windows_not_persisted_total; it is 0 for the benchmark and demo plans. Files are synced every --view-sync-interval-ms (default 5 s, VictoriaMetrics' default -inmemoryDataFlushInterval), so a window is durable within that interval; texts are stored once per file. A file of another plan is renamed *.stale; a torn last record is cut off at load; after a restart, windows whose samples arrived before it stay missing.

Review fixes

Shared query-time results: no orphaned entries or leaked bytes (an entry is created computing, under the map lock), waiters get the computer's failure, results are kept only for times the watermark has passed and expire behind it. Range queries: at most one step per query worker in flight, the first failure or a dropped client stops the rest, steps measured apart from instant queries, the 11,000-step limit as Prometheus applies it. A panicking query run answers with an error and the worker keeps serving. Not done: the precompute test gate stays on the production path (one uncontended lock per window); test helpers are not yet consolidated into tests/common; building MaterializedView values through serde JSON and the per-request work linear in retained panes are recorded as follow-ups in the design doc.

Code deleted, replaced by the Planner

Backend code Replaced by
sds.rs (definitions, keys, build order) materialized_views, ingestion_refresh_groups
precompute.rs pane builds ingestion runs per refresh window
raw_sample_window.rs DataStore::read_raw_data with the executor's Scan/TimeRange
per_sample.rs (per-sample state updates) none yet: views are built from the window's raw samples
query.rs raw fetches from Prometheus, QueryRun panes query runs over the data plane's own store

Their tests are deleted with them (per_sample, precompute, query, query_run, sds, answers).

Tests

  • New planner_executor_parity: 18 public workloads (examples/, plus ASAPPlanner's random-mixed-seed-862 01–06 and four sketch-plan snapshot workloads copied unchanged into data_plane/tests/workloads/), each planned with raw data kept and not kept. The data plane runs over HTTP with --no-forward; the Planner executor runs in process on the same plan and samples. Result after the re-pin and with shared query-time results on: 36 plans (18 workloads, raw data kept and not; two raw-only workloads cannot be planned without raw data), 148 of 148 queries equal, all with rows. Plans with a KLL sketch are compared within 5%: KLL seeds its compaction coin at random, so two builds of one window differ within its error (the dashboard plan's quantile query). The design doc's six example workloads are among them. The same harness plans the mixed workloads 01–03 with range_evaluation and compares each range step with the Planner executor's run at that time: 162 of 162 steps equal.
  • New a_query_behind_the_newest_window_is_still_answered.
  • New: a query is answered from the refreshed views while a refresh is held (concurrency); two queries sharing a OneEvaluation node, run concurrently at two times, compute it once per time and answer as runs that compute it themselves, and a plan that marks that node None is refused (shared_results); a planned range query equals its per-step instant answers and an unplanned one is refused (range_queries); exact, DDSketch and KLL views answer alike after a restart, another plan's files are not loaded, and windows before a restart are not stored empty (view_persistence); each persisted sketch kernel round-trips through bytes, a torn tail is cut off, waiters share the computer's result or failure, range steps are measured apart (unit and range_queries tests); /metrics reports the phases (server); the columnar read returns the row read's rows, pane eviction, and query-time result expiry (unit tests).
  • Adapted to the Planner's current inputs: fixed_interval becomes fixed_interval_at, and values_non_negative becomes column_signs. Two expectations changed with the Planner's selection:
    • The signed-sample DDSketch test now needs ignore_accuracy_guarantees, because there is no guaranteed plan without raw data. It is bounded by the plan's own α (0.1).
    • The joint-quantile test no longer asserts one shared sketch. The Planner now builds two per-series DDSketch chains (their Scans differ only by a region column); answers are still within α.
  • Precompute task pool and series split (precompute_pool, plus scheduler unit tests in engine.rs):
    • Every public workload plan without a KLL sketch, the 60 s dashboard included, stores byte-identical view files with 1, 4, 8 and 12 threads. Groups are split by series in most plans, and with 12 parts most parts are empty. The feed moves the watermark one minute at a time, because a jump skips windows past retention depending on timing.
    • planner_executor_parity passes with PARITY_PRECOMPUTE_THREADS 1, 4, 8 and 12 (KLL within 5% as before).
    • A window whose first part is slowed by 1 s is still committed before the group's later windows.
    • Every run reads only views committed through its end, and raw shards and views that do not change while it runs (checked per window under a concurrent feed).
    • Groups closing at one instant are ready within the query delay.
    • The 60 s dashboard window, with a run taking 4 s over every series, is ready within its 2 s deadline on 8 threads.
    • A full precompute backlog still makes Remote Write answer 503.
    • Unit tests: out-of-order completion commits in window order; a split window commits once every part is in; a window waits for the groups it reads; a serial group runs one window at a time; barriers.
    • Failing first:
      • With the commit order, the read-group wait and the serial rule removed, the unit tests and the slow-window test fail.
      • With one thread, the groups-closing and deadline tests fail.
      • With the parts merged without key order, the byte-identity test fails.
  • Result cache: planner_executor_parity asks every query (instant and range) twice with the cache on and once with it off; all three must agree with the reference. New result_cache tests: a repeated range runs no cached step; an instant query reads the range step at its time; a step answered before its raw samples arrived runs again (fails if non-final answers are cached); eviction by age and by LRU bytes, view reads by binary search and shared raw chunk conversion (unit tests).
  • cargo fmt --check, cargo clippy --workspace --all-targets -D warnings and cargo test --workspace pass (after the review fixes: data_plane lib 32, ingest_shards 10, planner_executor_parity 2, precompute_pool 6, result_cache 5, server 10, view_persistence 7, plus concurrency, no_forward, planner_executor_answers, range_queries, series_ids, shared_results, view_memory, window_levels).
  • End to end: Prometheus 3.5 scraping itself every second sends Remote Write to data_plane --no-forward. Four planned queries (rate, increase, max_over_time, sum(sum_over_time)) all run from tumbling-window views refreshed at ingestion. After about 2 h, at three evaluation times, every answer equals Prometheus's own answer exactly (56 + 56 + 1 + 1 series). RSS stayed at 19 MB.

Not done yet

  • Views refreshed on first read (query-time AcrossEvaluations).
  • A UnivMon view whose heaps mix key types is not persisted (an asap_sketchlib limitation).
  • At 10k series the views plan writes 6.5 MB per window; on a slow disk, syncing it limits refreshes (a separate sync thread or persisting fewer views would help).
  • Executor follow-ups from the profile: DDSketch merge clones before merging; group tables are not pre-sized.
  • Views are built from each window's raw samples rather than updated per sample; precompute CPU is now the ingest limit for view-heavy plans.
  • The end-to-end Prometheus run above predates this PR's threading changes and was not repeated.
  • A planned query is answered only once the windows it reads are refreshed, so a query at "now" waits for --allowed-lateness-ms.
  • The shards' own throughput limit is not measured: that needs several concurrent senders.
  • The Planner charges every step of a refreshed range; with the result cache only new steps cost.

Review fixes (3e52dad..b5570f5)

Independent reviews (correctness; dead code and duplication; doc alignment) before merging. Fixed on this branch, each with a failing test first where it is a behavior change:

Finding Fix
A range step given as a huge duration wrapped the step times, so one request grew a Vec without end steps above MAX_TIME_MS refused; step times stop instead of wrapping (44b7122)
A raw read that reached evicted samples returned a partial window, answered and cached as final the read fails, so the step is forwarded (refused under --no-forward) and never cached; reads before the first sample still succeed, as in Prometheus (5e1159d)
A late sample for a query-time Scan was accepted behind the published watermark after answers at that time were cached such samples are dropped and counted as dropped_late (75d4325)
A sample at an extreme timestamp overflowed window arithmetic and stopped an ingest shard dropped and counted in the router (f51bedc)
A view-file record that did not decode was skipped, shifting the file's text dictionary loading stops at that record and the file is cut there (71afe3c)
clippy type_complexity on the CI toolchain named label-map types (3e52dad)
Dead code: remote_write::decode, series_ids::intern, DataPlane.samples, duplicate serde_json dev-dependency, items pub but used only in the crate test-only / deleted / narrowed (cc7b148, 15e7185, f2667ab, 8c992c1, dbca98c)
Duplication: the view-retention cutoff in two files; test helpers copied across data_plane/tests one helper (f39fc7b); shared helpers in tests/common (b5570f5)
A local default path in scripts/profile.sh; a stray disk-usage file removed (bf6a678, 2554970)
README, module docs, flag help and the design doc described one ingest and one precompute thread, per-window fdatasync and an old pin; issue numbers in the design doc updated to the code (af8e56e, c2644e5, 2133c23)

Pins ASAPPlanner 1760a69f (asap-planner, asap-executor). The plan file is
now wire version 15: a PhysicalASAPDAG with each view's and query-time
Scan's retention_ms, and the query texts.

control_plane: plan_installable returns (InstallablePlan,
SelectedPlanReport); deployment inputs are DeploymentCapabilities and
CostInputs; --cost-inputs, --report; load_plan checks the plan compiles.
The backend declares query_time_results_retained: false (no view
refreshed on first read yet).

data_plane: the stored-summary, precompute, per-sample and raw-window
modules are replaced by the executor. A time-indexed store keeps raw
samples (Remote Write) and view rows (InMemoryDataStore); ingestion runs
one ingestion run per refresh group window; each query is one query run
of its root. Views are kept a margin past their retention so queries
behind ingestion are answered. Raw samples are no longer read from
Prometheus.

Tests: planner_executor_parity compares the data plane over HTTP with the
Planner executor in process on 14 public workloads, raw data kept or not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zzylol zzylol added enhancement New feature or request area: control plane Planning, configuration, routing, and control-plane orchestration area: data plane Runtime ingestion, storage, and query-serving data paths integration: ASAPPlanner ASAPPlanner API, IR, candidate, and selected-plan integration test: end-to-end Process-boundary, differential, conformance, and full-stack tests labels Nov 17, 2026
The deployment capabilities gained counter semantics, state families
not mergeable across windows and excluded candidates; the backend keeps
their defaults. The parity test binds its reference raw tables for
appending, since binding a table whole is now test-support only.
Queries no longer queue behind ingestion on the engine thread. The
engine thread publishes a snapshot of the store after each command;
query-N worker threads (--query-threads, default one per core) each run
one query to completion over the newest snapshot. A run's output stream
is not Send, so a run never moves between threads.

The store is copy on write: materialized views move from the executor's
InMemoryDataStore to a ViewStore whose clones share every batch, with
the same tiling read and retention. Raw data is cloned when a snapshot
still reads it; the next commit replaces it with shared columnar panes.
The engine thread becomes two: the ingest thread stores samples, keeps
the watermark and closes refresh windows; the precompute thread runs each
closed window's ingestion run and publishes the views. A refresh reads the
raw data as it was when its window closed (the snapshot travels with the
job), so it reads the samples the serial engine read. Closed windows not
yet refreshed are bounded (--max-precompute-backlog, default 256): when
the backlog is full the ingest thread waits, its queue fills and Remote
Write gets 503. The backlog is reported on /api/v1/status/asap.

Raw samples are held columnar by pane: each Remote Write request's
samples of a pane become one immutable chunk (time, value and a series
code per row; series identity text computed once per series when it is
interned); a pane the watermark has passed is compacted into one chunk;
eviction drops whole panes and reads never return samples at or before
the eviction cutoff. The pane width is the gcd of the refresh groups'
widths and phases and 60 s. A read returns one batch per chunk of the
panes overlapping the range, the open pane's chunks included, filtering
only chunks that straddle the range.

Tests: a query is answered from the refreshed views while a refresh is
held; pane eviction; the columnar read returns the row-wise read's rows.
…; ingest bench

Threads are named ingest, precompute and query-N. Each phase records its
thread CPU time (CLOCK_THREAD_CPUTIME_ID) and wall time: Remote Write
decode on the HTTP threads, append on the ingest thread, each refresh
group's ingestion runs on the precompute thread, and each planned query's
runs on the workers. GET /metrics exports them in the Prometheus text
format with query latency, sample and window counters, the queued samples
and precompute backlog, the watermark lag, the samples, panes and bytes
held as raw data, the bytes of views, RSS, and each thread's CPU time by
name.

scripts/profile.sh records a perf profile of a running data_plane and
writes a flamegraph. The ingest_bench example feeds Remote Write requests
in process and reports samples per second and CPU per thread.

The README describes the threads, the backlog bound and the metrics.
… per evaluation time

What runs once is the plan's decision, by each query-time node's
Lifetime. A OneEvaluation node that several queries' runs reach is cut
out of them into a run of its own; each query run binds it as a
materialized view slot (an on-first-read descriptor, so asap-executor
is unchanged). Its output at evaluation time T lives in the view store's
query-time partition, keyed by (T, node): the first run that needs it
computes it while concurrent runs wait for it (single flight), later
runs read it, and results are dropped once T is more than
--query-time-results-retention-ms behind the newest evaluation time or
when more than --query-time-results-max-bytes are held. A failed
computation is not kept. A plan that would compute any other node in
several queries' runs is refused at install. --no-shared-results
computes the node in every query's run, as before, for comparison.

/metrics exports the partition's bytes, entries, computes and hits.

Tests: two queries of #509 Example 1 sharing a OneEvaluation node, run
concurrently at two evaluation times, compute it once per time and answer
as runs that compute it themselves; a plan that marks that node None is
refused; the partition's single computation and expiry. The parity test
passes with sharing on (126 of 126 queries equal).
…r step

/api/v1/query_range parses query, start and end (Unix seconds) and step
(seconds or a Prometheus duration), refusing a non-positive step, an end
before the start or more than 11,000 steps with 400 bad_data as
Prometheus does. A planned query is evaluated at every step, the steps in
parallel on the query workers (a shared OneEvaluation node is computed
once per step time), and answered as a Prometheus matrix; it is not
answered if any step is not, and is then forwarded with the reason, or
refused under --no-forward, as an instant query is.

Tests: a planned range query's value at each step equals the instant
answer at that time; an unplanned range query is refused under
--no-forward; the parity harness plans the mixed workloads 01-03 with
range_evaluation over each query's last three evaluations and compares
every step with the Planner executor's query run at that time (162 of 162
steps equal).
Each view has one append-only file in --view-dir (default asap-views;
--no-view-persistence keeps views in memory only): one record per
refreshed window, a little-endian length and a MessagePack payload with
the window's (start, end] and rows, synced before the next window is
refreshed. At startup the records within each view's retention are
loaded back into the view store, and a window every view of its group
already stores is not refreshed again (windows_already_stored). A partial
record at a file's end is ignored. Raw samples and open windows are not
persisted.

Plain values and exact states (ExactAccumulator, IncreaseAccumulator,
which the executor serializes) are stored. Sketch states have no byte
encoding in asap_sketchlib or the executor yet, so a window of a view
holding one is not written; /metrics counts those windows
(asap_view_windows_not_persisted_total) beside the bytes written, the
sync time and the windows loaded.

EngineOptions is no longer Copy: it holds the view directory.

Test: a data plane restarted over the view files of an earlier one
answers a query as before without any sample written again.
docs/design/data-plane-demo.md describes the demo: goals and non-goals,
the ingest, precompute and query-worker threads, columnar panes and the
open pane, backpressure, OneEvaluation results shared per evaluation time
as the plan decides, range queries, view persistence, the /metrics
names, the evaluation design and the rejected alternatives. The README
links it.
The design records why one chain width makes long windows at a low
sample rate no cheaper than raw scans, and the planned levels of chains
(a coarse chain computed from the fine chain's view at ingestion time),
with their cost and the work they need. The alternatives table records
that stored views are bounded, separately from how a window is computed
and from freshness at the open window.
@zzylol zzylol added area: query engine PromQL lowering, execution, result shaping, and fallback behavior feature: range evaluation Range-query steps, windows, timestamps, and temporal evaluation documentation Improvements or additions to documentation labels Nov 20, 2026
…an; safe restarts

Sketch states are stored as asap_sketchlib's ASAPv1 bytes with the
kernel they rebuild (DDSketch, KLL, Count-Min, KMV, Hydra-KLL), read and
rebuilt through the executor kernels' public sketch fields. HyperLogLog,
UnivMon, Count-Min-with-heap and Hydra-Count-Min kernels expose no way
to rebuild their state, so views holding them are still not persisted
and are counted.

Review fixes:
- One file per refresh group, one record per window holding every view
  appended, one fdatasync per window (was one per view and window).
- Each file starts with a header naming the plan (a hash of the plan
  file) and the group; another plan's file is renamed *.stale and never
  loaded, so a changed plan cannot load wrong views.
- A torn or oversized last record is cut off at load, so records
  appended afterwards stay aligned; a garbage length no longer sizes an
  allocation.
- A window is skipped per view: a view that already stores it is not
  appended to again and no duplicate record is written, while the
  group's other views still get it.
- After a restart that loaded views, a declared stream start no longer
  makes the first window complete, so windows whose samples arrived
  before the restart stay missing instead of being stored empty.
- The ingest thread no longer overwrites windows_already_stored.

Tests: each persisted kernel round-trips through bytes (re-encoded
bytes, estimates and merges equal); torn tail and other-plan files;
exact, DDSketch and KLL views answer alike after a restart; another
plan's files are not loaded; windows before a restart are not stored
empty.
…orkers

- Query-time results: an entry is created in a computing state under the
  map lock, so expiry never drops one being computed and its bytes are
  counted only while it is held; a failure reaches the runs that waited
  for it and is not kept; a result is kept only for a time the watermark
  has passed (a client's far-future time is computed per run), and
  results expire behind the watermark, not behind the newest client time.
  The ingest thread publishes the watermark with the raw data.
- Range queries: at most one step per query worker in flight; the first
  unanswered step, or the client going away, drops the remaining steps,
  and a worker skips a step nobody waits for. Steps are measured as
  phase=query_range_step and requests as asap_range_query_latency_seconds,
  apart from instant queries. A range is refused, as Prometheus does,
  only when (end - start) / step exceeds 11,000.
- A panic in a query run answers that query with an error; the worker
  keeps serving.
- Smaller: no String per sample in ingest_at; a chunk's series identity
  dictionary is built on its first read; PublishedStore::run_store
  removed; series_ids doc; the bench reuses metrics::thread_cpu_seconds
  (clock ticks from sysconf), persists views by default and can split a
  scrape into many small requests.

Tests: single computation and expiry behind the watermark; waiters share
the computer's result or failure; range steps measured apart.
Copies, unchanged, ASAPPlanner's chains-10m-5m-3m-10s,
rollup-quantile-nested-labels and topk-by-job-and-global sketch-plan
workloads and the rangeheavy-t5-q05-q09-1h memo example, plus the demo's
dashboard workload, into the parity harness (raw data kept and not).
The dashboard plan's quantile chain is a KLL sketch, which seeds its
compaction coin at random, so a plan with KLL is compared within 5%;
every other plan exactly.
Adds how the precompute and query engines execute six plans of different
shapes, corrected to the implementation (refresh groups in plan order, a
Scan under two groups read by each, OneEvaluation nodes cut when read by
several queries' runs, range steps bounded per worker), and corrects the
design after review: late samples, view retention wording, the new public
surface, query memory beyond the run limit, worker panics, query-time
result retention behind the watermark, range-query bounds and metrics,
per-group view files with one sync per window tied to the plan, restart
behavior, sketch persistence and its gaps, and follow-ups.
The data_plane binary (and the benches) use jemalloc: Remote Write
decoding and query runs allocate many small, short-lived objects across
threads. The write handler's decode-and-intern step moves into
remote_write::decode_samples, and the decode_bench example measures it
per sample for two request shapes.

decode_bench, microseconds per sample (system allocator -> jemalloc):
one series per sample (10,000 x 1) 1.80 -> 1.51; 100 samples per series
(100 x 100) 0.083 -> 0.083.
…es cache

decode_samples walks the decompressed protobuf in place instead of
building a WriteRequest of owned Strings. Each TimeSeries' encoded label
bytes are the key of a per-thread cache of interned series, so a known
series costs one hash and one compare, with no lock and no allocation;
only a new series is parsed, validated (UTF-8, __name__, no duplicate
label) and interned. The samples vector is sized from the body up front.
Errors are those of the full decode.

decode_bench, microseconds per sample (jemalloc, before -> after): one
series per sample (10,000 x 1) 1.51 -> 0.38; 100 samples per series
(100 x 100) 0.083 -> 0.089.

Test: the in-place decode gives the full decode's samples, series and
errors.
…r file

View files are synced every --view-sync-interval-ms (default 5,000:
VictoriaMetrics' default -inmemoryDataFlushInterval as of v1.155.0)
instead of after every window, and the precompute thread also syncs when
no window closes, so a refreshed window is durable within that interval,
the same class of guarantee VictoriaMetrics gives its in-memory data; 0
syncs after every window. The files are synced when the engine stops.

A text (label value or series identity) is stored once per file: each
record carries the texts it adds to the file's dictionary and rows hold
indices. Loading replays the dictionary, and appends after a restart
continue it.

Test: a second restart after windows written since the first reads them
back alike (dictionary continued across restarts).
result_samples parsed every row's series identity JSON into a label map.
The interner now indexes series by identity text, so a row of an input
series takes its labels from the interned series; only a series the
query built (its labels changed) is parsed.
ingest_at looked each sample's metric up in a BTreeSet<String> and a
SipHash map keyed by an Arc<str> clone, and checked every refresh group
for a start. Samples of one metric come together, so the last metric is
checked by pointer first, the per-request metric list holds whether a
Scan reads it and its eviction cutoff (taken at its first routed sample,
as before), and groups are checked for a start only while one has not
started.
inferno stays the default; without it the FlameGraph perl scripts in
FLAMEGRAPH_DIR (default /mydata/bin/FlameGraph-41fee1f) draw the graph,
and the error names whichever tools are missing. Full stacks need
CARGO_PROFILE_RELEASE_DEBUG=true.
…e_from_disk

Pins ASAPPlanner main 056da657, whose executor gives every sketch kernel
one interface: sketch() to read its asap_sketchlib state and
restore_from_disk to rebuild it. All persisted kernels now use it, and
four more persist: HyperLogLog (with its precision), weighted frequency
(Count-Min or Count Sketch, with or without a heap), UnivMon (with its
key-byte counters) and Hydra-Count-Min, whose grid is shared by every
group of a build and is stored once per record and rebuilt once. A
UnivMon whose heaps mix key types has no ASAPv1 encoding; its windows
are counted as not persisted and the refresh goes on.

The design doc records the executor follow-ups the profile found
(DDSketch merge clones before merging; group tables not pre-sized).

Tests: every kernel round-trips through bytes (re-encoded bytes,
readouts and merges equal; a merged UnivMon compared on L2 only, since
its merge breaks heap ties nondeterministically); a shared Hydra-Count-Min
grid is stored once and answers every group as before.
…te path

The precompute thread publishes a refreshed window and queues its rows;
a pool of --persist-threads persist-N threads (default a quarter of the
cores, 1 to 4) encodes, writes and syncs them. Refresh group g goes to
thread g % n, so each file has one writer and keeps window order. The
queue is bounded by bytes (--max-persist-backlog-bytes, default 256 MiB);
only when it is full does the precompute thread wait, exported as
asap_view_persist_wait_seconds_total, with asap_persist_backlog_bytes and
asap_persist_backlog_windows. Each persist thread syncs its files every
--view-sync-interval-ms on its own timer, and writes and syncs everything
queued when the engine stops. An event-time barrier answers once the
windows before it are written, without the precompute thread waiting.

Tests: with every window's write delayed, windows are refreshed and
answer at once, and stopping the engine still writes every queued window;
with three persist threads, every query of a plan with several refresh
groups answers alike after a restart.
@zzylol
zzylol marked this pull request as ready for review November 23, 2026 21:08
Add series_ids::Labels for a shared label set and use it in the matrix
response and the result-label table, so clippy::type_complexity no
longer fires on newer toolchains.
A step given as a duration (e.g. 9223372036854775807ms) was not capped:
(end - start) / step was 0, so the step count check passed, and
start + i * step wrapped, so the evaluation times never ended and a
range request collected an unbounded list. Refuse a step beyond
MAX_TIME_MS and stop the evaluation times instead of overflowing.
Describe the precompute committer and its precompute-N workers, the
result cache and its flags, the query-time results' default byte bound,
the pinned Planner commit, and name the example without an issue number.
Sharded ingestion and multi-level window chains are built: say what
remains of each. Ingestion-time windows run in parallel, ordered by the
refresh task graph. Replace issue and PR numbers with short
descriptions, and mark the plan listings as illustrative output of an
earlier Planner commit.
RawData::read clamped a range's start to the eviction cutoff, so a step
whose window reached samples already evicted was answered from the
samples left (a partial window), served as a plan answer and kept in
the result cache. A range starting before the evicted samples now fails
the read, which the run reports like a missing view window
(Unanswered::MissingRows): the step is forwarded, or refused with
--no-forward, and never cached. A range before the first sample ever
received still reads as empty, as Prometheus answers it.
Module docs name the ingest shards and coordinator, the precompute pool
and committer, the persist threads and the range queries; the store's
raw table includes the series identity; view files are one per refresh
group, synced on the sync interval by their persist thread; query-time
results are kept behind the watermark.
…ecode

read_group skipped a record that did not decode and loaded the ones
after it, which then read the per-file text dictionary shifted by the
texts the skipped record added. Treat it as a torn tail: load the
records before it and cut the file there.
A Remote Write sample at i64::MIN reached window arithmetic
(window_end negates the timestamp) and overflowed, which panicked an
ingest shard and stopped the engine. The router now drops a sample
further from the epoch than prometheus::MAX_TIME_MS and counts it as
future or late. It is done in the router rather than in decode_samples
because the router keeps the per-outcome sample counters.
Only its own tests called it; it now lives in the tests module, and
decode_samples states its errors itself.
Only a test called it; the test interns label pairs with intern_pairs.
serde_json is already a normal dependency, which tests can use.
profile.sh defaulted FLAMEGRAPH_DIR to a path on one machine. It now
uses FLAMEGRAPH_DIR when set, else the scripts on the PATH.
main set the test-only EngineOptions fields by hand; it now fills them
with ..EngineOptions::default(). malloc_conf need not be pub: its
export_name keeps the _rjem_malloc_conf symbol global, which jemalloc
reads.
The router accepted a sample between a metric's eviction cutoff and the
published watermark when a query-time Scan reads that metric, but
answers at times the watermark has passed are final: kept in the result
cache and the query-time results. A cached answer then differed from
what a fresh run returned once the late sample arrived.

Of the two consistent fixes, dropping is the smaller one: the
watermark already means no sample at or before it is expected (it
includes the allowed lateness, and a barrier states every sample at or
before it was written), and the views already ignore such samples
(their windows are closed). Treating those answers as not final would
instead disable the result cache and the shared query-time results for
every query reading raw samples. Such samples are now counted as
dropped_late; eviction still follows the eviction cutoff.
Only tests read it. The server tests now check the samples the engine
routed (accepted, dropped or unrouted) on /api/v1/status/asap.
The view store and the view-file loader each converted retention_ms to
i64 and counted back from the newest window; both now call
store::oldest_kept_end, and the engine converts the query-time results'
retention with ingestion::millis like the other duration options.
SketchKernel, FrequencyKind, decode_sketch, PartitionReads, Routed,
IngestionError (with Ingestion::new and Routing::route that return
them), MAX_TIME_MS, MAX_RANGE_STEPS, BadTime, BadRange (with the
parsers that return them) and FORWARD_REASON_HEADER are pub(crate): no
test or example uses them. encode_sketch, which only tests call, moves
into the persist tests.
Move the helpers the integration tests repeated into tests/common:
posting a Remote Write request, the event-time barrier (and its
status), GET returning status, execution header and JSON, the instant
query URI, a /metrics value, per-job answers, the public workloads,
the no-forward engine options and the one-query plan builder.
ingestion::wall_clock_ms is public so a test reads the clock the data
plane reads.
@zzylol
zzylol merged commit 12aa458 into main Nov 23, 2026
1 check passed
zzylol added a commit that referenced this pull request Nov 24, 2026
…ster Remote Write decode (#864)

Tiered view storage, push precompute by default, and a faster Remote
Write decode in the data plane.

- Tiered view storage: views are written per refresh group and retention
  as time segments, deleted past retention. Decoded windows are cached in
  memory up to a budget (from the cgroup's memory.max) and an idle time;
  other windows are read back from mapped segments, with states read in
  place. Recovery indexes record heads without decoding.
- Push precompute, the default (--precompute-mode push): ingest shards
  fold samples into the open windows' pipelines as they arrive, in
  batches, under a precompute memory budget; groups that cannot be pushed,
  and every group under --precompute-mode window, run one ingestion run
  per closed window.
- Remote Write decode from 652 to 212 ns per sample.

#852's review fixes apply to the new paths. Review fixes: a mixed
push/window plan no longer drops samples an open push window reads; the
memory budget applies with several push groups; ranges before a view's
first window are read only with a declared stream start; segments are
never truncated; on-disk heads are bounds-checked; a cache remove racing
a load is not undone. Pins ASAPPlanner 737bfb1 with asap_sketchlib
patched to 355093f.
@zzylol
zzylol deleted the sync/planner-main-1760a69f branch November 24, 2026 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: control plane Planning, configuration, routing, and control-plane orchestration area: data plane Runtime ingestion, storage, and query-serving data paths area: query engine PromQL lowering, execution, result shaping, and fallback behavior documentation Improvements or additions to documentation enhancement New feature or request feature: range evaluation Range-query steps, windows, timestamps, and temporal evaluation integration: ASAPPlanner ASAPPlanner API, IR, candidate, and selected-plan integration test: end-to-end Process-boundary, differential, conformance, and full-stack tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant