Files
seaweedfs/weed/s3api/s3lifecycle/engine/hashes_test.go
T
Chris LuandClaude Opus 5.5 a91b333658 s3 lifecycle: persist daily walk checkpoints and resume them safely (#12650)
* 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>
2026-11-15 12:35:49 -08:00

297 lines
11 KiB
Go

package engine
import (
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReplayContentHash_EmptyWhenNoReplayActions(t *testing.T) {
var empty [32]byte
// Nil snapshot.
assert.Equal(t, empty, ReplayContentHash(nil))
// Walker-only rule: ExpirationDate compiles to a single ScanAtDate
// action which is excluded from ReplayContentHash.
when := time.Now().Add(24 * time.Hour)
rule := &s3lifecycle.Rule{
ID: "d",
Status: s3lifecycle.StatusEnabled,
ExpirationDate: when,
}
snap := buildSnapshotForViews(t, "b1", rule)
assert.Equal(t, empty, ReplayContentHash(snap))
}
func TestReplayContentHash_PartitionIndependent(t *testing.T) {
// Critical invariant: a retention shift that promotes a rule from
// replay to walk (or back) MUST NOT change ReplayContentHash. The hash
// is over the rule content as it appears in the base snapshot, not
// over the partition it ends up in.
rule := &s3lifecycle.Rule{
ID: "shift",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 30,
}
snap := buildSnapshotForViews(t, "b1", rule)
hashWithLargeRetention := ReplayContentHash(snap)
// Whether retention is 1d (promotes to walk) or 365d (stays in
// replay), the base snapshot's rule content didn't move — only its
// partition did. ReplayContentHash hashes the base, so this is
// trivially constant. We re-call here to pin the contract.
_, walkSmall := snap.RulesForShard(0, s3lifecycle.DaysToDuration(1))
require.NotNil(t, walkSmall, "precondition: 1d retention promotes 30d rule to walk")
replayLarge, _ := snap.RulesForShard(0, s3lifecycle.DaysToDuration(365))
require.NotNil(t, replayLarge, "precondition: 365d retention keeps 30d rule in replay")
hashAgain := ReplayContentHash(snap)
assert.Equal(t, hashWithLargeRetention, hashAgain,
"ReplayContentHash must be partition-independent")
}
func TestReplayContentHash_ChangesOnRuleContentEdit(t *testing.T) {
rule := &s3lifecycle.Rule{
ID: "r",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 30,
}
snap1 := buildSnapshotForViews(t, "b1", rule)
h1 := ReplayContentHash(snap1)
// Edit the TTL — RuleHash changes, so the action's key changes, and
// the content hash must move.
rule2 := &s3lifecycle.Rule{
ID: "r",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 60,
}
snap2 := buildSnapshotForViews(t, "b1", rule2)
h2 := ReplayContentHash(snap2)
assert.NotEqual(t, h1, h2, "TTL edit must change ReplayContentHash")
}
func TestReplayContentHash_StableAcrossRuleReorder(t *testing.T) {
r1 := &s3lifecycle.Rule{ID: "a", Status: s3lifecycle.StatusEnabled, ExpirationDays: 7, Prefix: "a/"}
r2 := &s3lifecycle.Rule{ID: "b", Status: s3lifecycle.StatusEnabled, ExpirationDays: 14, Prefix: "b/"}
snapAB := buildSnapshotForViews(t, "b1", r1, r2)
snapBA := buildSnapshotForViews(t, "b1", r2, r1)
assert.Equal(t, ReplayContentHash(snapAB), ReplayContentHash(snapBA),
"reordering rules at compile time must not change ReplayContentHash")
}
func TestReplayContentHash_ExcludesWalkerOnlyAndDisabled(t *testing.T) {
// Walker-only kinds and disabled rules don't affect ReplayContentHash;
// only replay-eligible action kinds count.
replayRule := &s3lifecycle.Rule{
ID: "rep",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 7,
}
snapOnlyReplay := buildSnapshotForViews(t, "b1", replayRule)
baseline := ReplayContentHash(snapOnlyReplay)
walkerRule := &s3lifecycle.Rule{
ID: "walker",
Status: s3lifecycle.StatusEnabled,
ExpiredObjectDeleteMarker: true,
}
disabledRule := &s3lifecycle.Rule{
ID: "off",
Status: s3lifecycle.StatusDisabled,
ExpirationDays: 90,
}
snapMixed := buildSnapshotForViews(t, "b1", replayRule, walkerRule, disabledRule)
mixed := ReplayContentHash(snapMixed)
assert.Equal(t, baseline, mixed,
"adding walker-only and disabled rules must not change ReplayContentHash")
}
func TestPromotedHash_EmptyWhenNoneAreInWalk(t *testing.T) {
var empty [32]byte
assert.Equal(t, empty, PromotedHash(nil, 0))
rule := &s3lifecycle.Rule{
ID: "fits",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 7,
}
snap := buildSnapshotForViews(t, "b1", rule)
// Large retention → rule stays in replay → no promotion.
assert.Equal(t, empty, PromotedHash(snap, s3lifecycle.DaysToDuration(365)))
}
func TestPromotedHash_ChangesOnReplayToWalkPromotion(t *testing.T) {
// Retention drops below the rule's TTL → rule promotes from replay to
// walk → PromotedHash changes from empty to non-empty.
rule := &s3lifecycle.Rule{
ID: "long",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 30,
}
snap := buildSnapshotForViews(t, "b1", rule)
before := PromotedHash(snap, s3lifecycle.DaysToDuration(365)) // replay
after := PromotedHash(snap, s3lifecycle.DaysToDuration(7)) // promoted to walk
var empty [32]byte
assert.Equal(t, empty, before)
assert.NotEqual(t, empty, after)
assert.NotEqual(t, before, after, "promotion must change PromotedHash")
}
func TestPromotedHash_ChangesOnWalkToReplayDemotion(t *testing.T) {
// Retention recovers from < TTL to >= TTL → rule demotes from walk to
// replay → PromotedHash changes from non-empty to empty.
rule := &s3lifecycle.Rule{
ID: "demo",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 30,
}
snap := buildSnapshotForViews(t, "b1", rule)
before := PromotedHash(snap, s3lifecycle.DaysToDuration(7)) // walk
after := PromotedHash(snap, s3lifecycle.DaysToDuration(365)) // demoted back to replay
var empty [32]byte
assert.NotEqual(t, empty, before)
assert.Equal(t, empty, after)
assert.NotEqual(t, before, after, "demotion must change PromotedHash")
}
func TestPromotedHash_StableWhenContentUnchangedAndPartitionStays(t *testing.T) {
// Same snapshot + same retentionWindow → same PromotedHash.
rule := &s3lifecycle.Rule{
ID: "stable",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 30,
}
snap := buildSnapshotForViews(t, "b1", rule)
rw := s3lifecycle.DaysToDuration(7) // promoted
assert.Equal(t, PromotedHash(snap, rw), PromotedHash(snap, rw))
}
func TestPromotedHash_MatchesRulesForShardWalkMembership(t *testing.T) {
// PromotedHash must agree with RulesForShard about which replay-
// eligible actions are in walk for the same retentionWindow.
rules := []*s3lifecycle.Rule{
{ID: "short", Status: s3lifecycle.StatusEnabled, ExpirationDays: 1},
{ID: "long", Status: s3lifecycle.StatusEnabled, ExpirationDays: 90},
{ID: "noncurrent", Status: s3lifecycle.StatusEnabled, NoncurrentVersionExpirationDays: 200},
}
snap := buildSnapshotForViews(t, "b1", rules...)
rw := s3lifecycle.DaysToDuration(30)
replay, walk := snap.RulesForShard(0, rw)
require.NotNil(t, replay)
require.NotNil(t, walk)
// Expected walk members (from replay-eligible kinds only): long, noncurrent.
wantPromoted := map[s3lifecycle.ActionKey]struct{}{}
for k := range walk.actions {
if isReplayKind(k.ActionKind) {
wantPromoted[k] = struct{}{}
}
}
assert.Len(t, wantPromoted, 2, "two of three replay-eligible rules should promote")
hash := PromotedHash(snap, rw)
var empty [32]byte
assert.NotEqual(t, empty, hash)
}
func TestMaxEffectiveTTL_NilAndEmpty(t *testing.T) {
assert.Equal(t, time.Duration(0), MaxEffectiveTTL(nil))
// Snapshot with only walker-only kinds → no active replay action →
// MaxEffectiveTTL returns 0.
rule := &s3lifecycle.Rule{
ID: "walk",
Status: s3lifecycle.StatusEnabled,
ExpiredObjectDeleteMarker: true,
}
snap := buildSnapshotForViews(t, "b1", rule)
assert.Equal(t, time.Duration(0), MaxEffectiveTTL(snap))
}
func TestMaxEffectiveTTL_ReturnsMaxAcrossActiveReplay(t *testing.T) {
// Three replay-eligible actions with mixed TTLs; MaxEffectiveTTL picks
// the largest.
rule := &s3lifecycle.Rule{
ID: "mix",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 10,
NoncurrentVersionExpirationDays: 60,
AbortMPUDaysAfterInitiation: 3,
}
snap := buildSnapshotForViews(t, "b1", rule)
got := MaxEffectiveTTL(snap)
assert.Equal(t, s3lifecycle.DaysToDuration(60), got)
}
func TestMaxEffectiveTTL_OperatesOnReplayView(t *testing.T) {
// The caller passes the *replay* snapshot here in production. Verify
// that on a replay view the answer is the max over the view's
// (active) replay actions and excludes anything that was routed away
// to walk via scan_only promotion.
rule := &s3lifecycle.Rule{
ID: "mix",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 10,
NoncurrentVersionExpirationDays: 200, // would promote at small retention
}
snap := buildSnapshotForViews(t, "b1", rule)
// Retention 30d: ExpirationDays(10) stays in replay; NoncurrentDays(200)
// promotes to walk. The replay view's MaxEffectiveTTL is therefore 10d.
replay, _ := snap.RulesForShard(0, s3lifecycle.DaysToDuration(30))
require.NotNil(t, replay)
assert.Equal(t, s3lifecycle.DaysToDuration(10), MaxEffectiveTTL(replay))
}
func TestMaxEffectiveTTL_IgnoresInactiveActions(t *testing.T) {
// Pre-BootstrapComplete event-driven action is in the snapshot but
// inactive. MaxEffectiveTTL only counts active actions.
rule := &s3lifecycle.Rule{
ID: "pending",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 30,
}
snap := New().Compile(
[]CompileInput{{Bucket: "b1", Rules: []*s3lifecycle.Rule{rule}}},
CompileOptions{}, // no PriorState → inactive
)
for _, a := range snap.actions {
require.False(t, a.IsActive(), "precondition")
}
assert.Equal(t, time.Duration(0), MaxEffectiveTTL(snap),
"inactive replay actions must not contribute to MaxEffectiveTTL")
}
func TestWalkContentHash_SeparatesWalksAndEdits(t *testing.T) {
var empty [32]byte
assert.Equal(t, empty, WalkContentHash(nil))
rules := []*s3lifecycle.Rule{
{ID: "days", Status: s3lifecycle.StatusEnabled, ExpirationDays: 30},
{ID: "date", Status: s3lifecycle.StatusEnabled, Prefix: "d/", ExpirationDate: time.Now().Add(24 * time.Hour)},
}
snap := buildSnapshotForViews(t, "b1", rules...)
_, walk := snap.RulesForShard(0, s3lifecycle.DaysToDuration(365))
require.NotNil(t, walk)
recovery := RecoveryView(snap)
assert.NotEqual(t, WalkContentHash(walk), WalkContentHash(recovery), "steady and recovery walks cover different rules")
assert.Equal(t, WalkContentHash(recovery), WalkContentHash(RecoveryView(buildSnapshotForViews(t, "b1", rules[1], rules[0]))))
edited := buildSnapshotForViews(t, "b1", rules[0], &s3lifecycle.Rule{ID: "date", Status: s3lifecycle.StatusEnabled, Prefix: "e/", ExpirationDate: rules[1].ExpirationDate})
assert.NotEqual(t, WalkContentHash(recovery), WalkContentHash(RecoveryView(edited)))
e := New()
versioned := e.Compile([]CompileInput{{Bucket: "b1", Versioned: true, Rules: rules}}, CompileOptions{})
assert.NotEqual(t, WalkContentHash(recovery), WalkContentHash(RecoveryView(versioned)), "versioning changes the listing")
}