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).
|
2016-01-29 03:35:01 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-12-22 02:35:00 +01:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2016-01-29 03:35:01 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "port/port.h"
|
|
|
|
|
2018-03-07 01:13:05 +01:00
|
|
|
#include "db/compaction.h"
|
2016-01-29 03:35:01 +01:00
|
|
|
#include "rocksdb/sst_file_manager.h"
|
|
|
|
#include "util/delete_scheduler.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
class Env;
|
|
|
|
class Logger;
|
|
|
|
|
|
|
|
// SstFileManager is used to track SST files in the DB and control there
|
|
|
|
// deletion rate.
|
|
|
|
// All SstFileManager public functions are thread-safe.
|
|
|
|
class SstFileManagerImpl : public SstFileManager {
|
|
|
|
public:
|
|
|
|
explicit SstFileManagerImpl(Env* env, std::shared_ptr<Logger> logger,
|
2017-11-17 20:56:41 +01:00
|
|
|
int64_t rate_bytes_per_sec,
|
2018-03-22 23:42:44 +01:00
|
|
|
double max_trash_db_ratio,
|
|
|
|
uint64_t bytes_max_delete_chunk);
|
2016-01-29 03:35:01 +01:00
|
|
|
|
|
|
|
~SstFileManagerImpl();
|
|
|
|
|
|
|
|
// DB will call OnAddFile whenever a new sst file is added.
|
|
|
|
Status OnAddFile(const std::string& file_path);
|
|
|
|
|
|
|
|
// DB will call OnDeleteFile whenever an sst file is deleted.
|
|
|
|
Status OnDeleteFile(const std::string& file_path);
|
|
|
|
|
|
|
|
// DB will call OnMoveFile whenever an sst file is move to a new path.
|
2017-06-13 01:51:37 +02:00
|
|
|
Status OnMoveFile(const std::string& old_path, const std::string& new_path,
|
|
|
|
uint64_t* file_size = nullptr);
|
2016-01-29 03:35:01 +01:00
|
|
|
|
2016-02-18 00:20:23 +01:00
|
|
|
// Update the maximum allowed space that should be used by RocksDB, if
|
|
|
|
// the total size of the SST files exceeds max_allowed_space, writes to
|
|
|
|
// RocksDB will fail.
|
|
|
|
//
|
|
|
|
// Setting max_allowed_space to 0 will disable this feature, maximum allowed
|
|
|
|
// space will be infinite (Default value).
|
|
|
|
//
|
|
|
|
// thread-safe.
|
|
|
|
void SetMaxAllowedSpaceUsage(uint64_t max_allowed_space) override;
|
|
|
|
|
2018-03-07 01:13:05 +01:00
|
|
|
void SetCompactionBufferSize(uint64_t compaction_buffer_size) override;
|
|
|
|
|
2016-02-18 00:20:23 +01:00
|
|
|
// Return true if the total size of SST files exceeded the maximum allowed
|
|
|
|
// space usage.
|
|
|
|
//
|
|
|
|
// thread-safe.
|
|
|
|
bool IsMaxAllowedSpaceReached() override;
|
|
|
|
|
2018-03-07 01:13:05 +01:00
|
|
|
bool IsMaxAllowedSpaceReachedIncludingCompactions() override;
|
|
|
|
|
|
|
|
// Returns true is there is enough (approximate) space for the specified
|
|
|
|
// compaction. Space is approximate because this function conservatively
|
|
|
|
// estimates how much space is currently being used by compactions (i.e.
|
|
|
|
// if a compaction has started, this function bumps the used space by
|
|
|
|
// the full compaction size).
|
|
|
|
bool EnoughRoomForCompaction(Compaction* c);
|
|
|
|
|
|
|
|
// Bookkeeping so total_file_sizes_ goes back to normal after compaction
|
|
|
|
// finishes
|
|
|
|
void OnCompactionCompletion(Compaction* c);
|
|
|
|
|
|
|
|
uint64_t GetCompactionsReservedSize();
|
|
|
|
|
2016-01-29 03:35:01 +01:00
|
|
|
// Return the total size of all tracked files.
|
|
|
|
uint64_t GetTotalSize() override;
|
|
|
|
|
|
|
|
// Return a map containing all tracked files and there corresponding sizes.
|
|
|
|
std::unordered_map<std::string, uint64_t> GetTrackedFiles() override;
|
|
|
|
|
|
|
|
// Return delete rate limit in bytes per second.
|
|
|
|
virtual int64_t GetDeleteRateBytesPerSecond() override;
|
|
|
|
|
2017-03-16 20:06:04 +01:00
|
|
|
// Update the delete rate limit in bytes per second.
|
|
|
|
virtual void SetDeleteRateBytesPerSecond(int64_t delete_rate) override;
|
|
|
|
|
2017-11-17 20:56:41 +01:00
|
|
|
// Return trash/DB size ratio where new files will be deleted immediately
|
|
|
|
virtual double GetMaxTrashDBRatio() override;
|
|
|
|
|
|
|
|
// Update trash/DB size ratio where new files will be deleted immediately
|
|
|
|
virtual void SetMaxTrashDBRatio(double ratio) override;
|
|
|
|
|
2017-10-27 22:25:54 +02:00
|
|
|
// Mark file as trash and schedule it's deletion.
|
2016-01-29 03:35:01 +01:00
|
|
|
virtual Status ScheduleFileDeletion(const std::string& file_path);
|
|
|
|
|
|
|
|
// Wait for all files being deleteing in the background to finish or for
|
|
|
|
// destructor to be called.
|
|
|
|
virtual void WaitForEmptyTrash();
|
|
|
|
|
2017-06-13 01:51:37 +02:00
|
|
|
DeleteScheduler* delete_scheduler() { return &delete_scheduler_; }
|
|
|
|
|
2016-01-29 03:35:01 +01:00
|
|
|
private:
|
|
|
|
// REQUIRES: mutex locked
|
|
|
|
void OnAddFileImpl(const std::string& file_path, uint64_t file_size);
|
|
|
|
// REQUIRES: mutex locked
|
|
|
|
void OnDeleteFileImpl(const std::string& file_path);
|
|
|
|
|
|
|
|
Env* env_;
|
|
|
|
std::shared_ptr<Logger> logger_;
|
|
|
|
// Mutex to protect tracked_files_, total_files_size_
|
|
|
|
port::Mutex mu_;
|
|
|
|
// The summation of the sizes of all files in tracked_files_ map
|
|
|
|
uint64_t total_files_size_;
|
2018-03-07 01:13:05 +01:00
|
|
|
// Compactions should only execute if they can leave at least
|
|
|
|
// this amount of buffer space for logs and flushes
|
|
|
|
uint64_t compaction_buffer_size_;
|
|
|
|
// Estimated size of the current ongoing compactions
|
|
|
|
uint64_t cur_compactions_reserved_size_;
|
2016-01-29 03:35:01 +01:00
|
|
|
// A map containing all tracked files and there sizes
|
|
|
|
// file_path => file_size
|
|
|
|
std::unordered_map<std::string, uint64_t> tracked_files_;
|
2016-02-18 00:20:23 +01:00
|
|
|
// The maximum allowed space (in bytes) for sst files.
|
|
|
|
uint64_t max_allowed_space_;
|
2017-10-27 22:25:54 +02:00
|
|
|
// DeleteScheduler used to throttle file deletition.
|
2016-01-29 03:35:01 +01:00
|
|
|
DeleteScheduler delete_scheduler_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
2016-12-22 02:35:00 +01:00
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|