mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-16 08:45:22 +01:00
* s3 lifecycle: walk checkpoints take each entry's resume position Versions of one object share a Path, so a checkpoint set from Path after the first version made a resume skip the rest. The ListFunc now gives each entry the start value that continues after it, and leaves it empty on all but an object's last version. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: resume a bucket listing where the walk stopped The filer listing skipped entries with key <= start, but its order is not key order: each directory emits its .versions groups before its files, and a directory's children come before a sibling like dir.txt. Positions are now one (pass, name) frame per directory level, so a resume seeds each listing and skips exactly what was finished. A version group is one unit, and a skipped group still keeps its bare null version out of the second pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: content hash of a walk view Hashes every non-disabled action of a view and the versioning of their buckets, so a saved walk position can be tied to the walk it came from: another rule set, another walk mode, or a bucket that turned versioned gives another hash. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: persist daily walk progress and resume it A walk that failed or was cut short started over on the next pass, listing again every bucket it had already finished. WalkBuckets now keeps the finished buckets and each unfinished bucket's position in a checkpoint file next to the shard cursors, one per shard set, and the next walk of the same view and shards resumes from it. The record is saved every 30s and when the walk stops, and cleared when a walk completes, so the next walk is full again and sees keys added behind the position; a clear that fails fails the walk rather than leave a record that would skip buckets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: a stuck resumed walk starts over A bucket that fails at the same place on every pass kept the walk from completing, so the buckets it had finished were never walked again and their due objects waited forever. A resumed walk that fails again without finishing anything clears its checkpoint, and the next walk is full. A canceled walk keeps it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: do not resume a walk checkpoint left for a week A checkpoint is cleared only by a later walk of the same shard set, so a set that stopped walking, such as a one-off per-shard walk, left its record behind, and a walk of that set much later would skip buckets finished long before. Records now carry their save time and are not resumed after seven days; a walk that keeps retrying saves on every attempt and stays fresh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: a failed walk without progress leaves its checkpoint alone A pass canceled while it read the checkpoint failed the read, then saved its own empty progress over the record it never loaded, so the next pass started every bucket again. A failed walk that finished nothing has nothing new to record and no longer writes; the stuck resume reset still clears. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: space out walk checkpoint saves after a failed one The save time advanced only on success, so once the filer refused a periodic save every following entry tried again and logged another warning. The interval now counts from the last attempt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: a walk checkpoint save that errors may still be on disk A save whose reply was lost left onDisk false, so a walk that then completed skipped the clear and the next walk resumed from the stale record instead of walking everything. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: keep a stalled walk checkpoint for a day A resumed walk that failed again without progress dropped its record right away, so an outage spanning two attempts restarted the whole walk. Treat the walk as stuck only once its record has not advanced for a day. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: a resumed walk counts its earlier buckets as walked Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: replay a resumed recovery walk from the walk that began it A recovery or cold-start pass rewinds replay to runNow - maxTTL. When the walk resumed a checkpoint, its finished buckets were listed in an earlier attempt, so an object there that was not yet due then and whose write is now older than maxTTL was neither expired by the walk nor replayed. The checkpoint now keeps the start of the walk that began it, WalkBuckets returns that time (its own start for a fresh walk), and runShard floors the recovery rewind and the cold-start replay at it. A record without a start is not resumed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: a cold start that resumed a walk replays on the next pass The pass's shared subscription starts at runNow - maxTTL for a shard without a cursor, before its walk reports an earlier start, so flooring the cold-start drain at the walk start could not deliver the older events. Such a shard now saves its cursor at the walk start minus maxTTL and returns, like the recovery branch, and the next pass replays the window from that cursor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: test that a cursor below the floor opens the next subscription Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3 lifecycle: test the cold-start replay across two Run passes A cold start whose walk resumed an earlier attempt sees an older event only on the next pass. The test runs Run twice against a filer that serves events past the requested floor, with a later in-window event that would otherwise move the cursor past the older one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
282 lines
8.9 KiB
Go
282 lines
8.9 KiB
Go
package dailyrun
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle/bootstrap"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle/engine"
|
|
)
|
|
|
|
// WalkCheckpoint is the saved progress of an unfinished WalkBuckets
|
|
// call. ViewHash (engine.WalkContentHash) and Shards identify the walk;
|
|
// a walk with another identity starts over.
|
|
type WalkCheckpoint struct {
|
|
ViewHash [32]byte
|
|
Shards []int
|
|
// Done lists finished buckets; Resume maps a partly walked bucket
|
|
// to its bootstrap.Checkpoint.LastScannedPath.
|
|
Done []string
|
|
Resume map[string]string
|
|
// When the walk that began this record started; every bucket in it
|
|
// was listed after that.
|
|
StartedAt time.Time
|
|
// Set by LoadWalk; SaveWalk stamps the current time.
|
|
SavedAt time.Time
|
|
}
|
|
|
|
// WalkCheckpointPersister keeps one WalkCheckpoint per shard set.
|
|
type WalkCheckpointPersister interface {
|
|
LoadWalk(ctx context.Context, shards []int) (WalkCheckpoint, bool, error)
|
|
SaveWalk(ctx context.Context, cp WalkCheckpoint) error
|
|
}
|
|
|
|
type walkCheckpointFile struct {
|
|
Version int `json:"version"`
|
|
Shards []int `json:"shards"`
|
|
ViewHash []byte `json:"view_hash,omitempty"`
|
|
Done []string `json:"done,omitempty"`
|
|
Resume map[string]string `json:"resume,omitempty"`
|
|
StartedNs int64 `json:"started_ns,omitempty"`
|
|
SavedNs int64 `json:"saved_ns,omitempty"`
|
|
}
|
|
|
|
func walkCheckpointFileName(shards []int) string {
|
|
contiguous := len(shards) > 1
|
|
for i := 1; i < len(shards); i++ {
|
|
contiguous = contiguous && shards[i] == shards[i-1]+1
|
|
}
|
|
if contiguous {
|
|
return fmt.Sprintf("walk-%02d-%02d.json", shards[0], shards[len(shards)-1])
|
|
}
|
|
parts := make([]string, len(shards))
|
|
for i, sh := range shards {
|
|
parts[i] = fmt.Sprintf("%02d", sh)
|
|
}
|
|
return "walk-" + strings.Join(parts, "_") + ".json"
|
|
}
|
|
|
|
func (p *FilerCursorPersister) LoadWalk(ctx context.Context, shards []int) (WalkCheckpoint, bool, error) {
|
|
if p.Store == nil {
|
|
return WalkCheckpoint{}, false, errors.New("FilerCursorPersister: nil Store")
|
|
}
|
|
content, err := p.Store.Read(ctx, CursorDir, walkCheckpointFileName(shards))
|
|
if err != nil {
|
|
if errors.Is(err, filer_pb.ErrNotFound) {
|
|
return WalkCheckpoint{}, false, nil
|
|
}
|
|
return WalkCheckpoint{}, false, fmt.Errorf("walk checkpoint read: %w", err)
|
|
}
|
|
var f walkCheckpointFile
|
|
if err := json.Unmarshal(content, &f); err != nil {
|
|
return WalkCheckpoint{}, false, fmt.Errorf("walk checkpoint decode: %w", err)
|
|
}
|
|
if f.Version != cursorFileVersion {
|
|
return WalkCheckpoint{}, false, fmt.Errorf("walk checkpoint version: got %d, want %d", f.Version, cursorFileVersion)
|
|
}
|
|
if len(f.ViewHash) != 0 && len(f.ViewHash) != 32 {
|
|
return WalkCheckpoint{}, false, fmt.Errorf("walk checkpoint view_hash: got %d bytes, want 32", len(f.ViewHash))
|
|
}
|
|
cp := WalkCheckpoint{Shards: f.Shards, Done: f.Done, Resume: f.Resume, SavedAt: time.Unix(0, f.SavedNs)}
|
|
if f.StartedNs != 0 {
|
|
cp.StartedAt = time.Unix(0, f.StartedNs)
|
|
}
|
|
copy(cp.ViewHash[:], f.ViewHash)
|
|
return cp, true, nil
|
|
}
|
|
|
|
func (p *FilerCursorPersister) SaveWalk(ctx context.Context, cp WalkCheckpoint) error {
|
|
if p.Store == nil {
|
|
return errors.New("FilerCursorPersister: nil Store")
|
|
}
|
|
f := walkCheckpointFile{Version: cursorFileVersion, Shards: cp.Shards, Done: cp.Done, Resume: cp.Resume, SavedNs: time.Now().UnixNano()}
|
|
if !cp.StartedAt.IsZero() {
|
|
f.StartedNs = cp.StartedAt.UnixNano()
|
|
}
|
|
if cp.ViewHash != ([32]byte{}) {
|
|
f.ViewHash = cp.ViewHash[:]
|
|
}
|
|
content, err := json.Marshal(f)
|
|
if err != nil {
|
|
return fmt.Errorf("walk checkpoint encode: %w", err)
|
|
}
|
|
if err := p.Store.Save(ctx, CursorDir, walkCheckpointFileName(cp.Shards), content); err != nil {
|
|
return fmt.Errorf("walk checkpoint save: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// walkCheckpointInterval spaces the saves of a running walk, in ns.
|
|
var walkCheckpointInterval atomic.Int64
|
|
|
|
// A walk that keeps failing saves on every attempt; a record this old
|
|
// belongs to a shard set that stopped walking, and resuming it would
|
|
// skip buckets walked long ago.
|
|
const walkCheckpointMaxAge = 7 * 24 * time.Hour
|
|
|
|
// A resumed walk that keeps failing without progress is treated as stuck
|
|
// once its record has not advanced for this long.
|
|
const walkCheckpointStallLimit = 24 * time.Hour
|
|
|
|
func init() { walkCheckpointInterval.Store(int64(30 * time.Second)) }
|
|
|
|
// walkProgress tracks one WalkBuckets call against its checkpoint.
|
|
type walkProgress struct {
|
|
store WalkCheckpointPersister
|
|
cp WalkCheckpoint
|
|
done map[string]bool
|
|
onDisk bool // a saved record a finished walk must clear
|
|
resumed bool
|
|
progressed bool
|
|
savedAt time.Time
|
|
}
|
|
|
|
func loadWalkProgress(ctx context.Context, store WalkCheckpointPersister, view *engine.Snapshot, shards []int, buckets []string) *walkProgress {
|
|
sorted := slices.Clone(shards)
|
|
slices.Sort(sorted)
|
|
now := time.Now()
|
|
w := &walkProgress{
|
|
store: store,
|
|
cp: WalkCheckpoint{ViewHash: engine.WalkContentHash(view), Shards: sorted, Resume: map[string]string{}, StartedAt: now},
|
|
done: map[string]bool{},
|
|
savedAt: now,
|
|
}
|
|
if store == nil {
|
|
return w
|
|
}
|
|
saved, found, err := store.LoadWalk(ctx, sorted)
|
|
if err != nil {
|
|
glog.Warningf("walker: shards %v: %v; walking every bucket", sorted, err)
|
|
w.onDisk = true
|
|
return w
|
|
}
|
|
if !found {
|
|
return w
|
|
}
|
|
w.onDisk = len(saved.Done) > 0 || len(saved.Resume) > 0
|
|
if saved.ViewHash != w.cp.ViewHash || !slices.Equal(saved.Shards, sorted) || saved.StartedAt.IsZero() || time.Since(saved.SavedAt) > walkCheckpointMaxAge {
|
|
return w
|
|
}
|
|
listed := make(map[string]bool, len(buckets))
|
|
for _, b := range buckets {
|
|
listed[b] = true
|
|
}
|
|
for _, b := range saved.Done {
|
|
if listed[b] {
|
|
w.done[b] = true
|
|
}
|
|
}
|
|
for b, pos := range saved.Resume {
|
|
if listed[b] && !w.done[b] {
|
|
w.cp.Resume[b] = pos
|
|
}
|
|
}
|
|
w.cp.SavedAt = saved.SavedAt
|
|
w.resumed = len(w.done) > 0 || len(w.cp.Resume) > 0
|
|
if w.resumed {
|
|
w.cp.StartedAt = saved.StartedAt
|
|
glog.V(0).Infof("walker: shards %v resume with %d bucket(s) done and %d partly walked", sorted, len(w.done), len(w.cp.Resume))
|
|
}
|
|
return w
|
|
}
|
|
|
|
// track records each finished entry's position as the bucket's resume
|
|
// point, saving it every walkCheckpointInterval.
|
|
func (w *walkProgress) track(bucket string, list bootstrap.ListFunc) bootstrap.ListFunc {
|
|
if w.store == nil {
|
|
return list
|
|
}
|
|
return func(ctx context.Context, b, start string, cb func(*bootstrap.Entry) error) error {
|
|
return list(ctx, b, start, func(e *bootstrap.Entry) error {
|
|
if err := cb(e); err != nil {
|
|
return err
|
|
}
|
|
if e != nil && e.Resume != "" {
|
|
w.cp.Resume[bucket] = e.Resume
|
|
w.progressed = true
|
|
w.maybeSave(ctx)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
}
|
|
|
|
func (w *walkProgress) bucketDone(ctx context.Context, bucket string) {
|
|
w.done[bucket] = true
|
|
delete(w.cp.Resume, bucket)
|
|
w.progressed = true
|
|
w.maybeSave(ctx)
|
|
}
|
|
|
|
func (w *walkProgress) maybeSave(ctx context.Context) {
|
|
if w.store == nil || time.Since(w.savedAt) < time.Duration(walkCheckpointInterval.Load()) {
|
|
return
|
|
}
|
|
w.savedAt = time.Now()
|
|
if err := w.save(ctx); err != nil {
|
|
glog.Warningf("walker: shards %v: %v", w.cp.Shards, err)
|
|
}
|
|
}
|
|
|
|
func (w *walkProgress) save(ctx context.Context) error {
|
|
cp := w.cp
|
|
cp.Done = make([]string, 0, len(w.done))
|
|
for b := range w.done {
|
|
cp.Done = append(cp.Done, b)
|
|
}
|
|
slices.Sort(cp.Done)
|
|
kept := len(cp.Done) > 0 || len(cp.Resume) > 0
|
|
// A save that reports an error may still have been written.
|
|
w.onDisk = w.onDisk || kept
|
|
if err := w.store.SaveWalk(ctx, cp); err != nil {
|
|
return err
|
|
}
|
|
w.onDisk = kept
|
|
return nil
|
|
}
|
|
|
|
// finish saves where the walk stopped, or clears the checkpoint when it
|
|
// completed. A resumed walk that failed without progress on a record
|
|
// older than walkCheckpointStallLimit is cleared too, so one stuck
|
|
// bucket cannot keep the finished ones from ever being walked again.
|
|
// Any other failed walk without progress has nothing new to record and
|
|
// leaves the file, read or not, alone.
|
|
func (w *walkProgress) finish(ctx context.Context, walkErr error) error {
|
|
if w.store == nil {
|
|
return nil
|
|
}
|
|
reset := walkErr == nil
|
|
if walkErr != nil && !w.progressed {
|
|
if ctx.Err() != nil || !w.resumed || time.Since(w.cp.SavedAt) < walkCheckpointStallLimit {
|
|
return nil
|
|
}
|
|
glog.Warningf("walker: shards %v made no progress since %v; the next walk starts over", w.cp.Shards, w.cp.SavedAt.Format(time.RFC3339))
|
|
reset = true
|
|
}
|
|
if reset {
|
|
clear(w.done)
|
|
clear(w.cp.Resume)
|
|
}
|
|
if !w.onDisk && len(w.done) == 0 && len(w.cp.Resume) == 0 {
|
|
return nil
|
|
}
|
|
saveCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := w.save(saveCtx); err != nil {
|
|
if walkErr == nil {
|
|
// Left in place, the record would skip buckets next walk.
|
|
return fmt.Errorf("walker: shards %v: %w", w.cp.Shards, err)
|
|
}
|
|
glog.Warningf("walker: shards %v: %v", w.cp.Shards, err)
|
|
}
|
|
return nil
|
|
}
|