2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2012-09-15 02:11:35 +02:00
|
|
|
// Copyright (c) 2012 Facebook.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
2012-11-29 01:42:36 +01:00
|
|
|
// found in the LICENSE file.
|
2012-09-15 02:11:35 +02:00
|
|
|
|
2014-04-15 22:39:26 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2014-04-04 01:04:10 +02:00
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#include <inttypes.h>
|
2013-08-06 21:54:37 +02:00
|
|
|
#include <algorithm>
|
2012-09-15 02:11:35 +02:00
|
|
|
#include <string>
|
|
|
|
#include <stdint.h>
|
2013-08-06 21:54:37 +02:00
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "db/filename.h"
|
2012-09-15 02:11:35 +02:00
|
|
|
#include "db/version_set.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/env.h"
|
2012-09-15 02:11:35 +02:00
|
|
|
#include "port/port.h"
|
|
|
|
#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() {
|
|
|
|
MutexLock l(&mutex_);
|
2014-01-02 12:33:42 +01:00
|
|
|
++disable_delete_obsolete_files_;
|
|
|
|
if (disable_delete_obsolete_files_ == 1) {
|
|
|
|
Log(options_.info_log, "File Deletions Disabled");
|
2014-05-20 23:28:51 +02:00
|
|
|
} else {
|
|
|
|
Log(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) {
|
2013-11-09 00:23:46 +01:00
|
|
|
DeletionState deletion_state;
|
2014-01-02 12:33:42 +01:00
|
|
|
bool should_purge_files = false;
|
2013-11-09 00:23:46 +01:00
|
|
|
{
|
|
|
|
MutexLock 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) {
|
|
|
|
Log(options_.info_log, "File Deletions Enabled");
|
|
|
|
should_purge_files = true;
|
|
|
|
FindObsoleteFiles(deletion_state, true);
|
2014-05-20 23:28:51 +02:00
|
|
|
} else {
|
|
|
|
Log(options_.info_log,
|
|
|
|
"File Deletions Enable, but not really enabled. Counter: %d",
|
|
|
|
disable_delete_obsolete_files_);
|
2014-01-02 12:33:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (should_purge_files) {
|
|
|
|
PurgeObsoleteFiles(deletion_state);
|
2013-11-09 00:23:46 +01:00
|
|
|
}
|
|
|
|
LogFlush(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 {
|
|
|
|
return disable_delete_obsolete_files_;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
for (auto cfd : *versions_->GetColumnFamilySet()) {
|
|
|
|
cfd->Ref();
|
|
|
|
mutex_.Unlock();
|
|
|
|
status = FlushMemTable(cfd, FlushOptions());
|
|
|
|
mutex_.Lock();
|
2014-04-07 23:21:25 +02:00
|
|
|
cfd->Unref();
|
2014-02-25 22:16:59 +01:00
|
|
|
if (!status.ok()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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();
|
2013-10-03 23:38:32 +02:00
|
|
|
Log(options_.info_log, "Cannot Flush data %s\n",
|
|
|
|
status.ToString().c_str());
|
|
|
|
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()) {
|
|
|
|
cfd->current()->AddLiveFiles(&live);
|
|
|
|
}
|
2012-09-15 02:11:35 +02:00
|
|
|
|
2013-08-29 23:30:52 +02:00
|
|
|
ret.clear();
|
|
|
|
ret.reserve(live.size() + 2); //*.sst + CURRENT + MANIFEST
|
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_;
|
2013-08-29 23:30:52 +02:00
|
|
|
for (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(""));
|
|
|
|
ret.push_back(DescriptorFileName("", versions_->ManifestFileNumber()));
|
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
|
|
|
|
*manifest_file_size = versions_->ManifestFileSize();
|
|
|
|
|
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) {
|
2014-03-24 05:49:14 +01:00
|
|
|
// First get sorted files in db dir, then get sorted files from archived
|
|
|
|
// dir, to avoid a race condition where a log file is moved to archived
|
|
|
|
// dir in between.
|
|
|
|
Status s;
|
|
|
|
// list wal files in main db dir.
|
|
|
|
VectorLogPtr logs;
|
|
|
|
s = GetSortedWalsOfType(options_.wal_dir, logs, kAliveLogFile);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2013-08-06 21:54:37 +02:00
|
|
|
|
2014-03-24 05:49:14 +01:00
|
|
|
// Reproduce the race condition where a log file is moved
|
|
|
|
// to archived dir, between these two sync points, used in
|
|
|
|
// (DBTest,TransactionLogIteratorRace)
|
|
|
|
TEST_SYNC_POINT("DBImpl::GetSortedWalFiles:1");
|
|
|
|
TEST_SYNC_POINT("DBImpl::GetSortedWalFiles:2");
|
|
|
|
|
|
|
|
files.clear();
|
2013-08-29 23:30:52 +02:00
|
|
|
// list wal files in archive dir.
|
2013-10-01 23:46:52 +02:00
|
|
|
std::string archivedir = ArchivalDirectory(options_.wal_dir);
|
2013-08-06 21:54:37 +02:00
|
|
|
if (env_->FileExists(archivedir)) {
|
2014-03-24 05:49:14 +01:00
|
|
|
s = GetSortedWalsOfType(archivedir, files, kArchivedLogFile);
|
2013-08-06 21:54:37 +02:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 05:49:14 +01:00
|
|
|
|
|
|
|
uint64_t latest_archived_log_number = 0;
|
|
|
|
if (!files.empty()) {
|
|
|
|
latest_archived_log_number = files.back()->LogNumber();
|
2014-04-04 01:04:10 +02:00
|
|
|
Log(options_.info_log, "Latest Archived log: %" PRIu64,
|
2014-03-24 05:49:14 +01:00
|
|
|
latest_archived_log_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
files.reserve(files.size() + logs.size());
|
|
|
|
for (auto& log : logs) {
|
|
|
|
if (log->LogNumber() > latest_archived_log_number) {
|
|
|
|
files.push_back(std::move(log));
|
|
|
|
} else {
|
|
|
|
// When the race condition happens, we could see the
|
|
|
|
// same log in both db dir and archived dir. Simply
|
|
|
|
// ignore the one in db dir. Note that, if we read
|
|
|
|
// archived dir first, we would have missed the log file.
|
|
|
|
Log(options_.info_log, "%s already moved to archive",
|
|
|
|
log->PathName().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
2013-08-06 21:54:37 +02:00
|
|
|
}
|
|
|
|
|
2012-09-15 02:11:35 +02:00
|
|
|
}
|
2014-04-15 22:39:26 +02:00
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|