diff --git a/src/impl/MilvusClientImpl.cpp b/src/impl/MilvusClientImpl.cpp index 38c8a8f..2217784 100644 --- a/src/impl/MilvusClientImpl.cpp +++ b/src/impl/MilvusClientImpl.cpp @@ -423,8 +423,12 @@ MilvusClientImpl::ShowPartitions(const std::string& collection_name, const std:: partitions_info.reserve(count); } for (int i = 0; i < count; ++i) { + auto inmemory_percentage = 0; + if (response.inmemory_percentages_size() > i) { + inmemory_percentage = response.inmemory_percentages(i); + } partitions_info.emplace_back(response.partition_names(i), response.partitionids(i), - response.created_timestamps(i), response.inmemory_percentages(i)); + response.created_timestamps(i), inmemory_percentage); } }; diff --git a/test/st/MilvusServerTest.h b/test/st/MilvusServerTest.h index 11d8f5f..dcf45ab 100644 --- a/test/st/MilvusServerTest.h +++ b/test/st/MilvusServerTest.h @@ -29,36 +29,123 @@ namespace milvus { namespace test { -inline void -waitMilvusServerReady(const PythonMilvusServer& server) { - int max_retry = 60, retry = 0; - bool has; - - auto client = milvus::MilvusClient::Create(); - auto param = server.TestClientParam(); - client->Connect(*param); - auto status = client->HasCollection("no_such", has); - - while (!status.IsOk() && retry++ < max_retry) { - std::this_thread::sleep_for(std::chrono::seconds{5}); - client = milvus::MilvusClient::Create(); - client->Connect(*param); - status = client->HasCollection("no_such", has); - std::cout << "Wait milvus start done, try: " << retry << ", status: " << status.Message() << std::endl; - } - std::cout << "Wait milvus start done, status: " << status.Message() << std::endl; -} - -class MilvusServerTest : public ::testing::Test { - protected: +class MilvusServerClientPair { + private: PythonMilvusServer server_{}; std::shared_ptr client_{nullptr}; + public: + std::string collection_name{"Foo"}; + std::string partition_name{"Bar"}; + void - SetUp() override { + startServer() { server_.Start(); client_ = milvus::MilvusClient::Create(); - waitMilvusServerReady(server_); + waitMilvusServerReady(); + } + + PythonMilvusServer& + server() { + return server_; + } + + milvus::MilvusClient& + client() { + return *client_; + } + + void + waitMilvusServerReady() { + int max_retry = 60, retry = 0; + bool has; + + auto client = milvus::MilvusClient::Create(); + auto param = server_.TestClientParam(); + client->Connect(*param); + auto status = client->HasCollection("no_such", has); + + while (!status.IsOk() && retry++ < max_retry) { + std::this_thread::sleep_for(std::chrono::seconds{5}); + client = milvus::MilvusClient::Create(); + client->Connect(*param); + status = client->HasCollection("no_such", has); + std::cout << "Wait milvus start done, try: " << retry << ", status: " << status.Message() << std::endl; + } + std::cout << "Wait milvus start done, status: " << status.Message() << std::endl; + } + + void + createBinaryCollectionAndPartitions() { + milvus::CollectionSchema collection_schema(collection_name); + collection_schema.AddField(milvus::FieldSchema("id", milvus::DataType::INT64, "id", true, true)); + collection_schema.AddField(milvus::FieldSchema("age", milvus::DataType::INT16, "age")); + collection_schema.AddField( + milvus::FieldSchema("face", milvus::DataType::BINARY_VECTOR, "face signature").WithDimension(32)); + + auto status = client_->CreateCollection(collection_schema); + EXPECT_EQ(status.Message(), "OK"); + EXPECT_TRUE(status.IsOk()); + + status = client_->CreatePartition(collection_name, partition_name); + EXPECT_EQ(status.Message(), "OK"); + EXPECT_TRUE(status.IsOk()); + } + + void + createCollectionAndPartitions(bool create_flat_index) { + milvus::CollectionSchema collection_schema(collection_name); + collection_schema.AddField(milvus::FieldSchema("id", milvus::DataType::INT64, "id", true, true)); + collection_schema.AddField(milvus::FieldSchema("age", milvus::DataType::INT16, "age")); + collection_schema.AddField(milvus::FieldSchema("name", milvus::DataType::VARCHAR, "name").WithMaxLength(64)); + collection_schema.AddField( + milvus::FieldSchema("face", milvus::DataType::FLOAT_VECTOR, "face signature").WithDimension(4)); + + auto status = client_->CreateCollection(collection_schema); + EXPECT_EQ(status.Message(), "OK"); + EXPECT_TRUE(status.IsOk()); + + if (create_flat_index) { + milvus::IndexDesc index_desc("face", "", milvus::IndexType::FLAT, milvus::MetricType::L2, 0); + status = client_->CreateIndex(collection_name, index_desc); + EXPECT_EQ(status.Message(), "OK"); + EXPECT_TRUE(status.IsOk()); + } + + status = client_->CreatePartition(collection_name, partition_name); + EXPECT_EQ(status.Message(), "OK"); + EXPECT_TRUE(status.IsOk()); + } + + milvus::DmlResults + insertRecords(const std::vector& fields) { + milvus::DmlResults dml_results; + auto status = client_->Insert(collection_name, partition_name, fields, dml_results); + EXPECT_EQ(status.Message(), "OK"); + EXPECT_TRUE(status.IsOk()); + EXPECT_EQ(dml_results.IdArray().IntIDArray().size(), fields.front()->Count()); + return dml_results; + } + + void + loadCollection() { + auto status = client_->LoadCollection(collection_name); + EXPECT_EQ(status.Message(), "OK"); + EXPECT_TRUE(status.IsOk()); + } + + void + dropCollection() { + auto status = client_->DropCollection(collection_name); + EXPECT_TRUE(status.IsOk()); + } +}; + +class MilvusServerTest : public ::testing::Test, public MilvusServerClientPair { + protected: + void + SetUp() override { + startServer(); } void @@ -67,21 +154,17 @@ class MilvusServerTest : public ::testing::Test { }; template -class MilvusServerTestWithParam : public ::testing::TestWithParam { +class MilvusServerTestWithParam : public ::testing::TestWithParam, public MilvusServerClientPair { protected: - PythonMilvusServer server_{}; - std::shared_ptr client_{nullptr}; - void SetUp() override { - server_.Start(); - client_ = milvus::MilvusClient::Create(); - waitMilvusServerReady(server_); + startServer(); } void TearDown() override { } }; + } // namespace test } // namespace milvus diff --git a/test/st/TestCollection.cpp b/test/st/TestCollection.cpp index 482abb8..c10c368 100644 --- a/test/st/TestCollection.cpp +++ b/test/st/TestCollection.cpp @@ -22,8 +22,8 @@ using MilvusServerTestCollection = MilvusServerTestWithParam; TEST_P(MilvusServerTestCollection, CreateAndDeleteCollection) { auto using_string_primary_key = GetParam(); - milvus::ConnectParam connect_param{"127.0.0.1", server_.ListenPort()}; - client_->Connect(connect_param); + milvus::ConnectParam connect_param{"127.0.0.1", server().ListenPort()}; + client().Connect(connect_param); milvus::CollectionSchema collection_schema("Foo"); if (using_string_primary_key) { @@ -38,38 +38,43 @@ TEST_P(MilvusServerTestCollection, CreateAndDeleteCollection) { collection_schema.AddField( milvus::FieldSchema("face", milvus::DataType::FLOAT_VECTOR, "face signature").WithDimension(1024)); - auto status = client_->CreateCollection(collection_schema); + auto status = client().CreateCollection(collection_schema); EXPECT_EQ(status.Message(), "OK"); EXPECT_TRUE(status.IsOk()); // create index needed after 2.2.0 milvus::IndexDesc index_desc("face", "", milvus::IndexType::FLAT, milvus::MetricType::L2, 0); - status = client_->CreateIndex("Foo", index_desc); + status = client().CreateIndex("Foo", index_desc); EXPECT_TRUE(status.IsOk()); // test for https://github.com/milvus-io/milvus-sdk-cpp/issues/188 std::vector names; std::vector collection_infos; - status = client_->ShowCollections(names, collection_infos); + status = client().ShowCollections(names, collection_infos); EXPECT_TRUE(status.IsOk()); EXPECT_EQ(collection_infos.size(), 1); EXPECT_EQ(collection_infos.front().MemoryPercentage(), 0); EXPECT_EQ(collection_infos.front().Name(), "Foo"); + // test for https://github.com/milvus-io/milvus-sdk-cpp/issues/246 + milvus::PartitionsInfo partitionsInfo{}; + status = client().ShowPartitions(collection_name, std::vector{}, partitionsInfo); + EXPECT_TRUE(status.IsOk()); + names.emplace_back("Foo"); collection_infos.clear(); - status = client_->LoadCollection("Foo"); + status = client().LoadCollection("Foo"); EXPECT_TRUE(status.IsOk()); - status = client_->ShowCollections(names, collection_infos); + status = client().ShowCollections(names, collection_infos); EXPECT_TRUE(status.IsOk()); EXPECT_EQ(collection_infos.size(), 1); EXPECT_EQ(collection_infos.front().MemoryPercentage(), 100); - status = client_->RenameCollection("Foo", "Bar"); + status = client().RenameCollection("Foo", "Bar"); EXPECT_TRUE(status.IsOk()); - status = client_->DropCollection("Bar"); + status = client().DropCollection("Bar"); EXPECT_TRUE(status.IsOk()); } diff --git a/test/st/TestConnectWithTls.cpp b/test/st/TestConnectWithTls.cpp index 134d3c1..2d5266f 100644 --- a/test/st/TestConnectWithTls.cpp +++ b/test/st/TestConnectWithTls.cpp @@ -73,12 +73,12 @@ class MilvusServerTestWithTlsMode : public MilvusServerTest { std::array path{}; getcwd(path.data(), path.size()); std::string pwd = path.data(); - server_.SetTls(Mode, pwd + "/certs/server.crt", pwd + "/certs/server.key", pwd + "/certs/ca.crt"); + server().SetTls(Mode, pwd + "/certs/server.crt", pwd + "/certs/server.key", pwd + "/certs/ca.crt"); MilvusServerTest::SetUp(); ssl_client_ = milvus::MilvusClient::Create(); - auto param = server_.TestClientParam(); + auto param = server().TestClientParam(); ssl_client_->Connect(*param); } @@ -95,25 +95,25 @@ class DISABLED_MilvusServerTestWithTlsMode2 : public MilvusServerTestWithTlsMode TEST_F(MilvusServerTestWithTlsMode1, GenericTest) { bool has; - auto status = ssl_client_->HasCollection("nosuchcollection", has); + auto status = ssl_client_->HasCollection("no_such", has); EXPECT_TRUE(status.IsOk()); - status = client_->HasCollection("nosuchcollection", has); + status = client().HasCollection("no_such", has); EXPECT_FALSE(status.IsOk()); EXPECT_EQ(status.Code(), milvus::StatusCode::NOT_CONNECTED); } TEST_F(DISABLED_MilvusServerTestWithTlsMode2, GenericTest) { bool has; - auto status = ssl_client_->HasCollection("nosuchcollection", has); + auto status = ssl_client_->HasCollection("no_such", has); EXPECT_TRUE(status.IsOk()); - status = client_->HasCollection("nosuchcollection", has); + status = client().HasCollection("no_such", has); EXPECT_FALSE(status.IsOk()); EXPECT_EQ(status.Code(), milvus::StatusCode::NOT_CONNECTED); milvus::ConnectParam param{"127.0.0.1", 300}; param.EnableTls(); - client_->Connect(param); - status = client_->HasCollection("nosuchcollection", has); + client().Connect(param); + status = client().HasCollection("no_such", has); EXPECT_FALSE(status.IsOk()); EXPECT_EQ(status.Code(), milvus::StatusCode::NOT_CONNECTED); } diff --git a/test/st/TestConnectWithUser.cpp b/test/st/TestConnectWithUser.cpp index 9c63aeb..d722008 100644 --- a/test/st/TestConnectWithUser.cpp +++ b/test/st/TestConnectWithUser.cpp @@ -30,10 +30,10 @@ class MilvusServerTestWithAuth : public MilvusServerTest { void SetUp() override { - server_.SetAuthorizationEnabled(true); + server().SetAuthorizationEnabled(true); MilvusServerTest::SetUp(); root_client_ = milvus::MilvusClient::Create(); - root_client_->Connect({"127.0.0.1", server_.ListenPort(), "root", "Milvus"}); + root_client_->Connect({"127.0.0.1", server().ListenPort(), "root", "Milvus"}); } void @@ -46,8 +46,8 @@ TEST_F(MilvusServerTestWithAuth, GenericTest) { bool has; auto status = root_client_->HasCollection("nosuchcollection", has); EXPECT_TRUE(status.IsOk()); - // orig client with out user/pass - status = client_->HasCollection("nosuchcollection", has); + // orig client without user/pass + status = client().HasCollection("nosuchcollection", has); EXPECT_FALSE(status.IsOk()); EXPECT_EQ(status.Code(), milvus::StatusCode::NOT_CONNECTED); } diff --git a/test/st/TestGeneric.cpp b/test/st/TestGeneric.cpp index 6432397..86a94ac 100644 --- a/test/st/TestGeneric.cpp +++ b/test/st/TestGeneric.cpp @@ -24,11 +24,11 @@ using milvus::test::MilvusServerTest; class MilvusServerTestGeneric : public MilvusServerTest {}; TEST_F(MilvusServerTestGeneric, GetVersion) { - milvus::ConnectParam connect_param{"127.0.0.1", server_.ListenPort()}; - client_->Connect(connect_param); + milvus::ConnectParam connect_param{"127.0.0.1", server().ListenPort()}; + client().Connect(connect_param); std::string version; - auto status = client_->GetVersion(version); + auto status = client().GetVersion(version); EXPECT_TRUE(status.IsOk()); EXPECT_THAT(version, testing::StartsWith("v2.")); } diff --git a/test/st/TestSearch.cpp b/test/st/TestSearch.cpp index c41406a..9716086 100644 --- a/test/st/TestSearch.cpp +++ b/test/st/TestSearch.cpp @@ -22,71 +22,7 @@ using milvus::test::MilvusServerTest; using testing::UnorderedElementsAre; using testing::UnorderedElementsAreArray; -class MilvusServerTestSearch : public MilvusServerTest { - protected: - std::string collection_name{"Foo"}; - std::string partition_name{"Bar"}; - - void - SetUp() override { - MilvusServerTest::SetUp(); - milvus::ConnectParam connect_param{"127.0.0.1", server_.ListenPort()}; - client_->Connect(connect_param); - } - - void - TearDown() override { - MilvusServerTest::TearDown(); - } - - void - createCollectionAndPartitions(bool create_flat_index) { - milvus::CollectionSchema collection_schema(collection_name); - collection_schema.AddField(milvus::FieldSchema("id", milvus::DataType::INT64, "id", true, true)); - collection_schema.AddField(milvus::FieldSchema("age", milvus::DataType::INT16, "age")); - collection_schema.AddField(milvus::FieldSchema("name", milvus::DataType::VARCHAR, "name").WithMaxLength(64)); - collection_schema.AddField( - milvus::FieldSchema("face", milvus::DataType::FLOAT_VECTOR, "face signature").WithDimension(4)); - - auto status = client_->CreateCollection(collection_schema); - EXPECT_EQ(status.Message(), "OK"); - EXPECT_TRUE(status.IsOk()); - - if (create_flat_index) { - milvus::IndexDesc index_desc("face", "", milvus::IndexType::FLAT, milvus::MetricType::L2, 0); - status = client_->CreateIndex(collection_name, index_desc); - EXPECT_EQ(status.Message(), "OK"); - EXPECT_TRUE(status.IsOk()); - } - - status = client_->CreatePartition(collection_name, partition_name); - EXPECT_EQ(status.Message(), "OK"); - EXPECT_TRUE(status.IsOk()); - } - - milvus::DmlResults - insertRecords(const std::vector& fields) { - milvus::DmlResults dml_results; - auto status = client_->Insert(collection_name, partition_name, fields, dml_results); - EXPECT_EQ(status.Message(), "OK"); - EXPECT_TRUE(status.IsOk()); - EXPECT_EQ(dml_results.IdArray().IntIDArray().size(), fields.front()->Count()); - return dml_results; - } - - void - loadCollection() { - auto status = client_->LoadCollection(collection_name); - EXPECT_EQ(status.Message(), "OK"); - EXPECT_TRUE(status.IsOk()); - } - - void - dropCollection() { - auto status = client_->DropCollection(collection_name); - EXPECT_TRUE(status.IsOk()); - } -}; +class MilvusServerTestSearch : public MilvusServerTest {}; TEST_F(MilvusServerTestSearch, SearchWithoutIndex) { std::vector fields{ @@ -110,7 +46,7 @@ TEST_F(MilvusServerTestSearch, SearchWithoutIndex) { arguments.AddTargetVector("face", std::vector{0.f, 0.f, 0.f, 0.f}); arguments.AddTargetVector("face", std::vector{1.f, 1.f, 1.f, 1.f}); milvus::SearchResults search_results{}; - auto status = client_->Search(arguments, search_results); + auto status = client().Search(arguments, search_results); EXPECT_EQ(status.Message(), "OK"); EXPECT_TRUE(status.IsOk()); @@ -168,7 +104,7 @@ TEST_F(MilvusServerTestSearch, RangeSearch) { arguments.AddTargetVector("face", std::vector{0.f, 0.f, 0.f, 0.f}); arguments.AddTargetVector("face", std::vector{1.f, 1.f, 1.f, 1.f}); milvus::SearchResults search_results{}; - auto status = client_->Search(arguments, search_results); + auto status = client().Search(arguments, search_results); EXPECT_EQ(status.Message(), "OK"); EXPECT_TRUE(status.IsOk()); @@ -194,7 +130,7 @@ TEST_F(MilvusServerTestSearch, RangeSearch) { // add fields, then search again, should be 6 and 4 insertRecords(fields); loadCollection(); - status = client_->Search(arguments, search_results); + status = client().Search(arguments, search_results); EXPECT_TRUE(status.IsOk()); validateScores(6, 4); @@ -202,7 +138,7 @@ TEST_F(MilvusServerTestSearch, RangeSearch) { insertRecords(fields); insertRecords(fields); loadCollection(); - status = client_->Search(arguments, search_results); + status = client().Search(arguments, search_results); EXPECT_TRUE(status.IsOk()); validateScores(10, 8); @@ -227,11 +163,11 @@ TEST_F(MilvusServerTestSearch, SearchWithStringFilter) { arguments.SetTopK(10); arguments.AddOutputField("age"); arguments.AddOutputField("name"); - arguments.SetExpression("name like \"To%\""); // Tom match To% + arguments.SetExpression(R"(name like "To%")"); // Tom match To% arguments.AddTargetVector("face", std::vector{0.f, 0.f, 0.f, 0.f}); arguments.AddTargetVector("face", std::vector{1.f, 1.f, 1.f, 1.f}); milvus::SearchResults search_results{}; - auto status = client_->Search(arguments, search_results); + auto status = client().Search(arguments, search_results); EXPECT_EQ(status.Message(), "OK"); EXPECT_TRUE(status.IsOk()); @@ -279,7 +215,7 @@ TEST_F(MilvusServerTestSearch, SearchWithIVFIndex) { milvus::IndexDesc index_desc("face", "", milvus::IndexType::IVF_FLAT, milvus::MetricType::L2, 0); index_desc.AddExtraParam("nlist", 1024); - auto status = client_->CreateIndex(collection_name, index_desc); + auto status = client().CreateIndex(collection_name, index_desc); EXPECT_EQ(status.Message(), "OK"); EXPECT_TRUE(status.IsOk()); @@ -293,7 +229,7 @@ TEST_F(MilvusServerTestSearch, SearchWithIVFIndex) { arguments.AddTargetVector("face", std::vector{0.f, 0.f, 0.f, 0.f}); arguments.AddTargetVector("face", std::vector{1.f, 1.f, 1.f, 1.f}); milvus::SearchResults search_results{}; - status = client_->Search(arguments, search_results); + status = client().Search(arguments, search_results); EXPECT_EQ(status.Message(), "OK"); EXPECT_TRUE(status.IsOk()); diff --git a/test/st/TestSearchWithBinaryVectors.cpp b/test/st/TestSearchWithBinaryVectors.cpp index 17a2d35..a599b5d 100644 --- a/test/st/TestSearchWithBinaryVectors.cpp +++ b/test/st/TestSearchWithBinaryVectors.cpp @@ -22,63 +22,7 @@ using milvus::test::MilvusServerTest; using testing::UnorderedElementsAre; using testing::UnorderedElementsAreArray; -class MilvusServerTestSearchWithBinaryVectors : public MilvusServerTest { - protected: - std::string collection_name{"Foo"}; - std::string partition_name{"Bar"}; - - void - SetUp() override { - MilvusServerTest::SetUp(); - milvus::ConnectParam connect_param{"127.0.0.1", server_.ListenPort()}; - client_->Connect(connect_param); - } - - void - TearDown() override { - MilvusServerTest::TearDown(); - } - - void - createCollectionAndPartitions() { - milvus::CollectionSchema collection_schema(collection_name); - collection_schema.AddField(milvus::FieldSchema("id", milvus::DataType::INT64, "id", true, true)); - collection_schema.AddField(milvus::FieldSchema("age", milvus::DataType::INT16, "age")); - collection_schema.AddField( - milvus::FieldSchema("face", milvus::DataType::BINARY_VECTOR, "face signature").WithDimension(32)); - - auto status = client_->CreateCollection(collection_schema); - EXPECT_EQ(status.Message(), "OK"); - EXPECT_TRUE(status.IsOk()); - - status = client_->CreatePartition(collection_name, partition_name); - EXPECT_EQ(status.Message(), "OK"); - EXPECT_TRUE(status.IsOk()); - } - - milvus::DmlResults - insertRecords(const std::vector& fields) { - milvus::DmlResults dml_results; - auto status = client_->Insert(collection_name, partition_name, fields, dml_results); - EXPECT_EQ(status.Message(), "OK"); - EXPECT_TRUE(status.IsOk()); - EXPECT_EQ(dml_results.IdArray().IntIDArray().size(), fields.front()->Count()); - return dml_results; - } - - void - loadCollection() { - auto status = client_->LoadCollection(collection_name); - EXPECT_EQ(status.Message(), "OK"); - EXPECT_TRUE(status.IsOk()); - } - - void - dropCollection() { - auto status = client_->DropCollection(collection_name); - EXPECT_TRUE(status.IsOk()); - } -}; +class MilvusServerTestSearchWithBinaryVectors : public MilvusServerTest {}; // for issue #194 TEST_F(MilvusServerTestSearchWithBinaryVectors, RegressionIssue194) { @@ -96,11 +40,11 @@ TEST_F(MilvusServerTestSearchWithBinaryVectors, RegressionIssue194) { std::vector fields{std::make_shared("age", ages), std::make_shared("face", faces)}; - createCollectionAndPartitions(); + createBinaryCollectionAndPartitions(); auto dml_results = insertRecords(fields); milvus::IndexDesc index_desc("face", "", milvus::IndexType::BIN_FLAT, milvus::MetricType::HAMMING, 0); - auto status = client_->CreateIndex(collection_name, index_desc); + auto status = client().CreateIndex(collection_name, index_desc); EXPECT_EQ(status.Message(), "OK"); EXPECT_TRUE(status.IsOk()); @@ -113,7 +57,7 @@ TEST_F(MilvusServerTestSearchWithBinaryVectors, RegressionIssue194) { arguments.AddTargetVector("face", std::vector{255, 255, 255, 255}); arguments.AddTargetVector("face", std::vector{0, 0, 0, 0}); milvus::SearchResults search_results{}; - status = client_->Search(arguments, search_results); + status = client().Search(arguments, search_results); EXPECT_EQ(status.Message(), "OK"); EXPECT_TRUE(status.IsOk());