diff --git a/weed/filer/filer_notify.go b/weed/filer/filer_notify.go index 6ffde937a0..a1f47b892b 100644 --- a/weed/filer/filer_notify.go +++ b/weed/filer/filer_notify.go @@ -143,6 +143,17 @@ func (t *metaLogInflight) nextLocked() int64 { return tsNs } +// delivered keeps later stamps and fences above a position the buffer +// assigned: it renumbers an event appended out of stamp order past the +// last position, which can be past every stamp issued so far. +func (t *metaLogInflight) delivered(positionNs int64) { + t.Lock() + defer t.Unlock() + if positionNs > t.lastStampNs { + t.lastStampNs = positionNs + } +} + // done removes a stamp once the event has been appended to the buffer. func (t *metaLogInflight) done(tsNs int64) { t.Lock() @@ -337,6 +348,9 @@ func (f *Filer) logMetaEvent(ctx context.Context, event *filer_pb.SubscribeMetad glog.Errorf("failed to add data to log buffer for %s: %v", event.Directory, err) return } + // Before a locked writer releases its path, so a fenced read of that path + // lands above the position its event was delivered at. + f.metaLogInflight.delivered(tsNs) notifyRenameTarget(f.LocalMetaLogBuffer, event) // The in-memory copy now carries the position the stream assigns it, the // same value subscribers will see delivered. diff --git a/weed/filer/filer_notify_inflight_test.go b/weed/filer/filer_notify_inflight_test.go index ed5ce35838..ee617aa79b 100644 --- a/weed/filer/filer_notify_inflight_test.go +++ b/weed/filer/filer_notify_inflight_test.go @@ -161,6 +161,42 @@ func TestMetaLogInflightFenceOrdersWithStamps(t *testing.T) { } } +// Events appended out of stamp order are renumbered by the buffer past the +// last stamp; a fence drawn afterwards must still sit above every delivered +// position, also with the wall clock behind the log. +func TestLogFenceAboveRenumberedPositions(t *testing.T) { + f, _ := newTestFilerWithStubStore(t) + f.LocalDeliveredThroughTsNs(time.Now().Add(time.Hour).UnixNano()) + + var entries []*Entry + var lastStamp int64 + for _, name := range []string{"a", "b", "c"} { + entry := &Entry{FullPath: util.FullPath("/dir/" + name), Attr: Attr{Mode: 0o644}} + lastStamp = f.StampEntryCommitTsNs(entry) + entries = append(entries, entry) + } + var maxPosition int64 + for i := len(entries) - 1; i >= 0; i-- { + ctx, sink := WithMetadataEventSink(context.Background()) + f.NotifyUpdateEvent(ctx, nil, entries[i], false, false, nil) + if sink.Last() == nil { + t.Fatalf("no event for %s", entries[i].FullPath) + } + maxPosition = max(maxPosition, sink.Last().TsNs) + } + if maxPosition <= lastStamp { + t.Fatalf("appends were not renumbered: max position %d, last stamp %d", maxPosition, lastStamp) + } + if fence := f.LogFenceTsNs(); fence <= maxPosition { + t.Fatalf("fence %d not above delivered position %d", fence, maxPosition) + } + entry := &Entry{FullPath: "/dir/d", Attr: Attr{Mode: 0o644}} + if next := f.StampEntryCommitTsNs(entry); next <= maxPosition { + t.Fatalf("next stamp %d not above delivered position %d", next, maxPosition) + } + f.ReleaseEntryCommitTsNs(entry.CommitTsNs) +} + // TestSuppressedCreateReleasesParentDirectoryStamps pins the stamps of the // parent directories a rename creates or promotes on the way to its target: // the rename's covering event names the moved entry only, so a parent stamp