Skip to content

Commit

Permalink
Improve shutdown handling in blob_file_garbage_collector
Browse files Browse the repository at this point in the history
  • Loading branch information
umegane committed Feb 19, 2025
1 parent 335f5a6 commit 3a30365
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/limestone/blob_file_garbage_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ using limestone::api::log_entry;

// Iterate recursively over the root directory.
for (boost::filesystem::recursive_directory_iterator it(root), end; it != end; ++it) {
if (boost::filesystem::is_regular_file(it->path())) {
if (shutdown_requested_.load(std::memory_order_acquire)) {
break;
}
if (boost::filesystem::is_regular_file(it->path())) {
const boost::filesystem::path& file_path = it->path();

// Use blob_file_resolver's function to check if this file is a valid blob file.
Expand Down Expand Up @@ -131,6 +134,9 @@ using limestone::api::log_entry;
// Calculate the difference and perform deletion operations
scanned_blobs_->diff(*gc_exempt_blob_);
for (const auto &id : *scanned_blobs_) {
if (shutdown_requested_.load(std::memory_order_acquire)) {
break;
}
boost::filesystem::path file_path = resolver_->resolve_path(id);
boost::system::error_code ec;
file_ops_->remove(file_path, ec);
Expand Down Expand Up @@ -179,6 +185,11 @@ using limestone::api::log_entry;
}

void blob_file_garbage_collector::shutdown() {
// Use a dedicated mutex to ensure shutdown() is executed exclusively.
std::lock_guard<std::mutex> shutdown_lock(shutdown_mutex_);

shutdown_requested_.store(true, std::memory_order_release);

wait_for_blob_file_scan();
wait_for_scan_snapshot();
wait_for_cleanup();
Expand All @@ -193,6 +204,8 @@ using limestone::api::log_entry;
cleanup_thread_.join();
}
reset();

shutdown_requested_.store(false, std::memory_order_release);
}

void blob_file_garbage_collector::scan_snapshot(const boost::filesystem::path &snapshot_file, const boost::filesystem::path &compacted_file) {
Expand All @@ -209,12 +222,15 @@ using limestone::api::log_entry;
try {
auto cur = std::make_unique<my_cursor>(snapshot_file, compacted_file);
while (cur->next()) {
if (cur->type() == log_entry::entry_type::normal_with_blob) {
auto blob_ids = cur->blob_ids();
for (auto id : blob_ids) {
gc_exempt_blob_->add_blob_id(id);
}
}
if (shutdown_requested_.load(std::memory_order_acquire)) {
break;
}
if (cur->type() == log_entry::entry_type::normal_with_blob) {
auto blob_ids = cur->blob_ids();
for (auto id : blob_ids) {
gc_exempt_blob_->add_blob_id(id);
}
}
}
finalize_scan_and_cleanup();
} catch (const limestone_exception &e) {
Expand Down
2 changes: 2 additions & 0 deletions src/limestone/blob_file_garbage_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ class blob_file_garbage_collector {
// --- Others ---
mutable std::mutex mutex_; ///< Mutex for synchronizing access to state variables.
std::unique_ptr<file_operations> file_ops_; ///< Pointer to the file_operations implementation.
std::mutex shutdown_mutex_; ///< Mutex to ensure shutdown() is executed exclusively.
std::atomic_bool shutdown_requested_{false}; ///< Shutdown flag indicating if shutdown has been requested.

/**
* @brief The background function that scans the blob_root directory for BLOB files.
Expand Down

0 comments on commit 3a30365

Please sign in to comment.