milvus/tests/integration/getvector/array_struct_test.go
sthuang c102fa8b0b
enhance: [StorageV2] zero copy for packed writer record batch (#43779)
The Out of Memory (OOM) error occurs because a handler retains the
entire ImportRecordBatch in memory. Consequently, even when child arrays
within the batch are flushed, the memory for the complete batch is not
released. We temporarily fixed by deep copying record batch in #43724.

The proposed fix is to split the RecordBatch into smaller sub-batches by
column group. These sub-batches will be transferred via CGO, then
reassembled before being written to storage using the Storage V2 API.
Thus we can achieve zero-copy and only transferring references in CGO.

related: #43310

Signed-off-by: shaoting-huang <shaoting.huang@zilliz.com>
2025-08-15 10:11:44 +08:00

209 lines
6.1 KiB
Go

// 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 getvector
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/suite"
"google.golang.org/protobuf/proto"
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
"github.com/milvus-io/milvus/pkg/v2/common"
"github.com/milvus-io/milvus/pkg/v2/util/funcutil"
"github.com/milvus-io/milvus/pkg/v2/util/metric"
"github.com/milvus-io/milvus/tests/integration"
)
type TestArrayStructSuite struct {
integration.MiniClusterSuite
dbName string
// test params
nq int
topK int
indexType string
metricType string
vecType schemapb.DataType
}
func (s *TestArrayStructSuite) run() {
ctx, cancel := context.WithCancel(s.Cluster.GetContext())
defer cancel()
collection := fmt.Sprintf("TestGetVector_%d_%d_%s_%s_%s",
s.nq, s.topK, s.indexType, s.metricType, funcutil.GenRandomStr())
const (
NB = 10000
dim = 16
)
if len(s.dbName) > 0 {
createDataBaseStatus, err := s.Cluster.MilvusClient.CreateDatabase(ctx, &milvuspb.CreateDatabaseRequest{
DbName: s.dbName,
})
s.Require().NoError(err)
s.Require().Equal(createDataBaseStatus.GetErrorCode(), commonpb.ErrorCode_Success)
}
pkFieldName := "pkField"
vecFieldName := "vecField"
structFieldName := "structField"
structSubVecFieldName := "structSubVecField"
pk := &schemapb.FieldSchema{
FieldID: 100,
Name: pkFieldName,
IsPrimaryKey: true,
Description: "",
DataType: schemapb.DataType_Int64,
TypeParams: []*commonpb.KeyValuePair{
{
Key: common.MaxLengthKey,
Value: "100",
},
},
IndexParams: nil,
AutoID: false,
}
fVec := &schemapb.FieldSchema{
FieldID: 101,
Name: vecFieldName,
IsPrimaryKey: false,
Description: "",
DataType: schemapb.DataType_FloatVector,
TypeParams: []*commonpb.KeyValuePair{
{
Key: common.DimKey,
Value: fmt.Sprintf("%d", dim),
},
},
IndexParams: nil,
}
structSubVec := &schemapb.FieldSchema{
FieldID: 102,
Name: structSubVecFieldName,
IsPrimaryKey: false,
Description: "",
DataType: schemapb.DataType_ArrayOfVector,
ElementType: s.vecType,
TypeParams: []*commonpb.KeyValuePair{
{
Key: common.DimKey,
Value: fmt.Sprintf("%d", dim),
},
},
IndexParams: nil,
}
structField := &schemapb.StructArrayFieldSchema{
FieldID: 103,
Name: structFieldName,
Fields: []*schemapb.FieldSchema{structSubVec},
}
schema := &schemapb.CollectionSchema{
Name: collection,
Description: "",
AutoID: false,
Fields: []*schemapb.FieldSchema{pk, fVec},
StructArrayFields: []*schemapb.StructArrayFieldSchema{structField},
}
marshaledSchema, err := proto.Marshal(schema)
s.Require().NoError(err)
createCollectionStatus, err := s.Cluster.MilvusClient.CreateCollection(ctx, &milvuspb.CreateCollectionRequest{
DbName: s.dbName,
CollectionName: collection,
Schema: marshaledSchema,
ShardsNum: 2,
})
s.Require().NoError(err)
s.Require().Equal(createCollectionStatus.GetErrorCode(), commonpb.ErrorCode_Success)
fieldsData := make([]*schemapb.FieldData, 0, 5)
// pk
fieldsData = append(fieldsData, integration.NewInt64FieldData(pkFieldName, NB))
// vec
fieldsData = append(fieldsData, integration.NewFloatVectorFieldData(vecFieldName, NB, dim))
// struct
fieldsData = append(fieldsData, integration.NewStructArrayFieldData(structField, structFieldName, NB, dim))
hashKeys := integration.GenerateHashKeys(NB)
insertResult, err := s.Cluster.MilvusClient.Insert(ctx, &milvuspb.InsertRequest{
DbName: s.dbName,
CollectionName: collection,
FieldsData: fieldsData,
HashKeys: hashKeys,
NumRows: uint32(NB),
})
s.Require().NoError(err)
s.Require().Equal(insertResult.GetStatus().GetErrorCode(), commonpb.ErrorCode_Success)
// flush
flushResp, err := s.Cluster.MilvusClient.Flush(ctx, &milvuspb.FlushRequest{
DbName: s.dbName,
CollectionNames: []string{collection},
})
s.Require().NoError(err)
segmentIDs, has := flushResp.GetCollSegIDs()[collection]
ids := segmentIDs.GetData()
s.Require().NotEmpty(segmentIDs)
s.Require().True(has)
flushTs, has := flushResp.GetCollFlushTs()[collection]
s.Require().True(has)
s.WaitForFlush(ctx, ids, flushTs, s.dbName, collection)
segments, err := s.Cluster.ShowSegments(collection)
s.Require().NoError(err)
s.Require().NotEmpty(segments)
// // create index
// createIndexResult, err := s.Cluster.Proxy.CreateIndex(ctx, &milvuspb.CreateIndexRequest{
// DbName: s.dbName,
// CollectionName: collection,
// FieldName: structSubVecFieldName,
// IndexName: "_default",
// ExtraParams: integration.ConstructIndexParam(dim, s.indexType, s.metricType),
// })
// s.Require().NoError(err)
// s.Require().Equal(createIndexResult.GetErrorCode(), commonpb.ErrorCode_Success)
// s.WaitForIndexBuiltWithDB(ctx, s.dbName, collection, structSubVecFieldName)
}
func (s *TestArrayStructSuite) TestGetVector_ArrayStruct_FloatVector() {
s.nq = 10
s.topK = 10
s.indexType = integration.IndexHNSW
s.metricType = metric.L2
s.vecType = schemapb.DataType_FloatVector
s.run()
}
func TestGetVectorArrayStruct(t *testing.T) {
t.Skip("Skip integration test, need to refactor integration test framework.")
suite.Run(t, new(TestArrayStructSuite))
}