Skip to content

Commit fb2346f

Browse files
committed
[CF] Code cleanup part 1
Summary: I'm cleaning up some code preparing for the big diff review tomorrow. This is the first part of the cleanup. Changes are mostly cosmetic. The goal is to decrease amount of code difference between columnfamilies and master branch. This diff also fixes race condition when dropping column family. Test Plan: Ran db_stress with variety of parameters Reviewers: dhruba, haobo Differential Revision: https://reviews.facebook.net/D16833
1 parent 56ca833 commit fb2346f

16 files changed

+249
-255
lines changed

HISTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* By default, max_background_flushes is 1 and flush process is
55
removed from background compaction process. Flush process is now always
66
executed in high priority thread pool.
7+
* Column family support
8+
* If you write something to the non-default column family with disableWAL = true,
9+
you need to Flush the column family before exiting if you want your data to be persistent
710

811
## Unreleased (will be relased in 2.8)
912

db/column_family.cc

+56-68
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,8 @@ ColumnFamilyData::ColumnFamilyData(const std::string& dbname, uint32_t id,
185185
dropped_(false),
186186
internal_comparator_(options.comparator),
187187
internal_filter_policy_(options.filter_policy),
188-
options_(SanitizeOptions(&internal_comparator_, &internal_filter_policy_,
189-
options)),
190-
full_options_(*db_options, options_),
188+
options_(*db_options, SanitizeOptions(&internal_comparator_,
189+
&internal_filter_policy_, options)),
191190
mem_(nullptr),
192191
imm_(options.min_write_buffer_number_to_merge),
193192
super_version_(nullptr),
@@ -205,18 +204,19 @@ ColumnFamilyData::ColumnFamilyData(const std::string& dbname, uint32_t id,
205204
internal_stats_.reset(new InternalStats(options.num_levels, db_options->env,
206205
db_options->statistics.get()));
207206
table_cache_.reset(
208-
new TableCache(dbname, &full_options_, storage_options, table_cache));
207+
new TableCache(dbname, &options_, storage_options, table_cache));
209208
if (options_.compaction_style == kCompactionStyleUniversal) {
210-
compaction_picker_.reset(new UniversalCompactionPicker(
211-
&options_, &internal_comparator_, db_options->info_log.get()));
209+
compaction_picker_.reset(
210+
new UniversalCompactionPicker(&options_, &internal_comparator_));
212211
} else {
213-
compaction_picker_.reset(new LevelCompactionPicker(
214-
&options_, &internal_comparator_, db_options->info_log.get()));
212+
compaction_picker_.reset(
213+
new LevelCompactionPicker(&options_, &internal_comparator_));
215214
}
216215

217-
Log(full_options_.info_log, "Options for column family \"%s\":\n",
216+
Log(options_.info_log, "Options for column family \"%s\":\n",
218217
name.c_str());
219-
options_.Dump(full_options_.info_log.get());
218+
const ColumnFamilyOptions* cf_options = &options_;
219+
cf_options->Dump(options_.info_log.get());
220220
}
221221
}
222222

@@ -232,14 +232,27 @@ ColumnFamilyData::~ColumnFamilyData() {
232232
// it's nullptr for dummy CFD
233233
if (column_family_set_ != nullptr) {
234234
// remove from column_family_set
235-
column_family_set_->DropColumnFamily(this);
235+
column_family_set_->RemoveColumnFamily(this);
236236
}
237237

238238
if (current_ != nullptr) {
239239
current_->Unref();
240240
}
241241

242-
DeleteSuperVersion();
242+
if (super_version_ != nullptr) {
243+
// Release SuperVersion reference kept in ThreadLocalPtr.
244+
// This must be done outside of mutex_ since unref handler can lock mutex.
245+
super_version_->db_mutex->Unlock();
246+
local_sv_.reset();
247+
super_version_->db_mutex->Lock();
248+
249+
bool is_last_reference __attribute__((unused));
250+
is_last_reference = super_version_->Unref();
251+
assert(is_last_reference);
252+
super_version_->Cleanup();
253+
delete super_version_;
254+
super_version_ = nullptr;
255+
}
243256

244257
if (dummy_versions_ != nullptr) {
245258
// List must be empty
@@ -257,10 +270,6 @@ ColumnFamilyData::~ColumnFamilyData() {
257270
}
258271
}
259272

260-
InternalStats* ColumnFamilyData::internal_stats() {
261-
return internal_stats_.get();
262-
}
263-
264273
void ColumnFamilyData::SetCurrent(Version* current) {
265274
current_ = current;
266275
need_slowdown_for_num_level0_files_ =
@@ -320,23 +329,6 @@ void ColumnFamilyData::ResetThreadLocalSuperVersions() {
320329
}
321330
}
322331

323-
void ColumnFamilyData::DeleteSuperVersion() {
324-
if (super_version_ != nullptr) {
325-
// Release SuperVersion reference kept in ThreadLocalPtr.
326-
// This must be done outside of mutex_ since unref handler can lock mutex.
327-
super_version_->db_mutex->Unlock();
328-
local_sv_.reset();
329-
super_version_->db_mutex->Lock();
330-
331-
bool is_last_reference __attribute__((unused));
332-
is_last_reference = super_version_->Unref();
333-
assert(is_last_reference);
334-
super_version_->Cleanup();
335-
delete super_version_;
336-
super_version_ = nullptr;
337-
}
338-
}
339-
340332
ColumnFamilySet::ColumnFamilySet(const std::string& dbname,
341333
const DBOptions* db_options,
342334
const EnvOptions& storage_options,
@@ -345,6 +337,7 @@ ColumnFamilySet::ColumnFamilySet(const std::string& dbname,
345337
dummy_cfd_(new ColumnFamilyData(dbname, 0, "", nullptr, nullptr,
346338
ColumnFamilyOptions(), db_options,
347339
storage_options_, nullptr)),
340+
default_cfd_cache_(nullptr),
348341
db_name_(dbname),
349342
db_options_(db_options),
350343
storage_options_(storage_options),
@@ -367,10 +360,8 @@ ColumnFamilySet::~ColumnFamilySet() {
367360
}
368361

369362
ColumnFamilyData* ColumnFamilySet::GetDefault() const {
370-
auto cfd = GetColumnFamily(0);
371-
// default column family should always exist
372-
assert(cfd != nullptr);
373-
return cfd;
363+
assert(default_cfd_cache_ != nullptr);
364+
return default_cfd_cache_;
374365
}
375366

376367
ColumnFamilyData* ColumnFamilySet::GetColumnFamily(uint32_t id) const {
@@ -385,24 +376,13 @@ ColumnFamilyData* ColumnFamilySet::GetColumnFamily(uint32_t id) const {
385376
ColumnFamilyData* ColumnFamilySet::GetColumnFamily(const std::string& name)
386377
const {
387378
auto cfd_iter = column_families_.find(name);
388-
if (cfd_iter == column_families_.end()) {
379+
if (cfd_iter != column_families_.end()) {
380+
auto cfd = GetColumnFamily(cfd_iter->second);
381+
assert(cfd != nullptr);
382+
return cfd;
383+
} else {
389384
return nullptr;
390385
}
391-
return GetColumnFamily(cfd_iter->second);
392-
}
393-
394-
bool ColumnFamilySet::Exists(uint32_t id) {
395-
return column_family_data_.find(id) != column_family_data_.end();
396-
}
397-
398-
bool ColumnFamilySet::Exists(const std::string& name) {
399-
return column_families_.find(name) != column_families_.end();
400-
}
401-
402-
uint32_t ColumnFamilySet::GetID(const std::string& name) {
403-
auto cfd_iter = column_families_.find(name);
404-
assert(cfd_iter != column_families_.end());
405-
return cfd_iter->second;
406386
}
407387

408388
uint32_t ColumnFamilySet::GetNextColumnFamilyID() {
@@ -434,11 +414,22 @@ ColumnFamilyData* ColumnFamilySet::CreateColumnFamily(
434414
new_cfd->prev_ = prev;
435415
prev->next_ = new_cfd;
436416
dummy_cfd_->prev_ = new_cfd;
417+
if (id == 0) {
418+
default_cfd_cache_ = new_cfd;
419+
}
437420
return new_cfd;
438421
}
439422

423+
void ColumnFamilySet::Lock() {
424+
// spin lock
425+
while (spin_lock_.test_and_set(std::memory_order_acquire)) {
426+
}
427+
}
428+
429+
void ColumnFamilySet::Unlock() { spin_lock_.clear(std::memory_order_release); }
430+
440431
// under a DB mutex
441-
void ColumnFamilySet::DropColumnFamily(ColumnFamilyData* cfd) {
432+
void ColumnFamilySet::RemoveColumnFamily(ColumnFamilyData* cfd) {
442433
auto cfd_iter = column_family_data_.find(cfd->GetID());
443434
assert(cfd_iter != column_family_data_.end());
444435
Lock();
@@ -447,19 +438,16 @@ void ColumnFamilySet::DropColumnFamily(ColumnFamilyData* cfd) {
447438
Unlock();
448439
}
449440

450-
void ColumnFamilySet::Lock() {
451-
// spin lock
452-
while (spin_lock_.test_and_set(std::memory_order_acquire)) {
453-
}
454-
}
455-
456-
void ColumnFamilySet::Unlock() { spin_lock_.clear(std::memory_order_release); }
457-
458441
bool ColumnFamilyMemTablesImpl::Seek(uint32_t column_family_id) {
459-
// maybe outside of db mutex, should lock
460-
column_family_set_->Lock();
461-
current_ = column_family_set_->GetColumnFamily(column_family_id);
462-
column_family_set_->Unlock();
442+
if (column_family_id == 0) {
443+
// optimization for common case
444+
current_ = column_family_set_->GetDefault();
445+
} else {
446+
// maybe outside of db mutex, should lock
447+
column_family_set_->Lock();
448+
current_ = column_family_set_->GetColumnFamily(column_family_id);
449+
column_family_set_->Unlock();
450+
}
463451
handle_.SetCFD(current_);
464452
return current_ != nullptr;
465453
}
@@ -474,9 +462,9 @@ MemTable* ColumnFamilyMemTablesImpl::GetMemTable() const {
474462
return current_->mem();
475463
}
476464

477-
const Options* ColumnFamilyMemTablesImpl::GetFullOptions() const {
465+
const Options* ColumnFamilyMemTablesImpl::GetOptions() const {
478466
assert(current_ != nullptr);
479-
return current_->full_options();
467+
return current_->options();
480468
}
481469

482470
ColumnFamilyHandle* ColumnFamilyMemTablesImpl::GetColumnFamilyHandle() {

0 commit comments

Comments
 (0)