From 0744b654ca2c13593c07757cc533943bfb366351 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 22 Oct 2026 06:15:21 -0700 Subject: [PATCH 1/2] filer.remote.sync: extract processCreateEvent Moves the create branch of eachEntryFunc into a function, matching processDeleteEvent and processUpdateEvent, so the create path can be exercised directly in tests. No behavior change. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- weed/command/filer_remote_sync_dir.go | 119 ++++++++++++--------- weed/command/filer_remote_sync_dir_test.go | 1 + 2 files changed, 68 insertions(+), 52 deletions(-) diff --git a/weed/command/filer_remote_sync_dir.go b/weed/command/filer_remote_sync_dir.go index 63ddce7519..aee3d13e36 100644 --- a/weed/command/filer_remote_sync_dir.go +++ b/weed/command/filer_remote_sync_dir.go @@ -148,58 +148,7 @@ func (option *RemoteSyncOptions) makeEventProcessor(remoteStorage *remote_pb.Rem resp, message = reshaped, reshaped.EventNotification } if filer_pb.IsCreate(resp) { - if isMultipartUploadFile(message.NewParentPath, message.NewEntry.Name) { - return nil - } - // Propagate delete markers as deletions on the remote. - // Delete markers are zero-content version entries, so they - // would be filtered out by the HasData check below. - if isDeleteMarker(message.NewEntry) { - if newParent, newName, ok := rewriteVersionedSourcePath(message.NewParentPath, message.NewEntry.Name); ok { - dest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(newParent, newName), remoteStorageMountLocation) - return syncDeleteMarker(client, option, message, dest) - } - return nil - } - if !filer.HasData(message.NewEntry) { - return nil - } - glog.V(2).Infof("create: %+v", resp) - if !shouldSendToRemote(message.NewEntry) { - glog.V(2).Infof("skipping creating: %+v", resp) - return nil - } - // Rewrite internal versioning paths to the original S3 key - // to prevent double-versioning when central also has versioning enabled - parentPath, entryName := message.NewParentPath, message.NewEntry.Name - isRewrittenVersion := false - if newParent, newName, ok := rewriteVersionedSourcePath(parentPath, entryName); ok { - glog.V(0).Infof("rewrite versioned path %s/%s -> %s/%s", parentPath, entryName, newParent, newName) - parentPath, entryName = newParent, newName - isRewrittenVersion = true - } - dest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(parentPath, entryName), remoteStorageMountLocation) - if message.NewEntry.IsDirectory { - glog.V(0).Infof("mkdir %s", remote_storage.FormatLocation(dest)) - return client.WriteDirectory(dest, remoteWriteEntry(message.NewEntry, *option.storageClass)) - } - remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewParentPath, remoteWriteEntry(message.NewEntry, *option.storageClass), dest) - if errors.Is(writeErr, errSuperseded) { - glog.Errorf("skipping %s: %v", remote_storage.FormatLocation(dest), writeErr) - return nil - } - if writeErr != nil { - return writeErr - } - // Skip updateLocalEntry for versioned rewrites: the logical - // object (e.g. file.xml) has no filer entry in versioned - // buckets, and stamping the internal v_* entry with a - // RemoteEntry for the logical key is semantically wrong. - // Replay is safe because S3 PutObject is idempotent. - if isRewrittenVersion { - return nil - } - return updateLocalEntry(option, message.NewParentPath, message.NewEntry, remoteEntry) + return processCreateEvent(option, filerSource, *option.storageClass, client, mountedDir, remoteStorageMountLocation, resp) } if filer_pb.IsDelete(resp) { return processDeleteEvent(filerSource, client, mountedDir, remoteStorageMountLocation, resp) @@ -260,6 +209,72 @@ func isUnderMount(mountedDir string, dir string, name string) bool { return path == mount || strings.HasPrefix(path, mount+"/") } +// processCreateEvent uploads a created entry to the remote key its path maps +// to. +func processCreateEvent( + filerClient filer_pb.FilerClient, + filerSource filer_pb.FilerClient, + storageClass string, + client remote_storage.RemoteStorageClient, + mountedDir string, + remoteStorageMountLocation *remote_pb.RemoteStorageLocation, + resp *filer_pb.SubscribeMetadataResponse, +) error { + message := resp.EventNotification + if isMultipartUploadFile(message.NewParentPath, message.NewEntry.Name) { + return nil + } + // Propagate delete markers as deletions on the remote. + // Delete markers are zero-content version entries, so they + // would be filtered out by the HasData check below. + if isDeleteMarker(message.NewEntry) { + if newParent, newName, ok := rewriteVersionedSourcePath(message.NewParentPath, message.NewEntry.Name); ok { + dest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(newParent, newName), remoteStorageMountLocation) + return syncDeleteMarker(client, filerClient, message, dest) + } + return nil + } + if !filer.HasData(message.NewEntry) { + return nil + } + glog.V(2).Infof("create: %+v", resp) + if !shouldSendToRemote(message.NewEntry) { + glog.V(2).Infof("skipping creating: %+v", resp) + return nil + } + // Rewrite internal versioning paths to the original S3 key + // to prevent double-versioning when central also has versioning enabled + parentPath, entryName := message.NewParentPath, message.NewEntry.Name + isRewrittenVersion := false + if newParent, newName, ok := rewriteVersionedSourcePath(parentPath, entryName); ok { + glog.V(0).Infof("rewrite versioned path %s/%s -> %s/%s", parentPath, entryName, newParent, newName) + parentPath, entryName = newParent, newName + isRewrittenVersion = true + } + dest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(parentPath, entryName), remoteStorageMountLocation) + if message.NewEntry.IsDirectory { + glog.V(0).Infof("mkdir %s", remote_storage.FormatLocation(dest)) + return client.WriteDirectory(dest, remoteWriteEntry(message.NewEntry, storageClass)) + } + remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewParentPath, remoteWriteEntry(message.NewEntry, storageClass), dest) + if errors.Is(writeErr, errSuperseded) { + glog.Errorf("skipping %s: %v", remote_storage.FormatLocation(dest), writeErr) + return nil + } + if writeErr != nil { + return writeErr + } + // Skip updateLocalEntry for versioned rewrites: the logical + // object (e.g. file.xml) has no filer entry in versioned + // buckets, and stamping the internal v_* entry with a + // RemoteEntry for the logical key is semantically wrong. + // Replay is safe because S3 PutObject is idempotent. + if isRewrittenVersion { + return nil + } + return updateLocalEntry(filerClient, message.NewParentPath, message.NewEntry, remoteEntry) +} + // processDeleteEvent removes the remote object an entry mapped to. A remote // object that is already absent counts as deleted: the entry was never // uploaded (created and deleted faster than the sync ran, or a replay of an diff --git a/weed/command/filer_remote_sync_dir_test.go b/weed/command/filer_remote_sync_dir_test.go index 8b93e817c1..3fd28f63e1 100644 --- a/weed/command/filer_remote_sync_dir_test.go +++ b/weed/command/filer_remote_sync_dir_test.go @@ -2114,3 +2114,4 @@ func TestReshapeCrossMountRename(t *testing.T) { } }) } + From 0bd5eec4183b0b66b400434d66d58dc70d65fb74 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 22 Oct 2026 06:15:33 -0700 Subject: [PATCH 2/2] filer.remote.sync: upload a create reshaped from a rename into the mount A file synced under the mount keeps its RemoteEntry across renames. The rename out deletes the remote object, and the rename back in reshapes to a create whose inherited stamp still matches the entry, so shouldSendToRemote skipped the upload and the new key stayed empty. The reshaped create now sends unconditionally: the stamp describes the old key, not this one. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- weed/command/filer_remote_sync_dir.go | 14 ++++-- weed/command/filer_remote_sync_dir_test.go | 50 ++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/weed/command/filer_remote_sync_dir.go b/weed/command/filer_remote_sync_dir.go index aee3d13e36..407caee493 100644 --- a/weed/command/filer_remote_sync_dir.go +++ b/weed/command/filer_remote_sync_dir.go @@ -140,15 +140,20 @@ func (option *RemoteSyncOptions) makeEventProcessor(remoteStorage *remote_pb.Rem if filer_pb.IsEmpty(resp) { return nil } + mustSend := false if message.OldEntry != nil && message.NewEntry != nil { reshaped, inMount := reshapeCrossMountRename(mountedDir, resp) if !inMount { return nil } + // a create reshaped from a rename into the mount can carry a + // RemoteEntry earned at the old path: it stamps the old key, so + // it cannot vouch for this destination + mustSend = filer_pb.IsCreate(reshaped) resp, message = reshaped, reshaped.EventNotification } if filer_pb.IsCreate(resp) { - return processCreateEvent(option, filerSource, *option.storageClass, client, mountedDir, remoteStorageMountLocation, resp) + return processCreateEvent(option, filerSource, *option.storageClass, client, mountedDir, remoteStorageMountLocation, resp, mustSend) } if filer_pb.IsDelete(resp) { return processDeleteEvent(filerSource, client, mountedDir, remoteStorageMountLocation, resp) @@ -210,7 +215,9 @@ func isUnderMount(mountedDir string, dir string, name string) bool { } // processCreateEvent uploads a created entry to the remote key its path maps -// to. +// to. mustSend skips the sync-stamp check: a create reshaped out of a rename +// into the mount inherits a RemoteEntry stamped at the old path's key, which +// says nothing about whether this destination holds the content. func processCreateEvent( filerClient filer_pb.FilerClient, filerSource filer_pb.FilerClient, @@ -219,6 +226,7 @@ func processCreateEvent( mountedDir string, remoteStorageMountLocation *remote_pb.RemoteStorageLocation, resp *filer_pb.SubscribeMetadataResponse, + mustSend bool, ) error { message := resp.EventNotification if isMultipartUploadFile(message.NewParentPath, message.NewEntry.Name) { @@ -238,7 +246,7 @@ func processCreateEvent( return nil } glog.V(2).Infof("create: %+v", resp) - if !shouldSendToRemote(message.NewEntry) { + if !mustSend && !shouldSendToRemote(message.NewEntry) { glog.V(2).Infof("skipping creating: %+v", resp) return nil } diff --git a/weed/command/filer_remote_sync_dir_test.go b/weed/command/filer_remote_sync_dir_test.go index 3fd28f63e1..dd6f06ae34 100644 --- a/weed/command/filer_remote_sync_dir_test.go +++ b/weed/command/filer_remote_sync_dir_test.go @@ -2115,3 +2115,53 @@ func TestReshapeCrossMountRename(t *testing.T) { }) } +// TestRenameIntoMountUploadsDespiteTheOldStamp covers a file synced at a path +// under the mount, renamed out (which deleted its remote object), and renamed +// back in: the entry still carries the RemoteEntry the first sync stamped, so +// shouldSendToRemote would skip the create the reshape made, leaving the new +// key without an object. A reshaped create must send unconditionally. +func TestRenameIntoMountUploadsDespiteTheOldStamp(t *testing.T) { + const mountedDir = "/buckets/mount" + mountLoc := &remote_pb.RemoteStorageLocation{Name: "b2", Bucket: "bucket", Path: "/"} + + stamp := &filer_pb.RemoteEntry{StorageName: "b2", RemoteETag: "abc", RemoteSize: 7, RemoteMtime: 1786096669, LastLocalSyncTsNs: 1786096669*1e9 + 1} + newEntry := &filer_pb.Entry{ + Name: "back.txt", + Content: []byte("payload"), + Attributes: &filer_pb.FuseAttributes{Mtime: 1786096669}, + RemoteEntry: stamp, + } + resp, inMount := reshapeCrossMountRename(mountedDir, &filer_pb.SubscribeMetadataResponse{ + Directory: "/plain", + EventNotification: &filer_pb.EventNotification{ + OldEntry: &filer_pb.Entry{Name: "back.txt", Attributes: &filer_pb.FuseAttributes{Mtime: 1786096669}, RemoteEntry: stamp}, + NewParentPath: mountedDir + "/lock", + NewEntry: newEntry, + }, + }) + if !inMount || !filer_pb.IsCreate(resp) { + t.Fatalf("reshape = %+v inMount=%v, want a create", resp, inMount) + } + if shouldSendToRemote(resp.EventNotification.NewEntry) { + t.Fatal("precondition: the stamp the rename carried should fail shouldSendToRemote") + } + + remote := &recordingRemote{} + filerClient := &stubFilerClient{entry: newEntry} + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, resp, true); err != nil { + t.Fatal(err) + } + wantWrite := &remote_pb.RemoteStorageLocation{Name: "b2", Bucket: "bucket", Path: "/lock/back.txt"} + if len(remote.writes) != 1 || !proto.Equal(remote.writes[0], wantWrite) { + t.Fatalf("writes = %+v, want the new key %s written", remote.writes, remote_storage.FormatLocation(wantWrite)) + } + + // a plain create keeps trusting the stamp + remote = &recordingRemote{} + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, resp, false); err != nil { + t.Fatal(err) + } + if len(remote.writes) != 0 { + t.Errorf("writes = %+v, want the stamped create skipped", remote.writes) + } +}