DDLWorker and DDL queue problems
Finding and troubleshooting problems in the distributed_ddl_queue
DDLWorker is a subprocess (thread) of clickhouse-server that executes ON CLUSTER tasks at the node.
When you execute a DDL query with ON CLUSTER mycluster section, the query executor at the current node reads the cluster mycluster definition (remote_servers / system.clusters) and places tasks into Zookeeper znode task_queue/ddl/... for members of the cluster mycluster.
DDLWorker at all ClickHouse® nodes constantly check this task_queue for their tasks, executes them locally, and reports about the results back into task_queue.
The common issue is the different hostnames/IPAddresses in the cluster definition and locally.
So if the initiator node puts tasks for a host named Host1. But the Host1 thinks about own name as localhost or xdgt634678d (internal docker hostname) and never sees tasks for the Host1 because is looking tasks for xdgt634678d. The same with internal VS external IP addresses.
DDLWorker thread crashed
That causes ClickHouse to stop executing ON CLUSTER tasks.
Check that DDLWorker is alive:
ps -eL|grep DDL
18829 18876 ? 00:00:00 DDLWorkerClnr
18829 18879 ? 00:00:00 DDLWorker
ps -ef|grep 18829|grep -v grep
clickho+ 18829 18828 1 Feb09 ? 00:55:00 /usr/bin/clickhouse-server --con...
As you can see there are two threads: DDLWorker and DDLWorkerClnr.
The second thread – DDLWorkerCleaner cleans old tasks from task_queue. You can configure how many recent tasks to store:
config.xml
<yandex>
<distributed_ddl>
<path>/clickhouse/task_queue/ddl</path>
<pool_size>1</pool_size>
<max_tasks_in_queue>1000</max_tasks_in_queue>
<task_max_lifetime>604800</task_max_lifetime>
<cleanup_delay_period>60</cleanup_delay_period>
</distributed_ddl>
</yandex>
Default values:
cleanup_delay_period = 60 seconds – Sets how often to start cleanup to remove outdated data.
task_max_lifetime = 7 * 24 * 60 * 60 (in seconds = week) – Delete task if its age is greater than that.
max_tasks_in_queue = 1000 – How many tasks could be in the queue.
pool_size = 1 - How many ON CLUSTER queries can be run simultaneously.
Too intensive stream of ON CLUSTER command
Generally, it’s a bad design, but you can increase pool_size setting
Stuck DDL tasks in the distributed_ddl_queue
Sometimes DDL tasks
(the ones that use ON CLUSTER) can get stuck in the distributed_ddl_queue because the replicas can overload if multiple DDLs (thousands of CREATE/DROP/ALTER) are executed at the same time. This is very normal in heavy ETL jobs.This can be detected by checking the distributed_ddl_queue table and see if there are tasks that are not moving or are stuck for a long time.
If these DDLs are completed in some replicas but failed in others, the simplest way to solve this is to execute the failed command in the missed replicas without ON CLUSTER. If most of the DDLs failed, then check the number of unfinished records in distributed_ddl_queue on the other nodes, because most probably it will be as high as thousands.
First, backup the distributed_ddl_queue into a table so you will have a snapshot of the table with the states of the tasks. You can do this with the following command:
CREATE TABLE default.system_distributed_ddl_queue AS SELECT * FROM system.distributed_ddl_queue;
After this, we need to check from the backup table which tasks are not finished and execute them manually in the missed replicas, and review the pipeline which do ON CLUSTER command and does not abuse them. There is a new CREATE TEMPORARY TABLE command that can be used to avoid the ON CLUSTER command in some cases, where you need an intermediate table to do some operations and after that you can INSERT INTO the final table or do ALTER TABLE final ATTACH PARTITION FROM TABLE temp and this temp table will be dropped automatically after the session is closed.
1 - There are N unfinished hosts (0 of them are currently active).
There are N unfinished hosts (0 of them are currently active).
Sometimes your Distributed DDL queries are being stuck, and not executing on all or subset of nodes, there are a lot of possible reasons for that kind of behavior, so it would take some time and effort to investigate.
Possible reasons
ClickHouse® node can’t recognize itself
SELECT * FROM system.clusters; -- check is_local column, it should have 1 for itself
getent hosts clickhouse.local.net # or other name which should be local
hostname --fqdn
cat /etc/hosts
cat /etc/hostname
Debian / Ubuntu
There is an issue in Debian based images, when hostname being mapped to 127.0.1.1 address which doesn’t literally match network interface and ClickHouse fails to detect this address as local.
https://github.com/ClickHouse/ClickHouse/issues/23504
Previous task is being executed and taking some time
It’s usually some heavy operations like merges, mutations, alter columns, so it make sense to check those tables:
SHOW PROCESSLIST;
SELECT * FROM system.merges;
SELECT * FROM system.mutations;
In that case, you can just wait completion of previous task.
Previous task is stuck because of some error
In that case, the first step is to understand which exact task is stuck and why. There are some queries which can help with that.
-- list of all distributed ddl queries, path can be different in your installation
SELECT * FROM system.zookeeper WHERE path = '/clickhouse/task_queue/ddl/';
-- information about specific task.
SELECT * FROM system.zookeeper WHERE path = '/clickhouse/task_queue/ddl/query-0000001000/';
SELECT * FROM system.zookeeper WHERE path = '/clickhouse/task_queue/ddl/' AND name = 'query-0000001000';
-- 22.3
SELECT * FROM system.zookeeper WHERE path like '/clickhouse/task_queue/ddl/query-0000001000/%'
ORDER BY ctime, path SETTINGS allow_unrestricted_reads_from_keeper='true'
-- 22.6
SELECT path, name, value, ctime, mtime
FROM system.zookeeper WHERE path like '/clickhouse/task_queue/ddl/query-0000001000/%'
ORDER BY ctime, path SETTINGS allow_unrestricted_reads_from_keeper='true'
-- How many nodes executed this task
SELECT name, numChildren as finished_nodes FROM system.zookeeper
WHERE path = '/clickhouse/task_queue/ddl/query-0000001000/' AND name = 'finished';
┌─name─────┬─finished_nodes─┐
│ finished │ 0 │
└──────────┴────────────────┘
-- The nodes that are running the task
SELECT name, value, ctime, mtime FROM system.zookeeper
WHERE path = '/clickhouse/task_queue/ddl/query-0000001000/active/';
-- What was the result for the finished nodes
SELECT name, value, ctime, mtime FROM system.zookeeper
WHERE path = '/clickhouse/task_queue/ddl/query-0000001000/finished/';
-- Latest successfull executed tasks from query_log.
SELECT query FROM system.query_log WHERE query LIKE '%ddl_entry%' AND type = 2 ORDER BY event_time DESC LIMIT 5;
SELECT
FQDN(),
*
FROM clusterAllReplicas('cluster', system.metrics)
WHERE metric LIKE '%MaxDDLEntryID%'
┌─FQDN()───────────────────┬─metric────────┬─value─┬─description───────────────────────────┐
│ chi-ab.svc.cluster.local │ MaxDDLEntryID │ 1468 │ Max processed DDL entry of DDLWorker. │
└──────────────────────────┴───────────────┴───────┴───────────────────────────────────────┘
┌─FQDN()───────────────────┬─metric────────┬─value─┬─description───────────────────────────┐
│ chi-ab.svc.cluster.local │ MaxDDLEntryID │ 1468 │ Max processed DDL entry of DDLWorker. │
└──────────────────────────┴───────────────┴───────┴───────────────────────────────────────┘
┌─FQDN()───────────────────┬─metric────────┬─value─┬─description───────────────────────────┐
│ chi-ab.svc.cluster.local │ MaxDDLEntryID │ 1468 │ Max processed DDL entry of DDLWorker. │
└──────────────────────────┴───────────────┴───────┴───────────────────────────────────────┘
-- Information about task execution from logs.
grep -C 40 "ddl_entry" /var/log/clickhouse-server/clickhouse-server*.log
Issues that can prevent task execution
Obsolete Replicas
Obsolete replicas left in zookeeper.
SELECT database, table, zookeeper_path, replica_path zookeeper FROM system.replicas WHERE total_replicas != active_replicas;
SELECT * FROM system.zookeeper WHERE path = '/clickhouse/cluster/tables/01/database/table/replicas';
SYSTEM DROP REPLICA 'replica_name';
SYSTEM STOP REPLICATION QUEUES;
SYSTEM START REPLICATION QUEUES;
https://clickhouse.tech/docs/en/sql-reference/statements/system/#query_language-system-drop-replica
Tasks manually removed from DDL queue
Task were removed from DDL queue, but left in Replicated*MergeTree table queue.
grep -C 40 "ddl_entry" /var/log/clickhouse-server/clickhouse-server*.log
/var/log/clickhouse-server/clickhouse-server.log:2021.05.04 12:41:28.956888 [ 599 ] {} <Debug> DDLWorker: Processing task query-0000211211 (ALTER TABLE db.table_local ON CLUSTER `all-replicated` DELETE WHERE id = 1)
/var/log/clickhouse-server/clickhouse-server.log:2021.05.04 12:41:29.053555 [ 599 ] {} <Error> DDLWorker: ZooKeeper error: Code: 999, e.displayText() = Coordination::Exception: No node, Stack trace (when copying this message, always include the lines below):
/var/log/clickhouse-server/clickhouse-server.log-
/var/log/clickhouse-server/clickhouse-server.log-0. Coordination::Exception::Exception(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, Coordination::Error, int) @ 0xfb2f6b3 in /usr/bin/clickhouse
/var/log/clickhouse-server/clickhouse-server.log-1. Coordination::Exception::Exception(Coordination::Error) @ 0xfb2fb56 in /usr/bin/clickhouse
/var/log/clickhouse-server/clickhouse-server.log:2. DB::DDLWorker::createStatusDirs(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<zkutil::ZooKeeper> const&) @ 0xeb3127a in /usr/bin/clickhouse
/var/log/clickhouse-server/clickhouse-server.log:3. DB::DDLWorker::processTask(DB::DDLTask&) @ 0xeb36c96 in /usr/bin/clickhouse
/var/log/clickhouse-server/clickhouse-server.log:4. DB::DDLWorker::enqueueTask(std::__1::unique_ptr<DB::DDLTask, std::__1::default_delete<DB::DDLTask> >) @ 0xeb35f22 in /usr/bin/clickhouse
/var/log/clickhouse-server/clickhouse-server.log-5. ? @ 0xeb47aed in /usr/bin/clickhouse
/var/log/clickhouse-server/clickhouse-server.log-6. ThreadPoolImpl<ThreadFromGlobalPool>::worker(std::__1::__list_iterator<ThreadFromGlobalPool, void*>) @ 0x8633bcd in /usr/bin/clickhouse
/var/log/clickhouse-server/clickhouse-server.log-7. ThreadFromGlobalPool::ThreadFromGlobalPool<void ThreadPoolImpl<ThreadFromGlobalPool>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>)::'lambda1'()>(void&&, void ThreadPoolImpl<ThreadFromGlobalPool>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>)::'lambda1'()&&...)::'lambda'()::operator()() @ 0x863612f in /usr/bin/clickhouse
/var/log/clickhouse-server/clickhouse-server.log-8. ThreadPoolImpl<std::__1::thread>::worker(std::__1::__list_iterator<std::__1::thread, void*>) @ 0x8630ffd in /usr/bin/clickhouse
/var/log/clickhouse-server/clickhouse-server.log-9. ? @ 0x8634bb3 in /usr/bin/clickhouse
/var/log/clickhouse-server/clickhouse-server.log-10. start_thread @ 0x9609 in /usr/lib/x86_64-linux-gnu/libpthread-2.31.so
/var/log/clickhouse-server/clickhouse-server.log-11. __clone @ 0x122293 in /usr/lib/x86_64-linux-gnu/libc-2.31.so
/var/log/clickhouse-server/clickhouse-server.log- (version 21.1.8.30 (official build))
/var/log/clickhouse-server/clickhouse-server.log:2021.05.04 12:41:29.053951 [ 599 ] {} <Debug> DDLWorker: Processing task query-0000211211 (ALTER TABLE db.table_local ON CLUSTER `all-replicated` DELETE WHERE id = 1)
Context of this problem is:
- Constant pressure of cheap ON CLUSTER DELETE queries.
- One replica was down for a long amount of time (multiple days).
- Because of pressure on the DDL queue, it purged old records due to the
task_max_lifetime setting. - When a lagging replica comes up, it’s fail’s execute old queries from DDL queue, because at this point they were purged from it.
Solution:
- Reload/Restore this replica from scratch.
DDL path was changed in Zookeeper without restarting ClickHouse
Changing the DDL queue path in Zookeeper without restarting ClickHouse will make ClickHouse confused. If you need to do this ensure that you restart ClickHouse before submitting additional distributed DDL commands. Here’s an example.
-- Path before change:
SELECT *
FROM system.zookeeper
WHERE path = '/clickhouse/clickhouse101/task_queue'
┌─name─┬─value─┬─path─────────────────────────────────┐
│ ddl │ │ /clickhouse/clickhouse101/task_queue │
└──────┴───────┴──────────────────────────────────────┘
-- Path after change
SELECT *
FROM system.zookeeper
WHERE path = '/clickhouse/clickhouse101/task_queue'
┌─name─┬─value─┬─path─────────────────────────────────┐
│ ddl2 │ │ /clickhouse/clickhouse101/task_queue │
└──────┴───────┴──────────────────────────────────────┘
The reason is that ClickHouse will not “see” this change and will continue to look for tasks in the old path. Altering paths in Zookeeper should be avoided if at all possible. If necessary it must be done very carefully.
2 - Unblocking a stuck DDL queue by writing to system.zookeeper
Mark a stuck ON CLUSTER task as finished with INSERT INTO system.zookeeper, without touching the queue by hand in Keeper.
A distributed DDL task (ON CLUSTER) that never finishes blocks every task queued behind it on that host, because each host’s DDLWorker runs the queue in order. You see Unexpected error (N times in a row) ... maximum retries exceeded (UNFINISHED) in the server log and a growing list of Inactive rows in system.distributed_ddl_queue.
If the task’s effect is already applied (or you no longer want it), unblock the queue by writing the flag the worker waits for. INSERT INTO system.zookeeper does this. It is safer than rmr on the queue: you write one znode per host and leave the other entries alone.
It also needs no restart. The INSERT writes the same finished and executed znodes the worker writes itself, and the worker checks for them each time it retries a task. A 26.3 cluster drained its queue this way without a pod restart.
DDLWorker keeps its queue position and unfinished tasks in memory, so deleting a task it is still retrying may leave it out of sync with Keeper. On 26.10, rmr on a finished entry and on the whole queue path both worked without a restart. We did not test rmr on a task the worker is retrying, or on older builds.
flowchart TD
A["Oldest entry with status != Finished
(system.distributed_ddl_queue)"] --> B{"Is its effect applied
or no longer needed?"}
B -- "No" --> X["Fix the cause first
(this article does not apply)"]
B -- "Yes" --> C{"Any shards/<shard>/tries_to_execute > 3
and no shards/<shard>/executed?"}
C -- "Yes (replicated-table ALTER)" --> D["Recipe B: create shards/<shard>/executed
then Recipe A if rows stay Inactive"]
C -- "No" --> E["Recipe A: create finished/<host:port>
for each non-Finished host"]
D --> V["Verify in system.distributed_ddl_queue"]
E --> V
Prerequisites
allow_zookeeper_write is a server config key, not a user setting. Without it, INSERT fails with Prohibit writing to system.zookeeper, unless config allow_zookeeper_write as true (BAD_ARGUMENTS). Put it in config.d/, and remove it when you are done:
<clickhouse>
<allow_zookeeper_write>true</allow_zookeeper_write>
</clickhouse>
INSERT INTO system.zookeeper and its allow_zookeeper_write gate arrived in 22.6 (upstream commits from 2022-05-27 and 2022-06-01) and were backported to the 22.3 LTS from 22.3.16.- The recipes use the default queue path
/clickhouse/task_queue/ddl and the default Keeper. If distributed_ddl.path differs (check system.server_settings), replace the path in every query. If your queue is on an auxiliary Keeper, also set the zookeeperName column and filter on it when you read. - The user needs the
INSERT grant on system.zookeeper. - Reading with
path LIKE ... needs SETTINGS allow_unrestricted_reads_from_keeper = 1. An exact path = '...' filter does not.
What an INSERT does
Only name, value, path and zookeeperName are writable; every other column is MATERIALIZED (INSERT returns ILLEGAL_COLUMN).
SELECT name, type, default_kind FROM system.columns
WHERE database = 'system' AND table = 'zookeeper' ORDER BY position;
- Each row creates the znode
<path>/<name>, or sets its value if it exists (no version check). Missing parent znodes are created too. A typo in path therefore creates bogus nodes instead of failing. - All rows of one
INSERT go to Keeper as one transaction per Keeper cluster. - There is no delete. Removing a znode needs
clickhouse-keeper-client or zkCli. name cannot contain /.
The znodes of a DDL task
graph LR
Q["/clickhouse/task_queue/ddl/query-0000000004"] --> ACT["active/"]
Q --> FIN["finished/"]
Q --> SH["shards/"]
FIN --> F1["localhost:9000 = 0 (status OK)"]
SH --> S1["localhost:9000 (shard node)"]
S1 --> T["tries_to_execute = 1"]
S1 --> EX["executed = localhost:9000"]
The diagram uses the default distributed_ddl.path. Check yours in system.server_settings.
finished/<host>:<port>: one child per host that completed the task. Value 0\n means success (exception code 0). The name is the host name escaped with escapeForFileName (everything except [A-Za-z0-9_] becomes %XX: - is %2D, . is %2E).shards/<shard>/executed: for ALTER on replicated tables only one replica per shard executes; the others wait for this node. tries_to_execute counts attempts by the executor. The value of executed is informational; only its existence matters.- The
<shard> node name is not escaped: it is the shard’s replica names, sorted and joined with , (for example host-a:9000,host-b:9000). Its executed value is escaped.
Copy names from system.zookeeper or system.distributed_ddl_queue instead of typing them.
Recipe A: mark a task finished on the hosts that did not finish it
Find the oldest non-finished entry:
SELECT entry, host, port, status, exception_code, query
FROM system.distributed_ddl_queue
WHERE status != 'Finished'
ORDER BY entry, host LIMIT 20;
Create the finished znode for those hosts, taking the entry id from the view (never type a new query-NNN id):
INSERT INTO system.zookeeper (name, path, value)
SELECT concat(replaceAll(replaceAll(host, '-', '%2D'), '.', '%2E'), ':', toString(port)),
concat('/clickhouse/task_queue/ddl/', entry, '/finished'),
'0\n'
FROM system.distributed_ddl_queue
WHERE entry = 'query-0000000004' AND status != 'Finished';
The replaceAll pair is exact for DNS names and IPv4 addresses. To confirm the format on your cluster, list the children of finished/ on any finished entry: they are escaped (host%2Da%2Eexample%2Ecom:9000), and your new names must match them. For IPv6 literals, copy the real name from there.
Recipe B: set the shard executed flag
Use it when the shard node has tries_to_execute above 3 and no executed child:
SELECT name, value FROM system.zookeeper
WHERE path = '/clickhouse/task_queue/ddl/query-0000000004/shards/<shard node>';
INSERT INTO system.zookeeper (name, path, value)
SELECT 'executed', concat(path, '/', name),
replaceAll(replaceAll(splitByChar(',', name)[1], '-', '%2D'), '.', '%2E')
FROM system.zookeeper
WHERE path = '/clickhouse/task_queue/ddl/query-0000000004/shards'
AND name = '<shard node, copied from the listing>';
The waiting replicas then treat the task as executed and finish it themselves. If a row stays Inactive, follow with Recipe A.
Verify
SELECT host, status, exception_code FROM system.distributed_ddl_queue
WHERE entry = 'query-0000000004';
Then count what is still blocked: SELECT host, status, count() FROM system.distributed_ddl_queue WHERE status != 'Finished' GROUP BY host, status.
Before you run it
- Finished is not executed. Nothing runs the DDL. Check its effect first (for example
system.columns or system.data_skipping_indices on every replica), and re-run the DDL by hand if it did not apply. - The backlog replays. The next entries start executing in order as soon as the head is finished. Pause pipelines that issue DDL, and read the queued statements first.
- A wrong name doesn’t unblock the task. With an unescaped host (
ghost-host.example.com:9000) the original row stays Inactive and the view shows an extra phantom host row. Remove the stray node with clickhouse-keeper-client. - A malformed entry breaks reads. If you rewrite an entry’s
value (for example the hosts: list) in the wrong format, system.distributed_ddl_queue fails with CANNOT_PARSE_QUOTED_STRING. - Don’t wait for the cleanup. It purges entries older than
task_max_lifetime (default 7 days) or more than max_tasks_in_queue (default 1000) behind the newest one. That unblocks the head too, but it silently drops the purged entries without running them.
Version notes
- The recipes were tested on 26.10 with a single-node
default cluster, which covers the znode layout, escaping and status transitions. - When a task exhausts its retries, it ends as
UNFINISHED and the worker retries it forever. Upstream fixed this in 26.8 (PR 108505
), so 26.10 does not reproduce it. We have seen it on Altinity 26.3 builds. - Recipe A has been used on a 3-replica 26.3 cluster and drained the queue. Recipe B follows the worker’s source code and has been verified at the znode level only.