|
| 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 "bthread/countdown_event.h" |
| 21 | +#include "exec/tablet_info.h" // DorisNodesInfo |
| 22 | +#include "gen_cpp/Types_types.h" |
| 23 | +#include "gen_cpp/internal_service.pb.h" |
| 24 | +#include "runtime/exec_env.h" // ExecEnv |
| 25 | +#include "runtime/runtime_state.h" // RuntimeState |
| 26 | +#include "util/brpc_client_cache.h" // BrpcClientCache |
| 27 | +#include "util/defer_op.h" |
| 28 | +#include "vec/core/block.h" // Block |
| 29 | + |
| 30 | +namespace doris { |
| 31 | + |
| 32 | +Status RowIDFetcher::init(DorisNodesInfo* nodes_info) { |
| 33 | + for (auto [node_id, node_info] : nodes_info->nodes_info()) { |
| 34 | + auto client = ExecEnv::GetInstance()->brpc_internal_client_cache()->get_client( |
| 35 | + node_info.host, node_info.brpc_port); |
| 36 | + if (!client) { |
| 37 | + LOG(WARNING) << "Get rpc stub failed, host=" << node_info.host |
| 38 | + << ", port=" << node_info.brpc_port; |
| 39 | + return Status::InternalError("RowIDFetcher failed to init rpc client"); |
| 40 | + } |
| 41 | + _stubs.push_back(client); |
| 42 | + } |
| 43 | + return Status::OK(); |
| 44 | +} |
| 45 | + |
| 46 | +static std::string format_rowid(const GlobalRowLoacation& location) { |
| 47 | + return fmt::format("{} {} {} {}", location.tablet_id, |
| 48 | + location.row_location.rowset_id.to_string(), |
| 49 | + location.row_location.segment_id, location.row_location.row_id); |
| 50 | +} |
| 51 | + |
| 52 | +PMultiGetRequest RowIDFetcher::_init_fetch_request(const vectorized::ColumnString& row_ids) { |
| 53 | + PMultiGetRequest mget_req; |
| 54 | + _tuple_desc->to_protobuf(mget_req.mutable_desc()); |
| 55 | + for (auto slot : _tuple_desc->slots()) { |
| 56 | + slot->to_protobuf(mget_req.add_slots()); |
| 57 | + } |
| 58 | + for (size_t i = 0; i < row_ids.size(); ++i) { |
| 59 | + PMultiGetRequest::RowId row_id; |
| 60 | + StringRef row_id_rep = row_ids.get_data_at(i); |
| 61 | + auto location = reinterpret_cast<const GlobalRowLoacation*>(row_id_rep.data); |
| 62 | + row_id.set_tablet_id(location->tablet_id); |
| 63 | + row_id.set_rowset_id(location->row_location.rowset_id.to_string()); |
| 64 | + row_id.set_segment_id(location->row_location.segment_id); |
| 65 | + row_id.set_ordinal_id(location->row_location.row_id); |
| 66 | + *mget_req.add_rowids() = std::move(row_id); |
| 67 | + } |
| 68 | + mget_req.set_be_exec_version(_st->be_exec_version()); |
| 69 | + return mget_req; |
| 70 | +} |
| 71 | + |
| 72 | +static void fetch_callback(bthread::CountdownEvent* counter) { |
| 73 | + Defer __defer([&] { counter->signal(); }); |
| 74 | +} |
| 75 | + |
| 76 | +static Status MergeRPCResults(const std::vector<PMultiGetResponse>& rsps, |
| 77 | + const std::vector<brpc::Controller>& cntls, |
| 78 | + vectorized::MutableBlock* output_block) { |
| 79 | + for (const auto& cntl : cntls) { |
| 80 | + if (cntl.Failed()) { |
| 81 | + LOG(WARNING) << "Failed to fetch meet rpc error:" << cntl.ErrorText() |
| 82 | + << ", host:" << cntl.remote_side(); |
| 83 | + return Status::InternalError(cntl.ErrorText()); |
| 84 | + } |
| 85 | + } |
| 86 | + for (const auto& resp : rsps) { |
| 87 | + Status st(resp.status()); |
| 88 | + if (!st.ok()) { |
| 89 | + LOG(WARNING) << "Failed to fetch " << st.to_string(); |
| 90 | + return st; |
| 91 | + } |
| 92 | + vectorized::Block partial_block(resp.block()); |
| 93 | + output_block->merge(partial_block); |
| 94 | + } |
| 95 | + return Status::OK(); |
| 96 | +} |
| 97 | + |
| 98 | +Status RowIDFetcher::fetch(const vectorized::ColumnPtr& row_ids, |
| 99 | + vectorized::MutableBlock* res_block) { |
| 100 | + CHECK(!_stubs.empty()); |
| 101 | + res_block->clear_column_data(); |
| 102 | + vectorized::MutableBlock mblock({_tuple_desc}, row_ids->size()); |
| 103 | + PMultiGetRequest mget_req = _init_fetch_request(assert_cast<const vectorized::ColumnString&>( |
| 104 | + *vectorized::remove_nullable(row_ids).get())); |
| 105 | + std::vector<PMultiGetResponse> resps(_stubs.size()); |
| 106 | + std::vector<brpc::Controller> cntls(_stubs.size()); |
| 107 | + bthread::CountdownEvent counter(_stubs.size()); |
| 108 | + for (size_t i = 0; i < _stubs.size(); ++i) { |
| 109 | + cntls[i].set_timeout_ms(config::fetch_rpc_timeout_seconds * 1000); |
| 110 | + auto callback = brpc::NewCallback(fetch_callback, &counter); |
| 111 | + _stubs[i]->multiget_data(&cntls[i], &mget_req, &resps[i], callback); |
| 112 | + } |
| 113 | + counter.wait(); |
| 114 | + RETURN_IF_ERROR(MergeRPCResults(resps, cntls, &mblock)); |
| 115 | + // final sort by row_ids sequence, since row_ids is already sorted |
| 116 | + vectorized::Block tmp = mblock.to_block(); |
| 117 | + std::unordered_map<std::string, uint32_t> row_order; |
| 118 | + vectorized::ColumnPtr row_id_column = tmp.get_columns().back(); |
| 119 | + for (size_t x = 0; x < row_id_column->size(); ++x) { |
| 120 | + auto location = |
| 121 | + reinterpret_cast<const GlobalRowLoacation*>(row_id_column->get_data_at(x).data); |
| 122 | + row_order[format_rowid(*location)] = x; |
| 123 | + } |
| 124 | + for (size_t x = 0; x < row_ids->size(); ++x) { |
| 125 | + auto location = reinterpret_cast<const GlobalRowLoacation*>(row_ids->get_data_at(x).data); |
| 126 | + res_block->add_row(&tmp, row_order[format_rowid(*location)]); |
| 127 | + } |
| 128 | + return Status::OK(); |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace doris |
0 commit comments