2020-05-15 08:23:32 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// 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).
|
|
|
|
|
|
|
|
#include "table/block_based/block_based_table_reader.h"
|
|
|
|
|
2022-04-06 19:33:00 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "cache/cache_reservation_manager.h"
|
|
|
|
#include "db/db_test_util.h"
|
2020-05-15 08:23:32 +02:00
|
|
|
#include "db/table_properties_collector.h"
|
2020-07-09 23:33:42 +02:00
|
|
|
#include "file/file_util.h"
|
2020-05-15 08:23:32 +02:00
|
|
|
#include "options/options_helper.h"
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "port/stack_trace.h"
|
2022-04-06 19:33:00 +02:00
|
|
|
#include "rocksdb/compression_type.h"
|
2021-09-29 13:01:57 +02:00
|
|
|
#include "rocksdb/db.h"
|
2020-07-09 23:33:42 +02:00
|
|
|
#include "rocksdb/file_system.h"
|
2020-05-15 08:23:32 +02:00
|
|
|
#include "table/block_based/block_based_table_builder.h"
|
|
|
|
#include "table/block_based/block_based_table_factory.h"
|
2020-07-09 23:33:42 +02:00
|
|
|
#include "table/block_based/partitioned_index_iterator.h"
|
2020-05-15 08:23:32 +02:00
|
|
|
#include "table/format.h"
|
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#include "test_util/testutil.h"
|
2020-07-09 23:33:42 +02:00
|
|
|
#include "util/random.h"
|
2020-05-15 08:23:32 +02:00
|
|
|
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
|
2022-04-06 19:33:00 +02:00
|
|
|
class BlockBasedTableReaderBaseTest : public testing::Test {
|
2020-05-15 08:23:32 +02:00
|
|
|
protected:
|
2022-04-06 19:33:00 +02:00
|
|
|
// Prepare key-value pairs to occupy multiple blocks.
|
|
|
|
// Each value is 256B, every 16 pairs constitute 1 block.
|
|
|
|
// If mixed_with_human_readable_string_value == true,
|
|
|
|
// then adjacent blocks contain values with different compression
|
|
|
|
// complexity: human readable strings are easier to compress than random
|
|
|
|
// strings.
|
|
|
|
static std::map<std::string, std::string> GenerateKVMap(
|
|
|
|
int num_block = 100,
|
|
|
|
bool mixed_with_human_readable_string_value = false) {
|
|
|
|
std::map<std::string, std::string> kv;
|
2020-05-15 08:23:32 +02:00
|
|
|
|
2022-04-06 19:33:00 +02:00
|
|
|
Random rnd(101);
|
|
|
|
uint32_t key = 0;
|
|
|
|
for (int block = 0; block < num_block; block++) {
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
char k[9] = {0};
|
|
|
|
// Internal key is constructed directly from this key,
|
|
|
|
// and internal key size is required to be >= 8 bytes,
|
|
|
|
// so use %08u as the format string.
|
|
|
|
sprintf(k, "%08u", key);
|
|
|
|
std::string v;
|
|
|
|
if (mixed_with_human_readable_string_value) {
|
|
|
|
v = (block % 2) ? rnd.HumanReadableString(256)
|
|
|
|
: rnd.RandomString(256);
|
|
|
|
} else {
|
|
|
|
v = rnd.RandomString(256);
|
|
|
|
}
|
|
|
|
kv[std::string(k)] = v;
|
|
|
|
key++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kv;
|
|
|
|
}
|
2020-05-15 08:23:32 +02:00
|
|
|
|
2022-04-06 19:33:00 +02:00
|
|
|
void SetUp() override {
|
2020-07-09 23:33:42 +02:00
|
|
|
SetupSyncPointsToMockDirectIO();
|
2020-05-15 08:23:32 +02:00
|
|
|
test_dir_ = test::PerThreadDBPath("block_based_table_reader_test");
|
|
|
|
env_ = Env::Default();
|
|
|
|
fs_ = FileSystem::Default();
|
|
|
|
ASSERT_OK(fs_->CreateDir(test_dir_, IOOptions(), nullptr));
|
2022-04-06 19:33:00 +02:00
|
|
|
ConfigureTableFactory();
|
2020-05-15 08:23:32 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 19:33:00 +02:00
|
|
|
virtual void ConfigureTableFactory() = 0;
|
|
|
|
|
2020-07-09 23:33:42 +02:00
|
|
|
void TearDown() override { EXPECT_OK(DestroyDir(env_, test_dir_)); }
|
2020-05-15 08:23:32 +02:00
|
|
|
|
|
|
|
// Creates a table with the specificied key value pairs (kv).
|
|
|
|
void CreateTable(const std::string& table_name,
|
|
|
|
const CompressionType& compression_type,
|
|
|
|
const std::map<std::string, std::string>& kv) {
|
|
|
|
std::unique_ptr<WritableFileWriter> writer;
|
|
|
|
NewFileWriter(table_name, &writer);
|
|
|
|
|
|
|
|
// Create table builder.
|
2022-04-06 19:33:00 +02:00
|
|
|
ImmutableOptions ioptions(options_);
|
|
|
|
InternalKeyComparator comparator(options_.comparator);
|
2020-05-15 08:23:32 +02:00
|
|
|
ColumnFamilyOptions cf_options;
|
|
|
|
MutableCFOptions moptions(cf_options);
|
2021-05-18 03:27:42 +02:00
|
|
|
IntTblPropCollectorFactories factories;
|
2022-04-06 19:33:00 +02:00
|
|
|
std::unique_ptr<TableBuilder> table_builder(
|
|
|
|
options_.table_factory->NewTableBuilder(
|
|
|
|
TableBuilderOptions(ioptions, moptions, comparator, &factories,
|
|
|
|
compression_type, CompressionOptions(),
|
|
|
|
0 /* column_family_id */,
|
|
|
|
kDefaultColumnFamilyName, -1 /* level */),
|
|
|
|
writer.get()));
|
2020-05-15 08:23:32 +02:00
|
|
|
|
|
|
|
// Build table.
|
|
|
|
for (auto it = kv.begin(); it != kv.end(); it++) {
|
|
|
|
std::string k = ToInternalKey(it->first);
|
|
|
|
std::string v = it->second;
|
|
|
|
table_builder->Add(k, v);
|
|
|
|
}
|
|
|
|
ASSERT_OK(table_builder->Finish());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewBlockBasedTableReader(const FileOptions& foptions,
|
2021-05-05 22:59:21 +02:00
|
|
|
const ImmutableOptions& ioptions,
|
2020-05-15 08:23:32 +02:00
|
|
|
const InternalKeyComparator& comparator,
|
|
|
|
const std::string& table_name,
|
2022-04-06 19:33:00 +02:00
|
|
|
std::unique_ptr<BlockBasedTable>* table,
|
|
|
|
bool prefetch_index_and_filter_in_cache = true,
|
|
|
|
Status* status = nullptr) {
|
|
|
|
const MutableCFOptions moptions(options_);
|
|
|
|
TableReaderOptions table_reader_options = TableReaderOptions(
|
|
|
|
ioptions, moptions.prefix_extractor, EnvOptions(), comparator);
|
|
|
|
|
2020-05-15 08:23:32 +02:00
|
|
|
std::unique_ptr<RandomAccessFileReader> file;
|
|
|
|
NewFileReader(table_name, foptions, &file);
|
|
|
|
|
|
|
|
uint64_t file_size = 0;
|
|
|
|
ASSERT_OK(env_->GetFileSize(Path(table_name), &file_size));
|
|
|
|
|
2022-04-06 19:33:00 +02:00
|
|
|
std::unique_ptr<TableReader> general_table;
|
|
|
|
Status s = options_.table_factory->NewTableReader(
|
|
|
|
ReadOptions(), table_reader_options, std::move(file), file_size,
|
|
|
|
&general_table, prefetch_index_and_filter_in_cache);
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
table->reset(reinterpret_cast<BlockBasedTable*>(general_table.release()));
|
|
|
|
}
|
2020-05-15 08:23:32 +02:00
|
|
|
|
2022-04-06 19:33:00 +02:00
|
|
|
if (status) {
|
|
|
|
*status = s;
|
|
|
|
}
|
2020-05-15 08:23:32 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 20:06:26 +02:00
|
|
|
std::string Path(const std::string& fname) { return test_dir_ + "/" + fname; }
|
|
|
|
|
2020-05-15 08:23:32 +02:00
|
|
|
std::string test_dir_;
|
|
|
|
Env* env_;
|
|
|
|
std::shared_ptr<FileSystem> fs_;
|
2022-04-06 19:33:00 +02:00
|
|
|
Options options_;
|
2020-05-15 08:23:32 +02:00
|
|
|
|
2022-04-06 19:33:00 +02:00
|
|
|
private:
|
2020-05-15 08:23:32 +02:00
|
|
|
void WriteToFile(const std::string& content, const std::string& filename) {
|
|
|
|
std::unique_ptr<FSWritableFile> f;
|
|
|
|
ASSERT_OK(fs_->NewWritableFile(Path(filename), FileOptions(), &f, nullptr));
|
|
|
|
ASSERT_OK(f->Append(content, IOOptions(), nullptr));
|
|
|
|
ASSERT_OK(f->Close(IOOptions(), nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileWriter(const std::string& filename,
|
|
|
|
std::unique_ptr<WritableFileWriter>* writer) {
|
|
|
|
std::string path = Path(filename);
|
|
|
|
EnvOptions env_options;
|
|
|
|
FileOptions foptions;
|
|
|
|
std::unique_ptr<FSWritableFile> file;
|
|
|
|
ASSERT_OK(fs_->NewWritableFile(path, foptions, &file, nullptr));
|
|
|
|
writer->reset(new WritableFileWriter(std::move(file), path, env_options));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileReader(const std::string& filename, const FileOptions& opt,
|
|
|
|
std::unique_ptr<RandomAccessFileReader>* reader) {
|
|
|
|
std::string path = Path(filename);
|
|
|
|
std::unique_ptr<FSRandomAccessFile> f;
|
|
|
|
ASSERT_OK(fs_->NewRandomAccessFile(path, opt, &f, nullptr));
|
2021-03-15 12:32:24 +01:00
|
|
|
reader->reset(new RandomAccessFileReader(std::move(f), path,
|
|
|
|
env_->GetSystemClock().get()));
|
2020-05-15 08:23:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToInternalKey(const std::string& key) {
|
|
|
|
InternalKey internal_key(key, 0, ValueType::kTypeValue);
|
|
|
|
return internal_key.Encode().ToString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-06 19:33:00 +02:00
|
|
|
class BlockBasedTableReaderTest
|
|
|
|
: public BlockBasedTableReaderBaseTest,
|
|
|
|
public testing::WithParamInterface<std::tuple<
|
|
|
|
CompressionType, bool, BlockBasedTableOptions::IndexType, bool>> {
|
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
|
|
|
compression_type_ = std::get<0>(GetParam());
|
|
|
|
use_direct_reads_ = std::get<1>(GetParam());
|
|
|
|
BlockBasedTableReaderBaseTest::SetUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureTableFactory() override {
|
|
|
|
BlockBasedTableOptions opts;
|
|
|
|
opts.index_type = std::get<2>(GetParam());
|
|
|
|
opts.no_block_cache = std::get<3>(GetParam());
|
|
|
|
options_.table_factory.reset(
|
|
|
|
static_cast<BlockBasedTableFactory*>(NewBlockBasedTableFactory(opts)));
|
|
|
|
}
|
|
|
|
|
|
|
|
CompressionType compression_type_;
|
|
|
|
bool use_direct_reads_;
|
|
|
|
};
|
|
|
|
|
2020-05-15 08:23:32 +02:00
|
|
|
// Tests MultiGet in both direct IO and non-direct IO mode.
|
|
|
|
// The keys should be in cache after MultiGet.
|
|
|
|
TEST_P(BlockBasedTableReaderTest, MultiGet) {
|
2022-04-06 19:33:00 +02:00
|
|
|
std::map<std::string, std::string> kv =
|
|
|
|
BlockBasedTableReaderBaseTest::GenerateKVMap(
|
|
|
|
100 /* num_block */,
|
|
|
|
true /* mixed_with_human_readable_string_value */);
|
2020-05-15 08:23:32 +02:00
|
|
|
|
|
|
|
// Prepare keys, values, and statuses for MultiGet.
|
|
|
|
autovector<Slice, MultiGetContext::MAX_BATCH_SIZE> keys;
|
|
|
|
autovector<PinnableSlice, MultiGetContext::MAX_BATCH_SIZE> values;
|
|
|
|
autovector<Status, MultiGetContext::MAX_BATCH_SIZE> statuses;
|
|
|
|
{
|
|
|
|
const int step =
|
|
|
|
static_cast<int>(kv.size()) / MultiGetContext::MAX_BATCH_SIZE;
|
|
|
|
auto it = kv.begin();
|
|
|
|
for (int i = 0; i < MultiGetContext::MAX_BATCH_SIZE; i++) {
|
|
|
|
keys.emplace_back(it->first);
|
|
|
|
values.emplace_back();
|
|
|
|
statuses.emplace_back();
|
|
|
|
std::advance(it, step);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string table_name =
|
|
|
|
"BlockBasedTableReaderTest" + CompressionTypeToString(compression_type_);
|
|
|
|
CreateTable(table_name, compression_type_, kv);
|
|
|
|
|
|
|
|
std::unique_ptr<BlockBasedTable> table;
|
|
|
|
Options options;
|
2021-05-05 22:59:21 +02:00
|
|
|
ImmutableOptions ioptions(options);
|
2020-05-15 08:23:32 +02:00
|
|
|
FileOptions foptions;
|
|
|
|
foptions.use_direct_reads = use_direct_reads_;
|
|
|
|
InternalKeyComparator comparator(options.comparator);
|
|
|
|
NewBlockBasedTableReader(foptions, ioptions, comparator, table_name, &table);
|
|
|
|
|
|
|
|
// Ensure that keys are not in cache before MultiGet.
|
|
|
|
for (auto& key : keys) {
|
|
|
|
ASSERT_FALSE(table->TEST_KeyInCache(ReadOptions(), key));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare MultiGetContext.
|
|
|
|
autovector<GetContext, MultiGetContext::MAX_BATCH_SIZE> get_context;
|
|
|
|
autovector<KeyContext, MultiGetContext::MAX_BATCH_SIZE> key_context;
|
|
|
|
autovector<KeyContext*, MultiGetContext::MAX_BATCH_SIZE> sorted_keys;
|
|
|
|
for (size_t i = 0; i < keys.size(); ++i) {
|
|
|
|
get_context.emplace_back(
|
|
|
|
BytewiseComparator(), nullptr, nullptr, nullptr, GetContext::kNotFound,
|
|
|
|
keys[i], &values[i], nullptr, nullptr, nullptr, true /* do_merge */,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
|
|
|
|
key_context.emplace_back(nullptr, keys[i], &values[i], nullptr,
|
|
|
|
&statuses.back());
|
|
|
|
key_context.back().get_context = &get_context.back();
|
|
|
|
}
|
|
|
|
for (auto& key_ctx : key_context) {
|
|
|
|
sorted_keys.emplace_back(&key_ctx);
|
|
|
|
}
|
|
|
|
MultiGetContext ctx(&sorted_keys, 0, sorted_keys.size(), 0, ReadOptions());
|
|
|
|
|
|
|
|
// Execute MultiGet.
|
|
|
|
MultiGetContext::Range range = ctx.GetMultiGetRange();
|
2021-08-20 20:49:53 +02:00
|
|
|
PerfContext* perf_ctx = get_perf_context();
|
|
|
|
perf_ctx->Reset();
|
2020-05-15 08:23:32 +02:00
|
|
|
table->MultiGet(ReadOptions(), &range, nullptr);
|
|
|
|
|
2021-08-20 20:49:53 +02:00
|
|
|
ASSERT_GE(perf_ctx->block_read_count - perf_ctx->index_block_read_count -
|
|
|
|
perf_ctx->filter_block_read_count -
|
|
|
|
perf_ctx->compression_dict_block_read_count,
|
|
|
|
1);
|
|
|
|
ASSERT_GE(perf_ctx->block_read_byte, 1);
|
|
|
|
|
2020-05-15 08:23:32 +02:00
|
|
|
for (const Status& status : statuses) {
|
|
|
|
ASSERT_OK(status);
|
|
|
|
}
|
|
|
|
// Check that keys are in cache after MultiGet.
|
|
|
|
for (size_t i = 0; i < keys.size(); i++) {
|
|
|
|
ASSERT_TRUE(table->TEST_KeyInCache(ReadOptions(), keys[i]));
|
|
|
|
ASSERT_EQ(values[i].ToString(), kv[keys[i].ToString()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 19:33:00 +02:00
|
|
|
class BlockBasedTableReaderResOnlyCache : public CacheWrapper {
|
|
|
|
public:
|
|
|
|
explicit BlockBasedTableReaderResOnlyCache(std::shared_ptr<Cache> target)
|
|
|
|
: CacheWrapper(std::move(target)) {}
|
|
|
|
|
|
|
|
using Cache::Insert;
|
|
|
|
Status Insert(const Slice& key, void* value, size_t charge,
|
|
|
|
void (*deleter)(const Slice& key, void* value),
|
|
|
|
Handle** handle = nullptr,
|
|
|
|
Priority priority = Priority::LOW) override {
|
|
|
|
if (deleter == kNoopDeleterForBlockBasedTableReader) {
|
|
|
|
return target_->Insert(key, value, charge, deleter, handle, priority);
|
|
|
|
} else {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
using Cache::Release;
|
|
|
|
bool Release(Handle* handle, bool force_erase = false) override {
|
|
|
|
auto deleter = GetDeleter(handle);
|
|
|
|
if (deleter == kNoopDeleterForBlockBasedTableReader) {
|
|
|
|
return target_->Release(handle, force_erase);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const Cache::DeleterFn kNoopDeleterForBlockBasedTableReader;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Cache::DeleterFn
|
|
|
|
BlockBasedTableReaderResOnlyCache::kNoopDeleterForBlockBasedTableReader =
|
|
|
|
CacheReservationManagerImpl<CacheEntryRole::kBlockBasedTableReader>::
|
|
|
|
TEST_GetNoopDeleterForRole();
|
|
|
|
|
|
|
|
class BlockBasedTableReaderCapMemoryTest
|
|
|
|
: public BlockBasedTableReaderBaseTest,
|
|
|
|
public testing::WithParamInterface<
|
|
|
|
bool /* reserve_table_builder_memory */> {
|
|
|
|
protected:
|
|
|
|
static std::size_t CalculateMaxTableReaderNumBeforeCacheFull(
|
|
|
|
std::size_t cache_capacity, std::size_t approx_table_reader_mem) {
|
|
|
|
// To make calculation easier for testing
|
|
|
|
assert(cache_capacity % CacheReservationManagerImpl<
|
|
|
|
CacheEntryRole::kBlockBasedTableReader>::
|
|
|
|
GetDummyEntrySize() ==
|
|
|
|
0 &&
|
|
|
|
cache_capacity > 2 * CacheReservationManagerImpl<
|
|
|
|
CacheEntryRole::kBlockBasedTableReader>::
|
|
|
|
GetDummyEntrySize());
|
|
|
|
|
|
|
|
// We need to subtract 1 for max_num_dummy_entry to account for dummy
|
|
|
|
// entries' overhead, assumed the overhead is no greater than 1 dummy entry
|
|
|
|
// size
|
|
|
|
std::size_t max_num_dummy_entry =
|
|
|
|
(size_t)std::floor((
|
|
|
|
1.0 * cache_capacity /
|
|
|
|
CacheReservationManagerImpl<
|
|
|
|
CacheEntryRole::kBlockBasedTableReader>::GetDummyEntrySize())) -
|
|
|
|
1;
|
|
|
|
std::size_t cache_capacity_rounded_to_dummy_entry_multiples =
|
|
|
|
max_num_dummy_entry *
|
|
|
|
CacheReservationManagerImpl<
|
|
|
|
CacheEntryRole::kBlockBasedTableReader>::GetDummyEntrySize();
|
|
|
|
std::size_t max_table_reader_num = static_cast<std::size_t>(
|
|
|
|
std::floor(1.0 * cache_capacity_rounded_to_dummy_entry_multiples /
|
|
|
|
approx_table_reader_mem));
|
|
|
|
|
|
|
|
return max_table_reader_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetUp() override {
|
|
|
|
// To cache and re-use the same kv map and compression type in the test
|
|
|
|
// suite for elimiating variance caused by these two factors
|
|
|
|
kv_ = BlockBasedTableReaderBaseTest::GenerateKVMap();
|
|
|
|
compression_type_ = CompressionType::kNoCompression;
|
|
|
|
|
|
|
|
table_reader_res_only_cache_.reset(new BlockBasedTableReaderResOnlyCache(
|
|
|
|
NewLRUCache(6 * CacheReservationManagerImpl<
|
|
|
|
CacheEntryRole::kBlockBasedTableReader>::
|
|
|
|
GetDummyEntrySize(),
|
|
|
|
0 /* num_shard_bits */, true /* strict_capacity_limit */)));
|
|
|
|
|
|
|
|
// To ApproximateTableReaderMem() without encountering any potential errors
|
|
|
|
// caused by BlocBasedTableReader::reserve_table_reader_memory == true, we
|
|
|
|
// first turn off the feature to test
|
|
|
|
reserve_table_reader_memory_ = false;
|
|
|
|
BlockBasedTableReaderBaseTest::SetUp();
|
|
|
|
approx_table_reader_mem_ = ApproximateTableReaderMem();
|
|
|
|
|
|
|
|
// Now we condtionally turn on the feature to test
|
|
|
|
reserve_table_reader_memory_ = GetParam();
|
|
|
|
ConfigureTableFactory();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureTableFactory() override {
|
|
|
|
BlockBasedTableOptions table_options;
|
|
|
|
table_options.reserve_table_reader_memory = reserve_table_reader_memory_;
|
|
|
|
table_options.block_cache = table_reader_res_only_cache_;
|
|
|
|
|
|
|
|
table_options.cache_index_and_filter_blocks = false;
|
|
|
|
table_options.filter_policy.reset(NewBloomFilterPolicy(10, false));
|
|
|
|
table_options.partition_filters = true;
|
|
|
|
table_options.index_type = BlockBasedTableOptions::kTwoLevelIndexSearch;
|
|
|
|
|
|
|
|
options_.table_factory.reset(NewBlockBasedTableFactory(table_options));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool reserve_table_reader_memory_;
|
|
|
|
std::shared_ptr<BlockBasedTableReaderResOnlyCache>
|
|
|
|
table_reader_res_only_cache_;
|
|
|
|
std::size_t approx_table_reader_mem_;
|
|
|
|
std::map<std::string, std::string> kv_;
|
|
|
|
CompressionType compression_type_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::size_t ApproximateTableReaderMem() {
|
|
|
|
std::size_t approx_table_reader_mem = 0;
|
|
|
|
|
|
|
|
std::string table_name = "table_for_approx_table_reader_mem";
|
|
|
|
CreateTable(table_name, compression_type_, kv_);
|
|
|
|
|
|
|
|
std::unique_ptr<BlockBasedTable> table;
|
|
|
|
Status s;
|
|
|
|
NewBlockBasedTableReader(
|
|
|
|
FileOptions(), ImmutableOptions(options_),
|
|
|
|
InternalKeyComparator(options_.comparator), table_name, &table,
|
|
|
|
false /* prefetch_index_and_filter_in_cache */, &s);
|
|
|
|
assert(s.ok());
|
|
|
|
|
|
|
|
approx_table_reader_mem = table->ApproximateMemoryUsage();
|
|
|
|
assert(approx_table_reader_mem > 0);
|
|
|
|
return approx_table_reader_mem;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(CapMemoryUsageUnderCacheCapacity,
|
|
|
|
BlockBasedTableReaderCapMemoryTest, ::testing::Bool());
|
|
|
|
|
|
|
|
TEST_P(BlockBasedTableReaderCapMemoryTest, CapMemoryUsageUnderCacheCapacity) {
|
|
|
|
const std::size_t max_table_reader_num = BlockBasedTableReaderCapMemoryTest::
|
|
|
|
CalculateMaxTableReaderNumBeforeCacheFull(
|
|
|
|
table_reader_res_only_cache_->GetCapacity(),
|
|
|
|
approx_table_reader_mem_);
|
|
|
|
|
|
|
|
Status s = Status::OK();
|
|
|
|
std::size_t opened_table_reader_num = 0;
|
|
|
|
std::string table_name;
|
|
|
|
std::vector<std::unique_ptr<BlockBasedTable>> tables;
|
|
|
|
|
|
|
|
// Keep creating BlockBasedTableReader till hiting the memory limit based on
|
|
|
|
// cache capacity and creation fails or reaching a big number of table readers
|
|
|
|
while (s.ok() && opened_table_reader_num < 2 * max_table_reader_num) {
|
|
|
|
table_name = "table_" + std::to_string(opened_table_reader_num);
|
|
|
|
CreateTable(table_name, compression_type_, kv_);
|
|
|
|
tables.push_back(std::unique_ptr<BlockBasedTable>());
|
|
|
|
NewBlockBasedTableReader(
|
|
|
|
FileOptions(), ImmutableOptions(options_),
|
|
|
|
InternalKeyComparator(options_.comparator), table_name, &tables.back(),
|
|
|
|
false /* prefetch_index_and_filter_in_cache */, &s);
|
|
|
|
if (s.ok()) {
|
|
|
|
++opened_table_reader_num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reserve_table_reader_memory_) {
|
|
|
|
EXPECT_TRUE(s.IsMemoryLimit() &&
|
|
|
|
opened_table_reader_num < 2 * max_table_reader_num)
|
|
|
|
<< "s: " << s.ToString() << " opened_table_reader_num: "
|
|
|
|
<< std::to_string(opened_table_reader_num);
|
|
|
|
EXPECT_TRUE(s.ToString().find("memory limit based on cache capacity") !=
|
|
|
|
std::string::npos);
|
|
|
|
|
|
|
|
// Acceptable estimtation errors coming from
|
|
|
|
// 1. overstimate max_table_reader_num due to # dummy entries is high and
|
|
|
|
// results in metadata charge overhead greater than 1 dummy entry size
|
|
|
|
// (violating our assumption in calculating max_table_reader_nums)
|
|
|
|
// 2. overestimate/underestimate max_table_reader_num due to the gap between
|
|
|
|
// ApproximateTableReaderMem() and actual table reader mem
|
|
|
|
EXPECT_GE(opened_table_reader_num, max_table_reader_num * 0.99);
|
|
|
|
EXPECT_LE(opened_table_reader_num, max_table_reader_num * 1.01);
|
|
|
|
|
|
|
|
std::size_t updated_max_table_reader_num =
|
|
|
|
BlockBasedTableReaderCapMemoryTest::
|
|
|
|
CalculateMaxTableReaderNumBeforeCacheFull(
|
|
|
|
table_reader_res_only_cache_->GetCapacity() / 2,
|
|
|
|
approx_table_reader_mem_);
|
|
|
|
|
|
|
|
// Keep deleting BlockBasedTableReader to lower down memory usage from the
|
|
|
|
// memory limit to make the next creation succeeds
|
|
|
|
while (opened_table_reader_num >= updated_max_table_reader_num) {
|
|
|
|
tables.pop_back();
|
|
|
|
--opened_table_reader_num;
|
|
|
|
}
|
|
|
|
table_name = "table_for_successful_table_reader_open";
|
|
|
|
CreateTable(table_name, compression_type_, kv_);
|
|
|
|
tables.push_back(std::unique_ptr<BlockBasedTable>());
|
|
|
|
NewBlockBasedTableReader(
|
|
|
|
FileOptions(), ImmutableOptions(options_),
|
|
|
|
InternalKeyComparator(options_.comparator), table_name, &tables.back(),
|
|
|
|
false /* prefetch_index_and_filter_in_cache */, &s);
|
|
|
|
EXPECT_TRUE(s.ok()) << s.ToString();
|
|
|
|
|
|
|
|
tables.clear();
|
|
|
|
EXPECT_EQ(table_reader_res_only_cache_->GetPinnedUsage(), 0);
|
|
|
|
} else {
|
|
|
|
EXPECT_TRUE(s.ok() && opened_table_reader_num == 2 * max_table_reader_num)
|
|
|
|
<< "s: " << s.ToString() << " opened_table_reader_num: "
|
|
|
|
<< std::to_string(opened_table_reader_num);
|
|
|
|
EXPECT_EQ(table_reader_res_only_cache_->GetPinnedUsage(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:06:26 +02:00
|
|
|
class BlockBasedTableReaderTestVerifyChecksum
|
|
|
|
: public BlockBasedTableReaderTest {
|
|
|
|
public:
|
|
|
|
BlockBasedTableReaderTestVerifyChecksum() : BlockBasedTableReaderTest() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_P(BlockBasedTableReaderTestVerifyChecksum, ChecksumMismatch) {
|
2022-04-06 19:33:00 +02:00
|
|
|
std::map<std::string, std::string> kv =
|
|
|
|
BlockBasedTableReaderBaseTest::GenerateKVMap(800 /* num_block */);
|
2020-06-05 20:06:26 +02:00
|
|
|
|
|
|
|
std::string table_name =
|
|
|
|
"BlockBasedTableReaderTest" + CompressionTypeToString(compression_type_);
|
|
|
|
CreateTable(table_name, compression_type_, kv);
|
|
|
|
|
|
|
|
std::unique_ptr<BlockBasedTable> table;
|
|
|
|
Options options;
|
2021-05-05 22:59:21 +02:00
|
|
|
ImmutableOptions ioptions(options);
|
2020-06-05 20:06:26 +02:00
|
|
|
FileOptions foptions;
|
|
|
|
foptions.use_direct_reads = use_direct_reads_;
|
|
|
|
InternalKeyComparator comparator(options.comparator);
|
|
|
|
NewBlockBasedTableReader(foptions, ioptions, comparator, table_name, &table);
|
|
|
|
|
|
|
|
// Use the top level iterator to find the offset/size of the first
|
|
|
|
// 2nd level index block and corrupt the block
|
|
|
|
IndexBlockIter iiter_on_stack;
|
|
|
|
BlockCacheLookupContext context{TableReaderCaller::kUserVerifyChecksum};
|
|
|
|
InternalIteratorBase<IndexValue>* iiter = table->NewIndexIterator(
|
|
|
|
ReadOptions(), /*disable_prefix_seek=*/false, &iiter_on_stack,
|
|
|
|
/*get_context=*/nullptr, &context);
|
|
|
|
std::unique_ptr<InternalIteratorBase<IndexValue>> iiter_unique_ptr;
|
|
|
|
if (iiter != &iiter_on_stack) {
|
|
|
|
iiter_unique_ptr = std::unique_ptr<InternalIteratorBase<IndexValue>>(iiter);
|
|
|
|
}
|
|
|
|
ASSERT_OK(iiter->status());
|
|
|
|
iiter->SeekToFirst();
|
2020-09-30 01:26:58 +02:00
|
|
|
BlockHandle handle = static_cast<PartitionedIndexIterator*>(iiter)
|
2020-06-05 20:06:26 +02:00
|
|
|
->index_iter_->value()
|
|
|
|
.handle;
|
|
|
|
table.reset();
|
|
|
|
|
|
|
|
// Corrupt the block pointed to by handle
|
Fix many tests to run with MEM_ENV and ENCRYPTED_ENV; Introduce a MemoryFileSystem class (#7566)
Summary:
This PR does a few things:
1. The MockFileSystem class was split out from the MockEnv. This change would theoretically allow a MockFileSystem to be used by other Environments as well (if we created a means of constructing one). The MockFileSystem implements a FileSystem in its entirety and does not rely on any Wrapper implementation.
2. Make the RocksDB test suite work when MOCK_ENV=1 and ENCRYPTED_ENV=1 are set. To accomplish this, a few things were needed:
- The tests that tried to use the "wrong" environment (Env::Default() instead of env_) were updated
- The MockFileSystem was changed to support the features it was missing or mishandled (such as recursively deleting files in a directory or supporting renaming of a directory).
3. Updated the test framework to have a ROCKSDB_GTEST_SKIP macro. This can be used to flag tests that are skipped. Currently, this defaults to doing nothing (marks the test as SUCCESS) but will mark the tests as SKIPPED when RocksDB is upgraded to a version of gtest that supports this (gtest-1.10).
I have run a full "make check" with MEM_ENV, ENCRYPTED_ENV, both, and neither under both MacOS and RedHat. A few tests were disabled/skipped for the MEM/ENCRYPTED cases. The error_handler_fs_test fails/hangs for MEM_ENV (presumably a timing problem) and I will introduce another PR/issue to track that problem. (I will also push a change to disable those tests soon). There is one more test in DBTest2 that also fails which I need to investigate or skip before this PR is merged.
Theoretically, this PR should also allow the test suite to run against an Env loaded from the registry, though I do not have one to try it with currently.
Finally, once this is accepted, it would be nice if there was a CircleCI job to run these tests on a checkin so this effort does not become stale. I do not know how to do that, so if someone could write that job, it would be appreciated :)
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7566
Reviewed By: zhichao-cao
Differential Revision: D24408980
Pulled By: jay-zhuang
fbshipit-source-id: 911b1554a4d0da06fd51feca0c090a4abdcb4a5f
2020-10-27 18:31:34 +01:00
|
|
|
ASSERT_OK(test::CorruptFile(options.env, Path(table_name),
|
|
|
|
static_cast<int>(handle.offset()), 128));
|
2020-06-05 20:06:26 +02:00
|
|
|
|
|
|
|
NewBlockBasedTableReader(foptions, ioptions, comparator, table_name, &table);
|
|
|
|
Status s = table->VerifyChecksum(ReadOptions(),
|
|
|
|
TableReaderCaller::kUserVerifyChecksum);
|
|
|
|
ASSERT_EQ(s.code(), Status::kCorruption);
|
|
|
|
}
|
|
|
|
|
2020-05-15 08:23:32 +02:00
|
|
|
// Param 1: compression type
|
|
|
|
// Param 2: whether to use direct reads
|
2020-06-05 20:06:26 +02:00
|
|
|
// Param 3: Block Based Table Index type
|
|
|
|
// Param 4: BBTO no_block_cache option
|
2020-05-21 22:55:18 +02:00
|
|
|
#ifdef ROCKSDB_LITE
|
|
|
|
// Skip direct I/O tests in lite mode since direct I/O is unsupported.
|
2020-06-04 00:53:09 +02:00
|
|
|
INSTANTIATE_TEST_CASE_P(
|
2020-05-21 22:55:18 +02:00
|
|
|
MultiGet, BlockBasedTableReaderTest,
|
2020-06-05 20:06:26 +02:00
|
|
|
::testing::Combine(
|
|
|
|
::testing::ValuesIn(GetSupportedCompressions()),
|
|
|
|
::testing::Values(false),
|
|
|
|
::testing::Values(BlockBasedTableOptions::IndexType::kBinarySearch),
|
|
|
|
::testing::Values(false)));
|
2020-05-21 22:55:18 +02:00
|
|
|
#else // ROCKSDB_LITE
|
2020-06-04 00:53:09 +02:00
|
|
|
INSTANTIATE_TEST_CASE_P(
|
2020-05-15 08:23:32 +02:00
|
|
|
MultiGet, BlockBasedTableReaderTest,
|
2020-06-05 20:06:26 +02:00
|
|
|
::testing::Combine(
|
|
|
|
::testing::ValuesIn(GetSupportedCompressions()), ::testing::Bool(),
|
|
|
|
::testing::Values(BlockBasedTableOptions::IndexType::kBinarySearch),
|
|
|
|
::testing::Values(false)));
|
2020-05-21 22:55:18 +02:00
|
|
|
#endif // ROCKSDB_LITE
|
2020-06-05 20:06:26 +02:00
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
|
|
VerifyChecksum, BlockBasedTableReaderTestVerifyChecksum,
|
|
|
|
::testing::Combine(
|
|
|
|
::testing::ValuesIn(GetSupportedCompressions()),
|
|
|
|
::testing::Values(false),
|
|
|
|
::testing::Values(
|
|
|
|
BlockBasedTableOptions::IndexType::kTwoLevelIndexSearch),
|
|
|
|
::testing::Values(true)));
|
2020-05-15 08:23:32 +02:00
|
|
|
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|