Skip to content

Fix DatabaseReplicated to respect interserver_http_host config - #88378

Merged
alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
xiaohuanlin:fix-database-replicated-use-interserver-host
Oct 21, 2025
Merged

alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
xiaohuanlin:fix-database-replicated-use-interserver-host

Conversation

@xiaohuanlin

Copy link
Copy Markdown
Contributor

DatabaseReplicated ignored interserver_http_host when registering replicas in ZooKeeper, always using hostname instead. This prevented node replacements with same IP but different hostname from connecting to existing databases.

Now uses getInterserverIOAddress() like StorageReplicatedMergeTree does.

Fixes #88361

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed DatabaseReplicated to respect interserver_http_host configuration.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 11, 2025
@clickhouse-gh

clickhouse-gh Bot commented Oct 11, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [55614ec]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 11, 2025
UInt16 port = secure ? global_context->getTCPPortSecure().value_or(DBMS_DEFAULT_SECURE_PORT) : global_context->getTCPPort();
return Cluster::Address::toString(getFQDNOrHostName(), port) + ':' + toString(db_uuid);
return Cluster::Address::toString(host_port.first, port) + ':' + toString(db_uuid);

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.

Is it correct that we don't use interserver_http_port here?

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.

Yes, using TCP port instead of interserver_http_port is correct.
Reason: DatabaseReplicated uses Cluster for distributed DDL execution via TCP, not HTTP replication. When Cluster::Address parses the host_id, it extracts the embedded port from the "host:port:uuid" format.
This PR:

  • Uses interserver_http_host for the network interface
  • Uses TCP port for query communication (correct behavior)

Compare with StorageReplicatedMergeTree.cpp:9330 which explicitly separates replication_port (HTTP) from queries_port (TCP).

@alexey-milovidov alexey-milovidov self-assigned this Oct 12, 2025
@alexey-milovidov

Copy link
Copy Markdown
Member

@xiaohuanlin, please check why the server didn't start in the tests.

@xiaohuanlin
xiaohuanlin force-pushed the fix-database-replicated-use-interserver-host branch 3 times, most recently from 31381ab to 91e6104 Compare October 20, 2025 11:43
DatabaseReplicated ignored interserver_http_host when registering replicas
in ZooKeeper, always using hostname instead. This prevented node replacements
with same IP but different hostname from connecting to existing databases.

Now uses getInterserverIOAddress() like StorageReplicatedMergeTree does.

Fixes ClickHouse#88361
The test now dynamically configures each node's interserver_http_host
with its actual Docker container IP address at runtime, before creating
the replicated databases. This ensures nodes register with IP addresses
instead of hostnames in ZooKeeper.

Previously, removing the interserver_http_host config caused the system
to fall back to getFQDNOrHostName(), which returned hostnames.
The test needs to restart ClickHouse instances to apply interserver_http_host
configuration changes. This requires stay_alive=True parameter in the instance
configuration to allow restart operations during testing.
@xiaohuanlin
xiaohuanlin force-pushed the fix-database-replicated-use-interserver-host branch from a8d26f7 to 55614ec Compare October 20, 2025 23:31
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 21, 2025
Merged via the queue into ClickHouse:master with commit 7c6333b Oct 21, 2025
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 22, 2025
@fm4v
fm4v requested a review from tavplubix October 27, 2025 12:37
@fm4v

fm4v commented Oct 27, 2025

Copy link
Copy Markdown
Member

@tavplubix hi! Could you please review this PR as well?

@tavplubix

Copy link
Copy Markdown
Member

@xiaohuanlin @alexey-milovidov why interserver_http_host? It's not related to http, and also there are interserver_https_host (and if someone uses https only - they will have the same problem until they figure out that they need to configure http as well, although Replicated databases and Distributed tables don't use http(s) at all) and listen_host/interserver_listen_host - listen_host looks more suitable

Also, if someone already has an IP in interserver_http_host and a Replicated database, the database will fail to start up after upgrading (because it will switch to using IPs, and it will not match the hostname written in zookeeper):

2025.10.28 15:52:39.002476 [ 36888 ] {} <Error> DDLWorker(rdb): Error on initialization of rdb: Code: 253. DB::Exception: Replica 1 of shard 1 of replicated database at /test/rdb already exists. Replica host ID: 'localhost%2Elocaldomain:9000:3c9d2f6f-617d-4b74-89ee-a089df7b906b', current host ID: '127%2E0%2E0%2E1:9000:3c9d2f6f-617d-4b74-89ee-a089df7b906b'. (REPLICA_ALREADY_EXISTS), Stack trace (when copying this message, always include the lines below):

0. /home/tavplubix/ch/ClickHouse/contrib/llvm-project/libcxx/include/__exception/exception.h:113: Poco::Exception::Exception(String const&, int) @ 0x0000000023d61bb2
1. /home/tavplubix/ch/ClickHouse/src/Common/Exception.cpp:129: DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000001390a5ec
2. /home/tavplubix/ch/ClickHouse/src/Common/Exception.h:123: DB::Exception::Exception(String&&, int, String, bool) @ 0x000000000b7dc59c
3. /home/tavplubix/ch/ClickHouse/src/Common/Exception.h:58: DB::Exception::Exception(PreformattedMessage&&, int) @ 0x000000000b7dbed3
4. /home/tavplubix/ch/ClickHouse/src/Common/Exception.h:141: DB::Exception::Exception<String&, String&, String&, String&, String&>(int, FormatStringHelperImpl<std::type_identity<String&>::type, std::type_identity<String&>::type, std::type_identity<String&>::type, std::type_identity<String&>::type, std::type_identity<String&>::type>, String&, String&, String&, String&, String&) @ 0x0000000018bb4ec7
5. /home/tavplubix/ch/ClickHouse/src/Databases/DatabaseReplicated.cpp:573: DB::DatabaseReplicated::tryConnectToZooKeeperAndInitDatabase(DB::LoadingStrictnessLevel) @ 0x0000000018b853c9
6. /home/tavplubix/ch/ClickHouse/src/Databases/DatabaseReplicatedWorker.cpp:88: DB::DatabaseReplicatedDDLWorker::initializeMainThread() @ 0x0000000018bc0aa8
7. /home/tavplubix/ch/ClickHouse/src/Interpreters/DDLWorker.cpp:1212: DB::DDLWorker::runMainThread() @ 0x00000000199d12e0
8. /home/tavplubix/ch/ClickHouse/contrib/llvm-project/libcxx/include/__type_traits/invoke.h:117: ThreadFromGlobalPoolImpl<true, true>::ThreadFromGlobalPoolImpl<void (DB::DDLWorker::*)(), DB::DDLWorker*>(void (DB::DDLWorker::*&&)(), DB::DDLWorker*&&)::'lambda'()::operator()() @ 0x00000000199f4e79
9. /home/tavplubix/ch/ClickHouse/contrib/llvm-project/libcxx/include/__functional/function.h:716: ? @ 0x0000000013a5f063
10. /home/tavplubix/ch/ClickHouse/contrib/llvm-project/libcxx/include/__type_traits/invoke.h:117: void* std::__thread_proxy[abi:se190107]<std::tuple<std::unique_ptr<std::__thread_struct, std::default_delete<std::__thread_struct>>, void (ThreadPoolImpl<std::thread>::ThreadFromThreadPool::*)(), ThreadPoolImpl<std::thread>::ThreadFromThreadPool*>>(void*) @ 0x0000000013a66014
11. ? @ 0x00000000000969cb
12. ? @ 0x000000000011aa0c
 (version 25.11.1.1)

@xiaohuanlin

Copy link
Copy Markdown
Contributor Author

@tavplubix

Regarding the use of interserver_http_host

I understand your concern about the naming semantics. However, interserver_http_host is actually the most appropriate choice here, and here's why: According to ClickHouse's official documentation, interserver_http_host is defined as: "The hostname that can be used by other servers to access this server."
This is exactly what DatabaseReplicated needs: an advertise address that tells other replicas how to reach this node.
Why not listen_host? listen_host has a fundamentally different purpose:

  • listen_host: Where the server listens for incoming connections (can be :: or 0.0.0.0)
  • interserver_http_host: What address the server advertises to other nodes (must be a specific reachable address)

Regarding upgrade compatibility

You're right that this is an important concern. I need to think through the upgrade path more carefully to ensure existing deployments won't face REPLICA_ALREADY_EXISTS errors. Let me work on a proper migration strategy and get back to you.

@tavplubix

Copy link
Copy Markdown
Member

@xiaohuanlin is the comment above LLM-generated?

According to ClickHouse's official documentation, interserver_http_host is defined as: "The hostname that can be used by other servers to access this server."

Yes, but this is about HTTP interserver interface, which is being used by ReplicatedMergeTree for data replication.

DatabaseReplicated needs to create a cluster for Distributed tables like https://clickhouse.com/docs/operations/server-configuration-parameters/settings#remote_servers. They don't use HTTP to reach other nodes, they use native protocol and connect to tcp_port in the same way as clickhouse-client

Also, why interserver_http_host and not interserver_https_host?

But you're right that listen_host has a different purpose. Do we need a new configuration parameter to make it less confusing?

@xiaohuanlin

Copy link
Copy Markdown
Contributor Author

1. Configuration Semantics

Solution: Introduce replica_host configuration with fallback chain: replica_host → interserver_http_host → hostname

This provides semantic clarity while maintaining backward compatibility.

2. Upgrade Compatibility

Solution: Automatic migration based on UUID + port matching.

Logic: Parse both old and new host_id. If UUID and port match → same instance → auto-update ZooKeeper. Otherwise → reject.

All scenarios:

Scenario Old host_id New host_id UUID Port Behavior
No config change hostname hostname same same Exact match
User adds replica_host hostname IP same same Auto-migrate
Network change IP1 IP2 same same Auto-migrate
Different machines host1 host2 different same Reject
Port change host:9000 host:9001 same different Reject
IPv4 → IPv6 IPv4 IPv6 same same Auto-migrate

Why safe: UUID is unique per database instance. Matching UUID + port guarantees same server.

@tavplubix Let me know if this makes sense or if you'd prefer a different approach.

@tavplubix

Copy link
Copy Markdown
Member

LGTM

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 pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for using ip addresses instead of hostname in clickhouse

5 participants