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

fix potential data race when report resource group metric #8235

Merged
merged 7 commits into from
Oct 25, 2023
12 changes: 10 additions & 2 deletions dbms/src/Common/TiFlashMetrics.h
JaySon-Huang marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <ext/scope_guard.h>
#include <mutex>
#include <shared_mutex>

// to make GCC 11 happy
#include <cassert>
Expand Down Expand Up @@ -863,10 +864,16 @@ struct MetricFamily
T & get(size_t idx = 0) { return *(metrics[idx]); }
T & get(size_t idx, const String & resource_group_name)
{
if (metrics_map.find(resource_group_name) == metrics_map.end())
{
addMetricsForResourceGroup(resource_group_name);
std::shared_lock lock(mu);
if (metrics_map.find(resource_group_name) != metrics_map.end())
return *(metrics_map[resource_group_name][idx]);
}

std::lock_guard lock(mu);
if (metrics_map.find(resource_group_name) == metrics_map.end())
addMetricsForResourceGroup(resource_group_name);

return *(metrics_map[resource_group_name][idx]);
}

Expand All @@ -893,6 +900,7 @@ struct MetricFamily
prometheus::Family<T> * store_family;
std::vector<MetricArgType> store_args;
// <resource_group_name, metrics>
std::shared_mutex mu;
std::unordered_map<String, std::vector<T *>> metrics_map;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe change it to a better name?
Maybe resource_group_metrics_map and resource_group_metrics_mu

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

};

Expand Down