2022-10-11 11:39:22 +08:00
|
|
|
// 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.
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
package querycoordv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
2023-04-20 09:52:31 +08:00
|
|
|
"time"
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-11-07 14:02:20 +08:00
|
|
|
"github.com/blang/semver/v4"
|
2023-02-26 11:31:49 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
2023-11-07 14:02:20 +08:00
|
|
|
"github.com/samber/lo"
|
2023-09-07 07:25:14 +08:00
|
|
|
"github.com/tikv/client-go/v2/txnkv"
|
2023-04-06 19:14:32 +08:00
|
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
"go.uber.org/zap"
|
2022-11-08 20:13:03 +08:00
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
|
2022-09-15 18:48:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/allocator"
|
|
|
|
"github.com/milvus-io/milvus/internal/kv"
|
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
2023-09-07 07:25:14 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/kv/tikv"
|
2023-07-31 13:57:04 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore"
|
|
|
|
"github.com/milvus-io/milvus/internal/metastore/kv/querycoord"
|
2023-09-26 09:57:25 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2022-09-15 18:48:32 +08:00
|
|
|
"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"
|
2023-02-23 14:15:45 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/utils"
|
2022-09-15 18:48:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/sessionutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/tsoutil"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/common"
|
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/metrics"
|
2023-09-07 07:25:14 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util"
|
2023-12-22 18:36:44 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/expr"
|
2023-09-04 09:57:09 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
2023-04-06 19:14:32 +08:00
|
|
|
"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"
|
2022-09-15 18:48:32 +08:00
|
|
|
)
|
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
// Only for re-export
|
|
|
|
var Params = params.Params
|
2022-09-15 18:48:32 +08:00
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
wg sync.WaitGroup
|
2023-03-31 10:54:29 +08:00
|
|
|
status atomic.Int32
|
2022-09-15 18:48:32 +08:00
|
|
|
etcdCli *clientv3.Client
|
2023-09-07 07:25:14 +08:00
|
|
|
tikvCli *txnkv.Client
|
2022-11-04 14:25:38 +08:00
|
|
|
address string
|
2023-11-07 14:02:20 +08:00
|
|
|
session sessionutil.SessionInterface
|
2022-09-15 18:48:32 +08:00
|
|
|
kv kv.MetaKv
|
|
|
|
idAllocator func() (int64, error)
|
|
|
|
metricsCacheManager *metricsinfo.MetricsCacheManager
|
|
|
|
|
|
|
|
// Coordinators
|
2023-09-26 09:57:25 +08:00
|
|
|
dataCoord types.DataCoordClient
|
|
|
|
rootCoord types.RootCoordClient
|
2022-09-15 18:48:32 +08:00
|
|
|
|
|
|
|
// Meta
|
2023-07-31 13:57:04 +08:00
|
|
|
store metastore.QueryCoordCatalog
|
2022-09-15 18:48:32 +08:00
|
|
|
meta *meta.Meta
|
|
|
|
dist *meta.DistributionManager
|
|
|
|
targetMgr *meta.TargetManager
|
|
|
|
broker meta.Broker
|
|
|
|
|
|
|
|
// Session
|
2023-01-12 19:49:40 +08:00
|
|
|
cluster session.Cluster
|
|
|
|
nodeMgr *session.NodeManager
|
|
|
|
queryNodeCreator session.QueryNodeCreator
|
2022-09-15 18:48:32 +08:00
|
|
|
|
|
|
|
// Schedulers
|
|
|
|
jobScheduler *job.Scheduler
|
|
|
|
taskScheduler task.Scheduler
|
|
|
|
|
|
|
|
// HeartBeat
|
2023-02-23 14:15:45 +08:00
|
|
|
distController dist.Controller
|
2022-09-15 18:48:32 +08:00
|
|
|
|
|
|
|
// Checkers
|
|
|
|
checkerController *checkers.CheckerController
|
|
|
|
|
|
|
|
// Observers
|
|
|
|
collectionObserver *observers.CollectionObserver
|
2022-11-07 19:37:04 +08:00
|
|
|
targetObserver *observers.TargetObserver
|
2023-01-30 10:19:48 +08:00
|
|
|
replicaObserver *observers.ReplicaObserver
|
|
|
|
resourceObserver *observers.ResourceObserver
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-04-03 14:16:25 +08:00
|
|
|
balancer balance.Balance
|
|
|
|
balancerMap map[string]balance.Balance
|
2022-10-13 16:51:24 +08:00
|
|
|
|
|
|
|
// Active-standby
|
|
|
|
enableActiveStandBy bool
|
2023-02-09 15:24:31 +08:00
|
|
|
activateFunc func() error
|
2023-04-20 09:52:31 +08:00
|
|
|
|
|
|
|
nodeUpEventChan chan int64
|
2023-04-25 19:20:37 +08:00
|
|
|
notifyNodeUp chan struct{}
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2023-01-12 15:59:39 +08:00
|
|
|
func NewQueryCoord(ctx context.Context) (*Server, error) {
|
2022-09-15 18:48:32 +08:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
server := &Server{
|
2023-04-20 09:52:31 +08:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
nodeUpEventChan: make(chan int64, 10240),
|
2023-04-25 19:20:37 +08:00
|
|
|
notifyNodeUp: make(chan struct{}),
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
2022-10-10 15:55:22 +08:00
|
|
|
server.UpdateStateCode(commonpb.StateCode_Abnormal)
|
2023-01-12 19:49:40 +08:00
|
|
|
server.queryNodeCreator = session.DefaultQueryNodeCreator
|
2023-12-22 18:36:44 +08:00
|
|
|
expr.Register("querycoord", server)
|
2022-09-15 18:48:32 +08:00
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Register() error {
|
|
|
|
s.session.Register()
|
2022-10-13 16:51:24 +08:00
|
|
|
if s.enableActiveStandBy {
|
2023-02-09 15:24:31 +08:00
|
|
|
if err := s.session.ProcessActiveStandBy(s.activateFunc); err != nil {
|
|
|
|
log.Error("failed to activate standby server", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
2022-10-13 16:51:24 +08:00
|
|
|
}
|
2023-06-26 17:52:44 +08:00
|
|
|
metrics.NumNodes.WithLabelValues(fmt.Sprint(paramtable.GetNodeID()), typeutil.QueryCoordRole).Inc()
|
2023-04-12 20:12:28 +08:00
|
|
|
s.session.LivenessCheck(s.ctx, func() {
|
2023-11-07 14:02:20 +08:00
|
|
|
log.Error("QueryCoord disconnected from etcd, process will exit", zap.Int64("serverID", s.session.GetServerID()))
|
2024-02-29 17:01:50 +08:00
|
|
|
os.Exit(1)
|
2022-09-15 18:48:32 +08:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-30 11:11:50 +08:00
|
|
|
func (s *Server) initSession() error {
|
2022-09-15 18:48:32 +08:00
|
|
|
// Init QueryCoord session
|
2023-10-27 07:36:12 +08:00
|
|
|
s.session = sessionutil.NewSession(s.ctx)
|
2022-09-15 18:48:32 +08:00
|
|
|
if s.session == nil {
|
|
|
|
return fmt.Errorf("failed to create session")
|
|
|
|
}
|
2022-11-04 14:25:38 +08:00
|
|
|
s.session.Init(typeutil.QueryCoordRole, s.address, true, true)
|
2022-12-07 18:01:19 +08:00
|
|
|
s.enableActiveStandBy = Params.QueryCoordCfg.EnableActiveStandby.GetAsBool()
|
2022-10-13 16:51:24 +08:00
|
|
|
s.session.SetEnableActiveStandBy(s.enableActiveStandBy)
|
2023-01-30 11:11:50 +08:00
|
|
|
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 {
|
2023-02-09 15:24:31 +08:00
|
|
|
s.activateFunc = func() error {
|
2023-01-30 11:11:50 +08:00
|
|
|
log.Info("QueryCoord switch from standby to active, activating")
|
|
|
|
if err := s.initQueryCoord(); err != nil {
|
2023-02-09 15:24:31 +08:00
|
|
|
log.Error("QueryCoord init failed", zap.Error(err))
|
|
|
|
return err
|
2023-01-30 11:11:50 +08:00
|
|
|
}
|
|
|
|
if err := s.startQueryCoord(); err != nil {
|
2023-02-09 15:24:31 +08:00
|
|
|
log.Error("QueryCoord init failed", zap.Error(err))
|
|
|
|
return err
|
2023-01-30 11:11:50 +08:00
|
|
|
}
|
|
|
|
log.Info("QueryCoord startup success")
|
2023-02-09 15:24:31 +08:00
|
|
|
return nil
|
2023-01-30 11:11:50 +08:00
|
|
|
}
|
|
|
|
s.UpdateStateCode(commonpb.StateCode_StandBy)
|
|
|
|
log.Info("QueryCoord enter standby mode successfully")
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-01-30 11:11:50 +08:00
|
|
|
return s.initQueryCoord()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initQueryCoord() error {
|
2023-02-10 17:40:32 +08:00
|
|
|
s.UpdateStateCode(commonpb.StateCode_Initializing)
|
2023-11-27 16:30:26 +08:00
|
|
|
log.Info("start init querycoord", zap.Any("State", commonpb.StateCode_Initializing))
|
2023-09-07 07:25:14 +08:00
|
|
|
// Init KV and ID allocator
|
|
|
|
metaType := Params.MetaStoreCfg.MetaStoreType.GetValue()
|
|
|
|
var idAllocatorKV kv.TxnKV
|
|
|
|
log.Info(fmt.Sprintf("query coordinator connecting to %s.", metaType))
|
|
|
|
if metaType == util.MetaStoreTypeTiKV {
|
2023-11-23 19:34:23 +08:00
|
|
|
s.kv = tikv.NewTiKV(s.tikvCli, Params.TiKVCfg.MetaRootPath.GetValue(),
|
|
|
|
tikv.WithRequestTimeout(paramtable.Get().ServiceParam.TiKVCfg.RequestTimeout.GetAsDuration(time.Millisecond)))
|
2023-09-07 07:25:14 +08:00
|
|
|
idAllocatorKV = tsoutil.NewTSOTiKVBase(s.tikvCli, Params.TiKVCfg.KvRootPath.GetValue(), "querycoord-id-allocator")
|
|
|
|
} else if metaType == util.MetaStoreTypeEtcd {
|
2023-11-23 19:34:23 +08:00
|
|
|
s.kv = etcdkv.NewEtcdKV(s.etcdCli, Params.EtcdCfg.MetaRootPath.GetValue(),
|
|
|
|
etcdkv.WithRequestTimeout(paramtable.Get().ServiceParam.EtcdCfg.RequestTimeout.GetAsDuration(time.Millisecond)))
|
2023-09-07 07:25:14 +08:00
|
|
|
idAllocatorKV = tsoutil.NewTSOKVBase(s.etcdCli, Params.EtcdCfg.KvRootPath.GetValue(), "querycoord-id-allocator")
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("not supported meta store: %s", metaType)
|
|
|
|
}
|
|
|
|
log.Info(fmt.Sprintf("query coordinator successfully connected to %s.", metaType))
|
2022-09-15 18:48:32 +08:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2023-11-27 16:30:26 +08:00
|
|
|
log.Info("init ID allocator done")
|
2022-09-15 18:48:32 +08:00
|
|
|
|
|
|
|
// Init metrics cache manager
|
|
|
|
s.metricsCacheManager = metricsinfo.NewMetricsCacheManager()
|
|
|
|
|
|
|
|
// Init meta
|
2023-01-30 10:19:48 +08:00
|
|
|
s.nodeMgr = session.NewNodeManager()
|
2022-09-15 18:48:32 +08:00
|
|
|
err = s.initMeta()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Init session
|
2022-10-25 19:29:36 +08:00
|
|
|
log.Info("init session")
|
2023-01-12 19:49:40 +08:00
|
|
|
s.cluster = session.NewCluster(s.nodeMgr, s.queryNodeCreator)
|
2022-09-15 18:48:32 +08:00
|
|
|
|
|
|
|
// Init schedulers
|
2022-10-25 19:29:36 +08:00
|
|
|
log.Info("init schedulers")
|
2022-09-15 18:48:32 +08:00
|
|
|
s.jobScheduler = job.NewScheduler()
|
|
|
|
s.taskScheduler = task.NewScheduler(
|
|
|
|
s.ctx,
|
|
|
|
s.meta,
|
|
|
|
s.dist,
|
|
|
|
s.targetMgr,
|
|
|
|
s.broker,
|
|
|
|
s.cluster,
|
|
|
|
s.nodeMgr,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Init heartbeat
|
2022-10-25 19:29:36 +08:00
|
|
|
log.Info("init dist controller")
|
2022-09-15 18:48:32 +08:00
|
|
|
s.distController = dist.NewDistController(
|
|
|
|
s.cluster,
|
|
|
|
s.nodeMgr,
|
|
|
|
s.dist,
|
|
|
|
s.targetMgr,
|
|
|
|
s.taskScheduler,
|
|
|
|
)
|
|
|
|
|
2023-04-03 14:16:25 +08:00
|
|
|
// 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)
|
2024-02-19 15:24:50 +08:00
|
|
|
s.balancerMap[balance.MultiTargetBalancerName] = balance.NewMultiTargetBalancer(s.taskScheduler, s.nodeMgr, s.dist, s.meta, s.targetMgr)
|
|
|
|
|
2023-04-03 14:16:25 +08:00
|
|
|
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")
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
|
|
|
|
// Init checker controller
|
2022-10-25 19:29:36 +08:00
|
|
|
log.Info("init checker controller")
|
2022-09-15 18:48:32 +08:00
|
|
|
s.checkerController = checkers.NewCheckerController(
|
|
|
|
s.meta,
|
|
|
|
s.dist,
|
|
|
|
s.targetMgr,
|
|
|
|
s.balancer,
|
2023-04-03 14:16:25 +08:00
|
|
|
s.nodeMgr,
|
2022-09-15 18:48:32 +08:00
|
|
|
s.taskScheduler,
|
2023-07-19 21:22:58 +08:00
|
|
|
s.broker,
|
2022-09-15 18:48:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Init observers
|
|
|
|
s.initObserver()
|
|
|
|
|
2023-01-10 20:35:39 +08:00
|
|
|
// Init load status cache
|
|
|
|
meta.GlobalFailedLoadCache = meta.NewFailedLoadCache()
|
|
|
|
|
2023-11-27 16:30:26 +08:00
|
|
|
log.Info("init querycoord done", zap.Int64("nodeID", paramtable.GetNodeID()), zap.String("Address", s.address))
|
2022-09-15 18:48:32 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initMeta() error {
|
2022-11-08 20:13:03 +08:00
|
|
|
record := timerecord.NewTimeRecorder("querycoord")
|
|
|
|
|
2022-10-25 19:29:36 +08:00
|
|
|
log.Info("init meta")
|
2023-07-31 13:57:04 +08:00
|
|
|
s.store = querycoord.NewCatalog(s.kv)
|
2023-01-30 10:19:48 +08:00
|
|
|
s.meta = meta.NewMeta(s.idAllocator, s.store, s.nodeMgr)
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-03-20 14:55:57 +08:00
|
|
|
s.broker = meta.NewCoordinatorBroker(
|
|
|
|
s.dataCoord,
|
|
|
|
s.rootCoord,
|
|
|
|
)
|
|
|
|
|
2022-10-25 19:29:36 +08:00
|
|
|
log.Info("recover meta...")
|
2023-03-20 14:55:57 +08:00
|
|
|
err := s.meta.CollectionManager.Recover(s.broker)
|
2022-09-15 18:48:32 +08:00
|
|
|
if err != nil {
|
2023-09-06 17:43:14 +08:00
|
|
|
log.Warn("failed to recover collections", zap.Error(err))
|
2022-09-15 18:48:32 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-12-20 20:33:27 +08:00
|
|
|
collections := s.meta.GetAll()
|
|
|
|
log.Info("recovering collections...", zap.Int64s("collections", collections))
|
2023-05-15 16:33:22 +08:00
|
|
|
|
|
|
|
// We really update the metric after observers think the collection loaded.
|
|
|
|
metrics.QueryCoordNumCollections.WithLabelValues().Set(0)
|
|
|
|
|
2023-03-20 14:55:57 +08:00
|
|
|
metrics.QueryCoordNumPartitions.WithLabelValues().Set(float64(len(s.meta.GetAllPartitions())))
|
2022-11-10 15:01:04 +08:00
|
|
|
|
2022-12-20 20:33:27 +08:00
|
|
|
err = s.meta.ReplicaManager.Recover(collections)
|
2022-09-15 18:48:32 +08:00
|
|
|
if err != nil {
|
2023-09-06 17:43:14 +08:00
|
|
|
log.Warn("failed to recover replicas", zap.Error(err))
|
2022-09-15 18:48:32 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-30 10:19:48 +08:00
|
|
|
err = s.meta.ResourceManager.Recover()
|
|
|
|
if err != nil {
|
2023-09-06 17:43:14 +08:00
|
|
|
log.Warn("failed to recover resource groups", zap.Error(err))
|
2023-01-30 10:19:48 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
s.dist = &meta.DistributionManager{
|
|
|
|
SegmentDistManager: meta.NewSegmentDistManager(),
|
|
|
|
ChannelDistManager: meta.NewChannelDistManager(),
|
|
|
|
LeaderViewManager: meta.NewLeaderViewManager(),
|
|
|
|
}
|
2022-11-07 19:37:04 +08:00
|
|
|
s.targetMgr = meta.NewTargetManager(s.broker, s.meta)
|
2024-03-15 14:19:03 +08:00
|
|
|
err = s.targetMgr.Recover(s.store)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to recover collection targets", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:56:22 +08:00
|
|
|
log.Info("QueryCoord server initMeta done", zap.Duration("duration", record.ElapseSpan()))
|
2022-09-15 18:48:32 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initObserver() {
|
2022-10-25 19:29:36 +08:00
|
|
|
log.Info("init observers")
|
2022-11-07 19:37:04 +08:00
|
|
|
s.targetObserver = observers.NewTargetObserver(
|
2022-09-15 18:48:32 +08:00
|
|
|
s.meta,
|
|
|
|
s.targetMgr,
|
2022-11-07 19:37:04 +08:00
|
|
|
s.dist,
|
2022-11-07 17:05:06 +08:00
|
|
|
s.broker,
|
2023-11-08 11:36:22 +08:00
|
|
|
s.cluster,
|
2022-09-15 18:48:32 +08:00
|
|
|
)
|
2023-01-17 11:41:51 +08:00
|
|
|
s.collectionObserver = observers.NewCollectionObserver(
|
|
|
|
s.dist,
|
|
|
|
s.meta,
|
|
|
|
s.targetMgr,
|
|
|
|
s.targetObserver,
|
2023-05-08 14:06:41 +08:00
|
|
|
s.checkerController,
|
2023-01-17 11:41:51 +08:00
|
|
|
)
|
2023-01-30 10:19:48 +08:00
|
|
|
|
|
|
|
s.replicaObserver = observers.NewReplicaObserver(
|
|
|
|
s.meta,
|
|
|
|
s.dist,
|
|
|
|
)
|
|
|
|
|
|
|
|
s.resourceObserver = observers.NewResourceObserver(s.meta)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2023-12-27 14:30:53 +08:00
|
|
|
func (s *Server) afterStart() {}
|
2022-11-09 17:45:04 +08:00
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
func (s *Server) Start() error {
|
2023-01-30 11:11:50 +08:00
|
|
|
if !s.enableActiveStandBy {
|
|
|
|
if err := s.startQueryCoord(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info("QueryCoord started")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) startQueryCoord() error {
|
2022-09-15 18:48:32 +08:00
|
|
|
log.Info("start watcher...")
|
|
|
|
sessions, revision, err := s.session.GetSessions(typeutil.QueryNodeRole)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, node := range sessions {
|
2024-03-15 10:45:06 +08:00
|
|
|
s.nodeMgr.Add(session.NewNodeInfo(session.ImmutableNodeInfo{
|
|
|
|
NodeID: node.ServerID,
|
|
|
|
Address: node.Address,
|
|
|
|
Hostname: node.HostName,
|
|
|
|
Version: node.Version,
|
|
|
|
}))
|
2022-11-30 13:57:15 +08:00
|
|
|
s.taskScheduler.AddExecutor(node.ServerID)
|
2023-10-24 12:20:11 +08:00
|
|
|
|
|
|
|
if node.Stopping {
|
|
|
|
s.nodeMgr.Stopping(node.ServerID)
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
s.checkReplicas()
|
|
|
|
for _, node := range sessions {
|
|
|
|
s.handleNodeUp(node.ServerID)
|
|
|
|
}
|
2023-04-20 09:52:31 +08:00
|
|
|
|
|
|
|
s.wg.Add(2)
|
|
|
|
go s.handleNodeUpLoop()
|
2022-09-15 18:48:32 +08:00
|
|
|
go s.watchNodes(revision)
|
|
|
|
|
2023-12-27 14:30:53 +08:00
|
|
|
// check whether old node exist, if yes suspend auto balance until all old nodes down
|
|
|
|
s.updateBalanceConfigLoop(s.ctx)
|
|
|
|
|
2023-09-01 11:17:01 +08:00
|
|
|
// Recover dist, to avoid generate too much task when dist not ready after restart
|
|
|
|
s.distController.SyncAll(s.ctx)
|
|
|
|
|
2023-01-30 11:11:50 +08:00
|
|
|
s.startServerLoop()
|
2022-12-28 10:49:29 +08:00
|
|
|
s.afterStart()
|
2023-02-10 17:40:32 +08:00
|
|
|
s.UpdateStateCode(commonpb.StateCode_Healthy)
|
2023-11-07 14:02:20 +08:00
|
|
|
sessionutil.SaveServerInfo(typeutil.QueryCoordRole, s.session.GetServerID())
|
2022-12-28 10:49:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) startServerLoop() {
|
2023-09-27 16:27:27 +08:00
|
|
|
// start the components from inside to outside,
|
|
|
|
// to make the dependencies ready for every component
|
2022-09-15 18:48:32 +08:00
|
|
|
log.Info("start cluster...")
|
2023-09-27 16:27:27 +08:00
|
|
|
s.cluster.Start()
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-09-27 16:27:27 +08:00
|
|
|
log.Info("start observers...")
|
|
|
|
s.collectionObserver.Start()
|
|
|
|
s.targetObserver.Start()
|
|
|
|
s.replicaObserver.Start()
|
|
|
|
s.resourceObserver.Start()
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2022-09-21 14:42:51 +08:00
|
|
|
log.Info("start task scheduler...")
|
2023-09-27 16:27:27 +08:00
|
|
|
s.taskScheduler.Start()
|
2022-09-21 14:42:51 +08:00
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
log.Info("start checker controller...")
|
2023-09-27 16:27:27 +08:00
|
|
|
s.checkerController.Start()
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-09-27 16:27:27 +08:00
|
|
|
log.Info("start job scheduler...")
|
|
|
|
s.jobScheduler.Start()
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
2024-03-15 14:19:03 +08:00
|
|
|
// save target to meta store, after querycoord restart, make it fast to recover current target
|
|
|
|
if s.targetMgr != nil {
|
|
|
|
s.targetMgr.SaveCurrentTarget(s.store)
|
|
|
|
}
|
|
|
|
|
2023-09-27 16:27:27 +08:00
|
|
|
// FOLLOW the dependence graph:
|
|
|
|
// job scheduler -> checker controller -> task scheduler -> dist controller -> cluster -> session
|
|
|
|
// observers -> dist controller
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-09-27 16:27:27 +08:00
|
|
|
if s.jobScheduler != nil {
|
|
|
|
log.Info("stop job scheduler...")
|
|
|
|
s.jobScheduler.Stop()
|
2022-10-31 12:05:34 +08:00
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2022-10-31 12:05:34 +08:00
|
|
|
if s.checkerController != nil {
|
|
|
|
log.Info("stop checker controller...")
|
|
|
|
s.checkerController.Stop()
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2022-10-31 12:05:34 +08:00
|
|
|
if s.taskScheduler != nil {
|
|
|
|
log.Info("stop task scheduler...")
|
|
|
|
s.taskScheduler.Stop()
|
|
|
|
}
|
2022-09-21 14:42:51 +08:00
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
log.Info("stop observers...")
|
2022-10-31 12:05:34 +08:00
|
|
|
if s.collectionObserver != nil {
|
|
|
|
s.collectionObserver.Stop()
|
|
|
|
}
|
2022-11-07 19:37:04 +08:00
|
|
|
if s.targetObserver != nil {
|
|
|
|
s.targetObserver.Stop()
|
2022-10-31 12:05:34 +08:00
|
|
|
}
|
2023-01-30 10:19:48 +08:00
|
|
|
if s.replicaObserver != nil {
|
|
|
|
s.replicaObserver.Stop()
|
|
|
|
}
|
|
|
|
if s.resourceObserver != nil {
|
|
|
|
s.resourceObserver.Stop()
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-09-27 16:27:27 +08:00
|
|
|
if s.distController != nil {
|
|
|
|
log.Info("stop dist controller...")
|
|
|
|
s.distController.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.cluster != nil {
|
|
|
|
log.Info("stop cluster...")
|
|
|
|
s.cluster.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.session != nil {
|
|
|
|
s.session.Stop()
|
|
|
|
}
|
|
|
|
|
2024-03-15 10:33:05 +08:00
|
|
|
s.cancel()
|
2022-09-15 18:48:32 +08:00
|
|
|
s.wg.Wait()
|
2022-10-31 12:05:34 +08:00
|
|
|
log.Info("QueryCoord stop successfully")
|
2022-09-15 18:48:32 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateStateCode updates the status of the coord, including healthy, unhealthy
|
2022-10-10 15:55:22 +08:00
|
|
|
func (s *Server) UpdateStateCode(code commonpb.StateCode) {
|
2023-03-31 10:54:29 +08:00
|
|
|
s.status.Store(int32(code))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) State() commonpb.StateCode {
|
|
|
|
return commonpb.StateCode(s.status.Load())
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, req *milvuspb.GetComponentStatesRequest) (*milvuspb.ComponentStates, error) {
|
2023-11-22 09:12:22 +08:00
|
|
|
log.Debug("QueryCoord current state", zap.String("StateCode", s.State().String()))
|
2022-09-15 18:48:32 +08:00
|
|
|
nodeID := common.NotRegisteredID
|
|
|
|
if s.session != nil && s.session.Registered() {
|
2023-11-07 14:02:20 +08:00
|
|
|
nodeID = s.session.GetServerID()
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
2022-10-10 15:55:22 +08:00
|
|
|
serviceComponentInfo := &milvuspb.ComponentInfo{
|
2022-09-15 18:48:32 +08:00
|
|
|
// NodeID: Params.QueryCoordID, // will race with QueryCoord.Register()
|
|
|
|
NodeID: nodeID,
|
2023-03-31 10:54:29 +08:00
|
|
|
StateCode: s.State(),
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2022-10-10 15:55:22 +08:00
|
|
|
return &milvuspb.ComponentStates{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2023-09-04 09:57:09 +08:00
|
|
|
State: serviceComponentInfo,
|
2023-09-21 09:45:27 +08:00
|
|
|
// SubcomponentStates: subComponentInfos,
|
2022-09-15 18:48:32 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
func (s *Server) GetStatisticsChannel(ctx context.Context, req *internalpb.GetStatisticsChannelRequest) (*milvuspb.StringResponse, error) {
|
2022-09-15 18:48:32 +08:00
|
|
|
return &milvuspb.StringResponse{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2022-09-15 18:48:32 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
func (s *Server) GetTimeTickChannel(ctx context.Context, req *internalpb.GetTimeTickChannelRequest) (*milvuspb.StringResponse, error) {
|
2022-09-15 18:48:32 +08:00
|
|
|
return &milvuspb.StringResponse{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2023-09-04 09:57:09 +08:00
|
|
|
Value: Params.CommonCfg.QueryCoordTimeTick.GetValue(),
|
2022-09-15 18:48:32 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-04 14:25:38 +08:00
|
|
|
func (s *Server) SetAddress(address string) {
|
|
|
|
s.address = address
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
// SetEtcdClient sets etcd's client
|
|
|
|
func (s *Server) SetEtcdClient(etcdClient *clientv3.Client) {
|
|
|
|
s.etcdCli = etcdClient
|
|
|
|
}
|
|
|
|
|
2023-09-07 07:25:14 +08:00
|
|
|
func (s *Server) SetTiKVClient(client *txnkv.Client) {
|
|
|
|
s.tikvCli = client
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
// SetRootCoord sets root coordinator's client
|
2023-09-26 09:57:25 +08:00
|
|
|
func (s *Server) SetRootCoordClient(rootCoord types.RootCoordClient) error {
|
2022-09-15 18:48:32 +08:00
|
|
|
if rootCoord == nil {
|
|
|
|
return errors.New("null RootCoord interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.rootCoord = rootCoord
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDataCoord sets data coordinator's client
|
2023-09-26 09:57:25 +08:00
|
|
|
func (s *Server) SetDataCoordClient(dataCoord types.DataCoordClient) error {
|
2022-09-15 18:48:32 +08:00
|
|
|
if dataCoord == nil {
|
|
|
|
return errors.New("null DataCoord interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.dataCoord = dataCoord
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
func (s *Server) SetQueryNodeCreator(f func(ctx context.Context, addr string, nodeID int64) (types.QueryNodeClient, error)) {
|
2023-01-12 19:49:40 +08:00
|
|
|
s.queryNodeCreator = f
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
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
|
2023-08-14 18:57:32 +08:00
|
|
|
log.Warn("Session Watcher channel closed", zap.Int64("serverID", paramtable.GetNodeID()))
|
2022-09-15 18:48:32 +08:00
|
|
|
go s.Stop()
|
2023-11-07 14:02:20 +08:00
|
|
|
if s.session.IsTriggerKill() {
|
2022-09-15 18:48:32 +08:00
|
|
|
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),
|
|
|
|
)
|
2024-03-15 10:45:06 +08:00
|
|
|
s.nodeMgr.Add(session.NewNodeInfo(session.ImmutableNodeInfo{
|
|
|
|
NodeID: nodeID,
|
|
|
|
Address: addr,
|
|
|
|
Hostname: event.Session.HostName,
|
|
|
|
Version: event.Session.Version,
|
|
|
|
}))
|
2023-04-20 09:52:31 +08:00
|
|
|
s.nodeUpEventChan <- nodeID
|
2023-05-02 14:06:37 +08:00
|
|
|
select {
|
|
|
|
case s.notifyNodeUp <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2022-12-06 22:59:19 +08:00
|
|
|
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()
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 09:52:31 +08:00
|
|
|
func (s *Server) handleNodeUpLoop() {
|
|
|
|
defer s.wg.Done()
|
|
|
|
ticker := time.NewTicker(Params.QueryCoordCfg.CheckHealthInterval.GetAsDuration(time.Millisecond))
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.ctx.Done():
|
2023-04-25 19:20:37 +08:00
|
|
|
log.Info("handle node up loop exit due to context done")
|
2023-04-20 09:52:31 +08:00
|
|
|
return
|
2023-04-25 19:20:37 +08:00
|
|
|
case <-s.notifyNodeUp:
|
|
|
|
s.tryHandleNodeUp()
|
2023-04-20 09:52:31 +08:00
|
|
|
case <-ticker.C:
|
2023-04-25 19:20:37 +08:00
|
|
|
s.tryHandleNodeUp()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) tryHandleNodeUp() {
|
|
|
|
log := log.Ctx(s.ctx).WithRateGroup("qcv2.Server", 1, 60)
|
|
|
|
ctx, cancel := context.WithTimeout(s.ctx, Params.QueryCoordCfg.CheckHealthRPCTimeout.GetAsDuration(time.Millisecond))
|
|
|
|
defer cancel()
|
|
|
|
reasons, err := s.checkNodeHealth(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.RatedWarn(10, "unhealthy node exist, node up will be delayed",
|
|
|
|
zap.Int("delayedNodeUpEvents", len(s.nodeUpEventChan)),
|
|
|
|
zap.Int("unhealthyNodeNum", len(reasons)),
|
|
|
|
zap.Strings("unhealthyReason", reasons))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for len(s.nodeUpEventChan) > 0 {
|
|
|
|
nodeID := <-s.nodeUpEventChan
|
|
|
|
if s.nodeMgr.Get(nodeID) != nil {
|
|
|
|
// only if all nodes are healthy, node up event will be handled
|
|
|
|
s.handleNodeUp(nodeID)
|
|
|
|
s.metricsCacheManager.InvalidateSystemInfoMetrics()
|
|
|
|
s.checkerController.Check()
|
|
|
|
} else {
|
|
|
|
log.Warn("node already down",
|
|
|
|
zap.Int64("nodeID", nodeID))
|
2023-04-20 09:52:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
func (s *Server) handleNodeUp(node int64) {
|
|
|
|
log := log.With(zap.Int64("nodeID", node))
|
2022-11-30 13:57:15 +08:00
|
|
|
s.taskScheduler.AddExecutor(node)
|
2022-09-15 18:48:32 +08:00
|
|
|
s.distController.StartDistInstance(s.ctx, node)
|
|
|
|
|
2023-01-30 10:19:48 +08:00
|
|
|
// 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),
|
|
|
|
)
|
|
|
|
|
2023-03-01 11:11:47 +08:00
|
|
|
utils.AddNodesToCollectionsInRG(s.meta, meta.DefaultResourceGroupName, node)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleNodeDown(node int64) {
|
|
|
|
log := log.With(zap.Int64("nodeID", node))
|
2022-11-30 13:57:15 +08:00
|
|
|
s.taskScheduler.RemoveExecutor(node)
|
2022-09-15 18:48:32 +08:00
|
|
|
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)
|
2023-01-30 10:19:48 +08:00
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2023-01-30 10:19:48 +08:00
|
|
|
for _, node := range replica.GetNodes() {
|
2022-09-15 18:48:32 +08:00
|
|
|
if s.nodeMgr.Get(node) == nil {
|
|
|
|
toRemove = append(toRemove, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(toRemove) > 0 {
|
2022-09-29 18:02:54 +08:00
|
|
|
log := log.With(
|
|
|
|
zap.Int64("replicaID", replica.GetID()),
|
|
|
|
zap.Int64s("offlineNodes", toRemove),
|
|
|
|
)
|
2022-10-25 19:29:36 +08:00
|
|
|
log.Info("some nodes are offline, remove them from replica", zap.Any("toRemove", toRemove))
|
2022-09-15 18:48:32 +08:00
|
|
|
replica.RemoveNode(toRemove...)
|
|
|
|
err := s.meta.ReplicaManager.Put(replica)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to remove offline nodes from replica")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-07 14:02:20 +08:00
|
|
|
|
|
|
|
func (s *Server) updateBalanceConfigLoop(ctx context.Context) {
|
2023-11-09 10:00:22 +08:00
|
|
|
success := s.updateBalanceConfig()
|
|
|
|
if success {
|
|
|
|
return
|
|
|
|
}
|
2023-11-07 14:02:20 +08:00
|
|
|
|
2023-11-09 10:00:22 +08:00
|
|
|
s.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer s.wg.Done()
|
|
|
|
ticker := time.NewTicker(Params.QueryCoordCfg.CheckAutoBalanceConfigInterval.GetAsDuration(time.Second))
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Info("update balance config loop exit!")
|
2023-11-07 14:02:20 +08:00
|
|
|
return
|
|
|
|
|
2023-11-09 10:00:22 +08:00
|
|
|
case <-ticker.C:
|
|
|
|
success := s.updateBalanceConfig()
|
|
|
|
if success {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-11-07 14:02:20 +08:00
|
|
|
}
|
2023-11-09 10:00:22 +08:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) updateBalanceConfig() bool {
|
|
|
|
log := log.Ctx(s.ctx).WithRateGroup("qcv2.updateBalanceConfigLoop", 1, 60)
|
|
|
|
r := semver.MustParseRange("<2.3.0")
|
|
|
|
sessions, _, err := s.session.GetSessionsWithVersionRange(typeutil.QueryNodeRole, r)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("check query node version occur error on etcd", zap.Error(err))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sessions) == 0 {
|
|
|
|
// only balance channel when all query node's version >= 2.3.0
|
2023-12-27 14:30:53 +08:00
|
|
|
Params.Reset(Params.QueryCoordCfg.AutoBalance.Key)
|
2023-11-09 10:00:22 +08:00
|
|
|
log.Info("all old query node down, enable auto balance!")
|
|
|
|
return true
|
2023-11-07 14:02:20 +08:00
|
|
|
}
|
2023-11-09 10:00:22 +08:00
|
|
|
|
2023-12-27 14:30:53 +08:00
|
|
|
Params.Save(Params.QueryCoordCfg.AutoBalance.Key, "false")
|
2023-11-09 10:00:22 +08:00
|
|
|
log.RatedDebug(10, "old query node exist", zap.Strings("sessions", lo.Keys(sessions)))
|
|
|
|
return false
|
2023-11-07 14:02:20 +08:00
|
|
|
}
|