Add checking for DB::DestroyColumnFamilyHandle() (#9347)

Summary:
Closing https://github.com/facebook/rocksdb/issues/5006

Calling `DB::DestroyColumnFamilyHandle(column_family)` with `column_family` being the return value of
`DB::DefaultColumnFamily()` will return `Status::InvalidArgument()`.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/9347

Test Plan: make check

Reviewed By: akankshamahajan15

Differential Revision: D33369675

Pulled By: riversand963

fbshipit-source-id: a8266a4daddf2b7a773c2dc7f3eb9a4adfb6b6dd
This commit is contained in:
Yanqin Jin 2022-01-05 20:25:57 -08:00 committed by Facebook GitHub Bot
parent 6892f19b11
commit b2e53ab2d8
3 changed files with 36 additions and 0 deletions

View File

@ -5,6 +5,9 @@
* Added `TraceOptions::preserve_write_order`. When enabled it guarantees write records are traced in the same order they are logged to WAL and applied to the DB. By default it is disabled (false) to match the legacy behavior and prevent regression.
* Made the Env class extend the Customizable class. Implementations need to be registered with the ObjectRegistry and to implement a Name() method in order to be created via this method.
### Behavior Changes
* `DB::DestroyColumnFamilyHandle()` will return Status::InvalidArgument() if called with `DB::DefaultColumnFamily()`.
## 6.28.0 (2021-12-17)
### New Features
* Introduced 'CommitWithTimestamp' as a new tag. Currently, there is no API for user to trigger a write with this tag to the WAL. This is part of the efforts to support write-commited transactions with user-defined timestamps.

View File

@ -3632,6 +3632,35 @@ TEST_F(DBBasicTest, ManifestWriteFailure) {
Reopen(options);
}
TEST_F(DBBasicTest, DestroyDefaultCfHandle) {
Options options = GetDefaultOptions();
options.create_if_missing = true;
DestroyAndReopen(options);
CreateAndReopenWithCF({"pikachu"}, options);
for (const auto* h : handles_) {
ASSERT_NE(db_->DefaultColumnFamily(), h);
}
// We have two handles to the default column family. The two handles point to
// different ColumnFamilyHandle objects.
assert(db_->DefaultColumnFamily());
ASSERT_EQ(0U, db_->DefaultColumnFamily()->GetID());
assert(handles_[0]);
ASSERT_EQ(0U, handles_[0]->GetID());
// You can destroy handles_[...].
for (auto* h : handles_) {
ASSERT_OK(db_->DestroyColumnFamilyHandle(h));
}
handles_.clear();
// But you should not destroy db_->DefaultColumnFamily(), since it's going to
// be deleted in `DBImpl::CloseHelper()`. Before that, it may be used
// elsewhere internally too.
ColumnFamilyHandle* default_cf = db_->DefaultColumnFamily();
ASSERT_TRUE(db_->DestroyColumnFamilyHandle(default_cf).IsInvalidArgument());
}
#ifndef ROCKSDB_LITE
TEST_F(DBBasicTest, VerifyFileChecksums) {
Options options = GetDefaultOptions();

View File

@ -4031,6 +4031,10 @@ Status DB::DropColumnFamilies(
}
Status DB::DestroyColumnFamilyHandle(ColumnFamilyHandle* column_family) {
if (DefaultColumnFamily() == column_family) {
return Status::InvalidArgument(
"Cannot destroy the handle returned by DefaultColumnFamily()");
}
delete column_family;
return Status::OK();
}