2021-05-25 15:35:37 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.//
|
2021-04-19 11:35:38 +08:00
|
|
|
// 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-05-20 11:34:45 +08:00
|
|
|
|
2021-01-19 12:10:49 +08:00
|
|
|
package dataservice
|
|
|
|
|
2021-01-22 11:07:07 +08:00
|
|
|
import (
|
|
|
|
"context"
|
2021-03-05 10:15:27 +08:00
|
|
|
"errors"
|
2021-01-22 11:07:07 +08:00
|
|
|
"fmt"
|
2021-03-08 15:25:55 +08:00
|
|
|
"math/rand"
|
2021-01-26 15:14:49 +08:00
|
|
|
"strconv"
|
2021-01-23 20:22:59 +08:00
|
|
|
"sync"
|
2021-01-26 15:14:49 +08:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/logutil"
|
2021-03-04 16:01:30 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
grpcdatanodeclient "github.com/milvus-io/milvus/internal/distributed/datanode/client"
|
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
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-02-26 15:17:47 +08:00
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
"go.uber.org/zap"
|
2021-03-05 20:41:34 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/milvuspb"
|
2021-01-22 11:07:07 +08:00
|
|
|
)
|
|
|
|
|
2021-01-22 19:43:27 +08:00
|
|
|
const role = "dataservice"
|
|
|
|
|
2021-01-22 11:07:07 +08:00
|
|
|
type (
|
2021-01-22 19:43:27 +08:00
|
|
|
UniqueID = typeutil.UniqueID
|
|
|
|
Timestamp = typeutil.Timestamp
|
2021-01-22 11:07:07 +08:00
|
|
|
)
|
2021-03-05 20:41:34 +08:00
|
|
|
type Server struct {
|
2021-04-13 09:47:02 +08:00
|
|
|
ctx context.Context
|
|
|
|
serverLoopCtx context.Context
|
|
|
|
serverLoopCancel context.CancelFunc
|
|
|
|
serverLoopWg sync.WaitGroup
|
|
|
|
state atomic.Value
|
2021-04-24 11:29:15 +08:00
|
|
|
kvClient *etcdkv.EtcdKV
|
2021-04-13 09:47:02 +08:00
|
|
|
meta *meta
|
|
|
|
segAllocator segmentAllocatorInterface
|
|
|
|
statsHandler *statsHandler
|
|
|
|
allocator allocatorInterface
|
|
|
|
cluster *dataNodeCluster
|
|
|
|
masterClient types.MasterService
|
2021-05-19 18:36:05 +08:00
|
|
|
ddChannelMu struct {
|
2021-04-13 09:47:02 +08:00
|
|
|
sync.Mutex
|
|
|
|
name string
|
|
|
|
}
|
2021-05-21 19:28:52 +08:00
|
|
|
session *sessionutil.Session
|
2021-05-21 16:54:29 +08:00
|
|
|
flushMsgStream msgstream.MsgStream
|
2021-04-13 09:47:02 +08:00
|
|
|
insertChannels []string
|
|
|
|
msFactory msgstream.Factory
|
2021-05-26 18:41:37 +08:00
|
|
|
createDataNodeClient func(addr string) (types.DataNode, error)
|
2021-03-05 20:41:34 +08:00
|
|
|
}
|
2021-01-22 11:07:07 +08:00
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
func CreateServer(ctx context.Context, factory msgstream.Factory) (*Server, error) {
|
2021-03-08 15:25:55 +08:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2021-01-26 15:14:49 +08:00
|
|
|
s := &Server{
|
2021-04-13 09:47:02 +08:00
|
|
|
ctx: ctx,
|
|
|
|
cluster: newDataNodeCluster(),
|
|
|
|
msFactory: factory,
|
2021-01-26 15:14:49 +08:00
|
|
|
}
|
2021-02-03 17:30:10 +08:00
|
|
|
s.insertChannels = s.getInsertChannels()
|
2021-05-26 18:41:37 +08:00
|
|
|
s.createDataNodeClient = func(addr string) (types.DataNode, error) {
|
2021-06-04 16:29:35 +08:00
|
|
|
node, err := grpcdatanodeclient.NewClient(addr, 10*time.Second)
|
2021-05-25 15:47:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return node, nil
|
2021-04-13 09:47:02 +08:00
|
|
|
}
|
2021-03-12 14:22:09 +08:00
|
|
|
s.UpdateStateCode(internalpb.StateCode_Abnormal)
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("DataService", zap.Any("State", s.state.Load()))
|
2021-01-26 15:14:49 +08:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:30:10 +08:00
|
|
|
func (s *Server) getInsertChannels() []string {
|
2021-04-16 16:30:55 +08:00
|
|
|
channels := make([]string, 0, Params.InsertChannelNum)
|
2021-02-03 17:30:10 +08:00
|
|
|
var i int64 = 0
|
|
|
|
for ; i < Params.InsertChannelNum; i++ {
|
2021-04-16 16:30:55 +08:00
|
|
|
channels = append(channels, Params.InsertChannelPrefixName+strconv.FormatInt(i, 10))
|
2021-02-03 17:30:10 +08:00
|
|
|
}
|
|
|
|
return channels
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:41:34 +08:00
|
|
|
func (s *Server) SetMasterClient(masterClient types.MasterService) {
|
2021-01-26 15:14:49 +08:00
|
|
|
s.masterClient = masterClient
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 15:06:05 +08:00
|
|
|
// Register register data service at etcd
|
|
|
|
func (s *Server) Register() error {
|
2021-05-26 20:14:30 +08:00
|
|
|
s.session = sessionutil.NewSession(s.ctx, Params.MetaRootPath, []string{Params.EtcdAddress})
|
2021-05-24 09:44:49 +08:00
|
|
|
s.session.Init(typeutil.DataServiceRole, Params.IP, true)
|
2021-05-25 15:06:05 +08:00
|
|
|
Params.NodeID = s.session.ServerID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Init() error {
|
2021-01-22 19:43:27 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Start() error {
|
2021-01-26 09:43:41 +08:00
|
|
|
var err error
|
2021-02-08 14:30:54 +08:00
|
|
|
m := map[string]interface{}{
|
|
|
|
"PulsarAddress": Params.PulsarAddress,
|
|
|
|
"ReceiveBufSize": 1024,
|
|
|
|
"PulsarBufSize": 1024}
|
|
|
|
err = s.msFactory.SetParams(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-19 18:36:05 +08:00
|
|
|
if err := s.initMeta(); err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("DataService initMeta failed", zap.Error(err))
|
2021-05-19 18:36:05 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-13 09:47:02 +08:00
|
|
|
s.allocator = newAllocator(s.masterClient)
|
|
|
|
|
2021-05-21 18:30:41 +08:00
|
|
|
s.startSegmentAllocator()
|
2021-01-22 11:07:07 +08:00
|
|
|
s.statsHandler = newStatsHandler(s.meta)
|
2021-01-26 09:43:41 +08:00
|
|
|
if err = s.loadMetaFromMaster(); err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("DataService loadMetaFromMaster failed", zap.Error(err))
|
2021-01-22 11:07:07 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-01-27 11:34:16 +08:00
|
|
|
if err = s.initMsgProducer(); err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("DataService initMsgProducer failed", zap.Error(err))
|
2021-01-27 11:34:16 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-02-06 10:28:53 +08:00
|
|
|
s.startServerLoop()
|
2021-03-12 14:22:09 +08:00
|
|
|
s.UpdateStateCode(internalpb.StateCode_Healthy)
|
2021-02-19 15:37:04 +08:00
|
|
|
log.Debug("start success")
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("DataService", zap.Any("State", s.state.Load()))
|
2021-01-22 19:43:27 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-21 18:30:41 +08:00
|
|
|
func (s *Server) startSegmentAllocator() {
|
|
|
|
stream := s.initSegmentInfoChannel()
|
|
|
|
helper := createNewSegmentHelper(stream)
|
|
|
|
s.segAllocator = newSegmentAllocator(s.meta, s.allocator, withAllocHelper(helper))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initSegmentInfoChannel() msgstream.MsgStream {
|
|
|
|
segmentInfoStream, _ := s.msFactory.NewMsgStream(s.ctx)
|
|
|
|
segmentInfoStream.AsProducer([]string{Params.SegmentInfoChannelName})
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("DataService AsProducer: " + Params.SegmentInfoChannelName)
|
2021-05-21 18:30:41 +08:00
|
|
|
segmentInfoStream.Start()
|
|
|
|
return segmentInfoStream
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) UpdateStateCode(code internalpb.StateCode) {
|
2021-02-23 11:40:30 +08:00
|
|
|
s.state.Store(code)
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:14:49 +08:00
|
|
|
func (s *Server) checkStateIsHealthy() bool {
|
2021-03-12 14:22:09 +08:00
|
|
|
return s.state.Load().(internalpb.StateCode) == internalpb.StateCode_Healthy
|
2021-01-26 15:14:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-22 11:07:07 +08:00
|
|
|
func (s *Server) initMeta() error {
|
2021-02-26 15:17:47 +08:00
|
|
|
connectEtcdFn := func() error {
|
2021-05-25 15:35:37 +08:00
|
|
|
etcdClient, err := clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: []string{Params.EtcdAddress},
|
|
|
|
})
|
2021-02-26 15:17:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-24 11:29:15 +08:00
|
|
|
s.kvClient = etcdkv.NewEtcdKV(etcdClient, Params.MetaRootPath)
|
|
|
|
s.meta, err = newMeta(s.kvClient)
|
2021-02-26 15:17:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
2021-04-13 09:47:02 +08:00
|
|
|
return retry.Retry(100000, time.Millisecond*200, connectEtcdFn)
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initMsgProducer() error {
|
2021-02-04 18:11:19 +08:00
|
|
|
var err error
|
2021-05-21 16:54:29 +08:00
|
|
|
// segment flush stream
|
|
|
|
s.flushMsgStream, err = s.msFactory.NewMsgStream(s.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.flushMsgStream.AsProducer([]string{Params.SegmentInfoChannelName})
|
|
|
|
log.Debug("dataservice AsProducer:" + Params.SegmentInfoChannelName)
|
|
|
|
s.flushMsgStream.Start()
|
|
|
|
|
2021-01-22 11:07:07 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-23 20:22:59 +08:00
|
|
|
|
2021-01-22 19:43:27 +08:00
|
|
|
func (s *Server) loadMetaFromMaster() error {
|
2021-02-26 17:44:24 +08:00
|
|
|
ctx := context.Background()
|
2021-02-19 15:37:04 +08:00
|
|
|
log.Debug("loading collection meta from master")
|
2021-02-23 09:58:06 +08:00
|
|
|
var err error
|
|
|
|
if err = s.checkMasterIsHealthy(); err != nil {
|
2021-01-26 15:14:49 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-04-13 09:47:02 +08:00
|
|
|
if err = s.getDDChannel(); err != nil {
|
|
|
|
return err
|
2021-01-26 17:55:15 +08:00
|
|
|
}
|
2021-03-12 14:22:09 +08:00
|
|
|
collections, err := s.masterClient.ShowCollections(ctx, &milvuspb.ShowCollectionsRequest{
|
2021-01-22 19:43:27 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_ShowCollections,
|
2021-01-22 19:43:27 +08:00
|
|
|
MsgID: -1, // todo add msg id
|
|
|
|
Timestamp: 0, // todo
|
2021-01-26 15:14:49 +08:00
|
|
|
SourceID: Params.NodeID,
|
2021-01-22 19:43:27 +08:00
|
|
|
},
|
|
|
|
DbName: "",
|
|
|
|
})
|
2021-02-23 09:58:06 +08:00
|
|
|
if err = VerifyResponse(collections, err); err != nil {
|
2021-01-22 19:43:27 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, collectionName := range collections.CollectionNames {
|
2021-02-26 17:44:24 +08:00
|
|
|
collection, err := s.masterClient.DescribeCollection(ctx, &milvuspb.DescribeCollectionRequest{
|
2021-01-22 19:43:27 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_DescribeCollection,
|
2021-01-22 19:43:27 +08:00
|
|
|
MsgID: -1, // todo
|
|
|
|
Timestamp: 0, // todo
|
2021-01-26 15:14:49 +08:00
|
|
|
SourceID: Params.NodeID,
|
2021-01-22 19:43:27 +08:00
|
|
|
},
|
|
|
|
DbName: "",
|
|
|
|
CollectionName: collectionName,
|
|
|
|
})
|
2021-02-23 09:58:06 +08:00
|
|
|
if err = VerifyResponse(collection, err); err != nil {
|
|
|
|
log.Error("describe collection error", zap.String("collectionName", collectionName), zap.Error(err))
|
2021-01-22 19:43:27 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-03-12 14:22:09 +08:00
|
|
|
partitions, err := s.masterClient.ShowPartitions(ctx, &milvuspb.ShowPartitionsRequest{
|
2021-01-22 19:43:27 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_ShowPartitions,
|
2021-01-22 19:43:27 +08:00
|
|
|
MsgID: -1, // todo
|
|
|
|
Timestamp: 0, // todo
|
2021-01-26 15:14:49 +08:00
|
|
|
SourceID: Params.NodeID,
|
2021-01-22 19:43:27 +08:00
|
|
|
},
|
|
|
|
DbName: "",
|
|
|
|
CollectionName: collectionName,
|
|
|
|
CollectionID: collection.CollectionID,
|
|
|
|
})
|
2021-02-23 09:58:06 +08:00
|
|
|
if err = VerifyResponse(partitions, err); err != nil {
|
|
|
|
log.Error("show partitions error", zap.String("collectionName", collectionName), zap.Int64("collectionID", collection.CollectionID), zap.Error(err))
|
2021-01-22 19:43:27 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-04-09 09:55:04 +08:00
|
|
|
err = s.meta.AddCollection(&datapb.CollectionInfo{
|
2021-01-22 19:43:27 +08:00
|
|
|
ID: collection.CollectionID,
|
|
|
|
Schema: collection.Schema,
|
2021-01-23 14:41:29 +08:00
|
|
|
Partitions: partitions.PartitionIDs,
|
2021-01-22 19:43:27 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-02-23 09:58:06 +08:00
|
|
|
log.Error("add collection to meta error", zap.Int64("collectionID", collection.CollectionID), zap.Error(err))
|
2021-01-22 19:43:27 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 15:37:04 +08:00
|
|
|
log.Debug("load collection meta from master complete")
|
2021-01-22 19:43:27 +08:00
|
|
|
return nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
2021-01-26 15:14:49 +08:00
|
|
|
|
2021-04-13 09:47:02 +08:00
|
|
|
func (s *Server) getDDChannel() error {
|
|
|
|
s.ddChannelMu.Lock()
|
|
|
|
defer s.ddChannelMu.Unlock()
|
|
|
|
if len(s.ddChannelMu.name) == 0 {
|
|
|
|
resp, err := s.masterClient.GetDdChannel(s.ctx)
|
|
|
|
if err = VerifyResponse(resp, err); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.ddChannelMu.name = resp.Value
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:14:49 +08:00
|
|
|
func (s *Server) checkMasterIsHealthy() error {
|
|
|
|
ticker := time.NewTicker(300 * time.Millisecond)
|
|
|
|
ctx, cancel := context.WithTimeout(s.ctx, 30*time.Second)
|
|
|
|
defer func() {
|
|
|
|
ticker.Stop()
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
for {
|
2021-03-12 14:22:09 +08:00
|
|
|
var resp *internalpb.ComponentStates
|
2021-01-26 15:14:49 +08:00
|
|
|
var err error
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-03-05 10:15:27 +08:00
|
|
|
return errors.New("master is not healthy")
|
2021-01-26 15:14:49 +08:00
|
|
|
case <-ticker.C:
|
2021-02-26 17:44:24 +08:00
|
|
|
resp, err = s.masterClient.GetComponentStates(ctx)
|
2021-02-23 09:58:06 +08:00
|
|
|
if err = VerifyResponse(resp, err); err != nil {
|
2021-01-26 15:14:49 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 14:22:09 +08:00
|
|
|
if resp.State.StateCode == internalpb.StateCode_Healthy {
|
2021-01-26 15:14:49 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:43:41 +08:00
|
|
|
func (s *Server) startServerLoop() {
|
|
|
|
s.serverLoopCtx, s.serverLoopCancel = context.WithCancel(s.ctx)
|
2021-05-21 18:30:41 +08:00
|
|
|
s.serverLoopWg.Add(2)
|
2021-01-26 09:43:41 +08:00
|
|
|
go s.startStatsChannel(s.serverLoopCtx)
|
2021-05-25 15:35:37 +08:00
|
|
|
go s.startDataNodeTtLoop(s.serverLoopCtx)
|
2021-01-26 09:43:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) startStatsChannel(ctx context.Context) {
|
2021-03-04 16:01:30 +08:00
|
|
|
defer logutil.LogPanic()
|
2021-01-26 09:43:41 +08:00
|
|
|
defer s.serverLoopWg.Done()
|
2021-02-08 14:30:54 +08:00
|
|
|
statsStream, _ := s.msFactory.NewMsgStream(ctx)
|
2021-02-04 14:37:12 +08:00
|
|
|
statsStream.AsConsumer([]string{Params.StatisticsChannelName}, Params.DataServiceSubscriptionName)
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("DataService AsConsumer: " + Params.StatisticsChannelName + " : " + Params.DataServiceSubscriptionName)
|
2021-05-19 14:13:53 +08:00
|
|
|
// try to restore last processed pos
|
|
|
|
pos, err := s.loadStreamLastPos(streamTypeStats)
|
2021-05-25 16:22:02 +08:00
|
|
|
log.Debug("load last pos of stats channel", zap.Any("pos", pos))
|
2021-05-19 14:13:53 +08:00
|
|
|
if err == nil {
|
2021-05-29 23:21:34 +08:00
|
|
|
err = statsStream.Seek([]*internalpb.MsgPosition{pos})
|
2021-05-19 14:13:53 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to seek to last pos for statsStream",
|
2021-06-04 16:29:35 +08:00
|
|
|
zap.String("StatisticsChanName", Params.StatisticsChannelName),
|
2021-05-19 14:13:53 +08:00
|
|
|
zap.String("DataServiceSubscriptionName", Params.DataServiceSubscriptionName),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 09:43:41 +08:00
|
|
|
statsStream.Start()
|
|
|
|
defer statsStream.Close()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2021-03-25 14:41:46 +08:00
|
|
|
msgPack := statsStream.Consume()
|
2021-04-16 16:30:55 +08:00
|
|
|
if msgPack == nil {
|
2021-05-25 15:35:37 +08:00
|
|
|
return
|
2021-04-16 16:30:55 +08:00
|
|
|
}
|
2021-01-26 09:43:41 +08:00
|
|
|
for _, msg := range msgPack.Msgs {
|
2021-04-24 11:29:15 +08:00
|
|
|
if msg.Type() != commonpb.MsgType_SegmentStatistics {
|
|
|
|
log.Warn("receive unknown msg from segment statistics channel", zap.Stringer("msgType", msg.Type()))
|
|
|
|
continue
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
2021-04-24 11:29:15 +08:00
|
|
|
ssMsg := msg.(*msgstream.SegmentStatisticsMsg)
|
|
|
|
for _, stat := range ssMsg.SegStats {
|
2021-01-26 09:43:41 +08:00
|
|
|
if err := s.statsHandler.HandleSegmentStat(stat); err != nil {
|
2021-02-23 09:58:06 +08:00
|
|
|
log.Error("handle segment stat error", zap.Int64("segmentID", stat.SegmentID), zap.Error(err))
|
2021-01-26 09:43:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-05-19 14:13:53 +08:00
|
|
|
if ssMsg.MsgPosition != nil {
|
|
|
|
err := s.storeStreamPos(streamTypeStats, ssMsg.MsgPosition)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Fail to store current success pos for Stats stream",
|
|
|
|
zap.Stringer("pos", ssMsg.MsgPosition),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warn("Empty Msg Pos found ", zap.Int64("msgid", msg.ID()))
|
|
|
|
}
|
2021-01-26 09:43:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:35:37 +08:00
|
|
|
func (s *Server) startDataNodeTtLoop(ctx context.Context) {
|
2021-03-04 16:01:30 +08:00
|
|
|
defer logutil.LogPanic()
|
2021-01-26 09:43:41 +08:00
|
|
|
defer s.serverLoopWg.Done()
|
2021-05-25 15:35:37 +08:00
|
|
|
ttMsgStream, err := s.msFactory.NewMsgStream(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("new msg stream failed", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ttMsgStream.AsConsumer([]string{Params.TimeTickChannelName},
|
|
|
|
Params.DataServiceSubscriptionName)
|
|
|
|
log.Debug(fmt.Sprintf("dataservice AsConsumer:%s:%s",
|
|
|
|
Params.TimeTickChannelName, Params.DataServiceSubscriptionName))
|
|
|
|
ttMsgStream.Start()
|
|
|
|
defer ttMsgStream.Close()
|
2021-01-26 09:43:41 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-05-25 15:35:37 +08:00
|
|
|
log.Debug("data node tt loop done")
|
2021-01-26 09:43:41 +08:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2021-05-25 15:35:37 +08:00
|
|
|
msgPack := ttMsgStream.Consume()
|
2021-04-16 16:30:55 +08:00
|
|
|
if msgPack == nil {
|
2021-05-25 15:35:37 +08:00
|
|
|
return
|
2021-04-16 16:30:55 +08:00
|
|
|
}
|
2021-01-26 09:43:41 +08:00
|
|
|
for _, msg := range msgPack.Msgs {
|
2021-05-25 15:35:37 +08:00
|
|
|
if msg.Type() != commonpb.MsgType_DataNodeTt {
|
|
|
|
log.Warn("receive unexpected msg type from tt channel",
|
|
|
|
zap.Stringer("msgType", msg.Type()))
|
2021-01-26 09:43:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-05-25 15:35:37 +08:00
|
|
|
ttMsg := msg.(*msgstream.DataNodeTtMsg)
|
|
|
|
|
|
|
|
coll2Segs := make(map[UniqueID][]UniqueID)
|
|
|
|
ch := ttMsg.ChannelName
|
|
|
|
ts := ttMsg.Timestamp
|
|
|
|
segments, err := s.segAllocator.GetFlushableSegments(ctx, ch, ts)
|
2021-01-26 09:43:41 +08:00
|
|
|
if err != nil {
|
2021-05-25 15:35:37 +08:00
|
|
|
log.Warn("get flushable segments failed", zap.Error(err))
|
2021-01-26 09:43:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-05-25 15:35:37 +08:00
|
|
|
for _, id := range segments {
|
|
|
|
sInfo, err := s.meta.GetSegment(id)
|
2021-05-19 14:13:53 +08:00
|
|
|
if err != nil {
|
2021-05-25 15:35:37 +08:00
|
|
|
log.Error("get segment from meta error", zap.Int64("id", id),
|
2021-05-19 14:13:53 +08:00
|
|
|
zap.Error(err))
|
2021-05-25 15:35:37 +08:00
|
|
|
continue
|
2021-05-19 14:13:53 +08:00
|
|
|
}
|
2021-05-25 15:35:37 +08:00
|
|
|
collID, segID := sInfo.CollectionID, sInfo.ID
|
|
|
|
coll2Segs[collID] = append(coll2Segs[collID], segID)
|
|
|
|
}
|
|
|
|
|
|
|
|
for collID, segIDs := range coll2Segs {
|
|
|
|
s.cluster.FlushSegment(&datapb.FlushSegmentsRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Flush,
|
|
|
|
MsgID: -1, // todo add msg id
|
|
|
|
Timestamp: 0, // todo
|
|
|
|
SourceID: Params.NodeID,
|
|
|
|
},
|
|
|
|
CollectionID: collID,
|
|
|
|
SegmentIDs: segIDs,
|
|
|
|
})
|
2021-05-19 14:13:53 +08:00
|
|
|
}
|
2021-01-26 09:43:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 11:07:07 +08:00
|
|
|
func (s *Server) Stop() error {
|
2021-01-28 11:24:41 +08:00
|
|
|
s.cluster.ShutDownClients()
|
2021-05-25 15:35:37 +08:00
|
|
|
s.flushMsgStream.Close()
|
2021-01-23 20:22:59 +08:00
|
|
|
s.stopServerLoop()
|
2021-01-22 11:07:07 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-16 16:30:55 +08:00
|
|
|
// CleanMeta only for test
|
|
|
|
func (s *Server) CleanMeta() error {
|
2021-04-24 11:29:15 +08:00
|
|
|
return s.kvClient.RemoveWithPrefix("")
|
2021-04-16 16:30:55 +08:00
|
|
|
}
|
|
|
|
|
2021-01-23 20:22:59 +08:00
|
|
|
func (s *Server) stopServerLoop() {
|
|
|
|
s.serverLoopCancel()
|
|
|
|
s.serverLoopWg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-01-28 11:24:41 +08:00
|
|
|
func (s *Server) newDataNode(ip string, port int64, id UniqueID) (*dataNode, error) {
|
2021-05-26 18:41:37 +08:00
|
|
|
client, err := s.createDataNodeClient(fmt.Sprintf("%s:%d", ip, port))
|
2021-05-25 15:47:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-28 11:24:41 +08:00
|
|
|
if err := client.Init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-23 11:40:30 +08:00
|
|
|
|
2021-01-28 11:24:41 +08:00
|
|
|
if err := client.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &dataNode{
|
|
|
|
id: id,
|
|
|
|
address: struct {
|
|
|
|
ip string
|
|
|
|
port int64
|
|
|
|
}{ip: ip, port: port},
|
|
|
|
client: client,
|
|
|
|
channelNum: 0,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-26 09:45:54 +08:00
|
|
|
//func (s *Server) validateAllocRequest(collID UniqueID, partID UniqueID, channelName string) error {
|
|
|
|
// if !s.meta.HasCollection(collID) {
|
|
|
|
// return fmt.Errorf("can not find collection %d", collID)
|
|
|
|
// }
|
|
|
|
// if !s.meta.HasPartition(collID, partID) {
|
|
|
|
// return fmt.Errorf("can not find partition %d", partID)
|
|
|
|
// }
|
|
|
|
// for _, name := range s.insertChannels {
|
|
|
|
// if name == channelName {
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return fmt.Errorf("can not find channel %s", channelName)
|
|
|
|
//}
|
2021-04-12 16:35:51 +08:00
|
|
|
|
2021-03-23 16:57:59 +08:00
|
|
|
func (s *Server) loadCollectionFromMaster(ctx context.Context, collectionID int64) error {
|
2021-02-26 17:44:24 +08:00
|
|
|
resp, err := s.masterClient.DescribeCollection(ctx, &milvuspb.DescribeCollectionRequest{
|
2021-02-26 09:23:39 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_DescribeCollection,
|
2021-02-26 09:23:39 +08:00
|
|
|
SourceID: Params.NodeID,
|
|
|
|
},
|
|
|
|
DbName: "",
|
|
|
|
CollectionID: collectionID,
|
|
|
|
})
|
|
|
|
if err = VerifyResponse(resp, err); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-12 16:35:51 +08:00
|
|
|
presp, err := s.masterClient.ShowPartitions(ctx, &milvuspb.ShowPartitionsRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_ShowPartitions,
|
|
|
|
MsgID: -1, // todo
|
|
|
|
Timestamp: 0, // todo
|
|
|
|
SourceID: Params.NodeID,
|
2021-01-26 09:43:41 +08:00
|
|
|
},
|
2021-04-12 16:35:51 +08:00
|
|
|
DbName: "",
|
|
|
|
CollectionName: resp.Schema.Name,
|
|
|
|
CollectionID: resp.CollectionID,
|
|
|
|
})
|
|
|
|
if err = VerifyResponse(presp, err); err != nil {
|
|
|
|
log.Error("show partitions error", zap.String("collectionName", resp.Schema.Name), zap.Int64("collectionID", resp.CollectionID), zap.Error(err))
|
2021-01-26 09:43:41 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-04-12 16:35:51 +08:00
|
|
|
collInfo := &datapb.CollectionInfo{
|
|
|
|
ID: resp.CollectionID,
|
|
|
|
Schema: resp.Schema,
|
|
|
|
Partitions: presp.PartitionIDs,
|
|
|
|
}
|
|
|
|
return s.meta.AddCollection(collInfo)
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 15:35:37 +08:00
|
|
|
func (s *Server) prepareBinlogAndPos(req *datapb.SaveBinlogPathsRequest) (map[string]string, error) {
|
|
|
|
meta := make(map[string]string)
|
2021-05-20 11:34:45 +08:00
|
|
|
segInfo, err := s.meta.GetSegment(req.GetSegmentID())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to get segment info", zap.Int64("segmentID", req.GetSegmentID()), zap.Error(err))
|
2021-05-25 15:35:37 +08:00
|
|
|
return nil, err
|
2021-05-20 11:34:45 +08:00
|
|
|
}
|
|
|
|
log.Debug("segment", zap.Int64("segment", segInfo.CollectionID))
|
|
|
|
|
2021-05-26 12:21:55 +08:00
|
|
|
for _, fieldBlp := range req.Field2BinlogPaths {
|
|
|
|
fieldMeta, err := s.prepareField2PathMeta(req.SegmentID, fieldBlp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range fieldMeta {
|
|
|
|
meta[k] = v
|
|
|
|
}
|
2021-05-20 11:34:45 +08:00
|
|
|
}
|
2021-05-26 12:21:55 +08:00
|
|
|
|
2021-05-21 19:38:58 +08:00
|
|
|
ddlMeta, err := s.prepareDDLBinlogMeta(req.CollectionID, req.GetDdlBinlogPaths())
|
2021-05-20 11:34:45 +08:00
|
|
|
if err != nil {
|
2021-05-25 15:35:37 +08:00
|
|
|
return nil, err
|
2021-05-20 11:34:45 +08:00
|
|
|
}
|
|
|
|
for k, v := range ddlMeta {
|
|
|
|
meta[k] = v
|
|
|
|
}
|
2021-05-25 14:31:12 +08:00
|
|
|
segmentPos, err := s.prepareSegmentPos(segInfo, req.GetDmlPosition(), req.GetDdlPosition())
|
2021-05-21 19:38:58 +08:00
|
|
|
if err != nil {
|
2021-05-25 15:35:37 +08:00
|
|
|
return nil, err
|
2021-05-21 19:38:58 +08:00
|
|
|
}
|
|
|
|
for k, v := range segmentPos {
|
|
|
|
meta[k] = v
|
|
|
|
}
|
2021-05-20 11:34:45 +08:00
|
|
|
|
2021-05-25 15:35:37 +08:00
|
|
|
return meta, nil
|
2021-05-20 11:34:45 +08:00
|
|
|
}
|
2021-05-21 16:54:29 +08:00
|
|
|
|
2021-06-07 14:16:36 +08:00
|
|
|
func (s *Server) GetRecoveryInfo(ctx context.Context, req *datapb.GetRecoveryInfoRequest) (*datapb.GetRecoveryInfoResponse, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-05-21 16:54:29 +08:00
|
|
|
func composeSegmentFlushMsgPack(segmentID UniqueID) msgstream.MsgPack {
|
|
|
|
msgPack := msgstream.MsgPack{
|
|
|
|
Msgs: make([]msgstream.TsMsg, 0, 1),
|
|
|
|
}
|
|
|
|
completeFlushMsg := internalpb.SegmentFlushCompletedMsg{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_SegmentFlushDone,
|
|
|
|
MsgID: 0, // TODO
|
|
|
|
Timestamp: 0, // TODO
|
|
|
|
SourceID: Params.NodeID,
|
|
|
|
},
|
|
|
|
SegmentID: segmentID,
|
|
|
|
}
|
|
|
|
var msg msgstream.TsMsg = &msgstream.FlushCompletedMsg{
|
|
|
|
BaseMsg: msgstream.BaseMsg{
|
|
|
|
HashValues: []uint32{0},
|
|
|
|
},
|
|
|
|
SegmentFlushCompletedMsg: completeFlushMsg,
|
|
|
|
}
|
|
|
|
|
|
|
|
msgPack.Msgs = append(msgPack.Msgs, msg)
|
|
|
|
return msgPack
|
|
|
|
}
|