2021-10-20 19:02:41 +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
|
2021-04-19 13:50:12 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-20 19:02:41 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 13:50:12 +08:00
|
|
|
//
|
2021-10-20 19:02:41 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-04-19 13:50:12 +08:00
|
|
|
|
2021-01-15 14:38:36 +08:00
|
|
|
package grpcindexnode
|
|
|
|
|
|
|
|
import (
|
2021-01-19 18:32:57 +08:00
|
|
|
"context"
|
2021-03-23 01:49:50 +08:00
|
|
|
"fmt"
|
2021-01-19 18:32:57 +08:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
2021-11-29 14:25:17 +08:00
|
|
|
"time"
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2022-08-25 15:48:54 +08:00
|
|
|
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
2022-01-04 10:53:18 +08:00
|
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
2023-01-12 16:09:39 +08:00
|
|
|
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
2023-08-20 21:24:19 +08:00
|
|
|
"go.uber.org/atomic"
|
2022-01-04 10:53:18 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/keepalive"
|
|
|
|
|
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"
|
2023-09-26 10:15:25 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/distributed/utils"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/indexnode"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/indexpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2021-12-17 10:23:15 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2023-09-21 09:45:27 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/dependency"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
2023-09-21 09:45:27 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/tracer"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/etcd"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/funcutil"
|
2023-06-21 11:36:46 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/interceptor"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/logutil"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2021-01-15 14:38:36 +08:00
|
|
|
)
|
|
|
|
|
2021-09-24 20:41:56 +08:00
|
|
|
// Server is the grpc wrapper of IndexNode.
|
2021-01-15 14:38:36 +08:00
|
|
|
type Server struct {
|
2021-12-29 14:35:21 +08:00
|
|
|
indexnode types.IndexNodeComponent
|
2021-01-15 14:38:36 +08:00
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
grpcServer *grpc.Server
|
|
|
|
grpcErrChan chan error
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2023-08-20 21:24:19 +08:00
|
|
|
serverID atomic.Int64
|
|
|
|
|
2021-08-13 18:12:08 +08:00
|
|
|
loopCtx context.Context
|
|
|
|
loopCancel func()
|
|
|
|
loopWg sync.WaitGroup
|
2021-03-23 01:49:50 +08:00
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
etcdCli *clientv3.Client
|
2021-01-26 19:24:09 +08:00
|
|
|
}
|
|
|
|
|
2021-09-24 20:41:56 +08:00
|
|
|
// Run initializes and starts IndexNode's grpc service.
|
2021-01-29 17:08:31 +08:00
|
|
|
func (s *Server) Run() error {
|
|
|
|
if err := s.init(); err != nil {
|
2021-09-09 10:06:29 +08:00
|
|
|
return err
|
2021-01-26 19:24:09 +08:00
|
|
|
}
|
2021-10-27 14:53:15 +08:00
|
|
|
log.Debug("IndexNode init done ...")
|
2021-01-29 17:08:31 +08:00
|
|
|
if err := s.start(); err != nil {
|
2021-01-19 18:32:57 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-10-27 14:53:15 +08:00
|
|
|
log.Debug("IndexNode start done ...")
|
2021-01-29 17:08:31 +08:00
|
|
|
return nil
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
|
|
|
|
2021-11-18 10:45:12 +08:00
|
|
|
// startGrpcLoop starts the grep loop of IndexNode component.
|
2021-01-29 17:08:31 +08:00
|
|
|
func (s *Server) startGrpcLoop(grpcPort int) {
|
2021-01-20 18:26:20 +08:00
|
|
|
defer s.loopWg.Done()
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2022-12-16 15:59:23 +08:00
|
|
|
Params := ¶mtable.Get().IndexNodeGrpcServerCfg
|
2021-12-17 10:23:15 +08:00
|
|
|
log.Debug("IndexNode", zap.String("network address", Params.GetAddress()), zap.Int("network port: ", grpcPort))
|
2021-01-29 17:08:31 +08:00
|
|
|
lis, err := net.Listen("tcp", ":"+strconv.Itoa(grpcPort))
|
2021-01-19 18:32:57 +08:00
|
|
|
if err != nil {
|
2023-09-15 10:09:21 +08:00
|
|
|
log.Warn("IndexNode", zap.Error(err))
|
2021-01-29 17:08:31 +08:00
|
|
|
s.grpcErrChan <- err
|
|
|
|
return
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
ctx, cancel := context.WithCancel(s.loopCtx)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
kaep := keepalive.EnforcementPolicy{
|
2021-11-29 14:25:17 +08:00
|
|
|
MinTime: 5 * time.Second, // If a client pings more than once every 5 seconds, terminate the connection
|
|
|
|
PermitWithoutStream: true, // Allow pings even when there are no active streams
|
|
|
|
}
|
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
kasp := keepalive.ServerParameters{
|
2021-11-29 14:25:17 +08:00
|
|
|
Time: 60 * time.Second, // Ping the client if it is idle for 60 seconds to ensure the connection is still active
|
|
|
|
Timeout: 10 * time.Second, // Wait 10 second for the ping ack before assuming the connection is dead
|
|
|
|
}
|
|
|
|
|
2023-01-12 16:09:39 +08:00
|
|
|
opts := tracer.GetInterceptorOpts()
|
2021-03-08 15:49:42 +08:00
|
|
|
s.grpcServer = grpc.NewServer(
|
2021-11-29 14:25:17 +08:00
|
|
|
grpc.KeepaliveEnforcementPolicy(kaep),
|
|
|
|
grpc.KeepaliveParams(kasp),
|
2022-12-16 15:59:23 +08:00
|
|
|
grpc.MaxRecvMsgSize(Params.ServerMaxRecvSize.GetAsInt()),
|
|
|
|
grpc.MaxSendMsgSize(Params.ServerMaxSendSize.GetAsInt()),
|
2022-08-23 10:44:52 +08:00
|
|
|
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
|
2023-01-12 16:09:39 +08:00
|
|
|
otelgrpc.UnaryServerInterceptor(opts...),
|
2023-06-21 11:36:46 +08:00
|
|
|
logutil.UnaryTraceLoggerInterceptor,
|
|
|
|
interceptor.ClusterValidationUnaryServerInterceptor(),
|
2023-08-20 21:24:19 +08:00
|
|
|
interceptor.ServerIDValidationUnaryServerInterceptor(func() int64 {
|
|
|
|
if s.serverID.Load() == 0 {
|
|
|
|
s.serverID.Store(paramtable.GetNodeID())
|
|
|
|
}
|
|
|
|
return s.serverID.Load()
|
|
|
|
}),
|
2023-06-21 11:36:46 +08:00
|
|
|
)),
|
2022-08-23 10:44:52 +08:00
|
|
|
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
|
2023-01-12 16:09:39 +08:00
|
|
|
otelgrpc.StreamServerInterceptor(opts...),
|
2023-06-21 11:36:46 +08:00
|
|
|
logutil.StreamTraceLoggerInterceptor,
|
|
|
|
interceptor.ClusterValidationStreamServerInterceptor(),
|
2023-08-20 21:24:19 +08:00
|
|
|
interceptor.ServerIDValidationStreamServerInterceptor(func() int64 {
|
|
|
|
if s.serverID.Load() == 0 {
|
|
|
|
s.serverID.Store(paramtable.GetNodeID())
|
|
|
|
}
|
|
|
|
return s.serverID.Load()
|
|
|
|
}),
|
2023-06-21 11:36:46 +08:00
|
|
|
)))
|
2021-01-19 18:32:57 +08:00
|
|
|
indexpb.RegisterIndexNodeServer(s.grpcServer, s)
|
2021-01-29 17:08:31 +08:00
|
|
|
go funcutil.CheckGrpcReady(ctx, s.grpcErrChan)
|
|
|
|
if err := s.grpcServer.Serve(lis); err != nil {
|
|
|
|
s.grpcErrChan <- err
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 20:41:56 +08:00
|
|
|
// init initializes IndexNode's grpc service.
|
2021-01-29 17:08:31 +08:00
|
|
|
func (s *Server) init() error {
|
2022-12-16 15:59:23 +08:00
|
|
|
etcdConfig := ¶mtable.Get().EtcdCfg
|
|
|
|
Params := ¶mtable.Get().IndexNodeGrpcServerCfg
|
2021-01-29 17:08:31 +08:00
|
|
|
var err error
|
2022-12-16 15:59:23 +08:00
|
|
|
if !funcutil.CheckPortAvailable(Params.Port.GetAsInt()) {
|
|
|
|
paramtable.Get().Save(Params.Port.Key, fmt.Sprintf("%d", funcutil.GetAvailablePort()))
|
|
|
|
log.Warn("IndexNode get available port when init", zap.Int("Port", Params.Port.GetAsInt()))
|
2022-01-14 21:17:34 +08:00
|
|
|
}
|
2021-03-23 01:49:50 +08:00
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = s.Stop()
|
|
|
|
if err != nil {
|
2021-08-13 18:12:08 +08:00
|
|
|
log.Error("IndexNode Init failed, and Stop failed")
|
2021-01-29 17:08:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
s.loopWg.Add(1)
|
2022-12-16 15:59:23 +08:00
|
|
|
go s.startGrpcLoop(Params.Port.GetAsInt())
|
2021-01-29 17:08:31 +08:00
|
|
|
// wait for grpc server loop start
|
|
|
|
err = <-s.grpcErrChan
|
|
|
|
if err != nil {
|
2021-08-13 18:12:08 +08:00
|
|
|
log.Error("IndexNode", zap.Any("grpc error", err))
|
2021-01-29 17:08:31 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-01-20 18:26:20 +08:00
|
|
|
|
2022-11-30 18:23:15 +08:00
|
|
|
etcdCli, err := etcd.GetEtcdClient(
|
2022-12-16 15:59:23 +08:00
|
|
|
etcdConfig.UseEmbedEtcd.GetAsBool(),
|
|
|
|
etcdConfig.EtcdUseSSL.GetAsBool(),
|
|
|
|
etcdConfig.Endpoints.GetAsStrings(),
|
|
|
|
etcdConfig.EtcdTLSCert.GetValue(),
|
|
|
|
etcdConfig.EtcdTLSKey.GetValue(),
|
|
|
|
etcdConfig.EtcdTLSCACert.GetValue(),
|
|
|
|
etcdConfig.EtcdTLSMinVersion.GetValue())
|
2021-12-29 14:35:21 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("IndexNode connect to etcd failed", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.etcdCli = etcdCli
|
|
|
|
s.indexnode.SetEtcdClient(etcdCli)
|
2022-12-16 15:59:23 +08:00
|
|
|
s.indexnode.SetAddress(Params.GetAddress())
|
2021-03-08 10:09:48 +08:00
|
|
|
err = s.indexnode.Init()
|
2021-01-20 18:26:20 +08:00
|
|
|
if err != nil {
|
2021-08-13 18:12:08 +08:00
|
|
|
log.Error("IndexNode Init failed", zap.Error(err))
|
2021-01-29 17:08:31 +08:00
|
|
|
return err
|
2021-01-20 16:46:58 +08:00
|
|
|
}
|
2021-12-15 11:47:10 +08:00
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-20 16:46:58 +08:00
|
|
|
|
2021-09-24 20:41:56 +08:00
|
|
|
// start starts IndexNode's grpc service.
|
2021-01-29 17:08:31 +08:00
|
|
|
func (s *Server) start() error {
|
2021-03-08 10:09:48 +08:00
|
|
|
err := s.indexnode.Start()
|
2021-01-20 18:26:20 +08:00
|
|
|
if err != nil {
|
2021-01-26 09:38:40 +08:00
|
|
|
return err
|
2021-01-20 18:26:20 +08:00
|
|
|
}
|
2021-12-15 11:47:10 +08:00
|
|
|
err = s.indexnode.Register()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("IndexNode Register etcd failed", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("IndexNode Register etcd success")
|
2021-01-26 09:38:40 +08:00
|
|
|
return nil
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
|
|
|
|
2021-09-24 20:41:56 +08:00
|
|
|
// Stop stops IndexNode's grpc service.
|
2021-01-26 09:38:40 +08:00
|
|
|
func (s *Server) Stop() error {
|
2022-12-16 15:59:23 +08:00
|
|
|
Params := ¶mtable.Get().IndexNodeGrpcServerCfg
|
2021-12-17 10:23:15 +08:00
|
|
|
log.Debug("IndexNode stop", zap.String("Address", Params.GetAddress()))
|
2021-03-08 10:09:48 +08:00
|
|
|
if s.indexnode != nil {
|
|
|
|
s.indexnode.Stop()
|
2021-01-29 17:08:31 +08:00
|
|
|
}
|
2022-12-16 10:03:27 +08:00
|
|
|
s.loopCancel()
|
2021-12-29 14:35:21 +08:00
|
|
|
if s.etcdCli != nil {
|
|
|
|
defer s.etcdCli.Close()
|
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
if s.grpcServer != nil {
|
2023-09-26 10:15:25 +08:00
|
|
|
utils.GracefulStopGRPCServer(s.grpcServer)
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
2021-01-20 18:26:20 +08:00
|
|
|
s.loopWg.Wait()
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
return nil
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
// setServer sets the IndexNode's instance.
|
|
|
|
func (s *Server) setServer(indexNode types.IndexNodeComponent) error {
|
|
|
|
s.indexnode = indexNode
|
2021-09-06 17:54:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
// SetEtcdClient sets the etcd client for QueryNode component.
|
|
|
|
func (s *Server) SetEtcdClient(etcdCli *clientv3.Client) {
|
|
|
|
s.indexnode.SetEtcdClient(etcdCli)
|
|
|
|
}
|
|
|
|
|
2021-09-24 20:41:56 +08:00
|
|
|
// GetComponentStates gets the component states of IndexNode.
|
2022-10-10 15:55:22 +08:00
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, req *milvuspb.GetComponentStatesRequest) (*milvuspb.ComponentStates, error) {
|
2023-09-26 09:57:25 +08:00
|
|
|
return s.indexnode.GetComponentStates(ctx, req)
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
2021-01-26 14:31:31 +08:00
|
|
|
|
2021-09-24 20:41:56 +08:00
|
|
|
// GetStatisticsChannel gets the statistics channel of IndexNode.
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetStatisticsChannel(ctx context.Context, req *internalpb.GetStatisticsChannelRequest) (*milvuspb.StringResponse, error) {
|
2023-09-26 09:57:25 +08:00
|
|
|
return s.indexnode.GetStatisticsChannel(ctx, req)
|
2021-01-29 17:08:31 +08:00
|
|
|
}
|
|
|
|
|
2022-08-25 15:48:54 +08:00
|
|
|
// CreateJob sends the create index request to IndexNode.
|
|
|
|
func (s *Server) CreateJob(ctx context.Context, req *indexpb.CreateJobRequest) (*commonpb.Status, error) {
|
|
|
|
return s.indexnode.CreateJob(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryJobs querys index jobs statues
|
|
|
|
func (s *Server) QueryJobs(ctx context.Context, req *indexpb.QueryJobsRequest) (*indexpb.QueryJobsResponse, error) {
|
|
|
|
return s.indexnode.QueryJobs(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DropJobs drops index build jobs
|
|
|
|
func (s *Server) DropJobs(ctx context.Context, req *indexpb.DropJobsRequest) (*commonpb.Status, error) {
|
|
|
|
return s.indexnode.DropJobs(ctx, req)
|
2021-01-29 17:08:31 +08:00
|
|
|
}
|
2021-01-26 14:31:31 +08:00
|
|
|
|
2022-08-25 15:48:54 +08:00
|
|
|
// GetJobNum gets indexnode's job statisctics
|
|
|
|
func (s *Server) GetJobStats(ctx context.Context, req *indexpb.GetJobStatsRequest) (*indexpb.GetJobStatsResponse, error) {
|
|
|
|
return s.indexnode.GetJobStats(ctx, req)
|
2022-07-07 14:44:21 +08:00
|
|
|
}
|
|
|
|
|
2022-08-12 13:20:39 +08:00
|
|
|
// ShowConfigurations gets specified configurations para of IndexNode
|
|
|
|
func (s *Server) ShowConfigurations(ctx context.Context, req *internalpb.ShowConfigurationsRequest) (*internalpb.ShowConfigurationsResponse, error) {
|
|
|
|
return s.indexnode.ShowConfigurations(ctx, req)
|
|
|
|
}
|
|
|
|
|
2021-09-24 20:41:56 +08:00
|
|
|
// GetMetrics gets the metrics info of IndexNode.
|
2021-08-19 10:28:10 +08:00
|
|
|
func (s *Server) GetMetrics(ctx context.Context, request *milvuspb.GetMetricsRequest) (*milvuspb.GetMetricsResponse, error) {
|
|
|
|
return s.indexnode.GetMetrics(ctx, request)
|
|
|
|
}
|
|
|
|
|
2021-09-24 20:41:56 +08:00
|
|
|
// NewServer create a new IndexNode grpc server.
|
2022-04-07 22:05:32 +08:00
|
|
|
func NewServer(ctx context.Context, factory dependency.Factory) (*Server, error) {
|
2021-01-29 17:08:31 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2023-01-12 19:49:40 +08:00
|
|
|
node := indexnode.NewIndexNode(ctx1, factory)
|
2021-01-26 14:31:31 +08:00
|
|
|
|
|
|
|
return &Server{
|
2021-01-29 17:08:31 +08:00
|
|
|
loopCtx: ctx1,
|
|
|
|
loopCancel: cancel,
|
2021-03-08 10:09:48 +08:00
|
|
|
indexnode: node,
|
2021-01-29 17:08:31 +08:00
|
|
|
grpcErrChan: make(chan error),
|
2021-01-26 14:31:31 +08:00
|
|
|
}, nil
|
|
|
|
}
|