Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect catalog change for high throughput #121

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions engine/db/catalog/basic_meta_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ Status BasicMetaImpl::LoadDatabase(const std::string& db_catalog_path, const std
}

databases_[db_name] = db_schema;
db_mutexes_[db_name]; // Initializes a mutex for the new database
loaded_databases_paths_.insert(db_catalog_path);

return Status::OK();
Expand Down Expand Up @@ -304,6 +305,7 @@ Status BasicMetaImpl::DropDatabase(const std::string& db_name) {
}
loaded_databases_paths_.erase(path);
databases_.erase(db_name);
db_mutexes_.erase(db_name); // Remove mutex for the dropped database
return Status::OK();
}

Expand Down Expand Up @@ -445,6 +447,7 @@ Status ValidateSchema(TableSchema& table_schema, std::vector<EmbeddingModel> &em
}

Status BasicMetaImpl::CreateTable(const std::string& db_name, TableSchema& table_schema, size_t& table_id) {
std::lock_guard<std::mutex> lock(db_mutexes_[db_name]); // Acquire lock for this database
// Table name cannot be duplicated.
bool has_table = false;
auto status = HasTable(db_name, table_schema.name_, has_table);
Expand Down Expand Up @@ -519,6 +522,7 @@ Status BasicMetaImpl::GetTable(const std::string& db_name, const std::string& ta
}

Status BasicMetaImpl::DropTable(const std::string& db_name, const std::string& table_name) {
std::lock_guard<std::mutex> lock(db_mutexes_[db_name]); // Acquire lock for this database
auto it = databases_.find(db_name);
if (it == databases_.end()) {
return Status(DB_NOT_FOUND, "Database not found: " + db_name);
Expand Down
3 changes: 2 additions & 1 deletion engine/db/catalog/basic_meta_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <atomic>
#include <unordered_map>
#include <unordered_set>

#include <mutex>
#include "db/catalog/meta.hpp"
#include "services/embedding_service.hpp"

Expand Down Expand Up @@ -42,6 +42,7 @@ class BasicMetaImpl : public Meta {
void InjectEmbeddingService(std::shared_ptr<vectordb::engine::EmbeddingService> embedding_service) override;

private:
std::unordered_map<std::string, std::mutex> db_mutexes_; // Map to hold a mutex for each database
std::unordered_map<std::string, DatabaseSchema> databases_;
std::unordered_set<std::string> loaded_databases_paths_; // We cannot allow loading the same database twice
// If the segment is leader (handle sync to storage) or follower (passively sync from storage)
Expand Down