Repository navigation
[improvement](segment) reduce memory usage when open segments - #46570
Conversation
|
Thank you for your contribution to Apache Doris. Please clearly describe your PR:
|
e971b1a to
36370c7
Compare
|
run buildall |
061c170 to
a2e2bcf
Compare
|
run buildall |
a2e2bcf to
6921b73
Compare
|
run buildall |
6921b73 to
842ce50
Compare
|
run buildall |
TPC-H: Total hot run time: 34231 ms |
| if (seg_start == seg_end) { | ||
| seg_start = 0; | ||
| seg_end = segments.size(); | ||
| _segments_rows)); |
There was a problem hiding this comment.
把这个 _segment_rows 属性去掉吧,实际这个就这里用了一次,我们如果缓存下来,我怕有问题
| if (seg_start == seg_end) { | ||
| seg_start = 0; | ||
| seg_end = segments.size(); | ||
| _segments_rows)); |
There was a problem hiding this comment.
should also check rowid_conversion != nullptr, to avoid core
| // [pair.first, pair.second) segment in rs_reader, only effective in dup key | ||
| // and pipeline | ||
| std::pair<int, int> segment_offsets; | ||
| std::pair<int64_t, int64_t> segment_offsets; |
There was a problem hiding this comment.
remove get_segment_num_rows method from this class
| const auto& tmp_segments = segment_cache_handle.get_segments(); | ||
| _segments_rows[i] = tmp_segments[0]->num_rows(); | ||
| if (i >= seg_start && i < seg_end) { | ||
| segments[i] = tmp_segments[0]; |
| seg_end = segment_count; | ||
| } | ||
| if (_read_context->record_rowids) { | ||
| _segments_rows.resize(segment_count); |
There was a problem hiding this comment.
这里需要加一个comment,解释一下什么时候会走到这里
| auto segment_count = _rowset->num_segments(); | ||
| std::vector<segment_v2::SegmentSharedPtr> segments(segment_count); | ||
| auto [seg_start, seg_end] = _segment_offsets; | ||
| if (seg_start == seg_end) { |
There was a problem hiding this comment.
这里也需要加一下注释,为什么== 的时候end =segment_count 了
TPC-DS: Total hot run time: 198259 ms |
ClickBench: Total hot run time: 31.54 s |
aedcfb8 to
bfdaa6f
Compare
|
run buildall |
TPC-H: Total hot run time: 32928 ms |
TPC-DS: Total hot run time: 189023 ms |
ClickBench: Total hot run time: 31.11 s |
bfdaa6f to
df41457
Compare
|
run buildall |
TPC-H: Total hot run time: 32999 ms |
TPC-DS: Total hot run time: 195802 ms |
ClickBench: Total hot run time: 32.19 s |
|
TeamCity be ut coverage result: |
| RETURN_IF_ERROR(_read_context->rowid_conversion->init_segment_map(rowset()->rowset_id(), | ||
| segment_num_rows)); | ||
| } | ||
| const bool use_lazy_init_iterators = !_is_merge_iterator(); |
| return file_cache_key_from_path(seg_path).to_string(); | ||
| } | ||
|
|
||
| Status Segment::get_num_rows(io::FileSystemSPtr fs, const std::string& path, int64_t tablet_id, |
TPC-H: Total hot run time: 9985 ms |
9688288 to
ca2ea85
Compare
ca2ea85 to
edb84dc
Compare
|
run buildall |
|
PR approved by at least one committer and no changes requested. |
|
PR approved by anyone and no changes requested. |
TPC-H: Total hot run time: 32583 ms |
TPC-DS: Total hot run time: 188004 ms |
ClickBench: Total hot run time: 31.55 s |
|
TeamCity be ut coverage result: |
### What problem does this PR solve? Issue Number: close #xxx Related PR: #xxx Problem Summary: When there are a lot of segments in one rowset, it will consume plenty of memory if open all the segments all at once. This PR open segments one by one and release the `Segment` object immediately if it's not need to be kept for later use, thus reduce memory footprints dramatically.
…#46570) Issue Number: close #xxx Related PR: #xxx Problem Summary: When there are a lot of segments in one rowset, it will consume plenty of memory if open all the segments all at once. This PR open segments one by one and release the `Segment` object immediately if it's not need to be kept for later use, thus reduce memory footprints dramatically.
Issue Number: close #xxx Related PR: #xxx Problem Summary: When there are a lot of segments in one rowset, it will consume plenty of memory if open all the segments all at once. This PR open segments one by one and release the `Segment` object immediately if it's not need to be kept for later use, thus reduce memory footprints dramatically.
…taRowset (#68675) Related PR: #46570 Problem Summary: `BetaRowset::get_segment_num_rows()` loads segment rows via `DorisCallOnce`, which also caches a failed `Status`. Since a `BetaRowset` lives as long as its tablet version, a transient IO error (e.g. S3 SlowDown) on the first load makes every later caller fail with the same stale error until BE restarts. MOW compaction, which needs segment rows for rowid conversion, then keeps failing on that tablet. Fix: replace `DorisCallOnce` with a double-checked atomic flag and a mutex. The flag is only set after a successful load, so a failed load is retried by the next caller, while the loaded rows are still read without lock. ### Release note Fix compaction repeatedly failing with the same stale error after a transient IO error while loading segment rows.
…taRowset (#68675) Related PR: #46570 Problem Summary: `BetaRowset::get_segment_num_rows()` loads segment rows via `DorisCallOnce`, which also caches a failed `Status`. Since a `BetaRowset` lives as long as its tablet version, a transient IO error (e.g. S3 SlowDown) on the first load makes every later caller fail with the same stale error until BE restarts. MOW compaction, which needs segment rows for rowid conversion, then keeps failing on that tablet. Fix: replace `DorisCallOnce` with a double-checked atomic flag and a mutex. The flag is only set after a successful load, so a failed load is retried by the next caller, while the loaded rows are still read without lock. ### Release note Fix compaction repeatedly failing with the same stale error after a transient IO error while loading segment rows.
…69797) ### What problem does this PR solve? When one load flushes several memtables, its rowset has segments that overlap on keys, and the rowset reader merges them. Four bugs on that merge returned wrong rows; horizontal compaction reads through the same merge, so bugs 1 to 3 also wrote wrong rows into the compacted rowset. ```sql -- curl -X POST http://<be_host>:<be_webserver_port>/api/debug_point/add/MemTable.need_flush SET parallel_pipeline_task_num = 1; SET batch_size = 8192; CREATE TABLE mor_unique (k INT, v INT) UNIQUE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1 PROPERTIES ("replication_num" = "1", "disable_auto_compaction" = "true", "enable_unique_key_merge_on_write" = "false"); CREATE TABLE agg_sum (k INT, s BIGINT SUM) AGGREGATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1 PROPERTIES ("replication_num" = "1", "disable_auto_compaction" = "true"); CREATE TABLE seq_map (k INT, v1 INT, s1 BIGINT, v2 INT, s2 BIGINT) UNIQUE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1 PROPERTIES ("replication_num" = "1", "disable_auto_compaction" = "true", "enable_unique_key_merge_on_write" = "false", "light_schema_change" = "true", "sequence_mapping.s1" = "v1", "sequence_mapping.s2" = "v2"); CREATE TABLE mow_topn (k1 INT, k2 INT, v INT) UNIQUE KEY(k1, k2) DISTRIBUTED BY HASH(k1) BUCKETS 1 PROPERTIES ("replication_num" = "1", "disable_auto_compaction" = "true", "enable_unique_key_merge_on_write" = "true"); -- older write: v = k; newer write: v = 100 + k INSERT INTO mor_unique SELECT number % 4, number % 4 + if(number < 8192, 0, 100) FROM numbers("number" = "16384"); -- each write folds into s = 2048 per key, so every key sums to 4096 INSERT INTO agg_sum SELECT number % 4, 1 FROM numbers("number" = "16384"); -- older write: v1 = v2 = k, s1 = 2, s2 = 1; newer write: v1 = v2 = 100 + k, s1 = 1, s2 = 2 INSERT INTO seq_map SELECT number % 4, number % 4 + if(number < 8192, 0, 100), if(number < 8192, 2, 1), number % 4 + if(number < 8192, 0, 100), if(number < 8192, 1, 2) FROM numbers("number" = "16384"); -- older write: k2 = k1; newer write: k2 = k1 + 8192, so every k1 is in two keys INSERT INTO mow_topn SELECT number % 8192, number, number FROM numbers("number" = "16384"); ``` Results before the fix are from master `31a036fbcda`, after it from this PR. **1. A merge-on-read UNIQUE KEY table returns the older value (REPLACE columns of AGGREGATE KEY too):** the segment merge picks the newer row by `data_id()`, which `LazyInitSegmentIterator` (used for merges since #46570) answered with 0. ```sql SELECT k, v FROM mor_unique ORDER BY k; -- before: (0, 0) (1, 1) (2, 2) (3, 3) -- after: (0, 100) (1, 101) (2, 102) (3, 103) ``` **2. An AGGREGATE KEY table returns a key twice when a Block boundary cuts its rows (sequence-mapping tables too):** `Level0Iterator` took the same-key flag before loading the next Block, so the first row of every Block lost it. ```sql -- the segment merge yields k = 0 0 1 1 2 2 3 3; Blocks of 3 rows cut key 1 SELECT /*+ SET_VAR(batch_size = 3) */ k, s FROM agg_sum ORDER BY k; -- before: (0, 4096) (1, 2048) (1, 2048) (2, 4096) (3, 4096) -- after: (0, 4096) (1, 4096) (2, 4096) (3, 4096) ``` **3. A sequence-mapping table loses a column group:** the segment merge kept one row per key, while each column group must come from the row with the largest value of its own sequence column (#54936 handled only the merge across rowsets). ```sql -- v1 must come from the older write (s1 = 2), v2 from the newer one (s2 = 2) SELECT k, v1, s1, v2, s2 FROM seq_map ORDER BY k; -- before: (0, 0, 2, 0, 1) (1, 1, 2, 1, 1) (2, 2, 2, 2, 1) (3, 3, 2, 3, 1) -- after: (0, 0, 2, 100, 2) (1, 1, 2, 101, 2) (2, 2, 2, 102, 2) (3, 3, 2, 103, 2) ``` **4. `ORDER BY <key prefix> LIMIT n` on a merge-on-write UNIQUE KEY table misses rows:** the storage TopN merges segments by the ORDER BY columns, on which different keys compare equal, but still dropped "same key" rows as older versions (#15663). ```sql SELECT * FROM (SELECT k1, k2 FROM mow_topn ORDER BY k1 LIMIT 4) t ORDER BY k1, k2; -- before: (0, 0) (1, 1) (2, 2) (3, 3) -- after: (0, 0) (0, 8192) (1, 1) (1, 8193) ``` Fixes: (1) `LazyInitSegmentIterator::data_id()` answers its segment id; (2) `Level0Iterator::next()` takes the same-key flag after `refresh_current_row()`; (3) `TabletReader` keeps every row of a key for a sequence-mapping table, and `BlockReader` merges the column groups; (4) `BetaRowsetReader` drops older rows only when it merges by the whole key (a merge-on-write table's delete bitmap already filters them). Issue Number: close #xxx Related PR: #xxx Problem Summary: ### Release note None ### Check List (For Author) - Test <!-- At least one of them must be included. --> - [ ] Regression test - [ ] Unit Test - [ ] Manual test (add detailed scripts or steps below) - [ ] No need to test or manual test. Explain why: - [ ] This is a refactor/code format and no logic has been changed. - [ ] Previous test can cover this change. - [ ] No code files have been changed. - [ ] Other reason <!-- Add your reason? --> - Behavior changed: - [ ] No. - [ ] Yes. <!-- Explain the behavior change --> - Does this need documentation? - [ ] No. - [ ] Yes. <!-- Add document PR link here. eg: apache/doris-website#1214 --> ### Check List (For Reviewer who merge this PR) - [ ] Confirm the release note - [ ] Confirm test cases - [ ] Confirm document - [ ] Add branch pick label <!-- Add branch pick label that this PR should merge into --> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
What problem does this PR solve?
Issue Number: close #xxx
Related PR: #xxx
Problem Summary:
When there are a lot of segments in one rowset, it will consume plenty of memory if open all the segments all at once. This PR open segments one by one and release the
Segmentobject immediately if it's not need to be kept for later use, thus reduce memory footprints dramatically.Release note
None
Check List (For Author)
Test
Behavior changed:
Does this need documentation?
Check List (For Reviewer who merge this PR)