Skip to content

Commit

Permalink
[Enhancement] set empty delta column group when new segment generate (S…
Browse files Browse the repository at this point in the history
…tarRocks#25506)

Fixes StarRocks#20436
Set empty delta column group when new segment generate

Signed-off-by: luohaha <[email protected]>
  • Loading branch information
luohaha authored Jun 20, 2023
1 parent de0fc73 commit 5fe3afc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
4 changes: 4 additions & 0 deletions be/src/storage/tablet_updates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,8 @@ void TabletUpdates::_apply_normal_rowset_commit(const EditVersionInfo& version_i
for (auto& delvec_pair : new_del_vecs) {
tsid.segment_id = delvec_pair.first;
manager->set_cached_del_vec(tsid, delvec_pair.second);
// try to set empty dcg cache, for improving latency when reading
manager->set_cached_empty_delta_column_group(_tablet.data_dir()->get_meta(), tsid);
}
// 5. apply memory
_next_log_id++;
Expand Down Expand Up @@ -1836,6 +1838,8 @@ void TabletUpdates::_apply_compaction_commit(const EditVersionInfo& version_info
for (auto& delvec_pair : delvecs) {
tsid.segment_id = delvec_pair.first;
manager->set_cached_del_vec(tsid, delvec_pair.second);
// try to set empty dcg cache, for improving latency when reading
manager->set_cached_empty_delta_column_group(_tablet.data_dir()->get_meta(), tsid);
}
// 5. apply memory
_next_log_id++;
Expand Down
42 changes: 41 additions & 1 deletion be/src/storage/update_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ StatusOr<size_t> UpdateManager::clear_delta_column_group_before_version(KVStore*
std::vector<std::pair<TabletSegmentId, int64_t>> clear_dcgs;
const int64_t begin_ms = UnixMillis();
auto is_timeout = [begin_ms]() {
if (UnixMillis() > begin_ms + 100) { // only hold cache_clock for 100ms max.
if (UnixMillis() > begin_ms + 10) { // only hold cache_clock for 10ms max.
return true;
} else {
return false;
Expand Down Expand Up @@ -283,6 +283,46 @@ void UpdateManager::clear_cached_delta_column_group(const std::vector<TabletSegm
}
}

Status UpdateManager::set_cached_empty_delta_column_group(KVStore* meta, const TabletSegmentId& tsid) {
{
std::lock_guard<std::mutex> lg(_delta_column_group_cache_lock);
auto itr = _delta_column_group_cache.find(tsid);
if (itr != _delta_column_group_cache.end()) {
// already exist, not need to cache
return Status::OK();
}
}
// find from rocksdb
DeltaColumnGroupList new_dcgs;
RETURN_IF_ERROR(
TabletMetaManager::get_delta_column_group(meta, tsid.tablet_id, tsid.segment_id, INT64_MAX, &new_dcgs));
std::lock_guard<std::mutex> lg(_delta_column_group_cache_lock);
auto itr = _delta_column_group_cache.find(tsid);
if (itr != _delta_column_group_cache.end()) {
// already exist, not need to cache
return Status::OK();
}
if (new_dcgs.empty()) {
// only set empty dcgs
_delta_column_group_cache[tsid] = new_dcgs;
}
return Status::OK();
}

bool UpdateManager::get_cached_delta_column_group(const TabletSegmentId& tsid, int64_t version,
DeltaColumnGroupList* dcgs) {
// find in delta column group cache
std::lock_guard<std::mutex> lg(_delta_column_group_cache_lock);
auto itr = _delta_column_group_cache.find(tsid);
if (itr != _delta_column_group_cache.end()) {
search_delta_column_groups_by_version(itr->second, version, dcgs);
// hit cache
return true;
}
// miss cache
return false;
}

Status UpdateManager::set_cached_delta_column_group(KVStore* meta, const TabletSegmentId& tsid,
const DeltaColumnGroupPtr& dcg) {
{
Expand Down
4 changes: 4 additions & 0 deletions be/src/storage/update_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class UpdateManager {

Status set_cached_delta_column_group(KVStore* meta, const TabletSegmentId& tsid, const DeltaColumnGroupPtr& dcg);

Status set_cached_empty_delta_column_group(KVStore* meta, const TabletSegmentId& tsid);

bool get_cached_delta_column_group(const TabletSegmentId& tsid, int64_t version, DeltaColumnGroupList* dcgs);

void clear_cache();

void clear_cached_del_vec(const std::vector<TabletSegmentId>& tsids);
Expand Down
16 changes: 16 additions & 0 deletions be/test/storage/update_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,20 @@ TEST_F(UpdateManagerTest, testExpireEntry) {
ASSERT_EQ(peak_size - expiring_size, remaining_size);
}

TEST_F(UpdateManagerTest, testSetEmptyCachedDeltaColumnGroup) {
srand(time(nullptr));
create_tablet(rand(), rand());
TabletSegmentId tsid;
tsid.tablet_id = _tablet->tablet_id();
tsid.segment_id = 1;
_update_manager->set_cached_empty_delta_column_group(_tablet->data_dir()->get_meta(), tsid);
// search this empty dcg
DeltaColumnGroupList dcgs;
// search in cache
ASSERT_TRUE(_update_manager->get_cached_delta_column_group(tsid, 1, &dcgs));
ASSERT_TRUE(dcgs.empty());
_update_manager->get_delta_column_group(_tablet->data_dir()->get_meta(), tsid, 1, &dcgs);
ASSERT_TRUE(dcgs.empty());
}

} // namespace starrocks

0 comments on commit 5fe3afc

Please sign in to comment.