Skip to content

Collector views: reorder buffer, persisted incomplete marks, lineage frame in sketch.envelope - #897

Draft
zzylol wants to merge 4 commits into
feat/collector-state-lineagefrom
feat/collector-lineage-reorder-frame
Draft

zzylol wants to merge 4 commits into
feat/collector-state-lineagefrom
feat/collector-lineage-reorder-frame

Conversation

@zzylol

@zzylol zzylol commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: Collector view states: sequence numbers and deltas, incomplete windows, idle close from the declared delay #893's receiver treats a delta that arrives before its base as a gap, so a collector that exports concurrently or retries makes windows incomplete even though every record does reach the backend. Its incomplete marks are kept only in memory, so after a restart an incomplete window reads back complete and is answered from. Its lineage also travels as three OTLP attributes, which cost +48.7 B per state.
  • Change: a reorder buffer holds an early delta until its base arrives, for up to the collector's declared window_close_delay_ms plus the 2 s skew allowance (the same wait as the idle close). Incomplete marks are written to the view files (FORMAT_VERSION 5 → 6). The lineage is read from the binary frame in sketch.envelope (A1 | flags | uleb128 sequence | uleb128 base | ASAPv1), which ASAPCollector-public#64 emits.
  • Effect: if a lineage's records arrive as deltas 2 and 1 before their full, all of them are applied and the window answers as if they had arrived in order. Incomplete windows stay unanswered (and forwarded) after a restart. The lineage costs 3.8 B per state instead of 48.7 (golden on-time states, measured on the collector side).

Review guide

Behavior: before → after

The workload is quantile_over_time(0.99, data[5m]) (a DDSketch chain on the collector). The collector's window (T−5 s, T] for job a arrives as: full (seq 0); then, in separate requests, delta 2, delta 1, and a restarted instance's delta (epoch 2, seq 1) before its full.

#893 this PR
delta 2 before delta 1 gap → window incomplete → query forwarded held, then applied after 1; the answer equals the in-order answer (records_out_of_order_answer_as_in_order_ones)
a base that never comes incomplete at once incomplete after window_close_delay_ms + 2 s (default 8 s), counted held then incomplete
restart after a window was marked incomplete read back complete → answered from a partial state still incomplete → forwarded (an_incomplete_window_stays_incomplete_after_a_restart)
lineage on the wire asap.sequence, asap.state_kind, asap.base_sequence attributes frame A1 00 00 (full), A1 01 02 01 (delta 2); a bare ASAPv1 state is refused (400)
view files version 5 version 6; version 5 segments are still read; a version 5 binary sets version 6 segments aside

This serves correctness for collector views: no plausible-but-wrong answers across a reordered export or a restart.

Interfaces

crates/common/src/view_states.rs

+/// First byte of a version 1 lineage frame (marker nibble 0xA, version 1).
+pub const LINEAGE_FRAME_V1: u8 = 0xA1;
+/// A record's sequence, kind and ASAPv1 bytes from its sketch.envelope; Err (with the reason)
+/// for a bare ASAPv1 state, another frame version, unknown flags, a bad varint, or a
+/// self-contradicting kind.
+pub fn unframe_lineage(bytes: &[u8]) -> Result<(u64, StateKind, &[u8]), String>;
+/// The collector's encoding of record `sequence` (tests, tools).
+pub fn frame_lineage(sequence: u64, asapv1: &[u8]) -> Vec<u8>;
 pub mod view_identity {
     pub const PRODUCER_EPOCH: &str = "asap.producer_epoch";
-    pub const SEQUENCE: &str = "asap.sequence";
-    pub const STATE_KIND: &str = "asap.state_kind";
-    pub const BASE_SEQUENCE: &str = "asap.base_sequence";
 }

The three attribute constants are removed because the frame replaces the attributes.

crates/precompute/src/collector.rs

 pub struct CollectorViewOptions {
     pub plan_id: Option<String>,
     pub views: BTreeSet<PhysicalASAPNodeId>,
     pub idle_close_ms: u64,
+    /// How long an early record waits for its base (monotonic ms): the file's
+    /// window_close_delay_ms + 2000 skew allowance, as idle_close_ms; default 8000.
+    pub reorder_wait_ms: u64,
 }
 pub struct CollectorStateIngestReport {
     pub staged: u64,
     pub duplicate: u64,
     pub merged: u64,
     pub late_merged: u64,
     pub late_dropped: u64,
+    /// Held for their base (applied when it comes, or incomplete after the wait).
+    pub held: u64,
     /// Records of a window already incomplete.
     pub incomplete: u64,
     pub rejected: BTreeMap<&'static str, u64>,
 }
 pub mod outcome {
+    pub const HELD: &str = "held";
 }

crates/storage/src/{segment,persist,persist_pool}.rs

-pub(crate) const FORMAT_VERSION: u32 = 5;
+/// 6: record heads list incomplete marks.
+pub(crate) const FORMAT_VERSION: u32 = 6;
+/// Version 5 segments are still read (no marks).
+pub(crate) const OLDEST_READ_VERSION: u32 = 5;
 pub(crate) struct RecordHead {
     pub start_ms: i64,
     pub end_ms: i64,
     pub texts: (u64, u32),
     pub grids: (u64, u32),
     pub views: Vec<(PhysicalASAPNodeId, u64, u32)>,
     pub revision: u64,
+    /// Views whose window (start_ms, end_ms] is incomplete; #[serde(default)].
+    pub incomplete: Vec<PhysicalASAPNodeId>,
 }
 impl ViewFiles {
+    /// Appends a marks-only record per retention; returns the bytes added.
+    pub fn mark_incomplete(&mut self, group: usize, range_ms: (i64, i64), views: &[PhysicalASAPNodeId]) -> std::io::Result<u64>;
+    /// The marks `index` found (within retention), taken once.
+    pub fn take_incomplete(&mut self) -> BTreeSet<(PhysicalASAPNodeId, i64)>;
 }
 impl PersistPool {
+    /// Queues the marks to the group's persist thread.
+    pub fn queue_incomplete(&self, group: usize, range_ms: (i64, i64), views: Vec<PhysicalASAPNodeId>);
 }

Usage (as records_out_of_order_answer_as_in_order_ones and an_incomplete_window_stays_incomplete_after_a_restart run it):

// collector-views.json declares window_close_delay_ms = 6000 → CollectorViewOptions::from(&file).reorder_wait_ms = 8000 (+ 2 s skew).
// Request 1: full (epoch 1, seq 0) of job a's window ending T → staged, committed when the request ends.
// Request 2: sketch.envelope = A1 01 02 01 ++ asapv1 (delta 2): unframe_lineage → (2, Delta{base 1});
//   Staging::apply → Applies::Ahead → held (report.held = 1, outcome="held").
// Request 3: delta 1 → late_merged, and it releases delta 2 → late_merged. The window answers as in order.
// A base that never comes: Staging::expire_held (every ingest pass) after 8 s → window incomplete →
//   Precompute::mark_incomplete → ViewStore marks + PersistPool::queue_incomplete → a marks-only record.
// Restart: ViewFiles::index + take_incomplete → ViewStore::mark_incomplete → the query is still forwarded.

Algorithm changes

Receiver: reorder buffer

  • Problem: with a full applied, delta 2 arriving before delta 1 was taken as a gap, and the window was marked incomplete immediately, even though delta 1 arrived 10 ms later.
  • Before: apply_to → Merged / Duplicate / Broken; Broken marked the window at once.
  • After:
    1. apply_to → Merged / Duplicate / Ahead (a delta past its lineage's next sequence, or of an epoch with no full yet).
    2. An Ahead record goes into held[(group, end, series, epoch)][sequence]; a repeat of a held sequence is a duplicate.
    3. When a record merges, the held record at sequence + 1 is applied next, and so on.
    4. expire_held(now) runs on every ingest pass. When a held record is older than reorder_wait_ms, its window is marked incomplete (if still kept; otherwise it is counted late_dropped) and all records held for that window are dropped.
  • Correctness: a delta is still applied only onto its exact base, so applying records in any arrival order gives the same lineage state as in-order arrival. Incompleteness is decided only after the collector's own close-delay bound. Tests: records_before_their_base_wait_for_it, a_base_missing_past_the_reorder_wait_marks_its_window_incomplete, and the data-plane tests below.
  • Complexity: O(log n) per record for the held map. Memory is bounded by the records that arrive within one wait. The expiry scan is O(held) per pass and is empty in the common case.
  • Limits: held records are in memory only, so a backend restart loses them. Their windows then become incomplete through the deltas that follow, after the wait.

View files: incomplete marks

  • Before: marks lived only in ViewStore.
  • After: Precompute::mark_incomplete returns (group, range, views), and the committer queues it to the persist thread, which appends a marks-only record (RecordHead::incomplete) to each retention's active segment. index collects the marks within each view's retention of its newest window (all marks if the view has no window), and startup re-marks the store.
  • Correctness: a mark is independent of the window's revisions, so a later rewrite of the window does not clear it. Marks expire with their segment.
  • FORMAT_VERSION 5 → 6: a version 5 binary would cut a segment at the first head that has the new field. Bumping the version makes it set the segment aside instead. Version 6 reads version 5, so an upgrade keeps all persisted windows.

Decisions and open questions

  • Needs reviewer decision: None.
  • Decided (2026-11-30, coordinator review):
  • Decided (2026-11-30, design doc §8, decisions 30–34): persisted marks (30); reorder buffer (31); at-least-once via exporter retry, no resync request (32); KLL delta = partial sketch, a recorded departure from the reference protocol (33); the lineage frame, with the epoch staying in the metric metadata (34).

Verification

Remote runner (an eval host), with an rsynced copy and its own target dir. Formatting was checked locally.

Scope Result Newly failing Notes
cargo fmt --check pass –
cargo clippy -p common -p otlp -p storage -p precompute -p data_plane --all-targets -- -D warnings pass –
common lib 2 passed none the frame's byte vectors and its refusals
otlp 18 passed none lineage_frames_are_decoded_and_checked replaces the attribute test
storage lib 56 passed none new: incomplete_marks_are_indexed_after_a_restart, a_version_5_segment_reads_with_no_marks; an_older_format_version_segment_is_set_aside now uses version 4
precompute lib 16 passed none new: records_before_their_base_wait_for_it, a_base_missing_past_the_reorder_wait_marks_its_window_incomplete
data_plane --test collector_views 20 passed none new: records_out_of_order_answer_as_in_order_ones, an_incomplete_window_stays_incomplete_after_a_restart; on a manual clock: a_gap_leaves_the_window_unanswered, a_restarted_collector_resynchronizes_with_a_full (held, then incomplete after the wait)
data_plane --test view_persistence --test view_tiering 8 + 9 passed none the format bump does not affect persisted windows
skew-allowance commits 5c189c78 (test) → 60127a2e failing first: the_idle_close_is_the_files_close_delay_plus_skew and a_gap_leaves_the_window_unanswered; then precompute lib 16 and collector_views 20 passed, clippy clean none a_gap_leaves_the_window_unanswered now checks that the window is not yet incomplete at the declared 6 s, only at 8 s
failing-first commit 03406863 common 2, otlp 3, storage 1, precompute 3, collector_views 19 failed – as intended (stubs)

New tests:

  • records_before_their_base_wait_for_it: deltas 3, 2 and a new epoch's delta are held and applied in order once deltas 1 and the full arrive; a repeated held record is a duplicate; an open window with its delta before its full.
  • a_base_missing_past_the_reorder_wait_marks_its_window_incomplete: nothing is marked before the wait; at the wait the window is marked once and its held records are dropped; a base arriving later is not applied; other windows are untouched.
  • incomplete_marks_are_indexed_after_a_restart: stored and unstored windows; a later revision of the window; a mark past retention is dropped; marks are taken once.
  • records_out_of_order_answer_as_in_order_ones / an_incomplete_window_stays_incomplete_after_a_restart: end to end over HTTP, including a restart over the view files.

Bytes per state (collector side, ASAPCollector-public#64, same input): no lineage 215.9 B, attributes 264.6 B, frame 219.7 B.

Follow-ups

  • Deploy together with ASAPCollector-public#64: this backend refuses bare ASAPv1 states under an epoch, and the old backend refuses framed states.

🤖 Generated with Claude Code

https://claude.ai/code/session_01K1M6bo8kYWd83zXwh2Nobf

zzylol and others added 2 commits November 30, 2026 15:57
…neage frame (failing first)

Tests for decisions 30, 31 and 34 of the design doc (section 8), with
stubs for the new interfaces:

- common: the lineage frame's pinned bytes for sequences 0, 1, 300 and
  u64::MAX (the collector's encoder holds the same vectors) and each
  refusal with its reason.
- otlp: a state's sequence and kind come from the frame in
  sketch.envelope, the record's bytes are those behind it, and a bare
  ASAPv1 state, frame version 2, or a self-contradicting frame refuses
  the request.
- storage: incomplete marks are indexed after a restart (stored windows
  or not, whatever revision is read later, not past retention); a
  version 5 segment reads with no marks.
- precompute: records before their base wait for it and apply in order
  (closed and open windows, a new epoch's delta before its full); a base
  still missing past the reorder wait marks the window incomplete, not
  before; the reorder wait is the file's close delay.
- data plane: a gap is held, then incomplete after the close delay;
  records out of order in separate requests answer as in-order ones; an
  incomplete window stays incomplete after a restart over the view files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6bo8kYWd83zXwh2Nobf
…neage frame

Decisions 30, 31 and 34 of the design doc (section 8):

- Reorder buffer (31): a delta whose base has not been applied (a later
  sequence, or a new epoch's delta before its full) is held per lineage
  and sequence, and applied as soon as its base is, with the records held
  after it. A record held for the collector's declared close delay
  (CollectorViewOptions::reorder_wait_ms, the file's
  window_close_delay_ms; 6 s by default) with its base still missing
  marks its window incomplete (Staging::expire_held, every ingest pass);
  the window's other held records are dropped. New outcome "held".
- Persisted incomplete marks (30): Precompute::mark_incomplete returns
  the marks, the committer queues them to the persist thread
  (PersistPool::queue_incomplete), which appends a marks-only record
  (RecordHead::incomplete) per retention; ViewFiles::index collects them
  within retention and startup re-marks the store. FORMAT_VERSION 5 -> 6
  so a version 5 data plane sets the new segments aside rather than
  cutting them at the first new head; version 5 segments still read.
- Lineage frame (34): the otlp adapter reads the record's sequence and
  kind from the frame in front of its ASAPv1 bytes in sketch.envelope
  (common::unframe_lineage) instead of three point attributes; a bare
  ASAPv1 state, another frame version or a self-contradicting frame
  refuses the request. The attribute constants are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6bo8kYWd83zXwh2Nobf
zzylol and others added 2 commits November 30, 2026 16:24
…failing first)

The reorder wait is to be the declared window close delay plus the same
2 s clock-skew allowance as the idle close: the file's 3 s delay gives
a 5 s wait, the default 8 s. In the data plane, a held record is not yet
incomplete at the declared delay alone (6 s), only at 8 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6bo8kYWd83zXwh2Nobf
A held record's window is marked incomplete after the declared window
close delay plus the same 2 s clock-skew allowance as the idle close
(CollectorViewOptions::reorder_wait_ms: the file's delay + 2 s, default
8 s), for consistency with how long a stopped collector's windows wait.

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