-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[opt](s3) auto retry when meeting 429 error #35396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -37,6 +37,7 @@ | |||||||||||
| #include "io/fs/s3_common.h" | ||||||||||||
| #include "util/bvar_helper.h" | ||||||||||||
| #include "util/doris_metrics.h" | ||||||||||||
| #include "util/runtime_profile.h" | ||||||||||||
| #include "util/s3_util.h" | ||||||||||||
|
|
||||||||||||
| namespace doris::io { | ||||||||||||
|
|
@@ -45,13 +46,18 @@ bvar::Adder<uint64_t> s3_file_reader_read_counter("s3_file_reader", "read_at"); | |||||||||||
| bvar::Adder<uint64_t> s3_file_reader_total("s3_file_reader", "total_num"); | ||||||||||||
| bvar::Adder<uint64_t> s3_bytes_read_total("s3_file_reader", "bytes_read"); | ||||||||||||
| bvar::Adder<uint64_t> s3_file_being_read("s3_file_reader", "file_being_read"); | ||||||||||||
| bvar::Adder<uint64_t> s3_file_reader_too_many_request_counter("s3_file_reader", "too_many_request"); | ||||||||||||
| bvar::LatencyRecorder s3_bytes_per_read("s3_file_reader", "bytes_per_read"); // also QPS | ||||||||||||
| bvar::PerSecond<bvar::Adder<uint64_t>> s3_read_througthput("s3_file_reader", "s3_read_throughput", | ||||||||||||
| &s3_bytes_read_total); | ||||||||||||
| // Although we can get QPS from s3_bytes_per_read, but s3_bytes_per_read only | ||||||||||||
| // record successfull request, and s3_get_request_qps will record all request. | ||||||||||||
| bvar::PerSecond<bvar::Adder<uint64_t>> s3_get_request_qps("s3_file_reader", "s3_get_request", | ||||||||||||
| &s3_file_reader_read_counter); | ||||||||||||
|
|
||||||||||||
| Result<FileReaderSPtr> S3FileReader::create(std::shared_ptr<const ObjClientHolder> client, | ||||||||||||
| std::string bucket, std::string key, | ||||||||||||
| int64_t file_size) { | ||||||||||||
| std::string bucket, std::string key, int64_t file_size, | ||||||||||||
| RuntimeProfile* profile) { | ||||||||||||
| if (file_size < 0) { | ||||||||||||
| auto res = client->object_file_size(bucket, key); | ||||||||||||
| if (!res.has_value()) { | ||||||||||||
|
|
@@ -62,16 +68,17 @@ Result<FileReaderSPtr> S3FileReader::create(std::shared_ptr<const ObjClientHolde | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return std::make_shared<S3FileReader>(std::move(client), std::move(bucket), std::move(key), | ||||||||||||
| file_size); | ||||||||||||
| file_size, profile); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| S3FileReader::S3FileReader(std::shared_ptr<const ObjClientHolder> client, std::string bucket, | ||||||||||||
| std::string key, size_t file_size) | ||||||||||||
| std::string key, size_t file_size, RuntimeProfile* profile) | ||||||||||||
| : _path(fmt::format("s3://{}/{}", bucket, key)), | ||||||||||||
| _file_size(file_size), | ||||||||||||
| _bucket(std::move(bucket)), | ||||||||||||
| _key(std::move(key)), | ||||||||||||
| _client(std::move(client)) { | ||||||||||||
| _client(std::move(client)), | ||||||||||||
| _profile(profile) { | ||||||||||||
| DorisMetrics::instance()->s3_file_open_reading->increment(1); | ||||||||||||
| DorisMetrics::instance()->s3_file_reader_total->increment(1); | ||||||||||||
| s3_file_reader_total << 1; | ||||||||||||
|
|
@@ -113,23 +120,85 @@ Status S3FileReader::read_at_impl(size_t offset, Slice result, size_t* bytes_rea | |||||||||||
| if (!client) { | ||||||||||||
| return Status::InternalError("init s3 client error"); | ||||||||||||
| } | ||||||||||||
| // clang-format off | ||||||||||||
| auto resp = client->get_object( { .bucket = _bucket, .key = _key, }, | ||||||||||||
| to, offset, bytes_req, bytes_read); | ||||||||||||
| // clang-format on | ||||||||||||
| if (resp.status.code != ErrorCode::OK) { | ||||||||||||
| return std::move(Status(resp.status.code, std::move(resp.status.msg)) | ||||||||||||
| .append(fmt::format("failed to read from {}", _path.native()))); | ||||||||||||
| // // clang-format off | ||||||||||||
| // auto resp = client->get_object( { .bucket = _bucket, .key = _key, }, | ||||||||||||
| // to, offset, bytes_req, bytes_read); | ||||||||||||
| // // clang-format on | ||||||||||||
| // if (resp.status.code != ErrorCode::OK) { | ||||||||||||
| // return std::move(Status(resp.status.code, std::move(resp.status.msg)) | ||||||||||||
| // .append(fmt::format("failed to read from {}", _path.native()))); | ||||||||||||
| // } | ||||||||||||
| // if (*bytes_read != bytes_req) { | ||||||||||||
| // return Status::InternalError("failed to read from {}(bytes read: {}, bytes req: {})", | ||||||||||||
| // _path.native(), *bytes_read, bytes_req); | ||||||||||||
| SCOPED_BVAR_LATENCY(s3_bvar::s3_get_latency); | ||||||||||||
|
|
||||||||||||
| int retry_count = 0; | ||||||||||||
| const int base_wait_time = config::s3_read_base_wait_time_ms; // Base wait time in milliseconds | ||||||||||||
| const int max_wait_time = config::s3_read_max_wait_time_ms; // Maximum wait time in milliseconds | ||||||||||||
| const int max_retries = config::max_s3_client_retry; // wait 1s, 2s, 4s, 8s for each backoff | ||||||||||||
|
|
||||||||||||
| int total_sleep_time = 0; | ||||||||||||
| while (retry_count <= max_retries) { | ||||||||||||
| s3_file_reader_read_counter << 1; | ||||||||||||
| // clang-format off | ||||||||||||
| auto resp = client->get_object( { .bucket = _bucket, .key = _key, }, | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| to, offset, bytes_req, bytes_read); | ||||||||||||
| // clang-format on | ||||||||||||
| _s3_stats.total_get_request_counter++; | ||||||||||||
| if (resp.status.code != ErrorCode::OK) { | ||||||||||||
| if (resp.http_code == | ||||||||||||
| static_cast<int>(Aws::Http::HttpResponseCode::TOO_MANY_REQUESTS)) { | ||||||||||||
| s3_file_reader_too_many_request_counter << 1; | ||||||||||||
| retry_count++; | ||||||||||||
| int wait_time = std::min(base_wait_time * (1 << retry_count), | ||||||||||||
| max_wait_time); // Exponential backoff | ||||||||||||
| std::this_thread::sleep_for(std::chrono::milliseconds(wait_time)); | ||||||||||||
| _s3_stats.too_many_request_err_counter++; | ||||||||||||
| _s3_stats.too_many_request_sleep_time_ms += wait_time; | ||||||||||||
| total_sleep_time += wait_time; | ||||||||||||
| continue; | ||||||||||||
| } else { | ||||||||||||
| // Handle other errors | ||||||||||||
| return std::move(Status(resp.status.code, std::move(resp.status.msg)) | ||||||||||||
| .append("failed to read")); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| if (*bytes_read != bytes_req) { | ||||||||||||
| return Status::InternalError("failed to read (bytes read: {}, bytes req: {})", | ||||||||||||
| *bytes_read, bytes_req); | ||||||||||||
| } | ||||||||||||
| _s3_stats.total_bytes_read += bytes_req; | ||||||||||||
| s3_bytes_read_total << bytes_req; | ||||||||||||
| s3_bytes_per_read << bytes_req; | ||||||||||||
| DorisMetrics::instance()->s3_bytes_read_total->increment(bytes_req); | ||||||||||||
| if (retry_count > 0) { | ||||||||||||
| LOG(INFO) << fmt::format("read s3 file {} succeed after {} times with {} ms sleeping", | ||||||||||||
| _path.native(), retry_count, total_sleep_time); | ||||||||||||
| } | ||||||||||||
| return Status::OK(); | ||||||||||||
| } | ||||||||||||
| if (*bytes_read != bytes_req) { | ||||||||||||
| return Status::InternalError("failed to read from {}(bytes read: {}, bytes req: {})", | ||||||||||||
| _path.native(), *bytes_read, bytes_req); | ||||||||||||
| return Status::InternalError("failed to read from s3, exceeded maximum retries"); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void S3FileReader::_collect_profile_before_close() { | ||||||||||||
| if (_profile != nullptr) { | ||||||||||||
| const char* s3_profile_name = "S3Profile"; | ||||||||||||
| ADD_TIMER(_profile, s3_profile_name); | ||||||||||||
| RuntimeProfile::Counter* total_get_request_counter = | ||||||||||||
| ADD_CHILD_COUNTER(_profile, "TotalGetRequest", TUnit::UNIT, s3_profile_name); | ||||||||||||
| RuntimeProfile::Counter* too_many_request_err_counter = | ||||||||||||
| ADD_CHILD_COUNTER(_profile, "TooManyRequestErr", TUnit::UNIT, s3_profile_name); | ||||||||||||
| RuntimeProfile::Counter* too_many_request_sleep_time = ADD_CHILD_COUNTER( | ||||||||||||
| _profile, "TooManyRequestSleepTime", TUnit::TIME_MS, s3_profile_name); | ||||||||||||
| RuntimeProfile::Counter* total_bytes_read = | ||||||||||||
| ADD_CHILD_COUNTER(_profile, "TotalBytesRead", TUnit::BYTES, s3_profile_name); | ||||||||||||
|
|
||||||||||||
| COUNTER_UPDATE(total_get_request_counter, _s3_stats.total_get_request_counter); | ||||||||||||
| COUNTER_UPDATE(too_many_request_err_counter, _s3_stats.too_many_request_err_counter); | ||||||||||||
| COUNTER_UPDATE(too_many_request_sleep_time, _s3_stats.too_many_request_sleep_time_ms); | ||||||||||||
| COUNTER_UPDATE(total_bytes_read, _s3_stats.total_bytes_read); | ||||||||||||
| } | ||||||||||||
| s3_bytes_read_total << *bytes_read; | ||||||||||||
| s3_bytes_per_read << *bytes_read; | ||||||||||||
| s3_file_reader_read_counter << 1; | ||||||||||||
| DorisMetrics::instance()->s3_bytes_read_total->increment(*bytes_read); | ||||||||||||
| return Status::OK(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| } // namespace doris::io | ||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s3_bvar::s3_get_latency can get qps/P99 latency altogether. It seems no need to add this bvar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s3_get_latencycan only save "success" request.s3_get_request_qpssaves both "success" and "failed request"