Files
seaweedfs/weed/command/filer_sync_log_gap_test.go
T
Chris LuandClaude Opus 5.5 38e42101ee filer.sync: warn when the source metadata log no longer covers the sync
A sync replays the source's metadata log from its saved position, or from
the oldest log file when it has none. Once older log days are purged, a
position older than the log silently skips the purged changes, and a fresh
sync never copies entries whose last change was purged, so the target ends
up incomplete without any sign of it.

At start, compare the position with the oldest log file the source keeps;
for a fresh sync, look for a child of the source path whose last change
(commit time, or mtime for entries without one) predates the log, and log
a warning naming it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 03:27:00 -08:00

74 lines
2.8 KiB
Go

package command
import (
"context"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
func TestMetadataLogGap(t *testing.T) {
logStart := time.Date(2026, 11, 30, 3, 7, 0, 0, time.UTC)
logs := map[string][]*filer_pb.Entry{
filer.SystemLogDir: {
{Name: "2026-12-01", IsDirectory: true},
{Name: "2026-11-30", IsDirectory: true},
{Name: "not-a-day", IsDirectory: true},
},
filer.SystemLogDir + "/2026-11-30": {
{Name: "05-00.0000000a"},
{Name: "03-07.0000000b"},
},
}
child := func(name string, commitTsNs int64, mtime time.Time) *filer_pb.Entry {
return &filer_pb.Entry{Name: name, IsDirectory: true, CommitTsNs: commitTsNs, Attributes: &filer_pb.FuseAttributes{Mtime: mtime.Unix()}}
}
before, after := logStart.Add(-time.Hour), logStart.Add(time.Hour)
tests := []struct {
name string
dirs map[string][]*filer_pb.Entry
sourcePath string
offsetTsNs int64
want string
}{
{name: "no metadata log", dirs: map[string][]*filer_pb.Entry{"/buckets": {child("old", before.UnixNano(), before)}}, sourcePath: "/buckets"},
{name: "position inside the log", offsetTsNs: after.UnixNano()},
{name: "position at the oldest file", offsetTsNs: logStart.UnixNano()},
{name: "position before the log", offsetTsNs: before.UnixNano(), want: "after the saved sync position 2026-11-30T02:07:00Z"},
{name: "fresh sync, every entry in the log", dirs: map[string][]*filer_pb.Entry{"/buckets": {child("recent", after.UnixNano(), before)}}, sourcePath: "/buckets"},
{name: "fresh sync, entry changed before the log", dirs: map[string][]*filer_pb.Entry{"/buckets": {child("recent", after.UnixNano(), after), child("historical", before.UnixNano(), before)}}, sourcePath: "/buckets", want: "/buckets/historical was last changed at 2026-11-30T02:07:00Z"},
{name: "fresh sync, entry without commit time", dirs: map[string][]*filer_pb.Entry{"/buckets": {child("historical", 0, before)}}, sourcePath: "/buckets", want: "/buckets/historical"},
{name: "fresh sync of the root skips the log directory", dirs: map[string][]*filer_pb.Entry{"/": {child("topics", before.UnixNano(), before)}}, sourcePath: "/"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dirs := map[string][]*filer_pb.Entry{}
if tt.name != "no metadata log" {
for k, v := range logs {
dirs[k] = v
}
}
for k, v := range tt.dirs {
dirs[k] = v
}
got, err := metadataLogGap(context.Background(), &verifyTestInnerClient{entriesByDir: dirs}, tt.sourcePath, tt.offsetTsNs)
if err != nil {
t.Fatalf("metadataLogGap: %v", err)
}
if tt.want == "" {
if got != "" {
t.Fatalf("got gap %q, want none", got)
}
return
}
if !strings.Contains(got, tt.want) {
t.Fatalf("got gap %q, want it to mention %q", got, tt.want)
}
})
}
}