Skip to content

[client-v2] getNextAliveNode() always returns endpoints.get(0) — retries never fail over to alternate endpoints #2855

Description

@claude

Description

com.clickhouse.client.api.Client#getNextAliveNode() (client-v2) is hard-coded to return the first registered endpoint and never rotates:

// client-v2/src/main/java/com/clickhouse/client/api/Client.java:2200
private Endpoint getNextAliveNode() {
    return endpoints.get(0);
}

This method is the only mechanism the client-v2 INSERT / write / query code paths use to (re-)select an endpoint on retry. Every retry call site (in Client.java around lines 1329, 1359, 1376, 1551, 1567, 1583, 1680, 1697, 1725) reassigns selectedEndpoint = getNextAliveNode(), but since the method ignores any rotation/liveness state, retries always target the same (first) endpoint. If the first endpoint is unreachable, no registered alternative is ever attempted, and the client surfaces a connection failure even though other configured endpoints could have served the request.

The naming (getNextAliveNode) suggests the surrounding retry loop was authored expecting rotation/liveness logic that was never implemented, so the failover behavior is silently absent rather than explicitly disabled. The feature request to add real failover (#1838) was closed as not-planned (stale) on 2026-02-10, so this stub remains in the shipping code.

This is the Java-side analogue of clickhouse-go#136 (the Go driver failing to fall back to alt_hosts when the first host is down).

ClickHouse server version

Code analysis only; not verified against a running server. (Server in the investigation environment was 26.4.2.10, but reproduction is purely client-side and does not depend on server version.)

Reproduction

Minimal client-v2 snippet that exercises the broken path:

import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.query.QueryResponse;

public class FailoverRepro {
    public static void main(String[] args) throws Exception {
        // First endpoint is dead (no listener on :1); second is healthy.
        try (Client client = new Client.Builder()
                .addEndpoint("http://127.0.0.1:1")
                .addEndpoint("http://localhost:8123")
                .setUsername("default")
                .setPassword("")
                .setRetryOnFailures(com.clickhouse.client.api.ClientFaultCause.ConnectTimeout,
                                   com.clickhouse.client.api.ClientFaultCause.NoHttpResponse,
                                   com.clickhouse.client.api.ClientFaultCause.ServerRetryable)
                .retryOnFailures(5)  // any N >= 1
                .build()) {

            try (QueryResponse r = client.query("SELECT 1").get()) {
                // Expected: succeed via the healthy second endpoint after the first fails.
                // Actual:   all N+1 attempts hit 127.0.0.1:1 and the query throws a
                //           connection-refused / connect-timeout exception.
            }
        }
    }
}

Expected: the retry loop tries localhost:8123 after 127.0.0.1:1 fails to connect, and the query returns successfully.

Actual: every retry repeatedly targets 127.0.0.1:1 because getNextAliveNode() returns endpoints.get(0) unconditionally; the healthy alternate is never tried.

Suggested fix

Client.java:2200-2202. At minimum, getNextAliveNode() should rotate across endpoints — e.g., an AtomicInteger round-robin index incremented on each call, with the result taken modulo endpoints.size(). A more complete fix would also track endpoints recently marked as failed within the current retry loop and skip them, and only advance the index on retries triggered by connection-level failures or retryable 5xx responses so that successful requests keep affinity to a single endpoint.

Link

Related upstream report (Go client analogue): ClickHouse/clickhouse-go#136
Closed feature request for the same functionality in client-v2: #1838

Activity

  1. added
    client
    connectivity
    on May 15, 2026
  2. chernser commented on May 15, 2026

    @chernser
    Contributor

    This should be implemented as feature. This is not a bug.

  3. kishansinghifs1 commented on May 23, 2026

    @kishansinghifs1
    Contributor

    Hii @chernser can I work on this?

  4. chernser commented on May 24, 2026

    @chernser
    Contributor

    Good day, @kishansinghifs1 !

    yes, sure!
    please provide your implementation proposal and we will review it.
    Please also review this PR - new contribution guide #2859
    I believe this is useful to read. Thanks!

  5. kishansinghifs1 commented on May 24, 2026

    @kishansinghifs1
    Contributor

    Hiii @chernser, thanks for the opportunity. Here is the doc containing the implementation proposal along with the queries that need to be answered:

    Issue:#2855

  6. chernser commented on May 26, 2026

    @chernser
    Contributor

    @kishansinghifs1

    Thank you for the IP. Please add me to commenters so I can comment :-)

    Few followup questions:

    • IP suggests to have global host index so all requests will migrate to alive host. How to reset counter back? For example, I have primary and backup server.
    • What exceptions will switch to another host?
    • What would be the change to support "round robin" strategy in the future?
    • What would be the change to support switching back to failed node by timeout?

    Thanks!

  7. kishansinghifs1 commented on May 27, 2026

    @kishansinghifs1
    Contributor

    Hi @chernser, I have added you to the commenters.

    Answers to your questions:

    1. In a Primary-Backup setup, the client should always try the primary server first. The backup server is only used if the primary fails. Once the primary becomes healthy again, traffic should switch back to it automatically.

    2. Switching to another host happens on retryable failures such as:

    • HTTP 503
    • connection timeout
    • socket timeout
    • connection refused
    • other retryable network exceptions handled in shouldRetry(...)
    1. For future round-robin support, we can introduce a small NodeSelector interface. Different implementations can support Primary-Backup or Round-Robin strategies.

    2. To switch back to a failed node, we can use a timeout-based health check. After a failed node's timeout expires, the client can try that node again automatically.

  8. chernser commented on Jun 1, 2026

    @chernser
    Contributor

    Good day, @kishansinghifs1 !

    Thank you for response!
    Timeout option looks good. Lets implement failover scenario according to your spec. Everything else can be handled by other components and later.

    I approve the spec and please go ahead with implementation.
    In PR description try to describe yourself what is done.
    Then we will review and move forward.

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clientconnectivityenhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions