Files
seaweedfs/weed/filer/filer_notify_read_peer_test.go
T
Chris LuandClaude Opus 5.5 3d076dfcdf filer: aggregated metadata stream reads the log files of peers with their own store (#13116)
* filer: pass the reader's context to persisted metadata log listings

The day and minute listings behind ReadPersistedLogBuffer used
context.Background(), so nothing scoped to the subscription reached them.
List through one helper that takes the caller's context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: aggregated metadata stream reads the log files of peers with their own store

A filer with its own store never holds another filer's persisted
metadata log: log file writes emit no events and the bootstrap traverse
skips the log directory. The aggregated stream read history from the
local log directory and the in-memory aggregate ring only, and the ring
restarts empty, refilled from each peer's saved offset. A subscriber whose
cursor was older than that missed every event a peer recorded before it,
for example the S3 lifecycle worker replaying expirations.

Aggregated reads now also list the log directories of followed peers not
known to share this filer's store, merged by file name, so the existing
ordered reader and chunk-ref path serve their history. Local streams, which
peers aggregate from, stay local.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: bound peer log listings and fail the read when one fails

Each peer listing gets its own deadline, so a peer that stalls mid-listing
cannot hold the read. A failed listing now fails the read instead of
silently hiding that peer's persisted files; peers already retired as
unreachable are left out. Peers are asked from the same start name the
caller uses for its own store instead of the whole directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep a still-unreachable peer out of log listings after its mark expires

A followed peer that keeps refusing connections is marked removed once, and
the mark is dropped with its resume point an hour later. It was then listed
again and its failing listing ended every aggregated persisted-log read.
Leave out any peer refusing connections for peerUnreachableRemovalAfter,
marked or not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: subscribe tests track their peers on the test filer's store

Aggregated reads now list the log files of followed peers with their own
store. The subscribe tests' peers have no server and share the test
filer's store, so record that instead of dialing them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 17:46:29 -08:00

183 lines
6.4 KiB
Go

package filer
import (
"context"
"os"
"sort"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
)
func peerTestLogFile(day, name string, withChunks bool) *Entry {
entry := &Entry{FullPath: util.NewFullPath(string(util.NewFullPath(SystemLogDir, day)), name)}
if withChunks {
entry.Chunks = []*filer_pb.FileChunk{{FileId: "1,0a", Size: 10}}
}
return entry
}
func peerTestLogDay(day string) *Entry {
return &Entry{FullPath: util.NewFullPath(SystemLogDir, day), Attr: Attr{Mode: os.ModeDir | 0755}}
}
// newPeerLogTestFiler returns a filer whose own store holds its 12-30 file and
// that follows a peer with its own store, a peer on its store, and itself.
// Only the first is asked for its log directories.
func newPeerLogTestFiler(t *testing.T, withChunks bool) *Filer {
store := newStubFilerStore()
f := newReplayTestFiler(store)
store.entries[string(SystemLogDir)] = &Entry{FullPath: SystemLogDir, Attr: Attr{Mode: os.ModeDir | 0755}}
for _, e := range []*Entry{peerTestLogDay("2026-10-25"), peerTestLogFile("2026-10-25", "12-30.0000abcd", withChunks)} {
store.entries[string(e.FullPath)] = e
}
own := pb.ServerAddress("own:8888")
shared := pb.ServerAddress("shared:8888")
self := pb.ServerAddress("self:8888")
ma := NewMetaAggregator(f, self, nil)
for _, peer := range []pb.ServerAddress{own, shared, self} {
ma.peerChans[peer] = make(chan struct{})
}
ma.peerSharesStore[shared] = true
peerDirs := map[util.FullPath][]*Entry{
SystemLogDir: {peerTestLogDay("2026-10-24"), peerTestLogDay("2026-10-25")},
util.NewFullPath(SystemLogDir, "2026-10-24"): {peerTestLogFile("2026-10-24", "23-59.0000beef", withChunks)},
util.NewFullPath(SystemLogDir, "2026-10-25"): {
peerTestLogFile("2026-10-25", "12-28.0000beef", withChunks),
peerTestLogFile("2026-10-25", "12-29.0000beef", withChunks),
peerTestLogFile("2026-10-25", "12-30.0000abcd", withChunks),
peerTestLogFile("2026-10-25", "12-31.0000beef", withChunks),
},
}
ma.peerLogDirLister = func(ctx context.Context, peer pb.ServerAddress, dir util.FullPath, startFileName string, inclusive bool) ([]*Entry, error) {
if peer != own {
t.Errorf("listed %s on %s, which needs no listing", dir, peer)
}
return peerDirs[dir], nil
}
f.MetaAggregator = ma
return f
}
func refNames(refs []*filer_pb.LogFileChunkRef) []string {
var names []string
for _, ref := range refs {
names = append(names, time.Unix(0, ref.FileTsNs).UTC().Format("15-04")+"."+ref.FilerId)
}
sort.Strings(names)
return names
}
// TestAggregatedLogFileRefsIncludePeerFiles: a peer with its own store keeps
// its log files there, so the aggregated read lists them from the peer, from
// the scan start on and once per name. A local read stays local.
func TestAggregatedLogFileRefsIncludePeerFiles(t *testing.T) {
f := newPeerLogTestFiler(t, true)
start := log_buffer.NewMessagePosition(listingTestCursor.UnixNano(), -2)
refs, _, err := f.CollectLogFileRefs(WithPeerLogFiles(context.Background()), start, 0)
if err != nil {
t.Fatalf("CollectLogFileRefs: %v", err)
}
want := []string{"12-29.0000beef", "12-30.0000abcd", "12-31.0000beef"}
if got := refNames(refs); !equalStrings(got, want) {
t.Fatalf("aggregated refs = %v, want %v", got, want)
}
refs, _, err = f.CollectLogFileRefs(context.Background(), start, 0)
if err != nil {
t.Fatalf("CollectLogFileRefs: %v", err)
}
if got := refNames(refs); !equalStrings(got, []string{"12-30.0000abcd"}) {
t.Fatalf("local refs = %v, want only the own file", got)
}
}
// TestAggregatedPersistedLogScanIncludesPeerFiles: the same for the pass that
// reads the persisted log for an aggregated subscription.
func TestAggregatedPersistedLogScanIncludesPeerFiles(t *testing.T) {
// cached mode: the files hold no chunks, so nothing is read from volumes
f := newPeerLogTestFiler(t, false)
f.persistedLogCache = newPersistedLogCache(1 << 20)
start := log_buffer.NewMessagePosition(listingTestCursor.UnixNano(), -2)
for _, tc := range []struct {
ctx context.Context
want []string
}{
{WithPeerLogFiles(context.Background()), []string{"0000abcd", "0000beef"}},
{context.Background(), []string{"0000abcd"}},
} {
v, err := f.collectPersistedLogBuffer(tc.ctx, start, 0)
if err != nil {
t.Fatalf("collectPersistedLogBuffer: %v", err)
}
var got []string
for filerId := range v.perFilerIteratorMap {
got = append(got, filerId)
}
sort.Strings(got)
if !equalStrings(got, tc.want) {
t.Fatalf("filers read = %v, want %v", got, tc.want)
}
}
}
func equalStrings(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// TestAggregatedLogListingFailsOnPeerError: a peer listing that fails fails
// the read instead of hiding that peer's files, unless the peer was already
// retired as unreachable, also once its removal mark expired.
func TestAggregatedLogListingFailsOnPeerError(t *testing.T) {
f := newPeerLogTestFiler(t, true)
ma := f.MetaAggregator
ma.peerLogDirLister = func(ctx context.Context, peer pb.ServerAddress, dir util.FullPath, startFileName string, inclusive bool) ([]*Entry, error) {
return nil, context.DeadlineExceeded
}
start := log_buffer.NewMessagePosition(listingTestCursor.UnixNano(), -2)
if _, _, err := f.CollectLogFileRefs(WithPeerLogFiles(context.Background()), start, 0); err == nil {
t.Fatalf("CollectLogFileRefs succeeded with a peer it could not list")
}
own := pb.ServerAddress("own:8888")
ma.peerUnreachableSinceNs[own] = time.Now().UnixNano()
if _, _, err := f.CollectLogFileRefs(WithPeerLogFiles(context.Background()), start, 0); err == nil {
t.Fatalf("CollectLogFileRefs succeeded with a peer unreachable only briefly")
}
for _, retire := range []struct {
name string
mark func()
}{
{"marked removed", func() { ma.peerRemovedAtNs[own] = time.Now().UnixNano() }},
{"mark expired, still unreachable", func() {
delete(ma.peerRemovedAtNs, own)
ma.peerUnreachableSinceNs[own] = time.Now().Add(-peerUnreachableRemovalAfter - time.Hour).UnixNano()
}},
} {
retire.mark()
refs, _, err := f.CollectLogFileRefs(WithPeerLogFiles(context.Background()), start, 0)
if err != nil {
t.Fatalf("%s: CollectLogFileRefs: %v", retire.name, err)
}
if got := refNames(refs); !equalStrings(got, []string{"12-30.0000abcd"}) {
t.Fatalf("%s: refs = %v, want only the own file", retire.name, got)
}
}
}