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.
139 lines
4.8 KiB
139 lines
4.8 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
|
|
||
|
#pragma once
|
||
|
|
||
8 years ago
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
10 years ago
|
#include <map>
|
||
|
#include <queue>
|
||
|
#include <string>
|
||
|
#include <thread>
|
||
|
|
||
8 years ago
|
#include "monitoring/instrumented_mutex.h"
|
||
10 years ago
|
#include "port/port.h"
|
||
|
|
||
|
#include "rocksdb/status.h"
|
||
|
|
||
|
namespace rocksdb {
|
||
|
|
||
|
class Env;
|
||
|
class Logger;
|
||
9 years ago
|
class SstFileManagerImpl;
|
||
|
|
||
|
// DeleteScheduler allows the DB to enforce a rate limit on file deletion,
|
||
7 years ago
|
// Instead of deleteing files immediately, files are marked as trash
|
||
9 years ago
|
// 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 {
|
||
10 years ago
|
public:
|
||
7 years ago
|
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
|
|
||
9 years ago
|
~DeleteScheduler();
|
||
10 years ago
|
|
||
|
// Return delete rate limit in bytes per second
|
||
8 years ago
|
int64_t GetRateBytesPerSecond() { return rate_bytes_per_sec_.load(); }
|
||
|
|
||
|
// Set delete rate limit in bytes per second
|
||
|
void SetRateBytesPerSecond(int64_t bytes_per_sec) {
|
||
7 years ago
|
rate_bytes_per_sec_.store(bytes_per_sec);
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
6 years ago
|
// Mark file as trash directory and schedule it's deletion. If force_bg is
|
||
|
// 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,
|
||
|
const bool force_bg = false);
|
||
10 years ago
|
|
||
|
// Wait for all files being deleteing in the background to finish or for
|
||
|
// destructor to be called.
|
||
9 years ago
|
void WaitForEmptyTrash();
|
||
10 years ago
|
|
||
|
// Return a map containing errors that happened in BackgroundEmptyTrash
|
||
|
// file_path => error status
|
||
|
std::map<std::string, Status> GetBackgroundErrors();
|
||
|
|
||
8 years ago
|
uint64_t GetTotalTrashSize() { return total_trash_size_.load(); }
|
||
|
|
||
7 years ago
|
// 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) {
|
||
8 years ago
|
assert(r >= 0);
|
||
7 years ago
|
max_trash_db_ratio_.store(r);
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
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);
|
||
|
|
||
10 years ago
|
private:
|
||
7 years ago
|
Status MarkAsTrash(const std::string& file_path, std::string* path_in_trash);
|
||
10 years ago
|
|
||
|
Status 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
|
|
||
|
void BackgroundEmptyTrash();
|
||
|
|
||
|
Env* env_;
|
||
7 years ago
|
// total size of trash files
|
||
8 years ago
|
std::atomic<uint64_t> total_trash_size_;
|
||
10 years ago
|
// Maximum number of bytes that should be deleted per second
|
||
8 years ago
|
std::atomic<int64_t> rate_bytes_per_sec_;
|
||
10 years ago
|
// Mutex to protect queue_, pending_files_, bg_errors_, closing_
|
||
8 years ago
|
InstrumentedMutex mu_;
|
||
7 years ago
|
|
||
|
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.
|
||
|
};
|
||
|
|
||
7 years ago
|
// Queue of trash files that need to be deleted
|
||
7 years ago
|
std::queue<FileAndDir> queue_;
|
||
7 years ago
|
// Number of trash files that are waiting to be deleted
|
||
10 years ago
|
int32_t pending_files_;
|
||
7 years ago
|
uint64_t bytes_max_delete_chunk_;
|
||
10 years ago
|
// Errors that happened in BackgroundEmptyTrash (file_path => error)
|
||
|
std::map<std::string, Status> bg_errors_;
|
||
7 years ago
|
|
||
|
bool num_link_error_printed_ = false;
|
||
9 years ago
|
// Set to true in ~DeleteScheduler() to force BackgroundEmptyTrash to stop
|
||
10 years ago
|
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
|
||
8 years ago
|
InstrumentedCondVar cv_;
|
||
10 years ago
|
// Background thread running BackgroundEmptyTrash
|
||
8 years ago
|
std::unique_ptr<port::Thread> bg_thread_;
|
||
10 years ago
|
// Mutex to protect threads from file name conflicts
|
||
8 years ago
|
InstrumentedMutex file_move_mu_;
|
||
9 years ago
|
Logger* info_log_;
|
||
|
SstFileManagerImpl* sst_file_manager_;
|
||
7 years ago
|
// 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_;
|
||
10 years ago
|
static const uint64_t kMicrosInSecond = 1000 * 1000LL;
|
||
|
};
|
||
|
|
||
|
} // namespace rocksdb
|
||
8 years ago
|
|
||
|
#endif // ROCKSDB_LITE
|