Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[opt](scan) Release instances of Segment to avoid consuming a large amount of memory in ParallelScannerBuilder (#44189) #44608

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions be/src/olap/parallel_scanner_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <shared_mutex>

#include "olap/rowset/beta_rowset.h"
#include "olap/segment_loader.h"
#include "pipeline/exec/olap_scan_operator.h"
#include "vec/exec/scan/new_olap_scanner.h"

Expand Down Expand Up @@ -56,21 +57,18 @@ Status ParallelScannerBuilder<ParentType>::_build_scanners_by_rowid(
auto rowset = reader->rowset();
const auto rowset_id = rowset->rowset_id();

DCHECK(_segment_cache_handles.contains(rowset_id));
auto& segment_cache_handle = _segment_cache_handles[rowset_id];
const auto& segments_rows = _all_segments_rows[rowset_id];

if (rowset->num_rows() == 0) {
continue;
}

const auto& segments = segment_cache_handle.get_segments();
int segment_start = 0;
auto split = RowSetSplits(reader->clone());

for (size_t i = 0; i != segments.size(); ++i) {
const auto& segment = segments[i];
for (size_t i = 0; i != segments_rows.size(); ++i) {
const size_t rows_of_segment = segments_rows[i];
RowRanges row_ranges;
const size_t rows_of_segment = segment->num_rows();
int64_t offset_in_segment = 0;

// try to split large segments into RowRanges
Expand Down Expand Up @@ -118,15 +116,15 @@ Status ParallelScannerBuilder<ParentType>::_build_scanners_by_rowid(
// The non-empty `row_ranges` means there are some rows left in this segment not added into `split`.
if (!row_ranges.is_empty()) {
DCHECK_GT(rows_collected, 0);
DCHECK_EQ(row_ranges.to(), segment->num_rows());
DCHECK_EQ(row_ranges.to(), rows_of_segment);
split.segment_row_ranges.emplace_back(std::move(row_ranges));
}
}

DCHECK_LE(rows_collected, _rows_per_scanner);
if (rows_collected > 0) {
split.segment_offsets.first = segment_start;
split.segment_offsets.second = segments.size();
split.segment_offsets.second = segments_rows.size();
DCHECK_GT(split.segment_offsets.second, split.segment_offsets.first);
DCHECK_EQ(split.segment_row_ranges.size(),
split.segment_offsets.second - split.segment_offsets.first);
Expand Down Expand Up @@ -176,10 +174,14 @@ Status ParallelScannerBuilder<ParentType>::_load() {
auto rowset = rs_split.rs_reader->rowset();
RETURN_IF_ERROR(rowset->load());
const auto rowset_id = rowset->rowset_id();
auto& segment_cache_handle = _segment_cache_handles[rowset_id];
SegmentCacheHandle segment_cache_handle;

RETURN_IF_ERROR(SegmentLoader::instance()->load_segments(
std::dynamic_pointer_cast<BetaRowset>(rowset), &segment_cache_handle, true));

for (const auto& segment : segment_cache_handle.get_segments()) {
_all_segments_rows[rowset_id].emplace_back(segment->num_rows());
}
_total_rows += rowset->num_rows();
}
}
Expand Down
2 changes: 1 addition & 1 deletion be/src/olap/parallel_scanner_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ParallelScannerBuilder {

size_t _rows_per_scanner {_min_rows_per_scanner};

std::map<RowsetId, SegmentCacheHandle> _segment_cache_handles;
std::map<RowsetId, std::vector<size_t>> _all_segments_rows;

std::shared_ptr<RuntimeProfile> _scanner_profile;
RuntimeState* _state;
Expand Down
Loading