[Draft] Not That Trivial Resharding (basic infrastructure) by azat · Pull Request #58132 · ClickHouse/ClickHouse · GitHub
Skip to content

[Draft] Not That Trivial Resharding (basic infrastructure)#58132

Closed
azat wants to merge 84 commits into
ClickHouse:masterfrom
azat:replicated-cluster
Closed

[Draft] Not That Trivial Resharding (basic infrastructure)#58132
azat wants to merge 84 commits into
ClickHouse:masterfrom
azat:replicated-cluster

Conversation

@azat

@azat azat commented Dec 21, 2023

Copy link
Copy Markdown
Member

This is the draft of the re-sharding via ReplicatedMergeTree (as described in #45766).

Plan

I've splitted implementation into smaller peaces:

  • basic infrastructure
  • pre-alpha version
  • alpha version
  • production

I have checklist in my notes, that I can share later, once we will agree on the overall idea

Basic infrastructure

The main goal of this PR is to prepare some basic infrastructure with everything working in general (i.e. basic SELECT/INSERT, replication protocol, ALTERs, re-sharding) so that it can be merged as a first step and later the work can be done on top. This will allow to:

  • make PR smaller, and hence review easier (at least I hope)
  • reduce probability of conflicts
  • involve more people to work on it later (there many things that can be done in parallel actually, once the infrastructure will be there)

Known tech debt:

  • proper SELECTs support (i.e. now it does not work for secure connections, and if there is user/password)
    I think that this bits can be done on top, not spent time on them.
  • there are also some tests flakiness due to SYNC CLUSTER syncs only specific replica

Implementation notes

I've tried to keep as less changes as possible in the common code (hence sometimes you may find "odd" code, or I avoided introducing some new SYSTEM queries in this PR, that's how SYSTEM SYNC REPLICA CLUSTER appeared), i.e. not modifying StorageReplicatedMergeTree a lot, but keep code in a separate classes.

Changes should be 100% backward compatible if the cluster mode is not used.

So this PR includes:

  • basic SELECT support (but does not work when credential differs, or with secure protocol) - SELECT just gets the last cluster partitions map and send queries to those replicas with filter by specific partition
  • basic INSERT support - split blocks on INSERT and send then to different replicas synchronously, when the block created first it is assigned to cluster_replication_factor replicas
  • ALTERs/mutations/merges should work correctly (as far as I'm aware...)
  • basic re-sharding support (it does not have any policies, right now the algorithm is pretty dumb, just distributed partitions until all replicas will have equal number of them)

Coordinator changes

It introduces concept of "cluster partition", it is stored in the zookeeper_path/block_numbers/X.
Also there is some meta information that is required, and it is stored under zookeeper_path/cluster/.
And now each replication entry has list of replicas that should execute this log entry (it is filtered only on the moving it form log to queue).

Settings

  • table
    • cluster
    • cluster_replicaton_factor
  • query
    • cluster_query_shards

Queries

  • SYSTEM SYNC REPLICA CLUSTER
  • SYSTEM DROP CLUSTER REPLICA

Introspection

  • system.cluster_partitions

Notes for reviewers

Since this is a draft, please do not look thoroughly, I'm sharing this to get some feedback on the overall implementation and also coordinator communication. I will share some of mine concerns (but maybe you will find something else):

  • the main concern is that partition migration is synchronous, only after it done DROP_RANGE could be scheduled (and also now since DROP_RANGE is deferred you can get a race between removing the range of old parts during cloning partition and excecuting DROP_RANGE of the already migrated data from source replica, not a race, but assert that the state modified twice)
  • right now I'm using some existing zookeeper nodes (i.e. zookeeper_path/block_numbers/X to store information about cluster partition), and I'm not sure that this is a good idea

Changelog category (leave one):

  • New Feature

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

Not That Trivial Resharding

Documentation entry for user-facing changes

TBD

CI Settings (Only check the boxes if you know what you are doing):

  • Woolen Wolfdog

Cc: @tavplubix

azat added 30 commits December 21, 2023 14:48
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Before this patch it leads to this exception:

  Code: 10. DB::Exception: Received from localhost:9000. DB::Exception: Not found column key in block. There are only columns: _partition_id: While executing Remote. (NOT_FOUND_COLUMN_IN_BLOCK)

The problem is that intiial query required {"key"} columns, while the
queries on replicas has {"_partition_id"}, and so it thinks that it does
not need any column, while actually it is a virtual column, so it still
needs another column, that will be added in ReadFromMergeTree

And anyway from the remote node it will got only "_partition_id" column
(due to lack of projections), but initiator expects "key".

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
…edColumns()

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This was an attempt to fix _partition_id filter for replicated-cluster,
but this is not enough still, since analyzer adds idnex to the columns
and so if the order will differs, then the header will not match, and
this is what happens now.

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This patch contains:
====================

- Distributed SELECT from the ReplicatedMergeTree with cluster=1
- Distributed INSERT from the ReplicatedMergeTree with cluster=1

Implementaiton details
======================

TBD

Query Settings
==============

- cluster_query_shards

MergeTree Settings
==================

- cluster=false
- cluster_replication_factor=1

ZooKeeper
=========

- /replicas/$/host -- information about node (disk space, labels) (*in future*)
- /block_numbers/$ -- now contains replicas list

Changelog:
- Fix ALTER for replicated cluster mode (ALTER_METADATA should be
  executed on all replicas)

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This is just a set of "C++ scripts" as the initial re-sharding
implementation.

Sadly but, as usual in big changes, it is pretty hard to avoid changing
something else, and this patch is not an exception from this... (some
logging, interfaces, tests).

Changelog:
- simple implementation with fetchPart() + DROP_RANGE
- deferred DROP_RANGE
- Distributor with FSM
- restore from coordinator
- clonePartition and friends
- cleaner API for ReplicatedMergeTreeClusterPartition
- REVERT_MIGRATE_PARTITION
- allow multiple partitions at once
- check version of the cluster/distributor node to avoid parallel re-sharding assignments
- proper wait of distributor on shutdown to avoid UAF
- tests with parallel replica restarts and fixes for it (uneven
  distribution after re-sharding)
- Remove ReplicatedMergeTreeClusterDistributor::migratePartition()
- Even distribution on INSERT
- Introduce ReplicatedMergeTreeClusterPartitionSelector
- ReplicatedMergeTreeClusterPartitionSelector::allocateParitition()

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Likely should fix:

    Cannot process partition 3 (all_replicas: [1, 2, 4], active_replicas: [1, 2], MIGRATING 2 -> 4, version: 1), will revert: Code: 233. DB::Exception: Unexpected part name:

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
So that we do not need to look at is_dropped/is_shutdown storage flags
inside balancer.

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Triggered by 03011_replicated_cluster_readonly_re_sharding

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
…YNC REPLICA)

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Fixes: 03017_replicated_cluster_mutations_stress
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
…d use active replicas

Otherwise it is possible to go into a sitatuion when mutation entry will
be created for the partition that will be removed during migration, and
never finished:

    │ 2023-09-29 17:43:54.778175 │      8939 │ default.data_r2 (Cluster)                                                        │ Loading partition from coordinator: 9 (all_replicas: [3, 4, 2], active_replicas: [3, 4], MIGRATING 3 -> 2, version: 7)
    │ 2023-09-29 17:43:54.778266 │      9111 │ default.data_r2 (ClusterBalancer)                                                │ Source replica does not have part 9_0_0_0_3. Removing it from ZooKeeper.
    │ 2023-09-29 17:43:54.778271 │      9111 │ default.data_r2 (ClusterBalancer)                                                │ Source replica does not have part 9_0_0_0_2. Removing it from ZooKeeper.
    │ 2023-09-29 17:43:54.778275 │      9111 │ default.data_r2 (ClusterBalancer)                                                │ Source replica does not have part 9_0_0_0_1. Removing it from ZooKeeper.
    │ 2023-09-29 17:43:54.778280 │      9111 │ default.data_r2 (ClusterBalancer)                                                │ Source replica does not have part 9_0_0_0_4. Removing it from ZooKeeper.
    │ 2023-09-29 17:43:54.778284 │      9111 │ default.data_r2 (ClusterBalancer)                                                │ Source replica does not have part 9_0_0_0_5. Removing it from ZooKeeper.
    │ 2023-09-29 17:43:54.778410 │      8939 │ default.data_r2 (ReplicatedMergeTreeQueue)                                       │ Will apply 20 mutations and mutate part 9_0_0_0_5 to version 25 (the last version is 25)
    │ 2023-09-29 17:43:54.778684 │      8939 │ default.data_r2 (d966de30-d7b7-404e-ba59-2b5e63072f44)                           │ Created log entry for mutation 9_0_0_0_25
    │ 2023-09-29 17:43:54.778694 │      9111 │ default.data_r2 (ClusterBalancer)                                                │ Source replica does not have part 9_0_0_0_5. Removing it from working set.
    │ 2023-09-29 17:43:54.778701 │      9111 │ default.data_r2 (ClusterBalancer)                                                │ Detaching 9_0_0_0_5

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
azat added 7 commits April 27, 2024 16:45
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
…d is broken)

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Onelinears:

    $ for i in tests/queries/0_stateless/*replicated_cluster*.sql; do { echo -e "SET allow_experimental_analyzer=0; -- FIXME: analyzer is not supported yet\n"; cat $i; } | sponge $i; done
    $ for i in tests/queries/0_stateless/*replicated_cluster*.sh; do { echo -e '#!/usr/bin/env bash\n\nCLICKHOUSE_CLIENT_OPT+="--allow_experimental_analyzer=0" # FIXME: analyzer is not supported yet\n'; grep -v '#!/usr/bin/env bash' $i; } | sponge $i; done

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
…ePartition()

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
@tarunannapareddy

Copy link
Copy Markdown

azat added 3 commits June 27, 2024 13:31
Conflicts:
    src/Core/SettingsChangesHistory.h
    src/Interpreters/ClusterProxy/executeQuery.cpp
    src/Interpreters/ClusterProxy/executeQuery.h
    src/Storages/StorageReplicatedMergeTree.cpp

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
@azat azat force-pushed the replicated-cluster branch from 0913700 to fac5965 Compare June 27, 2024 14:07
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2024

Copy link
Copy Markdown
Contributor

Dear @azat, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@alexey-milovidov alexey-milovidov added the close in a month if not active This will be closed in case of no information label Oct 1, 2024
@Ted-Jiang

Ted-Jiang commented Oct 17, 2024

Copy link
Copy Markdown
Contributor

@azat Sorry for disturb you. May I ask what the progress in this Resharding implements? I want to contribute this feature together to make this go faster. Maybe setup some subtask 🤔

@azat

azat commented Oct 17, 2024

Copy link
Copy Markdown
Member Author

I would love to continue, and indeed, the idea is to provide some basic infrastructure for this, and after this, I could really use some help (there are lots of things would need to be done), but, there are couple of problems - I started this PR almost year ago and lots of things since then had been changed (i.e. I need to implement support for analyzer case) but one of the things that is not depends from me is the review, since I want to make sure that this is the best way to going forward.

I'm not saying that I'm going to abandon it, but I guess I need push more "energy" into this.

@Ted-Jiang

Ted-Jiang commented Oct 17, 2024

Copy link
Copy Markdown
Contributor

I'm not saying that I'm going to abandon it, but I guess I need push more "energy" into this.

Thanks for your kindly quick reply 👍 , As i am newbee in clickhouse community, will try my best to involve in.
@alexey-milovidov Could you give this some advice on this feature from core team's perspective? ❤️

@alexey-milovidov

Copy link
Copy Markdown
Member

I need this feature, as described here: #45766

@Ted-Jiang

Copy link
Copy Markdown
Contributor

As cluster_replication_factor i have question about the how two set this.

If i have only one cluster each has 4 replica:
create table like cluster=1, cluster_replication_factor=2;

what the difference with two cluster each has 2 replica
cluster=1, cluster_replication_factor=2; and cluster=2, cluster_replication_factor=2; 🤔

@Ted-Jiang

Copy link
Copy Markdown
Contributor

@azat I look through this pr carefully, nice design and clean code 👍

As the name == "cluster" setting on mergeTree is readonly. I think one big scenarios of this feature is: split existing ReplicatedMergeTree , Can i assume the existing one is create with cluster = 1 , cluster_replication_factor=replication_number?

@clickhouse-gh

clickhouse-gh Bot commented Dec 24, 2024

Copy link
Copy Markdown
Contributor

Dear @azat, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

5 similar comments
@clickhouse-gh

clickhouse-gh Bot commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

Dear @azat, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Apr 29, 2025

Copy link
Copy Markdown
Contributor

Dear @azat, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jul 1, 2025

Copy link
Copy Markdown
Contributor

Dear @azat, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2025

Copy link
Copy Markdown
Contributor

Dear @azat, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2025

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

close in a month if not active This will be closed in case of no information pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants