Copilot commented on code in PR #1033: URL: https://github.com/apache/iceberg-cpp/pull/1033#discussion_r4321302996
########## src/iceberg/util/scheduled_executor.cc: ########## @@ -0,0 +1,239 @@ +/* + * 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 <condition_variable> +#include <map> +#include <mutex> +#include <thread> +#include <unordered_map> +#include <unordered_set> +#include <utility> + +#include "iceberg/util/scheduled_executor_internal.h" + +namespace iceberg::internal { + +class SharedTimer; + +/// Per-executor registrations; all instances share the same timer thread. +class ScheduledExecutorState + : public std::enable_shared_from_this<ScheduledExecutorState> { + public: + explicit ScheduledExecutorState(Executor& executor); + ~ScheduledExecutorState(); + + ScheduledExecutorState(const ScheduledExecutorState&) = delete; + ScheduledExecutorState& operator=(const ScheduledExecutorState&) = delete; + + ScheduledTaskHandle Schedule(std::chrono::steady_clock::time_point deadline, + ExecutorTask task, + std::optional<TaskSubmissionCallback> on_submission); + void Cancel(uint64_t id); + void Shutdown(); + + private: + friend class SharedTimer; + + void Dispatch(uint64_t id, ExecutorTask task, + std::optional<TaskSubmissionCallback> on_submission); + + Executor& executor_; + SharedTimer& timer_; + std::mutex mutex_; + std::condition_variable cv_; + std::unordered_set<uint64_t> scheduled_; + size_t active_dispatches_ = 0; + bool shutdown_ = false; +}; + +// Process-lifetime storage keeps scheduling safe even from static destructors. +class SharedTimer { + public: + static SharedTimer& Instance() { + static auto* timer = new SharedTimer(); + return *timer; + } + + uint64_t Schedule(std::weak_ptr<ScheduledExecutorState> state, + std::chrono::steady_clock::time_point deadline, ExecutorTask task, + std::optional<TaskSubmissionCallback> on_submission) { + 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_submission = std::move(on_submission)}); + by_id_.emplace(id, it); + cv_.notify_one(); Review Comment: `inserted` is never used. If the project builds with `-Werror` (common in CI), this can fail compilation due to `-Wunused-variable`. Use `auto it = tasks_.emplace(...).first;` or explicitly ignore `inserted` (e.g., `(void)inserted;`). ########## 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 changes the positional argument order for a public factory (`metrics_executor` moved from 2nd to 4th), which is a source-level breaking change for any downstream callers passing `metrics_executor` positionally. Consider adding an overload that preserves the previous signature (and forwards to the new one), optionally marking it deprecated, to maintain compatibility. ########## src/iceberg/test/scheduled_executor_test.cc: ########## @@ -0,0 +1,254 @@ +/* + * 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 <atomic> +#include <chrono> +#include <cstdlib> +#include <future> +#include <memory> +#include <thread> + +#include <gtest/gtest.h> + +#include "iceberg/util/scheduled_executor_internal.h" + +namespace iceberg::internal { +namespace { + +// Small inline callbacks let the tests observe the timer's submitting thread. +class InlineExecutor : public Executor { + public: + Status Submit(ExecutorTask task) override { + ++submissions; + std::move(task)(); + return {}; + } + int submissions = 0; +}; Review Comment: `submissions` is mutated on the timer thread and read on the test thread. While current waits on futures *likely* establish the needed happens-before, this is subtle and easy to break with small test refactors. Making `submissions` an `std::atomic<int>` (or guarding it) would make the test's thread-safety explicit. -- 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]
