mirror: Use source time attr in multi-master mode (#3162)

This commit is contained in:
Anis Elleuch
2020-04-20 17:03:47 -07:00
committed by GitHub
parent 20dd7011b9
commit c7166eac3a
3 changed files with 77 additions and 65 deletions
+54 -36
View File
@@ -32,13 +32,13 @@ import (
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
differInFirst // only in source (FIRST)
differInSecond // only in target (SECOND)
differInNone differType = iota // does not differ
differInSize // differs in size
differInMMSourceMTime // differs in multi-master source modtime
differInMetadata // differs in metadata
differInType // differs in type, exfile/directory
differInFirst // only in source (FIRST)
differInSecond // only in target (SECOND)
)
func (d differType) String() string {
@@ -49,6 +49,8 @@ func (d differType) String() string {
return "size"
case differInMetadata:
return "metadata"
case differInMMSourceMTime:
return "mm-source-mtime"
case differInType:
return "type"
case differInFirst:
@@ -59,26 +61,48 @@ func (d differType) String() string {
return "unknown"
}
const multiMasterETagKey = "X-Amz-Meta-Mm-Etag"
const multiMasterSTagKey = "X-Amz-Meta-Mm-Stag"
const multiMasterSourceModTimeKey = "X-Amz-Meta-Mm-Source-Mtime"
func eTagMatch(src, tgt *ClientContent) bool {
if tgt.UserMetadata[multiMasterETagKey] != "" {
if tgt.UserMetadata[multiMasterETagKey] == src.UserMetadata[multiMasterETagKey] || tgt.UserMetadata[multiMasterETagKey] == src.ETag {
return true
}
func oldestNonNullTime(t1, t2 time.Time) time.Time {
if t1.IsZero() {
return t2
}
if src.UserMetadata[multiMasterETagKey] != "" {
if src.UserMetadata[multiMasterETagKey] == tgt.UserMetadata[multiMasterETagKey] || src.UserMetadata[multiMasterETagKey] == tgt.ETag {
return true
}
if t2.IsZero() {
return t1
}
return src.ETag == tgt.ETag
if t1.Before(t2) {
return t1
}
return t2
}
// multiMasterModTimeUpdated tries to calculate if the object copy in the target
// is older than the one in the source by comparing the modtime of the data.
func multiMasterModTimeUpdated(src, dst *ClientContent) bool {
if src == nil || dst == nil {
return false
}
if src.Time.IsZero() || dst.Time.IsZero() {
// This should only happen in a messy environment
// but we are returning false anyway so the caller
// function won't take any action.
return false
}
srcOriginLastModified, _ := time.Parse(time.RFC3339Nano, src.UserMetadata[multiMasterSourceModTimeKey])
dstOriginLastModified, _ := time.Parse(time.RFC3339Nano, dst.UserMetadata[multiMasterSourceModTimeKey])
srcActualModTime := oldestNonNullTime(src.Time, srcOriginLastModified)
dstActualModTime := oldestNonNullTime(dst.Time, dstOriginLastModified)
return srcActualModTime.After(dstActualModTime)
}
func metadataEqual(m1, m2 map[string]string) bool {
for k, v := range m1 {
if k == multiMasterETagKey || k == multiMasterSTagKey {
if k == multiMasterSourceModTimeKey {
continue
}
if m2[k] != v {
@@ -86,7 +110,7 @@ func metadataEqual(m1, m2 map[string]string) bool {
}
}
for k, v := range m2 {
if k == multiMasterETagKey || k == multiMasterSTagKey {
if k == multiMasterSourceModTimeKey {
continue
}
if m1[k] != v {
@@ -205,21 +229,7 @@ func differenceInternal(sourceClnt, targetClnt Client, sourceURL, targetURL stri
}
continue
}
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 {
if (srcType.IsRegular() && tgtType.IsRegular()) && srcSize != tgtSize {
// Regular files differing in size.
diffCh <- diffMessage{
FirstURL: srcCtnt.URL.String(),
@@ -228,6 +238,14 @@ func differenceInternal(sourceClnt, targetClnt Client, sourceURL, targetURL stri
firstContent: srcCtnt,
secondContent: tgtCtnt,
}
} else if multiMasterModTimeUpdated(srcCtnt, tgtCtnt) {
diffCh <- diffMessage{
FirstURL: srcCtnt.URL.String(),
SecondURL: tgtCtnt.URL.String(),
Diff: differInMMSourceMTime,
firstContent: srcCtnt,
secondContent: tgtCtnt,
}
} else if isMetadata &&
!metadataEqual(srcCtnt.UserMetadata, tgtCtnt.UserMetadata) &&
!metadataEqual(srcCtnt.Metadata, tgtCtnt.Metadata) {
+22 -28
View File
@@ -67,9 +67,9 @@ var (
Name: "preserve, a",
Usage: "preserve file(s)/object(s) attributes and bucket policy rules on target bucket(s)",
},
cli.StringFlag{
cli.BoolFlag{
Name: "multi-master",
Usage: `multi-master multi-site setup, "value" is the site tag for the multi-master deployment`,
Usage: "enable multi-master multi-site setup",
},
cli.StringSliceFlag{
Name: "exclude",
@@ -166,8 +166,8 @@ EXAMPLES:
{{.Prompt}} {{.HelpName}} -a backup/ s3/archive
15. Cross mirror between sites in a multi-master deployment.
Site-A: {{.Prompt}} {{.HelpName}} --watch --multi-master splunk-smartstore1 siteA siteB
Site-B: {{.Prompt}} {{.HelpName}} --watch --multi-master splunk-smartstore1 siteB siteA
Site-A: {{.Prompt}} {{.HelpName}} --watch --multi-master siteA siteB
Site-B: {{.Prompt}} {{.HelpName}} --watch --multi-master siteB siteA
`,
}
@@ -209,7 +209,6 @@ type mirrorJob struct {
encKeyDB map[string][]prefixSSEPair
multiMasterEnable bool
multiMasterSTag string
}
// mirrorMessage container for file mirror messages
@@ -301,13 +300,12 @@ func (mj *mirrorJob) doMirror(ctx context.Context, cancelMirror context.CancelFu
}
if mj.multiMasterEnable {
// Set multiMasterETagKey for the target.
if sURLs.SourceContent.UserMetadata[multiMasterETagKey] == "" {
sURLs.TargetContent.Metadata[multiMasterETagKey] = sURLs.SourceContent.ETag
}
if sURLs.SourceContent.UserMetadata[multiMasterSTagKey] == "" {
sURLs.TargetContent.Metadata[multiMasterSTagKey] = mj.multiMasterSTag
// If the source object already has source modtime attribute set, then
// use it in target. Otherwise use the S3 modtime instead.
if sURLs.SourceContent.Metadata[multiMasterSourceModTimeKey] != "" {
sURLs.TargetContent.Metadata[multiMasterSourceModTimeKey] = sURLs.SourceContent.Metadata[multiMasterSourceModTimeKey]
} else {
sURLs.TargetContent.Metadata[multiMasterSourceModTimeKey] = sURLs.SourceContent.Time.Format(time.RFC3339Nano)
}
}
@@ -420,22 +418,25 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance
if (event.Type == EventCreate) ||
(event.Type == EventCreateCopy) ||
(event.Type == EventCreatePutRetention) {
sourceModTime, _ := time.Parse(time.RFC3339Nano, event.Time)
mirrorURL := URLs{
SourceAlias: sourceAlias,
SourceContent: &ClientContent{
URL: *sourceURL,
Retention: event.Type == EventCreatePutRetention,
Size: event.Size,
Time: sourceModTime,
Metadata: event.UserMetadata,
},
TargetAlias: targetAlias,
TargetContent: &ClientContent{URL: *targetURL},
encKeyDB: mj.encKeyDB,
}
if mirrorURL.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.
if mj.multiMasterEnable &&
mirrorURL.SourceContent.Metadata[multiMasterSourceModTimeKey] != "" {
// If source has multi-master attributes, it means that the
// object was uploaded by "mc mirror", hence ignore the event
// to avoid copying it.
continue
}
if mirrorURL.SourceContent.Size == 0 && mirrorURL.SourceContent.Retention {
@@ -643,10 +644,7 @@ func (mj *mirrorJob) mirror(ctx context.Context, cancelMirror context.CancelFunc
return mj.monitorMirrorStatus()
}
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
}
func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch, isPreserve bool, multiMasterEnable bool, excludeOptions []string, olderThan, newerThan, storageClass string, userMetadata map[string]string, encKeyDB map[string][]prefixSSEPair) *mirrorJob {
mj := mirrorJob{
stopCh: make(chan struct{}),
@@ -657,7 +655,7 @@ func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch,
isRemove: isRemove,
isOverwrite: isOverwrite,
isWatch: isWatch,
isPreserve: isPreserve,
isPreserve: isPreserve || multiMasterEnable,
excludeOptions: excludeOptions,
olderThan: olderThan,
newerThan: newerThan,
@@ -667,7 +665,6 @@ func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch,
statusCh: make(chan URLs),
watcher: NewWatcher(UTCNow()),
multiMasterEnable: multiMasterEnable,
multiMasterSTag: multiMasterSTag,
}
mj.parallel, mj.queueCh = newParallelManager(mj.statusCh)
@@ -773,9 +770,6 @@ 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"),
@@ -783,12 +777,11 @@ func runMirror(srcURL, dstURL string, ctx *cli.Context, encKeyDB map[string][]pr
isOverwrite,
ctx.Bool("watch"),
ctx.Bool("a"),
multiMasterEnable,
ctx.Bool("multi-master"),
ctx.StringSlice("exclude"),
ctx.String("older-than"),
ctx.String("newer-than"),
ctx.String("storage-class"),
multiMasterSTag,
userMetaMap,
encKeyDB)
@@ -931,7 +924,8 @@ func mainMirror(ctx *cli.Context) error {
srcURL = origSrcURL
}
}
if ctx.String("multi-master") != "" {
if ctx.Bool("multi-master") {
for {
runMirror(srcURL, tgtURL, ctx, encKeyDB)
time.Sleep(time.Second * 2)
+1 -1
View File
@@ -149,7 +149,7 @@ func deltaSourceTarget(sourceURL, targetURL string, isFake, isOverwrite, isRemov
// No difference, continue.
case differInType:
URLsCh <- URLs{Error: errInvalidTarget(diffMsg.SecondURL)}
case differInSize, differInMetadata, differInETag:
case differInSize, differInMetadata, differInMMSourceMTime:
if !isOverwrite && !isFake {
// Size or time or etag differs but --overwrite not set.
URLsCh <- URLs{Error: errOverWriteNotAllowed(diffMsg.SecondURL)}