Skip to content

Preserve current endpoint across failed reconnects - #558

Merged
slabko merged 2 commits into
masterfrom
invalidate-connection-on-exception-2
Aug 29, 2026
Merged

Preserve current endpoint across failed reconnects#558
slabko merged 2 commits into
masterfrom
invalidate-connection-on-exception-2

Conversation

@slabko

@slabko slabko commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Background

The meaning of the optional current endpoint was confusing. Previously, it became empty only after a failed reconnection attempt, so its value did not consistently describe the client state or make it clear which endpoint would be tried next.

The current endpoint now represents the last successfully connected endpoint and always contains a value for a constructed client. It remains optional only for backward compatibility. This also defines reconnection behavior explicitly:

  • ResetConnection retries only the current endpoint and does not perform failover.
  • ResetConnectionEndpoint tries the current endpoint first, then the other configured endpoints.
  • If all failover attempts fail, the last successful endpoint remains current rather than being cleared.

Summary

  • preserve the last successfully connected endpoint across failed reconnection attempts
  • make reconnect and failover ordering consistent with the current endpoint
  • validate non-empty endpoint lists in RoundRobinEndpointsIterator
  • add coverage for reconnect and failover ordering

@slabko
slabko requested a review from mzitnik as a code owner August 28, 2026 15:59
@slabko
slabko force-pushed the invalidate-connection-on-exception-2 branch from cda1ce4 to 1918252 Compare August 28, 2026 16:16
@slabko
slabko requested a lite review from Copilot August 28, 2026 17:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR clarifies and stabilizes “current endpoint” semantics by making it represent the last successfully connected endpoint, and updates reconnect/failover logic and tests to match that definition.

Changes:

  • Preserve the last successfully connected endpoint across failed reconnect/failover attempts.
  • Make reconnect ordering consistent (retry current first, then fail over) and validate non-empty endpoint lists in the round-robin iterator.
  • Add unit test coverage for reconnect and failover ordering via a socket-factory adapter.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
ut/test_socket_factory_adapters.h Adds a socket factory adapter to record attempted endpoints and inject one-time failures for testing.
ut/client_ut.cpp Replaces/extends reconnect tests to assert “retry current before failover” ordering and wraparound behavior.
ut/BUILD.bazel Includes the new test helper header in the Bazel test target.
clickhouse/client.h Updates API documentation to reflect “current endpoint = last successful endpoint” semantics.
clickhouse/client.cpp Implements new endpoint preservation and ordering in reconnect/failover paths, and adjusts retry behavior.
clickhouse/base/endpoints_iterator.cpp Adds validation to reject empty endpoint lists at iterator construction.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread clickhouse/client.cpp
Comment on lines 616 to +620
} 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;

Copy link
Copy Markdown
Contributor Author

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

Comment thread clickhouse/client.cpp Outdated
Comment thread clickhouse/base/endpoints_iterator.cpp Outdated
Comment thread clickhouse/client.cpp
try {
socket_factory_->sleepFor(options_.retry_timeout);
ResetConnection();
} catch (const std::system_error&) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is consistent with other Reconnect* functions

@slabko
slabko force-pushed the invalidate-connection-on-exception-2 branch from 7e1a947 to 1918252 Compare August 28, 2026 17:25
Keep the current endpoint set to the last successfully connected endpoint. ResetConnection retries
only that endpoint, while ResetConnectionEndpoint tries it first before failing over to the
remaining endpoints. Restore the previous endpoint if all reconnection attempts fail.

Validate non-empty endpoint lists in RoundRobinEndpointsIterator and add coverage for reconnect and
failover ordering.
@slabko
slabko force-pushed the invalidate-connection-on-exception-2 branch from 1918252 to 4e6c88c Compare August 28, 2026 17:26
@slabko
slabko merged commit 8a081ee into master Aug 29, 2026
120 of 121 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants