Skip to content

refactor: split internal-metrics and storage crates out of data-plane (layout step 3) - #877

Draft
zzylol wants to merge 5 commits into
refactor/layout-folder-movesfrom
refactor/layout-storage-crates
Draft

zzylol wants to merge 5 commits into
refactor/layout-folder-movesfrom
refactor/layout-storage-crates

Conversation

@zzylol

@zzylol zzylol commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: crates/data-plane is still one crate. Its stores, its on-disk view format and its self-measurements live next to the ingest, query and HTTP code. Nothing stops a store from reaching into the engine, or a measurement type from reaching into the stores.
  • Change: step 3 of docs/design/repo-layout.md (docs(design): repository layout and engine boundaries #874). It adds two leaf crates, internal-metrics and then storage, and moves their files out of data-plane. Every caller imports from the new crates. storage does not depend on data_plane.
  • Effect: behavior is unchanged. The same binaries, examples, CLI flags, /metrics text and test results. For example, cargo run --release -p data_plane --example tier_bench still works; ViewRead now comes from storage::persist.

Review guide

  • Base: refactor/layout-folder-moves (refactor: move folders to the layout in docs/design/repo-layout.md (layout step 2) #876). This is a stacked draft.
  • Commits: the first is git mv only, so history follows each file. The second makes the crates build. The third adds the new packages to the CI format check. The fourth is the first review's fixes; the fifth the whole-stack review's (below).
  • Read first: crates/storage/Cargo.toml, crates/internal-metrics/Cargo.toml, crates/data-plane/Cargo.toml, crates/data-plane/src/metrics_endpoint.rs, and the Shared / render_metrics / serve_view_cache hunks of engine.rs.
  • Mechanical: import paths (crate::store → storage::store, crate::metrics → internal_metrics, …) in data-plane/src, data-plane/tests, benchmarks/micro.
  • GitHub shows metrics.rs → metrics_endpoint.rs as the rename and internal-metrics/src/lib.rs as a new file, because the larger half of the file stayed. In the first commit, metrics.rs is a pure rename to internal-metrics/src/lib.rs. metrics_endpoint.rs is that file's Gauges, render, escape, resident_bytes and its render test, unchanged except as listed below.

Old → new

Old (crates/data-plane/…) New
src/metrics.rs: thread_cpu_ns, Timer, PhaseStats, Latency, WindowLatency, QueryMetrics, PushMetrics, Metrics, thread_cpu_seconds crates/internal-metrics/src/lib.rs (package internal_metrics)
src/metrics.rs: Gauges, render, escape crates/data-plane/src/metrics_endpoint.rs (private module)
src/metrics.rs: resident_bytes internal_metrics::resident_bytes (with the other /proc readers)
src/{store,window_cache,memory_budget,segment,persist,state_codec}.rs crates/storage/src/ (package storage); state_codec stays a private module
src/series_ids.rs crates/storage/src/series_ids.rs (see Decisions)
ingestion::millis, ingestion::window_end storage::store::millis, storage::store::window_end
engine.rs: serve_view_cache (the view-cache thread's body) storage::memory_budget::serve_view_cache
Metrics::view_tier Gauges::view_tier, held by the engine's Shared::view_tier
tests/view_cache_charge.rs, tests/series_ids.rs crates/storage/tests/
metrics::tests::records_and_renders_phases split: the CPU assertion → internal_metrics::tests::a_phase_records_its_thread_cpu; the render assertions stay in metrics_endpoint::tests::renders_phases_and_queries

Dependency graph

control_plane ──► asap-planner
data_plane ──► storage ──► internal_metrics
     │            └──────► asap-executor, asap_sketchlib
     ├──► internal_metrics
     └──► asap-planner, asap-executor
(dev) data_plane ──► control_plane, asap_sketchlib (view_read_bench)

storage depends on internal_metrics, asap-executor (its DataStore trait, IR and kernels), asap_sketchlib (sketch encodings), and serde, serde_json, serde_bytes, rmp-serde, zstd, ahash, libc, futures, tracing and clap. internal_metrics depends on libc only. libc is only a dev-dependency of data_plane (the benchmarks' posix_fadvise), now that the /proc resident-memory reader is in internal_metrics. data_plane drops serde_bytes, zstd and rmp-serde. asap_sketchlib moves to its dev-dependencies, because only the view_read_bench example uses it now. Cargo.lock gains the two packages and these dependency edges, and no versions change.

Visibility changes

All other items keep the visibility they had in data_plane:

Item Before After Why
storage::store::RawData::read pub(crate) pub data_plane::push reads the raw store
storage::store::PartitionReads (type only; new stays pub(crate)) pub(crate) pub it appears in RawData::read's signature
storage::store::millis pub(crate) (in ingestion) pub ingestion and engine use it
storage::memory_budget::serve_view_cache private (in engine) pub engine spawns the view-cache thread
storage::memory_budget::monitored_target pub private only serve_view_cache, now in the same module, calls it
Gauges, render (data_plane::metrics_endpoint) pub in pub mod metrics pub(crate) in a private module only engine calls them
PhaseStats::render, Latency::render, PushMetrics::render, PushMetrics::render_gauges private pub metrics_endpoint::render calls them
data_plane::{store,persist,segment,window_cache,memory_budget,series_ids,metrics} pub mod removed nothing is re-exported. Callers import storage::… and internal_metrics::…

engine.rs

  • Moved: the view-cache monitor (serve_view_cache) to storage::memory_budget, next to the cgroup reads and monitored_target it calls. Its body is unchanged. engine.rs still spawns it as the view-cache thread, because spawning is part of EngineHandle::spawn.
  • Deferred to step 5: the persist-N threads (serve_persist, release_persist_queue, PersistJob, PersistBarrier, PersistPool). serve_persist takes the engine's Shared (its Metrics, persist_queue and persist_backlog), and PersistJob::Barrier carries advance_watermark's reply channel (PersistBarrier). To move them, storage would need a new interface for the backlog accounting, so they stay until step 5 splits precompute. view_tier(&EngineOptions) also stays, because it reads EngineOptions.

Behavior: before → after

No change. /metrics writes the same lines in the same order. The view-tier block now reads the tier from Gauges instead of Metrics. The thread names, CLI flags, defaults and file formats are the same.

Interfaces

 // data_plane
-pub mod memory_budget; pub mod metrics; pub mod persist; pub mod segment;
-pub mod series_ids; mod state_codec; pub mod store; pub mod window_cache;
+mod metrics_endpoint; // Gauges, render

 // internal_metrics (new crate; former data_plane::metrics minus Gauges/render)
 pub struct Metrics {
     …
-    pub view_tier: OnceLock<Arc<crate::store::ViewTier>>,
 }

 // data_plane::metrics_endpoint
-pub struct Gauges<'a> {
+pub(crate) struct Gauges<'a> {
     …
-    pub query_time_results: &'a QueryTimeResults,
+    pub query_time_results: &'a QueryTimeResults<Unanswered>,
+    pub view_tier: Option<&'a ViewTier>,
 }

 // storage::store (new crate; former data_plane::store)
-pub struct QueryTimeResults<E = crate::query::Unanswered>
+pub struct QueryTimeResults<E>
+pub fn millis(value: u64) -> i64              // from data_plane::ingestion
+pub fn window_end(timestamp_ms: i64, width_ms: i64) -> i64   // from data_plane::ingestion

 // storage::memory_budget (former data_plane::memory_budget)
+pub fn serve_view_cache(tier: Arc<ViewTier>, budget: Option<u64>, running: Arc<AtomicBool>)  // from engine.rs
-pub fn monitored_target(…)
+fn monitored_target(…)

QueryTimeResults loses its default type parameter because Unanswered is the query engine's error, which storage cannot name. Its users write QueryTimeResults<Unanswered>: QueryEngine, EngineHandle::query_time_results and Gauges.

Algorithm changes

None.

Decisions and open questions

Needs reviewer decision: none.

Decided (judgment calls made in this PR):

  • Gauges and render stay in data-plane. render reads IngestionStats (precompute), ResultCache (query), and QueryTimeResults and ViewTier (storage). If it moved to internal_metrics, that crate would depend on all three, and the design wants it to be a leaf. It is also a cycle today: store uses PhaseStats and Timer. A snapshot struct of plain numbers would be a new interface. So the exporter stays where the design puts wiring, and the measurement types move.
  • Metrics::view_tier → Gauges::view_tier. It was the only field of Metrics that named a store type, and only render read it. The engine now keeps the tier in Shared and passes it to render.
  • series_ids.rs goes to storage, not precompute. The design's table puts it in precompute. But store, segment and persist use Series and the interner, and storage must not depend on precompute. By the design's helper rule ("a helper two crates use goes in the lower of the two"), it goes to storage. Step 4 may move it to common.
  • millis and window_end go to storage::store. By the same rule: store and persist use them, and so does ingestion. window_end was already pub. Step 4 may move them to common with the other time helpers.
  • storage depends on clap. ViewRead derives clap::ValueEnum for --view-read. Removing that derive would need a mirror enum in the binary, so the dependency stays.
  • The metrics unit test splits in two. Its CPU assertion reads a private field of PhaseStats. That assertion now lives in internal_metrics. The render assertions stay with render, so no field becomes pub for a test.

Verification

Both trees were built and tested on the same host, with one target directory each and ulimit -n raised (see #876).

Scope #876 head this PR Newly failing
cargo fmt --all -- --check pass pass —
cargo build --workspace --all-targets --locked (all 8 micro benchmarks included) pass pass —
cargo clippy --workspace --all-targets --locked -- -D warnings pass pass —
cargo test --workspace --locked 189 passed, 0 failed, 3 ignored (31 suites) 190 passed, 0 failed, 3 ignored (35 suites) none

The four new suites are the unit tests and doc tests of internal_metrics and storage. The sorted lists of passing test names (test … ok) differ only by the planned test split:

-metrics::tests::records_and_renders_phases
+metrics_endpoint::tests::renders_phases_and_queries
+tests::a_phase_records_its_thread_cpu        (internal_metrics)

Every other test keeps its name, because the moved modules keep their names (store::tests::…, persist::tests::…). The two moved integration tests also keep their binary names (view_cache_charge, series_ids).

An independent review then checked the diff for non-move changes, visibility that is wider than needed, dependency direction, duplicated code and dead code. It found nothing blocking. The fourth commit applies its fixes:

  • serve_view_cache moves next to the cgroup code in memory_budget, so monitored_target is private.
  • Gauges and render are pub(crate).
  • The internal_metrics crate doc no longer claims the gauges.
  • The render test is renamed, the two storage::store imports in engine.rs are merged, and the asap_executor imports join the external group.
  • view_tiering.rs's header is rewrapped.

The format, build, clippy and test gate above ran on that final head.

Whole-stack review fixes (fifth commit)

  • CI checks formatting with cargo fmt --all -- --check instead of a hand-written -p list, so a new crate is checked too (the README and pre-commit already run it). Later PRs no longer edit the list.
  • resident_bytes moves to internal_metrics; libc becomes a dev-dependency of data_plane.
  • pub(crate) for storage items used only inside it: segment::{FORMAT_VERSION, is_sealed, max_end_ms, forget_after}, persist::ViewIndex (and the FORMAT_VERSION re-export), the state and text-key codecs (CompactKind, encode_states, decode_states, encode_text_keys, decode_text_keys), series_ids::labels_of_identity, WindowCache::{get_or_load, evict_idle}. Types other crates reach through a pub method (WindowLocation, WindowSlot, Written, CgroupMemory, …) stay pub.
  • README: this PR adds the crates/storage/ and crates/internal-metrics/ rows to the layout table.
  • Re-verified on the restacked head (f93e5e7): the gate above.

Follow-ups

@zzylol zzylol added the area: data plane Runtime ingestion, storage, and query-serving data paths label Nov 26, 2026
@zzylol zzylol added the enhancement New feature or request label Nov 27, 2026
@zzylol zzylol changed the title refactor: split internal-metrics and storage crates out of data-plane refactor: split internal-metrics and storage crates out of data-plane (layout step 3) Nov 27, 2026
git mv only, so the history follows each file; the next commit makes the
crates build.
Step 3 of docs/design/repo-layout.md. `internal_metrics` holds the
measurement types (phase timers, latency histograms, the engine's
`Metrics`); `storage` holds the raw and view stores, the window cache,
the memory budget, segments, persistence, state codecs and the series
interner, and the view-cache monitor thread's body from engine.rs.
`data_plane` depends on both; `storage` depends on `internal_metrics`
and `asap-executor`, never on `data_plane`.

The `/metrics` renderer and its `Gauges` stay in data-plane
(`metrics_endpoint.rs`) because they read ingestion, query and store
state. The view tier moves from `Metrics` to `Gauges`. `millis` and
`window_end` move to `storage::store`, the lower crate that uses them.
The persist-N threads stay in engine.rs until step 5.
serve_view_cache sits in memory_budget next to what it calls, so
monitored_target is private; Gauges and render are pub(crate); the
internal_metrics crate doc says what it holds now.
…cs split

- CI checks formatting with cargo fmt --all, so a new crate is checked
  too (the README and pre-commit already run it).
- The /proc resident-memory reader moves to internal-metrics, next to
  the other /proc readers; libc is only a dev-dependency of data-plane
  (the benchmarks' posix_fadvise).
- storage items used only inside storage are pub(crate): the segment
  format version and Segment's seal/end/forget helpers, the persisted
  view index, the state and text-key codecs, labels_of_identity, the
  window cache's get_or_load and evict_idle.
@zzylol
zzylol force-pushed the refactor/layout-folder-moves branch from 7c0fce8 to 373e0a0 Compare November 28, 2026 00:56
@zzylol
zzylol force-pushed the refactor/layout-storage-crates branch from 5c7cf0f to f93e5e7 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant