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