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

MaterializedMySQL: Introduce MySQL Binlog Client #57323

Merged
merged 1 commit into from Jan 10, 2024

Conversation

valbok
Copy link
Contributor

@valbok valbok commented Nov 28, 2023

The MySQL Binlog Client provides a mechanism in ClickHouse to share the binlog from a MySQL instance among multiple MaterializedMySQL databases. This avoids consuming unnecessary bandwidth and CPU when replicating more than one schema/database.

Suggesting to disable this feature by default for now. It should be explicitly enabled by SETTINGS use_binlog_client=1.
But if you would permanently enable it in MaterializedMySQLSettings, it should keep old behavior and all tests should pass too.

Details:

  1. Introduced IBinlog and its impl to read the binlog events from socket - BinlogFromSocket, or file - BinlogFromFile. Based on prev impl of EventBase and the same old binlog parsers. It fully keeps BC with old version. Fixed ./check-mysql-binlog to test new impl.
  2. Introduced BinlogEventsDispatcher, it reads the event from the source IBinlog and sends it to currently attached IBinlog instances.
  3. Introduced BinlogClient, which is used to group a list of BinlogEventsDispatcher by MySQL binlog connection which is defined by user:password@host:port. All dispatchers with the same binlog position should be merged to one.
  4. Introduced BinlogClientFactory, which is a singleton and it is used to track all binlogs created over the instance.
  5. Introduced use_binlog_client setting to MaterializedMySQL, which forces to reuse a BinlogClient if it already exists in BinlogClientFactory or create new one. By default, it is disabled.
  6. Introduced max_bytes_in_binlog_queue setting to define the limit of bytes in binlog's queue of events. If bytes in the queue increases this limit, BinlogEventsDispatcher will stop reading new events from source IBinlog until the space for new events will be freed.
  7. Introduced max_milliseconds_to_wait_in_binlog_queue setting to define max ms to wait when the max bytes exceeded.
  8. Introduced max_bytes_in_binlog_dispatcher_buffer setting to define max bytes in the binlog dispatcher's buffer before it is flushed to attached binlogs.
  9. Introduced max_flush_milliseconds_in_binlog_dispatcher setting to define max milliseconds in the binlog dispatcher's buffer to wait before it is flushed to attached binlogs.
  10. Introduced system.mysql_binlogs system table, which shows a list of active binlogs.
  11. Introduced UnparsedRowsEvent and MYSQL_UNPARSED_ROWS_EVENT, which defines that an event is not parsed and should be explicitly parsed later.

There are some additional improvements:

  • Postponing the parsing of the binlog event. For old implementation the parsing of the event is done every time it is received by a database. Now if the database is using The MySQL Binlog Client, the event is parsed only once and in its the sync thread.
  • Since there are many databases for one connection, it needs to deliver all DDL to databases regardless of table filter. But each database can filter them out on the sync thread if it is needed. Not possible to filter tables on Dispatcher's side.
  • Parsing schema/table from DDL which is used later to filter it out.
  • Fixed UInt32 overflow when Pos > End_log_pos.
  • Introduced timestamp to Position, to compare events or seconds behind MySQL if needed. Also added comparison functions.
  • Introduced clearing the pending GTID on reconnect. Author: kangxiang
  • Fixed a bug when it is not possible to apply DDL since syntax error or unsupported SQL.

@larspars is the author of following:

  • GTIDSets::contains() - allows to check if the GTID Sets contains another set.
  • ReplicationHelper - helper to be used in integration tests.
  • shouldReconnectOnException() - Improved the logic to retry on failure. Allows to reconnect if needed f.e.

Changelog category (leave one):

  • New Feature

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

  • Introduced MySQL Binlog Client for MaterializedMySQL: One binlog connection for many databases.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

@valbok
Copy link
Contributor Author

valbok commented Nov 28, 2023

Should it be an Improvement instead of Feature?

@valbok valbok force-pushed the mysql-binlog-client branch 2 times, most recently from af1f249 to 362ada5 Compare November 29, 2023 08:40
@valbok valbok marked this pull request as draft November 29, 2023 10:30
@kssenii kssenii added the can be tested Allows running workflows for external contributors label Nov 29, 2023
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-feature Pull request with new product feature label Nov 29, 2023
@robot-clickhouse-ci-2
Copy link
Contributor

robot-clickhouse-ci-2 commented Nov 29, 2023

This is an automated comment for commit 5c221d1 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
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ 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
Docs checkThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ 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
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
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
Check nameDescriptionStatus
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure

@valbok valbok marked this pull request as ready for review November 29, 2023 11:24
@valbok valbok force-pushed the mysql-binlog-client branch 2 times, most recently from 542de84 to 38f92ce Compare November 29, 2023 12:01
@valbok
Copy link
Contributor Author

valbok commented Nov 29, 2023

unknown file: Failure
C++ exception with description "Cannot open file /src/Databases/MySQL/tests/data/binlog.001390, errno: 2, strerror: No such file or directory" thrown in the test body.

Trying to understand why it could not find the files, could it be because they are not copied/bound?

@valbok valbok force-pushed the mysql-binlog-client branch 8 times, most recently from 02a9fed to 8cc2f46 Compare November 30, 2023 15:50
@valbok
Copy link
Contributor Author

valbok commented Dec 1, 2023

https://s3.amazonaws.com/clickhouse-test-reports/57323/8cc2f462674659212394c4f428b9252d92455c8b/integration_tests__tsan__[6_6].html

test_storage_kafka/test.py::test_system_kafka_consumers_rebalance_mv ERROR

@valbok valbok force-pushed the mysql-binlog-client branch 2 times, most recently from 316ec85 to 24716de Compare December 5, 2023 09:44
@valbok
Copy link
Contributor Author

valbok commented Dec 5, 2023

https://s3.amazonaws.com/clickhouse-test-reports/57323/24716de7c5041f912d775319edc6a7f46dbe366f/fuzzer_astfuzzerasan/report.html

2023.12.05 11:41:43.898675 [ 1332 ] {} <Fatal> BaseDaemon: 9. ./build_docker/./src/Storages/MergeTree/MergeTreeRangeReader.cpp:1246: DB::MergeTreeRangeReader::continueReadingChain(DB::MergeTreeRangeReader::ReadResult const&, unsigned long&) @ 0x00000000321ced64 in /workspace/clickhouse
2023.12.05 11:41:43.997817 [ 1332 ] {} <Fatal> BaseDaemon: 10. ./build_docker/./src/Storages/MergeTree/MergeTreeRangeReader.cpp:991: DB::MergeTreeRangeReader::read(unsigned long, DB::MarkRanges&) @ 0x00000000321ca4ad in /workspace/clickhouse```

@valbok valbok force-pushed the mysql-binlog-client branch 5 times, most recently from 7762822 to fa89fc2 Compare December 8, 2023 08:01
@valbok
Copy link
Contributor Author

valbok commented Dec 12, 2023

All test_materialized_mysql_database tests passed
@kssenii

@kssenii kssenii self-assigned this Dec 12, 2023
@valbok valbok force-pushed the mysql-binlog-client branch 4 times, most recently from a541bee to 089e3e5 Compare December 14, 2023 07:48
@valbok valbok force-pushed the mysql-binlog-client branch 5 times, most recently from b956dfc to dc7bd4c Compare December 27, 2023 13:50
@valbok valbok force-pushed the mysql-binlog-client branch 2 times, most recently from d97c614 to bef8514 Compare December 28, 2023 12:42
src/Databases/MySQL/MySQLBinlog.cpp Outdated Show resolved Hide resolved
src/Databases/MySQL/MySQLBinlog.cpp Outdated Show resolved Hide resolved
src/Databases/MySQL/MySQLBinlog.cpp Outdated Show resolved Hide resolved
src/Databases/MySQL/MySQLBinlog.cpp Outdated Show resolved Hide resolved
src/Databases/MySQL/MySQLBinlog.cpp Outdated Show resolved Hide resolved
src/Databases/MySQL/MySQLBinlog.cpp Outdated Show resolved Hide resolved
src/Databases/MySQL/MySQLBinlog.cpp Outdated Show resolved Hide resolved
src/Databases/MySQL/MySQLBinlog.cpp Outdated Show resolved Hide resolved
src/Databases/MySQL/MySQLBinlogClientCatalog.h Outdated Show resolved Hide resolved
src/Databases/MySQL/MySQLBinlogEventsDispatcher.cpp Outdated Show resolved Hide resolved
@valbok valbok force-pushed the mysql-binlog-client branch 6 times, most recently from 95eb2af to f818bb9 Compare January 3, 2024 12:07
One binlog connection for many databases.

Suggesting to disable this feature by default for now. It should be explicitly enabled by SETTINGS use_binlog_client=1.
But if you would permanently enable it in MaterializedMySQLSettings, it should keep old behavior and all tests should pass too.

1. Introduced `IBinlog` and its impl to read the binlog events from socket - `BinlogFromSocket`, or file - `BinlogFromFile`. Based on prev impl of `EventBase` and the same old binlog parsers. It fully keeps BC with old version. Fixed `./check-mysql-binlog` to test new impl.
2. Introduced `BinlogEventsDispatcher`, it reads the event from the source `IBinlog` and sends it to currently attached `IBinlog` instances.
3. Introduced `BinlogClient`, which is used to group a list of `BinlogEventsDispatcher` by MySQL binlog connection which is defined by `user:password@host:port`. All dispatchers with the same binlog position should be merged to one.
4. Introduced `BinlogClientFactory`, which is a singleton and it is used to track all binlogs created over the instance.
5. Introduced `use_binlog_client` setting to `MaterializedMySQL`, which forces to reuse a `BinlogClient` if it already exists in `BinlogClientCatalog` or create new one. By default, it is disabled.
6. Introduced `max_bytes_in_binlog_queue` setting to define the limit of bytes in binlog's queue of events. If bytes in the queue increases this limit, `BinlogEventsDispatcher` will stop reading new events from source `IBinlog` until the space for new events will be freed.
7. Introduced `max_milliseconds_to_wait_in_binlog_queue` setting to define max ms to wait when the max bytes exceeded.
7. Introduced `max_milliseconds_to_wait_in_binlog_queue` setting to define max ms to wait when the max bytes exceeded.
8. Introduced `max_bytes_in_binlog_dispatcher_buffer` setting to define max bytes in the binlog dispatcher's buffer before it is flushed to attached binlogs.
9. Introduced `max_flush_milliseconds_in_binlog_dispatcher` setting to define max milliseconds in the binlog dispatcher's buffer to wait before it is flushed to attached binlogs.
10. Introduced `system.mysql_binlogs` system table, which shows a list of active binlogs.
11. Introduced `UnparsedRowsEvent` and `MYSQL_UNPARSED_ROWS_EVENT`, which defines that an event is not parsed and should be explicitly parsed later.
12. Fixed bug when not possible to apply DDL since syntax error or unsupported SQL.

@larspars is the author of following:
`GTIDSets::contains()`
`ReplicationHelper`
`shouldReconnectOnException()`
Copy link
Member

@kssenii kssenii left a comment

Choose a reason for hiding this comment

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

Sorry for the delay - I was on vacation.

@kssenii
Copy link
Member

kssenii commented Jan 10, 2024

Stateless tests (aarch64) — Tests are not finished

#58508

@kssenii kssenii merged commit 1b51710 into ClickHouse:master Jan 10, 2024
264 of 266 checks passed
@valbok valbok deleted the mysql-binlog-client branch January 10, 2024 08:12
@valbok
Copy link
Contributor Author

valbok commented Jan 10, 2024

Thanks

@alexey-milovidov
Copy link
Member

@valbok
Copy link
Contributor Author

valbok commented Jan 30, 2024

@valbok, Hi! A test has failed: https://s3.amazonaws.com/clickhouse-test-reports/59166/383ae86ebb0da8a962521f55b08186331eb0f676/integration_tests__asan__analyzer__[4_6].html

Let's fix this test or revert this PR.

Thanks, will fix that asap. - #59370

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-feature Pull request with new product feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants