-
Notifications
You must be signed in to change notification settings - Fork 623
Feat: getNextAliveNode() to failover to alternate endpoints on retryable failures. #2880
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
kishansinghifs1
wants to merge
4
commits into
ClickHouse:main
Choose a base branch
from
kishansinghifs1:feature/getNextAliveNode
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
4 commits
Select commit
Hold shift + click to select a range
58d96c7
feat: implement client-side failover logic with endpoint health track…
kishansinghifs1 881ecfb
feat: add endpoint failover support with automatic retries and quaran…
kishansinghifs1 f4a17cd
fix: ensure HTTP 503 responses are returned for failover processing i…
kishansinghifs1 790cdeb
fix: ensure stream is reset on 503 retry in insert loop
kishansinghifs1 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
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
64 changes: 64 additions & 0 deletions
64
client-v2/src/main/java/com/clickhouse/client/api/transport/ClientNodeSelector.java
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,64 @@ | ||
| package com.clickhouse.client.api.transport; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * So we will look up for the first-alive node and then assign that node to | ||
| * client to talk. | ||
| * | ||
| * <p>Endpoints are tried in the order they were registered (index 0 is the | ||
| * "primary"). When a request fails, the failed endpoint is quarantined for | ||
| * {@link #DEFAULT_QUARANTINE_MS} milliseconds and the next alive endpoint | ||
| * is returned. Once the quarantine expires the node automatically becomes | ||
| * eligible again, so traffic returns to the primary without any explicit | ||
| * reset.</p> | ||
| * | ||
| * <p>If all endpoints are quarantined, the primary (index 0) is returned | ||
| * as a fallback to avoid a complete lockout.</p> | ||
| * | ||
| * <p>This class is thread-safe: concurrent callers may invoke | ||
| * {@link #getEndpoint()} and {@link #getNextAliveNode(Endpoint)} | ||
| * from different threads.</p> | ||
| */ | ||
| public class ClientNodeSelector { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(ClientNodeSelector.class); | ||
|
|
||
| static final long DEFAULT_QUARANTINE_MS = 30_000; | ||
|
|
||
| private final List<EndpointState> endpointStates; | ||
|
|
||
| public ClientNodeSelector(List<Endpoint> endpoints) { | ||
| List<EndpointState> states = new ArrayList<>(endpoints.size()); | ||
| for (Endpoint ep : endpoints) { | ||
| states.add(new EndpointState(ep)); | ||
| } | ||
| this.endpointStates = Collections.unmodifiableList(states); | ||
| } | ||
|
|
||
| public Endpoint getEndpoint() { | ||
| for (EndpointState state : endpointStates) { | ||
| if (state.isAlive()) { | ||
| return state.getEndpoint(); | ||
| } | ||
| } | ||
| LOG.warn("All endpoints are non-responsive, falling back to primary endpoint"); | ||
| return endpointStates.get(0).getEndpoint(); | ||
| } | ||
|
|
||
| public Endpoint getNextAliveNode(Endpoint failedEndpoint) { | ||
| for (EndpointState state : endpointStates) { | ||
| if (state.getEndpoint().equals(failedEndpoint)) { | ||
| state.markFailed(DEFAULT_QUARANTINE_MS); | ||
| LOG.warn("Endpoint {} quarantined for {} ms", failedEndpoint.getHost(), DEFAULT_QUARANTINE_MS); | ||
| break; | ||
| } | ||
| } | ||
| return getEndpoint(); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
client-v2/src/main/java/com/clickhouse/client/api/transport/EndpointState.java
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,30 @@ | ||
| package com.clickhouse.client.api.transport; | ||
|
|
||
| /** | ||
| *{@link Endpoint} is wrapped to track for failover. | ||
| * When a node fails, it can be quarantined for a fixed duration and after | ||
| * it is expired, it will be considered as alive again. | ||
| */ | ||
| class EndpointState { | ||
|
|
||
| private final Endpoint endpoint; | ||
|
|
||
| private volatile long failedUntil; | ||
|
|
||
| EndpointState(Endpoint endpoint) { | ||
| this.endpoint = endpoint; | ||
| this.failedUntil = 0; | ||
| } | ||
|
|
||
| Endpoint getEndpoint() { | ||
| return endpoint; | ||
| } | ||
|
|
||
| void markFailed(long quarantineMs) { | ||
| this.failedUntil = System.currentTimeMillis() + quarantineMs; | ||
| } | ||
|
|
||
| boolean isAlive() { | ||
| return System.currentTimeMillis() >= failedUntil; | ||
| } | ||
| } |
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.