diff --git a/src/client/http.rs b/src/client/http.rs index f164215a..58922b38 100644 --- a/src/client/http.rs +++ b/src/client/http.rs @@ -829,6 +829,12 @@ impl DatabricksHttpClient { let method = request.method().clone(); let url = request.url().clone(); let headers = request.headers().clone(); + // Preserve any per-request timeout the caller set on the incoming + // request (e.g. the CloudFetch download path caps each attempt at + // the remaining chunk budget). Rebuilding the request per attempt + // below would otherwise silently drop it and fall back to the + // client-wide `.timeout()`, defeating the deadline (audit H05). + let per_request_timeout = request.timeout().copied(); let body_bytes = request .body() .and_then(|b| b.as_bytes()) @@ -896,6 +902,12 @@ impl DatabricksHttpClient { req_builder = req_builder.body(body.clone()); } + // Re-apply the caller's per-request timeout on every rebuilt + // attempt so it survives the per-attempt request reconstruction. + if let Some(timeout) = per_request_timeout { + req_builder = req_builder.timeout(timeout); + } + let request = req_builder.build().map_err(|e| { Error::invalid_argument().message(format!("Failed to build request: {e}")) })?; diff --git a/src/reader/cloudfetch/chunk_downloader.rs b/src/reader/cloudfetch/chunk_downloader.rs index b0aa8ac9..518a5a8d 100644 --- a/src/reader/cloudfetch/chunk_downloader.rs +++ b/src/reader/cloudfetch/chunk_downloader.rs @@ -28,7 +28,7 @@ use crate::types::sea::CompressionCodec; use arrow_array::RecordBatch; use reqwest::Method; use std::sync::Arc; -use std::time::Instant; +use std::time::{Duration, Instant}; /// Downloads Arrow data from cloud storage presigned URLs. /// @@ -67,15 +67,30 @@ impl ChunkDownloader { /// /// # Arguments /// * `link` - CloudFetch link containing URL and headers + /// * `attempt_timeout` - Optional hard cap for this single HTTP + /// attempt, applied via reqwest's per-request timeout. It bounds + /// the whole attempt — connect, waiting for response headers, and + /// consuming the response body — so a stall in any phase can't + /// exceed it. Because reqwest's per-request timeout *overrides* the + /// client-wide one, we apply `min(request_timeout, attempt_timeout)` + /// so the static per-request safety net is never loosened. `None` + /// falls back to the HTTP client's static `request_timeout`. The + /// caller ([`download_chunk_with_retry`](crate::reader::cloudfetch)) + /// passes the remaining end-to-end chunk budget here so no attempt + /// outruns the deadline (audit H05). /// /// # Returns /// Parsed RecordBatches from the downloaded Arrow IPC data - pub async fn download(&self, link: &CloudFetchLink) -> Result> { + pub async fn download( + &self, + link: &CloudFetchLink, + attempt_timeout: Option, + ) -> Result> { let start = Instant::now(); debug!( - "Downloading chunk {} from {} ({} bytes expected)", - link.chunk_index, link.url, link.byte_count + "Downloading chunk {} from {} ({} bytes expected, attempt_timeout {:?})", + link.chunk_index, link.url, link.byte_count, attempt_timeout ); // Build request with custom headers from the link @@ -85,6 +100,19 @@ impl ChunkDownloader { request_builder = request_builder.header(key, value); } + // Cap this single attempt at the remaining chunk budget. reqwest's + // per-request timeout spans connect → headers → body, so a stall at + // any phase is bounded rather than the static request_timeout. It + // *overrides* (does not compose with) the client-wide `.timeout()`, + // so take `min(request_timeout, remaining)`: the remaining budget is + // the tighter bound with today's defaults, but the min preserves the + // static per-request safety net if `download_timeout` is ever + // configured larger than `request_timeout`. + if let Some(timeout) = attempt_timeout { + let request_timeout = self.http_client.config().read_timeout; + request_builder = request_builder.timeout(timeout.min(request_timeout)); + } + let request = request_builder .build() .map_err(|e| Error::io().message(format!("Failed to build download request: {}", e)))?; diff --git a/src/reader/cloudfetch/streaming_provider.rs b/src/reader/cloudfetch/streaming_provider.rs index fc0d3c5d..aefd7b51 100644 --- a/src/reader/cloudfetch/streaming_provider.rs +++ b/src/reader/cloudfetch/streaming_provider.rs @@ -224,6 +224,11 @@ struct DownloadCtx { metrics: Arc, max_retries: u32, retry_delay: std::time::Duration, + /// Absolute end-to-end wall-clock budget for downloading one chunk, + /// spanning every retry, the backoff sleeps, and each HTTP attempt. + /// See [`CloudFetchConfig::download_timeout`]. Enforced in + /// [`StreamingCloudFetchProvider::download_chunk_with_retry`]. + download_timeout: std::time::Duration, } #[derive(Debug)] @@ -395,6 +400,41 @@ impl StreamingCloudFetchProvider { provider } + /// Test-only constructor: build a provider around a pre-seeded + /// `chunks` map WITHOUT spawning the background `initialize` + /// task. Lets consumer-path tests (`wait_for_chunk`) observe the + /// exact `ChunkState` they seed. The injected `link_fetcher` / + /// `chunk_downloader` are never exercised by these tests. + #[cfg(test)] + fn for_test_with_chunks( + link_fetcher: Arc, + chunk_downloader: Arc, + chunks: Arc>, + end_of_stream: bool, + ) -> Arc { + let config = CloudFetchConfig::default(); + Arc::new(Self { + link_fetcher, + chunk_downloader, + max_chunks_in_memory: config.max_chunks_in_memory, + config, + schema: OnceLock::new(), + current_chunk_index: AtomicI64::new(0), + current_batch_buffer: Mutex::new(VecDeque::new()), + next_download_index: AtomicI64::new(0), + end_of_stream: AtomicBool::new(end_of_stream), + chunks, + chunks_in_memory: AtomicUsize::new(0), + chunk_state_changed: Arc::new(Notify::new()), + terminal_error: Mutex::new(None), + cancel_token: CancellationToken::new(), + runtime_handle: tokio::runtime::Handle::current(), + statement_id: None, + refresh_coordinator: Arc::new(RefreshCoordinator::default()), + metrics: Arc::new(CloudFetchMetrics::new(None)), + }) + } + /// Initialize the provider by fetching initial links and starting downloads. /// /// This runs as a background task immediately after creation to: @@ -716,6 +756,7 @@ impl StreamingCloudFetchProvider { metrics: Arc::clone(&self.metrics), max_retries: self.config.max_retries, retry_delay: self.config.retry_delay, + download_timeout: self.config.download_timeout, }; let chunk_state_changed = Arc::clone(&self.chunk_state_changed); let chunks = self.chunks.clone(); @@ -769,9 +810,13 @@ impl StreamingCloudFetchProvider { Err(e) => { error!("Failed to download chunk {}: {}", chunk_index, e); if let Some(mut entry) = chunks.get_mut(&chunk_index) { - let msg = e.to_string(); - entry.state = ChunkState::DownloadFailed(msg.clone()); - entry.last_failure_reason = Some(msg); + // Preserve the full typed error in the state so + // its `ErrorCode` (e.g. `Timeout` for a deadline) + // survives to the consumer path — see H05. Keep a + // stringified copy in `last_failure_reason` for the + // operator breadcrumb that outlives the state. + entry.last_failure_reason = Some(e.to_string()); + entry.state = ChunkState::DownloadFailed(e); } } } @@ -781,18 +826,53 @@ impl StreamingCloudFetchProvider { } } - /// Download a single chunk with retry and link refresh on expiry. + /// Stable terminal error for a chunk whose end-to-end download budget + /// ([`CloudFetchConfig::download_timeout`]) was exhausted. Classified as + /// [`Error::timeout`] so consumers (and the C ABI / ODBC bridge) get a + /// consistent, retryable timeout classification rather than an opaque + /// I/O error (audit H05). + fn download_deadline_error(chunk_index: i64, budget: std::time::Duration) -> Error { + Error::timeout().message(format!( + "CloudFetch chunk {} download exceeded the {:.1}s end-to-end deadline", + chunk_index, + budget.as_secs_f64() + )) + } + + /// Download a single chunk with retry and link refresh on expiry, + /// bounded end-to-end by [`CloudFetchConfig::download_timeout`]. async fn download_chunk_with_retry( chunk_index: i64, ctx: &DownloadCtx, ) -> Result> { let mut attempts = 0; + // Absolute end-to-end deadline for this chunk. Bounds the whole + // retry loop — every HTTP attempt, link refresh, and backoff sleep + // — so a permanently-stalled chunk surfaces a terminal timeout in + // bounded time instead of stacking `max_retries × request_timeout` + // into minutes (audit H05). A zero budget disables the deadline + // (falls back to attempt-count bounding only). + let deadline = (!ctx.download_timeout.is_zero()) + .then(|| std::time::Instant::now() + ctx.download_timeout); + loop { if ctx.cancel_token.is_cancelled() { return Err(Error::invalid_state().message("Download cancelled")); } + // Hard deadline check at the top of every attempt: if the + // budget is spent, stop promptly with a stable timeout error + // rather than starting another attempt or refresh. + if let Some(deadline) = deadline { + if std::time::Instant::now() >= deadline { + return Err(Self::download_deadline_error( + chunk_index, + ctx.download_timeout, + )); + } + } + // Get link (may need to refresh the whole forward batch // if expired). We always go via the batched refresh path: // refreshing all in-flight chunks in one RPC matches the @@ -813,15 +893,32 @@ impl StreamingCloudFetchProvider { triggering batched refresh", chunk_index ); - Self::refresh_links_batch( + // Bound the refresh RPC by the remaining chunk + // budget so a stalled refresh can't overshoot the + // end-to-end deadline by its own (much larger) + // per-request `request_timeout` (audit H05). This + // keeps the deadline genuinely end-to-end across + // the link-refresh step, matching the loop-level + // comment above. + let refresh = Self::refresh_links_batch( chunk_index, &ctx.link_fetcher, &ctx.chunks, &ctx.refresh_coordinator, &ctx.cancel_token, ctx.statement_id.as_deref(), - ) - .await + ); + match deadline { + Some(d) => tokio::time::timeout( + d.saturating_duration_since(std::time::Instant::now()), + refresh, + ) + .await + .map_err(|_| { + Self::download_deadline_error(chunk_index, ctx.download_timeout) + })?, + None => refresh.await, + } .map_err(|e| { // Wrap with chunk_index breadcrumb so the // downstream `error!("Failed to download @@ -914,9 +1011,15 @@ impl StreamingCloudFetchProvider { } }; - // Attempt download + // Attempt download. Cap this attempt at the remaining budget so + // a stalled connect / headers / body can't outrun the deadline + // (reqwest's per-request timeout spans all three). The downloader + // applies `min(request_timeout, remaining)` so the static + // per-request safety net is never loosened. `None` when no + // deadline is configured. let download_started = std::time::Instant::now(); - match ctx.downloader.download(&link).await { + let attempt_timeout = deadline.map(|d| d.saturating_duration_since(download_started)); + match ctx.downloader.download(&link, attempt_timeout).await { Ok(batches) => { ctx.metrics .record_chunk_download(chunk_index, download_started.elapsed()); @@ -1002,7 +1105,17 @@ impl StreamingCloudFetchProvider { entry.last_failure_reason = Some(e.to_string()); } - tokio::time::sleep(ctx.retry_delay).await; + // Never sleep past the deadline: cap the backoff at the + // remaining budget so the next iteration's deadline check + // fires promptly instead of after a full `retry_delay` + // overshoot. + let sleep_for = match deadline { + Some(d) => ctx + .retry_delay + .min(d.saturating_duration_since(std::time::Instant::now())), + None => ctx.retry_delay, + }; + tokio::time::sleep(sleep_for).await; } } } @@ -1425,7 +1538,11 @@ impl StreamingCloudFetchProvider { if let Some(entry) = self.chunks.get(&chunk_index) { match &entry.state { ChunkState::Downloaded => return Ok(()), - ChunkState::DownloadFailed(e) => return Err(Error::io().message(e.clone())), + // Return the preserved typed error verbatim so the + // consumer (including the C ABI / ODBC bridge) sees the + // original `ErrorCode` — e.g. `Timeout` for a deadline — + // rather than an opaque re-wrapped `Io` error (H05). + ChunkState::DownloadFailed(e) => return Err(e.clone()), ChunkState::ProcessingFailed(e) => return Err(Error::io().message(e.clone())), ChunkState::Cancelled => { return Err(Error::invalid_state().message("Operation cancelled")) @@ -1716,6 +1833,9 @@ mod tests { metrics: Arc::new(CloudFetchMetrics::new(Some(1))), max_retries: MAX_DOWNLOAD_ATTEMPTS, retry_delay: std::time::Duration::ZERO, + // This test isolates the attempt-count budget; disable the + // wall-clock deadline (0 = disabled) so it doesn't interfere. + download_timeout: std::time::Duration::ZERO, }; let result = tokio::time::timeout( @@ -1737,6 +1857,319 @@ mod tests { ); } + // ─── End-to-end download deadline (issue #321 / audit H05) ─────────── + // + // These tests exercise `download_chunk_with_retry`'s absolute + // `download_timeout` deadline: a stalled connection / headers / body + // and repeated failures must not let a single chunk outrun the budget. + // Each drives the real `ChunkDownloader` against a `wiremock` server so + // reqwest's per-request timeout (the actual cap) is genuinely exercised, + // not mocked away. + mod download_deadline { + use super::*; + use crate::client::retry::{build_retry_configs, RetryConfig}; + use crate::client::{DatabricksHttpClient, HttpClientConfig}; + use crate::kernel_error::ErrorCode; + use crate::types::sea::CompressionCodec; + use std::time::{Duration, Instant}; + use wiremock::matchers::{method, path}; + use wiremock::{Mock, MockServer, ResponseTemplate}; + + /// A link fetcher that only ever serves chunk 0's link pointing at + /// `url`. Refresh paths are unreachable — the link never expires + /// (1h out), so these tests isolate the download deadline from any + /// refresh behaviour. + #[derive(Debug)] + struct SingleLinkFetcher { + url: String, + } + + #[async_trait::async_trait] + impl ChunkLinkFetcher for SingleLinkFetcher { + async fn fetch_links(&self, _: i64, _: i64) -> Result { + unreachable!("deadline tests seed the chunks map directly") + } + async fn refetch_link(&self, _: i64, _: i64) -> Result { + unreachable!("link never expires in these tests") + } + async fn refetch_links_from(&self, _: i64, _: i64) -> Result> { + unreachable!("link never expires in these tests") + } + } + + impl SingleLinkFetcher { + fn link(&self) -> CloudFetchLink { + CloudFetchLink { + url: self.url.clone(), + ..create_test_link(0) + } + } + } + + /// Build a `DownloadCtx` whose downloader targets `server`, with the + /// given end-to-end `download_timeout` and (large) `max_retries` so + /// the wall-clock deadline — not the attempt count — is what bounds + /// the loop. The inner HTTP client's own retries are disabled so the + /// provider is the sole retry authority (mirrors production). + fn ctx_for( + server: &MockServer, + download_timeout: Duration, + cancel_token: CancellationToken, + ) -> DownloadCtx { + let http_client = Arc::new( + DatabricksHttpClient::new( + HttpClientConfig::default(), + build_retry_configs( + &RetryConfig { + max_retries: 0, + ..RetryConfig::default() + }, + &HashMap::new(), + ), + ) + .unwrap(), + ); + let downloader = Arc::new(ChunkDownloader::new( + http_client, + CompressionCodec::None, + 0.1, + )); + let fetcher = Arc::new(SingleLinkFetcher { + url: format!("{}/chunk-0.arrow", server.uri()), + }); + let chunks = Arc::new(DashMap::new()); + chunks.insert(0, entry_with_state(fetcher.link(), ChunkState::Downloading)); + DownloadCtx { + downloader, + link_fetcher: fetcher, + chunks, + refresh_coordinator: Arc::new(RefreshCoordinator::default()), + cancel_token, + statement_id: None, + metrics: Arc::new(CloudFetchMetrics::new(Some(1))), + // Large attempt budget so ONLY the wall-clock deadline can + // stop the loop — proves the deadline, not the counter. + max_retries: 10_000, + retry_delay: Duration::from_millis(20), + download_timeout, + } + } + + /// A stalled response body (headers arrive, body never completes) + /// must be cut off by the deadline, not the static request_timeout. + /// Without the per-attempt cap this hangs for the client's + /// `request_timeout` (default 120s) per attempt. + #[tokio::test] + async fn stalled_body_is_bounded_by_deadline() { + let server = MockServer::start().await; + // Delay far beyond the budget: stands in for a body that never + // makes progress. The per-attempt timeout must fire first. + Mock::given(method("GET")) + .and(path("/chunk-0.arrow")) + .respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(30))) + .mount(&server) + .await; + + let budget = Duration::from_millis(300); + let ctx = ctx_for(&server, budget, CancellationToken::new()); + + let started = Instant::now(); + // Outer guard is ~10x the budget: if the deadline is NOT + // enforced this trips and fails loudly instead of hanging CI. + let result = tokio::time::timeout( + Duration::from_secs(3), + StreamingCloudFetchProvider::download_chunk_with_retry(0, &ctx), + ) + .await + .expect("a stalled body must be bounded by the download deadline, not hang"); + let elapsed = started.elapsed(); + + let err = result.expect_err("a stalled body must surface a terminal error"); + assert_eq!( + err.code, + ErrorCode::Timeout, + "deadline expiry must be classified as a timeout, got: {err}" + ); + assert!( + err.message.contains("deadline"), + "unexpected error message: {err}" + ); + // Elapsed stays within a documented scheduling tolerance of the + // budget (≤ budget + generous slack for scheduler/backoff). + assert!( + elapsed < budget + Duration::from_secs(1), + "elapsed {elapsed:?} should stay within tolerance of the {budget:?} budget" + ); + } + + /// Repeated failures (persistent HTTP 500, each returned promptly) + /// must stop at the wall-clock deadline rather than running the full + /// attempt count. Proves the deadline bounds the retry/backoff loop. + #[tokio::test] + async fn retry_exhaustion_is_bounded_by_deadline() { + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/chunk-0.arrow")) + .respond_with(ResponseTemplate::new(500)) + .mount(&server) + .await; + + let budget = Duration::from_millis(250); + let ctx = ctx_for(&server, budget, CancellationToken::new()); + + let started = Instant::now(); + let result = tokio::time::timeout( + Duration::from_secs(3), + StreamingCloudFetchProvider::download_chunk_with_retry(0, &ctx), + ) + .await + .expect("retry loop must stop at the deadline, not run 10k attempts"); + let elapsed = started.elapsed(); + + let err = result.expect_err("persistent 500 within the budget must be terminal"); + assert_eq!( + err.code, + ErrorCode::Timeout, + "budget exhaustion must be classified as a timeout, got: {err}" + ); + assert!( + elapsed < budget + Duration::from_secs(1), + "elapsed {elapsed:?} should stay within tolerance of the {budget:?} budget" + ); + // Far fewer than max_retries attempts — the deadline, not the + // 10k counter, stopped the loop. + let attempts = server.received_requests().await.unwrap().len(); + assert!( + attempts < 100, + "deadline should stop the loop well before the 10k attempt cap, got {attempts}" + ); + } + + /// A cancellation while a chunk is retrying under a live deadline + /// must be observed promptly (at the next loop boundary) and win — + /// the loop must not run out the remaining budget once cancelled. + /// The chunk keeps failing fast (HTTP 500 returned immediately), so + /// attempts turn over quickly and the loop-top cancel check fires + /// well before the multi-second budget would. + #[tokio::test] + async fn cancel_under_live_deadline_returns_promptly() { + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/chunk-0.arrow")) + .respond_with(ResponseTemplate::new(500)) + .mount(&server) + .await; + + // Long budget: only cancellation, not the deadline, should end + // this loop. If cancel is NOT observed, the loop would spin on + // fast 500s until the 10s budget — the outer 3s guard catches + // that regression. + let cancel = CancellationToken::new(); + let ctx = ctx_for(&server, Duration::from_secs(10), cancel.clone()); + + let canceller = cancel.clone(); + tokio::spawn(async move { + tokio::time::sleep(Duration::from_millis(50)).await; + canceller.cancel(); + }); + + let started = Instant::now(); + let result = tokio::time::timeout( + Duration::from_secs(3), + StreamingCloudFetchProvider::download_chunk_with_retry(0, &ctx), + ) + .await + .expect("cancellation must be observed promptly, not run out the budget"); + let elapsed = started.elapsed(); + + let err = result.expect_err("a cancelled download must be an error"); + // The "Download cancelled" arm uses `Error::invalid_state()`, + // which maps to `ErrorCode::InvalidArgument`. + assert_eq!( + err.code, + ErrorCode::InvalidArgument, + "cancel should win over the (far-off) deadline, got: {err}" + ); + assert!( + err.message.contains("cancelled"), + "unexpected error message: {err}" + ); + // Returned near the cancel time, nowhere near the 10s budget. + assert!( + elapsed < Duration::from_secs(1), + "cancel should return promptly (~50ms), got {elapsed:?}" + ); + } + + /// H05 end-to-end: the deadline error's `ErrorCode::Timeout` + /// classification must survive the chunk-state machine and reach + /// the *consumer* path (`wait_for_chunk`), not just the direct + /// return of `download_chunk_with_retry`. Before the fix, + /// `ChunkState::DownloadFailed(String)` dropped the code and + /// `wait_for_chunk` re-wrapped it as `Error::io()` + /// (`ErrorCode::Internal`) — the very "opaque I/O error" the doc + /// promised consumers would not see. + #[tokio::test] + async fn deadline_timeout_classification_survives_to_consumer() { + // Minimal dummy deps — never exercised, `wait_for_chunk` + // only reads the seeded chunk state. + let http_client = Arc::new( + DatabricksHttpClient::new( + HttpClientConfig::default(), + build_retry_configs(&RetryConfig::default(), &HashMap::new()), + ) + .unwrap(), + ); + let downloader = Arc::new(ChunkDownloader::new( + http_client, + CompressionCodec::None, + 0.1, + )); + let fetcher: Arc = Arc::new(SingleLinkFetcher { + url: "https://unused.example.com/chunk-0.arrow".to_string(), + }); + + // Seed chunk 0 exactly as the download task would on a + // deadline expiry: DownloadFailed carrying the typed + // timeout error. + let deadline_err = + StreamingCloudFetchProvider::download_deadline_error(0, Duration::from_millis(300)); + assert_eq!(deadline_err.code, ErrorCode::Timeout); + let chunks: Arc> = Arc::new(DashMap::new()); + chunks.insert( + 0, + ChunkEntry { + link: None, + state: ChunkState::DownloadFailed(deadline_err), + batches: None, + last_failure_reason: None, + }, + ); + + let provider = StreamingCloudFetchProvider::for_test_with_chunks( + fetcher, downloader, chunks, true, + ); + + let err = provider + .wait_for_chunk(0) + .await + .expect_err("a deadline-failed chunk must surface an error to the consumer"); + assert_eq!( + err.code, + ErrorCode::Timeout, + "consumer path must preserve the Timeout classification, got: {err}" + ); + assert!( + err.retryable, + "a preserved timeout error must keep its retryable=true flag" + ); + assert!( + err.message.contains("deadline"), + "the original deadline message must be preserved: {err}" + ); + } + } + #[test] fn test_chunk_link_fetch_result_end_of_stream() { let result = ChunkLinkFetchResult::end_of_stream(); @@ -2103,7 +2536,7 @@ mod tests { 7, entry_with_state( expired_link(7), - ChunkState::DownloadFailed("network glitch".into()), + ChunkState::DownloadFailed(Error::network_error().message("network glitch")), ), ); chunks.insert( diff --git a/src/session.rs b/src/session.rs index 1fb50b38..2618e4f5 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1003,6 +1003,22 @@ fn build_proxy_config(proxy: Option<&ProxyConfig>) -> HttpProxyConfig { } } +/// End-to-end wall-clock budget for downloading a single CloudFetch +/// chunk. This is the single source of truth shared by two layers so they +/// can't drift: +/// +/// - the CloudFetch HTTP category's `overall_timeout` in +/// [`build_retry_configs_for_session`] (bounds the inner HTTP client), and +/// - the provider's [`CloudFetchConfig::download_timeout`] in +/// [`result_to_cloudfetch_config`] (bounds the provider retry loop and +/// caps each HTTP attempt at the remaining budget). +/// +/// Before both layers shared this budget, the per-attempt HTTP timeout +/// (`request_timeout`, default 120s) times the provider's `max_retries` +/// let a permanently-stuck chunk block the consumer for ~10 minutes on a +/// nominal budget (audit H05). +const CLOUDFETCH_DOWNLOAD_BUDGET: Duration = Duration::from_secs(20); + /// Build the per-category retry config for the HTTP client. /// /// The global config is driven by the public [`HttpConfig`] retry @@ -1072,7 +1088,7 @@ fn build_retry_configs_for_session( overrides.insert( RequestCategory::CloudFetch, RetryConfigOverrides { - overall_timeout: Some(Duration::from_secs(20)), + overall_timeout: Some(CLOUDFETCH_DOWNLOAD_BUDGET), max_retries: Some(0), ..Default::default() }, @@ -1163,6 +1179,10 @@ fn result_to_cloudfetch_config( link_prefetch_window: result.cloudfetch_link_prefetch_window, max_chunks_in_memory: result.cloudfetch_max_chunks_in_memory, inline_max_chunks_in_memory: result.inline_max_chunks_in_memory, + // Share one budget with the CloudFetch HTTP category + // (see CLOUDFETCH_DOWNLOAD_BUDGET) so the provider retry loop and + // the inner HTTP client can't drift (audit H05). + download_timeout: CLOUDFETCH_DOWNLOAD_BUDGET, ..Default::default() } } @@ -1555,6 +1575,32 @@ mod retry_config_tests { Duration::from_secs(20), "CloudFetch keeps its tight wall-clock bound" ); + assert_eq!( + cf.overall_timeout, CLOUDFETCH_DOWNLOAD_BUDGET, + "HTTP CloudFetch budget must be the shared constant" + ); + } + + #[test] + fn cloudfetch_provider_and_http_budgets_are_unified() { + // The provider retry loop (CloudFetchConfig::download_timeout) and + // the inner HTTP client (CloudFetch overall_timeout) must both be + // the single shared budget so a stuck chunk is bounded end-to-end + // and the two layers can't drift (audit H05). + let result = ResultConfig::default(); + let cf = result_to_cloudfetch_config(&result); + assert_eq!( + cf.download_timeout, CLOUDFETCH_DOWNLOAD_BUDGET, + "provider download_timeout must be the shared budget" + ); + + let http_cf = build_retry_configs_for_session(&config_with_http(HttpConfig::default())) + [&RequestCategory::CloudFetch] + .overall_timeout; + assert_eq!( + cf.download_timeout, http_cf, + "provider and HTTP CloudFetch budgets must match" + ); } #[test] diff --git a/src/types/cloudfetch.rs b/src/types/cloudfetch.rs index 56406ed9..9dfcc9e3 100644 --- a/src/types/cloudfetch.rs +++ b/src/types/cloudfetch.rs @@ -44,6 +44,23 @@ pub struct CloudFetchConfig { pub max_retries: u32, /// Delay between retry attempts. pub retry_delay: Duration, + /// Absolute end-to-end wall-clock budget for downloading a single + /// chunk, spanning every retry, the backoff sleeps between them, + /// and each individual HTTP attempt (connect, response headers, and + /// response body). + /// + /// Enforced as a hard deadline in + /// `StreamingCloudFetchProvider::download_chunk_with_retry`: the + /// remaining budget caps each HTTP attempt (via reqwest's + /// per-request timeout) so a stalled connection, stalled headers, or + /// stalled body can never exceed it, and the loop stops retrying + /// once the deadline passes — returning a stable timeout error + /// promptly rather than blocking the consumer for minutes. + /// + /// Without this bound the per-attempt HTTP timeout (`request_timeout`, + /// default 120s) multiplied by `max_retries` let a permanently-stuck + /// chunk block for ~10 minutes on a nominal budget (audit H05). + pub download_timeout: Duration, /// Timeout for waiting for a chunk to be ready. pub chunk_ready_timeout: Option, /// Log warning if download speed falls below this threshold (MB/s). @@ -71,6 +88,13 @@ impl Default for CloudFetchConfig { max_chunks_in_memory: 16, max_retries: 5, retry_delay: Duration::from_millis(1500), + // End-to-end per-chunk download budget. Matches the CloudFetch + // HTTP-retry `overall_timeout` set in + // `session::build_retry_configs_for_session`, so the provider + // retry loop and the inner HTTP client share one budget instead + // of stacking multiplicatively (audit H05). `session` re-projects + // this from the same source so the two can't drift. + download_timeout: Duration::from_secs(20), chunk_ready_timeout: Some(Duration::from_secs(30)), speed_threshold_mbps: 0.1, enabled: true, @@ -170,8 +194,14 @@ pub enum ChunkState { Downloading, /// Downloaded successfully. Downloaded, - /// Download failed (will retry). - DownloadFailed(String), + /// Download failed (will retry). Carries the full [`Error`] — + /// not a stringified message — so the terminal classification + /// (`ErrorCode`, `retryable`, cause chain) survives to the + /// consumer path (`wait_for_chunk`). Stringifying here would + /// collapse an `ErrorCode::Timeout` deadline error into an + /// opaque `ErrorCode::Internal` when the consumer re-wrapped it + /// (audit H05). + DownloadFailed(Error), /// Retrying after failure. DownloadRetry, /// Arrow parse failed (terminal). @@ -238,6 +268,9 @@ mod tests { assert_eq!(config.max_retries, 5); assert!(config.enabled); assert_eq!(config.batch_merge_target_rows, 0); // Disabled by default + // End-to-end per-chunk download budget (audit H05). Matches the + // CloudFetch HTTP-retry overall_timeout so the two layers can't drift. + assert_eq!(config.download_timeout, Duration::from_secs(20)); } #[test]