From 8ff23c85b99c3ee755e1ed516239a8073d090851 Mon Sep 17 00:00:00 2001 From: Martin Lettry Date: Sun, 29 Nov 2026 18:18:32 +0100 Subject: [PATCH] filer: cap list snapshots at the peers' delivery watermark A listing's snapshot only accounted for this filer's in-flight writes. A peer on the same store can have stamped a write below it that is not committed yet; the listing misses it and the client drops its event as already listed, leaving a stale entry (a mount reads a just-closed file as empty) until the next listing. --- weed/server/filer_grpc_server.go | 15 +++++++++- .../filer_grpc_server_list_snapshot_test.go | 29 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/weed/server/filer_grpc_server.go b/weed/server/filer_grpc_server.go index 8dc1352c97..7218d03f58 100644 --- a/weed/server/filer_grpc_server.go +++ b/weed/server/filer_grpc_server.go @@ -62,6 +62,19 @@ func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.L }, nil } +// listSnapshotTsNs is the position a listing claims to be complete through. +// The local claim only accounts for this filer's in-flight writes; a peer on +// the same store may have stamped a write below it that is not committed yet. +// Capping at the aggregated subscription's hold keeps such a write above the +// snapshot, so the client applies its event instead of dropping it as listed. +func (fs *FilerServer) listSnapshotTsNs(nowNs int64) int64 { + snapshotTsNs := fs.filer.LocalDeliveredThroughTsNs(nowNs) + if ma := fs.filer.MetaAggregator; ma != nil { + snapshotTsNs = min(snapshotTsNs, resolveAggReadHoldTsNs(ma.PeerLowWatermarkTsNs(), nowNs, metadataGapSettledHorizon)) + } + return snapshotTsNs +} + func (fs *FilerServer) ListEntries(req *filer_pb.ListEntriesRequest, stream filer_pb.SeaweedFiler_ListEntriesServer) (err error) { glog.V(4).Infof("ListEntries %v", req) @@ -80,7 +93,7 @@ func (fs *FilerServer) ListEntries(req *filer_pb.ListEntriesRequest, stream file includeLastFile := req.InclusiveStartFrom snapshotTsNs := req.SnapshotTsNs if snapshotTsNs == 0 { - snapshotTsNs = fs.filer.LocalDeliveredThroughTsNs(time.Now().UnixNano()) + snapshotTsNs = fs.listSnapshotTsNs(time.Now().UnixNano()) } sentSnapshot := false var listErr error diff --git a/weed/server/filer_grpc_server_list_snapshot_test.go b/weed/server/filer_grpc_server_list_snapshot_test.go index 6269388bc2..daf6be7fa7 100644 --- a/weed/server/filer_grpc_server_list_snapshot_test.go +++ b/weed/server/filer_grpc_server_list_snapshot_test.go @@ -4,10 +4,13 @@ import ( "context" "strconv" "testing" + "time" "google.golang.org/grpc" "google.golang.org/grpc/metadata" + "github.com/seaweedfs/seaweedfs/weed/filer" + "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" ) @@ -72,3 +75,29 @@ func TestListEntriesSnapshotBelowUnfinishedAndLaterStamps(t *testing.T) { t.Fatalf("stamp %d issued after the listing is not above its snapshot %d", laterTsNs, snapshotTsNs) } } + +// A peer on the same store commits writes this filer does not track in +// flight. Until the peer's stream has delivered past a write's stamp, the +// write may be missing from the store, so the snapshot must stay below the +// peers' delivery watermark too, or the client drops the write's event. +func TestListEntriesSnapshotBelowPeerWatermark(t *testing.T) { + store := newRenameTestStore() + store.entries["/dir"] = newDirectoryEntry("/dir", 100) + f := newRenameTestFiler(t, store) + fs := &FilerServer{filer: f, option: &FilerOption{DirListingLimit: 1000}} + ma := filer.NewMetaAggregator(f, "self:8888", nil) + f.MetaAggregator = ma + t.Cleanup(ma.Shutdown) + peer := pb.ServerAddress("peer:8888") + ma.TrackPeerForTesting(peer) + peerTsNs := time.Now().Add(-time.Second).UnixNano() + ma.ReportPeerWatermarksForTesting(peer, peerTsNs, peerTsNs) + + stream := &captureListStream{} + if err := fs.ListEntries(&filer_pb.ListEntriesRequest{Directory: "/dir"}, stream); err != nil { + t.Fatalf("ListEntries: %v", err) + } + if snapshotTsNs := stream.snapshotTsNs(t); snapshotTsNs > peerTsNs { + t.Fatalf("snapshot %d is past the peer's delivery watermark %d", snapshotTsNs, peerTsNs) + } +}