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"
|
2024-09-25 10:13:18 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-09-15 18:48:32 +08:00
|
|
|
"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"
|
2024-10-23 11:29:28 +08:00
|
|
|
"github.com/tidwall/gjson"
|
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"
|
|
|
|
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"
|
2024-09-25 10:13:18 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
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"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2024-05-08 10:05:29 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/proxyutil"
|
2022-09-15 18:48:32 +08:00
|
|
|
"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"
|
2024-09-25 10:13:18 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/config"
|
2024-06-25 21:18:15 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/kv"
|
2023-04-06 19:14:32 +08:00
|
|
|
"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
|
2024-08-02 17:20:15 +08:00
|
|
|
targetMgr meta.TargetManagerInterface
|
2022-09-15 18:48:32 +08:00
|
|
|
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
|
2024-05-08 10:05:29 +08:00
|
|
|
collectionObserver *observers.CollectionObserver
|
|
|
|
targetObserver *observers.TargetObserver
|
|
|
|
replicaObserver *observers.ReplicaObserver
|
|
|
|
resourceObserver *observers.ResourceObserver
|
|
|
|
leaderCacheObserver *observers.LeaderCacheObserver
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2024-05-21 14:29:39 +08:00
|
|
|
getBalancerFunc checkers.GetBalancerFunc
|
|
|
|
balancerMap map[string]balance.Balance
|
|
|
|
balancerLock sync.RWMutex
|
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{}
|
2024-05-08 10:05:29 +08:00
|
|
|
|
|
|
|
// proxy client manager
|
|
|
|
proxyCreator proxyutil.ProxyCreator
|
|
|
|
proxyWatcher proxyutil.ProxyWatcherInterface
|
|
|
|
proxyClientManager proxyutil.ProxyClientManagerInterface
|
2024-10-28 10:13:29 +08:00
|
|
|
|
|
|
|
metricsRequest *metricsinfo.MetricsRequest
|
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{}),
|
2024-05-21 14:29:39 +08:00
|
|
|
balancerMap: make(map[string]balance.Balance),
|
2024-10-28 10:13:29 +08:00
|
|
|
metricsRequest: metricsinfo.NewMetricsRequest(),
|
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()
|
2024-04-10 18:49:18 +08:00
|
|
|
afterRegister := func() {
|
|
|
|
metrics.NumNodes.WithLabelValues(fmt.Sprint(paramtable.GetNodeID()), typeutil.QueryCoordRole).Inc()
|
|
|
|
s.session.LivenessCheck(s.ctx, func() {
|
|
|
|
log.Error("QueryCoord disconnected from etcd, process will exit", zap.Int64("serverID", s.session.GetServerID()))
|
|
|
|
os.Exit(1)
|
|
|
|
})
|
|
|
|
}
|
2022-10-13 16:51:24 +08:00
|
|
|
if s.enableActiveStandBy {
|
2024-04-10 18:49:18 +08:00
|
|
|
go func() {
|
|
|
|
if err := s.session.ProcessActiveStandBy(s.activateFunc); err != nil {
|
|
|
|
log.Error("failed to activate standby server", zap.Error(err))
|
2024-06-19 11:16:00 +08:00
|
|
|
panic(err)
|
2024-04-10 18:49:18 +08:00
|
|
|
}
|
|
|
|
afterRegister()
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
afterRegister()
|
2022-10-13 16:51:24 +08:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-23 11:29:28 +08:00
|
|
|
func (s *Server) registerMetricsRequest() {
|
|
|
|
getSystemInfoAction := func(ctx context.Context, req *milvuspb.GetMetricsRequest, jsonReq gjson.Result) (string, error) {
|
|
|
|
return s.getSystemInfoMetrics(ctx, req)
|
|
|
|
}
|
2024-10-28 10:13:29 +08:00
|
|
|
|
|
|
|
QueryTasksAction := func(ctx context.Context, req *milvuspb.GetMetricsRequest, jsonReq gjson.Result) (string, error) {
|
|
|
|
return s.taskScheduler.GetTasksJSON(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.metricsRequest.RegisterMetricsRequest(metricsinfo.SystemInfoMetrics, getSystemInfoAction)
|
|
|
|
s.metricsRequest.RegisterMetricsRequest(metricsinfo.QueryCoordAllTasks, QueryTasksAction)
|
2024-10-23 11:29:28 +08:00
|
|
|
log.Info("register metrics actions finished")
|
|
|
|
}
|
|
|
|
|
2023-01-30 11:11:50 +08:00
|
|
|
func (s *Server) Init() error {
|
|
|
|
log.Info("QueryCoord start init",
|
|
|
|
zap.String("meta-root-path", Params.EtcdCfg.MetaRootPath.GetValue()),
|
|
|
|
zap.String("address", s.address))
|
|
|
|
|
2024-10-23 11:29:28 +08:00
|
|
|
s.registerMetricsRequest()
|
|
|
|
|
2023-01-30 11:11:50 +08:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2024-05-08 10:05:29 +08:00
|
|
|
// init proxy client manager
|
|
|
|
s.proxyClientManager = proxyutil.NewProxyClientManager(proxyutil.DefaultProxyCreator)
|
|
|
|
s.proxyWatcher = proxyutil.NewProxyWatcher(
|
|
|
|
s.etcdCli,
|
|
|
|
s.proxyClientManager.AddProxyClients,
|
|
|
|
)
|
|
|
|
s.proxyWatcher.AddSessionFunc(s.proxyClientManager.AddProxyClient)
|
|
|
|
s.proxyWatcher.DelSessionFunc(s.proxyClientManager.DelProxyClient)
|
|
|
|
log.Info("init proxy manager done")
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
// 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,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Init checker controller
|
2022-10-25 19:29:36 +08:00
|
|
|
log.Info("init checker controller")
|
2024-05-21 14:29:39 +08:00
|
|
|
s.getBalancerFunc = func() balance.Balance {
|
|
|
|
balanceKey := paramtable.Get().QueryCoordCfg.Balancer.GetValue()
|
|
|
|
s.balancerLock.Lock()
|
|
|
|
defer s.balancerLock.Unlock()
|
|
|
|
|
|
|
|
balancer, ok := s.balancerMap[balanceKey]
|
|
|
|
if ok {
|
|
|
|
return balancer
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("switch to new balancer", zap.String("name", balanceKey))
|
|
|
|
switch balanceKey {
|
|
|
|
case meta.RoundRobinBalancerName:
|
|
|
|
balancer = balance.NewRoundRobinBalancer(s.taskScheduler, s.nodeMgr)
|
|
|
|
case meta.RowCountBasedBalancerName:
|
|
|
|
balancer = balance.NewRowCountBasedBalancer(s.taskScheduler, s.nodeMgr, s.dist, s.meta, s.targetMgr)
|
|
|
|
case meta.ScoreBasedBalancerName:
|
|
|
|
balancer = balance.NewScoreBasedBalancer(s.taskScheduler, s.nodeMgr, s.dist, s.meta, s.targetMgr)
|
|
|
|
case meta.MultiTargetBalancerName:
|
|
|
|
balancer = balance.NewMultiTargetBalancer(s.taskScheduler, s.nodeMgr, s.dist, s.meta, s.targetMgr)
|
|
|
|
case meta.ChannelLevelScoreBalancerName:
|
|
|
|
balancer = balance.NewChannelLevelScoreBalancer(s.taskScheduler, s.nodeMgr, s.dist, s.meta, s.targetMgr)
|
|
|
|
default:
|
|
|
|
log.Info(fmt.Sprintf("default to use %s", meta.ScoreBasedBalancerName))
|
|
|
|
balancer = balance.NewScoreBasedBalancer(s.taskScheduler, s.nodeMgr, s.dist, s.meta, s.targetMgr)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.balancerMap[balanceKey] = balancer
|
|
|
|
return balancer
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
s.checkerController = checkers.NewCheckerController(
|
|
|
|
s.meta,
|
|
|
|
s.dist,
|
|
|
|
s.targetMgr,
|
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,
|
2024-05-21 14:29:39 +08:00
|
|
|
s.getBalancerFunc,
|
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,
|
2024-08-20 16:49:02 +08:00
|
|
|
s.proxyClientManager,
|
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)
|
2024-05-08 10:05:29 +08:00
|
|
|
|
|
|
|
s.leaderCacheObserver = observers.NewLeaderCacheObserver(
|
|
|
|
s.proxyClientManager,
|
|
|
|
)
|
|
|
|
s.dist.LeaderViewManager.SetNotifyFunc(s.leaderCacheObserver.RegisterEvent)
|
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
|
|
|
}
|
2024-05-22 10:03:40 +08:00
|
|
|
s.checkNodeStateInRG()
|
2022-09-15 18:48:32 +08:00
|
|
|
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)
|
|
|
|
|
2024-09-25 10:13:18 +08:00
|
|
|
// watch load config changes
|
|
|
|
s.watchLoadConfigChanges()
|
|
|
|
|
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)
|
|
|
|
|
2024-05-08 10:05:29 +08:00
|
|
|
if err := s.proxyWatcher.WatchProxy(s.ctx); err != nil {
|
|
|
|
log.Warn("querycoord failed to watch proxy", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
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() {
|
2024-05-14 13:25:32 +08:00
|
|
|
// leader cache observer shall be started before `SyncAll` call
|
|
|
|
s.leaderCacheObserver.Start(s.ctx)
|
|
|
|
// Recover dist, to avoid generate too much task when dist not ready after restart
|
|
|
|
s.distController.SyncAll(s.ctx)
|
|
|
|
|
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 {
|
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
|
|
|
}
|
2024-03-18 12:27:04 +08:00
|
|
|
|
|
|
|
// save target to meta store, after querycoord restart, make it fast to recover current target
|
|
|
|
// should save target after target observer stop, incase of target changed
|
|
|
|
if s.targetMgr != nil {
|
|
|
|
s.targetMgr.SaveCurrentTarget(s.store)
|
|
|
|
}
|
|
|
|
|
2023-01-30 10:19:48 +08:00
|
|
|
if s.replicaObserver != nil {
|
|
|
|
s.replicaObserver.Stop()
|
|
|
|
}
|
|
|
|
if s.resourceObserver != nil {
|
|
|
|
s.resourceObserver.Stop()
|
|
|
|
}
|
2024-05-08 10:05:29 +08:00
|
|
|
if s.leaderCacheObserver != nil {
|
|
|
|
s.leaderCacheObserver.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()
|
2024-05-20 10:21:38 +08:00
|
|
|
s.meta.ResourceManager.HandleNodeStopping(nodeID)
|
2022-12-06 22:59:19 +08:00
|
|
|
|
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) {
|
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
|
2024-04-15 08:13:19 +08:00
|
|
|
s.meta.ResourceManager.HandleNodeUp(node)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleNodeDown(node int64) {
|
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 tasks
|
|
|
|
s.taskScheduler.RemoveByNode(node)
|
2023-01-30 10:19:48 +08:00
|
|
|
|
2024-04-15 08:13:19 +08:00
|
|
|
s.meta.ResourceManager.HandleNodeDown(node)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2024-05-22 10:03:40 +08:00
|
|
|
func (s *Server) checkNodeStateInRG() {
|
|
|
|
for _, rgName := range s.meta.ListResourceGroups() {
|
|
|
|
rg := s.meta.ResourceManager.GetResourceGroup(rgName)
|
|
|
|
for _, node := range rg.GetNodes() {
|
|
|
|
info := s.nodeMgr.Get(node)
|
|
|
|
if info == nil {
|
|
|
|
s.meta.ResourceManager.HandleNodeDown(node)
|
|
|
|
} else if info.IsStoppingState() {
|
|
|
|
s.meta.ResourceManager.HandleNodeStopping(node)
|
2024-05-21 15:41:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2024-09-25 10:13:18 +08:00
|
|
|
|
|
|
|
func (s *Server) watchLoadConfigChanges() {
|
|
|
|
replicaNumHandler := config.NewHandler("watchReplicaNumberChanges", func(e *config.Event) {
|
|
|
|
log.Info("watch load config changes", zap.String("key", e.Key), zap.String("value", e.Value), zap.String("type", e.EventType))
|
|
|
|
|
|
|
|
collectionIDs := s.meta.GetAll()
|
|
|
|
if len(collectionIDs) == 0 {
|
|
|
|
log.Warn("no collection loaded, skip to trigger update load config")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
replicaNum, err := strconv.ParseInt(e.Value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("invalid cluster level load config, skip it", zap.String("key", e.Key), zap.String("value", e.Value))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if replicaNum <= 0 {
|
|
|
|
log.Info("invalid cluster level load config, skip it", zap.Int64("replica_num", replicaNum))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rgs := paramtable.Get().QueryCoordCfg.ClusterLevelLoadResourceGroups.GetAsStrings()
|
|
|
|
|
|
|
|
s.UpdateLoadConfig(s.ctx, &querypb.UpdateLoadConfigRequest{
|
|
|
|
CollectionIDs: collectionIDs,
|
|
|
|
ReplicaNumber: int32(replicaNum),
|
|
|
|
ResourceGroups: rgs,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
paramtable.Get().Watch(paramtable.Get().QueryCoordCfg.ClusterLevelLoadReplicaNumber.Key, replicaNumHandler)
|
|
|
|
|
|
|
|
rgHandler := config.NewHandler("watchResourceGroupChanges", func(e *config.Event) {
|
|
|
|
log.Info("watch load config changes", zap.String("key", e.Key), zap.String("value", e.Value), zap.String("type", e.EventType))
|
|
|
|
collectionIDs := s.meta.GetAll()
|
|
|
|
if len(collectionIDs) == 0 {
|
|
|
|
log.Warn("no collection loaded, skip to trigger update load config")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(e.Value) == 0 {
|
|
|
|
log.Warn("invalid cluster level load config, skip it", zap.String("key", e.Key), zap.String("value", e.Value))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rgs := strings.Split(e.Value, ",")
|
|
|
|
rgs = lo.Map(rgs, func(rg string, _ int) string { return strings.TrimSpace(rg) })
|
|
|
|
if len(rgs) == 0 {
|
|
|
|
log.Info("invalid cluster level load config, skip it", zap.Strings("resource_groups", rgs))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
replicaNum := paramtable.Get().QueryCoordCfg.ClusterLevelLoadReplicaNumber.GetAsInt64()
|
|
|
|
s.UpdateLoadConfig(s.ctx, &querypb.UpdateLoadConfigRequest{
|
|
|
|
CollectionIDs: collectionIDs,
|
|
|
|
ReplicaNumber: int32(replicaNum),
|
|
|
|
ResourceGroups: rgs,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
paramtable.Get().Watch(paramtable.Get().QueryCoordCfg.ClusterLevelLoadResourceGroups.Key, rgHandler)
|
|
|
|
}
|