hello_milvus_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Licensed to the LF AI & Data foundation under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package hellomilvus
  17. import (
  18. "context"
  19. "fmt"
  20. "testing"
  21. "time"
  22. "github.com/stretchr/testify/suite"
  23. "go.uber.org/zap"
  24. "google.golang.org/protobuf/proto"
  25. "github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
  26. "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
  27. "github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
  28. "github.com/milvus-io/milvus/internal/util/hookutil"
  29. "github.com/milvus-io/milvus/pkg/common"
  30. "github.com/milvus-io/milvus/pkg/log"
  31. "github.com/milvus-io/milvus/pkg/util/funcutil"
  32. "github.com/milvus-io/milvus/pkg/util/merr"
  33. "github.com/milvus-io/milvus/pkg/util/metric"
  34. "github.com/milvus-io/milvus/tests/integration"
  35. )
  36. type HelloMilvusSuite struct {
  37. integration.MiniClusterSuite
  38. indexType string
  39. metricType string
  40. vecType schemapb.DataType
  41. }
  42. func (s *HelloMilvusSuite) run() {
  43. ctx, cancel := context.WithCancel(context.Background())
  44. defer cancel()
  45. c := s.Cluster
  46. const (
  47. dim = 128
  48. dbName = ""
  49. rowNum = 3000
  50. )
  51. collectionName := "TestHelloMilvus" + funcutil.GenRandomStr()
  52. schema := integration.ConstructSchemaOfVecDataType(collectionName, dim, true, s.vecType)
  53. marshaledSchema, err := proto.Marshal(schema)
  54. s.NoError(err)
  55. createCollectionStatus, err := c.Proxy.CreateCollection(ctx, &milvuspb.CreateCollectionRequest{
  56. DbName: dbName,
  57. CollectionName: collectionName,
  58. Schema: marshaledSchema,
  59. ShardsNum: common.DefaultShardsNum,
  60. })
  61. s.NoError(err)
  62. if createCollectionStatus.GetErrorCode() != commonpb.ErrorCode_Success {
  63. log.Warn("createCollectionStatus fail reason", zap.String("reason", createCollectionStatus.GetReason()))
  64. }
  65. s.Equal(createCollectionStatus.GetErrorCode(), commonpb.ErrorCode_Success)
  66. log.Info("CreateCollection result", zap.Any("createCollectionStatus", createCollectionStatus))
  67. showCollectionsResp, err := c.Proxy.ShowCollections(ctx, &milvuspb.ShowCollectionsRequest{})
  68. s.NoError(err)
  69. s.Equal(showCollectionsResp.GetStatus().GetErrorCode(), commonpb.ErrorCode_Success)
  70. log.Info("ShowCollections result", zap.Any("showCollectionsResp", showCollectionsResp))
  71. var fVecColumn *schemapb.FieldData
  72. if s.vecType == schemapb.DataType_SparseFloatVector {
  73. fVecColumn = integration.NewSparseFloatVectorFieldData(integration.SparseFloatVecField, rowNum)
  74. } else {
  75. fVecColumn = integration.NewFloatVectorFieldData(integration.FloatVecField, rowNum, dim)
  76. }
  77. hashKeys := integration.GenerateHashKeys(rowNum)
  78. insertCheckReport := func() {
  79. timeoutCtx, cancelFunc := context.WithTimeout(ctx, 5*time.Second)
  80. defer cancelFunc()
  81. for {
  82. select {
  83. case <-timeoutCtx.Done():
  84. s.Fail("insert check timeout")
  85. case report := <-c.Extension.GetReportChan():
  86. reportInfo := report.(map[string]any)
  87. log.Info("insert report info", zap.Any("reportInfo", reportInfo))
  88. s.Equal(hookutil.OpTypeInsert, reportInfo[hookutil.OpTypeKey])
  89. s.NotEqualValues(0, reportInfo[hookutil.RequestDataSizeKey])
  90. return
  91. }
  92. }
  93. }
  94. go insertCheckReport()
  95. insertResult, err := c.Proxy.Insert(ctx, &milvuspb.InsertRequest{
  96. DbName: dbName,
  97. CollectionName: collectionName,
  98. FieldsData: []*schemapb.FieldData{fVecColumn},
  99. HashKeys: hashKeys,
  100. NumRows: uint32(rowNum),
  101. })
  102. s.NoError(err)
  103. s.Equal(insertResult.GetStatus().GetErrorCode(), commonpb.ErrorCode_Success)
  104. // flush
  105. flushResp, err := c.Proxy.Flush(ctx, &milvuspb.FlushRequest{
  106. DbName: dbName,
  107. CollectionNames: []string{collectionName},
  108. })
  109. s.NoError(err)
  110. segmentIDs, has := flushResp.GetCollSegIDs()[collectionName]
  111. ids := segmentIDs.GetData()
  112. s.Require().NotEmpty(segmentIDs)
  113. s.Require().True(has)
  114. flushTs, has := flushResp.GetCollFlushTs()[collectionName]
  115. s.True(has)
  116. segments, err := c.MetaWatcher.ShowSegments()
  117. s.NoError(err)
  118. s.NotEmpty(segments)
  119. for _, segment := range segments {
  120. log.Info("ShowSegments result", zap.String("segment", segment.String()))
  121. }
  122. s.WaitForFlush(ctx, ids, flushTs, dbName, collectionName)
  123. // create index
  124. createIndexStatus, err := c.Proxy.CreateIndex(ctx, &milvuspb.CreateIndexRequest{
  125. CollectionName: collectionName,
  126. FieldName: fVecColumn.FieldName,
  127. IndexName: "_default",
  128. ExtraParams: integration.ConstructIndexParam(dim, s.indexType, s.metricType),
  129. })
  130. if createIndexStatus.GetErrorCode() != commonpb.ErrorCode_Success {
  131. log.Warn("createIndexStatus fail reason", zap.String("reason", createIndexStatus.GetReason()))
  132. }
  133. s.NoError(err)
  134. s.Equal(commonpb.ErrorCode_Success, createIndexStatus.GetErrorCode())
  135. s.WaitForIndexBuilt(ctx, collectionName, fVecColumn.FieldName)
  136. // load
  137. loadStatus, err := c.Proxy.LoadCollection(ctx, &milvuspb.LoadCollectionRequest{
  138. DbName: dbName,
  139. CollectionName: collectionName,
  140. })
  141. s.NoError(err)
  142. if loadStatus.GetErrorCode() != commonpb.ErrorCode_Success {
  143. log.Warn("loadStatus fail reason", zap.String("reason", loadStatus.GetReason()))
  144. }
  145. s.Equal(commonpb.ErrorCode_Success, loadStatus.GetErrorCode())
  146. s.WaitForLoad(ctx, collectionName)
  147. // search
  148. expr := fmt.Sprintf("%s > 0", integration.Int64Field)
  149. nq := 10
  150. topk := 10
  151. roundDecimal := -1
  152. params := integration.GetSearchParams(s.indexType, s.metricType)
  153. searchReq := integration.ConstructSearchRequest("", collectionName, expr,
  154. fVecColumn.FieldName, s.vecType, nil, s.metricType, params, nq, dim, topk, roundDecimal)
  155. searchCheckReport := func() {
  156. timeoutCtx, cancelFunc := context.WithTimeout(ctx, 5*time.Second)
  157. defer cancelFunc()
  158. for {
  159. select {
  160. case <-timeoutCtx.Done():
  161. s.Fail("search check timeout")
  162. case report := <-c.Extension.GetReportChan():
  163. reportInfo := report.(map[string]any)
  164. log.Info("search report info", zap.Any("reportInfo", reportInfo))
  165. s.Equal(hookutil.OpTypeSearch, reportInfo[hookutil.OpTypeKey])
  166. s.NotEqualValues(0, reportInfo[hookutil.ResultDataSizeKey])
  167. s.NotEqualValues(0, reportInfo[hookutil.RelatedDataSizeKey])
  168. s.EqualValues(rowNum, reportInfo[hookutil.RelatedCntKey])
  169. return
  170. }
  171. }
  172. }
  173. go searchCheckReport()
  174. searchResult, err := c.Proxy.Search(ctx, searchReq)
  175. err = merr.CheckRPCCall(searchResult, err)
  176. s.NoError(err)
  177. queryCheckReport := func() {
  178. timeoutCtx, cancelFunc := context.WithTimeout(ctx, 5*time.Second)
  179. defer cancelFunc()
  180. for {
  181. select {
  182. case <-timeoutCtx.Done():
  183. s.Fail("query check timeout")
  184. case report := <-c.Extension.GetReportChan():
  185. reportInfo := report.(map[string]any)
  186. log.Info("query report info", zap.Any("reportInfo", reportInfo))
  187. s.Equal(hookutil.OpTypeQuery, reportInfo[hookutil.OpTypeKey])
  188. s.NotEqualValues(0, reportInfo[hookutil.ResultDataSizeKey])
  189. s.NotEqualValues(0, reportInfo[hookutil.RelatedDataSizeKey])
  190. s.EqualValues(rowNum, reportInfo[hookutil.RelatedCntKey])
  191. return
  192. }
  193. }
  194. }
  195. go queryCheckReport()
  196. queryResult, err := c.Proxy.Query(ctx, &milvuspb.QueryRequest{
  197. DbName: dbName,
  198. CollectionName: collectionName,
  199. Expr: "",
  200. OutputFields: []string{"count(*)"},
  201. })
  202. if queryResult.GetStatus().GetErrorCode() != commonpb.ErrorCode_Success {
  203. log.Warn("searchResult fail reason", zap.String("reason", queryResult.GetStatus().GetReason()))
  204. }
  205. s.NoError(err)
  206. s.Equal(commonpb.ErrorCode_Success, queryResult.GetStatus().GetErrorCode())
  207. deleteCheckReport := func() {
  208. timeoutCtx, cancelFunc := context.WithTimeout(ctx, 5*time.Second)
  209. defer cancelFunc()
  210. for {
  211. select {
  212. case <-timeoutCtx.Done():
  213. s.Fail("delete check timeout")
  214. case report := <-c.Extension.GetReportChan():
  215. reportInfo := report.(map[string]any)
  216. log.Info("delete report info", zap.Any("reportInfo", reportInfo))
  217. s.Equal(hookutil.OpTypeDelete, reportInfo[hookutil.OpTypeKey])
  218. s.EqualValues(2, reportInfo[hookutil.SuccessCntKey])
  219. s.EqualValues(0, reportInfo[hookutil.RelatedCntKey])
  220. return
  221. }
  222. }
  223. }
  224. go deleteCheckReport()
  225. deleteResult, err := c.Proxy.Delete(ctx, &milvuspb.DeleteRequest{
  226. DbName: dbName,
  227. CollectionName: collectionName,
  228. Expr: integration.Int64Field + " in [1, 2]",
  229. })
  230. if deleteResult.GetStatus().GetErrorCode() != commonpb.ErrorCode_Success {
  231. log.Warn("deleteResult fail reason", zap.String("reason", deleteResult.GetStatus().GetReason()))
  232. }
  233. s.NoError(err)
  234. s.Equal(commonpb.ErrorCode_Success, deleteResult.GetStatus().GetErrorCode())
  235. status, err := c.Proxy.ReleaseCollection(ctx, &milvuspb.ReleaseCollectionRequest{
  236. CollectionName: collectionName,
  237. })
  238. err = merr.CheckRPCCall(status, err)
  239. s.NoError(err)
  240. status, err = c.Proxy.DropCollection(ctx, &milvuspb.DropCollectionRequest{
  241. CollectionName: collectionName,
  242. })
  243. err = merr.CheckRPCCall(status, err)
  244. s.NoError(err)
  245. log.Info("TestHelloMilvus succeed")
  246. }
  247. func (s *HelloMilvusSuite) TestHelloMilvus_basic() {
  248. s.indexType = integration.IndexFaissIvfFlat
  249. s.metricType = metric.L2
  250. s.vecType = schemapb.DataType_FloatVector
  251. s.run()
  252. }
  253. func (s *HelloMilvusSuite) TestHelloMilvus_sparse_basic() {
  254. s.indexType = integration.IndexSparseInvertedIndex
  255. s.metricType = metric.IP
  256. s.vecType = schemapb.DataType_SparseFloatVector
  257. s.run()
  258. }
  259. func (s *HelloMilvusSuite) TestHelloMilvus_sparse_wand_basic() {
  260. s.indexType = integration.IndexSparseWand
  261. s.metricType = metric.IP
  262. s.vecType = schemapb.DataType_SparseFloatVector
  263. s.run()
  264. }
  265. func TestHelloMilvus(t *testing.T) {
  266. suite.Run(t, new(HelloMilvusSuite))
  267. }