Skip to content

feat: add new cleanup policy to retain latest n versions - #4614

Merged
jackye1995 merged 4 commits into
lance-format:mainfrom
wojiaodoubao:cleanup-retain-n
Sep 10, 2025
Merged

jackye1995 merged 4 commits into
lance-format:mainfrom
wojiaodoubao:cleanup-retain-n

Conversation

@wojiaodoubao

Copy link
Copy Markdown
Contributor

Related to #4036

@github-actions github-actions Bot added the enhancement New feature or request label Sep 2, 2025
@codecov-commenter

codecov-commenter commented Sep 2, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.86957% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.59%. Comparing base (103006f) to head (702214b).
⚠️ Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
rust/lance/src/dataset/cleanup.rs 85.79% 22 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4614      +/-   ##
==========================================
- Coverage   80.62%   80.59%   -0.03%     
==========================================
  Files         317      317              
  Lines      118785   119426     +641     
  Branches   118785   119426     +641     
==========================================
+ Hits        95769    96256     +487     
- Misses      19568    19714     +146     
- Partials     3448     3456       +8     
Flag Coverage Δ
unittests 80.59% <85.86%> (-0.03%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@wjones127 wjones127 self-assigned this Sep 2, 2025
Comment thread rust/lance/src/dataset/cleanup.rs Outdated
dataset: &'a Dataset,
before: DateTime<Utc>,
policy: CleanupPolicy,
delete_unverified: bool,

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.

why delete_unverified and error_if_old_versions_tagged are not a part of the policy?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My thoughts on this is: the CleanupPolicy only determines which manifests need to be cleaned. The delete_unverified and error_if_old_versions_tagged determines the behavior of cleanup task.

#[derive(Clone, Debug)]
struct CleanupTask<'a> {
    dataset: &'a Dataset,
    /// The policy determines which versions should be cleanup
    policy: CleanupPolicy,
    /// If true, delete unverified data files even if they are recent
    delete_unverified: bool,
    /// If true, return an Error if a tagged version is old
    error_if_old_versions_tagged: bool,
}

Comment thread rust/lance/src/dataset/cleanup.rs Outdated
}

#[derive(Debug, Clone)]
pub enum CleanupPolicy {

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 feel it makes more sense for this to be a struct, instead of an enum, so that people can express composite policies like "cleanup all versions before version X and also timestamp Y"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

"composite policies" is a great idea. Let me try this.

Comment thread rust/lance/src/dataset/cleanup.rs Outdated
return Ok(None);
}

// TODO: Support complex composite policies.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We need to support configuring more complex composite policies. May be we can add a new key lance.auto_cleanup.cleanup_policy, and the value could be something like this.

{
  "must": [{"retian_versions": 100}],
  "should":[],
  "must_not": [{"older_than": "2025-03-10T12:00:00Z"}],
}

I think may be I can add this at another pr, so I can keep this one clean.

@wojiaodoubao

Copy link
Copy Markdown
Contributor Author

Hi @jackye1995 @wjones127 , could you help review this when you have time, thanks very much!

@jackye1995

Copy link
Copy Markdown
Contributor

@wojiaodoubao I am thinking if doing this composite policy is a bit too much of a complication. I like the fact that it is trying to make the framework extensible, but when I said "composite policy", I was just thinking about the case that user gives a condition both for time and number of versions, because in your original version, the code allows either specifying time or version but not both.

When I look into other expiration APIs like Iceberg ExpireSnapshots, allowing users to configure these 2 seems sufficient, and we can just allow the API (e.g. having a CleanupBuilder) to take both and then handle the case that user specify both conditions. What do you think?

@wojiaodoubao

wojiaodoubao commented Sep 6, 2025 •

Copy link
Copy Markdown
Contributor Author

@wojiaodoubao I am thinking if doing this composite policy is a bit too much of a complication. I like the fact that it is trying to make the framework extensible, but when I said "composite policy", I was just thinking about the case that user gives a condition both for time and number of versions, because in your original version, the code allows either specifying time or version but not both.

When I look into other expiration APIs like Iceberg ExpireSnapshots, allowing users to configure these 2 seems sufficient, and we can just allow the API (e.g. having a CleanupBuilder) to take both and then handle the case that user specify both conditions. What do you think?

Thanks @jackye1995 your detailed explanation, it makes sense to me. I think I also understand why you said "delete_unverified and error_if_old_versions_tagged part of policy", we can use a CleanupPolicy struct to wrap all. Let me fix it.

@wojiaodoubao
wojiaodoubao force-pushed the cleanup-retain-n branch 2 times, most recently from 2b64fca to 0b02218 Compare September 6, 2025 14:19
@wojiaodoubao

Copy link
Copy Markdown
Contributor Author

Hi @jackye1995 , I have updated the pr. Please review when you have time, thanks very much!

@jackye1995
jackye1995 self-requested a review September 8, 2025 19:05
Comment thread rust/lance/src/dataset/cleanup.rs Outdated
policy: CleanupPolicy,
}

impl CleanupBuilder {

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 should be CleanupPolicyBuilder? Since it's building a policy.

When I say having a CleanupBuilder, I was more thinking about

CleanupBuilder::new(dataset)
  . before_timestamp(...)
  .execute()

kind of experience similar to MergeInsertBuilder, but the current one seems also fine.

if let Some(unmodified_since_val) = unmodified_since {
output
.try_filter(move |file| future::ready(file.last_modified < unmodified_since_val))
.try_filter(move |file| future::ready(file.last_modified <= unmodified_since_val))

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.

ah nice catch! I think you are right

@jackye1995 jackye1995 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.

looks good to me!

@jackye1995
jackye1995 merged commit e842a8f into lance-format:main Sep 10, 2025
jackye1995 pushed a commit to jackye1995/lance that referenced this pull request Jan 21, 2026
Xuanwo added a commit that referenced this pull request Oct 23, 2026
## Summary

- `cleanup_old_versions` clamped its `data/` listing to
`earliest_retained_manifest_time`. That floor is only sound for a file
owned by a manifest being removed; a file owned by *no* manifest —
abandoned `write_fragments()` output — has an mtime unrelated to it.
- Such a file is documented to be removed on its age, but it was never
listed, so its age was never consulted and `delete_unverified=true` had
no effect on it.
- When every aged version is retained (e.g. pinned by a tag) no manifest
is deleted, so #8708's escape hatch cannot fire and the floor never
advances: the orphan is collected by no run at all.
- The `data/` listing now runs through the moving unverified threshold —
`earliest_retained.max(verification_threshold)` — and drops the cutoff
entirely under `delete_unverified`, where a file is a candidate at any
age. `CleanupInspection::listing_unmodified_since` is unchanged, so
#8708's sparse-retain case and the other listings behave exactly as
before.
- This restores an advancing cutoff, which is what the listing had
before #4614 (the caller's `now - older_than`, per #8516). #8516 and
#8705 reported the tagged facet of the same clamp and #8708 fixed it;
this is the facet left over, reachable with no tag at all.
- Documents the contract the change relies on, which the Rust API and
the guide had drifted from: an unverifiable file is removed once it is
at least 7 days old, that threshold is fixed and independent of
`older_than`, and `delete_unverified` removes the age protection
entirely and requires that no other process is operating on the dataset.
The Python docstring already said this.
- Adds `cleanup_deletes_aged_orphan_newer_than_retained_manifests`,
covering both paths: an aged orphan under the default policy, and a
fresh orphan under `delete_unverified`. Both are written after every
manifest, under suffix-only retention with no tag anywhere.

`_transactions/` and `_deletions/` are clamped the same way and can
strand a file for the same reason. Left for a follow-up so each gets its
own regression test rather than widening destructive discovery across
four subtrees at once.

Fixes #8942

## Test plan

- [x] `cargo fmt --all --check` — clean
- [x] `cargo check -p lance --tests` — clean
- [x] `cargo clippy -p lance --tests -- -D warnings` — clean
- [x] `cargo test -p lance --lib dataset::cleanup` — 46 passed, 0
failed, including both new cases and #8708's
`cleanup_deletes_data_files_newer_than_tagged_version`
- [x] Mutation check: reverting the `data/` cutoff to `unmodified_since`
fails `case_1_aged`; removing only the `delete_unverified` arm fails
`case_2_delete_unverified`. Each case is gated on the branch it covers,
and no other test changes outcome.

---------

Co-authored-by: Jennifer Mell <[email protected]>
Co-authored-by: Xuanwo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants