Skip to content

Commit

Permalink
cherry-pick some upstream fixes (#58)
Browse files Browse the repository at this point in the history
* Fix Get does not return super version on error

Summary:
This is caught when I was testing facebook#2886.
Closes facebook#2907

Differential Revision: D5863153

Pulled By: yiwu-arbug

fbshipit-source-id: 8c54759ba1a0dc101f24ab50423e35731300612d

* fix data race

Summary:
Fix a TSAN failure in `DBRangeDelTest.ValidLevelSubcompactionBoundaries`:
https://gist.github.com/miasantreble/712e04b4de2ff7f193c98b1acf07e899
Closes facebook#3691

Differential Revision: D7541400

Pulled By: miasantreble

fbshipit-source-id: b0b4538980bce7febd0385e61d6e046580bcaefb

* check return status for Sync() and Append() calls to avoid corruption

Summary:
Right now in `SyncClosedLogs`, `CopyFile`, and `AddRecord`, where `Sync` and `Append` are invoked in a loop, the error status are not checked. This could lead to potential corruption as later calls will overwrite the error status.
Closes facebook#3740

Differential Revision: D7678848

Pulled By: miasantreble

fbshipit-source-id: 4b0b412975989dfe80348f73217b9c4122a4bd77

* Fix race condition between log_.erase and log_.back

Summary:
log_ contract specifies that it should not be modified unless both mutex_ and log_write_mutex_ are held. log_.erase however does that with only holding mutex_. This causes a race condition with two_write_queues since logs_.back is read with holding only log_write_mutex_ (which is correct according to logs_ contract) but logs_.erase is called concurrently. This is probably the cause of logs_.back returning nullptr in facebook#3852 although I could not reproduce it.
Fixes facebook#3852
Closes facebook#3859

Differential Revision: D8026103

Pulled By: maysamyabandeh

fbshipit-source-id: ee394e00fe4aa520d884c5ef87981e9d6b5ccb28

* Sync CURRENT file during checkpoint (facebook#4322)

Summary: For the CURRENT file forged during checkpoint, we were forgetting to `fsync` or `fdatasync` it after its creation. This PR fixes it.

Differential Revision: D9525939

Pulled By: ajkr

fbshipit-source-id: a505483644026ee3f501cfc0dcbe74832165b2e3
  • Loading branch information
huachaohuang committed Oct 30, 2018
1 parent 90d77f0 commit 57f5fbe
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 12 deletions.
3 changes: 3 additions & 0 deletions db/db_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,8 @@ void DBImpl::MarkLogsSynced(
assert(log.getting_synced);
if (status.ok() && logs_.size() > 1) {
logs_to_free_.push_back(log.ReleaseWriter());
// To modify logs_ both mutex_ and log_write_mutex_ must be held
InstrumentedMutexLock l(&log_write_mutex_);
it = logs_.erase(it);
} else {
log.getting_synced = false;
Expand Down Expand Up @@ -972,6 +974,7 @@ Status DBImpl::GetImpl(const ReadOptions& read_options,
RecordTick(stats_, MEMTABLE_HIT);
}
if (!done && !s.ok() && !s.IsMergeInProgress()) {
ReturnAndCleanupSuperVersion(cfd, sv);
return s;
}
}
Expand Down
3 changes: 3 additions & 0 deletions db/db_impl_compaction_flush.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Status DBImpl::SyncClosedLogs(JobContext* job_context) {
"[JOB %d] Syncing log #%" PRIu64, job_context->job_id,
log->get_log_number());
s = log->file()->Sync(immutable_db_options_.use_fsync);
if (!s.ok()) {
break;
}
}
if (s.ok()) {
s = directories_.GetWalDir()->Fsync();
Expand Down
7 changes: 5 additions & 2 deletions db/log_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ Status Writer::AddRecord(const Slice& slice) {
// Fill the trailer (literal below relies on kHeaderSize and
// kRecyclableHeaderSize being <= 11)
assert(header_size <= 11);
dest_->Append(
Slice("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", leftover));
s = dest_->Append(Slice("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
static_cast<size_t>(leftover)));
if (!s.ok()) {
break;
}
}
block_offset_ = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions db/repair_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ TEST_F(RepairTest, CorruptManifest) {

Close();
ASSERT_OK(env_->FileExists(manifest_path));
CreateFile(env_, manifest_path, "blah");
CreateFile(env_, manifest_path, "blah", false /* use_fsync */);
ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
Reopen(CurrentOptions());

Expand Down Expand Up @@ -136,7 +136,7 @@ TEST_F(RepairTest, CorruptSst) {
Flush();
auto sst_path = GetFirstSstPath();
ASSERT_FALSE(sst_path.empty());
CreateFile(env_, sst_path, "blah");
CreateFile(env_, sst_path, "blah", false /* use_fsync */);

Close();
ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
Expand Down
6 changes: 4 additions & 2 deletions db/version_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2565,7 +2565,9 @@ Status VersionSet::LogAndApply(ColumnFamilyData* column_family_data,
// Unlock during expensive operations. New writes cannot get here
// because &w is ensuring that all new writes get queued.
{

// Before releasing mutex, make a copy of mutable_cf_options and pass to
// `PrepareApply` to avoided a potential data race with backgroundflush
MutableCFOptions mutable_cf_options_copy(mutable_cf_options);
mu->Unlock();

TEST_SYNC_POINT("VersionSet::LogAndApply:WriteManifest");
Expand Down Expand Up @@ -2605,7 +2607,7 @@ Status VersionSet::LogAndApply(ColumnFamilyData* column_family_data,

if (!w.edit_list.front()->IsColumnFamilyManipulation()) {
// This is cpu-heavy operations, which should be called outside mutex.
v->PrepareApply(mutable_cf_options, true);
v->PrepareApply(mutable_cf_options_copy, true);
}

// Write new record to MANIFEST log
Expand Down
11 changes: 7 additions & 4 deletions util/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ Status CopyFile(Env* env, const std::string& source,
}
size -= slice.size();
}
dest_writer->Sync(use_fsync);
return Status::OK();
return dest_writer->Sync(use_fsync);
}

// Utility function to create a file with the provided contents
Status CreateFile(Env* env, const std::string& destination,
const std::string& contents) {
const std::string& contents, bool use_fsync) {
const EnvOptions soptions;
Status s;
unique_ptr<WritableFileWriter> dest_writer;
Expand All @@ -79,7 +78,11 @@ Status CreateFile(Env* env, const std::string& destination,
return s;
}
dest_writer.reset(new WritableFileWriter(std::move(destfile), soptions));
return dest_writer->Append(Slice(contents));
s = dest_writer->Append(Slice(contents));
if (!s.ok()) {
return s;
}
return dest_writer->Sync(use_fsync);
}

Status DeleteSSTFile(const ImmutableDBOptions* db_options,
Expand Down
2 changes: 1 addition & 1 deletion util/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern Status CopyFile(Env* env, const std::string& source,
bool use_fsync);

extern Status CreateFile(Env* env, const std::string& destination,
const std::string& contents);
const std::string& contents, bool use_fsync);

extern Status DeleteSSTFile(const ImmutableDBOptions* db_options,
const std::string& fname, uint32_t path_id);
Expand Down
3 changes: 2 additions & 1 deletion utilities/checkpoint/checkpoint_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ Status CheckpointImpl::CreateCheckpoint(const std::string& checkpoint_dir,
} /* copy_file_cb */,
[&](const std::string& fname, const std::string& contents, FileType) {
ROCKS_LOG_INFO(db_options.info_log, "Creating %s", fname.c_str());
return CreateFile(db_->GetEnv(), full_private_path + fname, contents);
return CreateFile(db_->GetEnv(), full_private_path + fname, contents,
db_options.use_fsync);
} /* create_file_cb */,
&sequence_number, log_size_for_flush);
// we copied all the files, enable file deletions
Expand Down

0 comments on commit 57f5fbe

Please sign in to comment.