server.go 19.7 KB
Newer Older
S
sunby 已提交
1 2 3
// 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
S
sunby 已提交
4 5 6 7 8 9 10
// 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.
11

12
package datacoord
S
sunby 已提交
13

S
sunby 已提交
14 15
import (
	"context"
S
sunby 已提交
16
	"errors"
17
	"fmt"
S
sunby 已提交
18
	"math/rand"
S
sunby 已提交
19
	"sync"
S
sunby 已提交
20 21 22
	"sync/atomic"
	"time"

23 24
	"github.com/milvus-io/milvus/internal/util/metricsinfo"

S
sunby 已提交
25
	datanodeclient "github.com/milvus-io/milvus/internal/distributed/datanode/client"
26
	rootcoordclient "github.com/milvus-io/milvus/internal/distributed/rootcoord/client"
S
sunby 已提交
27
	"github.com/milvus-io/milvus/internal/logutil"
28
	"go.uber.org/zap"
S
sunby 已提交
29

X
Xiangyu Wang 已提交
30 31 32 33 34
	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"
G
godchen 已提交
35
	"github.com/milvus-io/milvus/internal/util/sessionutil"
X
Xiangyu Wang 已提交
36
	"github.com/milvus-io/milvus/internal/util/typeutil"
N
neza2017 已提交
37

X
Xiangyu Wang 已提交
38 39
	"github.com/milvus-io/milvus/internal/proto/commonpb"
	"github.com/milvus-io/milvus/internal/proto/datapb"
S
sunby 已提交
40
	"github.com/milvus-io/milvus/internal/proto/internalpb"
X
Xiangyu Wang 已提交
41
	"github.com/milvus-io/milvus/internal/proto/milvuspb"
S
sunby 已提交
42 43
)

S
sunby 已提交
44
const connEtcdMaxRetryTime = 100000
S
sunby 已提交
45

46 47 48 49 50 51 52 53
var (
	// TODO: sunby put to config
	enableTtChecker  = true
	ttCheckerName    = "dataTtChecker"
	ttMaxInterval    = 3 * time.Minute
	ttCheckerWarnMsg = fmt.Sprintf("we haven't received tt for %f minutes", ttMaxInterval.Minutes())
)

S
sunby 已提交
54
type (
55 56 57
	// UniqueID shortcut for typeutil.UniqueID
	UniqueID = typeutil.UniqueID
	// Timestamp shortcurt for typeutil.Timestamp
S
sunby 已提交
58
	Timestamp = typeutil.Timestamp
S
sunby 已提交
59
)
S
sunby 已提交
60

61
// ServerState type alias, presents datacoord Server State
62 63 64 65 66 67 68 69 70 71 72
type ServerState = int64

const (
	// ServerStateStopped state stands for just created or stopped `Server` instance
	ServerStateStopped ServerState = 0
	// ServerStateInitializing state stands initializing `Server` instance
	ServerStateInitializing ServerState = 1
	// ServerStateHealthy state stands for healthy `Server` instance
	ServerStateHealthy ServerState = 2
)

73 74 75 76 77
// DataNodeCreatorFunc creator function for datanode
type DataNodeCreatorFunc func(ctx context.Context, addr string) (types.DataNode, error)

// RootCoordCreatorFunc creator function for rootcoord
type RootCoordCreatorFunc func(ctx context.Context, metaRootPath string, etcdEndpoints []string) (types.RootCoord, error)
S
sunby 已提交
78

79 80 81
// makes sure Server implements `DataCoord`
var _ types.DataCoord = (*Server)(nil)

82 83 84
// makes sure Server implements `positionProvider`
var _ positionProvider = (*Server)(nil)

85 86
// Server implements `types.Datacoord`
// handles Data Cooridinator related jobs
N
neza2017 已提交
87
type Server struct {
S
sunby 已提交
88 89 90 91
	ctx              context.Context
	serverLoopCtx    context.Context
	serverLoopCancel context.CancelFunc
	serverLoopWg     sync.WaitGroup
92
	isServing        ServerState
S
sunby 已提交
93
	helper           ServerHelper
94

95 96 97 98
	kvClient        *etcdkv.EtcdKV
	meta            *meta
	segmentManager  Manager
	allocator       allocator
S
sunby 已提交
99
	cluster         *Cluster
100
	rootCoordClient types.RootCoord
101

102 103
	metricsCacheManager *metricsinfo.MetricsCacheManager

104 105
	flushCh   chan UniqueID
	msFactory msgstream.Factory
106

107 108 109
	session *sessionutil.Session
	liveCh  <-chan bool
	eventCh <-chan *sessionutil.SessionEvent
110

111 112
	dataNodeCreator        DataNodeCreatorFunc
	rootCoordClientCreator RootCoordCreatorFunc
N
neza2017 已提交
113
}
S
sunby 已提交
114

115
// ServerHelper datacoord server injection helper
S
sunby 已提交
116 117 118 119 120 121 122 123 124 125
type ServerHelper struct {
	eventAfterHandleDataNodeTt func()
}

func defaultServerHelper() ServerHelper {
	return ServerHelper{
		eventAfterHandleDataNodeTt: func() {},
	}
}

126
// Option utility function signature to set DataCoord server attributes
S
sunby 已提交
127 128
type Option func(svr *Server)

129
// SetRootCoordCreator returns an `Option` setting RootCoord creator with provided parameter
130
func SetRootCoordCreator(creator RootCoordCreatorFunc) Option {
S
sunby 已提交
131 132 133 134 135
	return func(svr *Server) {
		svr.rootCoordClientCreator = creator
	}
}

136
// SetServerHelper returns an `Option` setting ServerHelp with provided parameter
S
sunby 已提交
137 138 139 140 141 142
func SetServerHelper(helper ServerHelper) Option {
	return func(svr *Server) {
		svr.helper = helper
	}
}

143 144 145 146 147 148 149
// SetCluster returns an `Option` setting Cluster with provided parameter
func SetCluster(cluster *Cluster) Option {
	return func(svr *Server) {
		svr.cluster = cluster
	}
}

150 151 152 153 154 155 156
// SetDataNodeCreator returns an `Option` setting DataNode create function
func SetDataNodeCreator(creator DataNodeCreatorFunc) Option {
	return func(svr *Server) {
		svr.dataNodeCreator = creator
	}
}

157
// CreateServer create `Server` instance
S
sunby 已提交
158
func CreateServer(ctx context.Context, factory msgstream.Factory, opts ...Option) (*Server, error) {
S
sunby 已提交
159
	rand.Seed(time.Now().UnixNano())
S
sunby 已提交
160
	s := &Server{
S
sunby 已提交
161 162 163
		ctx:                    ctx,
		msFactory:              factory,
		flushCh:                make(chan UniqueID, 1024),
164
		dataNodeCreator:        defaultDataNodeCreatorFunc,
S
sunby 已提交
165
		rootCoordClientCreator: defaultRootCoordCreatorFunc,
S
sunby 已提交
166
		helper:                 defaultServerHelper(),
167 168

		metricsCacheManager: metricsinfo.NewMetricsCacheManager(),
S
sunby 已提交
169
	}
S
sunby 已提交
170 171 172 173

	for _, opt := range opts {
		opt(s)
	}
S
sunby 已提交
174 175 176
	return s, nil
}

177
// defaultDataNodeCreatorFunc defines the default behavior to get a DataNode
G
godchen 已提交
178 179
func defaultDataNodeCreatorFunc(ctx context.Context, addr string) (types.DataNode, error) {
	return datanodeclient.NewClient(ctx, addr)
S
sunby 已提交
180 181
}

G
godchen 已提交
182 183
func defaultRootCoordCreatorFunc(ctx context.Context, metaRootPath string, etcdEndpoints []string) (types.RootCoord, error) {
	return rootcoordclient.NewClient(ctx, metaRootPath, etcdEndpoints)
S
sunby 已提交
184 185
}

186 187
// Register register data service at etcd
func (s *Server) Register() error {
188
	s.session = sessionutil.NewSession(s.ctx, Params.MetaRootPath, Params.EtcdEndpoints)
189 190 191
	if s.session == nil {
		return errors.New("failed to initialize session")
	}
192
	s.liveCh = s.session.Init(typeutil.DataCoordRole, Params.IP, true)
193
	Params.NodeID = s.session.ServerID
X
Xiaofan 已提交
194
	Params.SetLogger(typeutil.UniqueID(-1))
195 196 197
	return nil
}

198
// Init change server state to Initializing
199
func (s *Server) Init() error {
200
	atomic.StoreInt64(&s.isServing, ServerStateInitializing)
S
sunby 已提交
201 202 203
	return nil
}

204 205 206 207 208 209 210
// Start initialize `Server` members and start loops, follow steps are taken:
// 1. initialize message factory parameters
// 2. initialize root coord client, meta, datanode cluster, segment info channel,
//		allocator, segment manager
// 3. start service discovery and server loops, which includes message stream handler (segment statistics,datanode tt)
//		datanodes etcd watch, etcd alive check and flush completed status check
// 4. set server state to Healthy
S
sunby 已提交
211
func (s *Server) Start() error {
212
	var err error
S
sunby 已提交
213 214 215 216 217 218 219 220
	m := map[string]interface{}{
		"PulsarAddress":  Params.PulsarAddress,
		"ReceiveBufSize": 1024,
		"PulsarBufSize":  1024}
	err = s.msFactory.SetParams(m)
	if err != nil {
		return err
	}
221
	if err = s.initRootCoordClient(); err != nil {
S
sunby 已提交
222 223
		return err
	}
224

S
sunby 已提交
225 226 227
	if err = s.initMeta(); err != nil {
		return err
	}
228

S
sunby 已提交
229 230 231
	if err = s.initCluster(); err != nil {
		return err
	}
232

C
congqixia 已提交
233
	s.allocator = newRootCoordAllocator(s.rootCoordClient)
234

235
	s.startSegmentManager()
S
sunby 已提交
236 237 238
	if err = s.initServiceDiscovery(); err != nil {
		return err
	}
239

S
sunby 已提交
240
	s.startServerLoop()
241 242
	Params.CreatedTime = time.Now()
	Params.UpdatedTime = time.Now()
243
	atomic.StoreInt64(&s.isServing, ServerStateHealthy)
244
	log.Debug("dataCoordinator startup success")
245

S
sunby 已提交
246
	return nil
247 248 249
}

func (s *Server) initCluster() error {
S
sunby 已提交
250
	var err error
251 252 253 254 255
	// cluster could be set by options
	// by-pass default NewCluster process if already set
	if s.cluster == nil {
		s.cluster, err = NewCluster(s.ctx, s.kvClient, NewNodesInfo(), s)
	}
S
sunby 已提交
256
	return err
257
}
G
groot 已提交
258

259 260 261
func (s *Server) initServiceDiscovery() error {
	sessions, rev, err := s.session.GetSessions(typeutil.DataNodeRole)
	if err != nil {
262
		log.Debug("dataCoord initMeta failed", zap.Error(err))
G
godchen 已提交
263 264
		return err
	}
265 266
	log.Debug("registered sessions", zap.Any("sessions", sessions))

S
sunby 已提交
267
	datanodes := make([]*NodeInfo, 0, len(sessions))
268
	for _, session := range sessions {
S
sunby 已提交
269
		info := &datapb.DataNodeInfo{
270 271 272
			Address:  session.Address,
			Version:  session.ServerID,
			Channels: []*datapb.ChannelStatus{},
S
sunby 已提交
273 274 275
		}
		nodeInfo := NewNodeInfo(s.ctx, info)
		datanodes = append(datanodes, nodeInfo)
276
	}
G
godchen 已提交
277

S
sunby 已提交
278
	s.cluster.Startup(datanodes)
279

280
	s.eventCh = s.session.WatchServices(typeutil.DataNodeRole, rev+1)
S
sunby 已提交
281 282 283
	return nil
}

284
func (s *Server) startSegmentManager() {
285
	s.segmentManager = newSegmentManager(s.meta, s.allocator)
286 287
}

S
sunby 已提交
288
func (s *Server) initMeta() error {
289
	connectEtcdFn := func() error {
X
XuanYang-cn 已提交
290
		etcdKV, err := etcdkv.NewEtcdKV(Params.EtcdEndpoints, Params.MetaRootPath)
291 292 293
		if err != nil {
			return err
		}
X
XuanYang-cn 已提交
294 295

		s.kvClient = etcdKV
G
godchen 已提交
296
		s.meta, err = newMeta(s.kvClient)
297 298 299 300
		if err != nil {
			return err
		}
		return nil
S
sunby 已提交
301
	}
G
godchen 已提交
302
	return retry.Do(s.ctx, connectEtcdFn, retry.Attempts(connEtcdMaxRetryTime))
S
sunby 已提交
303 304
}

305 306
func (s *Server) startServerLoop() {
	s.serverLoopCtx, s.serverLoopCancel = context.WithCancel(s.ctx)
307
	s.serverLoopWg.Add(4)
308
	go s.startStatsChannel(s.serverLoopCtx)
S
sunby 已提交
309
	go s.startDataNodeTtLoop(s.serverLoopCtx)
310
	go s.startWatchService(s.serverLoopCtx)
S
sunby 已提交
311
	go s.startFlushLoop(s.serverLoopCtx)
312
	go s.session.LivenessCheck(s.serverLoopCtx, s.liveCh, func() {
313 314
		if err := s.Stop(); err != nil {
			log.Error("failed to stop server", zap.Error(err))
G
godchen 已提交
315
		}
316
	})
317 318 319
}

func (s *Server) startStatsChannel(ctx context.Context) {
S
sunby 已提交
320
	defer logutil.LogPanic()
321
	defer s.serverLoopWg.Done()
G
groot 已提交
322
	statsStream, _ := s.msFactory.NewMsgStream(ctx)
323
	statsStream.AsConsumer([]string{Params.StatisticsChannelName}, Params.DataCoordSubscriptionName)
324
	log.Debug("dataCoord create stats channel consumer",
325
		zap.String("channelName", Params.StatisticsChannelName),
326
		zap.String("descriptionName", Params.DataCoordSubscriptionName))
327 328 329 330 331
	statsStream.Start()
	defer statsStream.Close()
	for {
		select {
		case <-ctx.Done():
S
sunby 已提交
332
			log.Debug("stats channel shutdown")
333 334 335
			return
		default:
		}
336
		msgPack := statsStream.Consume()
S
sunby 已提交
337
		if msgPack == nil {
S
sunby 已提交
338
			log.Debug("receive nil stats msg, shutdown stats channel")
S
sunby 已提交
339
			return
S
sunby 已提交
340
		}
341
		for _, msg := range msgPack.Msgs {
342
			if msg.Type() != commonpb.MsgType_SegmentStatistics {
S
sunby 已提交
343 344
				log.Warn("receive unknown msg from segment statistics channel",
					zap.Stringer("msgType", msg.Type()))
345
				continue
S
sunby 已提交
346
			}
347 348
			ssMsg := msg.(*msgstream.SegmentStatisticsMsg)
			for _, stat := range ssMsg.SegStats {
S
sunby 已提交
349
				s.meta.SetCurrentRows(stat.GetSegmentID(), stat.GetNumRows())
350
			}
351 352 353 354
		}
	}
}

S
sunby 已提交
355
func (s *Server) startDataNodeTtLoop(ctx context.Context) {
S
sunby 已提交
356
	defer logutil.LogPanic()
357
	defer s.serverLoopWg.Done()
S
sunby 已提交
358 359 360 361 362 363
	ttMsgStream, err := s.msFactory.NewMsgStream(ctx)
	if err != nil {
		log.Error("new msg stream failed", zap.Error(err))
		return
	}
	ttMsgStream.AsConsumer([]string{Params.TimeTickChannelName},
364
		Params.DataCoordSubscriptionName)
365 366 367
	log.Debug("dataCoord create time tick channel consumer",
		zap.String("timeTickChannelName", Params.TimeTickChannelName),
		zap.String("subscriptionName", Params.DataCoordSubscriptionName))
S
sunby 已提交
368 369
	ttMsgStream.Start()
	defer ttMsgStream.Close()
370 371 372 373 374

	var checker *LongTermChecker
	if enableTtChecker {
		checker = NewLongTermChecker(ctx, ttCheckerName, ttMaxInterval, ttCheckerWarnMsg)
		checker.Start()
375
		defer checker.Stop()
376
	}
377 378 379
	for {
		select {
		case <-ctx.Done():
S
sunby 已提交
380
			log.Debug("data node tt loop shutdown")
381 382 383
			return
		default:
		}
S
sunby 已提交
384
		msgPack := ttMsgStream.Consume()
S
sunby 已提交
385
		if msgPack == nil {
S
sunby 已提交
386
			log.Debug("receive nil tt msg, shutdown tt channel")
S
sunby 已提交
387
			return
S
sunby 已提交
388
		}
389
		for _, msg := range msgPack.Msgs {
S
sunby 已提交
390
			if msg.Type() != commonpb.MsgType_DataNodeTt {
391
				log.Warn("receive unexpected msg type from tt channel",
S
sunby 已提交
392
					zap.Stringer("msgType", msg.Type()))
393 394
				continue
			}
S
sunby 已提交
395
			ttMsg := msg.(*msgstream.DataNodeTtMsg)
396 397 398
			if enableTtChecker {
				checker.Check()
			}
S
sunby 已提交
399 400 401

			ch := ttMsg.ChannelName
			ts := ttMsg.Timestamp
402 403
			if err := s.segmentManager.ExpireAllocations(ch, ts); err != nil {
				log.Warn("failed to expire allocations", zap.Error(err))
G
godchen 已提交
404 405
				continue
			}
406
			segments, err := s.segmentManager.GetFlushableSegments(ctx, ch, ts)
407
			if err != nil {
S
sunby 已提交
408
				log.Warn("get flushable segments failed", zap.Error(err))
409 410
				continue
			}
411

S
sunby 已提交
412 413 414
			if len(segments) == 0 {
				continue
			}
415
			log.Debug("flush segments", zap.Int64s("segmentIDs", segments))
416
			segmentInfos := make([]*datapb.SegmentInfo, 0, len(segments))
S
sunby 已提交
417
			for _, id := range segments {
S
sunby 已提交
418 419
				sInfo := s.meta.GetSegment(id)
				if sInfo == nil {
S
sunby 已提交
420
					log.Error("get segment from meta error", zap.Int64("id", id),
421
						zap.Error(err))
S
sunby 已提交
422
					continue
423
				}
S
sunby 已提交
424
				segmentInfos = append(segmentInfos, sInfo.SegmentInfo)
S
sunby 已提交
425
				s.meta.SetLastFlushTime(id, time.Now())
426
			}
S
sunby 已提交
427
			if len(segmentInfos) > 0 {
S
sunby 已提交
428
				s.cluster.Flush(segmentInfos)
S
sunby 已提交
429
			}
430
		}
S
sunby 已提交
431
		s.helper.eventAfterHandleDataNodeTt()
432 433 434 435
	}
}

func (s *Server) startWatchService(ctx context.Context) {
S
sunby 已提交
436
	defer logutil.LogPanic()
437 438 439 440 441 442
	defer s.serverLoopWg.Done()
	for {
		select {
		case <-ctx.Done():
			log.Debug("watch service shutdown")
			return
S
sunby 已提交
443
		case event := <-s.eventCh:
444
			s.handleSessionEvent(ctx, event)
445 446 447
		}
	}
}
S
sunby 已提交
448

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
// handles session events - DataNodes Add/Del
func (s *Server) handleSessionEvent(ctx context.Context, event *sessionutil.SessionEvent) {
	if event == nil {
		return
	}
	info := &datapb.DataNodeInfo{
		Address:  event.Session.Address,
		Version:  event.Session.ServerID,
		Channels: []*datapb.ChannelStatus{},
	}
	node := NewNodeInfo(ctx, info)
	switch event.EventType {
	case sessionutil.SessionAddEvent:
		log.Info("received datanode register",
			zap.String("address", info.Address),
			zap.Int64("serverID", info.Version))
		s.cluster.Register(node)
		s.metricsCacheManager.InvalidateSystemInfoMetrics()
	case sessionutil.SessionDelEvent:
		log.Info("received datanode unregister",
			zap.String("address", info.Address),
			zap.Int64("serverID", info.Version))
		s.cluster.UnRegister(node)
		s.metricsCacheManager.InvalidateSystemInfoMetrics()
	default:
		log.Warn("receive unknown service event type",
			zap.Any("type", event.EventType))
	}
}

S
sunby 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491
func (s *Server) startFlushLoop(ctx context.Context) {
	defer logutil.LogPanic()
	defer s.serverLoopWg.Done()
	ctx2, cancel := context.WithCancel(ctx)
	defer cancel()
	// send `Flushing` segments
	go s.handleFlushingSegments(ctx2)
	for {
		select {
		case <-ctx.Done():
			log.Debug("flush loop shutdown")
			return
		case segmentID := <-s.flushCh:
492 493
			//Ignore return error
			_ = s.postFlush(ctx, segmentID)
S
sunby 已提交
494 495 496 497
		}
	}
}

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
// post function after flush is done
// 1. check segment id is valid
// 2. notify RootCoord segment is flushed
// 3. change segment state to `Flushed` in meta
func (s *Server) postFlush(ctx context.Context, segmentID UniqueID) error {
	segment := s.meta.GetSegment(segmentID)
	if segment == nil {
		log.Warn("failed to get flused segment", zap.Int64("id", segmentID))
		return errors.New("segment not found")
	}
	// Notify RootCoord segment is flushed
	req := &datapb.SegmentFlushCompletedMsg{
		Base: &commonpb.MsgBase{
			MsgType: commonpb.MsgType_SegmentFlushDone,
		},
		Segment: segment.SegmentInfo,
	}
	resp, err := s.rootCoordClient.SegmentFlushCompleted(ctx, req)
	if err = VerifyResponse(resp, err); err != nil {
		log.Warn("failed to call SegmentFlushComplete", zap.Int64("segmentID", segmentID), zap.Error(err))
		return err
	}
	// set segment to SegmentState_Flushed
	if err = s.meta.SetState(segmentID, commonpb.SegmentState_Flushed); err != nil {
		log.Error("flush segment complete failed", zap.Error(err))
		return err
	}
	log.Debug("flush segment complete", zap.Int64("id", segmentID))
	return nil
}

// recovery logic, fetch all Segment in `Flushing` state and do Flush notification logic
S
sunby 已提交
530 531 532 533 534 535 536 537 538 539 540
func (s *Server) handleFlushingSegments(ctx context.Context) {
	segments := s.meta.GetFlushingSegments()
	for _, segment := range segments {
		select {
		case <-ctx.Done():
			return
		case s.flushCh <- segment.ID:
		}
	}
}

541
func (s *Server) initRootCoordClient() error {
S
sunby 已提交
542
	var err error
G
godchen 已提交
543
	if s.rootCoordClient, err = s.rootCoordClientCreator(s.ctx, Params.MetaRootPath, Params.EtcdEndpoints); err != nil {
S
sunby 已提交
544 545
		return err
	}
546
	if err = s.rootCoordClient.Init(); err != nil {
S
sunby 已提交
547 548
		return err
	}
549
	return s.rootCoordClient.Start()
S
sunby 已提交
550
}
551

552 553 554 555
// Stop do the Server finalize processes
// it checks the server status is healthy, if not, just quit
// if Server is healthy, set server state to stopped, release etcd session,
//	stop message stream client and stop server loops
S
sunby 已提交
556
func (s *Server) Stop() error {
557
	if !atomic.CompareAndSwapInt64(&s.isServing, ServerStateHealthy, ServerStateStopped) {
S
sunby 已提交
558 559
		return nil
	}
560
	log.Debug("dataCoord server shutdown")
S
sunby 已提交
561
	s.cluster.Close()
S
sunby 已提交
562
	s.stopServerLoop()
S
sunby 已提交
563 564 565
	return nil
}

S
sunby 已提交
566 567
// CleanMeta only for test
func (s *Server) CleanMeta() error {
568
	log.Debug("clean meta", zap.Any("kv", s.kvClient))
569
	return s.kvClient.RemoveWithPrefix("")
S
sunby 已提交
570 571
}

S
sunby 已提交
572 573 574 575 576
func (s *Server) stopServerLoop() {
	s.serverLoopCancel()
	s.serverLoopWg.Wait()
}

577 578 579 580 581 582 583 584 585 586 587 588 589 590
//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)
//}
S
sunby 已提交
591

592 593
func (s *Server) loadCollectionFromRootCoord(ctx context.Context, collectionID int64) error {
	resp, err := s.rootCoordClient.DescribeCollection(ctx, &milvuspb.DescribeCollectionRequest{
S
sunby 已提交
594
		Base: &commonpb.MsgBase{
595
			MsgType:  commonpb.MsgType_DescribeCollection,
S
sunby 已提交
596 597 598 599 600 601 602 603
			SourceID: Params.NodeID,
		},
		DbName:       "",
		CollectionID: collectionID,
	})
	if err = VerifyResponse(resp, err); err != nil {
		return err
	}
604
	presp, err := s.rootCoordClient.ShowPartitions(ctx, &milvuspb.ShowPartitionsRequest{
S
sunby 已提交
605 606
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_ShowPartitions,
S
sunby 已提交
607 608
			MsgID:     0,
			Timestamp: 0,
S
sunby 已提交
609
			SourceID:  Params.NodeID,
610
		},
S
sunby 已提交
611 612 613 614 615
		DbName:         "",
		CollectionName: resp.Schema.Name,
		CollectionID:   resp.CollectionID,
	})
	if err = VerifyResponse(presp, err); err != nil {
616 617
		log.Error("show partitions error", zap.String("collectionName", resp.Schema.Name),
			zap.Int64("collectionID", resp.CollectionID), zap.Error(err))
618 619
		return err
	}
S
sunby 已提交
620
	collInfo := &datapb.CollectionInfo{
621 622 623 624
		ID:             resp.CollectionID,
		Schema:         resp.Schema,
		Partitions:     presp.PartitionIDs,
		StartPositions: resp.GetStartPositions(),
S
sunby 已提交
625
	}
S
sunby 已提交
626 627
	s.meta.AddCollection(collInfo)
	return nil
S
sunby 已提交
628 629
}

S
sunby 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
// GetVChanPositions get vchannel latest postitions with provided dml channel names
func (s *Server) GetVChanPositions(vchans []vchannel, seekFromStartPosition bool) ([]*datapb.VchannelInfo, error) {
	if s.kvClient == nil {
		return nil, errNilKvClient
	}
	pairs := make([]*datapb.VchannelInfo, 0, len(vchans))

	for _, vchan := range vchans {
		segments := s.meta.GetSegmentsByChannel(vchan.DmlChannel)
		flushedSegmentIDs := make([]UniqueID, 0)
		unflushed := make([]*datapb.SegmentInfo, 0)
		var seekPosition *internalpb.MsgPosition
		var useUnflushedPosition bool
		for _, s := range segments {
			if s.State == commonpb.SegmentState_Flushing || s.State == commonpb.SegmentState_Flushed {
				flushedSegmentIDs = append(flushedSegmentIDs, s.ID)
				if seekPosition == nil || (!useUnflushedPosition && s.DmlPosition.Timestamp > seekPosition.Timestamp) {
					seekPosition = s.DmlPosition
				}
				continue
			}
651

S
sunby 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665
			if s.DmlPosition == nil {
				continue
			}

			unflushed = append(unflushed, s.SegmentInfo)

			if seekPosition == nil || !useUnflushedPosition || s.DmlPosition.Timestamp < seekPosition.Timestamp {
				useUnflushedPosition = true
				if !seekFromStartPosition {
					seekPosition = s.DmlPosition
				} else {
					seekPosition = s.StartPosition
				}
			}
666 667
		}

S
sunby 已提交
668 669 670 671 672 673 674 675 676
		pairs = append(pairs, &datapb.VchannelInfo{
			CollectionID:      vchan.CollectionID,
			ChannelName:       vchan.DmlChannel,
			SeekPosition:      seekPosition,
			UnflushedSegments: unflushed,
			FlushedSegments:   flushedSegmentIDs,
		})
	}
	return pairs, nil
677
}