Coverage Report

Created: 2026-08-20 03:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/rowid_fetcher.h
Line
Count
Source
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
#pragma once
19
20
#include <brpc/controller.h>
21
#include <bthread/countdown_event.h>
22
#include <gen_cpp/DataSinks_types.h>
23
#include <gen_cpp/internal_service.pb.h>
24
25
#include <condition_variable>
26
#include <functional>
27
#include <memory>
28
#include <mutex>
29
#include <semaphore>
30
#include <utility>
31
#include <vector>
32
33
#include "common/status.h"
34
#include "core/block/block.h"
35
#include "core/data_type/data_type.h"
36
#include "storage/id_manager.h"
37
#include "storage/tablet_info.h" // DorisNodesInfo
38
39
namespace doris {
40
41
class DorisNodesInfo;
42
class RuntimeState;
43
class TupleDescriptor;
44
class ScannerScheduler;
45
namespace io {
46
enum class FileCacheMissPolicy : uint8_t;
47
}
48
49
struct FileMapping;
50
struct SegKey;
51
struct SegItem;
52
struct HashOfSegKey;
53
struct IteratorKey;
54
struct IteratorItem;
55
struct HashOfIteratorKey;
56
57
0
inline void fetch_callback(bthread::CountdownEvent* counter) {
58
0
    Defer __defer([&] { counter->signal(); });
59
0
}
60
61
template <typename T>
62
class ColumnStr;
63
using ColumnString = ColumnStr<UInt32>;
64
class MutableBlock;
65
66
// fetch rows by global rowid
67
// tablet_id/rowset_name/segment_id/ordinal_id
68
69
struct FetchOption {
70
    TupleDescriptor* desc = nullptr;
71
    RuntimeState* runtime_state = nullptr;
72
    TFetchOption t_fetch_opt;
73
};
74
75
class RowIDFetcher {
76
public:
77
0
    RowIDFetcher(FetchOption fetch_opt) : _fetch_option(std::move(fetch_opt)) {}
78
    Status init();
79
    Status fetch(const ColumnPtr& row_ids, Block* block);
80
81
private:
82
    PMultiGetRequest _init_fetch_request(const ColumnString& row_ids) const;
83
    Status _merge_rpc_results(const PMultiGetRequest& request,
84
                              const std::vector<PMultiGetResponse>& rsps,
85
                              const std::vector<brpc::Controller>& cntls, Block* output_block,
86
                              std::vector<PRowLocation>* rows_id) const;
87
88
    std::vector<std::shared_ptr<PBackendService_Stub>> _stubs;
89
    FetchOption _fetch_option;
90
};
91
92
struct RowStoreReadStruct {
93
0
    RowStoreReadStruct(std::string& buffer) : row_store_buffer(buffer) {};
94
    std::string& row_store_buffer;
95
    DataTypeSerDeSPtrs serdes;
96
    std::unordered_map<uint32_t, uint32_t> col_uid_to_idx;
97
    std::vector<std::string> default_values;
98
};
99
100
class RowIdStorageReader {
101
public:
102
    //external profile info key.
103
    static const std::string ScannersRunningTimeProfile;
104
    static const std::string InitReaderAvgTimeProfile;
105
    static const std::string GetBlockAvgTimeProfile;
106
    static const std::string FileReadLinesProfile;
107
    static const std::string TopNLazyMaterializationSecondPhaseLocalIOCount;
108
    static const std::string TopNLazyMaterializationSecondPhaseLocalIOBytes;
109
    static const std::string TopNLazyMaterializationSecondPhaseRemoteIOCount;
110
    static const std::string TopNLazyMaterializationSecondPhaseRemoteIOBytes;
111
    static const std::string TopNLazyMaterializationSecondPhaseSkipCacheIOCount;
112
    static const std::string TopNLazyMaterializationSecondPhaseWriteCacheBytes;
113
    static const std::string TopNLazyMaterializationSecondPhaseLocalIOTime;
114
    static const std::string TopNLazyMaterializationSecondPhaseRemoteIOTime;
115
    static const std::string TopNLazyMaterializationSecondPhaseWriteCacheIOTime;
116
    static const std::string TopNLazyMaterializationSecondPhaseRowsRead;
117
    static const std::string TopNLazyMaterializationSecondPhaseSegmentsRead;
118
119
    static Status read_by_rowids(const PMultiGetRequest& request, PMultiGetResponse* response);
120
    static Status read_by_rowids(const PMultiGetRequestV2& request, PMultiGetResponseV2* response);
121
122
private:
123
    struct ExternalFetchStatistics;
124
125
    static Status read_doris_format_row(
126
            const std::shared_ptr<IdFileMap>& id_file_map,
127
            const std::shared_ptr<FileMapping>& file_mapping, const std::vector<uint32_t>& row_id,
128
            std::vector<SlotDescriptor>& slots, const TabletSchema& full_read_schema,
129
            RowStoreReadStruct& row_store_read_struct, OlapReaderStatistics& stats,
130
            int64_t* acquire_tablet_ms, int64_t* acquire_rowsets_ms, int64_t* acquire_segments_ms,
131
            int64_t* lookup_row_data_ms, std::unordered_map<SegKey, SegItem, HashOfSegKey>& seg_map,
132
            std::unordered_map<IteratorKey, IteratorItem, HashOfIteratorKey>& iterator_map,
133
            io::FileCacheMissPolicy file_cache_miss_policy, Block& result_block);
134
135
    static Status read_batch_doris_format_row(
136
            const PRequestBlockDesc& request_block_desc, std::shared_ptr<IdFileMap> id_file_map,
137
            std::vector<SlotDescriptor>& slots, const TUniqueId& query_id, Block& result_block,
138
            OlapReaderStatistics& stats, int64_t* acquire_tablet_ms, int64_t* acquire_rowsets_ms,
139
            int64_t* acquire_segments_ms, int64_t* lookup_row_data_ms,
140
            io::FileCacheMissPolicy file_cache_miss_policy);
141
142
    static Status read_batch_external_row(
143
            const uint64_t workload_group_id, const PRequestBlockDesc& request_block_desc,
144
            std::shared_ptr<IdFileMap> id_file_map, std::vector<SlotDescriptor>& slots,
145
            std::shared_ptr<FileMapping> first_file_mapping, const TUniqueId& query_id,
146
            Block& result_block, PRuntimeProfileTree* pprofile, int64_t* init_reader_avg_ms,
147
            int64_t* get_block_avg_ms, size_t* scan_range_cnt);
148
149
    static Status read_external_row_from_file_mapping(
150
            size_t idx, const std::multimap<segment_v2::rowid_t, size_t>& row_ids,
151
            const std::shared_ptr<FileMapping>& file_mapping,
152
            const std::vector<SlotDescriptor>& scan_slots, const TUniqueId& query_id,
153
            const std::shared_ptr<RuntimeState>& runtime_state, std::vector<Block>& scan_blocks,
154
            std::vector<std::pair<size_t, size_t>>& row_id_block_idx,
155
            std::vector<ExternalFetchStatistics>& fetch_statistics,
156
            const TFileScanRangeParams& rpc_scan_params,
157
            const std::unordered_map<std::string, int>& colname_to_slot_id,
158
            std::counting_semaphore<>& semaphore, TupleDescriptor& tuple_desc);
159
160
    static std::string source_column_key(const SlotDescriptor& slot, uint32_t column_idx);
161
162
    friend class RowIdStorageReaderTest;
163
164
    static Status submit_external_scan_tasks(
165
            ScannerScheduler* scheduler, std::counting_semaphore<>& semaphore, size_t task_count,
166
            const std::function<std::string(size_t)>& make_task_id,
167
            const std::function<Status(size_t)>& run_task);
168
169
    struct ExternalFetchStatistics {
170
        int64_t init_reader_ms = 0;
171
        int64_t get_block_ms = 0;
172
        std::string file_read_bytes;
173
        std::string file_read_times;
174
    };
175
};
176
177
template <typename Func>
178
68
auto scope_timer_run(Func fn, int64_t* cost) -> decltype(fn()) {
179
68
    MonotonicStopWatch watch;
180
68
    watch.start();
181
68
    auto res = fn();
182
68
    *cost += watch.elapsed_time() / 1000 / 1000;
183
68
    return res;
184
68
}
unity_9_cxx.cxx:_ZN5doris15scope_timer_runIZNS_11FileScanner21read_lines_from_rangeERKNS_14TFileRangeDescERKNSt7__cxx114listIlSaIlEEEPNS_5BlockERKNS_23ExternalFileMappingInfoEPlSG_E3$_0EEDTclfp_EET_SG_
Line
Count
Source
178
25
auto scope_timer_run(Func fn, int64_t* cost) -> decltype(fn()) {
179
25
    MonotonicStopWatch watch;
180
25
    watch.start();
181
25
    auto res = fn();
182
25
    *cost += watch.elapsed_time() / 1000 / 1000;
183
25
    return res;
184
25
}
unity_9_cxx.cxx:_ZN5doris15scope_timer_runIZNS_11FileScanner21read_lines_from_rangeERKNS_14TFileRangeDescERKNSt7__cxx114listIlSaIlEEEPNS_5BlockERKNS_23ExternalFileMappingInfoEPlSG_E3$_1EEDTclfp_EET_SG_
Line
Count
Source
178
25
auto scope_timer_run(Func fn, int64_t* cost) -> decltype(fn()) {
179
25
    MonotonicStopWatch watch;
180
25
    watch.start();
181
25
    auto res = fn();
182
25
    *cost += watch.elapsed_time() / 1000 / 1000;
183
25
    return res;
184
25
}
unity_8_cxx.cxx:_ZN5doris15scope_timer_runIZNS_18RowIdStorageReader14read_by_rowidsERKNS_16PMultiGetRequestEPNS_17PMultiGetResponseEE3$_1EEDTclfp_EET_Pl
Line
Count
Source
178
6
auto scope_timer_run(Func fn, int64_t* cost) -> decltype(fn()) {
179
6
    MonotonicStopWatch watch;
180
6
    watch.start();
181
6
    auto res = fn();
182
6
    *cost += watch.elapsed_time() / 1000 / 1000;
183
6
    return res;
184
6
}
unity_8_cxx.cxx:_ZN5doris15scope_timer_runIZNS_18RowIdStorageReader14read_by_rowidsERKNS_16PMultiGetRequestEPNS_17PMultiGetResponseEE3$_2EEDTclfp_EET_Pl
Line
Count
Source
178
6
auto scope_timer_run(Func fn, int64_t* cost) -> decltype(fn()) {
179
6
    MonotonicStopWatch watch;
180
6
    watch.start();
181
6
    auto res = fn();
182
6
    *cost += watch.elapsed_time() / 1000 / 1000;
183
6
    return res;
184
6
}
unity_8_cxx.cxx:_ZN5doris15scope_timer_runIZNS_18RowIdStorageReader14read_by_rowidsERKNS_16PMultiGetRequestEPNS_17PMultiGetResponseEE3$_4EEDTclfp_EET_Pl
Line
Count
Source
178
6
auto scope_timer_run(Func fn, int64_t* cost) -> decltype(fn()) {
179
6
    MonotonicStopWatch watch;
180
6
    watch.start();
181
6
    auto res = fn();
182
6
    *cost += watch.elapsed_time() / 1000 / 1000;
183
6
    return res;
184
6
}
Unexecuted instantiation: unity_8_cxx.cxx:_ZN5doris15scope_timer_runIZNS_18RowIdStorageReader14read_by_rowidsERKNS_16PMultiGetRequestEPNS_17PMultiGetResponseEE3$_5EEDTclfp_EET_Pl
Unexecuted instantiation: unity_8_cxx.cxx:_ZN5doris15scope_timer_runIZNS_18RowIdStorageReader23read_batch_external_rowEmRKNS_17PRequestBlockDescESt10shared_ptrINS_9IdFileMapEERSt6vectorINS_14SlotDescriptorESaIS9_EES5_INS_11FileMappingEERKNS_9TUniqueIdERNS_5BlockEPNS_19PRuntimeProfileTreeEPlSM_PmE3$_0EEDTclfp_EET_SM_
Unexecuted instantiation: unity_8_cxx.cxx:_ZN5doris15scope_timer_runIZNS_18RowIdStorageReader21read_doris_format_rowERKSt10shared_ptrINS_9IdFileMapEERKS2_INS_11FileMappingEERKSt6vectorIjSaIjEERSB_INS_14SlotDescriptorESaISG_EERKNS_12TabletSchemaERNS_18RowStoreReadStructERNS_20OlapReaderStatisticsEPlSR_SR_SR_RSt13unordered_mapINS_6SegKeyENS_7SegItemENS_12HashOfSegKeyESt8equal_toIST_ESaISt4pairIKST_SU_EEERSS_INS_11IteratorKeyENS_12IteratorItemENS_17HashOfIteratorKeyESW_IS14_ESaISY_IKS14_S15_EEENS_2io19FileCacheMissPolicyERNS_5BlockEE3$_1EEDTclfp_EET_SR_
Unexecuted instantiation: unity_8_cxx.cxx:_ZN5doris15scope_timer_runIZNS_18RowIdStorageReader21read_doris_format_rowERKSt10shared_ptrINS_9IdFileMapEERKS2_INS_11FileMappingEERKSt6vectorIjSaIjEERSB_INS_14SlotDescriptorESaISG_EERKNS_12TabletSchemaERNS_18RowStoreReadStructERNS_20OlapReaderStatisticsEPlSR_SR_SR_RSt13unordered_mapINS_6SegKeyENS_7SegItemENS_12HashOfSegKeyESt8equal_toIST_ESaISt4pairIKST_SU_EEERSS_INS_11IteratorKeyENS_12IteratorItemENS_17HashOfIteratorKeyESW_IS14_ESaISY_IKS14_S15_EEENS_2io19FileCacheMissPolicyERNS_5BlockEE3$_2EEDTclfp_EET_SR_
Unexecuted instantiation: unity_8_cxx.cxx:_ZN5doris15scope_timer_runIZNS_18RowIdStorageReader21read_doris_format_rowERKSt10shared_ptrINS_9IdFileMapEERKS2_INS_11FileMappingEERKSt6vectorIjSaIjEERSB_INS_14SlotDescriptorESaISG_EERKNS_12TabletSchemaERNS_18RowStoreReadStructERNS_20OlapReaderStatisticsEPlSR_SR_SR_RSt13unordered_mapINS_6SegKeyENS_7SegItemENS_12HashOfSegKeyESt8equal_toIST_ESaISt4pairIKST_SU_EEERSS_INS_11IteratorKeyENS_12IteratorItemENS_17HashOfIteratorKeyESW_IS14_ESaISY_IKS14_S15_EEENS_2io19FileCacheMissPolicyERNS_5BlockEE3$_3EEDTclfp_EET_SR_
Unexecuted instantiation: unity_8_cxx.cxx:_ZN5doris15scope_timer_runIZNS_18RowIdStorageReader21read_doris_format_rowERKSt10shared_ptrINS_9IdFileMapEERKS2_INS_11FileMappingEERKSt6vectorIjSaIjEERSB_INS_14SlotDescriptorESaISG_EERKNS_12TabletSchemaERNS_18RowStoreReadStructERNS_20OlapReaderStatisticsEPlSR_SR_SR_RSt13unordered_mapINS_6SegKeyENS_7SegItemENS_12HashOfSegKeyESt8equal_toIST_ESaISt4pairIKST_SU_EEERSS_INS_11IteratorKeyENS_12IteratorItemENS_17HashOfIteratorKeyESW_IS14_ESaISY_IKS14_S15_EEENS_2io19FileCacheMissPolicyERNS_5BlockEE3$_4EEDTclfp_EET_SR_
185
} // namespace doris