Skip to content

support canceling ongoing CompactFiles#13687

Closed
virajthakur wants to merge 8 commits intofacebook:mainfrom
virajthakur:viraj-compactfiles-cancel
Closed

support canceling ongoing CompactFiles#13687
virajthakur wants to merge 8 commits intofacebook:mainfrom
virajthakur:viraj-compactfiles-cancel

Conversation

@virajthakur
Copy link
Contributor

@virajthakur virajthakur commented Jun 12, 2025

Add an atomic bool to CompactionOptions to cancel an ongoing CompactFiles() operation, in the same fashion we do for CompactRange().

Test Plan: ./db_test2 --gtest_filter=DBTest2.TestCancelCompactFiles

@virajthakur virajthakur marked this pull request as draft June 12, 2025 20:16
@virajthakur virajthakur requested a review from Copilot June 12, 2025 20:21
@facebook-github-bot
Copy link
Contributor

@virajthakur has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds support for canceling ongoing manual CompactFiles() operations by introducing a cancellation flag, wiring it into the compaction logic, and exercising it in a new test.

  • Introduce std::atomic<bool>* canceled in CompactionOptions with default initialization.
  • Update DBImpl::CompactFilesImpl to check the new cancel flag alongside existing pause logic.
  • Add TestCancelCompactFiles in db_test2.cc and fix minor typos/removals in test files.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
include/rocksdb/options.h Added canceled flag to CompactionOptions and initialized it to nullptr.
db/db_impl/db_impl_compaction_flush.cc Wired the cancel flag into CompactFilesImpl and registered a syncpoint callback.
db/db_test2.cc Removed unused header, fixed typo, and added TestCancelCompactFiles.
Comments suppressed due to low confidence (4)

include/rocksdb/options.h:2229

  • [nitpick] Using a raw pointer for the cancellation flag can lead to dangling pointers if CompactionOptions is copied or the referenced atomic goes out of scope. Consider owning an std::atomic<bool> directly or using a safer handle (e.g., std::optional<std::reference_wrapper<std::atomic<bool>>>) to manage lifetime.
std::atomic<bool>* canceled;

include/rocksdb/options.h:2229

  • Document that the atomic<bool>* canceled pointer must remain valid for the entire duration of the compaction to avoid undefined behavior.
std::atomic<bool>* canceled;

db/db_test2.cc:5693

  • [nitpick] Variable name disable_compaction is a bit generic; consider renaming to disable_manual_compaction for clarity.
bool disable_compaction = false;

db/db_impl/db_impl_compaction_flush.cc:1443

  • Add a unit test where CompactionOptions.canceled remains nullptr to verify that compaction proceeds normally and no null-pointer dereference occurs.
(compact_options.canceled &&

Viraj Thakur added 2 commits June 12, 2025 14:43
@facebook-github-bot
Copy link
Contributor

@virajthakur has updated the pull request. You must reimport the pull request before landing.

@virajthakur virajthakur requested a review from cbi42 June 12, 2025 21:45
@facebook-github-bot
Copy link
Contributor

@virajthakur has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@virajthakur has updated the pull request. You must reimport the pull request before landing.

@virajthakur virajthakur marked this pull request as ready for review June 12, 2025 21:47
@facebook-github-bot
Copy link
Contributor

@virajthakur has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@virajthakur virajthakur changed the title support canceling ongoing CompactFilestasks support canceling ongoing CompactFiles Jun 13, 2025
Copy link
Contributor

@jaykorean jaykorean left a comment

Choose a reason for hiding this comment

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

LGTM!

ROCKSDB_NAMESPACE::EnvOptions(), options};

// ingest large SST files
std::vector<std::string> external_sst_file_names;
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that we are ingesting files for this test. This seems to be the same as what TestCompactFiles do and this is totally fine.

Just FYI in case you want to just call PUTs, flush them and call CompactFiles() on those SST files without knowing the file names in advance.

std::vector<std::string> inputs;
{
std::vector<LiveFileMetaData> fmetas;
db_->GetLiveFilesMetaData(&fmetas);
bool included_one_l1 = false;
for (const auto& meta : fmetas) {
if (meta.level == 0) {
inputs.emplace_back(meta.relative_filename);
} else if (!included_one_l1) {
inputs.emplace_back(meta.relative_filename);
included_one_l1 = true;
}
}
}
ASSERT_EQ(static_cast<size_t>(3), inputs.size());
{
std::vector<std::string> output_file_names;
CompactionJobInfo compaction_job_info;
ASSERT_OK(db_->CompactFiles(CompactionOptions(), inputs, /*output_level=*/1,
/*output_path_id=*/-1, &output_file_names,
&compaction_job_info));
ASSERT_EQ(kNumFiles * kKeysPerFile + 2, output_file_names.size());

@facebook-github-bot
Copy link
Contributor

@virajthakur has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@virajthakur has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@virajthakur merged this pull request in 4bdfb7e.

@cbi42
Copy link
Contributor

cbi42 commented Jun 16, 2025

IIUC, this only cancels CompactFiles() before compaction starts running. We should be able to cancel an ongoing compaction similar to CompactRange():

is_manual ? manual_compaction->canceled

SoujanyaPonnapalli pushed a commit to tyler-griggs/rocksdb that referenced this pull request Jul 20, 2025
Summary:
Add an atomic bool to CompactionOptions to cancel an ongoing CompactFiles() operation, in the same fashion we do for CompactRange().

Pull Request resolved: facebook#13687

Test Plan: ./db_test2 --gtest_filter=DBTest2.TestCancelCompactFiles

Reviewed By: jaykorean

Differential Revision: D76538529

Pulled By: virajthakur

fbshipit-source-id: 77db5b4fb4cbd5280584834df28e51a72b084dab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants