@@ -26,6 +26,12 @@ Redis::Redis(Storage* const s, const DataType& type)
2626}
2727
2828Redis::~Redis () {
29+ stop_big_keys_log_ = true ;
30+ std::lock_guard<std::mutex> lock (bg_thread_mutex_);
31+ bg_thread_cond_.notify_all ();
32+ std::unique_lock<std::mutex> lock (bg_thread_mutex_);
33+ bg_thread_exit_cond_.wait_for (lock, std::chrono::milliseconds (200 ),
34+ [this ]() { return this ->bg_threads_count_ .load () == 0 ; });
2935 std::vector<rocksdb::ColumnFamilyHandle*> tmp_handles = handles_;
3036 handles_.clear ();
3137 for (auto handle : tmp_handles) {
@@ -36,7 +42,7 @@ Redis::~Redis() {
3642 if (default_compact_range_options_.canceled ) {
3743 delete default_compact_range_options_.canceled ;
3844 }
39- stop_big_keys_log_ = true ;
45+
4046}
4147
4248Status Redis::GetScanStartPoint (const Slice& key, const Slice& pattern, int64_t cursor, std::string* start_point) {
@@ -252,7 +258,9 @@ void Redis::CheckAndRecordBigKeys(
252258}
253259void Redis::StartBigKeysExpireCheckThread () {
254260 std::thread ([this ]() {
255- while (!stop_big_keys_log_) {
261+ std::lock_guard<std::mutex> lock (bg_thread_mutex_);
262+ this ->bg_threads_count_ ++;
263+ while (!this ->stop_big_keys_log_ .load ()) {
256264 {
257265 std::lock_guard<std::mutex> lock (big_keys_mutex_);
258266 std::vector<std::string> expired_keys;
@@ -270,12 +278,21 @@ void Redis::StartBigKeysExpireCheckThread() {
270278 }
271279 }
272280 }
281+ std::unique_lock<std::mutex> lock (bg_thread_mutex_);
282+ bg_thread_cond_.wait_for (lock,
283+ std::chrono::minutes (storage_->bigkeys_log_interval_ ),
284+ [this ]() { return this ->stop_big_keys_log_ .load (); });
285+ std::lock_guard<std::mutex> lock (bg_thread_mutex_);
286+ this ->bg_threads_count_ --;
287+ bg_thread_exit_cond_.notify_all ();
273288 std::this_thread::sleep_for (std::chrono::minutes (storage_->bigkeys_log_interval_ ));
274289 }
275290 }).detach ();
276291}
277292void Redis::StartBigKeysLogThread () {
278293 std::thread ([this ]() {
294+ std::lock_guard<std::mutex> lock (bg_thread_mutex_);
295+ this ->bg_threads_count_ ++;
279296 while (!stop_big_keys_log_) {
280297 {
281298 uint32_t current_log_interval = storage_->bigkeys_log_interval_ ;
@@ -306,6 +323,13 @@ void Redis::StartBigKeysLogThread() {
306323 }
307324 }
308325 }
326+ std::unique_lock<std::mutex> lock (bg_thread_mutex_);
327+ bg_thread_cond_.wait_for (lock,
328+ std::chrono::minutes (storage_->bigkeys_log_interval_ ),
329+ [this ]() { return this ->stop_big_keys_log_ .load (); });
330+ std::lock_guard<std::mutex> lock (bg_thread_mutex_);
331+ this ->bg_threads_count_ --;
332+ bg_thread_exit_cond_.notify_all ();
309333 std::this_thread::sleep_for (std::chrono::minutes (storage_->bigkeys_log_interval_ ));
310334 }
311335 }).detach ();
0 commit comments