aws-multipart-upload 0.1.0

SDK plugin for S3 multipart uploads
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std::fmt::{self, Debug, Formatter};
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};

use bytesize::ByteSize;
use futures::ready;
use multipart_write::{FusedMultipartWrite, MultipartWrite};

use super::part_buffer::PartBuffer;
use super::{ShouldComplete, Uploaded};
use crate::client::part::{CompletedParts, PartBody, PartNumber};
use crate::client::request::*;
use crate::client::{UploadClient, UploadData};
use crate::error::{Error, ErrorWithUpload as _};
use crate::uri::{ObjectUri, ObjectUriIter};

/// Returned by `Upload` when sending a part was successful.
#[derive(Debug, Clone, Copy)]
pub struct UploadStatus {
    /// Last recorded size in bytes of all parts that have been added to the
    /// upload successfully.
    pub upload_bytes: u64,
    /// Total size in bytes of all parts that have been sent.
    pub total_bytes: u64,
    /// Total number of parts that have been sent.
    pub total_parts: u64,
    /// Total duration of this upload.
    pub duration: Duration,
    /// Whether the current upload should be completed.
    pub should_complete: bool,
}

impl ShouldComplete for UploadStatus {
    fn should_complete(&self) -> bool {
        self.should_complete
    }
}

/// `MultipartUpload` is a multipart writer that also manages state transition
/// of `UploadInner`.
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
#[pin_project::pin_project]
pub(crate) struct MultipartUpload {
    #[pin]
    inner: UploadInner,
    #[pin]
    fut: Option<SendCompleteUpload>,
    client: UploadClient,
    iter: ObjectUriIter,
    state: GlobalState,
    config: GlobalConfig,
}

impl MultipartUpload {
    pub(crate) fn new(
        client: UploadClient,
        mut iter: ObjectUriIter,
        bytes: ByteSize,
        capacity: Option<usize>,
    ) -> Self {
        let max_bytes = bytes.as_u64();
        let config = GlobalConfig { max_bytes, capacity };
        let inner = UploadInner::create_upload_maybe(&client, iter.next());
        let state = GlobalState::new(config);
        Self { inner, fut: None, client, iter, state, config }
    }
}

impl FusedMultipartWrite<PartBody> for MultipartUpload {
    fn is_terminated(&self) -> bool {
        self.inner.is_terminated()
    }
}

impl MultipartWrite<PartBody> for MultipartUpload {
    type Error = Error;
    type Output = Uploaded;
    type Recv = UploadStatus;

    fn poll_ready(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        let mut this = self.as_mut().project();
        match this.inner.as_mut().project() {
            // Can't send parts if there is no upload.
            UploadProj::Pending(fut) => {
                let data = ready!(fut.poll(cx))?;
                trace!(
                    id = %&data.id,
                    uri = %&data.uri,
                    "new upload created",
                );
                let new_inner = UploadInner::active(
                    this.client,
                    data,
                    this.config.capacity,
                );
                *this.state = GlobalState::new(*this.config);
                this.inner.set(new_inner);
                Poll::Ready(Ok(()))
            },
            UploadProj::Active(upl) => upl.poll_ready(cx),
            UploadProj::Terminated => {
                Poll::Ready(Err(Error::state("polled upload after completion")))
            },
        }
    }

    fn start_send(
        mut self: Pin<&mut Self>,
        part: PartBody,
    ) -> Result<Self::Recv, Self::Error> {
        let this = self.as_mut().project();
        let upl =
            this.inner.get_active_proj().expect("called send with no upload");
        let recv = upl.start_send(part)?;
        Ok(this.state.upload_status(recv))
    }

    fn poll_flush(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        let this = self.as_mut().project();
        // It should not be the case that `poll_flush` is called without an
        // active upload. We've finished an upload in this case and are
        // waiting for the next one, so flushing when there hasn't been
        // anything written doesn't make sense.
        //
        // I's not against the law though, and it makes sense to return
        // immediately if there's nothing to flush, so that's what we
        // do.
        let Some(upl) = this.inner.get_active_proj() else {
            return Poll::Ready(Ok(()));
        };
        upl.poll_flush(cx)
    }

    fn poll_complete(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<Self::Output, Self::Error>> {
        let mut this = self.as_mut().project();
        // First poll drains active requests and sets the CompleteUpload future.
        if this.fut.is_none() {
            let upl = this
                .inner
                .as_mut()
                .get_active_proj()
                .expect("called complete with no upload");
            let out = ready!(upl.poll_complete(cx))?;
            trace!(
                id = %&out.data.id,
                uri = %&out.data.uri,
                bytes = out.parts.size(),
                parts = out.parts.count(),
                "starting complete upload",
            );
            let req = CompleteRequest::new(&out.data, out.parts);
            let fut = SendCompleteUpload::new(this.client, req);
            this.fut.set(Some(fut));
        }
        let fut = this.fut.as_mut().as_pin_mut().unwrap();
        let resp = ready!(fut.poll(cx))?;
        let output = this.state.uploaded(resp);
        // Now transition `UploadInner` to be `Pending` a new upload.
        //
        // If there isn't another URI to get, `Terminated` is the state instead.
        // Either way this value should have a valid state when the method
        // returns, just if it's `Terminated` the fuse behavior will mean it's
        // not polled again.
        let next = this.iter.next();
        trace!(
            uri = %&output.uri,
            etag = %&output.etag,
            terminated = next.is_none(),
            "completed upload",
        );
        let new_inner = UploadInner::create_upload_maybe(this.client, next);
        this.fut.set(None);
        this.inner.set(new_inner);
        Poll::Ready(Ok(output))
    }
}

#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
#[pin_project::pin_project(project = UploadProj)]
enum UploadInner {
    Active(#[pin] UploadParts),
    Pending(#[pin] SendCreateUpload),
    Terminated,
}

impl UploadInner {
    fn active(
        client: &UploadClient,
        data: UploadData,
        capacity: Option<usize>,
    ) -> Self {
        Self::Active(UploadParts::new(client, data, capacity))
    }

    /// Try to create a new upload, but return `Terminated` if the URI provided
    /// is `None`.
    fn create_upload_maybe(
        client: &UploadClient,
        uri: Option<ObjectUri>,
    ) -> Self {
        let Some(uri) = uri else {
            return Self::Terminated;
        };
        let req = CreateRequest::new(uri);
        let fut = SendCreateUpload::new(client, req);
        Self::Pending(fut)
    }

    fn is_terminated(&self) -> bool {
        matches!(self, Self::Terminated)
    }

    fn get_active_proj(self: Pin<&mut Self>) -> Option<Pin<&mut UploadParts>> {
        let UploadProj::Active(upl) = self.project() else {
            return None;
        };
        Some(upl)
    }
}

/// The main state of `Upload` manages polling the buffer of request futures.
#[must_use = "futures do nothing unless polled"]
#[pin_project::pin_project]
struct UploadParts {
    #[pin]
    buf: PartBuffer,
    client: UploadClient,
    data: UploadData,
    parts: CompletedParts,
    current: PartNumber,
    is_terminated: bool,
}

impl UploadParts {
    fn new(
        client: &UploadClient,
        data: UploadData,
        capacity: Option<usize>,
    ) -> Self {
        Self {
            buf: PartBuffer::new(capacity),
            client: client.clone(),
            data,
            parts: CompletedParts::default(),
            current: PartNumber::new(),
            is_terminated: false,
        }
    }
}

impl FusedMultipartWrite<PartBody> for UploadParts {
    fn is_terminated(&self) -> bool {
        self.buf.is_terminated() || self.is_terminated
    }
}

impl MultipartWrite<PartBody> for UploadParts {
    type Error = Error;
    type Output = PartBufOutput;
    type Recv = PartBufRecv;

    fn poll_ready(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        let this = self.project();
        ready!(this.buf.poll_ready(cx)).err_with_upl(
            &this.data.id,
            &this.data.uri,
            this.parts,
        )?;
        Poll::Ready(Ok(()))
    }

    fn start_send(
        self: Pin<&mut Self>,
        body: PartBody,
    ) -> Result<Self::Recv, Self::Error> {
        let mut this = self.project();
        // Increments the part number while returning the current value.
        let ptnum = this.current.fetch_incr();
        let ptb = body.size();
        let req = UploadPartRequest::new(this.data, body, ptnum);
        let fut = SendUploadPart::new(this.client, req);
        let upb = this.buf.as_mut().start_send(fut)?;
        trace!(
            id = %&this.data.id,
            uri = %&this.data.uri,
            part_number = %ptnum,
            part_bytes = ptb,
            upload_bytes = upb,
            "sent part upload",
        );
        Ok(PartBufRecv::new(upb, ptb))
    }

    fn poll_flush(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        let this = self.project();
        let completed = ready!(this.buf.poll_complete(cx)).err_with_upl(
            &this.data.id,
            &this.data.uri,
            this.parts,
        )?;
        this.parts.append(completed);
        Poll::Ready(Ok(()))
    }

    fn poll_complete(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<Self::Output, Self::Error>> {
        let this = self.project();
        let completed = ready!(this.buf.poll_complete(cx)).err_with_upl(
            &this.data.id,
            &this.data.uri,
            this.parts,
        )?;
        let mut parts = std::mem::take(this.parts);
        parts.append(completed);
        let data = this.data.clone();
        trace!(
            id = %&data.id,
            uri = %&data.uri,
            bytes = parts.size(),
            parts = parts.count(),
            "finished uploading parts",
        );
        // To ensure this isn't accidentally polled again.
        *this.is_terminated = true;
        Poll::Ready(Ok(PartBufOutput { parts, data }))
    }
}

impl Debug for UploadParts {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("UploadParts")
            .field("buf", &self.buf)
            .field("client", &self.client)
            .field("data", &self.data)
            .field("parts", &self.parts)
            .field("current", &self.current)
            .field("is_terminated", &self.is_terminated)
            .finish()
    }
}

/// User-defined constraints on the overall upload.
#[derive(Debug, Clone, Copy)]
struct GlobalConfig {
    max_bytes: u64,
    // Capacity for the future buffer; limit to concurrency.
    capacity: Option<usize>,
}

/// To track overall upload progress.
#[derive(Debug, Clone, Copy)]
struct GlobalState {
    total_bytes: u64,
    total_parts: u64,
    start: Instant,
    config: GlobalConfig,
}

impl GlobalState {
    fn new(config: GlobalConfig) -> Self {
        Self { total_bytes: 0, total_parts: 0, start: Instant::now(), config }
    }

    fn should_complete(&self) -> bool {
        self.total_bytes >= self.config.max_bytes
    }

    fn upload_status(&mut self, recv: PartBufRecv) -> UploadStatus {
        let part_bytes = recv.part_bytes as u64;
        self.total_bytes += part_bytes;
        self.total_parts += 1;
        UploadStatus {
            total_bytes: self.total_bytes,
            total_parts: self.total_parts,
            upload_bytes: recv.upload_bytes as u64,
            duration: self.start.elapsed(),
            should_complete: self.should_complete(),
        }
    }

    fn uploaded(&self, resp: CompletedUpload) -> Uploaded {
        Uploaded {
            uri: resp.uri,
            etag: resp.etag,
            bytes: self.total_bytes,
            parts: self.total_parts,
            items: None,
            duration: self.start.elapsed(),
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct PartBufRecv {
    upload_bytes: usize,
    part_bytes: usize,
}

impl PartBufRecv {
    fn new(upload_bytes: usize, part_bytes: usize) -> Self {
        Self { upload_bytes, part_bytes }
    }
}

#[derive(Debug, Clone)]
struct PartBufOutput {
    parts: CompletedParts,
    data: UploadData,
}