2021-04-19 05:47:10 +00: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 02:12:14 +00:00
|
|
|
package querynode
|
2020-08-25 07:45:19 +00:00
|
|
|
|
2020-09-02 02:38:08 +00:00
|
|
|
/*
|
|
|
|
|
2020-10-23 10:01:24 +00:00
|
|
|
#cgo CFLAGS: -I${SRCDIR}/../core/output/include
|
2020-09-02 02:38:08 +00:00
|
|
|
|
2020-10-31 07:11:47 +00:00
|
|
|
#cgo LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_segcore -Wl,-rpath=${SRCDIR}/../core/output/lib
|
2020-09-02 02:38:08 +00:00
|
|
|
|
2020-11-25 02:31:51 +00:00
|
|
|
#include "segcore/collection_c.h"
|
|
|
|
#include "segcore/segment_c.h"
|
2021-04-16 06:02:49 +00:00
|
|
|
#include "segcore/segcore_init_c.h"
|
2020-09-02 02:38:08 +00:00
|
|
|
|
|
|
|
*/
|
2020-08-25 07:45:19 +00:00
|
|
|
import "C"
|
2020-09-02 02:38:08 +00:00
|
|
|
|
2020-08-25 07:45:19 +00:00
|
|
|
import (
|
2020-10-15 13:31:50 +00:00
|
|
|
"context"
|
2021-03-22 08:36:10 +00:00
|
|
|
"errors"
|
2021-01-11 10:35:54 +00:00
|
|
|
"fmt"
|
2021-03-08 07:25:55 +00:00
|
|
|
"math/rand"
|
2021-05-21 11:28:52 +00:00
|
|
|
"strconv"
|
2021-01-21 07:20:23 +00:00
|
|
|
"sync/atomic"
|
2021-03-08 07:25:55 +00:00
|
|
|
"time"
|
2021-01-11 10:35:54 +00:00
|
|
|
|
2021-03-05 01:21:35 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-04-22 06:45:57 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
queryPb "github.com/milvus-io/milvus/internal/proto/querypb"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2021-05-21 11:28:52 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/util/sessionutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2020-08-25 07:45:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type QueryNode struct {
|
2020-12-08 06:41:04 +00:00
|
|
|
queryNodeLoopCtx context.Context
|
2020-12-10 08:31:09 +00:00
|
|
|
queryNodeLoopCancel context.CancelFunc
|
2020-10-15 13:31:50 +00:00
|
|
|
|
2021-02-23 03:40:30 +00:00
|
|
|
QueryNodeID UniqueID
|
2021-01-21 07:20:23 +00:00
|
|
|
stateCode atomic.Value
|
2020-08-25 07:45:19 +00:00
|
|
|
|
2021-05-28 02:26:30 +00:00
|
|
|
// internal components
|
|
|
|
historical *historical
|
|
|
|
streaming *streaming
|
2020-08-25 07:45:19 +00:00
|
|
|
|
2021-01-15 07:28:54 +00:00
|
|
|
// internal services
|
2021-05-28 02:26:30 +00:00
|
|
|
searchService *searchService
|
2021-01-18 02:09:17 +00:00
|
|
|
|
2021-01-26 05:41:41 +00:00
|
|
|
// clients
|
2021-03-08 02:09:48 +00:00
|
|
|
masterService types.MasterService
|
|
|
|
queryService types.QueryService
|
|
|
|
indexService types.IndexService
|
|
|
|
dataService types.DataService
|
2021-02-08 06:30:54 +00:00
|
|
|
|
|
|
|
msFactory msgstream.Factory
|
2021-04-12 01:18:43 +00:00
|
|
|
scheduler *taskScheduler
|
2021-05-21 11:28:52 +00:00
|
|
|
|
|
|
|
session *sessionutil.Session
|
2020-11-05 02:52:50 +00:00
|
|
|
}
|
2020-09-07 09:01:46 +00:00
|
|
|
|
2021-02-23 03:40:30 +00:00
|
|
|
func NewQueryNode(ctx context.Context, queryNodeID UniqueID, factory msgstream.Factory) *QueryNode {
|
2021-03-08 07:25:55 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2020-12-08 06:41:04 +00:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2021-01-21 07:20:23 +00:00
|
|
|
node := &QueryNode{
|
2021-01-11 10:35:54 +00:00
|
|
|
queryNodeLoopCtx: ctx1,
|
|
|
|
queryNodeLoopCancel: cancel,
|
|
|
|
QueryNodeID: queryNodeID,
|
2021-05-28 02:26:30 +00:00
|
|
|
searchService: nil,
|
|
|
|
msFactory: factory,
|
2021-01-11 10:35:54 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 01:18:43 +00:00
|
|
|
node.scheduler = newTaskScheduler(ctx1)
|
2021-03-12 06:22:09 +00:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Abnormal)
|
2021-05-28 02:26:30 +00:00
|
|
|
|
2021-01-21 07:20:23 +00:00
|
|
|
return node
|
|
|
|
}
|
2020-11-13 09:20:13 +00:00
|
|
|
|
2021-02-08 06:30:54 +00:00
|
|
|
func NewQueryNodeWithoutID(ctx context.Context, factory msgstream.Factory) *QueryNode {
|
2021-01-27 01:50:52 +00:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
node := &QueryNode{
|
|
|
|
queryNodeLoopCtx: ctx1,
|
|
|
|
queryNodeLoopCancel: cancel,
|
2021-05-28 02:26:30 +00:00
|
|
|
searchService: nil,
|
|
|
|
msFactory: factory,
|
2021-01-27 01:50:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 01:18:43 +00:00
|
|
|
node.scheduler = newTaskScheduler(ctx1)
|
2021-03-12 06:22:09 +00:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Abnormal)
|
2021-01-27 01:50:52 +00:00
|
|
|
|
2021-02-23 03:40:30 +00:00
|
|
|
return node
|
2020-09-15 07:53:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 07:06:05 +00:00
|
|
|
// Register register query node at etcd
|
|
|
|
func (node *QueryNode) Register() error {
|
2021-05-26 12:14:30 +00:00
|
|
|
node.session = sessionutil.NewSession(node.queryNodeLoopCtx, Params.MetaRootPath, []string{Params.EtcdAddress})
|
2021-05-25 07:06:05 +00:00
|
|
|
node.session.Init(typeutil.QueryNodeRole, Params.QueryNodeIP+":"+strconv.FormatInt(Params.QueryNodePort, 10), false)
|
|
|
|
Params.QueryNodeID = node.session.ServerID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:01:29 +00:00
|
|
|
func (node *QueryNode) Init() error {
|
2021-02-26 09:44:24 +00:00
|
|
|
ctx := context.Background()
|
2021-05-14 02:05:18 +00:00
|
|
|
|
2021-05-28 02:26:30 +00:00
|
|
|
node.historical = newHistorical(node.queryNodeLoopCtx,
|
|
|
|
node.masterService,
|
|
|
|
node.dataService,
|
|
|
|
node.indexService,
|
|
|
|
node.msFactory)
|
|
|
|
node.streaming = newStreaming()
|
|
|
|
|
2021-04-16 06:02:49 +00:00
|
|
|
C.SegcoreInit()
|
2021-01-22 06:28:06 +00:00
|
|
|
registerReq := &queryPb.RegisterNodeRequest{
|
2021-02-18 08:26:02 +00:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
SourceID: Params.QueryNodeID,
|
|
|
|
},
|
2021-01-21 07:20:23 +00:00
|
|
|
Address: &commonpb.Address{
|
|
|
|
Ip: Params.QueryNodeIP,
|
|
|
|
Port: Params.QueryNodePort,
|
|
|
|
},
|
|
|
|
}
|
2021-01-27 01:50:52 +00:00
|
|
|
|
2021-03-08 02:09:48 +00:00
|
|
|
resp, err := node.queryService.RegisterNode(ctx, registerReq)
|
2021-01-21 07:20:23 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-10 14:06:22 +00:00
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_Success {
|
2021-02-18 08:26:02 +00: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
|
2021-04-23 10:11:26 +00:00
|
|
|
case "SearchChannelName":
|
2021-04-24 02:44:28 +00:00
|
|
|
Params.SearchChannelNames = append(Params.SearchChannelNames, kv.Value)
|
2021-04-23 10:11:26 +00:00
|
|
|
case "SearchResultChannelName":
|
2021-04-24 02:44:28 +00:00
|
|
|
Params.SearchResultChannelNames = append(Params.SearchResultChannelNames, kv.Value)
|
2021-02-18 08:26:02 +00:00
|
|
|
default:
|
2021-03-05 02:15:27 +00:00
|
|
|
return fmt.Errorf("Invalid key: %v", kv.Key)
|
2021-02-18 08:26:02 +00:00
|
|
|
}
|
2021-01-21 07:20:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 01:21:35 +00:00
|
|
|
log.Debug("", zap.Int64("QueryNodeID", Params.QueryNodeID))
|
2021-01-21 07:20:23 +00:00
|
|
|
|
2021-03-08 02:09:48 +00:00
|
|
|
if node.masterService == nil {
|
2021-03-05 01:21:35 +00:00
|
|
|
log.Error("null master service detected")
|
2021-01-30 08:02:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 02:09:48 +00:00
|
|
|
if node.indexService == nil {
|
2021-03-05 01:21:35 +00:00
|
|
|
log.Error("null index service detected")
|
2021-01-26 05:41:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 02:09:48 +00:00
|
|
|
if node.dataService == nil {
|
2021-03-05 01:21:35 +00:00
|
|
|
log.Error("null data service detected")
|
2021-01-26 05:41:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 08:02:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *QueryNode) Start() error {
|
2021-02-08 06:30:54 +00: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 03:37:16 +00:00
|
|
|
// init services and manager
|
2021-05-28 02:26:30 +00:00
|
|
|
// TODO: pass node.streaming.replica to search service
|
|
|
|
node.searchService = newSearchService(node.queryNodeLoopCtx, node.historical.replica, node.streaming.replica, node.msFactory)
|
2020-09-15 07:53:10 +00:00
|
|
|
|
2021-04-12 01:18:43 +00:00
|
|
|
// start task scheduler
|
|
|
|
go node.scheduler.Start()
|
|
|
|
|
2021-01-19 03:37:16 +00:00
|
|
|
// start services
|
2020-11-19 09:09:22 +00:00
|
|
|
go node.searchService.start()
|
2021-05-28 02:26:30 +00:00
|
|
|
go node.historical.start()
|
2021-03-12 06:22:09 +00:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Healthy)
|
2021-01-21 02:01:29 +00:00
|
|
|
return nil
|
2020-11-05 02:52:50 +00:00
|
|
|
}
|
2020-09-15 07:53:10 +00:00
|
|
|
|
2021-01-21 02:01:29 +00:00
|
|
|
func (node *QueryNode) Stop() error {
|
2021-03-12 06:22:09 +00:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Abnormal)
|
2020-12-08 06:41:04 +00:00
|
|
|
node.queryNodeLoopCancel()
|
|
|
|
|
2020-11-24 08:12:39 +00:00
|
|
|
// close services
|
2021-05-28 02:26:30 +00:00
|
|
|
if node.historical != nil {
|
|
|
|
node.historical.close()
|
|
|
|
}
|
|
|
|
if node.streaming != nil {
|
|
|
|
node.streaming.close()
|
2020-11-24 08:12:39 +00:00
|
|
|
}
|
|
|
|
if node.searchService != nil {
|
2020-12-08 06:41:04 +00:00
|
|
|
node.searchService.close()
|
2020-11-24 08:12:39 +00:00
|
|
|
}
|
2021-01-21 02:01:29 +00:00
|
|
|
return nil
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (node *QueryNode) UpdateStateCode(code internalpb.StateCode) {
|
2021-02-23 03:40:30 +00:00
|
|
|
node.stateCode.Store(code)
|
|
|
|
}
|
|
|
|
|
2021-03-08 02:09:48 +00:00
|
|
|
func (node *QueryNode) SetMasterService(master types.MasterService) error {
|
2021-01-27 06:41:56 +00:00
|
|
|
if master == nil {
|
|
|
|
return errors.New("null master service interface")
|
|
|
|
}
|
2021-03-08 02:09:48 +00:00
|
|
|
node.masterService = master
|
2021-01-27 06:41:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 02:09:48 +00:00
|
|
|
func (node *QueryNode) SetQueryService(query types.QueryService) error {
|
2021-01-27 01:50:52 +00:00
|
|
|
if query == nil {
|
2021-01-27 06:41:56 +00:00
|
|
|
return errors.New("null query service interface")
|
2021-01-27 01:50:52 +00:00
|
|
|
}
|
2021-03-08 02:09:48 +00:00
|
|
|
node.queryService = query
|
2021-01-27 01:50:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 02:09:48 +00:00
|
|
|
func (node *QueryNode) SetIndexService(index types.IndexService) error {
|
2021-01-26 05:41:41 +00:00
|
|
|
if index == nil {
|
|
|
|
return errors.New("null index service interface")
|
|
|
|
}
|
2021-03-08 02:09:48 +00:00
|
|
|
node.indexService = index
|
2021-01-26 05:41:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 02:09:48 +00:00
|
|
|
func (node *QueryNode) SetDataService(data types.DataService) error {
|
2021-01-26 05:41:41 +00:00
|
|
|
if data == nil {
|
|
|
|
return errors.New("null data service interface")
|
|
|
|
}
|
2021-03-08 02:09:48 +00:00
|
|
|
node.dataService = data
|
2021-01-26 05:41:41 +00:00
|
|
|
return nil
|
|
|
|
}
|