mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-16 00:38:53 +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>
193 lines
7.5 KiB
Go
193 lines
7.5 KiB
Go
package dailyrun
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/s3_lifecycle_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle/bootstrap"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle/reader"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle/router"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func allShards() []int {
|
|
out := make([]int, s3lifecycle.ShardCount)
|
|
for i := range out {
|
|
out[i] = i
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestWalkBuckets_RecordsEveryBucketFailure(t *testing.T) {
|
|
list := func(_ context.Context, bucket, _ string, _ func(*bootstrap.Entry) error) error {
|
|
if strings.HasPrefix(bucket, "bad") {
|
|
return errors.New("list " + bucket + " failed")
|
|
}
|
|
return nil
|
|
}
|
|
runStats := &RunStats{}
|
|
_, err := WalkBuckets(context.Background(), snapshotForBucketRule(t, "good", 7), []int{0},
|
|
[]string{"bad-a", "good", "bad-b"}, list, &recordingDispatcher{}, runStats, nil)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "bad-a", "the returned error stays the first bucket's")
|
|
|
|
sum := runStats.Summary()
|
|
assert.Equal(t, 1, sum.BucketsWalked)
|
|
assert.Equal(t, 2, sum.BucketsFailed)
|
|
assert.Equal(t, 0, sum.BucketsSkipped)
|
|
require.Len(t, sum.BucketFailures, 2)
|
|
assert.Equal(t, "bad-a", sum.BucketFailures[0].Bucket)
|
|
assert.Contains(t, sum.BucketFailures[0].Error, "list bad-a failed")
|
|
assert.Equal(t, "bad-b", sum.BucketFailures[1].Bucket)
|
|
assert.Equal(t, PhaseWalk, sum.Phase)
|
|
assert.Equal(t, "bad-b", sum.Bucket)
|
|
}
|
|
|
|
func TestWalkBuckets_CancelMarksRemainingBucketsSkipped(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
list := func(ctx context.Context, bucket, _ string, _ func(*bootstrap.Entry) error) error {
|
|
if bucket == "second" {
|
|
cancel()
|
|
return ctx.Err()
|
|
}
|
|
return nil
|
|
}
|
|
runStats := &RunStats{}
|
|
_, err := WalkBuckets(ctx, snapshotForBucketRule(t, "first", 7), []int{0},
|
|
[]string{"first", "second", "third"}, list, &recordingDispatcher{}, runStats, nil)
|
|
assert.ErrorIs(t, err, context.Canceled)
|
|
|
|
sum := runStats.Summary()
|
|
assert.Equal(t, 1, sum.BucketsWalked)
|
|
assert.Equal(t, 0, sum.BucketsFailed, "a cancelled walk is not a bucket failure")
|
|
assert.Equal(t, 2, sum.BucketsSkipped)
|
|
assert.Empty(t, sum.BucketFailures)
|
|
}
|
|
|
|
func TestRunStats_FailedWalkWinsAcrossWalksOfOneBucket(t *testing.T) {
|
|
runStats := &RunStats{}
|
|
runStats.finishBucket("b", bucketSkipped, nil)
|
|
runStats.finishBucket("b", bucketWalked, nil)
|
|
runStats.finishBucket("b", bucketFailed, errors.New("first"))
|
|
runStats.finishBucket("b", bucketFailed, errors.New("second"))
|
|
runStats.finishBucket("b", bucketWalked, nil)
|
|
|
|
sum := runStats.Summary()
|
|
assert.Equal(t, 1, sum.BucketsFailed)
|
|
assert.Equal(t, 0, sum.BucketsWalked)
|
|
require.Len(t, sum.BucketFailures, 1)
|
|
assert.Equal(t, "first", sum.BucketFailures[0].Error)
|
|
}
|
|
|
|
func TestRunStats_BoundsReportedFailures(t *testing.T) {
|
|
runStats := &RunStats{}
|
|
long := strings.Repeat("x", 2*maxReportedErrorLen)
|
|
for i := 0; i < maxReportedBucketFailures+5; i++ {
|
|
runStats.finishBucket("b-"+intToStr(i), bucketFailed, errors.New(long))
|
|
}
|
|
sum := runStats.Summary()
|
|
assert.Equal(t, maxReportedBucketFailures+5, sum.BucketsFailed, "the count stays exact")
|
|
require.Len(t, sum.BucketFailures, maxReportedBucketFailures)
|
|
assert.LessOrEqual(t, len(sum.BucketFailures[0].Error), maxReportedErrorLen+len("..."))
|
|
}
|
|
|
|
func TestWalkBuckets_CountsEntriesAndObjects(t *testing.T) {
|
|
old := time.Now().Add(-90 * 24 * time.Hour)
|
|
entries := []*bootstrap.Entry{
|
|
{Path: "a", VersionID: "v3", ModTime: old, IsLatest: true},
|
|
{Path: "a", VersionID: "v2", ModTime: old},
|
|
{Path: "a", VersionID: "v1", ModTime: old},
|
|
{Path: "b", ModTime: old, IsLatest: true},
|
|
{Path: "dir", IsDirectory: true},
|
|
{Path: ".uploads/u1", DestKey: "c", IsDirectory: true, IsMPUInit: true, ModTime: old},
|
|
}
|
|
runStats := &RunStats{}
|
|
_, err := WalkBuckets(context.Background(), snapshotForBucketRule(t, "bkt", 7), allShards(),
|
|
[]string{"bkt"}, bootstrap.EntryCallback(entries), &recordingDispatcher{}, runStats, nil)
|
|
require.NoError(t, err)
|
|
|
|
sum := runStats.Summary()
|
|
assert.Equal(t, int64(5), sum.EntriesVisited, "versions and upload records count, plain directories do not")
|
|
assert.Equal(t, int64(2), sum.ObjectsVisited, "versions of one key count as one object")
|
|
assert.Equal(t, 1, sum.BucketsWalked)
|
|
}
|
|
|
|
func TestWalkerDispatcher_CountsOutcomes(t *testing.T) {
|
|
runStats := &RunStats{}
|
|
a := sampleAction(t, s3lifecycle.ActionKindExpirationDays)
|
|
e := &bootstrap.Entry{Path: "obj"}
|
|
|
|
d := &WalkerDispatcher{Client: &walkerStubClient{outcome: s3_lifecycle_pb.LifecycleDeleteOutcome_DONE}, Stats: runStats}
|
|
require.NoError(t, d.Delete(context.Background(), a, e))
|
|
require.NoError(t, d.Delete(context.Background(), a, e))
|
|
d.Client = &walkerStubClient{outcome: s3_lifecycle_pb.LifecycleDeleteOutcome_SKIPPED_OBJECT_LOCK}
|
|
require.NoError(t, d.Delete(context.Background(), a, e))
|
|
d.Client = &walkerStubClient{err: errors.New("unavailable")}
|
|
require.Error(t, d.Delete(context.Background(), a, e))
|
|
d.Client = &walkerStubClient{nilResp: true}
|
|
require.Error(t, d.Delete(context.Background(), a, e))
|
|
|
|
assert.Equal(t, map[string]int64{"DONE": 2, "SKIPPED_OBJECT_LOCK": 1, "RPC_ERROR": 2}, runStats.Summary().Outcomes)
|
|
}
|
|
|
|
func TestProcessMatches_CountsOutcomes(t *testing.T) {
|
|
runNow := time.Now()
|
|
rule := s3lifecycle.ActionKey{Bucket: "b", RuleHash: [8]byte{1}, ActionKind: s3lifecycle.ActionKindExpirationDays}
|
|
matches := []router.Match{
|
|
{Key: rule, Bucket: "b", ObjectKey: "a", DueTime: runNow.Add(-time.Hour)},
|
|
{Key: rule, Bucket: "b", ObjectKey: "b", DueTime: runNow.Add(-time.Hour)},
|
|
{Key: rule, Bucket: "b", ObjectKey: "c", DueTime: runNow.Add(-time.Hour)},
|
|
}
|
|
client := &recordingClient{responses: []s3_lifecycle_pb.LifecycleDeleteOutcome{
|
|
s3_lifecycle_pb.LifecycleDeleteOutcome_DONE,
|
|
s3_lifecycle_pb.LifecycleDeleteOutcome_NOOP_RESOLVED,
|
|
s3_lifecycle_pb.LifecycleDeleteOutcome_RETRY_LATER,
|
|
}}
|
|
runStats := &RunStats{}
|
|
_, halted, err := processMatches(context.Background(), Config{Client: client, Stats: runStats}, runNow, &reader.Event{}, matches)
|
|
require.NoError(t, err)
|
|
assert.True(t, halted)
|
|
assert.Equal(t, map[string]int64{"DONE": 1, "NOOP_RESOLVED": 1, "RETRY_LATER": 1}, runStats.Summary().Outcomes)
|
|
}
|
|
|
|
func TestRunStats_NilIsANoOp(t *testing.T) {
|
|
var runStats *RunStats
|
|
runStats.startBucket("b")
|
|
runStats.finishBucket("b", bucketFailed, errors.New("x"))
|
|
runStats.startReplay()
|
|
runStats.visitEntry(true)
|
|
runStats.scanEvent()
|
|
runStats.recordOutcome("DONE")
|
|
runStats.setShardErrors(1)
|
|
assert.Equal(t, RunSummary{}, runStats.Summary())
|
|
}
|
|
|
|
// A walk reads entries it does not visit: the versioned pre-pass reads
|
|
// plain files without emitting them, and a resumed walk re-reads its
|
|
// start entry.
|
|
func TestRunStats_CountListedCountsSkippedEntries(t *testing.T) {
|
|
mtime := time.Now().Add(-7 * 24 * time.Hour)
|
|
client := &fakeFiler{tree: map[string][]*filer_pb.Entry{
|
|
"/buckets/bkt": {file("a.txt", mtime, 1), file("b.txt", mtime, 1), file("c.txt", mtime, 1)},
|
|
}}
|
|
stats := &RunStats{}
|
|
emitted := 0
|
|
require.NoError(t, FilerListFunc(stats.CountListed(client), "/buckets", nil)(context.Background(), "bkt", "2:b.txt", func(*bootstrap.Entry) error {
|
|
emitted++
|
|
return nil
|
|
}))
|
|
|
|
assert.Equal(t, 1, emitted)
|
|
assert.Equal(t, int64(3), stats.Summary().EntriesListed, "c.txt in the pre-pass, b.txt and c.txt in pass 2")
|
|
var nilStats *RunStats
|
|
assert.Same(t, client, nilStats.CountListed(client))
|
|
}
|