Files
seaweedfs/weed/command/filer_sync_log_gap.go
Chris LuandClaude Opus 5.5 c637d60dfc filer.sync: keep an informational replay cursor on the source, and let the log purge honor it (#13176)
* filer.sync: save the sync position on the source filer as a replay cursor

The position stays authoritative on the target. The source copy holds the
last position the target checkpoint stored (a fresh sync without one holds
just before the first event it received) and is rewritten hourly while the
sync runs, even when the position does not move.

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

* filer.sync: do not replicate the sync cursors of the source

A target never holds cursors that describe the source's log. A rename into
or out of the cursor directory replicates the ordinary side.

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

* metadata log purge: keep the logs a filer.sync still needs

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

* filer.sync: describe the sync in its source cursor

The source cursor is informational: besides the position it records the
source and target filers and paths, exclusions, host, version, when the
sync started, whether the position is a target checkpoint, and the
processed and failed event times, for display in admin tools. The
target checkpoint stays authoritative.

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

* filer.sync: save the first sync cursor when the first event arrives and retry failed saves

A fresh sync only saved its cursor on the next minute tick, and a failed
save still pushed the next attempt out by an hour, leaving the purge
without a cursor while the sync replayed old log days.

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

* filer.backup: do not seed the source's sync cursors in the initial snapshot

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

* metadata log purge: say that a position not saved for 7 days stops holding the log

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

* filer.backup test: lock the recording sink for concurrent snapshot workers

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

* filer.sync: leave the sync cursors out of the metadata log gap check

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 13:09:23 -08:00

125 lines
5.0 KiB
Go

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 synced 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, excludePaths []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 || isNotSynced(string(util.NewFullPath(dir, entry.Name)), excludePaths) {
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 isNotSynced(p string, excludePaths []string) bool {
return util.IsEqualOrUnder(p, filer.SystemLogDir) || strings.HasPrefix(filer.SystemLogDir, p+"/") ||
isSyncCursor(util.FullPath(p)) || isMultipartUploadDir(p+"/") || matchesExcludePath(p, excludePaths)
}
// 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, excludePaths []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, excludePaths, 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, excludePaths []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, excludePaths, 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)
}
}