Finer locking during collection creation.

This commit is contained in:
Kishore Nallan 2022-07-19 16:33:27 +05:30
parent cbdae4b192
commit bb5d6a308d
2 changed files with 31 additions and 0 deletions

View File

@ -292,6 +292,7 @@ Option<Collection*> CollectionManager::create_collection(const std::string& name
const std::string& fallback_field_type,
const std::vector<std::string>& symbols_to_index,
const std::vector<std::string>& token_separators) {
std::unique_lock lock(mutex);
if(store->contains(Collection::get_meta_key(name))) {
return Option<Collection*>(409, std::string("A collection with name `") + name + "` already exists.");
@ -340,6 +341,7 @@ Option<Collection*> CollectionManager::create_collection(const std::string& name
return Option<Collection*>(500, "Could not write to on-disk storage.");
}
lock.unlock();
add_to_collections(new_collection);
return Option<Collection*>(new_collection);

View File

@ -104,6 +104,35 @@ TEST_F(CollectionManagerTest, CollectionCreation) {
ASSERT_EQ("1", next_collection_id);
}
TEST_F(CollectionManagerTest, ParallelCollectionCreation) {
std::vector<std::thread> threads;
for(size_t i = 0; i < 10; i++) {
threads.emplace_back([i, &collectionManager = collectionManager]() {
nlohmann::json coll_json = R"({
"name": "parcoll",
"fields": [
{"name": "title", "type": "string"}
]
})"_json;
coll_json["name"] = coll_json["name"].get<std::string>() + std::to_string(i+1);
auto coll_op = collectionManager.create_collection(coll_json);
ASSERT_TRUE(coll_op.ok());
});
}
for(auto& thread : threads){
thread.join();
}
int64_t prev_id = INT32_MAX;
for(auto coll: collectionManager.get_collections()) {
// collections are sorted by ID, in descending order
ASSERT_TRUE(coll->get_collection_id() < prev_id);
prev_id = coll->get_collection_id();
}
}
TEST_F(CollectionManagerTest, ShouldInitCollection) {
nlohmann::json collection_meta1 =
nlohmann::json::parse("{\"name\": \"foobar\", \"id\": 100, \"fields\": [{\"name\": \"org\", \"type\": "