Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions cmd/pbm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"log"
"os"
"reflect"
"strings"
"time"

"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -62,31 +60,30 @@ func runConfig(

switch {
case len(c.set) > 0:
oldCfg, err := pbm.GetConfig(ctx)
if err != nil {
if !errors.Is(err, mongo.ErrNoDocuments) {
return nil, errors.Wrap(err, "unable to get current config")
}
oldCfg = &config.Config{}
}

var o confVals
rsnc := false
for k, v := range c.set {
err := config.SetConfigVar(ctx, conn, k, v)
if err != nil {
return nil, errors.Wrapf(err, "set %s", k)
}
o = append(o, confKV{k, v})
}

path := strings.Split(k, ".")
if !rsnc && len(path) > 0 && path[0] == "storage" {
rsnc = true
}
newCfg, err := pbm.GetConfig(ctx)
if err != nil {
return nil, errors.Wrap(err, "unable to get updated config")
}
if rsnc {
cid, err := pbm.SyncFromStorage(ctx, false)
if err != nil {
return nil, errors.Wrap(err, "resync")
}

if c.wait {
if err := waitForResyncWithTimeout(ctx, pbm, cid, c.waitTime); err != nil {
return nil, err
}
}
if err := resyncIfNeeded(ctx, pbm, oldCfg, newCfg, c); err != nil {
return nil, err
}
return o, nil
case len(c.key) > 0:
Expand Down Expand Up @@ -138,18 +135,8 @@ func runConfig(
return nil, errors.Wrap(err, "unable to set config: write to db")
}

// resync storage only if Storage options have changed
if !reflect.DeepEqual(newCfg.Storage, oldCfg.Storage) {
cid, err := pbm.SyncFromStorage(ctx, false)
if err != nil {
return nil, errors.Wrap(err, "resync")
}

if c.wait {
if err := waitForResyncWithTimeout(ctx, pbm, cid, c.waitTime); err != nil {
return nil, err
}
}
if err := resyncIfNeeded(ctx, pbm, oldCfg, newCfg, c); err != nil {
return nil, err
}

return newCfg, nil
Expand All @@ -171,3 +158,20 @@ func readConfigFromFile(filename string) (*config.Config, error) {

return config.Parse(file)
}

func resyncIfNeeded(ctx context.Context, pbm *sdk.Client, oldCfg, newCfg *config.Config, c *configOpts) error {
if newCfg.Storage.IsSameStorage(&oldCfg.Storage) {
return nil
}

cid, err := pbm.SyncFromStorage(ctx, false)
if err != nil {
return errors.Wrap(err, "resync")
}

if !c.wait {
return nil
}

return waitForResyncWithTimeout(ctx, pbm, cid, c.waitTime)
}
80 changes: 74 additions & 6 deletions pbm/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/percona/percona-backup-mongodb/pbm/storage/oss"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mongodb"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -43,9 +44,10 @@ func TestIsSameStorage(t *testing.T) {
InsecureSkipTLSVerify: false,
}
eq := &s3.Config{
Region: "eu",
Bucket: "b1",
Prefix: "p1",
Region: "eu",
Bucket: "b1",
Prefix: "p1",
EndpointURL: "ep.com",
}
if !cfg.IsSameStorage(eq) {
t.Errorf("config storage should identify the same instance: cfg=%+v, eq=%+v", cfg, eq)
Expand All @@ -68,6 +70,12 @@ func TestIsSameStorage(t *testing.T) {
if cfg.IsSameStorage(neq) {
t.Errorf("storage instances has different prefix: cfg=%+v, eq=%+v", cfg, neq)
}

neq = cfg.Clone()
neq.EndpointURL = "ep2.com"
if cfg.IsSameStorage(neq) {
t.Errorf("storage instances has different EndpointURL: cfg=%+v, eq=%+v", cfg, neq)
}
})

t.Run("Azure", func(t *testing.T) {
Expand All @@ -82,9 +90,10 @@ func TestIsSameStorage(t *testing.T) {
}

eq := &azure.Config{
Account: "a1",
Container: "c1",
Prefix: "p1",
Account: "a1",
Container: "c1",
Prefix: "p1",
EndpointURL: "az.com",
}
if !cfg.IsSameStorage(eq) {
t.Errorf("config storage should identify the same instance: cfg=%+v, eq=%+v", cfg, eq)
Expand All @@ -107,6 +116,12 @@ func TestIsSameStorage(t *testing.T) {
if cfg.IsSameStorage(neq) {
t.Errorf("storage instances has different prefix: cfg=%+v, eq=%+v", cfg, neq)
}

neq = cfg.Clone()
neq.EndpointURL = "az2.com"
if cfg.IsSameStorage(neq) {
t.Errorf("storage instances has different EndpointURL: cfg=%+v, eq=%+v", cfg, neq)
}
})

t.Run("GCS", func(t *testing.T) {
Expand Down Expand Up @@ -217,6 +232,59 @@ func TestIsSameStorage(t *testing.T) {
t.Errorf("storage instances has different prefix: cfg=%+v, eq=%+v", cfg, neq)
}
})

t.Run("oss", func(t *testing.T) {
cfg := &oss.Config{
Region: "eu",
EndpointURL: "ep.com",
Bucket: "b1",
Prefix: "p1",
Credentials: oss.Credentials{
AccessKeyID: "k1",
AccessKeySecret: "k2",
SecurityToken: "sect",
},
ConnectTimeout: 10 * time.Second,
UploadPartSize: 6 << 20,
MaxObjSizeGB: floatPtr(1.1),
Retryer: &oss.Retryer{},
ServerSideEncryption: &oss.SSE{},
}
eq := &oss.Config{
Region: "eu",
EndpointURL: "ep.com",
Bucket: "b1",
Prefix: "p1",
}
if !cfg.IsSameStorage(eq) {
t.Errorf("config storage should identify the same instance: cfg=%+v, eq=%+v, diff=%s",
cfg, eq, cmp.Diff(*cfg, *eq))
}

neq := cfg.Clone()
neq.Region = "us"
if cfg.IsSameStorage(neq) {
t.Errorf("storage instances has different region: cfg=%+v, eq=%+v", cfg, neq)
}

neq = cfg.Clone()
neq.EndpointURL = "ep2.com"
if cfg.IsSameStorage(neq) {
t.Errorf("storage instances has different EndpointURL: cfg=%+v, eq=%+v", cfg, neq)
}

neq = cfg.Clone()
neq.Bucket = "b2"
if cfg.IsSameStorage(neq) {
t.Errorf("storage instances has different bucket: cfg=%+v, eq=%+v", cfg, neq)
}

neq = cfg.Clone()
neq.Prefix = "p2"
if cfg.IsSameStorage(neq) {
t.Errorf("storage instances has different prefix: cfg=%+v, eq=%+v", cfg, neq)
}
})
}

func TestCastError(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions pbm/storage/azure/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func (cfg *Config) IsSameStorage(other *Config) bool {
if cfg.Prefix != other.Prefix {
return false
}
if cfg.EndpointURL != other.EndpointURL {
return false
}
if !maps.Equal(cfg.EndpointURLMap, other.EndpointURLMap) {
return false
}

return true
}

Expand Down
4 changes: 4 additions & 0 deletions pbm/storage/oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
}

type SSE struct {
EncryptionMethod string `bson:"encryptionMethod,omitempty" json:"encryptionMethod,omitempty" yaml:"encryptionMethod,omitempty"`

Check failure on line 47 in pbm/storage/oss/client.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

The line is 131 characters long, which exceeds the maximum of 120 characters. (lll)
EncryptionAlgorithm string `bson:"encryptionAlgorithm,omitempty" json:"encryptionAlgorithm,omitempty" yaml:"encryptionAlgorithm,omitempty"`

Check failure on line 48 in pbm/storage/oss/client.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

The line is 140 characters long, which exceeds the maximum of 120 characters. (lll)
EncryptionKeyID string `bson:"encryptionKeyId,omitempty" json:"encryptionKeyId,omitempty" yaml:"encryptionKeyId,omitempty"`

Check failure on line 49 in pbm/storage/oss/client.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

The line is 128 characters long, which exceeds the maximum of 120 characters. (lll)
}

type Retryer struct {
Expand Down Expand Up @@ -78,6 +78,10 @@
if cfg.Prefix != other.Prefix {
return false
}
if cfg.EndpointURL != other.EndpointURL {
return false
}

return true
}

Expand Down
6 changes: 6 additions & 0 deletions pbm/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ func (cfg *Config) IsSameStorage(other *Config) bool {
if cfg.Prefix != other.Prefix {
return false
}
if cfg.EndpointURL != other.EndpointURL {
return false
}
if !maps.Equal(cfg.EndpointURLMap, other.EndpointURLMap) {
return false
}
return true
}

Expand Down
Loading