-
Notifications
You must be signed in to change notification settings - Fork 208
Preserve current endpoint across failed reconnects #558
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -179,15 +179,6 @@ std::unique_ptr<SocketFactory> GetSocketFactory(const ClientOptions& opts) { | |
| return std::make_unique<NonSecureSocketFactory>(); | ||
| } | ||
|
|
||
| std::unique_ptr<EndpointsIteratorBase> GetEndpointsIterator(const ClientOptions& opts) { | ||
| if (opts.endpoints.empty()) | ||
| { | ||
| throw ValidationError("The list of endpoints is empty"); | ||
| } | ||
|
|
||
| return std::make_unique<RoundRobinEndpointsIterator>(opts.endpoints); | ||
| } | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| class Client::Impl { | ||
|
|
@@ -264,18 +255,11 @@ class Client::Impl { | |
|
|
||
| void InitializeStreams(std::unique_ptr<SocketBase>&& socket); | ||
|
|
||
| inline size_t GetConnectionAttempts() const | ||
| { | ||
| return options_.endpoints.size() * options_.send_retries; | ||
| } | ||
|
|
||
| private: | ||
| /// In case of network errors tries to reconnect to server and | ||
| /// call fuc several times. | ||
| void RetryGuard(std::function<void()> func); | ||
|
|
||
| void RetryConnectToTheEndpoint(std::function<void()>& func); | ||
|
|
||
| private: | ||
| enum class State : uint8_t { | ||
| Idle = 0, | ||
|
|
@@ -317,6 +301,8 @@ class Client::Impl { | |
| std::unique_ptr<SocketBase> socket_; | ||
| std::unique_ptr<EndpointsIteratorBase> endpoints_iterator; | ||
|
|
||
| // current_endpoint_ points to the last successfully connected endpoint, and always | ||
| // holds a value. The variable remains wrapped as optional for backwards compatibility. | ||
| std::optional<Endpoint> current_endpoint_; | ||
|
|
||
| ServerInfo server_info_; | ||
|
|
@@ -342,7 +328,8 @@ Client::Impl::Impl(const ClientOptions& opts, | |
| : options_(modifyClientOptions(opts)) | ||
| , events_(nullptr) | ||
| , socket_factory_(std::move(socket_factory)) | ||
| , endpoints_iterator(GetEndpointsIterator(options_)) | ||
| , endpoints_iterator(std::make_unique<RoundRobinEndpointsIterator>(options_.endpoints)) | ||
| , current_endpoint_(endpoints_iterator->Next()) | ||
| { | ||
| CreateConnection(); | ||
|
|
||
|
|
@@ -619,36 +606,39 @@ void Client::Impl::ResetConnection() { | |
| } | ||
|
|
||
| void Client::Impl::ResetConnectionEndpoint() { | ||
| current_endpoint_.reset(); | ||
| for (size_t i = 0; i < options_.endpoints.size();) | ||
| std::optional<Endpoint> last_endpoint = current_endpoint_; | ||
| for (size_t i = 1; ; ++i) | ||
| { | ||
| try | ||
| { | ||
| current_endpoint_ = endpoints_iterator->Next(); | ||
| ResetConnection(); | ||
| return; | ||
| } catch (const std::system_error&) { | ||
| if (++i == options_.endpoints.size()) | ||
| current_endpoint_ = endpoints_iterator->Next(); | ||
| if (i >= options_.endpoints.size()) | ||
| { | ||
| current_endpoint_.reset(); | ||
| current_endpoint_ = last_endpoint; | ||
| throw; | ||
| } | ||
| } catch (...) { | ||
| current_endpoint_ = last_endpoint; | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void Client::Impl::CreateConnection() { | ||
| // make sure to try to connect to each endpoint at least once even if `options_.send_retries` is 0 | ||
| const size_t max_attempts = (options_.send_retries ? options_.send_retries : 1); | ||
| for (size_t i = 0; i < max_attempts;) | ||
| for (size_t i = 1; ; ++i) | ||
| { | ||
| try | ||
| { | ||
| // Try to connect to each endpoint before throwing exception. | ||
| ResetConnectionEndpoint(); | ||
| return; | ||
| } catch (const std::system_error&) { | ||
| if (++i >= max_attempts) | ||
| if (i >= max_attempts) | ||
| { | ||
| throw; | ||
| } | ||
|
|
@@ -1232,32 +1222,36 @@ bool Client::Impl::ReceiveHello() { | |
|
|
||
| void Client::Impl::RetryGuard(std::function<void()> func) { | ||
|
|
||
| if (current_endpoint_) | ||
| { | ||
| for (unsigned int i = 0; ; ++i) { | ||
| try { | ||
| func(); | ||
| return; | ||
| } catch (const std::system_error&) { | ||
| bool ok = true; | ||
| for (unsigned int i = 1; ; ++i) { | ||
| try { | ||
| func(); | ||
| return; | ||
| } catch (const std::system_error&) { | ||
| // if send_retries == 0 do not try anymore, throw right away | ||
| if (options_.send_retries == 0) { | ||
| throw; | ||
| } | ||
|
|
||
| try { | ||
| socket_factory_->sleepFor(options_.retry_timeout); | ||
| ResetConnection(); | ||
| } catch (...) { | ||
| ok = false; | ||
| } | ||
| // If `send_retries` attempts failed, try other endpoints | ||
| if (i >= options_.send_retries) { | ||
| break; | ||
| } | ||
|
|
||
| if (!ok && i == options_.send_retries) { | ||
| break; | ||
| } | ||
| // otherwise sleep and try again | ||
| try { | ||
| socket_factory_->sleepFor(options_.retry_timeout); | ||
| ResetConnection(); | ||
| } catch (const std::system_error&) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is consistent with other |
||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| // Connections with current_endpoint_ are broken. | ||
| // Trying to establish with the another one from the list. | ||
| size_t connection_attempts_count = GetConnectionAttempts(); | ||
| for (size_t i = 0; i < connection_attempts_count;) | ||
| // Trying to establish with another one from the list. | ||
| size_t connection_attempts_count = options_.endpoints.size() * options_.send_retries; | ||
| std::optional<Endpoint> last_endpoint = current_endpoint_; | ||
| for (size_t i = 1; ; ++i) | ||
| { | ||
| try | ||
| { | ||
|
|
@@ -1267,11 +1261,14 @@ void Client::Impl::RetryGuard(std::function<void()> func) { | |
| func(); | ||
| return; | ||
| } catch (const std::system_error&) { | ||
| if (++i == connection_attempts_count) | ||
| if (i >= connection_attempts_count) | ||
| { | ||
| current_endpoint_.reset(); | ||
| current_endpoint_ = last_endpoint; | ||
| throw; | ||
| } | ||
| } catch (...) { | ||
| current_endpoint_ = last_endpoint; | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
|
|
||
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,62 @@ | ||
| #pragma once | ||
|
|
||
| #include "clickhouse/base/socket.h" | ||
|
|
||
| #include <chrono> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <system_error> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| namespace clickhouse { | ||
|
|
||
| /** Records requested endpoints and optionally fails one matching connection attempt. | ||
| * | ||
| * Successful attempts are redirected to actual_endpoint, allowing tests to exercise | ||
| * failover between distinct logical endpoints using a single reachable server. Setting | ||
| * fail_endpoint makes the next matching attempt throw connection_refused and then | ||
| * clears the value. The wrapped factory must outlive the adapter. | ||
| */ | ||
| struct FailOnceSocketFactoryAdapter : public SocketFactory { | ||
| SocketFactory & socket_factory; | ||
| Endpoint actual_endpoint; | ||
| std::vector<Endpoint> connect_requests{}; | ||
| std::optional<Endpoint> fail_endpoint{}; | ||
|
|
||
| FailOnceSocketFactoryAdapter(SocketFactory & socket_factory, | ||
| Endpoint actual_endpoint) | ||
| : socket_factory(socket_factory) | ||
| , actual_endpoint(std::move(actual_endpoint)) | ||
| {} | ||
|
|
||
| std::unique_ptr<SocketBase> connect(const ClientOptions& opts, | ||
| const Endpoint& endpoint) override { | ||
| connect_requests.push_back(endpoint); | ||
|
|
||
| if (fail_endpoint && fail_endpoint.value() == endpoint) { | ||
| fail_endpoint.reset(); | ||
| throw std::system_error(std::make_error_code(std::errc::connection_refused)); | ||
| } | ||
|
|
||
| return socket_factory.connect(opts, actual_endpoint); | ||
| } | ||
|
|
||
| void SetFailEndpoint(std::optional<Endpoint> endpoint) { | ||
| fail_endpoint = std::move(endpoint); | ||
| } | ||
|
|
||
| const std::vector<Endpoint> & ConnectRequests() const { | ||
| return connect_requests; | ||
| } | ||
|
|
||
| void ClearConnectRequests() { | ||
| connect_requests.clear(); | ||
| } | ||
|
|
||
| void sleepFor(const std::chrono::milliseconds& duration) override { | ||
| socket_factory.sleepFor(duration); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace clickhouse |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That preserves the existing behavior