forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcs.go
432 lines (389 loc) · 12.9 KB
/
gcs.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
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package storage
import (
"bytes"
"context"
"io"
"os"
"path"
"strings"
"cloud.google.com/go/storage"
"github.com/pingcap/errors"
backuppb "github.com/pingcap/kvproto/pkg/brpb"
"github.com/pingcap/log"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/spf13/pflag"
"go.uber.org/zap"
"golang.org/x/oauth2/google"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
const (
gcsEndpointOption = "gcs.endpoint"
gcsStorageClassOption = "gcs.storage-class"
gcsPredefinedACL = "gcs.predefined-acl"
gcsCredentialsFile = "gcs.credentials-file"
)
// GCSBackendOptions are options for configuration the GCS storage.
type GCSBackendOptions struct {
Endpoint string `json:"endpoint" toml:"endpoint"`
StorageClass string `json:"storage-class" toml:"storage-class"`
PredefinedACL string `json:"predefined-acl" toml:"predefined-acl"`
CredentialsFile string `json:"credentials-file" toml:"credentials-file"`
}
func (options *GCSBackendOptions) apply(gcs *backuppb.GCS) error {
gcs.Endpoint = options.Endpoint
gcs.StorageClass = options.StorageClass
gcs.PredefinedAcl = options.PredefinedACL
if options.CredentialsFile != "" {
b, err := os.ReadFile(options.CredentialsFile)
if err != nil {
return errors.Trace(err)
}
gcs.CredentialsBlob = string(b)
}
return nil
}
func defineGCSFlags(flags *pflag.FlagSet) {
// TODO: remove experimental tag if it's stable
flags.String(gcsEndpointOption, "", "(experimental) Set the GCS endpoint URL")
flags.String(gcsStorageClassOption, "", "(experimental) Specify the GCS storage class for objects")
flags.String(gcsPredefinedACL, "", "(experimental) Specify the GCS predefined acl for objects")
flags.String(gcsCredentialsFile, "", "(experimental) Set the GCS credentials file path")
}
func hiddenGCSFlags(flags *pflag.FlagSet) {
_ = flags.MarkHidden(gcsEndpointOption)
_ = flags.MarkHidden(gcsStorageClassOption)
_ = flags.MarkHidden(gcsPredefinedACL)
_ = flags.MarkHidden(gcsCredentialsFile)
}
func (options *GCSBackendOptions) parseFromFlags(flags *pflag.FlagSet) error {
var err error
options.Endpoint, err = flags.GetString(gcsEndpointOption)
if err != nil {
return errors.Trace(err)
}
options.StorageClass, err = flags.GetString(gcsStorageClassOption)
if err != nil {
return errors.Trace(err)
}
options.PredefinedACL, err = flags.GetString(gcsPredefinedACL)
if err != nil {
return errors.Trace(err)
}
options.CredentialsFile, err = flags.GetString(gcsCredentialsFile)
if err != nil {
return errors.Trace(err)
}
return nil
}
// GCSStorage defines some standard operations for BR/Lightning on the GCS storage.
// It implements the `ExternalStorage` interface.
type GCSStorage struct {
gcs *backuppb.GCS
bucket *storage.BucketHandle
}
// GetBucketHandle gets the handle to the GCS API on the bucket.
func (s *GCSStorage) GetBucketHandle() *storage.BucketHandle {
return s.bucket
}
// GetOptions gets the external storage operations for the GCS.
func (s *GCSStorage) GetOptions() *backuppb.GCS {
return s.gcs
}
// DeleteFile delete the file in storage
func (s *GCSStorage) DeleteFile(ctx context.Context, name string) error {
object := s.objectName(name)
err := s.bucket.Object(object).Delete(ctx)
return errors.Trace(err)
}
func (s *GCSStorage) objectName(name string) string {
return path.Join(s.gcs.Prefix, name)
}
// WriteFile writes data to a file to storage.
func (s *GCSStorage) WriteFile(ctx context.Context, name string, data []byte) error {
object := s.objectName(name)
wc := s.bucket.Object(object).NewWriter(ctx)
wc.StorageClass = s.gcs.StorageClass
wc.PredefinedACL = s.gcs.PredefinedAcl
_, err := wc.Write(data)
if err != nil {
return errors.Trace(err)
}
return wc.Close()
}
// ReadFile reads the file from the storage and returns the contents.
func (s *GCSStorage) ReadFile(ctx context.Context, name string) ([]byte, error) {
object := s.objectName(name)
rc, err := s.bucket.Object(object).NewReader(ctx)
if err != nil {
return nil, errors.Annotatef(err,
"failed to read gcs file, file info: input.bucket='%s', input.key='%s'",
s.gcs.Bucket, object)
}
defer rc.Close()
size := rc.Attrs.Size
var b []byte
if size < 0 {
// happened when using fake-gcs-server in integration test
b, err = io.ReadAll(rc)
} else {
b = make([]byte, size)
_, err = io.ReadFull(rc, b)
}
return b, errors.Trace(err)
}
// FileExists return true if file exists.
func (s *GCSStorage) FileExists(ctx context.Context, name string) (bool, error) {
object := s.objectName(name)
_, err := s.bucket.Object(object).Attrs(ctx)
if err != nil {
if errors.Cause(err) == storage.ErrObjectNotExist { // nolint:errorlint
return false, nil
}
return false, errors.Trace(err)
}
return true, nil
}
// Open a Reader by file path.
func (s *GCSStorage) Open(ctx context.Context, path string) (ExternalFileReader, error) {
object := s.objectName(path)
handle := s.bucket.Object(object)
attrs, err := handle.Attrs(ctx)
if err != nil {
if errors.Cause(err) == storage.ErrObjectNotExist { // nolint:errorlint
return nil, errors.Annotatef(err,
"the object doesn't exist, file info: input.bucket='%s', input.key='%s'",
s.gcs.Bucket, path)
}
return nil, errors.Annotatef(err,
"failed to get gcs file attribute, file info: input.bucket='%s', input.key='%s'",
s.gcs.Bucket, path)
}
return &gcsObjectReader{
storage: s,
name: path,
objHandle: handle,
reader: nil, // lazy create
ctx: ctx,
totalSize: attrs.Size,
}, nil
}
// WalkDir traverse all the files in a dir.
//
// fn is the function called for each regular file visited by WalkDir.
// The first argument is the file path that can be used in `Open`
// function; the second argument is the size in byte of the file determined
// by path.
func (s *GCSStorage) WalkDir(ctx context.Context, opt *WalkOption, fn func(string, int64) error) error {
if opt == nil {
opt = &WalkOption{}
}
prefix := path.Join(s.gcs.Prefix, opt.SubDir)
if len(prefix) > 0 && !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
if len(opt.ObjPrefix) != 0 {
prefix += opt.ObjPrefix
}
query := &storage.Query{Prefix: prefix}
// only need each object's name and size
err := query.SetAttrSelection([]string{"Name", "Size"})
if err != nil {
return errors.Trace(err)
}
iter := s.bucket.Objects(ctx, query)
for {
attrs, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return errors.Trace(err)
}
// when walk on specify directory, the result include storage.Prefix,
// which can not be reuse in other API(Open/Read) directly.
// so we use TrimPrefix to filter Prefix for next Open/Read.
path := strings.TrimPrefix(attrs.Name, s.gcs.Prefix)
// trim the prefix '/' to ensure that the path returned is consistent with the local storage
path = strings.TrimPrefix(path, "/")
if err = fn(path, attrs.Size); err != nil {
return errors.Trace(err)
}
}
return nil
}
func (s *GCSStorage) URI() string {
return "gcs://" + s.gcs.Bucket + "/" + s.gcs.Prefix
}
// Create implements ExternalStorage interface.
func (s *GCSStorage) Create(ctx context.Context, name string, _ *WriterOption) (ExternalFileWriter, error) {
object := s.objectName(name)
wc := s.bucket.Object(object).NewWriter(ctx)
wc.StorageClass = s.gcs.StorageClass
wc.PredefinedACL = s.gcs.PredefinedAcl
return newFlushStorageWriter(wc, &emptyFlusher{}, wc), nil
}
// Rename file name from oldFileName to newFileName.
func (s *GCSStorage) Rename(ctx context.Context, oldFileName, newFileName string) error {
data, err := s.ReadFile(ctx, oldFileName)
if err != nil {
return errors.Trace(err)
}
err = s.WriteFile(ctx, newFileName, data)
if err != nil {
return errors.Trace(err)
}
return s.DeleteFile(ctx, oldFileName)
}
// NewGCSStorage creates a GCS external storage implementation.
func NewGCSStorage(ctx context.Context, gcs *backuppb.GCS, opts *ExternalStorageOptions) (*GCSStorage, error) {
var clientOps []option.ClientOption
if opts.NoCredentials {
clientOps = append(clientOps, option.WithoutAuthentication())
} else {
if gcs.CredentialsBlob == "" {
creds, err := google.FindDefaultCredentials(ctx, storage.ScopeReadWrite)
if err != nil {
return nil, errors.Annotatef(berrors.ErrStorageInvalidConfig, "%v Or you should provide '--gcs.credentials_file'", err)
}
if opts.SendCredentials {
if len(creds.JSON) <= 0 {
return nil, errors.Annotate(berrors.ErrStorageInvalidConfig,
"You should provide '--gcs.credentials_file' when '--send-credentials-to-tikv' is true")
}
gcs.CredentialsBlob = string(creds.JSON)
}
if creds != nil {
clientOps = append(clientOps, option.WithCredentials(creds))
}
} else {
clientOps = append(clientOps, option.WithCredentialsJSON([]byte(gcs.GetCredentialsBlob())))
}
}
if gcs.Endpoint != "" {
clientOps = append(clientOps, option.WithEndpoint(gcs.Endpoint))
}
if opts.HTTPClient != nil {
clientOps = append(clientOps, option.WithHTTPClient(opts.HTTPClient))
}
client, err := storage.NewClient(ctx, clientOps...)
if err != nil {
return nil, errors.Trace(err)
}
if !opts.SendCredentials {
// Clear the credentials if exists so that they will not be sent to TiKV
gcs.CredentialsBlob = ""
}
bucket := client.Bucket(gcs.Bucket)
// check whether it's a bug before #647, to solve case #2
// If the storage is set as gcs://bucket/prefix/,
// the backupmeta is written correctly to gcs://bucket/prefix/backupmeta,
// but the SSTs are written wrongly to gcs://bucket/prefix//*.sst (note the extra slash).
// see details about case 2 at https://github.com/pingcap/br/issues/675#issuecomment-753780742
sstInPrefix := hasSSTFiles(ctx, bucket, gcs.Prefix)
sstInPrefixSlash := hasSSTFiles(ctx, bucket, gcs.Prefix+"//")
if sstInPrefixSlash && !sstInPrefix {
// This is a old bug, but we must make it compatible.
// so we need find sst in slash directory
gcs.Prefix += "//"
}
return &GCSStorage{gcs: gcs, bucket: bucket}, nil
}
func hasSSTFiles(ctx context.Context, bucket *storage.BucketHandle, prefix string) bool {
query := storage.Query{Prefix: prefix}
_ = query.SetAttrSelection([]string{"Name"})
it := bucket.Objects(ctx, &query)
for {
attrs, err := it.Next()
if err == iterator.Done { // nolint:errorlint
break
}
if err != nil {
log.Warn("failed to list objects on gcs, will use default value for `prefix`", zap.Error(err))
break
}
if strings.HasSuffix(attrs.Name, ".sst") {
log.Info("sst file found in prefix slash", zap.String("file", attrs.Name))
return true
}
}
return false
}
// gcsObjectReader wrap storage.Reader and add the `Seek` method.
type gcsObjectReader struct {
storage *GCSStorage
name string
objHandle *storage.ObjectHandle
reader io.ReadCloser
pos int64
totalSize int64
// reader context used for implement `io.Seek`
// currently, lightning depends on package `xitongsys/parquet-go` to read parquet file and it needs `io.Seeker`
// See: https://github.com/xitongsys/parquet-go/blob/207a3cee75900b2b95213627409b7bac0f190bb3/source/source.go#L9-L10
ctx context.Context
}
// Read implement the io.Reader interface.
func (r *gcsObjectReader) Read(p []byte) (n int, err error) {
if r.reader == nil {
rc, err := r.objHandle.NewRangeReader(r.ctx, r.pos, -1)
if err != nil {
return 0, errors.Annotatef(err,
"failed to read gcs file, file info: input.bucket='%s', input.key='%s'",
r.storage.gcs.Bucket, r.name)
}
r.reader = rc
}
n, err = r.reader.Read(p)
r.pos += int64(n)
return n, err
}
// Close implement the io.Closer interface.
func (r *gcsObjectReader) Close() error {
if r.reader == nil {
return nil
}
return r.reader.Close()
}
// Seek implement the io.Seeker interface.
//
// Currently, tidb-lightning depends on this method to read parquet file for gcs storage.
func (r *gcsObjectReader) Seek(offset int64, whence int) (int64, error) {
var realOffset int64
switch whence {
case io.SeekStart:
realOffset = offset
case io.SeekCurrent:
realOffset = r.pos + offset
case io.SeekEnd:
if offset > 0 {
return 0, errors.Annotatef(berrors.ErrInvalidArgument, "Seek: offset '%v' should be negative.", offset)
}
realOffset = offset + r.totalSize
default:
return 0, errors.Annotatef(berrors.ErrStorageUnknown, "Seek: invalid whence '%d'", whence)
}
if realOffset < 0 {
return 0, errors.Annotatef(berrors.ErrInvalidArgument, "Seek: offset '%v' out of range. current pos is '%v'. total size is '%v'", offset, r.pos, r.totalSize)
}
if realOffset == r.pos {
return realOffset, nil
}
if r.reader != nil {
_ = r.reader.Close()
r.reader = nil
}
r.pos = realOffset
if realOffset >= r.totalSize {
r.reader = io.NopCloser(bytes.NewReader(nil))
return realOffset, nil
}
rc, err := r.objHandle.NewRangeReader(r.ctx, r.pos, -1)
if err != nil {
return 0, errors.Annotatef(err,
"failed to read gcs file, file info: input.bucket='%s', input.key='%s'",
r.storage.gcs.Bucket, r.name)
}
r.reader = rc
return realOffset, nil
}