Files
Chris Lu 443ae1cd83 s3 lifecycle: share daily bucket walk and prune listings (#11797)
* s3 lifecycle: share the daily bucket walk across shards

The daily pass invoked the walker once per shard, and every shard's
walk listed the full bucket tree while keeping only its own 1/16 of
the entries — 16 full listings per pass by default (#11791).

Run now groups the shards by the walk branch they'll take (recovery
vs steady, using the same cursor data and hash checks runShard applies)
and starts one shared walk per group, so a pass lists each bucket at
most twice. WalkerFunc and WalkBuckets take a shard set instead of a
single shard. runShard keeps its own decision and waits on its group's
walk; a shard whose freshly-loaded cursor disagrees with the plan falls
back to a per-shard walk, so mid-pass cursor rewrites can't under-cover
a shard.

* s3 lifecycle: skip the .versions/ pre-pass on non-versioned buckets

walkBucketTree listed every directory twice — once looking for
.versions/ dirs, once for regular entries. On a bucket that never had
versioning enabled the first pass can only find coincidentally-named
user folders, so the extra listing is pure cost (#11791).

FilerListFunc now takes the walk view and consults
Snapshot.BucketVersioned; non-versioned buckets skip pass 1 entirely.
A *-suffixed .versions user folder on such a bucket is recursed into
as a regular directory so real objects inside still surface; a nil
view keeps the two-pass behavior.

* s3 lifecycle: prune walk directories to active rule prefixes

The daily walk listed every directory under a bucket even when no
rule prefix could reach it. Collect the active rule prefixes of each
bucket from the walk view (new Snapshot.BucketPrefixes) and skip
recursing into directories whose keys can never match; a bucket
with no active actions is skipped entirely, saving the listing.

.uploads stays in scope regardless of prefixes because AbortMPU
rules match DestKey, and .versions expansion is pruned only
conservatively (the logical key is a string-prefix of the dir key).

* s3 lifecycle: join planned walks before Run returns

planWalks starts a shared walk goroutine per group, but a shard whose
authoritative cursor load then fails exits runShard without waiting on
it. Run returned while the walk still listed or dispatched deletes
against connections the caller closes on return. Join every planned
walk after the shard waitgroup.

* s3 lifecycle: treat object-locked buckets as versioned in the walk

Object Lock requires versioning and every writer sets both extended
attributes, but IsBucketVersioned only read ExtVersioningKey. A locked
bucket missing that attribute would have its .versions dirs walked as
ordinary folders, hiding version siblings from lifecycle. Classify
ExtObjectLockEnabledKey as versioned too.
2026-10-17 15:21:53 -07:00

118 lines
4.0 KiB
Go

package scheduler
import (
"context"
"strings"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/lifecycle_xml"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle/engine"
)
// The bucket lifecycle configuration lives here rather than in weed/s3api so
// callers outside the gateway (the admin dashboard, the scheduler itself) and
// the gateway share one definition of the extended-attribute keys and the
// size cap. weed/s3api aliases these; nothing redeclares the values.
const (
BucketLifecycleConfigurationXMLKey = "s3-bucket-lifecycle-configuration-xml"
BucketLifecycleTransitionMinimumObjectSizeKey = "s3-bucket-lifecycle-transition-default-minimum-object-size"
// MaxBucketLifecycleConfigurationSize caps the serialized XML, mirroring
// the S3 API's limit.
MaxBucketLifecycleConfigurationSize = 1 << 20
)
// ParseError is returned alongside successfully-loaded inputs so callers can
// surface malformed bucket configs rather than silently dropping them.
type ParseError struct {
Bucket string
Err error
}
// LoadCompileInputs walks the buckets directory and returns one
// engine.CompileInput per bucket that carries a non-empty lifecycle
// configuration. Pagination loops with startFrom so clusters with more
// than one page of buckets don't drop the tail.
func LoadCompileInputs(ctx context.Context, client filer_pb.SeaweedFilerClient, bucketsPath string) ([]engine.CompileInput, []ParseError, error) {
var (
inputs []engine.CompileInput
parseErrors []ParseError
startFrom string
)
const pageSize uint32 = 1024
for {
pageCount := 0
var lastName string
err := filer_pb.SeaweedList(ctx, client, bucketsPath, "", func(entry *filer_pb.Entry, isLast bool) error {
pageCount++
lastName = entry.Name
if !entry.IsDirectory {
return nil
}
xmlBytes, ok := entry.Extended[BucketLifecycleConfigurationXMLKey]
if !ok || len(xmlBytes) == 0 {
return nil
}
rules, err := lifecycle_xml.ParseCanonical(xmlBytes)
if err != nil {
parseErrors = append(parseErrors, ParseError{Bucket: entry.Name, Err: err})
return nil
}
if len(rules) == 0 {
return nil
}
inputs = append(inputs, engine.CompileInput{
Bucket: entry.Name,
Rules: rules,
Versioned: IsBucketVersioned(entry),
})
return nil
}, startFrom, false, pageSize)
if err != nil {
return nil, nil, err
}
if uint32(pageCount) < pageSize {
break
}
startFrom = lastName
}
return inputs, parseErrors, nil
}
// IsBucketVersioned returns true when the bucket's filer entry has the
// versioning extended attribute set to Enabled or Suspended, or when
// Object Lock is on: lock requires versioning, so the gateway writes
// version siblings even if the versioning attribute is absent.
func IsBucketVersioned(entry *filer_pb.Entry) bool {
if v, ok := entry.Extended[s3_constants.ExtVersioningKey]; ok {
s := strings.ToLower(strings.TrimSpace(string(v)))
if s == "enabled" || s == "suspended" {
return true
}
}
return string(entry.Extended[s3_constants.ExtObjectLockEnabledKey]) == s3_constants.ObjectLockEnabled
}
// AllActivePriorStates seeds every compiled action as bootstrap-complete +
// event-driven so the scheduler dispatches whatever fires immediately.
// Production bootstrap walks set this incrementally per bucket; the
// scheduler runs out-of-band of that flow for now.
func AllActivePriorStates(inputs []engine.CompileInput) map[s3lifecycle.ActionKey]engine.PriorState {
prior := map[s3lifecycle.ActionKey]engine.PriorState{}
for _, in := range inputs {
for _, rule := range in.Rules {
hash := s3lifecycle.RuleHash(rule)
for _, kind := range s3lifecycle.RuleActionKinds(rule) {
key := s3lifecycle.ActionKey{Bucket: in.Bucket, RuleHash: hash, ActionKind: kind}
prior[key] = engine.PriorState{
BootstrapComplete: true,
Mode: engine.ModeEventDriven,
}
}
}
}
return prior
}