Coverage Report

Created: 2026-07-11 17:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
bvar::Adder<int64_t> s3_request_retry_too_many_requests_count(
28
        "s3_request_retry_too_many_requests_count");
29
bvar::Adder<int64_t> s3_request_failed_too_many_requests_count(
30
        "s3_request_failed_too_many_requests_count");
31
32
17
void record_object_request_failed(int http_code) {
33
17
    if (http_code == static_cast<int>(Aws::Http::HttpResponseCode::TOO_MANY_REQUESTS)) {
34
0
        s3_request_failed_too_many_requests_count << 1;
35
0
    }
36
17
}
37
38
S3CustomRetryStrategy::S3CustomRetryStrategy(int maxRetries, bool retry_slow_down)
39
75
        : DefaultRetryStrategy(maxRetries), _retry_slow_down(retry_slow_down) {}
40
41
37
S3CustomRetryStrategy::~S3CustomRetryStrategy() = default;
42
43
bool S3CustomRetryStrategy::ShouldRetry(const Aws::Client::AWSError<Aws::Client::CoreErrors>& error,
44
1.20k
                                        long attemptedRetries) const {
45
1.20k
    if (attemptedRetries >= m_maxRetries) {
46
0
        return false;
47
0
    }
48
49
1.20k
    if (!_retry_slow_down && error.GetExceptionName() == "SlowDown" &&
50
1.20k
        error.GetResponseCode() == Aws::Http::HttpResponseCode::SERVICE_UNAVAILABLE) {
51
0
        return false;
52
0
    }
53
54
1.20k
    if (Aws::Http::IsRetryableHttpResponseCode(error.GetResponseCode()) || error.ShouldRetry()) {
55
0
        object_request_retry_count << 1;
56
0
        if (error.GetResponseCode() == Aws::Http::HttpResponseCode::TOO_MANY_REQUESTS) {
57
0
            s3_request_retry_too_many_requests_count << 1;
58
0
        }
59
0
        LOG(INFO) << "retry due to error: " << error << ", attempt: " << attemptedRetries + 1 << "/"
60
0
                  << m_maxRetries;
61
0
        return true;
62
0
    }
63
64
1.20k
    return false;
65
1.20k
}
66
#ifdef USE_AZURE
67
68
std::unique_ptr<Azure::Core::Http::RawResponse> AzureRetryRecordPolicy::Send(
69
        Azure::Core::Http::Request& request, Azure::Core::Http::Policies::NextHttpPolicy nextPolicy,
70
0
        Azure::Core::Context const& context) const {
71
    // https://learn.microsoft.com/en-us/azure/developer/cpp/sdk/fundamentals/http-pipelines-and-retries
72
73
0
    std::unique_ptr<Azure::Core::Http::RawResponse> response = nextPolicy.Send(request, context);
74
0
    int32_t retry_count =
75
0
            Azure::Core::Http::Policies::_internal::RetryPolicy::GetRetryCount(context);
76
77
0
    if (static_cast<int>(response->GetStatusCode()) > 299 ||
78
0
        static_cast<int>(response->GetStatusCode()) < 200) {
79
0
        if (retry_count > 0) {
80
0
            object_request_retry_count << 1;
81
0
            if (static_cast<int>(response->GetStatusCode()) ==
82
0
                static_cast<int>(Aws::Http::HttpResponseCode::TOO_MANY_REQUESTS)) {
83
0
                s3_request_retry_too_many_requests_count << 1;
84
0
            }
85
0
        }
86
87
        // If the response is not successful, we log the retry attempt and status code.
88
0
        LOG(INFO) << "azure retry retry_count: " << retry_count
89
0
                  << ", status code: " << static_cast<int>(response->GetStatusCode())
90
0
                  << ", reason: " << response->GetReasonPhrase();
91
0
    }
92
93
0
    return response;
94
0
}
95
96
0
std::unique_ptr<AzureRetryRecordPolicy::HttpPolicy> AzureRetryRecordPolicy::Clone() const {
97
0
    return std::make_unique<AzureRetryRecordPolicy>(*this);
98
0
}
99
#endif
100
} // namespace doris