Adopt fills each multipart part before it flies — r2 refused the short ones
Browse files at this commitdbcb2887bfbdb56e550696280bbc0f22d012ede4Parent: bc1a1da- fix
- adopt fills each multipart part before it flies — r2 refused the short ones
1 changed file +29 -9
crates/in-core/src/store/r2.rsmodified+29 -9
| @@ -26,8 +26,8 @@ use object_store::{ | |||
| 26 | GetOptions, GetRange, MultipartUpload, ObjectStore, ObjectStoreExt, PutPayload, | 26 | GetOptions, GetRange, MultipartUpload, ObjectStore, ObjectStoreExt, PutPayload, |
| 27 | }; | 27 | }; |
| 28 | 28 | ||
| 29 | -use super::blobs::{BlobError, Blobs}; | ||
| 30 | use super::FileSpan; | 29 | use super::FileSpan; |
| 30 | +use super::blobs::{BlobError, Blobs}; | ||
| 31 | 31 | ||
| 32 | /// The part size [`R2Blobs::adopt`] streams up in: 8 MiB, the chunk size the | 32 | /// The part size [`R2Blobs::adopt`] streams up in: 8 MiB, the chunk size the |
| 33 | /// server fixes for uploads, so the staging side and the bucket side speak | 33 | /// server fixes for uploads, so the staging side and the bucket side speak |
| @@ -120,21 +120,38 @@ impl Blobs for R2Blobs { | |||
| 120 | return Ok(()); | 120 | return Ok(()); |
| 121 | } | 121 | } |
| 122 | // The staged file streams up in parts — one PART_SIZE buffer in | 122 | // The staged file streams up in parts — one PART_SIZE buffer in |
| 123 | - // memory at a time, never one allocation the file's size. The buffer | 123 | + // memory at a time, never one allocation the file's size. Reads are |
| 124 | - // is reused per read and copied once into each part's payload, so | 124 | + // ACCUMULATED until a part is full: a `read` may return short of the |
| 125 | - // the next read cannot overwrite bytes still in flight. | 125 | + // buffer (a regular file usually fills it, but nothing promises it), |
| 126 | + // and S3 refuses a complete whose non-final parts fall under the | ||
| 127 | + // 5 MiB minimum — R2 answers EntityTooSmall, the local fake does | ||
| 128 | + // not, so the sizing is made true here rather than trusted to the | ||
| 129 | + // filesystem. Only the last part is short, which is the legal shape. | ||
| 126 | let mut file = tokio::fs::File::open(staged).await?; | 130 | let mut file = tokio::fs::File::open(staged).await?; |
| 127 | let mut upload = self.store.put_multipart(&path).await?; | 131 | let mut upload = self.store.put_multipart(&path).await?; |
| 128 | let attempt = async { | 132 | let attempt = async { |
| 129 | let mut buf = vec![0u8; PART_SIZE]; | 133 | let mut buf = vec![0u8; PART_SIZE]; |
| 134 | + let mut filled = 0usize; | ||
| 130 | loop { | 135 | loop { |
| 131 | - let n = tokio::io::AsyncReadExt::read(&mut file, &mut buf).await?; | 136 | + let n = tokio::io::AsyncReadExt::read(&mut file, &mut buf[filled..]).await?; |
| 132 | if n == 0 { | 137 | if n == 0 { |
| 138 | + if filled > 0 { | ||
| 139 | + // The tail: the one part allowed to be short. | ||
| 140 | + upload | ||
| 141 | + .put_part(PutPayload::from(bytes::Bytes::copy_from_slice( | ||
| 142 | + &buf[..filled], | ||
| 143 | + ))) | ||
| 144 | + .await?; | ||
| 145 | + } | ||
| 133 | break; | 146 | break; |
| 134 | } | 147 | } |
| 135 | - upload | 148 | + filled += n; |
| 136 | - .put_part(PutPayload::from(bytes::Bytes::copy_from_slice(&buf[..n]))) | 149 | + if filled == PART_SIZE { |
| 137 | - .await?; | 150 | + upload |
| 151 | + .put_part(PutPayload::from(bytes::Bytes::copy_from_slice(&buf))) | ||
| 152 | + .await?; | ||
| 153 | + filled = 0; | ||
| 154 | + } | ||
| 138 | } | 155 | } |
| 139 | upload.complete().await?; | 156 | upload.complete().await?; |
| 140 | Ok::<(), BlobError>(()) | 157 | Ok::<(), BlobError>(()) |
| @@ -182,7 +199,10 @@ impl Blobs for R2Blobs { | |||
| 182 | } | 199 | } |
| 183 | let result = match self | 200 | let result = match self |
| 184 | .store | 201 | .store |
| 185 | - .get_opts(&path, GetOptions::default().with_range(Some(GetRange::Bounded(start..end)))) | 202 | + .get_opts( |
| 203 | + &path, | ||
| 204 | + GetOptions::default().with_range(Some(GetRange::Bounded(start..end))), | ||
| 205 | + ) | ||
| 186 | .await | 206 | .await |
| 187 | { | 207 | { |
| 188 | Ok(result) => result, | 208 | Ok(result) => result, |