zhjwpku commented on code in PR #1060:
URL: https://github.com/apache/iceberg-cpp/pull/1060#discussion_r4660826398
##########
src/iceberg/catalog/rest/http_client.cc:
##########
@@ -72,6 +231,319 @@ std::unordered_map<std::string, std::string>
HttpResponse::headers() const {
namespace {
+// The retry rules below mirror
+// org.apache.iceberg.rest.ExponentialHttpRequestRetryStrategy.
+
+constexpr std::string_view kHeaderRetryAfter = "Retry-After";
+
+/// \brief HTTP methods whose repetition is safe (RFC 9110 idempotent methods).
+bool IsIdempotent(HttpMethod method) {
+ switch (method) {
+ case HttpMethod::kGet:
+ case HttpMethod::kHead:
+ case HttpMethod::kDelete:
+ case HttpMethod::kPut:
+ return true;
+ default:
+ return false;
+ }
+}
+
+/// \brief Request-execution I/O failures eligible for Java-style retries.
+/// An empty reply corresponds to NoHttpResponseException. Premature entity
+/// closure, malformed responses, DNS, connect, TLS and timeout errors are
excluded.
+bool IsRetriableTransportError(cpr::ErrorCode code) {
+ switch (code) {
+ case cpr::ErrorCode::GOT_NOTHING:
+ case cpr::ErrorCode::SEND_ERROR:
+ case cpr::ErrorCode::RECV_ERROR:
+ case cpr::ErrorCode::HTTP2:
+ case cpr::ErrorCode::HTTP2_STREAM:
+ return true;
+ default:
+ return false;
+ }
+}
+
+} // namespace
+
+namespace {
+
+std::chrono::milliseconds Backoff(int64_t exec_count) {
+ if (exec_count <= 0) return std::chrono::milliseconds::zero();
+ return std::chrono::milliseconds(
+ 1000 * (exec_count > 7 ? int64_t{64} : int64_t{1} << (exec_count - 1)));
+}
+
+std::optional<std::chrono::milliseconds> RetryAfter(
+ const cpr::Response& response, std::chrono::system_clock::time_point now) {
+ auto it = response.header.find(std::string(kHeaderRetryAfter));
+ if (it == response.header.end()) return std::nullopt;
+ auto value = std::string_view(it->second);
+ if (value.starts_with('+')) value.remove_prefix(1);
+ int64_t seconds = 0;
+ auto [end, ec] = std::from_chars(value.data(), value.data() + value.size(),
seconds);
+ if (ec == std::errc{} && end == value.data() + value.size()) {
+ if (seconds <= 0) return std::nullopt;
+ constexpr auto kMaxSeconds = (std::chrono::milliseconds::max)().count() /
1000;
+ if (seconds > kMaxSeconds) return (std::chrono::milliseconds::max)();
+ return std::chrono::milliseconds(seconds * 1000);
+ }
+ const auto date = curl_getdate(it->second.c_str(), nullptr);
+ if (date < 0) return std::nullopt;
+ const auto now_ms =
+
std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch())
+ .count();
+ const auto date_seconds = static_cast<int64_t>(date);
+ if (date_seconds > (std::chrono::milliseconds::max)().count() / 1000) {
+ return (std::chrono::milliseconds::max)();
+ }
+ const auto date_ms = date_seconds * 1000;
+ if (date_ms <= now_ms) return std::nullopt;
+ int64_t delay;
+ if (!CheckedSubtract(date_ms, now_ms, delay)) return
(std::chrono::milliseconds::max)();
+ return std::chrono::milliseconds(delay);
+}
+
+} // namespace
+
+namespace detail {
+
+bool ShouldRetry(const cpr::Response& response, const HttpRequest& request,
+ int64_t exec_count, const HttpClientOptions& options,
+ std::optional<std::chrono::milliseconds> elapsed,
+ std::chrono::system_clock::time_point now) {
+ if (exec_count > options.max_retries) return false;
+ const bool has_key = request.headers.contains("Idempotency-Key");
+ const bool safe = IsIdempotent(request.method) || has_key;
+ if (response.error) {
+ // Java reads the final response entity outside HttpRequestRetryExec. A
body
+ // read failure is propagated, even for a successful status or keyed
request.
+ if (response.status_code != 0 || !safe ||
+ !IsRetriableTransportError(response.error.code)) {
+ return false;
Review Comment:
Fixed by tracking final-header completion explicitly. Partial-header resets
can retry safe requests; body-read failures remain non-retryable. Added
regression tests for both cases.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]