forked from wal-g/wal-g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.go
427 lines (350 loc) · 11 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
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
package walg
import (
"archive/tar"
"fmt"
"io"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/s3/s3manager/s3manageriface"
"github.com/jackc/pgx"
"github.com/pierrec/lz4"
"github.com/pkg/errors"
)
// MAXRETRIES is the maximum number of retries for upload.
var MAXRETRIES = 7
// Given an S3 bucket name, attempt to determine its region
func findS3BucketRegion(bucket string, config *aws.Config) (string, error) {
input := s3.GetBucketLocationInput{
Bucket: aws.String(bucket),
}
sess, err := session.NewSession(config.WithRegion("us-east-1"))
if err != nil {
return "", err
}
output, err := s3.New(sess).GetBucketLocation(&input)
if err != nil {
return "", err
}
if output.LocationConstraint == nil {
// buckets in "US Standard", a.k.a. us-east-1, are returned as a nil region
return "us-east-1", nil
}
// all other regions are strings
return *output.LocationConstraint, nil
}
// Configure connects to S3 and creates an uploader. It makes sure
// that a valid session has started; if invalid, returns AWS error
// and `<nil>` values.
//
// Requires these environment variables to be set:
// WALE_S3_PREFIX
//
// Able to configure the upload part size in the S3 uploader.
func Configure() (*TarUploader, *Prefix, error) {
waleS3Prefix := os.Getenv("WALE_S3_PREFIX")
if waleS3Prefix == "" {
return nil, nil, &UnsetEnvVarError{names: []string{"WALE_S3_PREFIX"}}
}
u, err := url.Parse(waleS3Prefix)
if err != nil {
return nil, nil, errors.Wrapf(err, "Configure: failed to parse url '%s'", waleS3Prefix)
}
if u.Scheme == "" || u.Host == "" {
return nil, nil, fmt.Errorf("Missing url scheme=%q and/or host=%q", u.Scheme, u.Host)
}
bucket := u.Host
var server = ""
if len(u.Path) > 0 {
// TODO: Unchecked assertion: first char is '/'
server = u.Path[1:]
}
if len(server) > 0 && server[len(server)-1] == '/' {
// Allover the code this parameter is concatenated with '/'.
// TODO: Get rid of numerous string literals concatenated with this
server = server[:len(server)-1]
}
config := defaults.Get().Config
config.MaxRetries = &MAXRETRIES
if _, err := config.Credentials.Get(); err != nil {
return nil, nil, errors.Wrapf(err, "Configure: failed to get AWS credentials; please specify AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY")
}
if endpoint := os.Getenv("AWS_ENDPOINT"); endpoint != "" {
config.Endpoint = aws.String(endpoint)
}
s3ForcePathStyleStr := os.Getenv("AWS_S3_FORCE_PATH_STYLE")
if len(s3ForcePathStyleStr) > 0 {
s3ForcePathStyle, err := strconv.ParseBool(s3ForcePathStyleStr)
if err != nil {
return nil, nil, errors.Wrap(err, "Configure: failed parse AWS_S3_FORCE_PATH_STYLE")
}
config.S3ForcePathStyle = aws.Bool(s3ForcePathStyle)
}
region := os.Getenv("AWS_REGION")
if region == "" {
region, err = findS3BucketRegion(bucket, config)
if err != nil {
return nil, nil, errors.Wrapf(err, "Configure: AWS_REGION is not set and s3:GetBucketLocation failed")
}
}
config = config.WithRegion(region)
pre := &Prefix{
Bucket: aws.String(bucket),
Server: aws.String(server),
}
sess, err := session.NewSession(config)
if err != nil {
return nil, nil, errors.Wrap(err, "Configure: failed to create new session")
}
pre.Svc = s3.New(sess)
upload := NewTarUploader(pre.Svc, bucket, server, region)
var con = getMaxUploadConcurrency(10)
storageClass, ok := os.LookupEnv("WALG_S3_STORAGE_CLASS")
if ok {
upload.StorageClass = storageClass
}
serverSideEncryption, ok := os.LookupEnv("WALG_S3_SSE")
if ok {
upload.ServerSideEncryption = serverSideEncryption
}
sseKmsKeyId, ok := os.LookupEnv("WALG_S3_SSE_KMS_ID")
if ok {
upload.SSEKMSKeyId = sseKmsKeyId
}
// Only aws:kms implies sseKmsKeyId
if (serverSideEncryption == "aws:kms") == (sseKmsKeyId == "") {
return nil, nil, errors.New("Configure: WALG_S3_SSE_KMS_ID must be set iff using aws:kms encryption")
}
upload.Upl = CreateUploader(pre.Svc, 20*1024*1024, con) //default 10 concurrency streams at 20MB
return upload, pre, err
}
// CreateUploader returns an uploader with customizable concurrency
// and partsize.
func CreateUploader(svc s3iface.S3API, partsize, concurrency int) s3manageriface.UploaderAPI {
up := s3manager.NewUploaderWithClient(svc, func(u *s3manager.Uploader) {
u.PartSize = int64(partsize)
u.Concurrency = concurrency
})
return up
}
// Helper function to upload to S3. If an error occurs during upload, retries will
// occur in exponentially incremental seconds.
func (tu *TarUploader) upload(input *s3manager.UploadInput, path string) (err error) {
upl := tu.Upl
_, e := upl.Upload(input)
if e == nil {
tu.Success = true
return nil
}
if multierr, ok := e.(s3manager.MultiUploadFailure); ok {
log.Printf("upload: failed to upload '%s' with UploadID '%s'.", path, multierr.UploadID())
} else {
log.Printf("upload: failed to upload '%s': %s.", path, e.Error())
}
return e
}
// createUploadInput creates a s3manager.UploadInput for a TarUploader using
// the specified path and reader.
func (tu *TarUploader) createUploadInput(path string, reader io.Reader) *s3manager.UploadInput {
uploadInput := &s3manager.UploadInput{
Bucket: aws.String(tu.bucket),
Key: aws.String(path),
Body: reader,
StorageClass: aws.String(tu.StorageClass),
}
if tu.ServerSideEncryption != "" {
uploadInput.ServerSideEncryption = aws.String(tu.ServerSideEncryption)
if tu.SSEKMSKeyId != "" {
// Only aws:kms implies sseKmsKeyId, checked during validation
uploadInput.SSEKMSKeyId = aws.String(tu.SSEKMSKeyId)
}
}
return uploadInput
}
// StartUpload creates a lz4 writer and runs upload in the background once
// a compressed tar member is finished writing.
func (s *S3TarBall) StartUpload(name string, crypter Crypter) io.WriteCloser {
pr, pw := io.Pipe()
tupl := s.tu
path := tupl.server + "/basebackups_005/" + s.bkupName + "/tar_partitions/" + name
input := tupl.createUploadInput(path, pr)
fmt.Printf("Starting part %d ...\n", s.number)
tupl.wg.Add(1)
go func() {
defer tupl.wg.Done()
err := tupl.upload(input, path)
if re, ok := err.(Lz4Error); ok {
log.Printf("FATAL: could not upload '%s' due to compression error\n%+v\n", path, re)
}
if err != nil {
log.Printf("upload: could not upload '%s'\n", path)
log.Printf("FATAL%v\n", err)
}
}()
if crypter.IsUsed() {
wc, err := crypter.Encrypt(pw)
if err != nil {
log.Fatal("upload: encryption error ",err)
}
return &Lz4CascadeClose2{lz4.NewWriter(wc), wc, pw}
}
return &Lz4CascadeClose{lz4.NewWriter(pw), pw}
}
// UploadWal compresses a WAL file using LZ4 and uploads to S3. Returns
// the first error encountered and an empty string upon failure.
func (tu *TarUploader) UploadWal(path string, pre *Prefix, verify bool) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", errors.Wrapf(err, "UploadWal: failed to open file %s\n", path)
}
lz := &LzPipeWriter{
Input: f,
}
lz.Compress(&OpenPGPCrypter{})
p := sanitizePath(tu.server + "/wal_005/" + filepath.Base(path) + ".lz4")
reader := lz.Output
if verify {
reader = newMd5Reader(reader)
}
input := tu.createUploadInput(p, reader)
tu.wg.Add(1)
go func() {
defer tu.wg.Done()
err = tu.upload(input, path)
}()
tu.Finish()
fmt.Println("WAL PATH:", p)
if verify {
sum := reader.(*md5Reader).Sum()
a := &Archive{
Prefix: pre,
Archive: aws.String(p),
}
eTag, err := a.GetETag()
if err != nil {
log.Fatalf("Unable to verify WAL %s", err)
}
if eTag == nil {
log.Fatalf("Unable to verify WAL: nil ETag ")
}
trimETag := strings.Trim(*eTag, "\"")
if sum != trimETag {
log.Fatalf("WAL verification failed: md5 %s ETag %s", sum, trimETag)
}
fmt.Println("ETag ", trimETag)
}
return p, err
}
// HandleSentinel uploads the compressed tar file of `pg_control`. Will only be called
// after the rest of the backup is successfully uploaded to S3. Returns
// an error upon failure.
func (bundle *Bundle) HandleSentinel() error {
fileName := bundle.Sen.Info.Name()
info := bundle.Sen.Info
path := bundle.Sen.path
bundle.NewTarBall(false)
tarBall := bundle.Tb
tarBall.SetUp(&bundle.Crypter, "pg_control.tar.lz4")
tarWriter := tarBall.Tw()
hdr, err := tar.FileInfoHeader(info, fileName)
if err != nil {
return errors.Wrap(err, "HandleSentinel: failed to grab header info")
}
hdr.Name = strings.TrimPrefix(path, tarBall.Trim())
fmt.Println(hdr.Name)
err = tarWriter.WriteHeader(hdr)
if err != nil {
return errors.Wrap(err, "HandleSentinel: failed to write header")
}
if info.Mode().IsRegular() {
f, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "HandleSentinel: failed to open file %s\n", path)
}
lim := &io.LimitedReader{
R: f,
N: int64(hdr.Size),
}
_, err = io.Copy(tarWriter, lim)
if err != nil {
return errors.Wrap(err, "HandleSentinel: copy failed")
}
tarBall.AddSize(hdr.Size)
f.Close()
}
err = tarBall.CloseTar()
if err != nil {
return errors.Wrap(err, "HandleSentinel: failed to close tarball")
}
return nil
}
// HandleLabelFiles creates the `backup_label` and `tablespace_map` Files and uploads
// it to S3 by stopping the backup. Returns error upon failure.
func (bundle *Bundle) HandleLabelFiles(conn *pgx.Conn) (uint64, error) {
var lb string
var sc string
var lsnStr string
queryRunner, err := NewPgQueryRunner(conn)
if err != nil {
return 0, errors.Wrap(err, "HandleLabelFiles: Failed to build query runner.")
}
lb, sc, lsnStr, err = queryRunner.StopBackup()
if err != nil {
return 0, errors.Wrap(err, "HandleLabelFiles: failed to stop backup")
}
lsn, err := ParseLsn(lsnStr)
if err != nil {
return 0, errors.Wrap(err, "HandleLabelFiles: failed to parse finish LSN")
}
if queryRunner.Version < 90600 {
return lsn, nil
}
bundle.NewTarBall(false)
tarBall := bundle.Tb
tarBall.SetUp(&bundle.Crypter)
tarWriter := tarBall.Tw()
lhdr := &tar.Header{
Name: "backup_label",
Mode: int64(0600),
Size: int64(len(lb)),
Typeflag: tar.TypeReg,
}
err = tarWriter.WriteHeader(lhdr)
if err != nil {
return 0, errors.Wrap(err, "HandleLabelFiles: failed to write header")
}
_, err = io.Copy(tarWriter, strings.NewReader(lb))
if err != nil {
return 0, errors.Wrap(err, "HandleLabelFiles: copy failed")
}
fmt.Println(lhdr.Name)
shdr := &tar.Header{
Name: "tablespace_map",
Mode: int64(0600),
Size: int64(len(sc)),
Typeflag: tar.TypeReg,
}
err = tarWriter.WriteHeader(shdr)
if err != nil {
return 0, errors.Wrap(err, "HandleLabelFiles: failed to write header")
}
_, err = io.Copy(tarWriter, strings.NewReader(sc))
if err != nil {
return 0, errors.Wrap(err, "HandleLabelFiles: copy failed")
}
fmt.Println(shdr.Name)
err = tarBall.CloseTar()
if err != nil {
return 0, errors.Wrap(err, "HandleLabelFiles: failed to close tarball")
}
return lsn, nil
}