From df2fa630c110e3ff7487e71c848c55ee79e60107 Mon Sep 17 00:00:00 2001 From: Martin Lettry <59311729+TLGINO@users.noreply.github.com> Date: Tue, 1 Dec 2026 00:28:08 +0100 Subject: [PATCH] mount: fence events below a cached entry's own commit stamp (#13152) A listing snapshot capped at the peers' delivery watermark can trail entries already in the store. A delayed event older than the listed version then rolled the entry back, e.g. a completed file to its empty create, until the newer event arrived. A live entry now counts as versioned at least at its CommitTsNs. Co-authored-by: Chris Lu Co-authored-by: Claude Opus 5.5 --- weed/mount/meta_cache/meta_cache.go | 8 ++- .../mount/meta_cache/meta_cache_apply_test.go | 61 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/weed/mount/meta_cache/meta_cache.go b/weed/mount/meta_cache/meta_cache.go index e93c9a6290..dbbe8c83a7 100644 --- a/weed/mount/meta_cache/meta_cache.go +++ b/weed/mount/meta_cache/meta_cache.go @@ -647,9 +647,15 @@ func (mc *MetaCache) entryVersionBlocksLocked(ctx context.Context, fp util.FullP // event can correct it. return false } - if !tombstone && !mc.entryExistsLocked(ctx, fp) { + entry, err := mc.localStore.FindEntry(ctx, fp) + live := err == nil && entry != nil && !isTtlExpired(entry) + if !tombstone && !live { recordTsNs = 0 } + // A live entry reflects its own commit, even under an older listing floor. + if live && entry.CommitTsNs > recordTsNs { + recordTsNs = entry.CommitTsNs + } floorTsNs := mc.entryVersionFloorLocked(fp, recordTsNs) if removal && !tombstone { return floorTsNs > tsNs diff --git a/weed/mount/meta_cache/meta_cache_apply_test.go b/weed/mount/meta_cache/meta_cache_apply_test.go index 2e90ea6aaf..eae43d809c 100644 --- a/weed/mount/meta_cache/meta_cache_apply_test.go +++ b/weed/mount/meta_cache/meta_cache_apply_test.go @@ -957,3 +957,64 @@ func TestUnversionedWriteDoesNotInheritDirectoryFloor(t *testing.T) { } mc.WaitForEntryInvalidations() } + +// A listing snapshot can trail the listed entries: a peer's held watermark +// caps it below writes already in the store. A delayed older event must not +// roll a listed entry back to the state it replaced. +func TestBuildKeepsListedEntryNewerThanSnapshot(t *testing.T) { + mc, _, _, _ := newTestMetaCache(t, map[util.FullPath]bool{ + "/": true, + "/dir": true, + }) + defer mc.Shutdown() + + if err := mc.BeginDirectoryBuild(context.Background(), util.FullPath("/dir")); err != nil { + t.Fatalf("begin build: %v", err) + } + if err := mc.doBatchInsertEntries(context.Background(), []*filer.Entry{{ + FullPath: "/dir/file", + Attr: filer.Attr{ + Crtime: time.Unix(1, 0), + Mtime: time.Unix(1, 0), + Mode: 0100644, + FileSize: 300, + }, + CommitTsNs: 3000, + }}); err != nil { + t.Fatalf("batch insert: %v", err) + } + if err := mc.CompleteDirectoryBuild(context.Background(), util.FullPath("/dir"), 2000, nil); err != nil { + t.Fatalf("complete build: %v", err) + } + + apply := func(tsNs int64, oldEntry, newEntry *filer_pb.Entry) { + t.Helper() + resp := &filer_pb.SubscribeMetadataResponse{ + Directory: "/dir", + TsNs: tsNs, + EventNotification: &filer_pb.EventNotification{ + OldEntry: oldEntry, + NewEntry: newEntry, + NewParentPath: "/dir", + }, + } + if err := mc.ApplyMetadataResponse(context.Background(), resp, SubscriberMetadataResponseApplyOptions); err != nil { + t.Fatalf("apply event at %d: %v", tsNs, err) + } + } + wantSize := func(want uint64, why string) { + t.Helper() + entry, _, err := mc.FindEntry(context.Background(), util.FullPath("/dir/file")) + if err != nil || entry.FileSize != want { + t.Fatalf("entry = %+v, %v; want size %d (%s)", entry, err, want, why) + } + } + + apply(2500, nil, &filer_pb.Entry{Name: "file", CommitTsNs: 2500, Attributes: &filer_pb.FuseAttributes{FileMode: 0100644}}) + wantSize(300, "the create below the listed commit is already reflected") + apply(2500, &filer_pb.Entry{Name: "file"}, nil) + wantSize(300, "a removal below the listed commit is already reflected") + apply(3500, &filer_pb.Entry{Name: "file"}, &filer_pb.Entry{Name: "file", CommitTsNs: 3500, Attributes: &filer_pb.FuseAttributes{FileSize: 500, FileMode: 0100644}}) + wantSize(500, "a change past the listed commit applies") + mc.WaitForEntryInvalidations() +}