Skip to content

Commit

Permalink
[opt](merge-on-write) avoid to check delete bitmap while lookup rowke…
Browse files Browse the repository at this point in the history
…y in some situation to reduce CPU cost
  • Loading branch information
zhannngchen committed Sep 29, 2024
1 parent 421fde0 commit 3a5b803
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
29 changes: 19 additions & 10 deletions be/src/olap/base_tablet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ Status BaseTablet::lookup_row_key(const Slice& encoded_key, TabletSchema* latest
const std::vector<RowsetSharedPtr>& specified_rowsets,
RowLocation* row_location, uint32_t version,
std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches,
RowsetSharedPtr* rowset, bool with_rowid) {
RowsetSharedPtr* rowset, bool with_rowid,
bool is_partial_update) {
SCOPED_BVAR_LATENCY(g_tablet_lookup_rowkey_latency);
size_t seq_col_length = 0;
// use the latest tablet schema to decide if the tablet has sequence column currently
Expand All @@ -458,6 +459,8 @@ Status BaseTablet::lookup_row_key(const Slice& encoded_key, TabletSchema* latest
Slice(encoded_key.get_data(), encoded_key.get_size() - seq_col_length - rowid_length);
RowLocation loc;

bool need_to_check_delete_bitmap = is_partial_update || with_seq_col;

for (size_t i = 0; i < specified_rowsets.size(); i++) {
auto& rs = specified_rowsets[i];
auto& segments_key_bounds = rs->rowset_meta()->get_segments_key_bounds();
Expand Down Expand Up @@ -496,16 +499,21 @@ Status BaseTablet::lookup_row_key(const Slice& encoded_key, TabletSchema* latest
if (!s.ok() && !s.is<KEY_ALREADY_EXISTS>()) {
return s;
}
if (s.ok() && _tablet_meta->delete_bitmap().contains_agg_without_cache(
{loc.rowset_id, loc.segment_id, version}, loc.row_id)) {
// if has sequence col, we continue to compare the sequence_id of
// all rowsets, util we find an existing key.
if (schema->has_sequence_col()) {
continue;
if (s.ok() && need_to_check_delete_bitmap) {
// check if the key is already mark deleted
if (_tablet_meta->delete_bitmap().contains_agg_without_cache(
{loc.rowset_id, loc.segment_id, version}, loc.row_id)) {
// if has sequence col, we continue to compare the sequence_id of
// all rowsets, util we find an existing key.
if (with_seq_col) {
continue;
}
// The key is deleted, we need to break the loop and return
// KEY_NOT_FOUND.
break;
}
// The key is deleted, we don't need to search for it any more.
break;
}

// `st` is either OK or KEY_ALREADY_EXISTS now.
// for partial update, even if the key is already exists, we still need to
// read it's original values to keep all columns align.
Expand Down Expand Up @@ -661,7 +669,8 @@ Status BaseTablet::calc_segment_delete_bitmap(RowsetSharedPtr rowset,

RowsetSharedPtr rowset_find;
auto st = lookup_row_key(key, rowset_schema.get(), true, specified_rowsets, &loc,
dummy_version.first - 1, segment_caches, &rowset_find);
dummy_version.first - 1, segment_caches, &rowset_find, false,
is_partial_update);
bool expected_st = st.ok() || st.is<KEY_NOT_FOUND>() || st.is<KEY_ALREADY_EXISTS>();
// It's a defensive DCHECK, we need to exclude some common errors to avoid core-dump
// while stress test
Expand Down
3 changes: 2 additions & 1 deletion be/src/olap/base_tablet.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ class BaseTablet {
const std::vector<RowsetSharedPtr>& specified_rowsets,
RowLocation* row_location, uint32_t version,
std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches,
RowsetSharedPtr* rowset = nullptr, bool with_rowid = true);
RowsetSharedPtr* rowset = nullptr, bool with_rowid = true,
bool is_partial_update = false);

// calc delete bitmap when flush memtable, use a fake version to calc
// For example, cur max version is 5, and we use version 6 to calc but
Expand Down

0 comments on commit 3a5b803

Please sign in to comment.