Skip to content

Commit

Permalink
fix: check f3 running status on healthz and readyz endpoints (#5178)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 authored Jan 24, 2025
1 parent 0062b17 commit bfe406e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ pub(super) async fn start(
config: Config,
shutdown_send: mpsc::Sender<()>,
) -> anyhow::Result<()> {
if opts.detach {
tracing::warn!("F3 sidecar is disabled in detach mode");
std::env::set_var("FOREST_F3_SIDECAR_FFI_ENABLED", "0");
}

let chain_config = Arc::new(ChainConfig::from_chain(config.chain()));
if chain_config.is_testnet() {
CurrentNetwork::set_global(Network::Testnet);
Expand Down
2 changes: 1 addition & 1 deletion src/f3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn run_f3_sidecar_if_enabled(
}

/// Whether F3 sidecar via FFI is enabled.
fn is_sidecar_ffi_enabled(chain_config: &ChainConfig) -> bool {
pub fn is_sidecar_ffi_enabled(chain_config: &ChainConfig) -> bool {
// Respect the environment variable when set, and fallback to chain config when not set.
let enabled =
is_env_set_and_truthy("FOREST_F3_SIDECAR_FFI_ENABLED").unwrap_or(chain_config.f3_enabled);
Expand Down
17 changes: 17 additions & 0 deletions src/health/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ahash::HashMap;
use axum::extract::{self, Query};

use crate::db::SettingsExt;
use crate::rpc::f3::F3IsRunning;
use crate::{chain_sync::SyncStage, networks::calculate_expected_epoch};

use super::{AppError, ForestState};
Expand Down Expand Up @@ -43,6 +44,7 @@ pub(crate) async fn livez(
/// - The current epoch of the node is not too far behind the network
/// - The RPC server is running
/// - The Ethereum mapping is up to date
/// - The F3 side car is running if enabled
///
/// If any of these conditions are not met, the nod is **not** ready to serve requests.
pub(crate) async fn readyz(
Expand All @@ -56,6 +58,7 @@ pub(crate) async fn readyz(
ready &= check_epoch_up_to_date(&state, &mut acc);
ready &= check_rpc_server_running(&state, &mut acc).await;
ready &= check_eth_mapping_up_to_date(&state, &mut acc);
ready &= check_f3_running(&state, &mut acc).await;

if ready {
Ok(acc.result_ok())
Expand All @@ -77,6 +80,7 @@ pub(crate) async fn healthz(
healthy &= check_rpc_server_running(&state, &mut acc).await;
healthy &= check_sync_state_not_error(&state, &mut acc);
healthy &= check_peers_connected(&state, &mut acc);
healthy &= check_f3_running(&state, &mut acc).await;

if healthy {
Ok(acc.result_ok())
Expand Down Expand Up @@ -169,6 +173,19 @@ fn check_eth_mapping_up_to_date(state: &ForestState, acc: &mut MessageAccumulato
}
}

async fn check_f3_running(state: &ForestState, acc: &mut MessageAccumulator) -> bool {
if !crate::f3::is_sidecar_ffi_enabled(&state.chain_config) {
acc.push_ok("f3 disabled");
true
} else if F3IsRunning::is_f3_running().await.unwrap_or_default() {
acc.push_ok("f3 running");
true
} else {
acc.push_err("f3 not running");
false
}
}

/// Sample message accumulator for healthcheck responses. It is intended to accumulate messages for
/// verbose responses.
struct MessageAccumulator {
Expand Down
13 changes: 10 additions & 3 deletions src/rpc/methods/f3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,15 @@ impl RpcMethod<1> for F3GetF3PowerTable {
}

pub enum F3IsRunning {}

impl F3IsRunning {
pub async fn is_f3_running() -> anyhow::Result<bool> {
let client = get_rpc_http_client()?;
let response = client.request(Self::NAME, ArrayParams::new()).await?;
Ok(response)
}
}

impl RpcMethod<0> for F3IsRunning {
const NAME: &'static str = "Filecoin.F3IsRunning";
const PARAM_NAMES: [&'static str; 0] = [];
Expand All @@ -663,9 +672,7 @@ impl RpcMethod<0> for F3IsRunning {
type Ok = bool;

async fn handle(_: Ctx<impl Blockstore>, (): Self::Params) -> Result<Self::Ok, ServerError> {
let client = get_rpc_http_client()?;
let response = client.request(Self::NAME, ArrayParams::new()).await?;
Ok(response)
Ok(Self::is_f3_running().await?)
}
}

Expand Down

0 comments on commit bfe406e

Please sign in to comment.