-
Notifications
You must be signed in to change notification settings - Fork 115
feat(hive): add HmsClient connection lifecycle and URI parsing #796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MisterRaindrop
wants to merge
5
commits into
apache:main
Choose a base branch
from
MisterRaindrop:feat/hive-hms-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1949226
feat(hive): HmsClient connection lifecycle and URI parsing
MisterRaindrop dd4f08d
ci(hive): build, test and lint iceberg_hive with bundled Thrift
MisterRaindrop 19e055c
refactor(hive): read HMS timeouts as typed int properties
MisterRaindrop cc991bf
ci: pin Arrow's bundled Thrift download to the archive mirror
MisterRaindrop 2a22e96
fix(hive): add direct includes flagged by clang-tidy include-cleaner
MisterRaindrop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| /* | ||
| * 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/catalog/hive/hms_client.h" | ||
|
|
||
| #include <cctype> | ||
| #include <charconv> | ||
| #include <cstddef> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <system_error> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <thrift/Thrift.h> | ||
| #include <thrift/protocol/TBinaryProtocol.h> | ||
| #include <thrift/transport/TBufferTransports.h> | ||
| #include <thrift/transport/TSocket.h> | ||
| #include <thrift/transport/TTransportException.h> | ||
|
|
||
| #include "ThriftHiveMetastore.h" | ||
| #include "iceberg/catalog/hive/hive_catalog_properties.h" | ||
| #include "iceberg/result.h" | ||
| #include "iceberg/util/macros.h" | ||
|
|
||
| namespace iceberg::hive { | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr std::string_view kThriftPrefix = "thrift://"; | ||
|
|
||
| std::string_view StripScheme(std::string_view spec) { | ||
| if (spec.starts_with(kThriftPrefix)) { | ||
| return spec.substr(kThriftPrefix.size()); | ||
| } | ||
| return spec; | ||
| } | ||
|
|
||
| std::string_view Trim(std::string_view spec) { | ||
| while (!spec.empty() && (std::isspace(static_cast<unsigned char>(spec.front())) != 0)) { | ||
| spec.remove_prefix(1); | ||
| } | ||
| while (!spec.empty() && (std::isspace(static_cast<unsigned char>(spec.back())) != 0)) { | ||
| spec.remove_suffix(1); | ||
| } | ||
| return spec; | ||
| } | ||
|
|
||
| Result<HmsEndpoint> ParseSingleEndpoint(std::string_view spec) { | ||
| spec = Trim(spec); | ||
| spec = StripScheme(spec); | ||
| spec = Trim(spec); | ||
| if (spec.empty()) { | ||
| return InvalidArgument("Empty HMS endpoint in URI list."); | ||
| } | ||
|
|
||
| HmsEndpoint endpoint; | ||
| const auto colon = spec.rfind(':'); | ||
| if (colon == std::string_view::npos) { | ||
| endpoint.host = std::string(spec); | ||
| endpoint.port = kDefaultHmsPort; | ||
| return endpoint; | ||
| } | ||
|
|
||
| endpoint.host = std::string(spec.substr(0, colon)); | ||
| if (endpoint.host.empty()) { | ||
| return InvalidArgument("HMS endpoint has empty host: '{}'.", spec); | ||
| } | ||
|
|
||
| const auto port_str = spec.substr(colon + 1); | ||
| if (port_str.empty()) { | ||
| endpoint.port = kDefaultHmsPort; | ||
| return endpoint; | ||
| } | ||
|
|
||
| int port = 0; | ||
| const auto* const port_end = port_str.data() + port_str.size(); | ||
| const auto [ptr, ec] = std::from_chars(port_str.data(), port_end, port); | ||
| if (ec != std::errc() || ptr != port_end || port <= 0 || port > 65535) { | ||
| return InvalidArgument("Invalid HMS port in endpoint '{}'.", spec); | ||
| } | ||
| endpoint.port = port; | ||
| return endpoint; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| Result<std::vector<HmsEndpoint>> ParseHmsUris(std::string_view uri) { | ||
| std::vector<HmsEndpoint> endpoints; | ||
| if (Trim(uri).empty()) { | ||
| return InvalidArgument("HMS URI is empty."); | ||
| } | ||
|
|
||
| std::size_t pos = 0; | ||
| while (pos <= uri.size()) { | ||
| const auto comma = uri.find(',', pos); | ||
| const auto piece = uri.substr( | ||
| pos, comma == std::string_view::npos ? std::string_view::npos : comma - pos); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto endpoint, ParseSingleEndpoint(piece)); | ||
| endpoints.push_back(std::move(endpoint)); | ||
| if (comma == std::string_view::npos) { | ||
| break; | ||
| } | ||
| pos = comma + 1; | ||
| } | ||
| return endpoints; | ||
| } | ||
|
|
||
| // Fields are declared in dependency order (socket <- transport <- protocol | ||
| // <- client) so the client tears down before the transport it borrows. | ||
| class HmsClient::Impl { | ||
| public: | ||
| std::shared_ptr<apache::thrift::transport::TSocket> socket; | ||
| std::shared_ptr<apache::thrift::transport::TTransport> transport; | ||
| std::shared_ptr<apache::thrift::protocol::TProtocol> protocol; | ||
| std::unique_ptr<Apache::Hadoop::Hive::ThriftHiveMetastoreClient> client; | ||
| }; | ||
|
|
||
| HmsClient::HmsClient(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {} | ||
|
|
||
| HmsClient::~HmsClient() { | ||
| if (impl_ && impl_->transport && impl_->transport->isOpen()) { | ||
| try { | ||
| impl_->transport->close(); | ||
| } catch (const apache::thrift::TException&) { | ||
| // Best-effort close on teardown; ignore exceptions. | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Result<std::unique_ptr<HmsClient>> HmsClient::Connect( | ||
| const HiveCatalogProperties& config) { | ||
| ICEBERG_ASSIGN_OR_RAISE(auto uri, config.Uri()); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto endpoints, ParseHmsUris(uri)); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto transport_mode, config.ThriftTransport()); | ||
|
|
||
| if (endpoints.size() > 1) { | ||
| return InvalidArgument( | ||
| "Multi-endpoint HMS URIs are not yet supported; HA failover is not " | ||
| "implemented. Configure a single endpoint. Got {} endpoints.", | ||
| endpoints.size()); | ||
| } | ||
| const HmsEndpoint& endpoint = endpoints.front(); | ||
| const int connect_timeout_ms = config.Get(HiveCatalogProperties::kConnectTimeoutMs); | ||
| const int socket_timeout_ms = config.Get(HiveCatalogProperties::kSocketTimeoutMs); | ||
|
|
||
| auto socket = | ||
| std::make_shared<apache::thrift::transport::TSocket>(endpoint.host, endpoint.port); | ||
| socket->setConnTimeout(connect_timeout_ms); | ||
| socket->setRecvTimeout(socket_timeout_ms); | ||
| socket->setSendTimeout(socket_timeout_ms); | ||
|
|
||
| std::shared_ptr<apache::thrift::transport::TTransport> transport; | ||
| switch (transport_mode) { | ||
| case HiveThriftTransport::kBuffered: | ||
| transport = std::make_shared<apache::thrift::transport::TBufferedTransport>(socket); | ||
| break; | ||
| case HiveThriftTransport::kFramed: | ||
| transport = std::make_shared<apache::thrift::transport::TFramedTransport>(socket); | ||
| break; | ||
| } | ||
|
|
||
| auto protocol = std::make_shared<apache::thrift::protocol::TBinaryProtocol>(transport); | ||
| auto client = | ||
| std::make_unique<Apache::Hadoop::Hive::ThriftHiveMetastoreClient>(protocol); | ||
|
|
||
| try { | ||
| transport->open(); | ||
| } catch (const apache::thrift::transport::TTransportException& e) { | ||
| return IOError("Failed to connect to HMS at {}:{} : {}", endpoint.host, endpoint.port, | ||
| e.what()); | ||
| } catch (const apache::thrift::TException& e) { | ||
| return IOError("Thrift error contacting HMS at {}:{} : {}", endpoint.host, | ||
| endpoint.port, e.what()); | ||
| } | ||
|
|
||
| auto impl = std::make_unique<HmsClient::Impl>(); | ||
| impl->socket = std::move(socket); | ||
| impl->transport = std::move(transport); | ||
| impl->protocol = std::move(protocol); | ||
| impl->client = std::move(client); | ||
| return std::unique_ptr<HmsClient>(new HmsClient(std::move(impl))); | ||
| } | ||
|
|
||
| } // namespace iceberg::hive | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * 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 <memory> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <vector> | ||
|
|
||
| #include "iceberg/catalog/hive/hive_catalog_properties.h" | ||
| #include "iceberg/catalog/hive/iceberg_hive_export.h" | ||
| #include "iceberg/result.h" | ||
|
|
||
| /// \file iceberg/catalog/hive/hms_client.h | ||
| /// \brief Thin wrapper around the generated Hive Metastore Thrift client. | ||
| /// | ||
| /// Thrift types are kept out of the public interface via a pImpl. | ||
|
|
||
| namespace iceberg::hive { | ||
|
|
||
| /// \brief Hive's well-known metastore port, used when an HMS URI omits one. | ||
| inline constexpr int kDefaultHmsPort = 9083; | ||
|
|
||
| /// \brief A single host:port pair parsed from an HMS URI. | ||
| /// | ||
| /// HMS URIs commonly take one of the forms: | ||
| /// * `thrift://host:port` (Java HiveCatalog convention) | ||
| /// * `host:port` (iceberg-rust HmsCatalog convention) | ||
| /// * comma-separated list of either form for HA failover | ||
| /// | ||
| /// `port` defaults to 9083 (Hive's well-known metastore port) when the | ||
| /// caller omits an explicit port. | ||
| struct ICEBERG_HIVE_EXPORT HmsEndpoint { | ||
| std::string host; | ||
| int port = kDefaultHmsPort; | ||
| }; | ||
|
|
||
| /// \brief Parse an HMS URI string into one or more endpoints. | ||
| /// | ||
| /// Each comma-separated segment is treated as an independent endpoint; | ||
| /// surrounding whitespace and an optional `thrift://` scheme prefix are | ||
| /// stripped. Returns an InvalidArgument error if any segment fails to | ||
| /// produce a non-empty host and a port within (0, 65535]. | ||
| ICEBERG_HIVE_EXPORT Result<std::vector<HmsEndpoint>> ParseHmsUris(std::string_view uri); | ||
|
|
||
| /// \brief A live connection to a Hive Metastore over Thrift. | ||
| /// | ||
| /// Construction goes through `Connect`, which parses the URI, selects the | ||
| /// transport and opens it so that configuration errors surface as | ||
| /// `iceberg::Error` rather than C++ exceptions. | ||
| class ICEBERG_HIVE_EXPORT HmsClient { | ||
| public: | ||
| ~HmsClient(); | ||
|
|
||
| HmsClient(const HmsClient&) = delete; | ||
| HmsClient& operator=(const HmsClient&) = delete; | ||
| HmsClient(HmsClient&&) = delete; | ||
| HmsClient& operator=(HmsClient&&) = delete; | ||
|
|
||
| /// \brief Connect to the Hive Metastore described by `config`. | ||
| /// | ||
| /// Exactly one endpoint is supported: a multi-endpoint `config.Uri()` | ||
| /// returns InvalidArgument until HA failover is implemented in a future | ||
| /// commit. | ||
| static Result<std::unique_ptr<HmsClient>> Connect(const HiveCatalogProperties& config); | ||
|
|
||
| private: | ||
| class Impl; | ||
| std::unique_ptr<Impl> impl_; | ||
|
|
||
| explicit HmsClient(std::unique_ptr<Impl> impl); | ||
| }; | ||
|
|
||
| } // namespace iceberg::hive |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.