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).
|
2015-08-05 05:45:27 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-12-22 02:35:00 +01:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2015-08-05 05:45:27 +02:00
|
|
|
#include <map>
|
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/instrumented_mutex.h"
|
2015-08-05 05:45:27 +02:00
|
|
|
#include "port/port.h"
|
|
|
|
|
|
|
|
#include "rocksdb/status.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
class Env;
|
|
|
|
class Logger;
|
2016-01-29 03:35:01 +01:00
|
|
|
class SstFileManagerImpl;
|
|
|
|
|
|
|
|
// DeleteScheduler allows the DB to enforce a rate limit on file deletion,
|
2017-10-27 22:25:54 +02:00
|
|
|
// Instead of deleteing files immediately, files are marked as trash
|
2016-01-29 03:35:01 +01:00
|
|
|
// and deleted in a background thread that apply sleep penlty between deletes
|
|
|
|
// if they are happening in a rate faster than rate_bytes_per_sec,
|
|
|
|
//
|
|
|
|
// Rate limiting can be turned off by setting rate_bytes_per_sec = 0, In this
|
|
|
|
// case DeleteScheduler will delete files immediately.
|
|
|
|
class DeleteScheduler {
|
2015-08-05 05:45:27 +02:00
|
|
|
public:
|
2017-10-27 22:25:54 +02:00
|
|
|
DeleteScheduler(Env* env, int64_t rate_bytes_per_sec, Logger* info_log,
|
2017-11-17 20:56:41 +01:00
|
|
|
SstFileManagerImpl* sst_file_manager,
|
2018-03-22 23:42:44 +01:00
|
|
|
double max_trash_db_ratio, uint64_t bytes_max_delete_chunk);
|
2015-08-05 05:45:27 +02:00
|
|
|
|
2016-01-29 03:35:01 +01:00
|
|
|
~DeleteScheduler();
|
2015-08-05 05:45:27 +02:00
|
|
|
|
|
|
|
// Return delete rate limit in bytes per second
|
2017-03-16 20:06:04 +01:00
|
|
|
int64_t GetRateBytesPerSecond() { return rate_bytes_per_sec_.load(); }
|
|
|
|
|
|
|
|
// Set delete rate limit in bytes per second
|
|
|
|
void SetRateBytesPerSecond(int64_t bytes_per_sec) {
|
2017-11-17 20:56:41 +01:00
|
|
|
rate_bytes_per_sec_.store(bytes_per_sec);
|
2017-03-16 20:06:04 +01:00
|
|
|
}
|
2015-08-05 05:45:27 +02:00
|
|
|
|
2017-10-27 22:25:54 +02:00
|
|
|
// Mark file as trash directory and schedule it's deletion
|
2015-08-05 05:45:27 +02:00
|
|
|
Status DeleteFile(const std::string& fname);
|
|
|
|
|
|
|
|
// Wait for all files being deleteing in the background to finish or for
|
|
|
|
// destructor to be called.
|
2015-08-20 00:02:17 +02:00
|
|
|
void WaitForEmptyTrash();
|
2015-08-05 05:45:27 +02:00
|
|
|
|
|
|
|
// Return a map containing errors that happened in BackgroundEmptyTrash
|
|
|
|
// file_path => error status
|
|
|
|
std::map<std::string, Status> GetBackgroundErrors();
|
|
|
|
|
2017-06-13 01:51:37 +02:00
|
|
|
uint64_t GetTotalTrashSize() { return total_trash_size_.load(); }
|
|
|
|
|
2017-11-17 20:56:41 +01:00
|
|
|
// Return trash/DB size ratio where new files will be deleted immediately
|
|
|
|
double GetMaxTrashDBRatio() {
|
|
|
|
return max_trash_db_ratio_.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update trash/DB size ratio where new files will be deleted immediately
|
|
|
|
void SetMaxTrashDBRatio(double r) {
|
2017-06-13 01:51:37 +02:00
|
|
|
assert(r >= 0);
|
2017-11-17 20:56:41 +01:00
|
|
|
max_trash_db_ratio_.store(r);
|
2017-06-13 01:51:37 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 22:25:54 +02:00
|
|
|
static const std::string kTrashExtension;
|
|
|
|
static bool IsTrashFile(const std::string& file_path);
|
|
|
|
|
|
|
|
// Check if there are any .trash filse in path, and schedule their deletion
|
|
|
|
// Or delete immediately if sst_file_manager is nullptr
|
|
|
|
static Status CleanupDirectory(Env* env, SstFileManagerImpl* sfm,
|
|
|
|
const std::string& path);
|
|
|
|
|
2015-08-05 05:45:27 +02:00
|
|
|
private:
|
2017-10-27 22:25:54 +02:00
|
|
|
Status MarkAsTrash(const std::string& file_path, std::string* path_in_trash);
|
2015-08-05 05:45:27 +02:00
|
|
|
|
|
|
|
Status DeleteTrashFile(const std::string& path_in_trash,
|
2018-03-22 23:42:44 +01:00
|
|
|
uint64_t* deleted_bytes, bool* is_complete);
|
2015-08-05 05:45:27 +02:00
|
|
|
|
|
|
|
void BackgroundEmptyTrash();
|
|
|
|
|
|
|
|
Env* env_;
|
2017-10-27 22:25:54 +02:00
|
|
|
// total size of trash files
|
2017-06-13 01:51:37 +02:00
|
|
|
std::atomic<uint64_t> total_trash_size_;
|
2015-08-05 05:45:27 +02:00
|
|
|
// Maximum number of bytes that should be deleted per second
|
2017-03-16 20:06:04 +01:00
|
|
|
std::atomic<int64_t> rate_bytes_per_sec_;
|
2015-08-05 05:45:27 +02:00
|
|
|
// Mutex to protect queue_, pending_files_, bg_errors_, closing_
|
2016-10-18 07:22:48 +02:00
|
|
|
InstrumentedMutex mu_;
|
2017-10-27 22:25:54 +02:00
|
|
|
// Queue of trash files that need to be deleted
|
2015-08-05 05:45:27 +02:00
|
|
|
std::queue<std::string> queue_;
|
2017-10-27 22:25:54 +02:00
|
|
|
// Number of trash files that are waiting to be deleted
|
2015-08-05 05:45:27 +02:00
|
|
|
int32_t pending_files_;
|
2018-03-22 23:42:44 +01:00
|
|
|
uint64_t bytes_max_delete_chunk_;
|
2015-08-05 05:45:27 +02:00
|
|
|
// Errors that happened in BackgroundEmptyTrash (file_path => error)
|
|
|
|
std::map<std::string, Status> bg_errors_;
|
2016-01-29 03:35:01 +01:00
|
|
|
// Set to true in ~DeleteScheduler() to force BackgroundEmptyTrash to stop
|
2015-08-05 05:45:27 +02:00
|
|
|
bool closing_;
|
|
|
|
// Condition variable signaled in these conditions
|
|
|
|
// - pending_files_ value change from 0 => 1
|
|
|
|
// - pending_files_ value change from 1 => 0
|
|
|
|
// - closing_ value is set to true
|
2016-10-18 07:22:48 +02:00
|
|
|
InstrumentedCondVar cv_;
|
2015-08-05 05:45:27 +02:00
|
|
|
// Background thread running BackgroundEmptyTrash
|
2017-02-06 23:43:55 +01:00
|
|
|
std::unique_ptr<port::Thread> bg_thread_;
|
2015-08-05 05:45:27 +02:00
|
|
|
// Mutex to protect threads from file name conflicts
|
2016-10-18 07:22:48 +02:00
|
|
|
InstrumentedMutex file_move_mu_;
|
2016-01-29 03:35:01 +01:00
|
|
|
Logger* info_log_;
|
|
|
|
SstFileManagerImpl* sst_file_manager_;
|
2017-11-17 20:56:41 +01:00
|
|
|
// If the trash size constitutes for more than this fraction of the total DB
|
|
|
|
// size we will start deleting new files passed to DeleteScheduler
|
|
|
|
// immediately
|
|
|
|
std::atomic<double> max_trash_db_ratio_;
|
2015-08-05 05:45:27 +02:00
|
|
|
static const uint64_t kMicrosInSecond = 1000 * 1000LL;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
2016-12-22 02:35:00 +01:00
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|