Skip to content

Respect prefer_locahost_replica in parallel_distributed_insert_select#72190

Merged
nickitat merged 2 commits intoClickHouse:masterfrom
filimonov:patch-12
Dec 3, 2024
Merged

Respect prefer_locahost_replica in parallel_distributed_insert_select#72190
nickitat merged 2 commits intoClickHouse:masterfrom
filimonov:patch-12

Conversation

@filimonov
Copy link
Contributor

@filimonov filimonov commented Nov 20, 2024

Changelog category (leave one):

  • Improvement

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

Respect prefer_locahost_replica when building plan for distributed INSERT ... SELECT.


Currently the local part of the pipeline to execute complex insert .. select with parallel_distributed_insert_select is mixed together with remote pipelines just waiting for the remote query execution.

So if the local pipeline takes all (or most of) threads, the remote pipelines will not be started at all for a longer time.

The simplest possible workaround for that - to disable prefer_localhost_replica to make all subqueries executed in remote way is not possible currently, since the feature just ignores prefer_locahost_replica=0

@filimonov
Copy link
Contributor Author

Also i think it will be better to expand max_threads here to max_distributed_connections similar to

size_t max_threads = std::min<size_t>(settings[Setting::max_distributed_connections], jobs_count);

and

/**
* To simultaneously query more remote servers when async_socket_for_remote is off
* instead of max_threads, max_distributed_connections is used:
* since threads there mostly spend time waiting for data from remote servers,
* we can increase the degree of parallelism to avoid sequential querying of remote servers.
*
* DANGER: that can lead to insane number of threads working if there are a lot of stream and prefer_localhost_replica is used.
*
* That is not needed when async_socket_for_remote is on, because in that case
* threads are not blocked waiting for data from remote servers.
*
*/
bool is_sync_remote = false;
if (storage && storage->isRemote() && !settings[Setting::async_socket_for_remote])
{
is_sync_remote = true;
max_threads_execute_query = max_streams = settings[Setting::max_distributed_connections];
}

@filimonov filimonov marked this pull request as draft November 20, 2024 21:08
@ilejn
Copy link
Contributor

ilejn commented Nov 29, 2024

The simplest possible workaround for that - to enable prefer_localhost_replica

@filimonov , you mean "to disable prefer_localhost_replica", right?

@filimonov
Copy link
Contributor Author

filimonov commented Dec 2, 2024

The test (manual, since that can't be stable in CI/CD because it would measure the time, and i can't just check the EXPLAIN, because of #72188 )

drop table if exists src;
drop table if exists dst;
drop table if exists dst_local;
create table dst_local engine=MergeTree ORDER BY tuple() AS system.numbers;
create table dst engine=Distributed('test_cluster_two_shards', currentDatabase(), dst_local, rand());
create table src engine=Distributed('test_cluster_two_shards', system, numbers_mt, rand());

set parallel_distributed_insert_select=2, prefer_localhost_replica=0

/* select from number_mt used as a trivial illustration of the query which can be much faster when multithreaded  */
insert into dst select number from src where not(sleepEachRow(0.001)) limit 10000 SETTINGS max_block_size = 1, max_threads=50;

Time before the change (local pipeline executed single threaded, linearly with remote pipeline).

0 rows in set. Elapsed: 11.795 sec. Processed 20.09 thousand rows, 160.68 KB (1.70 thousand rows/s., 13.62 KB/s.)
Peak memory usage: 3.25 MiB.

Time after the change (both local & remote are executed as subqueries which can use enough threads).

0 rows in set. Elapsed: 0.234 sec. Processed 20.10 thousand rows, 160.78 KB (85.81 thousand rows/s., 686.49 KB/s.)
Peak memory usage: 2.09 MiB.

@filimonov filimonov marked this pull request as ready for review December 2, 2024 22:46
@nickitat nickitat self-assigned this Dec 3, 2024
@nickitat nickitat added the can be tested Allows running workflows for external contributors label Dec 3, 2024
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-improvement Pull request with some product improvements label Dec 3, 2024
@robot-ch-test-poll3
Copy link
Contributor

robot-ch-test-poll3 commented Dec 3, 2024

This is an automated comment for commit f245a49 with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure
Successful checks
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integration tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success

@nickitat nickitat changed the title respect prefer_locahost_replica=0 in parallel_distributed_insert_select Respect prefer_locahost_replica in parallel_distributed_insert_select Dec 3, 2024
@nickitat
Copy link
Member

nickitat commented Dec 3, 2024

yeah, i guess the problem is exactly that we execute local query with a single thread. for unclear reason. pipeline looks fine. i mean we could make workaround working, but it won't solve anything in practice, because prefer_locahost_replica enabled by default.
would you like to dig a little deeper and fix the problem? otherwise pls create an issue.

@nickitat nickitat added this pull request to the merge queue Dec 3, 2024
Merged via the queue into ClickHouse:master with commit 13b48a4 Dec 3, 2024
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Dec 3, 2024
@filimonov
Copy link
Contributor Author

filimonov commented Dec 4, 2024

but it won't solve anything in practice, because prefer_locahost_replica enabled by default

Actually i don't like that feature (for many reasons), and prefer to keep it disabled.

Some problems of that are listed here #6349

But in short - it CHANGES the way HOW the query is executed, instead of changing WHERE the query will be executed (as you can think from it's name), it make a lot of things more complex and behavior non-transparent.

I would even opt for changing the default if you ask me, or change it to behave like the name says. Just prefer the localhost insead the other replica from same shard (but still run one more subquery there).

would you like to dig a little deeper and fix the problem?

I did some simple experiments, failed to find why it's single threaded (since it's insert select probably max_insert_threads should help, but it doesn't, or maybe something else).

But anyway i have some doubts that even if we will allow more thread to execute the load pipeline it will be a robust fix. (because if the pipeline mixes ReadFromRemote & some compute-heavy step - there is always a risk that the ReadFromRemote will not get its' thread and will not start the remote jobs while compute-heavy steps will not finish their work).

otherwise pls create an issue.

OK. #72792

Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Feb 14, 2025
Respect `prefer_locahost_replica` in `parallel_distributed_insert_select`
Enmk added a commit to Altinity/ClickHouse that referenced this pull request Feb 14, 2025
…efer_locahost_replica

23.8 Backport of ClickHouse#72190: Respect `prefer_locahost_replica` in `parallel_distributed_insert_select`
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Feb 24, 2025
Respect `prefer_locahost_replica` in `parallel_distributed_insert_select`
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Feb 25, 2025
Respect `prefer_locahost_replica` in `parallel_distributed_insert_select`
zvonand pushed a commit to Altinity/ClickHouse that referenced this pull request Feb 27, 2025
Respect `prefer_locahost_replica` in `parallel_distributed_insert_select`
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Feb 27, 2025
Respect `prefer_locahost_replica` in `parallel_distributed_insert_select`
Enmk added a commit to Altinity/ClickHouse that referenced this pull request Mar 1, 2025
…ahost_replica

24.8 Backport of ClickHouse#72190 - Respect `prefer_locahost_replica` in `parallel_distributed_insert_select`
Enmk added a commit to Altinity/ClickHouse that referenced this pull request Mar 6, 2025
…ahost_replica-in-parallel_distributed_insert_select

24.3 Backport of ClickHouse#72190 Respect `prefer_locahost_replica` in `parallel_distributed_insert_select`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants