Files
seaweedfs/weed/shell/command_fs_log.go
T
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

193 lines
6.9 KiB
Go

package shell
import (
"context"
"flag"
"fmt"
"io"
"strings"
"time"
"github.com/seaweedfs/seaweedfs/weed/cluster"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"github.com/seaweedfs/seaweedfs/weed/wdclient"
)
func init() {
Commands = append(Commands, &commandFsLogPurge{})
}
type commandFsLogPurge struct {
}
// filerDiscoveryTimeout bounds the master round trip used to list the filers.
const filerDiscoveryTimeout = 30 * time.Second
// LogPurgeKeepFrom is the start of the oldest metadata log day the store
// behind a filer must keep for its TTLs, lifecycle rules and replay jobs, and
// what holds it there.
var LogPurgeKeepFrom func(ctx context.Context, client filer_pb.FilerClient, lookup wdclient.HasLookupFileIdFunction, runNow time.Time) (time.Time, string, error)
func (c *commandFsLogPurge) Name() string {
return "fs.log.purge"
}
func (c *commandFsLogPurge) Help() string {
return `purge filer logs
fs.log.purge [-v] [-daysAgo 365] [-force] [-filer=<host:port>,...]
Each filer keeps its own metadata logs, so the purge runs on every filer
registered with the master by default. Use -filer to restrict it to a
subset of the cluster.
Logs still needed by a filer.conf TTL, an S3 lifecycle rule, a running
replay job or a filer.sync reading from the filer (until it has been
stopped for 7 days) are kept even when older than -daysAgo, and a filer whose
needs cannot be read is not purged. Pass -force to purge them anyway.
`
}
func (c *commandFsLogPurge) HasTag(CommandTag) bool {
return false
}
func (c *commandFsLogPurge) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
fsLogPurgeCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
daysAgo := fsLogPurgeCommand.Uint("daysAgo", 365, "purge logs older than N days")
verbose := fsLogPurgeCommand.Bool("v", false, "verbose mode")
force := fsLogPurgeCommand.Bool("force", false, "purge logs that TTLs, lifecycle rules or replay jobs still need")
filerList := fsLogPurgeCommand.String("filer", "", "comma separated filer addresses to purge (default: all filers in the cluster)")
if err = fsLogPurgeCommand.Parse(args); err != nil {
return err
}
filers, err := resolveLogPurgeFilers(*filerList, commandEnv, *verbose, writer)
if err != nil {
return err
}
modificationTimeAgo := time.Now().Add(-time.Hour * 24 * time.Duration(*daysAgo)).Unix()
var failed []string
for _, filerAddress := range filers {
if *verbose {
fmt.Fprintf(writer, "purging filer %s\n", filerAddress)
}
if purgeErr := purgeFilerLogs(commandEnv, filerAddress, modificationTimeAgo, *force, *verbose, writer); purgeErr != nil {
// one unreachable filer must not stop the purge of the others
fmt.Fprintf(writer, "purge filer %s: %v\n", filerAddress, purgeErr)
failed = append(failed, fmt.Sprintf("%s: %v", filerAddress, purgeErr))
}
}
if len(failed) > 0 {
return fmt.Errorf("failed to purge %d of %d filers: %s", len(failed), len(filers), strings.Join(failed, "; "))
}
return nil
}
// resolveLogPurgeFilers returns the filers to purge: the explicit -filer list
// when given, otherwise every filer registered with the master.
func resolveLogPurgeFilers(filerList string, commandEnv *CommandEnv, verbose bool, writer io.Writer) ([]pb.ServerAddress, error) {
if filerList != "" {
var filers []pb.ServerAddress
for _, address := range strings.Split(filerList, ",") {
if address = strings.TrimSpace(address); address != "" {
filers = append(filers, pb.ServerAddress(address))
}
}
if len(filers) == 0 {
return nil, fmt.Errorf("-filer %q lists no filer address", filerList)
}
return filers, nil
}
if commandEnv.MasterClient != nil && commandEnv.option.FilerGroup != nil {
// bound the master round trip: a shell whose master connection dropped
// must still fall back to the filer it is connected to instead of
// waiting forever before purging anything.
ctx, cancel := context.WithTimeout(context.Background(), filerDiscoveryTimeout)
defer cancel()
var filers []pb.ServerAddress
listErr := commandEnv.MasterClient.WithClient(ctx, false, func(client master_pb.SeaweedClient) error {
resp, err := client.ListClusterNodes(ctx, &master_pb.ListClusterNodesRequest{
ClientType: cluster.FilerType,
FilerGroup: *commandEnv.option.FilerGroup,
})
if err != nil {
return err
}
for _, node := range resp.ClusterNodes {
filers = append(filers, pb.ServerAddress(node.Address))
}
return nil
})
if listErr == nil && len(filers) > 0 {
return filers, nil
}
// a master that cannot be reached or lists nothing is not fatal: the
// filer this shell talks to is still worth purging.
if listErr != nil && verbose {
fmt.Fprintf(writer, "cannot list filers from master: %v\n", listErr)
}
}
commandEnv.filerMu.Lock()
address := commandEnv.option.FilerAddress
commandEnv.filerMu.Unlock()
if address != "" {
return []pb.ServerAddress{address}, nil
}
return nil, fmt.Errorf("no filer found to purge")
}
// purgeFilerLogs purges the logs of a single filer, leaving the caller's
// command environment untouched.
func purgeFilerLogs(commandEnv *CommandEnv, filerAddress pb.ServerAddress, modificationTimeAgo int64, force, verbose bool, writer io.Writer) error {
options := *commandEnv.option
options.FilerAddress = filerAddress
filerEnv := &CommandEnv{
env: commandEnv.env,
MasterClient: commandEnv.MasterClient,
option: &options,
locker: commandEnv.locker,
noLock: commandEnv.noLock,
forceNoLock: commandEnv.forceNoLock,
verbose: commandEnv.verbose,
}
if LogPurgeKeepFrom != nil {
keep, reason, err := LogPurgeKeepFrom(context.Background(), filerEnv, commandEnv.MasterClient, time.Now())
switch {
case err != nil && !force:
return fmt.Errorf("read the logs still needed (use -force to purge anyway): %w", err)
case err != nil:
fmt.Fprintf(writer, "warning: filer %s: -force purging without knowing the logs still needed: %v\n", filerAddress, err)
case modificationTimeAgo < keep.Unix():
case force:
fmt.Fprintf(writer, "warning: filer %s: -force purging logs from %s on that %s still needs\n", filerAddress, keep.Format("2006-01-02"), reason)
default:
fmt.Fprintf(writer, "filer %s: keeping logs from %s on for %s (use -force to purge them)\n", filerAddress, keep.Format("2006-01-02"), reason)
modificationTimeAgo = keep.Unix() - 1
}
}
return filer_pb.ReadDirAllEntries(context.Background(), filerEnv, filer.SystemLogDir, "", func(entry *filer_pb.Entry, isLast bool) error {
if entry.Attributes.Mtime > modificationTimeAgo {
return nil
}
if errDel := filer_pb.Remove(context.Background(), filerEnv, filer.SystemLogDir, entry.Name, true, true, true, false, nil); errDel != nil {
return errDel
}
if verbose {
fmt.Fprintf(writer, "delete %s\n", entry.Name)
}
return nil
})
}