Skip to content

feat(lifecycle): skip index creation on large collections and mass-expiration rules#2748

Open
delthas wants to merge 6 commits into
development/9.4from
improvement/BB-779/skip-index-on-large-collection
Open

feat(lifecycle): skip index creation on large collections and mass-expiration rules#2748
delthas wants to merge 6 commits into
development/9.4from
improvement/BB-779/skip-index-on-large-collection

Conversation

@delthas
Copy link
Copy Markdown
Contributor

@delthas delthas commented Jun 4, 2026

Summary

Add two pre-flight heuristics to the conductor's index-creation path so we keep lifecycle on v1 (full scan) in these cases instead of paying the build cost:

  1. Collection too big — if count > 1M OR storageSize > 1 GiB, skip auto-creating v2 lifecycle indexes. The build's transient disk usage on existing large collections is unpredictable; spill files plus the final index can easily exceed 5–10× the _id_ index size during the merge phase. Risky on tight deployments.

  2. Mass-expiration rules — if every enabled rule has no filter (no rulePrefix, no tags) and an Expiration action that is either already-past or within ~1 day in the future (date) or smallest-legal (days <= 1), skip auto-creating the indexes. They'd provide little selectivity benefit and be churned out before paying for themselves. The "all rules disabled / no rules at all" case also skips (vacuous truth — no enabled selective rule to benefit from indexing).

Both guards complement (do not replace) the BB-753 free-disk check and the BB-778 SosAPI check. Buckets that already have the v2 indexes continue to use v2 regardless of these guards.

Implementation notes

  • Mass-expiration verdict is precomputed once per bucket in listMongodbBuckets and plumbed via task.allRulesAreMassExpiration.
  • Collection-size check reuses the collStats fetch BB-753 already performs — no extra round trip.
  • Rule helpers operate on the MongoDB internal lifecycle format (lowercase ruleStatus, actions[], etc.), distinct from the AWS S3 format that util/rules.js and RulesReducer.js consume. The precedent for parsing the MongoDB internal shape is LifecycleOplogPopulatorUtils.isBucketExtensionEnabled.
  • Two new metric tags on LifecycleMetrics.onLegacyTask: 'collectionTooBig' and 'massExpirationRule'.

Design decisions (open to bikeshedding)

  • "Mass expiration" naming. Plain English, avoids the DB "selectivity" convention clash (a rule that matches everything is "low selectivity" in DB terms — confusing). Open to better names if reviewers have suggestions — "blanket expiration", "wholesale expiration", etc.
  • Dedicated util/mongoRules.js file rather than inlining in LifecycleConductor.js or extending LifecycleOplogPopulatorUtils.js. Rationale: the helpers are pure rule-shape predicates, not conductor- or populator-specific. Direct unit testability (19 edge-case tests in mongoRules.spec.js) is worth the extra file. Codebase precedent: util/rules.js + util/RulesReducer.js are both single-consumer files with their own specs.
  • Precomputed boolean on the task vs. stashing the whole rules array. Chose precomputed boolean (mirrors task.isLifecycled and task.hasSosApi — both flat primitives). The alternative — task.lifecycleRules: [...] with the helper called inside _indexesGetOrCreate — would offer flexibility for future rule-shape checks but breaks the existing flat-primitive convention. If/when we accumulate 3+ rule-based heuristics, that's the natural refactor moment.

Full rationale in BB-779.

Issue: BB-779

…piration rules

Add two pre-flight heuristics to the conductor's index-creation path:

1. Collection size guard: if the collection already has more than 1M
   documents or its on-disk storage exceeds 1 GiB, skip auto-creating
   the v2 lifecycle indexes. The build's transient disk usage on existing
   large collections is unpredictable (spill files plus the final index
   can easily exceed 5-10x the _id_ index size during the merge phase),
   and can put the cluster at risk on tight deployments.

2. Mass-expiration guard: if every enabled lifecycle rule has no filter
   (no prefix, no tags) and an Expiration action that is either already-
   past (Date) or smallest-legal (Days <= 1), skip auto-creating the
   indexes. The indexes provide little selectivity benefit and would be
   churned out before paying for themselves.

Both guards complement (do not replace) the existing BB-753 free-disk
check and the BB-778 SosAPI check. Buckets that already have the v2
indexes continue to use v2 regardless of these guards.

The mass-expiration verdict is precomputed once per bucket in
listMongodbBuckets and plumbed via task.allRulesAreMassExpiration. The
collection-size check reuses the collStats fetch that BB-753 already
does — no extra round trip.

The rule helpers live in extensions/lifecycle/util/mongoRules.js and
operate on the MongoDB internal lifecycle format (lowercase ruleStatus,
actions, etc.), distinct from the AWS S3 format that
extensions/lifecycle/util/rules.js and RulesReducer.js consume.

Issue: BB-779
@bert-e
Copy link
Copy Markdown
Contributor

bert-e commented Jun 4, 2026

Hello delthas,

My role is to assist you with the merge of this
pull request. Please type @bert-e help to get information
on this process, or consult the user documentation.

Available options
name description privileged authored
/after_pull_request Wait for the given pull request id to be merged before continuing with the current one.
/bypass_author_approval Bypass the pull request author's approval
/bypass_build_status Bypass the build and test status
/bypass_commit_size Bypass the check on the size of the changeset TBA
/bypass_incompatible_branch Bypass the check on the source branch prefix
/bypass_jira_check Bypass the Jira issue check
/bypass_peer_approval Bypass the pull request peers' approval
/bypass_leader_approval Bypass the pull request leaders' approval
/approve Instruct Bert-E that the author has approved the pull request. ✍️
/create_pull_requests Allow the creation of integration pull requests.
/create_integration_branches Allow the creation of integration branches.
/no_octopus Prevent Wall-E from doing any octopus merge and use multiple consecutive merge instead
/unanimity Change review acceptance criteria from one reviewer at least to all reviewers
/wait Instruct Bert-E not to run until further notice.
Available commands
name description privileged
/help Print Bert-E's manual in the pull request.
/status Print Bert-E's current status in the pull request TBA
/clear Remove all comments from Bert-E from the history TBA
/retry Re-start a fresh build TBA
/build Re-start a fresh build TBA
/force_reset Delete integration branches & pull requests, and restart merge process from the beginning.
/reset Try to remove integration branches unless there are commits on them which do not appear on the source branch.

Status report is not available.

@scality scality deleted a comment from bert-e Jun 4, 2026
@bert-e
Copy link
Copy Markdown
Contributor

bert-e commented Jun 4, 2026

Request integration branches

Waiting for integration branch creation to be requested by the user.

To request integration branches, please comment on this pull request with the following command:

/create_integration_branches

Alternatively, the /approve and /create_pull_requests commands will automatically
create the integration branches.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.64%. Comparing base (f92d68c) to head (7523139).

Additional details and impacted files

Impacted file tree graph

Files with missing lines Coverage Δ
...tensions/lifecycle/conductor/LifecycleConductor.js 87.02% <100.00%> (+0.36%) ⬆️
extensions/lifecycle/util/mongoRules.js 100.00% <100.00%> (ø)

... and 3 files with indirect coverage changes

Components Coverage Δ
Bucket Notification 80.22% <ø> (ø)
Core Library 80.61% <ø> (-0.15%) ⬇️
Ingestion 70.63% <ø> (ø)
Lifecycle 79.67% <100.00%> (+0.20%) ⬆️
Oplog Populator 85.83% <ø> (ø)
Replication 59.67% <ø> (-0.04%) ⬇️
Bucket Scanner 85.76% <ø> (ø)
@@                 Coverage Diff                 @@
##           development/9.4    #2748      +/-   ##
===================================================
- Coverage            74.65%   74.64%   -0.02%     
===================================================
  Files                  199      200       +1     
  Lines                13654    13684      +30     
===================================================
+ Hits                 10194    10215      +21     
- Misses                3450     3459       +9     
  Partials                10       10              
Flag Coverage Δ
api:retry 9.10% <0.00%> (-0.03%) ⬇️
api:routes 8.92% <0.00%> (-0.03%) ⬇️
bucket-scanner 85.76% <ø> (ø)
ft_test:queuepopulator 9.06% <0.00%> (-1.07%) ⬇️
ingestion 12.48% <0.00%> (-0.03%) ⬇️
lib 7.73% <0.00%> (-0.02%) ⬇️
lifecycle 18.89% <13.33%> (-0.01%) ⬇️
notification 1.02% <0.00%> (-0.01%) ⬇️
oplogPopulator 0.14% <0.00%> (-0.01%) ⬇️
replication 18.60% <0.00%> (-0.05%) ⬇️
unit 51.43% <100.00%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…ongoDB calls

The mass-expiration guard is intentionally placed before the
diskUsage/collStats fetch to avoid the I/O cost on buckets we'll
skip anyway. Add an explicit test where the MongoDB client throws if
either call is made — the test passes iff the short-circuit holds.

Issue: BB-779
@scality scality deleted a comment from claude Bot Jun 4, 2026
Spec files for util/ helpers live flat under tests/unit/lifecycle/
(see rules.spec.js, RulesReducer.spec.js). Match that placement.

Issue: BB-779
@scality scality deleted a comment from claude Bot Jun 4, 2026
delthas added 2 commits June 5, 2026 11:22
Drop function-level JSDocs (name + impl already convey the contract).
Condense the mongoRules.js file header to the format-distinction warning
(real footgun). Drop threshold-constants rationale (visible from names).
Condense the mass-expiration guard comment to the non-obvious "churned
out before paying for itself" bit. Keep the empty-array carve-out inside
allRulesAreMassExpiration where the surprising no-skip behavior lives.

Issue: BB-779
Previously allRulesAreMassExpiration returned false when no rule was
enabled, on the theory that the user might re-enable a selective rule.
But that's overly defensive: nothing's running today, so we shouldn't
proactively pay the index build cost. Drop the special case so vacuous
truth applies — every() over zero enabled rules returns true, and we
skip indexing as desired.

Issue: BB-779
@scality scality deleted a comment from claude Bot Jun 5, 2026
@scality scality deleted a comment from claude Bot Jun 5, 2026
@delthas delthas marked this pull request as ready for review June 5, 2026 09:58
@delthas delthas requested review from a team, DarkIsDude and francoisferrand June 5, 2026 09:59
A Date one day in the future is semantically equivalent to Days=1: by
the next conductor cycle the rule will be expiring everything. Apply
the same 1-day slack to the Date branch as Days <= 1 already implies.

Issue: BB-779
@claude
Copy link
Copy Markdown

claude Bot commented Jun 5, 2026

LGTM — clean, well-tested implementation of the two pre-flight heuristics.

Verified:
- Guard ordering is correct: mass-expiration check short-circuits before any MongoDB calls (disk/collStats), collection-size check reuses the existing collStats fetch with no extra round trip.
- Existing v2 indexes are respected regardless of either guard (isV2 check happens first).
- Vacuous-truth behavior for empty/null/all-disabled rules is safe: non-lifecycled buckets already short-circuit at the earlier isLifecycled check.
- allRulesAreMassExpiration is only computed in the mongodb bucket source path, consistent with the existing hasSosApi pattern.
- mongoRules.js correctly targets only the MongoDB internal format (rulePrefix, actionName, ruleStatus) and handles edge cases (empty filter, missing actions, null rules, mixed action types).
- Test coverage is thorough: 19 edge-case tests for the rule helpers, plus conductor-level tests covering short-circuit behavior, v2-bypass, docCount/storageSize thresholds, and projection verification.

Review by Claude Code

@scality scality deleted a comment from claude Bot Jun 5, 2026

function allRulesAreMassExpiration(rules, currentDate = new Date()) {
return (rules || [])
.filter(r => r && r.ruleStatus === 'Enabled')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is already checked inside the isMassExpirationRule ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants