2019-11-07 20:05:39 +01:00
|
|
|
package gcsremote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2022-09-26 15:28:20 +02:00
|
|
|
"time"
|
2019-11-07 20:05:39 +01:00
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2022-09-26 15:28:20 +02:00
|
|
|
"github.com/googleapis/gax-go/v2"
|
2019-11-07 20:05:39 +01:00
|
|
|
"google.golang.org/api/iterator"
|
|
|
|
"google.golang.org/api/option"
|
2023-04-14 04:29:06 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/fscommon"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2019-11-07 20:05:39 +01: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 14:31:39 +02:00
|
|
|
//
|
|
|
|
// The returned fs must be stopped when no long needed with MustStop call.
|
2019-11-07 20:05:39 +01: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 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot create gcs client with credsFile %q: %w", fs.CredsFilePath, err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
client = c
|
|
|
|
} else {
|
|
|
|
c, err := storage.NewClient(ctx)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot create default gcs client: %w", err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
client = c
|
|
|
|
}
|
2022-09-26 15:28:20 +02:00
|
|
|
|
|
|
|
client.SetRetry(
|
|
|
|
storage.WithPolicy(storage.RetryAlways),
|
|
|
|
storage.WithBackoff(gax.Backoff{
|
|
|
|
Initial: time.Second,
|
|
|
|
Max: time.Minute * 3,
|
|
|
|
Multiplier: 3,
|
|
|
|
}))
|
2019-11-07 20:05:39 +01:00
|
|
|
fs.bkt = client.Bucket(fs.Bucket)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-09 14:31:39 +02:00
|
|
|
// MustStop stops fs.
|
|
|
|
func (fs *FS) MustStop() {
|
|
|
|
fs.bkt = nil
|
|
|
|
}
|
|
|
|
|
2019-11-07 20:05:39 +01: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 20:06:34 +01:00
|
|
|
// selectAttrs contains object attributes to select in ListParts.
|
|
|
|
var selectAttrs = []string{
|
|
|
|
"Name",
|
|
|
|
"Size",
|
|
|
|
}
|
|
|
|
|
2019-11-07 20:05:39 +01: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 20:06:34 +01:00
|
|
|
if err := q.SetAttrSelection(selectAttrs); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("error in SetAttrSelection: %w", err)
|
2019-11-19 20:06:34 +01:00
|
|
|
}
|
2019-11-07 20:05:39 +01: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 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("error when iterating objects at %q: %w", dir, err)
|
2019-11-07 20:05:39 +01: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 14:24:26 +01:00
|
|
|
if fscommon.IgnorePath(file) {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-07 20:05:39 +01: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 {
|
|
|
|
o := fs.object(p)
|
|
|
|
ctx := context.Background()
|
|
|
|
if err := o.Delete(ctx); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot delete %q at %s (remote path %q): %w", p.Path, fs, o.ObjectName(), err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot copy %q from %s to %s: %w", p.Path, src, fs, err)
|
2019-11-07 20:05:39 +01: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 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot open reader for %q at %s (remote path %q): %w", p.Path, fs, o.ObjectName(), err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
n, err := io.Copy(w, r)
|
|
|
|
if err1 := r.Close(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot download %q from at %s (remote path %q): %w", p.Path, fs, o.ObjectName(), err)
|
2019-11-07 20:05:39 +01: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 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot upload data to %q at %s (remote path %q): %w", p.Path, fs, o.ObjectName(), err)
|
2019-11-07 20:05:39 +01: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 14:24:26 +01: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 {
|
|
|
|
path := fs.Dir + filePath
|
|
|
|
o := fs.bkt.Object(path)
|
|
|
|
ctx := context.Background()
|
|
|
|
if err := o.Delete(ctx); err != nil {
|
|
|
|
if err != storage.ErrObjectNotExist {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot delete %q at %s (remote path %q): %w", filePath, fs, o.ObjectName(), err)
|
2020-01-09 14:24:26 +01: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 {
|
|
|
|
path := fs.Dir + filePath
|
|
|
|
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 21:58:18 +02: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 14:24:26 +01: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 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot close %q at %s (remote path %q): %w", filePath, fs, o.ObjectName(), err)
|
2020-01-09 14:24:26 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasFile returns ture if filePath exists at fs.
|
|
|
|
func (fs *FS) HasFile(filePath string) (bool, error) {
|
|
|
|
path := fs.Dir + filePath
|
|
|
|
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 21:58:18 +02: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 14:24:26 +01:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|