common/cpp/obj_retry_strategy.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 "obj_retry_strategy.h" |
19 | | |
20 | | #include <aws/core/http/HttpResponse.h> |
21 | | #include <bvar/reducer.h> |
22 | | #include <glog/logging.h> |
23 | | |
24 | | namespace doris { |
25 | | |
26 | | bvar::Adder<int64_t> object_request_retry_count("object_request_retry_count"); |
27 | | |
28 | | S3CustomRetryStrategy::S3CustomRetryStrategy(int maxRetries, bool retry_slow_down) |
29 | 2 | : DefaultRetryStrategy(maxRetries), _retry_slow_down(retry_slow_down) {} |
30 | | |
31 | 2 | S3CustomRetryStrategy::~S3CustomRetryStrategy() = default; |
32 | | |
33 | | bool S3CustomRetryStrategy::ShouldRetry(const Aws::Client::AWSError<Aws::Client::CoreErrors>& error, |
34 | 0 | long attemptedRetries) const { |
35 | 0 | if (attemptedRetries >= m_maxRetries) { |
36 | 0 | return false; |
37 | 0 | } |
38 | | |
39 | 0 | if (!_retry_slow_down && error.GetExceptionName() == "SlowDown" && |
40 | 0 | error.GetResponseCode() == Aws::Http::HttpResponseCode::SERVICE_UNAVAILABLE) { |
41 | 0 | return false; |
42 | 0 | } |
43 | | |
44 | 0 | if (Aws::Http::IsRetryableHttpResponseCode(error.GetResponseCode()) || error.ShouldRetry()) { |
45 | 0 | object_request_retry_count << 1; |
46 | 0 | LOG(INFO) << "retry due to error: " << error << ", attempt: " << attemptedRetries + 1 << "/" |
47 | 0 | << m_maxRetries; |
48 | 0 | return true; |
49 | 0 | } |
50 | | |
51 | 0 | return false; |
52 | 0 | } |
53 | | #ifdef USE_AZURE |
54 | | |
55 | | std::unique_ptr<Azure::Core::Http::RawResponse> AzureRetryRecordPolicy::Send( |
56 | | Azure::Core::Http::Request& request, Azure::Core::Http::Policies::NextHttpPolicy nextPolicy, |
57 | 0 | Azure::Core::Context const& context) const { |
58 | | // https://learn.microsoft.com/en-us/azure/developer/cpp/sdk/fundamentals/http-pipelines-and-retries |
59 | |
|
60 | 0 | std::unique_ptr<Azure::Core::Http::RawResponse> response = nextPolicy.Send(request, context); |
61 | 0 | int32_t retry_count = |
62 | 0 | Azure::Core::Http::Policies::_internal::RetryPolicy::GetRetryCount(context); |
63 | |
|
64 | 0 | if (static_cast<int>(response->GetStatusCode()) > 299 || |
65 | 0 | static_cast<int>(response->GetStatusCode()) < 200) { |
66 | 0 | if (retry_count > 0) { |
67 | 0 | object_request_retry_count << 1; |
68 | 0 | } |
69 | | |
70 | | // If the response is not successful, we log the retry attempt and status code. |
71 | 0 | LOG(INFO) << "azure retry retry_count: " << retry_count |
72 | 0 | << ", status code: " << static_cast<int>(response->GetStatusCode()) |
73 | 0 | << ", reason: " << response->GetReasonPhrase(); |
74 | 0 | } |
75 | |
|
76 | 0 | return response; |
77 | 0 | } |
78 | | |
79 | 0 | std::unique_ptr<AzureRetryRecordPolicy::HttpPolicy> AzureRetryRecordPolicy::Clone() const { |
80 | 0 | return std::make_unique<AzureRetryRecordPolicy>(*this); |
81 | 0 | } |
82 | | #endif |
83 | | } // namespace doris |