Skip to content

Commit

Permalink
feat: replace RpcActions with MetricActions
Browse files Browse the repository at this point in the history
  • Loading branch information
ermineJose committed Feb 4, 2025
1 parent ab29551 commit 895fdd5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
3 changes: 1 addition & 2 deletions ant-node-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl From<u8> for VerbosityLevel {
}

use crate::error::{Error, Result};
use ant_service_management::metric::MetricClient;
use ant_service_management::rpc::RpcActions;
use ant_service_management::metric::{MetricActions, MetricClient};
use ant_service_management::{
control::ServiceControl, error::Error as ServiceError, NodeRegistry,
NodeService, NodeServiceData, ServiceStateActions, ServiceStatus, UpgradeOptions,
Expand Down
5 changes: 2 additions & 3 deletions ant-node-manager/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ use crate::helpers::{
use ant_bootstrap::PeersArgs;
use ant_evm::{EvmNetwork, RewardsAddress};
use ant_logging::LogFormat;
use ant_service_management::metric::MetricClient;
use ant_service_management::metric::{MetricActions, MetricClient};
use ant_service_management::{
control::ServiceControl,
rpc::RpcActions,
NodeRegistry, NodeServiceData, ServiceStatus,
};
use color_eyre::eyre::OptionExt;
Expand Down Expand Up @@ -382,7 +381,7 @@ pub struct RunNodeOptions {
pub async fn run_node(
run_options: RunNodeOptions,
launcher: &dyn Launcher,
metric_client: &dyn RpcActions,
metric_client: &dyn MetricActions,
) -> Result<NodeServiceData> {
info!("Launching node {}...", run_options.number);
println!("Launching node {}...", run_options.number);
Expand Down
35 changes: 11 additions & 24 deletions ant-service-management/src/metric.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// use ant_protocol::CLOSE_GROUP_SIZE;
use async_trait::async_trait;
use libp2p::PeerId;
use crate::rpc::{RpcActions, NodeInfo, NetworkInfo, RecordAddress};
use crate::rpc::{NodeInfo, NetworkInfo};
use tokio::time::Duration;
use std::path::PathBuf;
use crate::error::{Result,Error};

// const MAX_CONNECTION_RETRY_ATTEMPTS: u8 = 5;
//const CONNECTION_RETRY_DELAY_SEC: Duration = Duration::from_secs(1);

#[async_trait]
pub trait MetricActions: Sync {
async fn node_info(&self) -> Result<NodeInfo>;
async fn network_info(&self) -> Result<NetworkInfo>;
async fn is_node_connected_to_network(&self, timeout: Duration) -> Result<()>;
}
#[derive(Debug, Clone)]
pub struct NodeInfoMetrics {
peer_id: PeerId,
Expand Down Expand Up @@ -158,7 +164,7 @@ impl MetricClient {
}

#[async_trait]
impl RpcActions for MetricClient {
impl MetricActions for MetricClient {
async fn node_info(&self) -> Result<NodeInfo> {
let scrape = self.get_endpoint_metrics("metadata_extended").await?;
let mut node_info = NodeInfoMetrics::default();
Expand Down Expand Up @@ -202,21 +208,10 @@ impl RpcActions for MetricClient {
})
}

async fn record_addresses(&self) -> Result<Vec<RecordAddress>> {
Ok(vec![])
}

async fn node_restart(&self, _delay_millis: u64, _retain_peer_id: bool) -> Result<()> {
Ok(())
}

async fn node_stop(&self, _delay_millis: u64) -> Result<()> {
Ok(())
}

async fn is_node_connected_to_network(&self, _timeout: Duration) -> Result<()> {
// This is causing 5 mins delay during starting the node,
// need to debug and fix without this, nodes are starting normally
// Todo: This is causing 5 mins delay during starting the node,
// Todo: metrics server starts way later than the rpc server in node, need to refactor it further.

// let max_attempts = std::cmp::max(1, timeout.as_secs() / CONNECTION_RETRY_DELAY_SEC.as_secs());
// trace!(
// "Metric conneciton max attempts set to: {max_attempts} with retry_delay of {:?}",
Expand Down Expand Up @@ -254,12 +249,4 @@ impl RpcActions for MetricClient {
// }
Ok(())
}

async fn update_log_level(&self, _log_levels: String) -> Result<()> {
Ok(())
}

async fn node_update(&self, _delay_millis: u64) -> Result<()> {
Ok(())
}
}
14 changes: 7 additions & 7 deletions ant-service-management/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{error::Result, rpc::RpcActions, ServiceStateActions, ServiceStatus, UpgradeOptions};
use crate::{error::Result, metric::MetricActions, ServiceStateActions, ServiceStatus, UpgradeOptions};
use ant_bootstrap::PeersArgs;
use ant_evm::{AttoTokens, EvmNetwork, RewardsAddress};
use ant_logging::LogFormat;
Expand All @@ -25,18 +25,18 @@ use std::{

pub struct NodeService<'a> {
pub service_data: &'a mut NodeServiceData,
pub rpc_actions: Box<dyn RpcActions + Send>,
pub metric_actions: Box<dyn MetricActions + Send>,
/// Used to enable dynamic startup delay based on the time it takes for a node to connect to the network.
pub connection_timeout: Option<Duration>,
}

impl<'a> NodeService<'a> {
pub fn new(
service_data: &'a mut NodeServiceData,
rpc_actions: Box<dyn RpcActions + Send>,
metric_actions: Box<dyn MetricActions + Send>,
) -> NodeService<'a> {
NodeService {
rpc_actions,
metric_actions,
service_data,
connection_timeout: None,
}
Expand Down Expand Up @@ -176,18 +176,18 @@ impl ServiceStateActions for NodeService<'_> {
"Performing dynamic startup delay for {}",
self.service_data.service_name
);
self.rpc_actions
self.metric_actions
.is_node_connected_to_network(connection_timeout)
.await?;
}

let node_info = self
.rpc_actions
.metric_actions
.node_info()
.await
.inspect_err(|err| error!("Error obtaining node_info via RPC: {err:?}"))?;
let network_info = self
.rpc_actions
.metric_actions
.network_info()
.await
.inspect_err(|err| error!("Error obtaining network_info via RPC: {err:?}"))?;
Expand Down

0 comments on commit 895fdd5

Please sign in to comment.