From 9870704420a0a49ca64fe2fb0137e67caff378c1 Mon Sep 17 00:00:00 2001 From: sdong Date: Mon, 20 Jul 2020 12:09:35 -0700 Subject: [PATCH] Fix a minor data race in stats dumping threads initialization (#7151) Summary: https://github.com/facebook/rocksdb/pull/7145 creates a minor data race against the stat creation counter. Turn it to atomic. Pull Request resolved: https://github.com/facebook/rocksdb/pull/7151 Test Plan: Run the test. Reviewed By: ajkr Differential Revision: D22631014 fbshipit-source-id: c6fb69ac5b9df7139795dacea5ce9fb9fd3278d7 --- db/db_impl/db_impl.cc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/db/db_impl/db_impl.cc b/db/db_impl/db_impl.cc index cb5308127..36fe87912 100644 --- a/db/db_impl/db_impl.cc +++ b/db/db_impl/db_impl.cc @@ -689,14 +689,13 @@ void DBImpl::StartTimedTasks() { // threads dumping at once, which causes severe lock contention in // jemalloc. Ensure successive `DB::Open()`s are staggered by at least // one second in the common case. - static uint64_t stats_dump_threads_started = 0; + static std::atomic stats_dump_threads_started(0); thread_dump_stats_.reset(new ROCKSDB_NAMESPACE::RepeatableThread( [this]() { DBImpl::DumpStats(); }, "dump_st", env_, static_cast(stats_dump_period_sec) * kMicrosInSecond, - stats_dump_threads_started % + stats_dump_threads_started.fetch_add(1) % static_cast(stats_dump_period_sec) * kMicrosInSecond)); - ++stats_dump_threads_started; } } stats_persist_period_sec = mutable_db_options_.stats_persist_period_sec; @@ -1097,15 +1096,14 @@ Status DBImpl::SetDBOptions( // severe lock contention in jemalloc. Ensure successive enabling of // `stats_dump_period_sec` are staggered by at least one second in the // common case. - static uint64_t stats_dump_threads_started = 0; + static std::atomic stats_dump_threads_started(0); thread_dump_stats_.reset(new ROCKSDB_NAMESPACE::RepeatableThread( [this]() { DBImpl::DumpStats(); }, "dump_st", env_, static_cast(new_options.stats_dump_period_sec) * kMicrosInSecond, - stats_dump_threads_started % + stats_dump_threads_started.fetch_add(1) % static_cast(new_options.stats_dump_period_sec) * kMicrosInSecond)); - ++stats_dump_threads_started; } else { thread_dump_stats_.reset(); } -- GitLab