From b38263272961fbada8b3acd0654de3a558e8663f Mon Sep 17 00:00:00 2001 From: ZhaoMing Date: Fri, 25 Feb 2022 14:02:48 +0800 Subject: [PATCH] More tests --- db/db_test.cc | 271 +++++++++++++++++++++--------- table/block_based_table_reader.cc | 26 ++- table/filter_block.h | 2 - table/partitioned_filter_block.cc | 33 ---- table/partitioned_filter_block.h | 2 - 5 files changed, 197 insertions(+), 137 deletions(-) diff --git a/db/db_test.cc b/db/db_test.cc index 904c368333..895f61e9ca 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -5744,30 +5744,44 @@ TEST_F(DBTest, SkipValueGet) { TEST_F(DBTest, ForceEvict) { Options options = CurrentOptions(); + CompressionType compressions[] = {kZlibCompression, kBZip2Compression, + kLZ4Compression, kLZ4HCCompression, + kXpressCompression}; + BlockBasedTableOptions bbto; bbto.cache_index_and_filter_blocks = false; + bbto.index_type = BlockBasedTableOptions::kTwoLevelIndexSearch; + bbto.filter_policy.reset(TERARKDB_NAMESPACE::NewBloomFilterPolicy(10)); + bbto.partition_filters = true; + bbto.block_cache = TERARKDB_NAMESPACE::NewLRUCache(64ull << 20, 4); options.table_factory.reset(NewBlockBasedTableFactory(bbto)); options.statistics = TERARKDB_NAMESPACE::CreateDBStatistics(); + options.optimize_filters_for_hits = false; options.target_file_size_base = 1; options.disable_auto_compactions = true; + options.compression = kNoCompression; - int evict = 0; - int force_evict = 0; - int found = 0; - int open = 0; - int run = 0; - int failures = 0; + int TC_evict = 0; + int TC_force_evict = 0; + int TC_found = 0; + int TC_open = 0; + int TC_run = 0; + int BC_erase = 0; + int BC_failures = 0; + + int test_sst = 40; + int max_open_files = 20; SyncPoint::GetInstance()->SetCallBack("TableCache::Evict", - [&](void* /* arg */) { ++evict; }); + [&](void* /* arg */) { ++TC_evict; }); SyncPoint::GetInstance()->SetCallBack( - "TableCache::ForceEvict", [&](void* /* arg */) { ++force_evict; }); + "TableCache::ForceEvict", [&](void* /* arg */) { ++TC_force_evict; }); SyncPoint::GetInstance()->SetCallBack("TableCache::ForceEvict:Found", - [&](void* /* arg */) { ++found; }); + [&](void* /* arg */) { ++TC_found; }); SyncPoint::GetInstance()->SetCallBack("TableCache::ForceEvict:Open", - [&](void* /* arg */) { ++open; }); + [&](void* /* arg */) { ++TC_open; }); SyncPoint::GetInstance()->SetCallBack("TableCache::ForceEvict:Run", - [&](void* /* arg */) { ++run; }); + [&](void* /* arg */) { ++TC_run; }); SyncPoint::GetInstance()->EnableProcessing(); auto do_something = [&] { @@ -5778,106 +5792,195 @@ TEST_F(DBTest, ForceEvict) { for (int i = 0; i < 40; ++i) { ASSERT_OK(db_->Get(ReadOptions{}, db_->DefaultColumnFamily(), Key(i))); } - evict = 0; - force_evict = 0; - found = 0; - open = 0; - run = 0; + TC_evict = 0; + TC_force_evict = 0; + TC_found = 0; + TC_open = 0; + TC_run = 0; TestGetAndResetTickerCount(options, TERARKDB_NAMESPACE::BLOCK_CACHE_ERASE); TestGetAndResetTickerCount(options, TERARKDB_NAMESPACE::BLOCK_CACHE_ERASE_FAILURES); ASSERT_OK(db_->CompactRange(TERARKDB_NAMESPACE::CompactRangeOptions(), db_->DefaultColumnFamily(), nullptr, nullptr)); + for (int i = 0; i < 40; ++i) { + ASSERT_OK(db_->Get(ReadOptions{}, db_->DefaultColumnFamily(), Key(i))); + } - failures = TestGetAndResetTickerCount( - options, TERARKDB_NAMESPACE::BLOCK_CACHE_ERASE) - - TestGetAndResetTickerCount( - options, TERARKDB_NAMESPACE::BLOCK_CACHE_ERASE_FAILURES); - - // fprintf(stderr, - // "evict = %d;" - // "force_evict = %d;" - // "found = %d;" - // "open = %d;" - // "run = %d;" - // "failures = %d;\n", - // evict, force_evict, found, open, run, failures); + BC_erase = TestGetAndResetTickerCount( + options, TERARKDB_NAMESPACE::BLOCK_CACHE_ERASE); + BC_failures = TestGetAndResetTickerCount( + options, TERARKDB_NAMESPACE::BLOCK_CACHE_ERASE_FAILURES); + + fprintf(stderr, + "TC_evict = %d;" + "TC_force_evict = %d;" + "TC_found = %d;" + "TC_open = %d;" + "TC_run = %d;" + "BC_erase = %d;" + "BC_failures = %d;\n", + TC_evict, TC_force_evict, TC_found, TC_open, TC_run, BC_erase, + BC_failures); }; options.table_evict_type = TERARKDB_NAMESPACE::kSkipForceEvict; options.max_open_files = -1; DestroyAndReopen(options); do_something(); - // always evict all - ASSERT_EQ(evict, 40); - // no try force_evict - ASSERT_EQ(force_evict, 0); - // no foound - ASSERT_EQ(found, 0); - // no open - ASSERT_EQ(open, 0); - // no run force_evict - ASSERT_EQ(run, 0); - // no cache evict failures - ASSERT_EQ(failures, 0); + ASSERT_EQ(TC_evict, test_sst); + ASSERT_EQ(TC_force_evict, 0); + ASSERT_EQ(TC_found, 0); + ASSERT_EQ(TC_open, 0); + ASSERT_EQ(TC_run, 0); + // block_cache erase: + // index_block(N) + // filter_block(N) + // filter_block_partitions(N) + ASSERT_EQ(BC_erase, test_sst * 3); + // block_cache failures: + // index_block(N) + // filter_block(N) + ASSERT_EQ(BC_failures, test_sst * 2); options.table_evict_type = TERARKDB_NAMESPACE::kForceEvictIfOpen; - options.max_open_files = 20; + options.max_open_files = max_open_files; DestroyAndReopen(options); do_something(); - // always evict all - ASSERT_EQ(evict, 40); - // try force_evict all - ASSERT_EQ(force_evict, 40); - // found 20+ (for max_open_files) - ASSERT_GE(found, 20); - ASSERT_LT(found, 40); - // no open - ASSERT_EQ(open, 0); - // run force_evict 20+ (for max_open_files) - ASSERT_GE(run, 20); - ASSERT_LT(run, 40); - // cache evict failures 20+ (for max_open_files) - ASSERT_GE(failures, 20); - ASSERT_LT(failures, 40); + ASSERT_EQ(TC_evict, test_sst); + ASSERT_EQ(TC_force_evict, test_sst); + ASSERT_GE(TC_found, max_open_files); + ASSERT_LT(TC_found, test_sst); + ASSERT_EQ(TC_open, 0); + ASSERT_EQ(TC_run, TC_found); + // block_cache erase: + // index_block(N+F) + // index_block_partitions(F) + // filter_block(N) + // filter_block_partitions(N) + // data_blocks(F) + ASSERT_EQ(BC_erase, test_sst * 3 + TC_found * 3); + // block_cache failures: + // index_block(N+F) + // filter_block(N) + ASSERT_EQ(BC_failures, test_sst * 2 + TC_found); options.table_evict_type = TERARKDB_NAMESPACE::kForceEvictIfOpen; options.max_open_files = -1; DestroyAndReopen(options); do_something(); - // always evict all - ASSERT_EQ(evict, 40); - // try force_evict all - ASSERT_EQ(force_evict, 40); - // found all - ASSERT_EQ(found, 40); - // no open - ASSERT_EQ(open, 0); - // run all force_evict - ASSERT_EQ(run, 40); - // cache evict failures all - ASSERT_EQ(failures, 40); + ASSERT_EQ(TC_evict, test_sst); + ASSERT_EQ(TC_force_evict, test_sst); + ASSERT_EQ(TC_found, test_sst); + ASSERT_EQ(TC_open, 0); + ASSERT_EQ(TC_run, test_sst); + // block_cache erase: + // index_block(2N) + // index_block_partitions(N) + // filter_block(N) + // filter_block_partitions(N) + // data_blocks(N) + ASSERT_EQ(BC_erase, test_sst * 6); + // block_cache failures: + // index_block(2N) + // filter_block(N) + ASSERT_EQ(BC_failures, test_sst * 3); options.table_evict_type = TERARKDB_NAMESPACE::kAlwaysForceEvict; - options.max_open_files = 20; + options.max_open_files = max_open_files; DestroyAndReopen(options); do_something(); - // always evict all - ASSERT_EQ(evict, 40); - // try force_evict all - ASSERT_EQ(force_evict, 40); - // found 20+ (for max_open_files) - ASSERT_GE(found, 20); - ASSERT_LT(found, 40); - // open 20- (for max_open_files) - ASSERT_GT(open, 0); - ASSERT_LE(open, 20); - // run all force_evict - ASSERT_EQ(run, 40); - // cache evict failures all - ASSERT_EQ(failures, 40); + ASSERT_EQ(TC_evict, test_sst); + ASSERT_EQ(TC_force_evict, test_sst); + ASSERT_GE(TC_found, max_open_files); + ASSERT_LT(TC_found, test_sst); + ASSERT_EQ(TC_open, test_sst - TC_found); + ASSERT_EQ(TC_run, test_sst); + // block_cache erase: + // index_block(2N+O) + // index_block_partitions(N) + // filter_block(N+O) + // filter_block_partitions(N+O) + // data_blocks(N) + ASSERT_EQ(BC_erase, test_sst * 6 + TC_open * 3); + // block_cache failures: + // index_block(2N+O) + // filter_block(N+O) + ASSERT_EQ(BC_failures, test_sst * 3 + TC_open * 2); + + for (auto comp : compressions) { + if (CompressionTypeSupported(comp)) { + options.compression = comp; + break; + } + } + if (options.compression == kNoCompression) { + return; + } + bbto.no_block_cache = true; + bbto.block_cache_compressed.swap(bbto.block_cache); + options.table_factory.reset(NewBlockBasedTableFactory(bbto)); + + options.table_evict_type = TERARKDB_NAMESPACE::kSkipForceEvict; + options.max_open_files = -1; + DestroyAndReopen(options); + do_something(); + ASSERT_EQ(TC_evict, test_sst); + ASSERT_EQ(TC_force_evict, 0); + ASSERT_EQ(TC_found, 0); + ASSERT_EQ(TC_open, 0); + ASSERT_EQ(TC_run, 0); + ASSERT_EQ(BC_erase, 0); + ASSERT_EQ(BC_failures, 0); + + options.table_evict_type = TERARKDB_NAMESPACE::kForceEvictIfOpen; + options.max_open_files = max_open_files; + DestroyAndReopen(options); + do_something(); + ASSERT_EQ(TC_evict, test_sst); + ASSERT_EQ(TC_force_evict, test_sst); + ASSERT_GE(TC_found, max_open_files); + ASSERT_LT(TC_found, test_sst); + ASSERT_EQ(TC_open, 0); + ASSERT_EQ(TC_run, TC_found); + // block_cache_compressed erase: + // index_block_partitions(F) + // data_blocks(F) + ASSERT_EQ(BC_erase, TC_found * 2); + ASSERT_EQ(BC_failures, 0); + + options.table_evict_type = TERARKDB_NAMESPACE::kForceEvictIfOpen; + options.max_open_files = -1; + DestroyAndReopen(options); + do_something(); + ASSERT_EQ(TC_evict, test_sst); + ASSERT_EQ(TC_force_evict, test_sst); + ASSERT_EQ(TC_found, test_sst); + ASSERT_EQ(TC_open, 0); + ASSERT_EQ(TC_run, test_sst); + // block_cache_compressed erase: + // index_block_partitions(N) + // data_blocks(N) + ASSERT_EQ(BC_erase, test_sst * 2); + ASSERT_EQ(BC_failures, 0); + + options.table_evict_type = TERARKDB_NAMESPACE::kAlwaysForceEvict; + options.max_open_files = max_open_files; + DestroyAndReopen(options); + do_something(); + ASSERT_EQ(TC_evict, test_sst); + ASSERT_EQ(TC_force_evict, test_sst); + ASSERT_GE(TC_found, max_open_files); + ASSERT_LT(TC_found, test_sst); + ASSERT_EQ(TC_open, test_sst - TC_found); + ASSERT_EQ(TC_run, test_sst); + // block_cache_compressed erase: + // index_block_partitions(N) + // data_blocks(N) + ASSERT_EQ(BC_erase, test_sst * 2); + ASSERT_EQ(BC_failures, 0); } + } // namespace TERARKDB_NAMESPACE int main(int argc, char** argv) { diff --git a/table/block_based_table_reader.cc b/table/block_based_table_reader.cc index b80420a53d..85a265c198 100644 --- a/table/block_based_table_reader.cc +++ b/table/block_based_table_reader.cc @@ -386,7 +386,7 @@ class PartitionIndexReader : public IndexReader, public Cleanable { rep->compressed_cache_key_prefix_size, handle, compressed_cache_key); - bool erased = block_cache->Erase(key); + bool erased = block_cache_compressed->Erase(key); ++block_cache_erase_count; block_cache_erase_failures_count += !erased; } @@ -2634,16 +2634,16 @@ Status BlockBasedTable::ForceEvict() { uint64_t block_cache_erase_failures_count = 0; auto do_evict = [=, &cache_key, &compressed_cache_key, - &block_cache_erase_count, - &block_cache_erase_failures_count](uint64_t offset) { - if (block_cache) { + &block_cache_erase_count, &block_cache_erase_failures_count]( + uint64_t offset, bool with_block_cache_compressed) { + if (block_cache != nullptr) { char* end = EncodeVarint64(cache_key + cache_key_prefix_size, offset); bool erased = block_cache->Erase( Slice(cache_key, static_cast(end - cache_key))); ++block_cache_erase_count; block_cache_erase_failures_count += !erased; } - if (block_cache_compressed) { + if (with_block_cache_compressed && block_cache_compressed != nullptr) { char* end = EncodeVarint64( compressed_cache_key + compressed_cache_key_prefix_size, offset); bool erased = block_cache_compressed->Erase( @@ -2654,31 +2654,25 @@ Status BlockBasedTable::ForceEvict() { } }; - // FilterBlock - if (rep_->filter_entry.value != nullptr) { - rep_->filter_entry.value->ForceEvict(); - } - if (rep_->filter_policy != nullptr && - !rep_->table_options.cache_index_and_filter_blocks) { - do_evict(rep_->filter_handle.offset()); - } - ReadOptions read_options; + read_options.fill_cache = false; std::unique_ptr> iiter( NewIndexIterator(read_options)); // DataBlock for (iiter->SeekToFirst(); iiter->Valid(); iiter->Next()) { - do_evict(iiter->value().offset()); + do_evict(iiter->value().offset(), true); } iiter.reset(); + // FilterBlock is evicted in BlockBasedTable::Close() + // IndexBlock if (rep_->index_reader != nullptr) { rep_->index_reader->ForceEvict(); } - do_evict(rep_->dummy_index_reader_offset); + do_evict(rep_->dummy_index_reader_offset, false); RecordTick(rep_->ioptions.statistics, BLOCK_CACHE_ERASE, block_cache_erase_count); diff --git a/table/filter_block.h b/table/filter_block.h index 465e90742d..40908fc2d8 100644 --- a/table/filter_block.h +++ b/table/filter_block.h @@ -126,8 +126,6 @@ class FilterBlockReader { virtual void CacheDependencies(bool /*pin*/, const SliceTransform* /*prefix_extractor*/) {} - virtual void ForceEvict() {} - virtual bool RangeMayExist(const Slice* /*iterate_upper_bound*/, const Slice& user_key, const SliceTransform* prefix_extractor, diff --git a/table/partitioned_filter_block.cc b/table/partitioned_filter_block.cc index 9a124d5657..9378825775 100644 --- a/table/partitioned_filter_block.cc +++ b/table/partitioned_filter_block.cc @@ -362,37 +362,4 @@ void PartitionedFilterBlockReader::CacheDependencies( } } -void PartitionedFilterBlockReader::ForceEvict() { - // Before read partitions, prefetch them to avoid lots of IOs - auto rep = table_->rep_; - IndexBlockIter biter; - Statistics* kNullStats = nullptr; - idx_on_fltr_blk_->NewIterator( - &comparator_, comparator_.user_comparator(), &biter, kNullStats, true, - index_key_includes_seq_, index_value_is_full_); - - BlockHandle handle; - char cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length]; - - uint64_t block_cache_erase_count = 0; - uint64_t block_cache_erase_failures_count = 0; - - Cache* block_cache = rep->table_options.block_cache.get(); - for (biter.SeekToFirst(); biter.Valid(); biter.Next()) { - handle = biter.value(); - - auto key = BlockBasedTable::GetCacheKey( - rep->cache_key_prefix, rep->cache_key_prefix_size, handle, cache_key); - - bool erased = block_cache->Erase(key); - ++block_cache_erase_count; - block_cache_erase_failures_count += !erased; - } - - RecordTick(rep->ioptions.statistics, BLOCK_CACHE_ERASE, - block_cache_erase_count); - RecordTick(rep->ioptions.statistics, BLOCK_CACHE_ERASE_FAILURES, - block_cache_erase_failures_count); -} - } // namespace TERARKDB_NAMESPACE diff --git a/table/partitioned_filter_block.h b/table/partitioned_filter_block.h index 9291073624..8377706203 100644 --- a/table/partitioned_filter_block.h +++ b/table/partitioned_filter_block.h @@ -101,8 +101,6 @@ class PartitionedFilterBlockReader : public FilterBlockReader, virtual void CacheDependencies( bool bin, const SliceTransform* prefix_extractor) override; - virtual void ForceEvict() override; - const SliceTransform* prefix_extractor_; std::unique_ptr idx_on_fltr_blk_; const InternalKeyComparator comparator_;