2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 23:59:46 +02:00
|
|
|
//
|
2012-09-15 02:11:35 +02:00
|
|
|
|
2014-04-15 22:39:26 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2014-09-05 08:14:37 +02:00
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
2014-04-04 01:04:10 +02:00
|
|
|
#define __STDC_FORMAT_MACROS
|
2014-09-05 08:14:37 +02:00
|
|
|
#endif
|
|
|
|
|
2014-04-04 01:04:10 +02:00
|
|
|
#include <inttypes.h>
|
2016-06-10 04:03:10 +02:00
|
|
|
#include <stdint.h>
|
2013-08-06 21:54:37 +02:00
|
|
|
#include <algorithm>
|
2012-09-15 02:11:35 +02:00
|
|
|
#include <string>
|
2013-08-06 21:54:37 +02:00
|
|
|
#include "db/db_impl.h"
|
2014-11-15 00:43:10 +01:00
|
|
|
#include "db/job_context.h"
|
2012-09-15 02:11:35 +02:00
|
|
|
#include "db/version_set.h"
|
2016-06-10 04:03:10 +02:00
|
|
|
#include "port/port.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/env.h"
|
2016-06-10 04:03:10 +02:00
|
|
|
#include "util/file_util.h"
|
2017-04-04 03:27:24 +02:00
|
|
|
#include "util/filename.h"
|
2012-09-15 02:11:35 +02:00
|
|
|
#include "util/mutexlock.h"
|
2014-03-24 05:49:14 +01:00
|
|
|
#include "util/sync_point.h"
|
2012-09-15 02:11:35 +02:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2012-09-15 02:11:35 +02:00
|
|
|
|
|
|
|
Status DBImpl::DisableFileDeletions() {
|
2015-02-05 06:39:45 +01:00
|
|
|
InstrumentedMutexLock l(&mutex_);
|
2014-01-02 12:33:42 +01:00
|
|
|
++disable_delete_obsolete_files_;
|
|
|
|
if (disable_delete_obsolete_files_ == 1) {
|
2017-03-16 03:22:52 +01:00
|
|
|
ROCKS_LOG_INFO(immutable_db_options_.info_log, "File Deletions Disabled");
|
2014-05-20 23:28:51 +02:00
|
|
|
} else {
|
2017-03-16 03:22:52 +01:00
|
|
|
ROCKS_LOG_WARN(immutable_db_options_.info_log,
|
|
|
|
"File Deletions Disabled, but already disabled. Counter: %d",
|
|
|
|
disable_delete_obsolete_files_);
|
2014-01-02 12:33:42 +01:00
|
|
|
}
|
2012-09-15 02:11:35 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2014-01-02 12:33:42 +01:00
|
|
|
Status DBImpl::EnableFileDeletions(bool force) {
|
2015-02-12 18:54:48 +01:00
|
|
|
// Job id == 0 means that this is not our background process, but rather
|
|
|
|
// user thread
|
|
|
|
JobContext job_context(0);
|
2018-10-30 18:32:05 +01:00
|
|
|
bool file_deletion_enabled = false;
|
2013-11-09 00:23:46 +01:00
|
|
|
{
|
2015-02-05 06:39:45 +01:00
|
|
|
InstrumentedMutexLock l(&mutex_);
|
2014-01-02 12:33:42 +01:00
|
|
|
if (force) {
|
|
|
|
// if force, we need to enable file deletions right away
|
|
|
|
disable_delete_obsolete_files_ = 0;
|
|
|
|
} else if (disable_delete_obsolete_files_ > 0) {
|
|
|
|
--disable_delete_obsolete_files_;
|
|
|
|
}
|
|
|
|
if (disable_delete_obsolete_files_ == 0) {
|
2018-10-30 18:32:05 +01:00
|
|
|
file_deletion_enabled = true;
|
2014-10-28 19:54:33 +01:00
|
|
|
FindObsoleteFiles(&job_context, true);
|
2018-01-18 02:37:10 +01:00
|
|
|
bg_cv_.SignalAll();
|
2014-01-02 12:33:42 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-30 18:32:05 +01:00
|
|
|
if (file_deletion_enabled) {
|
|
|
|
ROCKS_LOG_INFO(immutable_db_options_.info_log, "File Deletions Enabled");
|
2014-10-28 19:54:33 +01:00
|
|
|
PurgeObsoleteFiles(job_context);
|
2018-10-30 18:32:05 +01:00
|
|
|
} else {
|
|
|
|
ROCKS_LOG_WARN(immutable_db_options_.info_log,
|
|
|
|
"File Deletions Enable, but not really enabled. Counter: %d",
|
|
|
|
disable_delete_obsolete_files_);
|
2013-11-09 00:23:46 +01:00
|
|
|
}
|
2014-11-15 01:57:17 +01:00
|
|
|
job_context.Clean();
|
2016-09-24 01:34:04 +02:00
|
|
|
LogFlush(immutable_db_options_.info_log);
|
2012-09-15 02:11:35 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2014-08-27 01:26:29 +02:00
|
|
|
int DBImpl::IsFileDeletionsEnabled() const {
|
2018-03-22 06:07:55 +01:00
|
|
|
return !disable_delete_obsolete_files_;
|
2014-08-27 01:26:29 +02:00
|
|
|
}
|
|
|
|
|
2012-11-29 01:42:36 +01:00
|
|
|
Status DBImpl::GetLiveFiles(std::vector<std::string>& ret,
|
2013-10-03 23:38:32 +02:00
|
|
|
uint64_t* manifest_file_size,
|
|
|
|
bool flush_memtable) {
|
2012-09-24 23:01:01 +02:00
|
|
|
*manifest_file_size = 0;
|
2012-09-15 02:11:35 +02:00
|
|
|
|
2014-02-25 22:16:59 +01:00
|
|
|
mutex_.Lock();
|
|
|
|
|
2013-10-03 23:38:32 +02:00
|
|
|
if (flush_memtable) {
|
|
|
|
// flush all dirty data to disk.
|
2014-02-25 22:16:59 +01:00
|
|
|
Status status;
|
2018-10-27 00:06:44 +02:00
|
|
|
if (atomic_flush_) {
|
|
|
|
autovector<ColumnFamilyData*> cfds;
|
|
|
|
SelectColumnFamiliesForAtomicFlush(&cfds);
|
2014-02-25 22:16:59 +01:00
|
|
|
mutex_.Unlock();
|
2018-10-27 00:06:44 +02:00
|
|
|
status = AtomicFlushMemTables(cfds, FlushOptions(),
|
|
|
|
FlushReason::kGetLiveFiles);
|
2014-02-25 22:16:59 +01:00
|
|
|
mutex_.Lock();
|
2018-10-27 00:06:44 +02:00
|
|
|
} else {
|
|
|
|
for (auto cfd : *versions_->GetColumnFamilySet()) {
|
|
|
|
if (cfd->IsDropped()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cfd->Ref();
|
|
|
|
mutex_.Unlock();
|
|
|
|
status = FlushMemTable(cfd, FlushOptions(), FlushReason::kGetLiveFiles);
|
|
|
|
TEST_SYNC_POINT("DBImpl::GetLiveFiles:1");
|
|
|
|
TEST_SYNC_POINT("DBImpl::GetLiveFiles:2");
|
|
|
|
mutex_.Lock();
|
|
|
|
cfd->Unref();
|
|
|
|
if (!status.ok()) {
|
|
|
|
break;
|
|
|
|
}
|
2014-02-25 22:16:59 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-07 23:21:25 +02:00
|
|
|
versions_->GetColumnFamilySet()->FreeDeadColumnFamilies();
|
|
|
|
|
2013-10-03 23:38:32 +02:00
|
|
|
if (!status.ok()) {
|
2014-02-25 22:16:59 +01:00
|
|
|
mutex_.Unlock();
|
2017-03-16 03:22:52 +01:00
|
|
|
ROCKS_LOG_ERROR(immutable_db_options_.info_log, "Cannot Flush data %s\n",
|
|
|
|
status.ToString().c_str());
|
2013-10-03 23:38:32 +02:00
|
|
|
return status;
|
|
|
|
}
|
2012-09-15 02:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a set of all of the live *.sst files
|
2014-07-02 18:54:20 +02:00
|
|
|
std::vector<FileDescriptor> live;
|
2014-02-07 23:47:16 +01:00
|
|
|
for (auto cfd : *versions_->GetColumnFamilySet()) {
|
2015-03-20 01:04:29 +01:00
|
|
|
if (cfd->IsDropped()) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-07 23:47:16 +01:00
|
|
|
cfd->current()->AddLiveFiles(&live);
|
|
|
|
}
|
2012-09-15 02:11:35 +02:00
|
|
|
|
2013-08-29 23:30:52 +02:00
|
|
|
ret.clear();
|
2016-06-10 04:03:10 +02:00
|
|
|
ret.reserve(live.size() + 3); // *.sst + CURRENT + MANIFEST + OPTIONS
|
2012-09-15 02:11:35 +02:00
|
|
|
|
|
|
|
// create names of the live files. The names are not absolute
|
|
|
|
// paths, instead they are relative to dbname_;
|
2018-10-10 02:13:53 +02:00
|
|
|
for (const auto& live_file : live) {
|
2014-07-02 18:54:20 +02:00
|
|
|
ret.push_back(MakeTableFileName("", live_file.GetNumber()));
|
2012-09-15 02:11:35 +02:00
|
|
|
}
|
|
|
|
|
2013-08-29 23:30:52 +02:00
|
|
|
ret.push_back(CurrentFileName(""));
|
2014-11-04 02:45:55 +01:00
|
|
|
ret.push_back(DescriptorFileName("", versions_->manifest_file_number()));
|
2016-06-10 04:03:10 +02:00
|
|
|
ret.push_back(OptionsFileName("", versions_->options_file_number()));
|
2012-09-15 02:11:35 +02:00
|
|
|
|
2012-09-24 23:01:01 +02:00
|
|
|
// find length of manifest file while holding the mutex lock
|
2014-11-04 02:45:55 +01:00
|
|
|
*manifest_file_size = versions_->manifest_file_size();
|
2012-09-24 23:01:01 +02:00
|
|
|
|
2014-02-25 22:16:59 +01:00
|
|
|
mutex_.Unlock();
|
2012-09-15 02:11:35 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2013-08-06 21:54:37 +02:00
|
|
|
Status DBImpl::GetSortedWalFiles(VectorLogPtr& files) {
|
2018-01-18 02:37:10 +01:00
|
|
|
{
|
|
|
|
// If caller disabled deletions, this function should return files that are
|
|
|
|
// guaranteed not to be deleted until deletions are re-enabled. We need to
|
|
|
|
// wait for pending purges to finish since WalManager doesn't know which
|
|
|
|
// files are going to be purged. Additional purges won't be scheduled as
|
|
|
|
// long as deletions are disabled (so the below loop must terminate).
|
|
|
|
InstrumentedMutexLock l(&mutex_);
|
|
|
|
while (disable_delete_obsolete_files_ > 0 &&
|
|
|
|
pending_purge_obsolete_files_ > 0) {
|
|
|
|
bg_cv_.Wait();
|
|
|
|
}
|
|
|
|
}
|
2014-10-30 01:43:37 +01:00
|
|
|
return wal_manager_.GetSortedWalFiles(files);
|
2013-08-06 21:54:37 +02:00
|
|
|
}
|
2014-11-14 20:38:26 +01:00
|
|
|
|
2012-09-15 02:11:35 +02:00
|
|
|
}
|
2014-04-15 22:39:26 +02:00
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|