|
1 | | -//! Implementation of the GET /health/ready endpoint |
| 1 | +//! # Implementation of the GET /health/ready endpoint |
| 2 | +//! |
| 3 | +//! This module provides an HTTP endpoint to monitor the readiness of the service's |
| 4 | +//! database connections and attempt to reconnect to any databases that are not currently |
| 5 | +//! live. It uses the `LIVE_INDEX_DB` and `LIVE_EVENT_DB` atomic booleans defined in the |
| 6 | +//! parent module to track the status of the Index DB and Event DB, respectively. |
| 7 | +//! |
| 8 | +//! ## Key Features |
| 9 | +//! |
| 10 | +//! 1. **Reconnection Logic**: |
| 11 | +//! - If either `LIVE_INDEX_DB` or `LIVE_EVENT_DB` is `false`, the service will attempt |
| 12 | +//! to reconnect to the corresponding database. |
| 13 | +//! - If the reconnection attempt is successful, the respective flag (`LIVE_INDEX_DB` |
| 14 | +//! or `LIVE_EVENT_DB`) is set to `true`. |
| 15 | +//! - If the reconnection attempt fails, the flag remains `false`. |
| 16 | +//! |
| 17 | +//! 2. **Readiness Check Logic**: |
| 18 | +//! - After attempting to reconnect to any non-live databases, the endpoint checks the |
| 19 | +//! status of both `LIVE_INDEX_DB` and `LIVE_EVENT_DB`. |
| 20 | +//! - If both flags are `true`, the endpoint returns a `204 No Content` response, |
| 21 | +//! indicating that all databases are live and the service is healthy. |
| 22 | +//! - If either flag is `false`, the endpoint returns a `503 Service Unavailable` |
| 23 | +//! response, indicating that at least one database is not live and the service is |
| 24 | +//! unhealthy. |
| 25 | +//! |
| 26 | +//! ## How It Works |
| 27 | +//! |
| 28 | +//! - When the endpoint is called, it first checks the status of `LIVE_INDEX_DB` and |
| 29 | +//! `LIVE_EVENT_DB`. |
| 30 | +//! - For any database that is not live (i.e., its flag is `false`), the service attempts |
| 31 | +//! to reconnect to that database. |
| 32 | +//! - If the reconnection attempt is successful, the corresponding flag is set to `true`. |
| 33 | +//! - After attempting to reconnect, the endpoint checks the status of both flags: |
| 34 | +//! - If both `LIVE_INDEX_DB` and `LIVE_EVENT_DB` are `true`, the endpoint returns `204 |
| 35 | +//! No Content`. |
| 36 | +//! - If either flag is `false`, the endpoint returns `503 Service Unavailable`. |
| 37 | +//! |
| 38 | +//! ## Example Scenarios |
| 39 | +//! |
| 40 | +//! 1. **Both Databases Live**: |
| 41 | +//! - `LIVE_INDEX_DB` and `LIVE_EVENT_DB` are both `true`. |
| 42 | +//! - No reconnection attempts are made. |
| 43 | +//! - The endpoint returns `204 No Content`. |
| 44 | +//! |
| 45 | +//! 2. **Index DB Not Live, Reconnection Successful**: |
| 46 | +//! - `LIVE_INDEX_DB` is `false`, and `LIVE_EVENT_DB` is `true`. |
| 47 | +//! - The service attempts to reconnect to the Index DB and succeeds. |
| 48 | +//! - `LIVE_INDEX_DB` is set to `true`. |
| 49 | +//! - The endpoint returns `204 No Content`. |
| 50 | +//! |
| 51 | +//! 3. **Event DB Not Live, Reconnection Fails**: |
| 52 | +//! - `LIVE_INDEX_DB` is `true`, and `LIVE_EVENT_DB` is `false`. |
| 53 | +//! - The service attempts to reconnect to the Event DB but fails. |
| 54 | +//! - `LIVE_EVENT_DB` remains `false`. |
| 55 | +//! - The endpoint returns `503 Service Unavailable`. |
| 56 | +//! |
| 57 | +//! 4. **Both Databases Not Live, Reconnection Partially Successful**: |
| 58 | +//! - `LIVE_INDEX_DB` and `LIVE_EVENT_DB` are both `false`. |
| 59 | +//! - The service attempts to reconnect to both databases. |
| 60 | +//! - Reconnection to the Index DB succeeds (`LIVE_INDEX_DB` is set to `true`), but |
| 61 | +//! reconnection to the Event DB fails (`LIVE_EVENT_DB` remains `false`). |
| 62 | +//! - The endpoint returns `503 Service Unavailable`. |
| 63 | +//! |
| 64 | +//! ## Notes |
| 65 | +//! |
| 66 | +//! - The reconnection logic ensures that the service actively attempts to restore |
| 67 | +//! connectivity to any non-live databases, improving robustness and reliability. |
| 68 | +//! - The atomic booleans (`LIVE_INDEX_DB` and `LIVE_EVENT_DB`) are thread-safe, allowing |
| 69 | +//! concurrent access without issues. |
| 70 | +//! - This endpoint is useful for monitoring and automatically recovering from transient |
| 71 | +//! database connectivity issues, ensuring that the service remains operational whenever |
| 72 | +//! possible. |
| 73 | +//! |
| 74 | +//! This endpoint complements the initialization and readiness monitoring endpoints by |
| 75 | +//! providing ongoing connectivity checks and recovery attempts for the service's |
| 76 | +//! databases. |
2 | 77 | use poem_openapi::ApiResponse; |
3 | 78 |
|
4 | | -use crate::service::common::responses::WithErrorResponses; |
| 79 | +use crate::{ |
| 80 | + cardano::INDEXING_DB_READY_WAIT_INTERVAL, |
| 81 | + db::{ |
| 82 | + event::{establish_connection, EventDB}, |
| 83 | + index::session::CassandraSession, |
| 84 | + }, |
| 85 | + service::{ |
| 86 | + common::{responses::WithErrorResponses, types::headers::retry_after::RetryAfterOption}, |
| 87 | + utilities::health::{event_db_is_live, index_db_is_live, set_event_db_liveness}, |
| 88 | + }, |
| 89 | +}; |
5 | 90 |
|
6 | 91 | /// Endpoint responses. |
7 | 92 | #[derive(ApiResponse)] |
@@ -35,5 +120,41 @@ pub(crate) type AllResponses = WithErrorResponses<Responses>; |
35 | 120 | /// service that are ready. |
36 | 121 | #[allow(clippy::unused_async)] |
37 | 122 | pub(crate) async fn endpoint() -> AllResponses { |
38 | | - Responses::NoContent.into() |
| 123 | + // Check Event DB connection |
| 124 | + let event_db_live = event_db_is_live(); |
| 125 | + |
| 126 | + // When check fails, attempt to re-connect |
| 127 | + if !event_db_live { |
| 128 | + establish_connection(); |
| 129 | + // Re-check, and update Event DB service liveness flag |
| 130 | + set_event_db_liveness(EventDB::connection_is_ok()); |
| 131 | + }; |
| 132 | + |
| 133 | + // Check Index DB connection |
| 134 | + let index_db_live = index_db_is_live(); |
| 135 | + |
| 136 | + // When check fails, attempt to re-connect |
| 137 | + if !index_db_live { |
| 138 | + CassandraSession::init(); |
| 139 | + // Re-check connection to Indexing DB (internally updates the liveness flag) |
| 140 | + drop(CassandraSession::wait_until_ready(INDEXING_DB_READY_WAIT_INTERVAL, true).await); |
| 141 | + } |
| 142 | + |
| 143 | + let success_response = Responses::NoContent.into(); |
| 144 | + |
| 145 | + // Return 204 response if check passed initially. |
| 146 | + if index_db_live && event_db_live { |
| 147 | + return success_response; |
| 148 | + } |
| 149 | + |
| 150 | + // Otherwise, re-check, and return 204 response if all is good. |
| 151 | + if index_db_is_live() && event_db_is_live() { |
| 152 | + return success_response; |
| 153 | + } |
| 154 | + |
| 155 | + // Otherwise, return 503 response. |
| 156 | + AllResponses::service_unavailable( |
| 157 | + &anyhow::anyhow!("Service is not ready, do not send other requests."), |
| 158 | + RetryAfterOption::Default, |
| 159 | + ) |
39 | 160 | } |
0 commit comments