mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
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>
125 lines
4.8 KiB
Go
125 lines
4.8 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 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)
|
|
}
|
|
}
|