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-01-22 09:36:18 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/allocator"
|
2021-03-08 19:39:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
2021-01-22 09:36:18 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2021-03-08 10:09:48 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/proxypb"
|
2021-03-08 19:39:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/types"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/funcutil"
|
|
|
|
"github.com/zilliztech/milvus-distributed/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-01-28 20:51:44 +08:00
|
|
|
initParams *internalpb2.InitParams
|
|
|
|
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
|
|
|
|
tsoAllocator *allocator.TimestampAllocator
|
2021-01-29 09:27:26 +08:00
|
|
|
segAssigner *SegIDAssigner
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-02-04 14:37:12 +08:00
|
|
|
manipulationMsgStream msgstream.MsgStream
|
|
|
|
queryMsgStream msgstream.MsgStream
|
2021-02-08 14:30:54 +08:00
|
|
|
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-10 15:27:26 +08:00
|
|
|
node.UpdateStateCode(internalpb2.StateCode_Abnormal)
|
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-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-03-08 10:09:48 +08:00
|
|
|
err := funcutil.WaitForComponentHealthy(ctx, node.proxyService, "ProxyService", 100, time.Millisecond*200)
|
2021-01-28 20:51:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("service was 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 {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
if response.Status.ErrorCode != commonpb.ErrorCode_Success {
|
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 {
|
|
|
|
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 {
|
|
|
|
err := funcutil.WaitForComponentHealthy(ctx, node.dataService, "DataService", 100, time.Millisecond*200)
|
2021-01-29 09:27:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
// wait for queryService state changed to Healthy
|
|
|
|
if node.queryService != nil {
|
|
|
|
err := funcutil.WaitForComponentHealthy(ctx, node.queryService, "QueryService", 100, time.Millisecond*200)
|
2021-01-29 09:27:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for indexservice state changed to Healthy
|
2021-03-08 10:09:48 +08:00
|
|
|
if node.indexService != nil {
|
|
|
|
err := funcutil.WaitForComponentHealthy(ctx, node.indexService, "IndexService", 100, time.Millisecond*200)
|
2021-01-29 09:27:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_Success {
|
2021-01-29 09:27:26 +08:00
|
|
|
return errors.New(resp.Status.Reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
Params.SearchChannelNames = []string{resp.RequestChannel}
|
|
|
|
Params.SearchResultChannelNames = []string{resp.ResultChannel}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{}{
|
|
|
|
"PulsarAddress": Params.PulsarAddress,
|
|
|
|
"ReceiveBufSize": Params.MsgStreamSearchBufSize,
|
|
|
|
"PulsarBufSize": 1024}
|
|
|
|
err = node.msFactory.SetParams(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
node.queryMsgStream, _ = node.msFactory.NewMsgStream(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-01-22 09:36:18 +08:00
|
|
|
idAllocator, err := allocator.NewIDAllocator(node.ctx, masterAddr)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
tsoAllocator, err := allocator.NewTimestampAllocator(node.ctx, masterAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node.tsoAllocator = tsoAllocator
|
2021-01-28 20:51:44 +08:00
|
|
|
node.tsoAllocator.PeerID = Params.ProxyID
|
2021-01-22 09:36:18 +08:00
|
|
|
|
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.manipulationMsgStream, _ = node.msFactory.NewMsgStream(node.ctx)
|
2021-02-04 14:37:12 +08:00
|
|
|
node.manipulationMsgStream.AsProducer(Params.InsertChannelNames)
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("proxynode", zap.Strings("proxynode AsProducer", Params.InsertChannelNames))
|
2021-03-05 16:52:45 +08:00
|
|
|
repackFunc := func(tsMsgs []msgstream.TsMsg, hashKeys [][]int32) (map[int32]*msgstream.MsgPack, error) {
|
2021-01-23 20:58:46 +08:00
|
|
|
return insertRepackFunc(tsMsgs, hashKeys, node.segAssigner, true)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
2021-03-05 16:52:45 +08:00
|
|
|
node.manipulationMsgStream.SetRepackFunc(repackFunc)
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("create manipulation message stream ...")
|
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.manipulationMsgStream.Start()
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("start manipulation message stream ...")
|
2021-01-29 17:29:31 +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.tsoAllocator.Start()
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("start tso 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-10 15:27:26 +08:00
|
|
|
node.UpdateStateCode(internalpb2.StateCode_Healthy)
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("proxy node is healthy ...")
|
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-02-02 19:54:31 +08:00
|
|
|
globalInsertChannelsMap.closeAllMsgStream()
|
2021-01-22 09:36:18 +08:00
|
|
|
node.tsoAllocator.Close()
|
|
|
|
node.idAllocator.Close()
|
|
|
|
node.segAssigner.Close()
|
|
|
|
node.sched.Close()
|
|
|
|
node.manipulationMsgStream.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
|
|
|
}
|