Skip to content

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

Open
delthas wants to merge 1 commit 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 1 commit into
development/9.4from
improvement/BB-779/skip-index-on-large-collection

Conversation

@delthas

@delthas delthas commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

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

@bert-e

bert-e commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

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

bert-e commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

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

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.69%. Comparing base (f92d68c) to head (8e0341f).
⚠️ Report is 5 commits behind head on development/9.4.

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 1 file with indirect coverage changes

Components Coverage Δ
Bucket Notification 80.22% <ø> (ø)
Core Library 80.71% <ø> (-0.04%) ⬇️
Ingestion 70.63% <ø> (ø)
Lifecycle 79.65% <100.00%> (+0.19%) ⬆️
Oplog Populator 85.83% <ø> (ø)
Replication 59.71% <ø> (ø)
Bucket Scanner 85.76% <ø> (ø)
@@                 Coverage Diff                 @@
##           development/9.4    #2748      +/-   ##
===================================================
+ Coverage            74.65%   74.69%   +0.03%     
===================================================
  Files                  199      200       +1     
  Lines                13654    13682      +28     
===================================================
+ Hits                 10194    10220      +26     
- Misses                3450     3452       +2     
  Partials                10       10              
Flag Coverage Δ
api:retry 9.10% <0.00%> (-0.02%) ⬇️
api:routes 8.92% <0.00%> (-0.02%) ⬇️
bucket-scanner 85.76% <ø> (ø)
ft_test:queuepopulator 10.06% <0.00%> (-0.06%) ⬇️
ingestion 12.48% <0.00%> (-0.03%) ⬇️
lib 7.71% <0.00%> (-0.04%) ⬇️
lifecycle 18.88% <14.28%> (-0.02%) ⬇️
notification 1.02% <0.00%> (-0.01%) ⬇️
oplogPopulator 0.14% <0.00%> (-0.01%) ⬇️
replication 18.60% <0.00%> (-0.04%) ⬇️
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.

@scality scality deleted a comment from claude Bot Jun 4, 2026
@scality scality deleted a comment from claude Bot Jun 4, 2026
@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
@scality scality deleted a comment from claude Bot Jun 5, 2026
Comment thread extensions/lifecycle/util/mongoRules.js Outdated
…piration rules

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

- Collection too big: skip when count > 1M or storageSize > 1 GiB, where the
  build's transient disk usage is unpredictable on existing large collections.
- Mass-expiration rules: skip when every enabled rule has no filter and an
  Expiration action that is already-past / within ~1 day (date) or days <= 1;
  the index would add little selectivity and be churned before it pays off.
  Buckets with no enabled selective rule (all disabled / none) also skip.

Both complement the existing free-disk (BB-753) and SosAPI (BB-778) checks;
buckets that already have the v2 indexes keep using v2. The per-rule verdict
lives in util/mongoRules.js (ruleAllowsIndexSkip / allRulesAreMassExpiration),
precomputed once per bucket and plumbed via task.allRulesAreMassExpiration.

Issue: BB-779
@delthas delthas force-pushed the improvement/BB-779/skip-index-on-large-collection branch from 7523139 to 8e0341f Compare June 8, 2026 08:45
@claude

claude Bot commented Jun 8, 2026

Copy link
Copy Markdown

LGTM — both new guards are well-placed and well-tested. The mass-expiration check correctly short-circuits before the MongoDB round trips, the collection-size check reuses the existing collStats fetch, and the vacuous-truth behavior for empty/null rules is safe because the isLifecycled guard fires first. Test coverage is thorough (19 edge-case tests for mongoRules, 6 new conductor tests including the short-circuit verification). No correctness, async-handling, or metric issues found.

Review by Claude Code

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

@francoisferrand francoisferrand left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not sure if this should land in 9.4 or 9.5, let's confirm this with product team

return false;
}
if (action.date && new Date(action.date).getTime() <= dateCutoff) {
return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

that is not correct:

  • setting a date "10 years ago" will not remove a lot
  • esp. once the "mass deletion" has been done, the rule deprecates...but will still prevent creating an index

→ this condition would be something like new Date(action.date).getTime() >= currentDate.getTime() - 24 * 60 * 60 * 1000

if (action.date && new Date(action.date).getTime() <= dateCutoff) {
return true;
}
if (action.days !== undefined && action.days <= 1) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

some is not appropriate for this case: e.g. we often want to expire non-current versions "quickly", but not current versions... If we want to define an heuristic on days, should be when all actions match.

}
// Allow a day of slack: a rule expiring within ~1 day still clears the
// whole collection, same as Days <= 1.
const dateCutoff = currentDate.getTime() + 24 * 60 * 60 * 1000;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this 24h should be another constant

Comment on lines +39 to +40
const MAX_AUTO_INDEX_DOC_COUNT = 1000000;
const MAX_AUTO_INDEX_STORAGE_BYTES = 1024 * 1024 * 1024;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if we should make these "customizable" (i.e. try read from env) ?
i.e. could we have case where we want/need to override the heuristic threshold, at some customer?

(it seems we would only skip v1, max-expire objects, then create index : so not really needed, but I wonder if there could be corner cases...)

// Operates on the MongoDB internal lifecycle format (lowercase ruleStatus,
// actions[], filter.rulePrefix) — NOT the AWS S3 format used by util/rules.js.

function ruleAllowsIndexSkip(rule, currentDate = new Date()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

function name is not clear...
not sure I like allRulesAreMassExpiration, but at least it is clear → ruleIsMassExpiration ?

}

function allRulesAreMassExpiration(rules, currentDate = new Date()) {
return (rules || []).every(r => ruleAllowsIndexSkip(r, currentDate));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should it not be "any" ?
as long as we have 1 mass-expiration, everything will expire and index is not used... (for now, i.e. until the max expiration has been done)

const BUCKET_CHECKPOINT_PUSH_NUMBER = 50;
const BUCKET_CHECKPOINT_PUSH_NUMBER_BUCKETD = 50;
const ACCOUNT_SPLITTER = ':';
const MAX_AUTO_INDEX_DOC_COUNT = 1000000;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is 1'000'000 that many documents?
That would be about 1GB of total metadata usage, is it really a high-enough threshold?

(same for index: is 1GB of index really the turning point)

if (action.date && new Date(action.date).getTime() <= dateCutoff) {
return true;
}
if (action.days !== undefined && action.days <= 1) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is days a good criteria for mass-expiration?

  • Some use-case may rely on (lots of) short-lived data, and thus use these even though they could benefit form index (lots of data created in the day)
  • What we really need is one "pattern" we can document to expire the whole bucket: this is the one that really needs to be detected. Others are probably corner cases, and handled by index failure handling/...

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.

4 participants