Skip to content

Add connection reestablisher to mysql and postgres dictionary pools - #71101

Merged
nikitamikhaylov merged 14 commits into
masterfrom
imp-mysql-bg-reconnect
Dec 16, 2024
Merged

nikitamikhaylov merged 14 commits into
masterfrom
imp-mysql-bg-reconnect

Conversation

@yakov-olkhovskiy

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Reestablish connection to MySQL and Postgres dictionary replicas in background so it wouldn't delay requests to corresponding dictionaries.

closes https://github.com/ClickHouse/product/issues/1161

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

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

  • Allow: All Required Checks
  • Allow: Stateless tests
  • Allow: Stateful tests
  • Allow: Integration Tests
  • Allow: Performance tests
  • Allow: All Builds
  • Allow: batch 1, 2 for multi-batch jobs
  • Allow: batch 3, 4, 5, 6 for multi-batch jobs

  • Exclude: Style check
  • Exclude: Fast test
  • Exclude: All with ASAN
  • Exclude: All with TSAN, MSAN, UBSAN, Coverage
  • Exclude: All with aarch64, release, debug

  • Run only fuzzers related jobs (libFuzzer fuzzers, AST fuzzers, etc.)
  • Exclude: AST fuzzers

  • Do not test
  • Woolen Wolfdog
  • Upload binaries for special builds
  • Disable merge-commit
  • Disable CI cache

@yakov-olkhovskiy yakov-olkhovskiy changed the title add connection reistablisher to mysql pool Add connection reistablisher to mysql and posgres dictionary pools Oct 27, 2024
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-improvement Pull request with some product improvements label Oct 27, 2024
@robot-ch-test-poll1

robot-ch-test-poll1 commented Oct 27, 2024 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit 5427be7 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
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
Stateless testsRuns stateless 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

@yakov-olkhovskiy yakov-olkhovskiy changed the title Add connection reistablisher to mysql and posgres dictionary pools [WIP] Add connection reistablisher to mysql and posgress dictionary pools Oct 27, 2024
@alexey-milovidov alexey-milovidov changed the title [WIP] Add connection reistablisher to mysql and posgress dictionary pools [WIP] Add connection reistablisher to mysql and postgres dictionary pools Oct 27, 2024
@alexey-milovidov alexey-milovidov changed the title [WIP] Add connection reistablisher to mysql and postgres dictionary pools [WIP] Add connection reestablisher to mysql and postgres dictionary pools Oct 29, 2024
@yakov-olkhovskiy yakov-olkhovskiy changed the title [WIP] Add connection reestablisher to mysql and postgres dictionary pools Add connection reestablisher to mysql and postgres dictionary pools Nov 4, 2024
@nikitamikhaylov nikitamikhaylov self-assigned this Nov 14, 2024
- `table` – Name of the table and schema if exists.
- `connection_string` – Connection string.
- `invalidate_query` – Query for checking the dictionary status. Optional parameter. Read more in the section [Refreshing dictionary data using LIFETIME](#refreshing-dictionary-data-using-lifetime).
- `bg_reconnect` – Reconnect to replica in background if connection fails. Optional parameter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
- `bg_reconnect` – Reconnect to replica in background if connection fails. Optional parameter.
- `background_reconnect` – Reconnect to replica in background if connection fails. Optional parameter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there any specific reason why the name should be shortened? The full one looks cleaner. Also, please change the rest of the usage.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

no specific reason I think, we can change it...

Comment thread src/Common/mysqlxx/PoolWithFailover.cpp Outdated
@@ -1,3 +1,5 @@
#include "Common/Exception.h"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
#include "Common/Exception.h"
#include <Common/Exception.h>

if (dns_cache_updater)
dns_cache_updater->start();

auto replicas_reconnector = ReplicasReconnector::init(global_context);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Passing the whole global_context looks a bit scary. Maybe we can create a task in BackgroundSchedulePool and pass it to the ReplicasReconnector along with a server setting?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

we don't hold on global context actually - ReplicasReconnector::init is a static function initializing a singleton (probably I've invented an "initializable singleton" pattern here :) ) - inside this function ReplicasReconnector constructor is called which uses global_context to create a task, that's all. Regarding saving bg_reconnect_mysql_dict_interval server setting I'm not sure, because config can be changed run-time - then we will need to synchronize possible change of this setting from config update task - which seems inconvenient...

Comment thread src/Common/ReplicasReconnector.h Outdated
Comment on lines +46 to +47
std::unique_ptr<ReplicasReconnector> ret(new ReplicasReconnector(context));
instance_ptr(ret.get());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isn't it a memory leak or double-free? The ret object is being destroyed after leaving this function scope.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ReplicasReconnector::init() is returning this std::unique_ptr<ReplicasReconnector> to Server::main local variable, so it undergoes RVO copy elision and nothing is destroyed at this point. And local variable in Server::main keeps this singleton alive during server's lifetime and destroys it when Server::main is finished - it's the whole point of "initializable singleton" - to keep its lifetime under control.

Comment thread src/Common/ReplicasReconnector.h Outdated
Comment on lines +74 to +80
static ReplicasReconnector * instance_ptr = nullptr;

if (deinitialize)
{
instance_ptr = nullptr;
return instance_ptr;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can make is a class field and move the deinitialization to a separate method.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It seems more convenient to keep it centralized and compact - it serves 3 functions - singleton initialization, deinitialization and access, and this function is never accessed outside... but probably having less "if's" is better and less obfuscated... I'll take a look...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

Comment thread src/Common/ReplicasReconnector.h Outdated
for (auto it = reconnectors.cbegin(); !emergency_stop && it != reconnectors.end();)
{
lock.unlock();
bool res = (*it)(interval_milliseconds);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

try ... catch to be safe?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

oh, yeah, or we're risking killing the thread... thanks

@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue Dec 16, 2024
Merged via the queue into master with commit 0a42c6d Dec 16, 2024
@nikitamikhaylov
nikitamikhaylov deleted the imp-mysql-bg-reconnect branch December 16, 2024 12:06
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Dec 16, 2024
@Algunenano

Copy link
Copy Markdown
Member

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

Labels

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