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"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2021-03-12 06:22:09 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2021-01-22 01:36:18 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/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
|
|
|
|
}
|
2020-12-10 08:31:09 +00:00
|
|
|
searchProducerChannels := Params.SearchChannelNames
|
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 searchRequest
|
|
|
|
searchReq := milvuspb.SearchRequest{
|
2020-11-17 02:07:42 +00:00
|
|
|
Dsl: dslString,
|
|
|
|
PlaceholderGroup: placeGroupByte,
|
|
|
|
}
|
2021-04-01 20:39:23 +00:00
|
|
|
searchReqBytes, err := proto.Marshal(&searchReq)
|
2020-11-17 02:07:42 +00:00
|
|
|
if err != nil {
|
2021-04-01 20:39:23 +00:00
|
|
|
return err
|
2020-11-17 02:07:42 +00:00
|
|
|
}
|
|
|
|
blob := commonpb.Blob{
|
2021-04-01 20:39:23 +00:00
|
|
|
Value: searchReqBytes,
|
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,
|
|
|
|
},
|
|
|
|
ResultChannelID: "0",
|
2020-11-17 02:07:42 +00:00
|
|
|
Query: &blob,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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 sendTimeTick(ctx context.Context) error {
|
|
|
|
// init message stream
|
|
|
|
msFactory, err := newMessageStreamFactory()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-03 11:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate timeTick
|
|
|
|
timeTickMsgPack := msgstream.MsgPack{}
|
|
|
|
baseMsg := msgstream.BaseMsg{
|
2021-04-01 20:39:23 +00:00
|
|
|
BeginTimestamp: Timestamp(20),
|
|
|
|
EndTimestamp: Timestamp(20),
|
2020-12-03 11:00:11 +00:00
|
|
|
HashValues: []uint32{0},
|
|
|
|
}
|
2021-03-12 06:22:09 +00:00
|
|
|
timeTickResult := internalpb.TimeTickMsg{
|
2021-01-18 11:32:08 +00:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 06:45:35 +00:00
|
|
|
MsgType: commonpb.MsgType_TimeTick,
|
2021-01-18 11:32:08 +00:00
|
|
|
MsgID: 0,
|
2021-04-01 20:39:23 +00:00
|
|
|
Timestamp: Timestamp(20),
|
2021-01-18 11:32:08 +00:00
|
|
|
SourceID: 0,
|
|
|
|
},
|
2020-12-03 11:00:11 +00:00
|
|
|
}
|
|
|
|
timeTickMsg := &msgstream.TimeTickMsg{
|
|
|
|
BaseMsg: baseMsg,
|
|
|
|
TimeTickMsg: timeTickResult,
|
|
|
|
}
|
|
|
|
timeTickMsgPack.Msgs = append(timeTickMsgPack.Msgs, timeTickMsg)
|
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// produce timeTick message
|
2020-12-10 08:31:09 +00:00
|
|
|
insertChannels := Params.InsertChannelNames
|
2021-04-01 20:39:23 +00:00
|
|
|
insertStream, _ := msFactory.NewMsgStream(ctx)
|
2021-02-04 06:37:12 +00:00
|
|
|
insertStream.AsProducer(insertChannels)
|
2021-04-01 20:39:23 +00:00
|
|
|
insertStream.Start()
|
2020-12-10 08:31:09 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
err = insertStream.Broadcast(&timeTickMsgPack)
|
|
|
|
return err
|
|
|
|
}
|
2020-12-10 08:31:09 +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 dataSync
|
|
|
|
newDS := newDataSyncService(node.queryNodeLoopCtx, node.replica, msFactory, collectionID)
|
|
|
|
err = node.addDataSyncService(collectionID, newDS)
|
2020-12-03 11:00:11 +00:00
|
|
|
assert.NoError(t, err)
|
2021-04-01 20:39:23 +00:00
|
|
|
ds, err := node.getDataSyncService(collectionID)
|
2020-12-03 11:00:11 +00:00
|
|
|
assert.NoError(t, err)
|
2021-04-01 20:39:23 +00:00
|
|
|
go ds.start()
|
2020-12-03 11:00:11 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// start search service
|
|
|
|
node.searchService = newSearchService(node.queryNodeLoopCtx, node.replica, msFactory)
|
|
|
|
go node.searchService.start()
|
|
|
|
node.searchService.startSearchCollection(collectionID)
|
2020-12-03 11:00:11 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
tSafe := node.replica.getTSafe(collectionID)
|
|
|
|
assert.NotNil(t, tSafe)
|
|
|
|
tSafe.set(1000)
|
2020-12-03 11:00:11 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// load segment
|
|
|
|
err = node.replica.addSegment(segmentID, defaultPartitionID, collectionID, segmentTypeSealed)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
segment, err := node.replica.getSegmentByID(segmentID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = loadFields(segment, DIM, N)
|
|
|
|
assert.NoError(t, err)
|
2021-03-22 08:36:10 +00:00
|
|
|
|
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 dataSync
|
|
|
|
newDS := newDataSyncService(node.queryNodeLoopCtx, node.replica, msFactory, collectionID)
|
|
|
|
err = node.addDataSyncService(collectionID, newDS)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ds, err := node.getDataSyncService(collectionID)
|
2020-12-03 11:00:11 +00:00
|
|
|
assert.NoError(t, err)
|
2021-04-01 20:39:23 +00:00
|
|
|
go ds.start()
|
2020-12-03 11:00:11 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// start search service
|
2021-02-08 06:30:54 +00:00
|
|
|
node.searchService = newSearchService(node.queryNodeLoopCtx, node.replica, msFactory)
|
2020-12-03 11:00:11 +00:00
|
|
|
go node.searchService.start()
|
2021-03-22 21:59:46 +00:00
|
|
|
node.searchService.startSearchCollection(collectionID)
|
2020-12-03 11:00:11 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
tSafe := node.replica.getTSafe(collectionID)
|
|
|
|
assert.NotNil(t, tSafe)
|
|
|
|
tSafe.set(1000)
|
2020-12-10 08:31:09 +00:00
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
// load segments
|
|
|
|
err = node.replica.addSegment(segmentID1, defaultPartitionID, collectionID, segmentTypeSealed)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
segment1, err := node.replica.getSegmentByID(segmentID1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = loadFields(segment1, DIM, N)
|
2020-12-10 08:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-04-01 20:39:23 +00:00
|
|
|
err = node.replica.addSegment(segmentID2, defaultPartitionID, collectionID, segmentTypeSealed)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
segment2, err := node.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
|
|
|
}
|