From 2d8bd7aecce71c9074edacaba3a2f1c056a568a6 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas <634494+krishnasrinivas@users.noreply.github.com> Date: Sun, 8 Dec 2019 16:25:50 -0800 Subject: [PATCH] Support for multi master sites (#2991) --- cmd/client-fs.go | 3 ++ cmd/client-s3.go | 9 ++++ cmd/client.go | 2 + cmd/difference.go | 59 ++++++++++++++++++++++-- cmd/mirror-main.go | 110 +++++++++++++++++++++++++++++++-------------- cmd/mirror-url.go | 4 +- 6 files changed, 148 insertions(+), 39 deletions(-) diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 713bc38d..8510740f 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -1186,3 +1186,6 @@ func (f *fsClient) fsStat(isIncomplete bool) (os.FileInfo, *probe.Error) { } return st, nil } + +func (f *fsClient) AddUserAgent(app, version string) { +} diff --git a/cmd/client-s3.go b/cmd/client-s3.go index a4003249..307829d2 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -940,6 +940,10 @@ func (c *s3Client) removeIncompleteObjects(bucket string, objectsCh <-chan strin return removeObjectErrorCh } +func (c *s3Client) AddUserAgent(app string, version string) { + c.api.SetAppInfo(app, version) +} + // Remove - remove object or bucket(s). func (c *s3Client) Remove(isIncomplete, isRemoveBucket bool, contentCh <-chan *clientContent) <-chan *probe.Error { errorCh := make(chan *probe.Error) @@ -1958,6 +1962,11 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan *clientContent, metadat content.ETag = object.ETag content.Time = object.LastModified content.Type = os.FileMode(0664) + content.Expires = object.Expires + content.UserMetadata = map[string]string{} + for k, v := range object.UserMetadata { + content.UserMetadata[k] = v + } contentCh <- content } } diff --git a/cmd/client.go b/cmd/client.go index 892b6752..415c0f9c 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -82,6 +82,8 @@ type Client interface { // GetURL returns back internal url GetURL() clientURL + + AddUserAgent(app, version string) } // Content container for content metadata diff --git a/cmd/difference.go b/cmd/difference.go index facefa57..a2fd0199 100644 --- a/cmd/difference.go +++ b/cmd/difference.go @@ -17,7 +17,6 @@ package cmd import ( - "reflect" "strings" "time" "unicode/utf8" @@ -34,6 +33,7 @@ type differType int const ( differInNone differType = iota // does not differ + differInETag // differs in ETag differInSize // differs in size differInMetadata // differs in metadata differInType // differs in type, exfile/directory @@ -59,6 +59,43 @@ func (d differType) String() string { return "unknown" } +const multiMasterETagKey = "X-Amz-Meta-Mm-Etag" +const multiMasterSTagKey = "X-Amz-Meta-Mm-Stag" + +func eTagMatch(src, tgt *clientContent) bool { + if tgt.UserMetadata[multiMasterETagKey] != "" { + if tgt.UserMetadata[multiMasterETagKey] == src.UserMetadata[multiMasterETagKey] || tgt.UserMetadata[multiMasterETagKey] == src.ETag { + return true + } + } + if src.UserMetadata[multiMasterETagKey] != "" { + if src.UserMetadata[multiMasterETagKey] == tgt.UserMetadata[multiMasterETagKey] || src.UserMetadata[multiMasterETagKey] == tgt.ETag { + return true + } + } + return src.ETag == tgt.ETag +} + +func metadataEqual(m1, m2 map[string]string) bool { + for k, v := range m1 { + if k == multiMasterETagKey || k == multiMasterSTagKey { + continue + } + if m2[k] != v { + return false + } + } + for k, v := range m2 { + if k == multiMasterETagKey || k == multiMasterSTagKey { + continue + } + if m1[k] != v { + return false + } + } + return true +} + func objectDifference(sourceClnt, targetClnt Client, sourceURL, targetURL string, isMetadata bool) (diffCh chan diffMessage) { return difference(sourceClnt, targetClnt, sourceURL, targetURL, isMetadata, true, false, DirNone) } @@ -168,7 +205,21 @@ func differenceInternal(sourceClnt, targetClnt Client, sourceURL, targetURL stri } continue } - if (srcType.IsRegular() && tgtType.IsRegular()) && srcSize != tgtSize { + if eTagMatch(srcCtnt, tgtCtnt) { + // If ETag matches, only thing that can differ is metadata. + if isMetadata && + !metadataEqual(srcCtnt.UserMetadata, tgtCtnt.UserMetadata) && + !metadataEqual(srcCtnt.Metadata, tgtCtnt.Metadata) { + // Regular files user requesting additional metadata to same file. + diffCh <- diffMessage{ + FirstURL: srcCtnt.URL.String(), + SecondURL: tgtCtnt.URL.String(), + Diff: differInMetadata, + firstContent: srcCtnt, + secondContent: tgtCtnt, + } + } + } else if (srcType.IsRegular() && tgtType.IsRegular()) && srcSize != tgtSize { // Regular files differing in size. diffCh <- diffMessage{ FirstURL: srcCtnt.URL.String(), @@ -178,8 +229,8 @@ func differenceInternal(sourceClnt, targetClnt Client, sourceURL, targetURL stri secondContent: tgtCtnt, } } else if isMetadata && - !reflect.DeepEqual(srcCtnt.UserMetadata, tgtCtnt.UserMetadata) && - !reflect.DeepEqual(srcCtnt.Metadata, tgtCtnt.Metadata) { + !metadataEqual(srcCtnt.UserMetadata, tgtCtnt.UserMetadata) && + !metadataEqual(srcCtnt.Metadata, tgtCtnt.Metadata) { // Regular files user requesting additional metadata to same file. diffCh <- diffMessage{ diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 04ce4ad3..5e378d53 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -67,6 +67,10 @@ var ( Name: "preserve, a", Usage: "preserve file(s)/object(s) attributes and bucket policy rules on target bucket(s)", }, + cli.StringFlag{ + Name: "multi-master", + Usage: "tag for multi master mode", + }, cli.StringSliceFlag{ Name: "exclude", Usage: "exclude object(s) that match specified object name pattern", @@ -160,9 +164,14 @@ EXAMPLES: 14. Mirror a local folder recursively to Amazon S3 cloud storage and preserve all local file attributes. {{.Prompt}} {{.HelpName}} -a backup/ s3/archive + + 15. Mirror from one site to another in a multi-master setup. + {{.Prompt}} {{.HelpName}} --watch --multi-master SITETAG site1/ site2/ `, } +const uaMirrorAppName = "mc-mirror" + type mirrorJob struct { // the channel to trap SIGKILL signals @@ -198,6 +207,9 @@ type mirrorJob struct { excludeOptions []string encKeyDB map[string][]prefixSSEPair + + multiMasterEnable bool + multiMasterSTag string } // mirrorMessage container for file mirror messages @@ -236,7 +248,7 @@ func (mj *mirrorJob) doRemove(sURLs URLs) URLs { if pErr != nil { return sURLs.WithError(pErr) } - + clnt.AddUserAgent(uaMirrorAppName, Version) contentCh := make(chan *clientContent, 1) contentCh <- &clientContent{URL: *newClientURL(sURLs.TargetContent.URL.Path)} close(contentCh) @@ -288,6 +300,19 @@ func (mj *mirrorJob) doMirror(ctx context.Context, cancelMirror context.CancelFu sURLs.TargetContent.Metadata["X-Amz-Storage-Class"] = mj.storageClass } + // Set multiMasterETagKey for the target. + if sURLs.SourceContent.UserMetadata[multiMasterETagKey] != "" { + sURLs.TargetContent.Metadata[multiMasterETagKey] = sURLs.SourceContent.UserMetadata[multiMasterETagKey] + } else { + sURLs.TargetContent.Metadata[multiMasterETagKey] = sURLs.SourceContent.ETag + } + + if sURLs.SourceContent.UserMetadata[multiMasterSTagKey] != "" { + sURLs.TargetContent.Metadata[multiMasterSTagKey] = sURLs.SourceContent.UserMetadata[multiMasterSTagKey] + } else { + sURLs.TargetContent.Metadata[multiMasterSTagKey] = mj.multiMasterSTag + } + if mj.isPreserve { attrValue, pErr := getFileAttrMeta(sURLs, mj.encKeyDB) if pErr != nil { @@ -404,8 +429,6 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance if (event.Type == EventCreate) || (event.Type == EventCreatePutRetention) { - // we are checking if a destination file exists now, and if we only - // overwrite it when force is enabled. mirrorURL := URLs{ SourceAlias: sourceAlias, SourceContent: &clientContent{URL: *sourceURL, Retention: event.Type == EventCreatePutRetention}, @@ -413,19 +436,27 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance TargetContent: &clientContent{URL: *targetURL}, encKeyDB: mj.encKeyDB, } + sourceClient, err := newClient(aliasedPath) + if err != nil { + // cannot create sourceclient + mj.statusCh <- mirrorURL.WithError(err) + continue + } + // we are checking if a destination file exists now, and if we only + // overwrite it when force is enabled. + sourceContent, err := sourceClient.Stat(false, true, false, srcSSE) + if err != nil { + // source doesn't exist anymore + mj.statusCh <- mirrorURL.WithError(err) + continue + } + if sourceContent.Metadata[multiMasterETagKey] != "" { + // If source has multiMasterETagKey, it means that the object was uplooaded by "mc mirror" + // hence ignore the event to avoid copying it. + continue + } + mirrorURL.SourceContent = sourceContent if event.Size == 0 { - sourceClient, err := newClient(aliasedPath) - if err != nil { - // cannot create sourceclient - mj.statusCh <- mirrorURL.WithError(err) - continue - } - sourceContent, err := sourceClient.Stat(false, false, false, srcSSE) - if err != nil { - // source doesn't exist anymore - mj.statusCh <- mirrorURL.WithError(err) - continue - } targetClient, err := newClient(targetPath) if err != nil { // cannot create targetclient @@ -440,7 +471,7 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance } // doesn't exist shouldQueue = true } - if shouldQueue || mj.isOverwrite { + if shouldQueue || mj.isOverwrite || mj.multiMasterEnable { // adjust total, because we want to show progress of // the item still queued to be copied. mj.status.Add(sourceContent.Size) @@ -453,7 +484,7 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance continue } shouldQueue := false - if !mj.isOverwrite { + if !mj.isOverwrite && !mj.multiMasterEnable { targetClient, err := newClient(targetPath) if err != nil { // cannot create targetclient @@ -470,7 +501,7 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance } // doesn't exist shouldQueue = true } - if shouldQueue || mj.isOverwrite { + if shouldQueue || mj.isOverwrite || mj.multiMasterEnable { mirrorURL.SourceContent.Size = event.Size // adjust total, because we want to show progress // of the itemj stiil queued to be copied. @@ -482,6 +513,9 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance mj.statusCh <- mj.doMirror(ctx, cancelMirror, mirrorURL) } } else if event.Type == EventRemove { + if strings.Contains(event.UserAgent, uaMirrorAppName) { + continue + } mirrorURL := URLs{ SourceAlias: sourceAlias, SourceContent: nil, @@ -491,7 +525,7 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance } mirrorURL.TotalCount = mj.status.GetCounts() mirrorURL.TotalSize = mj.status.Get() - if mirrorURL.TargetContent != nil && mj.isRemove { + if mirrorURL.TargetContent != nil && (mj.isRemove || mj.multiMasterEnable) { mj.statusCh <- mj.doRemove(mirrorURL) } } @@ -603,7 +637,10 @@ func (mj *mirrorJob) mirror(ctx context.Context, cancelMirror context.CancelFunc return mj.monitorMirrorStatus() } -func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch, isPreserve bool, excludeOptions []string, olderThan, newerThan string, storageClass string, userMetadata map[string]string, encKeyDB map[string][]prefixSSEPair) *mirrorJob { +func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch, isPreserve, multiMasterEnable bool, excludeOptions []string, olderThan, newerThan string, storageClass string, multiMasterSTag string, userMetadata map[string]string, encKeyDB map[string][]prefixSSEPair) *mirrorJob { + if multiMasterEnable { + isPreserve = true + } mj := mirrorJob{ trapCh: signalTrap(os.Interrupt, syscall.SIGTERM, syscall.SIGKILL), m: new(sync.Mutex), @@ -611,19 +648,21 @@ func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch, sourceURL: srcURL, targetURL: dstURL, - isFake: isFake, - isRemove: isRemove, - isOverwrite: isOverwrite, - isWatch: isWatch, - isPreserve: isPreserve, - excludeOptions: excludeOptions, - olderThan: olderThan, - newerThan: newerThan, - storageClass: storageClass, - userMetadata: userMetadata, - encKeyDB: encKeyDB, - statusCh: make(chan URLs), - watcher: NewWatcher(UTCNow()), + isFake: isFake, + isRemove: isRemove, + isOverwrite: isOverwrite, + isWatch: isWatch, + isPreserve: isPreserve, + excludeOptions: excludeOptions, + olderThan: olderThan, + newerThan: newerThan, + storageClass: storageClass, + userMetadata: userMetadata, + encKeyDB: encKeyDB, + statusCh: make(chan URLs), + watcher: NewWatcher(UTCNow()), + multiMasterEnable: multiMasterEnable, + multiMasterSTag: multiMasterSTag, } mj.parallel, mj.queueCh = newParallelManager(mj.statusCh) @@ -702,6 +741,9 @@ func runMirror(srcURL, dstURL string, ctx *cli.Context, encKeyDB map[string][]pr fatalIf(err, "Unable to initialize `"+dstURL+"`.") } + multiMasterSTag := ctx.String("multi-master") + multiMasterEnable := multiMasterSTag != "" + // Create a new mirror job and execute it mj := newMirrorJob(srcURL, dstURL, ctx.Bool("fake"), @@ -709,10 +751,12 @@ func runMirror(srcURL, dstURL string, ctx *cli.Context, encKeyDB map[string][]pr isOverwrite, ctx.Bool("watch"), ctx.Bool("a"), + multiMasterEnable, ctx.StringSlice("exclude"), ctx.String("older-than"), ctx.String("newer-than"), ctx.String("storage-class"), + multiMasterSTag, userMetaMap, encKeyDB) diff --git a/cmd/mirror-url.go b/cmd/mirror-url.go index a6c079f8..5ed992c4 100644 --- a/cmd/mirror-url.go +++ b/cmd/mirror-url.go @@ -149,9 +149,9 @@ func deltaSourceTarget(sourceURL, targetURL string, isFake, isOverwrite, isRemov // No difference, continue. case differInType: URLsCh <- URLs{Error: errInvalidTarget(diffMsg.SecondURL)} - case differInSize, differInMetadata: + case differInSize, differInMetadata, differInETag: if !isOverwrite && !isFake { - // Size or time differs but --overwrite not set. + // Size or time or etag differs but --overwrite not set. URLsCh <- URLs{Error: errOverWriteNotAllowed(diffMsg.SecondURL)} continue }