Files
seaweedfs/weed/command/filer_sync_cursor.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

74 lines
2.4 KiB
Go

package command
import (
"context"
"encoding/json"
"fmt"
"sync/atomic"
"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 syncCursorSaveInterval = time.Hour
func syncCursorName(targetFilerSignature int32, signaturePrefix string) string {
return fmt.Sprintf("%08x-%016x.json", uint32(targetFilerSignature), uint64(util.HashStringToLong(signaturePrefix)))
}
// saveSyncCursor keeps the sync position on the source filer as a replay
// cursor, so a metadata log purge there keeps the logs the sync has yet to read.
func saveSyncCursor(sourceGrpcDialOption grpc.DialOption, sourceFiler pb.ServerAddress, sourceTokenSource security.FilerTokenSource, name string, cursor filer.SyncCursor) error {
if cursor.TsNs <= 0 {
return nil
}
cursor.SavedNs = time.Now().UnixNano()
content, err := json.Marshal(cursor)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = pb.WithGrpcFilerClient(false, 0, sourceFiler, sourceGrpcDialOption, sourceTokenSource, func(client filer_pb.SeaweedFilerClient) error {
return filer.SaveInsideFiler(ctx, client, filer.SyncCursorDir, name, content)
})
if err != nil {
glog.V(1).Infof("save sync position on %s: %v", sourceFiler, err)
}
return err
}
// keepSyncCursor saves the cursor once the sync has a position, retries a
// failed save on the next tick, and then rewrites it hourly even when the
// position is stuck, so the purge keeps honoring a running sync and lets a
// stopped one expire. A fresh sync has no checkpoint until its oldest event
// finishes, and a restart replays everything it has received.
func keepSyncCursor(stop, firstSeen <-chan struct{}, tick <-chan time.Time, storedOffsetTsNs, firstSeenTsNs *atomic.Int64, save func(tsNs int64, checkpointed bool) error) {
var lastSaveTime time.Time
for {
tsNs := storedOffsetTsNs.Load()
checkpointed := tsNs > 0
if !checkpointed {
tsNs = firstSeenTsNs.Load() - 1
}
if tsNs > 0 && (lastSaveTime.IsZero() || time.Since(lastSaveTime) >= syncCursorSaveInterval) {
if save(tsNs, checkpointed) == nil {
lastSaveTime = time.Now()
}
}
select {
case <-stop:
return
case <-firstSeen:
firstSeen = nil
case <-tick:
}
}
}