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 "io/fs/s3_file_writer.h" |
19 | | |
20 | | #include <aws/s3/model/CompletedPart.h> |
21 | | #include <bvar/recorder.h> |
22 | | #include <bvar/reducer.h> |
23 | | #include <bvar/window.h> |
24 | | #include <fmt/core.h> |
25 | | #include <glog/logging.h> |
26 | | |
27 | | #include <sstream> |
28 | | #include <tuple> |
29 | | #include <utility> |
30 | | |
31 | | #include "common/config.h" |
32 | | #include "common/status.h" |
33 | | #include "cpp/sync_point.h" |
34 | | #include "io/cache/block_file_cache.h" |
35 | | #include "io/cache/block_file_cache_factory.h" |
36 | | #include "io/cache/file_block.h" |
37 | | #include "io/cache/file_cache_common.h" |
38 | | #include "io/fs/file_writer.h" |
39 | | #include "io/fs/path.h" |
40 | | #include "io/fs/s3_file_bufferpool.h" |
41 | | #include "io/fs/s3_file_system.h" |
42 | | #include "io/fs/s3_obj_storage_client.h" |
43 | | #include "runtime/exec_env.h" |
44 | | #include "util/debug_points.h" |
45 | | #include "util/s3_util.h" |
46 | | #include "util/stopwatch.hpp" |
47 | | |
48 | | namespace doris::io { |
49 | | |
50 | | bvar::Adder<uint64_t> s3_file_writer_total("s3_file_writer_total_num"); |
51 | | bvar::Adder<uint64_t> s3_bytes_written_total("s3_file_writer_bytes_written"); |
52 | | bvar::Adder<uint64_t> s3_file_created_total("s3_file_writer_file_created"); |
53 | | bvar::Adder<uint64_t> s3_file_being_written("s3_file_writer_file_being_written"); |
54 | | bvar::Adder<uint64_t> s3_file_writer_async_close_queuing("s3_file_writer_async_close_queuing"); |
55 | | bvar::Adder<uint64_t> s3_file_writer_async_close_processing( |
56 | | "s3_file_writer_async_close_processing"); |
57 | | bvar::IntRecorder s3_file_writer_first_append_to_close_ms_recorder; |
58 | | bvar::Window<bvar::IntRecorder> s3_file_writer_first_append_to_close_ms_window( |
59 | | "s3_file_writer_first_append_to_close_ms", |
60 | | &s3_file_writer_first_append_to_close_ms_recorder, /*window_size=*/10); |
61 | | |
62 | | S3FileWriter::S3FileWriter(std::shared_ptr<ObjClientHolder> client, std::string bucket, |
63 | | std::string key, const FileWriterOptions* opts) |
64 | 2.09k | : _obj_storage_path_opts({.path = fmt::format("s3://{}/{}", bucket, key), |
65 | 2.09k | .bucket = std::move(bucket), |
66 | 2.09k | .key = std::move(key)}), |
67 | 2.09k | _used_by_s3_committer(opts ? opts->used_by_s3_committer : false), |
68 | 2.09k | _obj_client(std::move(client)) { |
69 | 2.09k | s3_file_writer_total << 1; |
70 | 2.09k | s3_file_being_written << 1; |
71 | 2.09k | Aws::Http::SetCompliantRfc3986Encoding(true); |
72 | | |
73 | 2.09k | init_cache_builder(opts, _obj_storage_path_opts.path); |
74 | 2.09k | } |
75 | | |
76 | 2.09k | S3FileWriter::~S3FileWriter() { |
77 | 2.09k | if (_async_close_pack != nullptr) { |
78 | | // For thread safety |
79 | 0 | std::ignore = _async_close_pack->future.get(); |
80 | 0 | _async_close_pack = nullptr; |
81 | 2.09k | } else if (state() == State::OPENED) { |
82 | 1.01k | WARN_IF_ERROR(abort(), "failed to abort unfinished S3 writer"); |
83 | 1.07k | } else { |
84 | | // Consider one situation where the file writer is destructed after it submit at least one async task |
85 | | // without calling close(), then there exists one occasion where the async task is executed right after |
86 | | // the correspoding S3 file writer is already destructed |
87 | 1.07k | _wait_until_finish(fmt::format("wait s3 file {} upload to be finished", |
88 | 1.07k | _obj_storage_path_opts.path.native())); |
89 | 1.07k | } |
90 | 2.09k | if (state() == State::OPENED && !_failed) { |
91 | 0 | s3_bytes_written_total << _bytes_appended; |
92 | 0 | } |
93 | 2.09k | s3_file_being_written << -1; |
94 | 2.09k | } |
95 | | |
96 | 1.01k | Status S3FileWriter::abort() { |
97 | 1.01k | if (state() == State::CLOSED) { |
98 | 0 | return Status::OK(); |
99 | 0 | } |
100 | 1.01k | if (state() == State::ASYNC_CLOSING) { |
101 | 0 | return Status::InternalError("cannot abort an asynchronously closing S3 writer"); |
102 | 0 | } |
103 | 1.01k | RETURN_IF_ERROR(_abort_impl()); |
104 | 1.01k | _state = State::CLOSED; |
105 | 1.01k | return Status::OK(); |
106 | 1.01k | } |
107 | | |
108 | 1.02k | Status S3FileWriter::_abort_impl() { |
109 | 1.02k | _wait_until_finish( |
110 | 1.02k | fmt::format("wait s3 file {} before abort", _obj_storage_path_opts.path.native())); |
111 | 1.02k | _pending_buf.reset(); |
112 | 1.02k | if (_multipart_upload_started) { |
113 | 8 | const auto& client = _obj_client->get(); |
114 | 8 | if (client == nullptr) { |
115 | 0 | return Status::InternalError("invalid obj storage client"); |
116 | 0 | } |
117 | 8 | auto response = client->abort_multipart_upload(_obj_storage_path_opts); |
118 | 8 | if (response.status.code != ErrorCode::OK) { |
119 | 0 | return {response.status.code, std::move(response.status.msg)}; |
120 | 0 | } |
121 | 8 | } |
122 | | // Once abort returns, no destructor or retry may complete the abandoned upload. |
123 | 1.02k | return Status::OK(); |
124 | 1.02k | } |
125 | | |
126 | 48 | Status S3FileWriter::_create_multi_upload_request() { |
127 | 48 | LOG(INFO) << "create_multi_upload_request " << _obj_storage_path_opts.path.native(); |
128 | 48 | const auto& client = _obj_client->get(); |
129 | 48 | if (nullptr == client) { |
130 | 0 | return Status::InternalError<false>("invalid obj storage client"); |
131 | 0 | } |
132 | 48 | auto resp = client->create_multipart_upload(_obj_storage_path_opts); |
133 | 48 | if (resp.resp.status.code == ErrorCode::OK) { |
134 | | // Some providers identify staged uploads by block IDs instead of a server-issued upload ID. |
135 | 47 | _multipart_upload_started = true; |
136 | 47 | _obj_storage_path_opts.upload_id = resp.upload_id; |
137 | 47 | } |
138 | 48 | return {resp.resp.status.code, std::move(resp.resp.status.msg)}; |
139 | 48 | } |
140 | | |
141 | 3.17k | void S3FileWriter::_wait_until_finish(std::string_view task_name) { |
142 | 3.17k | auto timeout_duration = config::s3_file_writer_log_interval_second; |
143 | 3.17k | auto msg = fmt::format( |
144 | 3.17k | "{} multipart upload already takes {} seconds, bucket={}, key={}, upload_id={}", |
145 | 3.17k | task_name, timeout_duration, _obj_storage_path_opts.bucket, |
146 | 3.17k | _obj_storage_path_opts.path.native(), |
147 | 3.17k | _obj_storage_path_opts.upload_id.has_value() ? *_obj_storage_path_opts.upload_id : ""); |
148 | 3.17k | timespec current_time; |
149 | | // We don't need high accuracy here, so we use time(nullptr) |
150 | | // since it's the fastest way to get current time(second) |
151 | 3.17k | auto current_time_second = time(nullptr); |
152 | 3.17k | current_time.tv_sec = current_time_second + timeout_duration; |
153 | 3.17k | current_time.tv_nsec = 0; |
154 | | // bthread::countdown_event::timed_wait() should use absolute time |
155 | 3.17k | while (0 != _countdown_event.timed_wait(current_time)) { |
156 | 0 | current_time.tv_sec += timeout_duration; |
157 | 0 | LOG(WARNING) << msg; |
158 | 0 | } |
159 | 3.17k | } |
160 | | |
161 | 1.09k | Status S3FileWriter::close(bool non_block) { |
162 | 1.09k | if (state() == State::CLOSED) { |
163 | 0 | return Status::InternalError("S3FileWriter already closed, file path {}, file key {}", |
164 | 0 | _obj_storage_path_opts.path.native(), |
165 | 0 | _obj_storage_path_opts.key); |
166 | 0 | } |
167 | 1.09k | if (state() == State::ASYNC_CLOSING) { |
168 | 16 | if (non_block) { |
169 | 0 | return Status::InternalError("Don't submit async close multi times"); |
170 | 0 | } |
171 | 16 | CHECK(_async_close_pack != nullptr); |
172 | 16 | _st = _async_close_pack->future.get(); |
173 | 16 | _async_close_pack = nullptr; |
174 | | // We should wait for all the pre async task to be finished |
175 | 16 | _state = State::CLOSED; |
176 | | // The next time we call close() with no matter non_block true or false, it would always return the |
177 | | // '_st' value because this writer is already closed. |
178 | 16 | if (!non_block && _st.ok()) { |
179 | 11 | _record_close_latency(); |
180 | 11 | } |
181 | 16 | return _st; |
182 | 16 | } |
183 | 1.07k | if (non_block) { |
184 | 1.01k | _state = State::ASYNC_CLOSING; |
185 | 1.01k | _async_close_pack = std::make_unique<AsyncCloseStatusPack>(); |
186 | 1.01k | _async_close_pack->future = _async_close_pack->promise.get_future(); |
187 | 1.01k | s3_file_writer_async_close_queuing << 1; |
188 | 1.01k | Status submit_status = Status::OK(); |
189 | 1.01k | DBUG_EXECUTE_IF("S3FileWriter.close.submit_async_close.inject_error", { |
190 | 1.01k | submit_status = Status::IOError("S3FileWriter.close.submit_async_close.inject_error"); |
191 | 1.01k | }); |
192 | 1.01k | if (submit_status.ok()) { |
193 | 1.01k | submit_status = |
194 | 1.01k | ExecEnv::GetInstance()->non_block_close_thread_pool()->submit_func([&]() { |
195 | 1.01k | s3_file_writer_async_close_queuing << -1; |
196 | 1.01k | s3_file_writer_async_close_processing << 1; |
197 | 1.01k | _st = _close_impl(); |
198 | 1.01k | if (!_st.ok()) { |
199 | | // A failed completion must not leave server-side multipart state behind. |
200 | 5 | WARN_IF_ERROR(_abort_impl(), "failed to abort incomplete S3 upload"); |
201 | 5 | } |
202 | 1.01k | _async_close_pack->promise.set_value(_st); |
203 | 1.01k | s3_file_writer_async_close_processing << -1; |
204 | 1.01k | }); |
205 | 1.01k | } |
206 | 1.01k | if (!submit_status.ok()) { |
207 | 0 | s3_file_writer_async_close_queuing << -1; |
208 | 0 | LOG(WARNING) << "failed to submit async close for " |
209 | 0 | << _obj_storage_path_opts.path.native() |
210 | 0 | << ", fallback to sync close, status=" << submit_status; |
211 | 0 | _st = _close_impl(); |
212 | 0 | if (!_st.ok()) { |
213 | 0 | WARN_IF_ERROR(_abort_impl(), "failed to abort incomplete S3 upload"); |
214 | 0 | } |
215 | 0 | _async_close_pack->promise.set_value(_st); |
216 | 0 | return _st; |
217 | 0 | } |
218 | 1.01k | return Status::OK(); |
219 | 1.01k | } |
220 | 59 | _st = _close_impl(); |
221 | 59 | if (!_st.ok()) { |
222 | 2 | WARN_IF_ERROR(_abort_impl(), "failed to abort incomplete S3 upload"); |
223 | 2 | } |
224 | 59 | _state = State::CLOSED; |
225 | 59 | if (!non_block && _st.ok()) { |
226 | 57 | _record_close_latency(); |
227 | 57 | } |
228 | 59 | return _st; |
229 | 1.07k | } |
230 | | |
231 | 1.06k | void S3FileWriter::_record_close_latency() { |
232 | 1.06k | if (_close_latency_recorded || !_first_append_timestamp.has_value()) { |
233 | 1 | return; |
234 | 1 | } |
235 | 1.06k | auto now = std::chrono::steady_clock::now(); |
236 | 1.06k | auto latency_ms = |
237 | 1.06k | std::chrono::duration_cast<std::chrono::milliseconds>(now - *_first_append_timestamp) |
238 | 1.06k | .count(); |
239 | 1.06k | s3_file_writer_first_append_to_close_ms_recorder << latency_ms; |
240 | 1.06k | if (auto* sampler = s3_file_writer_first_append_to_close_ms_recorder.get_sampler()) { |
241 | 1.06k | sampler->take_sample(); |
242 | 1.06k | } |
243 | 1.06k | _close_latency_recorded = true; |
244 | 1.06k | } |
245 | | |
246 | 1.00k | Status S3FileWriter::try_finish_close() { |
247 | 1.00k | if (state() == State::CLOSED) { |
248 | 0 | return _st; |
249 | 0 | } |
250 | 1.00k | if (state() != State::ASYNC_CLOSING) { |
251 | 0 | return Status::NotSupported("S3FileWriter is not async closing"); |
252 | 0 | } |
253 | 1.00k | CHECK(_async_close_pack != nullptr); |
254 | 1.00k | if (_async_close_pack->future.wait_for(std::chrono::seconds(0)) != std::future_status::ready) { |
255 | 0 | return Status::NeedSendAgain("async close is not finished"); |
256 | 0 | } |
257 | 1.00k | _st = _async_close_pack->future.get(); |
258 | 1.00k | _async_close_pack = nullptr; |
259 | 1.00k | _state = State::CLOSED; |
260 | 1.00k | if (_st.ok()) { |
261 | 1.00k | _record_close_latency(); |
262 | 1.00k | } |
263 | 1.00k | return _st; |
264 | 1.00k | } |
265 | | |
266 | 1.33k | bool S3FileWriter::_complete_part_task_callback(Status s) { |
267 | 1.33k | bool ret = false; |
268 | 1.33k | if (!s.ok()) [[unlikely]] { |
269 | 3 | VLOG_NOTICE << "failed at key: " << _obj_storage_path_opts.key |
270 | 0 | << ", status: " << s.to_string(); |
271 | 3 | std::unique_lock<std::mutex> _lck {_completed_lock}; |
272 | 3 | _failed = true; |
273 | 3 | ret = true; |
274 | 3 | _st = std::move(s); |
275 | 3 | } |
276 | | // After the signal, there is a scenario where the previous invocation of _wait_until_finish |
277 | | // returns to the caller, and subsequently, the S3 file writer is destructed. |
278 | | // This means that accessing _failed afterwards would result in a heap use after free vulnerability. |
279 | 1.33k | _countdown_event.signal(); |
280 | 1.33k | return ret; |
281 | 1.33k | } |
282 | | |
283 | 1.33k | Status S3FileWriter::_build_upload_buffer() { |
284 | 1.33k | auto builder = FileBufferBuilder(); |
285 | 1.33k | builder.set_type(BufferType::UPLOAD) |
286 | 1.33k | .set_upload_callback([part_num = _cur_part_num, this](UploadFileBuffer& buf) { |
287 | 300 | _upload_one_part(part_num, buf); |
288 | 300 | }) |
289 | 1.33k | .set_file_offset(_bytes_appended) |
290 | 1.33k | .set_sync_after_complete_task([this](auto&& PH1) { |
291 | 1.33k | return _complete_part_task_callback(std::forward<decltype(PH1)>(PH1)); |
292 | 1.33k | }) |
293 | 1.62k | .set_is_cancelled([this]() { return _failed.load(); }); |
294 | 1.33k | if (_cache_builder != nullptr) { |
295 | | // We would load the data into file cache asynchronously which indicates |
296 | | // that this instance of S3FileWriter might have been destructed when we |
297 | | // try to do writing into file cache, so we make the lambda capture the variable |
298 | | // we need by value to extend their lifetime |
299 | 4 | int64_t id = get_tablet_id(_obj_storage_path_opts.path.native()).value_or(0); |
300 | 4 | builder.set_allocate_file_blocks_holder([builder = *_cache_builder, |
301 | 4 | offset = _bytes_appended, |
302 | 4 | tablet_id = id]() -> FileBlocksHolderPtr { |
303 | 4 | return builder.allocate_cache_holder(offset, config::s3_write_buffer_size, tablet_id); |
304 | 4 | }); |
305 | 4 | } |
306 | 1.33k | RETURN_IF_ERROR(builder.build(&_pending_buf)); |
307 | 1.33k | auto* buf = dynamic_cast<UploadFileBuffer*>(_pending_buf.get()); |
308 | 1.33k | DCHECK(buf != nullptr); |
309 | 1.33k | return Status::OK(); |
310 | 1.33k | } |
311 | | |
312 | 1.33k | Status S3FileWriter::_submit_upload_buffer(const std::shared_ptr<FileBuffer>& buf) { |
313 | 1.33k | _countdown_event.add_count(); |
314 | 1.33k | DBUG_EXECUTE_IF("S3FileWriter.submit_upload_buffer.inject_error", { |
315 | 1.33k | auto st = Status::IOError("S3FileWriter.submit_upload_buffer.inject_error"); |
316 | 1.33k | _complete_part_task_callback(st); |
317 | 1.33k | return st; |
318 | 1.33k | }); |
319 | 1.33k | auto st = FileBuffer::submit(buf); |
320 | 1.33k | if (!st.ok()) [[unlikely]] { |
321 | 0 | _complete_part_task_callback(st); |
322 | 0 | } |
323 | 1.33k | return st; |
324 | 1.33k | } |
325 | | |
326 | 1.07k | Status S3FileWriter::_close_impl() { |
327 | 1.07k | VLOG_DEBUG << "S3FileWriter::close, path: " << _obj_storage_path_opts.path.native(); |
328 | | |
329 | 1.07k | DBUG_EXECUTE_IF("S3FileWriter._close_impl.inject_error", { |
330 | 1.07k | if (_obj_storage_path_opts.key.ends_with(".dat")) { |
331 | 1.07k | return Status::IOError("S3FileWriter._close_impl.inject_error"); |
332 | 1.07k | } |
333 | 1.07k | }); |
334 | | |
335 | 1.07k | if (_cur_part_num == 1 && _pending_buf) { // data size is less than config::s3_write_buffer_size |
336 | 1.02k | RETURN_IF_ERROR(_set_upload_to_remote_less_than_buffer_size()); |
337 | 1.02k | } |
338 | | |
339 | 1.07k | if (_bytes_appended == 0) { |
340 | 3 | DCHECK_EQ(_cur_part_num, 1); |
341 | | // No data written, but need to create an empty file |
342 | 3 | RETURN_IF_ERROR(_build_upload_buffer()); |
343 | 3 | if (!_used_by_s3_committer) { |
344 | 3 | auto* pending_buf = dynamic_cast<UploadFileBuffer*>(_pending_buf.get()); |
345 | 3 | pending_buf->set_upload_to_remote([this](UploadFileBuffer& buf) { _put_object(buf); }); |
346 | 3 | } else { |
347 | 0 | RETURN_IF_ERROR(_create_multi_upload_request()); |
348 | 0 | } |
349 | 3 | } |
350 | | |
351 | 1.07k | if (_pending_buf != nullptr) { // there is remaining data in buffer need to be uploaded |
352 | 1.06k | auto st = _submit_upload_buffer(_pending_buf); |
353 | 1.06k | _pending_buf = nullptr; |
354 | 1.06k | if (!st.ok()) { |
355 | 0 | _wait_until_finish("pending buffer submit failed"); |
356 | 0 | return st; |
357 | 0 | } |
358 | 1.06k | } |
359 | | |
360 | 1.07k | RETURN_IF_ERROR(_complete()); |
361 | 1.07k | SYNC_POINT_RETURN_WITH_VALUE("s3_file_writer::close", Status()); |
362 | | |
363 | 1.06k | return Status::OK(); |
364 | 1.06k | } |
365 | | |
366 | 10.4k | Status S3FileWriter::appendv(const Slice* data, size_t data_cnt) { |
367 | 10.4k | if (state() != State::OPENED) [[unlikely]] { |
368 | 0 | return Status::InternalError("append to closed file: {}", |
369 | 0 | _obj_storage_path_opts.path.native()); |
370 | 0 | } |
371 | | |
372 | 10.4k | if (!_first_append_timestamp.has_value()) { |
373 | 1.07k | _first_append_timestamp = std::chrono::steady_clock::now(); |
374 | 1.07k | } |
375 | | |
376 | 10.4k | size_t buffer_size = config::s3_write_buffer_size; |
377 | 10.4k | TEST_SYNC_POINT_RETURN_WITH_VALUE("s3_file_writer::appenv", Status()); |
378 | 20.9k | for (size_t i = 0; i < data_cnt; i++) { |
379 | 10.5k | size_t data_size = data[i].get_size(); |
380 | 21.2k | for (size_t pos = 0, data_size_to_append = 0; pos < data_size; pos += data_size_to_append) { |
381 | 10.7k | if (_failed) { |
382 | 0 | return _st; |
383 | 0 | } |
384 | 10.7k | if (!_pending_buf) { |
385 | 1.33k | RETURN_IF_ERROR(_build_upload_buffer()); |
386 | 1.33k | } |
387 | | // we need to make sure all parts except the last one to be 5MB or more |
388 | | // and shouldn't be larger than buf |
389 | 10.7k | data_size_to_append = std::min(data_size - pos, _pending_buf->get_file_offset() + |
390 | 10.7k | buffer_size - _bytes_appended); |
391 | | |
392 | | // if the buffer has memory buf inside, the data would be written into memory first then S3 then file cache |
393 | | // it would be written to cache then S3 if the buffer doesn't have memory preserved |
394 | 10.7k | RETURN_IF_ERROR(_pending_buf->append_data( |
395 | 10.7k | Slice {data[i].get_data() + pos, data_size_to_append})); |
396 | 10.7k | TEST_SYNC_POINT_CALLBACK("s3_file_writer::appenv_1", &_pending_buf, _cur_part_num); |
397 | | |
398 | | // If this is the last part and the data size is less than s3_write_buffer_size, |
399 | | // the pending_buf will be handled by _close_impl() and _complete() |
400 | | // If this is the last part and the data size is equal to s3_write_buffer_size, |
401 | | // the pending_buf is handled here and submitted. it will be waited by _complete() |
402 | 10.7k | if (_pending_buf->get_size() == buffer_size) { |
403 | | // only create multiple upload request when the data size is |
404 | | // larger or equal to s3_write_buffer_size than one memory buffer |
405 | 269 | if (_cur_part_num == 1) { |
406 | 48 | RETURN_IF_ERROR(_create_multi_upload_request()); |
407 | 48 | } |
408 | 268 | _cur_part_num++; |
409 | 268 | auto st = _submit_upload_buffer(_pending_buf); |
410 | 268 | _pending_buf = nullptr; |
411 | 268 | RETURN_IF_ERROR(st); |
412 | 268 | } |
413 | 10.7k | _bytes_appended += data_size_to_append; |
414 | 10.7k | } |
415 | 10.5k | } |
416 | 10.4k | return Status::OK(); |
417 | 10.4k | } |
418 | | |
419 | 300 | void S3FileWriter::_upload_one_part(int part_num, UploadFileBuffer& buf) { |
420 | 300 | VLOG_DEBUG << "upload_one_part " << _obj_storage_path_opts.path.native() |
421 | 0 | << " part=" << part_num; |
422 | 300 | if (buf.is_cancelled()) { |
423 | 0 | LOG_INFO("file {} skip part {} because previous failure {}", |
424 | 0 | _obj_storage_path_opts.path.native(), part_num, _st); |
425 | 0 | return; |
426 | 0 | } |
427 | 300 | const auto& client = _obj_client->get(); |
428 | 300 | if (nullptr == client) { |
429 | 0 | LOG_WARNING("failed to upload part, key={}, part_num={} bacause of null obj client", |
430 | 0 | _obj_storage_path_opts.key, part_num); |
431 | 0 | buf.set_status(Status::InternalError<false>("invalid obj storage client")); |
432 | 0 | return; |
433 | 0 | } |
434 | 300 | auto resp = client->upload_part(_obj_storage_path_opts, buf.get_string_view_data(), part_num); |
435 | 300 | if (resp.resp.status.code != ErrorCode::OK) { |
436 | 1 | LOG_WARNING("failed to upload part, key={}, part_num={}, status={}", |
437 | 1 | _obj_storage_path_opts.key, part_num, resp.resp.status.msg); |
438 | 1 | buf.set_status(Status(resp.resp.status.code, std::move(resp.resp.status.msg))); |
439 | 1 | return; |
440 | 1 | } |
441 | 299 | s3_bytes_written_total << buf.get_size(); |
442 | | |
443 | 299 | ObjectCompleteMultiPart completed_part { |
444 | 299 | part_num, resp.etag.has_value() ? std::move(resp.etag.value()) : ""}; |
445 | | |
446 | 299 | std::unique_lock<std::mutex> lck {_completed_lock}; |
447 | 299 | _completed_parts.emplace_back(std::move(completed_part)); |
448 | 299 | } |
449 | | |
450 | | // if enabled check |
451 | | // 1. issue a head object request for existence check |
452 | | // 2. check the file size |
453 | | Status check_after_upload(ObjStorageClient* client, const ObjectStorageResponse& upload_res, |
454 | | const ObjectStoragePathOptions& path_opt, int64_t bytes_appended, |
455 | 1.06k | const std::string& put_or_comp) { |
456 | 1.06k | if (!config::enable_s3_object_check_after_upload) return Status::OK(); |
457 | | |
458 | 1.06k | auto head_res = client->head_object(path_opt); |
459 | | |
460 | | // clang-format off |
461 | 1.06k | auto err_msg = [&]() { |
462 | 0 | std::stringstream ss; |
463 | 0 | ss << "failed to check object after upload=" << put_or_comp |
464 | 0 | << " file_path=" << path_opt.path.native() |
465 | 0 | << fmt::format(" {}_err=", put_or_comp) << upload_res.status.msg |
466 | 0 | << fmt::format(" {}_code=", put_or_comp) << upload_res.status.code |
467 | 0 | << fmt::format(" {}_http_code=", put_or_comp) << upload_res.http_code |
468 | 0 | << fmt::format(" {}_request_id=", put_or_comp) << upload_res.request_id |
469 | 0 | << " head_err=" << head_res.resp.status.msg |
470 | 0 | << " head_code=" << head_res.resp.status.code |
471 | 0 | << " head_http_code=" << head_res.resp.http_code |
472 | 0 | << " head_request_id=" << head_res.resp.request_id; |
473 | 0 | return ss.str(); |
474 | 0 | }; |
475 | | // clang-format on |
476 | | |
477 | | // TODO(gavin): make it fail by injection |
478 | 1.06k | TEST_SYNC_POINT_CALLBACK("S3FileWriter::check_after_load", &head_res); |
479 | 1.06k | if (head_res.resp.status.code != ErrorCode::OK && head_res.resp.http_code != 200) { |
480 | 0 | LOG(WARNING) << "failed to issue head object after upload, " << err_msg(); |
481 | 0 | DCHECK(false) << "failed to issue head object after upload, " << err_msg(); |
482 | | // FIXME(gavin): we should retry if this HEAD fails? |
483 | 0 | return Status::IOError( |
484 | 0 | "failed to issue head object after upload, status_code={}, http_code={}, err={}", |
485 | 0 | head_res.resp.status.code, head_res.resp.http_code, head_res.resp.status.msg); |
486 | 0 | } |
487 | 1.06k | if (head_res.file_size != bytes_appended) { |
488 | 0 | LOG(WARNING) << "failed to check size after upload, expected_size=" << bytes_appended |
489 | 0 | << " actual_size=" << head_res.file_size << err_msg(); |
490 | 0 | DCHECK_EQ(bytes_appended, head_res.file_size) |
491 | 0 | << "failed to check size after upload," << err_msg(); |
492 | 0 | return Status::IOError( |
493 | 0 | "failed to check object size after upload, expected_size={} actual_size={}", |
494 | 0 | bytes_appended, head_res.file_size); |
495 | 0 | } |
496 | 1.06k | return Status::OK(); |
497 | 1.06k | } |
498 | | |
499 | 1.07k | Status S3FileWriter::_complete() { |
500 | 1.07k | const auto& client = _obj_client->get(); |
501 | 1.07k | if (nullptr == client) { |
502 | 0 | return Status::InternalError<false>("invalid obj storage client"); |
503 | 0 | } |
504 | 1.07k | if (_failed) { |
505 | 0 | _wait_until_finish("early quit"); |
506 | 0 | return _st; |
507 | 0 | } |
508 | | // When the part num is only one, it means the data is less than 5MB so we can just put it. |
509 | 1.07k | if (_cur_part_num == 1) { |
510 | 1.03k | _wait_until_finish("PutObject"); |
511 | 1.03k | return _st; |
512 | 1.03k | } |
513 | | // Wait multipart load and finish. |
514 | 44 | _wait_until_finish("Complete"); |
515 | 44 | TEST_SYNC_POINT_CALLBACK("S3FileWriter::_complete:1", |
516 | 44 | std::make_pair(&_failed, &_completed_parts)); |
517 | 44 | if (_used_by_s3_committer) { // S3 committer will complete multipart upload file on FE side. |
518 | 0 | s3_file_created_total << 1; // Assume that it will be created successfully |
519 | 0 | return Status::OK(); |
520 | 0 | } |
521 | | |
522 | | // check number of parts |
523 | 44 | int64_t expected_num_parts1 = (_bytes_appended / config::s3_write_buffer_size) + |
524 | 44 | !!(_bytes_appended % config::s3_write_buffer_size); |
525 | 44 | int64_t expected_num_parts2 = |
526 | 44 | (_bytes_appended % config::s3_write_buffer_size) ? _cur_part_num : _cur_part_num - 1; |
527 | 44 | DCHECK_EQ(expected_num_parts1, expected_num_parts2) |
528 | 0 | << " bytes_appended=" << _bytes_appended << " cur_part_num=" << _cur_part_num |
529 | 0 | << " s3_write_buffer_size=" << config::s3_write_buffer_size; |
530 | 44 | if (_failed || _completed_parts.size() != static_cast<size_t>(expected_num_parts1) || |
531 | 44 | expected_num_parts1 != expected_num_parts2) { |
532 | 3 | _st = Status::InternalError( |
533 | 3 | "failed to complete multipart upload, error status={} failed={} #complete_parts={} " |
534 | 3 | "#expected_parts={} " |
535 | 3 | "completed_parts_list={} file_path={} file_size={} has left buffer not uploaded={}", |
536 | 3 | _st, _failed, _completed_parts.size(), expected_num_parts1, _dump_completed_part(), |
537 | 3 | _obj_storage_path_opts.path.native(), _bytes_appended, _pending_buf != nullptr); |
538 | 3 | LOG(WARNING) << _st; |
539 | 3 | return _st; |
540 | 3 | } |
541 | | // make sure _completed_parts are ascending order |
542 | 41 | std::sort(_completed_parts.begin(), _completed_parts.end(), |
543 | 867 | [](auto& p1, auto& p2) { return p1.part_num < p2.part_num; }); |
544 | 41 | TEST_SYNC_POINT_CALLBACK("S3FileWriter::_complete:2", &_completed_parts); |
545 | 41 | LOG(INFO) << "complete_multipart_upload " << _obj_storage_path_opts.path.native() |
546 | 41 | << " size=" << _bytes_appended << " number_parts=" << _completed_parts.size() |
547 | 41 | << " s3_write_buffer_size=" << config::s3_write_buffer_size; |
548 | 41 | auto resp = client->complete_multipart_upload(_obj_storage_path_opts, _completed_parts); |
549 | 41 | if (resp.status.code != ErrorCode::OK) { |
550 | 2 | LOG_WARNING("failed to complete multipart upload, err={}, file_path={}", resp.status.msg, |
551 | 2 | _obj_storage_path_opts.path.native()); |
552 | 2 | return {resp.status.code, std::move(resp.status.msg)}; |
553 | 2 | } |
554 | | |
555 | 39 | RETURN_IF_ERROR(check_after_upload(client.get(), resp, _obj_storage_path_opts, _bytes_appended, |
556 | 39 | "complete_multipart")); |
557 | | |
558 | 39 | s3_file_created_total << 1; |
559 | 39 | return Status::OK(); |
560 | 39 | } |
561 | | |
562 | 1.02k | Status S3FileWriter::_set_upload_to_remote_less_than_buffer_size() { |
563 | 1.02k | auto* buf = dynamic_cast<UploadFileBuffer*>(_pending_buf.get()); |
564 | 1.02k | DCHECK(buf != nullptr); |
565 | 1.02k | if (_used_by_s3_committer) { |
566 | | // If used_by_s3_committer, we always use multi-parts uploading. |
567 | 0 | buf->set_upload_to_remote([part_num = _cur_part_num, this](UploadFileBuffer& buf) { |
568 | 0 | _upload_one_part(part_num, buf); |
569 | 0 | }); |
570 | 0 | DCHECK(_cur_part_num == 1); |
571 | 0 | RETURN_IF_ERROR(_create_multi_upload_request()); |
572 | 1.02k | } else { |
573 | | // if we only need to upload one file less than 5MB, we can just |
574 | | // call PutObject to reduce the network IO |
575 | 1.02k | buf->set_upload_to_remote([this](UploadFileBuffer& b) { _put_object(b); }); |
576 | 1.02k | } |
577 | 1.02k | return Status::OK(); |
578 | 1.02k | } |
579 | | |
580 | 1.03k | void S3FileWriter::_put_object(UploadFileBuffer& buf) { |
581 | 1.03k | MonotonicStopWatch timer; |
582 | 1.03k | timer.start(); |
583 | | |
584 | 1.03k | if (state() == State::CLOSED) { |
585 | 0 | DCHECK(state() != State::CLOSED) |
586 | 0 | << "state=" << (int)state() << " path=" << _obj_storage_path_opts.path.native(); |
587 | 0 | LOG_WARNING("failed to put object because file closed, file path {}", |
588 | 0 | _obj_storage_path_opts.path.native()); |
589 | 0 | buf.set_status(Status::InternalError<false>("try to put closed file")); |
590 | 0 | return; |
591 | 0 | } |
592 | 1.03k | const auto& client = _obj_client->get(); |
593 | 1.03k | if (nullptr == client) { |
594 | 0 | buf.set_status(Status::InternalError<false>("invalid obj storage client")); |
595 | 0 | return; |
596 | 0 | } |
597 | 1.03k | TEST_SYNC_POINT_RETURN_WITH_VOID("S3FileWriter::_put_object", this, &buf); |
598 | 1.02k | auto resp = client->put_object(_obj_storage_path_opts, buf.get_string_view_data()); |
599 | 1.02k | timer.stop(); |
600 | | |
601 | 1.02k | if (resp.status.code != ErrorCode::OK) { |
602 | 0 | LOG_WARNING("failed to put object, put object failed because {}, file path {}, time={}ms", |
603 | 0 | resp.status.msg, _obj_storage_path_opts.path.native(), |
604 | 0 | timer.elapsed_time_milliseconds()); |
605 | 0 | buf.set_status({resp.status.code, std::move(resp.status.msg)}); |
606 | 0 | return; |
607 | 0 | } |
608 | | |
609 | 1.02k | auto st = check_after_upload(client.get(), resp, _obj_storage_path_opts, _bytes_appended, |
610 | 1.02k | "put_object"); |
611 | 1.02k | if (!st.ok()) { |
612 | 0 | buf.set_status(st); |
613 | 0 | return; |
614 | 0 | } |
615 | | |
616 | 1.02k | LOG(INFO) << "put_object " << _obj_storage_path_opts.path.native() |
617 | 1.02k | << " size=" << _bytes_appended << " time=" << timer.elapsed_time_milliseconds() |
618 | 1.02k | << "ms"; |
619 | 1.02k | s3_file_created_total << 1; |
620 | 1.02k | s3_bytes_written_total << buf.get_size(); |
621 | 1.02k | } |
622 | | |
623 | 3 | std::string S3FileWriter::_dump_completed_part() const { |
624 | 3 | std::stringstream ss; |
625 | 3 | ss << "part_numbers:"; |
626 | 3 | for (const auto& part : _completed_parts) { |
627 | 2 | ss << " " << part.part_num; |
628 | 2 | } |
629 | 3 | return ss.str(); |
630 | 3 | } |
631 | | |
632 | | } // namespace doris::io |