2021-04-19 10:09:43 +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-22 09:36:18 +08:00
|
|
|
package proxynode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-08 10:09:48 +08:00
|
|
|
"errors"
|
2021-01-22 09:36:18 +08:00
|
|
|
"math/rand"
|
|
|
|
"sync"
|
2021-03-04 22:27:12 +08:00
|
|
|
"sync/atomic"
|
2021-01-22 09:36:18 +08:00
|
|
|
"time"
|
|
|
|
|
2021-03-08 19:39:36 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/allocator"
|
|
|
|
"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"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/proxypb"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
2021-05-21 19:28:52 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/sessionutil"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2021-01-22 09:36:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type UniqueID = typeutil.UniqueID
|
|
|
|
type Timestamp = typeutil.Timestamp
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
type ProxyNode struct {
|
2021-01-22 09:36:18 +08:00
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
initParams *internalpb.InitParams
|
2021-01-28 20:51:44 +08:00
|
|
|
ip string
|
|
|
|
port int
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-03-04 22:27:12 +08:00
|
|
|
stateCode atomic.Value
|
2021-01-29 09:27:26 +08:00
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
masterService types.MasterService
|
|
|
|
indexService types.IndexService
|
|
|
|
dataService types.DataService
|
|
|
|
proxyService types.ProxyService
|
|
|
|
queryService types.QueryService
|
2021-01-28 20:51:44 +08:00
|
|
|
|
|
|
|
sched *TaskScheduler
|
|
|
|
tick *timeTick
|
2021-01-22 09:36:18 +08:00
|
|
|
|
|
|
|
idAllocator *allocator.IDAllocator
|
2021-05-21 13:11:21 +08:00
|
|
|
tsoAllocator *TimestampAllocator
|
2021-01-29 09:27:26 +08:00
|
|
|
segAssigner *SegIDAssigner
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-05-21 19:28:52 +08:00
|
|
|
session *sessionutil.Session
|
|
|
|
|
2021-04-22 11:04:59 +08:00
|
|
|
queryMsgStream msgstream.MsgStream
|
|
|
|
msFactory msgstream.Factory
|
2021-01-22 09:36:18 +08:00
|
|
|
|
|
|
|
// Add callback functions at different stages
|
|
|
|
startCallbacks []func()
|
|
|
|
closeCallbacks []func()
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func NewProxyNode(ctx context.Context, factory msgstream.Factory) (*ProxyNode, error) {
|
2021-01-22 09:36:18 +08:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2021-03-05 16:52:45 +08:00
|
|
|
node := &ProxyNode{
|
2021-02-08 14:30:54 +08:00
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
msFactory: factory,
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
2021-03-12 14:22:09 +08:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Abnormal)
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("ProxyNode", zap.Any("State", node.stateCode.Load()))
|
2021-01-22 09:36:18 +08:00
|
|
|
return node, nil
|
2021-01-29 09:27:26 +08:00
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 15:06:05 +08:00
|
|
|
// Register register proxy node at etcd
|
|
|
|
func (node *ProxyNode) Register() error {
|
2021-05-26 20:14:30 +08:00
|
|
|
node.session = sessionutil.NewSession(node.ctx, Params.MetaRootPath, []string{Params.EtcdAddress})
|
2021-05-25 15:06:05 +08:00
|
|
|
node.session.Init(typeutil.ProxyNodeRole, Params.NetworkAddress, false)
|
|
|
|
Params.ProxyID = node.session.ServerID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (node *ProxyNode) Init() error {
|
2021-01-29 09:27:26 +08:00
|
|
|
// todo wait for proxyservice state changed to Healthy
|
2021-02-26 17:44:24 +08:00
|
|
|
ctx := context.Background()
|
2021-01-29 09:27:26 +08:00
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
err := funcutil.WaitForComponentHealthy(ctx, node.proxyService, "ProxyService", 1000000, time.Millisecond*200)
|
2021-01-28 20:51:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyService is ready ...")
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
request := &proxypb.RegisterNodeRequest{
|
|
|
|
Address: &commonpb.Address{
|
|
|
|
Ip: Params.IP,
|
|
|
|
Port: int64(Params.NetworkPort),
|
|
|
|
},
|
2021-01-28 20:51:44 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
response, err := node.proxyService.RegisterNode(ctx, request)
|
2021-01-28 20:51:44 +08:00
|
|
|
if err != nil {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode RegisterNode failed", zap.Error(err))
|
2021-01-28 20:51:44 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
if response.Status.ErrorCode != commonpb.ErrorCode_Success {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode RegisterNode failed", zap.String("Reason", response.Status.Reason))
|
2021-01-29 09:27:26 +08:00
|
|
|
return errors.New(response.Status.Reason)
|
2021-01-28 20:51:44 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
err = Params.LoadConfigFromInitParams(response.InitParams)
|
2021-01-28 20:51:44 +08:00
|
|
|
if err != nil {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode LoadConfigFromInitParams failed", zap.Error(err))
|
2021-01-28 20:51:44 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
// wait for dataservice state changed to Healthy
|
2021-03-08 10:09:48 +08:00
|
|
|
if node.dataService != nil {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode wait for dataService ready")
|
2021-03-22 16:36:10 +08:00
|
|
|
err := funcutil.WaitForComponentHealthy(ctx, node.dataService, "DataService", 1000000, time.Millisecond*200)
|
2021-01-29 09:27:26 +08:00
|
|
|
if err != nil {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode wait for dataService ready failed", zap.Error(err))
|
2021-01-29 09:27:26 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode dataService is ready")
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
// wait for queryService state changed to Healthy
|
|
|
|
if node.queryService != nil {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode wait for queryService ready")
|
2021-03-22 16:36:10 +08:00
|
|
|
err := funcutil.WaitForComponentHealthy(ctx, node.queryService, "QueryService", 1000000, time.Millisecond*200)
|
2021-01-29 09:27:26 +08:00
|
|
|
if err != nil {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode wait for queryService ready failed", zap.Error(err))
|
2021-01-29 09:27:26 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode queryService is ready")
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for indexservice state changed to Healthy
|
2021-03-08 10:09:48 +08:00
|
|
|
if node.indexService != nil {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode wait for indexService ready")
|
2021-03-22 16:36:10 +08:00
|
|
|
err := funcutil.WaitForComponentHealthy(ctx, node.indexService, "IndexService", 1000000, time.Millisecond*200)
|
2021-01-29 09:27:26 +08:00
|
|
|
if err != nil {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode wait for indexService ready failed", zap.Error(err))
|
2021-01-29 09:27:26 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode indexService is ready")
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
if node.queryService != nil {
|
|
|
|
resp, err := node.queryService.CreateQueryChannel(ctx)
|
2021-01-29 09:27:26 +08:00
|
|
|
if err != nil {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode CreateQueryChannel failed", zap.Error(err))
|
2021-01-29 09:27:26 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_Success {
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode CreateQueryChannel failed", zap.String("reason", resp.Status.Reason))
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
return errors.New(resp.Status.Reason)
|
|
|
|
}
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode CreateQueryChannel success")
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
Params.SearchChannelNames = []string{resp.RequestChannel}
|
|
|
|
Params.SearchResultChannelNames = []string{resp.ResultChannel}
|
2021-05-29 11:19:30 +08:00
|
|
|
Params.RetrieveChannelNames = []string{resp.RequestChannel}
|
|
|
|
Params.RetrieveResultChannelNames = []string{resp.ResultChannel}
|
2021-06-03 14:58:34 +08:00
|
|
|
log.Debug("ProxyNode CreateQueryChannel success", zap.Any("SearchChannelNames", Params.SearchChannelNames))
|
|
|
|
log.Debug("ProxyNode CreateQueryChannel success", zap.Any("SearchResultChannelNames", Params.SearchResultChannelNames))
|
|
|
|
log.Debug("ProxyNode CreateQueryChannel success", zap.Any("RetrieveChannelNames", Params.RetrieveChannelNames))
|
|
|
|
log.Debug("ProxyNode CreateQueryChannel success", zap.Any("RetrieveResultChannelNames", Params.RetrieveResultChannelNames))
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo
|
2021-03-08 10:09:48 +08:00
|
|
|
//Params.InsertChannelNames, err = node.dataService.GetInsertChannels()
|
2021-01-29 09:27:26 +08:00
|
|
|
//if err != nil {
|
|
|
|
// return err
|
|
|
|
//}
|
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
m := map[string]interface{}{
|
2021-04-23 10:10:46 +08:00
|
|
|
"PulsarAddress": Params.PulsarAddress,
|
|
|
|
"PulsarBufSize": 1024}
|
2021-02-08 14:30:54 +08:00
|
|
|
err = node.msFactory.SetParams(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:16:04 +08:00
|
|
|
node.queryMsgStream, _ = node.msFactory.NewQueryMsgStream(node.ctx)
|
2021-02-04 14:37:12 +08:00
|
|
|
node.queryMsgStream.AsProducer(Params.SearchChannelNames)
|
2021-03-05 18:16:50 +08:00
|
|
|
// FIXME(wxyu): use log.Debug instead
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("proxynode", zap.Strings("proxynode AsProducer:", Params.SearchChannelNames))
|
|
|
|
log.Debug("create query message stream ...")
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-01-28 20:51:44 +08:00
|
|
|
masterAddr := Params.MasterAddress
|
2021-05-26 20:14:30 +08:00
|
|
|
idAllocator, err := allocator.NewIDAllocator(node.ctx, masterAddr, Params.MetaRootPath, []string{Params.EtcdAddress})
|
2021-01-22 09:36:18 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node.idAllocator = idAllocator
|
2021-01-28 20:51:44 +08:00
|
|
|
node.idAllocator.PeerID = Params.ProxyID
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-05-21 13:11:21 +08:00
|
|
|
tsoAllocator, err := NewTimestampAllocator(node.masterService, Params.ProxyID)
|
2021-01-22 09:36:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node.tsoAllocator = tsoAllocator
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
segAssigner, err := NewSegIDAssigner(node.ctx, node.dataService, node.lastTick)
|
2021-01-22 09:36:18 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
node.segAssigner = segAssigner
|
2021-01-28 20:51:44 +08:00
|
|
|
node.segAssigner.PeerID = Params.ProxyID
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
node.sched, err = NewTaskScheduler(node.ctx, node.idAllocator, node.tsoAllocator, node.msFactory)
|
2021-01-22 09:36:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
node.tick = newTimeTick(node.ctx, node.tsoAllocator, time.Millisecond*200, node.sched.TaskDoneTest, node.msFactory)
|
2021-01-22 09:36:18 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (node *ProxyNode) Start() error {
|
2021-03-08 10:09:48 +08:00
|
|
|
err := InitMetaCache(node.masterService)
|
2021-01-31 14:55:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("init global meta cache ...")
|
2021-01-31 14:55:36 +08:00
|
|
|
|
2021-01-30 15:30:38 +08:00
|
|
|
initGlobalInsertChannelsMap(node)
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("init global insert channels map ...")
|
2021-01-30 15:30:38 +08:00
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.queryMsgStream.Start()
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("start query message stream ...")
|
2021-01-29 17:29:31 +08:00
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.sched.Start()
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("start scheduler ...")
|
2021-01-29 17:29:31 +08:00
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.idAllocator.Start()
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("start id allocator ...")
|
2021-01-29 17:29:31 +08:00
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.segAssigner.Start()
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("start seg assigner ...")
|
2021-01-29 17:29:31 +08:00
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.tick.Start()
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("start time tick ...")
|
2021-01-22 09:36:18 +08:00
|
|
|
|
|
|
|
// Start callbacks
|
|
|
|
for _, cb := range node.startCallbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Healthy)
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("ProxyNode", zap.Any("State", node.stateCode.Load()))
|
2021-03-04 22:27:12 +08:00
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (node *ProxyNode) Stop() error {
|
2021-01-22 09:36:18 +08:00
|
|
|
node.cancel()
|
|
|
|
|
2021-04-08 15:41:28 +08:00
|
|
|
globalInsertChannelsMap.CloseAllMsgStream()
|
2021-01-22 09:36:18 +08:00
|
|
|
node.idAllocator.Close()
|
|
|
|
node.segAssigner.Close()
|
|
|
|
node.sched.Close()
|
|
|
|
node.queryMsgStream.Close()
|
|
|
|
node.tick.Close()
|
|
|
|
|
|
|
|
node.wg.Wait()
|
|
|
|
|
|
|
|
for _, cb := range node.closeCallbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddStartCallback adds a callback in the startServer phase.
|
2021-03-05 16:52:45 +08:00
|
|
|
func (node *ProxyNode) AddStartCallback(callbacks ...func()) {
|
2021-01-22 09:36:18 +08:00
|
|
|
node.startCallbacks = append(node.startCallbacks, callbacks...)
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (node *ProxyNode) lastTick() Timestamp {
|
2021-01-22 09:36:18 +08:00
|
|
|
return node.tick.LastTick()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCloseCallback adds a callback in the Close phase.
|
2021-03-05 16:52:45 +08:00
|
|
|
func (node *ProxyNode) AddCloseCallback(callbacks ...func()) {
|
2021-01-22 09:36:18 +08:00
|
|
|
node.closeCallbacks = append(node.closeCallbacks, callbacks...)
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
func (node *ProxyNode) SetMasterClient(cli types.MasterService) {
|
|
|
|
node.masterService = cli
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
func (node *ProxyNode) SetIndexServiceClient(cli types.IndexService) {
|
|
|
|
node.indexService = cli
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
func (node *ProxyNode) SetDataServiceClient(cli types.DataService) {
|
|
|
|
node.dataService = cli
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
func (node *ProxyNode) SetProxyServiceClient(cli types.ProxyService) {
|
|
|
|
node.proxyService = cli
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
func (node *ProxyNode) SetQueryServiceClient(cli types.QueryService) {
|
|
|
|
node.queryService = cli
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|