fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
354 lines
11 KiB
354 lines
11 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// 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).
|
||
10 years ago
|
|
||
8 years ago
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
6 years ago
|
#include "file/delete_scheduler.h"
|
||
9 years ago
|
|
||
10 years ago
|
#include <thread>
|
||
|
#include <vector>
|
||
|
|
||
6 years ago
|
#include "file/sst_file_manager_impl.h"
|
||
10 years ago
|
#include "port/port.h"
|
||
|
#include "rocksdb/env.h"
|
||
8 years ago
|
#include "util/logging.h"
|
||
10 years ago
|
#include "util/mutexlock.h"
|
||
|
#include "util/sync_point.h"
|
||
|
|
||
|
namespace rocksdb {
|
||
|
|
||
7 years ago
|
DeleteScheduler::DeleteScheduler(Env* env, int64_t rate_bytes_per_sec,
|
||
|
Logger* info_log,
|
||
7 years ago
|
SstFileManagerImpl* sst_file_manager,
|
||
7 years ago
|
double max_trash_db_ratio,
|
||
|
uint64_t bytes_max_delete_chunk)
|
||
10 years ago
|
: env_(env),
|
||
8 years ago
|
total_trash_size_(0),
|
||
10 years ago
|
rate_bytes_per_sec_(rate_bytes_per_sec),
|
||
|
pending_files_(0),
|
||
7 years ago
|
bytes_max_delete_chunk_(bytes_max_delete_chunk),
|
||
10 years ago
|
closing_(false),
|
||
|
cv_(&mu_),
|
||
9 years ago
|
info_log_(info_log),
|
||
7 years ago
|
sst_file_manager_(sst_file_manager),
|
||
|
max_trash_db_ratio_(max_trash_db_ratio) {
|
||
8 years ago
|
assert(sst_file_manager != nullptr);
|
||
7 years ago
|
assert(max_trash_db_ratio >= 0);
|
||
8 years ago
|
bg_thread_.reset(
|
||
|
new port::Thread(&DeleteScheduler::BackgroundEmptyTrash, this));
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
DeleteScheduler::~DeleteScheduler() {
|
||
10 years ago
|
{
|
||
8 years ago
|
InstrumentedMutexLock l(&mu_);
|
||
10 years ago
|
closing_ = true;
|
||
|
cv_.SignalAll();
|
||
|
}
|
||
|
if (bg_thread_) {
|
||
|
bg_thread_->join();
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
Status DeleteScheduler::DeleteFile(const std::string& file_path,
|
||
6 years ago
|
const std::string& dir_to_sync,
|
||
|
const bool force_bg) {
|
||
9 years ago
|
Status s;
|
||
6 years ago
|
if (rate_bytes_per_sec_.load() <= 0 || (!force_bg &&
|
||
8 years ago
|
total_trash_size_.load() >
|
||
6 years ago
|
sst_file_manager_->GetTotalSize() * max_trash_db_ratio_.load())) {
|
||
8 years ago
|
// Rate limiting is disabled or trash size makes up more than
|
||
|
// max_trash_db_ratio_ (default 25%) of the total DB size
|
||
8 years ago
|
TEST_SYNC_POINT("DeleteScheduler::DeleteFile");
|
||
9 years ago
|
s = env_->DeleteFile(file_path);
|
||
8 years ago
|
if (s.ok()) {
|
||
9 years ago
|
sst_file_manager_->OnDeleteFile(file_path);
|
||
|
}
|
||
|
return s;
|
||
10 years ago
|
}
|
||
|
|
||
|
// Move file to trash
|
||
7 years ago
|
std::string trash_file;
|
||
|
s = MarkAsTrash(file_path, &trash_file);
|
||
|
|
||
10 years ago
|
if (!s.ok()) {
|
||
7 years ago
|
ROCKS_LOG_ERROR(info_log_, "Failed to mark %s as trash", file_path.c_str());
|
||
9 years ago
|
s = env_->DeleteFile(file_path);
|
||
8 years ago
|
if (s.ok()) {
|
||
9 years ago
|
sst_file_manager_->OnDeleteFile(file_path);
|
||
|
}
|
||
|
return s;
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
// Update the total trash size
|
||
|
uint64_t trash_file_size = 0;
|
||
|
env_->GetFileSize(trash_file, &trash_file_size);
|
||
|
total_trash_size_.fetch_add(trash_file_size);
|
||
|
|
||
10 years ago
|
// Add file to delete queue
|
||
|
{
|
||
8 years ago
|
InstrumentedMutexLock l(&mu_);
|
||
7 years ago
|
queue_.emplace(trash_file, dir_to_sync);
|
||
10 years ago
|
pending_files_++;
|
||
|
if (pending_files_ == 1) {
|
||
|
cv_.SignalAll();
|
||
|
}
|
||
|
}
|
||
|
return s;
|
||
|
}
|
||
|
|
||
9 years ago
|
std::map<std::string, Status> DeleteScheduler::GetBackgroundErrors() {
|
||
8 years ago
|
InstrumentedMutexLock l(&mu_);
|
||
10 years ago
|
return bg_errors_;
|
||
|
}
|
||
|
|
||
7 years ago
|
const std::string DeleteScheduler::kTrashExtension = ".trash";
|
||
|
bool DeleteScheduler::IsTrashFile(const std::string& file_path) {
|
||
|
return (file_path.size() >= kTrashExtension.size() &&
|
||
|
file_path.rfind(kTrashExtension) ==
|
||
|
file_path.size() - kTrashExtension.size());
|
||
|
}
|
||
|
|
||
|
Status DeleteScheduler::CleanupDirectory(Env* env, SstFileManagerImpl* sfm,
|
||
|
const std::string& path) {
|
||
10 years ago
|
Status s;
|
||
7 years ago
|
// Check if there are any files marked as trash in this path
|
||
|
std::vector<std::string> files_in_path;
|
||
|
s = env->GetChildren(path, &files_in_path);
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
|
for (const std::string& current_file : files_in_path) {
|
||
|
if (!DeleteScheduler::IsTrashFile(current_file)) {
|
||
|
// not a trash file, skip
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
Status file_delete;
|
||
|
std::string trash_file = path + "/" + current_file;
|
||
|
if (sfm) {
|
||
|
// We have an SstFileManager that will schedule the file delete
|
||
|
sfm->OnAddFile(trash_file);
|
||
7 years ago
|
file_delete = sfm->ScheduleFileDeletion(trash_file, path);
|
||
7 years ago
|
} else {
|
||
|
// Delete the file immediately
|
||
|
file_delete = env->DeleteFile(trash_file);
|
||
|
}
|
||
|
|
||
|
if (s.ok() && !file_delete.ok()) {
|
||
|
s = file_delete;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
Status DeleteScheduler::MarkAsTrash(const std::string& file_path,
|
||
|
std::string* trash_file) {
|
||
|
// Sanity check of the path
|
||
10 years ago
|
size_t idx = file_path.rfind("/");
|
||
|
if (idx == std::string::npos || idx == file_path.size() - 1) {
|
||
|
return Status::InvalidArgument("file_path is corrupted");
|
||
|
}
|
||
|
|
||
7 years ago
|
Status s;
|
||
|
if (DeleteScheduler::IsTrashFile(file_path)) {
|
||
|
// This is already a trash file
|
||
7 years ago
|
*trash_file = file_path;
|
||
10 years ago
|
return s;
|
||
|
}
|
||
|
|
||
7 years ago
|
*trash_file = file_path + kTrashExtension;
|
||
10 years ago
|
// TODO(tec) : Implement Env::RenameFileIfNotExist and remove
|
||
|
// file_move_mu mutex.
|
||
7 years ago
|
int cnt = 0;
|
||
8 years ago
|
InstrumentedMutexLock l(&file_move_mu_);
|
||
10 years ago
|
while (true) {
|
||
7 years ago
|
s = env_->FileExists(*trash_file);
|
||
10 years ago
|
if (s.IsNotFound()) {
|
||
|
// We found a path for our file in trash
|
||
7 years ago
|
s = env_->RenameFile(file_path, *trash_file);
|
||
10 years ago
|
break;
|
||
|
} else if (s.ok()) {
|
||
|
// Name conflict, generate new random suffix
|
||
7 years ago
|
*trash_file = file_path + std::to_string(cnt) + kTrashExtension;
|
||
10 years ago
|
} else {
|
||
|
// Error during FileExists call, we cannot continue
|
||
|
break;
|
||
|
}
|
||
7 years ago
|
cnt++;
|
||
10 years ago
|
}
|
||
8 years ago
|
if (s.ok()) {
|
||
7 years ago
|
sst_file_manager_->OnMoveFile(file_path, *trash_file);
|
||
9 years ago
|
}
|
||
10 years ago
|
return s;
|
||
|
}
|
||
|
|
||
9 years ago
|
void DeleteScheduler::BackgroundEmptyTrash() {
|
||
|
TEST_SYNC_POINT("DeleteScheduler::BackgroundEmptyTrash");
|
||
10 years ago
|
|
||
|
while (true) {
|
||
8 years ago
|
InstrumentedMutexLock l(&mu_);
|
||
10 years ago
|
while (queue_.empty() && !closing_) {
|
||
|
cv_.Wait();
|
||
|
}
|
||
|
|
||
|
if (closing_) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Delete all files in queue_
|
||
|
uint64_t start_time = env_->NowMicros();
|
||
|
uint64_t total_deleted_bytes = 0;
|
||
8 years ago
|
int64_t current_delete_rate = rate_bytes_per_sec_.load();
|
||
10 years ago
|
while (!queue_.empty() && !closing_) {
|
||
8 years ago
|
if (current_delete_rate != rate_bytes_per_sec_.load()) {
|
||
|
// User changed the delete rate
|
||
|
current_delete_rate = rate_bytes_per_sec_.load();
|
||
|
start_time = env_->NowMicros();
|
||
|
total_deleted_bytes = 0;
|
||
|
}
|
||
|
|
||
|
// Get new file to delete
|
||
7 years ago
|
const FileAndDir& fad = queue_.front();
|
||
|
std::string path_in_trash = fad.fname;
|
||
10 years ago
|
|
||
|
// We dont need to hold the lock while deleting the file
|
||
|
mu_.Unlock();
|
||
|
uint64_t deleted_bytes = 0;
|
||
7 years ago
|
bool is_complete = true;
|
||
10 years ago
|
// Delete file from trash and update total_penlty value
|
||
7 years ago
|
Status s =
|
||
|
DeleteTrashFile(path_in_trash, fad.dir, &deleted_bytes, &is_complete);
|
||
10 years ago
|
total_deleted_bytes += deleted_bytes;
|
||
|
mu_.Lock();
|
||
7 years ago
|
if (is_complete) {
|
||
|
queue_.pop();
|
||
|
}
|
||
10 years ago
|
|
||
|
if (!s.ok()) {
|
||
|
bg_errors_[path_in_trash] = s;
|
||
|
}
|
||
|
|
||
|
// Apply penlty if necessary
|
||
8 years ago
|
uint64_t total_penlty;
|
||
|
if (current_delete_rate > 0) {
|
||
|
// rate limiting is enabled
|
||
|
total_penlty =
|
||
|
((total_deleted_bytes * kMicrosInSecond) / current_delete_rate);
|
||
|
while (!closing_ && !cv_.TimedWait(start_time + total_penlty)) {}
|
||
|
} else {
|
||
|
// rate limiting is disabled
|
||
|
total_penlty = 0;
|
||
|
}
|
||
9 years ago
|
TEST_SYNC_POINT_CALLBACK("DeleteScheduler::BackgroundEmptyTrash:Wait",
|
||
10 years ago
|
&total_penlty);
|
||
10 years ago
|
|
||
7 years ago
|
if (is_complete) {
|
||
|
pending_files_--;
|
||
|
}
|
||
10 years ago
|
if (pending_files_ == 0) {
|
||
9 years ago
|
// Unblock WaitForEmptyTrash since there are no more files waiting
|
||
10 years ago
|
// to be deleted
|
||
|
cv_.SignalAll();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
Status DeleteScheduler::DeleteTrashFile(const std::string& path_in_trash,
|
||
7 years ago
|
const std::string& dir_to_sync,
|
||
7 years ago
|
uint64_t* deleted_bytes,
|
||
|
bool* is_complete) {
|
||
10 years ago
|
uint64_t file_size;
|
||
|
Status s = env_->GetFileSize(path_in_trash, &file_size);
|
||
7 years ago
|
*is_complete = true;
|
||
|
TEST_SYNC_POINT("DeleteScheduler::DeleteTrashFile:DeleteFile");
|
||
10 years ago
|
if (s.ok()) {
|
||
7 years ago
|
bool need_full_delete = true;
|
||
|
if (bytes_max_delete_chunk_ != 0 && file_size > bytes_max_delete_chunk_) {
|
||
7 years ago
|
uint64_t num_hard_links = 2;
|
||
|
// We don't have to worry aobut data race between linking a new
|
||
|
// file after the number of file link check and ftruncte because
|
||
|
// the file is now in trash and no hardlink is supposed to create
|
||
|
// to trash files by RocksDB.
|
||
|
Status my_status = env_->NumFileLinks(path_in_trash, &num_hard_links);
|
||
7 years ago
|
if (my_status.ok()) {
|
||
7 years ago
|
if (num_hard_links == 1) {
|
||
6 years ago
|
std::unique_ptr<WritableFile> wf;
|
||
7 years ago
|
my_status =
|
||
|
env_->ReopenWritableFile(path_in_trash, &wf, EnvOptions());
|
||
|
if (my_status.ok()) {
|
||
|
my_status = wf->Truncate(file_size - bytes_max_delete_chunk_);
|
||
|
if (my_status.ok()) {
|
||
|
TEST_SYNC_POINT("DeleteScheduler::DeleteTrashFile:Fsync");
|
||
|
my_status = wf->Fsync();
|
||
|
}
|
||
|
}
|
||
|
if (my_status.ok()) {
|
||
|
*deleted_bytes = bytes_max_delete_chunk_;
|
||
|
need_full_delete = false;
|
||
|
*is_complete = false;
|
||
|
} else {
|
||
|
ROCKS_LOG_WARN(info_log_,
|
||
|
"Failed to partially delete %s from trash -- %s",
|
||
|
path_in_trash.c_str(), my_status.ToString().c_str());
|
||
|
}
|
||
|
} else {
|
||
|
ROCKS_LOG_INFO(info_log_,
|
||
|
"Cannot delete %s slowly through ftruncate from trash "
|
||
|
"as it has other links",
|
||
|
path_in_trash.c_str());
|
||
7 years ago
|
}
|
||
7 years ago
|
} else if (!num_link_error_printed_) {
|
||
|
ROCKS_LOG_INFO(
|
||
|
info_log_,
|
||
|
"Cannot delete files slowly through ftruncate from trash "
|
||
|
"as Env::NumFileLinks() returns error: %s",
|
||
|
my_status.ToString().c_str());
|
||
|
num_link_error_printed_ = true;
|
||
7 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
7 years ago
|
if (need_full_delete) {
|
||
|
s = env_->DeleteFile(path_in_trash);
|
||
7 years ago
|
if (!dir_to_sync.empty()) {
|
||
|
std::unique_ptr<Directory> dir_obj;
|
||
|
if (s.ok()) {
|
||
|
s = env_->NewDirectory(dir_to_sync, &dir_obj);
|
||
|
}
|
||
|
if (s.ok()) {
|
||
|
s = dir_obj->Fsync();
|
||
|
TEST_SYNC_POINT_CALLBACK(
|
||
|
"DeleteScheduler::DeleteTrashFile::AfterSyncDir",
|
||
|
reinterpret_cast<void*>(const_cast<std::string*>(&dir_to_sync)));
|
||
|
}
|
||
|
}
|
||
7 years ago
|
*deleted_bytes = file_size;
|
||
|
sst_file_manager_->OnDeleteFile(path_in_trash);
|
||
|
}
|
||
|
}
|
||
10 years ago
|
if (!s.ok()) {
|
||
|
// Error while getting file size or while deleting
|
||
8 years ago
|
ROCKS_LOG_ERROR(info_log_, "Failed to delete %s from trash -- %s",
|
||
|
path_in_trash.c_str(), s.ToString().c_str());
|
||
10 years ago
|
*deleted_bytes = 0;
|
||
|
} else {
|
||
7 years ago
|
total_trash_size_.fetch_sub(*deleted_bytes);
|
||
10 years ago
|
}
|
||
|
|
||
|
return s;
|
||
|
}
|
||
|
|
||
9 years ago
|
void DeleteScheduler::WaitForEmptyTrash() {
|
||
8 years ago
|
InstrumentedMutexLock l(&mu_);
|
||
10 years ago
|
while (pending_files_ > 0 && !closing_) {
|
||
|
cv_.Wait();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
} // namespace rocksdb
|
||
8 years ago
|
|
||
|
#endif // ROCKSDB_LITE
|