Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Add test & fine grain locking for collection manager

ozanarmagan0dad1a369f40
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 6a3e8492..98cbc50b 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -534,12 +534,12 @@ Option<nlohmann::json> CollectionManager::drop_collection(const std::string& col
const auto& embedding_fields = collection->get_embedding_fields();
+ u_lock.unlock();
for(const auto& embedding_field : embedding_fields) {
const auto& model_name = embedding_field.embed[fields::model_config]["model_name"].get<std::string>();
process_embedding_field_delete(model_name);
}
- u_lock.unlock();
// don't hold any collection manager locks here, since this can take some time
delete collection;
@@ -1565,8 +1565,7 @@ Option<Collection*> CollectionManager::clone_collection(const string& existing_n
}
void CollectionManager::process_embedding_field_delete(const std::string& model_name) {
- // can't have a shared lock here
- // because we will be already acquiring a lock on collection manager if we are deleting a collection
+ std::shared_lock lock(mutex);
bool found = false;
for(const auto& collection: collections) {
diff --git a/test/collection_vector_search_test.cpp b/test/collection_vector_search_test.cpp
index 9d329374..19074c8d 100644
--- a/test/collection_vector_search_test.cpp
+++ b/test/collection_vector_search_test.cpp
@@ -2398,6 +2398,83 @@ TEST_F(CollectionVectorTest, TestUnloadingModelsOnDrop) {
drop_op = coll->alter(drop_schema);
ASSERT_TRUE(drop_op.ok());
+ text_embedders = TextEmbedderManager::get_instance()._get_text_embedders();
+ ASSERT_EQ(0, text_embedders.size());
+}
+
+
+TEST_F(CollectionVectorTest, TestUnloadModelsCollectionHaveTwoEmbeddingField) {
+ nlohmann::json actual_schema = R"({
+ "name": "test",
+ "fields": [
+ {
+ "name": "title",
+ "type": "string"
+ },
+ {
+ "name": "title_vec",
+ "type": "float[]",
+ "embed": {
+ "from": [
+ "title"
+ ],
+ "model_config": {
+ "model_name": "ts/e5-small"
+ }
+ }
+ },
+ {
+ "name": "title_vec2",
+ "type": "float[]",
+ "embed": {
+ "from": [
+ "title"
+ ],
+ "model_config": {
+ "model_name": "ts/e5-small"
+ }
+ }
+ }
+ ]
+ })"_json;
+
+ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");
+
+ auto schema = actual_schema;
+ auto collection_create_op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(collection_create_op.ok());
+
+ auto coll = collection_create_op.get();
+ auto text_embedders = TextEmbedderManager::get_instance()._get_text_embedders();
+ ASSERT_EQ(1, text_embedders.size());
+
+ nlohmann::json drop_schema = R"({
+ "fields": [
+ {
+ "name": "title_vec",
+ "drop": true
+ }
+ ]
+ })"_json;
+
+ auto drop_op = coll->alter(drop_schema);
+ ASSERT_TRUE(drop_op.ok());
+
+ text_embedders = TextEmbedderManager::get_instance()._get_text_embedders();
+ ASSERT_EQ(1, text_embedders.size());
+
+ drop_schema = R"({
+ "fields": [
+ {
+ "name": "title_vec2",
+ "drop": true
+ }
+ ]
+ })"_json;
+
+ drop_op = coll->alter(drop_schema);
+ ASSERT_TRUE(drop_op.ok());
+
text_embedders = TextEmbedderManager::get_instance()._get_text_embedders();
ASSERT_EQ(0, text_embedders.size());
}
\ No newline at end of file