From b65fdd2a916857b8a9414768ef646911ffac7eff Mon Sep 17 00:00:00 2001 From: Austin Barrington Date: Tue, 10 Nov 2026 16:07:00 +0000 Subject: [PATCH 1/5] Tag backfill on the metadata port; StageTimer in telemetry --- .../src/adapters/chdb/native_adapter.rs | 9 ++-- .../src/adapters/http/shard_handlers.rs | 6 +-- .../src/adapters/metadata/rocksdb_meta.rs | 41 ++++++++++++++ .../src/application/ingest_metadata.rs | 53 ++----------------- hyperbytedb/src/application/mod.rs | 1 - .../src/application/peer_ingestion_service.rs | 2 +- hyperbytedb/src/application/query_service.rs | 6 +-- hyperbytedb/src/application/stage_timer.rs | 46 ---------------- hyperbytedb/src/ports/metadata.rs | 49 +++++++++++++++++ hyperbytedb/src/server.rs | 2 +- hyperbytedb/src/telemetry.rs | 44 +++++++++++++++ 11 files changed, 147 insertions(+), 112 deletions(-) delete mode 100644 hyperbytedb/src/application/stage_timer.rs diff --git a/hyperbytedb/src/adapters/chdb/native_adapter.rs b/hyperbytedb/src/adapters/chdb/native_adapter.rs index 26dcd5f..f763d5a 100644 --- a/hyperbytedb/src/adapters/chdb/native_adapter.rs +++ b/hyperbytedb/src/adapters/chdb/native_adapter.rs @@ -53,7 +53,6 @@ use parking_lot::RwLock; use crate::adapters::chdb::catalog; use crate::adapters::chdb::connection_pool::ChdbConnectionPool; use crate::adapters::chdb::session::{SharedSession, execute_connection}; -use crate::application::ingest_metadata::backfill_tag_metadata; use crate::domain::chdb_naming::{ field_column_name, quote_backticks, quoted_series_table_name, quoted_table_name, tag_column_name, unquoted_series_table_name, unquoted_table_name, @@ -543,7 +542,8 @@ impl ChdbNativeAdapter { tag_pairs.push((k.clone(), v.clone())); } } - backfill_tag_metadata(meta, &key.db, &key.rp, &key.measurement, tag_pairs).await + meta.backfill_tag_keys(&key.db, &key.rp, &key.measurement, tag_pairs) + .await } async fn ddl_mutex(&self, key: &TableKey) -> Arc> { @@ -1374,9 +1374,8 @@ impl ChdbNativeAdapter { use crate::domain::prepared_wal::PreparedMeasurementBatch; - let mut stage = crate::application::stage_timer::StageTimer::new( - "hyperbytedb_prepare_measurement_stage_seconds", - ); + let mut stage = + crate::telemetry::StageTimer::new("hyperbytedb_prepare_measurement_stage_seconds"); let ensured = self.ensure_table(key, points).await?; stage.lap("ensure_table"); let origins = vec![origin_node_id; points.len()]; diff --git a/hyperbytedb/src/adapters/http/shard_handlers.rs b/hyperbytedb/src/adapters/http/shard_handlers.rs index c238b50..49f0690 100644 --- a/hyperbytedb/src/adapters/http/shard_handlers.rs +++ b/hyperbytedb/src/adapters/http/shard_handlers.rs @@ -420,8 +420,7 @@ async fn apply_shard_write_batch( } }; - let mut stage = - crate::application::stage_timer::StageTimer::new("hyperbytedb_shard_primary_stage_seconds"); + let mut stage = crate::telemetry::StageTimer::new("hyperbytedb_shard_primary_stage_seconds"); // Check and parse each part; group the good ones by (db, rp). let mut groups: Vec = Vec::new(); for (index, req) in parts.into_iter().enumerate() { @@ -879,8 +878,7 @@ pub async fn handle_shard_query( ) .into_response() }; - let mut stage = - crate::application::stage_timer::StageTimer::new("hyperbytedb_shard_query_stage_seconds"); + let mut stage = crate::telemetry::StageTimer::new("hyperbytedb_shard_query_stage_seconds"); let mut batches = match state.query_port.execute_sql_arrow(&sql).await { Ok(b) => b, Err(e) => return failed(e), diff --git a/hyperbytedb/src/adapters/metadata/rocksdb_meta.rs b/hyperbytedb/src/adapters/metadata/rocksdb_meta.rs index b95e81a..8d9b342 100644 --- a/hyperbytedb/src/adapters/metadata/rocksdb_meta.rs +++ b/hyperbytedb/src/adapters/metadata/rocksdb_meta.rs @@ -2659,6 +2659,47 @@ impl MetadataPort for RocksDbMetadata { mod series_range_tests { use super::*; + /// Backfill adds the keys a measurement lacks and keeps the list sorted. + #[tokio::test] + async fn backfill_adds_missing_tag_keys() { + let dir = tempfile::tempdir().unwrap(); + let meta = RocksDbMetadata::open(dir.path()).unwrap(); + let cpu = MeasurementMeta { + name: "cpu".into(), + tag_keys: vec!["host".into()], + ..Default::default() + }; + meta.register_metadata_batch("db", "autogen", &[cpu], &[]) + .await + .unwrap(); + meta.backfill_tag_keys("db", "autogen", "cpu", vec![("dc".into(), "x".into())]) + .await + .unwrap(); + let got = meta + .get_measurement("db", "autogen", "cpu") + .await + .unwrap() + .unwrap(); + assert_eq!(got.tag_keys, ["dc", "host"]); + } + + /// Backfill for a measurement not yet known creates it with the keys. + #[tokio::test] + async fn backfill_creates_unknown_measurement() { + let dir = tempfile::tempdir().unwrap(); + let meta = RocksDbMetadata::open(dir.path()).unwrap(); + meta.backfill_tag_keys("db", "autogen", "mem", vec![("host".into(), "a".into())]) + .await + .unwrap(); + let got = meta + .get_measurement("db", "autogen", "mem") + .await + .unwrap() + .unwrap(); + assert_eq!(got.tag_keys, ["host"]); + assert!(got.field_types.is_empty()); + } + /// A tag value already on disk is not new after a restart: rewriting /// host=a after one counted it again (SHOW TAG VALUES CARDINALITY 3 for /// a and b) and made it look new to the cardinality limit. diff --git a/hyperbytedb/src/application/ingest_metadata.rs b/hyperbytedb/src/application/ingest_metadata.rs index 04fcc7f..e5e45cd 100644 --- a/hyperbytedb/src/application/ingest_metadata.rs +++ b/hyperbytedb/src/application/ingest_metadata.rs @@ -376,55 +376,6 @@ async fn merged_tag_keys( Ok(tag_keys) } -/// Ensure SHOW TAG KEYS/VALUES indexes reflect tags observed on written points. -/// Merges with existing measurement metadata rather than replacing tag keys. -pub async fn backfill_tag_metadata( - metadata: &Arc, - db: &str, - rp: &str, - measurement: &str, - tags: impl IntoIterator, -) -> Result<(), HyperbytedbError> { - let mut tag_keys: BTreeSet = BTreeSet::new(); - let mut seen: HashSet<(String, String)> = HashSet::new(); - let mut tag_batch: Vec<(String, String)> = Vec::new(); - for (k, v) in tags { - tag_keys.insert(k.clone()); - if seen.insert((k.clone(), v.clone())) { - tag_batch.push((k, v)); - } - } - if tag_keys.is_empty() { - return Ok(()); - } - - let mut meas_updates: Vec = Vec::new(); - if let Some(mut existing) = metadata.get_measurement(db, rp, measurement).await? { - let before_len = existing.tag_keys.len(); - for k in &tag_keys { - if !existing.tag_keys.contains(k) { - existing.tag_keys.push(k.clone()); - } - } - existing.tag_keys.sort(); - if existing.tag_keys.len() != before_len { - meas_updates.push(existing); - } - } else { - meas_updates.push(MeasurementMeta { - name: measurement.to_string(), - field_types: HashMap::new(), - tag_keys: tag_keys.into_iter().collect(), - ..Default::default() - }); - } - - let tag_entries = vec![(measurement.to_string(), tag_batch)]; - metadata - .register_metadata_batch(db, rp, &meas_updates, &tag_entries) - .await -} - /// Load series rows from a chDB `_series` table and persist them to RocksDB for /// `SHOW SERIES` / `SHOW TAG VALUES` (used after MV destination dimension backfill). pub async fn register_series_from_series_table( @@ -488,7 +439,9 @@ pub async fn register_series_from_series_table( metadata .register_series_batch(db, rp, measurement, &entries) .await?; - backfill_tag_metadata(metadata, db, rp, measurement, tag_pairs).await + metadata + .backfill_tag_keys(db, rp, measurement, tag_pairs) + .await } /// Fast-path metadata preparation for columnar batches. diff --git a/hyperbytedb/src/application/mod.rs b/hyperbytedb/src/application/mod.rs index 9e7eadd..15412e2 100644 --- a/hyperbytedb/src/application/mod.rs +++ b/hyperbytedb/src/application/mod.rs @@ -34,7 +34,6 @@ pub mod shard_routing; pub mod shard_scheduler; pub mod shard_transfer; pub mod sharded_mv_backfill; -pub mod stage_timer; pub mod statement_summary; pub mod tag_condition; pub mod wal_append; diff --git a/hyperbytedb/src/application/peer_ingestion_service.rs b/hyperbytedb/src/application/peer_ingestion_service.rs index 6ceb0ba..cbf0c90 100644 --- a/hyperbytedb/src/application/peer_ingestion_service.rs +++ b/hyperbytedb/src/application/peer_ingestion_service.rs @@ -1,7 +1,7 @@ use std::sync::Arc; -use crate::application::stage_timer::StageTimer; use crate::ports::forward_queue::{ForwardEntry, ForwardQueuePort}; +use crate::telemetry::StageTimer; use async_trait::async_trait; use metrics::{counter, histogram}; diff --git a/hyperbytedb/src/application/query_service.rs b/hyperbytedb/src/application/query_service.rs index a1a21c7..3a5d3f3 100644 --- a/hyperbytedb/src/application/query_service.rs +++ b/hyperbytedb/src/application/query_service.rs @@ -1951,8 +1951,7 @@ async fn run_series_query( epoch: Option<&str>, group_by_tags: &[String], ) -> Result, HyperbytedbError> { - let mut stage = - crate::application::stage_timer::StageTimer::new("hyperbytedb_query_local_stage_seconds"); + let mut stage = crate::telemetry::StageTimer::new("hyperbytedb_query_local_stage_seconds"); let sql = with_execution_time_limit(sql, svc.query_timeout_secs()); let mut batches = svc.query_port.execute_sql_arrow(&sql).await?; let mut builder = crate::application::query_arrow_series::SeriesBuilder::new( @@ -2686,8 +2685,7 @@ async fn execute_sharded_measurement_query( epoch: Option<&str>, group_by_tags: &[String], ) -> Result, HyperbytedbError> { - let mut stage = - crate::application::stage_timer::StageTimer::new("hyperbytedb_query_stage_seconds"); + let mut stage = crate::telemetry::StageTimer::new("hyperbytedb_query_stage_seconds"); // The region SQL names columns this way; the merge must use the same names. let named = to_clickhouse::with_output_names(stmt); let stmt = &named; diff --git a/hyperbytedb/src/application/stage_timer.rs b/hyperbytedb/src/application/stage_timer.rs deleted file mode 100644 index 58503e7..0000000 --- a/hyperbytedb/src/application/stage_timer.rs +++ /dev/null @@ -1,46 +0,0 @@ -//! Per-stage latency for a request path, recorded into one histogram with a -//! `stage` label. - -use std::time::Instant; - -/// Records the time since the previous lap (or since creation) under a -/// stage name. -pub struct StageTimer { - metric: &'static str, - last: Instant, -} - -impl StageTimer { - #[must_use] - pub fn new(metric: &'static str) -> Self { - Self { - metric, - last: Instant::now(), - } - } - - /// Record the time since the last lap as `stage`, and start the next. - pub fn lap(&mut self, stage: &'static str) { - let now = Instant::now(); - metrics::histogram!(self.metric, "stage" => stage) - .record(now.duration_since(self.last).as_secs_f64()); - self.last = now; - } -} - -/// Measure how late the async runtime wakes a task: sleep 10 ms, record how -/// much longer it took. Starved workers (blocked on a lock, or the whole -/// pod frozen by its CPU quota) show up here whatever the cause. -pub fn spawn_scheduling_probe() { - tokio::spawn(async { - let nominal = std::time::Duration::from_millis(10); - loop { - let start = Instant::now(); - tokio::time::sleep(nominal).await; - let late = start.elapsed().saturating_sub(nominal); - metrics::histogram!("hyperbytedb_runtime_wake_delay_seconds") - .record(late.as_secs_f64()); - tokio::time::sleep(std::time::Duration::from_millis(40)).await; - } - }); -} diff --git a/hyperbytedb/src/ports/metadata.rs b/hyperbytedb/src/ports/metadata.rs index 5ee2505..ba5a16d 100644 --- a/hyperbytedb/src/ports/metadata.rs +++ b/hyperbytedb/src/ports/metadata.rs @@ -175,6 +175,55 @@ pub trait MetadataPort: Send + Sync { entries: &[(String, String)], ) -> Result<(), HyperbytedbError>; + /// Add `tags`' keys to `measurement`'s tag keys (creating it, with no + /// fields, when unknown) and record the tag values, in one batch. + async fn backfill_tag_keys( + &self, + db: &str, + rp: &str, + measurement: &str, + tags: Vec<(String, String)>, + ) -> Result<(), HyperbytedbError> { + use std::collections::{BTreeSet, HashMap, HashSet}; + let mut tag_keys: BTreeSet = BTreeSet::new(); + let mut seen: HashSet<(String, String)> = HashSet::new(); + let mut tag_batch: Vec<(String, String)> = Vec::new(); + for (k, v) in tags { + tag_keys.insert(k.clone()); + if seen.insert((k.clone(), v.clone())) { + tag_batch.push((k, v)); + } + } + if tag_keys.is_empty() { + return Ok(()); + } + + let mut meas_updates: Vec = Vec::new(); + if let Some(mut existing) = self.get_measurement(db, rp, measurement).await? { + let before_len = existing.tag_keys.len(); + for k in &tag_keys { + if !existing.tag_keys.contains(k) { + existing.tag_keys.push(k.clone()); + } + } + existing.tag_keys.sort(); + if existing.tag_keys.len() != before_len { + meas_updates.push(existing); + } + } else { + meas_updates.push(MeasurementMeta { + name: measurement.to_string(), + field_types: HashMap::new(), + tag_keys: tag_keys.into_iter().collect(), + ..Default::default() + }); + } + + let tag_entries = vec![(measurement.to_string(), tag_batch)]; + self.register_metadata_batch(db, rp, &meas_updates, &tag_entries) + .await + } + /// Batch-register multiple measurements and their tag values in a single write. /// Default falls back to individual calls; RocksDB overrides with a single WriteBatch. async fn register_metadata_batch( diff --git a/hyperbytedb/src/server.rs b/hyperbytedb/src/server.rs index 5404869..c18de06 100644 --- a/hyperbytedb/src/server.rs +++ b/hyperbytedb/src/server.rs @@ -46,7 +46,7 @@ async fn serve_inner( if let Some(log_level) = log_level { log_level.register(&bootstrapped.app_state.runtime_settings); } - crate::application::stage_timer::spawn_scheduling_probe(); + crate::telemetry::spawn_scheduling_probe(); let disk_paths = bootstrapped.disk_paths.clone(); let disk_config = bootstrapped.disk_config.clone(); let disk_read_only = bootstrapped.disk_read_only.clone(); diff --git a/hyperbytedb/src/telemetry.rs b/hyperbytedb/src/telemetry.rs index a7db41b..156eaf0 100644 --- a/hyperbytedb/src/telemetry.rs +++ b/hyperbytedb/src/telemetry.rs @@ -247,3 +247,47 @@ mod otel { let _ = span.set_parent(cx); } } + +// ── Per-stage latency: one histogram per request path, a `stage` label per lap. + +/// Records the time since the previous lap (or since creation) under a +/// stage name. +pub struct StageTimer { + metric: &'static str, + last: std::time::Instant, +} + +impl StageTimer { + #[must_use] + pub fn new(metric: &'static str) -> Self { + Self { + metric, + last: std::time::Instant::now(), + } + } + + /// Record the time since the last lap as `stage`, and start the next. + pub fn lap(&mut self, stage: &'static str) { + let now = std::time::Instant::now(); + metrics::histogram!(self.metric, "stage" => stage) + .record(now.duration_since(self.last).as_secs_f64()); + self.last = now; + } +} + +/// Measure how late the async runtime wakes a task: sleep 10 ms, record how +/// much longer it took. Starved workers (blocked on a lock, or the whole +/// pod frozen by its CPU quota) show up here whatever the cause. +pub fn spawn_scheduling_probe() { + tokio::spawn(async { + let nominal = std::time::Duration::from_millis(10); + loop { + let start = std::time::Instant::now(); + tokio::time::sleep(nominal).await; + let late = start.elapsed().saturating_sub(nominal); + metrics::histogram!("hyperbytedb_runtime_wake_delay_seconds") + .record(late.as_secs_f64()); + tokio::time::sleep(std::time::Duration::from_millis(40)).await; + } + }); +} From a9d6d696b442cf1be32e354380674a04919c06fc Mon Sep 17 00:00:00 2001 From: Austin Barrington Date: Tue, 10 Nov 2026 16:20:21 +0000 Subject: [PATCH 2/5] Columnar timestamp rule lives with the columnar batch --- .../src/adapters/chdb/native_adapter.rs | 4 +-- .../src/application/columnar_msgpack.rs | 33 +------------------ hyperbytedb/src/domain/columnar_batch.rs | 31 +++++++++++++++++ 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/hyperbytedb/src/adapters/chdb/native_adapter.rs b/hyperbytedb/src/adapters/chdb/native_adapter.rs index f763d5a..19e00ec 100644 --- a/hyperbytedb/src/adapters/chdb/native_adapter.rs +++ b/hyperbytedb/src/adapters/chdb/native_adapter.rs @@ -1285,7 +1285,7 @@ impl ChdbNativeAdapter { wire: &crate::domain::columnar_batch::ColumnarMsgpackBatch, precision: Option<&str>, ) -> Result { - use crate::application::columnar_msgpack::columnar_timestamps_ns; + use crate::domain::columnar_batch::columnar_timestamps_ns; use crate::domain::prepared_wal::{PreparedMeasurementBatch, PreparedWalSlot}; use crate::domain::series::series_id; @@ -2339,7 +2339,7 @@ fn columnar_representative_point( wire: &crate::domain::columnar_batch::ColumnarMsgpackBatch, precision: Option<&str>, ) -> Result { - use crate::application::columnar_msgpack::columnar_timestamps_ns; + use crate::domain::columnar_batch::columnar_timestamps_ns; let ts = columnar_timestamps_ns(wire, precision)?; let mut fields = BTreeMap::new(); diff --git a/hyperbytedb/src/application/columnar_msgpack.rs b/hyperbytedb/src/application/columnar_msgpack.rs index 804f1db..645f5c5 100644 --- a/hyperbytedb/src/application/columnar_msgpack.rs +++ b/hyperbytedb/src/application/columnar_msgpack.rs @@ -15,7 +15,7 @@ use crate::error::HyperbytedbError; /// `Content-Type` for columnar msgpack v1 writes. pub const CONTENT_TYPE: &str = "application/vnd.hyperbytedb.columnar-msgpack.v1"; -pub use crate::domain::columnar_batch::ColumnarMsgpackBatch; +pub use crate::domain::columnar_batch::{ColumnarMsgpackBatch, columnar_timestamps_ns}; /// Decode the wire format from raw bytes. pub fn decode_columnar_batch(body: &[u8]) -> Result { @@ -46,37 +46,6 @@ pub fn decode_columnar_batch_limited( decode_columnar_batch(body) } -/// Nanosecond timestamps for every row in a columnar batch. -pub fn columnar_timestamps_ns( - wire: &ColumnarMsgpackBatch, - precision: Option<&str>, -) -> Result, HyperbytedbError> { - let n = wire.values.len(); - if n == 0 { - return Ok(Vec::new()); - } - - let precision_val = Precision::from_str_opt(precision); - - if let Some(ref ts) = wire.timestamps { - if ts.len() != n { - return Err(HyperbytedbError::ColumnarMsgpackParse { - reason: format!( - "timestamps length {} does not match values length {}", - ts.len(), - n - ), - }); - } - return Ok(ts.iter().map(|t| precision_val.to_nanos(*t)).collect()); - } - - let now = chrono::Utc::now() - .timestamp_nanos_opt() - .ok_or(HyperbytedbError::WallClockTimestampUnavailable)?; - Ok(vec![now; n]) -} - /// Encode columnar wire data as line protocol without expanding to `Vec`. pub fn columnar_wire_to_line_protocol( wire: &ColumnarMsgpackBatch, diff --git a/hyperbytedb/src/domain/columnar_batch.rs b/hyperbytedb/src/domain/columnar_batch.rs index 49984b9..2d3c402 100644 --- a/hyperbytedb/src/domain/columnar_batch.rs +++ b/hyperbytedb/src/domain/columnar_batch.rs @@ -16,6 +16,37 @@ pub struct ColumnarMsgpackBatch { pub timestamps: Option>, } +/// Nanosecond timestamps for every row in a columnar batch. +pub fn columnar_timestamps_ns( + wire: &ColumnarMsgpackBatch, + precision: Option<&str>, +) -> Result, crate::error::HyperbytedbError> { + let n = wire.values.len(); + if n == 0 { + return Ok(Vec::new()); + } + + let precision_val = crate::domain::database::Precision::from_str_opt(precision); + + if let Some(ref ts) = wire.timestamps { + if ts.len() != n { + return Err(crate::error::HyperbytedbError::ColumnarMsgpackParse { + reason: format!( + "timestamps length {} does not match values length {}", + ts.len(), + n + ), + }); + } + return Ok(ts.iter().map(|t| precision_val.to_nanos(*t)).collect()); + } + + let now = chrono::Utc::now() + .timestamp_nanos_opt() + .ok_or(crate::error::HyperbytedbError::WallClockTimestampUnavailable)?; + Ok(vec![now; n]) +} + #[cfg(test)] mod tests { use super::*; From 8b61086198444edb65bff8774a5b2f4da703d6fc Mon Sep 17 00:00:00 2001 From: Austin Barrington Date: Tue, 10 Nov 2026 16:36:31 +0000 Subject: [PATCH 3/5] Catch-up sync is an application service over PeerRpc --- .../src/adapters/cluster/http_peer_rpc.rs | 47 +++++ hyperbytedb/src/adapters/cluster/mod.rs | 1 - .../src/adapters/cluster/peer_client.rs | 7 - .../src/adapters/http/peer_handlers.rs | 39 ++-- .../cluster/catch_up.rs} | 198 +++++++----------- hyperbytedb/src/application/cluster/mod.rs | 1 + .../src/application/shard_scheduler.rs | 2 +- hyperbytedb/src/composition/bootstrap.rs | 12 +- hyperbytedb/src/ports/peer_rpc.rs | 27 +++ 9 files changed, 184 insertions(+), 150 deletions(-) rename hyperbytedb/src/{adapters/cluster/sync_client.rs => application/cluster/catch_up.rs} (85%) diff --git a/hyperbytedb/src/adapters/cluster/http_peer_rpc.rs b/hyperbytedb/src/adapters/cluster/http_peer_rpc.rs index 4f06dae..35397de 100644 --- a/hyperbytedb/src/adapters/cluster/http_peer_rpc.rs +++ b/hyperbytedb/src/adapters/cluster/http_peer_rpc.rs @@ -107,6 +107,16 @@ impl HttpPeerRpc { Err(PeerRpcError::Status { code, body }) } + /// The body as JSON `T`; a body that does not parse is `Unreachable`. + async fn decode( + resp: reqwest::Response, + what: &str, + ) -> Result { + let body = Self::body(resp).await?; + serde_json::from_slice(&body) + .map_err(|e| PeerRpcError::Unreachable(format!("parse {what}: {e}"))) + } + async fn body(resp: reqwest::Response) -> Result { resp.bytes() .await @@ -377,6 +387,43 @@ impl PeerRpc for HttpPeerRpc { }) } + async fn join( + &self, + addr: &str, + req: &crate::domain::cluster::sync::JoinRequest, + timeout: Duration, + ) -> Result { + let resp = self + .post_json(addr, "/internal/membership/join", req, timeout) + .await?; + Self::decode(resp, "join response").await + } + + async fn sync_metadata_snapshot( + &self, + addr: &str, + timeout: Duration, + ) -> Result { + let resp = self.get(addr, "/internal/sync/metadata", timeout).await?; + Self::decode(resp, "metadata snapshot").await + } + + async fn sync_wal( + &self, + addr: &str, + from_seq: u64, + max_entries: u32, + timeout: Duration, + ) -> Result { + let path = format!("/internal/sync/wal?from_seq={from_seq}&max_entries={max_entries}"); + let resp = self.get(addr, &path, timeout).await?; + Self::decode(resp, "WAL sync response").await + } + + async fn ping(&self, addr: &str, timeout: Duration) -> Result<(), PeerRpcError> { + self.get(addr, "/ping", timeout).await.map(|_| ()) + } + async fn probe(&self, addr: &str, timeout: Duration) -> ProbeReply { let get = |path: &str| { self.client() diff --git a/hyperbytedb/src/adapters/cluster/mod.rs b/hyperbytedb/src/adapters/cluster/mod.rs index 0433b45..7938493 100644 --- a/hyperbytedb/src/adapters/cluster/mod.rs +++ b/hyperbytedb/src/adapters/cluster/mod.rs @@ -8,4 +8,3 @@ pub mod peer_client; pub mod peer_http; pub mod raft; pub mod replication_log; -pub mod sync_client; diff --git a/hyperbytedb/src/adapters/cluster/peer_client.rs b/hyperbytedb/src/adapters/cluster/peer_client.rs index a864fc4..cf225d7 100644 --- a/hyperbytedb/src/adapters/cluster/peer_client.rs +++ b/hyperbytedb/src/adapters/cluster/peer_client.rs @@ -182,13 +182,6 @@ impl PeerClient { self } - /// A peer's committed `map_version`, read from `/internal/shard/map`. - pub async fn fetch_map_version(&self, peer_addr: &str) -> Result { - let map = - super::sync_client::fetch_shard_map(self.http_client().clone(), peer_addr).await?; - Ok(map.map_version) - } - /// The client for the next peer request (round-robin over connections). pub fn http_client(&self) -> &reqwest::Client { let i = self diff --git a/hyperbytedb/src/adapters/http/peer_handlers.rs b/hyperbytedb/src/adapters/http/peer_handlers.rs index 24a9aca..a8344e0 100644 --- a/hyperbytedb/src/adapters/http/peer_handlers.rs +++ b/hyperbytedb/src/adapters/http/peer_handlers.rs @@ -909,22 +909,29 @@ pub async fn handle_sync_trigger(State(state): State>) -> impl Int }; tokio::spawn(async move { - let mut sync_client = crate::adapters::cluster::sync_client::SyncClient::with_points_sink( - node_id, - { - let m = membership_clone.read().await; - m.get_node(node_id) - .map(|n| n.addr.clone()) - .unwrap_or_default() - }, - membership_clone.clone(), - metadata.clone(), - wal.clone(), - Some(points_sink), - max_points_per_request, - fallback_peers, - &cluster_auth, - ); + let mut sync_client = + crate::application::cluster::catch_up::CatchUpService::with_points_sink( + node_id, + { + let m = membership_clone.read().await; + m.get_node(node_id) + .map(|n| n.addr.clone()) + .unwrap_or_default() + }, + membership_clone.clone(), + metadata.clone(), + wal.clone(), + Some(points_sink), + max_points_per_request, + fallback_peers, + Arc::new( + crate::adapters::cluster::http_peer_rpc::HttpPeerRpc::with_client({ + use crate::adapters::cluster::peer_http::PeerHttp; + let auth = &cluster_auth; + auth.build(auth.client_builder()) + }), + ), + ); sync_client = sync_client.with_runtime_settings(runtime_settings); // Only a sharded node syncs a shard map. `state.shard_map` is a // `DisabledShardMap` otherwise, never `None`: attaching it made a diff --git a/hyperbytedb/src/adapters/cluster/sync_client.rs b/hyperbytedb/src/application/cluster/catch_up.rs similarity index 85% rename from hyperbytedb/src/adapters/cluster/sync_client.rs rename to hyperbytedb/src/application/cluster/catch_up.rs index 95f52ec..77d716f 100644 --- a/hyperbytedb/src/adapters/cluster/sync_client.rs +++ b/hyperbytedb/src/application/cluster/catch_up.rs @@ -1,22 +1,42 @@ -use crate::adapters::cluster::cluster_auth::ClusterAuth; -use crate::adapters::cluster::peer_http::PeerHttp; +//! Bringing this node up to date from a peer: on join and on reconnect. +//! It fetches through [`PeerRpc`](crate::ports::peer_rpc::PeerRpc) and +//! applies what it gets to this node's metadata, WAL and shard map. + use std::sync::Arc; use std::time::Duration; use crate::application::ingest_metadata::{IngestCardinalityLimits, prepare_batch_metadata}; use crate::application::wal_append::append_points_with_prepared; use crate::domain::cluster::membership::{NodeState, SharedMembership}; -use crate::domain::cluster::sync::{ - JoinRequest, JoinResponse, MetadataSnapshot, SyncManifest, WalSyncResponse, -}; -use crate::domain::sharding::{ShardMap, ShardMapJson}; +use crate::domain::cluster::peer_rpc::PeerRpcError; +use crate::domain::cluster::sync::{JoinRequest, SyncManifest}; use crate::error::HyperbytedbError; use crate::ports::metadata::MetadataPort; +use crate::ports::peer_rpc::SharedPeerRpc; use crate::ports::points_sink::PointsSinkPort; use crate::ports::sharding::ShardMapPort; use crate::ports::wal::WalPort; -pub struct SyncClient { +/// Each sync request's deadline (the old sync client's timeout). +const SYNC_TIMEOUT: Duration = Duration::from_secs(300); +/// WAL entries asked for per catch-up request. +const WAL_BATCH: u32 = 5000; + +/// A sync call's error: a peer that did not answer is `PeerUnreachable` +/// (`transport`), one that answered with an error status `SyncFailed` +/// (`status`), as the sync client mapped them. +fn sync_err(e: PeerRpcError, transport: &str, status: &str) -> HyperbytedbError { + match e { + PeerRpcError::Status { code, .. } => { + HyperbytedbError::SyncFailed(format!("{status}: {code}")) + } + PeerRpcError::Unreachable(e) => { + HyperbytedbError::PeerUnreachable(format!("{transport}: {e}")) + } + } +} + +pub struct CatchUpService { node_id: u64, node_addr: String, membership: SharedMembership, @@ -24,7 +44,7 @@ pub struct SyncClient { wal: Arc, points_sink: Option>, max_points_per_request: usize, - client: reqwest::Client, + rpc: SharedPeerRpc, /// Static peer addresses from config, used as fallback when the /// membership has no active peers yet (Raft hasn't formed). fallback_peer_addrs: Vec, @@ -32,7 +52,7 @@ pub struct SyncClient { runtime_settings: Option>, } -impl SyncClient { +impl CatchUpService { pub fn new( node_id: u64, node_addr: String, @@ -40,7 +60,7 @@ impl SyncClient { metadata: Arc, wal: Arc, fallback_peer_addrs: Vec, - cluster_auth: &ClusterAuth, + rpc: SharedPeerRpc, ) -> Self { Self::with_points_sink( node_id, @@ -51,7 +71,7 @@ impl SyncClient { None, 0, fallback_peer_addrs, - cluster_auth, + rpc, ) } @@ -65,14 +85,8 @@ impl SyncClient { points_sink: Option>, max_points_per_request: usize, fallback_peer_addrs: Vec, - cluster_auth: &ClusterAuth, + rpc: SharedPeerRpc, ) -> Self { - let client = cluster_auth.build( - cluster_auth - .client_builder() - .timeout(Duration::from_secs(300)), - ); - Self { node_id, node_addr, @@ -81,7 +95,7 @@ impl SyncClient { wal, points_sink, max_points_per_request, - client, + rpc, fallback_peer_addrs, shard_map: None, runtime_settings: None, @@ -254,10 +268,7 @@ impl SyncClient { if addr == &self.node_addr { continue; } - let url = format!("http://{}/ping", addr); - if let Ok(resp) = self.client.get(&url).send().await - && resp.status().is_success() - { + if self.rpc.ping(addr, SYNC_TIMEOUT).await.is_ok() { tracing::debug!(peer = %addr, "using fallback peer for sync"); return Some(addr.clone()); } @@ -266,31 +277,23 @@ impl SyncClient { } async fn send_join_request(&self, peer_addr: &str) -> Result<(), HyperbytedbError> { - let url = format!("http://{}/internal/membership/join", peer_addr); let req = JoinRequest { node_id: Some(self.node_id), addr: self.node_addr.clone(), }; - - let resp = self - .client - .post(&url) - .json(&req) - .send() - .await - .map_err(|e| HyperbytedbError::PeerUnreachable(format!("join request failed: {e}")))?; - - if !resp.status().is_success() { - return Err(HyperbytedbError::ClusterUnavailable(format!( - "join request rejected: {}", - resp.status() - ))); - } - - let join_resp: JoinResponse = resp - .json() - .await - .map_err(|e| HyperbytedbError::SyncFailed(format!("parse join response: {e}")))?; + let join_resp = match self.rpc.join(peer_addr, &req, SYNC_TIMEOUT).await { + Ok(r) => r, + Err(PeerRpcError::Status { code, .. }) => { + return Err(HyperbytedbError::ClusterUnavailable(format!( + "join request rejected: {code}" + ))); + } + Err(PeerRpcError::Unreachable(e)) => { + return Err(HyperbytedbError::PeerUnreachable(format!( + "join request failed: {e}" + ))); + } + }; let mut m = self.membership.write().await; *m = join_resp.membership; @@ -306,41 +309,18 @@ impl SyncClient { } async fn get_manifest(&self, peer_addr: &str) -> Result { - let url = format!("http://{}/internal/sync/manifest", peer_addr); - let resp = self.client.get(&url).send().await.map_err(|e| { - HyperbytedbError::PeerUnreachable(format!("manifest request failed: {e}")) - })?; - - if !resp.status().is_success() { - return Err(HyperbytedbError::SyncFailed(format!( - "manifest request failed: {}", - resp.status() - ))); - } - - resp.json() + self.rpc + .sync_manifest(peer_addr, SYNC_TIMEOUT) .await - .map_err(|e| HyperbytedbError::SyncFailed(format!("parse manifest: {e}"))) + .map_err(|e| sync_err(e, "manifest request failed", "manifest request failed")) } async fn sync_metadata(&self, peer_addr: &str) -> Result<(), HyperbytedbError> { - let url = format!("http://{}/internal/sync/metadata", peer_addr); - let resp = - self.client.get(&url).send().await.map_err(|e| { - HyperbytedbError::PeerUnreachable(format!("metadata sync failed: {e}")) - })?; - - if !resp.status().is_success() { - return Err(HyperbytedbError::SyncFailed(format!( - "metadata sync failed: {}", - resp.status() - ))); - } - - let snapshot: MetadataSnapshot = resp - .json() + let snapshot = self + .rpc + .sync_metadata_snapshot(peer_addr, SYNC_TIMEOUT) .await - .map_err(|e| HyperbytedbError::SyncFailed(format!("parse metadata snapshot: {e}")))?; + .map_err(|e| sync_err(e, "metadata sync failed", "metadata sync failed"))?; tracing::debug!( entries = snapshot.entries.len(), @@ -370,7 +350,11 @@ impl SyncClient { let Some(shard_map) = self.shard_map.as_ref() else { return Ok(()); }; - let remote = fetch_shard_map(self.client.clone(), peer_addr).await?; + let remote = self + .rpc + .shard_map(peer_addr, SYNC_TIMEOUT) + .await + .map_err(|e| sync_err(e, "shard map request failed", "shard map request failed"))?; let version = remote.map_version; // Never move the map backwards. `map_version` counts local applies and @@ -492,26 +476,11 @@ impl SyncClient { let mut total_entries = 0u64; loop { - let url = format!( - "http://{}/internal/sync/wal?from_seq={}&max_entries=5000", - peer_addr, - cursor + 1 - ); - - let resp = self.client.get(&url).send().await.map_err(|e| { - HyperbytedbError::PeerUnreachable(format!("WAL sync request failed: {e}")) - })?; - - if !resp.status().is_success() { - return Err(HyperbytedbError::SyncFailed(format!( - "WAL sync failed: {}", - resp.status() - ))); - } - - let wal_resp: WalSyncResponse = resp.json().await.map_err(|e| { - HyperbytedbError::SyncFailed(format!("parse WAL sync response: {e}")) - })?; + let wal_resp = self + .rpc + .sync_wal(peer_addr, cursor + 1, WAL_BATCH, SYNC_TIMEOUT) + .await + .map_err(|e| sync_err(e, "WAL sync request failed", "WAL sync failed"))?; if wal_resp.entries.is_empty() { break; @@ -568,7 +537,6 @@ impl SyncClient { } } -/// GET `/internal/shard/map` from `peer_addr` and return the committed snapshot. /// Whether a peer's shard map may replace the local one. /// /// Strictly-newer only. Equal is a no-op, and older must be refused: a sync @@ -579,28 +547,6 @@ pub fn remote_map_is_newer(remote_version: u64, local_version: u64) -> bool { remote_version > local_version } -pub async fn fetch_shard_map( - client: reqwest::Client, - peer_addr: &str, -) -> Result { - let url = format!("http://{peer_addr}/internal/shard/map"); - let resp = - client.get(&url).send().await.map_err(|e| { - HyperbytedbError::PeerUnreachable(format!("shard map request failed: {e}")) - })?; - if !resp.status().is_success() { - return Err(HyperbytedbError::SyncFailed(format!( - "shard map request failed: {}", - resp.status() - ))); - } - let json: ShardMapJson = resp - .json() - .await - .map_err(|e| HyperbytedbError::SyncFailed(format!("parse shard map: {e}")))?; - Ok(ShardMap::from(json)) -} - /// Fail closed when the peer advertises a higher WAL watermark but returns no /// readable entries — usually because the leader truncated past the gap. fn verify_catchup_progress( @@ -626,6 +572,14 @@ mod tests { use super::{remote_map_is_newer, verify_catchup_progress}; use crate::error::HyperbytedbError; + fn test_rpc() -> crate::ports::peer_rpc::SharedPeerRpc { + std::sync::Arc::new( + crate::adapters::cluster::http_peer_rpc::HttpPeerRpc::with_client( + reqwest::Client::new(), + ), + ) + } + /// A peer that answers sync calls and counts WAL catch-up requests. async fn spawn_sync_peer(wal_requests: std::sync::Arc) -> String { use axum::routing::{get, post}; @@ -687,14 +641,14 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let metadata = Arc::new(RocksDbMetadata::open(dir.path().join("meta")).unwrap()); - let client = super::SyncClient::new( + let client = super::CatchUpService::new( 1, "127.0.0.1:1".into(), new_shared(ClusterMembership::new()), metadata.clone(), Arc::new(RocksDbWal::open(dir.path().join("wal")).unwrap()), Vec::new(), - &Default::default(), + test_rpc(), ); let user = StoredUser { password_hash: "hash".into(), @@ -746,14 +700,14 @@ mod tests { consecutive_misses: 0, }); std::fs::create_dir_all(dir.path().join("meta")).unwrap(); - let client = super::SyncClient::new( + let client = super::CatchUpService::new( 1, "127.0.0.1:1".into(), new_shared(m), Arc::new(RocksDbMetadata::open(dir.path().join("meta2")).unwrap()), Arc::new(RocksDbWal::open(dir.path().join("wal")).unwrap()), Vec::new(), - &Default::default(), + test_rpc(), ) .with_shard_map(Arc::new( RocksDbShardMap::open(&dir.path().join("meta"), true).unwrap(), diff --git a/hyperbytedb/src/application/cluster/mod.rs b/hyperbytedb/src/application/cluster/mod.rs index a8ddbe6..a07af47 100644 --- a/hyperbytedb/src/application/cluster/mod.rs +++ b/hyperbytedb/src/application/cluster/mod.rs @@ -1,3 +1,4 @@ +pub mod catch_up; pub mod drain; pub mod heartbeat; pub mod sync_manifest; diff --git a/hyperbytedb/src/application/shard_scheduler.rs b/hyperbytedb/src/application/shard_scheduler.rs index 63d40fb..f844873 100644 --- a/hyperbytedb/src/application/shard_scheduler.rs +++ b/hyperbytedb/src/application/shard_scheduler.rs @@ -3805,7 +3805,7 @@ mod tests { } /// A peer that is reachable but cannot answer — the cheap, deterministic - /// stand-in for an unreachable one. `fetch_shard_map` turns any non-2xx + /// stand-in for an unreachable one. `PeerRpc::shard_map` turns any non-2xx /// into an error on the same path a connect failure takes, and unlike a /// closed port it can count how many times it was asked. async fn spawn_failing_mock_peer() -> (String, Arc) { diff --git a/hyperbytedb/src/composition/bootstrap.rs b/hyperbytedb/src/composition/bootstrap.rs index 87ba559..4aaf146 100644 --- a/hyperbytedb/src/composition/bootstrap.rs +++ b/hyperbytedb/src/composition/bootstrap.rs @@ -4,7 +4,7 @@ use crate::adapters::cluster::hinted_handoff::HintedHandoff; use crate::adapters::cluster::peer_client::PeerClient; use crate::adapters::cluster::raft::{HyperbytedbRaft, TypeConfig}; use crate::adapters::cluster::replication_log::ReplicationLog; -use crate::adapters::cluster::sync_client::SyncClient; +use crate::application::cluster::catch_up::CatchUpService; use crate::config::ClusterConfig; use crate::domain::cluster::membership::{ ClusterMembership, NodeInfo, NodeState, SharedMembership, @@ -119,7 +119,7 @@ impl ClusterBootstrap { tracing::info!("startup phase: syncing with cluster before accepting traffic"); - let mut sync_client = SyncClient::with_points_sink( + let mut sync_client = CatchUpService::with_points_sink( config.node_id, config.cluster_addr.clone(), self.membership.clone(), @@ -128,7 +128,13 @@ impl ClusterBootstrap { points_sink, max_points_per_request, self.peer_addrs.clone(), - self.peer_client.cluster_auth(), + Arc::new( + crate::adapters::cluster::http_peer_rpc::HttpPeerRpc::with_client({ + use crate::adapters::cluster::peer_http::PeerHttp; + let auth = self.peer_client.cluster_auth(); + auth.build(auth.client_builder()) + }), + ), ); sync_client = sync_client.with_runtime_settings(self.runtime_settings.clone()); if let Some(map) = shard_map { diff --git a/hyperbytedb/src/ports/peer_rpc.rs b/hyperbytedb/src/ports/peer_rpc.rs index 9f8e3a7..9d26387 100644 --- a/hyperbytedb/src/ports/peer_rpc.rs +++ b/hyperbytedb/src/ports/peer_rpc.rs @@ -144,6 +144,33 @@ pub trait PeerRpc: Send + Sync { timeout: Duration, ) -> Result; + /// `POST /internal/membership/join`. + async fn join( + &self, + addr: &str, + req: &crate::domain::cluster::sync::JoinRequest, + timeout: Duration, + ) -> Result; + + /// `GET /internal/sync/metadata`. + async fn sync_metadata_snapshot( + &self, + addr: &str, + timeout: Duration, + ) -> Result; + + /// `GET /internal/sync/wal?from_seq=&max_entries=`. + async fn sync_wal( + &self, + addr: &str, + from_seq: u64, + max_entries: u32, + timeout: Duration, + ) -> Result; + + /// `GET /ping`: answered with a success status. + async fn ping(&self, addr: &str, timeout: Duration) -> Result<(), PeerRpcError>; + /// `GET /health`, falling back to `GET /ping` when `/health` gets no /// answer; each request is bounded by `timeout`. async fn probe( From 2de7d0a35883bca114af23240b04d000c6d1aeaa Mon Sep 17 00:00:00 2001 From: Austin Barrington Date: Tue, 10 Nov 2026 16:48:48 +0000 Subject: [PATCH 4/5] Architecture test enforces the layering rules --- hyperbytedb/tests/architecture.rs | 172 ++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 hyperbytedb/tests/architecture.rs diff --git a/hyperbytedb/tests/architecture.rs b/hyperbytedb/tests/architecture.rs new file mode 100644 index 0000000..8cb88aa --- /dev/null +++ b/hyperbytedb/tests/architecture.rs @@ -0,0 +1,172 @@ +//! The hexagonal layering rules, enforced over `src/`: +//! +//! - `domain/` imports none of `adapters`, `application` or `ports`; +//! - `ports/` imports neither `adapters` nor `application`; +//! - `application/` does not import `adapters`; +//! - `adapters/` imports `application` only from the driving adapters +//! (`adapters/http/`) and the Raft state machine +//! (`adapters/cluster/raft/{log_store,state_machine}.rs`). +//! +//! Test modules (`#[cfg(test)] mod … { … }`) and comments are not checked. + +use std::path::{Path, PathBuf}; + +/// Known exceptions, as `"::"`. Empty: add one only +/// with a comment naming the plan that removes it. +const ALLOWLIST: &[&str] = &[]; + +const LAYERS: [&str; 3] = ["adapters", "application", "ports"]; + +fn rs_files(dir: &Path, out: &mut Vec) { + let Ok(entries) = std::fs::read_dir(dir) else { + return; + }; + for entry in entries.flatten() { + let path = entry.path(); + if path.is_dir() { + rs_files(&path, out); + } else if path.extension().is_some_and(|e| e == "rs") { + out.push(path); + } + } +} + +/// Whether `file` (relative to `src/`, `/`-separated) in `layer` may import +/// `target` (one of [`LAYERS`]). +fn allowed(layer: &str, file: &str, target: &str) -> bool { + match layer { + "domain" => false, + "ports" => target == "ports", + "application" => target != "adapters", + "adapters" => { + target != "application" + || file.starts_with("adapters/http/") + || file == "adapters/cluster/raft/log_store.rs" + || file == "adapters/cluster/raft/state_machine.rs" + } + _ => true, + } +} + +/// `crate::…` paths named on `line`, each as the full path. +fn crate_paths(line: &str) -> Vec<(&'static str, String)> { + let mut found = Vec::new(); + for layer in LAYERS { + let needle = format!("crate::{layer}"); + let mut rest = line; + while let Some(i) = rest.find(&needle) { + let tail = &rest[i..]; + let end = tail + .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == ':')) + .unwrap_or(tail.len()); + let path = tail[..end].trim_end_matches(':'); + // `crate::adapters` but not `crate::adaptersx`. + if path.len() == needle.len() || path[needle.len()..].starts_with("::") { + found.push((layer, path.to_string())); + } + rest = &tail[needle.len()..]; + } + } + found +} + +/// Every production line under `src_root` that breaks a layering rule, as +/// `":: imports "`. +fn forbidden_imports(src_root: &Path) -> Vec { + let mut files = Vec::new(); + rs_files(src_root, &mut files); + files.sort(); + let mut findings = Vec::new(); + for path in files { + let rel = path + .strip_prefix(src_root) + .unwrap() + .to_string_lossy() + .replace('\\', "/"); + let layer = rel.split('/').next().unwrap_or(""); + if !matches!(layer, "domain" | "ports" | "application" | "adapters") { + continue; + } + let text = std::fs::read_to_string(&path).unwrap(); + let lines: Vec<&str> = text.lines().collect(); + // Brace depth inside a `#[cfg(test)] mod`; `None` outside one. + let mut test_depth: Option = None; + let mut pending_cfg_test = false; + for (n, raw) in lines.iter().enumerate() { + let line = raw.trim(); + if let Some(depth) = test_depth.as_mut() { + *depth += line.matches('{').count() as i64 - line.matches('}').count() as i64; + if *depth <= 0 { + test_depth = None; + } + continue; + } + if line == "#[cfg(test)]" { + pending_cfg_test = true; + continue; + } + if pending_cfg_test { + pending_cfg_test = false; + if line.starts_with("mod ") || line.starts_with("pub mod ") { + if line.ends_with(';') { + continue; + } + let depth = line.matches('{').count() as i64 - line.matches('}').count() as i64; + if depth > 0 { + test_depth = Some(depth); + } + continue; + } + } + if line.starts_with("//") { + continue; + } + for (target, full) in crate_paths(line) { + if allowed(layer, &rel, target) { + continue; + } + let allowlisted = ALLOWLIST.iter().any(|entry| { + entry + .split_once("::") + .is_some_and(|(file, prefix)| file == rel && full.starts_with(prefix)) + }); + if !allowlisted { + findings.push(format!("{rel}:{}: {layer} imports {full}", n + 1)); + } + } + } + } + findings +} + +#[test] +fn scanner_flags_a_domain_adapter_import() { + let dir = tempfile::tempdir().unwrap(); + let domain = dir.path().join("domain"); + std::fs::create_dir_all(&domain).unwrap(); + std::fs::write( + domain.join("x.rs"), + "use crate::adapters::wal;\n\ + // use crate::adapters::comment;\n\ + #[cfg(test)]\n\ + mod tests {\n use crate::adapters::fine_in_tests;\n}\n", + ) + .unwrap(); + let findings = forbidden_imports(dir.path()); + assert_eq!( + findings, + ["domain/x.rs:1: domain imports crate::adapters::wal"] + ); +} + +#[test] +fn layering_rules_hold() { + let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); + let findings = forbidden_imports(&src); + assert!( + findings.is_empty(), + "layering violations ({}):\n{}", + findings.len(), + findings.join("\n") + ); +} From 6bad45baaee9ab19dcb5041e74fc6d085aaae088 Mon Sep 17 00:00:00 2001 From: Austin Barrington Date: Tue, 10 Nov 2026 17:03:58 +0000 Subject: [PATCH 5/5] Architecture test checks grouped imports --- hyperbytedb/tests/architecture.rs | 99 ++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/hyperbytedb/tests/architecture.rs b/hyperbytedb/tests/architecture.rs index 8cb88aa..cfa9ae7 100644 --- a/hyperbytedb/tests/architecture.rs +++ b/hyperbytedb/tests/architecture.rs @@ -70,6 +70,45 @@ fn crate_paths(line: &str) -> Vec<(&'static str, String)> { found } +/// The layer paths in a grouped import's body (the text after +/// `crate::{`, up to its closing brace): `adapters::x::Y` gives +/// `crate::adapters::x::Y`, `application::svc::{A, B}` gives +/// `crate::application::svc`. +fn grouped_paths(body: &str) -> Vec<(&'static str, String)> { + let mut items = Vec::new(); + let (mut depth, mut start) = (0usize, 0usize); + for (i, c) in body.char_indices() { + match c { + '{' => depth += 1, + '}' => depth = depth.saturating_sub(1), + ',' if depth == 0 => { + items.push(&body[start..i]); + start = i + 1; + } + _ => {} + } + } + items.push(&body[start..]); + let mut found = Vec::new(); + for item in items { + let item = item.trim(); + let path: String = item + .split("::{") + .next() + .unwrap_or("") + .chars() + .take_while(|c| c.is_alphanumeric() || *c == '_' || *c == ':') + .collect(); + let path = path.trim_end_matches(':'); + for layer in LAYERS { + if path == layer || path.starts_with(&format!("{layer}::")) { + found.push((layer, format!("crate::{path}"))); + } + } + } + found +} + /// Every production line under `src_root` that breaks a layering rule, as /// `":: imports "`. fn forbidden_imports(src_root: &Path) -> Vec { @@ -92,6 +131,8 @@ fn forbidden_imports(src_root: &Path) -> Vec { // Brace depth inside a `#[cfg(test)] mod`; `None` outside one. let mut test_depth: Option = None; let mut pending_cfg_test = false; + // An open `crate::{ … }` group: its first line and body so far. + let mut group: Option<(usize, String)> = None; for (n, raw) in lines.iter().enumerate() { let line = raw.trim(); if let Some(depth) = test_depth.as_mut() { @@ -121,7 +162,48 @@ fn forbidden_imports(src_root: &Path) -> Vec { if line.starts_with("//") { continue; } - for (target, full) in crate_paths(line) { + let mut paths: Vec<(usize, &'static str, String)> = Vec::new(); + let mut rest = line; + if group.is_none() + && let Some(i) = line.find("crate::{") + { + group = Some((n, String::new())); + rest = &line[i + "crate::{".len()..]; + paths.extend(crate_paths(&line[..i]).into_iter().map(|(t, f)| (n, t, f))); + } else if group.is_none() { + paths.extend(crate_paths(line).into_iter().map(|(t, f)| (n, t, f))); + } + if let Some((first, body)) = group.as_mut() { + // Close the group at the brace that balances `crate::{`. + let mut depth = 1 + body.matches('{').count() - body.matches('}').count(); + let mut end = None; + for (i, c) in rest.char_indices() { + match c { + '{' => depth += 1, + '}' => { + depth -= 1; + if depth == 0 { + end = Some(i); + break; + } + } + _ => {} + } + } + match end { + Some(i) => { + body.push_str(&rest[..i]); + let first = *first; + paths.extend(grouped_paths(body).into_iter().map(|(t, f)| (first, t, f))); + group = None; + } + None => { + body.push_str(rest); + body.push('\n'); + } + } + } + for (at, target, full) in paths { if allowed(layer, &rel, target) { continue; } @@ -131,7 +213,7 @@ fn forbidden_imports(src_root: &Path) -> Vec { .is_some_and(|(file, prefix)| file == rel && full.starts_with(prefix)) }); if !allowlisted { - findings.push(format!("{rel}:{}: {layer} imports {full}", n + 1)); + findings.push(format!("{rel}:{}: {layer} imports {full}", at + 1)); } } } @@ -152,10 +234,21 @@ fn scanner_flags_a_domain_adapter_import() { mod tests {\n use crate::adapters::fine_in_tests;\n}\n", ) .unwrap(); + // Grouped imports, on one line and across lines. + std::fs::write( + domain.join("y.rs"), + "use crate::{adapters::chdb::X, domain::Y};\n\ + use crate::{\n domain::Z,\n application::svc::{A, B},\n};\n", + ) + .unwrap(); let findings = forbidden_imports(dir.path()); assert_eq!( findings, - ["domain/x.rs:1: domain imports crate::adapters::wal"] + [ + "domain/x.rs:1: domain imports crate::adapters::wal", + "domain/y.rs:1: domain imports crate::adapters::chdb::X", + "domain/y.rs:2: domain imports crate::application::svc", + ] ); }