2017-05-10 21:54:35 +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).
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
2017-07-28 06:16:18 +00:00
|
|
|
#include <limits>
|
2017-05-10 21:54:35 +00:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
Blob DB: Inline small values in base DB
Summary:
Adding the `min_blob_size` option to allow storing small values in base db (in LSM tree) together with the key. The goal is to improve performance for small values, while taking advantage of blob db's low write amplification for large values.
Also adding expiration timestamp to blob index. It will be useful to evict stale blob indexes in base db by adding a compaction filter. I'll work on the compaction filter in future patches.
See blob_index.h for the new blob index format. There are 4 cases when writing a new key:
* small value w/o TTL: put in base db as normal value (i.e. ValueType::kTypeValue)
* small value w/ TTL: put (type, expiration, value) to base db.
* large value w/o TTL: write value to blob log and put (type, file, offset, size, compression) to base db.
* large value w/TTL: write value to blob log and put (type, expiration, file, offset, size, compression) to base db.
Closes https://github.com/facebook/rocksdb/pull/3066
Differential Revision: D6142115
Pulled By: yiwu-arbug
fbshipit-source-id: 9526e76e19f0839310a3f5f2a43772a4ad182cd0
2017-10-26 19:19:43 +00:00
|
|
|
#include <unordered_map>
|
2017-05-10 21:54:35 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-10-18 00:24:25 +00:00
|
|
|
#include "db/db_iter.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "rocksdb/compaction_filter.h"
|
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/listener.h"
|
|
|
|
#include "rocksdb/options.h"
|
2017-11-28 19:42:28 +00:00
|
|
|
#include "rocksdb/statistics.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "rocksdb/wal_filter.h"
|
|
|
|
#include "util/mutexlock.h"
|
|
|
|
#include "util/timer_queue.h"
|
|
|
|
#include "utilities/blob_db/blob_db.h"
|
2017-10-13 21:36:36 +00:00
|
|
|
#include "utilities/blob_db/blob_file.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "utilities/blob_db/blob_log_format.h"
|
|
|
|
#include "utilities/blob_db/blob_log_reader.h"
|
|
|
|
#include "utilities/blob_db/blob_log_writer.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
class DBImpl;
|
|
|
|
class ColumnFamilyHandle;
|
|
|
|
class ColumnFamilyData;
|
|
|
|
struct FlushJobInfo;
|
|
|
|
|
|
|
|
namespace blob_db {
|
|
|
|
|
2018-03-06 19:46:20 +00:00
|
|
|
struct BlobCompactionContext;
|
2019-12-13 18:11:03 +00:00
|
|
|
struct BlobCompactionContextGC;
|
2017-05-10 21:54:35 +00:00
|
|
|
class BlobDBImpl;
|
2018-03-06 19:46:20 +00:00
|
|
|
class BlobFile;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// Comparator to sort "TTL" aware Blob files based on the lower value of
|
|
|
|
// TTL range.
|
2018-06-26 05:32:29 +00:00
|
|
|
struct BlobFileComparatorTTL {
|
|
|
|
bool operator()(const std::shared_ptr<BlobFile>& lhs,
|
|
|
|
const std::shared_ptr<BlobFile>& rhs) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BlobFileComparator {
|
2017-05-10 21:54:35 +00:00
|
|
|
bool operator()(const std::shared_ptr<BlobFile>& lhs,
|
|
|
|
const std::shared_ptr<BlobFile>& rhs) const;
|
|
|
|
};
|
|
|
|
|
2017-07-28 06:16:18 +00:00
|
|
|
struct GCStats {
|
|
|
|
uint64_t blob_count = 0;
|
2017-11-28 19:42:28 +00:00
|
|
|
uint64_t num_keys_overwritten = 0;
|
|
|
|
uint64_t num_keys_expired = 0;
|
|
|
|
uint64_t num_keys_relocated = 0;
|
|
|
|
uint64_t bytes_overwritten = 0;
|
|
|
|
uint64_t bytes_expired = 0;
|
|
|
|
uint64_t bytes_relocated = 0;
|
2017-07-28 06:16:18 +00:00
|
|
|
};
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
/**
|
|
|
|
* The implementation class for BlobDB. This manages the value
|
|
|
|
* part in TTL aware sequentially written files. These files are
|
|
|
|
* Garbage Collected.
|
|
|
|
*/
|
|
|
|
class BlobDBImpl : public BlobDB {
|
|
|
|
friend class BlobFile;
|
|
|
|
friend class BlobDBIterator;
|
2019-11-11 22:00:25 +00:00
|
|
|
friend class BlobDBListener;
|
|
|
|
friend class BlobDBListenerGC;
|
2019-12-13 18:11:03 +00:00
|
|
|
friend class BlobIndexCompactionFilterGC;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
public:
|
2017-08-01 19:48:22 +00:00
|
|
|
// deletions check period
|
|
|
|
static constexpr uint32_t kDeleteCheckPeriodMillisecs = 2 * 1000;
|
|
|
|
|
|
|
|
// gc percentage each check period
|
|
|
|
static constexpr uint32_t kGCFilePercentage = 100;
|
|
|
|
|
|
|
|
// gc period
|
|
|
|
static constexpr uint32_t kGCCheckPeriodMillisecs = 60 * 1000;
|
|
|
|
|
|
|
|
// sanity check task
|
|
|
|
static constexpr uint32_t kSanityCheckPeriodMillisecs = 20 * 60 * 1000;
|
|
|
|
|
|
|
|
// how many random access open files can we tolerate
|
|
|
|
static constexpr uint32_t kOpenFilesTrigger = 100;
|
|
|
|
|
|
|
|
// how often to schedule reclaim open files.
|
|
|
|
static constexpr uint32_t kReclaimOpenFilesPeriodMillisecs = 1 * 1000;
|
|
|
|
|
|
|
|
// how often to schedule delete obs files periods
|
2017-08-21 01:12:38 +00:00
|
|
|
static constexpr uint32_t kDeleteObsoleteFilesPeriodMillisecs = 10 * 1000;
|
2017-08-01 19:48:22 +00:00
|
|
|
|
2018-08-21 05:38:34 +00:00
|
|
|
// how often to schedule expired files eviction.
|
|
|
|
static constexpr uint32_t kEvictExpiredFilesPeriodMillisecs = 10 * 1000;
|
2017-08-01 19:48:22 +00:00
|
|
|
|
2017-11-02 19:02:42 +00:00
|
|
|
// when should oldest file be evicted:
|
|
|
|
// on reaching 90% of blob_dir_size
|
|
|
|
static constexpr double kEvictOldestFileAtSize = 0.9;
|
|
|
|
|
2017-09-08 17:57:12 +00:00
|
|
|
using BlobDB::Put;
|
|
|
|
Status Put(const WriteOptions& options, const Slice& key,
|
|
|
|
const Slice& value) override;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-09-08 17:57:12 +00:00
|
|
|
using BlobDB::Get;
|
2017-08-21 01:12:38 +00:00
|
|
|
Status Get(const ReadOptions& read_options, ColumnFamilyHandle* column_family,
|
2017-07-13 00:56:40 +00:00
|
|
|
const Slice& key, PinnableSlice* value) override;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2018-08-07 00:28:40 +00:00
|
|
|
Status Get(const ReadOptions& read_options, ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key, PinnableSlice* value,
|
|
|
|
uint64_t* expiration) override;
|
|
|
|
|
2017-09-08 17:57:12 +00:00
|
|
|
using BlobDB::NewIterator;
|
|
|
|
virtual Iterator* NewIterator(const ReadOptions& read_options) override;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-10-18 00:24:25 +00:00
|
|
|
using BlobDB::NewIterators;
|
|
|
|
virtual Status NewIterators(
|
2018-03-05 21:08:17 +00:00
|
|
|
const ReadOptions& /*read_options*/,
|
|
|
|
const std::vector<ColumnFamilyHandle*>& /*column_families*/,
|
|
|
|
std::vector<Iterator*>* /*iterators*/) override {
|
2017-10-18 00:24:25 +00:00
|
|
|
return Status::NotSupported("Not implemented");
|
|
|
|
}
|
|
|
|
|
2017-09-08 17:57:12 +00:00
|
|
|
using BlobDB::MultiGet;
|
2017-05-10 21:54:35 +00:00
|
|
|
virtual std::vector<Status> MultiGet(
|
2017-08-21 01:12:38 +00:00
|
|
|
const ReadOptions& read_options,
|
2017-05-10 21:54:35 +00:00
|
|
|
const std::vector<Slice>& keys,
|
|
|
|
std::vector<std::string>* values) override;
|
|
|
|
|
|
|
|
virtual Status Write(const WriteOptions& opts, WriteBatch* updates) override;
|
|
|
|
|
2018-03-06 19:46:20 +00:00
|
|
|
virtual Status Close() override;
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
using BlobDB::PutWithTTL;
|
2017-09-08 17:57:12 +00:00
|
|
|
Status PutWithTTL(const WriteOptions& options, const Slice& key,
|
2017-08-04 00:46:00 +00:00
|
|
|
const Slice& value, uint64_t ttl) override;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
using BlobDB::PutUntil;
|
2017-09-08 17:57:12 +00:00
|
|
|
Status PutUntil(const WriteOptions& options, const Slice& key,
|
Blob DB: Inline small values in base DB
Summary:
Adding the `min_blob_size` option to allow storing small values in base db (in LSM tree) together with the key. The goal is to improve performance for small values, while taking advantage of blob db's low write amplification for large values.
Also adding expiration timestamp to blob index. It will be useful to evict stale blob indexes in base db by adding a compaction filter. I'll work on the compaction filter in future patches.
See blob_index.h for the new blob index format. There are 4 cases when writing a new key:
* small value w/o TTL: put in base db as normal value (i.e. ValueType::kTypeValue)
* small value w/ TTL: put (type, expiration, value) to base db.
* large value w/o TTL: write value to blob log and put (type, file, offset, size, compression) to base db.
* large value w/TTL: write value to blob log and put (type, expiration, file, offset, size, compression) to base db.
Closes https://github.com/facebook/rocksdb/pull/3066
Differential Revision: D6142115
Pulled By: yiwu-arbug
fbshipit-source-id: 9526e76e19f0839310a3f5f2a43772a4ad182cd0
2017-10-26 19:19:43 +00:00
|
|
|
const Slice& value, uint64_t expiration) override;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2019-11-11 22:00:25 +00:00
|
|
|
using BlobDB::CompactFiles;
|
|
|
|
Status CompactFiles(
|
|
|
|
const CompactionOptions& compact_options,
|
|
|
|
const std::vector<std::string>& input_file_names, const int output_level,
|
|
|
|
const int output_path_id = -1,
|
|
|
|
std::vector<std::string>* const output_file_names = nullptr,
|
|
|
|
CompactionJobInfo* compaction_job_info = nullptr) override;
|
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
BlobDBOptions GetBlobDBOptions() const override;
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
BlobDBImpl(const std::string& dbname, const BlobDBOptions& bdb_options,
|
2017-12-11 20:01:22 +00:00
|
|
|
const DBOptions& db_options,
|
|
|
|
const ColumnFamilyOptions& cf_options);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2018-08-27 17:56:28 +00:00
|
|
|
virtual Status DisableFileDeletions() override;
|
|
|
|
|
|
|
|
virtual Status EnableFileDeletions(bool force) override;
|
|
|
|
|
|
|
|
virtual Status GetLiveFiles(std::vector<std::string>&,
|
|
|
|
uint64_t* manifest_file_size,
|
|
|
|
bool flush_memtable = true) override;
|
|
|
|
virtual void GetLiveFilesMetaData(std::vector<LiveFileMetaData>*) override;
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
~BlobDBImpl();
|
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
Status Open(std::vector<ColumnFamilyHandle*>* handles);
|
|
|
|
|
2017-12-20 00:34:25 +00:00
|
|
|
Status SyncBlobFiles() override;
|
2017-12-11 20:01:22 +00:00
|
|
|
|
2019-12-13 18:11:03 +00:00
|
|
|
// Common part of the two GetCompactionContext methods below.
|
|
|
|
// REQUIRES: read lock on mutex_
|
|
|
|
void GetCompactionContextCommon(BlobCompactionContext* context) const;
|
|
|
|
|
2018-03-06 19:46:20 +00:00
|
|
|
void GetCompactionContext(BlobCompactionContext* context);
|
2019-12-13 18:11:03 +00:00
|
|
|
void GetCompactionContext(BlobCompactionContext* context,
|
|
|
|
BlobCompactionContextGC* context_gc);
|
2018-03-06 19:46:20 +00:00
|
|
|
|
2017-05-31 17:45:47 +00:00
|
|
|
#ifndef NDEBUG
|
Blob DB: Inline small values in base DB
Summary:
Adding the `min_blob_size` option to allow storing small values in base db (in LSM tree) together with the key. The goal is to improve performance for small values, while taking advantage of blob db's low write amplification for large values.
Also adding expiration timestamp to blob index. It will be useful to evict stale blob indexes in base db by adding a compaction filter. I'll work on the compaction filter in future patches.
See blob_index.h for the new blob index format. There are 4 cases when writing a new key:
* small value w/o TTL: put in base db as normal value (i.e. ValueType::kTypeValue)
* small value w/ TTL: put (type, expiration, value) to base db.
* large value w/o TTL: write value to blob log and put (type, file, offset, size, compression) to base db.
* large value w/TTL: write value to blob log and put (type, expiration, file, offset, size, compression) to base db.
Closes https://github.com/facebook/rocksdb/pull/3066
Differential Revision: D6142115
Pulled By: yiwu-arbug
fbshipit-source-id: 9526e76e19f0839310a3f5f2a43772a4ad182cd0
2017-10-26 19:19:43 +00:00
|
|
|
Status TEST_GetBlobValue(const Slice& key, const Slice& index_entry,
|
|
|
|
PinnableSlice* value);
|
|
|
|
|
2019-11-19 00:28:04 +00:00
|
|
|
void TEST_AddDummyBlobFile(uint64_t blob_file_number,
|
|
|
|
SequenceNumber immutable_sequence);
|
2019-11-11 22:00:25 +00:00
|
|
|
|
2017-07-28 06:16:18 +00:00
|
|
|
std::vector<std::shared_ptr<BlobFile>> TEST_GetBlobFiles() const;
|
|
|
|
|
2019-11-19 00:28:04 +00:00
|
|
|
std::vector<std::shared_ptr<BlobFile>> TEST_GetLiveImmNonTTLFiles() const;
|
|
|
|
|
2017-08-20 23:56:01 +00:00
|
|
|
std::vector<std::shared_ptr<BlobFile>> TEST_GetObsoleteFiles() const;
|
|
|
|
|
2017-08-25 17:40:25 +00:00
|
|
|
Status TEST_CloseBlobFile(std::shared_ptr<BlobFile>& bfile);
|
2017-07-28 06:16:18 +00:00
|
|
|
|
2018-03-06 19:46:20 +00:00
|
|
|
void TEST_ObsoleteBlobFile(std::shared_ptr<BlobFile>& blob_file,
|
|
|
|
SequenceNumber obsolete_seq = 0,
|
|
|
|
bool update_size = true);
|
|
|
|
|
2017-07-28 06:16:18 +00:00
|
|
|
Status TEST_GCFileAndUpdateLSM(std::shared_ptr<BlobFile>& bfile,
|
|
|
|
GCStats* gc_stats);
|
2017-08-20 23:56:01 +00:00
|
|
|
|
|
|
|
void TEST_RunGC();
|
2017-08-21 01:12:38 +00:00
|
|
|
|
2018-08-21 05:38:34 +00:00
|
|
|
void TEST_EvictExpiredFiles();
|
|
|
|
|
2017-08-21 01:12:38 +00:00
|
|
|
void TEST_DeleteObsoleteFiles();
|
2018-03-06 19:46:20 +00:00
|
|
|
|
|
|
|
uint64_t TEST_live_sst_size();
|
2019-03-26 17:43:22 +00:00
|
|
|
|
|
|
|
const std::string& TEST_blob_dir() const { return blob_dir_; }
|
2019-11-11 22:00:25 +00:00
|
|
|
|
|
|
|
void TEST_InitializeBlobFileToSstMapping(
|
|
|
|
const std::vector<LiveFileMetaData>& live_files);
|
|
|
|
|
|
|
|
void TEST_ProcessFlushJobInfo(const FlushJobInfo& info);
|
|
|
|
|
|
|
|
void TEST_ProcessCompactionJobInfo(const CompactionJobInfo& info);
|
|
|
|
|
2017-05-31 17:45:47 +00:00
|
|
|
#endif // !NDEBUG
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
private:
|
2017-10-18 00:24:25 +00:00
|
|
|
class GarbageCollectionWriteCallback;
|
Blob DB: Inline small values in base DB
Summary:
Adding the `min_blob_size` option to allow storing small values in base db (in LSM tree) together with the key. The goal is to improve performance for small values, while taking advantage of blob db's low write amplification for large values.
Also adding expiration timestamp to blob index. It will be useful to evict stale blob indexes in base db by adding a compaction filter. I'll work on the compaction filter in future patches.
See blob_index.h for the new blob index format. There are 4 cases when writing a new key:
* small value w/o TTL: put in base db as normal value (i.e. ValueType::kTypeValue)
* small value w/ TTL: put (type, expiration, value) to base db.
* large value w/o TTL: write value to blob log and put (type, file, offset, size, compression) to base db.
* large value w/TTL: write value to blob log and put (type, expiration, file, offset, size, compression) to base db.
Closes https://github.com/facebook/rocksdb/pull/3066
Differential Revision: D6142115
Pulled By: yiwu-arbug
fbshipit-source-id: 9526e76e19f0839310a3f5f2a43772a4ad182cd0
2017-10-26 19:19:43 +00:00
|
|
|
class BlobInserter;
|
2017-10-18 00:24:25 +00:00
|
|
|
|
2017-08-21 01:12:38 +00:00
|
|
|
// Create a snapshot if there isn't one in read options.
|
|
|
|
// Return true if a snapshot is created.
|
|
|
|
bool SetSnapshotIfNeeded(ReadOptions* read_options);
|
|
|
|
|
2017-11-28 19:42:28 +00:00
|
|
|
Status GetImpl(const ReadOptions& read_options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
2018-08-07 00:28:40 +00:00
|
|
|
PinnableSlice* value, uint64_t* expiration = nullptr);
|
2017-11-28 19:42:28 +00:00
|
|
|
|
Blob DB: Inline small values in base DB
Summary:
Adding the `min_blob_size` option to allow storing small values in base db (in LSM tree) together with the key. The goal is to improve performance for small values, while taking advantage of blob db's low write amplification for large values.
Also adding expiration timestamp to blob index. It will be useful to evict stale blob indexes in base db by adding a compaction filter. I'll work on the compaction filter in future patches.
See blob_index.h for the new blob index format. There are 4 cases when writing a new key:
* small value w/o TTL: put in base db as normal value (i.e. ValueType::kTypeValue)
* small value w/ TTL: put (type, expiration, value) to base db.
* large value w/o TTL: write value to blob log and put (type, file, offset, size, compression) to base db.
* large value w/TTL: write value to blob log and put (type, expiration, file, offset, size, compression) to base db.
Closes https://github.com/facebook/rocksdb/pull/3066
Differential Revision: D6142115
Pulled By: yiwu-arbug
fbshipit-source-id: 9526e76e19f0839310a3f5f2a43772a4ad182cd0
2017-10-26 19:19:43 +00:00
|
|
|
Status GetBlobValue(const Slice& key, const Slice& index_entry,
|
2018-08-07 00:28:40 +00:00
|
|
|
PinnableSlice* value, uint64_t* expiration = nullptr);
|
Blob DB: Inline small values in base DB
Summary:
Adding the `min_blob_size` option to allow storing small values in base db (in LSM tree) together with the key. The goal is to improve performance for small values, while taking advantage of blob db's low write amplification for large values.
Also adding expiration timestamp to blob index. It will be useful to evict stale blob indexes in base db by adding a compaction filter. I'll work on the compaction filter in future patches.
See blob_index.h for the new blob index format. There are 4 cases when writing a new key:
* small value w/o TTL: put in base db as normal value (i.e. ValueType::kTypeValue)
* small value w/ TTL: put (type, expiration, value) to base db.
* large value w/o TTL: write value to blob log and put (type, file, offset, size, compression) to base db.
* large value w/TTL: write value to blob log and put (type, expiration, file, offset, size, compression) to base db.
Closes https://github.com/facebook/rocksdb/pull/3066
Differential Revision: D6142115
Pulled By: yiwu-arbug
fbshipit-source-id: 9526e76e19f0839310a3f5f2a43772a4ad182cd0
2017-10-26 19:19:43 +00:00
|
|
|
|
2019-11-27 00:42:44 +00:00
|
|
|
Status GetRawBlobFromFile(const Slice& key, uint64_t file_number,
|
|
|
|
uint64_t offset, uint64_t size,
|
|
|
|
PinnableSlice* value,
|
|
|
|
CompressionType* compression_type);
|
|
|
|
|
2017-06-14 20:44:36 +00:00
|
|
|
Slice GetCompressedSlice(const Slice& raw,
|
|
|
|
std::string* compression_output) const;
|
|
|
|
|
2017-08-25 17:40:25 +00:00
|
|
|
// Close a file by appending a footer, and removes file from open files list.
|
2019-11-19 00:28:04 +00:00
|
|
|
// REQUIRES: lock held on write_mutex_, write lock held on both the db mutex_
|
2019-12-13 18:11:03 +00:00
|
|
|
// and the blob file's mutex_. If called on a blob file which is visible only
|
|
|
|
// to a single thread (like in the case of new files written during GC), the
|
|
|
|
// locks on write_mutex_ and the blob file's mutex_ can be avoided.
|
2019-11-19 00:28:04 +00:00
|
|
|
Status CloseBlobFile(std::shared_ptr<BlobFile> bfile);
|
2017-08-25 17:40:25 +00:00
|
|
|
|
|
|
|
// Close a file if its size exceeds blob_file_size
|
2019-11-19 00:28:04 +00:00
|
|
|
// REQUIRES: lock held on write_mutex_.
|
2017-08-25 17:40:25 +00:00
|
|
|
Status CloseBlobFileIfNeeded(std::shared_ptr<BlobFile>& bfile);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2018-03-06 19:46:20 +00:00
|
|
|
// Mark file as obsolete and move the file to obsolete file list.
|
|
|
|
//
|
|
|
|
// REQUIRED: hold write lock of mutex_ or during DB open.
|
|
|
|
void ObsoleteBlobFile(std::shared_ptr<BlobFile> blob_file,
|
|
|
|
SequenceNumber obsolete_seq, bool update_size);
|
|
|
|
|
Blob DB: Inline small values in base DB
Summary:
Adding the `min_blob_size` option to allow storing small values in base db (in LSM tree) together with the key. The goal is to improve performance for small values, while taking advantage of blob db's low write amplification for large values.
Also adding expiration timestamp to blob index. It will be useful to evict stale blob indexes in base db by adding a compaction filter. I'll work on the compaction filter in future patches.
See blob_index.h for the new blob index format. There are 4 cases when writing a new key:
* small value w/o TTL: put in base db as normal value (i.e. ValueType::kTypeValue)
* small value w/ TTL: put (type, expiration, value) to base db.
* large value w/o TTL: write value to blob log and put (type, file, offset, size, compression) to base db.
* large value w/TTL: write value to blob log and put (type, expiration, file, offset, size, compression) to base db.
Closes https://github.com/facebook/rocksdb/pull/3066
Differential Revision: D6142115
Pulled By: yiwu-arbug
fbshipit-source-id: 9526e76e19f0839310a3f5f2a43772a4ad182cd0
2017-10-26 19:19:43 +00:00
|
|
|
Status PutBlobValue(const WriteOptions& options, const Slice& key,
|
|
|
|
const Slice& value, uint64_t expiration,
|
2017-12-15 21:18:32 +00:00
|
|
|
WriteBatch* batch);
|
Blob DB: Inline small values in base DB
Summary:
Adding the `min_blob_size` option to allow storing small values in base db (in LSM tree) together with the key. The goal is to improve performance for small values, while taking advantage of blob db's low write amplification for large values.
Also adding expiration timestamp to blob index. It will be useful to evict stale blob indexes in base db by adding a compaction filter. I'll work on the compaction filter in future patches.
See blob_index.h for the new blob index format. There are 4 cases when writing a new key:
* small value w/o TTL: put in base db as normal value (i.e. ValueType::kTypeValue)
* small value w/ TTL: put (type, expiration, value) to base db.
* large value w/o TTL: write value to blob log and put (type, file, offset, size, compression) to base db.
* large value w/TTL: write value to blob log and put (type, expiration, file, offset, size, compression) to base db.
Closes https://github.com/facebook/rocksdb/pull/3066
Differential Revision: D6142115
Pulled By: yiwu-arbug
fbshipit-source-id: 9526e76e19f0839310a3f5f2a43772a4ad182cd0
2017-10-26 19:19:43 +00:00
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
Status AppendBlob(const std::shared_ptr<BlobFile>& bfile,
|
|
|
|
const std::string& headerbuf, const Slice& key,
|
Blob DB: Inline small values in base DB
Summary:
Adding the `min_blob_size` option to allow storing small values in base db (in LSM tree) together with the key. The goal is to improve performance for small values, while taking advantage of blob db's low write amplification for large values.
Also adding expiration timestamp to blob index. It will be useful to evict stale blob indexes in base db by adding a compaction filter. I'll work on the compaction filter in future patches.
See blob_index.h for the new blob index format. There are 4 cases when writing a new key:
* small value w/o TTL: put in base db as normal value (i.e. ValueType::kTypeValue)
* small value w/ TTL: put (type, expiration, value) to base db.
* large value w/o TTL: write value to blob log and put (type, file, offset, size, compression) to base db.
* large value w/TTL: write value to blob log and put (type, expiration, file, offset, size, compression) to base db.
Closes https://github.com/facebook/rocksdb/pull/3066
Differential Revision: D6142115
Pulled By: yiwu-arbug
fbshipit-source-id: 9526e76e19f0839310a3f5f2a43772a4ad182cd0
2017-10-26 19:19:43 +00:00
|
|
|
const Slice& value, uint64_t expiration,
|
|
|
|
std::string* index_entry);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2019-11-26 21:16:39 +00:00
|
|
|
// Create a new blob file and associated writer.
|
|
|
|
Status CreateBlobFileAndWriter(bool has_ttl,
|
|
|
|
const ExpirationRange& expiration_range,
|
|
|
|
const std::string& reason,
|
|
|
|
std::shared_ptr<BlobFile>* blob_file,
|
|
|
|
std::shared_ptr<Writer>* writer);
|
|
|
|
|
|
|
|
// Get the open non-TTL blob log file, or create a new one if no such file
|
|
|
|
// exists.
|
|
|
|
Status SelectBlobFile(std::shared_ptr<BlobFile>* blob_file);
|
|
|
|
|
|
|
|
// Get the open TTL blob log file for a certain expiration, or create a new
|
|
|
|
// one if no such file exists.
|
2018-10-23 22:01:36 +00:00
|
|
|
Status SelectBlobFileTTL(uint64_t expiration,
|
|
|
|
std::shared_ptr<BlobFile>* blob_file);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-08-04 00:46:00 +00:00
|
|
|
std::shared_ptr<BlobFile> FindBlobFileLocked(uint64_t expiration) const;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// periodic sanity check. Bunch of checks
|
|
|
|
std::pair<bool, int64_t> SanityCheck(bool aborted);
|
|
|
|
|
|
|
|
// delete files which have been garbage collected and marked
|
|
|
|
// obsolete. Check whether any snapshots exist which refer to
|
|
|
|
// the same
|
2017-08-21 01:12:38 +00:00
|
|
|
std::pair<bool, int64_t> DeleteObsoleteFiles(bool aborted);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// Major task to garbage collect expired and deleted blobs
|
|
|
|
std::pair<bool, int64_t> RunGC(bool aborted);
|
|
|
|
|
|
|
|
// periodically check if open blob files and their TTL's has expired
|
|
|
|
// if expired, close the sequential writer and make the file immutable
|
2018-08-21 05:38:34 +00:00
|
|
|
std::pair<bool, int64_t> EvictExpiredFiles(bool aborted);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// if the number of open files, approaches ULIMIT's this
|
|
|
|
// task will close random readers, which are kept around for
|
|
|
|
// efficiency
|
|
|
|
std::pair<bool, int64_t> ReclaimOpenFiles(bool aborted);
|
|
|
|
|
|
|
|
std::pair<bool, int64_t> RemoveTimerQ(TimerQueue* tq, bool aborted);
|
|
|
|
|
|
|
|
// Adds the background tasks to the timer queue
|
|
|
|
void StartBackgroundTasks();
|
|
|
|
|
|
|
|
// add a new Blob File
|
2019-11-26 21:16:39 +00:00
|
|
|
std::shared_ptr<BlobFile> NewBlobFile(bool has_ttl,
|
|
|
|
const ExpirationRange& expiration_range,
|
|
|
|
const std::string& reason);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2019-12-13 18:11:03 +00:00
|
|
|
// Register a new blob file.
|
|
|
|
// REQUIRES: write lock on mutex_.
|
|
|
|
void RegisterBlobFile(std::shared_ptr<BlobFile> blob_file);
|
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
// collect all the blob log files from the blob directory
|
|
|
|
Status GetAllBlobFiles(std::set<uint64_t>* file_numbers);
|
|
|
|
|
|
|
|
// Open all blob files found in blob_dir.
|
|
|
|
Status OpenAllBlobFiles();
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2019-11-11 22:00:25 +00:00
|
|
|
// Link an SST to a blob file. Comes in locking and non-locking varieties
|
|
|
|
// (the latter is used during Open).
|
|
|
|
template <typename Linker>
|
|
|
|
void LinkSstToBlobFileImpl(uint64_t sst_file_number,
|
|
|
|
uint64_t blob_file_number, Linker linker);
|
|
|
|
|
|
|
|
void LinkSstToBlobFile(uint64_t sst_file_number, uint64_t blob_file_number);
|
|
|
|
|
|
|
|
void LinkSstToBlobFileNoLock(uint64_t sst_file_number,
|
|
|
|
uint64_t blob_file_number);
|
|
|
|
|
|
|
|
// Unlink an SST from a blob file.
|
|
|
|
void UnlinkSstFromBlobFile(uint64_t sst_file_number,
|
|
|
|
uint64_t blob_file_number);
|
|
|
|
|
|
|
|
// Initialize the mapping between blob files and SSTs during Open.
|
|
|
|
void InitializeBlobFileToSstMapping(
|
|
|
|
const std::vector<LiveFileMetaData>& live_files);
|
|
|
|
|
2019-11-19 00:28:04 +00:00
|
|
|
// Update the mapping between blob files and SSTs after a flush and mark
|
|
|
|
// any unneeded blob files obsolete.
|
2019-11-11 22:00:25 +00:00
|
|
|
void ProcessFlushJobInfo(const FlushJobInfo& info);
|
|
|
|
|
2019-11-19 00:28:04 +00:00
|
|
|
// Update the mapping between blob files and SSTs after a compaction and
|
|
|
|
// mark any unneeded blob files obsolete.
|
2019-11-11 22:00:25 +00:00
|
|
|
void ProcessCompactionJobInfo(const CompactionJobInfo& info);
|
|
|
|
|
2019-11-19 00:28:04 +00:00
|
|
|
// Mark an immutable non-TTL blob file obsolete assuming it has no more SSTs
|
|
|
|
// linked to it, and all memtables from before the blob file became immutable
|
|
|
|
// have been flushed. Note: should only be called if the condition holds for
|
|
|
|
// all lower-numbered non-TTL blob files as well.
|
|
|
|
bool MarkBlobFileObsoleteIfNeeded(const std::shared_ptr<BlobFile>& blob_file,
|
|
|
|
SequenceNumber obsolete_seq);
|
|
|
|
|
|
|
|
// Mark all immutable non-TTL blob files that aren't needed by any SSTs as
|
|
|
|
// obsolete. Comes in two varieties; the version used during Open need not
|
|
|
|
// worry about locking or snapshots.
|
|
|
|
template <class Functor>
|
|
|
|
void MarkUnreferencedBlobFilesObsoleteImpl(Functor mark_if_needed);
|
|
|
|
|
|
|
|
void MarkUnreferencedBlobFilesObsolete();
|
|
|
|
void MarkUnreferencedBlobFilesObsoleteDuringOpen();
|
|
|
|
|
2019-11-11 22:00:25 +00:00
|
|
|
void UpdateLiveSSTSize();
|
|
|
|
|
2018-09-20 23:50:07 +00:00
|
|
|
Status GetBlobFileReader(const std::shared_ptr<BlobFile>& blob_file,
|
|
|
|
std::shared_ptr<RandomAccessFileReader>* reader);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// hold write mutex on file and call.
|
|
|
|
// Close the above Random Access reader
|
|
|
|
void CloseRandomAccessLocked(const std::shared_ptr<BlobFile>& bfile);
|
|
|
|
|
|
|
|
// hold write mutex on file and call
|
|
|
|
// creates a sequential (append) writer for this blobfile
|
|
|
|
Status CreateWriterLocked(const std::shared_ptr<BlobFile>& bfile);
|
|
|
|
|
|
|
|
// returns a Writer object for the file. If writer is not
|
|
|
|
// already present, creates one. Needs Write Mutex to be held
|
2018-10-23 22:01:36 +00:00
|
|
|
Status CheckOrCreateWriterLocked(const std::shared_ptr<BlobFile>& blob_file,
|
|
|
|
std::shared_ptr<Writer>* writer);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// Iterate through keys and values on Blob and write into
|
|
|
|
// separate file the remaining blobs and delete/update pointers
|
|
|
|
// in LSM atomically
|
|
|
|
Status GCFileAndUpdateLSM(const std::shared_ptr<BlobFile>& bfptr,
|
|
|
|
GCStats* gcstats);
|
|
|
|
|
|
|
|
// checks if there is no snapshot which is referencing the
|
|
|
|
// blobs
|
2017-11-02 22:50:30 +00:00
|
|
|
bool VisibleToActiveSnapshot(const std::shared_ptr<BlobFile>& file);
|
2017-05-10 21:54:35 +00:00
|
|
|
bool FileDeleteOk_SnapshotCheckLocked(const std::shared_ptr<BlobFile>& bfile);
|
|
|
|
|
2018-06-26 05:32:29 +00:00
|
|
|
void CopyBlobFiles(std::vector<std::shared_ptr<BlobFile>>* bfiles_copy);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-07-28 06:16:18 +00:00
|
|
|
uint64_t EpochNow() { return env_->NowMicros() / 1000000; }
|
|
|
|
|
2018-03-06 19:46:20 +00:00
|
|
|
// Check if inserting a new blob will make DB grow out of space.
|
|
|
|
// If is_fifo = true, FIFO eviction will be triggered to make room for the
|
|
|
|
// new blob. If force_evict = true, FIFO eviction will evict blob files
|
|
|
|
// even eviction will not make enough room for the new blob.
|
|
|
|
Status CheckSizeAndEvictBlobFiles(uint64_t blob_size,
|
|
|
|
bool force_evict = false);
|
2017-11-02 19:02:42 +00:00
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
// name of the database directory
|
|
|
|
std::string dbname_;
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
// the base DB
|
|
|
|
DBImpl* db_impl_;
|
2017-07-28 06:16:18 +00:00
|
|
|
Env* env_;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// the options that govern the behavior of Blob Storage
|
2017-08-01 19:48:22 +00:00
|
|
|
BlobDBOptions bdb_options_;
|
2017-05-10 21:54:35 +00:00
|
|
|
DBOptions db_options_;
|
2017-12-11 20:01:22 +00:00
|
|
|
ColumnFamilyOptions cf_options_;
|
2017-05-10 21:54:35 +00:00
|
|
|
EnvOptions env_options_;
|
|
|
|
|
2018-11-09 19:17:34 +00:00
|
|
|
// Raw pointer of statistic. db_options_ has a std::shared_ptr to hold
|
|
|
|
// ownership.
|
2017-11-28 19:42:28 +00:00
|
|
|
Statistics* statistics_;
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
// by default this is "blob_dir" under dbname_
|
|
|
|
// but can be configured
|
|
|
|
std::string blob_dir_;
|
|
|
|
|
|
|
|
// pointer to directory
|
|
|
|
std::unique_ptr<Directory> dir_ent_;
|
|
|
|
|
|
|
|
// Read Write Mutex, which protects all the data structures
|
|
|
|
// HEAVILY TRAFFICKED
|
2017-08-20 23:56:01 +00:00
|
|
|
mutable port::RWMutex mutex_;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-08-03 22:07:01 +00:00
|
|
|
// Writers has to hold write_mutex_ before writing.
|
|
|
|
mutable port::Mutex write_mutex_;
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
// counter for blob file number
|
|
|
|
std::atomic<uint64_t> next_file_number_;
|
|
|
|
|
|
|
|
// entire metadata of all the BLOB files memory
|
2017-08-20 23:56:01 +00:00
|
|
|
std::map<uint64_t, std::shared_ptr<BlobFile>> blob_files_;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2019-11-19 00:28:04 +00:00
|
|
|
// All live immutable non-TTL blob files.
|
|
|
|
std::map<uint64_t, std::shared_ptr<BlobFile>> live_imm_non_ttl_blob_files_;
|
|
|
|
|
|
|
|
// The largest sequence number that has been flushed.
|
|
|
|
SequenceNumber flush_sequence_;
|
|
|
|
|
2017-10-31 23:33:55 +00:00
|
|
|
// opened non-TTL blob file.
|
|
|
|
std::shared_ptr<BlobFile> open_non_ttl_file_;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// all the blob files which are currently being appended to based
|
|
|
|
// on variety of incoming TTL's
|
2018-06-26 05:32:29 +00:00
|
|
|
std::set<std::shared_ptr<BlobFile>, BlobFileComparatorTTL> open_ttl_files_;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2018-03-06 19:46:20 +00:00
|
|
|
// Flag to check whether Close() has been called on this DB
|
|
|
|
bool closed_;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// timer based queue to execute tasks
|
|
|
|
TimerQueue tqueue_;
|
|
|
|
|
|
|
|
// number of files opened for random access/GET
|
|
|
|
// counter is used to monitor and close excess RA files.
|
|
|
|
std::atomic<uint32_t> open_file_count_;
|
|
|
|
|
2018-03-06 19:46:20 +00:00
|
|
|
// Total size of all live blob files (i.e. exclude obsolete files).
|
|
|
|
std::atomic<uint64_t> total_blob_size_;
|
|
|
|
|
|
|
|
// total size of SST files.
|
|
|
|
std::atomic<uint64_t> live_sst_size_;
|
|
|
|
|
|
|
|
// Latest FIFO eviction timestamp
|
|
|
|
//
|
|
|
|
// REQUIRES: access with metex_ lock held.
|
|
|
|
uint64_t fifo_eviction_seq_;
|
|
|
|
|
|
|
|
// The expiration up to which latest FIFO eviction evicts.
|
|
|
|
//
|
|
|
|
// REQUIRES: access with metex_ lock held.
|
|
|
|
uint64_t evict_expiration_up_to_;
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
std::list<std::shared_ptr<BlobFile>> obsolete_files_;
|
|
|
|
|
2018-08-27 17:56:28 +00:00
|
|
|
// DeleteObsoleteFiles, DiableFileDeletions and EnableFileDeletions block
|
|
|
|
// on the mutex to avoid contention.
|
|
|
|
//
|
|
|
|
// While DeleteObsoleteFiles hold both mutex_ and delete_file_mutex_, note
|
|
|
|
// the difference. mutex_ only needs to be held when access the
|
|
|
|
// data-structure, and delete_file_mutex_ needs to be held the whole time
|
|
|
|
// during DeleteObsoleteFiles to avoid being run simultaneously with
|
|
|
|
// DisableFileDeletions.
|
|
|
|
//
|
|
|
|
// If both of mutex_ and delete_file_mutex_ needs to be held, it is adviced
|
|
|
|
// to hold delete_file_mutex_ first to avoid deadlock.
|
|
|
|
mutable port::Mutex delete_file_mutex_;
|
|
|
|
|
|
|
|
// Each call of DisableFileDeletions will increase disable_file_deletion_
|
|
|
|
// by 1. EnableFileDeletions will either decrease the count by 1 or reset
|
|
|
|
// it to zeor, depending on the force flag.
|
|
|
|
//
|
|
|
|
// REQUIRES: access with delete_file_mutex_ held.
|
|
|
|
int disable_file_deletions_ = 0;
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
uint32_t debug_level_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace blob_db
|
|
|
|
} // namespace rocksdb
|
|
|
|
#endif // ROCKSDB_LITE
|