diff --git a/weed/command/filer_remote_sync_dir.go b/weed/command/filer_remote_sync_dir.go index 632baa54e3..b32f9770aa 100644 --- a/weed/command/filer_remote_sync_dir.go +++ b/weed/command/filer_remote_sync_dir.go @@ -141,6 +141,7 @@ func (option *RemoteSyncOptions) makeEventProcessor(remoteStorage *remote_pb.Rem return nil } mustSend := false + var renameSrc util.FullPath if message.OldEntry != nil && message.NewEntry != nil { reshaped, inMount := reshapeCrossMountRename(mountedDir, resp) if !inMount { @@ -150,10 +151,13 @@ func (option *RemoteSyncOptions) makeEventProcessor(remoteStorage *remote_pb.Rem // RemoteEntry earned at the old path: it stamps the old key, so // it cannot vouch for this destination mustSend = filer_pb.IsCreate(reshaped) + if mustSend { + renameSrc = util.NewFullPath(resp.Directory, message.OldEntry.Name) + } resp, message = reshaped, reshaped.EventNotification } if filer_pb.IsCreate(resp) { - return processCreateEvent(option, filerSource, *option.storageClass, client, mountedDir, remoteStorageMountLocation, resp, mustSend) + return processCreateEvent(option, filerSource, *option.storageClass, client, mountedDir, remoteStorageMountLocation, resp, mustSend, renameSrc) } if filer_pb.IsDelete(resp) { return processDeleteEvent(filerSource, client, mountedDir, remoteStorageMountLocation, resp) @@ -217,7 +221,8 @@ func isUnderMount(mountedDir string, dir string, name string) bool { // processCreateEvent uploads a created entry to the remote key its path maps // 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. +// says nothing about whether this destination holds the content. renameSrc is +// the path that rename came from, and is empty for a genuine create. func processCreateEvent( filerClient filer_pb.FilerClient, filerSource filer_pb.FilerClient, @@ -227,6 +232,7 @@ func processCreateEvent( remoteStorageMountLocation *remote_pb.RemoteStorageLocation, resp *filer_pb.SubscribeMetadataResponse, mustSend bool, + renameSrc util.FullPath, ) error { message := resp.EventNotification if isMultipartUploadFile(message.NewParentPath, message.NewEntry.Name) { @@ -276,6 +282,9 @@ func processCreateEvent( 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) + if mustSend { + return recoverSupersededCreate(filerClient, filerSource, client, mountedDir, remoteStorageMountLocation, message.NewParentPath, message.NewEntry.Name, renameSrc, dest, storageClass) + } return nil } if writeErr != nil { @@ -733,6 +742,176 @@ func uploadCurrentEntry(filerClient filer_pb.FilerClient, filerSource filer_pb.F return updateLocalEntry(filerClient, dir, current, remoteEntry) } +// recoverSupersededCreate finishes a create reshaped from a rename into the +// mount when the filer has moved past the entry the event described. A plain +// superseded create skips because the superseding event follows in the log; a +// reshaped one has no later event for this destination. remote.uncache accepts +// the rename's inherited stamp and drops the chunks, and the update it logs +// skips the upload through isDroppedLocalCopy, so the content would exist +// nowhere. What the filer holds now decides: local content uploads here only +// when the superseding event will not (the stamp-edge case uploadCurrentEntry +// covers), and remote-only content copies from the object its stamp +// describes, the way completeRemoteOnlyRename finishes a rename. +func recoverSupersededCreate( + filerClient filer_pb.FilerClient, + filerSource filer_pb.FilerClient, + client remote_storage.RemoteStorageClient, + mountedDir string, + remoteStorageMountLocation *remote_pb.RemoteStorageLocation, + dir, name string, + renameSrc util.FullPath, + dest *remote_pb.RemoteStorageLocation, + storageClass string, +) error { + current, err := currentEntry(filerSource, dir, name) + if err != nil { + return err + } + if current == nil || current.IsDirectory { + return nil + } + if isRemoteOnly(current) { + return copyRenameInSource(filerClient, client, mountedDir, remoteStorageMountLocation, dir, current, renameSrc, dest, storageClass) + } + if shouldSendToRemote(current) { + glog.V(0).Infof("leaving %s to the event that superseded the rename", remote_storage.FormatLocation(dest)) + return nil + } + glog.V(0).Infof("uploading the current %s in place of the superseded rename", remote_storage.FormatLocation(dest)) + remoteEntry, writeErr := retriedWriteFile(client, filerSource, dir, remoteWriteEntry(current, storageClass), dest) + if errors.Is(writeErr, errSuperseded) { + glog.Errorf("skipping %s: %v", remote_storage.FormatLocation(dest), writeErr) + return nil + } + if writeErr != nil { + return writeErr + } + return updateLocalEntry(filerClient, dir, current, remoteEntry) +} + +// copyRenameInSource gives the destination key the content of a remote-only +// entry a rename carried into the mount. The object sits at the key the +// renamed-from path maps to on its own mount's remote, or where a +// pending-rename marker under this mount points. The source object is left +// alone: the source side removes it through its own delete event. When the +// destination already holds what the stamp describes (an earlier run copied +// it), only the stamp is refreshed. With no object on either side the event +// fails and holds the offset. +func copyRenameInSource( + filerClient filer_pb.FilerClient, + client remote_storage.RemoteStorageClient, + mountedDir string, + remoteStorageMountLocation *remote_pb.RemoteStorageLocation, + dir string, + current *filer_pb.Entry, + renameSrc util.FullPath, + dest *remote_pb.RemoteStorageLocation, + storageClass string, +) error { + srcClient, srcLoc, err := renameInSource(filerClient, client, mountedDir, remoteStorageMountLocation, current, renameSrc) + if err != nil { + return err + } + if existing, err := client.StatFile(dest); err == nil { + if remote_storage.Describes(current.RemoteEntry, existing) { + glog.V(0).Infof("%s already holds the renamed content", remote_storage.FormatLocation(dest)) + return updateLocalEntry(filerClient, dir, current, existing) + } + glog.V(0).Infof("%s holds an object the entry does not describe (size %d, want %d); replacing it from %s", remote_storage.FormatLocation(dest), existing.RemoteSize, current.RemoteEntry.RemoteSize, remote_storage.FormatLocation(srcLoc)) + } else if !errors.Is(err, remote_storage.ErrRemoteObjectNotFound) { + return err + } + stat, err := srcClient.StatFile(srcLoc) + if errors.Is(err, remote_storage.ErrRemoteObjectNotFound) { + return fmt.Errorf("%s: content is on neither %s nor %s", util.NewFullPath(dir, current.Name), remote_storage.FormatLocation(srcLoc), remote_storage.FormatLocation(dest)) + } + if err != nil { + return err + } + reader, err := openRemoteObject(srcClient, srcLoc, stat.RemoteSize) + if err != nil { + return err + } + defer reader.Close() + glog.V(0).Infof("copy %s -> %s", remote_storage.FormatLocation(srcLoc), remote_storage.FormatLocation(dest)) + remoteEntry, err := client.WriteFile(dest, remoteWriteEntry(current, storageClass), reader) + if err != nil { + return err + } + return updateLocalEntry(filerClient, dir, current, remoteEntry) +} + +// renameInSource locates the object holding a remote-only rename-in entry's +// content and the client that serves it. A pending-rename marker under this +// mount names a key on this remote; otherwise the renamed-from path is +// resolved through the filer's mount mappings to its own mount's remote +// location, served by this client when the source shares this storage. +func renameInSource( + filerClient filer_pb.FilerClient, + client remote_storage.RemoteStorageClient, + mountedDir string, + remoteStorageMountLocation *remote_pb.RemoteStorageLocation, + current *filer_pb.Entry, + renameSrc util.FullPath, +) (remote_storage.RemoteStorageClient, *remote_pb.RemoteStorageLocation, error) { + if src := util.FullPath(current.Extended[filer.ExtRemoteRenameSourceKey]); strings.HasPrefix(string(src), strings.TrimSuffix(mountedDir, "/")+"/") { + return client, toRemoteStorageLocation(util.FullPath(mountedDir), src, remoteStorageMountLocation), nil + } + mappings, err := readMountMappings(filerClient) + if err != nil { + return nil, nil, err + } + srcMount, srcRemoteLoc, err := filer.FindMountedRemoteMapping(mappings, string(renameSrc)) + if err != nil { + return nil, nil, fmt.Errorf("rename source %s: %w", renameSrc, err) + } + srcLoc := filer.MapFullPathToRemoteStorageLocation(util.FullPath(srcMount), srcRemoteLoc, renameSrc) + if srcRemoteLoc.Name == remoteStorageMountLocation.Name { + return client, srcLoc, nil + } + conf, err := readRemoteStorageConf(filerClient, srcRemoteLoc.Name) + if err != nil { + return nil, nil, fmt.Errorf("rename source %s: %w", renameSrc, err) + } + srcClient, err := remote_storage.GetRemoteStorage(conf) + if err != nil { + return nil, nil, fmt.Errorf("rename source %s: %w", renameSrc, err) + } + return srcClient, srcLoc, nil +} + +// readMountMappings reads the filer's remote mount table, the file +// DetectMountInfo reads at startup, through the event's filer client. +func readMountMappings(filerClient filer_pb.FilerClient) (mappings *remote_pb.RemoteStorageMapping, err error) { + var content []byte + err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { + var readErr error + content, readErr = filer.ReadInsideFiler(context.Background(), client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE) + return readErr + }) + if err != nil && !errors.Is(err, filer_pb.ErrNotFound) { + return nil, err + } + return filer.UnmarshalRemoteStorageMappings(content) +} + +// readRemoteStorageConf reads a remote storage's configuration entry. +func readRemoteStorageConf(filerClient filer_pb.FilerClient, storageName string) (*remote_pb.RemoteConf, error) { + var content []byte + if err := filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { + var readErr error + content, readErr = filer.ReadInsideFiler(context.Background(), client, filer.DirectoryEtcRemote, storageName+filer.REMOTE_STORAGE_CONF_SUFFIX) + return readErr + }); err != nil { + return nil, err + } + conf := &remote_pb.RemoteConf{} + if err := proto.Unmarshal(content, conf); err != nil { + return nil, fmt.Errorf("unmarshal %s/%s: %w", filer.DirectoryEtcRemote, storageName+filer.REMOTE_STORAGE_CONF_SUFFIX, err) + } + return conf, nil +} + // isSuperseded reports whether the filer has moved past the entry an event // described: it is deleted, or it no longer references every chunk the event // named. Those are the chunks the filer deletes when an entry is updated, so diff --git a/weed/command/filer_remote_sync_dir_test.go b/weed/command/filer_remote_sync_dir_test.go index d6a30155ef..5b004bd068 100644 --- a/weed/command/filer_remote_sync_dir_test.go +++ b/weed/command/filer_remote_sync_dir_test.go @@ -413,10 +413,12 @@ func TestIsMetadataOnlyUpdate(t *testing.T) { } // stubFilerClient serves one entry; nil means not found. A non-nil err fails -// every lookup instead. +// every lookup instead. entries, when set, answers lookups by "dir/name" +// before entry is consulted. type stubFilerClient struct { filer_pb.SeaweedFilerClient entry *filer_pb.Entry + entries map[string]*filer_pb.Entry err error updateErr error lookups int @@ -424,11 +426,14 @@ type stubFilerClient struct { updated *filer_pb.UpdateEntryRequest } -func (c *stubFilerClient) LookupDirectoryEntry(context.Context, *filer_pb.LookupDirectoryEntryRequest, ...grpc.CallOption) (*filer_pb.LookupDirectoryEntryResponse, error) { +func (c *stubFilerClient) LookupDirectoryEntry(_ context.Context, req *filer_pb.LookupDirectoryEntryRequest, _ ...grpc.CallOption) (*filer_pb.LookupDirectoryEntryResponse, error) { c.lookups++ if c.err != nil { return nil, c.err } + if c.entries != nil { + return &filer_pb.LookupDirectoryEntryResponse{Entry: c.entries[req.Directory+"/"+req.Name]}, nil + } return &filer_pb.LookupDirectoryEntryResponse{Entry: c.entry}, nil } @@ -2148,7 +2153,7 @@ func TestRenameIntoMountUploadsDespiteTheOldStamp(t *testing.T) { remote := &recordingRemote{} filerClient := &stubFilerClient{entry: newEntry} - if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, resp, true); err != nil { + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, resp, true, "/plain/back.txt"); err != nil { t.Fatal(err) } wantWrite := &remote_pb.RemoteStorageLocation{Name: "b2", Bucket: "bucket", Path: "/lock/back.txt"} @@ -2158,7 +2163,7 @@ func TestRenameIntoMountUploadsDespiteTheOldStamp(t *testing.T) { // a plain create keeps trusting the stamp remote = &recordingRemote{} - if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, resp, false); err != nil { + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, resp, false, ""); err != nil { t.Fatal(err) } if len(remote.writes) != 0 { @@ -2192,7 +2197,7 @@ func TestRenameIntoMountWritesAnEmptyFile(t *testing.T) { } remote := &recordingRemote{} filerClient := &stubFilerClient{entry: entry} - if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn(entry), true); err != nil { + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn(entry), true, "/plain/empty.txt"); err != nil { t.Fatal(err) } if len(remote.writes) != 1 || !proto.Equal(remote.writes[0], wantWrite) { @@ -2208,7 +2213,7 @@ func TestRenameIntoMountWritesAnEmptyFile(t *testing.T) { } remote := &recordingRemote{} filerClient := &stubFilerClient{entry: entry} - if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn(entry), true); err != nil { + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn(entry), true, "/plain/empty.txt"); err != nil { t.Fatal(err) } if len(remote.writes) != 0 { @@ -2227,7 +2232,7 @@ func TestRenameIntoMountWritesAnEmptyFile(t *testing.T) { } remote := &recordingRemote{} filerClient := &stubFilerClient{entry: entry} - if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn(entry), true); err != nil { + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn(entry), true, "/plain/empty.txt"); err != nil { t.Fatal(err) } if len(remote.writes) != 1 || !proto.Equal(remote.writes[0], wantWrite) { @@ -2240,7 +2245,7 @@ func TestRenameIntoMountWritesAnEmptyFile(t *testing.T) { entry := &filer_pb.Entry{Name: "empty.txt", Attributes: &filer_pb.FuseAttributes{Mtime: 1786096669}} remote := &recordingRemote{} filerClient := &stubFilerClient{entry: entry} - if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn(entry), false); err != nil { + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn(entry), false, ""); err != nil { t.Fatal(err) } if len(remote.writes) != 0 { @@ -2249,6 +2254,122 @@ func TestRenameIntoMountWritesAnEmptyFile(t *testing.T) { }) } +// TestSupersededRenameInRecoversTheCurrentEntry covers a rename into the +// mount whose create event the sync reaches only after remote.uncache dropped +// the moved file's chunks: the upload is superseded, and skipping it leaves +// the destination object missing once the source side's delete runs. +func TestSupersededRenameInRecoversTheCurrentEntry(t *testing.T) { + const mountedDir = "/buckets/mount" + mountLoc := &remote_pb.RemoteStorageLocation{Name: "b2", Bucket: "bucket", Path: "/mnt"} + srcMountLoc := &remote_pb.RemoteStorageLocation{Name: "b2", Bucket: "bucket", Path: "/plain-remote"} + + stamp := &filer_pb.RemoteEntry{StorageName: "b2", RemoteETag: "abc", RemoteSize: 4, RemoteMtime: 1786096669, LastLocalSyncTsNs: 1786096669*1e9 + 1} + // the entry as the rename logged it: local chunks plus the stamp earned + // under the source mount + eventEntry := &filer_pb.Entry{ + Name: "a.txt", + Attributes: &filer_pb.FuseAttributes{Mtime: 1786096669, FileSize: 4}, + Chunks: []*filer_pb.FileChunk{{FileId: "1,01111111", Size: 4}}, + RemoteEntry: stamp, + } + // the entry as remote.uncache left it: the chunks are gone and the stamp + // still describes the source mount's object + uncached := &filer_pb.Entry{ + Name: "a.txt", + Attributes: &filer_pb.FuseAttributes{Mtime: 1786096669, FileSize: 4}, + RemoteEntry: stamp, + } + mappingContent, err := proto.Marshal(&remote_pb.RemoteStorageMapping{Mappings: map[string]*remote_pb.RemoteStorageLocation{ + mountedDir: mountLoc, + "/plain": srcMountLoc, + }}) + if err != nil { + t.Fatal(err) + } + mappingEntry := &filer_pb.Entry{Name: filer.REMOTE_STORAGE_MOUNT_FILE, Content: mappingContent} + renameIn := &filer_pb.SubscribeMetadataResponse{ + Directory: mountedDir + "/lock", + EventNotification: &filer_pb.EventNotification{NewParentPath: mountedDir + "/lock", NewEntry: eventEntry}, + } + dest := &remote_pb.RemoteStorageLocation{Name: "b2", Bucket: "bucket", Path: "/mnt/lock/a.txt"} + + newFilerClient := func(entry *filer_pb.Entry) *stubFilerClient { + return &stubFilerClient{entries: map[string]*filer_pb.Entry{ + mountedDir + "/lock/a.txt": proto.Clone(entry).(*filer_pb.Entry), + filer.DirectoryEtcRemote + "/" + filer.REMOTE_STORAGE_MOUNT_FILE: mappingEntry, + }} + } + + t.Run("uncached meanwhile: the source object is copied and the entry stamped", func(t *testing.T) { + remote := &recordingRemote{objects: map[string][]byte{"/plain-remote/a.txt": []byte("data")}} + filerClient := newFilerClient(uncached) + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn, true, "/plain/a.txt"); err != nil { + t.Fatal(err) + } + if len(remote.writes) != 1 || !proto.Equal(remote.writes[0], dest) { + t.Fatalf("writes = %+v, want the source copied to %s", remote.writes, remote_storage.FormatLocation(dest)) + } + if string(remote.written[0]) != "data" { + t.Errorf("written = %q, want the source object's bytes", remote.written[0]) + } + if filerClient.updates != 1 { + t.Errorf("updates = %d, want the entry stamped with the copy", filerClient.updates) + } + }) + + t.Run("the destination already holds it: only a stamp", func(t *testing.T) { + remote := &recordingRemote{objects: map[string][]byte{"/mnt/lock/a.txt": []byte("data")}, statETags: map[string]string{"/mnt/lock/a.txt": "abc"}} + filerClient := newFilerClient(uncached) + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn, true, "/plain/a.txt"); err != nil { + t.Fatal(err) + } + if len(remote.writes) != 0 { + t.Errorf("writes = %+v, want none once the destination holds the content", remote.writes) + } + if filerClient.updates != 1 { + t.Errorf("updates = %d, want the entry stamped", filerClient.updates) + } + }) + + t.Run("no object on either side: the event fails", func(t *testing.T) { + remote := &recordingRemote{objects: map[string][]byte{}} + filerClient := newFilerClient(uncached) + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn, true, "/plain/a.txt"); err == nil { + t.Fatal("want the event to fail and hold the offset") + } + }) + + t.Run("a pending marker under the mount names the source", func(t *testing.T) { + marked := proto.Clone(uncached).(*filer_pb.Entry) + marked.Extended = map[string][]byte{filer.ExtRemoteRenameSourceKey: []byte(mountedDir + "/lock/old.txt")} + remote := &recordingRemote{objects: map[string][]byte{"/mnt/lock/old.txt": []byte("data")}} + filerClient := newFilerClient(marked) + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn, true, "/plain/a.txt"); err != nil { + t.Fatal(err) + } + if len(remote.writes) != 1 || !proto.Equal(remote.writes[0], dest) { + t.Fatalf("writes = %+v, want the marked source copied to %s", remote.writes, remote_storage.FormatLocation(dest)) + } + }) + + t.Run("rewritten since: the superseding event uploads", func(t *testing.T) { + rewritten := &filer_pb.Entry{ + Name: "a.txt", + Attributes: &filer_pb.FuseAttributes{Mtime: 1786096700, FileSize: 4}, + Chunks: []*filer_pb.FileChunk{{FileId: "2,02222222", Size: 4}}, + RemoteEntry: stamp, + } + remote := &recordingRemote{} + filerClient := newFilerClient(rewritten) + if err := processCreateEvent(filerClient, filerClient, "", remote, mountedDir, mountLoc, renameIn, true, "/plain/a.txt"); err != nil { + t.Fatal(err) + } + if len(remote.writes) != 0 { + t.Errorf("writes = %+v, want the rewrite's own event to upload", remote.writes) + } + }) +} + func TestIsSystemPath(t *testing.T) { tests := []struct { dir string