lifecycle worker: NoncurrentVersionExpiration support (#8810)

* lifecycle worker: add NoncurrentVersionExpiration support

Add version-aware scanning to the rule-based execution path. When the
walker encounters a .versions directory, processVersionsDirectory():
- Lists all version entries (v_<versionId>)
- Sorts by version timestamp (newest first)
- Walks non-current versions with ShouldExpireNoncurrentVersion()
  which handles both NoncurrentDays and NewerNoncurrentVersions
- Extracts successor time from version IDs (both old/new format)
- Skips delete markers in noncurrent version counting
- Falls back to entry Mtime when version ID timestamp is unavailable

Helper functions:
- sortVersionsByTimestamp: insertion sort by version ID timestamp
- getEntryVersionTimestamp: extracts timestamp with Mtime fallback

* lifecycle worker: address review feedback for noncurrent versions

- Use sentinel errLimitReached in versions directory handler
- Set NoncurrentIndex on ObjectInfo for proper NewerNoncurrentVersions
  evaluation

* lifecycle worker: fail closed on XML parse error, guard zero Mtime

- Fail closed when lifecycle XML exists but fails to parse, instead
  of falling back to TTL which could apply broader rules
- Guard Mtime > 0 before using time.Unix(mtime, 0) to avoid mapping
  unset Mtime to 1970, which would misorder versions and cause
  premature expiration

* lifecycle worker: count delete markers toward NoncurrentIndex

Noncurrent delete markers should count toward the
NewerNoncurrentVersions retention threshold so data versions
get the correct position index. Previously, skipping delete
markers without incrementing the index could retain too many
versions after delete/recreate cycles.

* lifecycle worker: fix version ordering, error propagation, and fail-closed scope

1. Use full version ID comparison (CompareVersionIds) for sorting
   .versions entries, not just decoded timestamps. Two versions with
   the same timestamp prefix but different random suffixes were
   previously misordered, potentially treating the newest version as
   noncurrent and deleting it.

2. Propagate .versions listing failures to the caller instead of
   swallowing them with (nil, 0). Transient filer errors on a
   .versions directory now surface in the job result.

3. Narrow the fail-closed path to only malformed lifecycle XML
   (errMalformedLifecycleXML). Transient filer LookupEntry errors
   now fall back to TTL with a warning, matching the original intent
   of "fail closed on bad config, not on network blips."

* lifecycle worker: only skip .uploads at bucket root

* lifecycle worker: sort.Slice, mixed-format test, XML presence tracking

- Replace manual insertion sort with sort.Slice in sortVersionsByVersionId
- Add TestCompareVersionIdsMixedFormats covering old/new format ordering
- Distinguish "no lifecycle XML" (nil) from "XML present but no effective
  rules" (non-nil empty slice) so buckets with all-disabled rules don't
  incorrectly fall back to filer.conf TTL expiration

* lifecycle worker: guard nil Attributes, use TrimSuffix in test

- Guard entry.Attributes != nil before accessing GetFileSize() and
  Mtime in both listExpiredObjectsByRules and processVersionsDirectory
- Use strings.TrimPrefix/TrimSuffix in TestVersionsDirectoryNaming
  to match the production code pattern

* lifecycle worker: skip TTL scan when XML present, fix test assertions

- When lifecycle XML is present but has no effective rules, skip
  object scanning entirely instead of falling back to TTL path
- Test sort output against concrete expected names instead of
  re-using the same comparator as the sort itself

---------

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Chris Lu
2026-03-28 12:58:21 -07:00
committed by GitHub
parent 9c3bc138a0
commit f6ec9941cb
4 changed files with 359 additions and 17 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/xml"
"errors"
"fmt"
"time"
@@ -65,8 +66,18 @@ type abortMPU struct {
DaysAfterInitiation int `xml:"DaysAfterInitiation"`
}
// errMalformedLifecycleXML indicates the lifecycle XML exists but could not be parsed.
// Callers should fail closed (not fall back to TTL) to avoid broader deletions.
var errMalformedLifecycleXML = errors.New("malformed lifecycle XML")
// loadLifecycleRulesFromBucket reads the lifecycle XML from a bucket's
// metadata and converts it to evaluator-friendly rules.
//
// Returns:
// - (rules, nil) when lifecycle XML is configured and parseable
// - (nil, nil) when no lifecycle XML is configured (caller should use TTL fallback)
// - (nil, errMalformedLifecycleXML) when XML exists but is malformed (fail closed)
// - (nil, err) for transient filer errors (caller should use TTL fallback with warning)
func loadLifecycleRulesFromBucket(
ctx context.Context,
client filer_pb.SeaweedFilerClient,
@@ -78,6 +89,7 @@ func loadLifecycleRulesFromBucket(
Name: bucket,
})
if err != nil {
// Transient filer error — not the same as malformed XML.
return nil, fmt.Errorf("lookup bucket %s: %w", bucket, err)
}
if resp.Entry == nil || resp.Entry.Extended == nil {
@@ -87,7 +99,17 @@ func loadLifecycleRulesFromBucket(
if len(xmlData) == 0 {
return nil, nil
}
return parseLifecycleXML(xmlData)
rules, parseErr := parseLifecycleXML(xmlData)
if parseErr != nil {
return nil, fmt.Errorf("%w: bucket %s: %v", errMalformedLifecycleXML, bucket, parseErr)
}
// Return non-nil empty slice when XML was present but yielded no rules
// (e.g., all rules disabled). This lets callers distinguish "no XML" (nil)
// from "XML present, no effective rules" (empty slice).
if rules == nil {
rules = []s3lifecycle.Rule{}
}
return rules, nil
}
// parseLifecycleXML parses lifecycle configuration XML and converts it