-
Notifications
You must be signed in to change notification settings - Fork 28
/
s3_utils.go
451 lines (376 loc) · 12.5 KB
/
s3_utils.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
// Copyright 2022-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbft
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/couchbase/cbgt"
log "github.com/couchbase/clog"
"github.com/couchbase/tools-common/cloud/v5/objstore/objcli"
"github.com/couchbase/tools-common/cloud/v5/objstore/objcli/objaws"
"github.com/couchbase/tools-common/cloud/v5/objstore/objutil"
"github.com/couchbase/tools-common/types/iface"
"github.com/couchbase/tools-common/types/ratelimit"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"golang.org/x/time/rate"
)
func GetS3Client(region string) (objcli.Client, error) {
client := objaws.NewClient(objaws.ClientOptions{ServiceAPI: s3.New(s3.Options{
Region: region,
})})
return client, nil
}
func CheckIfRemotePathIsValid(remotePath string) bool {
return strings.HasPrefix(remotePath, "s3://")
}
// A sample remote path is of the form:
// pause:s3://<s3-bucket-name>/fts/<path to file>
// This function aims to extract the S3 bucket name and path after the bucket name.
func GetRemoteBucketAndPathHook(remotePath string) (string, string, error) {
split := strings.SplitN(remotePath, ":", 2)
if len(split) < 2 {
return "", "", fmt.Errorf("s3_utils: malformed path")
}
cloudURL, err := objutil.ParseCloudOrFileURL(split[1])
if err != nil {
return "", "", fmt.Errorf("s3_utils: error parsing remote path: %v", err)
}
return cloudURL.Bucket, cloudURL.Path, nil
}
type ProgressReader struct {
io.Reader
copyStats *CopyPartitionStats
}
func (pt *ProgressReader) Read(p []byte) (int, error) {
n, err := pt.Reader.Read(p)
pt.copyStats.CopyPartitionNumBytesReceived += int32(n)
return n, err
}
func decompress(src io.Reader, dst string, ctx context.Context, copyStats *CopyPartitionStats) error {
rateLimitedZr := newRateLimitedReader(ctx, src)
customReader := &ProgressReader{
Reader: rateLimitedZr,
copyStats: copyStats,
}
zr, err := gzip.NewReader(customReader)
if err != nil {
return err
}
tr := tar.NewReader(zr)
// uncompress each element
for {
select {
case <-ctx.Done():
return fmt.Errorf("s3_utils: context cancelled")
default:
}
header, err := tr.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
atomic.AddInt32(©Stats.TotCopyPartitionErrors, 1)
return fmt.Errorf("s3_utils: decompress err: %v", err)
}
if header == nil {
continue
}
target := dst + string(os.PathSeparator) + header.Name
switch header.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(target, os.FileMode(header.Mode)); err != nil {
return fmt.Errorf("s3_utils: error creating directory: %v", err)
}
case tar.TypeReg:
err := os.MkdirAll(filepath.Dir(target), 0766)
if err != nil {
return fmt.Errorf("s3_utils: error creating file path %s: %v", target, err)
}
fileToWrite, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
// manually close here after each file operation; defering would cause each file close
// to wait until all operations have completed.
defer fileToWrite.Close()
if err != nil {
return fmt.Errorf("s3_utils: error opening file %s: %v", target, err)
}
log.Printf("s3_utils: uncompressing file: %s", target)
startTime := time.Now()
_, err = io.Copy(fileToWrite, tr)
if err != nil {
return err
}
downloadDuration := time.Since(startTime)
atomic.AddInt32(©Stats.TotCopyPartitionTimeInMs,
int32(downloadDuration.Milliseconds()))
}
}
return nil
}
func downloadFromBucket(c objcli.Client, bucket, key, pindexPath string,
copyStats *CopyPartitionStats, ctx context.Context) error {
atomic.AddInt32(©Stats.TotCopyPartitionStart, 1)
object, err := c.GetObject(ctx, objcli.GetObjectOptions{
Bucket: bucket,
Key: key,
})
if err != nil {
atomic.AddInt32(©Stats.TotCopyPartitionErrors, 1)
return err
}
resetCopyStats(copyStats, *object.ObjectAttrs.Size)
// decompressing the tar.gz object and adding it to pindex path
err = decompress(object.Body, pindexPath, ctx, copyStats)
if err != nil {
atomic.AddInt32(©Stats.TotCopyPartitionErrors, 1)
return err
}
atomic.AddInt32(©Stats.TotCopyPartitionFinished, 1)
return nil
}
func DownloadMetadata(client objcli.Client, ctx context.Context, bucket, remotePath string) ([]byte, error) {
// Ref : https://stackoverflow.com/questions/46019484/buffer-implementing-io-writerat-in-go
buf := manager.NewWriteAtBuffer([]byte{})
options := objutil.DownloadOptions{
Client: client,
Bucket: bucket,
Key: remotePath,
Writer: newRateLimitedWriterAt(ctx, buf),
}
log.Printf("s3_utils: downloading metadata from path %s", remotePath)
err := objutil.Download(options)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func newRateLimitedReader(ctx context.Context, reader io.Reader) *ratelimit.RateLimitedReader {
rateLimit := ctx.Value("rateLimit").(uint64)
// Limit to 'rateLimit' bytes per second.
rateLimiter := rate.NewLimiter(rate.Every(time.Second/time.Duration(rateLimit)), int(rateLimit))
return ratelimit.NewRateLimitedReader(ctx, reader, rateLimiter)
}
func newRateLimitedReadAtSeeker(ctx context.Context, reader iface.ReadAtSeeker) *ratelimit.RateLimitedReadAtSeeker {
rateLimit := ctx.Value("rateLimit").(uint64)
// Limit to 'rateLimit' bytes per second.
rateLimiter := rate.NewLimiter(rate.Every(time.Second/time.Duration(rateLimit)), int(rateLimit))
return ratelimit.NewRateLimitedReadAtSeeker(ctx, reader, rateLimiter)
}
func newRateLimitedWriterAt(ctx context.Context, writer io.WriterAt) *ratelimit.RateLimitedWriterAt {
rateLimit := ctx.Value("rateLimit").(uint64)
// Limit to 'rateLimit' bytes per second.
rateLimiter := rate.NewLimiter(rate.Every(time.Second/time.Duration(rateLimit)), int(rateLimit))
return ratelimit.NewRateLimitedWriterAt(ctx, writer, rateLimiter)
}
func UploadMetadata(client objcli.Client, ctx context.Context, bucket,
remotePath string, data []byte) error {
// Upload a file without saving it - S3
// Ref: https://stackoverflow.com/questions/47621804/upload-object-to-aws-s3-without-creating-a-file-using-aws-sdk-go
reader := strings.NewReader(string(data))
options := objutil.UploadOptions{
Client: client,
Bucket: bucket,
Key: remotePath,
Body: newRateLimitedReadAtSeeker(ctx, reader),
Options: objutil.Options{Context: ctx},
}
log.Printf("s3_utils: uploading metadata to path %s", remotePath)
err := objutil.Upload(options)
if err != nil {
log.Errorf("s3_utils: error uploading index defs: %v", err)
}
return err
}
// Uploads the pindex directory to S3 for a hibernated index.
func uploadPIndexFiles(mgr *cbgt.Manager, client objcli.Client, remotePath,
pindexName, path string, ctx context.Context) {
_, pindexes := mgr.CurrentMaps()
pindex, exists := pindexes[pindexName]
if !exists {
log.Errorf("s3_utils: pindex %s not in mgr cache", pindexName)
return
}
destForwarder, ok := pindex.Dest.(*cbgt.DestForwarder)
if !ok {
log.Errorf("s3_utils: unable to find dest forwarder and upload pindex")
return
}
dest, ok := destForwarder.DestProvider.(*BleveDest)
if !ok {
log.Errorf("s3_utils: unable to find dest and upload pindex")
return
}
bucket, keyPrefix, err := GetRemoteBucketAndPathHook(remotePath)
if err != nil {
log.Errorf("s3_utils: error getting bucket and key from remote path: %v", err)
atomic.AddInt32(&dest.copyStats.TotCopyPartitionErrors, 1)
return
}
atomic.AddInt32(&dest.copyStats.TotCopyPartitionStart, 1)
log.Printf("s3_utils: uploading path %s", path)
err = compressUploadToBucket(client, bucket, path, keyPrefix, dest.copyStats, ctx)
if err != nil {
log.Errorf("s3_utils: error compressing and uploading to bucket: %v", err)
atomic.AddInt32(&dest.copyStats.TotCopyPartitionErrors, 1)
return
}
atomic.AddInt32(&dest.copyStats.TotCopyPartitionFinished, 1)
}
func compressUploadToBucket(c objcli.Client, bucket, pindexPath,
keyPrefix string, copyStats *CopyPartitionStats, ctx context.Context) error {
pindexName := cbgt.PIndexNameFromPath(pindexPath)
key := keyPrefix + "/" + pindexName + ".tar.gz"
mpuOpts := objutil.MPUploaderOptions{
Client: c,
Key: key,
Bucket: bucket,
Options: objutil.Options{Context: ctx},
}
mpUploader, err := objutil.NewMPUploader(mpuOpts)
if err != nil {
return fmt.Errorf("s3_utils: failed to create uploader: %v", err)
}
startTime := time.Now()
errs := compressUploadUtil(pindexPath, ctx, mpUploader)
if errs != nil && len(errs) > 0 {
atomic.AddInt32(©Stats.TotCopyPartitionErrors, int32(len(errs)))
log.Errorf("s3_utils: errors compressing path %s: ", pindexPath)
for _, err := range errs {
log.Errorf("%v", err)
}
}
atomic.AddInt32(©Stats.TotCopyPartitionTimeInMs, int32(time.Since(startTime).Milliseconds()))
return nil
}
// Resets the copy partition stats of the pindex to the expected
// values before transfer(upload/download).
func resetCopyStats(copyStats *CopyPartitionStats, totalObjSize int64) {
atomic.StoreInt32(©Stats.CopyPartitionNumBytesExpected, int32(totalObjSize))
// Resetting it to 0 prior to each pindex transfer.
atomic.StoreInt32(©Stats.CopyPartitionNumBytesReceived, 0)
atomic.StoreInt32(©Stats.TotCopyPartitionErrors, 0)
}
func compressUploadUtil(src string, ctx context.Context, mpUploader *objutil.MPUploader) []error {
// A pipe reader and writer are used to synchronously read compressed
// bytes to be uploaded as they are being generated, without needing to
// wait for compression to be completed for all the files.
r, w := io.Pipe()
zw := gzip.NewWriter(w)
tw := tar.NewWriter(zw)
var wg sync.WaitGroup
wg.Add(2)
var errMutex sync.RWMutex
var errs []error
appendToErrs := func(err error) {
errMutex.Lock()
errs = append(errs, err)
errMutex.Unlock()
}
// This goroutine reads bytes from the writer and performs a multipart upload to
// object storage, till it hits EOF.
go func() {
defer func() {
r.Close()
wg.Done()
}()
// 6 MB in bytes
uploadSliceSize := 6 * 1024 * 1024
for {
slice := make([]byte, uploadSliceSize)
pos, readErr := io.ReadFull(r, slice)
if readErr != nil && !errors.Is(readErr, io.EOF) && !errors.Is(readErr, io.ErrUnexpectedEOF) {
appendToErrs(fmt.Errorf("s3_utils: could not read: %v", readErr))
return
}
buf := bytes.NewReader(slice[:pos])
err := mpUploader.Upload(newRateLimitedReadAtSeeker(ctx, buf))
if err != nil {
appendToErrs(fmt.Errorf("s3_utils: error MP upload: %v", err))
return
}
// Have hit an EOF here if we didn't return readErr earlier.
if readErr != nil {
break
}
}
err := mpUploader.Commit()
if err != nil {
appendToErrs(err)
}
}()
// This goroutine walks through the pindex files and writes the compressed bytes
// into the tar writer, which is then received by the reader.
go func() {
defer func() {
if err := tw.Close(); err != nil {
appendToErrs(fmt.Errorf("s3_utils: error closing tar writer: %v", err))
}
if err := zw.Close(); err != nil {
appendToErrs(fmt.Errorf("s3_utils: error closing zip writer: %v", err))
}
if err := w.Close(); err != nil {
appendToErrs(fmt.Errorf("s3_utils: error closing pipe writer: %v", err))
}
wg.Done()
}()
// walk through every file in the pindex folder
err := filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {
if isCanceled(ctx) {
return ctx.Err()
}
// Don't upload pindex_meta file
if strings.HasSuffix(file, cbgt.PINDEX_META_FILENAME) {
return nil
}
// generate tar header
header, err := tar.FileInfoHeader(fi, file)
if err != nil {
return err
}
// Upload files without the pindex path.
if !fi.IsDir() {
header.Name = file[len(src)+1:]
} else {
header.Name = filepath.ToSlash(file)
}
// if not a dir, write file content
if !fi.IsDir() {
log.Printf("s3_utils: compressing file %s", file)
// write header
if err := tw.WriteHeader(header); err != nil {
return err
}
data, err := os.Open(file)
if err != nil {
return err
}
_, err = io.Copy(tw, data)
if err != nil {
return err
}
}
return nil
})
if err != nil {
appendToErrs(err)
}
}()
wg.Wait()
return errs
}