/root/doris/be/src/runtime/load_channel.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 "runtime/load_channel.h" |
19 | | |
20 | | #include <gen_cpp/internal_service.pb.h> |
21 | | #include <glog/logging.h> |
22 | | |
23 | | #include "cloud/cloud_tablets_channel.h" |
24 | | #include "cloud/config.h" |
25 | | #include "common/logging.h" |
26 | | #include "olap/storage_engine.h" |
27 | | #include "runtime/exec_env.h" |
28 | | #include "runtime/fragment_mgr.h" |
29 | | #include "runtime/memory/mem_tracker.h" |
30 | | #include "runtime/tablets_channel.h" |
31 | | #include "runtime/thread_context.h" |
32 | | #include "runtime/workload_group/workload_group_manager.h" |
33 | | #include "util/debug_points.h" |
34 | | |
35 | | namespace doris { |
36 | | |
37 | | bvar::Adder<int64_t> g_loadchannel_cnt("loadchannel_cnt"); |
38 | | |
39 | | LoadChannel::LoadChannel(const UniqueId& load_id, int64_t timeout_s, bool is_high_priority, |
40 | | std::string sender_ip, int64_t backend_id, bool enable_profile, |
41 | | int64_t wg_id) |
42 | 0 | : _load_id(load_id), |
43 | 0 | _timeout_s(timeout_s), |
44 | 0 | _is_high_priority(is_high_priority), |
45 | 0 | _sender_ip(std::move(sender_ip)), |
46 | 0 | _backend_id(backend_id), |
47 | 0 | _enable_profile(enable_profile) { |
48 | 0 | std::shared_ptr<QueryContext> query_context = |
49 | 0 | ExecEnv::GetInstance()->fragment_mgr()->get_query_ctx(_load_id.to_thrift()); |
50 | |
|
51 | 0 | if (query_context != nullptr) { |
52 | 0 | _resource_ctx = query_context->resource_ctx(); |
53 | 0 | } else { |
54 | 0 | _resource_ctx = ResourceContext::create_shared(); |
55 | 0 | _resource_ctx->task_controller()->set_task_id(_load_id.to_thrift()); |
56 | | // when memtable on sink is not enabled, load can not find queryctx |
57 | 0 | std::shared_ptr<MemTrackerLimiter> mem_tracker = MemTrackerLimiter::create_shared( |
58 | 0 | MemTrackerLimiter::Type::LOAD, |
59 | 0 | fmt::format("(FromLoadChannel)Load#Id={}", _load_id.to_string())); |
60 | 0 | _resource_ctx->memory_context()->set_mem_tracker(mem_tracker); |
61 | 0 | WorkloadGroupPtr wg_ptr = nullptr; |
62 | 0 | if (wg_id > 0) { |
63 | 0 | std::vector<uint64_t> id_set; |
64 | 0 | id_set.push_back(wg_id); |
65 | 0 | wg_ptr = ExecEnv::GetInstance()->workload_group_mgr()->get_group(id_set); |
66 | 0 | _resource_ctx->set_workload_group(wg_ptr); |
67 | 0 | } |
68 | 0 | } |
69 | |
|
70 | 0 | g_loadchannel_cnt << 1; |
71 | | // _last_updated_time should be set before being inserted to |
72 | | // _load_channels in load_channel_mgr, or it may be erased |
73 | | // immediately by gc thread. |
74 | 0 | _last_updated_time.store(time(nullptr)); |
75 | 0 | _init_profile(); |
76 | 0 | } |
77 | | |
78 | 0 | LoadChannel::~LoadChannel() { |
79 | 0 | g_loadchannel_cnt << -1; |
80 | 0 | std::stringstream rows_str; |
81 | 0 | for (const auto& entry : _tablets_channels_rows) { |
82 | 0 | rows_str << ", index id: " << entry.first << ", total_received_rows: " << entry.second.first |
83 | 0 | << ", num_rows_filtered: " << entry.second.second; |
84 | 0 | } |
85 | 0 | LOG(INFO) << "load channel removed" |
86 | 0 | << " load_id=" << _load_id << ", is high priority=" << _is_high_priority |
87 | 0 | << ", sender_ip=" << _sender_ip << rows_str.str(); |
88 | 0 | } |
89 | | |
90 | 0 | void LoadChannel::_init_profile() { |
91 | 0 | _profile = std::make_unique<RuntimeProfile>("LoadChannels"); |
92 | 0 | _mgr_add_batch_timer = ADD_TIMER(_profile, "LoadChannelMgrAddBatchTime"); |
93 | 0 | _handle_mem_limit_timer = ADD_TIMER(_profile, "HandleMemLimitTime"); |
94 | 0 | _self_profile = |
95 | 0 | _profile->create_child(fmt::format("LoadChannel load_id={} (host={}, backend_id={})", |
96 | 0 | _load_id.to_string(), _sender_ip, _backend_id), |
97 | 0 | true, true); |
98 | 0 | _add_batch_number_counter = ADD_COUNTER(_self_profile, "NumberBatchAdded", TUnit::UNIT); |
99 | 0 | _add_batch_timer = ADD_TIMER(_self_profile, "AddBatchTime"); |
100 | 0 | _handle_eos_timer = ADD_CHILD_TIMER(_self_profile, "HandleEosTime", "AddBatchTime"); |
101 | 0 | _add_batch_times = ADD_COUNTER(_self_profile, "AddBatchTimes", TUnit::UNIT); |
102 | 0 | } |
103 | | |
104 | 0 | Status LoadChannel::open(const PTabletWriterOpenRequest& params) { |
105 | 0 | if (config::is_cloud_mode() && params.txn_expiration() <= 0) { |
106 | 0 | return Status::InternalError( |
107 | 0 | "The txn expiration of PTabletWriterOpenRequest is invalid, value={}", |
108 | 0 | params.txn_expiration()); |
109 | 0 | } |
110 | 0 | if (_resource_ctx->workload_group() != nullptr) { |
111 | 0 | RETURN_IF_ERROR(_resource_ctx->workload_group()->add_resource_ctx( |
112 | 0 | _resource_ctx->task_controller()->task_id(), _resource_ctx)); |
113 | 0 | } |
114 | 0 | SCOPED_ATTACH_TASK(_resource_ctx); |
115 | |
|
116 | 0 | int64_t index_id = params.index_id(); |
117 | 0 | std::shared_ptr<BaseTabletsChannel> channel; |
118 | 0 | { |
119 | 0 | std::lock_guard<std::mutex> l(_lock); |
120 | 0 | auto it = _tablets_channels.find(index_id); |
121 | 0 | if (it != _tablets_channels.end()) { |
122 | 0 | channel = it->second; |
123 | 0 | } else { |
124 | | // just for VLOG |
125 | 0 | if (_txn_id == 0) [[unlikely]] { |
126 | 0 | _txn_id = params.txn_id(); |
127 | 0 | } |
128 | | // create a new tablets channel |
129 | 0 | TabletsChannelKey key(params.id(), index_id); |
130 | 0 | BaseStorageEngine& engine = ExecEnv::GetInstance()->storage_engine(); |
131 | 0 | if (config::is_cloud_mode()) { |
132 | 0 | channel = std::make_shared<CloudTabletsChannel>(engine.to_cloud(), key, _load_id, |
133 | 0 | _is_high_priority, _self_profile); |
134 | 0 | } else { |
135 | 0 | channel = std::make_shared<TabletsChannel>(engine.to_local(), key, _load_id, |
136 | 0 | _is_high_priority, _self_profile); |
137 | 0 | } |
138 | 0 | { |
139 | 0 | std::lock_guard<std::mutex> l(_tablets_channels_lock); |
140 | 0 | _tablets_channels.insert({index_id, channel}); |
141 | 0 | } |
142 | 0 | } |
143 | 0 | } |
144 | |
|
145 | 0 | if (params.is_incremental()) { |
146 | | // incremental open would ensure not to open tablet repeatedly |
147 | 0 | RETURN_IF_ERROR(channel->incremental_open(params)); |
148 | 0 | } else { |
149 | 0 | RETURN_IF_ERROR(channel->open(params)); |
150 | 0 | } |
151 | | |
152 | 0 | _opened = true; |
153 | 0 | _last_updated_time.store(time(nullptr)); |
154 | 0 | return Status::OK(); |
155 | 0 | } |
156 | | |
157 | | Status LoadChannel::_get_tablets_channel(std::shared_ptr<BaseTabletsChannel>& channel, |
158 | 0 | bool& is_finished, const int64_t index_id) { |
159 | 0 | std::lock_guard<std::mutex> l(_lock); |
160 | 0 | auto it = _tablets_channels.find(index_id); |
161 | 0 | if (it == _tablets_channels.end()) { |
162 | 0 | if (_finished_channel_ids.find(index_id) != _finished_channel_ids.end()) { |
163 | | // this channel is already finished, just return OK |
164 | 0 | is_finished = true; |
165 | 0 | return Status::OK(); |
166 | 0 | } |
167 | 0 | std::stringstream ss; |
168 | 0 | ss << "load channel " << _load_id << " add batch with unknown index id: " << index_id; |
169 | 0 | return Status::InternalError(ss.str()); |
170 | 0 | } |
171 | | |
172 | 0 | is_finished = false; |
173 | 0 | channel = it->second; |
174 | 0 | return Status::OK(); |
175 | 0 | } |
176 | | |
177 | | Status LoadChannel::add_batch(const PTabletWriterAddBlockRequest& request, |
178 | 0 | PTabletWriterAddBlockResult* response) { |
179 | 0 | DBUG_EXECUTE_IF("LoadChannel.add_batch.failed", |
180 | 0 | { return Status::InternalError("fault injection"); }); |
181 | 0 | SCOPED_TIMER(_add_batch_timer); |
182 | 0 | COUNTER_UPDATE(_add_batch_times, 1); |
183 | 0 | SCOPED_ATTACH_TASK(_resource_ctx); |
184 | 0 | int64_t index_id = request.index_id(); |
185 | | // 1. get tablets channel |
186 | 0 | std::shared_ptr<BaseTabletsChannel> channel; |
187 | 0 | bool is_finished = false; |
188 | 0 | Status st = _get_tablets_channel(channel, is_finished, index_id); |
189 | 0 | if (!st.ok() || is_finished) { |
190 | 0 | return st; |
191 | 0 | } |
192 | | |
193 | | // 2. add block to tablets channel |
194 | 0 | if (request.has_block()) { |
195 | 0 | RETURN_IF_ERROR(channel->add_batch(request, response)); |
196 | 0 | _add_batch_number_counter->update(1); |
197 | 0 | } |
198 | | |
199 | | // 3. handle eos |
200 | | // if channel is incremental, maybe hang on close until all close request arrived. |
201 | 0 | if (request.has_eos() && request.eos()) { |
202 | 0 | st = _handle_eos(channel.get(), request, response); |
203 | 0 | _report_profile(response); |
204 | 0 | if (!st.ok()) { |
205 | 0 | return st; |
206 | 0 | } |
207 | 0 | } else if (_add_batch_number_counter->value() % 100 == 1) { |
208 | 0 | _report_profile(response); |
209 | 0 | } |
210 | 0 | _last_updated_time.store(time(nullptr)); |
211 | 0 | return st; |
212 | 0 | } |
213 | | |
214 | | Status LoadChannel::_handle_eos(BaseTabletsChannel* channel, |
215 | | const PTabletWriterAddBlockRequest& request, |
216 | 0 | PTabletWriterAddBlockResult* response) { |
217 | 0 | _self_profile->add_info_string("EosHost", fmt::format("{}", request.backend_id())); |
218 | 0 | bool finished = false; |
219 | 0 | auto index_id = request.index_id(); |
220 | |
|
221 | 0 | RETURN_IF_ERROR(channel->close(this, request, response, &finished)); |
222 | | |
223 | | // for init node, we close waiting(hang on) all close request and let them return together. |
224 | 0 | if (request.has_hang_wait() && request.hang_wait()) { |
225 | 0 | DCHECK(!channel->is_incremental_channel()); |
226 | 0 | VLOG_DEBUG << fmt::format("txn {}: reciever index {} close waiting by sender {}", _txn_id, |
227 | 0 | request.index_id(), request.sender_id()); |
228 | 0 | int count = 0; |
229 | 0 | while (!channel->is_finished()) { |
230 | 0 | bthread_usleep(1000); |
231 | 0 | count++; |
232 | 0 | } |
233 | | // now maybe finished or cancelled. |
234 | 0 | VLOG_TRACE << "reciever close wait finished!" << request.sender_id(); |
235 | 0 | if (count >= 1000 * _timeout_s) { // maybe config::streaming_load_rpc_max_alive_time_sec |
236 | 0 | return Status::InternalError("Tablets channel didn't wait all close"); |
237 | 0 | } |
238 | 0 | } |
239 | | |
240 | 0 | if (finished) { |
241 | 0 | std::lock_guard<std::mutex> l(_lock); |
242 | 0 | { |
243 | 0 | std::lock_guard<std::mutex> l(_tablets_channels_lock); |
244 | 0 | _tablets_channels_rows.insert(std::make_pair( |
245 | 0 | index_id, |
246 | 0 | std::make_pair(channel->total_received_rows(), channel->num_rows_filtered()))); |
247 | 0 | _tablets_channels.erase(index_id); |
248 | 0 | } |
249 | 0 | LOG(INFO) << "txn " << _txn_id << " closed tablets_channel " << index_id; |
250 | 0 | _finished_channel_ids.emplace(index_id); |
251 | 0 | } |
252 | 0 | return Status::OK(); |
253 | 0 | } |
254 | | |
255 | 0 | void LoadChannel::_report_profile(PTabletWriterAddBlockResult* response) { |
256 | 0 | if (!_enable_profile) { |
257 | 0 | return; |
258 | 0 | } |
259 | | |
260 | | // TabletSink and LoadChannel in BE are M: N relationship, |
261 | | // Every once in a while LoadChannel will randomly return its own runtime profile to a TabletSink, |
262 | | // so usually all LoadChannel runtime profiles are saved on each TabletSink, |
263 | | // and the timeliness of the same LoadChannel profile saved on different TabletSinks is different, |
264 | | // and each TabletSink will periodically send fe reports all the LoadChannel profiles saved by itself, |
265 | | // and ensures to update the latest LoadChannel profile according to the timestamp. |
266 | 0 | _self_profile->set_timestamp(_last_updated_time); |
267 | |
|
268 | 0 | { |
269 | 0 | std::lock_guard<std::mutex> l(_tablets_channels_lock); |
270 | 0 | for (auto& it : _tablets_channels) { |
271 | 0 | it.second->refresh_profile(); |
272 | 0 | } |
273 | 0 | } |
274 | |
|
275 | 0 | TRuntimeProfileTree tprofile; |
276 | 0 | ThriftSerializer ser(false, 4096); |
277 | 0 | uint8_t* buf = nullptr; |
278 | 0 | uint32_t len = 0; |
279 | 0 | std::lock_guard<std::mutex> l(_profile_serialize_lock); |
280 | 0 | _profile->to_thrift(&tprofile); |
281 | 0 | auto st = ser.serialize(&tprofile, &len, &buf); |
282 | 0 | if (st.ok()) { |
283 | 0 | response->set_load_channel_profile(std::string((const char*)buf, len)); |
284 | 0 | } else { |
285 | 0 | LOG(WARNING) << "load channel TRuntimeProfileTree serialize failed, errmsg=" << st; |
286 | 0 | } |
287 | 0 | } |
288 | | |
289 | 0 | bool LoadChannel::is_finished() { |
290 | 0 | if (!_opened) { |
291 | 0 | return false; |
292 | 0 | } |
293 | 0 | std::lock_guard<std::mutex> l(_lock); |
294 | 0 | return _tablets_channels.empty(); |
295 | 0 | } |
296 | | |
297 | 0 | Status LoadChannel::cancel() { |
298 | 0 | std::lock_guard<std::mutex> l(_lock); |
299 | 0 | for (auto& it : _tablets_channels) { |
300 | 0 | static_cast<void>(it.second->cancel()); |
301 | 0 | } |
302 | 0 | return Status::OK(); |
303 | 0 | } |
304 | | |
305 | | } // namespace doris |