Repository navigation
feat(telemetry): add result stream metrics #274
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
0895ace
ed75184
f1f811d
20ba26e
baebdda
93aa333
7543536
5b432e6
b6ac1c5
be55acf
232f3bb
a2505d7
31abeb7
5d08aff
de6e20c
60bcd55
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 |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ use crate::kernel_error::{Error, Result}; | |
| use crate::klog::{debug, error, info, warn}; | ||
| use crate::reader::cloudfetch::chunk_downloader::ChunkDownloader; | ||
| use crate::reader::cloudfetch::link_fetcher::ChunkLinkFetcher; | ||
| use crate::reader::ResultReaderMetrics; | ||
| use crate::types::cloudfetch::{ | ||
| ChunkEntry, ChunkState, CloudFetchConfig, DEFAULT_CHUNK_READY_TIMEOUT_SECS, | ||
| }; | ||
|
|
@@ -32,7 +33,7 @@ use arrow_schema::SchemaRef; | |
| use arrow_select::concat::concat_batches; | ||
| use dashmap::DashMap; | ||
| use std::collections::VecDeque; | ||
| use std::sync::atomic::{AtomicBool, AtomicI64, AtomicUsize, Ordering}; | ||
| use std::sync::atomic::{AtomicBool, AtomicI32, AtomicI64, AtomicUsize, Ordering}; | ||
| use std::sync::{Arc, Mutex, OnceLock}; | ||
| use tokio::sync::Notify; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
@@ -168,6 +169,8 @@ pub struct StreamingCloudFetchProvider { | |
| // `Arc<...>` so spawned download tasks can hold their own clone | ||
| // without keeping the whole provider alive past stream end. | ||
| refresh_coordinator: Arc<RefreshCoordinator>, | ||
|
|
||
| metrics: Arc<CloudFetchMetrics>, | ||
| } | ||
|
|
||
| /// Coordinates concurrent batched link-refresh attempts. | ||
|
|
@@ -201,10 +204,90 @@ struct DownloadCtx { | |
| refresh_coordinator: Arc<RefreshCoordinator>, | ||
| cancel_token: CancellationToken, | ||
| statement_id: Option<Arc<str>>, | ||
| metrics: Arc<CloudFetchMetrics>, | ||
| max_retries: u32, | ||
| retry_delay: std::time::Duration, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct CloudFetchMetrics { | ||
| total_chunks_present: Option<i32>, | ||
| total_chunks_iterated: AtomicI32, | ||
| chunks_downloaded: AtomicI32, | ||
| initial_chunk_latency_ms: AtomicI64, | ||
| slowest_chunk_latency_ms: AtomicI64, | ||
| sum_chunks_download_time_ms: AtomicI64, | ||
| } | ||
|
|
||
| impl CloudFetchMetrics { | ||
| fn new(total_chunks_present: Option<i32>) -> Self { | ||
| Self { | ||
| total_chunks_present, | ||
| total_chunks_iterated: AtomicI32::new(0), | ||
| chunks_downloaded: AtomicI32::new(0), | ||
| initial_chunk_latency_ms: AtomicI64::new(-1), | ||
| slowest_chunk_latency_ms: AtomicI64::new(0), | ||
| sum_chunks_download_time_ms: AtomicI64::new(0), | ||
| } | ||
| } | ||
|
|
||
| fn record_chunk_download(&self, chunk_index: i64, elapsed: std::time::Duration) { | ||
| let millis = elapsed.as_millis().try_into().unwrap_or(i64::MAX); | ||
| // `initial_chunk_latency` is the time to download chunk 0 | ||
| // specifically, not the first download to *complete*. Under | ||
| // concurrency the fastest/smallest chunk often finishes first, | ||
| // so a first-to-complete gate would report a misleading value. | ||
| // Gate strictly on `chunk_index == 0` to match the JDBC/C# | ||
| // drivers' `initialChunkLatency` semantic. | ||
| if chunk_index == 0 { | ||
| self.initial_chunk_latency_ms | ||
| .store(millis, Ordering::Release); | ||
| } | ||
| self.slowest_chunk_latency_ms | ||
| .fetch_max(millis, Ordering::AcqRel); | ||
| self.sum_chunks_download_time_ms | ||
| .fetch_add(millis, Ordering::AcqRel); | ||
| // Count completed downloads so `snapshot` can distinguish a | ||
| // real ~0ms download from "no download ever completed". Both | ||
| // `slowest`/`sum` atomics initialize to 0, so without this | ||
| // counter a stream dropped/errored before any download finished | ||
| // would report `Some(0)` — indistinguishable from a genuine | ||
| // 0ms download and skewing server-side latency aggregates. | ||
| self.chunks_downloaded.fetch_add(1, Ordering::AcqRel); | ||
| } | ||
|
|
||
| fn record_chunk_iterated(&self) { | ||
| self.total_chunks_iterated.fetch_add(1, Ordering::AcqRel); | ||
| } | ||
|
|
||
| fn snapshot(&self) -> ResultReaderMetrics { | ||
| let initial = self.initial_chunk_latency_ms.load(Ordering::Acquire); | ||
| let iterated = self.total_chunks_iterated.load(Ordering::Acquire); | ||
| // Only emit `slowest`/`sum` once at least one download has | ||
| // completed. Both atomics initialize to 0, so gating on this | ||
| // counter keeps a no-download snapshot reporting `None` rather | ||
| // than a misleading `Some(0)` — mirroring the `-1` sentinel / | ||
| // `None` treatment of `initial_chunk_latency` above. | ||
| let downloaded = self.chunks_downloaded.load(Ordering::Acquire) > 0; | ||
| ResultReaderMetrics { | ||
| total_chunks_present: self.total_chunks_present, | ||
| total_chunks_iterated: Some(iterated), | ||
| initial_chunk_latency: (initial >= 0) | ||
| .then(|| std::time::Duration::from_millis(initial as u64)), | ||
| slowest_chunk_latency: downloaded.then(|| { | ||
| std::time::Duration::from_millis( | ||
| self.slowest_chunk_latency_ms.load(Ordering::Acquire) as u64, | ||
| ) | ||
| }), | ||
| sum_chunks_download_time: downloaded.then(|| { | ||
| std::time::Duration::from_millis( | ||
| self.sum_chunks_download_time_ms.load(Ordering::Acquire) as u64, | ||
| ) | ||
| }), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::fmt::Debug for StreamingCloudFetchProvider { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("StreamingCloudFetchProvider") | ||
|
|
@@ -261,6 +344,7 @@ impl StreamingCloudFetchProvider { | |
| config: CloudFetchConfig, | ||
| link_fetcher: Arc<dyn ChunkLinkFetcher>, | ||
| chunk_downloader: Arc<ChunkDownloader>, | ||
| total_chunks_present: Option<i32>, | ||
| runtime_handle: tokio::runtime::Handle, | ||
| ) -> Arc<Self> { | ||
| let statement_id = link_fetcher.statement_id().map(Arc::from); | ||
|
|
@@ -281,6 +365,7 @@ impl StreamingCloudFetchProvider { | |
| runtime_handle, | ||
| statement_id, | ||
| refresh_coordinator: Arc::new(RefreshCoordinator::default()), | ||
| metrics: Arc::new(CloudFetchMetrics::new(total_chunks_present)), | ||
| }); | ||
|
|
||
| // Start background initialization that fetches links and schedules downloads | ||
|
|
@@ -379,6 +464,7 @@ impl StreamingCloudFetchProvider { | |
|
|
||
| // Move to next chunk | ||
| let next_chunk = self.current_chunk_index.fetch_add(1, Ordering::Release) + 1; | ||
| self.metrics.record_chunk_iterated(); | ||
|
|
||
| debug!( | ||
| "Released chunk {}: chunks_in_memory={}/{}, consumer advancing to {}", | ||
|
|
@@ -564,6 +650,7 @@ impl StreamingCloudFetchProvider { | |
| refresh_coordinator: Arc::clone(&self.refresh_coordinator), | ||
| cancel_token: self.cancel_token.clone(), | ||
| statement_id: self.statement_id.clone(), | ||
| metrics: Arc::clone(&self.metrics), | ||
| max_retries: self.config.max_retries, | ||
| retry_delay: self.config.retry_delay, | ||
| }; | ||
|
|
@@ -765,8 +852,13 @@ impl StreamingCloudFetchProvider { | |
| }; | ||
|
|
||
| // Attempt download | ||
| let download_started = std::time::Instant::now(); | ||
|
jay-xiao446 marked this conversation as resolved.
Resolved
jay-xiao446 marked this conversation as resolved.
Resolved
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 — The comment above 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. Confirmed against the JDBC reference driver: the current per-attempt timer placement is correct and no change is needed. In JDBC, the retry loop lives in StreamingChunkDownloadTask.call() (link-refresh + RETRY_DELAY_MS backoff), but telemetry is recorded inside ArrowResultChunk.downloadData(), which captures 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. The finding was explicitly a confirmation request against the reference drivers, and the analysis here is internally consistent — but everything load-bearing in it describes JDBC internals ( 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. Verified the JDBC reference source directly (the review-bot said it lacked access). ArrowResultChunk.downloadData() captures 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 appreciate the detailed walkthrough of the JDBC internals, but the correctness pivot here rests entirely on reference-driver behavior ( 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.
No code change needed: verified in src/reader/cloudfetch/streaming_provider.rs (~L855-859) that |
||
| match ctx.downloader.download(&link).await { | ||
| Ok(batches) => return Ok(batches), | ||
| Ok(batches) => { | ||
| ctx.metrics | ||
| .record_chunk_download(chunk_index, download_started.elapsed()); | ||
| return Ok(batches); | ||
| } | ||
| Err(e) => { | ||
| // Every completed download failure consumes the hard attempt | ||
| // budget, even when its link became stale while in flight. | ||
|
|
@@ -853,6 +945,26 @@ impl StreamingCloudFetchProvider { | |
| } | ||
| } | ||
|
|
||
| pub(crate) fn metrics(&self) -> Option<ResultReaderMetrics> { | ||
|
oss-reviewer-bot[bot] marked this conversation as resolved.
Resolved
|
||
| let snapshot = self.metrics.snapshot(); | ||
| // Surface metrics whenever *any* datum was captured, not just | ||
| // once a chunk has been iterated (released from the buffer). | ||
| // `total_chunks_present` is known from the manifest at | ||
| // construction and `initial_chunk_latency` is stamped at | ||
| // chunk-0 download completion — both fire before the first | ||
| // `record_chunk_iterated`. Gating strictly on `iterated > 0` | ||
| // would drop this data for a stream that downloads but is | ||
| // dropped/errors before releasing any chunk, losing useful | ||
| // telemetry for partial/failed consumption. | ||
| let has_data = snapshot.total_chunks_present.is_some() | ||
| || snapshot | ||
| .total_chunks_iterated | ||
| .is_some_and(|count| count > 0) | ||
| || snapshot.initial_chunk_latency.is_some() | ||
| || snapshot.slowest_chunk_latency.is_some(); | ||
| has_data.then_some(snapshot) | ||
| } | ||
|
|
||
| /// Refresh presigned-URL links for `chunk_index` and every | ||
| /// subsequent chunk whose download is still pending or in flight. | ||
| /// | ||
|
|
@@ -1504,6 +1616,7 @@ mod tests { | |
| refresh_coordinator: Arc::new(RefreshCoordinator::default()), | ||
| cancel_token: CancellationToken::new(), | ||
| statement_id: None, | ||
| metrics: Arc::new(CloudFetchMetrics::new(Some(1))), | ||
| max_retries: MAX_DOWNLOAD_ATTEMPTS, | ||
| retry_delay: std::time::Duration::ZERO, | ||
| }; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.