/root/doris/be/src/exec/rowid_fetcher.cpp
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 | | #include "exec/rowid_fetcher.h" |
19 | | |
20 | | #include <brpc/callback.h> |
21 | | #include <butil/endpoint.h> |
22 | | #include <fmt/format.h> |
23 | | #include <gen_cpp/data.pb.h> |
24 | | #include <gen_cpp/internal_service.pb.h> |
25 | | #include <gen_cpp/olap_file.pb.h> |
26 | | #include <gen_cpp/types.pb.h> |
27 | | #include <glog/logging.h> |
28 | | #include <stddef.h> |
29 | | #include <stdint.h> |
30 | | |
31 | | #include <algorithm> |
32 | | #include <cstdint> |
33 | | #include <memory> |
34 | | #include <ostream> |
35 | | #include <string> |
36 | | #include <unordered_map> |
37 | | #include <utility> |
38 | | #include <vector> |
39 | | |
40 | | #include "bthread/countdown_event.h" |
41 | | #include "cloud/cloud_storage_engine.h" |
42 | | #include "cloud/cloud_tablet.h" |
43 | | #include "cloud/cloud_tablet_mgr.h" |
44 | | #include "cloud/config.h" |
45 | | #include "common/config.h" |
46 | | #include "common/consts.h" |
47 | | #include "common/exception.h" |
48 | | #include "common/signal_handler.h" |
49 | | #include "exec/tablet_info.h" // DorisNodesInfo |
50 | | #include "olap/olap_common.h" |
51 | | #include "olap/rowset/beta_rowset.h" |
52 | | #include "olap/storage_engine.h" |
53 | | #include "olap/tablet_fwd.h" |
54 | | #include "olap/tablet_manager.h" |
55 | | #include "olap/tablet_schema.h" |
56 | | #include "olap/utils.h" |
57 | | #include "runtime/descriptors.h" |
58 | | #include "runtime/exec_env.h" // ExecEnv |
59 | | #include "runtime/fragment_mgr.h" // FragmentMgr |
60 | | #include "runtime/runtime_state.h" // RuntimeState |
61 | | #include "runtime/types.h" |
62 | | #include "runtime/workload_group/workload_group_manager.h" |
63 | | #include "util/brpc_client_cache.h" // BrpcClientCache |
64 | | #include "util/defer_op.h" |
65 | | #include "vec/columns/column.h" |
66 | | #include "vec/columns/column_nullable.h" |
67 | | #include "vec/columns/column_string.h" |
68 | | #include "vec/common/assert_cast.h" |
69 | | #include "vec/common/string_ref.h" |
70 | | #include "vec/core/block.h" // Block |
71 | | #include "vec/data_types/data_type_factory.hpp" |
72 | | #include "vec/data_types/data_type_struct.h" |
73 | | #include "vec/data_types/serde/data_type_serde.h" |
74 | | #include "vec/exec/format/orc/vorc_reader.h" |
75 | | #include "vec/exec/format/parquet/vparquet_reader.h" |
76 | | #include "vec/exec/scan/file_scanner.h" |
77 | | #include "vec/functions/function_helpers.h" |
78 | | #include "vec/jsonb/serialize.h" |
79 | | |
80 | | namespace doris { |
81 | | |
82 | | #include "common/compile_check_begin.h" |
83 | | |
84 | 0 | Status RowIDFetcher::init() { |
85 | 0 | DorisNodesInfo nodes_info; |
86 | 0 | nodes_info.setNodes(_fetch_option.t_fetch_opt.nodes_info); |
87 | 0 | for (auto [node_id, node_info] : nodes_info.nodes_info()) { |
88 | 0 | auto client = ExecEnv::GetInstance()->brpc_internal_client_cache()->get_client( |
89 | 0 | node_info.host, node_info.brpc_port); |
90 | 0 | if (!client) { |
91 | 0 | LOG(WARNING) << "Get rpc stub failed, host=" << node_info.host |
92 | 0 | << ", port=" << node_info.brpc_port; |
93 | 0 | return Status::InternalError("RowIDFetcher failed to init rpc client, host={}, port={}", |
94 | 0 | node_info.host, node_info.brpc_port); |
95 | 0 | } |
96 | 0 | _stubs.push_back(client); |
97 | 0 | } |
98 | 0 | return Status::OK(); |
99 | 0 | } |
100 | | |
101 | 0 | PMultiGetRequest RowIDFetcher::_init_fetch_request(const vectorized::ColumnString& row_locs) const { |
102 | 0 | PMultiGetRequest mget_req; |
103 | 0 | _fetch_option.desc->to_protobuf(mget_req.mutable_desc()); |
104 | 0 | for (SlotDescriptor* slot : _fetch_option.desc->slots()) { |
105 | | // ignore rowid |
106 | 0 | if (slot->col_name() == BeConsts::ROWID_COL) { |
107 | 0 | continue; |
108 | 0 | } |
109 | 0 | slot->to_protobuf(mget_req.add_slots()); |
110 | 0 | } |
111 | 0 | for (size_t i = 0; i < row_locs.size(); ++i) { |
112 | 0 | PRowLocation row_loc; |
113 | 0 | StringRef row_id_rep = row_locs.get_data_at(i); |
114 | | // TODO: When transferring data between machines with different byte orders (endianness), |
115 | | // not performing proper handling may lead to issues in parsing and exchanging the data. |
116 | 0 | auto location = reinterpret_cast<const GlobalRowLoacation*>(row_id_rep.data); |
117 | 0 | row_loc.set_tablet_id(location->tablet_id); |
118 | 0 | row_loc.set_rowset_id(location->row_location.rowset_id.to_string()); |
119 | 0 | row_loc.set_segment_id(location->row_location.segment_id); |
120 | 0 | row_loc.set_ordinal_id(location->row_location.row_id); |
121 | 0 | *mget_req.add_row_locs() = std::move(row_loc); |
122 | 0 | } |
123 | | // Set column desc |
124 | 0 | for (const TColumn& tcolumn : _fetch_option.t_fetch_opt.column_desc) { |
125 | 0 | TabletColumn column(tcolumn); |
126 | 0 | column.to_schema_pb(mget_req.add_column_desc()); |
127 | 0 | } |
128 | 0 | PUniqueId& query_id = *mget_req.mutable_query_id(); |
129 | 0 | query_id.set_hi(_fetch_option.runtime_state->query_id().hi); |
130 | 0 | query_id.set_lo(_fetch_option.runtime_state->query_id().lo); |
131 | 0 | mget_req.set_be_exec_version(_fetch_option.runtime_state->be_exec_version()); |
132 | 0 | mget_req.set_fetch_row_store(_fetch_option.t_fetch_opt.fetch_row_store); |
133 | 0 | return mget_req; |
134 | 0 | } |
135 | | |
136 | 0 | static void fetch_callback(bthread::CountdownEvent* counter) { |
137 | 0 | Defer __defer([&] { counter->signal(); }); |
138 | 0 | } |
139 | | |
140 | | Status RowIDFetcher::_merge_rpc_results(const PMultiGetRequest& request, |
141 | | const std::vector<PMultiGetResponse>& rsps, |
142 | | const std::vector<brpc::Controller>& cntls, |
143 | | vectorized::Block* output_block, |
144 | 0 | std::vector<PRowLocation>* rows_id) const { |
145 | 0 | output_block->clear(); |
146 | 0 | for (const auto& cntl : cntls) { |
147 | 0 | if (cntl.Failed()) { |
148 | 0 | LOG(WARNING) << "Failed to fetch meet rpc error:" << cntl.ErrorText() |
149 | 0 | << ", host:" << cntl.remote_side(); |
150 | 0 | return Status::InternalError(cntl.ErrorText()); |
151 | 0 | } |
152 | 0 | } |
153 | 0 | vectorized::DataTypeSerDeSPtrs serdes; |
154 | 0 | std::unordered_map<uint32_t, uint32_t> col_uid_to_idx; |
155 | 0 | std::vector<std::string> default_values; |
156 | 0 | default_values.resize(_fetch_option.desc->slots().size()); |
157 | 0 | auto merge_function = [&](const PMultiGetResponse& resp) { |
158 | 0 | Status st(Status::create(resp.status())); |
159 | 0 | if (!st.ok()) { |
160 | 0 | LOG(WARNING) << "Failed to fetch " << st.to_string(); |
161 | 0 | return st; |
162 | 0 | } |
163 | 0 | for (const PRowLocation& row_id : resp.row_locs()) { |
164 | 0 | rows_id->push_back(row_id); |
165 | 0 | } |
166 | | // Merge binary rows |
167 | 0 | if (request.fetch_row_store()) { |
168 | 0 | CHECK(resp.row_locs().size() == resp.binary_row_data_size()); |
169 | 0 | if (output_block->is_empty_column()) { |
170 | 0 | *output_block = vectorized::Block(_fetch_option.desc->slots(), 1); |
171 | 0 | } |
172 | 0 | if (serdes.empty() && col_uid_to_idx.empty()) { |
173 | 0 | serdes = vectorized::create_data_type_serdes(_fetch_option.desc->slots()); |
174 | 0 | for (int i = 0; i < _fetch_option.desc->slots().size(); ++i) { |
175 | 0 | col_uid_to_idx[_fetch_option.desc->slots()[i]->col_unique_id()] = i; |
176 | 0 | default_values[i] = _fetch_option.desc->slots()[i]->col_default_value(); |
177 | 0 | } |
178 | 0 | } |
179 | 0 | for (int i = 0; i < resp.binary_row_data_size(); ++i) { |
180 | 0 | RETURN_IF_ERROR(vectorized::JsonbSerializeUtil::jsonb_to_block( |
181 | 0 | serdes, resp.binary_row_data(i).data(), resp.binary_row_data(i).size(), |
182 | 0 | col_uid_to_idx, *output_block, default_values, {})); |
183 | 0 | } |
184 | 0 | return Status::OK(); |
185 | 0 | } |
186 | | // Merge partial blocks |
187 | 0 | vectorized::Block partial_block; |
188 | 0 | RETURN_IF_ERROR(partial_block.deserialize(resp.block())); |
189 | 0 | if (partial_block.is_empty_column()) { |
190 | 0 | return Status::OK(); |
191 | 0 | } |
192 | 0 | CHECK(resp.row_locs().size() == partial_block.rows()); |
193 | 0 | if (output_block->is_empty_column()) { |
194 | 0 | output_block->swap(partial_block); |
195 | 0 | } else if (partial_block.columns() != output_block->columns()) { |
196 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR>( |
197 | 0 | "Merge block not match, self:[{}], input:[{}], ", output_block->dump_types(), |
198 | 0 | partial_block.dump_types()); |
199 | 0 | } else { |
200 | 0 | for (int i = 0; i < output_block->columns(); ++i) { |
201 | 0 | output_block->get_by_position(i).column->assume_mutable()->insert_range_from( |
202 | 0 | *partial_block.get_by_position(i) |
203 | 0 | .column->convert_to_full_column_if_const() |
204 | 0 | .get(), |
205 | 0 | 0, partial_block.rows()); |
206 | 0 | } |
207 | 0 | } |
208 | 0 | return Status::OK(); |
209 | 0 | }; |
210 | |
|
211 | 0 | for (const auto& resp : rsps) { |
212 | 0 | RETURN_IF_ERROR(merge_function(resp)); |
213 | 0 | } |
214 | 0 | return Status::OK(); |
215 | 0 | } |
216 | | |
217 | 0 | bool _has_char_type(const vectorized::DataTypePtr& type) { |
218 | 0 | switch (type->get_primitive_type()) { |
219 | 0 | case TYPE_CHAR: { |
220 | 0 | return true; |
221 | 0 | } |
222 | 0 | case TYPE_ARRAY: { |
223 | 0 | const auto* arr_type = |
224 | 0 | assert_cast<const vectorized::DataTypeArray*>(remove_nullable(type).get()); |
225 | 0 | return _has_char_type(arr_type->get_nested_type()); |
226 | 0 | } |
227 | 0 | case TYPE_MAP: { |
228 | 0 | const auto* map_type = |
229 | 0 | assert_cast<const vectorized::DataTypeMap*>(remove_nullable(type).get()); |
230 | 0 | return _has_char_type(map_type->get_key_type()) || |
231 | 0 | _has_char_type(map_type->get_value_type()); |
232 | 0 | } |
233 | 0 | case TYPE_STRUCT: { |
234 | 0 | const auto* struct_type = |
235 | 0 | assert_cast<const vectorized::DataTypeStruct*>(remove_nullable(type).get()); |
236 | 0 | return std::any_of( |
237 | 0 | struct_type->get_elements().begin(), struct_type->get_elements().end(), |
238 | 0 | [&](const vectorized::DataTypePtr& dt) -> bool { return _has_char_type(dt); }); |
239 | 0 | } |
240 | 0 | default: |
241 | 0 | return false; |
242 | 0 | } |
243 | 0 | } |
244 | | |
245 | | Status RowIDFetcher::fetch(const vectorized::ColumnPtr& column_row_ids, |
246 | 0 | vectorized::Block* res_block) { |
247 | 0 | CHECK(!_stubs.empty()); |
248 | 0 | PMultiGetRequest mget_req = _init_fetch_request(assert_cast<const vectorized::ColumnString&>( |
249 | 0 | *vectorized::remove_nullable(column_row_ids).get())); |
250 | 0 | std::vector<PMultiGetResponse> resps(_stubs.size()); |
251 | 0 | std::vector<brpc::Controller> cntls(_stubs.size()); |
252 | 0 | bthread::CountdownEvent counter(cast_set<int>(_stubs.size())); |
253 | 0 | for (size_t i = 0; i < _stubs.size(); ++i) { |
254 | 0 | cntls[i].set_timeout_ms(_fetch_option.runtime_state->execution_timeout() * 1000); |
255 | 0 | auto callback = brpc::NewCallback(fetch_callback, &counter); |
256 | 0 | _stubs[i]->multiget_data(&cntls[i], &mget_req, &resps[i], callback); |
257 | 0 | } |
258 | 0 | counter.wait(); |
259 | | |
260 | | // Merge |
261 | 0 | std::vector<PRowLocation> rows_locs; |
262 | 0 | rows_locs.reserve(rows_locs.size()); |
263 | 0 | RETURN_IF_ERROR(_merge_rpc_results(mget_req, resps, cntls, res_block, &rows_locs)); |
264 | 0 | if (rows_locs.size() < column_row_ids->size()) { |
265 | 0 | return Status::InternalError("Miss matched return row loc count {}, expected {}, input {}", |
266 | 0 | rows_locs.size(), res_block->rows(), column_row_ids->size()); |
267 | 0 | } |
268 | | // Final sort by row_ids sequence, since row_ids is already sorted if need |
269 | 0 | std::map<GlobalRowLoacation, size_t> positions; |
270 | 0 | for (size_t i = 0; i < rows_locs.size(); ++i) { |
271 | 0 | RowsetId rowset_id; |
272 | 0 | rowset_id.init(rows_locs[i].rowset_id()); |
273 | 0 | GlobalRowLoacation grl(rows_locs[i].tablet_id(), rowset_id, |
274 | 0 | cast_set<uint32_t>(rows_locs[i].segment_id()), |
275 | 0 | cast_set<uint32_t>(rows_locs[i].ordinal_id())); |
276 | 0 | positions[grl] = i; |
277 | 0 | }; |
278 | | // TODO remove this warning code |
279 | 0 | if (positions.size() < rows_locs.size()) { |
280 | 0 | LOG(WARNING) << "cwntains duplicated row entry"; |
281 | 0 | } |
282 | 0 | vectorized::IColumn::Permutation permutation; |
283 | 0 | permutation.reserve(column_row_ids->size()); |
284 | 0 | for (size_t i = 0; i < column_row_ids->size(); ++i) { |
285 | 0 | auto location = |
286 | 0 | reinterpret_cast<const GlobalRowLoacation*>(column_row_ids->get_data_at(i).data); |
287 | 0 | permutation.push_back(positions[*location]); |
288 | 0 | } |
289 | 0 | for (size_t i = 0; i < res_block->columns(); ++i) { |
290 | 0 | res_block->get_by_position(i).column = |
291 | 0 | res_block->get_by_position(i).column->permute(permutation, permutation.size()); |
292 | 0 | } |
293 | | // Check row consistency |
294 | 0 | RETURN_IF_CATCH_EXCEPTION(res_block->check_number_of_rows()); |
295 | | // shrink for char type |
296 | 0 | std::vector<size_t> char_type_idx; |
297 | 0 | for (size_t i = 0; i < _fetch_option.desc->slots().size(); i++) { |
298 | 0 | const auto& column_desc = _fetch_option.desc->slots()[i]; |
299 | 0 | const auto type = column_desc->type(); |
300 | 0 | if (_has_char_type(type)) { |
301 | 0 | char_type_idx.push_back(i); |
302 | 0 | } |
303 | 0 | } |
304 | 0 | res_block->shrink_char_type_column_suffix_zero(char_type_idx); |
305 | 0 | VLOG_DEBUG << "dump block:" << res_block->dump_data(0, 10); |
306 | 0 | return Status::OK(); |
307 | 0 | } |
308 | | |
309 | | struct IteratorKey { |
310 | | int64_t tablet_id; |
311 | | RowsetId rowset_id; |
312 | | uint64_t segment_id; |
313 | | int slot_id; |
314 | | |
315 | | // unordered map std::equal_to |
316 | 0 | bool operator==(const IteratorKey& rhs) const { |
317 | 0 | return tablet_id == rhs.tablet_id && rowset_id == rhs.rowset_id && |
318 | 0 | segment_id == rhs.segment_id && slot_id == rhs.slot_id; |
319 | 0 | } |
320 | | }; |
321 | | |
322 | | struct HashOfIteratorKey { |
323 | 0 | size_t operator()(const IteratorKey& key) const { |
324 | 0 | size_t seed = 0; |
325 | 0 | seed = HashUtil::hash64(&key.tablet_id, sizeof(key.tablet_id), seed); |
326 | 0 | seed = HashUtil::hash64(&key.rowset_id.hi, sizeof(key.rowset_id.hi), seed); |
327 | 0 | seed = HashUtil::hash64(&key.rowset_id.mi, sizeof(key.rowset_id.mi), seed); |
328 | 0 | seed = HashUtil::hash64(&key.rowset_id.lo, sizeof(key.rowset_id.lo), seed); |
329 | 0 | seed = HashUtil::hash64(&key.segment_id, sizeof(key.segment_id), seed); |
330 | 0 | seed = HashUtil::hash64(&key.slot_id, sizeof(key.slot_id), seed); |
331 | 0 | return seed; |
332 | 0 | } |
333 | | }; |
334 | | |
335 | | struct IteratorItem { |
336 | | std::unique_ptr<ColumnIterator> iterator; |
337 | | // for holding the reference of segment to avoid use after release |
338 | | SegmentSharedPtr segment; |
339 | | }; |
340 | | |
341 | | Status RowIdStorageReader::read_by_rowids(const PMultiGetRequest& request, |
342 | 0 | PMultiGetResponse* response) { |
343 | | // read from storage engine row id by row id |
344 | 0 | OlapReaderStatistics stats; |
345 | 0 | vectorized::Block result_block; |
346 | 0 | int64_t acquire_tablet_ms = 0; |
347 | 0 | int64_t acquire_rowsets_ms = 0; |
348 | 0 | int64_t acquire_segments_ms = 0; |
349 | 0 | int64_t lookup_row_data_ms = 0; |
350 | | |
351 | | // init desc |
352 | 0 | std::vector<SlotDescriptor> slots; |
353 | 0 | slots.reserve(request.slots().size()); |
354 | 0 | for (const auto& pslot : request.slots()) { |
355 | 0 | slots.push_back(SlotDescriptor(pslot)); |
356 | 0 | } |
357 | | |
358 | | // init read schema |
359 | 0 | TabletSchema full_read_schema; |
360 | 0 | for (const ColumnPB& column_pb : request.column_desc()) { |
361 | 0 | full_read_schema.append_column(TabletColumn(column_pb)); |
362 | 0 | } |
363 | |
|
364 | 0 | std::unordered_map<IteratorKey, IteratorItem, HashOfIteratorKey> iterator_map; |
365 | | // read row by row |
366 | 0 | for (int i = 0; i < request.row_locs_size(); ++i) { |
367 | 0 | const auto& row_loc = request.row_locs(i); |
368 | 0 | MonotonicStopWatch watch; |
369 | 0 | watch.start(); |
370 | 0 | BaseTabletSPtr tablet = scope_timer_run( |
371 | 0 | [&]() { |
372 | 0 | auto res = ExecEnv::get_tablet(row_loc.tablet_id(), nullptr, true); |
373 | 0 | return !res.has_value() ? nullptr |
374 | 0 | : std::dynamic_pointer_cast<BaseTablet>(res.value()); |
375 | 0 | }, |
376 | 0 | &acquire_tablet_ms); |
377 | 0 | RowsetId rowset_id; |
378 | 0 | rowset_id.init(row_loc.rowset_id()); |
379 | 0 | if (!tablet) { |
380 | 0 | continue; |
381 | 0 | } |
382 | | // We ensured it's rowset is not released when init Tablet reader param, rowset->update_delayed_expired_timestamp(); |
383 | 0 | BetaRowsetSharedPtr rowset = std::static_pointer_cast<BetaRowset>(scope_timer_run( |
384 | 0 | [&]() { |
385 | 0 | return ExecEnv::GetInstance()->storage_engine().get_quering_rowset(rowset_id); |
386 | 0 | }, |
387 | 0 | &acquire_rowsets_ms)); |
388 | 0 | if (!rowset) { |
389 | 0 | LOG(INFO) << "no such rowset " << rowset_id; |
390 | 0 | continue; |
391 | 0 | } |
392 | 0 | size_t row_size = 0; |
393 | 0 | Defer _defer([&]() { |
394 | 0 | LOG_EVERY_N(INFO, 100) |
395 | 0 | << "multiget_data single_row, cost(us):" << watch.elapsed_time() / 1000 |
396 | 0 | << ", row_size:" << row_size; |
397 | 0 | *response->add_row_locs() = row_loc; |
398 | 0 | }); |
399 | | // TODO: supoort session variable enable_page_cache and disable_file_cache if necessary. |
400 | 0 | SegmentCacheHandle segment_cache; |
401 | 0 | RETURN_IF_ERROR(scope_timer_run( |
402 | 0 | [&]() { |
403 | 0 | return SegmentLoader::instance()->load_segments(rowset, &segment_cache, true); |
404 | 0 | }, |
405 | 0 | &acquire_segments_ms)); |
406 | | // find segment |
407 | 0 | auto it = std::find_if(segment_cache.get_segments().cbegin(), |
408 | 0 | segment_cache.get_segments().cend(), |
409 | 0 | [&row_loc](const segment_v2::SegmentSharedPtr& seg) { |
410 | 0 | return seg->id() == row_loc.segment_id(); |
411 | 0 | }); |
412 | 0 | if (it == segment_cache.get_segments().end()) { |
413 | 0 | continue; |
414 | 0 | } |
415 | 0 | segment_v2::SegmentSharedPtr segment = *it; |
416 | 0 | GlobalRowLoacation row_location(row_loc.tablet_id(), rowset->rowset_id(), |
417 | 0 | cast_set<uint32_t>(row_loc.segment_id()), |
418 | 0 | cast_set<uint32_t>(row_loc.ordinal_id())); |
419 | | // fetch by row store, more effcient way |
420 | 0 | if (request.fetch_row_store()) { |
421 | 0 | CHECK(tablet->tablet_schema()->has_row_store_for_all_columns()); |
422 | 0 | RowLocation loc(rowset_id, segment->id(), cast_set<uint32_t>(row_loc.ordinal_id())); |
423 | 0 | std::string* value = response->add_binary_row_data(); |
424 | 0 | RETURN_IF_ERROR(scope_timer_run( |
425 | 0 | [&]() { return tablet->lookup_row_data({}, loc, rowset, stats, *value); }, |
426 | 0 | &lookup_row_data_ms)); |
427 | 0 | row_size = value->size(); |
428 | 0 | continue; |
429 | 0 | } |
430 | | |
431 | | // fetch by column store |
432 | 0 | if (result_block.is_empty_column()) { |
433 | 0 | result_block = vectorized::Block(slots, request.row_locs().size()); |
434 | 0 | } |
435 | 0 | VLOG_DEBUG << "Read row location " |
436 | 0 | << fmt::format("{}, {}, {}, {}", row_location.tablet_id, |
437 | 0 | row_location.row_location.rowset_id.to_string(), |
438 | 0 | row_location.row_location.segment_id, |
439 | 0 | row_location.row_location.row_id); |
440 | 0 | for (int x = 0; x < slots.size(); ++x) { |
441 | 0 | auto row_id = static_cast<segment_v2::rowid_t>(row_loc.ordinal_id()); |
442 | 0 | vectorized::MutableColumnPtr column = |
443 | 0 | result_block.get_by_position(x).column->assume_mutable(); |
444 | 0 | IteratorKey iterator_key {.tablet_id = tablet->tablet_id(), |
445 | 0 | .rowset_id = rowset_id, |
446 | 0 | .segment_id = row_loc.segment_id(), |
447 | 0 | .slot_id = slots[x].id()}; |
448 | 0 | IteratorItem& iterator_item = iterator_map[iterator_key]; |
449 | 0 | if (iterator_item.segment == nullptr) { |
450 | | // hold the reference |
451 | 0 | iterator_map[iterator_key].segment = segment; |
452 | 0 | } |
453 | 0 | segment = iterator_item.segment; |
454 | 0 | RETURN_IF_ERROR(segment->seek_and_read_by_rowid(full_read_schema, &slots[x], row_id, |
455 | 0 | column, stats, iterator_item.iterator)); |
456 | 0 | } |
457 | 0 | } |
458 | | // serialize block if not empty |
459 | 0 | if (!result_block.is_empty_column()) { |
460 | 0 | VLOG_DEBUG << "dump block:" << result_block.dump_data(0, 10) |
461 | 0 | << ", be_exec_version:" << request.be_exec_version(); |
462 | 0 | [[maybe_unused]] size_t compressed_size = 0; |
463 | 0 | [[maybe_unused]] size_t uncompressed_size = 0; |
464 | 0 | int be_exec_version = request.has_be_exec_version() ? request.be_exec_version() : 0; |
465 | 0 | RETURN_IF_ERROR(result_block.serialize(be_exec_version, response->mutable_block(), |
466 | 0 | &uncompressed_size, &compressed_size, |
467 | 0 | segment_v2::CompressionTypePB::LZ4)); |
468 | 0 | } |
469 | | |
470 | 0 | LOG(INFO) << "Query stats: " |
471 | 0 | << fmt::format( |
472 | 0 | "query_id:{}, " |
473 | 0 | "hit_cached_pages:{}, total_pages_read:{}, compressed_bytes_read:{}, " |
474 | 0 | "io_latency:{}ns, " |
475 | 0 | "uncompressed_bytes_read:{}," |
476 | 0 | "bytes_read:{}," |
477 | 0 | "acquire_tablet_ms:{}, acquire_rowsets_ms:{}, acquire_segments_ms:{}, " |
478 | 0 | "lookup_row_data_ms:{}", |
479 | 0 | print_id(request.query_id()), stats.cached_pages_num, |
480 | 0 | stats.total_pages_num, stats.compressed_bytes_read, stats.io_ns, |
481 | 0 | stats.uncompressed_bytes_read, stats.bytes_read, acquire_tablet_ms, |
482 | 0 | acquire_rowsets_ms, acquire_segments_ms, lookup_row_data_ms); |
483 | 0 | return Status::OK(); |
484 | 0 | } |
485 | | |
486 | | Status RowIdStorageReader::read_by_rowids(const PMultiGetRequestV2& request, |
487 | 0 | PMultiGetResponseV2* response) { |
488 | 0 | if (request.request_block_descs_size()) { |
489 | 0 | auto tquery_id = ((UniqueId)request.query_id()).to_thrift(); |
490 | 0 | std::vector<vectorized::Block> result_blocks(request.request_block_descs_size()); |
491 | |
|
492 | 0 | OlapReaderStatistics stats; |
493 | 0 | int64_t acquire_tablet_ms = 0; |
494 | 0 | int64_t acquire_rowsets_ms = 0; |
495 | 0 | int64_t acquire_segments_ms = 0; |
496 | 0 | int64_t lookup_row_data_ms = 0; |
497 | |
|
498 | 0 | int64_t external_init_reader_avg_ms = 0; |
499 | 0 | int64_t external_get_block_avg_ms = 0; |
500 | 0 | size_t external_scan_range_cnt = 0; |
501 | | |
502 | | // Add counters for different file mapping types |
503 | 0 | std::unordered_map<FileMappingType, int64_t> file_type_counts; |
504 | |
|
505 | 0 | auto id_file_map = |
506 | 0 | ExecEnv::GetInstance()->get_id_manager()->get_id_file_map(request.query_id()); |
507 | | // if id_file_map is null, means the BE not have scan range, just return ok |
508 | 0 | if (!id_file_map) { |
509 | | // padding empty block to response |
510 | 0 | for (int i = 0; i < request.request_block_descs_size(); ++i) { |
511 | 0 | response->add_blocks(); |
512 | 0 | } |
513 | 0 | return Status::OK(); |
514 | 0 | } |
515 | | |
516 | 0 | for (int i = 0; i < request.request_block_descs_size(); ++i) { |
517 | 0 | const auto& request_block_desc = request.request_block_descs(i); |
518 | 0 | PMultiGetBlockV2* pblock = response->add_blocks(); |
519 | 0 | if (request_block_desc.row_id_size() >= 1) { |
520 | | // Since this block belongs to the same table, we only need to take the first type for judgment. |
521 | 0 | auto first_file_id = request_block_desc.file_id(0); |
522 | 0 | auto first_file_mapping = id_file_map->get_file_mapping(first_file_id); |
523 | 0 | if (!first_file_mapping) { |
524 | 0 | return Status::InternalError( |
525 | 0 | "Backend:{} file_mapping not found, query_id: {}, file_id: {}", |
526 | 0 | BackendOptions::get_localhost(), print_id(request.query_id()), |
527 | 0 | first_file_id); |
528 | 0 | } |
529 | 0 | file_type_counts[first_file_mapping->type] += request_block_desc.row_id_size(); |
530 | | |
531 | | // prepare slots to build block |
532 | 0 | std::vector<SlotDescriptor> slots; |
533 | 0 | slots.reserve(request_block_desc.slots_size()); |
534 | 0 | for (const auto& pslot : request_block_desc.slots()) { |
535 | 0 | slots.push_back(SlotDescriptor(pslot)); |
536 | 0 | } |
537 | | // prepare block char vector shrink for char type |
538 | 0 | std::vector<size_t> char_type_idx; |
539 | 0 | for (int j = 0; j < slots.size(); ++j) { |
540 | 0 | auto slot = slots[j]; |
541 | 0 | if (_has_char_type(slot.type())) { |
542 | 0 | char_type_idx.push_back(j); |
543 | 0 | } |
544 | 0 | } |
545 | |
|
546 | 0 | try { |
547 | 0 | if (first_file_mapping->type == FileMappingType::INTERNAL) { |
548 | 0 | RETURN_IF_ERROR(read_batch_doris_format_row( |
549 | 0 | request_block_desc, id_file_map, slots, tquery_id, result_blocks[i], |
550 | 0 | stats, &acquire_tablet_ms, &acquire_rowsets_ms, |
551 | 0 | &acquire_segments_ms, &lookup_row_data_ms)); |
552 | 0 | } else { |
553 | 0 | RETURN_IF_ERROR(read_batch_external_row( |
554 | 0 | request.wg_id(), request_block_desc, id_file_map, slots, |
555 | 0 | first_file_mapping, tquery_id, result_blocks[i], |
556 | 0 | pblock->mutable_profile(), &external_init_reader_avg_ms, |
557 | 0 | &external_get_block_avg_ms, &external_scan_range_cnt)); |
558 | 0 | } |
559 | 0 | } catch (const Exception& e) { |
560 | 0 | return Status::Error<false>(e.code(), "Row id fetch failed because {}", |
561 | 0 | e.what()); |
562 | 0 | } |
563 | | |
564 | | // after read the block, shrink char type block |
565 | 0 | result_blocks[i].shrink_char_type_column_suffix_zero(char_type_idx); |
566 | 0 | } |
567 | | |
568 | 0 | [[maybe_unused]] size_t compressed_size = 0; |
569 | 0 | [[maybe_unused]] size_t uncompressed_size = 0; |
570 | 0 | int be_exec_version = request.has_be_exec_version() ? request.be_exec_version() : 0; |
571 | 0 | RETURN_IF_ERROR(result_blocks[i].serialize(be_exec_version, pblock->mutable_block(), |
572 | 0 | &uncompressed_size, &compressed_size, |
573 | 0 | segment_v2::CompressionTypePB::LZ4)); |
574 | 0 | } |
575 | | |
576 | | // Build file type statistics string |
577 | 0 | std::string file_type_stats; |
578 | 0 | for (const auto& [type, count] : file_type_counts) { |
579 | 0 | if (!file_type_stats.empty()) { |
580 | 0 | file_type_stats += ", "; |
581 | 0 | } |
582 | 0 | file_type_stats += fmt::format("{}:{}", type, count); |
583 | 0 | } |
584 | |
|
585 | 0 | LOG(INFO) << "Query stats: " |
586 | 0 | << fmt::format( |
587 | 0 | "query_id:{}, " |
588 | 0 | "Internal table:" |
589 | 0 | "hit_cached_pages:{}, total_pages_read:{}, compressed_bytes_read:{}, " |
590 | 0 | "io_latency:{}ns, uncompressed_bytes_read:{}, bytes_read:{}, " |
591 | 0 | "acquire_tablet_ms:{}, acquire_rowsets_ms:{}, acquire_segments_ms:{}, " |
592 | 0 | "lookup_row_data_ms:{}, file_types:[{}]; " |
593 | 0 | "External table : init_reader_ms:{}, get_block_ms:{}, " |
594 | 0 | "external_scan_range_cnt:{}", |
595 | 0 | print_id(request.query_id()), stats.cached_pages_num, |
596 | 0 | stats.total_pages_num, stats.compressed_bytes_read, stats.io_ns, |
597 | 0 | stats.uncompressed_bytes_read, stats.bytes_read, acquire_tablet_ms, |
598 | 0 | acquire_rowsets_ms, acquire_segments_ms, lookup_row_data_ms, |
599 | 0 | file_type_stats, external_init_reader_avg_ms, |
600 | 0 | external_get_block_avg_ms, external_scan_range_cnt); |
601 | 0 | } |
602 | | |
603 | 0 | if (request.has_gc_id_map() && request.gc_id_map()) { |
604 | 0 | ExecEnv::GetInstance()->get_id_manager()->remove_id_file_map(request.query_id()); |
605 | 0 | } |
606 | |
|
607 | 0 | return Status::OK(); |
608 | 0 | } |
609 | | |
610 | | Status RowIdStorageReader::read_batch_doris_format_row( |
611 | | const PRequestBlockDesc& request_block_desc, std::shared_ptr<IdFileMap> id_file_map, |
612 | | std::vector<SlotDescriptor>& slots, const TUniqueId& query_id, |
613 | | vectorized::Block& result_block, OlapReaderStatistics& stats, int64_t* acquire_tablet_ms, |
614 | 0 | int64_t* acquire_rowsets_ms, int64_t* acquire_segments_ms, int64_t* lookup_row_data_ms) { |
615 | 0 | if (result_block.is_empty_column()) [[likely]] { |
616 | 0 | result_block = vectorized::Block(slots, request_block_desc.row_id_size()); |
617 | 0 | } |
618 | |
|
619 | 0 | TabletSchema full_read_schema; |
620 | 0 | for (const ColumnPB& column_pb : request_block_desc.column_descs()) { |
621 | 0 | full_read_schema.append_column(TabletColumn(column_pb)); |
622 | 0 | } |
623 | 0 | std::unordered_map<IteratorKey, IteratorItem, HashOfIteratorKey> iterator_map; |
624 | 0 | std::string row_store_buffer; |
625 | 0 | RowStoreReadStruct row_store_read_struct(row_store_buffer); |
626 | 0 | if (request_block_desc.fetch_row_store()) { |
627 | 0 | for (int i = 0; i < request_block_desc.slots_size(); ++i) { |
628 | 0 | row_store_read_struct.serdes.emplace_back(slots[i].get_data_type_ptr()->get_serde()); |
629 | 0 | row_store_read_struct.col_uid_to_idx[slots[i].col_unique_id()] = i; |
630 | 0 | row_store_read_struct.default_values.emplace_back(slots[i].col_default_value()); |
631 | 0 | } |
632 | 0 | } |
633 | |
|
634 | 0 | for (int j = 0; j < request_block_desc.row_id_size(); ++j) { |
635 | 0 | auto file_id = request_block_desc.file_id(j); |
636 | 0 | auto file_mapping = id_file_map->get_file_mapping(file_id); |
637 | 0 | if (!file_mapping) { |
638 | 0 | return Status::InternalError( |
639 | 0 | "Backend:{} file_mapping not found, query_id: {}, file_id: {}", |
640 | 0 | BackendOptions::get_localhost(), print_id(query_id), file_id); |
641 | 0 | } |
642 | | |
643 | 0 | RETURN_IF_ERROR(read_doris_format_row( |
644 | 0 | id_file_map, file_mapping, request_block_desc.row_id(j), slots, full_read_schema, |
645 | 0 | row_store_read_struct, stats, acquire_tablet_ms, acquire_rowsets_ms, |
646 | 0 | acquire_segments_ms, lookup_row_data_ms, iterator_map, result_block)); |
647 | 0 | } |
648 | 0 | return Status::OK(); |
649 | 0 | } |
650 | | |
651 | | const std::string RowIdStorageReader::ScannersRunningTimeProfile = "ScannersRunningTime"; |
652 | | const std::string RowIdStorageReader::InitReaderAvgTimeProfile = "InitReaderAvgTime"; |
653 | | const std::string RowIdStorageReader::GetBlockAvgTimeProfile = "GetBlockAvgTime"; |
654 | | const std::string RowIdStorageReader::FileReadLinesProfile = "FileReadLines"; |
655 | | |
656 | | Status RowIdStorageReader::read_batch_external_row( |
657 | | const uint64_t workload_group_id, const PRequestBlockDesc& request_block_desc, |
658 | | std::shared_ptr<IdFileMap> id_file_map, std::vector<SlotDescriptor>& slots, |
659 | | std::shared_ptr<FileMapping> first_file_mapping, const TUniqueId& query_id, |
660 | | vectorized::Block& result_block, PRuntimeProfileTree* pprofile, int64_t* init_reader_avg_ms, |
661 | 0 | int64_t* get_block_avg_ms, size_t* scan_range_cnt) { |
662 | 0 | TFileScanRangeParams rpc_scan_params; |
663 | 0 | TupleDescriptor tuple_desc(request_block_desc.desc(), false); |
664 | 0 | std::unordered_map<std::string, int> colname_to_slot_id; |
665 | 0 | std::shared_ptr<RuntimeState> runtime_state = nullptr; |
666 | |
|
667 | 0 | int max_file_scanners = 0; |
668 | 0 | { |
669 | 0 | if (result_block.is_empty_column()) [[likely]] { |
670 | 0 | result_block = vectorized::Block(slots, request_block_desc.row_id_size()); |
671 | 0 | } |
672 | |
|
673 | 0 | auto& external_info = first_file_mapping->get_external_file_info(); |
674 | 0 | int plan_node_id = external_info.plan_node_id; |
675 | 0 | const auto& first_scan_range_desc = external_info.scan_range_desc; |
676 | |
|
677 | 0 | DCHECK(id_file_map->get_external_scan_params().contains(plan_node_id)); |
678 | 0 | const auto* old_scan_params = &(id_file_map->get_external_scan_params().at(plan_node_id)); |
679 | 0 | rpc_scan_params = *old_scan_params; |
680 | |
|
681 | 0 | rpc_scan_params.required_slots.clear(); |
682 | 0 | rpc_scan_params.column_idxs.clear(); |
683 | 0 | rpc_scan_params.slot_name_to_schema_pos.clear(); |
684 | |
|
685 | 0 | std::set partition_name_set(first_scan_range_desc.columns_from_path_keys.begin(), |
686 | 0 | first_scan_range_desc.columns_from_path_keys.end()); |
687 | 0 | for (auto slot_idx = 0; slot_idx < slots.size(); ++slot_idx) { |
688 | 0 | auto& slot = slots[slot_idx]; |
689 | 0 | tuple_desc.add_slot(&slot); |
690 | 0 | colname_to_slot_id.emplace(slot.col_name(), slot.id()); |
691 | 0 | TFileScanSlotInfo slot_info; |
692 | 0 | slot_info.slot_id = slot.id(); |
693 | 0 | auto column_idx = request_block_desc.column_idxs(slot_idx); |
694 | 0 | if (partition_name_set.contains(slot.col_name())) { |
695 | | //This is partition column. |
696 | 0 | slot_info.is_file_slot = false; |
697 | 0 | } else { |
698 | 0 | rpc_scan_params.column_idxs.emplace_back(column_idx); |
699 | 0 | slot_info.is_file_slot = true; |
700 | 0 | } |
701 | 0 | rpc_scan_params.default_value_of_src_slot.emplace(slot.id(), TExpr {}); |
702 | 0 | rpc_scan_params.required_slots.emplace_back(slot_info); |
703 | 0 | rpc_scan_params.slot_name_to_schema_pos.emplace(slot.col_name(), column_idx); |
704 | 0 | } |
705 | |
|
706 | 0 | const auto& query_options = id_file_map->get_query_options(); |
707 | 0 | const auto& query_globals = id_file_map->get_query_globals(); |
708 | | /* |
709 | | * The scan stage needs the information in query_options to generate different behaviors according to the specific variables: |
710 | | * query_options.hive_parquet_use_column_names, query_options.truncate_char_or_varchar_columns,query_globals.time_zone ... |
711 | | * |
712 | | * To ensure the same behavior as the scan stage, I get query_options query_globals from id_file_map, then create runtime_state |
713 | | * and pass it to vfile_scanner so that the runtime_state information is the same as the scan stage and the behavior is also consistent. |
714 | | */ |
715 | 0 | runtime_state = RuntimeState::create_shared( |
716 | 0 | query_id, -1, query_options, query_globals, ExecEnv::GetInstance(), |
717 | 0 | ExecEnv::GetInstance()->rowid_storage_reader_tracker()); |
718 | |
|
719 | 0 | max_file_scanners = id_file_map->get_max_file_scanners(); |
720 | 0 | } |
721 | | |
722 | | // Hash(TFileRangeDesc) => { all the rows that need to be read and their positions in the result block. } + file mapping |
723 | 0 | std::map<std::string, |
724 | 0 | std::pair<std::map<segment_v2::rowid_t, size_t>, std::shared_ptr<FileMapping>>> |
725 | 0 | scan_rows; |
726 | | |
727 | | // Block corresponding to the order of `scan_rows` map. |
728 | 0 | std::vector<vectorized::Block> scan_blocks; |
729 | | |
730 | | // row_id (Indexing of vectors) => < In which block, which line in the block > |
731 | 0 | std::vector<std::pair<size_t, size_t>> row_id_block_idx; |
732 | | |
733 | | // Count the time/bytes it takes to read each TFileRangeDesc. (for profile) |
734 | 0 | std::vector<ExternalFetchStatistics> fetch_statistics; |
735 | |
|
736 | 0 | auto hash_file_range = [](const TFileRangeDesc& file_range_desc) { |
737 | 0 | std::string value; |
738 | 0 | value.resize(file_range_desc.path.size() + sizeof(file_range_desc.start_offset)); |
739 | 0 | auto* ptr = value.data(); |
740 | |
|
741 | 0 | memcpy(ptr, &file_range_desc.start_offset, sizeof(file_range_desc.start_offset)); |
742 | 0 | ptr += sizeof(file_range_desc.start_offset); |
743 | 0 | memcpy(ptr, file_range_desc.path.data(), file_range_desc.path.size()); |
744 | 0 | return value; |
745 | 0 | }; |
746 | |
|
747 | 0 | for (int j = 0; j < request_block_desc.row_id_size(); ++j) { |
748 | 0 | auto file_id = request_block_desc.file_id(j); |
749 | 0 | auto file_mapping = id_file_map->get_file_mapping(file_id); |
750 | 0 | if (!file_mapping) { |
751 | 0 | return Status::InternalError( |
752 | 0 | "Backend:{} file_mapping not found, query_id: {}, file_id: {}", |
753 | 0 | BackendOptions::get_localhost(), print_id(query_id), file_id); |
754 | 0 | } |
755 | | |
756 | 0 | const auto& external_info = file_mapping->get_external_file_info(); |
757 | 0 | const auto& scan_range_desc = external_info.scan_range_desc; |
758 | |
|
759 | 0 | auto scan_range_hash = hash_file_range(scan_range_desc); |
760 | 0 | if (scan_rows.contains(scan_range_hash)) { |
761 | 0 | scan_rows.at(scan_range_hash).first.emplace(request_block_desc.row_id(j), j); |
762 | 0 | } else { |
763 | 0 | std::map<segment_v2::rowid_t, size_t> tmp {{request_block_desc.row_id(j), j}}; |
764 | 0 | scan_rows.emplace(scan_range_hash, std::make_pair(tmp, file_mapping)); |
765 | 0 | } |
766 | 0 | } |
767 | | |
768 | 0 | scan_blocks.resize(scan_rows.size()); |
769 | 0 | row_id_block_idx.resize(request_block_desc.row_id_size()); |
770 | 0 | fetch_statistics.resize(scan_rows.size()); |
771 | | |
772 | | // Get the workload group for subsequent scan task submission. |
773 | 0 | std::vector<uint64_t> workload_group_ids; |
774 | 0 | workload_group_ids.emplace_back(workload_group_id); |
775 | 0 | auto wg = ExecEnv::GetInstance()->workload_group_mgr()->get_group(workload_group_ids); |
776 | 0 | doris::pipeline::TaskScheduler* exec_sched = nullptr; |
777 | 0 | vectorized::SimplifiedScanScheduler* scan_sched = nullptr; |
778 | 0 | vectorized::SimplifiedScanScheduler* remote_scan_sched = nullptr; |
779 | 0 | wg->get_query_scheduler(&exec_sched, &scan_sched, &remote_scan_sched); |
780 | 0 | DCHECK(remote_scan_sched); |
781 | |
|
782 | 0 | int64_t scan_running_time = 0; |
783 | 0 | RETURN_IF_ERROR(scope_timer_run( |
784 | 0 | [&]() -> Status { |
785 | | // Make sure to insert data into result_block only after all scan tasks have been executed. |
786 | 0 | std::atomic<int> producer_count {0}; |
787 | 0 | std::condition_variable cv; |
788 | 0 | std::mutex mtx; |
789 | | |
790 | | //semaphore: Limit the number of scan tasks submitted at one time |
791 | 0 | std::counting_semaphore semaphore {max_file_scanners}; |
792 | |
|
793 | 0 | size_t idx = 0; |
794 | 0 | for (const auto& [_, scan_info] : scan_rows) { |
795 | 0 | semaphore.acquire(); |
796 | 0 | RETURN_IF_ERROR(remote_scan_sched->submit_scan_task( |
797 | 0 | vectorized::SimplifiedScanTask( |
798 | 0 | [&, scan_info, idx]() { |
799 | 0 | auto& row_ids = scan_info.first; |
800 | 0 | auto& file_mapping = scan_info.second; |
801 | |
|
802 | 0 | SCOPED_ATTACH_TASK( |
803 | 0 | ExecEnv::GetInstance() |
804 | 0 | ->rowid_storage_reader_tracker()); |
805 | 0 | signal::set_signal_task_id(query_id); |
806 | |
|
807 | 0 | scan_blocks[idx] = |
808 | 0 | vectorized::Block(slots, scan_info.first.size()); |
809 | |
|
810 | 0 | size_t j = 0; |
811 | 0 | std::list<int64_t> read_ids; |
812 | | //Generate an ordered list with the help of the orderliness of the map. |
813 | 0 | for (const auto& [row_id, result_block_idx] : row_ids) { |
814 | 0 | read_ids.emplace_back(row_id); |
815 | 0 | row_id_block_idx[result_block_idx] = |
816 | 0 | std::make_pair(idx, j); |
817 | 0 | j++; |
818 | 0 | } |
819 | |
|
820 | 0 | auto& external_info = |
821 | 0 | file_mapping->get_external_file_info(); |
822 | 0 | auto& scan_range_desc = external_info.scan_range_desc; |
823 | | |
824 | | // Clear to avoid reading iceberg position delete file... |
825 | 0 | scan_range_desc.table_format_params.iceberg_params = |
826 | 0 | TIcebergFileDesc {}; |
827 | | |
828 | | // Clear to avoid reading hive transactional delete delta file... |
829 | 0 | scan_range_desc.table_format_params |
830 | 0 | .transactional_hive_params = |
831 | 0 | TTransactionalHiveDesc {}; |
832 | |
|
833 | 0 | std::unique_ptr<RuntimeProfile> sub_runtime_profile = |
834 | 0 | std::make_unique<RuntimeProfile>( |
835 | 0 | "ExternalRowIDFetcher"); |
836 | 0 | { |
837 | 0 | std::unique_ptr<vectorized::FileScanner> |
838 | 0 | vfile_scanner_ptr = |
839 | 0 | vectorized::FileScanner::create_unique( |
840 | 0 | runtime_state.get(), |
841 | 0 | sub_runtime_profile.get(), |
842 | 0 | &rpc_scan_params, |
843 | 0 | &colname_to_slot_id, |
844 | 0 | &tuple_desc); |
845 | |
|
846 | 0 | RETURN_IF_ERROR( |
847 | 0 | vfile_scanner_ptr->prepare_for_read_lines( |
848 | 0 | scan_range_desc)); |
849 | 0 | RETURN_IF_ERROR( |
850 | 0 | vfile_scanner_ptr->read_lines_from_range( |
851 | 0 | scan_range_desc, read_ids, |
852 | 0 | &scan_blocks[idx], external_info, |
853 | 0 | &fetch_statistics[idx].init_reader_ms, |
854 | 0 | &fetch_statistics[idx].get_block_ms)); |
855 | 0 | } |
856 | |
|
857 | 0 | auto file_read_bytes_counter = |
858 | 0 | sub_runtime_profile->get_counter( |
859 | 0 | vectorized::FileScanner:: |
860 | 0 | FileReadBytesProfile); |
861 | |
|
862 | 0 | if (file_read_bytes_counter != nullptr) { |
863 | 0 | fetch_statistics[idx].file_read_bytes = |
864 | 0 | PrettyPrinter::print( |
865 | 0 | file_read_bytes_counter->value(), |
866 | 0 | file_read_bytes_counter->type()); |
867 | 0 | } |
868 | |
|
869 | 0 | auto file_read_times_counter = |
870 | 0 | sub_runtime_profile->get_counter( |
871 | 0 | vectorized::FileScanner:: |
872 | 0 | FileReadTimeProfile); |
873 | 0 | if (file_read_times_counter != nullptr) { |
874 | 0 | fetch_statistics[idx].file_read_times = |
875 | 0 | PrettyPrinter::print( |
876 | 0 | file_read_times_counter->value(), |
877 | 0 | file_read_times_counter->type()); |
878 | 0 | } |
879 | |
|
880 | 0 | semaphore.release(); |
881 | 0 | if (++producer_count == scan_rows.size()) { |
882 | 0 | std::lock_guard<std::mutex> lock(mtx); |
883 | 0 | cv.notify_one(); |
884 | 0 | } |
885 | 0 | return Status::OK(); |
886 | 0 | }, |
887 | 0 | nullptr, nullptr), |
888 | 0 | fmt::format("{}-read_batch_external_row-{}", print_id(query_id), idx))); |
889 | 0 | idx++; |
890 | 0 | } |
891 | |
|
892 | 0 | { |
893 | 0 | std::unique_lock<std::mutex> lock(mtx); |
894 | 0 | cv.wait(lock, [&] { return producer_count == scan_rows.size(); }); |
895 | 0 | } |
896 | 0 | return Status::OK(); |
897 | 0 | }, |
898 | 0 | &scan_running_time)); |
899 | | |
900 | | // Insert the read data into result_block. |
901 | 0 | for (size_t column_id = 0; column_id < result_block.get_columns().size(); column_id++) { |
902 | 0 | auto dst_col = |
903 | 0 | const_cast<vectorized::IColumn*>(result_block.get_columns()[column_id].get()); |
904 | |
|
905 | 0 | std::vector<const vectorized::IColumn*> scan_src_columns; |
906 | 0 | scan_src_columns.reserve(row_id_block_idx.size()); |
907 | 0 | std::vector<size_t> scan_positions; |
908 | 0 | scan_positions.reserve(row_id_block_idx.size()); |
909 | 0 | for (const auto& [pos_block, block_idx] : row_id_block_idx) { |
910 | 0 | DCHECK(scan_blocks.size() > pos_block); |
911 | 0 | DCHECK(scan_blocks[pos_block].get_columns().size() > column_id); |
912 | 0 | scan_src_columns.emplace_back(scan_blocks[pos_block].get_columns()[column_id].get()); |
913 | 0 | scan_positions.emplace_back(block_idx); |
914 | 0 | } |
915 | 0 | dst_col->insert_from_multi_column(scan_src_columns, scan_positions); |
916 | 0 | } |
917 | | |
918 | | // Statistical runtime profile information. |
919 | 0 | std::unique_ptr<RuntimeProfile> runtime_profile = |
920 | 0 | std::make_unique<RuntimeProfile>("ExternalRowIDFetcher"); |
921 | 0 | { |
922 | 0 | runtime_profile->add_info_string(ScannersRunningTimeProfile, |
923 | 0 | std::to_string(scan_running_time) + "ms"); |
924 | 0 | fmt::memory_buffer file_read_lines_buffer; |
925 | 0 | format_to(file_read_lines_buffer, "["); |
926 | 0 | fmt::memory_buffer file_read_bytes_buffer; |
927 | 0 | format_to(file_read_bytes_buffer, "["); |
928 | 0 | fmt::memory_buffer file_read_times_buffer; |
929 | 0 | format_to(file_read_times_buffer, "["); |
930 | |
|
931 | 0 | size_t idx = 0; |
932 | 0 | for (const auto& [_, scan_info] : scan_rows) { |
933 | 0 | format_to(file_read_lines_buffer, "{}, ", scan_info.first.size()); |
934 | 0 | *init_reader_avg_ms = fetch_statistics[idx].init_reader_ms; |
935 | 0 | *get_block_avg_ms += fetch_statistics[idx].get_block_ms; |
936 | 0 | format_to(file_read_bytes_buffer, "{}, ", fetch_statistics[idx].file_read_bytes); |
937 | 0 | format_to(file_read_times_buffer, "{}, ", fetch_statistics[idx].file_read_times); |
938 | 0 | idx++; |
939 | 0 | } |
940 | |
|
941 | 0 | format_to(file_read_lines_buffer, "]"); |
942 | 0 | format_to(file_read_bytes_buffer, "]"); |
943 | 0 | format_to(file_read_times_buffer, "]"); |
944 | |
|
945 | 0 | *init_reader_avg_ms /= fetch_statistics.size(); |
946 | 0 | *get_block_avg_ms /= fetch_statistics.size(); |
947 | 0 | runtime_profile->add_info_string(InitReaderAvgTimeProfile, |
948 | 0 | std::to_string(*init_reader_avg_ms) + "ms"); |
949 | 0 | runtime_profile->add_info_string(GetBlockAvgTimeProfile, |
950 | 0 | std::to_string(*init_reader_avg_ms) + "ms"); |
951 | 0 | runtime_profile->add_info_string(FileReadLinesProfile, |
952 | 0 | fmt::to_string(file_read_lines_buffer)); |
953 | 0 | runtime_profile->add_info_string(vectorized::FileScanner::FileReadBytesProfile, |
954 | 0 | fmt::to_string(file_read_bytes_buffer)); |
955 | 0 | runtime_profile->add_info_string(vectorized::FileScanner::FileReadTimeProfile, |
956 | 0 | fmt::to_string(file_read_times_buffer)); |
957 | 0 | } |
958 | |
|
959 | 0 | runtime_profile->to_proto(pprofile, 2); |
960 | |
|
961 | 0 | *scan_range_cnt = scan_rows.size(); |
962 | |
|
963 | 0 | return Status::OK(); |
964 | 0 | } |
965 | | |
966 | | Status RowIdStorageReader::read_doris_format_row( |
967 | | const std::shared_ptr<IdFileMap>& id_file_map, |
968 | | const std::shared_ptr<FileMapping>& file_mapping, int64_t row_id, |
969 | | std::vector<SlotDescriptor>& slots, const TabletSchema& full_read_schema, |
970 | | RowStoreReadStruct& row_store_read_struct, OlapReaderStatistics& stats, |
971 | | int64_t* acquire_tablet_ms, int64_t* acquire_rowsets_ms, int64_t* acquire_segments_ms, |
972 | | int64_t* lookup_row_data_ms, |
973 | | std::unordered_map<IteratorKey, IteratorItem, HashOfIteratorKey>& iterator_map, |
974 | 0 | vectorized::Block& result_block) { |
975 | 0 | auto [tablet_id, rowset_id, segment_id] = file_mapping->get_doris_format_info(); |
976 | 0 | BaseTabletSPtr tablet = scope_timer_run( |
977 | 0 | [&]() { |
978 | 0 | auto res = ExecEnv::get_tablet(tablet_id); |
979 | 0 | return !res.has_value() ? nullptr |
980 | 0 | : std::dynamic_pointer_cast<BaseTablet>(res.value()); |
981 | 0 | }, |
982 | 0 | acquire_tablet_ms); |
983 | 0 | if (!tablet) { |
984 | 0 | return Status::InternalError( |
985 | 0 | "Backend:{} tablet not found, tablet_id: {}, rowset_id: {}, segment_id: {}, " |
986 | 0 | "row_id: {}", |
987 | 0 | BackendOptions::get_localhost(), tablet_id, rowset_id.to_string(), segment_id, |
988 | 0 | row_id); |
989 | 0 | } |
990 | | |
991 | 0 | BetaRowsetSharedPtr rowset = std::static_pointer_cast<BetaRowset>( |
992 | 0 | scope_timer_run([&]() { return id_file_map->get_temp_rowset(tablet_id, rowset_id); }, |
993 | 0 | acquire_rowsets_ms)); |
994 | 0 | if (!rowset) { |
995 | 0 | return Status::InternalError( |
996 | 0 | "Backend:{} rowset_id not found, tablet_id: {}, rowset_id: {}, segment_id: {}, " |
997 | 0 | "row_id: {}", |
998 | 0 | BackendOptions::get_localhost(), tablet_id, rowset_id.to_string(), segment_id, |
999 | 0 | row_id); |
1000 | 0 | } |
1001 | | |
1002 | 0 | SegmentCacheHandle segment_cache; |
1003 | 0 | RETURN_IF_ERROR(scope_timer_run( |
1004 | 0 | [&]() { |
1005 | 0 | return SegmentLoader::instance()->load_segments(rowset, &segment_cache, true); |
1006 | 0 | }, |
1007 | 0 | acquire_segments_ms)); |
1008 | | |
1009 | 0 | auto it = |
1010 | 0 | std::find_if(segment_cache.get_segments().cbegin(), segment_cache.get_segments().cend(), |
1011 | 0 | [segment_id](const segment_v2::SegmentSharedPtr& seg) { |
1012 | 0 | return seg->id() == segment_id; |
1013 | 0 | }); |
1014 | 0 | if (it == segment_cache.get_segments().end()) { |
1015 | 0 | return Status::InternalError( |
1016 | 0 | "Backend:{} segment not found, tablet_id: {}, rowset_id: {}, segment_id: {}, " |
1017 | 0 | "row_id: {}", |
1018 | 0 | BackendOptions::get_localhost(), tablet_id, rowset_id.to_string(), segment_id, |
1019 | 0 | row_id); |
1020 | 0 | } |
1021 | 0 | segment_v2::SegmentSharedPtr segment = *it; |
1022 | | |
1023 | | // if row_store_read_struct not empty, means the line we should read from row_store |
1024 | 0 | if (!row_store_read_struct.default_values.empty()) { |
1025 | 0 | CHECK(tablet->tablet_schema()->has_row_store_for_all_columns()); |
1026 | 0 | RowLocation loc(rowset_id, segment->id(), cast_set<uint32_t>(row_id)); |
1027 | 0 | row_store_read_struct.row_store_buffer.clear(); |
1028 | 0 | RETURN_IF_ERROR(scope_timer_run( |
1029 | 0 | [&]() { |
1030 | 0 | return tablet->lookup_row_data({}, loc, rowset, stats, |
1031 | 0 | row_store_read_struct.row_store_buffer); |
1032 | 0 | }, |
1033 | 0 | lookup_row_data_ms)); |
1034 | | |
1035 | 0 | RETURN_IF_ERROR(vectorized::JsonbSerializeUtil::jsonb_to_block( |
1036 | 0 | row_store_read_struct.serdes, row_store_read_struct.row_store_buffer.data(), |
1037 | 0 | row_store_read_struct.row_store_buffer.size(), row_store_read_struct.col_uid_to_idx, |
1038 | 0 | result_block, row_store_read_struct.default_values, {})); |
1039 | 0 | } else { |
1040 | 0 | for (int x = 0; x < slots.size(); ++x) { |
1041 | 0 | vectorized::MutableColumnPtr column = |
1042 | 0 | result_block.get_by_position(x).column->assume_mutable(); |
1043 | 0 | IteratorKey iterator_key {.tablet_id = tablet_id, |
1044 | 0 | .rowset_id = rowset_id, |
1045 | 0 | .segment_id = segment_id, |
1046 | 0 | .slot_id = slots[x].id()}; |
1047 | 0 | IteratorItem& iterator_item = iterator_map[iterator_key]; |
1048 | 0 | if (iterator_item.segment == nullptr) { |
1049 | 0 | iterator_map[iterator_key].segment = segment; |
1050 | 0 | } |
1051 | 0 | segment = iterator_item.segment; |
1052 | 0 | RETURN_IF_ERROR(segment->seek_and_read_by_rowid(full_read_schema, &slots[x], |
1053 | 0 | cast_set<uint32_t>(row_id), column, |
1054 | 0 | stats, iterator_item.iterator)); |
1055 | 0 | } |
1056 | 0 | } |
1057 | | |
1058 | 0 | return Status::OK(); |
1059 | 0 | } |
1060 | | |
1061 | | #include "common/compile_check_end.h" |
1062 | | |
1063 | | } // namespace doris |