Skip to content

Commit

Permalink
fix vector index coredump (TuGraph-family#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljcui authored Sep 23, 2024
1 parent 0579709 commit 334c4fc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/core/index_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ IndexManager::IndexManager(KvTransaction& txn, SchemaManager* v_schema_manager,
Schema* schema = v_schema_manager->GetSchema(idx.label);
FMA_DBG_ASSERT(schema);
FMA_DBG_ASSERT(schema->DetachProperty());
LOG_INFO() << FMA_FMT("start building vertex index for {}:{} in detached model",
LOG_INFO() << FMA_FMT("start building vertex vector index for {}:{} in detached model",
idx.label, idx.field);
const _detail::FieldExtractor* extractor = schema->GetFieldExtractor(idx.field);
FMA_DBG_ASSERT(extractor);
Expand Down Expand Up @@ -128,7 +128,7 @@ IndexManager::IndexManager(KvTransaction& txn, SchemaManager* v_schema_manager,
kv_iter.reset();
LOG_DEBUG() << "index count: " << count;
schema->MarkVectorIndexed(extractor->GetFieldId(), vsag_index.release());
LOG_INFO() << FMA_FMT("end building vector index for {}:{} in detached model",
LOG_INFO() << FMA_FMT("end building vertex vector index for {}:{} in detached model",
idx.label, idx.field);
} else {
LOG_ERROR() << "Unknown index type: " << index_name;
Expand Down
2 changes: 1 addition & 1 deletion src/core/vector_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class VectorIndex {
const std::vector<int64_t>& vids, int64_t num_vectors) = 0;

// build index
virtual bool Build() = 0;
virtual void Build() = 0;

// serialize index
virtual std::vector<uint8_t> Save() = 0;
Expand Down
6 changes: 2 additions & 4 deletions src/core/vsag_hnsw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void HNSW::Add(const std::vector<std::vector<float>>& vectors,
}
}

bool HNSW::Build() {
void HNSW::Build() {
nlohmann::json hnsw_parameters{
{"max_degree", index_spec_[0]},
{"ef_construction", index_spec_[1]}
Expand All @@ -81,10 +81,8 @@ bool HNSW::Build() {
createindex_ = std::move(temp.value());
index_ = createindex_.get();
} else {
LOG_WARN() << FMA_FMT("create vsag index error: {}", temp.error().message);
return false;
THROW_CODE(VectorIndexException, temp.error().message);
}
return true;
}

// serialize index
Expand Down
2 changes: 1 addition & 1 deletion src/core/vsag_hnsw.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class HNSW : public VectorIndex {
const std::vector<int64_t>& vids, int64_t num_vectors) override;

// build index
bool Build() override;
void Build() override;

// serialize index
std::vector<uint8_t> Save() override;
Expand Down
16 changes: 8 additions & 8 deletions src/cypher/procedure/procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4192,14 +4192,14 @@ void VectorFunc::AddVertexVectorIndex(RTContext *ctx, const cypher::Record *reco
if (parameter.count("hnsm_m")) {
hnsm_m = (int)parameter.at("hnsm_m").AsInt64();
}
CYPHER_ARG_CHECK((hnsm_m < 2048 && hnsm_m > 2),
"M should be an integer in the range (2,2048)");
CYPHER_ARG_CHECK((hnsm_m <= 64 && hnsm_m >= 5),
"hnsm.m should be an integer in the range [5, 64]");
int hnsm_ef_construction = 100;
if (parameter.count("hnsm_ef_construction")) {
hnsm_ef_construction = (int)parameter.at("hnsm_ef_construction").AsInt64();
}
CYPHER_ARG_CHECK((hnsm_ef_construction < 65536 && hnsm_ef_construction > 1),
"efConstruction should be an integer in the range (1,65536)");
CYPHER_ARG_CHECK((hnsm_ef_construction <= 1000 && hnsm_ef_construction >= hnsm_m),
"hnsm.efConstruction should be an integer in the range [hnsm.m,1000]");
std::vector<int> index_spec = {hnsm_m, hnsm_ef_construction};
auto ac_db = ctx->galaxy_->OpenGraph(ctx->user_, ctx->graph_);
bool success = ac_db.AddVectorIndex(true, label, field, index_type,
Expand Down Expand Up @@ -4301,13 +4301,13 @@ void VectorFunc::VertexVectorIndexQuery(RTContext *ctx, const cypher::Record *re
if (parameter.count("top_k")) {
top_k = parameter.at("top_k").AsInt64();
}
int ef_search = 100;
CYPHER_ARG_CHECK((top_k >= 1), "top_k must be greater than 0");
int ef_search = 200;
if (parameter.count("hnsw_ef_search")) {
ef_search = parameter.at("hnsw_ef_search").AsInt64();
}
CYPHER_ARG_CHECK((ef_search <= 65536 && ef_search >= top_k),
"Please check the parameter,"
"ef should be an integer in the range [top_k, 65536]");
CYPHER_ARG_CHECK((ef_search <= 1000 && ef_search >= 1),
"hnsw.ef_search should be an integer in the range [1, 1000]");
auto res = index->Search(query_vector, top_k, ef_search);
for (auto& item : res) {
Record r;
Expand Down
12 changes: 6 additions & 6 deletions test/test_vsag_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ class TestVsag : public TuGraphTest {
void TearDown() override {}
};

TEST_F(TestVsag, BuildIndex) { ASSERT_TRUE(vector_index->Build()); }
TEST_F(TestVsag, BuildIndex) { EXPECT_NO_THROW(vector_index->Build()); }

TEST_F(TestVsag, AddVectors) {
ASSERT_TRUE(vector_index->Build());
EXPECT_NO_THROW(vector_index->Build());
EXPECT_NO_THROW(vector_index->Add(vectors, vids, num_vectors));
}

TEST_F(TestVsag, SearchIndex) {
ASSERT_TRUE(vector_index->Build());
EXPECT_NO_THROW(vector_index->Build());
EXPECT_NO_THROW(vector_index->Add(vectors, vids, num_vectors));
std::vector<float> query(vectors[0].begin(), vectors[0].end());
std::vector<std::pair<int64_t, float>> ret;
Expand All @@ -75,12 +75,12 @@ TEST_F(TestVsag, SearchIndex) {
}

TEST_F(TestVsag, SaveAndLoadIndex) {
ASSERT_TRUE(vector_index->Build());
EXPECT_NO_THROW(vector_index->Build());
EXPECT_NO_THROW(vector_index->Add(vectors, vids, num_vectors));
std::vector<uint8_t> serialized_index = vector_index->Save();
ASSERT_FALSE(serialized_index.empty());
lgraph::HNSW vector_index_loaded("label", "name", "l2", "hnsw", dim, index_spec);
ASSERT_TRUE(vector_index_loaded.Build());
EXPECT_NO_THROW(vector_index_loaded.Build());
vector_index_loaded.Load(serialized_index);
std::vector<float> query(vectors[0].begin(), vectors[0].end());
auto ret = vector_index_loaded.Search(query, 10, 10);
Expand All @@ -89,7 +89,7 @@ TEST_F(TestVsag, SaveAndLoadIndex) {
}

TEST_F(TestVsag, DeleteVectors) {
ASSERT_TRUE(vector_index->Build());
EXPECT_NO_THROW(vector_index->Build());
EXPECT_NO_THROW(vector_index->Add(vectors, vids, num_vectors));
std::vector<int64_t> delete_vids = {vids[0], vids[1]};
EXPECT_NO_THROW(vector_index->Add({}, delete_vids, 0));
Expand Down

0 comments on commit 334c4fc

Please sign in to comment.