2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00: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).
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2016-12-22 01:35:00 +00:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2016-01-29 02:35:01 +00:00
|
|
|
#include "util/delete_scheduler.h"
|
2015-08-19 22:02:17 +00:00
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "rocksdb/env.h"
|
2017-03-16 02:22:52 +00:00
|
|
|
#include "util/logging.h"
|
2015-08-05 03:45:27 +00:00
|
|
|
#include "util/mutexlock.h"
|
2017-03-16 02:22:52 +00:00
|
|
|
#include "util/sst_file_manager_impl.h"
|
2015-08-05 03:45:27 +00:00
|
|
|
#include "util/sync_point.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
DeleteScheduler::DeleteScheduler(Env* env, int64_t rate_bytes_per_sec,
|
|
|
|
Logger* info_log,
|
2017-11-17 19:56:41 +00:00
|
|
|
SstFileManagerImpl* sst_file_manager,
|
|
|
|
double max_trash_db_ratio)
|
2015-08-05 03:45:27 +00:00
|
|
|
: env_(env),
|
2017-06-12 23:51:37 +00:00
|
|
|
total_trash_size_(0),
|
2015-08-05 03:45:27 +00:00
|
|
|
rate_bytes_per_sec_(rate_bytes_per_sec),
|
|
|
|
pending_files_(0),
|
|
|
|
closing_(false),
|
|
|
|
cv_(&mu_),
|
2016-01-29 02:35:01 +00:00
|
|
|
info_log_(info_log),
|
2017-11-17 19:56:41 +00:00
|
|
|
sst_file_manager_(sst_file_manager),
|
|
|
|
max_trash_db_ratio_(max_trash_db_ratio) {
|
2017-06-12 23:51:37 +00:00
|
|
|
assert(sst_file_manager != nullptr);
|
2017-11-17 19:56:41 +00:00
|
|
|
assert(max_trash_db_ratio >= 0);
|
2017-03-16 19:06:04 +00:00
|
|
|
bg_thread_.reset(
|
|
|
|
new port::Thread(&DeleteScheduler::BackgroundEmptyTrash, this));
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 02:35:01 +00:00
|
|
|
DeleteScheduler::~DeleteScheduler() {
|
2015-08-05 03:45:27 +00:00
|
|
|
{
|
2016-10-18 05:22:48 +00:00
|
|
|
InstrumentedMutexLock l(&mu_);
|
2015-08-05 03:45:27 +00:00
|
|
|
closing_ = true;
|
|
|
|
cv_.SignalAll();
|
|
|
|
}
|
|
|
|
if (bg_thread_) {
|
|
|
|
bg_thread_->join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 02:35:01 +00:00
|
|
|
Status DeleteScheduler::DeleteFile(const std::string& file_path) {
|
|
|
|
Status s;
|
2017-06-12 23:51:37 +00:00
|
|
|
if (rate_bytes_per_sec_.load() <= 0 ||
|
|
|
|
total_trash_size_.load() >
|
2017-11-17 19:56:41 +00:00
|
|
|
sst_file_manager_->GetTotalSize() * max_trash_db_ratio_.load()) {
|
2017-06-12 23:51:37 +00:00
|
|
|
// Rate limiting is disabled or trash size makes up more than
|
|
|
|
// max_trash_db_ratio_ (default 25%) of the total DB size
|
2017-03-16 19:06:04 +00:00
|
|
|
TEST_SYNC_POINT("DeleteScheduler::DeleteFile");
|
2016-01-29 02:35:01 +00:00
|
|
|
s = env_->DeleteFile(file_path);
|
2017-06-12 23:51:37 +00:00
|
|
|
if (s.ok()) {
|
2016-01-29 02:35:01 +00:00
|
|
|
sst_file_manager_->OnDeleteFile(file_path);
|
|
|
|
}
|
|
|
|
return s;
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move file to trash
|
2017-10-27 20:25:54 +00:00
|
|
|
std::string trash_file;
|
|
|
|
s = MarkAsTrash(file_path, &trash_file);
|
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
if (!s.ok()) {
|
2017-10-27 20:25:54 +00:00
|
|
|
ROCKS_LOG_ERROR(info_log_, "Failed to mark %s as trash", file_path.c_str());
|
2016-01-29 02:35:01 +00:00
|
|
|
s = env_->DeleteFile(file_path);
|
2017-06-12 23:51:37 +00:00
|
|
|
if (s.ok()) {
|
2016-01-29 02:35:01 +00:00
|
|
|
sst_file_manager_->OnDeleteFile(file_path);
|
|
|
|
}
|
|
|
|
return s;
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
// 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);
|
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
// Add file to delete queue
|
|
|
|
{
|
2016-10-18 05:22:48 +00:00
|
|
|
InstrumentedMutexLock l(&mu_);
|
2017-10-27 20:25:54 +00:00
|
|
|
queue_.push(trash_file);
|
2015-08-05 03:45:27 +00:00
|
|
|
pending_files_++;
|
|
|
|
if (pending_files_ == 1) {
|
|
|
|
cv_.SignalAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-01-29 02:35:01 +00:00
|
|
|
std::map<std::string, Status> DeleteScheduler::GetBackgroundErrors() {
|
2016-10-18 05:22:48 +00:00
|
|
|
InstrumentedMutexLock l(&mu_);
|
2015-08-05 03:45:27 +00:00
|
|
|
return bg_errors_;
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
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) {
|
2015-08-05 03:45:27 +00:00
|
|
|
Status s;
|
2017-10-27 20:25:54 +00:00
|
|
|
// 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);
|
|
|
|
file_delete = sfm->ScheduleFileDeletion(trash_file);
|
|
|
|
} 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
|
2015-08-05 03:45:27 +00:00
|
|
|
size_t idx = file_path.rfind("/");
|
|
|
|
if (idx == std::string::npos || idx == file_path.size() - 1) {
|
|
|
|
return Status::InvalidArgument("file_path is corrupted");
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
Status s;
|
|
|
|
if (DeleteScheduler::IsTrashFile(file_path)) {
|
|
|
|
// This is already a trash file
|
2017-12-13 02:15:15 +00:00
|
|
|
*trash_file = file_path;
|
2015-08-05 03:45:27 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
*trash_file = file_path + kTrashExtension;
|
2015-08-05 03:45:27 +00:00
|
|
|
// TODO(tec) : Implement Env::RenameFileIfNotExist and remove
|
|
|
|
// file_move_mu mutex.
|
2017-10-27 20:25:54 +00:00
|
|
|
int cnt = 0;
|
2016-10-18 05:22:48 +00:00
|
|
|
InstrumentedMutexLock l(&file_move_mu_);
|
2015-08-05 03:45:27 +00:00
|
|
|
while (true) {
|
2017-10-27 20:25:54 +00:00
|
|
|
s = env_->FileExists(*trash_file);
|
2015-08-05 03:45:27 +00:00
|
|
|
if (s.IsNotFound()) {
|
|
|
|
// We found a path for our file in trash
|
2017-10-27 20:25:54 +00:00
|
|
|
s = env_->RenameFile(file_path, *trash_file);
|
2015-08-05 03:45:27 +00:00
|
|
|
break;
|
|
|
|
} else if (s.ok()) {
|
|
|
|
// Name conflict, generate new random suffix
|
2017-10-27 20:25:54 +00:00
|
|
|
*trash_file = file_path + std::to_string(cnt) + kTrashExtension;
|
2015-08-05 03:45:27 +00:00
|
|
|
} else {
|
|
|
|
// Error during FileExists call, we cannot continue
|
|
|
|
break;
|
|
|
|
}
|
2017-10-27 20:25:54 +00:00
|
|
|
cnt++;
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
2017-06-12 23:51:37 +00:00
|
|
|
if (s.ok()) {
|
2017-10-27 20:25:54 +00:00
|
|
|
sst_file_manager_->OnMoveFile(file_path, *trash_file);
|
2016-01-29 02:35:01 +00:00
|
|
|
}
|
2015-08-05 03:45:27 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-01-29 02:35:01 +00:00
|
|
|
void DeleteScheduler::BackgroundEmptyTrash() {
|
|
|
|
TEST_SYNC_POINT("DeleteScheduler::BackgroundEmptyTrash");
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
while (true) {
|
2016-10-18 05:22:48 +00:00
|
|
|
InstrumentedMutexLock l(&mu_);
|
2015-08-05 03:45:27 +00:00
|
|
|
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;
|
2017-03-16 19:06:04 +00:00
|
|
|
int64_t current_delete_rate = rate_bytes_per_sec_.load();
|
2015-08-05 03:45:27 +00:00
|
|
|
while (!queue_.empty() && !closing_) {
|
2017-03-16 19:06:04 +00:00
|
|
|
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
|
2015-08-05 03:45:27 +00:00
|
|
|
std::string path_in_trash = queue_.front();
|
|
|
|
queue_.pop();
|
|
|
|
|
|
|
|
// We dont need to hold the lock while deleting the file
|
|
|
|
mu_.Unlock();
|
|
|
|
uint64_t deleted_bytes = 0;
|
|
|
|
// Delete file from trash and update total_penlty value
|
|
|
|
Status s = DeleteTrashFile(path_in_trash, &deleted_bytes);
|
|
|
|
total_deleted_bytes += deleted_bytes;
|
|
|
|
mu_.Lock();
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
bg_errors_[path_in_trash] = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply penlty if necessary
|
2017-03-16 19:06:04 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-01-29 02:35:01 +00:00
|
|
|
TEST_SYNC_POINT_CALLBACK("DeleteScheduler::BackgroundEmptyTrash:Wait",
|
2015-08-06 02:16:52 +00:00
|
|
|
&total_penlty);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
pending_files_--;
|
|
|
|
if (pending_files_ == 0) {
|
2015-08-19 22:02:17 +00:00
|
|
|
// Unblock WaitForEmptyTrash since there are no more files waiting
|
2015-08-05 03:45:27 +00:00
|
|
|
// to be deleted
|
|
|
|
cv_.SignalAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 02:35:01 +00:00
|
|
|
Status DeleteScheduler::DeleteTrashFile(const std::string& path_in_trash,
|
|
|
|
uint64_t* deleted_bytes) {
|
2015-08-05 03:45:27 +00:00
|
|
|
uint64_t file_size;
|
|
|
|
Status s = env_->GetFileSize(path_in_trash, &file_size);
|
|
|
|
if (s.ok()) {
|
2016-01-29 02:35:01 +00:00
|
|
|
TEST_SYNC_POINT("DeleteScheduler::DeleteTrashFile:DeleteFile");
|
2015-08-05 03:45:27 +00:00
|
|
|
s = env_->DeleteFile(path_in_trash);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
// Error while getting file size or while deleting
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_ERROR(info_log_, "Failed to delete %s from trash -- %s",
|
|
|
|
path_in_trash.c_str(), s.ToString().c_str());
|
2015-08-05 03:45:27 +00:00
|
|
|
*deleted_bytes = 0;
|
|
|
|
} else {
|
|
|
|
*deleted_bytes = file_size;
|
2017-06-12 23:51:37 +00:00
|
|
|
total_trash_size_.fetch_sub(file_size);
|
|
|
|
sst_file_manager_->OnDeleteFile(path_in_trash);
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-01-29 02:35:01 +00:00
|
|
|
void DeleteScheduler::WaitForEmptyTrash() {
|
2016-10-18 05:22:48 +00:00
|
|
|
InstrumentedMutexLock l(&mu_);
|
2015-08-05 03:45:27 +00:00
|
|
|
while (pending_files_ > 0 && !closing_) {
|
|
|
|
cv_.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
2016-12-22 01:35:00 +00:00
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|