grpc

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package grpc provides the gRPC server implementation for Agent communication. grpc 包提供用于 Agent 通信的 gRPC 服务器实现。

Index

Constants

View Source
const (
	// DefaultGRPCPort is the default port for gRPC server.
	// DefaultGRPCPort 是 gRPC 服务器的默认端口。
	DefaultGRPCPort = 17890

	// DefaultMaxRecvMsgSize is the default maximum receive message size (16MB).
	// DefaultMaxRecvMsgSize 是默认的最大接收消息大小(16MB)。
	DefaultMaxRecvMsgSize = 16 * 1024 * 1024

	// DefaultMaxSendMsgSize is the default maximum send message size (16MB).
	// DefaultMaxSendMsgSize 是默认的最大发送消息大小(16MB)。
	DefaultMaxSendMsgSize = 16 * 1024 * 1024

	// DefaultHeartbeatInterval is the default heartbeat interval for Agent configuration.
	// DefaultHeartbeatInterval 是 Agent 配置的默认心跳间隔。
	DefaultHeartbeatInterval = 10
)

Default configuration values for gRPC server gRPC 服务器的默认配置值

Variables

View Source
var (
	// ErrServerNotRunning indicates the server is not running.
	// ErrServerNotRunning 表示服务器未运行。
	ErrServerNotRunning = errors.New("grpc: server is not running")

	// ErrServerAlreadyRunning indicates the server is already running.
	// ErrServerAlreadyRunning 表示服务器已在运行。
	ErrServerAlreadyRunning = errors.New("grpc: server is already running")

	// ErrInvalidTLSConfig indicates invalid TLS configuration.
	// ErrInvalidTLSConfig 表示无效的 TLS 配置。
	ErrInvalidTLSConfig = errors.New("grpc: invalid TLS configuration")
)

Errors for gRPC server operations gRPC 服务器操作的错误定义

View Source
var ErrClusterMonitorPushUnavailable = errors.New("cluster monitor config push is not configured / 未配置按集群下发监控配置的能力")

ErrClusterMonitorPushUnavailable 缺少主机服务、节点提供者或 Agent 管理器,无法按集群下发监控配置。 ErrClusterMonitorPushUnavailable means the host service, node provider or Agent manager is missing, so a cluster-scoped monitor config cannot be pushed.

Functions

func RegisterAgentReadyHook added in v1.2.0

func RegisterAgentReadyHook(hook AgentReadyHook)

RegisterAgentReadyHook 注册 Agent 就绪回调。 RegisterAgentReadyHook registers an Agent-ready callback.

func SetClusterNodeProvider

func SetClusterNodeProvider(provider ClusterNodeProvider)

SetClusterNodeProvider sets the cluster node provider for gRPC handlers. SetClusterNodeProvider 设置 gRPC 处理器的集群节点提供者。

func SetDiagnosticsService

func SetDiagnosticsService(svc *diagnostics.Service)

SetDiagnosticsService sets the diagnostics service for gRPC handlers. SetDiagnosticsService 设置 gRPC 处理器的诊断服务。

func SetMonitorService

func SetMonitorService(svc MonitorService)

SetMonitorService sets the monitor service for gRPC handlers. SetMonitorService 设置 gRPC 处理器的监控服务。

Types

type AgentReadyHook added in v1.2.0

type AgentReadyHook func(ctx context.Context, hostID uint)

AgentReadyHook 在 Agent(重新)注册且命令流就绪后被调用,例如重推离线期间未下发的配置。 AgentReadyHook is called after an Agent (re-)registers and its command stream is ready, e.g. to re-push configs that could not be delivered while it was offline.

type ClusterNodeProvider

type ClusterNodeProvider interface {
	GetNodeByHostAndInstallDirAndRole(ctx context.Context, hostID uint, installDir, role string) (clusterID, nodeID uint, found bool, err error)
	// GetNodesByHostID returns all nodes on a specific host with their cluster's monitor config.
	// GetNodesByHostID 返回特定主机上的所有节点及其集群的监控配置。
	GetNodesByHostID(ctx context.Context, hostID uint) ([]*NodeWithMonitorConfig, error)
	// UpdateNodeProcessStatus updates the process PID and status for a node.
	// UpdateNodeProcessStatus 更新节点的进程 PID 和状态。
	UpdateNodeProcessStatus(ctx context.Context, nodeID uint, pid int, status string) error
	// RefreshClusterStatusFromNodes recalculates cluster status from its nodes (e.g. after heartbeat).
	// RefreshClusterStatusFromNodes 根据节点状态重新计算集群状态(如心跳更新节点后)。
	RefreshClusterStatusFromNodes(ctx context.Context, clusterID uint)
	// GetClusterNodeDisplayInfo returns cluster name and node display "主机名 - 角色" for audit resource name.
	// GetClusterNodeDisplayInfo 返回集群名及节点展示「主机名 - 角色」,用于审计资源名称。
	GetClusterNodeDisplayInfo(ctx context.Context, clusterID, nodeID uint) (clusterName, nodeDisplay string)
}

ClusterNodeProvider provides cluster node information. ClusterNodeProvider 提供集群节点信息。

type MonitorService

type MonitorService interface {
	GetConfig(ctx context.Context, clusterID uint) (*monitor.MonitorConfig, error)
	RecordEventFromReport(ctx context.Context, clusterID, nodeID, hostID uint, eventType monitor.ProcessEventType, pid int, processName, installDir, role string, details map[string]string) error
}

MonitorService provides monitor-related operations for gRPC handlers. MonitorService 为 gRPC 处理器提供监控相关操作。

type NodeWithMonitorConfig

type NodeWithMonitorConfig struct {
	ClusterID     uint                   `json:"cluster_id"`
	NodeID        uint                   `json:"node_id"`
	InstallDir    string                 `json:"install_dir"`
	Role          string                 `json:"role"`
	ProcessPID    int                    `json:"process_pid"`
	MonitorConfig *monitor.MonitorConfig `json:"monitor_config"`
	// JavaHome 是节点指定的 JDK(cluster_nodes.java_home),Agent 自动拉起时使用;为空表示自动探测。
	// JavaHome is the node JDK (cluster_nodes.java_home) used by Agent auto-restart; empty means auto detection.
	JavaHome string `json:"java_home"`
}

NodeWithMonitorConfig represents a node with its cluster's monitor config. NodeWithMonitorConfig 表示带有集群监控配置的节点。

type Server

type Server struct {
	pb.UnimplementedAgentServiceServer
	// contains filtered or unexported fields
}

Server represents the gRPC server for Agent communication. Server 表示用于 Agent 通信的 gRPC 服务器。 Requirements: 8.1 - Implements gRPC server with TLS support and interceptors.

func NewServer

func NewServer(config *ServerConfig, agentManager *agent.Manager, hostService *host.Service, auditRepo *audit.Repository, logger *zap.Logger) *Server

NewServer creates a new gRPC server instance. NewServer 创建一个新的 gRPC 服务器实例。 Requirements: 8.1 - Creates gRPC server with configuration.

func (*Server) CommandStream

CommandStream handles bidirectional streaming for command dispatch and result reporting. CommandStream 处理用于命令分发和结果上报的双向流。 Requirements: 1.5, 8.6 - Implements bidirectional stream for command dispatching.

func (*Server) DownloadPackage added in v1.2.0

func (s *Server) DownloadPackage(req *pb.DownloadPackageRequest, stream grpc.ServerStreamingServer[pb.PackageChunk]) error

DownloadPackage streams a registered installation package to the Agent (raw bytes, resumable). DownloadPackage 向 Agent 流式发送已登记的安装包(原始字节,支持断点续传)。

func (*Server) GetDiagnosticsLogCursors

func (s *Server) GetDiagnosticsLogCursors(ctx context.Context, req *pb.DiagnosticsCursorRequest) (*pb.DiagnosticsCursorResponse, error)

GetDiagnosticsLogCursors returns all diagnostics log cursors for a given agent. GetDiagnosticsLogCursors 返回某个 Agent 的所有诊断日志游标。

func (*Server) GetPort

func (s *Server) GetPort() int

GetPort returns the server port. GetPort 返回服务器端口。

func (*Server) HandleDiscoverClusters

func (s *Server) HandleDiscoverClusters(ctx context.Context, agentID string, params map[string]string) (*pb.CommandResponse, error)

HandleDiscoverClusters handles DISCOVER_CLUSTERS command. HandleDiscoverClusters 处理 DISCOVER_CLUSTERS 命令。 Requirements: 1.3, 1.7 - Trigger agent discovery Task 11.1

func (*Server) HandleProcessEventReport

func (s *Server) HandleProcessEventReport(ctx context.Context, agentID string, report *pb.ProcessEventReport) error

HandleProcessEventReport handles ProcessEventReport from agent. HandleProcessEventReport 处理来自 Agent 的 ProcessEventReport。 Requirements: 3.4, 3.5 - Receive and process process events Task 11.3

func (*Server) HandleUpdateMonitorConfig

func (s *Server) HandleUpdateMonitorConfig(ctx context.Context, agentID string, config *monitor.MonitorConfig, processes []*monitor.TrackedProcessInfo) error

HandleUpdateMonitorConfig handles UPDATE_MONITOR_CONFIG command. HandleUpdateMonitorConfig 处理 UPDATE_MONITOR_CONFIG 命令。 Requirements: 5.4, 7.5 - Push monitor config to agent Task 11.2

func (*Server) Heartbeat

func (s *Server) Heartbeat(ctx context.Context, req *pb.HeartbeatRequest) (*pb.HeartbeatResponse, error)

Heartbeat handles Agent heartbeat requests. Heartbeat 处理 Agent 心跳请求。 Requirements: 1.3, 3.3 - Processes heartbeat, updates host resource usage.

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns whether the server is running. IsRunning 返回服务器是否正在运行。

func (*Server) LogStream

LogStream handles log streaming from Agents. LogStream 处理来自 Agent 的日志流。 Requirements: 10.2, 10.3 - Receives Agent logs and stores to audit log.

func (*Server) PushClusterMonitorConfigToHost added in v1.2.0

func (s *Server) PushClusterMonitorConfigToHost(ctx context.Context, clusterID uint, hostID uint) error

PushClusterMonitorConfigToHost 向主机 Agent 下发只针对一个集群的监控配置:跟踪列表只包含该集群在此主机上的节点; 该集群在此主机已无节点时下发空列表,Agent 据此只清理该集群的跟踪项,其他集群的跟踪项保持不变(缩容后调用)。 PushClusterMonitorConfigToHost pushes a monitor config scoped to one cluster to the host's Agent: the tracked list holds only this cluster's nodes on the host; when the cluster has no node left there, an empty list is sent so the Agent drops only this cluster's tracked processes and keeps other clusters' entries (called after scale-in).

func (*Server) Register

func (s *Server) Register(ctx context.Context, req *pb.RegisterRequest) (*pb.RegisterResponse, error)

Register handles Agent registration requests. Register 处理 Agent 注册请求。 Requirements: 1.1, 3.2 - Handles Agent registration, matches host IP, updates Agent status.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the gRPC server. Start 启动 gRPC 服务器。 Requirements: 8.1 - Starts gRPC server with configured options.

func (*Server) Stop

func (s *Server) Stop()

Stop gracefully stops the gRPC server. Stop 优雅地停止 gRPC 服务器。

type ServerConfig

type ServerConfig struct {
	// Port is the port number for the gRPC server.
	// Port 是 gRPC 服务器的端口号。
	Port int

	// TLSEnabled indicates whether TLS is enabled.
	// TLSEnabled 表示是否启用 TLS。
	TLSEnabled bool

	// CertFile is the path to the TLS certificate file.
	// CertFile 是 TLS 证书文件的路径。
	CertFile string

	// KeyFile is the path to the TLS key file.
	// KeyFile 是 TLS 密钥文件的路径。
	KeyFile string

	// CAFile is the path to the CA certificate file for client verification.
	// CAFile 是用于客户端验证的 CA 证书文件路径。
	CAFile string

	// MaxRecvMsgSize is the maximum receive message size in bytes.
	// MaxRecvMsgSize 是最大接收消息大小(字节)。
	MaxRecvMsgSize int

	// MaxSendMsgSize is the maximum send message size in bytes.
	// MaxSendMsgSize 是最大发送消息大小(字节)。
	MaxSendMsgSize int

	// HeartbeatInterval is the heartbeat interval to send to Agents (seconds).
	// HeartbeatInterval 是发送给 Agent 的心跳间隔(秒)。
	HeartbeatInterval int
}

ServerConfig holds configuration for the gRPC server. ServerConfig 保存 gRPC 服务器的配置。 Requirements: 8.1 - Configures gRPC server options including TLS and interceptors.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL