Skip to content

Commit

Permalink
fix potential data race when report resource group metric (#8235)
Browse files Browse the repository at this point in the history
close #8233
  • Loading branch information
guo-shaoge committed Oct 25, 2023
1 parent 84ba14b commit 57c0c57
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions dbms/src/Common/TiFlashMetrics.h
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

0 comments on commit 57c0c57

Please sign in to comment.