forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is the only required service that lives both on json and api, so just bringing it over -- will delete the other one shortly
- Loading branch information
Showing
6 changed files
with
67 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) The Diem Core Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use anyhow::{ensure, Result}; | ||
use std::{ | ||
ops::Sub, | ||
sync::Arc, | ||
time::{Duration, SystemTime, UNIX_EPOCH}, | ||
}; | ||
use storage_interface::MoveDbReader; | ||
use warp::{filters::BoxedFilter, reject, Filter, Reply}; | ||
|
||
// HealthCheckParams is optional params for different layer's health check. | ||
// If no param is provided, server return 200 by default to indicate HTTP server is running health. | ||
#[derive(serde::Deserialize)] | ||
struct HealthCheckParams { | ||
// Health check returns 200 when this param is provided and meet the following condition: | ||
// server latest ledger info timestamp >= server current time timestamp - duration_secs | ||
pub duration_secs: Option<u64>, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct HealthCheckError; | ||
impl reject::Reject for HealthCheckError {} | ||
|
||
pub fn health_check_route(health_diem_db: Arc<dyn MoveDbReader>) -> BoxedFilter<(impl Reply,)> { | ||
warp::path!("-" / "healthy") | ||
.and(warp::path::end()) | ||
.and(warp::query().map(move |params: HealthCheckParams| params)) | ||
.and(warp::any().map(move || health_diem_db.clone())) | ||
.and(warp::any().map(SystemTime::now)) | ||
.and_then(health_check) | ||
.boxed() | ||
} | ||
|
||
async fn health_check( | ||
params: HealthCheckParams, | ||
db: Arc<dyn MoveDbReader>, | ||
now: SystemTime, | ||
) -> Result<Box<dyn warp::Reply>, warp::Rejection> { | ||
if let Some(duration) = params.duration_secs { | ||
let ledger_info = db | ||
.get_latest_ledger_info() | ||
.map_err(|_| reject::custom(HealthCheckError))?; | ||
let timestamp = ledger_info.ledger_info().timestamp_usecs(); | ||
|
||
check_latest_ledger_info_timestamp(duration, timestamp, now) | ||
.map_err(|_| reject::custom(HealthCheckError))?; | ||
} | ||
Ok(Box::new("diem-node:ok")) | ||
} | ||
|
||
pub fn check_latest_ledger_info_timestamp( | ||
duration_sec: u64, | ||
timestamp_usecs: u64, | ||
now: SystemTime, | ||
) -> Result<()> { | ||
let timestamp = Duration::from_micros(timestamp_usecs); | ||
let expectation = now | ||
.sub(Duration::from_secs(duration_sec)) | ||
.duration_since(UNIX_EPOCH)?; | ||
ensure!(timestamp >= expectation); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
mod accounts; | ||
mod context; | ||
mod events; | ||
mod health_check; | ||
mod index; | ||
pub(crate) mod log; | ||
mod metrics; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters