This is an automated email from the ASF dual-hosted git repository.

viirya pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git


The following commit(s) were added to refs/heads/master by this push:
     new ede3184c [SPARK-61425][CONNECT] Frame artifact uploads in one place 
and reject a chunked artifact that changed size during its upload
ede3184c is described below

commit ede3184cdb87d2087fc70eafe258fad38dc1fb57
Author: Liang-Chi Hsieh <[email protected]>
AuthorDate: Tue Nov 10 18:11:40 2026 -0800

    [SPARK-61425][CONNECT] Frame artifact uploads in one place and reject a 
chunked artifact that changed size during its upload
    
    ### What changes were proposed in this pull request?
    
    [SPARK-61425](https://issues.apache.org/jira/browse/SPARK-61425): frame 
artifact uploads in one place, and make a chunked artifact whose data is not of 
its declared size an error.
    
    `crates/spark-connect-core/src/artifact.rs` framed artifacts in three 
places, each with its own state machine: `build_artifact_request_stream` 
(public, eager), `cache_artifact_requests` (cache artifacts held in memory) and 
`ArtifactFrames` (the lazy frames of a file upload). They are now one lazy 
iterator, `Frames<D>`, over data that is either read (`ReadData`) or held in 
memory (`Bytes`):
    
    - an artifact of more than `CHUNK_SIZE` bytes is a `BeginChunk` with its 
first chunk, then a `Chunk` for each further one, each full but the last;
    - consecutive artifacts of at most `CHUNK_SIZE` bytes each share a `Batch` 
for as long as their sizes sum to at most `CHUNK_SIZE`.
    
    This is the framing of PySpark's `ArtifactManager._add_artifacts`.
    
    - `ArtifactFrames` is `Frames<ReadData>`, and `cache_artifact_requests` 
returns a `Frames<Bytes>` whose chunks still share the allocation of the 
artifact. Neither signature changes, so `client.rs` is untouched.
    - `build_artifact_request_stream` keeps its signature and collects the 
frames. Nothing in `crates/spark-connect` or `crates/pyspark-rs` calls it, but 
it is public API of a published crate and the only way to get the requests 
without a client.
    
    The requests on the wire do not change. The first commit adds a test that 
pins them, and passes on the code before the refactor: 13 artifacts (empty, 
exactly a chunk, a byte over, small ones that cross a batch boundary, small 
ones between chunked ones, several chunks, a whole number of chunks) go through 
the three implementations, and the request sequence and the CRC of its encoding 
are asserted. The three already agreed for every input: the two batch 
conditions were written different [...]
    
    A chunked artifact is now checked against the size it declared: data that 
ends before it, or goes on past it, ends the frames with an error such as
    
    ```
    Artifact jars/resized.jar changed size during its upload: expected 65537 
bytes, read 100 bytes
    ```
    
    ### Why are the changes needed?
    
    Three copies of one policy can drift apart, and only tests held them 
together.
    
    The size of an artifact is taken before its data is read, and `total_bytes` 
and `num_chunks` of the `BeginChunk` come from it. Nothing compared the data 
read with it. For an artifact declared as 65537 bytes, before this change:
    
    | data read | frames | error |
    |---|---|---|
    | 0 bytes | none | none |
    | 100 bytes | `BeginChunk` (3 chunks declared, 100 bytes) | none |
    | 131072 bytes | `BeginChunk` and 3 `Chunk`s, one more than declared | none 
|
    
    With nothing read, the server receives an empty stream and answers with 
success, so the upload returned `Ok` with nothing uploaded. In the other cases 
the server (`SparkConnectAddArtifactsHandler`, v4.3.0) rejects the upload with 
"Missing data chunks" or "Excessive data chunks"; the client now reports it 
first, with the name and both sizes, and sends no chunk past the declared size.
    
    A batched artifact declares no size, so it is still sent as it is read, as 
PySpark sends it.
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. An upload of an artifact of more than `CHUNK_SIZE` bytes whose data 
changes size while it is read now fails with an error naming the artifact, the 
expected size and the size read. Before, it succeeded without uploading 
anything when no data could be read, and otherwise failed with the server's 
error.
    
    ### How was this patch tested?
    
    New unit tests in `artifact.rs`: the pinned framing (passes before and 
after the refactor), a chunked artifact that is shorter or longer than declared 
(through `ArtifactFrames` and `build_artifact_request_stream`), a batched 
artifact whose data differs from its declared size, and an artifact that cannot 
be opened among others.
    
    ```
    cargo test -p apache-spark-connect-core --release     # 185 unit tests, 
tests/artifact_uploads.rs and the other integration tests
    cargo test -p apache-spark-connect --release --lib    # 596 passed
    cargo test -p apache-spark-connect --release --no-run
    cargo test -p apache-spark-connect --release --features wasm-udf --no-run
    cargo fmt --all --check
    ```
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code
    
    Closes #502 from viirya/SPARK-61425.
    
    Authored-by: Liang-Chi Hsieh <[email protected]>
    Signed-off-by: Liang-Chi Hsieh <[email protected]>
---
 crates/spark-connect-core/src/artifact.rs | 692 +++++++++++++++++++++---------
 1 file changed, 479 insertions(+), 213 deletions(-)

diff --git a/crates/spark-connect-core/src/artifact.rs 
b/crates/spark-connect-core/src/artifact.rs
index 385bea75..591b9510 100644
--- a/crates/spark-connect-core/src/artifact.rs
+++ b/crates/spark-connect-core/src/artifact.rs
@@ -130,149 +130,38 @@ pub(crate) fn cache_artifact_requests(
     session_id: String,
     artifacts: Vec<(String, Bytes)>,
 ) -> impl Iterator<Item = AddArtifactsRequest> + Send + 'static {
-    let mut artifacts = artifacts.into_iter().peekable();
-    let mut remaining_chunks = None::<Bytes>;
-
-    std::iter::from_fn(move || {
-        let payload = if let Some(mut data) = remaining_chunks.take() {
-            let chunk_data = data.split_to(CHUNK_SIZE.min(data.len()));
-            if !data.is_empty() {
-                remaining_chunks = Some(data);
-            }
-            
add_artifacts_request::Payload::Chunk(add_artifacts_request::ArtifactChunk {
-                crc: compute_crc32(&chunk_data) as i64,
-                data: chunk_data,
+    // Data in memory is always of its size and never fails to be read, so 
these frames end
+    // only at the last artifact.
+    Frames::over(
+        session_id,
+        artifacts.into_iter().map(|(name, data)| {
+            Ok(FrameSource {
+                name,
+                size: data.len() as u64,
+                data,
             })
-        } else {
-            let (_, next_data) = artifacts.peek()?;
-            if next_data.len() > CHUNK_SIZE {
-                let (name, mut data) = artifacts.next().unwrap();
-                let total_bytes = data.len() as i64;
-                let num_chunks = data.len().div_ceil(CHUNK_SIZE) as i64;
-                let chunk_data = data.split_to(CHUNK_SIZE);
-                remaining_chunks = Some(data);
-                add_artifacts_request::Payload::BeginChunk(
-                    add_artifacts_request::BeginChunkedArtifact {
-                        name,
-                        total_bytes,
-                        num_chunks,
-                        initial_chunk: 
Some(add_artifacts_request::ArtifactChunk {
-                            crc: compute_crc32(&chunk_data) as i64,
-                            data: chunk_data,
-                        }),
-                    },
-                )
-            } else {
-                let mut batch_artifacts = Vec::new();
-                let mut batch_size = 0;
-                while let Some((_, data)) = artifacts.peek() {
-                    if data.len() > CHUNK_SIZE || batch_size + data.len() > 
CHUNK_SIZE {
-                        break;
-                    }
-                    let (name, data) = artifacts.next().unwrap();
-                    batch_size += data.len();
-                    
batch_artifacts.push(add_artifacts_request::SingleChunkArtifact {
-                        name,
-                        data: Some(add_artifacts_request::ArtifactChunk {
-                            crc: compute_crc32(&data) as i64,
-                            data,
-                        }),
-                    });
-                }
-                
add_artifacts_request::Payload::Batch(add_artifacts_request::Batch {
-                    artifacts: batch_artifacts,
-                })
-            }
-        };
-        Some(AddArtifactsRequest {
-            session_id: session_id.clone(),
-            payload: Some(payload),
-            ..Default::default()
-        })
-    })
+        }),
+    )
 }
 
 /// Build a stream of AddArtifactsRequest messages for artifacts.
 /// Small artifacts (<= CHUNK_SIZE) are batched together.
 /// Large artifacts (> CHUNK_SIZE) are sent as chunked requests.
+///
+/// All the requests are built before any is returned, so every artifact is 
held in memory at
+/// once. The requests are those that an upload produces as its RPC consumes 
them.
 pub fn build_artifact_request_stream(
     session_id: String,
     artifacts: Vec<NamedArtifact>,
 ) -> Result<Vec<AddArtifactsRequest>> {
-    let mut requests = Vec::new();
-    let mut batch_artifacts = Vec::new();
-    let mut batch_size = 0usize;
-
-    for artifact in artifacts {
-        let size = artifact.data.size()? as usize;
-
-        if size > CHUNK_SIZE {
-            // Flush batch if non-empty
-            if !batch_artifacts.is_empty() {
-                let mut request = AddArtifactsRequest::default();
-                request.session_id = session_id.clone();
-                request.payload = Some(add_artifacts_request::Payload::Batch(
-                    add_artifacts_request::Batch {
-                        artifacts: batch_artifacts,
-                    },
-                ));
-                requests.push(request);
-                batch_artifacts = Vec::new();
-                batch_size = 0;
-            }
-
-            // Send chunked artifact
-            requests.extend(build_chunked_artifact_requests(
-                session_id.clone(),
-                artifact,
-            )?);
-        } else {
-            // Add to batch
-            if batch_size + size > CHUNK_SIZE && !batch_artifacts.is_empty() {
-                // Flush current batch
-                let mut request = AddArtifactsRequest::default();
-                request.session_id = session_id.clone();
-                request.payload = Some(add_artifacts_request::Payload::Batch(
-                    add_artifacts_request::Batch {
-                        artifacts: batch_artifacts,
-                    },
-                ));
-                requests.push(request);
-                batch_artifacts = Vec::new();
-                batch_size = 0;
-            }
-
-            // Read artifact data
-            let mut stream = artifact.data.stream()?;
-            let mut data = Vec::new();
-            stream
-                .read_to_end(&mut data)
-                .map_err(|e| SparkError::connect_msg(format!("Failed to read 
artifact: {}", e)))?;
-            let crc = compute_crc32(&data) as i64;
-
-            batch_artifacts.push(add_artifacts_request::SingleChunkArtifact {
-                name: artifact.name,
-                data: Some(add_artifacts_request::ArtifactChunk {
-                    data: Bytes::from(data),
-                    crc,
-                }),
-            });
-            batch_size += size;
-        }
-    }
-
-    // Flush remaining batch
-    if !batch_artifacts.is_empty() {
-        let mut request = AddArtifactsRequest::default();
-        request.session_id = session_id.clone();
-        request.payload = Some(add_artifacts_request::Payload::Batch(
-            add_artifacts_request::Batch {
-                artifacts: batch_artifacts,
-            },
-        ));
-        requests.push(request);
-    }
-
+    let mut frames = Frames::over(
+        session_id,
+        artifacts
+            .into_iter()
+            .map(|artifact| FrameSource::open(&artifact)),
+    );
+    let requests: Vec<AddArtifactsRequest> = frames.by_ref().collect();
+    take_frame_failure(&frames.failure())?;
     Ok(requests)
 }
 
@@ -292,38 +181,141 @@ pub(crate) fn take_frame_failure(failure: &FrameFailure) 
-> Result<()> {
     }
 }
 
-/// The frames of an upload of one artifact, produced as the RPC consumes 
them, so that a large
-/// artifact is held one chunk at a time rather than as a whole. An attempt 
that is retried
-/// builds its frames again, from the start of the artifact.
-///
-/// The frames are those of [`build_artifact_request_stream`] for the 
artifact: one batch when it
-/// is at most [`CHUNK_SIZE`] bytes, otherwise a `BeginChunk` and then 
`Chunk`s, each a full
-/// chunk but the last.
-pub(crate) struct ArtifactFrames {
-    session_id: String,
+/// The data of one artifact, as its frames read it.
+pub(crate) trait FrameData {
+    /// All of the data, for an artifact that is sent whole in a batch.
+    fn read_all(&mut self) -> Result<Bytes>;
+
+    /// The next [`CHUNK_SIZE`] bytes of the data, or the fewer (or none) that 
remain at its
+    /// end.
+    fn read_chunk(&mut self) -> Result<Bytes>;
+}
+
+/// Data in memory: its frames are slices that share its allocation.
+impl FrameData for Bytes {
+    fn read_all(&mut self) -> Result<Bytes> {
+        Ok(std::mem::take(self))
+    }
+
+    fn read_chunk(&mut self) -> Result<Bytes> {
+        Ok(self.split_to(CHUNK_SIZE.min(self.len())))
+    }
+}
+
+/// Data that is read as its frames are produced, one chunk held at a time.
+pub(crate) struct ReadData(Box<dyn Read + Send>);
+
+impl FrameData for ReadData {
+    fn read_all(&mut self) -> Result<Bytes> {
+        let mut data = Vec::new();
+        self.0
+            .read_to_end(&mut data)
+            .map_err(|e| SparkError::connect_msg(format!("Failed to read 
artifact: {}", e)))?;
+        Ok(Bytes::from(data))
+    }
+
+    fn read_chunk(&mut self) -> Result<Bytes> {
+        let mut buf = vec![0u8; CHUNK_SIZE];
+        let filled = read_full(self.0.as_mut(), &mut buf).map_err(|e| {
+            SparkError::connect_msg(format!("Failed to read artifact chunk: 
{}", e))
+        })?;
+        buf.truncate(filled);
+        Ok(Bytes::from(buf))
+    }
+}
+
+/// One artifact to frame: its name, the size it declares, and its data.
+pub(crate) struct FrameSource<D> {
     name: String,
     size: u64,
-    /// The data, until its last frame is produced or a read fails.
-    reader: Option<Box<dyn Read + Send>>,
-    /// Whether the first chunk of a chunked artifact has been produced.
-    begun: bool,
+    data: D,
+}
+
+impl FrameSource<ReadData> {
+    /// Take the size of `artifact` and open its data.
+    fn open(artifact: &NamedArtifact) -> Result<Self> {
+        Ok(Self {
+            name: artifact.name.clone(),
+            size: artifact.data.size()?,
+            data: ReadData(artifact.data.stream()?),
+        })
+    }
+}
+
+impl<D: FrameData> FrameSource<D> {
+    /// Read the next chunk of an artifact of which `sent` bytes are framed, 
and return it with
+    /// whether it is the last one. The chunks of an artifact are announced by 
the size it
+    /// declares (`total_bytes` and `num_chunks` of its `BeginChunk`), so data 
that ends
+    /// before that size or goes on past it is an error; the server would 
reject the upload,
+    /// or, when no chunk at all could be read, would be sent nothing and 
answer with success.
+    fn next_chunk(&mut self, sent: &mut u64) -> Result<(Bytes, bool)> {
+        let chunk = self.data.read_chunk()?;
+        *sent += chunk.len() as u64;
+        let last = chunk.len() < CHUNK_SIZE;
+        if *sent > self.size || (last && *sent < self.size) {
+            return Err(SparkError::connect_msg(format!(
+                "Artifact {} changed size during its upload: expected {} 
bytes, read {} bytes{}",
+                self.name,
+                self.size,
+                *sent,
+                if last { "" } else { " so far" }
+            )));
+        }
+        Ok((chunk, last))
+    }
+}
+
+/// The frames of an upload of artifacts, produced as the RPC consumes them, 
so that a large
+/// artifact that is read is held one chunk at a time rather than as a whole.
+///
+/// This is the one place that decides how artifacts are framed, as PySpark's
+/// `ArtifactManager._add_artifacts` frames them. In the order of the 
artifacts:
+///
+/// - an artifact of more than [`CHUNK_SIZE`] bytes is a `BeginChunk` with its 
first chunk and
+///   then a `Chunk` for each further one, each a full chunk but the last;
+/// - consecutive artifacts of at most [`CHUNK_SIZE`] bytes each share a 
`Batch` for as long
+///   as their sizes sum to at most [`CHUNK_SIZE`]; the artifact that would 
exceed it starts
+///   the next batch. A batch has at least one artifact.
+///
+/// The frames end early at an artifact that cannot be opened or read, and at 
a chunked
+/// artifact whose data is not of the size it declared; [`Frames::failure`] 
has the error.
+pub(crate) struct Frames<D> {
+    session_id: String,
+    /// The artifacts that are not framed yet, until the last is taken or the 
frames fail.
+    sources: Option<Box<dyn Iterator<Item = Result<FrameSource<D>>> + Send>>,
+    /// An artifact taken from `sources` that did not fit the batch being 
built.
+    lookahead: Option<FrameSource<D>>,
+    /// The chunked artifact whose `BeginChunk` is produced, with the count of 
its bytes that
+    /// are framed, until its last chunk is.
+    chunked: Option<(FrameSource<D>, u64)>,
     failure: FrameFailure,
 }
 
+/// The frames of an upload of one artifact, read as the RPC consumes them. An 
attempt that is
+/// retried builds its frames again, from the start of the artifact.
+pub(crate) type ArtifactFrames = Frames<ReadData>;
+
 impl ArtifactFrames {
     /// Open the data of `artifact`, so that a data source that cannot be read 
is an error before
     /// any frame is sent.
     pub(crate) fn new(session_id: String, artifact: &NamedArtifact) -> 
Result<Self> {
-        let size = artifact.data.size()?;
-        let reader = artifact.data.stream()?;
-        Ok(Self {
+        let source = FrameSource::open(artifact)?;
+        Ok(Self::over(session_id, std::iter::once(Ok(source))))
+    }
+}
+
+impl<D: FrameData> Frames<D> {
+    fn over(
+        session_id: String,
+        sources: impl Iterator<Item = Result<FrameSource<D>>> + Send + 'static,
+    ) -> Self {
+        Self {
             session_id,
-            name: artifact.name.clone(),
-            size,
-            reader: Some(reader),
-            begun: false,
+            sources: Some(Box::new(sources)),
+            lookahead: None,
+            chunked: None,
             failure: Arc::default(),
-        })
+        }
     }
 
     /// The failure that ends these frames early, which the upload checks once 
its RPC returns.
@@ -331,62 +323,81 @@ impl ArtifactFrames {
         self.failure.clone()
     }
 
+    /// The next artifact to frame.
+    fn next_source(&mut self) -> Result<Option<FrameSource<D>>> {
+        if let Some(source) = self.lookahead.take() {
+            return Ok(Some(source));
+        }
+        let next = self.sources.as_mut().and_then(Iterator::next);
+        if next.is_none() {
+            self.sources = None;
+        }
+        next.transpose()
+    }
+
     fn next_payload(&mut self) -> 
Result<Option<add_artifacts_request::Payload>> {
-        let Some(mut reader) = self.reader.take() else {
+        // The next chunk of the chunked artifact that is begun. Data that 
ends on a chunk
+        // boundary is seen to end only by a read of nothing, which is no 
frame.
+        if let Some((mut source, mut sent)) = self.chunked.take() {
+            let (data, last) = source.next_chunk(&mut sent)?;
+            if !last {
+                self.chunked = Some((source, sent));
+            }
+            if !data.is_empty() {
+                return 
Ok(Some(add_artifacts_request::Payload::Chunk(chunk(data))));
+            }
+        }
+
+        let Some(mut source) = self.next_source()? else {
             return Ok(None);
         };
-        if self.size <= CHUNK_SIZE as u64 {
-            let mut data = Vec::new();
-            reader
-                .read_to_end(&mut data)
-                .map_err(|e| SparkError::connect_msg(format!("Failed to read 
artifact: {}", e)))?;
-            let crc = compute_crc32(&data) as i64;
-            let artifact = add_artifacts_request::SingleChunkArtifact {
-                name: self.name.clone(),
-                data: Some(add_artifacts_request::ArtifactChunk {
-                    data: Bytes::from(data),
-                    crc,
-                }),
+        if source.size > CHUNK_SIZE as u64 {
+            let mut sent = 0;
+            let (data, last) = source.next_chunk(&mut sent)?;
+            let begin = add_artifacts_request::BeginChunkedArtifact {
+                name: source.name.clone(),
+                total_bytes: source.size as i64,
+                num_chunks: source.size.div_ceil(CHUNK_SIZE as u64) as i64,
+                initial_chunk: Some(chunk(data)),
             };
-            return Ok(Some(add_artifacts_request::Payload::Batch(
-                add_artifacts_request::Batch {
-                    artifacts: vec![artifact],
-                },
-            )));
-        }
-        let mut buf = vec![0u8; CHUNK_SIZE];
-        let filled = read_full(reader.as_mut(), &mut buf).map_err(|e| {
-            SparkError::connect_msg(format!("Failed to read artifact chunk: 
{}", e))
-        })?;
-        buf.truncate(filled);
-        if filled == 0 {
-            return Ok(None);
-        }
-        if filled == CHUNK_SIZE {
-            self.reader = Some(reader);
+            if !last {
+                self.chunked = Some((source, sent));
+            }
+            return Ok(Some(add_artifacts_request::Payload::BeginChunk(begin)));
         }
-        let crc = compute_crc32(&buf) as i64;
-        let chunk = add_artifacts_request::ArtifactChunk {
-            data: Bytes::from(buf),
-            crc,
-        };
-        if self.begun {
-            Ok(Some(add_artifacts_request::Payload::Chunk(chunk)))
-        } else {
-            self.begun = true;
-            Ok(Some(add_artifacts_request::Payload::BeginChunk(
-                add_artifacts_request::BeginChunkedArtifact {
-                    name: self.name.clone(),
-                    total_bytes: self.size as i64,
-                    num_chunks: self.size.div_ceil(CHUNK_SIZE as u64) as i64,
-                    initial_chunk: Some(chunk),
-                },
-            )))
+
+        // A batch: this artifact, and those after it for as long as they fit.
+        let mut artifacts = Vec::new();
+        let mut batch_size = 0;
+        loop {
+            batch_size += source.size;
+            artifacts.push(add_artifacts_request::SingleChunkArtifact {
+                name: source.name,
+                data: Some(chunk(source.data.read_all()?)),
+            });
+            match self.next_source()? {
+                Some(next) if next.size <= CHUNK_SIZE as u64 - batch_size => 
source = next,
+                next => {
+                    self.lookahead = next;
+                    break;
+                }
+            }
         }
+        Ok(Some(add_artifacts_request::Payload::Batch(
+            add_artifacts_request::Batch { artifacts },
+        )))
+    }
+}
+
+/// A chunk of an artifact with its CRC.
+fn chunk(data: Bytes) -> add_artifacts_request::ArtifactChunk {
+    add_artifacts_request::ArtifactChunk {
+        crc: compute_crc32(&data) as i64,
+        data,
     }
 }
 
-impl Iterator for ArtifactFrames {
+impl<D: FrameData> Iterator for Frames<D> {
     type Item = AddArtifactsRequest;
 
     fn next(&mut self) -> Option<AddArtifactsRequest> {
@@ -398,7 +409,9 @@ impl Iterator for ArtifactFrames {
             }),
             Ok(None) => None,
             Err(error) => {
-                self.reader = None;
+                self.sources = None;
+                self.lookahead = None;
+                self.chunked = None;
                 *self.failure.lock().unwrap_or_else(PoisonError::into_inner) = 
Some(error);
                 None
             }
@@ -420,17 +433,6 @@ fn read_full(reader: &mut dyn Read, buf: &mut [u8]) -> 
std::io::Result<usize> {
     Ok(filled)
 }
 
-/// Build requests for a large chunked artifact.
-fn build_chunked_artifact_requests(
-    session_id: String,
-    artifact: NamedArtifact,
-) -> Result<Vec<AddArtifactsRequest>> {
-    let mut frames = ArtifactFrames::new(session_id, &artifact)?;
-    let requests: Vec<AddArtifactsRequest> = frames.by_ref().collect();
-    take_frame_failure(&frames.failure())?;
-    Ok(requests)
-}
-
 /// A path of `addArtifacts` that PySpark's `ArtifactManager` does not upload.
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub enum UnsupportedArtifact {
@@ -1142,4 +1144,268 @@ mod tests {
         let batch = build_artifact_request_stream("s".to_string(), 
vec![make()]).unwrap();
         assert_eq!(a, batch);
     }
+
+    /// The artifacts of the pinned framing, by name and size: an empty one, 
one of exactly a
+    /// chunk, one a byte over a chunk, small ones that cross a batch 
boundary, small ones
+    /// between chunked ones, and ones of several chunks, the last a whole 
number of chunks.
+    const PINNED_ARTIFACTS: [(&str, usize); 13] = [
+        ("files/empty", 0),
+        ("files/exact", CHUNK_SIZE),
+        ("jars/over.jar", CHUNK_SIZE + 1),
+        ("pyfiles/a.py", 10_000),
+        ("pyfiles/b.py", 10_000),
+        ("pyfiles/c.py", 10_000),
+        ("pyfiles/d.py", 10_000),
+        ("files/tiny", 5),
+        ("jars/multi.jar", CHUNK_SIZE * 3 + 7),
+        ("files/one", 1),
+        ("files/rest", CHUNK_SIZE - 1),
+        ("files/spill", 1),
+        ("jars/whole.jar", CHUNK_SIZE * 2),
+    ];
+
+    fn pinned_data(index: usize, size: usize) -> Vec<u8> {
+        (0..size).map(|i| (i * 31 + index * 7) as u8).collect()
+    }
+
+    fn pinned_named(index: usize) -> NamedArtifact {
+        let (name, size) = PINNED_ARTIFACTS[index];
+        NamedArtifact::new(
+            name.to_string(),
+            Box::new(InMemoryArtifact::new(pinned_data(index, size))),
+        )
+    }
+
+    /// One line for each request: its payload, with the length and the CRC of 
each chunk.
+    fn framing(requests: &[AddArtifactsRequest]) -> Vec<String> {
+        let chunk = |chunk: &Option<add_artifacts_request::ArtifactChunk>| {
+            let chunk = chunk.as_ref().unwrap();
+            assert_eq!(chunk.crc, compute_crc32(&chunk.data) as i64);
+            format!("{}:{:08x}", chunk.data.len(), chunk.crc)
+        };
+        requests
+            .iter()
+            .map(|request| {
+                assert_eq!(request.session_id, "session");
+                match request.payload.as_ref().unwrap() {
+                    add_artifacts_request::Payload::Batch(batch) => {
+                        let artifacts: Vec<String> = batch
+                            .artifacts
+                            .iter()
+                            .map(|artifact| format!("{}={}", artifact.name, 
chunk(&artifact.data)))
+                            .collect();
+                        format!("batch [{}]", artifacts.join(", "))
+                    }
+                    add_artifacts_request::Payload::BeginChunk(begin) => 
format!(
+                        "begin {} total={} chunks={} {}",
+                        begin.name,
+                        begin.total_bytes,
+                        begin.num_chunks,
+                        chunk(&begin.initial_chunk)
+                    ),
+                    add_artifacts_request::Payload::Chunk(data) => {
+                        format!("chunk {}", chunk(&Some(data.clone())))
+                    }
+                }
+            })
+            .collect()
+    }
+
+    /// The CRC of the requests as they are encoded for the wire, one after 
another.
+    fn wire_crc(requests: &[AddArtifactsRequest]) -> u32 {
+        use prost::Message;
+        let mut hasher = Hasher::new();
+        for request in requests {
+            hasher.update(&request.encode_length_delimited_to_vec());
+        }
+        hasher.finalize()
+    }
+
+    /// Pins the requests that every way of framing artifacts puts on the wire.
+    #[test]
+    fn the_framing_of_artifacts_is_pinned() {
+        let all = 0..PINNED_ARTIFACTS.len();
+        let eager = build_artifact_request_stream(
+            "session".to_string(),
+            all.clone().map(pinned_named).collect(),
+        )
+        .unwrap();
+        let cached: Vec<AddArtifactsRequest> = cache_artifact_requests(
+            "session".to_string(),
+            all.clone()
+                .map(|index| {
+                    let (name, size) = PINNED_ARTIFACTS[index];
+                    (name.to_string(), Bytes::from(pinned_data(index, size)))
+                })
+                .collect(),
+        )
+        .collect();
+        assert_eq!(
+            framing(&eager),
+            [
+                "batch [files/empty=0:00000000, files/exact=32768:7bc368d8]",
+                "begin jars/over.jar total=32769 chunks=2 32768:5f6349da",
+                "chunk 1:35bac28a",
+                "batch [pyfiles/a.py=10000:d625043c, 
pyfiles/b.py=10000:39b2157a, \
+                 pyfiles/c.py=10000:f0508888]",
+                "batch [pyfiles/d.py=10000:ed3d6035, files/tiny=5:df828f04]",
+                "begin jars/multi.jar total=98311 chunks=4 32768:6036fb23",
+                "chunk 32768:6036fb23",
+                "chunk 32768:6036fb23",
+                "chunk 7:d0965bdd",
+                "batch [files/one=1:6464c2b0, files/rest=32767:a89fc864]",
+                "batch [files/spill=1:da6fd2a0]",
+                "begin jars/whole.jar total=65536 chunks=2 32768:a1b78124",
+                "chunk 32768:a1b78124",
+            ]
+        );
+        assert_eq!(wire_crc(&eager), 0x29c4_d88a);
+        assert_eq!(framing(&cached), framing(&eager));
+        assert_eq!(cached, eager);
+
+        // One artifact at a time, as an upload of a file frames it.
+        let mut single = Vec::new();
+        for index in all {
+            let artifact = pinned_named(index);
+            let mut frames = ArtifactFrames::new("session".to_string(), 
&artifact).unwrap();
+            let lazy: Vec<AddArtifactsRequest> = frames.by_ref().collect();
+            take_frame_failure(&frames.failure()).unwrap();
+            let alone =
+                build_artifact_request_stream("session".to_string(), 
vec![pinned_named(index)])
+                    .unwrap();
+            assert_eq!(lazy, alone, "{}", artifact.name);
+            let (name, size) = PINNED_ARTIFACTS[index];
+            let alone_cached: Vec<AddArtifactsRequest> = 
cache_artifact_requests(
+                "session".to_string(),
+                vec![(name.to_string(), Bytes::from(pinned_data(index, 
size)))],
+            )
+            .collect();
+            assert_eq!(lazy, alone_cached, "{}", artifact.name);
+            single.extend(lazy);
+        }
+        assert_eq!(
+            framing(&single),
+            [
+                "batch [files/empty=0:00000000]",
+                "batch [files/exact=32768:7bc368d8]",
+                "begin jars/over.jar total=32769 chunks=2 32768:5f6349da",
+                "chunk 1:35bac28a",
+                "batch [pyfiles/a.py=10000:d625043c]",
+                "batch [pyfiles/b.py=10000:39b2157a]",
+                "batch [pyfiles/c.py=10000:f0508888]",
+                "batch [pyfiles/d.py=10000:ed3d6035]",
+                "batch [files/tiny=5:df828f04]",
+                "begin jars/multi.jar total=98311 chunks=4 32768:6036fb23",
+                "chunk 32768:6036fb23",
+                "chunk 32768:6036fb23",
+                "chunk 7:d0965bdd",
+                "batch [files/one=1:6464c2b0]",
+                "batch [files/rest=32767:a89fc864]",
+                "batch [files/spill=1:da6fd2a0]",
+                "begin jars/whole.jar total=65536 chunks=2 32768:a1b78124",
+                "chunk 32768:a1b78124",
+            ]
+        );
+        assert_eq!(wire_crc(&single), 0xd422_9fba);
+    }
+
+    /// Data that is shorter or longer when it is read than the size it 
declared, as a file
+    /// that is written to between the two.
+    struct Resized {
+        declared: usize,
+        actual: usize,
+    }
+
+    impl ArtifactData for Resized {
+        fn size(&self) -> Result<u64> {
+            Ok(self.declared as u64)
+        }
+
+        fn stream(&self) -> Result<Box<dyn Read + Send>> {
+            Ok(Box::new(Cursor::new(vec![9u8; self.actual])))
+        }
+    }
+
+    fn resized(declared: usize, actual: usize) -> NamedArtifact {
+        NamedArtifact::new(
+            "jars/resized.jar".to_string(),
+            Box::new(Resized { declared, actual }),
+        )
+    }
+
+    #[test]
+    fn a_chunked_artifact_that_is_not_of_its_declared_size_is_an_error() {
+        let declared = CHUNK_SIZE * 2 + 1;
+        // Shorter, down to nothing at all, which used to end the frames 
before a `BeginChunk`
+        // and so upload nothing without an error; and longer.
+        let cases = [
+            (0, "read 0 bytes"),
+            (100, "read 100 bytes"),
+            (CHUNK_SIZE, "read 32768 bytes"),
+            (CHUNK_SIZE * 2, "read 65536 bytes"),
+            (CHUNK_SIZE * 2 + 2, "read 65538 bytes"),
+            (CHUNK_SIZE * 4, "read 98304 bytes so far"),
+        ];
+        for (actual, read) in cases {
+            let artifact = resized(declared, actual);
+            let mut frames = ArtifactFrames::new("session".to_string(), 
&artifact).unwrap();
+            let failure = frames.failure();
+            let sent = frames.by_ref().count();
+            assert!(frames.next().is_none());
+            // No frame is sent past the size that the `BeginChunk` declared.
+            assert!(sent <= 3, "{actual}: {sent} frames");
+            let error = take_frame_failure(&failure).unwrap_err().to_string();
+            let expected = format!(
+                "Artifact jars/resized.jar changed size during its upload: \
+                 expected 65537 bytes, {read}"
+            );
+            assert!(error.ends_with(&expected), "{actual}: {error}");
+
+            let error = build_artifact_request_stream(
+                "session".to_string(),
+                vec![resized(declared, actual)],
+            )
+            .unwrap_err()
+            .to_string();
+            assert!(error.ends_with(&expected), "{actual}: {error}");
+        }
+        // With nothing read, nothing is sent.
+        let mut frames = ArtifactFrames::new("session".to_string(), 
&resized(declared, 0)).unwrap();
+        assert!(frames.next().is_none());
+        assert!(take_frame_failure(&frames.failure()).is_err());
+    }
+
+    #[test]
+    fn a_batched_artifact_is_sent_as_it_is_read() {
+        // A batch declares no size, so the data read is what is uploaded, as 
PySpark does.
+        for actual in [0, 3, CHUNK_SIZE + 1] {
+            let artifact = resized(10, actual);
+            let mut frames = ArtifactFrames::new("session".to_string(), 
&artifact).unwrap();
+            let requests: Vec<AddArtifactsRequest> = frames.by_ref().collect();
+            take_frame_failure(&frames.failure()).unwrap();
+            let crc = compute_crc32(&vec![9u8; actual]);
+            assert_eq!(
+                framing(&requests),
+                [format!("batch [jars/resized.jar={actual}:{crc:08x}]")]
+            );
+        }
+    }
+
+    #[test]
+    fn an_artifact_that_cannot_be_opened_ends_the_frames_of_those_after_it() {
+        let dir = tempfile::tempdir().unwrap();
+        let artifacts = vec![
+            pinned_named(3),
+            NamedArtifact::new(
+                "files/missing".to_string(),
+                Box::new(FileArtifact::new(dir.path().join("missing"))),
+            ),
+            pinned_named(4),
+        ];
+        let error = build_artifact_request_stream("session".to_string(), 
artifacts).unwrap_err();
+        assert!(
+            error.to_string().contains("Failed to get artifact size"),
+            "{error}"
+        );
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to