From b04092e41e3c86c9bf7d7f7733eaabd563131eb7 Mon Sep 17 00:00:00 2001 From: aspect Date: Thu, 5 Sep 2024 18:00:24 +0300 Subject: [PATCH] add proxy limit field to sysinfo (#544) --- rpc/core/src/model/message.rs | 11 ++++-- rpc/core/src/model/tests.rs | 1 + rpc/grpc/core/proto/rpc.proto | 1 + rpc/grpc/core/src/convert/message.rs | 2 ++ rpc/service/src/service.rs | 1 + utils/src/sysinfo.rs | 50 ++++++++++++++++++++++++++-- 6 files changed, 61 insertions(+), 5 deletions(-) diff --git a/rpc/core/src/model/message.rs b/rpc/core/src/model/message.rs index 779f7593a..ba8d6abf7 100644 --- a/rpc/core/src/model/message.rs +++ b/rpc/core/src/model/message.rs @@ -1899,6 +1899,7 @@ pub struct GetSystemInfoResponse { pub cpu_physical_cores: u16, pub total_memory: u64, pub fd_limit: u32, + pub proxy_socket_limit_per_cpu_core: Option, } impl std::fmt::Debug for GetSystemInfoResponse { @@ -1910,19 +1911,21 @@ impl std::fmt::Debug for GetSystemInfoResponse { .field("cpu_physical_cores", &self.cpu_physical_cores) .field("total_memory", &self.total_memory) .field("fd_limit", &self.fd_limit) + .field("proxy_socket_limit_per_cpu_core", &self.proxy_socket_limit_per_cpu_core) .finish() } } impl Serializer for GetSystemInfoResponse { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - store!(u16, &1, writer)?; + store!(u16, &2, writer)?; store!(String, &self.version, writer)?; store!(Option>, &self.system_id, writer)?; store!(Option>, &self.git_hash, writer)?; store!(u16, &self.cpu_physical_cores, writer)?; store!(u64, &self.total_memory, writer)?; store!(u32, &self.fd_limit, writer)?; + store!(Option, &self.proxy_socket_limit_per_cpu_core, writer)?; Ok(()) } @@ -1930,7 +1933,7 @@ impl Serializer for GetSystemInfoResponse { impl Deserializer for GetSystemInfoResponse { fn deserialize(reader: &mut R) -> std::io::Result { - let _version = load!(u16, reader)?; + let payload_version = load!(u16, reader)?; let version = load!(String, reader)?; let system_id = load!(Option>, reader)?; let git_hash = load!(Option>, reader)?; @@ -1938,7 +1941,9 @@ impl Deserializer for GetSystemInfoResponse { let total_memory = load!(u64, reader)?; let fd_limit = load!(u32, reader)?; - Ok(Self { version, system_id, git_hash, cpu_physical_cores, total_memory, fd_limit }) + let proxy_socket_limit_per_cpu_core = if payload_version > 1 { load!(Option, reader)? } else { None }; + + Ok(Self { version, system_id, git_hash, cpu_physical_cores, total_memory, fd_limit, proxy_socket_limit_per_cpu_core }) } } diff --git a/rpc/core/src/model/tests.rs b/rpc/core/src/model/tests.rs index 11ebe7de1..d931f5ac2 100644 --- a/rpc/core/src/model/tests.rs +++ b/rpc/core/src/model/tests.rs @@ -970,6 +970,7 @@ mod mockery { cpu_physical_cores: mock(), total_memory: mock(), fd_limit: mock(), + proxy_socket_limit_per_cpu_core: mock(), } } } diff --git a/rpc/grpc/core/proto/rpc.proto b/rpc/grpc/core/proto/rpc.proto index 0147e15e2..7a38f8852 100644 --- a/rpc/grpc/core/proto/rpc.proto +++ b/rpc/grpc/core/proto/rpc.proto @@ -851,6 +851,7 @@ message GetSystemInfoResponseMessage{ uint32 coreNum = 4; uint64 totalMemory = 5; uint32 fdLimit = 6; + uint32 proxySocketLimitPerCpuCore = 7; RPCError error = 1000; } diff --git a/rpc/grpc/core/src/convert/message.rs b/rpc/grpc/core/src/convert/message.rs index a04f9c863..c0e75cf03 100644 --- a/rpc/grpc/core/src/convert/message.rs +++ b/rpc/grpc/core/src/convert/message.rs @@ -480,6 +480,7 @@ from!(item: RpcResult<&kaspa_rpc_core::GetSystemInfoResponse>, protowire::GetSys total_memory : item.total_memory, core_num : item.cpu_physical_cores as u32, fd_limit : item.fd_limit, + proxy_socket_limit_per_cpu_core : item.proxy_socket_limit_per_cpu_core.unwrap_or_default(), error: None, } }); @@ -962,6 +963,7 @@ try_from!(item: &protowire::GetSystemInfoResponseMessage, RpcResult 0).then_some(item.proxy_socket_limit_per_cpu_core), } }); diff --git a/rpc/service/src/service.rs b/rpc/service/src/service.rs index d498f522f..2c22fd6bb 100644 --- a/rpc/service/src/service.rs +++ b/rpc/service/src/service.rs @@ -1067,6 +1067,7 @@ NOTE: This error usually indicates an RPC conversion error between the node and cpu_physical_cores: self.system_info.cpu_physical_cores, total_memory: self.system_info.total_memory, fd_limit: self.system_info.fd_limit, + proxy_socket_limit_per_cpu_core: self.system_info.proxy_socket_limit_per_cpu_core, }; Ok(response) diff --git a/utils/src/sysinfo.rs b/utils/src/sysinfo.rs index 4e009d449..ba6f25110 100644 --- a/utils/src/sysinfo.rs +++ b/utils/src/sysinfo.rs @@ -2,21 +2,32 @@ use crate::fd_budget; use crate::git; use crate::hex::ToHex; use sha2::{Digest, Sha256}; -use std::fs::File; +use std::fs::{read_to_string, File}; use std::io::Read; +use std::path::PathBuf; +// use std::fs::read_to_string; use std::sync::OnceLock; static SYSTEM_INFO: OnceLock = OnceLock::new(); #[derive(Clone)] pub struct SystemInfo { + /// unique system (machine) identifier pub system_id: Option>, + /// full git commit hash pub git_hash: Option>, + /// short git commit hash pub git_short_hash: Option>, + /// crate (workspace) version pub version: String, + /// number of physical CPU cores pub cpu_physical_cores: u16, + /// total system memory in bytes pub total_memory: u64, + /// file descriptor limit of the current process pub fd_limit: u32, + /// maximum number of sockets per CPU core + pub proxy_socket_limit_per_cpu_core: Option, } // provide hex encoding for system_id, git_hash, and git_short_hash @@ -30,6 +41,7 @@ impl std::fmt::Debug for SystemInfo { .field("cpu_physical_cores", &self.cpu_physical_cores) .field("total_memory", &self.total_memory) .field("fd_limit", &self.fd_limit) + .field("proxy_socket_limit_per_cpu_core", &self.proxy_socket_limit_per_cpu_core) .finish() } } @@ -46,8 +58,18 @@ impl Default for SystemInfo { let git_hash = git::hash(); let git_short_hash = git::short_hash(); let version = git::version(); + let proxy_socket_limit_per_cpu_core = Self::try_proxy_socket_limit_per_cpu_core(); - SystemInfo { system_id, git_hash, git_short_hash, version, cpu_physical_cores, total_memory, fd_limit } + SystemInfo { + system_id, + git_hash, + git_short_hash, + version, + cpu_physical_cores, + total_memory, + fd_limit, + proxy_socket_limit_per_cpu_core, + } }); (*system_info).clone() } @@ -72,6 +94,19 @@ impl SystemInfo { sha256.update(some_id.as_bytes()); Some(sha256.finalize().to_vec()) } + + fn try_proxy_socket_limit_per_cpu_core() -> Option { + let nginx_config_path = PathBuf::from("/etc/nginx/nginx.conf"); + if nginx_config_path.exists() { + read_to_string(nginx_config_path) + .ok() + .and_then(|content| content.lines().find(|line| line.trim().starts_with("worker_connections")).map(String::from)) + .and_then(|line| line.split_whitespace().nth(1).map(|v| v.replace(";", ""))) + .and_then(|value| value.parse::().ok()) + } else { + None + } + } } impl AsRef for SystemInfo { @@ -79,3 +114,14 @@ impl AsRef for SystemInfo { self } } + +// #[cfg(test)] +// mod tests { +// use super::*; + +// #[test] +// fn test_system_info() { +// let system_info = SystemInfo::default(); +// println!("{:#?}", system_info); +// } +// }