2021-01-22 09:36:18 +08:00
|
|
|
package proxynode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-01-29 17:29:31 +08:00
|
|
|
"log"
|
2021-01-22 09:36:18 +08:00
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/proxypb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/retry"
|
|
|
|
|
2021-02-24 09:48:17 +08:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/uber/jaeger-client-go/config"
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/allocator"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UniqueID = typeutil.UniqueID
|
|
|
|
type Timestamp = typeutil.Timestamp
|
|
|
|
|
|
|
|
type NodeImpl struct {
|
|
|
|
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-01-29 09:27:26 +08:00
|
|
|
stateCode internalpb2.StateCode
|
|
|
|
|
|
|
|
masterClient MasterClient
|
2021-01-28 20:51:44 +08:00
|
|
|
indexServiceClient IndexServiceClient
|
|
|
|
dataServiceClient DataServiceClient
|
2021-01-29 09:27:26 +08:00
|
|
|
proxyServiceClient ProxyServiceClient
|
|
|
|
queryServiceClient QueryServiceClient
|
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
|
|
|
|
|
|
|
closer io.Closer
|
|
|
|
|
|
|
|
// Add callback functions at different stages
|
|
|
|
startCallbacks []func()
|
|
|
|
closeCallbacks []func()
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
func NewProxyNodeImpl(ctx context.Context, factory msgstream.Factory) (*NodeImpl, error) {
|
2021-01-22 09:36:18 +08:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
node := &NodeImpl{
|
2021-02-08 14:30:54 +08:00
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
msFactory: factory,
|
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-01-29 09:27:26 +08:00
|
|
|
type Component interface {
|
|
|
|
GetComponentStates() (*internalpb2.ComponentStates, error)
|
|
|
|
}
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (node *NodeImpl) waitForServiceReady(service Component, serviceName string) error {
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
checkFunc := func() error {
|
|
|
|
resp, err := service.GetComponentStates()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
|
|
|
|
return errors.New(resp.Status.Reason)
|
|
|
|
}
|
|
|
|
if resp.State.StateCode != internalpb2.StateCode_HEALTHY {
|
|
|
|
return errors.New("")
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-28 20:51:44 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
// wait for 10 seconds
|
2021-02-26 15:17:47 +08:00
|
|
|
err := retry.Retry(200, time.Millisecond*200, checkFunc)
|
2021-01-28 20:51:44 +08:00
|
|
|
if err != nil {
|
2021-01-29 09:27:26 +08:00
|
|
|
errMsg := fmt.Sprintf("ProxyNode wait for %s ready failed", serviceName)
|
|
|
|
return errors.New(errMsg)
|
2021-01-28 20:51:44 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *NodeImpl) Init() error {
|
|
|
|
// todo wait for proxyservice state changed to Healthy
|
|
|
|
|
|
|
|
err := node.waitForServiceReady(node.proxyServiceClient, "ProxyService")
|
2021-01-28 20:51:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("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-01-29 09:27:26 +08:00
|
|
|
response, err := node.proxyServiceClient.RegisterNode(request)
|
2021-01-28 20:51:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
if response.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
|
|
|
|
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-02-24 09:48:17 +08:00
|
|
|
// TODO
|
|
|
|
cfg := &config.Configuration{
|
|
|
|
ServiceName: fmt.Sprintf("proxy_node_%d", Params.ProxyID),
|
|
|
|
Sampler: &config.SamplerConfig{
|
|
|
|
Type: "const",
|
|
|
|
Param: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tracer, closer, err := cfg.NewTracer()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
|
|
|
|
}
|
|
|
|
opentracing.SetGlobalTracer(tracer)
|
|
|
|
node.closer = closer
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
// wait for dataservice state changed to Healthy
|
|
|
|
if node.dataServiceClient != nil {
|
|
|
|
err = node.waitForServiceReady(node.dataServiceClient, "DataService")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for queryservice state changed to Healthy
|
|
|
|
if node.queryServiceClient != nil {
|
|
|
|
err = node.waitForServiceReady(node.queryServiceClient, "QueryService")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for indexservice state changed to Healthy
|
|
|
|
if node.indexServiceClient != nil {
|
|
|
|
err = node.waitForServiceReady(node.indexServiceClient, "IndexService")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.queryServiceClient != nil {
|
|
|
|
resp, err := node.queryServiceClient.CreateQueryChannel()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
|
|
|
|
return errors.New(resp.Status.Reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
Params.SearchChannelNames = []string{resp.RequestChannel}
|
|
|
|
Params.SearchResultChannelNames = []string{resp.ResultChannel}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.UpdateStateCode(internalpb2.StateCode_HEALTHY)
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("proxy node is healthy ...")
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
// todo
|
|
|
|
//Params.InsertChannelNames, err = node.dataServiceClient.GetInsertChannels()
|
|
|
|
//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-01-29 17:29:31 +08:00
|
|
|
log.Println("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-01-29 09:27:26 +08:00
|
|
|
segAssigner, err := NewSegIDAssigner(node.ctx, node.dataServiceClient, 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-01-22 09:36:18 +08:00
|
|
|
repackFuncImpl := 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
|
|
|
}
|
|
|
|
node.manipulationMsgStream.SetRepackFunc(repackFuncImpl)
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *NodeImpl) Start() error {
|
2021-01-31 14:55:36 +08:00
|
|
|
err := InitMetaCache(node.masterClient)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Println("init global meta cache ...")
|
|
|
|
|
2021-01-30 15:30:38 +08:00
|
|
|
initGlobalInsertChannelsMap(node)
|
|
|
|
log.Println("init global insert channels map ...")
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.manipulationMsgStream.Start()
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("start manipulation message stream ...")
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.queryMsgStream.Start()
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("start query message stream ...")
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.sched.Start()
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("start scheduler ...")
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.idAllocator.Start()
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("start id allocator ...")
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.tsoAllocator.Start()
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("start tso allocator ...")
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.segAssigner.Start()
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("start seg assigner ...")
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
node.tick.Start()
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("start time tick ...")
|
2021-01-22 09:36:18 +08:00
|
|
|
|
|
|
|
// Start callbacks
|
|
|
|
for _, cb := range node.startCallbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *NodeImpl) Stop() error {
|
2021-02-24 09:48:17 +08:00
|
|
|
if err := node.closer.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
if node.closer != nil {
|
|
|
|
err := node.closer.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cb := range node.closeCallbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddStartCallback adds a callback in the startServer phase.
|
|
|
|
func (node *NodeImpl) AddStartCallback(callbacks ...func()) {
|
|
|
|
node.startCallbacks = append(node.startCallbacks, callbacks...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *NodeImpl) lastTick() Timestamp {
|
|
|
|
return node.tick.LastTick()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCloseCallback adds a callback in the Close phase.
|
|
|
|
func (node *NodeImpl) AddCloseCallback(callbacks ...func()) {
|
|
|
|
node.closeCallbacks = append(node.closeCallbacks, callbacks...)
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
func (node *NodeImpl) SetMasterClient(cli MasterClient) {
|
|
|
|
node.masterClient = cli
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *NodeImpl) SetIndexServiceClient(cli IndexServiceClient) {
|
|
|
|
node.indexServiceClient = cli
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *NodeImpl) SetDataServiceClient(cli DataServiceClient) {
|
|
|
|
node.dataServiceClient = cli
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *NodeImpl) SetProxyServiceClient(cli ProxyServiceClient) {
|
|
|
|
node.proxyServiceClient = cli
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *NodeImpl) SetQueryServiceClient(cli QueryServiceClient) {
|
|
|
|
node.queryServiceClient = cli
|
|
|
|
}
|