Fix `DBImpl::GetColumnFamilyHandleUnlocked` data race (#4666)

Summary:
Hi, yiwu-arbug, I found that `DBImpl::GetColumnFamilyHandleUnlocked` still have data race condition, because `column_family_memtables_` has a stateful cache `current_` and `column_family_memtables_::Seek` maybe call without the protection of `mutex_` by a write thread

check 859dbda6e3/db/write_batch.cc (L1188)  and   859dbda6e3/db/write_batch.cc (L1756)  and  859dbda6e3/db/db_impl_write.cc (L318)

So it's better to use `versions_->GetColumnFamilySet()->GetColumnFamily` instead.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4666

Differential Revision: D13027117

Pulled By: yiwu-arbug

fbshipit-source-id: 4e3778eaf8e7f7c8577bbd78129b6a5fd7ce79fb
main
DorianZheng 6 years ago committed by Facebook Github Bot
parent d761857d56
commit 09426ae1c7
  1. 8
      db/db_impl.cc

@ -2215,16 +2215,16 @@ ColumnFamilyHandle* DBImpl::GetColumnFamilyHandle(uint32_t column_family_id) {
// REQUIRED: mutex is NOT held. // REQUIRED: mutex is NOT held.
std::unique_ptr<ColumnFamilyHandle> DBImpl::GetColumnFamilyHandleUnlocked( std::unique_ptr<ColumnFamilyHandle> DBImpl::GetColumnFamilyHandleUnlocked(
uint32_t column_family_id) { uint32_t column_family_id) {
ColumnFamilyMemTables* cf_memtables = column_family_memtables_.get();
InstrumentedMutexLock l(&mutex_); InstrumentedMutexLock l(&mutex_);
if (!cf_memtables->Seek(column_family_id)) { auto* cfd =
versions_->GetColumnFamilySet()->GetColumnFamily(column_family_id);
if (cfd == nullptr) {
return nullptr; return nullptr;
} }
return std::unique_ptr<ColumnFamilyHandleImpl>( return std::unique_ptr<ColumnFamilyHandleImpl>(
new ColumnFamilyHandleImpl(cf_memtables->current(), this, &mutex_)); new ColumnFamilyHandleImpl(cfd, this, &mutex_));
} }
void DBImpl::GetApproximateMemTableStats(ColumnFamilyHandle* column_family, void DBImpl::GetApproximateMemTableStats(ColumnFamilyHandle* column_family,

Loading…
Cancel
Save