-
Notifications
You must be signed in to change notification settings - Fork 17
/
upload.go
344 lines (291 loc) · 9.2 KB
/
upload.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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package uplink
import (
"context"
"errors"
"io"
"runtime"
"sync"
"time"
_ "unsafe" // for go:linkname
"github.com/zeebo/errs"
"storj.io/common/leak"
"storj.io/common/pb"
"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"
)
// ErrUploadDone is returned when either Abort or Commit has already been called.
var ErrUploadDone = errors.New("upload done")
// UploadOptions contains additional options for uploading.
type UploadOptions struct {
// When Expires is zero, there is no expiration.
Expires time.Time
}
// UploadObject starts an upload to the specific key.
//
// It is not guaranteed that the uncommitted object is visible through ListUploads while uploading.
func (project *Project) UploadObject(ctx context.Context, bucket, key string, options *UploadOptions) (_ *Upload, err error) {
metaOpts := &metaclient.UploadOptions{}
if options != nil {
metaOpts.Expires = options.Expires
}
return project.uploadObjectWithRetention(ctx, bucket, key, metaOpts)
}
func (project *Project) uploadObjectWithRetention(ctx context.Context, bucket, key string, options *metaclient.UploadOptions) (_ *Upload, err error) {
upload := &Upload{
bucket: bucket,
stats: newOperationStats(ctx, project.access.satelliteURL),
}
upload.task = mon.TaskNamed("Upload")(&ctx)
defer func() {
if err != nil {
upload.stats.flagFailure(err)
upload.emitEvent(false)
}
}()
defer upload.stats.trackWorking()()
defer mon.Task()(&ctx)(&err)
if bucket == "" {
return nil, errwrapf("%w (%q)", ErrBucketNameInvalid, bucket)
}
if key == "" {
return nil, errwrapf("%w (%q)", ErrObjectKeyInvalid, key)
}
if options == nil {
options = &metaclient.UploadOptions{}
}
// N.B. we always call dbCleanup which closes the db because
// closing it earlier has the benefit of returning a connection to
// the pool, so we try to do that as early as possible.
db, err := project.dialMetainfoDB(ctx)
if err != nil {
return nil, convertKnownErrors(err, bucket, key)
}
defer func() { err = errs.Combine(err, db.Close()) }()
obj, err := db.CreateObject(ctx, bucket, key, nil)
if err != nil {
return nil, convertKnownErrors(err, bucket, key)
}
info := obj.Info()
ctx, cancel := context.WithCancel(ctx)
upload.cancel = cancel
upload.object = &info
meta := dynamicMetadata{upload.object}
mutableStream, err := obj.CreateDynamicStream(ctx, meta)
if err != nil {
return nil, convertKnownErrors(err, bucket, key)
}
// Return the connection to the pool as soon as we can.
if err := db.Close(); err != nil {
return nil, convertKnownErrors(err, bucket, key)
}
// TODO: don't calculate this twice.
if encPath, err := encryptPath(project, bucket, key); err == nil {
upload.stats.encPath = encPath
}
streams, err := project.getStreamsStore(ctx)
if err != nil {
return nil, convertKnownErrors(err, bucket, key)
}
upload.streams = streams
if project.concurrentSegmentUploadConfig == nil {
upload.upload = stream.NewUpload(ctx, mutableStream, streams, options)
} else {
sched := scheduler.New(project.concurrentSegmentUploadConfig.SchedulerOptions)
u, err := streams.UploadObject(ctx, mutableStream.BucketName(), mutableStream.Path(), mutableStream, sched, options)
if err != nil {
return nil, convertKnownErrors(err, bucket, key)
}
upload.upload = u
}
upload.tracker = project.tracker.Child("upload", 1)
return upload, nil
}
type dynamicMetadata struct{ *metaclient.Object }
func (dyn dynamicMetadata) Metadata() ([]byte, error) {
return pb.Marshal(&pb.SerializableMeta{
UserDefined: CustomMetadata(dyn.Object.Metadata).Clone(),
})
}
type streamUpload interface {
io.Writer
Commit() error
Abort() error
Meta() *streams.Meta
}
// Upload is an upload to Storj Network.
type Upload struct {
mu sync.Mutex
closed bool
aborted bool
cancel context.CancelFunc
upload streamUpload
bucket string
object *metaclient.Object
streams *streams.Store
stats operationStats
task func(*error)
tracker leak.Ref
}
// Info returns the last information about the uploaded object.
func (upload *Upload) Info() *Object {
if upload.object == nil {
return nil
}
obj := convertObject(upload.object)
meta := upload.upload.Meta()
if meta != nil {
obj.System.ContentLength = meta.Size
obj.System.Created = meta.Modified
obj.version = meta.Version
obj.isVersioned = meta.IsVersioned
}
return obj
}
// 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 *Upload) Write(p []byte) (n int, err 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.object.Path)
}
// Commit commits data to the store.
//
// Returns ErrUploadDone when either Abort or Commit has already been called.
func (upload *Upload) 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
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.object.Path)
}
// Abort aborts the upload.
//
// Returns ErrUploadDone when either Abort or Commit has already been called.
func (upload *Upload) 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(),
)
track()
upload.stats.flagFailure(err)
upload.emitEvent(true)
return convertKnownErrors(err, upload.bucket, upload.object.Path)
}
func (upload *Upload) emitEvent(aborted bool) {
message, err := upload.stats.err()
upload.task(&err)
expires := false
if upload.upload != nil {
meta := upload.upload.Meta()
if meta != nil && !meta.Expiration.IsZero() {
expires = true
}
}
evs.Event("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.Bool("expires", expires),
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),
// upload.upload.Meta().Expiration
// segment count
// ram available
)
}
// SetCustomMetadata updates custom metadata to be included with the object.
// If it is nil, it won't be modified.
func (upload *Upload) SetCustomMetadata(ctx context.Context, custom CustomMetadata) error {
upload.mu.Lock()
defer upload.mu.Unlock()
if upload.aborted {
return errwrapf("%w: upload aborted", ErrUploadDone)
}
if upload.closed {
return errwrapf("%w: already committed", ErrUploadDone)
}
if upload.upload.Meta() != nil {
return errwrapf("%w: already committed", ErrUploadDone)
}
if custom != nil {
if err := custom.Verify(); err != nil {
return packageError.Wrap(err)
}
upload.object.Metadata = custom.Clone()
}
return nil
}
// uploadObjectWithRetention exposes the private project.uploadObjectWithRetention method.
//
// NB: this is used with linkname in private/object.
// It needs to be updated when this is updated.
//
//lint:ignore U1000, used with linkname
//nolint:deadcode,unused
//go:linkname uploadObjectWithRetention
func uploadObjectWithRetention(ctx context.Context, project *Project, bucket, key string, options *metaclient.UploadOptions) (_ *Upload, err error) {
return project.uploadObjectWithRetention(ctx, bucket, key, options)
}
// upload_getMetaclientObject exposes the upload's private metaclient.Object.
//
// NB: this is used with linkname in private/object.
// It needs to be updated when this is updated.
//
//lint:ignore U1000, used with linkname
//nolint:deadcode,unused
//go:linkname upload_getMetaclientObject
func upload_getMetaclientObject(u *Upload) *metaclient.Object { return u.object }
// upload_getStreamMeta exposes the upload's stream metadata.
//
// NB: this is used with linkname in private/object.
// It needs to be updated when this is updated.
//
//lint:ignore U1000, used with linkname
//nolint:deadcode,unused
//go:linkname upload_getStreamMeta
func upload_getStreamMeta(u *Upload) *streams.Meta { return u.upload.Meta() }