wgtmac commented on code in PR #633:
URL: https://github.com/apache/iceberg-cpp/pull/633#discussion_r3193255372


##########
src/iceberg/util/retry_util.cc:
##########
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/util/retry_util.h"
+
+#include <algorithm>
+#include <chrono>
+#include <cmath>
+#include <cstdint>
+#include <limits>
+#include <optional>
+#include <random>
+#include <thread>
+
+#include "iceberg/util/retry_util_internal.h"
+
+namespace iceberg {
+namespace {
+
+const RetryTestHooks*& ActiveRetryTestHooks() {
+  // Keep test hooks thread-local so fake retry timing in one test thread does 
not
+  // leak into unrelated retry work or require synchronization around a global 
pointer.
+  static thread_local const RetryTestHooks* active_retry_test_hooks = nullptr;
+  return active_retry_test_hooks;
+}
+
+RetryTestHooks::TimePoint RetryNow() {
+  const auto* hooks = GetActiveRetryTestHooks();
+  if (hooks != nullptr && hooks->now) {
+    return hooks->now();
+  }
+  return RetryTestHooks::Clock::now();
+}
+
+void RetrySleepFor(RetryTestHooks::Duration duration) {
+  const auto* hooks = GetActiveRetryTestHooks();
+  if (hooks != nullptr && hooks->sleep_for) {
+    hooks->sleep_for(duration);
+    return;
+  }
+  std::this_thread::sleep_for(duration);
+}
+
+int32_t ApplyRetryJitter(int32_t base_delay_ms) {
+  const auto* hooks = GetActiveRetryTestHooks();
+  if (hooks != nullptr && hooks->jitter) {
+    return hooks->jitter(base_delay_ms);
+  }
+
+  static thread_local std::mt19937 gen(std::random_device{}());
+  const int32_t jitter_range = std::max(1, base_delay_ms / 10);
+  std::uniform_int_distribution<> dis(0, jitter_range - 1);
+  const int64_t jittered_delay_ms = static_cast<int64_t>(base_delay_ms) + 
dis(gen);
+  return static_cast<int32_t>(
+      std::min<int64_t>(jittered_delay_ms, 
std::numeric_limits<int32_t>::max()));
+}
+
+}  // namespace
+
+const RetryTestHooks* GetActiveRetryTestHooks() { return 
ActiveRetryTestHooks(); }
+
+void SetActiveRetryTestHooks(const RetryTestHooks* hooks) {
+  ActiveRetryTestHooks() = hooks;
+}
+
+Status RetryRunner::ValidateConfig() const {
+  if (config_.num_retries < 0) {
+    return InvalidArgument("num_retries must be non-negative, got {}",
+                           config_.num_retries);
+  }
+  if (config_.num_retries == 0) {
+    return {};

Review Comment:
   Keeping the current return behavior here. There is no `RetryExhausted` 
wrapper today; retry exhaustion returns the last task error so callers keep the 
original `ErrorKind` and message. Collecting all failed-run reasons would be a 
separate error aggregation or metrics change.



-- 
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]

Reply via email to