diff --git a/db/flush_scheduler.cc b/db/flush_scheduler.cc index 8735a6b36..9c6c04efe 100644 --- a/db/flush_scheduler.cc +++ b/db/flush_scheduler.cc @@ -13,9 +13,11 @@ namespace rocksdb { void FlushScheduler::ScheduleFlush(ColumnFamilyData* cfd) { #ifndef NDEBUG - std::lock_guard lock(checking_mutex_); - assert(checking_set_.count(cfd) == 0); - checking_set_.insert(cfd); + { + std::lock_guard lock(checking_mutex_); + assert(checking_set_.count(cfd) == 0); + checking_set_.insert(cfd); + } #endif // NDEBUG cfd->Ref(); // Suppress false positive clang analyzer warnings. @@ -32,9 +34,6 @@ void FlushScheduler::ScheduleFlush(ColumnFamilyData* cfd) { } ColumnFamilyData* FlushScheduler::TakeNextColumnFamily() { -#ifndef NDEBUG - std::lock_guard lock(checking_mutex_); -#endif // NDEBUG while (true) { if (head_.load(std::memory_order_relaxed) == nullptr) { return nullptr; @@ -47,9 +46,12 @@ ColumnFamilyData* FlushScheduler::TakeNextColumnFamily() { delete node; #ifndef NDEBUG - auto iter = checking_set_.find(cfd); - assert(iter != checking_set_.end()); - checking_set_.erase(iter); + { + std::lock_guard lock(checking_mutex_); + auto iter = checking_set_.find(cfd); + assert(iter != checking_set_.end()); + checking_set_.erase(iter); + } #endif // NDEBUG if (!cfd->IsDropped()) { @@ -65,12 +67,12 @@ ColumnFamilyData* FlushScheduler::TakeNextColumnFamily() { } bool FlushScheduler::Empty() { -#ifndef NDEBUG - std::lock_guard lock(checking_mutex_); -#endif // NDEBUG auto rv = head_.load(std::memory_order_relaxed) == nullptr; #ifndef NDEBUG - assert(rv == checking_set_.empty()); + std::lock_guard lock(checking_mutex_); + // Empty is allowed to be called concurrnetly with ScheduleFlush. It would + // only miss the recent schedules. + assert((rv == checking_set_.empty()) || rv); #endif // NDEBUG return rv; }