Skip to content

Commit

Permalink
ForceEvict support for PartitionedFilter & PartitionedReader
Browse files Browse the repository at this point in the history
  • Loading branch information
mm304321141 committed Feb 24, 2022
1 parent 5a02a3f commit db1fe8c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
58 changes: 58 additions & 0 deletions table/block_based_table_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,58 @@ class PartitionIndexReader : public IndexReader, public Cleanable {
}
}

virtual void ForceEvict() override {
auto rep = table_->rep_;
IndexBlockIter biter;
Statistics* kNullStats = nullptr;
index_block_->NewIterator<IndexBlockIter>(
icomparator_, icomparator_->user_comparator(), &biter, kNullStats, true,
index_key_includes_seq_, index_value_is_full_);

BlockHandle handle;
char
cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length];
char compressed_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();
Cache* block_cache_compressed =
rep->immortal_table ? nullptr
: rep->table_options.block_cache_compressed.get();

for (biter.SeekToFirst(); biter.Valid(); biter.Next()) {
handle = biter.value();

if (block_cache != nullptr) {
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;
}
if (block_cache_compressed != nullptr) {
auto key =
BlockBasedTable::GetCacheKey(rep->compressed_cache_key_prefix,
rep->compressed_cache_key_prefix_size,
handle, compressed_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);
}

virtual size_t size() const override { return index_block_->size(); }
virtual size_t usable_size() const override {
return index_block_->usable_size();
Expand Down Expand Up @@ -2603,6 +2655,9 @@ 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());
Expand All @@ -2620,6 +2675,9 @@ Status BlockBasedTable::ForceEvict() {
iiter.reset();

// IndexBlock
if (rep_->index_reader != nullptr) {
rep_->index_reader->ForceEvict();
}
do_evict(rep_->dummy_index_reader_offset);

RecordTick(rep_->ioptions.statistics, BLOCK_CACHE_ERASE,
Expand Down
2 changes: 2 additions & 0 deletions table/block_based_table_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ class BlockBasedTable : public TableReader {

virtual void CacheDependencies(bool /* unused */) {}

virtual void ForceEvict() {}

// Prefetch all the blocks referenced by this index to the buffer
void PrefetchBlocks(FilePrefetchBuffer* buf);

Expand Down
2 changes: 2 additions & 0 deletions table/filter_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ 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,
Expand Down
33 changes: 33 additions & 0 deletions table/partitioned_filter_block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,37 @@ 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<IndexBlockIter>(
&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
2 changes: 2 additions & 0 deletions table/partitioned_filter_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ 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<Block> idx_on_fltr_blk_;
const InternalKeyComparator comparator_;
Expand Down

0 comments on commit db1fe8c

Please sign in to comment.