Repository navigation
fix(obsv): wire live per-database metrics sampler (fixes #386) #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AubaidAhmedSaiyed
wants to merge
1
commit into
NodeDB-Lab:main
Choose a base branch
from
AubaidAhmedSaiyed:fix-sampler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| //! Background sampler for per-database Prometheus metrics. | ||
| //! | ||
| //! Samples live gauges every 10 seconds across memory, admission, storage, | ||
| //! virtual queue depths, and rolling-window WAL fsync latency. | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
|
|
||
| use tracing::info; | ||
|
|
||
| use crate::control::metrics::histogram::HistogramSnapshot; | ||
| use crate::control::state::SharedState; | ||
|
|
||
| /// Spawn the 10-second periodic database metrics sampler. | ||
| pub fn spawn_database_metrics_sampler(shared: &Arc<SharedState>) { | ||
| let shared_sampler = Arc::clone(shared); | ||
| crate::control::shutdown::spawn_loop( | ||
| &shared.loop_registry, | ||
| &shared.shutdown, | ||
| "database_metrics_sampler", | ||
| crate::control::shutdown::ShutdownPhase::DrainingControlPlane, | ||
| move |mut shutdown| async move { | ||
| let mut tick = tokio::time::interval(Duration::from_secs(10)); | ||
| let mut last_fsync_snapshot: HistogramSnapshot = shared_sampler | ||
| .system_metrics | ||
| .as_ref() | ||
| .map(|s| s.wal_fsync_seconds.snapshot_counts()) | ||
| .unwrap_or_default(); | ||
|
|
||
| loop { | ||
| tokio::select! { | ||
| _ = shutdown.wait_cancelled() => break, | ||
| _ = tick.tick() => {} | ||
| } | ||
| if shutdown.is_cancelled() { | ||
| break; | ||
| } | ||
| let catalog = shared_sampler.credentials.catalog(); | ||
| let databases = match catalog.list_databases() { | ||
| Ok(d) => d, | ||
| Err(e) => { | ||
| tracing::warn!(error = %e, "database_metrics_sampler: catalog list error"); | ||
| continue; | ||
| } | ||
| }; | ||
|
|
||
| // Compute rolling P99 fsync latency across the 10s tick window. | ||
| let curr_fsync_snapshot = shared_sampler | ||
| .system_metrics | ||
| .as_ref() | ||
| .map(|s| s.wal_fsync_seconds.snapshot_counts()) | ||
| .unwrap_or_default(); | ||
| let wal_latency_p99 = | ||
| last_fsync_snapshot.delta_percentile(&curr_fsync_snapshot, 0.99); | ||
| last_fsync_snapshot = curr_fsync_snapshot; | ||
|
|
||
| // Lock dispatcher ONCE per sampler tick to read virtual-queue depths for all databases. | ||
| let queue_depths: HashMap<u64, u64> = { | ||
| let d = shared_sampler | ||
| .dispatcher | ||
| .lock() | ||
| .unwrap_or_else(|p| p.into_inner()); | ||
| databases | ||
| .iter() | ||
| .map(|db| (db.id.as_u64(), d.virtual_queue_depth(db.id.as_u64()))) | ||
| .collect() | ||
| }; | ||
|
|
||
| for db in databases { | ||
| let depth = queue_depths.get(&db.id.as_u64()).copied().unwrap_or(0); | ||
| sample_database(&shared_sampler, db.id, &db.name, depth, wal_latency_p99); | ||
| } | ||
| } | ||
| }, | ||
| ); | ||
| info!("database metrics sampler running"); | ||
| } | ||
|
|
||
| /// Sample and record gauges for a single database (internal helper, not pub). | ||
| fn sample_database( | ||
| shared: &SharedState, | ||
| db_id: nodedb_types::DatabaseId, | ||
| db_name: &str, | ||
| bridge_queue_depth: u64, | ||
| wal_latency_p99: u64, | ||
| ) { | ||
| // 1. connections: active connections holding an admission permit for this database | ||
| let connections = shared | ||
| .admission_registry | ||
| .database_live_connections(db_id) | ||
| .map(u64::from) | ||
| .unwrap_or(0); | ||
| shared | ||
| .database_metrics | ||
| .set_connections(db_id, db_name, connections); | ||
|
|
||
| // 2. memory: resident memory in bytes allocated by this database in governor | ||
| let memory_bytes = shared.governor.database_usage_bytes(db_id) as u64; | ||
| shared | ||
| .database_metrics | ||
| .set_memory_bytes(db_id, db_name, memory_bytes); | ||
|
|
||
| // 3. storage: real on-disk storage usage from system metrics if present | ||
| if let Some(m) = &shared.system_metrics { | ||
| let storage_bytes = m.database_storage_bytes(db_name); | ||
| shared | ||
| .database_metrics | ||
| .set_storage_bytes(db_id, db_name, storage_bytes); | ||
| } | ||
|
|
||
| // 4. bridge_queue_depth: sum of SPSC bridge virtual-queue depths across cores | ||
| shared | ||
| .database_metrics | ||
| .set_bridge_queue_depth(db_id, db_name, bridge_queue_depth); | ||
|
|
||
| // 5. wal_latency_p99: node-wide rolling 10s P99 group-commit fsync latency | ||
| shared | ||
| .database_metrics | ||
| .set_wal_latency_p99(db_id, db_name, wal_latency_p99); | ||
|
|
||
| // 6. maintenance: cumulative maintenance CPU seconds tracked by MaintenanceBudgetTracker | ||
| let maintenance_cpu = shared.maintenance_budget.cumulative_cpu_secs(db_id); | ||
| shared | ||
| .database_metrics | ||
| .set_maintenance_cpu_secs(db_id, db_name, maintenance_cpu); | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::bridge::dispatch::Dispatcher; | ||
| use crate::control::metrics::histogram::HistogramSnapshot; | ||
| use crate::control::state::SharedState; | ||
| use crate::wal::WalManager; | ||
| use nodedb_mem::engine::EngineId; | ||
| use nodedb_types::{DatabaseId, TenantId}; | ||
|
|
||
| #[tokio::test] | ||
| async fn sampler_records_memory_connections_and_wal_fsync() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let wal = Arc::new( | ||
| WalManager::open_for_testing(&dir.path().join("sampler-test.wal")).expect("open wal"), | ||
| ); | ||
| let (dispatcher, _data_sides) = Dispatcher::new(1, 64); | ||
| let shared = SharedState::new(dispatcher, wal).expect("construct state"); | ||
|
|
||
| if let Some(metrics) = shared.system_metrics.as_ref() { | ||
| for _ in 0..90 { | ||
| metrics.record_wal_fsync(100); | ||
| } | ||
| for _ in 0..10 { | ||
| metrics.record_wal_fsync(500); | ||
| } | ||
| } | ||
|
|
||
| let db_id = DatabaseId::new(42); | ||
| let db_name = "test_analytics"; | ||
|
|
||
| // Reserve memory in governor for db_id. | ||
| let _token = shared | ||
| .governor | ||
| .try_reserve(db_id, TenantId::new(1), EngineId::Vector, 8192) | ||
| .expect("reserve memory"); | ||
|
|
||
| // Acquire connection permit for db_id. | ||
| let _permit = shared | ||
| .admission_registry | ||
| .try_acquire_database(db_id) | ||
| .expect("acquire permit"); | ||
|
|
||
| let curr_snapshot = shared | ||
| .system_metrics | ||
| .as_ref() | ||
| .map(|s| s.wal_fsync_seconds.snapshot_counts()) | ||
| .unwrap_or_default(); | ||
| let delta_p99 = HistogramSnapshot::default().delta_percentile(&curr_snapshot, 0.99); | ||
|
|
||
| // Run sample_database. | ||
| sample_database(&shared, db_id, db_name, 5, delta_p99); | ||
|
|
||
| // Verify gauge outputs in DatabaseMetricsRegistry. | ||
| let mut text = String::new(); | ||
| shared.database_metrics.render_prometheus(&mut text); | ||
| assert!(text.contains(&format!( | ||
| "nodedb_database_connections{{database=\"{db_name}\"}} 1" | ||
| ))); | ||
| assert!(text.contains(&format!( | ||
| "nodedb_database_memory_used_bytes{{database=\"{db_name}\"}} 8192" | ||
| ))); | ||
| assert!(text.contains(&format!( | ||
| "nodedb_database_bridge_queue_depth{{database=\"{db_name}\"}} 5" | ||
| ))); | ||
| assert!(text.contains(&format!( | ||
| "nodedb_database_wal_commit_latency_p99_us{{database=\"{db_name}\"}} 500" | ||
| ))); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocker.
database_budgetshas an entry only afterALTER DATABASE … SET QUOTA.try_reservecredits a database only when it has a budget.A database with no quota reports 0 bytes forever. The doc line above states this, but the gauge still exports 0 as if it were real.
Track per-database allocation for every database, independent of whether a budget is set.