2020-11-26 16:01:31 +08:00
|
|
|
package querynode
|
2020-10-24 10:45:57 +08:00
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
2020-11-12 17:58:05 +08:00
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-24 10:45:57 +08:00
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2020-11-13 17:20:13 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/etcdpb"
|
2020-11-12 12:04:12 +08:00
|
|
|
internalPb "github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2020-11-13 17:20:13 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/schemapb"
|
2020-11-12 12:04:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// NOTE: start pulsar before test
|
2020-11-19 17:09:22 +08:00
|
|
|
func TestDataSyncService_Start(t *testing.T) {
|
2020-11-17 20:00:23 +08:00
|
|
|
Params.Init()
|
2020-11-12 12:04:12 +08:00
|
|
|
var ctx context.Context
|
|
|
|
|
|
|
|
if closeWithDeadline {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
d := time.Now().Add(ctxTimeInMillisecond * time.Millisecond)
|
|
|
|
ctx, cancel = context.WithDeadline(context.Background(), d)
|
|
|
|
defer cancel()
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
// init query node
|
2020-11-21 15:06:46 +08:00
|
|
|
pulsarURL, _ := Params.pulsarAddress()
|
2020-11-17 20:00:23 +08:00
|
|
|
node := NewQueryNode(ctx, 0)
|
2020-11-12 12:04:12 +08:00
|
|
|
|
|
|
|
// init meta
|
2020-11-13 17:20:13 +08:00
|
|
|
collectionName := "collection0"
|
2020-11-12 12:04:12 +08:00
|
|
|
fieldVec := schemapb.FieldSchema{
|
2020-11-28 19:06:48 +08:00
|
|
|
Name: "vec",
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
DataType: schemapb.DataType_VECTOR_FLOAT,
|
2020-11-12 12:04:12 +08:00
|
|
|
TypeParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "dim",
|
|
|
|
Value: "16",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldInt := schemapb.FieldSchema{
|
2020-11-28 19:06:48 +08:00
|
|
|
Name: "age",
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
DataType: schemapb.DataType_INT32,
|
2020-11-12 12:04:12 +08:00
|
|
|
TypeParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "dim",
|
|
|
|
Value: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
schema := schemapb.CollectionSchema{
|
2020-11-28 19:06:48 +08:00
|
|
|
Name: collectionName,
|
|
|
|
AutoID: true,
|
2020-11-12 12:04:12 +08:00
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
&fieldVec, &fieldInt,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
collectionMeta := etcdpb.CollectionMeta{
|
2020-11-13 15:17:18 +08:00
|
|
|
ID: UniqueID(0),
|
2020-11-12 12:04:12 +08:00
|
|
|
Schema: &schema,
|
|
|
|
CreateTime: Timestamp(0),
|
2020-11-13 15:17:18 +08:00
|
|
|
SegmentIDs: []UniqueID{0},
|
2020-11-12 12:04:12 +08:00
|
|
|
PartitionTags: []string{"default"},
|
|
|
|
}
|
|
|
|
|
|
|
|
collectionMetaBlob := proto.MarshalTextString(&collectionMeta)
|
|
|
|
assert.NotEqual(t, "", collectionMetaBlob)
|
|
|
|
|
2020-11-17 20:00:23 +08:00
|
|
|
var err = (*node.replica).addCollection(&collectionMeta, collectionMetaBlob)
|
2020-11-13 17:20:13 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-11-17 20:00:23 +08:00
|
|
|
collection, err := (*node.replica).getCollectionByName(collectionName)
|
2020-11-13 17:20:13 +08:00
|
|
|
assert.NoError(t, err)
|
2020-11-12 12:04:12 +08:00
|
|
|
assert.Equal(t, collection.meta.Schema.Name, "collection0")
|
2020-11-13 15:17:18 +08:00
|
|
|
assert.Equal(t, collection.meta.ID, UniqueID(0))
|
2020-11-17 20:00:23 +08:00
|
|
|
assert.Equal(t, (*node.replica).getCollectionNum(), 1)
|
2020-11-12 12:04:12 +08:00
|
|
|
|
2020-11-17 20:00:23 +08:00
|
|
|
err = (*node.replica).addPartition(collection.ID(), collectionMeta.PartitionTags[0])
|
2020-11-12 12:04:12 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
segmentID := UniqueID(0)
|
2020-11-17 20:00:23 +08:00
|
|
|
err = (*node.replica).addSegment(segmentID, collectionMeta.PartitionTags[0], UniqueID(0))
|
2020-11-12 12:04:12 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// test data generate
|
|
|
|
const msgLength = 10
|
|
|
|
const DIM = 16
|
|
|
|
const N = 10
|
|
|
|
|
|
|
|
var vec = [DIM]float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
|
|
|
var rawData []byte
|
|
|
|
for _, ele := range vec {
|
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, math.Float32bits(ele))
|
|
|
|
rawData = append(rawData, buf...)
|
|
|
|
}
|
|
|
|
bs := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(bs, 1)
|
|
|
|
rawData = append(rawData, bs...)
|
|
|
|
var records []*commonpb.Blob
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
blob := &commonpb.Blob{
|
|
|
|
Value: rawData,
|
|
|
|
}
|
|
|
|
records = append(records, blob)
|
|
|
|
}
|
|
|
|
|
|
|
|
timeRange := TimeRange{
|
|
|
|
timestampMin: 0,
|
|
|
|
timestampMax: math.MaxUint64,
|
|
|
|
}
|
|
|
|
|
|
|
|
// messages generate
|
2020-11-17 14:10:07 +08:00
|
|
|
insertMessages := make([]msgstream.TsMsg, 0)
|
2020-11-12 12:04:12 +08:00
|
|
|
for i := 0; i < msgLength; i++ {
|
|
|
|
var msg msgstream.TsMsg = &msgstream.InsertMsg{
|
|
|
|
BaseMsg: msgstream.BaseMsg{
|
|
|
|
HashValues: []int32{
|
|
|
|
int32(i), int32(i),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
InsertRequest: internalPb.InsertRequest{
|
|
|
|
MsgType: internalPb.MsgType_kInsert,
|
2020-11-13 15:17:18 +08:00
|
|
|
ReqID: int64(0),
|
2020-11-12 12:04:12 +08:00
|
|
|
CollectionName: "collection0",
|
|
|
|
PartitionTag: "default",
|
2020-11-13 15:17:18 +08:00
|
|
|
SegmentID: int64(0),
|
|
|
|
ChannelID: int64(0),
|
|
|
|
ProxyID: int64(0),
|
2020-11-12 12:04:12 +08:00
|
|
|
Timestamps: []uint64{uint64(i + 1000), uint64(i + 1000)},
|
2020-11-13 15:17:18 +08:00
|
|
|
RowIDs: []int64{int64(i), int64(i)},
|
2020-11-12 12:04:12 +08:00
|
|
|
RowData: []*commonpb.Blob{
|
|
|
|
{Value: rawData},
|
|
|
|
{Value: rawData},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-11-17 14:10:07 +08:00
|
|
|
insertMessages = append(insertMessages, msg)
|
2020-11-12 12:04:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
msgPack := msgstream.MsgPack{
|
|
|
|
BeginTs: timeRange.timestampMin,
|
|
|
|
EndTs: timeRange.timestampMax,
|
|
|
|
Msgs: insertMessages,
|
|
|
|
}
|
|
|
|
|
2020-11-19 17:09:22 +08:00
|
|
|
// generate timeTick
|
|
|
|
timeTickMsgPack := msgstream.MsgPack{}
|
|
|
|
baseMsg := msgstream.BaseMsg{
|
|
|
|
BeginTimestamp: 0,
|
|
|
|
EndTimestamp: 0,
|
|
|
|
HashValues: []int32{0},
|
|
|
|
}
|
|
|
|
timeTickResult := internalPb.TimeTickMsg{
|
|
|
|
MsgType: internalPb.MsgType_kTimeTick,
|
|
|
|
PeerID: UniqueID(0),
|
|
|
|
Timestamp: math.MaxUint64,
|
|
|
|
}
|
|
|
|
timeTickMsg := &msgstream.TimeTickMsg{
|
|
|
|
BaseMsg: baseMsg,
|
|
|
|
TimeTickMsg: timeTickResult,
|
|
|
|
}
|
|
|
|
timeTickMsgPack.Msgs = append(timeTickMsgPack.Msgs, timeTickMsg)
|
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
// pulsar produce
|
|
|
|
const receiveBufSize = 1024
|
2020-11-26 16:01:31 +08:00
|
|
|
producerChannels := Params.insertChannelNames()
|
2020-11-12 12:04:12 +08:00
|
|
|
|
|
|
|
insertStream := msgstream.NewPulsarMsgStream(ctx, receiveBufSize)
|
2020-11-18 19:46:18 +08:00
|
|
|
insertStream.SetPulsarClient(pulsarURL)
|
2020-11-12 12:04:12 +08:00
|
|
|
insertStream.CreatePulsarProducers(producerChannels)
|
|
|
|
|
|
|
|
var insertMsgStream msgstream.MsgStream = insertStream
|
|
|
|
insertMsgStream.Start()
|
2020-11-19 17:09:22 +08:00
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
err = insertMsgStream.Produce(&msgPack)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-11-19 17:09:22 +08:00
|
|
|
err = insertMsgStream.Broadcast(&timeTickMsgPack)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
// dataSync
|
2020-11-17 20:00:23 +08:00
|
|
|
node.dataSyncService = newDataSyncService(node.ctx, node.replica)
|
2020-11-12 12:04:12 +08:00
|
|
|
go node.dataSyncService.start()
|
|
|
|
|
|
|
|
node.Close()
|
|
|
|
|
2020-11-12 17:58:05 +08:00
|
|
|
<-ctx.Done()
|
2020-11-12 12:04:12 +08:00
|
|
|
}
|