diff --git a/db/db_impl.cc b/db/db_impl.cc index 49b9595..c6da3a6 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -216,18 +216,26 @@ void DBImpl::MaybeIgnoreError(Status* s) const { } void DBImpl::DeleteObsoleteFiles() { + mutex_.AssertHeld(); + if (!bg_error_.ok()) { // After a background error, we don't know whether a new version may // or may not have been committed, so we cannot safely garbage collect. return; } + uint64_t log_number = versions_->LogNumber(); + uint64_t prev_log_number = versions_->PrevLogNumber(); + uint64_t manifest_file_number = versions_->ManifestFileNumber(); + // Make a set of all of the live files std::set live = pending_outputs_; versions_->AddLiveFiles(&live); std::vector filenames; env_->GetChildren(dbname_, &filenames); // Ignoring errors on purpose + + mutex_.Unlock(); uint64_t number; FileType type; for (size_t i = 0; i < filenames.size(); i++) { @@ -235,13 +243,13 @@ void DBImpl::DeleteObsoleteFiles() { bool keep = true; switch (type) { case kLogFile: - keep = ((number >= versions_->LogNumber()) || - (number == versions_->PrevLogNumber())); + keep = ((number >= log_number) || + (number == prev_log_number)); break; case kDescriptorFile: // Keep my manifest file, and any newer incarnations' // (in case there is a race that allows other incarnations) - keep = (number >= versions_->ManifestFileNumber()); + keep = (number >= manifest_file_number); break; case kTableFile: keep = (live.find(number) != live.end()); @@ -269,6 +277,7 @@ void DBImpl::DeleteObsoleteFiles() { } } } + mutex_.Lock(); } Status DBImpl::Recover(VersionEdit* edit) {