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
18 changes: 13 additions & 5 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,11 +864,17 @@ 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(resource_group_metrics_mu);
if (resource_group_metrics_map.find(resource_group_name) != resource_group_metrics_map.end())
return *(resource_group_metrics_map[resource_group_name][idx]);
}
return *(metrics_map[resource_group_name][idx]);

std::lock_guard lock(resource_group_metrics_mu);
if (resource_group_metrics_map.find(resource_group_name) == resource_group_metrics_map.end())
addMetricsForResourceGroup(resource_group_name);

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

private:
Expand All @@ -886,14 +893,15 @@ struct MetricFamily
auto & metric = MetricTrait::add(*store_family, resource_group_name, MetricArgType{});
metrics_temp.emplace_back(&metric);
}
metrics_map[resource_group_name] = metrics_temp;
resource_group_metrics_map[resource_group_name] = metrics_temp;
}

std::vector<T *> metrics;
prometheus::Family<T> * store_family;
std::vector<MetricArgType> store_args;
// <resource_group_name, metrics>
std::unordered_map<String, std::vector<T *>> metrics_map;
std::shared_mutex resource_group_metrics_mu;
std::unordered_map<String, std::vector<T *>> resource_group_metrics_map;
};

/// Centralized registry of TiFlash metrics.
Expand Down