Skip to content

feat(common): engine boundary types and the fallback trait (layout step 4) - #878

Draft
zzylol wants to merge 3 commits into
refactor/layout-storage-cratesfrom
refactor/layout-common
Draft

zzylol wants to merge 3 commits into
refactor/layout-storage-cratesfrom
refactor/layout-common

Conversation

@zzylol

@zzylol zzylol commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: the data plane has no boundary between its parts. Adapters, engines and stores call each other's concrete types, so step 5 (split precompute and query) and steps 6–8 (adapters) have no interface to meet at.
  • Change: step 4 of docs/design/repo-layout.md (docs(design): repository layout and engine boundaries #874). A new crate, crates/common (package common), holds the boundary types and the one boundary trait, FallbackClient, with the names of ASAPQuery's engine-boundary design (docs(query-engine): design engine boundaries for ASAPPlanner plans ASAPQuery#935). It has no implementations, and no crate uses it yet.
  • Effect: behavior is unchanged. For example, step 5's query crate answers with common::EngineQueryResult and QueryResult, and steps 6–8's query adapters implement common::FallbackClient.

Review guide

  • Base: refactor/layout-storage-crates (refactor: split internal-metrics and storage crates out of data-plane (layout step 3) #877). This is a stacked draft.
  • Commits:
    1. The crate. It also adds the workspace member, the asap-types and async-trait workspace dependencies and the Cargo.lock entry.
    2. The Planner sync script now rewrites three pins (asap-types added) and updates three packages. Its test and README follow.
    3. Whole-stack review fixes: the engine and store traits are dropped (see Decisions), and the README's Planner pin is corrected.
  • Read first: crates/common/src/lib.rs (the public surface), then query_engine.rs and fallback.rs.
  • Mechanical: Cargo.lock, the workflow line, the sync script and its test.

Behavior: before → after

No change. Nothing calls common yet.

Interfaces

All of this is new, in crates/common:

+// Dependencies: asap-planner, asap-types (same rev as asap-executor), async-trait, thiserror
+pub use asap_planner::{InstallablePlan, PhysicalASAPNodeId};
+pub use asap_types::workload::QueryLanguage;
+pub type LabelSet = BTreeMap<String, String>;
+
+// Requests and results
+pub struct QueryRequest { pub language: QueryLanguage, pub query: String, pub eval: QueryEval }
+pub enum QueryEval { Instant { time_ms: i64 }, Range { start_ms: i64, end_ms: i64, step_ms: i64 } }
+pub enum QueryResult { Vector(InstantVector), Matrix(RangeVector), Scalar(Sample), Table(Table) }
+pub struct InstantVector { pub timestamp_ms: i64, pub series: Vec<InstantVectorElement> }
+pub struct InstantVectorElement { pub labels: LabelSet, pub value: f64 }
+pub struct RangeVector { pub series: Vec<RangeVectorElement> }
+pub struct RangeVectorElement { pub labels: LabelSet, pub samples: Vec<Sample> }
+pub struct Sample { pub timestamp_ms: i64, pub value: f64 }
+pub struct Table { pub columns: Vec<String>, pub rows: Vec<Vec<TableValue>> }
+pub enum TableValue { String(String), Float(f64) }
+
+// Plan
+pub struct PlanId(pub u64);
+pub struct InstalledPlan { pub id: PlanId, pub plan: InstallablePlan }
+pub struct InstallError { pub message: String }
+
+// Query
+pub enum AnswerSource { Planned { plan: PlanId, root: PhysicalASAPNodeId } /* CapabilityMatched later */ }
+pub enum EngineQueryResult { Ready { result: QueryResult }, NotReady }
+pub struct QueryEngineError { pub message: String }
+
+// Fallback
+#[async_trait] pub trait FallbackClient: Send + Sync {
+    async fn execute(&self, request: &QueryRequest, headers: &[(String, String)])
+        -> Result<FallbackResponse, FallbackError>;
+}
+pub struct FallbackResponse { pub status_code: u16, pub content_type: String, pub body: Vec<u8> }
+pub struct FallbackError { pub message: String }

Each error type has a new(message) constructor.

 # scripts/sync-asapplanner-main.sh
-if replacements != 2:
+if replacements != 3:
 cargo update -p asap-planner -p asap-executor \
+  -p asap-types \
   --precise "$planner_rev"

Mapping to #935

Item #935 decision
common names plan types only, no executor runtime 1
QueryRequest.language; QueryLanguage from asap-types 6, 22
QueryEval times in i64 ms 7
QueryResult::{Scalar, Table} 14 (adapters reject unsupported variants: 29)
Label names inside QueryResult (LabelSet per series) 31
PlanId counter 38
InstalledPlan { id, plan } 42
AnswerSource::Planned { plan, root }; CapabilityMatched later 4, 25, 35; T2
EngineQueryResult::{Ready, NotReady} 35, 36
QueryEngineError a single type I12
FallbackClient takes QueryRequest "Kept unchanged", 3

Deviations from #935

Deviation Reason
QueryEval, not an unnamed eval enum (the design doc says Eval) ASAPQuery's own QueryRequest code (ProjectASAP/ASAPQuery#937) names it QueryEval, so adapters moved from there compile unchanged
QueryResult element structs carry labels: LabelSet (names and values). ASAPQuery's carry KeyByLabelValues (values only), timestamp: u64 and a values field Decision 31 puts label names inside the result. PromQL series in one result can have different label sets, so one name list per result does not fit. The fields follow the naming rule (timestamp_ms: i64 per decision 7; series holds series). Moved adapters map these fields
FallbackClient::execute(request, headers), FallbackResponse { status_code, content_type, body }, FallbackError. ASAPQuery has execute_query, execute_query_with_headers, FallbackResponse::{Json, Text} and StatusCode errors No HTTP crate in common, so a status code is a u16 and headers are (name, value) pairs. The backend's proxy forwards the client's headers and returns the backend's status, content type and bytes unchanged. JSON and TSV fit that form, and no parse is needed. One method with a headers slice covers both of ASAPQuery's methods. get_runtime_info waits for T4
Async methods via async-trait The query routes hold their fallback client as Arc<dyn FallbackClient> (#935's pseudocode). Native async fn in traits is not object safe on this toolchain (1.98)
No IngestEngine, QueryEngine, ViewStore or RawDataStore trait (decisions 2, 8, 13, 43–47 name them), and no RawDataBatch, IngestError, ViewId or ViewInstance* Each has one implementation in this backend, so the engines and stores are concrete types; a trait is added with its second implementation (repo-layout decision 4). The engines' install and the query engine's execute are inherent methods in step 5

Algorithm changes

None. The crate has no logic.

Decisions and open questions

Needs reviewer decision: none.

Decided (judgment calls made in this PR):

  • Dependencies: asap-planner plus asap-types, not asap-executor. InstallablePlan is defined only in asap-planner, which re-exports PhysicalASAPDAG, PhysicalASAPNodeId and RawDataRetention. QueryLanguage and Source are only in asap-types. asap-types is added at the same rev and the sync script keeps all three pins together. common names no executor item. asap-planner itself depends on asap-executor and the frontends, so they still compile as transitive dependencies. Dropping that would mean replacing InstalledPlan.plan with its DAG and query texts (a deviation from decision 42), so this PR keeps decision 42.
  • No engine or store traits (whole-stack review). The first version of this PR had IngestEngine, QueryEngine, ViewStore and RawDataStore. Across the stack, each had exactly one implementation; storage::boundary (the only ViewStore/RawDataStore implementations) had no production caller, nor did IngestEngine::ingest, shutdown or QueryEngine::execute_batch; and the only dyn engine use was PlanInstaller's, which existed for a test fake. So they are deleted with their types (and the Source and RawDataRetention re-exports only they named); storage keeps its own state codecs. QueryRequest, the result types and FallbackClient stay.
  • QueryOutcome and NotAnswerableReason wait for data-plane (step 5). Only QueryDispatcher and the HTTP routes that call it use them. Adapters never see them. If step 6's HTTP adapter turns out to need them, they move here then.
  • No PhysicalASAPDAG re-export. No signature names it. Callers reach it as InstallablePlan.dag.
  • LabelSet is a type alias for BTreeMap<String, String>, the shape storage::series_ids::Labels wraps. A sorted map gives one order for each label set.
  • AnswerSource::CapabilityMatched is a comment, not a variant. Adding it later will break exhaustive matches, which is the point.

Verification

Run on one remote host with ulimit -n raised, as in #877.

Scope #877 head this PR Newly failing
cargo fmt --all -- --check pass pass —
cargo clippy --workspace --all-targets --locked -- -D warnings pass pass —
cargo test --workspace --locked 190 passed, 0 failed, 3 ignored (35 suites) 191 passed, 0 failed, 3 ignored (38 suites; forward_mode::bodies_wait_for_a_raw_store_that_is_down and planner_executor_parity::push_precompute_stores_the_window_paths_view_rows failed once under load, timing-dependent, and passed alone, 3/3 and 1/1) none
python3 -m unittest discover -s scripts/ci -p 'test_*.py' 5 passed 5 passed none

The only new test is the_fallback_client_is_usable_as_a_trait_object (tests/trait_shapes.rs): it implements FallbackClient outside the crate with a trivial fake and holds it as Arc<dyn FallbackClient>.

An independent review covered over-abstraction, naming, fidelity to #935, dependency direction, correctness, and dead code; a later whole-stack review dropped the engine and store traits (see Decisions). Fixes in this head:

  • FallbackClient::execute takes the client's headers. Without them, step 6 would lose the header forwarding the proxy does today.
  • LabelSet is at the crate root.
  • The dependency comments say what asap-planner brings in transitively.
  • README: the Planner pin is 1d527bc for asap-planner, asap-executor and asap-types (it said 737bfb1).

Follow-ups

  • Done: the design doc (docs(design): repository layout and engine boundaries #874, now the stack's base) shows QueryEval, Table(Table), the FallbackClient above, the transitive asap-planner note, and no engine or store traits.
  • QueryOutcome and NotAnswerableReason live in data-plane (step 5).
  • series_ids, millis and window_end stay in storage: no common item needs them.

@zzylol zzylol added enhancement New feature or request area: data plane Runtime ingestion, storage, and query-serving data paths integration: ASAPPlanner ASAPPlanner API, IR, candidate, and selected-plan integration labels Nov 26, 2026
@zzylol zzylol changed the title feat(common): engine boundary types and traits feat(common): engine boundary types and the fallback trait (layout step 4) Nov 27, 2026
Step 4 of docs/design/repo-layout.md: crates/common holds the data
plane's boundary types and traits (requests and results, ingest, plan,
query, stores, fallback) with the names of ASAPQuery's engine-boundary
design. No implementations and no callers yet. Adds the common package
to the CI format check.
common depends on asap-types directly (QueryLanguage, Source), so the
Planner sync script rewrites three pins and updates three packages.
The whole-stack review found no second implementation of IngestEngine,
QueryEngine, ViewStore or RawDataStore, and no production caller of
IngestEngine::ingest, shutdown or QueryEngine::execute_batch. A trait is
added with its second implementation, so common keeps the types and
FallbackClient (three implementations later in the stack):

- ViewStore, RawDataStore and their types (ViewId, ViewInstance*,
  ViewState), RawDataBatch, IngestEngine and IngestError are deleted,
  with the Source and RawDataRetention re-exports only they named.
- QueryEngine and its batch-of-one tests are deleted; AnswerSource,
  EngineQueryResult and QueryEngineError stay for the query engine's
  inherent execute.
- The trait-shape test checks FallbackClient alone.
- README: the Planner pin is 1d527bc for all three Planner crates.
@zzylol
zzylol force-pushed the refactor/layout-storage-crates branch from 5c7cf0f to f93e5e7 Compare November 28, 2026 00:56
@zzylol
zzylol force-pushed the refactor/layout-common branch from 3f39078 to d784733 Compare November 28, 2026 00:56
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 integration: ASAPPlanner ASAPPlanner API, IR, candidate, and selected-plan integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant