2019-11-07 19:05:39 +00:00
|
|
|
package gcsremote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-10 12:13:23 +00:00
|
|
|
"errors"
|
2019-11-07 19:05:39 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-12-04 08:21:29 +00:00
|
|
|
"path"
|
2019-11-07 19:05:39 +00:00
|
|
|
"strings"
|
2022-09-26 13:28:20 +00:00
|
|
|
"time"
|
2019-11-07 19:05:39 +00:00
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2022-09-26 13:28:20 +00:00
|
|
|
"github.com/googleapis/gax-go/v2"
|
2019-11-07 19:05:39 +00:00
|
|
|
"google.golang.org/api/iterator"
|
|
|
|
"google.golang.org/api/option"
|
2023-04-14 02:29:06 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/fscommon"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2019-11-07 19:05:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FS represents filesystem for backups in GCS.
|
|
|
|
//
|
|
|
|
// Init must be called before calling other FS methods.
|
|
|
|
type FS struct {
|
|
|
|
// Path to GCP credentials file.
|
|
|
|
//
|
|
|
|
// Default credentials are used if empty.
|
|
|
|
CredsFilePath string
|
|
|
|
|
|
|
|
// GCS bucket to use.
|
|
|
|
Bucket string
|
|
|
|
|
|
|
|
// Directory in the bucket to write to.
|
|
|
|
Dir string
|
|
|
|
|
|
|
|
bkt *storage.BucketHandle
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes fs.
|
2020-10-09 12:31:39 +00:00
|
|
|
//
|
|
|
|
// The returned fs must be stopped when no long needed with MustStop call.
|
2019-11-07 19:05:39 +00:00
|
|
|
func (fs *FS) Init() error {
|
|
|
|
if fs.bkt != nil {
|
|
|
|
logger.Panicf("BUG: fs.Init has been already called")
|
|
|
|
}
|
|
|
|
for strings.HasPrefix(fs.Dir, "/") {
|
|
|
|
fs.Dir = fs.Dir[1:]
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(fs.Dir, "/") {
|
|
|
|
fs.Dir += "/"
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
var client *storage.Client
|
|
|
|
if len(fs.CredsFilePath) > 0 {
|
|
|
|
creds := option.WithCredentialsFile(fs.CredsFilePath)
|
|
|
|
c, err := storage.NewClient(ctx, creds)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot create gcs client with credsFile %q: %w", fs.CredsFilePath, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
client = c
|
|
|
|
} else {
|
|
|
|
c, err := storage.NewClient(ctx)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot create default gcs client: %w", err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
client = c
|
|
|
|
}
|
2022-09-26 13:28:20 +00:00
|
|
|
|
|
|
|
client.SetRetry(
|
|
|
|
storage.WithPolicy(storage.RetryAlways),
|
|
|
|
storage.WithBackoff(gax.Backoff{
|
|
|
|
Initial: time.Second,
|
|
|
|
Max: time.Minute * 3,
|
|
|
|
Multiplier: 3,
|
|
|
|
}))
|
2019-11-07 19:05:39 +00:00
|
|
|
fs.bkt = client.Bucket(fs.Bucket)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-09 12:31:39 +00:00
|
|
|
// MustStop stops fs.
|
|
|
|
func (fs *FS) MustStop() {
|
|
|
|
fs.bkt = nil
|
|
|
|
}
|
|
|
|
|
2019-11-07 19:05:39 +00:00
|
|
|
// String returns human-readable description for fs.
|
|
|
|
func (fs *FS) String() string {
|
|
|
|
return fmt.Sprintf("GCS{bucket: %q, dir: %q}", fs.Bucket, fs.Dir)
|
|
|
|
}
|
|
|
|
|
2019-11-19 19:06:34 +00:00
|
|
|
// selectAttrs contains object attributes to select in ListParts.
|
|
|
|
var selectAttrs = []string{
|
|
|
|
"Name",
|
|
|
|
"Size",
|
|
|
|
}
|
|
|
|
|
2019-11-07 19:05:39 +00:00
|
|
|
// ListParts returns all the parts for fs.
|
|
|
|
func (fs *FS) ListParts() ([]common.Part, error) {
|
|
|
|
dir := fs.Dir
|
|
|
|
ctx := context.Background()
|
|
|
|
q := &storage.Query{
|
|
|
|
Prefix: dir,
|
|
|
|
}
|
2019-11-19 19:06:34 +00:00
|
|
|
if err := q.SetAttrSelection(selectAttrs); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("error in SetAttrSelection: %w", err)
|
2019-11-19 19:06:34 +00:00
|
|
|
}
|
2019-11-07 19:05:39 +00:00
|
|
|
it := fs.bkt.Objects(ctx, q)
|
|
|
|
var parts []common.Part
|
|
|
|
for {
|
|
|
|
attr, err := it.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
return parts, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("error when iterating objects at %q: %w", dir, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
file := attr.Name
|
|
|
|
if !strings.HasPrefix(file, dir) {
|
|
|
|
return nil, fmt.Errorf("unexpected prefix for gcs key %q; want %q", file, dir)
|
|
|
|
}
|
2020-01-09 13:24:26 +00:00
|
|
|
if fscommon.IgnorePath(file) {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-07 19:05:39 +00:00
|
|
|
var p common.Part
|
|
|
|
if !p.ParseFromRemotePath(file[len(dir):]) {
|
|
|
|
logger.Infof("skipping unknown object %q", file)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.ActualSize = uint64(attr.Size)
|
|
|
|
parts = append(parts, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePart deletes part p from fs.
|
|
|
|
func (fs *FS) DeletePart(p common.Part) error {
|
2023-10-10 12:13:23 +00:00
|
|
|
path := p.RemotePath(fs.Dir)
|
|
|
|
return fs.delete(path)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveEmptyDirs recursively removes empty dirs in fs.
|
|
|
|
func (fs *FS) RemoveEmptyDirs() error {
|
|
|
|
// GCS has no directories, so nothing to remove.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyPart copies p from srcFS to fs.
|
|
|
|
func (fs *FS) CopyPart(srcFS common.OriginFS, p common.Part) error {
|
|
|
|
src, ok := srcFS.(*FS)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("cannot perform server-side copying from %s to %s: both of them must be GCS", srcFS, fs)
|
|
|
|
}
|
|
|
|
srcObj := src.object(p)
|
|
|
|
dstObj := fs.object(p)
|
|
|
|
|
|
|
|
copier := dstObj.CopierFrom(srcObj)
|
|
|
|
ctx := context.Background()
|
|
|
|
attr, err := copier.Run(ctx)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot copy %q from %s to %s: %w", p.Path, src, fs, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
if uint64(attr.Size) != p.Size {
|
|
|
|
return fmt.Errorf("unexpected %q size after copying from %s to %s; got %d bytes; want %d bytes", p.Path, src, fs, attr.Size, p.Size)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadPart downloads part p from fs to w.
|
|
|
|
func (fs *FS) DownloadPart(p common.Part, w io.Writer) error {
|
|
|
|
o := fs.object(p)
|
|
|
|
ctx := context.Background()
|
|
|
|
r, err := o.NewReader(ctx)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot open reader for %q at %s (remote path %q): %w", p.Path, fs, o.ObjectName(), err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
n, err := io.Copy(w, r)
|
|
|
|
if err1 := r.Close(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot download %q from at %s (remote path %q): %w", p.Path, fs, o.ObjectName(), err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
if uint64(n) != p.Size {
|
|
|
|
return fmt.Errorf("wrong data size downloaded from %q at %s; got %d bytes; want %d bytes", p.Path, fs, n, p.Size)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadPart uploads part p from r to fs.
|
|
|
|
func (fs *FS) UploadPart(p common.Part, r io.Reader) error {
|
|
|
|
o := fs.object(p)
|
|
|
|
ctx := context.Background()
|
|
|
|
w := o.NewWriter(ctx)
|
|
|
|
n, err := io.Copy(w, r)
|
|
|
|
if err1 := w.Close(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot upload data to %q at %s (remote path %q): %w", p.Path, fs, o.ObjectName(), err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
if uint64(n) != p.Size {
|
|
|
|
return fmt.Errorf("wrong data size uploaded to %q at %s; got %d bytes; want %d bytes", p.Path, fs, n, p.Size)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) object(p common.Part) *storage.ObjectHandle {
|
|
|
|
path := p.RemotePath(fs.Dir)
|
|
|
|
return fs.bkt.Object(path)
|
|
|
|
}
|
2020-01-09 13:24:26 +00:00
|
|
|
|
|
|
|
// DeleteFile deletes filePath at fs if it exists.
|
|
|
|
//
|
|
|
|
// The function does nothing if the filePath doesn't exists.
|
|
|
|
func (fs *FS) DeleteFile(filePath string) error {
|
2023-12-04 08:21:29 +00:00
|
|
|
path := path.Join(fs.Dir, filePath)
|
2023-10-10 12:13:23 +00:00
|
|
|
return fs.delete(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) delete(path string) error {
|
|
|
|
if *common.DeleteAllObjectVersions {
|
|
|
|
return fs.deleteObjectWithGenerations(path)
|
|
|
|
}
|
|
|
|
return fs.deleteObject(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteObjectWithGenerations deletes object at path and all its generations.
|
|
|
|
func (fs *FS) deleteObjectWithGenerations(path string) error {
|
|
|
|
it := fs.bkt.Objects(context.Background(), &storage.Query{
|
|
|
|
Versions: true,
|
|
|
|
Prefix: path,
|
|
|
|
})
|
|
|
|
ctx := context.Background()
|
|
|
|
for {
|
|
|
|
attrs, err := it.Next()
|
|
|
|
if errors.Is(err, iterator.Done) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot read %q at %s: %w", path, fs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fs.bkt.Object(path).Generation(attrs.Generation).Delete(ctx); err != nil {
|
|
|
|
if !errors.Is(err, storage.ErrObjectNotExist) {
|
|
|
|
return fmt.Errorf("cannot delete %q at %s: %w", path, fs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteObject deletes object at path.
|
|
|
|
// It does not specify a Generation, so it will delete the latest generation of the object.
|
|
|
|
func (fs *FS) deleteObject(path string) error {
|
2020-01-09 13:24:26 +00:00
|
|
|
o := fs.bkt.Object(path)
|
|
|
|
ctx := context.Background()
|
|
|
|
if err := o.Delete(ctx); err != nil {
|
2023-10-10 12:13:23 +00:00
|
|
|
if !errors.Is(err, storage.ErrObjectNotExist) {
|
|
|
|
return fmt.Errorf("cannot delete %q at %s: %w", o.ObjectName(), fs, err)
|
2020-01-09 13:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-10 12:13:23 +00:00
|
|
|
|
2020-01-09 13:24:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFile creates filePath at fs and puts data into it.
|
|
|
|
//
|
|
|
|
// The file is overwritten if it exists.
|
|
|
|
func (fs *FS) CreateFile(filePath string, data []byte) error {
|
2023-12-04 08:21:29 +00:00
|
|
|
path := path.Join(fs.Dir, filePath)
|
2020-01-09 13:24:26 +00:00
|
|
|
o := fs.bkt.Object(path)
|
|
|
|
ctx := context.Background()
|
|
|
|
w := o.NewWriter(ctx)
|
|
|
|
n, err := w.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
_ = w.Close()
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot upload %d bytes to %q at %s (remote path %q): %w", len(data), filePath, fs, o.ObjectName(), err)
|
2020-01-09 13:24:26 +00:00
|
|
|
}
|
|
|
|
if n != len(data) {
|
|
|
|
_ = w.Close()
|
|
|
|
return fmt.Errorf("wrong data size uploaded to %q at %s (remote path %q); got %d bytes; want %d bytes", filePath, fs, o.ObjectName(), n, len(data))
|
|
|
|
}
|
|
|
|
if err := w.Close(); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot close %q at %s (remote path %q): %w", filePath, fs, o.ObjectName(), err)
|
2020-01-09 13:24:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasFile returns ture if filePath exists at fs.
|
|
|
|
func (fs *FS) HasFile(filePath string) (bool, error) {
|
2023-12-04 08:21:29 +00:00
|
|
|
path := path.Join(fs.Dir, filePath)
|
2020-01-09 13:24:26 +00:00
|
|
|
o := fs.bkt.Object(path)
|
|
|
|
ctx := context.Background()
|
|
|
|
_, err := o.Attrs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if err == storage.ErrObjectNotExist {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-06-30 19:58:18 +00:00
|
|
|
return false, fmt.Errorf("unexpected error when obtaining attributes for %q at %s (remote path %q): %w", filePath, fs, o.ObjectName(), err)
|
2020-01-09 13:24:26 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2023-08-14 00:17:12 +00:00
|
|
|
|
|
|
|
// ReadFile returns the content of filePath at fs.
|
|
|
|
func (fs *FS) ReadFile(filePath string) ([]byte, error) {
|
2023-12-04 08:21:29 +00:00
|
|
|
path := path.Join(fs.Dir, filePath)
|
|
|
|
o := fs.bkt.Object(path)
|
2023-08-14 00:17:12 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
r, err := o.NewReader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot read %q at %s (remote path %q): %w", filePath, fs, o.ObjectName(), err)
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
return io.ReadAll(r)
|
|
|
|
}
|