Skip to content

Commit 7ccfb97

Browse files
committed
[feature](be) Extend range read-ahead to point queries
### What problem does this PR solve? Issue Number: None Related PR: apache#67292 Problem Summary: Short-circuit point queries read row-store data or missing column-store values through exact row IDs. Cold data pages previously followed the synchronous reader one page or column at a time. Create one request-scoped range-read context, initialize the row-store or all missing-column iterators against the shared read-ahead reader, submit the required data pages before decoding, and retain the existing row-cache and result-padding behavior. Read-ahead initialization or IO failure falls back to the original reader. Include the reusable row-id page preparation helpers, SegmentReadAhead exact-row entry point, and its tests in this point-query component. TopN second-stage integration remains unchanged from master and is outside this PR series. ### Release note Use query range read-ahead for point-query data pages when enable_query_read_ahead is enabled. ### Check List (For Author) - Test: Unit Test - ./run-be-ut.sh --run --filter=SegmentReadAheadTest.*:RowCacheTest.*:LookupConnectionCacheTest.* -j100 - Behavior changed: Yes; point-query data-page reads use the range read-ahead path when the feature is enabled. - Does this need documentation: Yes; covered by the read-ahead and hole-fill design document.
1 parent b9396d9 commit 7ccfb97

8 files changed

Lines changed: 561 additions & 32 deletions

File tree

‎be/src/service/point_query_executor.cpp‎

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
#include "storage/rowset/rowset_fwd.h"
5656
#include "storage/schema.h"
5757
#include "storage/segment/column_reader.h"
58+
#include "storage/segment/rowid_read_ahead.h"
59+
#include "storage/segment/segment_read_ahead.h"
5860
#include "storage/tablet/tablet_schema.h"
5961
#include "storage/utils.h"
6062
#include "util/defer_op.h"
@@ -528,6 +530,12 @@ Status PointQueryExecutor::_lookup_row_key() {
528530
Status PointQueryExecutor::_lookup_row_data() {
529531
// 3. get values
530532
SCOPED_TIMER(&_profile_metrics.lookup_data_ns);
533+
std::shared_ptr<io::FileRangeReadContext> read_ahead_context;
534+
if (segment_v2::SegmentReadAhead::enabled()) {
535+
auto* scheduler = ExecEnv::GetInstance()->file_range_read_scheduler();
536+
DORIS_CHECK(scheduler != nullptr);
537+
read_ahead_context = scheduler->create_context();
538+
}
531539
{
532540
auto result_columns_guard = _result_block->mutate_columns_scoped();
533541
MutableColumns& result_columns = result_columns_guard.mutable_columns();
@@ -552,10 +560,17 @@ Status PointQueryExecutor::_lookup_row_data() {
552560
io_ctx.reader_type = ReaderType::READER_QUERY;
553561
io_ctx.file_cache_stats = &_profile_metrics.read_stats.file_cache_stats;
554562
io_ctx.remote_scan_cache_write_limiter = _remote_scan_cache_write_limiter.get();
555-
RETURN_IF_ERROR(_tablet->lookup_row_data(
556-
_row_read_ctxs[i]._primary_key, _row_read_ctxs[i]._row_location.value(),
557-
*(_row_read_ctxs[i]._rowset_ptr), _profile_metrics.read_stats, value,
558-
use_row_cache, &io_ctx));
563+
if (read_ahead_context != nullptr) {
564+
RETURN_IF_ERROR(_tablet->lookup_row_data_with_read_ahead(
565+
_row_read_ctxs[i]._primary_key, _row_read_ctxs[i]._row_location.value(),
566+
*(_row_read_ctxs[i]._rowset_ptr), _profile_metrics.read_stats, value,
567+
use_row_cache, &io_ctx, read_ahead_context));
568+
} else {
569+
RETURN_IF_ERROR(_tablet->lookup_row_data(
570+
_row_read_ctxs[i]._primary_key, _row_read_ctxs[i]._row_location.value(),
571+
*(_row_read_ctxs[i]._rowset_ptr), _profile_metrics.read_stats, value,
572+
use_row_cache, &io_ctx));
573+
}
559574
// serialize value to block, currently only jsonb row format
560575
RETURN_IF_ERROR(JsonbSerializeUtil::jsonb_to_columns(
561576
_reusable->get_data_type_serdes(), value.data(), value.size(),
@@ -598,37 +613,80 @@ Status PointQueryExecutor::_lookup_row_data() {
598613
return seg->id() == row_loc.segment_id;
599614
});
600615
const auto& segment = *it;
601-
const auto tablet_schema = _tablet->tablet_schema();
602-
for (int cid : _reusable->missing_col_uids()) {
603-
int pos = _reusable->get_col_uid_to_idx().at(cid);
616+
StorageReadOptions read_ahead_options(_read_stats);
617+
read_ahead_options.use_page_cache = !config::disable_storage_page_cache;
618+
read_ahead_options.io_ctx = io_ctx;
619+
// Pre-read iterators need the same rowset constants as the synchronous path:
620+
// a version column in rowset [7-7] must produce 7, not its stored placeholder 0.
621+
read_ahead_options.version = rowset->version();
622+
read_ahead_options.commit_tso = rowset->commit_tso();
623+
std::unique_ptr<segment_v2::SegmentReadAhead> read_ahead;
624+
if (read_ahead_context != nullptr) {
625+
const auto status = segment_v2::SegmentReadAhead::create_for_query(
626+
segment->file_reader(), ExecEnv::GetInstance(), read_ahead_context,
627+
read_ahead_options, &read_ahead);
628+
if (!status.ok()) {
629+
LOG_EVERY_N(WARNING, 100)
630+
<< "failed to initialize point-read column read-ahead; use the "
631+
"original read path: "
632+
<< status;
633+
}
634+
}
635+
if (read_ahead != nullptr) {
636+
const auto missing_column_count = _reusable->missing_col_uids().size();
637+
std::vector<StorageReadOptions> read_options(missing_column_count,
638+
read_ahead_options);
639+
std::vector<std::unique_ptr<ColumnIterator>> iterators(missing_column_count);
640+
std::vector<segment_v2::RowIdColumnRead> column_reads;
641+
column_reads.reserve(missing_column_count);
642+
size_t read_index = 0;
643+
for (int cid : _reusable->missing_col_uids()) {
644+
int pos = _reusable->get_col_uid_to_idx().at(cid);
645+
SlotDescriptor* slot = _reusable->tuple_desc()->slots()[pos];
646+
column_reads.push_back({.slot = slot,
647+
.result = &result_columns[pos],
648+
.read_options = &read_options[read_index],
649+
.iterator = &iterators[read_index]});
650+
++read_index;
651+
}
604652
std::vector<segment_v2::rowid_t> row_ids {
605653
static_cast<segment_v2::rowid_t>(row_loc.row_id)};
606-
auto& column = result_columns[pos];
607-
std::unique_ptr<ColumnIterator> iter;
608-
SlotDescriptor* slot = _reusable->tuple_desc()->slots()[pos];
609-
int32_t index = slot->col_unique_id() >= 0
610-
? tablet_schema->field_index(slot->col_unique_id())
611-
: tablet_schema->field_index(slot->col_name());
612-
// A light schema change adds a column without writing anything, and
613-
// BaseTablet::_max_version_schema only moves forward on write, so until the
614-
// next load the tablet schema here does not have it. No segment holds it
615-
// either, so describe it from the slot and let the segment answer with its
616-
// default -- the same answer it gives once a load does bring the column in.
617-
TabletColumn column_awaiting_a_load;
618-
if (index < 0) {
619-
column_awaiting_a_load = ReadSchema::tablet_column_from_slot(*slot);
654+
RETURN_IF_ERROR(segment_v2::read_columns_by_rowids_with_read_ahead(
655+
*segment, *_tablet->tablet_schema(), row_ids, column_reads,
656+
*read_ahead));
657+
} else {
658+
const auto tablet_schema = _tablet->tablet_schema();
659+
for (int cid : _reusable->missing_col_uids()) {
660+
int pos = _reusable->get_col_uid_to_idx().at(cid);
661+
std::vector<segment_v2::rowid_t> row_ids {
662+
static_cast<segment_v2::rowid_t>(row_loc.row_id)};
663+
auto& column = result_columns[pos];
664+
std::unique_ptr<ColumnIterator> iter;
665+
SlotDescriptor* slot = _reusable->tuple_desc()->slots()[pos];
666+
int32_t index = slot->col_unique_id() >= 0
667+
? tablet_schema->field_index(slot->col_unique_id())
668+
: tablet_schema->field_index(slot->col_name());
669+
// A light schema change adds a column without writing anything, and
670+
// BaseTablet::_max_version_schema only moves forward on write, so until the
671+
// next load the tablet schema here does not have it. No segment holds it
672+
// either, so describe it from the slot and let the segment answer with its
673+
// default -- the same answer it gives once a load does bring the column in.
674+
TabletColumn column_awaiting_a_load;
675+
if (index < 0) {
676+
column_awaiting_a_load = ReadSchema::tablet_column_from_slot(*slot);
677+
}
678+
const TabletColumn& read_column =
679+
index >= 0 ? tablet_schema->column(index) : column_awaiting_a_load;
680+
StorageReadOptions storage_read_options(_read_stats);
681+
storage_read_options.io_ctx = io_ctx;
682+
// A point lookup bypasses TabletReader, so supply the rowset context here. For
683+
// example, reading __DORIS_VERSION_COL__ from rowset [7-7] must return 7
684+
// instead of its on-disk placeholder 0.
685+
storage_read_options.version = rowset->version();
686+
storage_read_options.commit_tso = rowset->commit_tso();
687+
RETURN_IF_ERROR(segment->seek_and_read_by_rowid(
688+
read_column, slot, row_ids, column, storage_read_options, iter));
620689
}
621-
const TabletColumn& read_column =
622-
index >= 0 ? tablet_schema->column(index) : column_awaiting_a_load;
623-
StorageReadOptions storage_read_options(_read_stats);
624-
storage_read_options.io_ctx = io_ctx;
625-
// A point lookup bypasses TabletReader, so supply the rowset context here. For
626-
// example, reading __DORIS_VERSION_COL__ from rowset [7-7] must return 7
627-
// instead of its on-disk placeholder 0.
628-
storage_read_options.version = rowset->version();
629-
storage_read_options.commit_tso = rowset->commit_tso();
630-
RETURN_IF_ERROR(segment->seek_and_read_by_rowid(
631-
read_column, slot, row_ids, column, storage_read_options, iter));
632690
}
633691
}
634692
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include "storage/segment/rowid_read_ahead.h"
19+
20+
#include <algorithm>
21+
#include <array>
22+
23+
#include "common/config.h"
24+
#include "runtime/descriptors.h"
25+
#include "storage/schema.h"
26+
#include "storage/segment/column_reader.h"
27+
#include "storage/segment/segment.h"
28+
#include "storage/segment/segment_read_ahead.h"
29+
#include "storage/tablet/tablet_schema.h"
30+
31+
namespace doris::segment_v2 {
32+
namespace {
33+
34+
Status prepare_column_iterator(Segment& segment, const TabletColumn& column,
35+
StorageReadOptions& read_options,
36+
std::unique_ptr<ColumnIterator>& iterator,
37+
SegmentReadAhead& read_ahead) {
38+
DORIS_CHECK(read_options.stats != nullptr);
39+
if (iterator == nullptr) {
40+
RETURN_IF_ERROR(segment.new_column_iterator(column, &iterator, &read_options));
41+
auto io_ctx = read_options.io_ctx;
42+
io_ctx.reader_type = ReaderType::READER_QUERY;
43+
io_ctx.file_cache_stats = &read_options.stats->file_cache_stats;
44+
ColumnIteratorOptions options {
45+
.use_page_cache = !config::disable_storage_page_cache,
46+
.file_reader = read_ahead.file_reader().get(),
47+
.stats = read_options.stats,
48+
.io_ctx = io_ctx,
49+
};
50+
RETURN_IF_ERROR(iterator->init(options));
51+
}
52+
return Status::OK();
53+
}
54+
55+
Status prepare_slot_iterator(Segment& segment, const TabletSchema& schema, RowIdColumnRead& column,
56+
SegmentReadAhead& read_ahead, ColumnIterator** prepared) {
57+
DORIS_CHECK(column.slot != nullptr);
58+
DORIS_CHECK(column.read_options != nullptr);
59+
DORIS_CHECK(column.iterator != nullptr);
60+
DORIS_CHECK(prepared != nullptr);
61+
*prepared = nullptr;
62+
63+
// Variant columns need Segment's existing path resolution and casting logic. Leaving their
64+
// iterator empty makes the subsequent seek_and_read_by_rowid() follow that path unchanged.
65+
if (!column.slot->column_paths().empty()) {
66+
return Status::OK();
67+
}
68+
const int index = column.slot->col_unique_id() >= 0
69+
? schema.field_index(column.slot->col_unique_id())
70+
: schema.field_index(column.slot->col_name());
71+
if (index < 0) {
72+
return Status::OK();
73+
}
74+
const auto& tablet_column = schema.column(index);
75+
if (tablet_column.type() == FieldType::OLAP_FIELD_TYPE_VARIANT) {
76+
return Status::OK();
77+
}
78+
79+
RETURN_IF_ERROR(prepare_column_iterator(segment, tablet_column, *column.read_options,
80+
*column.iterator, read_ahead));
81+
*prepared = column.iterator->get();
82+
return Status::OK();
83+
}
84+
85+
} // namespace
86+
87+
Status prepare_columns_by_rowids_with_read_ahead(Segment& segment, const TabletSchema& schema,
88+
const std::vector<uint32_t>& row_ids,
89+
std::span<RowIdColumnRead> columns,
90+
SegmentReadAhead& read_ahead) {
91+
if (row_ids.empty()) {
92+
return Status::OK();
93+
}
94+
95+
std::vector<ColumnIterator*> prepared;
96+
prepared.reserve(columns.size());
97+
for (auto& column : columns) {
98+
ColumnIterator* iterator = nullptr;
99+
RETURN_IF_ERROR(prepare_slot_iterator(segment, schema, column, read_ahead, &iterator));
100+
if (iterator != nullptr) {
101+
prepared.push_back(iterator);
102+
}
103+
}
104+
if (!prepared.empty()) {
105+
static_cast<void>(read_ahead.prefetch_by_rowids(row_ids.data(), row_ids.size(), prepared));
106+
}
107+
108+
return Status::OK();
109+
}
110+
111+
Status read_columns_by_rowids_with_read_ahead(Segment& segment, const TabletSchema& schema,
112+
const std::vector<uint32_t>& row_ids,
113+
std::span<RowIdColumnRead> columns,
114+
SegmentReadAhead& read_ahead) {
115+
RETURN_IF_ERROR(prepare_columns_by_rowids_with_read_ahead(segment, schema, row_ids, columns,
116+
read_ahead));
117+
118+
for (auto& column : columns) {
119+
DORIS_CHECK(column.result != nullptr);
120+
const int index = column.slot->col_unique_id() >= 0
121+
? schema.field_index(column.slot->col_unique_id())
122+
: schema.field_index(column.slot->col_name());
123+
// A newly added column can be absent from the tablet schema until the next load.
124+
// Preserve the point-query path's slot-derived default in that interval.
125+
TabletColumn column_awaiting_a_load;
126+
if (index < 0) {
127+
column_awaiting_a_load = ReadSchema::tablet_column_from_slot(*column.slot);
128+
}
129+
const TabletColumn& read_column =
130+
index >= 0 ? schema.column(index) : column_awaiting_a_load;
131+
RETURN_IF_ERROR(segment.seek_and_read_by_rowid(read_column, column.slot, row_ids,
132+
*column.result, *column.read_options,
133+
*column.iterator));
134+
}
135+
return Status::OK();
136+
}
137+
138+
Status read_column_by_rowids_with_read_ahead(Segment& segment, const TabletColumn& column,
139+
const std::vector<uint32_t>& row_ids,
140+
MutableColumnPtr& result,
141+
StorageReadOptions& read_options,
142+
std::unique_ptr<ColumnIterator>& iterator,
143+
SegmentReadAhead& read_ahead) {
144+
if (row_ids.empty()) {
145+
return Status::OK();
146+
}
147+
DORIS_CHECK(std::is_sorted(row_ids.begin(), row_ids.end()));
148+
DORIS_CHECK(std::adjacent_find(row_ids.begin(), row_ids.end()) == row_ids.end());
149+
RETURN_IF_ERROR(prepare_column_iterator(segment, column, read_options, iterator, read_ahead));
150+
std::array<ColumnIterator*, 1> columns {iterator.get()};
151+
static_cast<void>(read_ahead.prefetch_by_rowids(row_ids.data(), row_ids.size(), columns));
152+
return iterator->read_by_rowids(row_ids.data(), row_ids.size(), result);
153+
}
154+
155+
} // namespace doris::segment_v2

0 commit comments

Comments
 (0)