From e80c45aa259cfc547650266ee37edbf4955a4c35 Mon Sep 17 00:00:00 2001 From: panda-sheep <59197347+panda-sheep@users.noreply.github.com> Date: Tue, 25 Jan 2022 15:04:13 +0800 Subject: [PATCH] Fix create fulltext index failed (#3747) (#3793) * fix bug: the same tagId/edgetype under different spaces, failed to create fulltext indexes * fix bug: the same tagId/edgetype under different spaces, failed to create fulltext indexes Co-authored-by: Doodle <13706157+critical27@users.noreply.github.com> --- .../processors/index/FTIndexProcessor.cpp | 4 +- src/meta/test/IndexProcessorTest.cpp | 70 ++++++++++++++++++- src/meta/test/TestUtils.h | 23 ++++-- 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/src/meta/processors/index/FTIndexProcessor.cpp b/src/meta/processors/index/FTIndexProcessor.cpp index 860e109e023..29389376dc3 100644 --- a/src/meta/processors/index/FTIndexProcessor.cpp +++ b/src/meta/processors/index/FTIndexProcessor.cpp @@ -100,7 +100,9 @@ void CreateFTIndexProcessor::process(const cpp2::CreateFTIndexReq& req) { onFinished(); return; } - if (index.get_depend_schema() == indexItem.get_depend_schema()) { + // Because tagId/edgeType is the space range, judge the spaceId and schemaId + if (index.get_space_id() == indexItem.get_space_id() && + index.get_depend_schema() == indexItem.get_depend_schema()) { LOG(ERROR) << "Depends on the same schema , index : " << indexName; handleErrorCode(nebula::cpp2::ErrorCode::E_EXISTED); onFinished(); diff --git a/src/meta/test/IndexProcessorTest.cpp b/src/meta/test/IndexProcessorTest.cpp index 683b0bf212a..0b971b0024c 100644 --- a/src/meta/test/IndexProcessorTest.cpp +++ b/src/meta/test/IndexProcessorTest.cpp @@ -1585,6 +1585,15 @@ void mockSchemas(kvstore::KVStore* kv) { schemas.emplace_back(MetaServiceUtils::schemaEdgeKey(1, edgeType, ver), MetaServiceUtils::schemaVal("test_edge", srcsch)); + // space 2 + schemas.emplace_back(MetaServiceUtils::indexTagKey(2, "test_tag"), tagIdVal); + schemas.emplace_back(MetaServiceUtils::schemaTagKey(2, tagId, ver), + MetaServiceUtils::schemaVal("test_tag", srcsch)); + + schemas.emplace_back(MetaServiceUtils::indexEdgeKey(2, "test_edge"), edgeTypeVal); + schemas.emplace_back(MetaServiceUtils::schemaEdgeKey(2, edgeType, ver), + MetaServiceUtils::schemaVal("test_edge", srcsch)); + folly::Baton baton; kv->asyncMultiPut(0, 0, std::move(schemas), [&](nebula::cpp2::ErrorCode code) { ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, code); @@ -1597,6 +1606,7 @@ TEST(IndexProcessorTest, CreateFTIndexTest) { fs::TempDir rootPath("/tmp/CreateFTIndexTest.XXXXXX"); auto kv = MockCluster::initMetaKV(rootPath.path()); TestUtils::assembleSpace(kv.get(), 1, 1); + TestUtils::assembleSpace(kv.get(), 2, 1, 1, 1, true); mockSchemas(kv.get()); for (auto id : {5, 6}) { // expected error. column col_fixed_string_2 is fixed_string, @@ -1654,7 +1664,7 @@ TEST(IndexProcessorTest, CreateFTIndexTest) { } else { schemaId.set_edge_type(6); } - index.set_space_id(2); + index.set_space_id(3); index.set_depend_schema(std::move(schemaId)); index.set_fields({"col_string"}); req.set_fulltext_index_name("test_ft_index"); @@ -1852,6 +1862,63 @@ TEST(IndexProcessorTest, CreateFTIndexTest) { auto resp = std::move(f).get(); ASSERT_EQ(nebula::cpp2::ErrorCode::E_INDEX_NOT_FOUND, resp.get_code()); } + + // expected success + // Different spaces, the same tag name(same tagId), create full-text indexes with different names. + { + { + for (auto i = 0; i < 2; ++i) { + cpp2::CreateFTIndexReq req; + cpp2::FTIndex index; + nebula::cpp2::SchemaID schemaId; + schemaId.set_tag_id(5); + index.set_space_id(i + 1); + index.set_depend_schema(std::move(schemaId)); + index.set_fields({"col_string", "col_fixed_string_1"}); + req.set_fulltext_index_name(folly::stringPrintf("ft_tag_index_space%d", i + 1)); + req.set_index(std::move(index)); + + auto* processor = CreateFTIndexProcessor::instance(kv.get()); + auto f = processor->getFuture(); + processor->process(req); + auto resp = std::move(f).get(); + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, resp.get_code()); + } + } + { + cpp2::ListFTIndexesReq req; + auto* processor = ListFTIndexesProcessor::instance(kv.get()); + auto f = processor->getFuture(); + processor->process(req); + auto resp = std::move(f).get(); + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, resp.get_code()); + auto indexes = resp.get_indexes(); + ASSERT_EQ(2, indexes.size()); + for (auto i = 0u; i < indexes.size(); ++i) { + auto key = folly::stringPrintf("ft_tag_index_space%u", i + 1); + auto iter = indexes.find(key); + ASSERT_NE(indexes.end(), iter); + std::vector fields = {"col_string", "col_fixed_string_1"}; + ASSERT_EQ(fields, iter->second.get_fields()); + ASSERT_EQ(i + 1, iter->second.get_space_id()); + nebula::cpp2::SchemaID schemaId; + schemaId.tag_id_ref() = 5; + ASSERT_EQ(schemaId, iter->second.get_depend_schema()); + } + } + { + for (auto i = 0; i < 2; ++i) { + cpp2::DropFTIndexReq req; + req.set_space_id(i + 1); + req.set_fulltext_index_name(folly::stringPrintf("ft_tag_index_space%d", i + 1)); + auto* processor = DropFTIndexProcessor::instance(kv.get()); + auto f = processor->getFuture(); + processor->process(req); + auto resp = std::move(f).get(); + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, resp.get_code()); + } + } + } } TEST(IndexProcessorTest, DropWithFTIndexTest) { @@ -2297,7 +2364,6 @@ TEST(ProcessorTest, IndexIdInSpaceRangeTest) { ASSERT_EQ(14, resp.get_id().get_index_id()); } } - } // namespace meta } // namespace nebula diff --git a/src/meta/test/TestUtils.h b/src/meta/test/TestUtils.h index 023a424f32f..531ad5b495f 100644 --- a/src/meta/test/TestUtils.h +++ b/src/meta/test/TestUtils.h @@ -217,16 +217,25 @@ class TestUtils { GraphSpaceID id, int32_t partitionNum, int32_t replica = 1, - int32_t totalHost = 1) { + int32_t totalHost = 1, + bool multispace = false) { // mock the part distribution like create space cpp2::SpaceDesc properties; - properties.set_space_name("test_space"); - properties.set_partition_num(partitionNum); - properties.set_replica_factor(replica); - auto spaceVal = MetaServiceUtils::spaceVal(properties); + if (multispace) { + properties.space_name_ref() = folly::stringPrintf("test_space_%d", id); + } else { + properties.space_name_ref() = "test_space"; + } + properties.partition_num_ref() = partitionNum; + properties.replica_factor_ref() = replica; std::vector data; - data.emplace_back(MetaServiceUtils::indexSpaceKey("test_space"), - std::string(reinterpret_cast(&id), sizeof(GraphSpaceID))); + if (multispace) { + data.emplace_back(MetaServiceUtils::indexSpaceKey(folly::stringPrintf("test_space_%d", id)), + std::string(reinterpret_cast(&id), sizeof(GraphSpaceID))); + } else { + data.emplace_back(MetaServiceUtils::indexSpaceKey("test_space"), + std::string(reinterpret_cast(&id), sizeof(GraphSpaceID))); + } data.emplace_back(MetaServiceUtils::spaceKey(id), MetaServiceUtils::spaceVal(properties)); std::vector allHosts;