be/src/exec/sink/load_stream_stub.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/sink/load_stream_stub.h" |
19 | | |
20 | | #include <sstream> |
21 | | |
22 | | #include "common/cast_set.h" |
23 | | #include "runtime/query_context.h" |
24 | | #include "storage/rowset/rowset_writer.h" |
25 | | #include "util/brpc_client_cache.h" |
26 | | #include "util/debug_points.h" |
27 | | #include "util/network_util.h" |
28 | | #include "util/thrift_util.h" |
29 | | #include "util/uid_util.h" |
30 | | |
31 | | namespace doris { |
32 | | |
33 | | int LoadStreamReplyHandler::on_received_messages(brpc::StreamId id, butil::IOBuf* const messages[], |
34 | 3.99k | size_t size) { |
35 | 3.99k | auto stub = _stub.lock(); |
36 | 3.99k | if (!stub) { |
37 | 0 | LOG(WARNING) << "stub is not exist when on_received_messages, " << *this |
38 | 0 | << ", stream_id=" << id; |
39 | 0 | return 0; |
40 | 0 | } |
41 | 7.99k | for (size_t i = 0; i < size; i++) { |
42 | 3.99k | butil::IOBufAsZeroCopyInputStream wrapper(*messages[i]); |
43 | 3.99k | PLoadStreamResponse response; |
44 | 3.99k | response.ParseFromZeroCopyStream(&wrapper); |
45 | | |
46 | 3.99k | if (response.eos()) { |
47 | 3.99k | stub->_is_eos.store(true); |
48 | 3.99k | } |
49 | | |
50 | 3.99k | Status st = Status::create<false>(response.status()); |
51 | | |
52 | 3.99k | std::stringstream ss; |
53 | 3.99k | ss << "on_received_messages, " << *this << ", stream_id=" << id; |
54 | 3.99k | if (response.success_tablet_ids_size() > 0) { |
55 | 2.02k | ss << ", success tablet ids:"; |
56 | 13.4k | for (auto tablet_id : response.success_tablet_ids()) { |
57 | 13.4k | ss << " " << tablet_id; |
58 | 13.4k | } |
59 | 2.02k | std::lock_guard<bthread::Mutex> lock(stub->_success_tablets_mutex); |
60 | 13.4k | for (auto tablet_id : response.success_tablet_ids()) { |
61 | 13.4k | stub->_success_tablets.push_back(tablet_id); |
62 | 13.4k | } |
63 | 2.02k | } |
64 | 3.99k | if (response.failed_tablets_size() > 0) { |
65 | 0 | ss << ", failed tablet ids:"; |
66 | 0 | for (auto pb : response.failed_tablets()) { |
67 | 0 | ss << " " << pb.id() << ":" << Status::create(pb.status()); |
68 | 0 | } |
69 | 0 | std::lock_guard<bthread::Mutex> lock(stub->_failed_tablets_mutex); |
70 | 0 | for (auto pb : response.failed_tablets()) { |
71 | 0 | stub->_failed_tablets.emplace(pb.id(), Status::create(pb.status())); |
72 | 0 | } |
73 | 0 | } |
74 | 3.99k | if (response.tablet_schemas_size() > 0) { |
75 | 4 | ss << ", tablet schema num: " << response.tablet_schemas_size(); |
76 | 4 | std::lock_guard<bthread::Mutex> lock(stub->_schema_mutex); |
77 | 4 | for (const auto& schema : response.tablet_schemas()) { |
78 | 4 | auto tablet_schema = std::make_unique<TabletSchema>(); |
79 | 4 | tablet_schema->init_from_pb(schema.tablet_schema()); |
80 | 4 | stub->_tablet_schema_for_index->emplace(schema.index_id(), |
81 | 4 | std::move(tablet_schema)); |
82 | 4 | stub->_enable_unique_mow_for_index->emplace( |
83 | 4 | schema.index_id(), schema.enable_unique_key_merge_on_write()); |
84 | 4 | } |
85 | 4 | stub->_schema_cv.notify_all(); |
86 | 4 | } |
87 | 3.99k | ss << ", status: " << st; |
88 | 3.99k | LOG(INFO) << ss.str(); |
89 | | |
90 | 3.99k | if (response.tablet_load_rowset_num_infos_size() > 0) { |
91 | 0 | stub->_refresh_back_pressure_version_wait_time(response.tablet_load_rowset_num_infos()); |
92 | 0 | } |
93 | | |
94 | 3.99k | if (response.has_load_stream_profile()) { |
95 | 0 | TRuntimeProfileTree tprofile; |
96 | 0 | const uint8_t* buf = |
97 | 0 | reinterpret_cast<const uint8_t*>(response.load_stream_profile().data()); |
98 | 0 | uint32_t len = cast_set<uint32_t>(response.load_stream_profile().size()); |
99 | 0 | auto status = deserialize_thrift_msg(buf, &len, false, &tprofile); |
100 | 0 | if (status.ok()) { |
101 | | // TODO |
102 | | //_sink->_state->load_channel_profile()->update(tprofile); |
103 | 0 | } else { |
104 | 0 | LOG(WARNING) << "load stream TRuntimeProfileTree deserialize failed, errmsg=" |
105 | 0 | << status; |
106 | 0 | } |
107 | 0 | } |
108 | 3.99k | } |
109 | 3.99k | return 0; |
110 | 3.99k | } |
111 | | |
112 | 3.99k | void LoadStreamReplyHandler::on_closed(brpc::StreamId id) { |
113 | 3.99k | Defer defer {[this]() { delete this; }}; |
114 | 3.99k | LOG(INFO) << "on_closed, " << *this << ", stream_id=" << id; |
115 | 3.99k | auto stub = _stub.lock(); |
116 | 3.99k | if (!stub) { |
117 | 0 | LOG(WARNING) << "stub is not exist when on_closed, " << *this; |
118 | 0 | return; |
119 | 0 | } |
120 | 3.99k | stub->_is_closed.store(true); |
121 | 3.99k | } |
122 | | |
123 | 7.99k | inline std::ostream& operator<<(std::ostream& ostr, const LoadStreamReplyHandler& handler) { |
124 | 7.99k | ostr << "LoadStreamReplyHandler load_id=" << UniqueId(handler._load_id) |
125 | 7.99k | << ", dst_id=" << handler._dst_id; |
126 | 7.99k | return ostr; |
127 | 7.99k | } |
128 | | |
129 | | LoadStreamStub::LoadStreamStub(PUniqueId load_id, int64_t src_id, |
130 | | std::shared_ptr<IndexToTabletSchema> schema_map, |
131 | | std::shared_ptr<IndexToEnableMoW> mow_map, bool incremental) |
132 | 4.05k | : _load_id(load_id), |
133 | 4.05k | _src_id(src_id), |
134 | 4.05k | _tablet_schema_for_index(schema_map), |
135 | 4.05k | _enable_unique_mow_for_index(mow_map), |
136 | 4.05k | _is_incremental(incremental) {}; |
137 | | |
138 | 4.05k | LoadStreamStub::~LoadStreamStub() { |
139 | 4.05k | if (_is_open.load() && !_is_closed.load()) { |
140 | 0 | auto ret = brpc::StreamClose(_stream_id); |
141 | 0 | LOG(INFO) << *this << " is deconstructed, close " << (ret == 0 ? "success" : "failed"); |
142 | 0 | } |
143 | 4.05k | } |
144 | | |
145 | | // open_load_stream |
146 | | Status LoadStreamStub::open(BrpcClientCache<PBackendService_Stub>* client_cache, |
147 | | const NodeInfo& node_info, int64_t txn_id, |
148 | | const OlapTableSchemaParam& schema, |
149 | | const std::vector<PTabletID>& tablets_for_schema, int total_streams, |
150 | 10.8k | int64_t idle_timeout_ms, bool enable_profile) { |
151 | 10.8k | std::unique_lock<bthread::Mutex> lock(_open_mutex); |
152 | 10.8k | if (_is_init.load()) { |
153 | 6.84k | return _status; |
154 | 6.84k | } |
155 | 3.98k | _is_init.store(true); |
156 | 3.98k | _dst_id = node_info.id; |
157 | 3.98k | brpc::StreamOptions opt; |
158 | 3.98k | opt.max_buf_size = cast_set<int>(config::load_stream_max_buf_size); |
159 | 3.98k | opt.idle_timeout_ms = idle_timeout_ms; |
160 | 3.98k | opt.messages_in_batch = config::load_stream_messages_in_batch; |
161 | 3.98k | opt.handler = new LoadStreamReplyHandler(_load_id, _dst_id, shared_from_this()); |
162 | 3.98k | brpc::Controller cntl; |
163 | 3.98k | if (int ret = brpc::StreamCreate(&_stream_id, cntl, &opt)) { |
164 | 0 | delete opt.handler; |
165 | 0 | _status = Status::Error<true>(ret, "Failed to create stream"); |
166 | 0 | return _status; |
167 | 0 | } |
168 | 3.98k | cntl.set_timeout_ms(config::open_load_stream_timeout_ms); |
169 | 3.98k | POpenLoadStreamRequest request; |
170 | 3.98k | *request.mutable_load_id() = _load_id; |
171 | 3.98k | request.set_src_id(_src_id); |
172 | 3.98k | request.set_txn_id(txn_id); |
173 | 3.98k | request.set_enable_profile(enable_profile); |
174 | 3.98k | if (_is_incremental) { |
175 | 0 | request.set_total_streams(0); |
176 | 3.99k | } else if (total_streams > 0) { |
177 | 3.99k | request.set_total_streams(total_streams); |
178 | 18.4E | } else { |
179 | 18.4E | _status = Status::InternalError("total_streams should be greator than 0"); |
180 | 18.4E | return _status; |
181 | 18.4E | } |
182 | 3.99k | request.set_idle_timeout_ms(idle_timeout_ms); |
183 | 3.99k | schema.to_protobuf(request.mutable_schema()); |
184 | 3.99k | for (auto& tablet : tablets_for_schema) { |
185 | 2.05k | *request.add_tablets() = tablet; |
186 | 2.05k | } |
187 | 3.99k | POpenLoadStreamResponse response; |
188 | | // set connection_group "streaming" to distinguish with non-streaming connections |
189 | 3.99k | const auto& stub = client_cache->get_client(node_info.host, node_info.brpc_port); |
190 | 3.99k | if (stub == nullptr) { |
191 | 0 | return Status::InternalError("failed to init brpc client to {}:{}", node_info.host, |
192 | 0 | node_info.brpc_port); |
193 | 0 | } |
194 | 3.99k | stub->open_load_stream(&cntl, &request, &response, nullptr); |
195 | 3.99k | for (const auto& resp : response.tablet_schemas()) { |
196 | 2.05k | auto tablet_schema = std::make_unique<TabletSchema>(); |
197 | 2.05k | tablet_schema->init_from_pb(resp.tablet_schema()); |
198 | 2.05k | _tablet_schema_for_index->emplace(resp.index_id(), std::move(tablet_schema)); |
199 | 2.05k | _enable_unique_mow_for_index->emplace(resp.index_id(), |
200 | 2.05k | resp.enable_unique_key_merge_on_write()); |
201 | 2.05k | } |
202 | 3.99k | if (response.tablet_load_rowset_num_infos_size() > 0) { |
203 | 0 | _refresh_back_pressure_version_wait_time(response.tablet_load_rowset_num_infos()); |
204 | 0 | } |
205 | 3.99k | if (cntl.Failed()) { |
206 | 0 | brpc::StreamClose(_stream_id); |
207 | 0 | _status = Status::InternalError("Failed to connect to backend {}: {}", _dst_id, |
208 | 0 | cntl.ErrorText()); |
209 | 0 | return _status; |
210 | 0 | } |
211 | 3.99k | LOG(INFO) << "open load stream to host=" << node_info.host << ", port=" << node_info.brpc_port |
212 | 3.99k | << ", " << *this; |
213 | 3.99k | _is_open.store(true); |
214 | 3.99k | _status = Status::OK(); |
215 | 3.99k | return _status; |
216 | 3.99k | } |
217 | | |
218 | | // APPEND_DATA |
219 | | Status LoadStreamStub::append_data(int64_t partition_id, int64_t index_id, int64_t tablet_id, |
220 | | int32_t segment_id, uint64_t offset, std::span<const Slice> data, |
221 | 104k | bool segment_eos, FileType file_type) { |
222 | 104k | if (!_is_open.load()) { |
223 | 0 | add_failed_tablet(tablet_id, _status); |
224 | 0 | return _status; |
225 | 0 | } |
226 | 104k | DBUG_EXECUTE_IF("LoadStreamStub.skip_send_segment", { return Status::OK(); }); |
227 | 104k | PStreamHeader header; |
228 | 104k | header.set_src_id(_src_id); |
229 | 104k | *header.mutable_load_id() = _load_id; |
230 | 104k | header.set_partition_id(partition_id); |
231 | 104k | header.set_index_id(index_id); |
232 | 104k | header.set_tablet_id(tablet_id); |
233 | 104k | header.set_segment_id(segment_id); |
234 | 104k | header.set_segment_eos(segment_eos); |
235 | 104k | header.set_offset(offset); |
236 | 104k | header.set_opcode(doris::PStreamHeader::APPEND_DATA); |
237 | 104k | header.set_file_type(file_type); |
238 | 104k | return _encode_and_send(header, data); |
239 | 104k | } |
240 | | |
241 | | // ADD_SEGMENT |
242 | | Status LoadStreamStub::add_segment(int64_t partition_id, int64_t index_id, int64_t tablet_id, |
243 | 3.64k | int32_t segment_id, const SegmentStatistics& segment_stat) { |
244 | 3.64k | if (!_is_open.load()) { |
245 | 0 | add_failed_tablet(tablet_id, _status); |
246 | 0 | return _status; |
247 | 0 | } |
248 | 3.64k | DBUG_EXECUTE_IF("LoadStreamStub.skip_send_segment", { return Status::OK(); }); |
249 | 3.64k | PStreamHeader header; |
250 | 3.64k | header.set_src_id(_src_id); |
251 | 3.64k | *header.mutable_load_id() = _load_id; |
252 | 3.64k | header.set_partition_id(partition_id); |
253 | 3.64k | header.set_index_id(index_id); |
254 | 3.64k | header.set_tablet_id(tablet_id); |
255 | 3.64k | header.set_segment_id(segment_id); |
256 | 3.64k | header.set_opcode(doris::PStreamHeader::ADD_SEGMENT); |
257 | 3.64k | segment_stat.to_pb(header.mutable_segment_statistics()); |
258 | 3.64k | return _encode_and_send(header); |
259 | 3.64k | } |
260 | | |
261 | | // CLOSE_LOAD |
262 | | Status LoadStreamStub::close_load(const std::vector<PTabletID>& tablets_to_commit, |
263 | 3.99k | int num_incremental_streams) { |
264 | 3.99k | if (!_is_open.load()) { |
265 | 0 | return _status; |
266 | 0 | } |
267 | 3.99k | PStreamHeader header; |
268 | 3.99k | *header.mutable_load_id() = _load_id; |
269 | 3.99k | header.set_src_id(_src_id); |
270 | 3.99k | header.set_opcode(doris::PStreamHeader::CLOSE_LOAD); |
271 | 13.4k | for (const auto& tablet : tablets_to_commit) { |
272 | 13.4k | *header.add_tablets() = tablet; |
273 | 13.4k | } |
274 | 3.99k | header.set_num_incremental_streams(num_incremental_streams); |
275 | 3.99k | _status = _encode_and_send(header); |
276 | 3.99k | if (!_status.ok()) { |
277 | 0 | LOG(WARNING) << "stream " << _stream_id << " close failed: " << _status; |
278 | 0 | return _status; |
279 | 0 | } |
280 | 3.99k | _is_closing.store(true); |
281 | 3.99k | return Status::OK(); |
282 | 3.99k | } |
283 | | |
284 | | // GET_SCHEMA |
285 | 4 | Status LoadStreamStub::get_schema(const std::vector<PTabletID>& tablets) { |
286 | 4 | if (!_is_open.load()) { |
287 | 0 | return _status; |
288 | 0 | } |
289 | 4 | PStreamHeader header; |
290 | 4 | *header.mutable_load_id() = _load_id; |
291 | 4 | header.set_src_id(_src_id); |
292 | 4 | header.set_opcode(doris::PStreamHeader::GET_SCHEMA); |
293 | 4 | std::ostringstream oss; |
294 | 4 | oss << "fetching tablet schema from stream " << _stream_id |
295 | 4 | << ", load id: " << print_id(_load_id) << ", tablet id:"; |
296 | 4 | for (const auto& tablet : tablets) { |
297 | 4 | *header.add_tablets() = tablet; |
298 | 4 | oss << " " << tablet.tablet_id(); |
299 | 4 | } |
300 | 4 | if (tablets.size() == 0) { |
301 | 0 | oss << " none"; |
302 | 0 | } |
303 | 4 | LOG(INFO) << oss.str(); |
304 | 4 | return _encode_and_send(header); |
305 | 4 | } |
306 | | |
307 | | Status LoadStreamStub::wait_for_schema(int64_t partition_id, int64_t index_id, int64_t tablet_id, |
308 | 3.66k | int64_t timeout_ms) { |
309 | 3.66k | if (!_is_open.load()) { |
310 | 0 | return _status; |
311 | 0 | } |
312 | 3.66k | if (_tablet_schema_for_index->contains(index_id)) { |
313 | 3.65k | return Status::OK(); |
314 | 3.65k | } |
315 | 4 | PTabletID tablet; |
316 | 4 | tablet.set_partition_id(partition_id); |
317 | 4 | tablet.set_index_id(index_id); |
318 | 4 | tablet.set_tablet_id(tablet_id); |
319 | 4 | RETURN_IF_ERROR(get_schema({tablet})); |
320 | | |
321 | 4 | MonotonicStopWatch watch; |
322 | 4 | watch.start(); |
323 | 8 | while (!_tablet_schema_for_index->contains(index_id) && |
324 | 8 | watch.elapsed_time() / 1000 / 1000 < timeout_ms) { |
325 | 4 | RETURN_IF_ERROR(check_cancel()); |
326 | 4 | static_cast<void>(wait_for_new_schema(100)); |
327 | 4 | } |
328 | | |
329 | 4 | if (!_tablet_schema_for_index->contains(index_id)) { |
330 | 0 | return Status::TimedOut("timeout to get tablet schema for index {}", index_id); |
331 | 0 | } |
332 | 4 | return Status::OK(); |
333 | 4 | } |
334 | | |
335 | 62.3k | Status LoadStreamStub::close_finish_check(RuntimeState* state, bool* is_closed) { |
336 | 62.3k | DBUG_EXECUTE_IF("LoadStreamStub::close_wait.long_wait", DBUG_BLOCK); |
337 | 62.3k | DBUG_EXECUTE_IF("LoadStreamStub::close_finish_check.close_failed", |
338 | 62.3k | { return Status::InternalError("close failed"); }); |
339 | 62.3k | *is_closed = true; |
340 | 62.3k | if (!_is_open.load()) { |
341 | | // we don't need to close wait on non-open streams |
342 | 0 | return Status::OK(); |
343 | 0 | } |
344 | | // If stream is cancelled (e.g., due to connection failure), treat it as closed |
345 | | // to avoid waiting indefinitely for a stream that will never respond. |
346 | 62.3k | if (_is_cancelled.load()) { |
347 | 0 | return check_cancel(); |
348 | 0 | } |
349 | 62.3k | if (state->get_query_ctx()->is_cancelled()) { |
350 | 0 | return state->get_query_ctx()->exec_status(); |
351 | 0 | } |
352 | 62.3k | if (!_is_closing.load()) { |
353 | 28.3k | *is_closed = false; |
354 | 28.3k | return _status; |
355 | 28.3k | } |
356 | 34.0k | if (_is_closed.load()) { |
357 | 21.5k | RETURN_IF_ERROR(check_cancel()); |
358 | 21.5k | if (!_is_eos.load()) { |
359 | 0 | return Status::InternalError("Stream closed without EOS, {}", to_string()); |
360 | 0 | } |
361 | 21.5k | return Status::OK(); |
362 | 21.5k | } |
363 | 12.4k | *is_closed = false; |
364 | 12.4k | return Status::OK(); |
365 | 34.0k | } |
366 | | |
367 | 32 | void LoadStreamStub::cancel(Status reason) { |
368 | 32 | LOG(WARNING) << *this << " is cancelled because of " << reason; |
369 | 32 | if (_is_open.load()) { |
370 | 32 | brpc::StreamClose(_stream_id); |
371 | 32 | } |
372 | 32 | { |
373 | 32 | std::lock_guard<bthread::Mutex> lock(_cancel_mutex); |
374 | 32 | _cancel_st = reason; |
375 | 32 | _is_cancelled.store(true); |
376 | 32 | } |
377 | 32 | _is_closed.store(true); |
378 | 32 | } |
379 | | |
380 | 112k | Status LoadStreamStub::_encode_and_send(PStreamHeader& header, std::span<const Slice> data) { |
381 | 112k | butil::IOBuf buf; |
382 | 112k | size_t header_len = header.ByteSizeLong(); |
383 | 112k | buf.append(reinterpret_cast<uint8_t*>(&header_len), sizeof(header_len)); |
384 | 112k | buf.append(header.SerializeAsString()); |
385 | 112k | size_t data_len = std::transform_reduce(data.begin(), data.end(), 0, std::plus(), |
386 | 340k | [](const Slice& s) { return s.get_size(); }); |
387 | 112k | buf.append(reinterpret_cast<uint8_t*>(&data_len), sizeof(data_len)); |
388 | 340k | for (const auto& slice : data) { |
389 | 340k | buf.append(slice.get_data(), slice.get_size()); |
390 | 340k | } |
391 | 112k | bool eos = header.opcode() == doris::PStreamHeader::CLOSE_LOAD; |
392 | 112k | bool get_schema = header.opcode() == doris::PStreamHeader::GET_SCHEMA; |
393 | 112k | add_bytes_written(buf.size()); |
394 | 112k | return _send_with_buffer(buf, eos || get_schema); |
395 | 112k | } |
396 | | |
397 | 112k | Status LoadStreamStub::_send_with_buffer(butil::IOBuf& buf, bool sync) { |
398 | 112k | butil::IOBuf output; |
399 | 112k | std::unique_lock<decltype(_buffer_mutex)> buffer_lock(_buffer_mutex); |
400 | 112k | _buffer.append(buf); |
401 | 112k | if (!sync && _buffer.size() < config::brpc_streaming_client_batch_bytes) { |
402 | 108k | return Status::OK(); |
403 | 108k | } |
404 | 3.94k | output.swap(_buffer); |
405 | | // acquire send lock while holding buffer lock, to ensure the message order |
406 | 3.94k | std::lock_guard<decltype(_send_mutex)> send_lock(_send_mutex); |
407 | 3.94k | buffer_lock.unlock(); |
408 | 18.4E | VLOG_DEBUG << "send buf size : " << output.size() << ", sync: " << sync; |
409 | 3.94k | auto st = _send_with_retry(output); |
410 | 3.94k | if (!st.ok()) { |
411 | 0 | _handle_failure(output, st); |
412 | 0 | } |
413 | 3.94k | return st; |
414 | 112k | } |
415 | | |
416 | 0 | void LoadStreamStub::_handle_failure(butil::IOBuf& buf, Status st) { |
417 | 0 | while (buf.size() > 0) { |
418 | | // step 1: parse header |
419 | 0 | size_t hdr_len = 0; |
420 | 0 | buf.cutn((void*)&hdr_len, sizeof(size_t)); |
421 | 0 | butil::IOBuf hdr_buf; |
422 | 0 | PStreamHeader hdr; |
423 | 0 | buf.cutn(&hdr_buf, hdr_len); |
424 | 0 | butil::IOBufAsZeroCopyInputStream wrapper(hdr_buf); |
425 | 0 | hdr.ParseFromZeroCopyStream(&wrapper); |
426 | | |
427 | | // step 2: cut data |
428 | 0 | size_t data_len = 0; |
429 | 0 | buf.cutn((void*)&data_len, sizeof(size_t)); |
430 | 0 | butil::IOBuf data_buf; |
431 | 0 | buf.cutn(&data_buf, data_len); |
432 | | |
433 | | // step 3: handle failure |
434 | 0 | switch (hdr.opcode()) { |
435 | 0 | case PStreamHeader::ADD_SEGMENT: |
436 | 0 | case PStreamHeader::APPEND_DATA: { |
437 | 0 | DBUG_EXECUTE_IF("LoadStreamStub._handle_failure.append_data_failed", { |
438 | 0 | add_failed_tablet(hdr.tablet_id(), st); |
439 | 0 | return; |
440 | 0 | }); |
441 | 0 | DBUG_EXECUTE_IF("LoadStreamStub._handle_failure.add_segment_failed", { |
442 | 0 | add_failed_tablet(hdr.tablet_id(), st); |
443 | 0 | return; |
444 | 0 | }); |
445 | 0 | add_failed_tablet(hdr.tablet_id(), st); |
446 | 0 | } break; |
447 | 0 | case PStreamHeader::CLOSE_LOAD: { |
448 | 0 | DBUG_EXECUTE_IF("LoadStreamStub._handle_failure.close_load_failed", { |
449 | 0 | brpc::StreamClose(_stream_id); |
450 | 0 | return; |
451 | 0 | }); |
452 | 0 | brpc::StreamClose(_stream_id); |
453 | 0 | } break; |
454 | 0 | case PStreamHeader::GET_SCHEMA: { |
455 | 0 | DBUG_EXECUTE_IF("LoadStreamStub._handle_failure.get_schema_failed", { |
456 | | // Just log and let wait_for_schema timeout |
457 | 0 | std::ostringstream oss; |
458 | 0 | for (const auto& tablet : hdr.tablets()) { |
459 | 0 | oss << " " << tablet.tablet_id(); |
460 | 0 | } |
461 | 0 | LOG(WARNING) << "failed to send GET_SCHEMA request, tablet_id:" << oss.str() << ", " |
462 | 0 | << *this; |
463 | 0 | return; |
464 | 0 | }); |
465 | | // Just log and let wait_for_schema timeout |
466 | 0 | std::ostringstream oss; |
467 | 0 | for (const auto& tablet : hdr.tablets()) { |
468 | 0 | oss << " " << tablet.tablet_id(); |
469 | 0 | } |
470 | 0 | LOG(WARNING) << "failed to send GET_SCHEMA request, tablet_id:" << oss.str() << ", " |
471 | 0 | << *this; |
472 | 0 | } break; |
473 | 0 | default: |
474 | 0 | LOG(WARNING) << "unexpected stream message " << hdr.opcode() << ", " << *this; |
475 | 0 | DCHECK(false); |
476 | 0 | } |
477 | 0 | } |
478 | 0 | } |
479 | | |
480 | 3.99k | Status LoadStreamStub::_send_with_retry(butil::IOBuf& buf) { |
481 | 3.99k | for (;;) { |
482 | 3.99k | RETURN_IF_ERROR(check_cancel()); |
483 | 3.99k | int ret; |
484 | 3.99k | { |
485 | 3.99k | DBUG_EXECUTE_IF("LoadStreamStub._send_with_retry.delay_before_send", { |
486 | 3.99k | int64_t delay_ms = dp->param<int64_t>("delay_ms", 1000); |
487 | 3.99k | bthread_usleep(delay_ms * 1000); |
488 | 3.99k | }); |
489 | 3.99k | brpc::StreamWriteOptions options; |
490 | 3.99k | options.write_in_background = config::enable_brpc_stream_write_background; |
491 | 3.99k | ret = brpc::StreamWrite(_stream_id, buf, &options); |
492 | 3.99k | } |
493 | 3.99k | DBUG_EXECUTE_IF("LoadStreamStub._send_with_retry.stream_write_failed", { ret = EPIPE; }); |
494 | 3.99k | switch (ret) { |
495 | 3.99k | case 0: |
496 | 3.99k | return Status::OK(); |
497 | 0 | case EAGAIN: { |
498 | 0 | const timespec time = butil::seconds_from_now(config::load_stream_eagain_wait_seconds); |
499 | 0 | int wait_ret = brpc::StreamWait(_stream_id, &time); |
500 | 0 | if (wait_ret != 0) { |
501 | 0 | return Status::InternalError("StreamWait failed, err={}, {}", wait_ret, |
502 | 0 | to_string()); |
503 | 0 | } |
504 | 0 | break; |
505 | 0 | } |
506 | 0 | default: |
507 | 0 | return Status::InternalError("StreamWrite failed, err={}, {}", ret, to_string()); |
508 | 3.99k | } |
509 | 3.99k | } |
510 | 3.99k | } |
511 | | |
512 | | void LoadStreamStub::_refresh_back_pressure_version_wait_time( |
513 | | const ::google::protobuf::RepeatedPtrField<::doris::PTabletLoadRowsetInfo>& |
514 | 0 | tablet_load_infos) { |
515 | 0 | int64_t max_rowset_num_gap = 0; |
516 | | // if any one tablet is under high load pressure, we would make the whole procedure |
517 | | // sleep to prevent the corresponding BE return -235 |
518 | 0 | std::for_each( |
519 | 0 | tablet_load_infos.begin(), tablet_load_infos.end(), |
520 | 0 | [&max_rowset_num_gap](auto& load_info) { |
521 | 0 | int64_t cur_rowset_num = load_info.current_rowset_nums(); |
522 | 0 | int64_t high_load_point = load_info.max_config_rowset_nums() * |
523 | 0 | (config::load_back_pressure_version_threshold / 100); |
524 | 0 | DCHECK(cur_rowset_num > high_load_point); |
525 | 0 | max_rowset_num_gap = std::max(max_rowset_num_gap, cur_rowset_num - high_load_point); |
526 | 0 | }); |
527 | | // to slow down the high load pressure |
528 | | // we would use the rowset num gap to calculate one sleep time |
529 | | // for example: |
530 | | // if the max tablet version is 2000, there are 3 BE |
531 | | // A: ==================== 1800 |
532 | | // B: =================== 1700 |
533 | | // C: ================== 1600 |
534 | | // ================== 1600 |
535 | | // ^ |
536 | | // the high load point |
537 | | // then then max gap is 1800 - (max tablet version * config::load_back_pressure_version_threshold / 100) = 200, |
538 | | // we would make the whole send procesure sleep |
539 | | // 1200ms for compaction to be done toe reduce the high pressure |
540 | 0 | auto max_time = config::max_load_back_pressure_version_wait_time_ms; |
541 | 0 | if (UNLIKELY(max_rowset_num_gap > 0)) { |
542 | 0 | _load_back_pressure_version_wait_time_ms.store( |
543 | 0 | std::min(max_rowset_num_gap + 1000, max_time)); |
544 | 0 | LOG(INFO) << "try to back pressure version, wait time(ms): " |
545 | 0 | << _load_back_pressure_version_wait_time_ms << ", load id: " << print_id(_load_id) |
546 | 0 | << ", max_rowset_num_gap: " << max_rowset_num_gap; |
547 | 0 | } |
548 | 0 | } |
549 | | |
550 | 0 | std::string LoadStreamStub::to_string() { |
551 | 0 | std::ostringstream ss; |
552 | 0 | ss << *this; |
553 | 0 | return ss.str(); |
554 | 0 | } |
555 | | |
556 | 4.02k | inline std::ostream& operator<<(std::ostream& ostr, const LoadStreamStub& stub) { |
557 | 4.02k | ostr << "LoadStreamStub load_id=" << print_id(stub._load_id) << ", src_id=" << stub._src_id |
558 | 4.02k | << ", dst_id=" << stub._dst_id << ", stream_id=" << stub._stream_id; |
559 | 4.02k | return ostr; |
560 | 4.02k | } |
561 | | |
562 | | Status LoadStreamStubs::open(BrpcClientCache<PBackendService_Stub>* client_cache, |
563 | | const NodeInfo& node_info, int64_t txn_id, |
564 | | const OlapTableSchemaParam& schema, |
565 | | const std::vector<PTabletID>& tablets_for_schema, int total_streams, |
566 | 5.47k | int64_t idle_timeout_ms, bool enable_profile) { |
567 | 5.47k | bool get_schema = true; |
568 | 5.47k | auto status = Status::OK(); |
569 | 5.47k | bool first_stream = true; |
570 | 10.8k | for (auto& stream : _streams) { |
571 | 10.8k | Status st; |
572 | 10.8k | if (get_schema) { |
573 | 5.46k | st = stream->open(client_cache, node_info, txn_id, schema, tablets_for_schema, |
574 | 5.46k | total_streams, idle_timeout_ms, enable_profile); |
575 | 5.46k | } else { |
576 | 5.35k | st = stream->open(client_cache, node_info, txn_id, schema, {}, total_streams, |
577 | 5.35k | idle_timeout_ms, enable_profile); |
578 | 5.35k | } |
579 | | // Simulate one stream open failure within LoadStreamStubs. |
580 | | // This causes the successfully opened streams to be cancelled, |
581 | | // reproducing the bug where cancelled streams cause close_wait timeout. |
582 | 10.8k | DBUG_EXECUTE_IF("LoadStreamStubs.open.fail_one_stream", { |
583 | 10.8k | if (st.ok() && !first_stream) { |
584 | 10.8k | st = Status::InternalError("Injected stream open failure"); |
585 | 10.8k | } |
586 | 10.8k | }); |
587 | 10.8k | if (st.ok()) { |
588 | 10.8k | get_schema = false; |
589 | 18.4E | } else { |
590 | 18.4E | LOG(WARNING) << "open stream failed: " << st << "; stream: " << *stream; |
591 | 18.4E | status = st; |
592 | | // no break here to try get schema from the rest streams |
593 | 18.4E | } |
594 | 10.8k | first_stream = false; |
595 | 10.8k | } |
596 | | // only mark open when all streams open success |
597 | 5.47k | _open_success.store(status.ok()); |
598 | | // cancel all streams if open failed |
599 | 5.47k | if (!status.ok()) { |
600 | 0 | cancel(status); |
601 | 0 | } |
602 | 5.47k | return status; |
603 | 5.47k | } |
604 | | |
605 | | Status LoadStreamStubs::close_load(const std::vector<PTabletID>& tablets_to_commit, |
606 | 2.05k | int num_incremental_streams) { |
607 | 2.05k | if (!_open_success.load()) { |
608 | 0 | return Status::InternalError("streams not open"); |
609 | 0 | } |
610 | 2.05k | bool first = true; |
611 | 2.05k | auto status = Status::OK(); |
612 | 3.99k | for (auto& stream : _streams) { |
613 | 3.99k | Status st; |
614 | 3.99k | if (first) { |
615 | 2.05k | st = stream->close_load(tablets_to_commit, num_incremental_streams); |
616 | 2.05k | first = false; |
617 | 2.05k | } else { |
618 | 1.93k | st = stream->close_load({}, num_incremental_streams); |
619 | 1.93k | } |
620 | 3.99k | if (!st.ok()) { |
621 | | LOG(WARNING) << "close_load failed: " << st << "; stream: " << *stream; |
622 | 0 | } |
623 | 3.99k | } |
624 | 2.05k | return status; |
625 | 2.05k | } |
626 | | |
627 | | } // namespace doris |