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
|
|
|
|
2020-12-08 14:41:04 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/etcdpb"
|
2021-01-29 15:22:24 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/querypb"
|
2020-12-08 14:41:04 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/schemapb"
|
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-01-29 15:22:24 +08:00
|
|
|
type queryServiceMock struct{}
|
|
|
|
|
2020-12-08 14:41:04 +08:00
|
|
|
func setup() {
|
2020-11-16 21:10:43 +08:00
|
|
|
Params.Init()
|
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,
|
|
|
|
DataType: schemapb.DataType_VECTOR_BINARY,
|
|
|
|
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,
|
|
|
|
DataType: schemapb.DataType_VECTOR_FLOAT,
|
|
|
|
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,
|
|
|
|
DataType: schemapb.DataType_INT32,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-18 10:38:41 +08:00
|
|
|
var err = node.replica.addCollection(collectionMeta.ID, collectionMeta.Schema)
|
2020-12-08 14:41:04 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-02-03 11:52:19 +08:00
|
|
|
collection, err := node.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)
|
2020-12-08 14:41:04 +08:00
|
|
|
assert.Equal(t, node.replica.getCollectionNum(), 1)
|
|
|
|
|
2021-02-03 18:12:48 +08:00
|
|
|
err = node.replica.addPartition(collection.ID(), collectionMeta.PartitionIDs[0])
|
2020-12-08 14:41:04 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-02-03 18:12:48 +08:00
|
|
|
err = node.replica.addSegment(segmentID, collectionMeta.PartitionIDs[0], collectionID, segTypeGrowing)
|
2020-12-08 14:41:04 +08:00
|
|
|
assert.NoError(t, 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-01-22 11:17:18 +08:00
|
|
|
svr := NewQueryNode(ctx, 0)
|
2021-01-29 15:22:24 +08:00
|
|
|
err := svr.SetQueryService(&queryServiceMock{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
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.DDChannelNames = makeNewChannelNames(Params.DDChannelNames, suffix)
|
|
|
|
Params.InsertChannelNames = makeNewChannelNames(Params.InsertChannelNames, suffix)
|
|
|
|
Params.SearchChannelNames = makeNewChannelNames(Params.SearchChannelNames, suffix)
|
|
|
|
Params.SearchResultChannelNames = makeNewChannelNames(Params.SearchResultChannelNames, suffix)
|
|
|
|
Params.StatsChannelName = Params.StatsChannelName + suffix
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:22:24 +08:00
|
|
|
func (q *queryServiceMock) RegisterNode(req *querypb.RegisterNodeRequest) (*querypb.RegisterNodeResponse, error) {
|
|
|
|
return &querypb.RegisterNodeResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
},
|
|
|
|
InitParams: &internalpb2.InitParams{
|
|
|
|
NodeID: int64(1),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
localNode.Stop()
|
2020-11-16 17:01:10 +08:00
|
|
|
}
|