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

Store clusters from ClusterDiscovery in separate map #48795

Merged
merged 4 commits into from
Apr 20, 2023

Conversation

vdimir
Copy link
Member

@vdimir vdimir commented Apr 14, 2023

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Ref #47176

Context:

Clusters defined in discovery mode were stored in shared->clusters with others. But the variable shared->clusters is assigned in several places (e.g., on config reloading), and it's challenging to maintain it in the correct state. Current PR moves data structure to store auto-discovered clusters out of shared->clusters, similar to how it's done for the replicated database.

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-not-for-changelog This PR should not be mentioned in the changelog label Apr 14, 2023
@vdimir vdimir marked this pull request as ready for review April 17, 2023 09:00
auto clusters = getClustersImpl()->getContainer();
if (shared->cluster_discovery)
{
for (const auto & [name, cluster] : shared->cluster_discovery->getClusters())
Copy link
Member

Choose a reason for hiding this comment

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

oh no, you take a lock each time and possibly get a different cluster map each time?

Copy link
Member Author

Choose a reason for hiding this comment

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

Inside shared->cluster_discovery, we have a mutex to protect cluster_impls. It can be updated anytime (on the server added to/removed from the cluster).
As for shared->cluster_discovery itself, it initialized once at Context::startClusterDiscovery, but maybe it's worth protecting it with shared->clusters_mutex also (as in tryGetCluster), because I'm not really sure that getClusters can't be called during server startup.

Copy link
Member

Choose a reason for hiding this comment

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

It can be updated anytime (on the server added to/removed from the cluster).

Yes, but that's a problem when iterating no?
Each loop iteration you will end up with different possible iterators if map changes.

Copy link
Member Author

Choose a reason for hiding this comment

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

I assumed that

for (const auto & [name, cluster] : shared->cluster_discovery->getClusters())
{
...

is the same as

const auto & tmp = shared->cluster_discovery->getClusters();
for (const auto & [name, cluster] : tmp)
{
...

and getClusters is just called once.

https://godbolt.org/z/sj6Ydc7zT

But we can change the code to be like the second snippet to ensure it.

Suggested change
for (const auto & [name, cluster] : shared->cluster_discovery->getClusters())
const auto & cluster_discovery_map = shared->cluster_discovery->getClusters();
for (const auto & [name, cluster] : cluster_discovery_map)

Copy link
Member

Choose a reason for hiding this comment

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

there is a cool tool for things like this, C++ Insights
your example

But yeah, I'm still afraid to have code that relies on such assumptions because it's not obvious

@antonio2368 antonio2368 self-assigned this Apr 17, 2023
@vdimir vdimir force-pushed the vdimir/fix-cluster-discovery-47176 branch from 4fcd435 to b31f696 Compare April 19, 2023 14:44
@vdimir
Copy link
Member Author

vdimir commented Apr 19, 2023

Rerun CI to check integration tests broken by bad docker image

@vdimir vdimir merged commit ad516d2 into master Apr 20, 2023
138 checks passed
@vdimir vdimir deleted the vdimir/fix-cluster-discovery-47176 branch April 20, 2023 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-not-for-changelog This PR should not be mentioned in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants