From 2c52cdf27c7bbd57b52f954eb50d4ca353d62f9d Mon Sep 17 00:00:00 2001 From: Poorna Krishnamoorthy Date: Sat, 12 Jun 2021 18:02:25 -0700 Subject: [PATCH] Add flag to `mc replicate` for existing object replication (#3698) This allows triggering a resyncing of previously replicated objects when existing object replication is enabled. It is useful in scenarios where replication target is entirely lost and content needs to be replicated again. --- cmd/admin-bucket-remote-add.go | 2 + cmd/admin-bucket-remote-ls.go | 2 + cmd/auto-complete.go | 1 + cmd/client-fs.go | 9 ++ cmd/client-s3.go | 15 ++++ cmd/client.go | 1 + cmd/replicate-add.go | 43 ++++++--- cmd/replicate-edit.go | 15 ++-- cmd/replicate-main.go | 1 + cmd/replicate-reset.go | 130 ++++++++++++++++++++++++++++ docs/minio-client-complete-guide.md | 6 ++ go.mod | 4 +- go.sum | 10 ++- 13 files changed, 214 insertions(+), 25 deletions(-) create mode 100644 cmd/replicate-reset.go diff --git a/cmd/admin-bucket-remote-add.go b/cmd/admin-bucket-remote-add.go index e2d92e5a..c2e26cba 100644 --- a/cmd/admin-bucket-remote-add.go +++ b/cmd/admin-bucket-remote-add.go @@ -138,6 +138,8 @@ type RemoteMessage struct { ReplicationSync bool `json:"replicationSync"` Proxy bool `json:"proxy"` HealthCheckDuration time.Duration `json:"healthcheckDuration"` + ResetID string `json:"resetID"` + ResetBefore time.Time `json:"resetBeforeDate"` } func (r RemoteMessage) String() string { diff --git a/cmd/admin-bucket-remote-ls.go b/cmd/admin-bucket-remote-ls.go index ad958fee..f1c75e33 100644 --- a/cmd/admin-bucket-remote-ls.go +++ b/cmd/admin-bucket-remote-ls.go @@ -158,6 +158,8 @@ func printRemotes(ctx *cli.Context, urlStr string, targets []madmin.BucketTarget ReplicationSync: target.ReplicationSync, Bandwidth: target.BandwidthLimit, Proxy: !target.DisableProxy, + ResetID: target.ResetID, + ResetBefore: target.ResetBeforeDate, }) } } diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 35950a9c..7dd9d4e3 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -272,6 +272,7 @@ var completeCmds = map[string]complete.Predictor{ "/replicate/export": s3Complete{deepLevel: 2}, "/replicate/import": s3Complete{deepLevel: 2}, "/replicate/status": s3Complete{deepLevel: 2}, + "/replicate/resync": s3Complete{deepLevel: 2}, "/tag/list": s3Completer, "/tag/remove": s3Completer, diff --git a/cmd/client-fs.go b/cmd/client-fs.go index b4348888..11fd8a68 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -1171,6 +1171,15 @@ func (f *fsClient) GetReplicationMetrics(ctx context.Context) (replication.Metri }) } +// ResetReplication - kicks off replication again on previously replicated objects if existing object +// replication is enabled in the replication config, not implemented +func (f *fsClient) ResetReplication(ctx context.Context, before time.Duration) (string, *probe.Error) { + return "", probe.NewError(APINotImplemented{ + API: "ResetReplication", + APIType: "filesystem", + }) +} + // Get encryption info for a bucket, not implemented. func (f *fsClient) GetEncryption(ctx context.Context) (string, string, *probe.Error) { return "", "", probe.NewError(APINotImplemented{ diff --git a/cmd/client-s3.go b/cmd/client-s3.go index d521d262..f027d4a7 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -2478,6 +2478,21 @@ func (c *S3Client) GetReplicationMetrics(ctx context.Context) (replication.Metri return metrics, nil } +// ResetReplication - kicks off replication again on previously replicated objects if existing object +// replication is enabled in the replication config.Optional to provide a timestamp +func (c *S3Client) ResetReplication(ctx context.Context, before time.Duration) (string, *probe.Error) { + bucket, _ := c.url2BucketAndObject() + if bucket == "" { + return "", probe.NewError(BucketNameEmpty{}) + } + + rID, e := c.api.ResetBucketReplication(ctx, bucket, before) + if e != nil { + return "", probe.NewError(e) + } + return rID, nil +} + // GetEncryption - gets bucket encryption info. func (c *S3Client) GetEncryption(ctx context.Context) (algorithm, keyID string, err *probe.Error) { bucket, _ := c.url2BucketAndObject() diff --git a/cmd/client.go b/cmd/client.go index 1dff3c56..e0807ff3 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -159,6 +159,7 @@ type Client interface { SetReplication(ctx context.Context, cfg *replication.Config, opts replication.Options) *probe.Error RemoveReplication(ctx context.Context) *probe.Error GetReplicationMetrics(ctx context.Context) (replication.Metrics, *probe.Error) + ResetReplication(ctx context.Context, before time.Duration) (string, *probe.Error) // Encryption operations GetEncryption(ctx context.Context) (string, string, *probe.Error) SetEncryption(ctx context.Context, algorithm, kmsKeyID string) *probe.Error diff --git a/cmd/replicate-add.go b/cmd/replicate-add.go index 7c02568b..3e72a5a0 100644 --- a/cmd/replicate-add.go +++ b/cmd/replicate-add.go @@ -62,7 +62,7 @@ var replicateAddFlags = []cli.Flag{ }, cli.StringFlag{ Name: "replicate", - Usage: "comma separated list to enable replication of delete markers, deletion of versioned objects and replica metadata sync(in the case of active-active replication).Valid options are \"delete-marker\", \"delete\" ,\"replica-metadata-sync\" and \"\"", + Usage: "comma separated list to enable replication of delete markers, deletion of versioned objects and replica metadata sync(in the case of active-active replication).Valid options are \"delete-marker\", \"delete\" ,\"replica-metadata-sync\", \"existing-objects\" and \"\"", }, } @@ -97,7 +97,16 @@ EXAMPLES: --storage-class "STANDARD" --disable \ --arn 'arn:minio:replica::c5be6b16-769d-432a-9ef1-4567081f3566:destbucket' \ --priority 1 \ - --remote-bucket "destbucket" + --remote-bucket "destbucket" + + 3. Add replication configuration rule with existing object replication, delete marker replication and versioned deletes + enabled on bucket "mybucket" for alias "myminio". + {{.Prompt}} {{.HelpName}} myminio/mybucket/prefix --tags "key1=value1&key2=value2" \ + --storage-class "STANDARD" --disable \ + --arn 'arn:minio:replica::c5be6b16-769d-432a-9ef1-4567081f3566:destbucket' \ + --priority 1 \ + --remote-bucket "destbucket" \ + --replicate "existing-objects,delete,delete-marker" `, } @@ -163,6 +172,7 @@ func mainReplicateAdd(cliCtx *cli.Context) error { dmReplicateStatus := disableStatus deleteReplicationStatus := disableStatus replicaSync := enableStatus + existingReplicationStatus := disableStatus if cliCtx.IsSet("replicate") { replSlice := strings.Split(cliCtx.String("replicate"), ",") for _, opt := range replSlice { @@ -173,23 +183,28 @@ func mainReplicateAdd(cliCtx *cli.Context) error { deleteReplicationStatus = enableStatus case "replica-metadata-sync": replicaSync = enableStatus + case "existing-objects": + existingReplicationStatus = enableStatus + default: - fatalIf(probe.NewError(fmt.Errorf("invalid value for --replicate flag %s", cliCtx.String("replicate"))), "--replicate flag takes one or more comma separated string with values \"delete, delete-marker, replica-metadata-sync\" or \"\" to disable these settings") + fatalIf(probe.NewError(fmt.Errorf("invalid value for --replicate flag %s", cliCtx.String("replicate"))), "--replicate flag takes one or more comma separated string with values \"delete, delete-marker, replica-metadata-sync\",\"existing-objects\" or \"\" to disable these settings") } } } + opts := replication.Options{ - TagString: cliCtx.String("tags"), - RoleArn: cliCtx.String("arn"), - StorageClass: cliCtx.String("storage-class"), - Priority: strconv.Itoa(cliCtx.Int("priority")), - RuleStatus: ruleStatus, - ID: cliCtx.String("id"), - DestBucket: cliCtx.String("remote-bucket"), - Op: replication.AddOption, - ReplicateDeleteMarkers: dmReplicateStatus, - ReplicateDeletes: deleteReplicationStatus, - ReplicaSync: replicaSync, + TagString: cliCtx.String("tags"), + RoleArn: cliCtx.String("arn"), + StorageClass: cliCtx.String("storage-class"), + Priority: strconv.Itoa(cliCtx.Int("priority")), + RuleStatus: ruleStatus, + ID: cliCtx.String("id"), + DestBucket: cliCtx.String("remote-bucket"), + Op: replication.AddOption, + ReplicateDeleteMarkers: dmReplicateStatus, + ReplicateDeletes: deleteReplicationStatus, + ReplicaSync: replicaSync, + ExistingObjectReplicate: existingReplicationStatus, } fatalIf(client.SetReplication(ctx, &rcfg, opts), "Could not add replication rule") printMsg(replicateAddMessage{ diff --git a/cmd/replicate-edit.go b/cmd/replicate-edit.go index 634abd06..059b1b1b 100644 --- a/cmd/replicate-edit.go +++ b/cmd/replicate-edit.go @@ -58,7 +58,7 @@ var replicateEditFlags = []cli.Flag{ }, cli.StringFlag{ Name: "replicate", - Usage: "comma separated list to enable replication of delete markers, deletion of versioned objects and syncing replica metadata modifications.Valid options are \"delete-marker\", \"delete\",\"replica-metadata-sync\" and \"\"", + Usage: "comma separated list to enable replication of delete markers, deletion of versioned objects and syncing replica metadata modifications.Valid options are \"delete-marker\", \"delete\",\"replica-metadata-sync\", \"existing-objects\" and \"\"", }, } @@ -97,8 +97,8 @@ EXAMPLES: 6. Disable delete marker and versioned delete replication on a replication configuration rule with ID "kxYD.491" on a target myminio/bucket. {{.Prompt}} {{.HelpName}} myminio/mybucket --id "kxYD.491" --replicate "" - 7. Enable replica metadata sync ,delete marker and versioned delete replication on a replication configuration rule with ID "kxYD.491" on a target myminio/bucket. - {{.Prompt}} {{.HelpName}} myminio/mybucket --id "kxYD.491" --replicate "delete,delete-marker,replica-metadata-sync" + 7. Enable existing object replication on a configuration rule with ID "kxYD.491" on a target myminio/bucket. Rule previously had enabled delete marker and versioned delete replication. + {{.Prompt}} {{.HelpName}} myminio/mybucket --id "kxYD.491" --replicate "existing-objects,delete-marker,delete" `, } @@ -157,12 +157,14 @@ func mainReplicateEdit(cliCtx *cli.Context) error { fatalIf(err.Trace(args...), "--state can be either `enable` or `disable`") } } - var vDeleteReplicate, dmReplicate, replicasync string + var vDeleteReplicate, dmReplicate, replicasync, existingReplState string if cliCtx.IsSet("replicate") { replSlice := strings.Split(cliCtx.String("replicate"), ",") vDeleteReplicate = disableStatus dmReplicate = disableStatus replicasync = disableStatus + existingReplState = disableStatus + for _, opt := range replSlice { switch strings.TrimSpace(strings.ToLower(opt)) { case "delete-marker": @@ -171,7 +173,8 @@ func mainReplicateEdit(cliCtx *cli.Context) error { vDeleteReplicate = enableStatus case "replica-metadata-sync": replicasync = enableStatus - + case "existing-objects": + existingReplState = enableStatus default: if opt != "" { fatalIf(probe.NewError(fmt.Errorf("invalid value for --replicate flag %s", cliCtx.String("replicate"))), "--replicate flag takes one or more comma separated string with values \"delete, delete-marker, replica-metadata-sync\" or \"\"") @@ -199,7 +202,9 @@ func mainReplicateEdit(cliCtx *cli.Context) error { opts.ReplicateDeletes = vDeleteReplicate opts.ReplicateDeleteMarkers = dmReplicate opts.ReplicaSync = replicasync + opts.ExistingObjectReplicate = existingReplState } + fatalIf(client.SetReplication(ctx, &rcfg, opts), "Could not modify replication rule") printMsg(replicateEditMessage{ Op: "set", diff --git a/cmd/replicate-main.go b/cmd/replicate-main.go index 94f95db9..f4ae0bb1 100644 --- a/cmd/replicate-main.go +++ b/cmd/replicate-main.go @@ -24,6 +24,7 @@ var replicateSubcommands = []cli.Command{ replicateEditCmd, replicateListCmd, replicateStatusCmd, + replicateResetCmd, replicateExportCmd, replicateImportCmd, replicateRemoveCmd, diff --git a/cmd/replicate-reset.go b/cmd/replicate-reset.go new file mode 100644 index 00000000..08373759 --- /dev/null +++ b/cmd/replicate-reset.go @@ -0,0 +1,130 @@ +// Copyright (c) 2015-2021 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "context" + "fmt" + "strings" + "time" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" + "maze.io/x/duration" +) + +var replicateResetFlags = []cli.Flag{ + cli.StringFlag{ + Name: "older-than", + Usage: "re-replicate objects older than n days", + }, +} + +var replicateResetCmd = cli.Command{ + Name: "resync", + Usage: "re-replicate all previously replicated objects", + Aliases: []string{"reset"}, + Action: mainReplicateReset, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: append(globalFlags, replicateResetFlags...), + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Re-replicate previously replicated objects in bucket "mybucket" for alias "myminio". + {{.Prompt}} {{.HelpName}} myminio/mybucket + + 2. Re-replicate all objects older than 60 days in bucket "mybucket". + {{.Prompt}} {{.HelpName}} myminio/mybucket --older-than 60d +`, +} + +// checkReplicateResetSyntax - validate all the passed arguments +func checkReplicateResetSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "reset", 1) // last argument is exit code + } +} + +type replicateResetMessage struct { + Op string `json:"op"` + URL string `json:"url"` + ResetID string `json:"resetID"` + Status string `json:"status"` +} + +func (r replicateResetMessage) JSON() string { + r.Status = "success" + jsonMessageBytes, e := json.MarshalIndent(r, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(jsonMessageBytes) +} + +func (r replicateResetMessage) String() string { + return console.Colorize("replicateResetMessage", fmt.Sprintf("Replication reset started for %s with ID %s", r.URL, r.ResetID)) +} + +func mainReplicateReset(cliCtx *cli.Context) error { + ctx, cancelReplicateReset := context.WithCancel(globalContext) + defer cancelReplicateReset() + + console.SetColor("replicateResetMessage", color.New(color.FgGreen)) + + checkReplicateResetSyntax(cliCtx) + + // Get the alias parameter from cli + args := cliCtx.Args() + aliasedURL := args.Get(0) + // Create a new Client + client, err := newClient(aliasedURL) + fatalIf(err, "Unable to initialize connection.") + var olderThanStr string + var olderThan time.Duration + if cliCtx.IsSet("older-than") { + olderThanStr = cliCtx.String("older-than") + if olderThanStr != "" { + days, e := duration.ParseDuration(olderThanStr) + if e != nil || !strings.ContainsAny(olderThanStr, "dwy") { + fatalIf(probe.NewError(e), "Unable to parse older-than=`"+olderThanStr+"`.") + } + if days == 0 { + fatalIf(probe.NewError(e), "older-than cannot be set to zero") + } + olderThan = time.Duration(days.Days()) + } + } + + replicateReset, err := client.ResetReplication(ctx, olderThan) + fatalIf(err.Trace(args...), "Unable to reset replication") + printMsg(replicateResetMessage{ + Op: "status", + URL: aliasedURL, + ResetID: replicateReset, + }) + return nil +} diff --git a/docs/minio-client-complete-guide.md b/docs/minio-client-complete-guide.md index a38c7aa1..93df8996 100644 --- a/docs/minio-client-complete-guide.md +++ b/docs/minio-client-complete-guide.md @@ -1883,3 +1883,9 @@ mc replicate export myminio/mybucket > /data/replicate/config ``` mc replicate status myminio/mybucket ``` + +*Example: Resync replication of previously replicated objects from `mybucket` on alias `myminio` to configured remote target in replication configuration. + +``` +mc replicate resync myminio/mybucket +``` diff --git a/go.mod b/go.mod index 477493de..109f32fa 100644 --- a/go.mod +++ b/go.mod @@ -16,9 +16,9 @@ require ( github.com/minio/cli v1.22.0 github.com/minio/colorjson v1.0.1 github.com/minio/filepath v1.0.0 - github.com/minio/madmin-go v1.0.10-0.20210602195449-b1bf23ec13e4 + github.com/minio/madmin-go v1.0.11 github.com/minio/md5-simd v1.1.1 // indirect - github.com/minio/minio-go/v7 v7.0.11-0.20210517200026-f0518ca447d6 + github.com/minio/minio-go/v7 v7.0.11-0.20210607181445-e162fdb8e584 github.com/minio/pkg v1.0.3 github.com/minio/sha256-simd v1.0.0 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index f1b8f29b..c56c35b8 100644 --- a/go.sum +++ b/go.sum @@ -249,14 +249,14 @@ github.com/minio/colorjson v1.0.1 h1:+hvfP8C1iMB95AT+ZFDRE+Knn9QPd9lg0CRJY9DRpos github.com/minio/colorjson v1.0.1/go.mod h1:oPM3zQQY8Gz9NGtgvuBEjQ+gPZLKAGc7T+kjMlwtOgs= github.com/minio/filepath v1.0.0 h1:fvkJu1+6X+ECRA6G3+JJETj4QeAYO9sV43I79H8ubDY= github.com/minio/filepath v1.0.0/go.mod h1:/nRZA2ldl5z6jT9/KQuvZcQlxZIMQoFFQPvEXx9T/Bw= -github.com/minio/madmin-go v1.0.10-0.20210602195449-b1bf23ec13e4 h1:AxtnO3AODg0t2IPXbrqmDBhGZTcrUhlT/ixdLQQ3164= -github.com/minio/madmin-go v1.0.10-0.20210602195449-b1bf23ec13e4/go.mod h1:BK+z4XRx7Y1v8SFWXsuLNqQqnq5BO/axJ8IDJfgyvfs= +github.com/minio/madmin-go v1.0.11 h1:oyTsINMIgyF1OrjtAgef+gfbwH0i6Kxk3waY1mAcN58= +github.com/minio/madmin-go v1.0.11/go.mod h1:BK+z4XRx7Y1v8SFWXsuLNqQqnq5BO/axJ8IDJfgyvfs= github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw= github.com/minio/md5-simd v1.1.1 h1:9ojcLbuZ4gXbB2sX53MKn8JUZ0sB/2wfwsEcRw+I08U= github.com/minio/md5-simd v1.1.1/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw= github.com/minio/minio-go/v7 v7.0.11-0.20210302210017-6ae69c73ce78/go.mod h1:mTh2uJuAbEqdhMVl6CMIIZLUeiMiWtJR4JB8/5g2skw= -github.com/minio/minio-go/v7 v7.0.11-0.20210517200026-f0518ca447d6 h1:GVR+UTvfe2r2YTYHWrA/yRF5nouMjJh3kwxNTZ8npso= -github.com/minio/minio-go/v7 v7.0.11-0.20210517200026-f0518ca447d6/go.mod h1:td4gW1ldOsj1PbSNS+WYK43j+P1XVhX/8W8awaYlBFo= +github.com/minio/minio-go/v7 v7.0.11-0.20210607181445-e162fdb8e584 h1:GgOqN/DE44zZh3+sg5elR6diWq+Qv4dN7U2KvPVfe1I= +github.com/minio/minio-go/v7 v7.0.11-0.20210607181445-e162fdb8e584/go.mod h1:WoyW+ySKAKjY98B9+7ZbI8z8S3jaxaisdcvj9TGlazA= github.com/minio/pkg v1.0.3 h1:tUhM6lG/BdNB0+5f2RbE4ifCAYwMs6cRJnZ/AY0WIeQ= github.com/minio/pkg v1.0.3/go.mod h1:obU54TZ9QlMv0TRaDgQ/JTzf11ZSXxnSfLrm4tMtBP8= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= @@ -377,6 +377,8 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/assertions v1.1.1 h1:T/YLemO5Yp7KPzS+lVtu+WsHn8yoSwTfItdAd1r3cck= github.com/smartystreets/assertions v1.1.1/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=