server.go 19.7 KB
Newer Older
Y
yah01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.

B
Bingyi Sun 已提交
17 18 19 20 21 22 23 24 25 26
package querycoordv2

import (
	"context"
	"fmt"
	"os"
	"sync"
	"syscall"
	"time"

27
	"github.com/cockroachdb/errors"
28 29 30 31
	clientv3 "go.etcd.io/etcd/client/v3"
	"go.uber.org/atomic"
	"go.uber.org/zap"
	"golang.org/x/sync/errgroup"
32

S
SimFG 已提交
33 34
	"github.com/milvus-io/milvus-proto/go-api/commonpb"
	"github.com/milvus-io/milvus-proto/go-api/milvuspb"
B
Bingyi Sun 已提交
35 36 37 38 39 40 41 42 43 44 45 46
	"github.com/milvus-io/milvus/internal/allocator"
	"github.com/milvus-io/milvus/internal/kv"
	etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
	"github.com/milvus-io/milvus/internal/querycoordv2/balance"
	"github.com/milvus-io/milvus/internal/querycoordv2/checkers"
	"github.com/milvus-io/milvus/internal/querycoordv2/dist"
	"github.com/milvus-io/milvus/internal/querycoordv2/job"
	"github.com/milvus-io/milvus/internal/querycoordv2/meta"
	"github.com/milvus-io/milvus/internal/querycoordv2/observers"
	"github.com/milvus-io/milvus/internal/querycoordv2/params"
	"github.com/milvus-io/milvus/internal/querycoordv2/session"
	"github.com/milvus-io/milvus/internal/querycoordv2/task"
47
	"github.com/milvus-io/milvus/internal/querycoordv2/utils"
B
Bingyi Sun 已提交
48 49 50
	"github.com/milvus-io/milvus/internal/types"
	"github.com/milvus-io/milvus/internal/util/sessionutil"
	"github.com/milvus-io/milvus/internal/util/tsoutil"
51 52 53 54 55 56 57
	"github.com/milvus-io/milvus/pkg/common"
	"github.com/milvus-io/milvus/pkg/log"
	"github.com/milvus-io/milvus/pkg/metrics"
	"github.com/milvus-io/milvus/pkg/util/metricsinfo"
	"github.com/milvus-io/milvus/pkg/util/paramtable"
	"github.com/milvus-io/milvus/pkg/util/timerecord"
	"github.com/milvus-io/milvus/pkg/util/typeutil"
B
Bingyi Sun 已提交
58 59 60 61
)

var (
	// Only for re-export
E
Enwei Jiao 已提交
62
	Params = params.Params
B
Bingyi Sun 已提交
63 64 65 66 67 68
)

type Server struct {
	ctx                 context.Context
	cancel              context.CancelFunc
	wg                  sync.WaitGroup
69
	status              atomic.Int32
B
Bingyi Sun 已提交
70
	etcdCli             *clientv3.Client
E
Enwei Jiao 已提交
71
	address             string
B
Bingyi Sun 已提交
72 73 74 75 76 77
	session             *sessionutil.Session
	kv                  kv.MetaKv
	idAllocator         func() (int64, error)
	metricsCacheManager *metricsinfo.MetricsCacheManager

	// Coordinators
C
cai.zhang 已提交
78 79
	dataCoord types.DataCoord
	rootCoord types.RootCoord
B
Bingyi Sun 已提交
80 81 82 83 84 85 86 87 88

	// Meta
	store     meta.Store
	meta      *meta.Meta
	dist      *meta.DistributionManager
	targetMgr *meta.TargetManager
	broker    meta.Broker

	// Session
W
wayblink 已提交
89 90 91
	cluster          session.Cluster
	nodeMgr          *session.NodeManager
	queryNodeCreator session.QueryNodeCreator
B
Bingyi Sun 已提交
92 93 94 95 96 97

	// Schedulers
	jobScheduler  *job.Scheduler
	taskScheduler task.Scheduler

	// HeartBeat
98
	distController dist.Controller
B
Bingyi Sun 已提交
99 100 101 102 103 104 105

	// Checkers
	checkerController *checkers.CheckerController

	// Observers
	collectionObserver *observers.CollectionObserver
	leaderObserver     *observers.LeaderObserver
W
wei liu 已提交
106
	targetObserver     *observers.TargetObserver
W
wei liu 已提交
107 108
	replicaObserver    *observers.ReplicaObserver
	resourceObserver   *observers.ResourceObserver
B
Bingyi Sun 已提交
109

110 111
	balancer    balance.Balance
	balancerMap map[string]balance.Balance
112 113 114

	// Active-standby
	enableActiveStandBy bool
115
	activateFunc        func() error
B
Bingyi Sun 已提交
116 117
}

118
func NewQueryCoord(ctx context.Context) (*Server, error) {
B
Bingyi Sun 已提交
119 120
	ctx, cancel := context.WithCancel(ctx)
	server := &Server{
121 122
		ctx:    ctx,
		cancel: cancel,
B
Bingyi Sun 已提交
123
	}
124
	server.UpdateStateCode(commonpb.StateCode_Abnormal)
W
wayblink 已提交
125
	server.queryNodeCreator = session.DefaultQueryNodeCreator
B
Bingyi Sun 已提交
126 127 128 129 130
	return server, nil
}

func (s *Server) Register() error {
	s.session.Register()
131
	if s.enableActiveStandBy {
132 133 134 135
		if err := s.session.ProcessActiveStandBy(s.activateFunc); err != nil {
			log.Error("failed to activate standby server", zap.Error(err))
			return err
		}
136
	}
B
Bingyi Sun 已提交
137
	go s.session.LivenessCheck(s.ctx, func() {
138
		log.Error("QueryCoord disconnected from etcd, process will exit", zap.Int64("serverID", paramtable.GetNodeID()))
B
Bingyi Sun 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151
		if err := s.Stop(); err != nil {
			log.Fatal("failed to stop server", zap.Error(err))
		}
		// manually send signal to starter goroutine
		if s.session.TriggerKill {
			if p, err := os.FindProcess(os.Getpid()); err == nil {
				p.Signal(syscall.SIGINT)
			}
		}
	})
	return nil
}

152
func (s *Server) initSession() error {
B
Bingyi Sun 已提交
153
	// Init QueryCoord session
154
	s.session = sessionutil.NewSession(s.ctx, Params.EtcdCfg.MetaRootPath.GetValue(), s.etcdCli)
B
Bingyi Sun 已提交
155 156 157
	if s.session == nil {
		return fmt.Errorf("failed to create session")
	}
E
Enwei Jiao 已提交
158
	s.session.Init(typeutil.QueryCoordRole, s.address, true, true)
159
	s.enableActiveStandBy = Params.QueryCoordCfg.EnableActiveStandby.GetAsBool()
160
	s.session.SetEnableActiveStandBy(s.enableActiveStandBy)
161 162 163 164 165 166 167 168 169 170 171 172 173
	return nil
}

func (s *Server) Init() error {
	log.Info("QueryCoord start init",
		zap.String("meta-root-path", Params.EtcdCfg.MetaRootPath.GetValue()),
		zap.String("address", s.address))

	if err := s.initSession(); err != nil {
		return err
	}

	if s.enableActiveStandBy {
174
		s.activateFunc = func() error {
175 176
			log.Info("QueryCoord switch from standby to active, activating")
			if err := s.initQueryCoord(); err != nil {
177 178
				log.Error("QueryCoord init failed", zap.Error(err))
				return err
179 180
			}
			if err := s.startQueryCoord(); err != nil {
181 182
				log.Error("QueryCoord init failed", zap.Error(err))
				return err
183 184
			}
			log.Info("QueryCoord startup success")
185
			return nil
186 187 188 189 190
		}
		s.UpdateStateCode(commonpb.StateCode_StandBy)
		log.Info("QueryCoord enter standby mode successfully")
		return nil
	}
B
Bingyi Sun 已提交
191

192 193 194 195
	return s.initQueryCoord()
}

func (s *Server) initQueryCoord() error {
196 197
	s.UpdateStateCode(commonpb.StateCode_Initializing)
	log.Info("QueryCoord", zap.Any("State", commonpb.StateCode_Initializing))
B
Bingyi Sun 已提交
198
	// Init KV
199
	etcdKV := etcdkv.NewEtcdKV(s.etcdCli, Params.EtcdCfg.MetaRootPath.GetValue())
B
Bingyi Sun 已提交
200
	s.kv = etcdKV
X
Xiaofan 已提交
201
	log.Info("query coordinator try to connect etcd success")
B
Bingyi Sun 已提交
202 203

	// Init ID allocator
204
	idAllocatorKV := tsoutil.NewTSOKVBase(s.etcdCli, Params.EtcdCfg.KvRootPath.GetValue(), "querycoord-id-allocator")
B
Bingyi Sun 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218
	idAllocator := allocator.NewGlobalIDAllocator("idTimestamp", idAllocatorKV)
	err := idAllocator.Initialize()
	if err != nil {
		log.Error("query coordinator id allocator initialize failed", zap.Error(err))
		return err
	}
	s.idAllocator = func() (int64, error) {
		return idAllocator.AllocOne()
	}

	// Init metrics cache manager
	s.metricsCacheManager = metricsinfo.NewMetricsCacheManager()

	// Init meta
W
wei liu 已提交
219
	s.nodeMgr = session.NewNodeManager()
B
Bingyi Sun 已提交
220 221 222 223 224
	err = s.initMeta()
	if err != nil {
		return err
	}
	// Init session
X
Xiaofan 已提交
225
	log.Info("init session")
W
wayblink 已提交
226
	s.cluster = session.NewCluster(s.nodeMgr, s.queryNodeCreator)
B
Bingyi Sun 已提交
227 228

	// Init schedulers
X
Xiaofan 已提交
229
	log.Info("init schedulers")
B
Bingyi Sun 已提交
230 231 232 233 234 235 236 237 238 239 240 241
	s.jobScheduler = job.NewScheduler()
	s.taskScheduler = task.NewScheduler(
		s.ctx,
		s.meta,
		s.dist,
		s.targetMgr,
		s.broker,
		s.cluster,
		s.nodeMgr,
	)

	// Init heartbeat
X
Xiaofan 已提交
242
	log.Info("init dist controller")
B
Bingyi Sun 已提交
243 244 245 246 247 248 249 250
	s.distController = dist.NewDistController(
		s.cluster,
		s.nodeMgr,
		s.dist,
		s.targetMgr,
		s.taskScheduler,
	)

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	// Init balancer map and balancer
	log.Info("init all available balancer")
	s.balancerMap = make(map[string]balance.Balance)
	s.balancerMap[balance.RoundRobinBalancerName] = balance.NewRoundRobinBalancer(s.taskScheduler, s.nodeMgr)
	s.balancerMap[balance.RowCountBasedBalancerName] = balance.NewRowCountBasedBalancer(s.taskScheduler,
		s.nodeMgr, s.dist, s.meta, s.targetMgr)
	s.balancerMap[balance.ScoreBasedBalancerName] = balance.NewScoreBasedBalancer(s.taskScheduler,
		s.nodeMgr, s.dist, s.meta, s.targetMgr)
	if balancer, ok := s.balancerMap[params.Params.QueryCoordCfg.Balancer.GetValue()]; ok {
		s.balancer = balancer
		log.Info("use config balancer", zap.String("balancer", params.Params.QueryCoordCfg.Balancer.GetValue()))
	} else {
		s.balancer = s.balancerMap[balance.RowCountBasedBalancerName]
		log.Info("use rowCountBased auto balancer")
	}
B
Bingyi Sun 已提交
266 267

	// Init checker controller
X
Xiaofan 已提交
268
	log.Info("init checker controller")
B
Bingyi Sun 已提交
269 270 271 272 273
	s.checkerController = checkers.NewCheckerController(
		s.meta,
		s.dist,
		s.targetMgr,
		s.balancer,
274
		s.nodeMgr,
B
Bingyi Sun 已提交
275 276 277 278 279 280
		s.taskScheduler,
	)

	// Init observers
	s.initObserver()

281 282 283
	// Init load status cache
	meta.GlobalFailedLoadCache = meta.NewFailedLoadCache()

B
Bingyi Sun 已提交
284 285 286 287 288
	log.Info("QueryCoord init success")
	return err
}

func (s *Server) initMeta() error {
289 290
	record := timerecord.NewTimeRecorder("querycoord")

X
Xiaofan 已提交
291
	log.Info("init meta")
B
Bingyi Sun 已提交
292
	s.store = meta.NewMetaStore(s.kv)
W
wei liu 已提交
293
	s.meta = meta.NewMeta(s.idAllocator, s.store, s.nodeMgr)
B
Bingyi Sun 已提交
294

295 296 297 298 299
	s.broker = meta.NewCoordinatorBroker(
		s.dataCoord,
		s.rootCoord,
	)

X
Xiaofan 已提交
300
	log.Info("recover meta...")
301
	err := s.meta.CollectionManager.Recover(s.broker)
B
Bingyi Sun 已提交
302 303 304 305
	if err != nil {
		log.Error("failed to recover collections")
		return err
	}
306 307 308
	collections := s.meta.GetAll()
	log.Info("recovering collections...", zap.Int64s("collections", collections))
	metrics.QueryCoordNumCollections.WithLabelValues().Set(float64(len(collections)))
309
	metrics.QueryCoordNumPartitions.WithLabelValues().Set(float64(len(s.meta.GetAllPartitions())))
Y
yah01 已提交
310

311
	err = s.meta.ReplicaManager.Recover(collections)
B
Bingyi Sun 已提交
312 313 314 315 316
	if err != nil {
		log.Error("failed to recover replicas")
		return err
	}

W
wei liu 已提交
317 318 319 320 321 322
	err = s.meta.ResourceManager.Recover()
	if err != nil {
		log.Error("failed to recover resource groups")
		return err
	}

B
Bingyi Sun 已提交
323 324 325 326 327
	s.dist = &meta.DistributionManager{
		SegmentDistManager: meta.NewSegmentDistManager(),
		ChannelDistManager: meta.NewChannelDistManager(),
		LeaderViewManager:  meta.NewLeaderViewManager(),
	}
W
wei liu 已提交
328
	s.targetMgr = meta.NewTargetManager(s.broker, s.meta)
329
	log.Info("QueryCoord server initMeta done", zap.Duration("duration", record.ElapseSpan()))
B
Bingyi Sun 已提交
330 331 332 333
	return nil
}

func (s *Server) initObserver() {
X
Xiaofan 已提交
334
	log.Info("init observers")
B
Bingyi Sun 已提交
335 336 337 338
	s.leaderObserver = observers.NewLeaderObserver(
		s.dist,
		s.meta,
		s.targetMgr,
Y
yah01 已提交
339
		s.broker,
B
Bingyi Sun 已提交
340 341
		s.cluster,
	)
W
wei liu 已提交
342
	s.targetObserver = observers.NewTargetObserver(
B
Bingyi Sun 已提交
343 344
		s.meta,
		s.targetMgr,
W
wei liu 已提交
345
		s.dist,
346
		s.broker,
B
Bingyi Sun 已提交
347
	)
348 349 350 351 352 353
	s.collectionObserver = observers.NewCollectionObserver(
		s.dist,
		s.meta,
		s.targetMgr,
		s.targetObserver,
	)
W
wei liu 已提交
354 355 356 357 358 359 360

	s.replicaObserver = observers.NewReplicaObserver(
		s.meta,
		s.dist,
	)

	s.resourceObserver = observers.NewResourceObserver(s.meta)
B
Bingyi Sun 已提交
361 362
}

J
Jiquan Long 已提交
363 364 365
func (s *Server) afterStart() {
}

B
Bingyi Sun 已提交
366
func (s *Server) Start() error {
367 368 369 370 371 372 373 374 375 376
	if !s.enableActiveStandBy {
		if err := s.startQueryCoord(); err != nil {
			return err
		}
		log.Info("QueryCoord started")
	}
	return nil
}

func (s *Server) startQueryCoord() error {
B
Bingyi Sun 已提交
377 378 379 380 381 382 383
	log.Info("start watcher...")
	sessions, revision, err := s.session.GetSessions(typeutil.QueryNodeRole)
	if err != nil {
		return err
	}
	for _, node := range sessions {
		s.nodeMgr.Add(session.NewNodeInfo(node.ServerID, node.Address))
384
		s.taskScheduler.AddExecutor(node.ServerID)
B
Bingyi Sun 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397
	}
	s.checkReplicas()
	for _, node := range sessions {
		s.handleNodeUp(node.ServerID)
	}
	s.wg.Add(1)
	go s.watchNodes(revision)

	log.Info("start recovering dist and target")
	err = s.recover()
	if err != nil {
		return err
	}
398
	s.startServerLoop()
399
	s.afterStart()
400
	s.UpdateStateCode(commonpb.StateCode_Healthy)
401 402 403 404
	return nil
}

func (s *Server) startServerLoop() {
B
Bingyi Sun 已提交
405 406 407 408 409 410
	log.Info("start cluster...")
	s.cluster.Start(s.ctx)

	log.Info("start job scheduler...")
	s.jobScheduler.Start(s.ctx)

411 412 413
	log.Info("start task scheduler...")
	s.taskScheduler.Start(s.ctx)

B
Bingyi Sun 已提交
414 415 416 417 418 419
	log.Info("start checker controller...")
	s.checkerController.Start(s.ctx)

	log.Info("start observers...")
	s.collectionObserver.Start(s.ctx)
	s.leaderObserver.Start(s.ctx)
W
wei liu 已提交
420
	s.targetObserver.Start(s.ctx)
W
wei liu 已提交
421 422
	s.replicaObserver.Start(s.ctx)
	s.resourceObserver.Start(s.ctx)
B
Bingyi Sun 已提交
423 424 425 426
}

func (s *Server) Stop() error {
	s.cancel()
B
Bingyi Sun 已提交
427 428 429
	if s.session != nil {
		s.session.Revoke(time.Second)
	}
B
Bingyi Sun 已提交
430

B
Bingyi Sun 已提交
431 432 433 434
	if s.session != nil {
		log.Info("stop cluster...")
		s.cluster.Stop()
	}
B
Bingyi Sun 已提交
435

B
Bingyi Sun 已提交
436 437 438 439
	if s.distController != nil {
		log.Info("stop dist controller...")
		s.distController.Stop()
	}
B
Bingyi Sun 已提交
440

B
Bingyi Sun 已提交
441 442 443 444
	if s.checkerController != nil {
		log.Info("stop checker controller...")
		s.checkerController.Stop()
	}
B
Bingyi Sun 已提交
445

B
Bingyi Sun 已提交
446 447 448 449
	if s.taskScheduler != nil {
		log.Info("stop task scheduler...")
		s.taskScheduler.Stop()
	}
450

B
Bingyi Sun 已提交
451 452 453 454
	if s.jobScheduler != nil {
		log.Info("stop job scheduler...")
		s.jobScheduler.Stop()
	}
B
Bingyi Sun 已提交
455 456

	log.Info("stop observers...")
B
Bingyi Sun 已提交
457 458 459 460 461 462
	if s.collectionObserver != nil {
		s.collectionObserver.Stop()
	}
	if s.leaderObserver != nil {
		s.leaderObserver.Stop()
	}
W
wei liu 已提交
463 464
	if s.targetObserver != nil {
		s.targetObserver.Stop()
B
Bingyi Sun 已提交
465
	}
W
wei liu 已提交
466 467 468 469 470 471
	if s.replicaObserver != nil {
		s.replicaObserver.Stop()
	}
	if s.resourceObserver != nil {
		s.resourceObserver.Stop()
	}
B
Bingyi Sun 已提交
472 473

	s.wg.Wait()
B
Bingyi Sun 已提交
474
	log.Info("QueryCoord stop successfully")
B
Bingyi Sun 已提交
475 476 477 478
	return nil
}

// UpdateStateCode updates the status of the coord, including healthy, unhealthy
479
func (s *Server) UpdateStateCode(code commonpb.StateCode) {
480 481 482 483 484
	s.status.Store(int32(code))
}

func (s *Server) State() commonpb.StateCode {
	return commonpb.StateCode(s.status.Load())
B
Bingyi Sun 已提交
485 486
}

487
func (s *Server) GetComponentStates(ctx context.Context) (*milvuspb.ComponentStates, error) {
B
Bingyi Sun 已提交
488 489 490 491
	nodeID := common.NotRegisteredID
	if s.session != nil && s.session.Registered() {
		nodeID = s.session.ServerID
	}
492
	serviceComponentInfo := &milvuspb.ComponentInfo{
B
Bingyi Sun 已提交
493 494
		// NodeID:    Params.QueryCoordID, // will race with QueryCoord.Register()
		NodeID:    nodeID,
495
		StateCode: s.State(),
B
Bingyi Sun 已提交
496 497
	}

498
	return &milvuspb.ComponentStates{
B
Bingyi Sun 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_Success,
		},
		State: serviceComponentInfo,
		//SubcomponentStates: subComponentInfos,
	}, nil
}

func (s *Server) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
	return &milvuspb.StringResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_Success,
			Reason:    "",
		},
	}, nil
}

func (s *Server) GetTimeTickChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
	return &milvuspb.StringResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_Success,
			Reason:    "",
		},
522
		Value: Params.CommonCfg.QueryCoordTimeTick.GetValue(),
B
Bingyi Sun 已提交
523 524 525
	}, nil
}

E
Enwei Jiao 已提交
526 527 528 529
func (s *Server) SetAddress(address string) {
	s.address = address
}

B
Bingyi Sun 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
// SetEtcdClient sets etcd's client
func (s *Server) SetEtcdClient(etcdClient *clientv3.Client) {
	s.etcdCli = etcdClient
}

// SetRootCoord sets root coordinator's client
func (s *Server) SetRootCoord(rootCoord types.RootCoord) error {
	if rootCoord == nil {
		return errors.New("null RootCoord interface")
	}

	s.rootCoord = rootCoord
	return nil
}

// SetDataCoord sets data coordinator's client
func (s *Server) SetDataCoord(dataCoord types.DataCoord) error {
	if dataCoord == nil {
		return errors.New("null DataCoord interface")
	}

	s.dataCoord = dataCoord
	return nil
}

W
wayblink 已提交
555 556 557 558
func (s *Server) SetQueryNodeCreator(f func(ctx context.Context, addr string) (types.QueryNode, error)) {
	s.queryNodeCreator = f
}

B
Bingyi Sun 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
func (s *Server) recover() error {
	// Recover target managers
	group, ctx := errgroup.WithContext(s.ctx)
	for _, collection := range s.meta.GetAll() {
		collection := collection
		group.Go(func() error {
			return s.recoverCollectionTargets(ctx, collection)
		})
	}
	err := group.Wait()
	if err != nil {
		return err
	}

	// Recover dist
	s.distController.SyncAll(s.ctx)

	return nil
}

func (s *Server) recoverCollectionTargets(ctx context.Context, collection int64) error {
W
wei liu 已提交
580
	err := s.targetMgr.UpdateCollectionNextTarget(collection)
B
Bingyi Sun 已提交
581
	if err != nil {
582 583 584 585 586 587
		s.meta.CollectionManager.RemoveCollection(collection)
		s.meta.ReplicaManager.RemoveCollection(collection)
		log.Error("failed to recover collection due to update next target failed",
			zap.Int64("collectionID", collection),
			zap.Error(err),
		)
B
Bingyi Sun 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	}
	return nil
}

func (s *Server) watchNodes(revision int64) {
	defer s.wg.Done()

	eventChan := s.session.WatchServices(typeutil.QueryNodeRole, revision+1, nil)
	for {
		select {
		case <-s.ctx.Done():
			log.Info("stop watching nodes, QueryCoord stopped")
			return

		case event, ok := <-eventChan:
			if !ok {
				// ErrCompacted is handled inside SessionWatcher
605
				log.Error("Session Watcher channel closed", zap.Int64("serverID", paramtable.GetNodeID()))
B
Bingyi Sun 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
				go s.Stop()
				if s.session.TriggerKill {
					if p, err := os.FindProcess(os.Getpid()); err == nil {
						p.Signal(syscall.SIGINT)
					}
				}
				return
			}

			switch event.EventType {
			case sessionutil.SessionAddEvent:
				nodeID := event.Session.ServerID
				addr := event.Session.Address
				log.Info("add node to NodeManager",
					zap.Int64("nodeID", nodeID),
					zap.String("nodeAddr", addr),
				)
				s.nodeMgr.Add(session.NewNodeInfo(nodeID, addr))
				s.handleNodeUp(nodeID)
				s.metricsCacheManager.InvalidateSystemInfoMetrics()
626
				s.checkerController.Check()
B
Bingyi Sun 已提交
627

628 629 630 631 632 633 634 635 636 637
			case sessionutil.SessionUpdateEvent:
				nodeID := event.Session.ServerID
				addr := event.Session.Address
				log.Info("stopping the node",
					zap.Int64("nodeID", nodeID),
					zap.String("nodeAddr", addr),
				)
				s.nodeMgr.Stopping(nodeID)
				s.checkerController.Check()

B
Bingyi Sun 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650
			case sessionutil.SessionDelEvent:
				nodeID := event.Session.ServerID
				log.Info("a node down, remove it", zap.Int64("nodeID", nodeID))
				s.nodeMgr.Remove(nodeID)
				s.handleNodeDown(nodeID)
				s.metricsCacheManager.InvalidateSystemInfoMetrics()
			}
		}
	}
}

func (s *Server) handleNodeUp(node int64) {
	log := log.With(zap.Int64("nodeID", node))
651
	s.taskScheduler.AddExecutor(node)
B
Bingyi Sun 已提交
652 653
	s.distController.StartDistInstance(s.ctx, node)

W
wei liu 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666
	// need assign to new rg and replica
	rgName, err := s.meta.ResourceManager.HandleNodeUp(node)
	if err != nil {
		log.Warn("HandleNodeUp: failed to assign node to resource group",
			zap.Error(err),
		)
		return
	}

	log.Info("HandleNodeUp: assign node to resource group",
		zap.String("resourceGroup", rgName),
	)

W
wei liu 已提交
667
	utils.AddNodesToCollectionsInRG(s.meta, meta.DefaultResourceGroupName, node)
B
Bingyi Sun 已提交
668 669 670 671
}

func (s *Server) handleNodeDown(node int64) {
	log := log.With(zap.Int64("nodeID", node))
672
	s.taskScheduler.RemoveExecutor(node)
B
Bingyi Sun 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
	s.distController.Remove(node)

	// Clear dist
	s.dist.LeaderViewManager.Update(node)
	s.dist.ChannelDistManager.Update(node)
	s.dist.SegmentDistManager.Update(node)

	// Clear meta
	for _, collection := range s.meta.CollectionManager.GetAll() {
		log := log.With(zap.Int64("collectionID", collection))
		replica := s.meta.ReplicaManager.GetByCollectionAndNode(collection, node)
		if replica == nil {
			continue
		}
		err := s.meta.ReplicaManager.RemoveNode(replica.GetID(), node)
		if err != nil {
			log.Warn("failed to remove node from collection's replicas",
				zap.Int64("replicaID", replica.GetID()),
				zap.Error(err),
			)
		}
		log.Info("remove node from replica",
			zap.Int64("replicaID", replica.GetID()))
	}

	// Clear tasks
	s.taskScheduler.RemoveByNode(node)
W
wei liu 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712

	rgName, err := s.meta.ResourceManager.HandleNodeDown(node)
	if err != nil {
		log.Warn("HandleNodeDown: failed to remove node from resource group",
			zap.String("resourceGroup", rgName),
			zap.Error(err),
		)
		return
	}

	log.Info("HandleNodeDown: remove node from resource group",
		zap.String("resourceGroup", rgName),
	)
B
Bingyi Sun 已提交
713 714 715 716 717 718 719 720 721 722
}

// checkReplicas checks whether replica contains offline node, and remove those nodes
func (s *Server) checkReplicas() {
	for _, collection := range s.meta.CollectionManager.GetAll() {
		log := log.With(zap.Int64("collectionID", collection))
		replicas := s.meta.ReplicaManager.GetByCollection(collection)
		for _, replica := range replicas {
			replica := replica.Clone()
			toRemove := make([]int64, 0)
W
wei liu 已提交
723
			for _, node := range replica.GetNodes() {
B
Bingyi Sun 已提交
724 725 726 727 728 729
				if s.nodeMgr.Get(node) == nil {
					toRemove = append(toRemove, node)
				}
			}

			if len(toRemove) > 0 {
Y
yah01 已提交
730 731 732 733
				log := log.With(
					zap.Int64("replicaID", replica.GetID()),
					zap.Int64s("offlineNodes", toRemove),
				)
X
Xiaofan 已提交
734
				log.Info("some nodes are offline, remove them from replica", zap.Any("toRemove", toRemove))
B
Bingyi Sun 已提交
735 736 737 738 739 740 741 742 743
				replica.RemoveNode(toRemove...)
				err := s.meta.ReplicaManager.Put(replica)
				if err != nil {
					log.Warn("failed to remove offline nodes from replica")
				}
			}
		}
	}
}