Skip to content

collector_views: carry the lineage in a binary frame in sketch.envelope (48.7 → 3.8 B per state) - #64

Draft
zzylol wants to merge 2 commits into
perf/collector-views-series-internfrom
feat/collector-views-lineage-frame
Draft

zzylol wants to merge 2 commits into
perf/collector-views-series-internfrom
feat/collector-views-lineage-frame

Conversation

@zzylol

@zzylol zzylol commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Review guide

Behavior: before → after

The input is the golden test's 12 s stream of q01 (DDSketch α 0.05, 5 s windows). The collector emits three OTLP messages of 80 on-time states each, every one a full of epoch 42.

#61 (attributes) this PR (frame)
point attributes per state labels, sketch.envelope (ASAPv1), asap.sequence=0, asap.state_kind="full" labels, sketch.envelope = A1 00 00 + ASAPv1
a delta (seq 3) + asap.base_sequence=2 A1 01 03 02 + ASAPv1
metric metadata + asap.producer_epoch unchanged
bytes per state (OTLP protobuf; no lineage = 215.9) 264.6 (+48.7) 219.7 (+3.8)
q01 otlp-body.pb (1 state; no lineage = 669) 781 724
bare ASAPv1 state under a metric with an epoch (lineage = none) refused: "a bare ASAPv1 state without a lineage frame"

This serves the collector-views evaluation: the cost of delta transmission should be bytes per state, not tens of bytes per state.

Interfaces

envelope.rs

 /// A collector view state's place in its lineage (stays).
 pub struct Lineage {
     pub producer_epoch: u64,
     pub sequence: u64,
 }
+/// The first byte of a version 1 lineage frame: marker nibble 0xA, version 1.
+pub const LINEAGE_FRAME_V1: u8 = 0xA1;
 impl Lineage {
     pub fn base_sequence(&self) -> Option<u64>;
+    /// `asapv1` behind this record's lineage frame: the bytes of its `sketch.envelope`.
+    pub fn frame(&self, asapv1: &[u8]) -> Vec<u8>;
+    /// A framed state's sequence and its ASAPv1 bytes; Err (with the reason) for a bare
+    /// ASAPv1 state, another frame version, unknown flags, a bad varint, or a kind that
+    /// disagrees with its sequence and base.
+    pub fn unframe(bytes: &[u8]) -> Result<(u64, &[u8]), String>;
 }

otap/schema.rs

 pub const META_PRODUCER_EPOCH: &str = "asap.producer_epoch";
-pub const ATTR_SEQUENCE: &str = "asap.sequence";
-pub const ATTR_STATE_KIND: &str = "asap.state_kind";
-pub const ATTR_BASE_SEQUENCE: &str = "asap.base_sequence";
-pub const STATE_KIND_FULL: &str = "full";
-pub const STATE_KIND_DELTA: &str = "delta";

These constants are removed because the frame replaces the attributes. The wire format change is also the interface: see the frame layout in Summary and in docs/data_model.md.

Usage (the processor's path; checked by view_identity_is_otlp_metric_metadata and emitted_states_match_the_golden_bytes):

// collector-views.json = q01 (DDSketch α 0.05, 5 s windows on `data`); epoch 42 drawn at start.
let mut executor = ViewExecutor::with_epoch(&views, 42)?;
// The window (T-5 s, T] closes: one full per series, lineage {producer_epoch: 42, sequence: 0}.
let closed = executor.flush();
// encode: metric `data.asap_view.1`, metadata asap.producer_epoch = "42"; each point's
// sketch.envelope = Lineage::frame(asapv1) = [A1 00 00] ++ asapv1 (3 bytes more than the state).
let pdata = encode_envelopes_to_pdata(&closed.states)?;
// ASAPQuery-backend (or this codec's decoder) reads it back: Lineage::unframe gives
// (sequence 0, asapv1), the epoch comes from the metadata, and the labels hold no lineage.

Algorithm changes

None. The emission order, the sequences and the delta contents are #61's. Only their encoding changes.

Decisions and open questions

  • Needs reviewer decision: None.
  • Decided (2026-11-30, coordinator review): the frame stays a prefix inside sketch.envelope, with no sketchlib change. This PR deploys together with Collector views: reorder buffer, persisted incomplete marks, lineage frame in sketch.envelope ASAPQuery-backend#897: that backend refuses an unframed state under a producer epoch, and the older backend (#893) refuses framed states.
  • Decided (2026-11-30, recorded in ASAPQuery-backend's design doc, decision 34):
    • The lineage is a frame in front of the ASAPv1 bytes, not part of ASAPv1. Rejected: putting it into sketchlib's ASAPv1 metadata. That would mean a sketchlib change in every sketch's wire format, and ASAPv1 is a sketch's state, which the backend also stores. The lineage belongs to one transmission.
    • The producer epoch stays metric metadata. It is the same for every point of the metric, so it costs about 40 B per metric. As a random u64 in the frame it would be a 10-byte varint per state, which is cheaper only for metrics with fewer than about four states. Here a message has 80 states in 2 metrics.
    • The base is kept in the frame even though it is always sequence − 1. The approved design asks for sequence and base. It costs one byte per delta and lets the receiver check the record against itself.
    • Versioning. The first byte's high nibble 0xA marks a frame and the low nibble is its version. ASAPv1 starts with b'A' (0x41), so a bare state from an older collector is refused explicitly and never misread. A reader refuses a version it does not know.

Verification

Run locally in a separate target dir, which has since been deleted.

Scope Result Newly failing Notes
cargo fmt --check pass –
cargo clippy --all-targets --features otap-engine,stream-benchmark -- -D warnings pass –
cargo test --features otap-engine --lib 163 passed, 1 ignored none the ignored test was already ignored on the base
--test collector_views_e2e --test otap_pipeline_e2e --test runtime 2 + 1 + 32 passed none
failing-first commit 8852ebb 5 lib + 2 e2e failed – as intended

New or changed tests:

  • lineage_frames_are_these_bytes_and_round_trip pins the frame bytes for sequences 0, 1, 300 and u64::MAX. ASAPQuery-backend's decoder holds the same vectors.
  • bad_lineage_frames_are_refused checks each refusal and its reason: a bare ASAPv1 state, version 2, unknown flags, a truncated or overflowing varint, a full with a sequence, and a delta whose base is wrong.
  • a_bad_or_missing_lineage_frame_is_refused_on_decode replaces an_inconsistent_lineage_is_refused_on_decode. It covers a bad frame, a bare state under an epoch, and a frame without an epoch.
  • view_identity_is_otlp_metric_metadata: the point has no lineage attributes, and its envelope is A1 00 00 + ASAPv1.
  • emitted_states_match_the_golden_bytes strips the epoch and the frame. The golden file is unchanged.
  • q01_otlp_body_matches_golden: otlp-body.pb was regenerated (781 → 724 bytes).

Measurement: the same 12 s stream and the same three messages. Bytes are the encoded OTLP protobuf; per state = total / 240.

message bytes (3 messages) per state
no lineage (golden, stripped) 17,527 + 17,596 + 16,686 215.9
#61 attributes 21,425 + 21,494 + 20,584 264.6
this PR, frame 17,842 + 17,908 + 16,986 219.7

Follow-ups

🤖 Generated with Claude Code

https://claude.ai/code/session_01K1M6bo8kYWd83zXwh2Nobf

zzylol and others added 2 commits November 30, 2026 15:39
…irst)

Tests for moving a state's lineage out of the OTLP point attributes and
into a binary frame in front of its ASAPv1 bytes in `sketch.envelope`:

- envelope: pinned frame bytes for sequences 0, 1 and 300 and their round
  trip (the same vectors as ASAPQuery-backend's decoder), and refusals with
  their reason (a bare ASAPv1 state, frame version 2, unknown flags, a
  truncated or overflowing varint, a kind that disagrees with its sequence
  and base).
- codec: the q01 state's point carries no lineage attributes and its
  sketch.envelope is `A1 00 00` + the ASAPv1 bytes; a bad frame, a bare
  ASAPv1 state under a metric with a producer epoch, and a framed state
  under a metric without one are refused.
- collector_views_e2e: the golden comparison strips the epoch metadata and
  the full's 3-byte frame; the q01 wire fixture otlp-body.pb is the framed
  body (781 -> 724 bytes).

Lineage::frame and Lineage::unframe are stubs here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6bo8kYWd83zXwh2Nobf
A collector view state's sequence and kind move from three OTLP point
attributes (asap.sequence, asap.state_kind, asap.base_sequence) into a
binary lineage frame in front of its ASAPv1 bytes in sketch.envelope:

    [ 0xA1 | flags:u8 (bit 0: delta) | sequence:uleb128
           | base_sequence:uleb128 (deltas only) | ASAPv1 bytes ]

0xA1 is the frame's marker (0xA) and version (1); an ASAPv1 envelope
starts with b'A' (0x41), so the two never collide. A full adds 3 bytes,
a delta below sequence 128 adds 4. The decoder refuses a bare ASAPv1
state under a metric with asap.producer_epoch (a collector from before
the frame), another frame version, unknown flags, and a kind that
disagrees with its sequence and base.

asap.producer_epoch stays Metric metadata: it is the same for every point
of the metric, so it costs ~40 bytes per metric instead of a 10-byte
varint (a random u64) per state; it would only be cheaper in the frame for
metrics of fewer than about four states.

Bytes per state, same input (the golden test's 12 s stream, three
messages of 80 states, OTLP protobuf):
  no lineage            215.9 B
  lineage attributes    264.6 B (+48.7, +22.6%)
  lineage frame         219.7 B (+3.8, +1.8%)
The q01 one-state wire fixture goes 781 -> 724 bytes (669 without
lineage).

The golden states file is unchanged: its test strips the epoch and the
full's 3-byte frame, leaving the pre-lineage messages. The schema
constants for the three attributes are removed.

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

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant