Skip to content

Commit

Permalink
[Enhancement] add memory statistic for cloud native persistent index'…
Browse files Browse the repository at this point in the history
…s sstable (backport #50595) (#50678)

Co-authored-by: Yixin Luo <[email protected]>
  • Loading branch information
mergify[bot] and luohaha authored Sep 4, 2024
1 parent 50c218e commit 4673054
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
5 changes: 5 additions & 0 deletions be/src/storage/lake/lake_persistent_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,11 @@ size_t LakePersistentIndex::memory_usage() const {
if (_immutable_memtable != nullptr) {
mem_usage += _immutable_memtable->memory_usage();
}
for (const auto& sst_ptr : _sstables) {
if (sst_ptr != nullptr) {
mem_usage += sst_ptr->memory_usage();
}
}
return mem_usage;
}

Expand Down
4 changes: 4 additions & 0 deletions be/src/storage/lake/persistent_index_sstable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,8 @@ Status PersistentIndexSstable::multi_get(const Slice* keys, const KeyIndexSet& k
return Status::OK();
}

size_t PersistentIndexSstable::memory_usage() const {
return (_sst != nullptr) ? _sst->memory_usage() : 0;
}

} // namespace starrocks::lake
2 changes: 2 additions & 0 deletions be/src/storage/lake/persistent_index_sstable.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class PersistentIndexSstable {

const PersistentIndexSstablePB& sstable_pb() const { return _sstable_pb; }

size_t memory_usage() const;

private:
std::unique_ptr<sstable::Table> _sst{nullptr};
std::unique_ptr<sstable::FilterPolicy> _filter_policy{nullptr};
Expand Down
21 changes: 15 additions & 6 deletions be/src/storage/sstable/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ struct Table::Rep {
~Rep() {
delete filter;
delete[] filter_data;
filter_data_size = 0;
delete index_block;
}

Options options;
Status status;
RandomAccessFile* file;
uint64_t cache_id;
FilterBlockReader* filter;
const char* filter_data;
RandomAccessFile* file = nullptr;
uint64_t cache_id = 0;
FilterBlockReader* filter = nullptr;
const char* filter_data = nullptr;
size_t filter_data_size = 0;

BlockHandle metaindex_handle; // Handle to metaindex_block: saved from footer
Block* index_block;
Block* index_block = nullptr;
};

Status Table::Open(const Options& options, RandomAccessFile* file, uint64_t size, Table** table) {
Expand Down Expand Up @@ -135,7 +137,8 @@ void Table::ReadFilter(const Slice& filter_handle_value) {
return;
}
if (block.heap_allocated) {
rep_->filter_data = block.data.get_data(); // Will need to delete later
rep_->filter_data = block.data.get_data(); // Will need to delete later
rep_->filter_data_size = block.data.get_size(); // mem tracker will track this piece of memory.
}
rep_->filter = new FilterBlockReader(rep_->options.filter_policy, block.data);
}
Expand Down Expand Up @@ -302,6 +305,12 @@ Status Table::MultiGet(const ReadOptions& options, const Slice* keys, ForwardIt
return s;
}

size_t Table::memory_usage() const {
const size_t index_block_sz = (rep_->index_block != nullptr) ? rep_->index_block->size() : 0;
const size_t filter_data_sz = rep_->filter_data_size;
return index_block_sz + filter_data_sz;
}

// If new container wants to be supported in MultiGet, the initialization can be added here.
template Status Table::MultiGet<std::set<size_t>::iterator>(const ReadOptions& options, const Slice* keys,
std::set<size_t>::iterator begin,
Expand Down
2 changes: 2 additions & 0 deletions be/src/storage/sstable/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Table {
Status MultiGet(const ReadOptions&, const Slice* keys, ForwardIt begin, ForwardIt end,
std::vector<std::string>* values);

size_t memory_usage() const;

private:
struct Rep;

Expand Down
4 changes: 2 additions & 2 deletions be/test/storage/lake/lake_persistent_index_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ TEST_F(LakePersistentIndexTest, test_basic_api) {
auto index = std::make_unique<LakePersistentIndex>(_tablet_mgr.get(), tablet_id);
ASSERT_OK(index->init(_tablet_metadata->sstable_meta()));
ASSERT_OK(index->insert(N, key_slices.data(), values.data(), 0));
// insert duplicate should return error
// ASSERT_FALSE(index->insert(N, key_slices.data(), values.data(), 0).ok());
ASSERT_TRUE(index->memory_usage() > 0);

// test get
vector<IndexValue> get_values(keys.size());
Expand Down Expand Up @@ -233,6 +232,7 @@ TEST_F(LakePersistentIndexTest, test_major_compaction) {
// generate sst files.
index->minor_compact();
}
ASSERT_TRUE(index->memory_usage() > 0);

Tablet tablet(_tablet_mgr.get(), tablet_id);
auto tablet_metadata_ptr = std::make_shared<TabletMetadata>();
Expand Down
2 changes: 2 additions & 0 deletions be/test/storage/lake/persistent_index_sstable_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ TEST_F(PersistentIndexSstableTest, test_persistent_index_sstable) {
sstable_pb.set_filename(filename);
sstable_pb.set_filesize(filesize);
ASSERT_OK(sst->init(std::move(read_file), sstable_pb, cache_ptr.get()));
// check memory usage
ASSERT_TRUE(sst->memory_usage() > 0);

{
// 3. multi get with version (all keys included)
Expand Down

0 comments on commit 4673054

Please sign in to comment.