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
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-12-22 01:35:00 +00:00
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
#include <map>
|
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "monitoring/instrumented_mutex.h"
|
2015-08-05 03:45:27 +00:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "rocksdb/status.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
class Env;
|
2021-01-26 06:07:26 +00:00
|
|
|
class FileSystem;
|
2015-08-05 03:45:27 +00:00
|
|
|
class Logger;
|
2016-01-29 02:35:01 +00:00
|
|
|
class SstFileManagerImpl;
|
2021-01-26 06:07:26 +00:00
|
|
|
class SystemClock;
|
2016-01-29 02:35:01 +00:00
|
|
|
|
|
|
|
// DeleteScheduler allows the DB to enforce a rate limit on file deletion,
|
2017-10-27 20:25:54 +00:00
|
|
|
// Instead of deleteing files immediately, files are marked as trash
|
2021-01-07 23:21:51 +00:00
|
|
|
// and deleted in a background thread that apply sleep penalty between deletes
|
2016-01-29 02:35:01 +00:00
|
|
|
// 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 03:45:27 +00:00
|
|
|
public:
|
2021-03-15 11:32:24 +00:00
|
|
|
DeleteScheduler(SystemClock* clock, FileSystem* fs,
|
2021-01-26 06:07:26 +00:00
|
|
|
int64_t rate_bytes_per_sec, Logger* info_log,
|
|
|
|
SstFileManagerImpl* sst_file_manager,
|
2018-03-22 22:42:44 +00:00
|
|
|
double max_trash_db_ratio, uint64_t bytes_max_delete_chunk);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2016-01-29 02:35:01 +00:00
|
|
|
~DeleteScheduler();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
// Return delete rate limit in bytes per second
|
2017-03-16 19:06:04 +00: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 19:56:41 +00:00
|
|
|
rate_bytes_per_sec_.store(bytes_per_sec);
|
2020-03-24 18:24:53 +00:00
|
|
|
MaybeCreateBackgroundThread();
|
2017-03-16 19:06:04 +00:00
|
|
|
}
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2021-01-07 23:21:51 +00:00
|
|
|
// Mark file as trash directory and schedule its deletion. If force_bg is
|
2019-01-29 22:27:30 +00:00
|
|
|
// set, it forces the file to always be deleted in the background thread,
|
|
|
|
// except when rate limiting is disabled
|
|
|
|
Status DeleteFile(const std::string& fname, const std::string& dir_to_sync,
|
2022-10-25 01:34:52 +00:00
|
|
|
const bool force_bg = false);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
// Wait for all files being deleteing in the background to finish or for
|
|
|
|
// destructor to be called.
|
2015-08-19 22:02:17 +00:00
|
|
|
void WaitForEmptyTrash();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
// Return a map containing errors that happened in BackgroundEmptyTrash
|
|
|
|
// file_path => error status
|
|
|
|
std::map<std::string, Status> GetBackgroundErrors();
|
|
|
|
|
2017-06-12 23:51:37 +00:00
|
|
|
uint64_t GetTotalTrashSize() { return total_trash_size_.load(); }
|
|
|
|
|
2017-11-17 19:56:41 +00:00
|
|
|
// Return trash/DB size ratio where new files will be deleted immediately
|
2022-10-25 01:34:52 +00:00
|
|
|
double GetMaxTrashDBRatio() { return max_trash_db_ratio_.load(); }
|
2017-11-17 19:56:41 +00:00
|
|
|
|
|
|
|
// Update trash/DB size ratio where new files will be deleted immediately
|
|
|
|
void SetMaxTrashDBRatio(double r) {
|
2017-06-12 23:51:37 +00:00
|
|
|
assert(r >= 0);
|
2017-11-17 19:56:41 +00:00
|
|
|
max_trash_db_ratio_.store(r);
|
2017-06-12 23:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
static const std::string kTrashExtension;
|
|
|
|
static bool IsTrashFile(const std::string& file_path);
|
|
|
|
|
2021-01-07 23:21:51 +00:00
|
|
|
// Check if there are any .trash files in path, and schedule their deletion
|
2017-10-27 20:25:54 +00:00
|
|
|
// Or delete immediately if sst_file_manager is nullptr
|
|
|
|
static Status CleanupDirectory(Env* env, SstFileManagerImpl* sfm,
|
|
|
|
const std::string& path);
|
|
|
|
|
2020-06-05 16:41:03 +00:00
|
|
|
void SetStatisticsPtr(const std::shared_ptr<Statistics>& stats) {
|
2020-06-26 22:33:51 +00:00
|
|
|
InstrumentedMutexLock l(&mu_);
|
2020-06-05 16:41:03 +00:00
|
|
|
stats_ = stats;
|
|
|
|
}
|
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
private:
|
2017-10-27 20:25:54 +00:00
|
|
|
Status MarkAsTrash(const std::string& file_path, std::string* path_in_trash);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
Status DeleteTrashFile(const std::string& path_in_trash,
|
2018-04-26 20:51:39 +00:00
|
|
|
const std::string& dir_to_sync,
|
2018-03-22 22:42:44 +00:00
|
|
|
uint64_t* deleted_bytes, bool* is_complete);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
void BackgroundEmptyTrash();
|
|
|
|
|
2020-03-24 18:24:53 +00:00
|
|
|
void MaybeCreateBackgroundThread();
|
|
|
|
|
2021-03-15 11:32:24 +00:00
|
|
|
SystemClock* clock_;
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
FileSystem* fs_;
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
// total size of trash files
|
2017-06-12 23:51:37 +00:00
|
|
|
std::atomic<uint64_t> total_trash_size_;
|
2015-08-05 03:45:27 +00:00
|
|
|
// Maximum number of bytes that should be deleted per second
|
2017-03-16 19:06:04 +00:00
|
|
|
std::atomic<int64_t> rate_bytes_per_sec_;
|
2020-06-26 22:33:51 +00:00
|
|
|
// Mutex to protect queue_, pending_files_, bg_errors_, closing_, stats_
|
2016-10-18 05:22:48 +00:00
|
|
|
InstrumentedMutex mu_;
|
2018-04-26 20:51:39 +00:00
|
|
|
|
|
|
|
struct FileAndDir {
|
|
|
|
FileAndDir(const std::string& f, const std::string& d) : fname(f), dir(d) {}
|
|
|
|
std::string fname;
|
|
|
|
std::string dir; // empty will be skipped.
|
|
|
|
};
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
// Queue of trash files that need to be deleted
|
2018-04-26 20:51:39 +00:00
|
|
|
std::queue<FileAndDir> queue_;
|
2017-10-27 20:25:54 +00:00
|
|
|
// Number of trash files that are waiting to be deleted
|
2015-08-05 03:45:27 +00:00
|
|
|
int32_t pending_files_;
|
2018-03-22 22:42:44 +00:00
|
|
|
uint64_t bytes_max_delete_chunk_;
|
2015-08-05 03:45:27 +00:00
|
|
|
// Errors that happened in BackgroundEmptyTrash (file_path => error)
|
|
|
|
std::map<std::string, Status> bg_errors_;
|
2018-07-09 22:17:38 +00:00
|
|
|
|
|
|
|
bool num_link_error_printed_ = false;
|
2016-01-29 02:35:01 +00:00
|
|
|
// Set to true in ~DeleteScheduler() to force BackgroundEmptyTrash to stop
|
2015-08-05 03:45:27 +00: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 05:22:48 +00:00
|
|
|
InstrumentedCondVar cv_;
|
2015-08-05 03:45:27 +00:00
|
|
|
// Background thread running BackgroundEmptyTrash
|
2017-02-06 22:43:55 +00:00
|
|
|
std::unique_ptr<port::Thread> bg_thread_;
|
2015-08-05 03:45:27 +00:00
|
|
|
// Mutex to protect threads from file name conflicts
|
2016-10-18 05:22:48 +00:00
|
|
|
InstrumentedMutex file_move_mu_;
|
2016-01-29 02:35:01 +00:00
|
|
|
Logger* info_log_;
|
|
|
|
SstFileManagerImpl* sst_file_manager_;
|
2017-11-17 19:56:41 +00: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 03:45:27 +00:00
|
|
|
static const uint64_t kMicrosInSecond = 1000 * 1000LL;
|
2020-06-05 16:41:03 +00:00
|
|
|
std::shared_ptr<Statistics> stats_;
|
2015-08-05 03:45:27 +00:00
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2016-12-22 01:35:00 +00:00
|
|
|
|