Skip to content

fix: ready endpoint checks for fully hydrated cache #2553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/hermes/server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/hermes/server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hermes"
version = "0.8.5"
version = "0.8.6"
description = "Hermes is an agent that provides Verified Prices from the Pythnet Pyth Oracle."
edition = "2021"

Expand Down
32 changes: 27 additions & 5 deletions apps/hermes/server/src/api/rest/ready.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
use {
crate::{api::ApiState, state::aggregate::Aggregates},
crate::{
api::ApiState,
state::{aggregate::Aggregates, cache::Cache},
},
axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
Json,
},
serde_json::json,
};

/// Endpoint that returns OK (200) only when the cache is fully hydrated.
///
/// The cache is considered fully hydrated when all of the following conditions are met:
/// - `has_completed_recently`: The latest completed update is recent (within the staleness threshold)
/// - `is_not_behind`: The latest completed slot isn't too far behind the latest observed slot
/// - `is_metadata_loaded`: Price feeds metadata is not empty
///
/// If any of these conditions are not met, the endpoint returns SERVICE_UNAVAILABLE (503)
/// along with detailed metadata about the readiness state.
pub async fn ready<S>(State(state): State<ApiState<S>>) -> Response
where
S: Aggregates,
S: Aggregates + Cache,
{
let state = &*state.state;
match Aggregates::is_ready(state).await {
(true, _) => (StatusCode::OK, "OK").into_response(),
(false, metadata) => (StatusCode::SERVICE_UNAVAILABLE, Json(metadata)).into_response(),
let (aggregates_ready, metadata) = Aggregates::is_ready(state).await;
let cache_ready = Cache::is_cache_ready(state).await;

if aggregates_ready && cache_ready {
(StatusCode::OK, "OK").into_response()
} else {
let response_metadata = json!({
"aggregates_ready": aggregates_ready,
"cache_ready": cache_ready,
"details": metadata
});
(StatusCode::SERVICE_UNAVAILABLE, Json(response_metadata)).into_response()
}
}
15 changes: 15 additions & 0 deletions apps/hermes/server/src/state/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub trait Cache {
request_time: RequestTime,
filter: MessageStateFilter,
) -> Result<Vec<MessageState>>;
async fn is_cache_ready(&self) -> bool;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -274,6 +275,20 @@ where
let cache = self.into().wormhole_merkle_state_cache.read().await;
Ok(cache.get(&slot).cloned())
}

/// Checks if the cache contains sufficient data to serve requests.
/// Specifically, TWAP requests -- to serve a TWAP of X seconds, there
/// needs to be X seconds of data in the cache. We need to serve TWAPs
/// from the cache since TwapMessages only exist in Hermes at the moment,
/// not Benchmarks.
///
/// The cache is considered ready when it is at or near its maximum slot capacity.
async fn is_cache_ready(&self) -> bool {
let message_cache = self.into().accumulator_messages_cache.read().await;
let max_cache_size = self.into().cache_size as usize;

message_cache.len() >= (max_cache_size * 9 / 10)
}
}

async fn retrieve_message_state(
Expand Down
Loading