2021-04-19 05:47:10 +00:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed 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.
|
|
|
|
|
2021-01-16 02:12:14 +00:00
|
|
|
package querynode
|
2020-11-05 02:52:50 +00:00
|
|
|
|
2020-11-17 02:07:42 +00:00
|
|
|
import (
|
2020-12-05 09:39:58 +00:00
|
|
|
"context"
|
2020-11-17 02:07:42 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"math"
|
2021-04-01 20:39:23 +00:00
|
|
|
"math/rand"
|
2020-11-17 02:07:42 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-04-22 06:45:57 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/milvuspb"
|
2020-11-17 02:07:42 +00:00
|
|
|
)
|
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
func loadFields(segment *Segment, DIM int, N int) error {
|
|
|
|
// generate vector field
|
|
|
|
vectorFieldID := int64(100)
|
|
|
|
vectors := make([]float32, N*DIM)
|
|
|
|
for i := 0; i < N*DIM; i++ {
|
|
|
|
vectors[i] = rand.Float32()
|
|
|
|
}
|
2021-03-22 08:36:10 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// generate int field
|
|
|
|
agesFieldID := int64(101)
|
|
|
|
ages := make([]int32, N)
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
ages[i] = int32(N)
|
|
|
|
}
|
2020-11-17 02:07:42 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
err := segment.segmentLoadFieldData(vectorFieldID, N, vectors)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = segment.segmentLoadFieldData(agesFieldID, N, ages)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rowIDs := ages
|
|
|
|
err = segment.segmentLoadFieldData(rowIDFieldID, N, rowIDs)
|
|
|
|
return err
|
|
|
|
}
|
2020-11-17 02:07:42 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
func sendSearchRequest(ctx context.Context, DIM int) error {
|
|
|
|
// init message stream
|
|
|
|
msFactory, err := newMessageStreamFactory()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-23 02:07:45 +00:00
|
|
|
searchProducerChannels := []string{"test-query"}
|
2020-11-26 07:18:36 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
searchStream, _ := msFactory.NewMsgStream(ctx)
|
|
|
|
searchStream.AsProducer(searchProducerChannels)
|
|
|
|
searchStream.Start()
|
|
|
|
|
|
|
|
// generate search rawData
|
|
|
|
var vec = make([]float32, DIM)
|
|
|
|
for i := 0; i < DIM; i++ {
|
|
|
|
vec[i] = rand.Float32()
|
|
|
|
}
|
2020-11-26 07:18:36 +00:00
|
|
|
dslString := "{\"bool\": { \n\"vector\": {\n \"vec\": {\n \"metric_type\": \"L2\", \n \"params\": {\n \"nprobe\": 10 \n},\n \"query\": \"$0\",\"topk\": 10 \n } \n } \n } \n }"
|
2020-11-26 08:01:31 +00:00
|
|
|
var searchRawData1 []byte
|
|
|
|
var searchRawData2 []byte
|
|
|
|
for i, ele := range vec {
|
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, math.Float32bits(ele+float32(i*2)))
|
|
|
|
searchRawData1 = append(searchRawData1, buf...)
|
|
|
|
}
|
|
|
|
for i, ele := range vec {
|
2020-11-17 02:07:42 +00:00
|
|
|
buf := make([]byte, 4)
|
2020-11-26 08:01:31 +00:00
|
|
|
binary.LittleEndian.PutUint32(buf, math.Float32bits(ele+float32(i*4)))
|
|
|
|
searchRawData2 = append(searchRawData2, buf...)
|
2020-11-17 02:07:42 +00:00
|
|
|
}
|
2021-04-01 20:39:23 +00:00
|
|
|
|
|
|
|
// generate placeholder
|
2021-01-22 01:36:18 +00:00
|
|
|
placeholderValue := milvuspb.PlaceholderValue{
|
2020-11-17 02:07:42 +00:00
|
|
|
Tag: "$0",
|
2021-03-12 06:22:09 +00:00
|
|
|
Type: milvuspb.PlaceholderType_FloatVector,
|
2020-11-26 08:01:31 +00:00
|
|
|
Values: [][]byte{searchRawData1, searchRawData2},
|
2020-11-17 02:07:42 +00:00
|
|
|
}
|
2021-01-22 01:36:18 +00:00
|
|
|
placeholderGroup := milvuspb.PlaceholderGroup{
|
|
|
|
Placeholders: []*milvuspb.PlaceholderValue{&placeholderValue},
|
2020-11-17 02:07:42 +00:00
|
|
|
}
|
|
|
|
placeGroupByte, err := proto.Marshal(&placeholderGroup)
|
|
|
|
if err != nil {
|
2021-04-01 20:39:23 +00:00
|
|
|
return err
|
2020-11-17 02:07:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// generate searchMsg
|
2020-11-17 06:10:07 +00:00
|
|
|
searchMsg := &msgstream.SearchMsg{
|
2020-11-17 02:07:42 +00:00
|
|
|
BaseMsg: msgstream.BaseMsg{
|
2020-11-30 11:38:23 +00:00
|
|
|
HashValues: []uint32{0},
|
2020-11-17 02:07:42 +00:00
|
|
|
},
|
2021-03-12 06:22:09 +00:00
|
|
|
SearchRequest: internalpb.SearchRequest{
|
2021-01-18 11:32:08 +00:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 06:45:35 +00:00
|
|
|
MsgType: commonpb.MsgType_Search,
|
2021-01-18 11:32:08 +00:00
|
|
|
MsgID: 1,
|
2021-04-01 20:39:23 +00:00
|
|
|
Timestamp: Timestamp(10),
|
2021-01-18 11:32:08 +00:00
|
|
|
SourceID: 1,
|
|
|
|
},
|
2021-05-07 07:20:47 +00:00
|
|
|
ResultChannelID: "0",
|
|
|
|
Dsl: dslString,
|
|
|
|
PlaceholderGroup: placeGroupByte,
|
|
|
|
DslType: commonpb.DslType_Dsl,
|
2020-11-17 02:07:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
msgPackSearch := msgstream.MsgPack{}
|
2020-11-17 06:10:07 +00:00
|
|
|
msgPackSearch.Msgs = append(msgPackSearch.Msgs, searchMsg)
|
2020-11-17 02:07:42 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// produce search message
|
2021-03-25 06:41:46 +00:00
|
|
|
err = searchStream.Produce(&msgPackSearch)
|
2021-04-01 20:39:23 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-26 08:01:31 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
func TestSearch_Search(t *testing.T) {
|
|
|
|
const N = 10000
|
|
|
|
const DIM = 16
|
2020-12-10 08:31:09 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// init queryNode
|
|
|
|
collectionID := UniqueID(0)
|
|
|
|
segmentID := UniqueID(1)
|
|
|
|
node := newQueryNodeMock()
|
|
|
|
initTestMeta(t, node, collectionID, UniqueID(0))
|
2020-12-10 08:31:09 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
msFactory, err := newMessageStreamFactory()
|
2020-12-10 08:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// start search service
|
2021-06-23 12:26:10 +00:00
|
|
|
node.queryService = newQueryService(node.queryNodeLoopCtx,
|
2021-06-15 04:41:40 +00:00
|
|
|
node.historical,
|
|
|
|
node.streaming,
|
2021-05-28 07:40:32 +00:00
|
|
|
msFactory)
|
2020-12-03 11:00:11 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// load segment
|
2021-06-15 04:41:40 +00:00
|
|
|
err = node.historical.replica.addSegment(segmentID, defaultPartitionID, collectionID, "", segmentTypeSealed, true)
|
2021-04-01 20:39:23 +00:00
|
|
|
assert.NoError(t, err)
|
2021-05-28 02:26:30 +00:00
|
|
|
segment, err := node.historical.replica.getSegmentByID(segmentID)
|
2021-04-01 20:39:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = loadFields(segment, DIM, N)
|
|
|
|
assert.NoError(t, err)
|
2021-03-22 08:36:10 +00:00
|
|
|
|
2021-07-24 01:25:22 +00:00
|
|
|
node.queryService.addQueryCollection(collectionID)
|
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
err = sendSearchRequest(node.queryNodeLoopCtx, DIM)
|
|
|
|
assert.NoError(t, err)
|
2021-02-08 06:30:54 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2021-02-08 06:30:54 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
err = node.Stop()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-12-03 11:00:11 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
func TestSearch_SearchMultiSegments(t *testing.T) {
|
|
|
|
const N = 10000
|
2020-12-03 11:00:11 +00:00
|
|
|
const DIM = 16
|
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// init queryNode
|
|
|
|
collectionID := UniqueID(0)
|
|
|
|
segmentID1 := UniqueID(1)
|
|
|
|
segmentID2 := UniqueID(2)
|
|
|
|
node := newQueryNodeMock()
|
|
|
|
initTestMeta(t, node, collectionID, UniqueID(0))
|
2020-12-03 11:00:11 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
msFactory, err := newMessageStreamFactory()
|
|
|
|
assert.NoError(t, err)
|
2020-12-03 11:00:11 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// start search service
|
2021-06-23 12:26:10 +00:00
|
|
|
node.queryService = newQueryService(node.queryNodeLoopCtx,
|
2021-06-15 04:41:40 +00:00
|
|
|
node.historical,
|
|
|
|
node.streaming,
|
2021-05-28 07:40:32 +00:00
|
|
|
msFactory)
|
2021-06-23 12:26:10 +00:00
|
|
|
node.queryService.addQueryCollection(collectionID)
|
2020-12-03 11:00:11 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// load segments
|
2021-06-15 04:41:40 +00:00
|
|
|
err = node.historical.replica.addSegment(segmentID1, defaultPartitionID, collectionID, "", segmentTypeSealed, true)
|
2021-04-01 20:39:23 +00:00
|
|
|
assert.NoError(t, err)
|
2021-05-28 02:26:30 +00:00
|
|
|
segment1, err := node.historical.replica.getSegmentByID(segmentID1)
|
2021-04-01 20:39:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = loadFields(segment1, DIM, N)
|
2020-12-10 08:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-06-15 04:41:40 +00:00
|
|
|
err = node.historical.replica.addSegment(segmentID2, defaultPartitionID, collectionID, "", segmentTypeSealed, true)
|
2021-04-01 20:39:23 +00:00
|
|
|
assert.NoError(t, err)
|
2021-05-28 02:26:30 +00:00
|
|
|
segment2, err := node.historical.replica.getSegmentByID(segmentID2)
|
2020-11-26 08:01:31 +00:00
|
|
|
assert.NoError(t, err)
|
2021-04-01 20:39:23 +00:00
|
|
|
err = loadFields(segment2, DIM, N)
|
2020-11-26 08:01:31 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
err = sendSearchRequest(node.queryNodeLoopCtx, DIM)
|
|
|
|
assert.NoError(t, err)
|
2020-11-26 08:01:31 +00:00
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
2020-11-17 02:07:42 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
err = node.Stop()
|
|
|
|
assert.NoError(t, err)
|
2020-11-17 02:07:42 +00:00
|
|
|
}
|