Skip to content

Add replica_host configuration for DatabaseReplicated - #90117

Open
xiaohuanlin wants to merge 47 commits into
ClickHouse:masterfrom
xiaohuanlin:implement-replica-host-config
Open

xiaohuanlin wants to merge 47 commits into
ClickHouse:masterfrom
xiaohuanlin:implement-replica-host-config

Conversation

@xiaohuanlin

@xiaohuanlin xiaohuanlin commented Nov 15, 2025 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Add replica_host configuration parameter for DatabaseReplicated to specify advertised hostname for replica communication, independent from server bind address. Resolves #88378

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Details

Currently, DatabaseReplicated uses interserver_http_host or the system hostname, which may not be routable from other replicas.

This PR adds a new replica_host configuration parameter that allows administrators to explicitly specify the advertised hostname for DatabaseReplicated communication.

Resolves #88378

Changes

  1. New Configuration Parameter: replica_host

    • Optional parameter in config.xml
    • Takes precedence over interserver_http_host and system hostname
  2. Fallback Chain:

    replica_host (highest priority)
      ↓
    interserver_http_host
      ↓
    getFQDNOrHostName() (lowest priority)
    
  3. Automatic Migration:

    • When configuration changes (e.g., adding replica_host), automatically updates ZooKeeper if UUID and TCP port match
    • Prevents startup failures when only the advertised hostname changes
    • Rejects changes if UUID or port differ (different instance/misconfiguration)
  4. Implementation:

    • Added Context::getReplicaHost(), setReplicaHost(), hasReplicaHost()
    • Modified DatabaseReplicated::getHostID() to use fallback chain
    • Added parseHostID() helper for UUID and port validation
    • Server.cpp reads replica_host from config on startup

Configuration Example

    
    public.example.com

    
    0.0.0.0
    9000

Backward Compatibility

Fully backward compatible:

  • Optional parameter (no behavior change if not configured)
  • Existing interserver_http_host continues to work
  • Automatic migration prevents startup failures during config updates

Workflow [PR]
Sync PR [sync-upstream/pr/90117]

@xiaohuanlin

Copy link
Copy Markdown
Contributor Author

@tavplubix Please take a look if you get a chance

@tavplubix tavplubix self-assigned this Nov 17, 2025
@tavplubix tavplubix added the can be tested Allows running workflows for external contributors label Nov 17, 2025
@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [71a79e6]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
05621_documentation_no_docusaurus_syntax FAIL cidb
Code Review DROPPED
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_debug) DROPPED
Build (arm_asan_ubsan) DROPPED

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Nov 17, 2025
@xiaohuanlin
xiaohuanlin force-pushed the implement-replica-host-config branch 2 times, most recently from f56f354 to 3f60b22 Compare November 18, 2025 15:31
@xiaohuanlin

Copy link
Copy Markdown
Contributor Author

All integration tests for replica_host are passing across different configurations (asan, binary, tsan, arm). The feature works as expected.

Non-related Failures:

  1. Integration tests (flaky): CI tries to run init.py as a test because it's in the changed files. This is a one-time issue specific to this PR.
  2. Stateless test timeout: 03008_deduplication_mv_generates_several_blocks_replicated is a known flaky test. It tests ReplicatedMergeTree, not DatabaseReplicated.
  3. BuzzHouse: Fuzzing failures, unrelated.

Comment thread src/Databases/DatabaseReplicated.cpp Resolved
Comment thread src/Databases/DatabaseReplicated.cpp Resolved
@xiaohuanlin
xiaohuanlin force-pushed the implement-replica-host-config branch 2 times, most recently from 5121c8e to 7270f83 Compare November 23, 2025 16:26
@xiaohuanlin
xiaohuanlin requested a review from tavplubix December 8, 2025 03:25
@alexey-milovidov

Copy link
Copy Markdown
Member

The Hung check failure is fixed by #102008 and #102010, let's update the branch

Adds a new top-level config option `replica_host` that specifies the
hostname other replicas should use to reach this server. This is useful
when the FQDN returned by `getFQDNOrHostName()` or `interserver_http_host`
does not match the hostname used for inter-replica communication (e.g.
in containerized or NATed environments).

Fallback chain in `getHostID`:
1. `replica_host` (if configured)
2. `interserver_http_host` (existing behavior)

Closes ClickHouse#89915
@xiaohuanlin
xiaohuanlin force-pushed the implement-replica-host-config branch from 9daac39 to 2f0edb1 Compare April 10, 2026 02:24
@xiaohuanlin
xiaohuanlin force-pushed the implement-replica-host-config branch from a848a94 to 3c31bce Compare April 10, 2026 02:41
Comment thread programs/server/Server.cpp Resolved
Documents the new `replica_host` setting for `DatabaseReplicated`, including
its purpose, fallback chain (replica_host → interserver_io_host), and an
example XML configuration.
Cluster::Address::toString uses escapeForFileName(host) + ':' + port,
producing a literal colon as separator. The test was incorrectly using
'%3A' (URL-encoded colon) in the stale host_id, which caused
parseHostID to fail parsing the host:port segment and left uuid_in_keeper
as Nil, triggering REPLICA_ALREADY_EXISTS instead of the expected ZK update.
xiaohuanlin and others added 3 commits April 11, 2026 22:01
… documentation

Use the correct user-facing configuration key `interserver_http_host` instead
of the internal field name `interserver_io_host` in the docs for `replica_host`.
…ost-config

# Conflicts:
#	programs/server/Server.cpp
The fresh CI run after merging `master` flags `import time` as unused
(`F401`) in `tests/integration/test_replicated_database_replica_host/test.py`.
The import was never used; remove it so the `ruff` style check passes.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=90117&sha=4be55b1e39f7a598ebe15bc6ace29396e25288a9&name_0=PR&name_1=Style%20check

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread src/Databases/DatabaseReplicated.cpp Resolved
xiaohuanlin and others added 3 commits September 25, 2026 08:22
…ker`

`replica_host` replaces the interserver IO host in the host IDs that a
`Replicated` database publishes in its automatically generated cluster, but
`DDLWorker::markReplicasActive` preclaimed only the interserver IO host IDs.
So the first `ON CLUSTER` query after startup could see the `replica_host`
replica as inactive with the `*_only_active` output modes. Preclaim
`replica_host` with `tcp_port` and `tcp_port_secure` too, and add an
`ON CLUSTER` regression on a mixed `replica_host` / hostname cluster.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged the latest master (no conflicts; the changed translation units build). The red integration jobs at 75acebb all came from test_replicated_database_active_node_taken, a master test that expected CREATE DATABASE to succeed and fail later in the background. This PR's new check rejects the same case synchronously with REPLICA_ALREADY_EXISTS instead, which is still a clean exception and not a logical error. That test was removed from master in 4cbdcbc (2026-10-01), so the merge clears it. The json_key_formatting regression in Performance Comparison (arm_release, master_head, 3/6) is in JSON output formatting, a code path this PR does not touch. It should be checked again on the new run.

Comment thread src/Databases/DatabaseReplicated.cpp Resolved
alexey-milovidov and others added 2 commits October 12, 2026 03:21
# Conflicts:
#	src/Databases/DatabaseReplicated.cpp
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=90117&sha=126e3b5d644bda266f8f249c77caac53e1574fd1&name_0=PR&name_1=Stress%20test%20%28amd_debug%29 (Received signal 6 (internal) (STID: 3843-4f98): std::vector::front on an empty vector in ExpressionActions::linearizeActions, called from addConvertingActions in ReadFromRemote::addLazyPipe under the AST fuzzer). It is unrelated to this PR: the same STID also failed in #114734, #115970 and #123711 in the last 30 days. Please provide a fix in a separate PR. If the fix is already in progress, link it here.

The other failure, test_database_disk/test.py::test_copy_of_temporary_table_under_unrelated_name, is fixed on master by #125292. I merged master into this branch to pull that fix in.

@groeneai

Copy link
Copy Markdown
Collaborator

This is the abort that my open #122045 fixes, so I am not opening a separate PR.

I ran the query from fatal.log, SELECT arrayExists(x -> (x IN (SELECT 1)), [1]) FROM remote('127.0.0.{1,2}', system.one) LIMIT AFTER 256, on a debug server:

  • master 371b23a4: the server aborts with the CI stack (front() on an empty vector in ExpressionActions::linearizeActions <- addConvertingActions <- ReadFromRemote::addLazyPipe with the use_delayed_remote_source failpoint from the stress config, ExpressionStep::transformPipeline without it).
  • the Fix a server abort for a lambda in a query with LIMIT AFTER or LIMIT UNTIL #122045 CI debug build: returns 1 once per shard, the same as SELECT 1 FROM remote('127.0.0.{1,2}', system.one) LIMIT AFTER 256, with and without the failpoint, and the server stays up.

The fatal.log queries of #114734, #115970 and #123711 have the same shape: a lambda in the SELECT list, a remote or merge read, and LIMIT AFTER or LIMIT UNTIL. 05260_limit_after_until_lambda in #122045 covers it (arrayExists(x -> (x IN (SELECT 2)), [2]) FROM remote('127.0.0.{2,3}', system.one) LIMIT UNTIL dummy = 1).

#122045 is green on c9417bf3, mergeable with master, and still has no review (requested from @ KochetovNicolai and @ yariks5s on 2026-09-24).

Comment thread src/Interpreters/DDLWorker.cpp Resolved
alexey-milovidov and others added 2 commits October 21, 2026 14:30
…d_ddl.host_name` is set

With `<distributed_ddl><host_name>` configured, `DDLTask::findCurrentHostID` matched host IDs only
by that name. The cluster of a `Replicated` database lists the server under `replica_host`, so
`ON CLUSTER <replicated_db>` skipped the local task. Accept `replica_host` (in the bracketed form
for IPv6 literals, as published to the cluster) too, and add a regression test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Pushed 3a03561. It has two changes:

  • 2c4d4d3 addresses the DDLWorker review thread. With distributed_ddl.host_name set, replica_host is now also accepted as a self-name in DDLTask::findCurrentHostID. There is a regression test for it. The whole test_replicated_database_replica_host module passes locally (12 tests).
  • A merge of the latest master, with no conflicts.

@groeneai, thanks for linking #122045 for the linearizeActions abort.

The red jobs at 7b5fd79 do not touch this PR's code:

Both of those should clear on the new run, which is based on current master.

alexey-milovidov and others added 2 commits October 28, 2026 22:09
…_name` is set

With an explicit `distributed_ddl.host_name`, `DDLTask::findCurrentHostID` accepts
`replica_host` by name without resolving it, but `DDLWorker::markReplicasActive`
still required DNS resolution to a local address, so a non-resolving `replica_host`
was never marked active and `*_only_active` modes treated the running replica as
inactive. Apply the same explicit self-name rule there, and extend the explicit-host
integration test to check the `active` node ownership and use `throw_only_active`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Databases/DatabaseReplicated.cpp Resolved
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
alexey-milovidov and others added 3 commits November 5, 2026 09:43
…tion of a foreign active replica

The new test keeps a replica active on node1 and tries `CREATE DATABASE` on
node3 with the same database UUID, Keeper path, shard and replica name but a
different advertised host. It asserts `REPLICA_ALREADY_EXISTS` and that the
stored `host_id` and the `/active` owner in Keeper remain node1's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged the latest master (no conflicts) and pushed 30ac196. The changed translation units still build. The only red job at 74b9d28 was Upgrade check (amd_release). It flagged Division by zero ... While executing TTL errors from the 05073_column_ttl_recompute_materialized fixture, which has nothing to do with this PR. #127731 added that error to the upgrade-check allowlist and is now on master, so the job should pass on the next run.

Comment thread src/Databases/DatabaseReplicated.cpp Resolved
alexey-milovidov and others added 2 commits November 14, 2026 18:35
The automatically generated cluster of a `Replicated` database lists a bare
IPv6 advertised host in the bracketed form, so distributed DDL is queued for
`[<ipv6>]:<port>`. `DDLWorker::markReplicasActive` normalized only
`replica_host`, so with a bare IPv6 `interserver_http_host` and no
`replica_host`, the first `ON CLUSTER` query could see the running replica as
offline. Claim the bracketed form of the advertised host in both cases, and
share the normalization through `HostID::bracketIPv6Host`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Interpreters/DDLTask.cpp Outdated
if (config_host_name)
{
if (config_host_name != host.host_name)
if (config_host_name != host.host_name && advertised_replica_host != host.host_name)

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.

[Major] Normalize the explicit DDL self-name as well as the advertised name

The IPv6 normalization in fetchClusterTopology also applies when replica_host is unset, but this comparison still uses the unnormalized distributed_ddl.host_name. For example, configure both interserver_http_host and distributed_ddl.host_name as ::1, with no replica_host. The generated database cluster now publishes [::1]:9000, and markReplicasActive correctly claims that bracketed ID. However, findCurrentHostID compares "::1" and the empty advertised_replica_host against "[::1]", rejects the entry, and never reaches DNS-based matching. The worker skips the task, so CREATE TABLE default.t ON CLUSTER <db> ... times out without creating the local table despite the replica being marked active.

Please apply a consistent IPv6 representation to both sides of the name-based self comparisons, including the explicit configured name. Extend the fallback IPv6 regression with the matching bare IPv6 distributed_ddl.host_name and an actual ON CLUSTER operation; checking /active and system.clusters alone misses this dispatch failure.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🕵 Fixed in 71a79e6. DDLTask::findCurrentHostID now brings distributed_ddl.host_name, replica_host and the entry host to the bracketed form before comparing them, so host_name = ::1 matches the entry host [::1]. The by-name rule in DDLWorker::markReplicasActive uses the same form.

test_ipv6_interserver_host_marked_active now also runs CREATE TABLE ... ON CLUSTER <db> with throw_only_active and checks that the table exists, once without distributed_ddl.host_name and once with it set to ::1.

I could not run the test locally: the changed translation units compile, but the final link of clickhouse was repeatedly terminated by the memory watchdog on the build machine. So the test is verified only by CI on this commit.

alexey-milovidov and others added 2 commits November 23, 2026 17:11
# Conflicts:
#	src/Interpreters/DDLTask.h
…CurrentHostID`

The cluster of a `Replicated` database lists a bare IPv6 literal in brackets, also when
`replica_host` is unset and the interserver IO host is advertised. With an explicit
`distributed_ddl.host_name`, `DDLTask::findCurrentHostID` compares names instead of resolving
them, and it compared the configured name as is: `host_name` = `::1` did not match the entry
host `[::1]`, so the worker skipped the task and `ON CLUSTER` on the cluster of the database
timed out, although the replica was marked active.

Bring `host_name`, `replica_host` and the entry host to the bracketed form before comparing,
and do the same for the by-name rule in `DDLWorker::markReplicasActive`.

Extend `test_ipv6_interserver_host_marked_active` with an actual `ON CLUSTER` query, run both
without and with a bare IPv6 `distributed_ddl.host_name`.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Pushed 71a79e6. It has two changes:

  • A merge of the latest master. The one conflict was in src/Interpreters/DDLTask.h: master added [[nodiscard]] to HostID::toString next to the HostID::bracketIPv6Host declaration from this PR, and both are kept.
  • A fix for the open review thread: DDLTask::findCurrentHostID compares distributed_ddl.host_name, replica_host and the entry host in the bracketed IPv6 form. test_ipv6_interserver_host_marked_active now runs an actual ON CLUSTER query with and without a bare IPv6 distributed_ddl.host_name.

The changed translation units compile, but I could not link clickhouse on the build machine (the memory watchdog terminated the linker), so the extended test has not been run locally and needs the CI run on this commit.

The only red job at 45374fc was Build profile diff (arm_release), which failed with LogClusterNotReady: the endpoint never became ready. That is the CI logs cluster not answering, not this change; #126200 (open) deals with that readiness probe.

This branch has not been deployed

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

Labels

can be tested Allows running workflows for external contributors comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. external Author is not a member of the ClickHouse organization manual approve Manual approve required to run CI pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants