Backport #72518 to 24.10: Fix data race in ProfileEvents::Counters::setParent().

This commit is contained in:
robot-clickhouse 2024-12-17 15:10:20 +00:00
parent dd0907fc77
commit f8fa3557af
2 changed files with 15 additions and 4 deletions

View File

@ -950,6 +950,15 @@ Counters::Counters(VariableContext level_, Counters * parent_)
counters = counters_holder.get();
}
Counters::Counters(Counters && src) noexcept
: counters(std::exchange(src.counters, nullptr))
, counters_holder(std::move(src.counters_holder))
, parent(src.parent.exchange(nullptr))
, trace_profile_events(src.trace_profile_events)
, level(src.level)
{
}
void Counters::resetCounters()
{
if (counters)
@ -961,7 +970,7 @@ void Counters::resetCounters()
void Counters::reset()
{
parent = nullptr;
setParent(nullptr);
resetCounters();
}

View File

@ -60,7 +60,7 @@ namespace ProfileEvents
Counter * counters = nullptr;
std::unique_ptr<Counter[]> counters_holder;
/// Used to propagate increments
Counters * parent = nullptr;
std::atomic<Counters *> parent = {};
bool trace_profile_events = false;
public:
@ -74,6 +74,8 @@ namespace ProfileEvents
explicit Counters(Counter * allocated_counters) noexcept
: counters(allocated_counters), parent(nullptr), level(VariableContext::Global) {}
Counters(Counters && src) noexcept;
Counter & operator[] (Event event)
{
return counters[event];
@ -114,13 +116,13 @@ namespace ProfileEvents
/// Get parent (thread unsafe)
Counters * getParent()
{
return parent;
return parent.load(std::memory_order_relaxed);
}
/// Set parent (thread unsafe)
void setParent(Counters * parent_)
{
parent = parent_;
parent.store(parent_, std::memory_order_relaxed);
}
void setTraceProfileEvents(bool value)