This is an automated email from the ASF dual-hosted git repository.
wgtmac pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 62ea2e21 fix(rest): make RestCatalog safe to share across threads
(#1092)
62ea2e21 is described below
commit 62ea2e21ad8b1bc345871e9ceeefb44d93de17a0
Author: Sreesh Maheshwar <[email protected]>
AuthorDate: Mon Nov 23 06:09:42 2026 +0000
fix(rest): make RestCatalog safe to share across threads (#1092)
---
src/iceberg/catalog/rest/rest_catalog.cc | 1 +
src/iceberg/catalog/rest/rest_catalog.h | 8 +++-
src/iceberg/test/rest_catalog_integration_test.cc | 53 +++++++++++++++++++++--
src/iceberg/test/rest_catalog_unit_test.cc | 32 ++++++++++++++
4 files changed, 89 insertions(+), 5 deletions(-)
diff --git a/src/iceberg/catalog/rest/rest_catalog.cc
b/src/iceberg/catalog/rest/rest_catalog.cc
index 5f7e726b..a6ba928e 100644
--- a/src/iceberg/catalog/rest/rest_catalog.cc
+++ b/src/iceberg/catalog/rest/rest_catalog.cc
@@ -587,6 +587,7 @@ RestCatalog::RestCatalog(RestCatalogProperties config,
std::shared_ptr<FileIO> f
std::string_view RestCatalog::name() const { return name_; }
Result<std::shared_ptr<Catalog>> RestCatalog::AsCatalog() {
+ std::lock_guard lock(default_catalog_mutex_);
if (auto catalog = default_catalog_.lock()) {
return catalog;
}
diff --git a/src/iceberg/catalog/rest/rest_catalog.h
b/src/iceberg/catalog/rest/rest_catalog.h
index 561ff347..a8a0f834 100644
--- a/src/iceberg/catalog/rest/rest_catalog.h
+++ b/src/iceberg/catalog/rest/rest_catalog.h
@@ -21,6 +21,7 @@
#include <functional>
#include <memory>
+#include <mutex>
#include <optional>
#include <string>
#include <unordered_set>
@@ -43,6 +44,10 @@
namespace iceberg::rest {
/// \brief Session-aware REST catalog root.
+///
+/// Thread-safe, as are the catalogs from AsCatalog() and WithContext().
Custom HTTP
+/// clients, FileIOs, metrics reporters and auth managers must be thread-safe
too, as
+/// must the executors passed to Make().
class ICEBERG_REST_EXPORT RestCatalog final
: public SessionCatalog,
public std::enable_shared_from_this<RestCatalog> {
@@ -235,7 +240,8 @@ class ICEBERG_REST_EXPORT RestCatalog final
SnapshotMode snapshot_mode_;
std::optional<ScanPlanningMode> client_scan_planning_mode_;
SessionContext default_context_;
- std::weak_ptr<Catalog> default_catalog_;
+ std::mutex default_catalog_mutex_;
+ std::weak_ptr<Catalog> default_catalog_; // Guarded by
default_catalog_mutex_.
std::shared_ptr<MetricsReporter> reporter_;
Executor* metrics_executor_ = nullptr;
};
diff --git a/src/iceberg/test/rest_catalog_integration_test.cc
b/src/iceberg/test/rest_catalog_integration_test.cc
index a13d2dbd..2592cc2e 100644
--- a/src/iceberg/test/rest_catalog_integration_test.cc
+++ b/src/iceberg/test/rest_catalog_integration_test.cc
@@ -141,9 +141,9 @@ class RestCatalogIntegrationTest : public ::testing::Test {
return CreateCatalogWithProperties({});
}
- /// Create a catalog with additional properties merged on top of defaults.
- Result<std::shared_ptr<Catalog>> CreateCatalogWithProperties(
- const std::unordered_map<std::string, std::string>& extra) {
+ /// Create a catalog root with additional properties merged on top of
defaults.
+ Result<std::shared_ptr<RestCatalog>> CreateRootWithProperties(
+ const std::unordered_map<std::string, std::string>& extra = {}) {
auto config = RestCatalogProperties::default_properties();
config.Set(RestCatalogProperties::kUri, CatalogUri())
.Set(RestCatalogProperties::kName, std::string(kCatalogName))
@@ -153,7 +153,13 @@ class RestCatalogIntegrationTest : public ::testing::Test {
for (const auto& [k, v] : extra) {
config.mutable_configs()[k] = v;
}
- ICEBERG_ASSIGN_OR_RAISE(auto root, RestCatalog::Make(config));
+ return RestCatalog::Make(config);
+ }
+
+ /// Create a catalog with additional properties merged on top of defaults.
+ Result<std::shared_ptr<Catalog>> CreateCatalogWithProperties(
+ const std::unordered_map<std::string, std::string>& extra) {
+ ICEBERG_ASSIGN_OR_RAISE(auto root, CreateRootWithProperties(extra));
return root->AsCatalog();
}
@@ -451,6 +457,45 @@ TEST_F(RestCatalogIntegrationTest,
ConcurrentHttpClientRequests) {
}
}
+// Threads can share one RestCatalog and its default catalog.
+TEST_F(RestCatalogIntegrationTest, ConcurrentCatalogUse) {
+ constexpr int kThreads = 16;
+ constexpr int kIterationsPerThread = 50;
+
+ ICEBERG_UNWRAP_OR_FAIL(auto root, CreateRootWithProperties());
+
+ Namespace ns{.levels = {"test_concurrent_catalog_use"}};
+ TableIdentifier table_id{.ns = ns, .name = "events"};
+ std::string table_uuid;
+ // Drop this catalog so the threads below race to create the default catalog.
+ {
+ ICEBERG_UNWRAP_OR_FAIL(auto catalog, root->AsCatalog());
+ ASSERT_THAT(catalog->CreateNamespace(ns, {}), IsOk());
+ ICEBERG_UNWRAP_OR_FAIL(auto table, CreateDefaultTable(catalog, table_id));
+ table_uuid = table->uuid();
+ }
+
+ std::vector<std::thread> threads;
+ threads.reserve(kThreads);
+ for (int i = 0; i < kThreads; ++i) {
+ threads.emplace_back([&] {
+ ICEBERG_UNWRAP_OR_FAIL(auto catalog, root->AsCatalog());
+ for (int j = 0; j < kIterationsPerThread; ++j) {
+ ICEBERG_UNWRAP_OR_FAIL(auto table, catalog->LoadTable(table_id));
+ EXPECT_EQ(table->uuid(), table_uuid);
+ ASSERT_THAT(catalog->ListNamespaces(ns), IsOk());
+ }
+ });
+ }
+ for (auto& thread : threads) {
+ thread.join();
+ }
+
+ ICEBERG_UNWRAP_OR_FAIL(auto catalog, root->AsCatalog());
+ ASSERT_THAT(catalog->DropTable(table_id, /*purge=*/false), IsOk());
+ ASSERT_THAT(catalog->DropNamespace(ns), IsOk());
+}
+
// -- Namespace operations --
TEST_F(RestCatalogIntegrationTest, ListNamespaces) {
diff --git a/src/iceberg/test/rest_catalog_unit_test.cc
b/src/iceberg/test/rest_catalog_unit_test.cc
index b1fc9fad..59e672c8 100644
--- a/src/iceberg/test/rest_catalog_unit_test.cc
+++ b/src/iceberg/test/rest_catalog_unit_test.cc
@@ -17,11 +17,13 @@
* under the License.
*/
+#include <barrier>
#include <chrono>
#include <future>
#include <memory>
#include <optional>
#include <string>
+#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <vector>
@@ -500,6 +502,36 @@ TEST_F(RestCatalogLoadTableTest,
EXPECT_EQ(credentialed->credentials()[0].config.at("k"), "v");
}
+// Threads that hold the default catalog at the same time get the same
instance.
+TEST_F(RestCatalogLoadTableTest, ConcurrentAsCatalogCallsShareDefaultCatalog) {
+ constexpr int kThreads = 16;
+ constexpr int kRounds = 200;
+ ICEBERG_UNWRAP_OR_FAIL(auto root, MakeCatalog({}));
+
+ std::barrier sync(kThreads);
+ std::vector<std::shared_ptr<Catalog>> held(kThreads);
+ std::vector<std::thread> threads;
+ threads.reserve(kThreads);
+ for (int i = 0; i < kThreads; ++i) {
+ threads.emplace_back([&, i] {
+ for (int round = 0; round < kRounds; ++round) {
+ // No thread holds a default catalog here, so each round creates a new
one.
+ sync.arrive_and_wait();
+ held[i] = root->AsCatalog().value_or(nullptr);
+ sync.arrive_and_wait();
+ // ASSERT would return early and leave the other threads at the
barrier.
+ EXPECT_NE(held[i], nullptr);
+ EXPECT_EQ(held[i], held[0]);
+ sync.arrive_and_wait();
+ held[i].reset();
+ }
+ });
+ }
+ for (auto& thread : threads) {
+ thread.join();
+ }
+}
+
struct PlanningModeCase {
std::string client;
std::string table;