Skip to content

Commit c4a503f

Browse files
jay-zhuangfacebook-github-bot
authored andcommitted
Fix an race condition during multiple DB opening (facebook#8574)
Summary: ObjectLibrary is shared between multiple DB instances, the Register() could have race condition. Pull Request resolved: facebook#8574 Test Plan: pass the failed test Reviewed By: ajkr Differential Revision: D29855096 Pulled By: jay-zhuang fbshipit-source-id: 541eed0bd495d2c963d858d81e7eabf1ba16153c
1 parent 84eef26 commit c4a503f

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

include/rocksdb/utilities/object_registry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
#include <functional>
1111
#include <memory>
12+
#include <mutex>
1213
#include <regex>
1314
#include <string>
1415
#include <unordered_map>
1516
#include <vector>
17+
1618
#include "rocksdb/status.h"
1719

1820
namespace ROCKSDB_NAMESPACE {
@@ -109,6 +111,8 @@ class ObjectLibrary {
109111
// Adds the input entry to the list for the given type
110112
void AddEntry(const std::string& type, std::unique_ptr<Entry>& entry);
111113

114+
// Protects the entry map
115+
mutable std::mutex mu_;
112116
// ** FactoryFunctions for this loader, organized by type
113117
std::unordered_map<std::string, std::vector<std::unique_ptr<Entry>>> entries_;
114118

utilities/object_registry.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace ROCKSDB_NAMESPACE {
1515
// Otherwise, nullptr is returned
1616
const ObjectLibrary::Entry *ObjectLibrary::FindEntry(
1717
const std::string &type, const std::string &name) const {
18+
std::unique_lock<std::mutex> lock(mu_);
1819
auto entries = entries_.find(type);
1920
if (entries != entries_.end()) {
2021
for (const auto &entry : entries->second) {
@@ -28,11 +29,13 @@ const ObjectLibrary::Entry *ObjectLibrary::FindEntry(
2829

2930
void ObjectLibrary::AddEntry(const std::string &type,
3031
std::unique_ptr<Entry> &entry) {
32+
std::unique_lock<std::mutex> lock(mu_);
3133
auto &entries = entries_[type];
3234
entries.emplace_back(std::move(entry));
3335
}
3436

3537
size_t ObjectLibrary::GetFactoryCount(size_t *types) const {
38+
std::unique_lock<std::mutex> lock(mu_);
3639
*types = entries_.size();
3740
size_t factories = 0;
3841
for (const auto &e : entries_) {
@@ -42,6 +45,7 @@ size_t ObjectLibrary::GetFactoryCount(size_t *types) const {
4245
}
4346

4447
void ObjectLibrary::Dump(Logger *logger) const {
48+
std::unique_lock<std::mutex> lock(mu_);
4549
for (const auto &iter : entries_) {
4650
ROCKS_LOG_HEADER(logger, " Registered factories for type[%s] ",
4751
iter.first.c_str());

0 commit comments

Comments
 (0)