From 9f72cd4bbbe7720cbd766506605b314e08813d92 Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Sun, 31 May 2026 02:36:30 +0000 Subject: [PATCH] refactor: update health report to connection test and simplify proto definitions --- apps/nxmesh-agent/src/main.rs | 8 +- apps/nxmesh-master/src/service/agent/mod.rs | 39 +--- crates/nxmesh-proto/proto/agent.proto | 236 ++++++-------------- 3 files changed, 77 insertions(+), 206 deletions(-) diff --git a/apps/nxmesh-agent/src/main.rs b/apps/nxmesh-agent/src/main.rs index e3dc71f..8958005 100644 --- a/apps/nxmesh-agent/src/main.rs +++ b/apps/nxmesh-agent/src/main.rs @@ -62,14 +62,14 @@ async fn main() { // send a dummy heartbeat to verify the connection is working let client = master_connector.get_client(); - let request = nxmesh_proto::HealthReport { + let request = nxmesh_proto::TestRequest { ..Default::default() }; - match client.lock().await.report_health(request).await { - Ok(_) => info!("Successfully sent health report to master."), + match client.lock().await.connection_test(request).await { + Ok(_) => info!("Successfully sent connection test to master."), Err(e) => { - error!("Failed to send health report to master: {}", e); + error!("Failed to send connection test to master: {}", e); exit(1); } } diff --git a/apps/nxmesh-master/src/service/agent/mod.rs b/apps/nxmesh-master/src/service/agent/mod.rs index ad5a849..e8f329e 100644 --- a/apps/nxmesh-master/src/service/agent/mod.rs +++ b/apps/nxmesh-master/src/service/agent/mod.rs @@ -1,8 +1,6 @@ -use nxmesh_proto::{ - Ack, AgentMessage, HealthReport, MasterMessage, MetricsBatch, - agent_service_server::AgentService, -}; -use tracing::warn; +use nxmesh_proto::{AgentMessage, MasterMessage, agent_service_server::AgentService}; + +pub mod repo; #[derive(Debug, Default)] pub struct AgentServerService {} @@ -25,30 +23,13 @@ impl AgentService for AgentServerService { todo!() } - #[doc = " ReportHealth sends a health report to the master"] - #[allow( - mismatched_lifetime_syntaxes, - clippy::type_complexity, - clippy::type_repetition_in_bounds - )] - async fn report_health( + async fn connection_test( &self, - request: tonic::Request, - ) -> Result, tonic::Status> { - warn!("Received health report: {:?}", request.get_ref()); - todo!() - } - - #[doc = " ReportMetrics sends metrics batch to the master"] - #[allow( - mismatched_lifetime_syntaxes, - clippy::type_complexity, - clippy::type_repetition_in_bounds - )] - async fn report_metrics( - &self, - request: tonic::Request, - ) -> Result, tonic::Status> { - todo!() + _request: tonic::Request, + ) -> Result, tonic::Status> { + Ok(tonic::Response::new(nxmesh_proto::TestResponse { + success: true, + error_message: String::new(), + })) } } diff --git a/crates/nxmesh-proto/proto/agent.proto b/crates/nxmesh-proto/proto/agent.proto index ed42b96..9f57edd 100644 --- a/crates/nxmesh-proto/proto/agent.proto +++ b/crates/nxmesh-proto/proto/agent.proto @@ -11,103 +11,53 @@ service AgentService { // Stream establishes a persistent connection for real-time communication rpc Stream(stream AgentMessage) returns (stream MasterMessage); - // ReportHealth sends a health report to the master - rpc ReportHealth(HealthReport) returns (Ack); + rpc ConnectionTest(TestRequest) returns (TestResponse); +} - // ReportMetrics sends metrics batch to the master - rpc ReportMetrics(MetricsBatch) returns (Ack); +message TestRequest { + // no fields needed for test request +} + +message TestResponse { + bool success = 1; + string error_message = 2; // if success is false, this field should contain the error message +} + +// Messages sent from master to agent +message MasterMessage { + int64 timestamp = 1; + string message_id = 2; + oneof payload { + // requests + ConfigUpdate config_update = 3; + Command command = 4; + } } // Messages sent from agent to master message AgentMessage { string agent_id = 1; int64 timestamp = 2; + string message_id = 3; oneof payload { - RegistrationRequest registration = 3; - HealthReport health = 4; - ConfigStatus config_status = 5; - MetricsBatch metrics = 6; - LogBatch logs = 7; - Event event = 8; + // responses + ConfigUpdateResult config_update_result = 6; + CommandResult command_result = 7; } } -// Messages sent from master to agent -message MasterMessage { - int64 timestamp = 1; - oneof payload { - RegistrationResponse registration_response = 2; - ConfigUpdate config_update = 3; - Command command = 4; - Ack ack = 5; - Error error = 6; - } -} +// +// +// -// Registration -message RegistrationRequest { - string hostname = 1; - string ip_address = 2; - string version = 3; - repeated string capabilities = 4; - map labels = 5; - DeploymentMode deployment_mode = 6; -} - -message RegistrationResponse { - string agent_id = 1; - bool success = 2; - string error_message = 3; - int64 heartbeat_interval_seconds = 4; -} - -enum DeploymentMode { - DEPLOYMENT_MODE_UNSPECIFIED = 0; - DOCKER_SIDECAR = 1; - KUBERNETES_SIDECAR = 2; - STANDALONE = 3; -} - -// Health Reporting -message HealthReport { - NginxStatus nginx = 1; - SystemMetrics system = 2; - string config_checksum = 3; - int64 config_version = 4; - repeated Alert alerts = 5; -} - -message NginxStatus { - bool is_running = 1; - uint32 pid = 2; - uint64 uptime_seconds = 3; - uint32 active_connections = 4; - uint64 total_requests = 5; - float requests_per_second = 6; -} - -message SystemMetrics { - float cpu_percent = 1; - uint64 memory_used_bytes = 2; - uint64 memory_total_bytes = 3; - uint64 disk_used_bytes = 4; - uint64 disk_total_bytes = 5; - float load_average_1m = 6; -} - -message Alert { - string id = 1; - string severity = 2; // info, warning, error, critical - string message = 3; - int64 timestamp = 4; -} - -// Configuration +// ConfigUpdate represents a request from master to agent to update the configuration message ConfigUpdate { - string config_id = 1; - int64 version = 2; - repeated ConfigContent configs = 3; - repeated CertificateContent certificates = 4; + string config_id = 1; // unique identifier for this config update + string version = 2; + // The root config is the main nginx.conf file, this file will be used as the entry point for nginx configuration. The content of this file should include references to other config files if needed. The agent will write this root config to the nginx config directory and use it to reload nginx. + ConfigContent root_config = 3; + // The other config files that are referenced by the root config, e.g. "site.conf", "private/example.com.conf". If the root config does not reference any other config files, this field can be left empty. The agent will write these config files to the nginx config directory and ensure they are included in the root config. + repeated ConfigContent configs = 4; } message ConfigContent { @@ -116,113 +66,53 @@ message ConfigContent { string content = 2; } -message CertificateContent { - string id = 1; - // relative path from other config files, e.g. "certs/example.com.pem" - string path = 2; - string certificate_pem = 3; - string private_key_pem = 4; +message ConfigUpdateResult { + string config_id = 1; // should match the config_id in ConfigUpdate + bool success = 2; + ConfigUpdateError error_message = 3; // if success is false, this field should contain the error message } -message ConfigStatus { - string config_id = 1; - int64 version = 2; - ConfigApplyStatus status = 3; - string error_message = 4; - int64 applied_at = 5; +enum ConfigUpdateError { + UNKNOWN = 0; + INVALID_CONFIG = 1; // the config content is invalid, e.g. syntax error + WRITE_FAILED = 2; // failed to write the config file to disk + RELOAD_FAILED = 3; // failed to reload nginx with the new config } -enum ConfigApplyStatus { - CONFIG_APPLY_STATUS_UNSPECIFIED = 0; - PENDING = 1; - VALIDATING = 2; - APPLYING = 3; - SUCCESS = 4; - FAILED = 5; - ROLLED_BACK = 6; -} +// +// +// -// Metrics -message MetricsBatch { - int64 timestamp = 1; - repeated Metric metrics = 2; -} - -message Metric { - string name = 1; - double value = 2; - int64 timestamp = 3; - map labels = 4; - MetricType type = 5; -} - -enum MetricType { - METRIC_TYPE_UNSPECIFIED = 0; - GAUGE = 1; - COUNTER = 2; - HISTOGRAM = 3; -} - -// Logs -message LogBatch { - repeated LogEntry entries = 1; -} - -message LogEntry { - int64 timestamp = 1; - string level = 2; - string message = 3; - map fields = 4; -} - -// Commands +// Command represents a request from master to agent to execute a command, e.g. "reload", "test" message Command { - string command_id = 1; oneof command { - ReloadCommand reload = 2; - RestartCommand restart = 3; - StopCommand stop = 4; - GetStatusCommand get_status = 5; - ValidateConfigCommand validate_config = 6; + ReloadCommand reload = 1; + TestCommand test = 2; } } message ReloadCommand { - bool graceful = 1; + // no additional fields needed for reload command } -message RestartCommand { - bool force = 1; +message TestCommand { + // no additional fields needed for test command } -message StopCommand { - bool graceful = 1; - uint32 timeout_seconds = 2; +message CommandResult { + oneof result { + ReloadResult reload_result = 1; + TestResult test_result = 2; + } } -message GetStatusCommand {} - -message ValidateConfigCommand { - string config_content = 1; +message ReloadResult { + bool success = 1; + string error_message = 2; // if success is false, this field should contain the error message } -// Events -message Event { - string event_id = 1; - string event_type = 2; - int64 timestamp = 3; - map data = 4; +message TestResult { + bool success = 1; + string error_message = 2; // if success is false, this field should contain the error message } -// Common messages -message Ack { - string message_id = 1; - bool success = 2; - string error_message = 3; -} - -message Error { - string code = 1; - string message = 2; - map details = 3; -}