Skip to content

Commit 476416c

Browse files
committed
Some minor refactoring on the code
Summary: I made some cleanup while reading the source code in `db`. Most changes are about style, naming or C++ 11 new features. Test Plan: ran `make check` Reviewers: haobo, dhruba, sdong CC: leveldb Differential Revision: https://reviews.facebook.net/D15009
1 parent f1cec73 commit 476416c

7 files changed

+190
-174
lines changed

db/db_impl.cc

+123-109
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include <vector>
2121

2222
#include "db/builder.h"
23-
#include "db/dbformat.h"
2423
#include "db/db_iter.h"
24+
#include "db/dbformat.h"
2525
#include "db/filename.h"
2626
#include "db/log_reader.h"
2727
#include "db/log_writer.h"
@@ -43,7 +43,6 @@
4343
#include "rocksdb/statistics.h"
4444
#include "rocksdb/status.h"
4545
#include "rocksdb/table.h"
46-
#include "port/port.h"
4746
#include "table/block.h"
4847
#include "table/block_based_table_factory.h"
4948
#include "table/merger.h"
@@ -59,7 +58,7 @@
5958

6059
namespace rocksdb {
6160

62-
void dumpLeveldbBuildVersion(Logger * log);
61+
void DumpLeveldbBuildVersion(Logger * log);
6362

6463
// Information kept for every waiting writer
6564
struct DBImpl::Writer {
@@ -266,9 +265,7 @@ DBImpl::DBImpl(const Options& options, const std::string& dbname)
266265
storage_options_(options),
267266
bg_work_gate_closed_(false),
268267
refitting_level_(false) {
269-
270268
mem_->Ref();
271-
272269
env_->GetAbsolutePath(dbname, &db_absolute_path_);
273270

274271
stall_leveln_slowdown_.resize(options.num_levels);
@@ -282,16 +279,15 @@ DBImpl::DBImpl(const Options& options, const std::string& dbname)
282279
const int table_cache_size = options_.max_open_files - 10;
283280
table_cache_.reset(new TableCache(dbname_, &options_,
284281
storage_options_, table_cache_size));
285-
286282
versions_.reset(new VersionSet(dbname_, &options_, storage_options_,
287283
table_cache_.get(), &internal_comparator_));
288284

289-
dumpLeveldbBuildVersion(options_.info_log.get());
285+
DumpLeveldbBuildVersion(options_.info_log.get());
290286
options_.Dump(options_.info_log.get());
291287

292288
char name[100];
293-
Status st = env_->GetHostName(name, 100L);
294-
if (st.ok()) {
289+
Status s = env_->GetHostName(name, 100L);
290+
if (s.ok()) {
295291
host_name_ = name;
296292
} else {
297293
Log(options_.info_log, "Can't get hostname, use localhost as host name.");
@@ -502,7 +498,7 @@ void DBImpl::SuperVersion::Init(MemTable* new_mem, const MemTableList& new_imm,
502498
}
503499

504500
// Returns the list of live files in 'sst_live' and the list
505-
// of all files in the filesystem in 'all_files'.
501+
// of all files in the filesystem in 'candidate_files'.
506502
// no_full_scan = true -- never do the full scan using GetChildren()
507503
// force = false -- don't force the full scan, except every
508504
// options_.delete_obsolete_files_period_micros
@@ -554,15 +550,18 @@ void DBImpl::FindObsoleteFiles(DeletionState& deletion_state,
554550
versions_->AddLiveFiles(&deletion_state.sst_live);
555551

556552
if (doing_the_full_scan) {
557-
// set of all files in the directory
558-
env_->GetChildren(dbname_, &deletion_state.all_files); // Ignore errors
553+
// set of all files in the directory. We'll exclude files that are still
554+
// alive in the subsequent processings.
555+
env_->GetChildren(
556+
dbname_, &deletion_state.candidate_files
557+
); // Ignore errors
559558

560559
//Add log files in wal_dir
561560
if (options_.wal_dir != dbname_) {
562561
std::vector<std::string> log_files;
563562
env_->GetChildren(options_.wal_dir, &log_files); // Ignore errors
564-
deletion_state.all_files.insert(
565-
deletion_state.all_files.end(),
563+
deletion_state.candidate_files.insert(
564+
deletion_state.candidate_files.end(),
566565
log_files.begin(),
567566
log_files.end()
568567
);
@@ -575,11 +574,10 @@ void DBImpl::FindObsoleteFiles(DeletionState& deletion_state,
575574
// files in sst_delete_files and log_delete_files.
576575
// It is not necessary to hold the mutex when invoking this method.
577576
void DBImpl::PurgeObsoleteFiles(DeletionState& state) {
578-
579577
// check if there is anything to do
580-
if (!state.all_files.size() &&
581-
!state.sst_delete_files.size() &&
582-
!state.log_delete_files.size()) {
578+
if (state.candidate_files.empty() &&
579+
state.sst_delete_files.empty() &&
580+
state.log_delete_files.empty()) {
583581
return;
584582
}
585583

@@ -589,100 +587,114 @@ void DBImpl::PurgeObsoleteFiles(DeletionState& state) {
589587
if (state.manifest_file_number == 0) {
590588
return;
591589
}
592-
593-
uint64_t number;
594-
FileType type;
595590
std::vector<std::string> old_log_files;
596591

597592
// Now, convert live list to an unordered set, WITHOUT mutex held;
598593
// set is slow.
599-
std::unordered_set<uint64_t> live_set(state.sst_live.begin(),
600-
state.sst_live.end());
594+
std::unordered_set<uint64_t> sst_live(
595+
state.sst_live.begin(), state.sst_live.end()
596+
);
601597

602-
state.all_files.reserve(state.all_files.size() +
603-
state.sst_delete_files.size());
598+
auto& candidate_files = state.candidate_files;
599+
candidate_files.reserve(
600+
candidate_files.size() +
601+
state.sst_delete_files.size() +
602+
state.log_delete_files.size());
603+
// We may ignore the dbname when generating the file names.
604+
const char* kDumbDbName = "";
604605
for (auto file : state.sst_delete_files) {
605-
state.all_files.push_back(TableFileName("", file->number).substr(1));
606+
candidate_files.push_back(
607+
TableFileName(kDumbDbName, file->number).substr(1)
608+
);
606609
delete file;
607610
}
608611

609-
state.all_files.reserve(state.all_files.size() +
610-
state.log_delete_files.size());
611-
for (auto filenum : state.log_delete_files) {
612-
if (filenum > 0) {
613-
state.all_files.push_back(LogFileName("", filenum).substr(1));
612+
for (auto file_num : state.log_delete_files) {
613+
if (file_num > 0) {
614+
candidate_files.push_back(
615+
LogFileName(kDumbDbName, file_num).substr(1)
616+
);
614617
}
615618
}
616619

617-
// dedup state.all_files so we don't try to delete the same
620+
// dedup state.candidate_files so we don't try to delete the same
618621
// file twice
619-
sort(state.all_files.begin(), state.all_files.end());
620-
auto unique_end = unique(state.all_files.begin(), state.all_files.end());
621-
622-
for (size_t i = 0; state.all_files.begin() + i < unique_end; i++) {
623-
if (ParseFileName(state.all_files[i], &number, &type)) {
624-
bool keep = true;
625-
switch (type) {
626-
case kLogFile:
627-
keep = ((number >= state.log_number) ||
628-
(number == state.prev_log_number));
629-
break;
630-
case kDescriptorFile:
631-
// Keep my manifest file, and any newer incarnations'
632-
// (in case there is a race that allows other incarnations)
633-
keep = (number >= state.manifest_file_number);
634-
break;
635-
case kTableFile:
636-
keep = (live_set.find(number) != live_set.end());
637-
break;
638-
case kTempFile:
639-
// Any temp files that are currently being written to must
640-
// be recorded in pending_outputs_, which is inserted into "live"
641-
keep = (live_set.find(number) != live_set.end());
642-
break;
643-
case kInfoLogFile:
644-
keep = true;
645-
if (number != 0) {
646-
old_log_files.push_back(state.all_files[i]);
647-
}
648-
break;
649-
case kCurrentFile:
650-
case kDBLockFile:
651-
case kIdentityFile:
652-
case kMetaDatabase:
653-
keep = true;
654-
break;
655-
}
622+
sort(candidate_files.begin(), candidate_files.end());
623+
candidate_files.erase(
624+
unique(candidate_files.begin(), candidate_files.end()),
625+
candidate_files.end()
626+
);
656627

657-
if (!keep) {
658-
if (type == kTableFile) {
659-
// evict from cache
660-
table_cache_->Evict(number);
628+
for (const auto& to_delete : candidate_files) {
629+
uint64_t number;
630+
FileType type;
631+
// Ignore file if we cannot recognize it.
632+
if (!ParseFileName(to_delete, &number, &type)) {
633+
continue;
634+
}
635+
636+
bool keep = true;
637+
switch (type) {
638+
case kLogFile:
639+
keep = ((number >= state.log_number) ||
640+
(number == state.prev_log_number));
641+
break;
642+
case kDescriptorFile:
643+
// Keep my manifest file, and any newer incarnations'
644+
// (in case there is a race that allows other incarnations)
645+
keep = (number >= state.manifest_file_number);
646+
break;
647+
case kTableFile:
648+
keep = (sst_live.find(number) != sst_live.end());
649+
break;
650+
case kTempFile:
651+
// Any temp files that are currently being written to must
652+
// be recorded in pending_outputs_, which is inserted into "live"
653+
keep = (sst_live.find(number) != sst_live.end());
654+
break;
655+
case kInfoLogFile:
656+
keep = true;
657+
if (number != 0) {
658+
old_log_files.push_back(to_delete);
661659
}
662-
std::string fname = ((type == kLogFile) ? options_.wal_dir : dbname_) +
663-
"/" + state.all_files[i];
660+
break;
661+
case kCurrentFile:
662+
case kDBLockFile:
663+
case kIdentityFile:
664+
case kMetaDatabase:
665+
keep = true;
666+
break;
667+
}
668+
669+
if (keep) {
670+
continue;
671+
}
672+
673+
if (type == kTableFile) {
674+
// evict from cache
675+
table_cache_->Evict(number);
676+
}
677+
std::string fname = ((type == kLogFile) ? options_.wal_dir : dbname_) +
678+
"/" + to_delete;
679+
Log(options_.info_log,
680+
"Delete type=%d #%lu",
681+
int(type),
682+
(unsigned long)number);
683+
684+
if (type == kLogFile &&
685+
(options_.WAL_ttl_seconds > 0 || options_.WAL_size_limit_MB > 0)) {
686+
Status s = env_->RenameFile(fname,
687+
ArchivedLogFileName(options_.wal_dir, number));
688+
if (!s.ok()) {
664689
Log(options_.info_log,
665-
"Delete type=%d #%lu",
666-
int(type),
667-
(unsigned long)number);
668-
669-
Status st;
670-
if (type == kLogFile && (options_.WAL_ttl_seconds > 0 ||
671-
options_.WAL_size_limit_MB > 0)) {
672-
st = env_->RenameFile(fname,
673-
ArchivedLogFileName(options_.wal_dir, number));
674-
if (!st.ok()) {
675-
Log(options_.info_log,
676-
"RenameFile logfile #%lu FAILED -- %s\n",
677-
(unsigned long)number, st.ToString().c_str());
678-
}
679-
} else {
680-
st = env_->DeleteFile(fname);
681-
if (!st.ok()) {
682-
Log(options_.info_log, "Delete type=%d #%lu FAILED -- %s\n",
683-
int(type), (unsigned long)number, st.ToString().c_str());
684-
}
685-
}
690+
"RenameFile logfile #%lu FAILED -- %s\n",
691+
(unsigned long)number, s.ToString().c_str());
692+
}
693+
} else {
694+
Status s = env_->DeleteFile(fname);
695+
if (!s.ok()) {
696+
Log(options_.info_log, "Delete type=%d #%lu FAILED -- %s\n",
697+
int(type), (unsigned long)number, s.ToString().c_str());
686698
}
687699
}
688700
}
@@ -839,7 +851,9 @@ void DBImpl::PurgeObsoleteWALFiles() {
839851

840852
// If externalTable is set, then apply recovered transactions
841853
// to that table. This is used for readonly mode.
842-
Status DBImpl::Recover(VersionEdit* edit, MemTable* external_table,
854+
Status DBImpl::Recover(
855+
VersionEdit* edit,
856+
MemTable* external_table,
843857
bool error_if_log_file_exist) {
844858
mutex_.AssertHeld();
845859

@@ -906,10 +920,11 @@ Status DBImpl::Recover(VersionEdit* edit, MemTable* external_table,
906920
if (!s.ok()) {
907921
return s;
908922
}
909-
uint64_t number;
910-
FileType type;
923+
911924
std::vector<uint64_t> logs;
912925
for (size_t i = 0; i < filenames.size(); i++) {
926+
uint64_t number;
927+
FileType type;
913928
if (ParseFileName(filenames[i], &number, &type)
914929
&& type == kLogFile
915930
&& ((number >= min_log) || (number == prev_log))) {
@@ -925,12 +940,12 @@ Status DBImpl::Recover(VersionEdit* edit, MemTable* external_table,
925940

926941
// Recover in the order in which the logs were generated
927942
std::sort(logs.begin(), logs.end());
928-
for (size_t i = 0; i < logs.size(); i++) {
929-
s = RecoverLogFile(logs[i], edit, &max_sequence, external_table);
943+
for (const auto& log : logs) {
944+
s = RecoverLogFile(log, edit, &max_sequence, external_table);
930945
// The previous incarnation may not have written any MANIFEST
931946
// records after allocating this log number. So we manually
932947
// update the file number allocation counter in VersionSet.
933-
versions_->MarkFileNumberUsed(logs[i]);
948+
versions_->MarkFileNumberUsed(log);
934949
}
935950

936951
if (s.ok()) {
@@ -1147,7 +1162,6 @@ Status DBImpl::WriteLevel0Table(std::vector<MemTable*> &mems, VersionEdit* edit,
11471162
}
11481163
base->Unref();
11491164

1150-
11511165
// re-acquire the most current version
11521166
base = versions_->current();
11531167

@@ -3285,7 +3299,7 @@ Status DBImpl::MakeRoomForWrite(bool force,
32853299

32863300
} else {
32873301
unique_ptr<WritableFile> lfile;
3288-
MemTable* memtmp = nullptr;
3302+
MemTable* new_mem = nullptr;
32893303

32903304
// Attempt to switch to a new memtable and trigger compaction of old.
32913305
// Do this without holding the dbmutex lock.
@@ -3306,7 +3320,7 @@ Status DBImpl::MakeRoomForWrite(bool force,
33063320
// Our final size should be less than write_buffer_size
33073321
// (compression, etc) but err on the side of caution.
33083322
lfile->SetPreallocationBlockSize(1.1 * options_.write_buffer_size);
3309-
memtmp = new MemTable(
3323+
new_mem = new MemTable(
33103324
internal_comparator_, mem_rep_factory_, NumberLevels(), options_);
33113325
new_superversion = new SuperVersion(options_.max_write_buffer_number);
33123326
}
@@ -3315,7 +3329,7 @@ Status DBImpl::MakeRoomForWrite(bool force,
33153329
if (!s.ok()) {
33163330
// Avoid chewing through file number space in a tight loop.
33173331
versions_->ReuseFileNumber(new_log_number);
3318-
assert (!memtmp);
3332+
assert (!new_mem);
33193333
break;
33203334
}
33213335
logfile_number_ = new_log_number;
@@ -3325,7 +3339,7 @@ Status DBImpl::MakeRoomForWrite(bool force,
33253339
if (force) {
33263340
imm_.FlushRequested();
33273341
}
3328-
mem_ = memtmp;
3342+
mem_ = new_mem;
33293343
mem_->Ref();
33303344
Log(options_.info_log,
33313345
"New memtable created with log file: #%lu\n",
@@ -3806,7 +3820,7 @@ Status DB::Open(const Options& options, const std::string& dbname, DB** dbptr) {
38063820
delete impl;
38073821
return s;
38083822
}
3809-
impl->mutex_.Lock();
3823+
impl->mutex_.Lock(); // DBImpl::Recover() requires lock being held
38103824
VersionEdit edit(impl->NumberLevels());
38113825
s = impl->Recover(&edit); // Handles create_if_missing, error_if_exists
38123826
if (s.ok()) {
@@ -3929,7 +3943,7 @@ Status DestroyDB(const std::string& dbname, const Options& options) {
39293943

39303944
//
39313945
// A global method that can dump out the build version
3932-
void dumpLeveldbBuildVersion(Logger * log) {
3946+
void DumpLeveldbBuildVersion(Logger * log) {
39333947
Log(log, "Git sha %s", rocksdb_build_git_sha);
39343948
Log(log, "Compile time %s %s",
39353949
rocksdb_build_compile_time, rocksdb_build_compile_date);

0 commit comments

Comments
 (0)