From 0b3bb80be85506512b5b1384ef5ff445cd6871fe Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 27 Nov 2026 12:31:08 -0800 Subject: [PATCH] filer.sync: retry an update whose destination lookup failed transiently FilerSink.UpdateEntry reported every lookup error as a missing entry, so the replicator and filer.sync deleted the destination key and created it again. For a directory, a canceled or unavailable lookup removed it with everything under it. Only a real not-found now lets the caller recreate the key; other lookup errors are returned for retry. Co-Authored-By: Claude Opus 5.5 --- weed/replication/sink/filersink/filer_sink.go | 18 ++++-- .../sink/filersink/filer_sink_lookup_test.go | 55 +++++++++++++++++++ 2 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 weed/replication/sink/filersink/filer_sink_lookup_test.go diff --git a/weed/replication/sink/filersink/filer_sink.go b/weed/replication/sink/filersink/filer_sink.go index ef0670788c..a93de1a65e 100644 --- a/weed/replication/sink/filersink/filer_sink.go +++ b/weed/replication/sink/filersink/filer_sink.go @@ -290,10 +290,7 @@ func (fs *FilerSink) entryMissing(key string) (bool, error) { if lookupErr == nil { return nil } - // The string check is a deliberate compatibility fallback: a cross-cluster - // gRPC error often arrives as a plain status string that no longer wraps the - // filer_pb.ErrNotFound sentinel, so errors.Is alone would miss it. - if errors.Is(lookupErr, filer_pb.ErrNotFound) || strings.Contains(lookupErr.Error(), filer_pb.ErrNotFound.Error()) { + if isEntryNotFound(lookupErr) { missing = true return nil } @@ -302,6 +299,14 @@ func (fs *FilerSink) entryMissing(key string) (bool, error) { return missing, err } +// isEntryNotFound reports a lookup not-found. The string check is a deliberate +// compatibility fallback: a cross-cluster gRPC error often arrives as a plain +// status string that no longer wraps the filer_pb.ErrNotFound sentinel, so +// errors.Is alone would miss it. +func isEntryNotFound(err error) bool { + return errors.Is(err, filer_pb.ErrNotFound) || strings.Contains(err.Error(), filer_pb.ErrNotFound.Error()) +} + func (fs *FilerSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error { return fs.createEntry(key, entry, signatures, true) } @@ -420,7 +425,10 @@ func (fs *FilerSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParent }) if err != nil { - return false, fmt.Errorf("lookup %s: %w", key, err) + // Only a real not-found lets the caller delete and recreate the key; any + // other lookup error must be retried, or a transient failure would wipe + // a directory with everything under it. + return !isEntryNotFound(err), fmt.Errorf("lookup %s: %w", key, err) } glog.V(4).Infof("entry %s: old %q new %q existing %q", key, oldEntry.GetName(), newEntry.GetName(), existingEntry.GetName()) diff --git a/weed/replication/sink/filersink/filer_sink_lookup_test.go b/weed/replication/sink/filersink/filer_sink_lookup_test.go new file mode 100644 index 0000000000..d54f99a7ac --- /dev/null +++ b/weed/replication/sink/filersink/filer_sink_lookup_test.go @@ -0,0 +1,55 @@ +package filersink + +import ( + "context" + "net" + "testing" + + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/status" +) + +type lookupErrFiler struct { + filer_pb.UnimplementedSeaweedFilerServer + err error +} + +func (f *lookupErrFiler) LookupDirectoryEntry(context.Context, *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) { + return nil, f.err +} + +func TestUpdateEntryRecreatesOnlyOnNotFound(t *testing.T) { + for _, tc := range []struct { + name string + err error + wantFound bool + }{ + {"not found", status.Error(codes.NotFound, filer_pb.ErrNotFound.Error()), false}, + {"canceled", status.Error(codes.Canceled, "context canceled"), true}, + {"unavailable", status.Error(codes.Unavailable, "connection refused"), true}, + } { + t.Run(tc.name, func(t *testing.T) { + lis, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + srv := grpc.NewServer() + filer_pb.RegisterSeaweedFilerServer(srv, &lookupErrFiler{err: tc.err}) + go srv.Serve(lis) + defer srv.Stop() + + fs := &FilerSink{grpcAddress: lis.Addr().String(), grpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials())} + dir := &filer_pb.Entry{Name: "object.versions", IsDirectory: true} + found, err := fs.UpdateEntry("/buckets/b/object.versions", dir, "/buckets/b", dir, true, nil) + if err == nil { + t.Fatal("expected the lookup error") + } + if found != tc.wantFound { + t.Fatalf("foundExistingEntry = %v, want %v (err %v)", found, tc.wantFound, err) + } + }) + } +}