2021-04-19 13:47:10 +08: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 10:12:14 +08:00
|
|
|
package querynode
|
2020-09-12 16:57:37 +08:00
|
|
|
|
2020-11-16 17:01:10 +08:00
|
|
|
import (
|
|
|
|
"context"
|
2020-12-10 16:31:09 +08:00
|
|
|
"math/rand"
|
2020-12-08 14:41:04 +08:00
|
|
|
"os"
|
2020-12-10 16:31:09 +08:00
|
|
|
"strconv"
|
2020-11-16 17:01:10 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-12-08 14:41:04 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-10 16:31:09 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/etcdpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/schemapb"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2020-11-16 17:01:10 +08:00
|
|
|
)
|
|
|
|
|
2021-01-06 18:19:44 +08:00
|
|
|
const ctxTimeInMillisecond = 5000
|
2021-02-05 10:53:11 +08:00
|
|
|
const debug = false
|
2020-11-16 17:01:10 +08:00
|
|
|
|
2021-02-03 18:12:48 +08:00
|
|
|
const defaultPartitionID = UniqueID(2021)
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
type queryServiceMock struct {
|
|
|
|
types.QueryService
|
|
|
|
}
|
2021-01-29 15:22:24 +08:00
|
|
|
|
2020-12-08 14:41:04 +08:00
|
|
|
func setup() {
|
2021-02-23 11:40:30 +08:00
|
|
|
os.Setenv("QUERY_NODE_ID", "1")
|
2020-11-16 21:10:43 +08:00
|
|
|
Params.Init()
|
2021-02-23 11:40:30 +08:00
|
|
|
//Params.QueryNodeID = 1
|
2021-02-18 16:26:02 +08:00
|
|
|
Params.initQueryTimeTickChannelName()
|
|
|
|
Params.initStatsChannelName()
|
2020-12-22 22:08:03 +08:00
|
|
|
Params.MetaRootPath = "/etcd/test/root/querynode"
|
2020-12-08 14:41:04 +08:00
|
|
|
}
|
|
|
|
|
2021-02-03 18:12:48 +08:00
|
|
|
func genTestCollectionMeta(collectionID UniqueID, isBinary bool) *etcdpb.CollectionInfo {
|
2021-01-06 18:19:44 +08:00
|
|
|
var fieldVec schemapb.FieldSchema
|
|
|
|
if isBinary {
|
|
|
|
fieldVec = schemapb.FieldSchema{
|
|
|
|
FieldID: UniqueID(100),
|
|
|
|
Name: "vec",
|
|
|
|
IsPrimaryKey: false,
|
2021-03-12 14:22:09 +08:00
|
|
|
DataType: schemapb.DataType_BinaryVector,
|
2021-01-06 18:19:44 +08:00
|
|
|
TypeParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "dim",
|
2021-01-12 18:03:24 +08:00
|
|
|
Value: "128",
|
2021-01-06 18:19:44 +08:00
|
|
|
},
|
2020-12-08 14:41:04 +08:00
|
|
|
},
|
2021-01-06 18:19:44 +08:00
|
|
|
IndexParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "metric_type",
|
|
|
|
Value: "JACCARD",
|
|
|
|
},
|
2020-12-08 14:41:04 +08:00
|
|
|
},
|
2021-01-06 18:19:44 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fieldVec = schemapb.FieldSchema{
|
|
|
|
FieldID: UniqueID(100),
|
|
|
|
Name: "vec",
|
|
|
|
IsPrimaryKey: false,
|
2021-03-12 14:22:09 +08:00
|
|
|
DataType: schemapb.DataType_FloatVector,
|
2021-01-06 18:19:44 +08:00
|
|
|
TypeParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "dim",
|
|
|
|
Value: "16",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "metric_type",
|
|
|
|
Value: "L2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-12-08 14:41:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fieldInt := schemapb.FieldSchema{
|
2021-01-04 10:13:01 +08:00
|
|
|
FieldID: UniqueID(101),
|
2020-12-08 14:41:04 +08:00
|
|
|
Name: "age",
|
|
|
|
IsPrimaryKey: false,
|
2021-03-12 14:22:09 +08:00
|
|
|
DataType: schemapb.DataType_Int32,
|
2020-12-08 14:41:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
schema := schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
&fieldVec, &fieldInt,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:12:48 +08:00
|
|
|
collectionMeta := etcdpb.CollectionInfo{
|
|
|
|
ID: collectionID,
|
|
|
|
Schema: &schema,
|
|
|
|
CreateTime: Timestamp(0),
|
|
|
|
PartitionIDs: []UniqueID{defaultPartitionID},
|
2020-12-08 14:41:04 +08:00
|
|
|
}
|
2021-01-04 10:13:01 +08:00
|
|
|
|
2020-12-08 14:41:04 +08:00
|
|
|
return &collectionMeta
|
|
|
|
}
|
|
|
|
|
2021-02-03 11:52:19 +08:00
|
|
|
func initTestMeta(t *testing.T, node *QueryNode, collectionID UniqueID, segmentID UniqueID, optional ...bool) {
|
2021-01-12 18:03:24 +08:00
|
|
|
isBinary := false
|
|
|
|
if len(optional) > 0 {
|
|
|
|
isBinary = optional[0]
|
|
|
|
}
|
2021-02-03 11:52:19 +08:00
|
|
|
collectionMeta := genTestCollectionMeta(collectionID, isBinary)
|
2020-12-08 14:41:04 +08:00
|
|
|
|
2021-05-28 10:26:30 +08:00
|
|
|
var err = node.historical.replica.addCollection(collectionMeta.ID, collectionMeta.Schema)
|
2020-12-08 14:41:04 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-05-28 10:26:30 +08:00
|
|
|
collection, err := node.historical.replica.getCollectionByID(collectionID)
|
2020-12-08 14:41:04 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-10 16:31:09 +08:00
|
|
|
assert.Equal(t, collection.ID(), collectionID)
|
2021-05-28 10:26:30 +08:00
|
|
|
assert.Equal(t, node.historical.replica.getCollectionNum(), 1)
|
2020-12-08 14:41:04 +08:00
|
|
|
|
2021-05-28 10:26:30 +08:00
|
|
|
err = node.historical.replica.addPartition(collection.ID(), collectionMeta.PartitionIDs[0])
|
2020-12-08 14:41:04 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-06-15 12:41:40 +08:00
|
|
|
err = node.historical.replica.addSegment(segmentID, collectionMeta.PartitionIDs[0], collectionID, "", segmentTypeGrowing, true)
|
2020-12-08 14:41:04 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func initSearchChannel(ctx context.Context, searchChan string, resultChan string, node *QueryNode) {
|
|
|
|
searchReq := &querypb.AddQueryChannelRequest{
|
2021-02-09 17:09:26 +08:00
|
|
|
RequestChannelID: searchChan,
|
|
|
|
ResultChannelID: resultChan,
|
|
|
|
}
|
2021-03-12 14:22:09 +08:00
|
|
|
_, err := node.AddQueryChannel(ctx, searchReq)
|
2021-02-09 17:09:26 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:28:54 +08:00
|
|
|
func newQueryNodeMock() *QueryNode {
|
2020-11-16 17:01:10 +08:00
|
|
|
|
|
|
|
var ctx context.Context
|
2020-12-08 14:41:04 +08:00
|
|
|
|
2021-02-05 10:53:11 +08:00
|
|
|
if debug {
|
|
|
|
ctx = context.Background()
|
|
|
|
} else {
|
2020-11-16 17:01:10 +08:00
|
|
|
var cancel context.CancelFunc
|
|
|
|
d := time.Now().Add(ctxTimeInMillisecond * time.Millisecond)
|
|
|
|
ctx, cancel = context.WithDeadline(context.Background(), d)
|
2020-12-10 16:31:09 +08:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
cancel()
|
|
|
|
}()
|
2020-11-16 17:01:10 +08:00
|
|
|
}
|
|
|
|
|
2021-06-09 11:37:55 +08:00
|
|
|
msFactory, err := newMessageStreamFactory()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-02-23 11:40:30 +08:00
|
|
|
svr := NewQueryNode(ctx, Params.QueryNodeID, msFactory)
|
2021-06-09 11:37:55 +08:00
|
|
|
err = svr.SetQueryService(&queryServiceMock{})
|
2021-01-29 15:22:24 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-05-28 10:26:30 +08:00
|
|
|
svr.historical = newHistorical(svr.queryNodeLoopCtx, nil, nil, nil, svr.msFactory)
|
2021-06-09 11:37:55 +08:00
|
|
|
svr.streaming = newStreaming(ctx, msFactory)
|
2021-01-29 15:22:24 +08:00
|
|
|
|
2020-12-08 14:41:04 +08:00
|
|
|
return svr
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:31:09 +08:00
|
|
|
func makeNewChannelNames(names []string, suffix string) []string {
|
|
|
|
var ret []string
|
|
|
|
for _, name := range names {
|
|
|
|
ret = append(ret, name+suffix)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshChannelNames() {
|
2020-12-24 20:55:40 +08:00
|
|
|
suffix := "-test-query-node" + strconv.FormatInt(rand.Int63n(1000000), 10)
|
2020-12-10 16:31:09 +08:00
|
|
|
Params.StatsChannelName = Params.StatsChannelName + suffix
|
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (q *queryServiceMock) RegisterNode(ctx context.Context, req *querypb.RegisterNodeRequest) (*querypb.RegisterNodeResponse, error) {
|
2021-01-29 15:22:24 +08:00
|
|
|
return &querypb.RegisterNodeResponse{
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-01-29 15:22:24 +08:00
|
|
|
},
|
2021-03-12 14:22:09 +08:00
|
|
|
InitParams: &internalpb.InitParams{
|
2021-01-29 15:22:24 +08:00
|
|
|
NodeID: int64(1),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 04:39:23 +08:00
|
|
|
func newMessageStreamFactory() (msgstream.Factory, error) {
|
|
|
|
const receiveBufSize = 1024
|
|
|
|
|
|
|
|
pulsarURL := Params.PulsarAddress
|
|
|
|
msFactory := msgstream.NewPmsFactory()
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"receiveBufSize": receiveBufSize,
|
|
|
|
"pulsarAddress": pulsarURL,
|
|
|
|
"pulsarBufSize": 1024}
|
|
|
|
err := msFactory.SetParams(m)
|
|
|
|
return msFactory, err
|
|
|
|
}
|
|
|
|
|
2020-12-08 14:41:04 +08:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
setup()
|
2020-12-10 16:31:09 +08:00
|
|
|
refreshChannelNames()
|
2020-12-08 14:41:04 +08:00
|
|
|
exitCode := m.Run()
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: start pulsar and etcd before test
|
|
|
|
func TestQueryNode_Start(t *testing.T) {
|
2021-01-15 15:28:54 +08:00
|
|
|
localNode := newQueryNodeMock()
|
2021-01-21 15:20:23 +08:00
|
|
|
localNode.Start()
|
2021-02-23 11:40:30 +08:00
|
|
|
<-localNode.queryNodeLoopCtx.Done()
|
2021-01-21 15:20:23 +08:00
|
|
|
localNode.Stop()
|
2020-11-16 17:01:10 +08:00
|
|
|
}
|