-
Notifications
You must be signed in to change notification settings - Fork 17
/
multipart.go
489 lines (412 loc) · 13.1 KB
/
multipart.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package uplink
import (
"context"
"errors"
"math"
"runtime"
"strings"
"sync"
"time"
"github.com/zeebo/errs"
"storj.io/common/base58"
"storj.io/common/leak"
"storj.io/common/pb"
"storj.io/common/storj"
"storj.io/eventkit"
"storj.io/uplink/private/eestream/scheduler"
"storj.io/uplink/private/metaclient"
"storj.io/uplink/private/storage/streams"
"storj.io/uplink/private/stream"
"storj.io/uplink/private/testuplink"
)
// ErrUploadIDInvalid is returned when the upload ID is invalid.
var ErrUploadIDInvalid = errors.New("upload ID invalid")
// UploadInfo contains information about an upload.
type UploadInfo struct {
UploadID string
Key string
IsPrefix bool
System SystemMetadata
Custom CustomMetadata
}
// CommitUploadOptions options for committing multipart upload.
type CommitUploadOptions struct {
CustomMetadata CustomMetadata
}
// BeginUpload begins a new multipart upload to bucket and key.
//
// Use UploadPart to upload individual parts.
//
// Use CommitUpload to finish the upload.
//
// Use AbortUpload to cancel the upload at any time.
//
// UploadObject is a convenient way to upload single part objects.
func (project *Project) BeginUpload(ctx context.Context, bucket, key string, options *UploadOptions) (info UploadInfo, err error) {
defer mon.Task()(&ctx)(&err)
switch {
case bucket == "":
return UploadInfo{}, errwrapf("%w (%q)", ErrBucketNameInvalid, bucket)
case key == "":
return UploadInfo{}, errwrapf("%w (%q)", ErrObjectKeyInvalid, key)
}
if options == nil {
options = &UploadOptions{}
}
encPath, err := encryptPath(project, bucket, key)
if err != nil {
return UploadInfo{}, packageError.Wrap(err)
}
metainfoClient, err := project.dialMetainfoClient(ctx)
if err != nil {
return UploadInfo{}, packageError.Wrap(err)
}
defer func() { err = errs.Combine(err, metainfoClient.Close()) }()
response, err := metainfoClient.BeginObject(ctx, metaclient.BeginObjectParams{
Bucket: []byte(bucket),
EncryptedObjectKey: []byte(encPath.Raw()),
ExpiresAt: options.Expires,
EncryptionParameters: project.encryptionParameters,
})
if err != nil {
return UploadInfo{}, convertKnownErrors(err, bucket, key)
}
encodedStreamID := base58.CheckEncode(response.StreamID[:], 1)
return UploadInfo{
Key: key,
UploadID: encodedStreamID,
System: SystemMetadata{
Expires: options.Expires,
},
}, nil
}
// CommitUpload commits a multipart upload to bucket and key started with BeginUpload.
//
// uploadID is an upload identifier returned by BeginUpload.
func (project *Project) CommitUpload(ctx context.Context, bucket, key, uploadID string, opts *CommitUploadOptions) (object *Object, err error) {
defer mon.Task()(&ctx)(&err)
// TODO add completedPart to options when we will have implementation for that
if opts == nil {
opts = &CommitUploadOptions{}
}
metainfoDB, err := project.dialMetainfoDB(ctx)
if err != nil {
return nil, packageError.Wrap(err)
}
defer func() { err = errs.Combine(err, metainfoDB.Close()) }()
mObject, err := metainfoDB.CommitObject(ctx, bucket, key, uploadID, opts.CustomMetadata, project.encryptionParameters)
if err != nil {
return nil, convertKnownErrors(err, bucket, key)
}
return convertObject(&mObject), nil
}
// UploadPart uploads a part with partNumber to a multipart upload started with BeginUpload.
//
// uploadID is an upload identifier returned by BeginUpload.
func (project *Project) UploadPart(ctx context.Context, bucket, key, uploadID string, partNumber uint32) (_ *PartUpload, err error) {
upload := &PartUpload{
bucket: bucket,
key: key,
part: &Part{
PartNumber: partNumber,
},
stats: newOperationStats(ctx, project.access.satelliteURL),
eTagCh: make(chan []byte, 1),
}
upload.task = mon.TaskNamed("PartUpload")(&ctx)
defer func() {
if err != nil {
upload.stats.flagFailure(err)
upload.emitEvent(false)
}
}()
defer upload.stats.trackWorking()()
defer mon.Task()(&ctx)(&err)
switch {
case bucket == "":
return nil, errwrapf("%w (%q)", ErrBucketNameInvalid, bucket)
case key == "":
return nil, errwrapf("%w (%q)", ErrObjectKeyInvalid, key)
case uploadID == "":
return nil, packageError.Wrap(ErrUploadIDInvalid)
case partNumber >= math.MaxInt32:
return nil, packageError.New("partNumber should be less than max(int32)")
}
decodedStreamID, version, err := base58.CheckDecode(uploadID)
if err != nil || version != 1 {
return nil, packageError.Wrap(ErrUploadIDInvalid)
}
if encPath, err := encryptPath(project, bucket, key); err == nil {
upload.stats.encPath = encPath
}
ctx, cancel := context.WithCancel(ctx)
upload.cancel = cancel
streams, err := project.getStreamsStore(ctx)
if err != nil {
return nil, convertKnownErrors(err, bucket, key)
}
upload.streams = streams
if project.concurrentSegmentUploadConfig == nil {
upload.upload = stream.NewUploadPart(ctx, bucket, key, decodedStreamID, partNumber, upload.eTagCh, streams)
} else {
sched := scheduler.New(project.concurrentSegmentUploadConfig.SchedulerOptions)
u, err := streams.UploadPart(ctx, bucket, key, decodedStreamID, int32(partNumber), upload.eTagCh, sched)
if err != nil {
return nil, convertKnownErrors(err, bucket, key)
}
upload.upload = u
}
upload.tracker = project.tracker.Child("upload-part", 1)
return upload, nil
}
// AbortUpload aborts a multipart upload started with BeginUpload.
//
// uploadID is an upload identifier returned by BeginUpload.
func (project *Project) AbortUpload(ctx context.Context, bucket, key, uploadID string) (err error) {
defer mon.Task()(&ctx)(&err)
switch {
case bucket == "":
return errwrapf("%w (%q)", ErrBucketNameInvalid, bucket)
case key == "":
return errwrapf("%w (%q)", ErrObjectKeyInvalid, key)
case uploadID == "":
return packageError.Wrap(ErrUploadIDInvalid)
}
decodedStreamID, version, err := base58.CheckDecode(uploadID)
if err != nil || version != 1 {
return packageError.Wrap(ErrUploadIDInvalid)
}
id, err := storj.StreamIDFromBytes(decodedStreamID)
if err != nil {
return packageError.Wrap(err)
}
encPath, err := encryptPath(project, bucket, key)
if err != nil {
return convertKnownErrors(err, bucket, key)
}
metainfoClient, err := project.dialMetainfoClient(ctx)
if err != nil {
return convertKnownErrors(err, bucket, key)
}
defer func() { err = errs.Combine(err, metainfoClient.Close()) }()
_, err = metainfoClient.BeginDeleteObject(ctx, metaclient.BeginDeleteObjectParams{
Bucket: []byte(bucket),
EncryptedObjectKey: []byte(encPath.Raw()),
StreamID: id,
Status: int32(pb.Object_UPLOADING),
})
return convertKnownErrors(err, bucket, key)
}
// ListUploadParts returns an iterator over the parts of a multipart upload started with BeginUpload.
func (project *Project) ListUploadParts(ctx context.Context, bucket, key, uploadID string, options *ListUploadPartsOptions) *PartIterator {
defer mon.Task()(&ctx)(nil)
opts := metaclient.ListSegmentsParams{}
if options != nil {
opts.Cursor = metaclient.SegmentPosition{
PartNumber: int32(options.Cursor),
// cursor needs to be last segment in a part
// satellite can accept uint32 as segment index
// but protobuf is defined as int32 for now
Index: math.MaxInt32,
}
}
parts := PartIterator{
ctx: ctx,
project: project,
bucket: bucket,
key: key,
options: opts,
uploadID: uploadID,
}
switch {
case parts.bucket == "":
parts.err = errwrapf("%w (%q)", ErrBucketNameInvalid, parts.bucket)
return &parts
case parts.key == "":
parts.err = errwrapf("%w (%q)", ErrObjectKeyInvalid, parts.key)
return &parts
case parts.uploadID == "":
parts.err = packageError.Wrap(ErrUploadIDInvalid)
return &parts
}
decodedStreamID, version, err := base58.CheckDecode(uploadID)
if err != nil || version != 1 {
parts.err = packageError.Wrap(ErrUploadIDInvalid)
return &parts
}
parts.options.StreamID = decodedStreamID
return &parts
}
// ListUploads returns an iterator over the uncommitted uploads in bucket.
// Both multipart and regular uploads are returned. An object may not be
// visible through ListUploads until it has a committed part.
func (project *Project) ListUploads(ctx context.Context, bucket string, options *ListUploadsOptions) *UploadIterator {
defer mon.Task()(&ctx)(nil)
opts := metaclient.ListOptions{
Direction: metaclient.After,
Status: int32(pb.Object_UPLOADING), // TODO: define object status constants in storj package?
}
if options != nil {
opts.Prefix = options.Prefix
opts.Cursor = options.Cursor
opts.Recursive = options.Recursive
opts.IncludeSystemMetadata = options.System
opts.IncludeCustomMetadata = options.Custom
}
opts.Limit = testuplink.GetListLimit(ctx)
uploads := UploadIterator{
ctx: ctx,
project: project,
bucket: bucket,
options: opts,
}
if opts.Prefix != "" && !strings.HasSuffix(opts.Prefix, "/") {
uploads.listObjects = listPendingObjectStreams
} else {
uploads.listObjects = listObjects
}
if options != nil {
uploads.uploadOptions = *options
}
return &uploads
}
// Part part metadata.
type Part struct {
PartNumber uint32
// Size plain size of a part.
Size int64
Modified time.Time
ETag []byte
}
// PartUpload is a part upload to started multipart upload.
type PartUpload struct {
mu sync.Mutex
closed bool
aborted bool
cancel context.CancelFunc
upload streamUpload
bucket string
key string
part *Part
streams *streams.Store
eTagCh chan []byte
stats operationStats
task func(*error)
tracker leak.Ref
}
// Write uploads len(p) bytes from p to the object's data stream.
// It returns the number of bytes written from p (0 <= n <= len(p))
// and any error encountered that caused the write to stop early.
func (upload *PartUpload) Write(p []byte) (int, error) {
track := upload.stats.trackWorking()
n, err := upload.upload.Write(p)
upload.mu.Lock()
upload.stats.bytes += int64(n)
upload.stats.flagFailure(err)
track()
upload.mu.Unlock()
return n, convertKnownErrors(err, upload.bucket, upload.key)
}
// SetETag sets ETag for a part.
func (upload *PartUpload) SetETag(eTag []byte) error {
upload.mu.Lock()
defer upload.mu.Unlock()
if upload.part.ETag != nil {
return packageError.New("etag already set")
}
if upload.aborted {
return errwrapf("%w: upload aborted", ErrUploadDone)
}
if upload.closed {
return errwrapf("%w: already committed", ErrUploadDone)
}
upload.part.ETag = eTag
upload.eTagCh <- eTag
return nil
}
// Commit commits a part.
//
// Returns ErrUploadDone when either Abort or Commit has already been called.
func (upload *PartUpload) Commit() error {
track := upload.stats.trackWorking()
upload.mu.Lock()
defer upload.mu.Unlock()
if upload.aborted {
return errwrapf("%w: already aborted", ErrUploadDone)
}
if upload.closed {
return errwrapf("%w: already committed", ErrUploadDone)
}
upload.closed = true
// ETag must not be sent after a call to commit. The upload code waits on
// the channel before committing the last segment. Closing the channel
// allows the upload code to unblock if no eTag has been set. Not all
// multipart uploaders care about setting the eTag so we can't assume it
// has been set.
close(upload.eTagCh)
err := errs.Combine(
upload.upload.Commit(),
upload.streams.Close(),
upload.tracker.Close(),
)
upload.stats.flagFailure(err)
track()
upload.emitEvent(false)
return convertKnownErrors(err, upload.bucket, upload.key)
}
// Abort aborts the part upload.
//
// Returns ErrUploadDone when either Abort or Commit has already been called.
func (upload *PartUpload) Abort() error {
track := upload.stats.trackWorking()
upload.mu.Lock()
defer upload.mu.Unlock()
if upload.closed {
return errwrapf("%w: already committed", ErrUploadDone)
}
if upload.aborted {
return errwrapf("%w: already aborted", ErrUploadDone)
}
upload.aborted = true
upload.cancel()
err := errs.Combine(
upload.upload.Abort(),
upload.streams.Close(),
upload.tracker.Close(),
)
upload.stats.flagFailure(err)
track()
upload.emitEvent(true)
return convertKnownErrors(err, upload.bucket, upload.key)
}
// Info returns the last information about the uploaded part.
func (upload *PartUpload) Info() *Part {
if meta := upload.upload.Meta(); meta != nil {
upload.part.Size = meta.Size
upload.part.Modified = meta.Modified
}
return upload.part
}
func (upload *PartUpload) emitEvent(aborted bool) {
message, err := upload.stats.err()
upload.task(&err)
evs.Event("part-upload",
eventkit.Int64("bytes", upload.stats.bytes),
eventkit.Duration("user-elapsed", time.Since(upload.stats.start)),
eventkit.Duration("working-elapsed", upload.stats.working),
eventkit.Bool("success", err == nil),
eventkit.String("error", message),
eventkit.Bool("aborted", aborted),
eventkit.String("arch", runtime.GOARCH),
eventkit.String("os", runtime.GOOS),
eventkit.Int64("cpus", int64(runtime.NumCPU())),
eventkit.Int64("quic-rollout", int64(upload.stats.quicRollout)),
eventkit.String("satellite", upload.stats.satellite),
eventkit.Bytes("path-checksum", pathChecksum(upload.stats.encPath)),
eventkit.Int64("noise-version", noiseVersion),
// segment count
// ram available
)
}