8000 fix: drop removed replicas by name instead of array position by Slach · Pull Request #2128 · Altinity/clickhouse-operator · GitHub
[go: up one dir, main page]

Skip to content

fix: drop removed replicas by name instead of array position - #2128

Open
Slach wants to merge 8 commits into
0.28.0from
fix/1545-drop-replica-by-name
Open

Slach wants to merge 8 commits into
0.28.0from
fix/1545-drop-replica-by-name

Conversation

@Slach
@Slach Slach commented Oct 31, 2026
Copy link
Copy Markdown
Collaborator

Problem

If a shard has explicitly named replicas and the user removes entries that are not at the tail of layout.shards[].replicas (for example 0-0, 0-1, 0-2 are removed and 0-*-dedicated are kept), the operator runs SYSTEM DROP REPLICA for the replicas it keeps. It also runs that SQL on a pod that has already been deleted (no such host). StatefulSets and pods are removed correctly. Inserting a replica in the middle of the list marks the wrong host as added.

Root cause

  • ActionPlan.WalkRemoved / WalkAdded walk the messagediff spec diff. That diff compares slices by index, so the hosts it reports as removed or added are the positional tail of the list, not the hosts whose names were removed or added.
  • dropZKReplica runs the SQL on hostToDrop.GetShard().FirstHost(), which comes from the old CR. By the time dropZKReplicas runs, clean() may already have purged that host.

Fix

  • WalkRemoved / WalkAdded now match items by name between the old and new CR: cluster, then shard within the cluster, then host within the shard. They still report the most specific missing item (a whole cluster, a whole shard, or a host). WalkModified is unchanged.
  • dropZKReplicas now runs SYSTEM DROP REPLICA on a host of the same cluster and shard that survives in the new CR. If no such host exists, it skips with the existing error log. All gating stays the same: reconcile.host.drop.replicas.onDelete, retained volumes, and the force-drop path in the migrator.

Tests

  • TestActionPlanWalkByName covers removing the head, tail and middle replicas, inserting a replica in the middle, removing a whole shard or cluster, and a no-op change. The head, middle and insert cases fail on the old code.
  • TestSurvivingShardHost checks that the host chosen to run DROP REPLICA comes from the new CR and belongs to the same shard.
  • go build ./..., go vet -copylocks=false -printf=false ./pkg/apis/... ./pkg/controller/... and go test -vet=off -race -count=1 ./pkg/apis/... ./pkg/controller/... all pass.

fixes #1545

🤖 Generated with Claude Code

ActionPlan.WalkRemoved/WalkAdded walked the messagediff spec diff, which
compares slices by index. Removing replicas anywhere but at the tail of
layout.shards[].replicas reported the tail (kept) hosts as removed, so the
operator ran SYSTEM DROP REPLICA for live replicas. Inserting a replica in
the middle marked the wrong host as added.

Match clusters, shards and hosts by name between the old and new CR
instead. Run DROP REPLICA on a host of the same shard that survives in the
new CR rather than on the first host of the old shard, which may already
be purged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alex-zaitsev
Copy link
Copy Markdown
Member

@Slach , need e2e test with repro and demonstration of a fix.

@Slach
Slach requested a review from alex-zaitsev November 2, 2026 09:17
ActionPlan.WalkRemoved/WalkAdded walked the messagediff spec diff, which
compares slices by index. Removing replicas anywhere but at the tail of
layout.shards[].replicas reported the tail (kept) hosts as removed, so the
operator ran SYSTEM DROP REPLICA for live replicas. Inserting a replica in
the middle marked the wrong host as added.

Match clusters, shards and hosts by name between the old and new CR
instead. Run DROP REPLICA on a host of the same shard that survives in the
new CR rather than on the first host of the old shard, which may already
be purged.

Fixes #1545

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Slach
Slach force-pushed the fix/1545-drop-replica-by-name branch from e09be00 to 9cd1d31 Compare November 3, 2026 05:02
Slach and others added 5 commits November 3, 2026 09:03
…ection by pod name, extend test_010089 (#1545)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lickhouse-operator into fix/1545-drop-replica-by-name

# Conflicts:
#	pkg/controller/chi/worker-deleter.go
#	pkg/controller/chi/worker-deleter_test.go

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Operator dropping wrong replicas when removing older replicas from cluster layout

2 participants

0