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-08-25 15:45:19 +08:00
|
|
|
|
2020-09-02 10:38:08 +08:00
|
|
|
/*
|
|
|
|
|
2020-10-23 18:01:24 +08:00
|
|
|
#cgo CFLAGS: -I${SRCDIR}/../core/output/include
|
2020-09-02 10:38:08 +08:00
|
|
|
|
2020-10-31 15:11:47 +08:00
|
|
|
#cgo LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_segcore -Wl,-rpath=${SRCDIR}/../core/output/lib
|
2020-09-02 10:38:08 +08:00
|
|
|
|
2020-11-25 10:31:51 +08:00
|
|
|
#include "segcore/collection_c.h"
|
|
|
|
#include "segcore/segment_c.h"
|
2021-04-16 14:02:49 +08:00
|
|
|
#include "segcore/segcore_init_c.h"
|
2020-09-02 10:38:08 +08:00
|
|
|
|
|
|
|
*/
|
2020-08-25 15:45:19 +08:00
|
|
|
import "C"
|
2020-09-02 10:38:08 +08:00
|
|
|
|
2020-08-25 15:45:19 +08:00
|
|
|
import (
|
2020-10-15 21:31:50 +08:00
|
|
|
"context"
|
2021-03-22 16:36:10 +08:00
|
|
|
"errors"
|
2021-06-21 18:22:13 +08:00
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2021-06-19 11:45:09 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/kv"
|
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
"go.uber.org/zap"
|
2021-01-11 18:35:54 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2021-05-21 19:28:52 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/sessionutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2020-08-25 15:45:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type QueryNode struct {
|
2020-12-08 14:41:04 +08:00
|
|
|
queryNodeLoopCtx context.Context
|
2020-12-10 16:31:09 +08:00
|
|
|
queryNodeLoopCancel context.CancelFunc
|
2020-10-15 21:31:50 +08:00
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
QueryNodeID UniqueID
|
2021-01-21 15:20:23 +08:00
|
|
|
stateCode atomic.Value
|
2020-08-25 15:45:19 +08:00
|
|
|
|
2021-05-28 10:26:30 +08:00
|
|
|
// internal components
|
|
|
|
historical *historical
|
|
|
|
streaming *streaming
|
2020-08-25 15:45:19 +08:00
|
|
|
|
2021-01-15 15:28:54 +08:00
|
|
|
// internal services
|
2021-05-31 18:08:32 +08:00
|
|
|
searchService *searchService
|
|
|
|
retrieveService *retrieveService
|
2021-01-18 10:09:17 +08:00
|
|
|
|
2021-01-26 13:41:41 +08:00
|
|
|
// clients
|
2021-06-21 17:28:03 +08:00
|
|
|
rootCoord types.RootCoord
|
|
|
|
queryService types.QueryService
|
|
|
|
indexCoord types.IndexCoord
|
2021-06-21 18:22:13 +08:00
|
|
|
dataCoord types.DataCoord
|
2021-02-08 14:30:54 +08:00
|
|
|
|
|
|
|
msFactory msgstream.Factory
|
2021-04-12 09:18:43 +08:00
|
|
|
scheduler *taskScheduler
|
2021-05-21 19:28:52 +08:00
|
|
|
|
|
|
|
session *sessionutil.Session
|
2021-06-19 11:45:09 +08:00
|
|
|
|
|
|
|
minioKV kv.BaseKV // minio minioKV
|
|
|
|
etcdKV *etcdkv.EtcdKV
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
2020-09-07 17:01:46 +08:00
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
func NewQueryNode(ctx context.Context, queryNodeID UniqueID, factory msgstream.Factory) *QueryNode {
|
2021-03-08 15:25:55 +08:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2020-12-08 14:41:04 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2021-01-21 15:20:23 +08:00
|
|
|
node := &QueryNode{
|
2021-01-11 18:35:54 +08:00
|
|
|
queryNodeLoopCtx: ctx1,
|
|
|
|
queryNodeLoopCancel: cancel,
|
|
|
|
QueryNodeID: queryNodeID,
|
2021-05-28 10:26:30 +08:00
|
|
|
searchService: nil,
|
2021-05-31 18:08:32 +08:00
|
|
|
retrieveService: nil,
|
2021-05-28 10:26:30 +08:00
|
|
|
msFactory: factory,
|
2021-01-11 18:35:54 +08:00
|
|
|
}
|
|
|
|
|
2021-04-12 09:18:43 +08:00
|
|
|
node.scheduler = newTaskScheduler(ctx1)
|
2021-03-12 14:22:09 +08:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Abnormal)
|
2021-05-28 10:26:30 +08:00
|
|
|
|
2021-01-21 15:20:23 +08:00
|
|
|
return node
|
|
|
|
}
|
2020-11-13 17:20:13 +08:00
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
func NewQueryNodeWithoutID(ctx context.Context, factory msgstream.Factory) *QueryNode {
|
2021-01-27 09:50:52 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
node := &QueryNode{
|
|
|
|
queryNodeLoopCtx: ctx1,
|
|
|
|
queryNodeLoopCancel: cancel,
|
2021-05-28 10:26:30 +08:00
|
|
|
searchService: nil,
|
2021-05-31 18:08:32 +08:00
|
|
|
retrieveService: nil,
|
2021-05-28 10:26:30 +08:00
|
|
|
msFactory: factory,
|
2021-01-27 09:50:52 +08:00
|
|
|
}
|
|
|
|
|
2021-04-12 09:18:43 +08:00
|
|
|
node.scheduler = newTaskScheduler(ctx1)
|
2021-03-12 14:22:09 +08:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Abnormal)
|
2021-01-27 09:50:52 +08:00
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
return node
|
2020-09-15 15:53:10 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 15:06:05 +08:00
|
|
|
// Register register query node at etcd
|
|
|
|
func (node *QueryNode) Register() error {
|
2021-06-11 22:04:41 +08:00
|
|
|
node.session = sessionutil.NewSession(node.queryNodeLoopCtx, Params.MetaRootPath, Params.EtcdEndpoints)
|
2021-05-25 15:06:05 +08:00
|
|
|
node.session.Init(typeutil.QueryNodeRole, Params.QueryNodeIP+":"+strconv.FormatInt(Params.QueryNodePort, 10), false)
|
|
|
|
Params.QueryNodeID = node.session.ServerID
|
2021-06-19 11:45:09 +08:00
|
|
|
log.Debug("query nodeID", zap.Int64("nodeID", Params.QueryNodeID))
|
2021-06-19 12:38:06 +08:00
|
|
|
|
|
|
|
// This param needs valid QueryNodeID
|
|
|
|
Params.initMsgChannelSubName()
|
2021-05-25 15:06:05 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:01:29 +08:00
|
|
|
func (node *QueryNode) Init() error {
|
2021-06-19 11:45:09 +08:00
|
|
|
//ctx := context.Background()
|
|
|
|
connectEtcdFn := func() error {
|
|
|
|
etcdClient, err := clientv3.New(clientv3.Config{Endpoints: Params.EtcdEndpoints})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
etcdKV := etcdkv.NewEtcdKV(etcdClient, Params.MetaRootPath)
|
|
|
|
node.etcdKV = etcdKV
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("queryNode try to connect etcd")
|
|
|
|
err := retry.Retry(100000, time.Millisecond*200, connectEtcdFn)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("queryNode try to connect etcd failed", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("queryNode try to connect etcd success")
|
2021-05-14 10:05:18 +08:00
|
|
|
|
2021-05-28 10:26:30 +08:00
|
|
|
node.historical = newHistorical(node.queryNodeLoopCtx,
|
2021-06-21 17:28:03 +08:00
|
|
|
node.rootCoord,
|
2021-06-21 18:22:13 +08:00
|
|
|
node.dataCoord,
|
2021-06-21 17:28:03 +08:00
|
|
|
node.indexCoord,
|
2021-06-19 11:45:09 +08:00
|
|
|
node.msFactory,
|
|
|
|
node.etcdKV)
|
|
|
|
node.streaming = newStreaming(node.queryNodeLoopCtx, node.msFactory, node.etcdKV)
|
2021-05-28 10:26:30 +08:00
|
|
|
|
2021-04-16 14:02:49 +08:00
|
|
|
C.SegcoreInit()
|
2021-06-19 11:45:09 +08:00
|
|
|
//registerReq := &queryPb.RegisterNodeRequest{
|
|
|
|
// Base: &commonpb.MsgBase{
|
|
|
|
// SourceID: Params.QueryNodeID,
|
|
|
|
// },
|
|
|
|
// Address: &commonpb.Address{
|
|
|
|
// Ip: Params.QueryNodeIP,
|
|
|
|
// Port: Params.QueryNodePort,
|
|
|
|
// },
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//resp, err := node.queryService.RegisterNode(ctx, registerReq)
|
|
|
|
//if err != nil {
|
|
|
|
// log.Debug("QueryNode RegisterNode failed", zap.Error(err))
|
|
|
|
// panic(err)
|
|
|
|
//}
|
|
|
|
//if resp.Status.ErrorCode != commonpb.ErrorCode_Success {
|
|
|
|
// log.Debug("QueryNode RegisterNode failed", zap.Any("Reason", resp.Status.Reason))
|
|
|
|
// panic(resp.Status.Reason)
|
|
|
|
//}
|
|
|
|
//log.Debug("QueryNode RegisterNode success")
|
|
|
|
//
|
|
|
|
//for _, kv := range resp.InitParams.StartParams {
|
|
|
|
// switch kv.Key {
|
|
|
|
// case "StatsChannelName":
|
|
|
|
// Params.StatsChannelName = kv.Value
|
|
|
|
// case "TimeTickChannelName":
|
|
|
|
// Params.QueryTimeTickChannelName = kv.Value
|
|
|
|
// case "SearchChannelName":
|
|
|
|
// Params.SearchChannelNames = append(Params.SearchChannelNames, kv.Value)
|
|
|
|
// case "SearchResultChannelName":
|
|
|
|
// Params.SearchResultChannelNames = append(Params.SearchResultChannelNames, kv.Value)
|
|
|
|
// default:
|
|
|
|
// return fmt.Errorf("Invalid key: %v", kv.Key)
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//log.Debug("QueryNode Init ", zap.Int64("QueryNodeID", Params.QueryNodeID), zap.Any("searchChannelNames", Params.SearchChannelNames))
|
2021-01-21 15:20:23 +08:00
|
|
|
|
2021-06-21 17:28:03 +08:00
|
|
|
if node.rootCoord == nil {
|
|
|
|
log.Error("null rootCoord detected")
|
2021-01-30 16:02:10 +08:00
|
|
|
}
|
|
|
|
|
2021-06-21 17:28:03 +08:00
|
|
|
if node.indexCoord == nil {
|
|
|
|
log.Error("null indexCoord detected")
|
2021-01-26 13:41:41 +08:00
|
|
|
}
|
|
|
|
|
2021-06-21 18:22:13 +08:00
|
|
|
if node.dataCoord == nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error("null data service detected")
|
2021-01-26 13:41:41 +08:00
|
|
|
}
|
|
|
|
|
2021-01-30 16:02:10 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *QueryNode) Start() error {
|
2021-02-08 14:30:54 +08:00
|
|
|
var err error
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"PulsarAddress": Params.PulsarAddress,
|
|
|
|
"ReceiveBufSize": 1024,
|
|
|
|
"PulsarBufSize": 1024}
|
|
|
|
err = node.msFactory.SetParams(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-19 11:37:16 +08:00
|
|
|
// init services and manager
|
2021-05-28 10:26:30 +08:00
|
|
|
// TODO: pass node.streaming.replica to search service
|
2021-05-28 15:40:32 +08:00
|
|
|
node.searchService = newSearchService(node.queryNodeLoopCtx,
|
2021-06-15 12:41:40 +08:00
|
|
|
node.historical,
|
|
|
|
node.streaming,
|
2021-05-28 15:40:32 +08:00
|
|
|
node.msFactory)
|
2020-09-15 15:53:10 +08:00
|
|
|
|
2021-05-31 18:08:32 +08:00
|
|
|
node.retrieveService = newRetrieveService(node.queryNodeLoopCtx,
|
2021-06-15 12:41:40 +08:00
|
|
|
node.historical,
|
|
|
|
node.streaming,
|
2021-05-31 18:08:32 +08:00
|
|
|
node.msFactory,
|
|
|
|
)
|
|
|
|
|
2021-04-12 09:18:43 +08:00
|
|
|
// start task scheduler
|
|
|
|
go node.scheduler.Start()
|
|
|
|
|
2021-01-19 11:37:16 +08:00
|
|
|
// start services
|
2021-05-31 18:08:32 +08:00
|
|
|
go node.retrieveService.start()
|
2021-05-28 10:26:30 +08:00
|
|
|
go node.historical.start()
|
2021-03-12 14:22:09 +08:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Healthy)
|
2021-01-21 10:01:29 +08:00
|
|
|
return nil
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
2020-09-15 15:53:10 +08:00
|
|
|
|
2021-01-21 10:01:29 +08:00
|
|
|
func (node *QueryNode) Stop() error {
|
2021-03-12 14:22:09 +08:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Abnormal)
|
2020-12-08 14:41:04 +08:00
|
|
|
node.queryNodeLoopCancel()
|
|
|
|
|
2020-11-24 16:12:39 +08:00
|
|
|
// close services
|
2021-05-28 10:26:30 +08:00
|
|
|
if node.historical != nil {
|
|
|
|
node.historical.close()
|
|
|
|
}
|
|
|
|
if node.streaming != nil {
|
|
|
|
node.streaming.close()
|
2020-11-24 16:12:39 +08:00
|
|
|
}
|
|
|
|
if node.searchService != nil {
|
2020-12-08 14:41:04 +08:00
|
|
|
node.searchService.close()
|
2020-11-24 16:12:39 +08:00
|
|
|
}
|
2021-05-31 18:08:32 +08:00
|
|
|
if node.retrieveService != nil {
|
|
|
|
node.retrieveService.close()
|
|
|
|
}
|
2021-01-21 10:01:29 +08:00
|
|
|
return nil
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (node *QueryNode) UpdateStateCode(code internalpb.StateCode) {
|
2021-02-23 11:40:30 +08:00
|
|
|
node.stateCode.Store(code)
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:28:03 +08:00
|
|
|
func (node *QueryNode) SetRootCoord(rc types.RootCoord) error {
|
|
|
|
if rc == nil {
|
|
|
|
return errors.New("null root coord interface")
|
2021-01-27 14:41:56 +08:00
|
|
|
}
|
2021-06-21 17:28:03 +08:00
|
|
|
node.rootCoord = rc
|
2021-01-27 14:41:56 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
func (node *QueryNode) SetQueryService(query types.QueryService) error {
|
2021-01-27 09:50:52 +08:00
|
|
|
if query == nil {
|
2021-01-27 14:41:56 +08:00
|
|
|
return errors.New("null query service interface")
|
2021-01-27 09:50:52 +08:00
|
|
|
}
|
2021-03-08 10:09:48 +08:00
|
|
|
node.queryService = query
|
2021-01-27 09:50:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:28:03 +08:00
|
|
|
func (node *QueryNode) SetIndexCoord(index types.IndexCoord) error {
|
2021-01-26 13:41:41 +08:00
|
|
|
if index == nil {
|
2021-06-21 17:28:03 +08:00
|
|
|
return errors.New("null indexCoord interface")
|
2021-01-26 13:41:41 +08:00
|
|
|
}
|
2021-06-21 17:28:03 +08:00
|
|
|
node.indexCoord = index
|
2021-01-26 13:41:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 18:22:13 +08:00
|
|
|
func (node *QueryNode) SetDataCoord(data types.DataCoord) error {
|
2021-01-26 13:41:41 +08:00
|
|
|
if data == nil {
|
|
|
|
return errors.New("null data service interface")
|
|
|
|
}
|
2021-06-21 18:22:13 +08:00
|
|
|
node.dataCoord = data
|
2021-01-26 13:41:41 +08:00
|
|
|
return nil
|
|
|
|
}
|