Skip to content

Commit 4298372

Browse files
feat(relay): log range-parallel-stream progress at 16 MiB intervals
1 parent 647088f commit 4298372

1 file changed

Lines changed: 155 additions & 2 deletions

File tree

‎src/domain_fronter.rs‎

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ const APPS_SCRIPT_BODY_MAX_BYTES: u64 = 40 * 1024 * 1024;
178178
/// through.
179179
const MAX_STREAMED_RANGE_BYTES: u64 = 16 * 1024 * 1024 * 1024;
180180

181+
/// Byte interval between `range-parallel-stream` progress log lines.
182+
/// Large downloads through the streaming branch otherwise look stuck
183+
/// in the logs (one "starting N chunks" line at the top, nothing
184+
/// until completion or failure). At 16 MiB intervals the operator sees
185+
/// ~6 lines per 100 MiB and ~64 lines per 1 GiB — useful pace at the
186+
/// ~1.4 MB/s typical through-relay throughput, and quiet enough that
187+
/// even a 16 GiB file won't drown the log (~1024 progress lines over
188+
/// the multi-hour download). Per user feedback on PR #1085.
189+
const STREAM_PROGRESS_LOG_INTERVAL_BYTES: u64 = 16 * 1024 * 1024;
190+
181191
/// Hard ceiling on the buffered stitch buffer's `Vec::with_capacity(total)`
182192
/// allocation. Two roles:
183193
///
@@ -3582,6 +3592,7 @@ async fn stream_chunks_to_writer<W, S>(
35823592
writer: &mut W,
35833593
head: &[u8],
35843594
probe_body: &[u8],
3595+
total: u64,
35853596
fetches: S,
35863597
url_for_log: &str,
35873598
) -> std::io::Result<()>
@@ -3609,9 +3620,44 @@ where
36093620
writer.flush().await?;
36103621
futures_util::pin_mut!(fetches);
36113622

3623+
// Progress accounting: bytes emitted as wire body so far (the
3624+
// probe body, plus every successfully-written chunk). The head
3625+
// doesn't count — it's protocol framing, not body progress.
3626+
// `next_progress_log_at` is the next body-byte threshold at
3627+
// which we emit a progress line, advanced past the current
3628+
// count each time so a single large chunk crossing multiple
3629+
// intervals only logs once.
3630+
let mut body_bytes_emitted: u64 = probe_body.len() as u64;
3631+
let mut next_progress_log_at: u64 = STREAM_PROGRESS_LOG_INTERVAL_BYTES;
3632+
36123633
while let Some((s, e, chunk_result)) = fetches.next().await {
36133634
match chunk_result {
3614-
Ok(c) => writer.write_all(&c).await?,
3635+
Ok(c) => {
3636+
writer.write_all(&c).await?;
3637+
body_bytes_emitted = body_bytes_emitted.saturating_add(c.len() as u64);
3638+
if body_bytes_emitted >= next_progress_log_at {
3639+
// Percentage is well-defined here: streaming
3640+
// branch is only reached for total >
3641+
// APPS_SCRIPT_BODY_MAX_BYTES (≥ 40 MiB), so the
3642+
// divisor is never zero.
3643+
let pct = (body_bytes_emitted * 100) / total;
3644+
tracing::info!(
3645+
"range-parallel-stream: {}/{} MiB ({}%) emitted for {}",
3646+
body_bytes_emitted / (1024 * 1024),
3647+
total / (1024 * 1024),
3648+
pct,
3649+
url_for_log,
3650+
);
3651+
// Advance to the next interval past the current
3652+
// count — a chunk much larger than the interval
3653+
// (shouldn't happen at 256 KiB chunks, but defend
3654+
// against future tuning) skips intermediate
3655+
// thresholds rather than firing N log lines back
3656+
// to back.
3657+
next_progress_log_at = body_bytes_emitted
3658+
.saturating_add(STREAM_PROGRESS_LOG_INTERVAL_BYTES);
3659+
}
3660+
}
36153661
Err(reason) => {
36163662
tracing::warn!(
36173663
"range-parallel-stream: invalid chunk {}-{} for {} ({}); truncating response",
@@ -3664,7 +3710,7 @@ where
36643710
{
36653711
let head = assemble_200_head(probe_resp_headers, total);
36663712
let head = transform_head(&head);
3667-
stream_chunks_to_writer(writer, &head, probe_body, chunks_stream, url_for_log).await
3713+
stream_chunks_to_writer(writer, &head, probe_body, total, chunks_stream, url_for_log).await
36683714
}
36693715

36703716
/// Tiny adapter that lets `relay_parallel_range_to` write into a
@@ -5498,6 +5544,7 @@ hello";
54985544
&mut VecAsyncWriter(&mut buf),
54995545
head,
55005546
probe,
5547+
10,
55015548
fetches.map(|x| x),
55025549
"https://example.test/file",
55035550
)
@@ -5726,6 +5773,7 @@ hello";
57265773
&mut writer,
57275774
head,
57285775
probe,
5776+
12,
57295777
fetches.map(|x| x),
57305778
"https://example.test/file",
57315779
)
@@ -5747,6 +5795,109 @@ hello";
57475795
);
57485796
}
57495797

5798+
#[tokio::test(flavor = "current_thread")]
5799+
async fn stream_chunks_to_writer_emits_progress_log_at_each_16mib_boundary() {
5800+
// User feedback on PR #1085: large streamed downloads went
5801+
// silent in the logs between "starting N chunks" and
5802+
// completion, with no progress signal. This test locks in
5803+
// the periodic progress lines by capturing the tracing
5804+
// output of a synthetic 40 MiB stream and counting how many
5805+
// `range-parallel-stream:` lines mention "MiB" (the progress
5806+
// lines do; the start-up summary phrases it differently).
5807+
//
5808+
// At 40 MiB total and 16 MiB intervals we expect two
5809+
// crossings — at 16 MiB and 32 MiB. Strictly *not* one at
5810+
// 0 MiB (the threshold must be reached, not just initialised)
5811+
// and *not* one at 40 MiB (40 < next_progress_log_at=48 once
5812+
// we've crossed 32 MiB).
5813+
use futures_util::stream;
5814+
use std::sync::{Arc, Mutex};
5815+
use tracing_subscriber::fmt::MakeWriter;
5816+
5817+
#[derive(Clone, Default)]
5818+
struct LogCapture(Arc<Mutex<Vec<u8>>>);
5819+
impl std::io::Write for LogCapture {
5820+
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
5821+
self.0.lock().unwrap().extend_from_slice(buf);
5822+
Ok(buf.len())
5823+
}
5824+
fn flush(&mut self) -> std::io::Result<()> {
5825+
Ok(())
5826+
}
5827+
}
5828+
impl<'a> MakeWriter<'a> for LogCapture {
5829+
type Writer = Self;
5830+
fn make_writer(&'a self) -> Self::Writer {
5831+
self.clone()
5832+
}
5833+
}
5834+
5835+
let capture = LogCapture::default();
5836+
let subscriber = tracing_subscriber::fmt()
5837+
.with_writer(capture.clone())
5838+
.with_max_level(tracing::Level::INFO)
5839+
.with_ansi(false)
5840+
.with_target(false)
5841+
.finish();
5842+
let _guard = tracing::subscriber::set_default(subscriber);
5843+
5844+
// 40 MiB total. Probe is one 256 KiB chunk; the rest of the
5845+
// file is 159 same-sized chunks fed as a synthetic stream.
5846+
let chunk_size: u64 = 256 * 1024;
5847+
let total: u64 = 40 * 1024 * 1024;
5848+
let probe_body = vec![0u8; chunk_size as usize];
5849+
let mut chunks_data: Vec<(u64, u64, Result<Vec<u8>, &'static str>)> = Vec::new();
5850+
let mut start = chunk_size;
5851+
while start < total {
5852+
let end = (start + chunk_size - 1).min(total - 1);
5853+
let len = (end - start + 1) as usize;
5854+
chunks_data.push((start, end, Ok(vec![0u8; len])));
5855+
start = end + 1;
5856+
}
5857+
let head = format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", total).into_bytes();
5858+
5859+
let mut buf: Vec<u8> = Vec::new();
5860+
stream_chunks_to_writer(
5861+
&mut VecAsyncWriter(&mut buf),
5862+
&head,
5863+
&probe_body,
5864+
total,
5865+
stream::iter(chunks_data),
5866+
"https://example.test/big",
5867+
)
5868+
.await
5869+
.unwrap();
5870+
// Wire output sanity: head + 40 MiB body, exactly.
5871+
assert_eq!(buf.len() as u64, head.len() as u64 + total);
5872+
5873+
// Inspect the captured log. The two progress lines should
5874+
// mention `16/40` and `32/40` (MiB emitted / MiB total).
5875+
// Drop the subscriber guard so any inadvertent log lines
5876+
// from drop-handlers don't race with our read.
5877+
drop(_guard);
5878+
let log = String::from_utf8(capture.0.lock().unwrap().clone()).unwrap();
5879+
let progress_lines: Vec<&str> = log
5880+
.lines()
5881+
.filter(|l| l.contains("range-parallel-stream:") && l.contains(" MiB ("))
5882+
.collect();
5883+
assert_eq!(
5884+
progress_lines.len(),
5885+
2,
5886+
"expected 2 progress lines at the 16 / 32 MiB crossings; full log:\n{}",
5887+
log,
5888+
);
5889+
assert!(
5890+
progress_lines[0].contains("16/40 MiB (40%)"),
5891+
"first progress line should read 16/40 MiB (40%); got: {}",
5892+
progress_lines[0],
5893+
);
5894+
assert!(
5895+
progress_lines[1].contains("32/40 MiB (80%)"),
5896+
"second progress line should read 32/40 MiB (80%); got: {}",
5897+
progress_lines[1],
5898+
);
5899+
}
5900+
57505901
#[tokio::test]
57515902
async fn stream_chunks_to_writer_flushes_after_head_and_probe_for_first_byte_latency() {
57525903
// "First bytes quickly" lock-in: after writing head + probe
@@ -5771,6 +5922,7 @@ hello";
57715922
&mut writer,
57725923
head,
57735924
probe,
5925+
14,
57745926
fetches.map(|x| x),
57755927
"https://example.test/file",
57765928
)
@@ -6000,6 +6152,7 @@ hello";
60006152
&mut VecAsyncWriter(&mut buf),
60016153
head,
60026154
probe,
6155+
12,
60036156
fetches.map(|x| x),
60046157
"https://example.test/file",
60056158
)

0 commit comments

Comments
 (0)