Skip to content

feat(precompute): close an idle stream's last windows by wall-clock time - #889

Draft
zzylol wants to merge 2 commits into
chore/layout-cleanupsfrom
feat/idle-wall-clock-close
Draft

zzylol wants to merge 2 commits into
chore/layout-cleanupsfrom
feat/idle-wall-clock-close

Conversation

@zzylol

@zzylol zzylol commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: a window closes only when the watermark passes its end. The watermark follows the newest sample (less --allowed-lateness-ms) or an event-time barrier. When a stream stops and no producer sends a barrier, nothing moves it. The last windows of that stream then stay open, and queries over them are never answered.

    • Example: series written once a second through 40 s with lateness 0 and a 10 s window. The window (30, 40] waits for a sample after 40 s. If none comes, /api/v1/query?time=40 is never answered.
  • Change: the ingest coordinator keeps an idle watermark. This is decision 5 of docs/design/repo-layout.md (docs(design): repository layout and engine boundaries #874):

    idle = t + (now − a) − L − G
    
    • t: the newest sample time, capped at a.
    • a: when the flush that last ran a kept sample was sent. now − a is measured on the monotonic clock (Instant), so a step of the system clock does not move it.
    • L: --allowed-lateness-ms.
    • G: a 1 s grace.

    The watermark is the latest of the watermark from samples, the barrier, and the idle watermark, and it never goes back. A window (e − W, e] therefore closes once no sample has come for e − t + L + G, which is at most W + L + G.

  • Effect: a stopped stream's last windows close and are answered without a barrier.

    • A stream whose event time lags the wall clock (a replay or backfill) waits exactly as long as a live one, because the rule counts from t, not from now.
    • A sample that arrives later for such a window is late: it is dropped and counted, as today.
    • These windows are counted in asap_windows_closed_by_wall_clock_total and in ingestion.windows_closed_by_wall_clock in /api/v1/status/asap.

Review guide

  • Base: chore/layout-cleanups (chore: range step cap in the query engine, one plan test helper, QueryOutcome as Result #888, b192642), stacked on fix(otlp): OTLP/HTTP replies, Prometheus resource-attribute labels, DELTA sums rejected, in-place decoder #887 → chore: pin ASAPPlanner 566b35c and its asap_sketchlib d050037 (view format 5) #886. This is a draft; do not merge.
  • Read first:
    • crates/precompute/src/engine.rs serve_ingest. A tick checks idle_closes_at; if it is true, all shards are flushed, a and now are taken at that flush, and then advance_idle runs. Barriers are now applied before close(), so a window a barrier closes is not counted as closed by wall clock.
    • crates/precompute/src/ingestion.rs: idle_watermark_at, idle_closes_at, sample_arrived, advance_idle, event_watermark_ms, and the counter in close_windows.
  • Races handled:
    • An idle close always runs behind a flush. Samples admitted before the flush are run and reset a first, so they never land in a window already closed.
    • Reports a shard sends after it answered the flush are held for the next pass, as before, and do not reset a for this one.
    • Only samples ingestion keeps reset a (the new ShardReport::accepted_any). Unrouted, late and future samples do not, so a producer still sending other metrics does not keep the plan's stream open.
  • Monotonic idle time: flush returns a ClockReading (wall and monotonic ms). The wall time caps the watermark and t as before; the idle time now − a uses the monotonic one (WallClock::monotonic_ms, an Instant since process start, or the test's manual value).
  • Mechanical:
    • WallClock (system, or manual for tests) replaces direct wall_clock_ms() calls in the precompute engine: shard routing, flush, barrier cap and lag metrics. It is threaded through PrecomputeOptions/EngineOptions (#[doc(hidden)]).
    • Ingestion::advance_watermark is deleted; its one caller uses advance_watermark_at with the engine's clock.
    • Tests that replay past event times as fast as they can use WallClock::stopped(): 16 sites: 14 in crates/data-plane/tests (including common::no_forward_options and the parity harness), test_support::options, and the precompute engine's unit-test options. Without it, a pause of more than L + G (1 s with lateness 0) under load is taken for an idle stream. The first full gate showed exactly that: with_every_view_read_cold_the_data_plane_answers_what_the_planner_executor_answers failed under load and passed alone.

Behavior: before → after

Situation Before After
Stream writes through 40 s (lateness 0, live clock at 40 s), then stops (30, 40] never closes; query?time=40 not answered closes when the clock reaches 41 s (e − t + L + G = 0 + 0 + 1 s); answered
Same stream replayed 1 h behind the wall clock as above closes 1 s after its last write, not at once: still open at 0.999 s, closed at 1 s
Sample at 35 s after that wall-clock close n/a samples_dropped_late +1; the answer at 40 s is unchanged
A stream that keeps sending watermark from samples unchanged while samples come at least every G (1 s): the idle watermark stays behind the samples' watermark t − L
Barrier closes end ≤ T unchanged
Before the first sample, or after a restart until a sample arrives no close no close by wall clock (a unset); --stream-start-ms only marks the first window complete, as before
Producer that pauses longer than L + G with event time slower than the wall clock windows wait windows in the gap close; samples later sent for them are late (see open questions)

Interfaces

crates/precompute/src/ingestion.rs

+/// The wall clock the precompute engine reads: the system clock, or for tests one they set.
+#[derive(Debug, Clone, Default)]
+pub struct WallClock(Option<Arc<AtomicI64>>);
+impl PartialEq for WallClock { … }   // system == system; manual clocks by Arc::ptr_eq
+impl Eq for WallClock {}
+impl WallClock {
+    pub fn manual(now_ms: i64) -> Self;   // tests
+    pub fn stopped() -> Self;             // tests replaying past event times
+    pub fn set(&self, now_ms: i64);       // moves the wall and monotonic clocks together
+    pub fn step_wall_clock(&self, now_ms: i64); // tests: the wall clock alone, as an NTP step
+    pub fn now_ms(&self) -> i64;
+    pub fn monotonic_ms(&self) -> i64;    // Instant-based; only differences mean anything
+}
+pub(crate) struct ClockReading { pub wall_ms: i64, pub monotonic_ms: i64 }
 pub struct IngestionStats {
+    /// Windows closed by wall-clock time while no sample arrived, ahead of the samples and barriers.
+    pub windows_closed_by_wall_clock: u64,
 }
 impl Ingestion {
-    pub fn advance_watermark(&mut self, barrier_ms: i64) -> (i64, Vec<ClosedWindow>);
+    pub(crate) fn idle_closes_at(&self, now_monotonic_ms: i64) -> bool;
+    pub(crate) fn sample_arrived(&mut self, flushed: ClockReading);
+    pub(crate) fn advance_idle(&mut self, flushed_monotonic_ms: i64);
 }

crates/precompute/src/engine.rs, crates/data-plane/src/engine.rs

 pub struct PrecomputeOptions {
+    pub clock: WallClock,
 }
 pub struct EngineOptions {
+    #[doc(hidden)] pub clock: WallClock,
 }

/metrics:

+# TYPE asap_windows_closed_by_wall_clock_total counter
+asap_windows_closed_by_wall_clock_total 0

Tests

Three new tests in crates/data-plane/tests/ingest_shards.rs. They use a manual clock, with no wall-clock sleeps or timing assertions; a "pass" is a barrier at 0, which closes nothing itself.

  • the_last_window_of_a_stopped_stream_closes_by_wall_clock_time
  • a_lagging_stream_closes_no_window_early: checks both sides of the boundary: the clock at 1 h + 40 999 ms leaves (30, 40] open and 1 h + 41 000 ms closes it.
  • a_sample_after_a_wall_clock_close_is_late
  • a_wall_clock_step_closes_no_window_early (follow-up commit): the system clock steps 15 min forward while the monotonic clock does not; (30, 40] stays open and nothing is counted, then closes once 1 s of monotonic time passes. Before the fix it failed with 9 windows closed by wall clock.

Test-first: with idle_closes_at/advance_idle disabled, all three fail waiting for (30, 40] (ingest_shards.rs wait_for_answer_at_40s); the other 10 tests in the file pass.

Gates

  • cargo fmt --all --check, cargo clippy --workspace --all-targets --locked -D warnings: clean.
  • cargo test --workspace (ulimit -n 65536), at the monotonic-clock commit: 252 passed, 0 failed, 3 ignored = the base's 251 tests + the 4 new ones (base test names unchanged).
  • Parity dump (answers, statuses, /metrics, /api/v1/status/asap, OTLP replies) vs the base: byte-identical except the new windows_closed_by_wall_clock field (0) in the status JSON and the new asap_windows_closed_by_wall_clock_total family. The dump's streams do not go idle. Run on the previous chore: range step cap in the query engine, one plan test helper, QueryOutcome as Result #888 head; the rebase onto b192642 changes no code in this PR.
  • The monotonic-clock commit reads Instant once per flush and per coordinator tick, not per sample; parity and the bench were not rerun for it.
  • ingest_bench 10000 600 raw 1 memory, base/new alternating, 4 rounds each: every write acknowledged in median 1.12 s / 1.13 s, every window refreshed in median 2.26 s / 2.30 s (noise; spread 2.23–2.38 s on both). windows_closed_by_wall_clock 0 in every run.

Open questions

  1. A producer pause can now lose data. A replay or a Prometheus WAL catch-up whose event time advances slower than the wall clock, with a gap longer than L + G, has the windows in the gap closed. Samples later sent for them are dropped as late. This follows the approved rule. An alternative is to cap the idle watermark at the end of the window holding t, which still closes the last windows but loses at most one window on a pause. Another is an off switch.
  2. Query-time raw data is evicted while idle. Raw samples kept for query-time Scans are counted back from the watermark, which now moves while idle. After a lookback's worth of idleness they are evicted, as a barrier would do.
  3. The counter has no view label. It is engine-wide, like the other window counters.

When no sample arrives, the watermark stops, and the last windows of a
stream that stopped sending stay open until a sample or a barrier comes.

The ingest coordinator now keeps an idle watermark: the newest sample
time plus the wall-clock time since a sample last arrived, less the
allowed lateness and a 1 s grace. Because it counts from the newest
sample, not from the wall clock, a stream behind the wall clock (a
replay) closes no window earlier than if it kept sending. Only samples
ingestion keeps reset the time a sample last arrived.

The coordinator checks the idle watermark every tick and, when it would
close a window, flushes every shard first, so queued samples count
before it. Barriers are applied before the idle close, so a window a
barrier closes is not counted as closed by wall clock. The others are
counted (asap_windows_closed_by_wall_clock_total); a later sample for
them is late.

The engine reads the wall clock through precompute::ingestion::WallClock,
which tests set (manual) or stop (tests replaying past event times).
The idle watermark measured the time since a sample last arrived as the
difference of two wall-clock readings, so a forward step of the system
clock (NTP, a resumed VM) advanced it on a live stream and closed windows
early. Each flush now reads both clocks (ClockReading): the wall-clock
time still caps the watermark and the newest sample time, and the idle
time is measured on WallClock::monotonic_ms (an Instant since process
start, or the test's manual value). WallClock::step_wall_clock moves a
test's wall clock alone.
zzylol added a commit that referenced this pull request Nov 30, 2026
…k and are stored empty

Collector windows closed only when states arrived, so while a collector
stream was idle none closed and every range over the idle period failed
("has no rows tiling", HTTP 422), for good if the stream never resumed.
As #889's idle close does for samples, a collector view's group now has
an idle watermark on the monotonic clock: the newest state's window end
(capped at its arrival's wall-clock time) plus the monotonic time since
it arrived, less the collector's close delay and skew allowance
(CollectorViewOptions::idle_close_ms, default 6 s + 2 s). Windows it
passes close with no state and are stored empty; states or corrections
of them coming later merge in (#892). Nothing closes on the clock before
a group's first states.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6bo8kYWd83zXwh2Nobf
zzylol added a commit that referenced this pull request Nov 30, 2026
…k and are stored empty

Collector windows closed only when states arrived, so while a collector
stream was idle none closed and every range over the idle period failed
("has no rows tiling", HTTP 422), for good if the stream never resumed.
As #889's idle close does for samples, a collector view's group now has
an idle watermark on the monotonic clock: the newest state's window end
(capped at its arrival's wall-clock time) plus the monotonic time since
it arrived, less the collector's close delay and skew allowance
(CollectorViewOptions::idle_close_ms, default 6 s + 2 s). Windows it
passes close with no state and are stored empty; states or corrections
of them coming later merge in (#892). Nothing closes on the clock before
a group's first states.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6bo8kYWd83zXwh2Nobf
zzylol added a commit that referenced this pull request Nov 30, 2026
…k and are stored empty

Collector windows closed only when states arrived, so while a collector
stream was idle none closed and every range over the idle period failed
("has no rows tiling", HTTP 422), for good if the stream never resumed.
As #889's idle close does for samples, a collector view's group now has
an idle watermark on the monotonic clock: the newest state's window end
(capped at its arrival's wall-clock time) plus the monotonic time since
it arrived, less the collector's close delay and skew allowance
(CollectorViewOptions::idle_close_ms, default 6 s + 2 s). Windows it
passes close with no state and are stored empty; states or corrections
of them coming later merge in (#892). Nothing closes on the clock before
a group's first states.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6bo8kYWd83zXwh2Nobf
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 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant