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-05-14 10:05:18 +08:00
|
|
|
"encoding/json"
|
2021-03-08 10:09:48 +08:00
|
|
|
"errors"
|
2021-05-14 10:05:18 +08:00
|
|
|
"fmt"
|
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"
|
2021-05-14 10:05:18 +08:00
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
2021-04-22 14:45:57 +08: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"
|
|
|
|
"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-14 10:05:18 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2021-05-14 10:05:18 +08:00
|
|
|
"go.etcd.io/etcd/clientv3"
|
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
|
|
|
|
tsoAllocator *allocator.TimestampAllocator
|
2021-01-29 09:27:26 +08:00
|
|
|
segAssigner *SegIDAssigner
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-04-22 11:04:59 +08:00
|
|
|
queryMsgStream msgstream.MsgStream
|
|
|
|
msFactory msgstream.Factory
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-05-14 10:05:18 +08:00
|
|
|
etcdKV *etcdkv.EtcdKV
|
|
|
|
session struct {
|
|
|
|
NodeName string
|
|
|
|
IP string
|
|
|
|
LeaseID clientv3.LeaseID
|
|
|
|
}
|
|
|
|
|
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-03-22 19:28:43 +08:00
|
|
|
log.Debug("proxynode",
|
|
|
|
zap.Any("state of proxynode", internalpb.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-05-14 10:05:18 +08:00
|
|
|
connectEtcdFn := func() error {
|
|
|
|
etcdClient, err := clientv3.New(clientv3.Config{Endpoints: []string{Params.EtcdAddress}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node.etcdKV = etcdkv.NewEtcdKV(etcdClient, Params.MetaRootPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := retry.Retry(100000, time.Millisecond*200, connectEtcdFn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := node.registerService("proxynode", Params.NetworkAddress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
for range ch {
|
|
|
|
//TODO process lesase response
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
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-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 {
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
// wait for queryService state changed to Healthy
|
|
|
|
if node.queryService != nil {
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for indexservice state changed to Healthy
|
2021-03-08 10:09:48 +08:00
|
|
|
if node.indexService != nil {
|
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 {
|
|
|
|
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{}{
|
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-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.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.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-12 14:22:09 +08:00
|
|
|
node.UpdateStateCode(internalpb.StateCode_Healthy)
|
2021-03-22 19:28:43 +08:00
|
|
|
log.Debug("proxynode",
|
|
|
|
zap.Any("state of proxynode", internalpb.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-04-08 15:41:28 +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.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
|
|
|
}
|
2021-05-14 10:05:18 +08:00
|
|
|
|
|
|
|
func (node *ProxyNode) registerService(nodeName string, ip string) (<-chan *clientv3.LeaseKeepAliveResponse, error) {
|
|
|
|
respID, err := node.etcdKV.Grant(5)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("grant error %s\n", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node.session.NodeName = nodeName
|
|
|
|
node.session.IP = ip
|
|
|
|
node.session.LeaseID = respID
|
|
|
|
|
|
|
|
sessionJSON, err := json.Marshal(node.session)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = node.etcdKV.SaveWithLease(fmt.Sprintf("/node/%s", nodeName), string(sessionJSON), respID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("put lease error %s\n", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := node.etcdKV.KeepAlive(respID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("keep alive error %s\n", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ch, nil
|
|
|
|
}
|