Skip to content

Add skip_distributed_ddl option to cluster replica config - #100547

Open
tuanpach wants to merge 5 commits into
ClickHouse:masterfrom
tuanpach:add-skip-distributed-ddl
Open

tuanpach wants to merge 5 commits into
ClickHouse:masterfrom
tuanpach:add-skip-distributed-ddl

Conversation

@tuanpach

@tuanpach tuanpach commented Mar 24, 2026 •

Copy link
Copy Markdown
Member

When multiple replicas in a cluster share the same host (e.g. localhost), the DDL worker on that host claims and executes ON CLUSTER DDL tasks for every one of them, causing duplicate execution. This adds a per-replica <skip_distributed_ddl> config tag (default false) that excludes a replica from ON CLUSTER DDL dispatch while keeping it fully available for Distributed table query routing and reads/writes.

The change covers the whole dispatch path:

  • executeDDLQueryOnCluster builds the effective (non-skipped) replica set once and reuses it for both the task host list and default-database / access-rights derivation; it throws BAD_ARGUMENTS if every selected replica is skipped.
  • DDLWorker no longer registers skipped replicas as local hosts, so they do not claim tasks.
  • BACKUP / RESTORE ON CLUSTER share the same dispatch path, so their coordination host set is kept consistent: skipped replicas are excluded from the coordination wait-set and the RESTORE access precheck, while shard/replica numbering is preserved.

Closes: #95316

Changelog category (leave one):

  • Improvement

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

Added a per-replica skip_distributed_ddl cluster config option. A replica with this option set is excluded from ON CLUSTER DDL queries (and BACKUP/RESTORE ON CLUSTER) while remaining available for Distributed table routing. This avoids duplicate DDL execution when several replicas share the same physical host.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4e3ccfd]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_asan_ubsan, flaky) FAIL
test_ddl_worker_skip_stale_registration/test.py::test_skip_distributed_ddl_drops_stale_registration FAIL cidb

AI Review

Summary

This PR adds a per-replica skip_distributed_ddl cluster option and threads it through ON CLUSTER DDL plus BACKUP/RESTORE ON CLUSTER dispatch and coordination. Most of the previously raised correctness gaps are fixed in the current code, but the new early backup/restore precheck does not preserve the existing shard/replica selector validation semantics, so invalid shard_num/replica_num can now be misreported as skip_distributed_ddl errors or still fail only after the backup/restore engine has been opened.

Findings

⚠️ Majors

  • [src/Backups/BackupsWorker.cpp:115] The new throwIfAllReplicasSkipped() helper interprets filterHostIDs(...).empty() as “all selected replicas are skipped”, but filterHostIDs() is not a validator: it silently drops out-of-range shard/replica selections. That breaks the dispatch contract in two ways. First, BACKUP ... SETTINGS replica_num=3 on a two-replica cluster now throws the new BAD_ARGUMENTS / skip_distributed_ddl exception instead of the existing NO_SUCH_REPLICA from Cluster::filterAddressesByShardOrReplica(). Second, on a heterogeneous cluster, replica_num=2 can pass this precheck because one shard has replica 2, so coordination and the backup destination are initialized, and only then does executeDDLQueryOnCluster() fail when another shard lacks replica 2. The early guard should validate selectors with the same path as dispatch before treating an empty effective host set as “all skipped”.
Final Verdict

Status: ⚠️ Request changes

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 24, 2026
Comment thread programs/server/config.xml Resolved
@tuanpach
tuanpach force-pushed the add-skip-distributed-ddl branch from 621aba1 to 4cdffc1 Compare July 11, 2026 00:20
Comment thread src/Backups/BackupsWorker.cpp Resolved
Add a per-replica `skip_distributed_ddl` option to cluster replica
configuration. A replica with `skip_distributed_ddl` set stays available
for `Distributed` table routing and reads/writes, but is excluded from
`ON CLUSTER` DDL dispatch. This is useful when several replicas in a
cluster point at the same physical host (e.g. `localhost`) and would
otherwise each execute the same DDL task.

- `Cluster` parses the option, and `executeDDLQueryOnCluster` builds the
  effective (non-skipped) address list once, reusing it for the task
  host list and for default-database / access-rights derivation. It
  throws `BAD_ARGUMENTS` if every selected replica is skipped.
- `DDLWorker` no longer registers skipped replicas as local hosts, so
  they do not claim ON CLUSTER tasks.
- `BACKUP` / `RESTORE ON CLUSTER` reuse the same dispatch path, so their
  coordination host set is kept consistent. `cluster_host_ids` is a
  positional shard/replica map, so skipped replicas are marked with a
  `kSkippedHost` sentinel rather than dropped: this preserves shard/replica
  numbering while `filterHostIDs` excludes them from the coordination
  wait-set. The `RESTORE` access precheck skips them as well.

Adds documentation and integration tests.
@tuanpach
tuanpach force-pushed the add-skip-distributed-ddl branch from dd90ff0 to 1d3e919 Compare July 11, 2026 06:33
Comment thread src/Interpreters/DDLWorker.cpp Resolved
Comment thread src/Backups/BackupsWorker.cpp Resolved
tuanpach added 2 commits July 25, 2026 09:29
getAllHostIDsFromClusters only excluded skip_distributed_ddl replicas from
the config-derived host set, which feeds createReplicaDirs. But
markReplicasActive decides which replicas this server owns by iterating the
existing znodes under replicas_dir and reclaiming any that resolve to self,
without checking the allowed host set. So a replica registered before
skip_distributed_ddl was enabled kept its /replicas/<host_id>/active entry
after SYSTEM RELOAD CONFIG or restart, meaning the feature did not take
effect on existing deployments.

Gate the self-host claim on getAllHostIDsFromClusters: a self-host that is
no longer allowed is not re-marked active, and on reload its active node is
released so the replica stops claiming the host.

Add an integration test that enables skip_distributed_ddl on an already
registered replica and verifies it is no longer claimed as active after a
restart, while the still-allowed replica keeps its registration.
Covers the skip_distributed_ddl backup/restore path: a shard with one normal
replica and one replica excluded via skip_distributed_ddl. It checks that
BACKUP and RESTORE ON CLUSTER complete (the skipped replica is kept out of the
coordination host set instead of being waited on) and that the skipped replica
is not part of the ON CLUSTER DDL, exercising getClusterHostIDsForCoordination
and the skipped-replica branch of BackupsWorker::doRestore.
Comment thread src/Backups/BackupsWorker.cpp Resolved
executeDDLQueryOnCluster already throws BAD_ARGUMENTS when every selected
replica has skip_distributed_ddl, but only when the internal query is
dispatched - after the initiator created coordination and opened the
backup/restore engine. An unrelated storage error could therefore mask the
configuration error, and BACKUP could touch the destination (leaving partial
files behind with backups.remove_backup_files_after_failure = 0).

Validate the effective (non-skipped) host set up front in the backup and
restore initiators, before coordination and engine open, reusing filterHostIDs
so the check matches the dispatch/coordination host set and honors
shard_num/replica_num.

Extend the integration test to cover an all-skipped selection (via
replica_num) and assert the failure is reported before any backup artifact is
created and before the restore source is opened.
void throwIfAllReplicasSkipped(
const std::vector<Strings> & cluster_host_ids, size_t shard_num, size_t replica_num, std::string_view operation)
{
if (BackupSettings::Util::filterHostIDs(cluster_host_ids, shard_num, replica_num).empty())

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.

filterHostIDs() is only a selection helper; it does not preserve the cluster's shard/replica range validation. Using empty() here therefore conflates “all selected replicas are skipped” with “the requested shard/replica does not exist”.

Concrete repros:

  • On a two-replica cluster, BACKUP ... SETTINGS replica_num=3 now throws the new BAD_ARGUMENTS / skip_distributed_ddl error instead of the existing NO_SUCH_REPLICA from Cluster::filterAddressesByShardOrReplica().
  • On a heterogeneous cluster, replica_num=2 can even pass this precheck because one shard has replica 2, so coordination/storage are initialized, and only then does executeDDLQueryOnCluster() fail when another shard lacks replica 2.

Please validate the shard/replica selection with the same path as dispatch (or teach filterHostIDs() to throw INVALID_SHARD_ID / NO_SUCH_REPLICA) before interpreting an empty result as “all skipped”.

@clickhouse-gh

clickhouse-gh Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.20% -0.10%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.40% +0.00%

Changed lines: Changed C/C++ lines covered: 79/81 (97.53%) · Uncovered code

Full report · Diff report

@abashkeev abashkeev added the comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). label Sep 4, 2026

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

comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DDLWorker fails task execution with on cluster (since 25.8)

2 participants