Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api_changes/update_261026_stream_initial_fetch_size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Streamed reconstructions now start with a small first term block so the first bytes arrive sooner.

What changed
- New config `reconstruction.stream_initial_fetch_size` (default 2MB, `HF_XET_RECONSTRUCTION_STREAM_INITIAL_FETCH_SIZE`), capped at `min_reconstruction_fetch_size`. `reconstruct_to_stream`, `reconstruct_to_unordered_stream` and the wasm `reconstruct_to_writer` use it for their first block.
- `ReconstructionTermManager::new` takes a trailing `is_streaming: bool`.

Why this matters
- A stream yields nothing until its first term is downloaded, and that term could span a whole 256MB first block.

Usage notes
- Callers of `ReconstructionTermManager::new` pass `false` to keep the previous behavior.
38 changes: 33 additions & 5 deletions xet_data/src/file_reconstruction/file_reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ impl FileReconstructor {
/// Runs the file reconstruction with error handling and cancellation support.
/// Returns the number of bytes written.
///
/// When `is_streaming` is true, the progress completion assertions at the end
/// of reconstruction are skipped because the stream consumer reports bytes
/// asynchronously after this method returns.
/// When `is_streaming` is true, the first term block uses `stream_initial_fetch_size`,
/// and the progress completion assertions at the end of reconstruction are skipped
/// because the stream consumer reports bytes asynchronously after this method returns.
pub(crate) async fn run(
self,
data_writer: Box<dyn DataWriter>,
Expand All @@ -262,7 +262,7 @@ impl FileReconstructor {
self,
mut data_writer: Box<dyn DataWriter>,
run_state: &RunState,
_is_streaming: bool,
is_streaming: bool,
) -> std::result::Result<u64, RunError> {
let Self {
ctx,
Expand All @@ -286,6 +286,7 @@ impl FileReconstructor {
file_hash,
requested_range,
run_state.progress_updater().cloned(),
is_streaming,
)
.await?;

Expand Down Expand Up @@ -428,7 +429,7 @@ impl FileReconstructor {
run_state.log_progress("File reconstruction completed successfully");

#[cfg(debug_assertions)]
if !_is_streaming && let Some(updater) = run_state.progress_updater() {
if !is_streaming && let Some(updater) = run_state.progress_updater() {
updater.assert_complete();
if let Some(byte_range) = byte_range
&& byte_range.end < u64::MAX
Expand Down Expand Up @@ -722,6 +723,33 @@ mod tests {
reconstruct_and_verify_full(&client, &file_contents, test_config()).await;
}

#[tokio::test]
async fn test_stream_first_chunk_bounded_by_stream_initial_fetch_size() {
// A single term of 64 chunks: the first streamed chunk is the part of it covered by the first block.
let (client, file_contents) = setup_test_file(&[(1, (0, 64))]).await;

let mut config = ReconstructionConfig::default();
config.min_reconstruction_fetch_size = xet_runtime::utils::ByteSize::from("1mb");
config.stream_initial_fetch_size = xet_runtime::utils::ByteSize::from("500");

let mut stream = FileReconstructor::new(
&XetContext::default().unwrap(),
&(client as Arc<dyn Client>),
file_contents.file_hash,
)
.with_config(config)
.reconstruct_to_stream();

let first_chunk = stream.next().await.unwrap().unwrap();
assert!(first_chunk.len() <= 500 + TEST_CHUNK_SIZE, "first chunk is {} bytes", first_chunk.len());

let mut data = first_chunk.to_vec();
while let Some(chunk) = stream.next().await.unwrap() {
data.extend_from_slice(&chunk);
}
assert_eq!(data, file_contents.data);
}

// ==================== Progress tracker tests ====================

#[tokio::test]
Expand Down
20 changes: 13 additions & 7 deletions xet_data/src/file_reconstruction/reconstruction_terms/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl ReconstructionTermManager {
file_hash: MerkleHash,
file_byte_range: FileRange,
progress_updater: Option<Arc<ItemProgressUpdater>>,
is_streaming: bool,
) -> Result<Self> {
let completion_rate_estimator =
ExpWeightedMovingAvg::new_count_decay(config.completion_rate_estimator_half_life);
Expand Down Expand Up @@ -80,9 +81,16 @@ impl ReconstructionTermManager {
// once the first block is finished, we have a second block to start processing -- and
// an estimate of the completion time based on the first one. This helps us to get
// a better estimate of the completion time.
let initial_fetch_size = s.config.min_reconstruction_fetch_size.as_u64();
s.prefetch_block(initial_fetch_size).await?;
s.prefetch_block(2 * initial_fetch_size).await?;
//
// Only the first block of a stream is smaller, see `stream_initial_fetch_size`.
let min_fetch_size = s.config.min_reconstruction_fetch_size.as_u64();
let first_fetch_size = if is_streaming {
s.config.stream_initial_fetch_size.as_u64().min(min_fetch_size)
} else {
min_fetch_size
};
s.prefetch_block(first_fetch_size).await?;
s.prefetch_block(2 * min_fetch_size).await?;

debug!(
%file_hash,
Expand Down Expand Up @@ -237,10 +245,8 @@ impl ReconstructionTermManager {
}

async fn prefetch_block(&mut self, block_size: u64) -> Result<()> {
let block_size = block_size.clamp(
self.config.min_reconstruction_fetch_size.as_u64(),
self.config.max_reconstruction_fetch_size.as_u64(),
);
// `block_size` can be below `min_reconstruction_fetch_size` for the first block of a stream.
let block_size = block_size.min(self.config.max_reconstruction_fetch_size.as_u64());

// First, check the block range to see if we're over the requested range.
let mut prefetch_block_range =
Expand Down
9 changes: 9 additions & 0 deletions xet_runtime/src/config/groups/reconstruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ crate::config_group!({
/// Use the environment variable `HF_XET_RECONSTRUCTION_MAX_RECONSTRUCTION_FETCH_SIZE` to set this value.
ref max_reconstruction_fetch_size: ByteSize = ByteSize::from("8gb");

/// The size of the first fetch request of a streamed reconstruction, capped at `min_reconstruction_fetch_size`.
/// A stream returns no data until its first term is downloaded, so a small first fetch shortens the time to
/// first byte.
///
/// The default value is 2MB.
///
/// Use the environment variable `HF_XET_RECONSTRUCTION_STREAM_INITIAL_FETCH_SIZE` to set this value.
ref stream_initial_fetch_size: ByteSize = ByteSize::from("2mb");

/// The amount of download buffer always available for file reconstruction.
/// The full buffer size will be this plus the number of simultaneous active
/// file downloads times the per file size up to the global limit of
Expand Down
Loading