Skip to content

Commit ae16606

Browse files
committed
Merge branch 'master' into columnfamilies
Conflicts: db/version_set.cc db/version_set.h
2 parents cf783c6 + 832158e commit ae16606

File tree

11 files changed

+124
-29
lines changed

11 files changed

+124
-29
lines changed

db/db_impl.cc

+21-7
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,11 @@ Status DBImpl::Recover(
886886
return s;
887887
}
888888

889+
s = env_->NewDirectory(dbname_, &db_directory_);
890+
if (!s.ok()) {
891+
return s;
892+
}
893+
889894
s = env_->LockFile(LockFileName(dbname_), &db_lock_);
890895
if (!s.ok()) {
891896
return s;
@@ -1178,6 +1183,9 @@ Status DBImpl::WriteLevel0Table(std::vector<MemTable*> &mems, VersionEdit* edit,
11781183
(unsigned long) meta.number,
11791184
(unsigned long) meta.file_size,
11801185
s.ToString().c_str());
1186+
if (!options_.disableDataSync) {
1187+
db_directory_->Fsync();
1188+
}
11811189
mutex_.Lock();
11821190
}
11831191
base->Unref();
@@ -1272,8 +1280,8 @@ Status DBImpl::FlushMemTableToOutputFile(bool* madeProgress,
12721280

12731281
// Replace immutable memtable with the generated Table
12741282
s = imm_.InstallMemtableFlushResults(
1275-
mems, versions_.get(), s, &mutex_, options_.info_log.get(),
1276-
file_number, pending_outputs_, &deletion_state.memtables_to_free);
1283+
mems, versions_.get(), s, &mutex_, options_.info_log.get(), file_number,
1284+
pending_outputs_, &deletion_state.memtables_to_free, db_directory_.get());
12771285

12781286
if (s.ok()) {
12791287
InstallSuperVersion(deletion_state);
@@ -1401,7 +1409,7 @@ Status DBImpl::ReFitLevel(int level, int target_level) {
14011409
Log(options_.info_log, "Apply version edit:\n%s",
14021410
edit.DebugString().data());
14031411

1404-
status = versions_->LogAndApply(&edit, &mutex_);
1412+
status = versions_->LogAndApply(&edit, &mutex_, db_directory_.get());
14051413
superversion_to_free = InstallSuperVersion(new_superversion);
14061414
new_superversion = nullptr;
14071415

@@ -1974,7 +1982,7 @@ Status DBImpl::BackgroundCompaction(bool* madeProgress,
19741982
c->edit()->AddFile(c->level() + 1, f->number, f->file_size,
19751983
f->smallest, f->largest,
19761984
f->smallest_seqno, f->largest_seqno);
1977-
status = versions_->LogAndApply(c->edit(), &mutex_);
1985+
status = versions_->LogAndApply(c->edit(), &mutex_, db_directory_.get());
19781986
InstallSuperVersion(deletion_state);
19791987
Version::LevelSummaryStorage tmp;
19801988
Log(options_.info_log, "Moved #%lld to level-%d %lld bytes %s: %s\n",
@@ -2222,7 +2230,8 @@ Status DBImpl::InstallCompactionResults(CompactionState* compact) {
22222230
compact->compaction->output_level(), out.number, out.file_size,
22232231
out.smallest, out.largest, out.smallest_seqno, out.largest_seqno);
22242232
}
2225-
return versions_->LogAndApply(compact->compaction->edit(), &mutex_);
2233+
return versions_->LogAndApply(compact->compaction->edit(), &mutex_,
2234+
db_directory_.get());
22262235
}
22272236

22282237
//
@@ -2595,6 +2604,9 @@ Status DBImpl::DoCompactionWork(CompactionState* compact,
25952604
}
25962605
input.reset();
25972606

2607+
if (!options_.disableDataSync) {
2608+
db_directory_->Fsync();
2609+
}
25982610
CompactionStats stats;
25992611
stats.micros = env_->NowMicros() - start_micros - imm_micros;
26002612
MeasureTime(options_.statistics.get(), COMPACTION_TIME, stats.micros);
@@ -3886,7 +3898,7 @@ Status DBImpl::DeleteFile(std::string name) {
38863898
}
38873899
}
38883900
edit.DeleteFile(level, number);
3889-
status = versions_->LogAndApply(&edit, &mutex_);
3901+
status = versions_->LogAndApply(&edit, &mutex_, db_directory_.get());
38903902
if (status.ok()) {
38913903
InstallSuperVersion(deletion_state);
38923904
}
@@ -4032,7 +4044,8 @@ Status DB::OpenWithColumnFamilies(
40324044
edit.SetLogNumber(new_log_number);
40334045
impl->logfile_number_ = new_log_number;
40344046
impl->log_.reset(new log::Writer(std::move(lfile)));
4035-
s = impl->versions_->LogAndApply(&edit, &impl->mutex_);
4047+
s = impl->versions_->LogAndApply(&edit, &impl->mutex_,
4048+
impl->db_directory_.get());
40364049
}
40374050
if (s.ok()) {
40384051
// set column family handles
@@ -4053,6 +4066,7 @@ Status DB::OpenWithColumnFamilies(
40534066
impl->DeleteObsoleteFiles();
40544067
impl->MaybeScheduleFlushOrCompaction();
40554068
impl->MaybeScheduleLogDBDeployStats();
4069+
s = impl->db_directory_->Fsync();
40564070
}
40574071
}
40584072

db/db_impl.h

+2
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ class DBImpl : public DB {
440440

441441
std::string host_name_;
442442

443+
std::unique_ptr<Directory> db_directory_;
444+
443445
// Queue of writers.
444446
std::deque<Writer*> writers_;
445447
WriteBatch tmp_batch_;

db/memtablelist.cc

+5-7
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,10 @@ void MemTableList::PickMemtablesToFlush(std::vector<MemTable*>* ret) {
121121

122122
// Record a successful flush in the manifest file
123123
Status MemTableList::InstallMemtableFlushResults(
124-
const std::vector<MemTable*> &mems,
125-
VersionSet* vset, Status flushStatus,
126-
port::Mutex* mu, Logger* info_log,
127-
uint64_t file_number,
128-
std::set<uint64_t>& pending_outputs,
129-
std::vector<MemTable*>* to_delete) {
124+
const std::vector<MemTable*>& mems, VersionSet* vset, Status flushStatus,
125+
port::Mutex* mu, Logger* info_log, uint64_t file_number,
126+
std::set<uint64_t>& pending_outputs, std::vector<MemTable*>* to_delete,
127+
Directory* db_directory) {
130128
mu->AssertHeld();
131129

132130
// If the flush was not successful, then just reset state.
@@ -178,7 +176,7 @@ Status MemTableList::InstallMemtableFlushResults(
178176
(unsigned long)m->file_number_);
179177

180178
// this can release and reacquire the mutex.
181-
s = vset->LogAndApply(&m->edit_, mu);
179+
s = vset->LogAndApply(&m->edit_, mu, db_directory);
182180

183181
// we will be changing the version in the next code path,
184182
// so we better create a new one, since versions are immutable

db/memtablelist.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ class MemTableList {
9090
void PickMemtablesToFlush(std::vector<MemTable*>* mems);
9191

9292
// Commit a successful flush in the manifest file
93-
Status InstallMemtableFlushResults(const std::vector<MemTable*> &m,
94-
VersionSet* vset, Status flushStatus,
95-
port::Mutex* mu, Logger* info_log,
96-
uint64_t file_number,
97-
std::set<uint64_t>& pending_outputs,
98-
std::vector<MemTable*>* to_delete);
93+
Status InstallMemtableFlushResults(const std::vector<MemTable*>& m,
94+
VersionSet* vset, Status flushStatus,
95+
port::Mutex* mu, Logger* info_log,
96+
uint64_t file_number,
97+
std::set<uint64_t>& pending_outputs,
98+
std::vector<MemTable*>* to_delete,
99+
Directory* db_directory);
99100

100101
// New memtables are inserted at the front of the list.
101102
// Takes ownership of the referenced held on *m by the caller of Add().

db/version_set.cc

+6-3
Original file line numberDiff line numberDiff line change
@@ -1421,8 +1421,8 @@ void VersionSet::AppendVersion(ColumnFamilyData* column_family_data,
14211421
}
14221422

14231423
Status VersionSet::LogAndApply(ColumnFamilyData* column_family_data,
1424-
VersionEdit* edit,
1425-
port::Mutex* mu,
1424+
VersionEdit* edit, port::Mutex* mu,
1425+
Directory* db_directory,
14261426
bool new_descriptor_log) {
14271427
mu->AssertHeld();
14281428

@@ -1545,6 +1545,9 @@ Status VersionSet::LogAndApply(ColumnFamilyData* column_family_data,
15451545
// of it later
15461546
env_->DeleteFile(DescriptorFileName(dbname_, old_manifest_file_number));
15471547
}
1548+
if (!options_->disableDataSync && db_directory != nullptr) {
1549+
db_directory->Fsync();
1550+
}
15481551
}
15491552

15501553
// find offset in manifest file where this version is stored.
@@ -1967,7 +1970,7 @@ Status VersionSet::ReduceNumberOfLevels(const std::string& dbname,
19671970
VersionEdit ve;
19681971
port::Mutex dummy_mutex;
19691972
MutexLock l(&dummy_mutex);
1970-
return versions.LogAndApply(&ve, &dummy_mutex, true);
1973+
return versions.LogAndApply(&ve, &dummy_mutex, nullptr, true);
19711974
}
19721975

19731976
Status VersionSet::DumpManifest(Options& options, std::string& dscname,

db/version_set.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,14 @@ class VersionSet {
288288
// current version. Will release *mu while actually writing to the file.
289289
// REQUIRES: *mu is held on entry.
290290
// REQUIRES: no other thread concurrently calls LogAndApply()
291-
Status LogAndApply(ColumnFamilyData* column_family_data,
292-
VersionEdit* edit,
293-
port::Mutex* mu,
291+
Status LogAndApply(ColumnFamilyData* column_family_data, VersionEdit* edit,
292+
port::Mutex* mu, Directory* db_directory = nullptr,
294293
bool new_descriptor_log = false);
295294

296-
Status LogAndApply(VersionEdit* edit,
297-
port::Mutex* mu,
295+
Status LogAndApply(VersionEdit* edit, port::Mutex* mu,
296+
Directory* db_directory = nullptr,
298297
bool new_descriptor_log = false) {
299-
return LogAndApply(column_family_set_->GetDefault(), edit, mu,
298+
return LogAndApply(column_family_set_->GetDefault(), edit, mu, db_directory,
300299
new_descriptor_log);
301300
}
302301

hdfs/env_hdfs.h

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class HdfsEnv : public Env {
7070
unique_ptr<RandomRWFile>* result,
7171
const EnvOptions& options);
7272

73+
virtual Status NewDirectory(const std::string& name,
74+
unique_ptr<Directory>* result);
75+
7376
virtual bool FileExists(const std::string& fname);
7477

7578
virtual Status GetChildren(const std::string& path,
@@ -246,6 +249,11 @@ class HdfsEnv : public Env {
246249
return notsup;
247250
}
248251

252+
virtual Status NewDirectory(const std::string& name,
253+
unique_ptr<Directory>* result) {
254+
return notsup;
255+
}
256+
249257
virtual bool FileExists(const std::string& fname){return false;}
250258

251259
virtual Status GetChildren(const std::string& path,

helpers/memenv/memenv.cc

+11
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ class WritableFileImpl : public WritableFile {
221221
FileState* file_;
222222
};
223223

224+
class InMemoryDirectory : public Directory {
225+
public:
226+
virtual Status Fsync() { return Status::OK(); }
227+
};
228+
224229
class InMemoryEnv : public EnvWrapper {
225230
public:
226231
explicit InMemoryEnv(Env* base_env) : EnvWrapper(base_env) { }
@@ -274,6 +279,12 @@ class InMemoryEnv : public EnvWrapper {
274279
return Status::OK();
275280
}
276281

282+
virtual Status NewDirectory(const std::string& name,
283+
unique_ptr<Directory>* result) {
284+
result->reset(new InMemoryDirectory());
285+
return Status::OK();
286+
}
287+
277288
virtual bool FileExists(const std::string& fname) {
278289
MutexLock lock(&mutex_);
279290
return file_map_.find(fname) != file_map_.end();

include/rocksdb/env.h

+24
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class SequentialFile;
3333
class Slice;
3434
class WritableFile;
3535
class RandomRWFile;
36+
class Directory;
3637
struct Options;
3738

3839
using std::unique_ptr;
@@ -122,6 +123,16 @@ class Env {
122123
unique_ptr<RandomRWFile>* result,
123124
const EnvOptions& options) = 0;
124125

126+
// Create an object that represents a directory. Will fail if directory
127+
// doesn't exist. If the directory exists, it will open the directory
128+
// and create a new Directory object.
129+
//
130+
// On success, stores a pointer to the new Directory in
131+
// *result and returns OK. On failure stores nullptr in *result and
132+
// returns non-OK.
133+
virtual Status NewDirectory(const std::string& name,
134+
unique_ptr<Directory>* result) = 0;
135+
125136
// Returns true iff the named file exists.
126137
virtual bool FileExists(const std::string& fname) = 0;
127138

@@ -488,6 +499,15 @@ class RandomRWFile {
488499
void operator=(const RandomRWFile&);
489500
};
490501

502+
// Directory object represents collection of files and implements
503+
// filesystem operations that can be executed on directories.
504+
class Directory {
505+
public:
506+
virtual ~Directory() {}
507+
// Fsync directory
508+
virtual Status Fsync() = 0;
509+
};
510+
491511
// An interface for writing log messages.
492512
class Logger {
493513
public:
@@ -578,6 +598,10 @@ class EnvWrapper : public Env {
578598
const EnvOptions& options) {
579599
return target_->NewRandomRWFile(f, r, options);
580600
}
601+
virtual Status NewDirectory(const std::string& name,
602+
unique_ptr<Directory>* result) {
603+
return target_->NewDirectory(name, result);
604+
}
581605
bool FileExists(const std::string& f) { return target_->FileExists(f); }
582606
Status GetChildren(const std::string& dir, std::vector<std::string>* r) {
583607
return target_->GetChildren(dir, r);

util/env_hdfs.cc

+5
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ Status HdfsEnv::NewRandomRWFile(const std::string& fname,
366366
return Status::NotSupported("NewRandomRWFile not supported on HdfsEnv");
367367
}
368368

369+
virtual Status NewDirectory(const std::string& name,
370+
unique_ptr<Directory>* result) {
371+
return Status::NotSupported("NewDirectory not yet supported on HdfsEnv");
372+
}
373+
369374
bool HdfsEnv::FileExists(const std::string& fname) {
370375
int value = hdfsExists(fileSys_, fname.c_str());
371376
if (value == 0) {

util/env_posix.cc

+30
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,24 @@ class PosixRandomRWFile : public RandomRWFile {
867867
#endif
868868
};
869869

870+
class PosixDirectory : public Directory {
871+
public:
872+
explicit PosixDirectory(int fd) : fd_(fd) {}
873+
~PosixDirectory() {
874+
close(fd_);
875+
}
876+
877+
virtual Status Fsync() {
878+
if (fsync(fd_) == -1) {
879+
return IOError("directory", errno);
880+
}
881+
return Status::OK();
882+
}
883+
884+
private:
885+
int fd_;
886+
};
887+
870888
static int LockOrUnlock(const std::string& fname, int fd, bool lock) {
871889
mutex_lockedFiles.Lock();
872890
if (lock) {
@@ -1038,6 +1056,18 @@ class PosixEnv : public Env {
10381056
return s;
10391057
}
10401058

1059+
virtual Status NewDirectory(const std::string& name,
1060+
unique_ptr<Directory>* result) {
1061+
result->reset();
1062+
const int fd = open(name.c_str(), 0);
1063+
if (fd < 0) {
1064+
return IOError(name, errno);
1065+
} else {
1066+
result->reset(new PosixDirectory(fd));
1067+
}
1068+
return Status::OK();
1069+
}
1070+
10411071
virtual bool FileExists(const std::string& fname) {
10421072
return access(fname.c_str(), F_OK) == 0;
10431073
}

0 commit comments

Comments
 (0)