/
resumable_v2.go
526 lines (460 loc) · 15.7 KB
/
resumable_v2.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package kodocli
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"math/rand"
"sort"
"strings"
"sync"
"time"
"github.com/service-sdk/go-sdk-qn/v2/operation/internal/x/limit"
"github.com/service-sdk/go-sdk-qn/v2/operation/internal/x/httputil.v1"
"github.com/service-sdk/go-sdk-qn/v2/operation/internal/x/xlog.v8"
)
const minUploadPartSize = 1 << 22
const uploadPartRetryTimes = 5
const deletePartsRetryTimes = 10
const completePartsRetryTimes = 5
var ErrMd5NotMatch = httputil.NewError(406, "md5 not match")
// https://github.com/qbox/product/blob/master/kodo/resumable-up-v2/init_parts.md
func (p Uploader) initParts(ctx context.Context, host, bucket, key string, hasKey bool) (uploadId string, suggestedPartSize int64, err error) {
url1 := fmt.Sprintf("%s/buckets/%s/objects/%s/uploads", host, bucket, encodeKey(key, hasKey))
ret := struct {
UploadId string `json:"uploadId"`
SuggestedPartSize int64 `json:"suggestedPartSize,omitempty"`
}{}
err = p.Conn.Call(ctx, &ret, "POST", url1)
uploadId = ret.UploadId
suggestedPartSize = ret.SuggestedPartSize
return
}
type UploadPartRet struct {
Etag string `json:"etag"`
Md5 string `json:"md5"`
}
// https://github.com/qbox/product/blob/master/kodo/resumable-up-v2/upload_parts.md
func (p Uploader) uploadPart(ctx context.Context, host, bucket, key string, hasKey bool, uploadId string, partNum int, body io.Reader, bodyLen int) (ret UploadPartRet, err error) {
url1 := fmt.Sprintf("%s/buckets/%s/objects/%s/uploads/%s/%d", host, bucket, encodeKey(key, hasKey), uploadId, partNum)
h := md5.New()
tr := io.TeeReader(body, h)
err = p.Conn.CallWith(ctx, &ret, "PUT", url1, "application/octet-stream", tr, bodyLen)
if err != nil {
return
}
partMd5 := hex.EncodeToString(h.Sum(nil))
if partMd5 != ret.Md5 {
err = ErrMd5NotMatch
}
return
}
type CompleteMultipart struct {
Parts []Part `json:"parts"`
Fname string `json:"fname"`
MimeType string `json:"mimeType"`
Metadata map[string]string `json:"metadata"`
CustomVars map[string]string `json:"customVars"`
}
type Part struct {
PartNumber int `json:"partNumber"`
Etag string `json:"etag"`
}
// https://github.com/qbox/product/blob/master/kodo/resumable-up-v2/complete_parts.md
func (p Uploader) completeParts(ctx context.Context, host string, ret interface{}, bucket, key string, hasKey bool, uploadId string, mPart *CompleteMultipart) error {
key = encodeKey(key, hasKey)
metaData := make(map[string]string)
for k, v := range mPart.Metadata {
metaData["x-qn-meta-"+k] = v
}
mp := *mPart
mp.Metadata = metaData
url1 := fmt.Sprintf("%s/buckets/%s/objects/%s/uploads/%s", host, bucket, key, uploadId)
return p.Conn.CallWithJson(ctx, &ret, "POST", url1, mp)
}
type CompletePartsRet struct {
Hash string `json:"hash"`
Key string `json:"key"`
}
func (p *CompleteMultipart) Len() int {
return len(p.Parts)
}
func (p *CompleteMultipart) Swap(i, j int) {
p.Parts[i], p.Parts[j] = p.Parts[j], p.Parts[i]
}
func (p *CompleteMultipart) Less(i, j int) bool {
return p.Parts[i].PartNumber < p.Parts[j].PartNumber
}
func (p *CompleteMultipart) Sort() {
sort.Sort(p)
}
// https://github.com/qbox/product/blob/master/kodo/resumable-up-v2/delete_parts.md
func (p Uploader) deleteParts(ctx context.Context, host, bucket, key string, hasKey bool, uploadId string) error {
url1 := fmt.Sprintf("%s/buckets/%s/objects/%s/uploads/%s", host, bucket, encodeKey(key, hasKey), uploadId)
return p.Conn.Call(ctx, nil, "DELETE", url1)
}
func (p Uploader) Upload(ctx context.Context, ret interface{}, uptoken string, key string, f io.ReaderAt, fsize int64,
mp *CompleteMultipart, partNotify func(partIdx int, etag string)) error {
uploadParts := p.makeUploadParts(fsize)
return p.upload(ctx, ret, uptoken, key, true, f, fsize, true, uploadParts, mp, partNotify)
}
func (p Uploader) UploadWithParts(ctx context.Context, ret interface{}, uptoken string, key string, f io.ReaderAt, fsize int64, uploadParts []int64,
mp *CompleteMultipart, partNotify func(partIdx int, etag string)) error {
if !p.checkUploadParts(fsize, uploadParts) {
return errors.New("part size not equal with fsize")
}
return p.upload(ctx, ret, uptoken, key, true, f, fsize, false, uploadParts, mp, partNotify)
}
func (p Uploader) UploadWithoutKey(ctx context.Context, ret interface{}, uptoken string, f io.ReaderAt, fsize int64,
mp *CompleteMultipart, partNotify func(partIdx int, etag string)) error {
uploadParts := p.makeUploadParts(fsize)
return p.upload(ctx, ret, uptoken, "", false, f, fsize, true, uploadParts, mp, partNotify)
}
func (p Uploader) UploadWithoutKeyWithParts(ctx context.Context, ret interface{}, uptoken string, f io.ReaderAt, fsize int64, uploadParts []int64,
mp *CompleteMultipart, partNotify func(partIdx int, etag string)) error {
if !p.checkUploadParts(fsize, uploadParts) {
return errors.New("part size not equal with fsize")
}
return p.upload(ctx, ret, uptoken, "", false, f, fsize, false, uploadParts, mp, partNotify)
}
func (p Uploader) upload(ctx context.Context, ret interface{}, uptoken, key string, hasKey bool, f io.ReaderAt, fsize int64, usePartSizeAsSuggested bool, uploadParts []int64,
mp *CompleteMultipart, partNotify func(partIdx int, etag string)) error {
xl := xlog.FromContextSafe(ctx)
if fsize == 0 {
return errors.New("can't upload empty file")
}
policy, err := ParseUpToken(uptoken)
if err != nil {
return err
}
bucket := strings.Split(policy.Scope, ":")[0]
p.Conn.Client = newUptokenClient(uptoken, p.Conn.Transport)
upHost := p.chooseUpHost(make(map[string]struct{}))
uploadId, suggestedPartSize, err := p.initParts(ctx, upHost, bucket, key, hasKey)
if err != nil {
failHostName(upHost)
return err
} else {
succeedHostName(upHost)
}
concurrency := p.Concurrency
if usePartSizeAsSuggested && suggestedPartSize > 0 {
suggestedPartSize, concurrency = p.adaptivePartSizeAndConcurrency(ctx, suggestedPartSize)
uploadParts = p.makeUploadPartsByPartSize(fsize, suggestedPartSize)
}
var partUpErr error
partUpErrLock := sync.Mutex{}
partCnt := len(uploadParts)
parts := make([]Part, partCnt)
partUpCtx, cancel := context.WithCancel(ctx)
defer cancel()
var bkLimit = limit.NewBlockingCount(concurrency)
var wg sync.WaitGroup
var lastPartEnd int64 = 0
for i := 0; i < partCnt; i++ {
wg.Add(1)
bkLimit.Acquire(nil)
partSize := uploadParts[i]
offset := lastPartEnd
lastPartEnd = partSize + offset
go func(f io.ReaderAt, offset int64, partNum int, partSize int64) {
defer func() {
bkLimit.Release(nil)
wg.Done()
}()
select {
case <-partUpCtx.Done():
return
default:
}
var buf []byte = nil
if p.UseBuffer {
buf, err = ioutil.ReadAll(io.NewSectionReader(f, offset, partSize))
if err != nil {
partUpErrLock.Lock()
partUpErr = err
partUpErrLock.Unlock()
elog.Errorf("[%s] uploadPartErr: partNum=%d, err=%s", xl.ReqId(), partNum, err)
cancel()
return
}
}
getBody := func() (io.Reader, int) {
if buf == nil {
return io.NewSectionReader(f, offset, partSize), int(partSize)
} else {
return bytes.NewReader(buf), len(buf)
}
}
ret, err := p.uploadPartWithRetry(partUpCtx, bucket, key, hasKey, uploadId, partNum, getBody)
if err != nil {
partUpErrLock.Lock()
partUpErr = err
partUpErrLock.Unlock()
elog.Errorf("[%s] uploadPartErr: partNum=%d, err=%s", xl.ReqId(), partNum, err)
cancel()
return
}
parts[partNum-1] = Part{partNum, ret.Etag}
if partNotify != nil {
partNotify(partNum, ret.Etag)
}
}(f, offset, i+1, partSize)
}
wg.Wait()
if partUpErr != nil {
err = p.deletePartsWithRetry(ctx, bucket, key, hasKey, uploadId)
if err != nil {
return err
}
return partUpErr
}
if mp == nil {
mp = &CompleteMultipart{}
}
mp.Parts = parts
return p.completePartsWithRetry(ctx, ret, bucket, key, hasKey, uploadId, mp)
}
func (p Uploader) makeUploadParts(fsize int64) []int64 {
return p.makeUploadPartsByPartSize(fsize, p.UploadPartSize)
}
func (p Uploader) makeUploadPartsByPartSize(fsize, partSize int64) []int64 {
partCnt := p.partNumber(fsize, partSize)
uploadParts := make([]int64, partCnt)
for i := 0; i < partCnt-1; i++ {
uploadParts[i] = partSize
}
uploadParts[partCnt-1] = fsize - (int64(partCnt)-1)*partSize
return uploadParts
}
func (p Uploader) checkUploadParts(fsize int64, uploadParts []int64) bool {
var partSize int64 = 0
for _, size := range uploadParts {
partSize += size
}
return fsize == partSize
}
func (p Uploader) partNumber(fsize, partSize int64) int {
return int((fsize + partSize - 1) / partSize)
}
func (p Uploader) adaptivePartSizeAndConcurrency(ctx context.Context, suggestedPartSizeOrigin int64) (int64, int) {
const (
PartSizeMin int64 = 1024 * 1024
PartSizeAligned int64 = 1024 * 1024
)
xl := xlog.FromContextSafe(ctx)
if suggestedPartSizeOrigin < PartSizeMin {
elog.Errorf("[%s] Suggested part size is too small: origin=%d", xl.ReqId(), suggestedPartSizeOrigin)
return p.UploadPartSize, p.Concurrency
}
suggestedPartSizeNotAligned := suggestedPartSizeOrigin
if suggestedPartSizeNotAligned < p.UploadPartSize {
suggestedPartSizeNotAligned *= int64(math.Ceil(float64(p.UploadPartSize) / float64(suggestedPartSizeNotAligned)))
}
suggestedPartSize := suggestedPartSizeNotAligned / PartSizeAligned * PartSizeAligned
suggestedConcurrency := int(math.Ceil(float64(int64(p.Concurrency)*p.UploadPartSize) / float64(suggestedPartSize)))
elog.Infof("[%s] Original suggested part size: %d, Suggested part size: %d, Suggested concurrency: %d", xl.ReqId(), suggestedPartSizeOrigin, suggestedPartSize, suggestedConcurrency)
return suggestedPartSize, suggestedConcurrency
}
func NewSectionReader(r io.Reader, n int64) *sectionReader {
return §ionReader{r, 0, n}
}
type sectionReader struct {
r io.Reader
off int64
limit int64
}
func (s *sectionReader) Read(p []byte) (n int, err error) {
if s.off >= s.limit {
return 0, io.EOF
}
if max := s.limit - s.off; int64(len(p)) > max {
p = p[0:max]
}
n, err = s.r.Read(p)
s.off += int64(n)
return
}
func (p Uploader) StreamUpload(ctx context.Context, ret interface{}, uptoken, key string, reader io.Reader, partNotify func(partIdx int, etag string)) error {
return p.streamUpload(ctx, ret, uptoken, key, true, reader, partNotify)
}
func (p Uploader) StreamUploadWithoutKey(ctx context.Context, ret interface{}, uptoken string, reader io.Reader, partNotify func(partIdx int, etag string)) error {
return p.streamUpload(ctx, ret, uptoken, "", false, reader, partNotify)
}
func (p Uploader) streamUpload(ctx context.Context, ret interface{}, uptoken, key string, hasKey bool, reader io.Reader, partNotify func(partIdx int, etag string)) error {
policy, err := ParseUpToken(uptoken)
if err != nil {
return err
}
bucket := strings.Split(policy.Scope, ":")[0]
p.Conn.Client = newUptokenClient(uptoken, p.Conn.Transport)
upHost := p.chooseUpHost(map[string]struct{}{})
uploadId, suggestedPartSize, err := p.initParts(ctx, upHost, bucket, key, hasKey)
if err != nil {
failHostName(upHost)
return err
}
succeedHostName(upHost)
partUpCtx, cancel := context.WithCancel(ctx)
defer cancel()
partSize := p.UploadPartSize
concurrency := p.Concurrency
if suggestedPartSize > 0 {
partSize, concurrency = p.adaptivePartSizeAndConcurrency(ctx, suggestedPartSize)
}
var parts []Part
var partsLock sync.Mutex
var wg sync.WaitGroup
type PartData struct {
Data []byte
PartNumber int
}
partChan := make(chan PartData)
errorChan := make(chan error, concurrency)
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-partUpCtx.Done():
return
case partData, ok := <-partChan:
if !ok {
return
}
getBody := func() (io.Reader, int) {
return bytes.NewReader(partData.Data), len(partData.Data)
}
ret, err := p.uploadPartWithRetry(partUpCtx, bucket, key, hasKey, uploadId, partData.PartNumber, getBody)
if err != nil {
if err != context.Canceled {
errorChan <- err
cancel()
}
return
}
partsLock.Lock()
parts = append(parts, Part{PartNumber: partData.PartNumber, Etag: ret.Etag})
partsLock.Unlock()
if partNotify != nil {
partNotify(partData.PartNumber, ret.Etag)
}
}
}
}()
}
for partNum := 1; ; partNum++ {
data, err := ioutil.ReadAll(io.LimitReader(reader, partSize))
if err != nil {
close(partChan)
return err
} else if len(data) == 0 {
break
} else {
partChan <- PartData{Data: data, PartNumber: partNum}
}
}
close(partChan)
wg.Wait()
close(errorChan)
partUpErr := <-errorChan
if partUpErr != nil {
err = p.deletePartsWithRetry(ctx, bucket, key, hasKey, uploadId)
if err != nil {
return err
}
return partUpErr
}
completeMultipart := CompleteMultipart{Parts: parts}
completeMultipart.Sort()
return p.completePartsWithRetry(ctx, ret, bucket, key, hasKey, uploadId, &completeMultipart)
}
func (p Uploader) uploadPartWithRetry(ctx context.Context, bucket, key string, hasKey bool, uploadId string, partNum int, getBody func() (io.Reader, int)) (ret UploadPartRet, err error) {
xl := xlog.NewWith(xlog.FromContextSafe(ctx).ReqId() + "." + fmt.Sprint(partNum))
tryTimes := uploadPartRetryTimes
failedUpHosts := make(map[string]struct{})
for {
upHost := p.chooseUpHost(failedUpHosts)
bodyReader, bodySize := getBody()
ret, err = p.uploadPart(ctx, upHost, bucket, key, hasKey, uploadId, partNum, bodyReader, bodySize)
if err == nil {
succeedHostName(upHost)
break
} else {
if err == context.Canceled {
break
}
code := httputil.DetectCode(err)
if code == 509 { // 因为流量受限失败,不减少重试次数
failedUpHosts[upHost] = struct{}{}
failHostName(upHost)
elog.Warnf("[%s] uploadPartRetryLater: partNum=%d, err=%s", xl.ReqId(), partNum, err)
time.Sleep(time.Second * time.Duration(rand.Intn(9)+1))
} else if tryTimes > 1 && (code == 406 || code/100 != 4) {
failedUpHosts[upHost] = struct{}{}
failHostName(upHost)
tryTimes--
elog.Warnf("[%s] uploadPartRetry: partNum=%d, err=%s", xl.ReqId(), partNum, err)
time.Sleep(time.Second * 3)
} else {
succeedHostName(upHost)
break
}
}
}
return
}
func (p Uploader) completePartsWithRetry(ctx context.Context, ret interface{}, bucket, key string, hasKey bool, uploadId string, mp *CompleteMultipart) (err error) {
xl := xlog.FromContextSafe(ctx)
failedUpHosts := make(map[string]struct{})
for i := 0; i < completePartsRetryTimes; i++ {
upHost := p.chooseUpHost(failedUpHosts)
err = p.completeParts(ctx, upHost, ret, bucket, key, hasKey, uploadId, mp)
if err == context.Canceled {
break
}
code := httputil.DetectCode(err)
if err == nil || code/100 == 4 || code == 612 || code == 614 || code == 579 {
succeedHostName(upHost)
if code == 612 || code == 614 {
elog.Warnf("[%s] completeParts: err=%s", xl.ReqId(), err)
err = nil
}
break
} else {
failedUpHosts[upHost] = struct{}{}
failHostName(upHost)
elog.Errorf("[%s] completeParts: err=%s, code=%d", xl.ReqId(), err, code)
time.Sleep(time.Second * 3)
}
}
return
}
func (p Uploader) deletePartsWithRetry(ctx context.Context, bucket, key string, hasKey bool, uploadId string) (err error) {
xl := xlog.FromContextSafe(ctx)
failedUpHosts := make(map[string]struct{})
for i := 0; i < deletePartsRetryTimes; i++ {
upHost := p.chooseUpHost(failedUpHosts)
err = p.deleteParts(ctx, upHost, bucket, key, hasKey, uploadId)
if err == context.Canceled {
break
}
code := httputil.DetectCode(err)
if err == nil || code/100 == 4 {
succeedHostName(upHost)
break
} else {
failedUpHosts[upHost] = struct{}{}
failHostName(upHost)
elog.Errorf("[%s] deleteParts: err=%s", xl.ReqId(), err)
time.Sleep(time.Second * 3)
}
}
return
}