-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_upload_parts.go
383 lines (312 loc) · 9.86 KB
/
s3_upload_parts.go
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
package main
import (
"context"
"errors"
"fmt"
"log"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
// S3UploadParts manages the process of a multi-part upload. Callers should
// use UploadPart, CompleteUpload, and AbortUpload to add parts and finalize
// the process.
type S3UploadParts struct {
// ctx is a cancelable context that may be used to short-circuit
// processing of in-flight part uploads
ctx context.Context
// cancel may be used to cancel the context
cancel context.CancelCauseFunc
// st tracks the state of this upload
st *S3UploadState
// ch provides a channel for submitting parts to upload
ch chan *queuedPart
// pending tracks the number of queued parts still pending
pending *sync.WaitGroup
// opts records the Options provided when this S3UploadParts was
// created
opts *Options
// mu is a shared lock for any operations that might need to be gated
// for concurrency safety
mu *sync.Mutex
// callers may optionally use NextPartID to generate the PartID for
// their uploaded parts, and this counter tracks the next available
// PartID.
lastPartID int32
}
// NewS3UploadParts initializes a new S3UploadPart. The context may be used to
// cancel any in-flight uploads. The S3Hasher hr should be used to provide the
// hashed signatures of parts submitted via UploadPart (see S3HashReader and
// S3HashWriter).
func NewS3UploadParts(
ctx context.Context,
hr *S3Hasher,
create *s3.CreateMultipartUploadInput,
opts *Options) (*S3UploadParts, error) {
ctx, cancel := context.WithCancelCause(ctx)
s3client := opts.s3.Get()
out, err := s3client.CreateMultipartUpload(ctx, create)
opts.s3.Put(s3client)
if err != nil {
return nil, err
}
if opts.Verbose {
log.Printf("started upload of multi-part object %s/%s using UploadId %s",
*create.Bucket, *create.Key, *out.UploadId)
}
p := &S3UploadParts{
st: &S3UploadState{
hr: hr,
create: create,
createOutput: out,
uploadPartOutputs: make(map[int32]*s3.UploadPartOutput),
uploadPartErrors: make(map[int32]error),
mu: &sync.Mutex{},
},
ctx: ctx,
cancel: cancel,
ch: make(chan *queuedPart),
pending: &sync.WaitGroup{},
opts: opts,
mu: &sync.Mutex{},
}
for i := 0; i < p.opts.ConcurrentParts; i++ {
go func() {
for {
select {
case q := <-p.ch:
// received queuedPart
select {
case q.ch <- p.uploadPart(q.part):
// return results of upload
case <-p.ctx.Done():
// aborted due to canceled context
return
}
case <-p.ctx.Done():
// aborted due to canceled context
return
}
}
}()
}
return p, nil
}
var ErrMaxPartID = errors.New("partID limit reached")
// UploadID returns the in-flight UploadID for this multi-part upload, note
// that the id can be invalidated once CompleteMultipartUpload or
// AbortMultipartUpload are called.
func (p *S3UploadParts) UploadID() *string {
if p.st.createOutput != nil && p.st.createOutput.UploadId != nil {
return p.st.createOutput.UploadId
}
return nil
}
func (p *S3UploadParts) Bucket() *string {
if p.st.create != nil && p.st.create.Bucket != nil {
return p.st.create.Bucket
}
return nil
}
func (p *S3UploadParts) Key() *string {
if p.st.create != nil && p.st.create.Key != nil {
return p.st.create.Key
}
return nil
}
// NextPartID is a convenience method to return a sequence of PartID starting
// at 1 and ending at Options.MaxPartID. If MaxPartID has been reached then
// (0, ErrMaxPartID) is returned.
func (p *S3UploadParts) NextPartID() (int32, error) {
p.mu.Lock()
defer p.mu.Unlock()
if p.lastPartID == p.opts.MaxPartID {
return 0, ErrMaxPartID
}
p.lastPartID += 1
return p.lastPartID, nil
}
// Cancel will cancel the context associated with this S3UploadPart. After
// Cancel has been called the S3UploadPart will not process any more UploadPart
// requests, but AbortUpload and CompleteUpload may still be called.
func (p *S3UploadParts) Cancel(err error) {
p.cancel(err)
}
// Canceled returns true if the context associated with this S3UploadPart has
// been canceled.
func (p *S3UploadParts) Canceled() bool {
select {
case <-p.ctx.Done():
return true
default:
return false
}
}
// UploadPart submits part *s3.UploadPartInput to the worker routines for
// processing.
//
// The returned channel may optionally be polled to determine if there was an
// error processing the request, either because the context was canceled or
// because of an upload error. If there was an error it will also be available
// via PartResults usng the part's PartNumber.
//
// A caller may access the p.PartResults values after the return channel has
// been written to or after p.Wait unblocks.
func (p *S3UploadParts) UploadPart(part *s3.UploadPartInput) chan error {
// increment the pending WaitGroup by one
p.pending.Add(1)
q := &queuedPart{
// record the record the s3.UploadPartInput to process
part: part,
// channel is size 1 so that reading the result is optional for
// the caller
ch: make(chan error, 1),
}
go func(q *queuedPart) {
select {
case p.ch <- q:
// accepted for processing by a worker, it is now the
// responsibility of the uploadPart method to decrement
// the pending WaitGroup.
case <-p.ctx.Done():
// aborted due to canceled context
// decrement pending WaitGroup by one
p.pending.Done()
// record the cause of the cancelation
err := context.Cause(p.ctx)
// for this part number record the cancelation error a
// the results
p.st.setPartResults(q.part.PartNumber, nil, err)
// and return the cancelation error back to the caller
// if they are waiting for it
q.ch <- err
}
}(q)
return q.ch
}
// uploadPart actually submits the s3 client request to upload the part,
// records the outcome, and returns any error
func (p *S3UploadParts) uploadPart(part *s3.UploadPartInput) error {
defer p.pending.Done()
s3client := p.opts.s3.Get()
defer p.opts.s3.Put(s3client)
if p.opts.Verbose {
log.Printf("starting upload of %s/%s part %d using UploadId %s",
*part.Bucket, *part.Key, *part.PartNumber, *part.UploadId)
}
out, err := s3client.UploadPart(p.ctx, part)
if p.opts.Verbose {
outcome := "completed"
if err != nil {
outcome = "failed"
}
log.Printf("%s upload of %s/%s part %d using UploadId %s",
outcome, *part.Bucket, *part.Key, *part.PartNumber, *part.UploadId)
}
p.st.setPartResults(part.PartNumber, out, err)
return err
}
// Wait blocks until all the parts submitted via p.UploadPart have finished
// processing or have been rejected due to a canceled context, or until the
// underlying context has been canceled, or until any > 0 timeout has been
// reached.
func (p *S3UploadParts) Wait(timeout time.Duration) error {
var timer <-chan time.Time
if timeout > 0 {
timer = time.After(timeout)
}
done := make(chan bool, 1)
go func() {
p.pending.Wait()
done <- true
}()
select {
case <-done:
return nil
case <-p.ctx.Done():
return context.Cause(p.ctx)
case t := <-timer:
return fmt.Errorf("%w: %s (%s)", ErrTimeout, t, timeout)
}
}
// PartResults returns the results for a part's PartNumber. It is only
// guaranteed to return a result after p.Wait unblocks or after the original
// UploadPart's error channel has returned a value.
func (p *S3UploadParts) PartResults(partID int32) (*s3.UploadPartOutput, error) {
return p.st.uploadPartOutputs[partID], p.st.uploadPartErrors[partID]
}
// CompleteUpload attempts to complete an upload of parts. It should only be
// called once all the parts have been submitted via p.UploadPart and p.Wait
// has unblocked. If timeout is > 0 then the complete upload process will try
// to cancel the process if it takes longer than the specified timeout.
func (p *S3UploadParts) CompleteUpload(timeout time.Duration) error {
p.mu.Lock()
defer p.mu.Unlock()
s3client := p.opts.s3.Get()
defer p.opts.s3.Put(s3client)
var ctx context.Context
var cancelTimeout context.CancelFunc
if timeout > 0 {
ctx, cancelTimeout = context.WithTimeout(context.Background(), timeout)
defer cancelTimeout()
} else {
ctx = context.Background()
}
params, err := p.st.completeParts()
if err != nil {
p.st.completedError = err
} else {
if p.opts.Verbose {
log.Printf("completing upload for multi-part object %s/%s using UploadId %s",
*params.Bucket, *params.Key, *params.UploadId)
}
out, err := s3client.CompleteMultipartUpload(ctx, params)
p.st.completedOutput = out
p.st.completedError = err
if err == nil {
attr, err := getObjectAttributes(
ctx, *params.Bucket, *params.Key, p.opts)
p.st.objectAttributesOutput = attr
p.st.objectAttributesError = err
}
}
return p.st.completedError
}
// AbortUpload attempts to abort an upload of parts, if timeout is > 0 then the
// abort process will try to cancel the process if it takes longer than the
// specified timeout.
func (p *S3UploadParts) AbortUpload(timeout time.Duration) error {
p.mu.Lock()
defer p.mu.Unlock()
s3client := p.opts.s3.Get()
defer p.opts.s3.Put(s3client)
var ctx context.Context
var cancelTimeout context.CancelFunc
if timeout > 0 {
ctx, cancelTimeout = context.WithTimeout(context.Background(), timeout)
defer cancelTimeout()
} else {
ctx = context.Background()
}
params := &s3.AbortMultipartUploadInput{
Bucket: p.st.create.Bucket,
Key: p.st.create.Key,
UploadId: p.st.createOutput.UploadId,
}
if p.opts.Verbose {
log.Printf("aborting upload multi-part object %s/%s using UploadId %s",
*params.Bucket, *params.Key, *params.UploadId)
}
out, err := s3client.AbortMultipartUpload(ctx, params)
p.st.abortedOutput = out
p.st.abortedError = err
return err
}
// queuedPar combines a submitted part for upload with an error channel to
// return any error outcome to the caller. The channel will be size 1 to make
// polling the channel optional for the caller (since the results are also
// recorded in the S3UploadState)
type queuedPart struct {
part *s3.UploadPartInput
ch chan error
}