diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index 6aa75289..f85b7049 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -26,6 +26,7 @@ changes go in their respective binding's NEXT_CHANGELOG. ### Fixed - `Session::open` authenticates once before resolving feature flags. A failed OAuth token request now fails the open immediately instead of being sent a second time by CreateSession. +- Telemetry batches rejected with HTTP 429/503 are kept and sent again after a backoff instead of being dropped, at most 1,000 events per request (or one batch, if larger), and telemetry retries log at debug instead of warn. Flushes on session close never wait out the backoff. --- *Note: When making changes, please add your change under the appropriate diff --git a/src/client/http.rs b/src/client/http.rs index e5f9ad0b..d585e6b9 100644 --- a/src/client/http.rs +++ b/src/client/http.rs @@ -36,6 +36,18 @@ use std::sync::{Arc, OnceLock}; use std::time::{Duration, Instant}; use tokio::time::sleep; +/// Log a retry. Telemetry export is fail-open background work, so its +/// retries (e.g. a throttled `/telemetry-ext`) log at debug, not warn. +macro_rules! retry_log { + ($request_type:expr, $($arg:tt)*) => { + if $request_type.category() == RequestCategory::Telemetry { + debug!($($arg)*) + } else { + warn!($($arg)*) + } + }; +} + /// Build an `Error` for a transport-level non-2xx HTTP response, parsing /// the SEA error envelope out of the response body when present. /// @@ -988,7 +1000,8 @@ impl DatabricksHttpClient { )); } - warn!( + retry_log!( + request_type, "{:?}: HTTP {} (attempt {}/{}), waiting {:.1}s before retry", request_type, status.as_u16(), @@ -1040,7 +1053,8 @@ impl DatabricksHttpClient { ))); } - warn!( + retry_log!( + request_type, "{:?}: error (attempt {}/{}): {}, waiting {:.1}s before retry", request_type, attempts, diff --git a/src/telemetry/client.rs b/src/telemetry/client.rs index e625279e..3dd9501d 100644 --- a/src/telemetry/client.rs +++ b/src/telemetry/client.rs @@ -12,30 +12,75 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::exporter::{export_batch, TelemetryExporter}; +use super::exporter::{export_batch, TelemetryExportError, TelemetryExporter}; use super::models::TelemetryFrontendLog; use crate::config::TelemetryConfig; -use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering}; +use std::collections::VecDeque; +use std::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; use tokio::runtime::{Handle, Runtime}; const STATE_ACTIVE: u8 = 0; const STATE_CLOSING: u8 = 1; const STATE_CLOSED: u8 = 2; +// After a throttled background export, background flushes pause for this +// long, doubling per consecutive throttled export up to the cap. The endpoint +// sends `Retry-After: 1` when it throttles. +#[cfg(not(test))] +const INITIAL_THROTTLE_BACKOFF: Duration = Duration::from_secs(1); +#[cfg(test)] +const INITIAL_THROTTLE_BACKOFF: Duration = Duration::from_millis(200); +const MAX_THROTTLE_BACKOFF: Duration = Duration::from_secs(30); +/// Most events queued (at least one batch); past this the oldest are dropped. +/// Mainly reached while throttled (or behind a stuck export). Sized for +/// several seconds of backoff at ~1k events/s (~14 MB). +#[cfg(not(test))] +const MAX_PENDING_EVENTS: usize = 10_000; +#[cfg(test)] +const MAX_PENDING_EVENTS: usize = 200; +/// Most events per POST (at least one batch), so a backlog left by throttling +/// goes out in bounded requests (~1.4 MB) rather than one large one. +#[cfg(not(test))] +const MAX_EVENTS_PER_EXPORT: usize = 1_000; +#[cfg(test)] +const MAX_EVENTS_PER_EXPORT: usize = 50; + pub(super) struct TelemetryClient { exporter: Arc, batch_size: usize, - flush_interval: std::time::Duration, - close_flush_timeout: std::time::Duration, - pending: Mutex>, + flush_interval: Duration, + close_flush_timeout: Duration, + pending: Mutex>, queue_count: AtomicUsize, + /// Events dropped because the queue was full, not yet logged. + dropped: AtomicUsize, + /// A background flush is running; it re-checks the queue before exiting, + /// so `enqueue` need not spawn another. + background_flushing: AtomicBool, flush_lock: tokio::sync::Mutex<()>, + throttle: Mutex, state: AtomicU8, timer: Mutex>>, runtime_handle: Handle, } +#[derive(Default)] +struct Throttle { + until: Option, + backoff: Duration, +} + +/// Clears the flag when dropped, including if the flush is cancelled. +struct ClearOnDrop<'a>(&'a AtomicBool); + +impl Drop for ClearOnDrop<'_> { + fn drop(&mut self) { + self.0.store(false, Ordering::Release); + } +} + impl TelemetryClient { pub(super) fn new( config: &TelemetryConfig, @@ -57,9 +102,12 @@ impl TelemetryClient { batch_size: config.batch_size, flush_interval: config.flush_interval, close_flush_timeout: config.close_flush_timeout, - pending: Mutex::new(Vec::new()), + pending: Mutex::new(VecDeque::new()), queue_count: AtomicUsize::new(0), + dropped: AtomicUsize::new(0), + background_flushing: AtomicBool::new(false), flush_lock: tokio::sync::Mutex::new(()), + throttle: Mutex::new(Throttle::default()), state: AtomicU8::new(STATE_ACTIVE), timer: Mutex::new(None), runtime_handle, @@ -75,11 +123,20 @@ impl TelemetryClient { let count = { let mut pending = self.pending.lock().expect("poisoned telemetry queue"); - pending.push(log); - self.queue_count.fetch_add(1, Ordering::AcqRel) + 1 + if pending.len() >= self.pending_cap() { + pending.pop_front(); + self.dropped.fetch_add(1, Ordering::Relaxed); + } else { + self.queue_count.fetch_add(1, Ordering::AcqRel); + } + pending.push_back(log); + pending.len() }; - if count >= self.batch_size { + if count >= self.batch_size + && !self.background_flushing.load(Ordering::Acquire) + && !self.throttled() + { let client = Arc::clone(self); self.runtime_handle.spawn(async move { client.flush_once_if_idle().await; @@ -128,38 +185,133 @@ impl TelemetryClient { timer.abort(); } self.flush_bounded().await; + // The final flush stops at its first failed post; say what it left. + let abandoned = self.drain_pending(usize::MAX).len(); + let dropped = self.dropped.swap(0, Ordering::Relaxed); + if abandoned > 0 || dropped > 0 { + tracing::debug!( + abandoned, + dropped, + "telemetry client closed with undelivered events" + ); + } self.state.store(STATE_CLOSED, Ordering::Release); } + /// Background (batch-size or timer) flush. Skipped while throttled; a + /// throttled batch goes back to the queue for a later flush. async fn flush_once_if_idle(self: Arc) { + if self.throttled() { + return; + } let Ok(_guard) = self.flush_lock.try_lock() else { return; }; - self.export_pending().await; + self.background_flushing.store(true, Ordering::Release); + let _flushing = ClearOnDrop(&self.background_flushing); + // Keep going while a backlog left by throttling still fills a batch. + loop { + let delivered = self.export_once(true).await; + if !delivered || self.queue_count.load(Ordering::Acquire) < self.batch_size { + break; + } + } } + /// Session-release and close flush. Sends everything queued, in bounded + /// posts, without waiting out a backoff. While the client stays in use and + /// is throttled, the queue is left for the background flush after the + /// backoff. Once it is closing, it stops at the first failed post and a + /// throttled batch is dropped. async fn flush_once_wait(self: &Arc) { let _guard = self.flush_lock.lock().await; - self.export_pending().await; + let active = self.state.load(Ordering::Acquire) == STATE_ACTIVE; + if active && self.throttled() { + return; + } + while self.export_once(active).await {} } - async fn export_pending(&self) { - let batch = self.drain_pending(); + /// Export the oldest queued events in one request. Returns whether a + /// non-empty batch was delivered. With `requeue_if_throttled`, a batch the + /// endpoint rejected with 429/503 is re-queued. Any other failure, or a + /// cancelled export (flush timeout), drops it, since the server may + /// already have accepted it. + async fn export_once(&self, requeue_if_throttled: bool) -> bool { + let batch = self.drain_pending(MAX_EVENTS_PER_EXPORT.max(self.batch_size)); if batch.is_empty() { - return; + return false; } - export_batch(Arc::clone(&self.exporter), batch).await; + let result = export_batch(Arc::clone(&self.exporter), &batch).await; + let dropped = self.dropped.swap(0, Ordering::Relaxed); + if dropped > 0 { + tracing::debug!( + dropped, + "telemetry backlog full while throttled; dropped oldest events" + ); + } + match result { + Ok(()) => { + *self.throttle.lock().expect("poisoned telemetry throttle") = Throttle::default(); + true + } + // Re-queue first, so whoever sees the throttle also sees the batch. + Err(TelemetryExportError::Throttled(_)) if requeue_if_throttled => { + self.requeue(batch); + self.back_off(); + false + } + Err(_) => false, + } } - fn drain_pending(&self) -> Vec { - let drained = self - .pending - .lock() - .expect("poisoned telemetry queue") - .drain(..) - .collect::>(); - self.queue_count.fetch_sub(drained.len(), Ordering::AcqRel); + fn pending_cap(&self) -> usize { + MAX_PENDING_EVENTS.max(self.batch_size) + } + + fn throttled(&self) -> bool { + let throttle = self.throttle.lock().expect("poisoned telemetry throttle"); + throttle.until.is_some_and(|until| Instant::now() < until) + } + + fn back_off(&self) { + let mut throttle = self.throttle.lock().expect("poisoned telemetry throttle"); + throttle.backoff = if throttle.backoff.is_zero() { + INITIAL_THROTTLE_BACKOFF + } else { + (throttle.backoff * 2).min(MAX_THROTTLE_BACKOFF) + }; + throttle.until = Some(Instant::now() + throttle.backoff); + } + + /// Put a batch back ahead of events queued since it was drained. Once the + /// client is closing nothing would send it, so it is dropped instead. + fn requeue(&self, batch: Vec) { + let mut pending = self.pending.lock().expect("poisoned telemetry queue"); + if self.state.load(Ordering::Acquire) != STATE_ACTIVE { + tracing::debug!( + abandoned = batch.len(), + "telemetry client closed during a throttled export; dropping its events" + ); + return; + } + for log in batch.into_iter().rev() { + pending.push_front(log); + } + let excess = pending.len().saturating_sub(self.pending_cap()); + if excess > 0 { + pending.drain(..excess); + self.dropped.fetch_add(excess, Ordering::Relaxed); + } + self.queue_count.store(pending.len(), Ordering::Release); + } + + fn drain_pending(&self, limit: usize) -> Vec { + let mut pending = self.pending.lock().expect("poisoned telemetry queue"); + let n = pending.len().min(limit); + let drained = pending.drain(..n).collect::>(); + self.queue_count.store(pending.len(), Ordering::Release); drained } @@ -338,6 +490,439 @@ mod tests { assert_eq!(requests[0].proto_logs.len(), 1); } + #[tokio::test] + async fn throttled_background_export_is_retried_after_backoff() { + let exporter = ThrottlingExporter::new(1); + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 2, + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) + .expect("telemetry runtime should start"); + + client.enqueue(sample_log("one")); + client.enqueue(sample_log("two")); + wait_until(|| client.throttled()).await; + + // A full batch inside the backoff does not trigger another export. + client.enqueue(sample_log("three")); + tokio::time::sleep(Duration::from_millis(20)).await; + assert_eq!(exporter.ids().len(), 1); + + tokio::time::sleep(INITIAL_THROTTLE_BACKOFF).await; + client.enqueue(sample_log("four")); + wait_until(|| exporter.ids().len() == 2).await; + + assert_eq!(exporter.ids()[1], ["one", "two", "three", "four"]); + assert!(!client.throttled()); + client.close().await; + } + + #[tokio::test] + async fn close_flush_ignores_backoff_and_does_not_requeue() { + let exporter = ThrottlingExporter::new(usize::MAX); + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 1, + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) + .expect("telemetry runtime should start"); + + client.enqueue(sample_log("one")); + wait_until(|| client.throttled()).await; + client.enqueue(sample_log("two")); + assert!(client.throttled()); + + client.close().await; + + assert_eq!(exporter.ids(), [vec!["one"], vec!["one", "two"]]); + assert!(client.pending.lock().unwrap().is_empty()); + } + + #[tokio::test] + async fn session_release_flush_keeps_throttled_batch() { + let exporter = ThrottlingExporter::new(usize::MAX); + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 1, + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) + .expect("telemetry runtime should start"); + + client.enqueue(sample_log("one")); + wait_until(|| client.throttled()).await; + + // The flush run when a host's last session closes doesn't post into an + // active throttle; the client stays in use, so the batch stays queued + // for the background flush and the backoff is not extended. + client.flush_bounded().await; + + assert_eq!(exporter.ids(), [vec!["one"]]); + assert_eq!(client.pending.lock().unwrap().len(), 1); + assert_eq!( + client.throttle.lock().unwrap().backoff, + INITIAL_THROTTLE_BACKOFF + ); + client.close().await; + } + + #[tokio::test] + async fn session_release_flush_requeues_when_it_gets_throttled() { + let exporter = ThrottlingExporter::new(1); + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 100, + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) + .expect("telemetry runtime should start"); + client.enqueue(sample_log("one")); + + client.flush_bounded().await; + + assert_eq!(exporter.ids(), [vec!["one"]]); + assert_eq!(client.pending.lock().unwrap().len(), 1); + assert!(client.throttled()); + client.close().await; + } + + #[tokio::test] + async fn flush_sends_backlog_in_bounded_posts() { + let exporter = ThrottlingExporter::new(0); + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 2, + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) + .expect("telemetry runtime should start"); + let backlog = (0..120) + .map(|i| sample_log(&format!("e{i}"))) + .collect::>(); + client.queue_count.store(backlog.len(), Ordering::Release); + *client.pending.lock().unwrap() = backlog.into(); + + client.flush_bounded().await; + + let sizes = exporter.ids().iter().map(Vec::len).collect::>(); + assert_eq!(sizes, [MAX_EVENTS_PER_EXPORT, MAX_EVENTS_PER_EXPORT, 20]); + assert_eq!(exporter.ids()[0][0], "e0"); + client.close().await; + } + + #[tokio::test] + async fn background_flush_drains_backlog_in_bounded_posts() { + let exporter = ThrottlingExporter::new(0); + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 2, + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) + .expect("telemetry runtime should start"); + let backlog = (0..119) + .map(|i| sample_log(&format!("e{i}"))) + .collect::>(); + client.queue_count.store(backlog.len(), Ordering::Release); + *client.pending.lock().unwrap() = backlog.into(); + + client.enqueue(sample_log("e119")); + wait_until(|| exporter.ids().len() == 3).await; + + let sizes = exporter.ids().iter().map(Vec::len).collect::>(); + assert_eq!(sizes, [MAX_EVENTS_PER_EXPORT, MAX_EVENTS_PER_EXPORT, 20]); + assert!(client.pending.lock().unwrap().is_empty()); + client.close().await; + } + + #[tokio::test] + async fn enqueue_caps_backlog_while_throttled() { + let exporter = ThrottlingExporter::new(usize::MAX); + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 1, + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) + .expect("telemetry runtime should start"); + + client.enqueue(sample_log("first")); + wait_until(|| client.throttled()).await; + for i in 0..MAX_PENDING_EVENTS + 10 { + client.enqueue(sample_log(&format!("e{i}"))); + } + + { + let pending = client.pending.lock().unwrap(); + assert_eq!(pending.len(), MAX_PENDING_EVENTS); + assert_eq!(pending[0].frontend_log_event_id, "e10"); + assert_eq!( + pending[MAX_PENDING_EVENTS - 1].frontend_log_event_id, + format!("e{}", MAX_PENDING_EVENTS + 9) + ); + } + assert_eq!( + client.queue_count.load(Ordering::Acquire), + MAX_PENDING_EVENTS + ); + assert_eq!(client.dropped.load(Ordering::Relaxed), 11); + client.close().await; + } + + #[tokio::test] + async fn close_stops_at_first_failed_post_and_clears_the_rest() { + let exporter = ThrottlingExporter::new(usize::MAX); + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 2, + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new(&config, Arc::new(exporter.clone())) + .expect("telemetry runtime should start"); + let backlog = (0..120) + .map(|i| sample_log(&format!("e{i}"))) + .collect::>(); + client.queue_count.store(backlog.len(), Ordering::Release); + *client.pending.lock().unwrap() = backlog; + + client.close().await; + + assert_eq!(exporter.ids().len(), 1); + assert_eq!(exporter.ids()[0].len(), MAX_EVENTS_PER_EXPORT); + assert!(client.pending.lock().unwrap().is_empty()); + assert_eq!(client.queue_count.load(Ordering::Acquire), 0); + } + + #[tokio::test] + async fn timed_out_flush_does_not_requeue_in_flight_batch() { + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 100, + flush_interval: Duration::from_secs(60), + close_flush_timeout: Duration::from_millis(50), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new( + &config, + Arc::new(SlowThrottlingExporter::new(Duration::from_secs(3600))), + ) + .expect("telemetry runtime should start"); + client.enqueue(sample_log("one")); + client.enqueue(sample_log("two")); + + // The server may already have accepted a batch whose response came + // too late, so a cancelled export is not sent again. + client.flush_bounded().await; + + assert!(client.pending.lock().unwrap().is_empty()); + assert_eq!(client.queue_count.load(Ordering::Acquire), 0); + } + + #[tokio::test] + async fn background_flush_flag_covers_the_running_flush() { + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 1, + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let exporter = Arc::new(SlowThrottlingExporter::new(Duration::from_millis(100))); + let client = TelemetryClient::new(&config, exporter.clone()) + .expect("telemetry runtime should start"); + + client.enqueue(sample_log("one")); + wait_until(|| exporter.calls.load(Ordering::Acquire) == 1).await; + // Enqueues while the flush runs don't start more background flushes. + assert!(client.background_flushing.load(Ordering::Acquire)); + wait_until(|| !client.background_flushing.load(Ordering::Acquire)).await; + assert_eq!(exporter.calls.load(Ordering::Acquire), 1); + client.close().await; + } + + #[tokio::test] + async fn throttled_export_finishing_after_close_is_dropped() { + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 1, + flush_interval: Duration::from_secs(60), + close_flush_timeout: Duration::from_millis(20), + ..TelemetryConfig::default() + }; + let exporter = Arc::new(SlowThrottlingExporter::new(Duration::from_millis(200))); + let client = TelemetryClient::new(&config, exporter.clone()) + .expect("telemetry runtime should start"); + client.enqueue(sample_log("one")); + wait_until(|| exporter.calls.load(Ordering::Acquire) == 1).await; + + // close() gives up waiting for the in-flight background export. + client.close().await; + assert_eq!(client.state.load(Ordering::Acquire), STATE_CLOSED); + wait_until(|| client.flush_lock.try_lock().is_ok()).await; + + assert!(client.pending.lock().unwrap().is_empty()); + assert_eq!(client.queue_count.load(Ordering::Acquire), 0); + } + + /// Answers every export with a throttle after `delay`. + struct SlowThrottlingExporter { + delay: Duration, + calls: AtomicUsize, + } + + impl SlowThrottlingExporter { + fn new(delay: Duration) -> Self { + Self { + delay, + calls: AtomicUsize::new(0), + } + } + } + + impl TelemetryExporter for SlowThrottlingExporter { + fn export<'a>( + &'a self, + _request: TelemetryRequest, + ) -> std::pin::Pin< + Box< + dyn std::future::Future + + Send + + 'a, + >, + > { + Box::pin(async move { + self.calls.fetch_add(1, Ordering::AcqRel); + tokio::time::sleep(self.delay).await; + Err(TelemetryExportError::Throttled("HTTP 429".to_string())) + }) + } + } + + #[tokio::test] + async fn requeue_keeps_oldest_first_and_caps_backlog() { + let config = TelemetryConfig { + enabled: Some(true), + batch_size: 2, + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new(&config, Arc::new(ThrottlingExporter::new(0))) + .expect("telemetry runtime should start"); + *client.pending.lock().unwrap() = (0..5).map(|i| sample_log(&format!("new{i}"))).collect(); + + client.requeue( + (0..MAX_PENDING_EVENTS - 2) + .map(|i| sample_log(&format!("old{i}"))) + .collect(), + ); + + let pending = client.pending.lock().unwrap(); + assert_eq!(pending.len(), MAX_PENDING_EVENTS); + assert_eq!(pending[0].frontend_log_event_id, "old3"); + assert_eq!( + pending[MAX_PENDING_EVENTS - 1].frontend_log_event_id, + "new4" + ); + assert_eq!(client.dropped.load(Ordering::Relaxed), 3); + assert_eq!(client.queue_count.load(Ordering::Acquire), pending.len()); + } + + #[tokio::test] + async fn backoff_doubles_up_to_cap() { + let config = TelemetryConfig { + enabled: Some(true), + flush_interval: Duration::from_secs(60), + ..TelemetryConfig::default() + }; + let client = TelemetryClient::new(&config, Arc::new(ThrottlingExporter::new(0))) + .expect("telemetry runtime should start"); + + let mut seen = Vec::new(); + for _ in 0..10 { + client.back_off(); + seen.push(client.throttle.lock().unwrap().backoff); + } + + assert_eq!(seen[0], INITIAL_THROTTLE_BACKOFF); + assert_eq!(seen[1], INITIAL_THROTTLE_BACKOFF * 2); + assert_eq!(*seen.last().unwrap(), MAX_THROTTLE_BACKOFF); + } + + /// Throttles the first `throttled` exports, then accepts; records the + /// event ids of every export attempt. + #[derive(Clone)] + struct ThrottlingExporter { + throttled: usize, + attempts: Arc>>>, + } + + impl ThrottlingExporter { + fn new(throttled: usize) -> Self { + Self { + throttled, + attempts: Arc::default(), + } + } + + fn ids(&self) -> Vec> { + self.attempts.lock().unwrap().clone() + } + } + + impl TelemetryExporter for ThrottlingExporter { + fn export<'a>( + &'a self, + request: TelemetryRequest, + ) -> std::pin::Pin< + Box< + dyn std::future::Future + + Send + + 'a, + >, + > { + Box::pin(async move { + let ids = request + .proto_logs + .iter() + .map(|log| { + serde_json::from_str::(log).unwrap() + ["frontend_log_event_id"] + .as_str() + .unwrap() + .to_string() + }) + .collect(); + let mut attempts = self.attempts.lock().unwrap(); + attempts.push(ids); + if attempts.len() <= self.throttled { + Err(TelemetryExportError::Throttled("HTTP 429".to_string())) + } else { + Ok(()) + } + }) + } + } + + async fn wait_until(mut condition: impl FnMut() -> bool) { + tokio::time::timeout(Duration::from_secs(2), async { + while !condition() { + tokio::time::sleep(Duration::from_millis(5)).await; + } + }) + .await + .expect("condition should become true"); + } + fn sample_log(id: &str) -> TelemetryFrontendLog { TelemetryFrontendLog { frontend_log_event_id: id.to_string(), diff --git a/src/telemetry/databricks.rs b/src/telemetry/databricks.rs index 2c3a4c14..818a7cb5 100644 --- a/src/telemetry/databricks.rs +++ b/src/telemetry/databricks.rs @@ -16,6 +16,7 @@ use super::exporter::{TelemetryExportError, TelemetryExportResult, TelemetryExpo use super::models::TelemetryRequest; use crate::client::retry::RequestType; use crate::client::DatabricksHttpClient; +use crate::kernel_error::Error as KernelError; use reqwest::header::{HeaderValue, CONTENT_TYPE}; use reqwest::{Body, Method, Request, Url}; use std::future::Future; @@ -70,11 +71,18 @@ impl TelemetryExporter for DatabricksTelemetryExporter { .execute(request, RequestType::TelemetryExport) .await .map(|_| ()) - .map_err(|e| TelemetryExportError::Export(e.to_string())) + .map_err(export_error) }) } } +fn export_error(error: KernelError) -> TelemetryExportError { + match error.http_status { + Some(429 | 503) => TelemetryExportError::Throttled(error.to_string()), + _ => TelemetryExportError::Export(error.to_string()), + } +} + fn build_export_request( endpoint_url: &str, request: &TelemetryRequest, @@ -102,6 +110,7 @@ fn endpoint_url(host: &str, endpoint: TelemetryEndpoint) -> String { #[cfg(test)] mod tests { use super::*; + use std::time::Duration; #[test] fn endpoint_url_normalizes_host_and_endpoint() { @@ -118,6 +127,91 @@ mod tests { ); } + #[test] + fn rate_limit_statuses_classify_as_throttled() { + for status in [429, 503] { + assert!(matches!( + export_error(KernelError::from_http_status(status)), + TelemetryExportError::Throttled(_) + )); + } + for status in [400, 500] { + assert!(matches!( + export_error(KernelError::from_http_status(status)), + TelemetryExportError::Export(_) + )); + } + assert!(matches!( + export_error(KernelError::network_error()), + TelemetryExportError::Export(_) + )); + } + + /// Export through a real `DatabricksHttpClient` (with its retry loop) + /// against a mock `/telemetry-ext` that always answers `status`. + async fn export_against(status: u16, overall_timeout: Duration) -> TelemetryExportResult { + use crate::auth::PersonalAccessToken; + use crate::client::retry::RetryConfigOverrides; + use crate::client::retry::{build_retry_configs, RequestCategory, RetryConfig}; + use crate::client::HttpClientConfig; + use std::collections::HashMap; + use wiremock::matchers::{method, path}; + use wiremock::{Mock, MockServer, ResponseTemplate}; + + let server = MockServer::start().await; + Mock::given(method("POST")) + .and(path("/telemetry-ext")) + .respond_with(ResponseTemplate::new(status).insert_header("Retry-After", "1")) + .mount(&server) + .await; + let overrides = HashMap::from([( + RequestCategory::Telemetry, + RetryConfigOverrides { + min_wait: Some(Duration::from_millis(10)), + max_wait: Some(Duration::from_millis(10)), + overall_timeout: Some(overall_timeout), + max_retries: Some(2), + ..Default::default() + }, + )]); + let client = DatabricksHttpClient::new( + HttpClientConfig::default(), + build_retry_configs(&RetryConfig::default(), &overrides), + ) + .unwrap(); + client.set_auth_provider(Arc::new(PersonalAccessToken::new("token".to_string()))); + let exporter = DatabricksTelemetryExporter::new( + &server.uri(), + Arc::new(client), + TelemetryEndpoint::Authenticated, + ); + exporter + .export(TelemetryRequest { + upload_time: 1, + proto_logs: vec!["{}".to_string()], + }) + .await + } + + #[tokio::test] + async fn rate_limited_export_through_retry_loop_is_throttled() { + for status in [429, 503] { + // Retries exhausted, and retry budget exceeded before a retry. + for budget in [Duration::from_secs(10), Duration::from_millis(1)] { + let result = export_against(status, budget).await; + assert!( + matches!(result, Err(TelemetryExportError::Throttled(_))), + "HTTP {status}, budget {budget:?}: {result:?}" + ); + } + } + let result = export_against(500, Duration::from_secs(10)).await; + assert!( + matches!(result, Err(TelemetryExportError::Export(_))), + "{result:?}" + ); + } + #[test] fn build_export_request_sets_method_url_json_body_and_content_type() { let telemetry_request = TelemetryRequest { diff --git a/src/telemetry/exporter.rs b/src/telemetry/exporter.rs index 536ced0e..c1fa2cff 100644 --- a/src/telemetry/exporter.rs +++ b/src/telemetry/exporter.rs @@ -25,6 +25,10 @@ pub(crate) enum TelemetryExportError { Serialization(#[from] serde_json::Error), #[error("telemetry export failed: {0}")] Export(String), + /// The endpoint rate-limited the export (HTTP 429 or 503). The batch was + /// not accepted and can be sent again later. + #[error("telemetry export throttled: {0}")] + Throttled(String), } pub(crate) trait TelemetryExporter: Send + Sync { @@ -36,17 +40,13 @@ pub(crate) trait TelemetryExporter: Send + Sync { pub(super) async fn export_batch( exporter: Arc, - batch: Vec, -) { - let request = match TelemetryRequest::from_logs(&batch) { - Ok(request) => request, - Err(e) => { - tracing::debug!(error = %e, "failed to serialize telemetry batch"); - return; - } - }; + batch: &[TelemetryFrontendLog], +) -> TelemetryExportResult { + let request = TelemetryRequest::from_logs(batch).inspect_err(|e| { + tracing::debug!(error = %e, "failed to serialize telemetry batch"); + })?; - if let Err(e) = exporter.export(request).await { + exporter.export(request).await.inspect_err(|e| { tracing::debug!(error = %e, "failed to export telemetry batch"); - } + }) }