2023-05-31 20:32:31 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package insert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
|
2023-05-31 20:32:31 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/common"
|
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/funcutil"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
2023-07-18 14:23:20 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/metric"
|
2023-05-31 20:32:31 +08:00
|
|
|
"github.com/milvus-io/milvus/tests/integration"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InsertSuite struct {
|
|
|
|
integration.MiniClusterSuite
|
|
|
|
}
|
|
|
|
|
2024-04-10 19:57:18 +08:00
|
|
|
// insert request with duplicate field data should fail
|
2023-05-31 20:32:31 +08:00
|
|
|
func (s *InsertSuite) TestInsert() {
|
|
|
|
c := s.Cluster
|
|
|
|
ctx, cancel := context.WithCancel(c.GetContext())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
prefix := "TestInsert"
|
|
|
|
dbName := ""
|
|
|
|
collectionName := prefix + funcutil.GenRandomStr()
|
|
|
|
dim := 128
|
|
|
|
rowNum := 3000
|
|
|
|
|
|
|
|
schema := integration.ConstructSchema(collectionName, dim, false)
|
|
|
|
marshaledSchema, err := proto.Marshal(schema)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
createCollectionStatus, err := c.Proxy.CreateCollection(ctx, &milvuspb.CreateCollectionRequest{
|
|
|
|
DbName: dbName,
|
|
|
|
CollectionName: collectionName,
|
|
|
|
Schema: marshaledSchema,
|
|
|
|
ShardsNum: common.DefaultShardsNum,
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
err = merr.Error(createCollectionStatus)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("createCollectionStatus fail reason", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("CreateCollection result", zap.Any("createCollectionStatus", createCollectionStatus))
|
|
|
|
showCollectionsResp, err := c.Proxy.ShowCollections(ctx, &milvuspb.ShowCollectionsRequest{})
|
|
|
|
s.NoError(err)
|
|
|
|
s.True(merr.Ok(showCollectionsResp.GetStatus()))
|
|
|
|
log.Info("ShowCollections result", zap.Any("showCollectionsResp", showCollectionsResp))
|
|
|
|
|
|
|
|
// create index
|
|
|
|
createIndexStatus, err := c.Proxy.CreateIndex(ctx, &milvuspb.CreateIndexRequest{
|
|
|
|
CollectionName: collectionName,
|
|
|
|
FieldName: integration.FloatVecField,
|
|
|
|
IndexName: "_default",
|
2023-07-18 14:23:20 +08:00
|
|
|
ExtraParams: integration.ConstructIndexParam(dim, integration.IndexFaissIvfFlat, metric.IP),
|
2023-05-31 20:32:31 +08:00
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
err = merr.Error(createIndexStatus)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("createIndexStatus fail reason", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
s.WaitForIndexBuilt(ctx, collectionName, integration.FloatVecField)
|
|
|
|
log.Info("Create index done")
|
|
|
|
|
|
|
|
// load
|
|
|
|
loadStatus, err := c.Proxy.LoadCollection(ctx, &milvuspb.LoadCollectionRequest{
|
|
|
|
DbName: dbName,
|
|
|
|
CollectionName: collectionName,
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
err = merr.Error(loadStatus)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("LoadCollection fail reason", zap.Error(err))
|
|
|
|
}
|
|
|
|
s.WaitForLoad(ctx, collectionName)
|
|
|
|
log.Info("Load collection done")
|
|
|
|
|
|
|
|
pkFieldData := integration.NewInt64FieldData(integration.Int64Field, rowNum)
|
|
|
|
hashKeys := integration.GenerateHashKeys(rowNum)
|
|
|
|
insertResult, err := c.Proxy.Insert(ctx, &milvuspb.InsertRequest{
|
|
|
|
DbName: dbName,
|
|
|
|
CollectionName: collectionName,
|
|
|
|
FieldsData: []*schemapb.FieldData{pkFieldData, pkFieldData},
|
|
|
|
HashKeys: hashKeys,
|
|
|
|
NumRows: uint32(rowNum),
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
s.False(merr.Ok(insertResult.GetStatus()))
|
|
|
|
|
|
|
|
log.Info("==================")
|
|
|
|
log.Info("==================")
|
|
|
|
log.Info("TestInsert succeed")
|
|
|
|
log.Info("==================")
|
|
|
|
log.Info("==================")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsert(t *testing.T) {
|
|
|
|
suite.Run(t, new(InsertSuite))
|
|
|
|
}
|