2021-04-19 11:35:38 +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-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
|
|
|
"path"
|
|
|
|
"strconv"
|
2021-03-05 18:16:50 +08:00
|
|
|
"strings"
|
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-02-26 15:17:47 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
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/timesync"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
|
|
|
"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
|
|
|
|
msgProducer *timesync.MsgProducer
|
|
|
|
masterClient types.MasterService
|
|
|
|
ttMsgStream msgstream.MsgStream
|
|
|
|
k2sMsgStream msgstream.MsgStream
|
2021-05-19 18:36:05 +08:00
|
|
|
ddChannelMu struct {
|
2021-04-13 09:47:02 +08:00
|
|
|
sync.Mutex
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
segmentInfoStream msgstream.MsgStream
|
2021-05-21 16:54:29 +08:00
|
|
|
flushMsgStream msgstream.MsgStream
|
2021-04-13 09:47:02 +08:00
|
|
|
insertChannels []string
|
|
|
|
msFactory msgstream.Factory
|
|
|
|
ttBarrier timesync.TimeTickBarrier
|
|
|
|
createDataNodeClient func(addr string) types.DataNode
|
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-04-13 09:47:02 +08:00
|
|
|
s.createDataNodeClient = func(addr string) types.DataNode {
|
|
|
|
return grpcdatanodeclient.NewClient(addr)
|
|
|
|
}
|
2021-03-12 14:22:09 +08:00
|
|
|
s.UpdateStateCode(internalpb.StateCode_Abnormal)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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-01-22 11:07:07 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-01-27 11:34:16 +08:00
|
|
|
if err = s.initMsgProducer(); err != nil {
|
|
|
|
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-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})
|
|
|
|
log.Debug("dataservice AsProducer: " + Params.SegmentInfoChannelName)
|
|
|
|
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 {
|
|
|
|
etcdClient, err := clientv3.New(clientv3.Config{Endpoints: []string{Params.EtcdAddress}})
|
|
|
|
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-02-08 16:42:56 +08:00
|
|
|
if s.ttMsgStream, err = s.msFactory.NewMsgStream(s.ctx); err != nil {
|
2021-02-04 18:11:19 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.ttMsgStream.AsConsumer([]string{Params.TimeTickChannelName}, Params.DataServiceSubscriptionName)
|
2021-03-05 18:16:50 +08:00
|
|
|
log.Debug("dataservice AsConsumer: " + Params.TimeTickChannelName + " : " + Params.DataServiceSubscriptionName)
|
2021-01-22 19:43:27 +08:00
|
|
|
s.ttMsgStream.Start()
|
2021-02-06 10:28:53 +08:00
|
|
|
s.ttBarrier = timesync.NewHardTimeTickBarrier(s.ctx, s.ttMsgStream, s.cluster.GetNodeIDs())
|
|
|
|
s.ttBarrier.Start()
|
2021-02-08 14:30:54 +08:00
|
|
|
if s.k2sMsgStream, err = s.msFactory.NewMsgStream(s.ctx); err != nil {
|
2021-02-04 18:11:19 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.k2sMsgStream.AsProducer(Params.K2SChannelNames)
|
2021-03-05 18:16:50 +08:00
|
|
|
log.Debug("dataservice AsProducer: " + strings.Join(Params.K2SChannelNames, ", "))
|
2021-02-04 17:47:19 +08:00
|
|
|
s.k2sMsgStream.Start()
|
2021-02-04 18:11:19 +08:00
|
|
|
dataNodeTTWatcher := newDataNodeTimeTickWatcher(s.meta, s.segAllocator, s.cluster)
|
2021-01-26 09:43:41 +08:00
|
|
|
k2sMsgWatcher := timesync.NewMsgTimeTickWatcher(s.k2sMsgStream)
|
2021-02-04 18:11:19 +08:00
|
|
|
if s.msgProducer, err = timesync.NewTimeSyncMsgProducer(s.ttBarrier, dataNodeTTWatcher, k2sMsgWatcher); err != nil {
|
2021-01-22 11:07:07 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.msgProducer.Start(s.ctx)
|
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)
|
|
|
|
go s.startSegmentFlushChannel(s.serverLoopCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-05 18:16:50 +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)
|
|
|
|
if err == nil {
|
|
|
|
err = statsStream.Seek(pos)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to seek to last pos for statsStream",
|
|
|
|
zap.String("StatisChanName", Params.StatisticsChannelName),
|
|
|
|
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 {
|
|
|
|
continue
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) startSegmentFlushChannel(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
|
|
|
flushStream, _ := s.msFactory.NewMsgStream(ctx)
|
2021-02-04 14:37:12 +08:00
|
|
|
flushStream.AsConsumer([]string{Params.SegmentInfoChannelName}, Params.DataServiceSubscriptionName)
|
2021-03-05 18:16:50 +08:00
|
|
|
log.Debug("dataservice AsConsumer: " + Params.SegmentInfoChannelName + " : " + Params.DataServiceSubscriptionName)
|
2021-05-19 14:13:53 +08:00
|
|
|
|
|
|
|
// try to restore last processed pos
|
|
|
|
pos, err := s.loadStreamLastPos(streamTypeFlush)
|
|
|
|
if err == nil {
|
|
|
|
err = flushStream.Seek(pos)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to seek to last pos for segment flush Stream",
|
|
|
|
zap.String("SegInfoChannelName", Params.SegmentInfoChannelName),
|
|
|
|
zap.String("DataServiceSubscriptionName", Params.DataServiceSubscriptionName),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:43:41 +08:00
|
|
|
flushStream.Start()
|
|
|
|
defer flushStream.Close()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-02-19 15:37:04 +08:00
|
|
|
log.Debug("segment flush channel shut down")
|
2021-01-26 09:43:41 +08:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2021-03-25 14:41:46 +08:00
|
|
|
msgPack := flushStream.Consume()
|
2021-04-16 16:30:55 +08:00
|
|
|
if msgPack == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-26 09:43:41 +08:00
|
|
|
for _, msg := range msgPack.Msgs {
|
2021-03-10 14:45:35 +08:00
|
|
|
if msg.Type() != commonpb.MsgType_SegmentFlushDone {
|
2021-04-24 11:29:15 +08:00
|
|
|
log.Warn("receive unknown msg from segment flush channel", zap.Stringer("msgType", msg.Type()))
|
2021-01-26 09:43:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-04-24 11:29:15 +08:00
|
|
|
fcMsg := msg.(*msgstream.FlushCompletedMsg)
|
|
|
|
err := s.meta.FlushSegment(fcMsg.SegmentID, fcMsg.BeginTimestamp)
|
|
|
|
log.Debug("dataservice flushed segment", zap.Any("segmentID", fcMsg.SegmentID), zap.Error(err))
|
2021-01-26 09:43:41 +08:00
|
|
|
if err != nil {
|
2021-04-24 11:29:15 +08:00
|
|
|
log.Error("get segment from meta error", zap.Int64("segmentID", fcMsg.SegmentID), zap.Error(err))
|
2021-01-26 09:43:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-05-19 14:13:53 +08:00
|
|
|
|
|
|
|
if fcMsg.MsgPosition != nil {
|
|
|
|
err = s.storeStreamPos(streamTypeFlush, fcMsg.MsgPosition)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Fail to store current success pos for segment flush stream",
|
|
|
|
zap.Stringer("pos", fcMsg.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-01-22 11:07:07 +08:00
|
|
|
func (s *Server) Stop() error {
|
2021-01-28 11:24:41 +08:00
|
|
|
s.cluster.ShutDownClients()
|
2021-01-22 19:43:27 +08:00
|
|
|
s.ttMsgStream.Close()
|
2021-01-26 09:43:41 +08:00
|
|
|
s.k2sMsgStream.Close()
|
2021-04-13 09:47:02 +08:00
|
|
|
s.ttBarrier.Close()
|
2021-01-22 11:07:07 +08:00
|
|
|
s.msgProducer.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-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetComponentStates(ctx context.Context) (*internalpb.ComponentStates, error) {
|
|
|
|
resp := &internalpb.ComponentStates{
|
|
|
|
State: &internalpb.ComponentInfo{
|
2021-01-22 19:43:27 +08:00
|
|
|
NodeID: Params.NodeID,
|
|
|
|
Role: role,
|
2021-03-12 14:22:09 +08:00
|
|
|
StateCode: s.state.Load().(internalpb.StateCode),
|
2021-01-22 19:43:27 +08:00
|
|
|
},
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
2021-01-22 19:43:27 +08:00
|
|
|
},
|
|
|
|
}
|
2021-02-26 17:44:24 +08:00
|
|
|
dataNodeStates, err := s.cluster.GetDataNodeStates(ctx)
|
2021-01-22 19:43:27 +08:00
|
|
|
if err != nil {
|
|
|
|
resp.Status.Reason = err.Error()
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
resp.SubcomponentStates = dataNodeStates
|
2021-03-10 22:06:22 +08:00
|
|
|
resp.Status.ErrorCode = commonpb.ErrorCode_Success
|
2021-01-22 19:43:27 +08:00
|
|
|
return resp, nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (s *Server) GetTimeTickChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
|
2021-02-04 19:34:35 +08:00
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-02-04 19:34:35 +08:00
|
|
|
},
|
|
|
|
Value: Params.TimeTickChannelName,
|
|
|
|
}, nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (s *Server) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
|
2021-02-04 19:34:35 +08:00
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-02-04 19:34:35 +08:00
|
|
|
},
|
|
|
|
Value: Params.StatisticsChannelName,
|
|
|
|
}, nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (s *Server) RegisterNode(ctx context.Context, req *datapb.RegisterNodeRequest) (*datapb.RegisterNodeResponse, error) {
|
2021-01-23 20:22:59 +08:00
|
|
|
ret := &datapb.RegisterNodeResponse{
|
2021-01-22 11:07:07 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
2021-01-22 11:07:07 +08:00
|
|
|
},
|
2021-01-23 20:22:59 +08:00
|
|
|
}
|
2021-02-27 10:11:52 +08:00
|
|
|
log.Debug("DataService: RegisterNode:", zap.String("IP", req.Address.Ip), zap.Int64("Port", req.Address.Port))
|
2021-01-28 11:24:41 +08:00
|
|
|
node, err := s.newDataNode(req.Address.Ip, req.Address.Port, req.Base.SourceID)
|
|
|
|
if err != nil {
|
2021-04-13 09:47:02 +08:00
|
|
|
ret.Status.Reason = err.Error()
|
|
|
|
return ret, nil
|
2021-01-28 11:24:41 +08:00
|
|
|
}
|
2021-02-23 11:40:30 +08:00
|
|
|
|
2021-04-13 09:47:02 +08:00
|
|
|
resp, err := node.client.WatchDmChannels(s.ctx, &datapb.WatchDmChannelsRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: 0,
|
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: 0,
|
|
|
|
SourceID: Params.NodeID,
|
|
|
|
},
|
|
|
|
ChannelNames: s.insertChannels,
|
|
|
|
})
|
2021-02-23 11:40:30 +08:00
|
|
|
|
2021-04-13 09:47:02 +08:00
|
|
|
if err = VerifyResponse(resp, err); err != nil {
|
|
|
|
ret.Status.Reason = err.Error()
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.getDDChannel(); err != nil {
|
|
|
|
ret.Status.Reason = err.Error()
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.ttBarrier != nil {
|
|
|
|
if err = s.ttBarrier.AddPeer(node.id); err != nil {
|
2021-01-23 20:22:59 +08:00
|
|
|
ret.Status.Reason = err.Error()
|
2021-04-13 09:47:02 +08:00
|
|
|
return ret, nil
|
2021-01-23 20:22:59 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 09:47:02 +08:00
|
|
|
|
|
|
|
if err = s.cluster.Register(node); err != nil {
|
|
|
|
ret.Status.Reason = err.Error()
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:06:22 +08:00
|
|
|
ret.Status.ErrorCode = commonpb.ErrorCode_Success
|
2021-03-12 14:22:09 +08:00
|
|
|
ret.InitParams = &internalpb.InitParams{
|
2021-01-23 20:22:59 +08:00
|
|
|
NodeID: Params.NodeID,
|
|
|
|
StartParams: []*commonpb.KeyValuePair{
|
2021-04-13 09:47:02 +08:00
|
|
|
{Key: "DDChannelName", Value: s.ddChannelMu.name},
|
2021-01-23 20:22:59 +08:00
|
|
|
{Key: "SegmentStatisticsChannelName", Value: Params.StatisticsChannelName},
|
|
|
|
{Key: "TimeTickChannelName", Value: Params.TimeTickChannelName},
|
2021-01-25 15:17:17 +08:00
|
|
|
{Key: "CompleteFlushChannelName", Value: Params.SegmentInfoChannelName},
|
2021-01-23 20:22:59 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return ret, nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 11:24:41 +08:00
|
|
|
func (s *Server) newDataNode(ip string, port int64, id UniqueID) (*dataNode, error) {
|
2021-04-13 09:47:02 +08:00
|
|
|
client := s.createDataNodeClient(fmt.Sprintf("%s:%d", ip, port))
|
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-02-26 17:44:24 +08:00
|
|
|
func (s *Server) Flush(ctx context.Context, req *datapb.FlushRequest) (*commonpb.Status, error) {
|
2021-01-26 15:14:49 +08:00
|
|
|
if !s.checkStateIsHealthy() {
|
|
|
|
return &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
2021-01-26 15:14:49 +08:00
|
|
|
Reason: "server is initializing",
|
|
|
|
}, nil
|
|
|
|
}
|
2021-05-10 17:03:24 +08:00
|
|
|
if err := s.segAllocator.SealAllSegments(ctx, req.CollectionID); err != nil {
|
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: fmt.Sprintf("Seal all segments error %s", err),
|
|
|
|
}, nil
|
|
|
|
}
|
2021-01-22 19:43:27 +08:00
|
|
|
return &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-01-22 19:43:27 +08:00
|
|
|
}, nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) AssignSegmentID(ctx context.Context, req *datapb.AssignSegmentIDRequest) (*datapb.AssignSegmentIDResponse, error) {
|
2021-01-26 15:14:49 +08:00
|
|
|
if !s.checkStateIsHealthy() {
|
2021-04-16 16:30:55 +08:00
|
|
|
return &datapb.AssignSegmentIDResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
},
|
|
|
|
}, nil
|
2021-01-26 15:14:49 +08:00
|
|
|
}
|
2021-04-12 16:35:51 +08:00
|
|
|
|
|
|
|
assigns := make([]*datapb.SegmentIDAssignment, 0, len(req.SegmentIDRequests))
|
|
|
|
|
2021-04-16 16:30:55 +08:00
|
|
|
var appendFailedAssignment = func(err string) {
|
|
|
|
assigns = append(assigns, &datapb.SegmentIDAssignment{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: err,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
for _, r := range req.SegmentIDRequests {
|
2021-02-26 09:23:39 +08:00
|
|
|
if !s.meta.HasCollection(r.CollectionID) {
|
2021-03-23 16:57:59 +08:00
|
|
|
if err := s.loadCollectionFromMaster(ctx, r.CollectionID); err != nil {
|
2021-04-16 16:30:55 +08:00
|
|
|
appendFailedAssignment(fmt.Sprintf("can not load collection %d", r.CollectionID))
|
2021-02-26 09:23:39 +08:00
|
|
|
log.Error("load collection from master error", zap.Int64("collectionID", r.CollectionID), zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-04-12 16:35:51 +08:00
|
|
|
//if err := s.validateAllocRequest(r.CollectionID, r.PartitionID, r.ChannelName); err != nil {
|
|
|
|
//result.Status.Reason = err.Error()
|
|
|
|
//assigns = append(assigns, result)
|
|
|
|
//continue
|
|
|
|
//}
|
2021-01-22 11:07:07 +08:00
|
|
|
|
2021-05-21 18:30:41 +08:00
|
|
|
segmentID, retCount, expireTs, err := s.segAllocator.AllocSegment(ctx, r.CollectionID, r.PartitionID, r.ChannelName, int64(r.Count))
|
2021-04-12 16:35:51 +08:00
|
|
|
if err != nil {
|
2021-04-16 16:30:55 +08:00
|
|
|
appendFailedAssignment(fmt.Sprintf("allocation of collection %d, partition %d, channel %s, count %d error: %s",
|
|
|
|
r.CollectionID, r.PartitionID, r.ChannelName, r.Count, err.Error()))
|
2021-04-12 16:35:51 +08:00
|
|
|
continue
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-04-16 16:30:55 +08:00
|
|
|
result := &datapb.SegmentIDAssignment{
|
|
|
|
SegID: segmentID,
|
|
|
|
ChannelName: r.ChannelName,
|
|
|
|
Count: uint32(retCount),
|
|
|
|
CollectionID: r.CollectionID,
|
|
|
|
PartitionID: r.PartitionID,
|
|
|
|
ExpireTime: expireTs,
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
Reason: "",
|
|
|
|
},
|
|
|
|
}
|
2021-04-12 16:35:51 +08:00
|
|
|
assigns = append(assigns, result)
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
2021-04-16 16:30:55 +08:00
|
|
|
return &datapb.AssignSegmentIDResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
SegIDAssignments: assigns,
|
|
|
|
}, nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
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-03-12 14:22:09 +08:00
|
|
|
func (s *Server) ShowSegments(ctx context.Context, req *datapb.ShowSegmentsRequest) (*datapb.ShowSegmentsResponse, error) {
|
|
|
|
resp := &datapb.ShowSegmentsResponse{
|
2021-02-01 16:20:13 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
2021-02-01 16:20:13 +08:00
|
|
|
},
|
|
|
|
}
|
2021-01-26 15:14:49 +08:00
|
|
|
if !s.checkStateIsHealthy() {
|
2021-02-01 16:20:13 +08:00
|
|
|
resp.Status.Reason = "server is initializing"
|
|
|
|
return resp, nil
|
2021-01-26 15:14:49 +08:00
|
|
|
}
|
2021-02-02 14:25:58 +08:00
|
|
|
ids := s.meta.GetSegmentsOfPartition(req.CollectionID, req.PartitionID)
|
2021-03-10 22:06:22 +08:00
|
|
|
resp.Status.ErrorCode = commonpb.ErrorCode_Success
|
2021-02-01 16:20:13 +08:00
|
|
|
resp.SegmentIDs = ids
|
|
|
|
return resp, nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetSegmentStates(ctx context.Context, req *datapb.GetSegmentStatesRequest) (*datapb.GetSegmentStatesResponse, error) {
|
|
|
|
resp := &datapb.GetSegmentStatesResponse{
|
2021-01-22 11:07:07 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
2021-01-22 11:07:07 +08:00
|
|
|
},
|
|
|
|
}
|
2021-01-26 15:14:49 +08:00
|
|
|
if !s.checkStateIsHealthy() {
|
|
|
|
resp.Status.Reason = "server is initializing"
|
|
|
|
return resp, nil
|
|
|
|
}
|
2021-01-22 11:07:07 +08:00
|
|
|
|
2021-02-02 10:58:39 +08:00
|
|
|
for _, segmentID := range req.SegmentIDs {
|
|
|
|
state := &datapb.SegmentStateInfo{
|
|
|
|
Status: &commonpb.Status{},
|
|
|
|
SegmentID: segmentID,
|
|
|
|
}
|
|
|
|
segmentInfo, err := s.meta.GetSegment(segmentID)
|
|
|
|
if err != nil {
|
2021-03-10 22:06:22 +08:00
|
|
|
state.Status.ErrorCode = commonpb.ErrorCode_UnexpectedError
|
2021-02-02 10:58:39 +08:00
|
|
|
state.Status.Reason = "get segment states error: " + err.Error()
|
|
|
|
} else {
|
2021-03-10 22:06:22 +08:00
|
|
|
state.Status.ErrorCode = commonpb.ErrorCode_Success
|
2021-02-02 10:58:39 +08:00
|
|
|
state.State = segmentInfo.State
|
2021-02-04 11:19:48 +08:00
|
|
|
state.StartPosition = segmentInfo.StartPosition
|
|
|
|
state.EndPosition = segmentInfo.EndPosition
|
2021-02-02 10:58:39 +08:00
|
|
|
}
|
|
|
|
resp.States = append(resp.States, state)
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
resp.Status.ErrorCode = commonpb.ErrorCode_Success
|
2021-02-02 10:58:39 +08:00
|
|
|
|
2021-01-22 11:07:07 +08:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetInsertBinlogPaths(ctx context.Context, req *datapb.GetInsertBinlogPathsRequest) (*datapb.GetInsertBinlogPathsResponse, error) {
|
|
|
|
resp := &datapb.GetInsertBinlogPathsResponse{
|
2021-01-26 15:14:49 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
2021-01-26 15:14:49 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
p := path.Join(Params.SegmentFlushMetaPath, strconv.FormatInt(req.SegmentID, 10))
|
2021-04-24 11:29:15 +08:00
|
|
|
_, values, err := s.kvClient.LoadWithPrefix(p)
|
2021-01-26 15:14:49 +08:00
|
|
|
if err != nil {
|
|
|
|
resp.Status.Reason = err.Error()
|
|
|
|
return resp, nil
|
|
|
|
}
|
2021-03-23 18:50:13 +08:00
|
|
|
m := make(map[int64][]string)
|
|
|
|
tMeta := &datapb.SegmentFieldBinlogMeta{}
|
|
|
|
for _, v := range values {
|
|
|
|
if err := proto.UnmarshalText(v, tMeta); err != nil {
|
2021-03-30 20:19:30 +08:00
|
|
|
resp.Status.Reason = fmt.Errorf("DataService GetInsertBinlogPaths UnmarshalText datapb.SegmentFieldBinlogMeta err:%w", err).Error()
|
2021-03-23 18:50:13 +08:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
m[tMeta.FieldID] = append(m[tMeta.FieldID], tMeta.BinlogPath)
|
2021-01-26 15:14:49 +08:00
|
|
|
}
|
2021-03-23 18:50:13 +08:00
|
|
|
|
|
|
|
fids := make([]UniqueID, len(m))
|
|
|
|
paths := make([]*internalpb.StringList, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
fids = append(fids, k)
|
|
|
|
paths = append(paths, &internalpb.StringList{Values: v})
|
2021-01-26 15:14:49 +08:00
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
resp.Status.ErrorCode = commonpb.ErrorCode_Success
|
2021-03-23 18:50:13 +08:00
|
|
|
resp.FieldIDs = fids
|
2021-01-26 15:14:49 +08:00
|
|
|
resp.Paths = paths
|
|
|
|
return resp, nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetInsertChannels(ctx context.Context, req *datapb.GetInsertChannelsRequest) (*internalpb.StringList, error) {
|
|
|
|
return &internalpb.StringList{
|
2021-02-04 19:34:35 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-02-04 19:34:35 +08:00
|
|
|
},
|
|
|
|
Values: s.insertChannels,
|
|
|
|
}, nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetCollectionStatistics(ctx context.Context, req *datapb.GetCollectionStatisticsRequest) (*datapb.GetCollectionStatisticsResponse, error) {
|
|
|
|
resp := &datapb.GetCollectionStatisticsResponse{
|
2021-02-02 14:25:58 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
2021-02-02 14:25:58 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
nums, err := s.meta.GetNumRowsOfCollection(req.CollectionID)
|
|
|
|
if err != nil {
|
|
|
|
resp.Status.Reason = err.Error()
|
|
|
|
return resp, nil
|
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
resp.Status.ErrorCode = commonpb.ErrorCode_Success
|
2021-02-24 17:56:03 +08:00
|
|
|
resp.Stats = append(resp.Stats, &commonpb.KeyValuePair{Key: "row_count", Value: strconv.FormatInt(nums, 10)})
|
2021-02-02 14:25:58 +08:00
|
|
|
return resp, nil
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetPartitionStatistics(ctx context.Context, req *datapb.GetPartitionStatisticsRequest) (*datapb.GetPartitionStatisticsResponse, error) {
|
2021-05-10 17:39:08 +08:00
|
|
|
resp := &datapb.GetPartitionStatisticsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nums, err := s.meta.GetNumRowsOfPartition(req.CollectionID, req.PartitionID)
|
|
|
|
if err != nil {
|
|
|
|
resp.Status.Reason = err.Error()
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
resp.Status.ErrorCode = commonpb.ErrorCode_Success
|
|
|
|
resp.Stats = append(resp.Stats, &commonpb.KeyValuePair{Key: "row_count", Value: strconv.FormatInt(nums, 10)})
|
|
|
|
return resp, nil
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
2021-01-25 15:17:17 +08:00
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (s *Server) GetSegmentInfoChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
|
2021-02-04 19:34:35 +08:00
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-02-04 19:34:35 +08:00
|
|
|
},
|
|
|
|
Value: Params.SegmentInfoChannelName,
|
|
|
|
}, nil
|
2021-01-25 15:17:17 +08:00
|
|
|
}
|
2021-02-02 14:25:58 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetSegmentInfo(ctx context.Context, req *datapb.GetSegmentInfoRequest) (*datapb.GetSegmentInfoResponse, error) {
|
|
|
|
resp := &datapb.GetSegmentInfoResponse{
|
2021-02-04 10:25:01 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
2021-02-04 10:25:01 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if !s.checkStateIsHealthy() {
|
|
|
|
resp.Status.Reason = "data service is not healthy"
|
|
|
|
return resp, nil
|
|
|
|
}
|
2021-04-09 09:55:04 +08:00
|
|
|
infos := make([]*datapb.SegmentInfo, 0, len(req.SegmentIDs))
|
|
|
|
for _, id := range req.SegmentIDs {
|
|
|
|
info, err := s.meta.GetSegment(id)
|
2021-02-04 10:25:01 +08:00
|
|
|
if err != nil {
|
|
|
|
resp.Status.Reason = err.Error()
|
|
|
|
return resp, nil
|
|
|
|
}
|
2021-04-09 09:55:04 +08:00
|
|
|
infos = append(infos, info)
|
2021-02-04 10:25:01 +08:00
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
resp.Status.ErrorCode = commonpb.ErrorCode_Success
|
2021-02-04 10:25:01 +08:00
|
|
|
resp.Infos = infos
|
|
|
|
return resp, nil
|
|
|
|
}
|
2021-05-20 11:34:45 +08:00
|
|
|
|
|
|
|
// SaveBinlogPaths implement DataServiceServer
|
|
|
|
func (s *Server) SaveBinlogPaths(ctx context.Context, req *datapb.SaveBinlogPathsRequest) (*commonpb.Status, error) {
|
|
|
|
resp := &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
}
|
|
|
|
if !s.checkStateIsHealthy() {
|
|
|
|
resp.Reason = "server is initializing"
|
|
|
|
return resp, nil
|
|
|
|
}
|
2021-05-21 16:54:29 +08:00
|
|
|
if s.flushMsgStream == nil {
|
|
|
|
resp.Reason = "flush msg stream nil"
|
|
|
|
return resp, nil
|
|
|
|
}
|
2021-05-20 11:34:45 +08:00
|
|
|
|
|
|
|
// check segment id & collection id matched
|
|
|
|
_, err := s.meta.GetCollection(req.GetCollectionID())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to get collection info", zap.Int64("collectionID", req.GetCollectionID()), zap.Error(err))
|
|
|
|
resp.Reason = err.Error()
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
resp.Reason = err.Error()
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
log.Debug("segment", zap.Int64("segment", segInfo.CollectionID))
|
|
|
|
|
|
|
|
meta := make(map[string]string)
|
|
|
|
fieldMeta, err := s.prepareField2PathMeta(req.SegmentID, req.Field2BinlogPaths)
|
|
|
|
if err != nil {
|
|
|
|
resp.Reason = err.Error()
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
for k, v := range fieldMeta {
|
|
|
|
meta[k] = v
|
|
|
|
}
|
|
|
|
ddlMeta, err := s.prepareDDLBinlogMeta(req.CollectionID,
|
|
|
|
req.GetColl2TsBinlogPaths(), req.GetColl2DdlBinlogPaths())
|
|
|
|
if err != nil {
|
|
|
|
resp.Reason = err.Error()
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
for k, v := range ddlMeta {
|
|
|
|
meta[k] = v
|
|
|
|
}
|
2021-05-21 16:54:29 +08:00
|
|
|
// Save into k-v store
|
2021-05-20 11:34:45 +08:00
|
|
|
err = s.SaveBinLogMetaTxn(meta)
|
|
|
|
if err != nil {
|
|
|
|
resp.Reason = err.Error()
|
|
|
|
return resp, err
|
|
|
|
}
|
2021-05-21 16:54:29 +08:00
|
|
|
// write flush msg into segmentInfo/flush stream
|
|
|
|
msgPack := composeSegmentFlushMsgPack(req.SegmentID)
|
|
|
|
err = s.flushMsgStream.Produce(&msgPack)
|
|
|
|
if err != nil {
|
|
|
|
resp.Reason = err.Error()
|
|
|
|
return resp, err
|
|
|
|
}
|
2021-05-20 11:34:45 +08:00
|
|
|
|
|
|
|
resp.ErrorCode = commonpb.ErrorCode_Success
|
|
|
|
return resp, nil
|
|
|
|
}
|
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
|
|
|
|
}
|