-
Notifications
You must be signed in to change notification settings - Fork 8
/
lo.go
348 lines (308 loc) · 8.23 KB
/
lo.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
package main
import (
"bytes"
"crypto/md5"
"encoding/base64"
"fmt"
"github.com/ncw/swift"
"hash"
"io"
"math"
"strings"
"sync"
"syscall"
"time"
)
// defined by openstack
const (
minPartSize = 5 * 1024 * 1024
maxPartSize = 5 * 1024 * 1024 * 1024
maxObjSize = 5 * 1024 * 1024 * 1024 * 1024
)
type part struct {
r io.ReadSeeker
len int64
b *bytes.Buffer
// read by xml encoder
PartNumber int
ETag string
// Used for checksum of checksums on completion
contentMd5 string
}
type largeObject struct {
c *swift.Connection
container string
objectName string
timestamp string
expire string
bufsz int64
buf *bytes.Buffer
ch chan *part
part int
closed bool
err error
wg sync.WaitGroup
md5OfParts hash.Hash
md5 hash.Hash
bp *bp
}
// NewUploader provides a writer to upload data as a segmented upload
//
// It will upload all the segments into a second container named <container>_segments.
// These segments will have names like large_file/1290206778.25/00000000,
// large_file/1290206778.25/00000001, etc.
//
// The main benefit for using a separate container is that the main container listings
// will not be polluted with all the segment names. The reason for using the segment
// name format of <name>/<timestamp>/<segment> is so that an upload of a new
// file with the same name won’t overwrite the contents of the first until the last
// moment when the manifest file is updated.
//
// swift will manage these segment files for you, deleting old segments on deletes
// and overwrites, etc. You can override this behavior with the --leave-segments
// option if desired; this is useful if you want to have multiple versions of
// the same large object available.
func NewUploader(c *swift.Connection, path string, concurrency int, partSize int64, expireAfter int64) (*largeObject, error) {
pathParts := strings.SplitN(path, "/", 2)
objectName := "upload"
if len(pathParts) > 1 {
objectName = pathParts[1]
}
lo := largeObject{
c: c,
container: pathParts[0],
objectName: objectName,
timestamp: fmt.Sprintf("%d", time.Now().UnixNano()),
expire: fmt.Sprintf("%d", expireAfter),
bufsz: max64(minPartSize, partSize),
ch: make(chan *part),
md5OfParts: md5.New(),
md5: md5.New(),
bp: newBufferPool(minPartSize),
}
for i := 0; i < max(concurrency, 1); i++ {
go lo.worker()
}
// Create segment container if it doesn't already exist
err := c.ContainerCreate(lo.container+"_segments", nil)
if err != nil {
return nil, err
}
return &lo, nil
}
func (lo *largeObject) Write(b []byte) (int, error) {
if lo.closed {
lo.abort()
return 0, syscall.EINVAL
}
if lo.err != nil {
lo.abort()
return 0, lo.err
}
if lo.buf == nil {
lo.buf = <-lo.bp.get
lo.buf.Reset()
}
n, err := lo.buf.Write(b)
if err != nil {
lo.abort()
return n, err
}
if int64(lo.buf.Len()) >= lo.bufsz {
lo.flush()
}
return n, nil
}
func (lo *largeObject) flush() {
lo.wg.Add(1)
lo.part++
b := *lo.buf
part := &part{bytes.NewReader(b.Bytes()), int64(b.Len()), lo.buf, lo.part, "", ""}
var err error
part.contentMd5, part.ETag, err = lo.md5Content(part.r)
if err != nil {
lo.err = err
}
lo.ch <- part
lo.buf = nil
// double buffer size every 1000 parts to
// avoid exceeding the 10000-part AWS limit
// while still reaching the 5 Terabyte max object size
if lo.part%1000 == 0 {
lo.bufsz = min64(lo.bufsz*2, maxPartSize)
}
}
func (lo *largeObject) worker() {
for part := range lo.ch {
lo.retryPutPart(part)
}
}
// Calls putPart up to nTry times to recover from transient errors.
func (lo *largeObject) retryPutPart(part *part) {
defer lo.wg.Done()
var err error
for i := 0; i < 3; i++ {
time.Sleep(time.Duration(math.Exp2(float64(i))) * 100 * time.Millisecond) // exponential back-off
err = lo.putPart(part)
if err == nil {
lo.bp.give <- part.b
return
}
debugf("Error on attempt %d: Retrying part: %v, Error: %s", i, part, err)
}
lo.err = err
}
// uploads a part, checking the etag against the calculated value
func (lo *largeObject) putPart(part *part) error {
container := lo.container + "_segments"
objectName := lo.objectName + "/" + lo.timestamp + "/" + fmt.Sprintf("%d", part.PartNumber)
debug("putPart(", container, objectName, part.len, fmt.Sprintf("%x", part.contentMd5), part.ETag, ")")
if _, err := part.r.Seek(0, 0); err != nil { // move back to beginning, if retrying
return err
}
headers, err := lo.c.ObjectPut(container, objectName, part.r, true, "", "", nil)
if err != nil {
return err
}
s := headers["Etag"]
if part.ETag != s {
return fmt.Errorf("Response etag does not match. Remote:%s Calculated:%s", s, part.ETag)
}
return nil
}
func (lo *largeObject) Close() (err error) {
if lo.closed {
lo.abort()
return syscall.EINVAL
}
if lo.buf != nil {
buf := *lo.buf
if buf.Len() > 0 {
lo.flush()
}
}
lo.wg.Wait()
close(lo.ch)
lo.closed = true
lo.bp.quit <- true
if lo.part == 0 {
lo.abort()
return fmt.Errorf("0 bytes written")
}
if lo.err != nil {
lo.abort()
return lo.err
}
// Complete Multipart upload
debug("completeMultipart(", lo.container, lo.objectName, "X-Object-Manifest: ", lo.container+"_segments/"+lo.objectName+"/"+lo.timestamp, ")")
reqHeaders := map[string]string{"X-Object-Manifest": lo.container + "_segments/" + lo.objectName + "/" + lo.timestamp}
if lo.expire != "0" {
reqHeaders["X-Delete-After"] = lo.expire
}
var headers swift.Headers
for i := 0; i < 3; i++ { //NTry
headers, err = lo.c.ObjectPut(lo.container, lo.objectName, strings.NewReader(""), true, "", "", reqHeaders)
if err == nil {
break
}
}
if err != nil {
lo.abort()
return err
}
debugf("completeMultipart() Response: %#v", headers)
// Check md5 hash of concatenated part md5 hashes against ETag
/* Broken right now
_, hdrs, err := lo.c.Object(lo.container, lo.objectName)
if err != nil {
return err
}
calculatedMd5ofParts := fmt.Sprintf("%x", lo.md5OfParts.Sum(nil))
remoteMd5ofParts := hdrs["Etag"]
remoteMd5ofParts = remoteMd5ofParts[1 : len(remoteMd5ofParts)-1] // includes quote chars for some reason
if calculatedMd5ofParts != remoteMd5ofParts {
if err != nil {
return err
}
return fmt.Errorf("MD5 hash of part hashes comparison failed. Hash from multipart complete header: %s."+
" Calculated multipart hash: %s.", remoteMd5ofParts, calculatedMd5ofParts)
}
*/
if false { //Md5Check
for i := 0; i < 3; i++ { //NTry
if err = lo.putMd5(); err == nil {
break
}
}
return
}
return
}
// Try to abort multipart upload. Do not error on failure.
func (lo *largeObject) abort() {
debug("abort()")
objects, err := lo.c.ObjectNamesAll(lo.container+"_segments", nil)
if err != nil {
fmt.Printf("Error aborting multipart upload: %v\n", err)
return
}
for _, object := range objects {
if strings.HasPrefix(object, lo.objectName+"/"+lo.timestamp+"/") {
lo.c.ObjectDelete(lo.container+"_segments", object)
if err != nil {
fmt.Printf("Error aborting multipart upload: %v\n", err)
}
}
}
return
}
// Md5 functions
func (lo *largeObject) md5Content(r io.ReadSeeker) (string, string, error) {
h := md5.New()
mw := io.MultiWriter(h, lo.md5)
if _, err := io.Copy(mw, r); err != nil {
return "", "", err
}
sum := h.Sum(nil)
hexSum := fmt.Sprintf("%x", sum)
// add to checksum of all parts for verification on upload completion
if _, err := lo.md5OfParts.Write(sum); err != nil {
return "", "", err
}
return base64.StdEncoding.EncodeToString(sum), hexSum, nil
}
// Put md5 file in .md5 subdirectory of bucket where the file is stored
// e.g. the md5 for https://mybucket.s3.amazonaws.com/gof3r will be stored in
// https://mybucket.s3.amazonaws.com/gof3r.md5
func (lo *largeObject) putMd5() (err error) {
calcMd5 := fmt.Sprintf("%x", lo.md5.Sum(nil))
md5Reader := strings.NewReader(calcMd5)
debug("putMd5()", calcMd5, lo.container+"/"+lo.objectName+".md5")
_, err = lo.c.ObjectPut(lo.container, lo.objectName+".md5", md5Reader, true, "", "", nil)
return
}
// Min and Max functions
func min64(a, b int64) int64 {
if a < b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max64(a, b int64) int64 {
if a > b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}