Repository navigation
fix(telemetry): flush shared client on final release #413
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
Changes from all commits
0eb5058
ef92a85
6c423ec
f492bbd
93cf7f0
1bba9db
938ec87
45f8fa3
b205bd5
2b17cf9
447ab71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ use super::models::TelemetryFrontendLog; | |
| use crate::config::TelemetryConfig; | ||
| use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering}; | ||
| use std::sync::{Arc, Mutex}; | ||
| use tokio::runtime::Handle; | ||
| use tokio::runtime::{Handle, Runtime}; | ||
|
|
||
| const STATE_ACTIVE: u8 = 0; | ||
| const STATE_CLOSING: u8 = 1; | ||
|
|
@@ -37,11 +37,21 @@ pub(super) struct TelemetryClient { | |
| } | ||
|
|
||
| impl TelemetryClient { | ||
| pub(super) fn new(config: &TelemetryConfig, exporter: Arc<dyn TelemetryExporter>) -> Arc<Self> { | ||
| pub(super) fn new( | ||
| config: &TelemetryConfig, | ||
| exporter: Arc<dyn TelemetryExporter>, | ||
| ) -> Option<Arc<Self>> { | ||
| assert!( | ||
| !config.flush_interval.is_zero(), | ||
| "TelemetryClient requires a non-zero flush interval; TelemetryRuntime should filter invalid configs" | ||
| ); | ||
| let runtime_handle = match telemetry_runtime_handle() { | ||
| Ok(handle) => handle, | ||
| Err(error) => { | ||
| tracing::debug!(%error, "failed to create telemetry runtime; disabling telemetry"); | ||
| return None; | ||
| } | ||
| }; | ||
| let client = Arc::new(Self { | ||
| exporter, | ||
| batch_size: config.batch_size, | ||
|
|
@@ -52,10 +62,10 @@ impl TelemetryClient { | |
| flush_lock: tokio::sync::Mutex::new(()), | ||
| state: AtomicU8::new(STATE_ACTIVE), | ||
| timer: Mutex::new(None), | ||
| runtime_handle: Handle::current(), | ||
| runtime_handle, | ||
| }); | ||
| client.start_timer(); | ||
| client | ||
| Some(client) | ||
| } | ||
|
|
||
| pub(super) fn enqueue(self: &Arc<Self>, log: TelemetryFrontendLog) { | ||
|
|
@@ -86,6 +96,20 @@ impl TelemetryClient { | |
| let _ = tokio::time::timeout(timeout, self.flush_once_wait()).await; | ||
| } | ||
|
|
||
| pub(super) fn schedule_flush_bounded(self: &Arc<Self>) -> tokio::task::JoinHandle<()> { | ||
| let client = Arc::clone(self); | ||
| self.runtime_handle.spawn(async move { | ||
| client.flush_bounded().await; | ||
| }) | ||
| } | ||
|
|
||
| pub(super) fn schedule_close(self: &Arc<Self>) -> tokio::task::JoinHandle<()> { | ||
| let client = Arc::clone(self); | ||
| self.runtime_handle.spawn(async move { | ||
| client.close().await; | ||
| }) | ||
| } | ||
|
|
||
| pub(super) async fn close(self: &Arc<Self>) { | ||
| if self | ||
| .state | ||
|
|
@@ -158,13 +182,47 @@ impl TelemetryClient { | |
| } | ||
| } | ||
|
|
||
| // A real HTTP telemetry export needs more than a tiny stack: a host running | ||
| // with e.g. RUST_MIN_STACK=16384 would otherwise overflow the worker stack and | ||
| // abort the process. The worker stack floor is shared with the C-ABI runtime | ||
| // via `crate::util::runtime` (single source of truth); see that module for the | ||
| // floor-not-cap semantics and the #326 / #328 history. | ||
| use crate::util::runtime::{ambient_rust_min_stack, runtime_thread_stack_size}; | ||
|
|
||
| fn telemetry_runtime_handle() -> std::io::Result<Handle> { | ||
| // A host client can outlive the session that created it. Keep its timer, | ||
| // batch tasks, and Drop cleanup independent of any session-owned runtime. | ||
| // | ||
| // Only a successfully-built runtime is cached. A build failure is NOT | ||
| // memoized, so a transient failure (e.g. the OS momentarily can't spawn | ||
| // the worker thread under resource pressure) is retried on the next | ||
| // session rather than poisoning telemetry for the rest of the process. | ||
| static RUNTIME: Mutex<Option<Runtime>> = Mutex::new(None); | ||
| let mut runtime = RUNTIME.lock().expect("poisoned telemetry runtime"); | ||
| if let Some(runtime) = runtime.as_ref() { | ||
| return Ok(runtime.handle().clone()); | ||
| } | ||
| let built = tokio::runtime::Builder::new_multi_thread() | ||
| .worker_threads(1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low — Exports now always run on the dedicated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied the fix locally but the push failed (likely another commit landed on this branch since I started). This bundle covered 1 thread(s); all reverted. Re-trigger by replying to this thread. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for trying the fix. The push was rejected because the PR is in the merge queue, so nothing on the branch has changed, and the current code still sends exports through the creating session's pooled client. The finding is still open. Someone will need to apply the change by hand, either after taking the PR out of the queue or in a follow-up PR. The change is to give the telemetry exporter its own HTTP client built on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I agree with the finding: telemetry exports should not share the creating session's pooled reqwest::Client, whose connections can be driven by a session runtime that has since shut down. But I can't apply the fix in this PR. The branch is in the merge queue, and the earlier push was rejected with "Branches that are queued for merging cannot be updated". A human needs to either take the PR out of the queue and re-trigger me, or make the change in a follow-up PR. The change is to give the telemetry exporter its own HTTP client built on the |
||
| .thread_name("dbx-sql-telemetry") | ||
| .thread_stack_size(runtime_thread_stack_size(ambient_rust_min_stack())) | ||
| .enable_all() | ||
| .build()?; | ||
| let handle = built.handle().clone(); | ||
| *runtime = Some(built); | ||
| Ok(handle) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::telemetry::test_support::CapturingTelemetryExporter; | ||
| use crate::telemetry::TelemetryRequest; | ||
| use std::time::Duration; | ||
|
|
||
| // The pure floor-not-cap resolution (#326 / #328) is exercised once over | ||
| // the shared helper in `crate::util::runtime`. | ||
|
|
||
| #[tokio::test] | ||
| async fn batch_size_triggers_background_flush() { | ||
| let exporter = CapturingTelemetryExporter::default(); | ||
|
|
@@ -174,7 +232,8 @@ mod tests { | |
| flush_interval: Duration::from_secs(60), | ||
| ..TelemetryConfig::default() | ||
| }; | ||
| let client = TelemetryClient::new(&config, Arc::new(exporter.clone())); | ||
| let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) | ||
| .expect("telemetry runtime should start"); | ||
|
|
||
| client.enqueue(sample_log("one")); | ||
| assert!(exporter.requests().is_empty()); | ||
|
|
@@ -204,7 +263,8 @@ mod tests { | |
| flush_interval: Duration::from_secs(60), | ||
| ..TelemetryConfig::default() | ||
| }; | ||
| let client = TelemetryClient::new(&config, Arc::new(exporter.clone())); | ||
| let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) | ||
| .expect("telemetry runtime should start"); | ||
|
|
||
| let worker_client = Arc::clone(&client); | ||
| std::thread::spawn(move || { | ||
|
|
@@ -237,7 +297,8 @@ mod tests { | |
| flush_interval: Duration::from_millis(20), | ||
| ..TelemetryConfig::default() | ||
| }; | ||
| let client = TelemetryClient::new(&config, Arc::new(exporter.clone())); | ||
| let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) | ||
| .expect("telemetry runtime should start"); | ||
|
|
||
| client.enqueue(sample_log("one")); | ||
|
|
||
|
|
@@ -265,7 +326,8 @@ mod tests { | |
| flush_interval: Duration::from_secs(60), | ||
| ..TelemetryConfig::default() | ||
| }; | ||
| let client = TelemetryClient::new(&config, Arc::new(exporter.clone())); | ||
| let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) | ||
| .expect("telemetry runtime should start"); | ||
|
|
||
| client.enqueue(sample_log("one")); | ||
| client.close().await; | ||
|
|
||
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.
The new runtime omits thread_stack_size, so its worker inherits RUST_MIN_STACK. With RUST_MIN_STACK=16384, a real HTTP telemetry export causes stack overflow in dbx-sql-telemetry and aborts the process. The C ABI runtime already guards against this. Apply the same 2 MiB minimum here, preserving larger configured values.
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.
Both clean. The fix is complete and verified.
Pushed b205bd5.