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"
|
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-01-11 18:35:54 +08:00
|
|
|
"fmt"
|
2021-03-08 15:25:55 +08:00
|
|
|
"math/rand"
|
2021-03-25 04:40:59 +08:00
|
|
|
"sync"
|
2021-01-21 15:20:23 +08:00
|
|
|
"sync/atomic"
|
2021-03-08 15:25:55 +08:00
|
|
|
"time"
|
2021-01-11 18:35:54 +08:00
|
|
|
|
2021-03-05 09:21:35 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
2021-02-09 15:57:10 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2021-01-15 15:28:54 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2021-03-12 14:22:09 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2021-01-15 15:28:54 +08:00
|
|
|
queryPb "github.com/zilliztech/milvus-distributed/internal/proto/querypb"
|
2021-03-22 16:36:10 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/types"
|
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-03-05 16:52:45 +08:00
|
|
|
replica ReplicaInterface
|
2020-08-25 15:45:19 +08:00
|
|
|
|
2021-01-15 15:28:54 +08:00
|
|
|
// internal services
|
2021-03-22 16:36:10 +08:00
|
|
|
metaService *metaService
|
|
|
|
searchService *searchService
|
|
|
|
loadService *loadService
|
|
|
|
statsService *statsService
|
2021-03-25 04:40:59 +08:00
|
|
|
dsServicesMu sync.Mutex // guards dataSyncServices
|
|
|
|
dataSyncServices map[UniqueID]*dataSyncService
|
2021-01-18 10:09:17 +08:00
|
|
|
|
2021-01-26 13:41:41 +08:00
|
|
|
// clients
|
2021-03-08 10:09:48 +08:00
|
|
|
masterService types.MasterService
|
|
|
|
queryService types.QueryService
|
|
|
|
indexService types.IndexService
|
|
|
|
dataService types.DataService
|
2021-02-08 14:30:54 +08:00
|
|
|
|
|
|
|
msFactory msgstream.Factory
|
2021-04-12 09:18:43 +08:00
|
|
|
scheduler *taskScheduler
|
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-03-22 16:36:10 +08:00
|
|
|
dataSyncServices: make(map[UniqueID]*dataSyncService),
|
|
|
|
metaService: nil,
|
|
|
|
searchService: nil,
|
|
|
|
statsService: nil,
|
2021-02-08 14:30:54 +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-05 16:52:45 +08:00
|
|
|
node.replica = newCollectionReplica()
|
2021-03-12 14:22:09 +08:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Abnormal)
|
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-03-22 16:36:10 +08:00
|
|
|
dataSyncServices: make(map[UniqueID]*dataSyncService),
|
|
|
|
metaService: nil,
|
|
|
|
searchService: nil,
|
|
|
|
statsService: nil,
|
2021-02-08 14:30:54 +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-05 16:52:45 +08:00
|
|
|
node.replica = newCollectionReplica()
|
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-01-21 10:01:29 +08:00
|
|
|
func (node *QueryNode) Init() error {
|
2021-02-26 17:44:24 +08:00
|
|
|
ctx := context.Background()
|
2021-01-22 14:28:06 +08:00
|
|
|
registerReq := &queryPb.RegisterNodeRequest{
|
2021-02-18 16:26:02 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
SourceID: Params.QueryNodeID,
|
|
|
|
},
|
2021-01-21 15:20:23 +08:00
|
|
|
Address: &commonpb.Address{
|
|
|
|
Ip: Params.QueryNodeIP,
|
|
|
|
Port: Params.QueryNodePort,
|
|
|
|
},
|
|
|
|
}
|
2021-01-27 09:50:52 +08:00
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
resp, err := node.queryService.RegisterNode(ctx, registerReq)
|
2021-01-21 15:20:23 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_Success {
|
2021-02-18 16:26:02 +08:00
|
|
|
panic(resp.Status.Reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range resp.InitParams.StartParams {
|
|
|
|
switch kv.Key {
|
|
|
|
case "StatsChannelName":
|
|
|
|
Params.StatsChannelName = kv.Value
|
|
|
|
case "TimeTickChannelName":
|
|
|
|
Params.QueryTimeTickChannelName = kv.Value
|
|
|
|
case "QueryChannelName":
|
|
|
|
Params.SearchChannelNames = append(Params.SearchChannelNames, kv.Value)
|
|
|
|
case "QueryResultChannelName":
|
|
|
|
Params.SearchResultChannelNames = append(Params.SearchResultChannelNames, kv.Value)
|
|
|
|
default:
|
2021-03-05 10:15:27 +08:00
|
|
|
return fmt.Errorf("Invalid key: %v", kv.Key)
|
2021-02-18 16:26:02 +08:00
|
|
|
}
|
2021-01-21 15:20:23 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Debug("", zap.Int64("QueryNodeID", Params.QueryNodeID))
|
2021-01-21 15:20:23 +08:00
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
if node.masterService == nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error("null master service detected")
|
2021-01-30 16:02:10 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
if node.indexService == nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error("null index service detected")
|
2021-01-26 13:41:41 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
if node.dataService == 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-02-08 14:30:54 +08:00
|
|
|
node.searchService = newSearchService(node.queryNodeLoopCtx, node.replica, node.msFactory)
|
2021-03-22 16:36:10 +08:00
|
|
|
node.loadService = newLoadService(node.queryNodeLoopCtx, node.masterService, node.dataService, node.indexService, node.replica)
|
2021-02-08 14:30:54 +08:00
|
|
|
node.statsService = newStatsService(node.queryNodeLoopCtx, node.replica, node.loadService.segLoader.indexLoader.fieldStatsChan, node.msFactory)
|
2020-09-15 15:53:10 +08:00
|
|
|
|
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
|
2020-11-19 17:09:22 +08:00
|
|
|
go node.searchService.start()
|
2021-01-30 16:02:10 +08:00
|
|
|
go node.loadService.start()
|
2020-12-08 14:41:04 +08:00
|
|
|
go node.statsService.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
|
|
|
// free collectionReplica
|
2020-12-08 14:41:04 +08:00
|
|
|
node.replica.freeAll()
|
2020-11-24 16:12:39 +08:00
|
|
|
|
|
|
|
// close services
|
2021-03-22 16:36:10 +08:00
|
|
|
for _, dsService := range node.dataSyncServices {
|
|
|
|
if dsService != nil {
|
|
|
|
dsService.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-01-30 16:02:10 +08:00
|
|
|
if node.loadService != nil {
|
|
|
|
node.loadService.close()
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
2020-11-24 16:12:39 +08:00
|
|
|
if node.statsService != nil {
|
2020-12-08 14:41:04 +08:00
|
|
|
node.statsService.close()
|
2020-11-24 16:12:39 +08:00
|
|
|
}
|
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-03-08 10:09:48 +08:00
|
|
|
func (node *QueryNode) SetMasterService(master types.MasterService) error {
|
2021-01-27 14:41:56 +08:00
|
|
|
if master == nil {
|
|
|
|
return errors.New("null master service interface")
|
|
|
|
}
|
2021-03-08 10:09:48 +08:00
|
|
|
node.masterService = master
|
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-03-08 10:09:48 +08:00
|
|
|
func (node *QueryNode) SetIndexService(index types.IndexService) error {
|
2021-01-26 13:41:41 +08:00
|
|
|
if index == nil {
|
|
|
|
return errors.New("null index service interface")
|
|
|
|
}
|
2021-03-08 10:09:48 +08:00
|
|
|
node.indexService = index
|
2021-01-26 13:41:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
func (node *QueryNode) SetDataService(data types.DataService) error {
|
2021-01-26 13:41:41 +08:00
|
|
|
if data == nil {
|
|
|
|
return errors.New("null data service interface")
|
|
|
|
}
|
2021-03-08 10:09:48 +08:00
|
|
|
node.dataService = data
|
2021-01-26 13:41:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 04:40:59 +08:00
|
|
|
func (node *QueryNode) getDataSyncService(collectionID UniqueID) (*dataSyncService, error) {
|
|
|
|
node.dsServicesMu.Lock()
|
|
|
|
defer node.dsServicesMu.Unlock()
|
|
|
|
ds, ok := node.dataSyncServices[collectionID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("cannot found dataSyncService, collectionID =" + fmt.Sprintln(collectionID))
|
|
|
|
}
|
|
|
|
return ds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *QueryNode) addDataSyncService(collectionID UniqueID, ds *dataSyncService) error {
|
|
|
|
node.dsServicesMu.Lock()
|
|
|
|
defer node.dsServicesMu.Unlock()
|
|
|
|
if _, ok := node.dataSyncServices[collectionID]; ok {
|
|
|
|
return errors.New("dataSyncService has been existed, collectionID =" + fmt.Sprintln(collectionID))
|
|
|
|
}
|
|
|
|
node.dataSyncServices[collectionID] = ds
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *QueryNode) removeDataSyncService(collectionID UniqueID) {
|
|
|
|
node.dsServicesMu.Lock()
|
|
|
|
defer node.dsServicesMu.Unlock()
|
|
|
|
delete(node.dataSyncServices, collectionID)
|
|
|
|
}
|