Files
seaweedfs/weed/filer/filer_notify_unflushed_test.go
T
Chris LuandClaude Opus 5.5 ff9c9ca007 filer: keep the metadata log a shutdown could not flush and restore it on start (#13470)
* filer: keep the metadata log a shutdown could not flush and restore it on start

When the shutdown flush budget runs out, the events still in the log
buffer were dropped, so a peer or subscriber that had not read them yet
never got them. Keep the unflushed records in the filer store instead,
in parts small enough for stores that cap value size, and put them back
into the log buffer with their original timestamps when the store is
set, before the filer serves. The buffer then flushes them as usual.

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

* filer: keep the unflushed metadata log per address on a shared store

Filers sharing one store would overwrite and replay each other's kept
log under one key. Once a peer has been seen on this filer's store, key
the kept log by the filer's address; a store of its own keeps the
address-free key, so a filer restarting under a new address (a new pod
IP) still finds what it left.

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

* filer: drop the restored metadata log copy only once a flush covers it

The kept copy was deleted as soon as its records were back in the log
buffer, so a crash before the next flush lost them again. Keep it until
a flush covers the last restored entry, and let the next shutdown's
kept log replace it, since that shutdown's flushes and kept tail then
hold every restored record.

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

* filer: do not count the subscription to itself as a peer on the store

Every filer follows its own log, and that subscription always reports
the filer's own store, so every store looked shared and the kept log
was keyed by address even on an embedded store, where a new pod IP
then missed it.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 11:19:09 -08:00

194 lines
6.4 KiB
Go

package filer
import (
"fmt"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/cluster/lock_manager"
"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"
)
// restartOnStore starts a filer on store whose log buffer hands what it
// flushes to the returned function, which stops the buffer and decodes it.
func restartOnStore(t *testing.T, store FilerStore, host pb.ServerAddress) (*Filer, func() []*filer_pb.LogEntry) {
t.Helper()
f := NewFiler(pb.ServerDiscovery{}, nil, host, "", "", "", "", 255, nil)
f.LocalMetaLogBuffer.ShutdownLogBuffer()
f.LocalMetaLogBuffer.WaitForShutdown()
var flushed []byte
f.LocalMetaLogBuffer = log_buffer.NewLogBuffer("local", time.Hour, func(_ *log_buffer.LogBuffer, _, stopTime time.Time, buf []byte, _, _ int64) {
flushed = append(flushed, buf...)
f.noteMetaLogFlushed(stopTime.UnixNano())
}, nil, nil)
t.Cleanup(func() {
close(f.deletionQuit)
f.flushCancel()
})
f.SetStore(store)
return f, func() []*filer_pb.LogEntry {
f.LocalMetaLogBuffer.ShutdownLogBuffer()
f.LocalMetaLogBuffer.WaitForShutdown()
entries, _, err := decodeLogRecords(flushed)
if err != nil {
t.Fatalf("decode restored log: %v", err)
}
return entries
}
}
// followSelf gives f the aggregator state of its subscription to itself,
// which always reports this filer's own store.
func followSelf(f *Filer, self pb.ServerAddress) {
stop := make(chan struct{})
f.MetaAggregator = &MetaAggregator{
self: self,
peerChans: map[pb.ServerAddress]chan struct{}{self: stop},
peerSharesStore: map[pb.ServerAddress]bool{},
}
f.MetaAggregator.notePeerStore(self, stop, true)
}
func keptMetaLogKeys(store *stubFilerStore) (keys []string) {
store.mu.Lock()
defer store.mu.Unlock()
for key := range store.kv {
if strings.HasPrefix(key, KvKeyUnflushedMetaLog) {
keys = append(keys, key)
}
}
return keys
}
func addMetaLogEvent(t *testing.T, f *Filer, data string) int64 {
t.Helper()
tsNs, err := f.LocalMetaLogBuffer.AddDataToBufferAndGetTs([]byte("/dir"), []byte(data), 0)
if err != nil {
t.Fatal(err)
}
return tsNs
}
func TestShutdownKeepsUnflushedMetaLogForNextStart(t *testing.T) {
store := newStubFilerStore()
first := NewFiler(pb.ServerDiscovery{}, nil, "10.0.0.1:8888", "", "", "", "", 255, nil)
first.SetStore(store)
followSelf(first, "10.0.0.1:8888")
var wantTs []int64
var wantData []string
for i := 0; i < 3; i++ {
wantData = append(wantData, fmt.Sprintf("event %d", i))
wantTs = append(wantTs, addMetaLogEvent(t, first, wantData[i]))
}
// No master to assign a volume, and the shutdown flush budget already spent.
first.flushCancel()
first.Shutdown()
if len(keptMetaLogKeys(store)) == 0 {
t.Fatal("shutdown kept no unflushed metadata log")
}
// The address changes on every restart, as a pod IP does, and the second
// start cannot flush either: its shutdown keeps the restored events again.
second := NewFiler(pb.ServerDiscovery{}, nil, "10.0.0.2:8888", "", "", "", "", 255, nil)
second.SetStore(store)
if len(keptMetaLogKeys(store)) == 0 {
t.Fatal("restored copy dropped before a flush wrote it")
}
wantData = append(wantData, "event after restart")
wantTs = append(wantTs, addMetaLogEvent(t, second, wantData[3]))
second.flushCancel()
second.Shutdown()
third, stop := restartOnStore(t, store, "10.0.0.3:8888")
if fence := third.LogFenceTsNs(); fence <= wantTs[len(wantTs)-1] {
t.Errorf("new stamps start at %d, not above the restored %d", fence, wantTs[len(wantTs)-1])
}
got := stop()
if len(got) != len(wantTs) {
t.Fatalf("restored %d entries, want %d", len(got), len(wantTs))
}
for i, entry := range got {
if entry.TsNs != wantTs[i] || string(entry.Data) != wantData[i] || string(entry.Key) != "/dir" {
t.Errorf("entry %d = ts %d key %q data %q, want ts %d data %q", i, entry.TsNs, entry.Key, entry.Data, wantTs[i], wantData[i])
}
}
if keys := keptMetaLogKeys(store); len(keys) != 0 {
t.Errorf("flushed restored log left %v behind", keys)
}
}
func TestUnflushedMetaLogStaysWithItsFilerOnSharedStore(t *testing.T) {
store := newStubFilerStore()
wrapped := NewFilerStoreWrapper(store)
shared := &Filer{Store: wrapped, Dlm: lock_manager.NewDistributedLockManager("10.0.0.1:8888"), MetaAggregator: &MetaAggregator{}}
shared.MetaAggregator.storeShared.Store(true)
record := encodeLogRecord(t, &filer_pb.LogEntry{TsNs: 7, Key: []byte("/dir"), Data: []byte("event")})
if err := shared.keepUnflushedMetaLog(record); err != nil {
t.Fatal(err)
}
if _, stop := restartOnStore(t, store, "10.0.0.2:8888"); len(stop()) != 0 {
t.Error("a peer on the shared store restored another filer's log")
}
if _, stop := restartOnStore(t, store, "10.0.0.1:8888"); len(stop()) != 1 {
t.Error("the filer did not restore its own log")
}
}
func TestUnflushedMetaLogSplitsIntoParts(t *testing.T) {
store := newStubFilerStore()
f := &Filer{Store: NewFilerStoreWrapper(store)}
var buf []byte
var n int
for len(buf) <= 2*unflushedMetaLogPartSize {
buf = append(buf, encodeLogRecord(t, &filer_pb.LogEntry{TsNs: int64(n + 1), Data: make([]byte, 1000)})...)
n++
}
half := lastLogRecordEnd(buf, len(buf)/2)
if err := f.keepUnflushedMetaLog(buf[:half]); err != nil {
t.Fatal(err)
}
if err := f.keepUnflushedMetaLog(buf[half:]); err != nil {
t.Fatal(err)
}
for key, value := range store.kv {
if len(value) > unflushedMetaLogPartSize {
t.Errorf("%s holds %d bytes", key, len(value))
}
}
if _, stop := restartOnStore(t, store, "10.0.0.1:8888"); len(stop()) != n {
t.Errorf("restored a different number of entries than the %d kept", n)
}
}
func TestLastLogRecordEndKeepsRecordCutBySize(t *testing.T) {
first := encodeLogRecord(t, &filer_pb.LogEntry{TsNs: 1, Data: []byte("small")})
second := encodeLogRecord(t, &filer_pb.LogEntry{TsNs: 2, Data: make([]byte, 100)})
buf := append(append([]byte{}, first...), second...)
for _, tc := range []struct{ sent, want int }{
{0, 0},
{len(first), len(first)},
{len(first) + 10, len(first)},
{len(buf), len(buf)},
} {
if got := lastLogRecordEnd(buf, tc.sent); got != tc.want {
t.Errorf("after %d bytes sent: keep from %d, want %d", tc.sent, got, tc.want)
}
}
}
func encodeLogRecord(t *testing.T, entry *filer_pb.LogEntry) []byte {
t.Helper()
data, err := entry.MarshalVT()
if err != nil {
t.Fatal(err)
}
record := make([]byte, 4, 4+len(data))
util.Uint32toBytes(record, uint32(len(data)))
return append(record, data...)
}