ON CLUSTER query is written to a queue in ClickHouse Keeper together with the list of hosts that the cluster definition contains at that moment. Each of these hosts executes the query, and the server that received it waits for all of them. After a replica of a self-managed cluster is lost and replaced, this causes two different failures:
- While the lost host is still in the cluster definition, every
ON CLUSTERquery waits for it and ends withTIMEOUT_EXCEEDED. - The replacement host starts without databases and tables, and the queue does not create them for it.
ON CLUSTERqueries that touch existing tables then fail on this host.
my_cluster with one shard, the lost host host-3 with the replica name replica_3, and its replacement host-4.
The query times out
distributed_ddl_task_timeout seconds (180 by default) for a host that will never answer.
Remove the lost host from remote_servers in the configuration of every server, or replace it with the new host. Cluster definitions are reloaded without a restart, and all servers must have the same one. Check the result on each server:
ON CLUSTER queries before the configuration is fixed, change distributed_ddl_output_mode for the session. With null_status_on_timeout the query returns a NULL status for the unfinished host instead of an exception, and with throw_only_active it does not wait for hosts that are offline.
The query fails on the replacement host
Once the new host is part of the cluster definition, the sameALTER TABLE ... ON CLUSTER query reaches it and fails there, because the host has no such database or table yet:
ON CLUSTER queries does not bring a new host up to date, because every entry is addressed to the hosts that were in the cluster definition when the query was issued:
- A replacement with a new host name is not listed in the existing entries and skips them.
- A replacement that reuses the host name and port of the lost host executes only the entries that the lost host did not finish and that are still in the queue. Entries are removed when they are older than
task_max_lifetime(7 days by default) or when the queue holds more thanmax_tasks_in_queueentries (1000 by default), seedistributed_ddl.
ON CLUSTER. Take the definitions from a healthy replica with SHOW CREATE TABLE. For a ReplicatedMergeTree table:
- Use the same path in ClickHouse Keeper as the other replicas and a replica name that is not registered for this table. If the engine arguments contain the
{shard}and{replica}macros, define the macros in the configuration of the new host and keep the statement unchanged. - If the path contains the
{uuid}macro, the table must have the same UUID on all replicas.SHOW CREATE TABLE db.events SETTINGS show_table_uuid_in_table_create_query_if_not_nil = 1prints the definition with itsUUIDclause.
CREATE TABLE, the new replica copies the data from the other replicas by itself.
If the new host keeps the replica name of the lost one, the CREATE TABLE fails as long as the lost replica is registered:
Drop the lost replica
The lost replica is still registered in ClickHouse Keeper for every replicated table. On a healthy replica,system.replicas shows it as inactive:
replica_is_active map the lost replica has the value 0. Remove it with SYSTEM DROP REPLICA, run on a healthy replica:
SYSTEM DROP REPLICA 'replica_3' FROM DATABASE db does the same for all replicated tables of a database, and without a FROM clause for all replicated tables of the server. The statement works on an attached table, removes only an inactive replica, and never touches local data.
Entries left in the queue
The queries that were issued while the host was down stay in the queue as unfinished for that host.system.distributed_ddl_queue lists them:
Inactive. These rows need no cleanup: the other hosts do not wait for them, and the entries are removed automatically as described above.
If a host does not get past a queue entry
A host works through the queue in order. If its DDL worker fails on an entry with an unexpected error, it writesUnexpected error (N times in a row), will try to restart main thread to the server log, waits and starts again, so the entries after that one are not executed on this host and ON CLUSTER queries time out there. The preceding DDLWorker messages (Checking task query-...) show the entry.
- Fix the cause named in the error message if you can.
- Since ClickHouse 26.2,
SYSTEM RESET DDL WORKERmakes the worker of a host forget its state and read the queue again. - As the last resort, if an entry can never be processed, remove it from ClickHouse Keeper with
clickhouse-keeper-client: