diff --git a/dbms/src/Common/TiFlashMetrics.h b/dbms/src/Common/TiFlashMetrics.h index c4eb2a94f9e..e7ff836d7dd 100644 --- a/dbms/src/Common/TiFlashMetrics.h +++ b/dbms/src/Common/TiFlashMetrics.h @@ -28,6 +28,7 @@ #include #include +#include // to make GCC 11 happy #include @@ -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: @@ -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 metrics; prometheus::Family * store_family; std::vector store_args; // - std::unordered_map> metrics_map; + std::shared_mutex resource_group_metrics_mu; + std::unordered_map> resource_group_metrics_map; }; /// Centralized registry of TiFlash metrics.