allow mirror to copy zero byte file (#3084)

this PR also cleans up code and avoids
extra calls for source target
This commit is contained in:
Harshavardhana
2020-02-18 11:09:42 -08:00
committed by GitHub
parent 2f2aecb09d
commit 5d9a04bac6
4 changed files with 67 additions and 66 deletions
+44 -29
View File
@@ -603,25 +603,38 @@ func (c *s3Client) watchOneBucket(bucket, prefix, suffix string, events []string
u := *c.targetURL
u.Path = path.Join(string(u.Separator), bucketName, key)
if strings.HasPrefix(record.EventName, "s3:ObjectCreated:") {
if strings.HasPrefix(record.EventName, "s3:ObjectCreated:PutRetention") {
if strings.HasPrefix(record.EventName, "s3:ObjectCreated:Copy") {
eventChan <- EventInfo{
Time: record.EventTime,
Size: record.S3.Object.Size,
Path: u.String(),
Type: EventCreatePutRetention,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
Time: record.EventTime,
Size: record.S3.Object.Size,
UserMetadata: record.S3.Object.UserMetadata,
Path: u.String(),
Type: EventCreateCopy,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
}
} else if strings.HasPrefix(record.EventName, "s3:ObjectCreated:PutRetention") {
eventChan <- EventInfo{
Time: record.EventTime,
Size: record.S3.Object.Size,
UserMetadata: record.S3.Object.UserMetadata,
Path: u.String(),
Type: EventCreatePutRetention,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
}
} else {
eventChan <- EventInfo{
Time: record.EventTime,
Size: record.S3.Object.Size,
Path: u.String(),
Type: EventCreate,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
Time: record.EventTime,
Size: record.S3.Object.Size,
UserMetadata: record.S3.Object.UserMetadata,
Path: u.String(),
Type: EventCreate,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
}
}
} else if strings.HasPrefix(record.EventName, "s3:ObjectRemoved:") {
@@ -635,23 +648,25 @@ func (c *s3Client) watchOneBucket(bucket, prefix, suffix string, events []string
}
} else if record.EventName == minio.ObjectAccessedGet {
eventChan <- EventInfo{
Time: record.EventTime,
Size: record.S3.Object.Size,
Path: u.String(),
Type: EventAccessedRead,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
Time: record.EventTime,
Size: record.S3.Object.Size,
UserMetadata: record.S3.Object.UserMetadata,
Path: u.String(),
Type: EventAccessedRead,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
}
} else if record.EventName == minio.ObjectAccessedHead {
eventChan <- EventInfo{
Time: record.EventTime,
Size: record.S3.Object.Size,
Path: u.String(),
Type: EventAccessedStat,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
Time: record.EventTime,
Size: record.S3.Object.Size,
UserMetadata: record.S3.Object.UserMetadata,
Path: u.String(),
Type: EventAccessedStat,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
}
}
}
+10 -29
View File
@@ -410,9 +410,6 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance
}
sourceURL := newClientURL(eventPath)
// trim trailing slash from source url
sourceURLStr := strings.TrimSuffix(sourceURLFull, string(sourceURL.Separator))
aliasedPath := strings.Replace(eventPath, sourceURLStr, mj.sourceURL, -1)
// build target path, it is the relative of the eventPath with the sourceUrl
// joined to the targetURL.
@@ -427,45 +424,30 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance
// newClient needs the unexpanded path, newCLientURL needs the expanded path
targetAlias, expandedTargetPath, _ := mustExpandAlias(targetPath)
targetURL := newClientURL(expandedTargetPath)
sourcePath := filepath.ToSlash(filepath.Join(sourceAlias, sourceURL.Path))
srcSSE := getSSE(sourcePath, mj.encKeyDB[sourceAlias])
tgtSSE := getSSE(targetPath, mj.encKeyDB[targetAlias])
if (event.Type == EventCreate) ||
(event.Type == EventCreateCopy) ||
(event.Type == EventCreatePutRetention) {
mirrorURL := URLs{
SourceAlias: sourceAlias,
SourceContent: &clientContent{
URL: *sourceURL,
Retention: event.Type == EventCreatePutRetention,
Size: event.Size,
Metadata: event.UserMetadata,
},
TargetAlias: targetAlias,
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"
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.
continue
}
sourceContent.Retention = event.Type == EventCreatePutRetention
mirrorURL.SourceContent = sourceContent
if event.Size == 0 {
if mirrorURL.SourceContent.Size == 0 && mirrorURL.SourceContent.Retention {
targetClient, err := newClient(targetPath)
if err != nil {
// cannot create targetclient
@@ -475,7 +457,7 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance
shouldQueue := false
if !mj.isOverwrite {
_, err = targetClient.Stat(false, false, false, tgtSSE)
if err == nil || event.Type != EventCreatePutRetention {
if err == nil {
continue
} // doesn't exist
shouldQueue = true
@@ -483,7 +465,7 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance
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)
mj.status.Add(mirrorURL.SourceContent.Size)
mj.status.SetTotal(mj.status.Get()).Update()
mj.status.AddCounts(1)
mirrorURL.TotalSize = mj.status.Get()
@@ -502,7 +484,7 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance
}
_, err = targetClient.Stat(false, false, false, tgtSSE)
if err == nil {
if event.Type == EventCreatePutRetention {
if mirrorURL.SourceContent.Retention {
shouldQueue = true
} else {
continue
@@ -511,7 +493,6 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, cancelMirror context.Cance
shouldQueue = true
}
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.
mj.status.Add(mirrorURL.SourceContent.Size)
+12 -7
View File
@@ -29,6 +29,10 @@ type EventType string
const (
// EventCreate notifies when a new object is created
EventCreate EventType = "ObjectCreated"
// EventCreateCopy notifies when there was a server side copy
EventCreateCopy EventType = "ObjectCreated:Copy"
// EventCreatePutRetention notifies when a retention configuration is added to an object
EventCreatePutRetention EventType = "ObjectCreated:PutRetention"
// EventRemove notifies when a new object is deleted
@@ -44,13 +48,14 @@ const (
// EventInfo contains the information of the event that occurred and the source
// IP:PORT of the client which triggerred the event.
type EventInfo struct {
Time string
Size int64
Path string
Type EventType
Host string
Port string
UserAgent string
Time string
Size int64
UserMetadata map[string]string
Path string
Type EventType
Host string
Port string
UserAgent string
}
type watchParams struct {
+1 -1
View File
@@ -15,7 +15,7 @@ require (
github.com/mattn/go-runewidth v0.0.5 // indirect
github.com/minio/cli v1.22.0
github.com/minio/minio v0.0.0-20200118222113-b849fd7a756d
github.com/minio/minio-go/v6 v6.0.48
github.com/minio/minio-go/v6 v6.0.49-0.20200218155844-112c09f43c78
github.com/minio/sha256-simd v0.1.1
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/profile v1.3.0