mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
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>
This commit is contained in:
1 parent
df59c81a8c
commit
38e42101ee
3 files changed
+198
No files matched your search
@@ -374,6 +374,7 @@ func doSubscribeFilerMetaChanges(clientId int32, clientEpoch int32, sourceGrpcDi
|
||||
}
|
||||
|
||||
glog.V(0).Infof("start sync %s(%d) => %s(%d) from %v(%d)", sourceFiler, sourceFilerSignature, targetFiler, targetFilerSignature, time.Unix(0, sourceFilerOffsetTsNs), sourceFilerOffsetTsNs)
|
||||
warnMetadataLogGap(sourceGrpcDialOption, sourceFiler, sourceTokenSource, sourcePath, targetFiler, sourceFilerOffsetTsNs)
|
||||
|
||||
// create filer sink
|
||||
filerSource := &source.FilerSource{}
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/security"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
const (
|
||||
metadataLogGapProbeLimit = 1024
|
||||
logDayLayout = "2006-01-02"
|
||||
)
|
||||
|
||||
// oldestMetadataLogTime is the start of the oldest metadata log file the
|
||||
// filer still keeps, or zero when it keeps none.
|
||||
func oldestMetadataLogTime(ctx context.Context, client filer_pb.SeaweedFilerClient) (time.Time, error) {
|
||||
var oldestDay string
|
||||
err := filer_pb.SeaweedList(ctx, client, filer.SystemLogDir, "", func(entry *filer_pb.Entry, isLast bool) error {
|
||||
if _, parseErr := time.Parse(logDayLayout, entry.Name); parseErr == nil && entry.IsDirectory && (oldestDay == "" || entry.Name < oldestDay) {
|
||||
oldestDay = entry.Name
|
||||
}
|
||||
return nil
|
||||
}, "", false, math.MaxInt32)
|
||||
if errors.Is(err, filer_pb.ErrNotFound) || (err == nil && oldestDay == "") {
|
||||
return time.Time{}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
dayStart, _ := time.Parse(logDayLayout, oldestDay)
|
||||
var oldest time.Time
|
||||
err = filer_pb.SeaweedList(ctx, client, string(util.NewFullPath(filer.SystemLogDir, oldestDay)), "", func(entry *filer_pb.Entry, isLast bool) error {
|
||||
hourMinute, _, _ := strings.Cut(entry.Name, ".")
|
||||
if t, parseErr := time.Parse(logDayLayout+" 15-04", oldestDay+" "+hourMinute); parseErr == nil && (oldest.IsZero() || t.Before(oldest)) {
|
||||
oldest = t
|
||||
}
|
||||
return nil
|
||||
}, "", false, math.MaxInt32)
|
||||
if err != nil && !errors.Is(err, filer_pb.ErrNotFound) {
|
||||
return time.Time{}, err
|
||||
}
|
||||
if oldest.IsZero() {
|
||||
return dayStart, nil
|
||||
}
|
||||
return oldest, nil
|
||||
}
|
||||
|
||||
// entryChangedBefore is a child of dir whose last change is older than t, so
|
||||
// a replay of the log from t never writes it.
|
||||
func entryChangedBefore(ctx context.Context, client filer_pb.SeaweedFilerClient, dir string, t time.Time) (*filer_pb.Entry, error) {
|
||||
var found *filer_pb.Entry
|
||||
err := filer_pb.SeaweedList(ctx, client, dir, "", func(entry *filer_pb.Entry, isLast bool) error {
|
||||
if found != nil || isSystemLogAncestor(util.NewFullPath(dir, entry.Name)) {
|
||||
return nil
|
||||
}
|
||||
if entryLastChange(entry).Before(t) {
|
||||
found = entry
|
||||
}
|
||||
return nil
|
||||
}, "", false, metadataLogGapProbeLimit)
|
||||
if errors.Is(err, filer_pb.ErrNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return found, err
|
||||
}
|
||||
|
||||
func entryLastChange(entry *filer_pb.Entry) time.Time {
|
||||
if entry.CommitTsNs > 0 {
|
||||
return time.Unix(0, entry.CommitTsNs)
|
||||
}
|
||||
return time.Unix(entry.GetAttributes().GetMtime(), 0)
|
||||
}
|
||||
|
||||
func isSystemLogAncestor(p util.FullPath) bool {
|
||||
s := string(p)
|
||||
return s == filer.SystemLogDir || strings.HasPrefix(filer.SystemLogDir, s+"/") || strings.HasPrefix(s, filer.SystemLogDir+"/")
|
||||
}
|
||||
|
||||
// metadataLogGap describes what a sync starting at offsetTsNs cannot replay
|
||||
// because the source no longer keeps the metadata log for it, or "".
|
||||
func metadataLogGap(ctx context.Context, client filer_pb.SeaweedFilerClient, sourcePath string, offsetTsNs int64) (string, error) {
|
||||
logStart, err := oldestMetadataLogTime(ctx, client)
|
||||
if err != nil || logStart.IsZero() {
|
||||
return "", err
|
||||
}
|
||||
if offsetTsNs > 0 {
|
||||
if logStart.UnixNano() <= offsetTsNs {
|
||||
return "", nil
|
||||
}
|
||||
return "its metadata log starts at " + logStart.UTC().Format(time.RFC3339) + ", after the saved sync position " + time.Unix(0, offsetTsNs).UTC().Format(time.RFC3339) + "; changes made in between, if any, were purged and are not replicated", nil
|
||||
}
|
||||
entry, err := entryChangedBefore(ctx, client, sourcePath, logStart)
|
||||
if err != nil || entry == nil {
|
||||
return "", err
|
||||
}
|
||||
return "there is no saved sync position, so the sync replays its metadata log from its oldest file at " + logStart.UTC().Format(time.RFC3339) + ", but " + string(util.NewFullPath(sourcePath, entry.Name)) + " was last changed at " + entryLastChange(entry).UTC().Format(time.RFC3339) + "; entries not changed since the log was purged are not copied, so seed the target with the existing data first", nil
|
||||
}
|
||||
|
||||
func warnMetadataLogGap(sourceGrpcDialOption grpc.DialOption, sourceFiler pb.ServerAddress, sourceTokenSource security.FilerTokenSource, sourcePath string, targetFiler pb.ServerAddress, offsetTsNs int64) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
var gap string
|
||||
err := pb.WithGrpcFilerClient(false, 0, sourceFiler, sourceGrpcDialOption, sourceTokenSource, func(client filer_pb.SeaweedFilerClient) (err error) {
|
||||
gap, err = metadataLogGap(ctx, client, sourcePath, offsetTsNs)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
glog.V(1).Infof("check the metadata log of %s: %v", sourceFiler, err)
|
||||
return
|
||||
}
|
||||
if gap != "" {
|
||||
glog.Warningf("sync %s%s => %s may be incomplete: on %s, %s", sourceFiler, sourcePath, targetFiler, sourceFiler, gap)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user