|
| 1 | +// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
| 2 | +// This source code is licensed under both the GPLv2 (found in the |
| 3 | +// COPYING file in the root directory) and Apache 2.0 License |
| 4 | +// (found in the LICENSE.Apache file in the root directory). |
| 5 | + |
| 6 | +#include "db/version_set_deletion_scheduler.h" |
| 7 | + |
| 8 | +#include <memory> |
| 9 | + |
| 10 | +#include "db/version_set.h" |
| 11 | +#include "logging/logging.h" |
| 12 | +#include "port/port.h" |
| 13 | +#include "rocksdb/env.h" |
| 14 | +#include "util/mutexlock.h" |
| 15 | + |
| 16 | +namespace ROCKSDB_NAMESPACE { |
| 17 | + |
| 18 | +VersionSetDeletionScheduler::VersionSetDeletionScheduler(Logger* info_log) |
| 19 | + : version_delete_mutex_(), |
| 20 | + cv_(&version_delete_mutex_), |
| 21 | + deletion_queue_(), |
| 22 | + bg_thread_(nullptr), |
| 23 | + shutting_down_(false), |
| 24 | + pending_deletion_count_(0), |
| 25 | + info_log_(info_log) { |
| 26 | + // Start the background thread |
| 27 | + bg_thread_.reset(new port::Thread( |
| 28 | + &VersionSetDeletionScheduler::BackgroundDeletionThread, this)); |
| 29 | + |
| 30 | + ROCKS_LOG_INFO(info_log_, |
| 31 | + "VersionSetDeletionScheduler: Created dedicated background " |
| 32 | + "thread for storage deletion"); |
| 33 | +} |
| 34 | + |
| 35 | +VersionSetDeletionScheduler::~VersionSetDeletionScheduler() { Shutdown(); } |
| 36 | + |
| 37 | +void VersionSetDeletionScheduler::ScheduleDeletion( |
| 38 | + VersionStorageInfo* storage_info) { |
| 39 | + MutexLock lock(&version_delete_mutex_); |
| 40 | + |
| 41 | + if (shutting_down_) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + deletion_queue_.push(storage_info); |
| 46 | + pending_deletion_count_++; |
| 47 | + |
| 48 | + // Notify the background thread that work is available |
| 49 | + cv_.Signal(); |
| 50 | +} |
| 51 | + |
| 52 | +void VersionSetDeletionScheduler::Shutdown() { |
| 53 | + { |
| 54 | + MutexLock lock(&version_delete_mutex_); |
| 55 | + if (shutting_down_) { |
| 56 | + return; // Already shutting down |
| 57 | + } |
| 58 | + shutting_down_ = true; |
| 59 | + cv_.Signal(); // Wake up the background thread |
| 60 | + } |
| 61 | + |
| 62 | + // Wait for the background thread to finish |
| 63 | + if (bg_thread_ != nullptr) { |
| 64 | + bg_thread_->join(); |
| 65 | + bg_thread_.reset(); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void VersionSetDeletionScheduler::BackgroundDeletionThread() { |
| 70 | + version_delete_mutex_.Lock(); |
| 71 | + |
| 72 | + while (!shutting_down_) { |
| 73 | + // Wait for work or shutdown signal |
| 74 | + while (deletion_queue_.empty() && !shutting_down_) { |
| 75 | + cv_.Wait(); |
| 76 | + } |
| 77 | + |
| 78 | + // Process all pending deletion operations |
| 79 | + while (!deletion_queue_.empty() && !shutting_down_) { |
| 80 | + VersionStorageInfo* storage_info = deletion_queue_.front(); |
| 81 | + deletion_queue_.pop(); |
| 82 | + pending_deletion_count_--; |
| 83 | + |
| 84 | + // Unlock mutex while executing the deletion operation |
| 85 | + version_delete_mutex_.Unlock(); |
| 86 | + delete storage_info; |
| 87 | + |
| 88 | + // Re-acquire the mutex for the next iteration |
| 89 | + version_delete_mutex_.Lock(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + version_delete_mutex_.Unlock(); |
| 94 | +} |
| 95 | + |
| 96 | +} // namespace ROCKSDB_NAMESPACE |
0 commit comments