Skip to content

Commit

Permalink
Fix an race condition during multiple DB opening (facebook#8574)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jay-zhuang authored and facebook-github-bot committed Jul 22, 2021
1 parent 84eef26 commit c4a503f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/rocksdb/utilities/object_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

#include <functional>
#include <memory>
#include <mutex>
#include <regex>
#include <string>
#include <unordered_map>
#include <vector>

#include "rocksdb/status.h"

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

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

Expand Down
4 changes: 4 additions & 0 deletions utilities/object_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace ROCKSDB_NAMESPACE {
// Otherwise, nullptr is returned
const ObjectLibrary::Entry *ObjectLibrary::FindEntry(
const std::string &type, const std::string &name) const {
std::unique_lock<std::mutex> lock(mu_);
auto entries = entries_.find(type);
if (entries != entries_.end()) {
for (const auto &entry : entries->second) {
Expand All @@ -28,11 +29,13 @@ const ObjectLibrary::Entry *ObjectLibrary::FindEntry(

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

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

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

0 comments on commit c4a503f

Please sign in to comment.