Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context lock contention fix #55121

Merged

Conversation

kitaisreal
Copy link
Collaborator

Changelog category (leave one):

  • Performance Improvement

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

Fix contention on Context lock, this significantly improves performance for a lot of short-running concurrent queries.

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-performance Pull request with some performance improvements label Sep 29, 2023
@robot-ch-test-poll4
Copy link
Contributor

robot-ch-test-poll4 commented Sep 29, 2023

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

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

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
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR✅ success
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker image for serversThe check to build and optionally push the mentioned image to docker hub✅ 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. Integrational 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
Mergeable CheckChecks if all other necessary checks are successful✅ 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
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub✅ success
SQLTestThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool✅ success
SqllogicRun clickhouse on the sqllogic test set against sqlite and checks that all statements are passed✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ 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
Check nameDescriptionStatus
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors❌ failure
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❌ failure

@kitaisreal
Copy link
Collaborator Author

Benchmark to simulate complex short-running query, this is not worst case scenario.

./clickhouse-benchmark --query="SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT UserID, count(*) FROM (SELECT * FROM datasets.hits_v1 LIMIT 10) GROUP BY UserID))))))))) FORMAT Null" --concurrency=1000

Before:

localhost:9000, queries: 8698, QPS: 90.463, RPS: 904.634, MiB/s: 0.007, result RPS: 0.000, result MiB/s: 0.000.

0.000%		2.849 sec.
10.000%		9.661 sec.
20.000%		10.214 sec.
30.000%		10.499 sec.
40.000%		10.691 sec.
50.000%		10.851 sec.
60.000%		10.990 sec.
70.000%		11.145 sec.
80.000%		11.306 sec.
90.000%		11.529 sec.
95.000%		11.695 sec.
99.000%		12.049 sec.
99.900%		12.437 sec.
99.990%		12.986 sec.
SELECT
    quantileExact(0.5)(lock_wait_milliseconds),
    max(lock_wait_milliseconds)
FROM
(
    SELECT (ProfileEvents['ContextLockWaitMicroseconds']) / 1000. AS lock_wait_milliseconds
    FROM system.query_log
    WHERE lock_wait_milliseconds > 0
)

┌─quantileExact(0.5)(lock_wait_milliseconds)─┬─max(lock_wait_milliseconds)─┐
│                                  10428.938 │                   12771.711 │
└────────────────────────────────────────────┴─────────────────────────────┘

After:

Queries executed: 10177.

localhost:9000, queries: 10177, QPS: 175.670, RPS: 1756.703, MiB/s: 0.013, result RPS: 0.000, result MiB/s: 0.000.

0.000%		0.200 sec.
10.000%		3.185 sec.
20.000%		3.551 sec.
30.000%		4.096 sec.
40.000%		6.402 sec.
50.000%		6.559 sec.
60.000%		6.629 sec.
70.000%		6.697 sec.
80.000%		6.782 sec.
90.000%		6.947 sec.
95.000%		7.848 sec.
99.000%		9.959 sec.
99.900%		12.300 sec.
99.990%		13.092 sec.
SELECT quantileExact(0.5)(lock_wait_milliseconds), max(lock_wait_milliseconds) FROM (SELECT (ProfileEvents['ContextLockWaitMicroseconds'] / 1000.0) AS lock_wait_milliseconds FROM system.query_log WHERE lock_wait_milliseconds > 0)

┌─quantileExact(0.5)(lock_wait_milliseconds)─┬─max(lock_wait_milliseconds)─┐
│                                      0.001 │                       7.515 │
└────────────────────────────────────────────┴─────────────────────────────┘

@kitaisreal kitaisreal added the can be tested Allows running workflows for external contributors label Sep 29, 2023
@rschu1ze rschu1ze self-assigned this Sep 29, 2023
@KochetovNicolai KochetovNicolai self-assigned this Sep 29, 2023
@@ -172,15 +172,42 @@ namespace ErrorCodes
} while (false) \


/// Thread safe object initializer using double-checked locking
class Initializer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok. Maybe we can think about replacing it with std::call_once

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not something like

void initialize()
{
    static Initializer ...
}

Copy link
Member

@KochetovNicolai KochetovNicolai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not check all the lock calls, but overall, it looks good. Let\s merge.

@KochetovNicolai KochetovNicolai merged commit b67a0fe into ClickHouse:master Oct 3, 2023
274 of 276 checks passed
@KochetovNicolai KochetovNicolai added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 4, 2023
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 4, 2023
…532d1b73e1236ed54ce9e182598ee

Cherry pick #55121 to 23.9: Context lock contention fix
@alexey-milovidov alexey-milovidov removed the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 7, 2023
src/Interpreters/Context.cpp Show resolved Hide resolved
src/Interpreters/Context.cpp Show resolved Hide resolved
src/Interpreters/Context.cpp Show resolved Hide resolved
src/Interpreters/Context.cpp Show resolved Hide resolved
src/Interpreters/Context.cpp Show resolved Hide resolved
src/Interpreters/Context.cpp Show resolved Hide resolved
src/Interpreters/Context.cpp Show resolved Hide resolved
src/Interpreters/Context.cpp Show resolved Hide resolved
src/Interpreters/Context.cpp Show resolved Hide resolved
@kitaisreal
Copy link
Collaborator Author

@rschu1ze it is impossible to check all invariants in Context and we had a lot of bugs even before this refactoring, I started pull request that will use TSA for all Context and Shared context members to find bugs. It is not finished yet, but you can take a look #55278.

This was referenced Oct 8, 2023
yokofly added a commit to timeplus-io/proton that referenced this pull request Nov 21, 2023
porting ClickHouse/ClickHouse#55121
fix dup lock in calculateAccessRightsWithLock
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-performance Pull request with some performance improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants