Copilot commented on code in PR #1033:
URL: https://github.com/apache/iceberg-cpp/pull/1033#discussion_r4316023899


##########
src/iceberg/util/scheduled_executor.cc:
##########
@@ -0,0 +1,249 @@
+/*
+ * 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 <map>
+#include <thread>
+#include <unordered_map>
+#include <utility>
+
+#include "iceberg/util/scheduled_executor_internal.h"
+
+namespace iceberg::internal {
+
+// The timer thread retains its state until Run returns. This also allows the 
last
+// timer owner to be released on the timer thread during process teardown.
+class SharedTimerState : public std::enable_shared_from_this<SharedTimerState> 
{
+ public:
+  ~SharedTimerState() { Shutdown(); }
+
+  void Start() {
+    timer_thread_ = std::thread([self = shared_from_this()] { self->Run(); });
+  }
+
+  uint64_t Schedule(std::weak_ptr<ScheduledExecutorState> state,
+                    std::chrono::steady_clock::time_point deadline, 
ExecutorTask task,
+                    std::optional<ScheduledTaskRejection> on_rejected) {
+    std::lock_guard lock(mutex_);
+    auto id = next_id_++;
+    auto [it, inserted] = tasks_.emplace(std::pair{deadline, id},
+                                         Task{.state = std::move(state),
+                                              .task = std::move(task),
+                                              .on_rejected = 
std::move(on_rejected)});
+    by_id_.emplace(id, it);
+    cv_.notify_one();
+    return id;
+  }
+
+  void Cancel(uint64_t id) {
+    std::optional<Task> cancelled;
+    {
+      std::lock_guard lock(mutex_);
+      auto it = by_id_.find(id);
+      if (it == by_id_.end()) return;
+      cancelled.emplace(std::move(it->second->second));
+      tasks_.erase(it->second);
+      by_id_.erase(it);
+    }
+    cv_.notify_one();
+    // Release callback captures outside the queue lock.
+  }
+
+  bool IsTimerThread() const {
+    return timer_thread_.get_id() == std::this_thread::get_id();
+  }
+
+  void Shutdown() {
+    Queue discarded;
+    {
+      std::lock_guard lock(mutex_);
+      if (shutdown_) return;
+      shutdown_ = true;
+      tasks_.swap(discarded);
+      by_id_.clear();
+    }
+    cv_.notify_one();
+    if (!timer_thread_.joinable()) return;
+    if (IsTimerThread()) {
+      timer_thread_.detach();
+    } else {
+      timer_thread_.join();
+    }
+  }
+
+ private:
+  struct Task {
+    std::weak_ptr<ScheduledExecutorState> state;
+    ExecutorTask task;
+    std::optional<ScheduledTaskRejection> on_rejected;
+  };
+
+  using Key = std::pair<std::chrono::steady_clock::time_point, uint64_t>;
+  using Queue = std::map<Key, Task>;
+
+  void Run() {
+    while (true) {
+      std::optional<Task> task;
+      uint64_t id;
+      {
+        std::unique_lock lock(mutex_);
+        cv_.wait(lock, [this] { return shutdown_ || !tasks_.empty(); });
+        if (shutdown_) return;
+
+        auto key = tasks_.begin()->first;
+        if (cv_.wait_until(lock, key.first, [this, key] {
+              return shutdown_ || tasks_.empty() || tasks_.begin()->first != 
key;
+            })) {
+          continue;
+        }
+        auto it = tasks_.begin();
+        id = it->first.second;
+        task.emplace(std::move(it->second));
+        by_id_.erase(id);
+        tasks_.erase(it);
+      }
+      if (auto state = task->state.lock()) {
+        state->Dispatch(id, std::move(task->task), 
std::move(task->on_rejected));
+      }
+    }
+  }
+
+  std::mutex mutex_;
+  std::condition_variable cv_;
+  Queue tasks_;
+  std::unordered_map<uint64_t, Queue::iterator> by_id_;
+  uint64_t next_id_ = 1;
+  bool shutdown_ = false;
+  std::thread timer_thread_;
+};
+
+class SharedTimer {
+ public:
+  static std::shared_ptr<SharedTimer> Instance() {
+    static auto timer = std::make_shared<SharedTimer>();
+    return timer;
+  }
+
+  SharedTimer() : state_(std::make_shared<SharedTimerState>()) { 
state_->Start(); }
+
+  ~SharedTimer() { state_->Shutdown(); }
+
+  uint64_t Schedule(std::weak_ptr<ScheduledExecutorState> state,
+                    std::chrono::steady_clock::time_point deadline, 
ExecutorTask task,
+                    std::optional<ScheduledTaskRejection> on_rejected) {
+    return state_->Schedule(std::move(state), deadline, std::move(task),
+                            std::move(on_rejected));
+  }
+
+  void Cancel(uint64_t id) { state_->Cancel(id); }
+
+  bool IsTimerThread() const { return state_->IsTimerThread(); }
+
+ private:
+  std::shared_ptr<SharedTimerState> state_;
+};
+
+void ScheduledTask::Cancel() {
+  if (id_.has_value()) {
+    if (auto state = state_.lock()) state->Cancel(*id_);
+  }
+  state_.reset();
+  id_.reset();
+}
+
+ScheduledExecutorState::ScheduledExecutorState(Executor& executor)
+    : executor_(executor), timer_(SharedTimer::Instance()) {}
+
+ScheduledExecutorState::~ScheduledExecutorState() { Shutdown(); }
+
+ScheduledTask ScheduledExecutorState::Schedule(
+    std::chrono::steady_clock::time_point deadline, ExecutorTask task,
+    std::optional<ScheduledTaskRejection> on_rejected) {
+  std::lock_guard lock(mutex_);
+  if (shutdown_) return {};
+  auto id = timer_->Schedule(weak_from_this(), deadline, std::move(task),
+                             std::move(on_rejected));
+  scheduled_.insert(id);
+  return {weak_from_this(), id};
+}
+
+void ScheduledExecutorState::Cancel(uint64_t id) {
+  {
+    std::lock_guard lock(mutex_);
+    if (scheduled_.erase(id) == 0) return;
+  }
+  timer_->Cancel(id);
+}
+
+void ScheduledExecutorState::Shutdown() {
+  std::unordered_set<uint64_t> pending;
+  {
+    std::lock_guard lock(mutex_);
+    shutdown_ = true;
+    scheduled_.swap(pending);
+  }
+  for (auto id : pending) timer_->Cancel(id);
+  if (!timer_->IsTimerThread()) {
+    std::unique_lock lock(mutex_);
+    cv_.wait(lock, [this] { return active_dispatches_ == 0; });
+  }
+}

Review Comment:
   `Shutdown()` is called from both `ScheduledExecutor::~ScheduledExecutor()` 
and `ScheduledExecutorState::~ScheduledExecutorState()`, so it will commonly 
run multiple times during teardown. Making `Shutdown()` explicitly idempotent 
(e.g., early-return when `shutdown_` is already true) would reduce repeated 
work (re-cancel loops / extra waits) and make the lifecycle behavior clearer.



##########
src/iceberg/catalog/rest/rest_catalog.h:
##########
@@ -58,14 +58,19 @@ class ICEBERG_REST_EXPORT RestCatalog final
       std::function<Result<std::shared_ptr<HttpClient>>(const 
RestCatalogProperties&)>;
 
   /// \brief Create a RestCatalog instance.
+  /// \param client_factory Optional HTTP client factory, called with the 
initial config
+  /// and then the merged server config. When empty, uses the default HTTP 
client.
+  /// \param auth_executor Optional non-owning executor for OAuth2 token 
refresh. When
+  /// null, background token refresh is disabled. When non-null, it must 
outlive the
+  /// catalog and all authentication sessions created by it. It must enqueue 
work
+  /// asynchronously without blocking the shared timer thread.
   /// \param metrics_executor Optional non-owning executor for asynchronous 
REST metrics.
   /// It must outlive the catalog and all tables created by it. When null, 
REST metrics
   /// are reported synchronously.
-  /// \param client_factory Optional HTTP client factory, called with the 
initial config
-  /// and then the merged server config. When empty, uses the default HTTP 
client.
   static Result<std::shared_ptr<RestCatalog>> Make(const 
RestCatalogProperties& config,
-                                                   Executor* metrics_executor 
= nullptr,
-                                                   ClientFactory 
client_factory = {});
+                                                   ClientFactory 
client_factory = {},
+                                                   Executor* auth_executor = 
nullptr,
+                                                   Executor* metrics_executor 
= nullptr);
 

Review Comment:
   This reorders `RestCatalog::Make` parameters and removes the prior `(config, 
metrics_executor, client_factory)` calling pattern, which is a source-level 
breaking change for existing callers. Consider adding an overload that 
preserves the old signature (and forwards to the new one), or introducing a 
small options/config struct to avoid further signature churn while keeping call 
sites unambiguous.



##########
src/iceberg/util/scheduled_executor_internal.h:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <chrono>
+#include <condition_variable>
+#include <cstdint>
+#include <memory>
+#include <mutex>
+#include <optional>
+#include <unordered_set>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/result.h"
+#include "iceberg/util/executor.h"
+
+namespace iceberg::internal {
+
+class SharedTimer;
+class SharedTimerState;
+class ScheduledExecutorState;
+
+using ScheduledTaskRejection = FnOnce<void(Status)>;
+
+class ICEBERG_EXPORT ScheduledTask {
+ public:
+  ScheduledTask() = default;
+  ScheduledTask(std::weak_ptr<ScheduledExecutorState> state, uint64_t id)
+      : state_(std::move(state)), id_(id) {}
+
+  void Cancel();
+  bool IsValid() const { return id_.has_value() && !state_.expired(); }
+
+ private:
+  std::weak_ptr<ScheduledExecutorState> state_;
+  std::optional<uint64_t> id_;
+};

Review Comment:
   `ScheduledTask::Cancel()` mutates `state_`/`id_` without synchronization. If 
a `ScheduledTask` handle is shared across threads (a common pattern for 
cancellation tokens), concurrent `Cancel()` / `IsValid()` calls would be a data 
race. Either (mandatory) document that `ScheduledTask` is not thread-safe and 
must be externally synchronized, or (optional, safer API) move the handle state 
behind a shared object with internal synchronization/atomics so concurrent 
cancellation is well-defined.



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