2016-08-09 17:16:32 +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).
|
2016-08-09 17:16:32 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
|
|
|
#include <functional>
|
2020-06-23 20:41:03 +00:00
|
|
|
#include <limits>
|
2016-08-09 17:16:32 +00:00
|
|
|
#include <string>
|
2017-05-10 21:54:35 +00:00
|
|
|
#include <vector>
|
2020-06-23 20:41:03 +00:00
|
|
|
|
2016-08-09 17:16:32 +00:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/status.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "rocksdb/utilities/stackable_db.h"
|
2016-08-09 17:16:32 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
namespace blob_db {
|
|
|
|
|
2016-08-09 17:16:32 +00:00
|
|
|
// A wrapped database which puts values of KV pairs in a separate log
|
|
|
|
// and store location to the log in the underlying DB.
|
|
|
|
//
|
|
|
|
// The factory needs to be moved to include/rocksdb/utilities to allow
|
|
|
|
// users to use blob DB.
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-06-23 20:41:03 +00:00
|
|
|
constexpr uint64_t kNoExpiration = std::numeric_limits<uint64_t>::max();
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
struct BlobDBOptions {
|
2020-05-06 20:58:25 +00:00
|
|
|
// Name of the directory under the base DB where blobs will be stored. Using
|
|
|
|
// a directory where the base DB stores its SST files is not supported.
|
|
|
|
// Default is "blob_dir"
|
2017-08-01 19:48:22 +00:00
|
|
|
std::string blob_dir = "blob_dir";
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// whether the blob_dir path is relative or absolute.
|
2017-08-01 19:48:22 +00:00
|
|
|
bool path_relative = true;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2018-03-06 19:46:20 +00:00
|
|
|
// When max_db_size is reached, evict blob files to free up space
|
|
|
|
// instead of returnning NoSpace error on write. Blob files will be
|
2018-06-26 05:32:29 +00:00
|
|
|
// evicted from oldest to newest, based on file creation time.
|
2017-08-01 19:48:22 +00:00
|
|
|
bool is_fifo = false;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2018-03-06 19:46:20 +00:00
|
|
|
// Maximum size of the database (including SST files and blob files).
|
|
|
|
//
|
|
|
|
// Default: 0 (no limits)
|
|
|
|
uint64_t max_db_size = 0;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// a new bucket is opened, for ttl_range. So if ttl_range is 600seconds
|
|
|
|
// (10 minutes), and the first bucket starts at 1471542000
|
|
|
|
// then the blob buckets will be
|
|
|
|
// first bucket is 1471542000 - 1471542600
|
|
|
|
// second bucket is 1471542600 - 1471543200
|
|
|
|
// and so on
|
2017-08-04 00:46:00 +00:00
|
|
|
uint64_t ttl_range_secs = 3600;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2019-01-16 00:25:03 +00:00
|
|
|
// The smallest value to store in blob log. Values smaller than this threshold
|
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
|
|
|
// will be inlined in base DB together with the key.
|
|
|
|
uint64_t min_blob_size = 0;
|
|
|
|
|
2017-12-20 00:34:25 +00:00
|
|
|
// Allows OS to incrementally sync blob files to disk for every
|
|
|
|
// bytes_per_sync bytes written. Users shouldn't rely on it for
|
|
|
|
// persistency guarantee.
|
|
|
|
uint64_t bytes_per_sync = 512 * 1024;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// the target size of each blob file. File will become immutable
|
|
|
|
// after it exceeds that size
|
2017-08-01 19:48:22 +00:00
|
|
|
uint64_t blob_file_size = 256 * 1024 * 1024;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
// what compression to use for Blob's
|
2017-08-01 19:48:22 +00:00
|
|
|
CompressionType compression = kNoCompression;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2019-12-13 18:11:03 +00:00
|
|
|
// If enabled, BlobDB cleans up stale blobs in non-TTL files during compaction
|
|
|
|
// by rewriting the remaining live blobs to new files.
|
2017-11-02 22:47:30 +00:00
|
|
|
bool enable_garbage_collection = false;
|
|
|
|
|
2019-12-13 18:11:03 +00:00
|
|
|
// The cutoff in terms of blob file age for garbage collection. Blobs in
|
|
|
|
// the oldest N non-TTL blob files will be rewritten when encountered during
|
|
|
|
// compaction, where N = garbage_collection_cutoff * number_of_non_TTL_files.
|
|
|
|
double garbage_collection_cutoff = 0.25;
|
|
|
|
|
2017-11-02 22:47:30 +00:00
|
|
|
// Disable all background job. Used for test only.
|
2017-08-01 19:48:22 +00:00
|
|
|
bool disable_background_tasks = false;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-08-01 19:48:22 +00:00
|
|
|
void Dump(Logger* log) const;
|
2017-05-10 21:54:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class BlobDB : public StackableDB {
|
|
|
|
public:
|
2020-02-20 20:07:53 +00:00
|
|
|
using ROCKSDB_NAMESPACE::StackableDB::Put;
|
2017-09-08 17:57:12 +00:00
|
|
|
virtual Status Put(const WriteOptions& options, const Slice& key,
|
|
|
|
const Slice& value) override = 0;
|
2017-05-10 21:54:35 +00:00
|
|
|
virtual Status Put(const WriteOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
2017-09-08 17:57:12 +00:00
|
|
|
const Slice& value) override {
|
2019-12-20 02:03:24 +00:00
|
|
|
if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
|
2017-09-08 17:57:12 +00:00
|
|
|
return Status::NotSupported(
|
|
|
|
"Blob DB doesn't support non-default column family.");
|
|
|
|
}
|
|
|
|
return Put(options, key, value);
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
using ROCKSDB_NAMESPACE::StackableDB::Delete;
|
2017-09-08 17:57:12 +00:00
|
|
|
virtual Status Delete(const WriteOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key) override {
|
2019-12-20 02:03:24 +00:00
|
|
|
if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
|
2017-09-08 17:57:12 +00:00
|
|
|
return Status::NotSupported(
|
|
|
|
"Blob DB doesn't support non-default column family.");
|
|
|
|
}
|
2018-03-02 20:54:24 +00:00
|
|
|
assert(db_ != nullptr);
|
|
|
|
return db_->Delete(options, column_family, key);
|
2017-09-08 17:57:12 +00:00
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-09-08 17:57:12 +00:00
|
|
|
virtual Status PutWithTTL(const WriteOptions& options, const Slice& key,
|
|
|
|
const Slice& value, uint64_t ttl) = 0;
|
2017-05-10 21:54:35 +00:00
|
|
|
virtual Status PutWithTTL(const WriteOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
2017-08-04 00:46:00 +00:00
|
|
|
const Slice& value, uint64_t ttl) {
|
2019-12-20 02:03:24 +00:00
|
|
|
if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
|
2017-09-08 17:57:12 +00:00
|
|
|
return Status::NotSupported(
|
|
|
|
"Blob DB doesn't support non-default column family.");
|
|
|
|
}
|
|
|
|
return PutWithTTL(options, key, value, ttl);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 00:46:00 +00:00
|
|
|
// Put with expiration. Key with expiration time equal to
|
|
|
|
// std::numeric_limits<uint64_t>::max() means the key don't expire.
|
2017-09-08 17:57:12 +00:00
|
|
|
virtual Status PutUntil(const WriteOptions& options, const Slice& key,
|
|
|
|
const Slice& value, uint64_t expiration) = 0;
|
2017-05-10 21:54:35 +00:00
|
|
|
virtual Status PutUntil(const WriteOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
2017-08-04 00:46:00 +00:00
|
|
|
const Slice& value, uint64_t expiration) {
|
2019-12-20 02:03:24 +00:00
|
|
|
if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
|
2017-09-08 17:57:12 +00:00
|
|
|
return Status::NotSupported(
|
|
|
|
"Blob DB doesn't support non-default column family.");
|
|
|
|
}
|
|
|
|
return PutUntil(options, key, value, expiration);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
using ROCKSDB_NAMESPACE::StackableDB::Get;
|
2017-05-10 21:54:35 +00:00
|
|
|
virtual Status Get(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
2017-07-13 00:56:40 +00:00
|
|
|
PinnableSlice* value) override = 0;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2018-08-07 00:28:40 +00:00
|
|
|
// Get value and expiration.
|
|
|
|
virtual Status Get(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
|
|
|
PinnableSlice* value, uint64_t* expiration) = 0;
|
|
|
|
virtual Status Get(const ReadOptions& options, const Slice& key,
|
|
|
|
PinnableSlice* value, uint64_t* expiration) {
|
|
|
|
return Get(options, DefaultColumnFamily(), key, value, expiration);
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
using ROCKSDB_NAMESPACE::StackableDB::MultiGet;
|
2017-05-10 21:54:35 +00:00
|
|
|
virtual std::vector<Status> MultiGet(
|
|
|
|
const ReadOptions& options,
|
|
|
|
const std::vector<Slice>& keys,
|
|
|
|
std::vector<std::string>* values) override = 0;
|
2017-09-08 17:57:12 +00:00
|
|
|
virtual std::vector<Status> MultiGet(
|
|
|
|
const ReadOptions& options,
|
|
|
|
const std::vector<ColumnFamilyHandle*>& column_families,
|
|
|
|
const std::vector<Slice>& keys,
|
|
|
|
std::vector<std::string>* values) override {
|
|
|
|
for (auto column_family : column_families) {
|
2019-12-20 02:03:24 +00:00
|
|
|
if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
|
2017-09-08 17:57:12 +00:00
|
|
|
return std::vector<Status>(
|
|
|
|
column_families.size(),
|
|
|
|
Status::NotSupported(
|
|
|
|
"Blob DB doesn't support non-default column family."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MultiGet(options, keys, values);
|
|
|
|
}
|
Introduce a new MultiGet batching implementation (#5011)
Summary:
This PR introduces a new MultiGet() API, with the underlying implementation grouping keys based on SST file and batching lookups in a file. The reason for the new API is twofold - the definition allows callers to allocate storage for status and values on stack instead of std::vector, as well as return values as PinnableSlices in order to avoid copying, and it keeps the original MultiGet() implementation intact while we experiment with batching.
Batching is useful when there is some spatial locality to the keys being queries, as well as larger batch sizes. The main benefits are due to -
1. Fewer function calls, especially to BlockBasedTableReader::MultiGet() and FullFilterBlockReader::KeysMayMatch()
2. Bloom filter cachelines can be prefetched, hiding the cache miss latency
The next step is to optimize the binary searches in the level_storage_info, index blocks and data blocks, since we could reduce the number of key comparisons if the keys are relatively close to each other. The batching optimizations also need to be extended to other formats, such as PlainTable and filter formats. This also needs to be added to db_stress.
Benchmark results from db_bench for various batch size/locality of reference combinations are given below. Locality was simulated by offsetting the keys in a batch by a stride length. Each SST file is about 8.6MB uncompressed and key/value size is 16/100 uncompressed. To focus on the cpu benefit of batching, the runs were single threaded and bound to the same cpu to eliminate interference from other system events. The results show a 10-25% improvement in micros/op from smaller to larger batch sizes (4 - 32).
Batch Sizes
1 | 2 | 4 | 8 | 16 | 32
Random pattern (Stride length 0)
4.158 | 4.109 | 4.026 | 4.05 | 4.1 | 4.074 - Get
4.438 | 4.302 | 4.165 | 4.122 | 4.096 | 4.075 - MultiGet (no batching)
4.461 | 4.256 | 4.277 | 4.11 | 4.182 | 4.14 - MultiGet (w/ batching)
Good locality (Stride length 16)
4.048 | 3.659 | 3.248 | 2.99 | 2.84 | 2.753
4.429 | 3.728 | 3.406 | 3.053 | 2.911 | 2.781
4.452 | 3.45 | 2.833 | 2.451 | 2.233 | 2.135
Good locality (Stride length 256)
4.066 | 3.786 | 3.581 | 3.447 | 3.415 | 3.232
4.406 | 4.005 | 3.644 | 3.49 | 3.381 | 3.268
4.393 | 3.649 | 3.186 | 2.882 | 2.676 | 2.62
Medium locality (Stride length 4096)
4.012 | 3.922 | 3.768 | 3.61 | 3.582 | 3.555
4.364 | 4.057 | 3.791 | 3.65 | 3.57 | 3.465
4.479 | 3.758 | 3.316 | 3.077 | 2.959 | 2.891
dbbench command used (on a DB with 4 levels, 12 million keys)-
TEST_TMPDIR=/dev/shm numactl -C 10 ./db_bench.tmp -use_existing_db=true -benchmarks="readseq,multireadrandom" -write_buffer_size=4194304 -target_file_size_base=4194304 -max_bytes_for_level_base=16777216 -num=12000000 -reads=12000000 -duration=90 -threads=1 -compression_type=none -cache_size=4194304000 -batch_size=32 -disable_auto_compactions=true -bloom_bits=10 -cache_index_and_filter_blocks=true -pin_l0_filter_and_index_blocks_in_cache=true -multiread_batched=true -multiread_stride=4
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5011
Differential Revision: D14348703
Pulled By: anand1976
fbshipit-source-id: 774406dab3776d979c809522a67bedac6c17f84b
2019-04-11 21:24:09 +00:00
|
|
|
virtual void MultiGet(const ReadOptions& /*options*/,
|
|
|
|
ColumnFamilyHandle* /*column_family*/,
|
|
|
|
const size_t num_keys, const Slice* /*keys*/,
|
|
|
|
PinnableSlice* /*values*/, Status* statuses,
|
|
|
|
const bool /*sorted_input*/ = false) override {
|
|
|
|
for (size_t i = 0; i < num_keys; ++i) {
|
|
|
|
statuses[i] = Status::NotSupported(
|
|
|
|
"Blob DB doesn't support batched MultiGet");
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
using ROCKSDB_NAMESPACE::StackableDB::SingleDelete;
|
2017-09-08 17:57:12 +00:00
|
|
|
virtual Status SingleDelete(const WriteOptions& /*wopts*/,
|
|
|
|
ColumnFamilyHandle* /*column_family*/,
|
|
|
|
const Slice& /*key*/) override {
|
|
|
|
return Status::NotSupported("Not supported operation in blob db.");
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
using ROCKSDB_NAMESPACE::StackableDB::Merge;
|
2017-09-08 17:57:12 +00:00
|
|
|
virtual Status Merge(const WriteOptions& /*options*/,
|
|
|
|
ColumnFamilyHandle* /*column_family*/,
|
|
|
|
const Slice& /*key*/, const Slice& /*value*/) override {
|
2017-05-10 21:54:35 +00:00
|
|
|
return Status::NotSupported("Not supported operation in blob db.");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Status Write(const WriteOptions& opts,
|
|
|
|
WriteBatch* updates) override = 0;
|
2020-02-20 20:07:53 +00:00
|
|
|
using ROCKSDB_NAMESPACE::StackableDB::NewIterator;
|
2017-09-08 17:57:12 +00:00
|
|
|
virtual Iterator* NewIterator(const ReadOptions& options) override = 0;
|
|
|
|
virtual Iterator* NewIterator(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family) override {
|
2019-12-20 02:03:24 +00:00
|
|
|
if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
|
2017-09-08 17:57:12 +00:00
|
|
|
// Blob DB doesn't support non-default column family.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return NewIterator(options);
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:00:25 +00:00
|
|
|
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 = 0;
|
|
|
|
Status CompactFiles(
|
|
|
|
const CompactionOptions& compact_options,
|
|
|
|
ColumnFamilyHandle* column_family,
|
|
|
|
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 {
|
2019-12-20 02:03:24 +00:00
|
|
|
if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
|
2019-11-11 22:00:25 +00:00
|
|
|
return Status::NotSupported(
|
|
|
|
"Blob DB doesn't support non-default column family.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return CompactFiles(compact_options, input_file_names, output_level,
|
|
|
|
output_path_id, output_file_names, compaction_job_info);
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
using ROCKSDB_NAMESPACE::StackableDB::Close;
|
2018-03-06 19:46:20 +00:00
|
|
|
virtual Status Close() override = 0;
|
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
// Opening blob db.
|
2017-05-10 21:54:35 +00:00
|
|
|
static Status Open(const Options& options, const BlobDBOptions& bdb_options,
|
|
|
|
const std::string& dbname, BlobDB** blob_db);
|
|
|
|
|
|
|
|
static Status Open(const DBOptions& db_options,
|
|
|
|
const BlobDBOptions& bdb_options,
|
|
|
|
const std::string& dbname,
|
|
|
|
const std::vector<ColumnFamilyDescriptor>& column_families,
|
|
|
|
std::vector<ColumnFamilyHandle*>* handles,
|
2017-12-11 20:01:22 +00:00
|
|
|
BlobDB** blob_db);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
virtual BlobDBOptions GetBlobDBOptions() const = 0;
|
|
|
|
|
2017-12-20 00:34:25 +00:00
|
|
|
virtual Status SyncBlobFiles() = 0;
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
virtual ~BlobDB() {}
|
|
|
|
|
|
|
|
protected:
|
2017-12-11 20:01:22 +00:00
|
|
|
explicit BlobDB();
|
2017-05-10 21:54:35 +00:00
|
|
|
};
|
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
// Destroy the content of the database.
|
|
|
|
Status DestroyBlobDB(const std::string& dbname, const Options& options,
|
|
|
|
const BlobDBOptions& bdb_options);
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
} // namespace blob_db
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2017-05-10 21:54:35 +00:00
|
|
|
#endif // ROCKSDB_LITE
|