Skip to content

Commit 929c51e

Browse files
solssonclaude
andcommitted
cache: serve /q/health/ready alias for kkv-client onReady compat
The @yolean/kafka-keyvalue Node client's KafkaKeyValue.onReady() polls KKV_CACHE_HOST_READINESS_ENDPOINT, which defaults to `/q/health/ready` — the path the legacy Yolean/kafka-keyvalue Quarkus binary served via SmallRye-Health. mirror-v3's cache-v1 surface didn't implement that path, so every kkv consumer (live-v3, boards-v1, notifications-v1, etc.) polled forever, got 404, treated it as "not ready yet", and the consumer pod's own readiness probe never passed. Observed in production as `{"msg":"Polling cache for readiness","attempt":163}` log spam from a consumer that should have come up minutes earlier. This commit serves the alias with the same readiness gate as `/cache/v1`: - 200 OK (empty body) once CacheState::is_ready(). - 503 Service Unavailable while bootstrap is still in flight. The body stays empty rather than mimicking SmallRye-Health's `{"status":"UP",...}` JSON — the kkv Node client only checks the status code, and the lighter payload keeps the readiness probe cheap (every 3 s per consumer pod on busy fleets). Kept off the OpenAPI spec because it's a compat shim for an existing client, not a public surface mirror-v3 wants to commit to. The Quarkus `/q/...` path namespace is unlikely to collide with anything else mirror-v3 might want to add later. Tests: a 503-then-200 readiness gate test parallel to `raw_returns_503_until_caught_up`, plus a guard test that asserts `/q/health/ready` doesn't appear in the committed OpenAPI JSON. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 34610aa commit 929c51e

2 files changed

Lines changed: 108 additions & 4 deletions

File tree

‎crates/mirror-cache/src/lib.rs‎

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88
//!
99
//! The server also exposes:
1010
//!
11+
//! - `GET /q/health/ready` — drop-in compat alias for the legacy
12+
//! Quarkus kkv health endpoint. Returns `200 OK` with an empty
13+
//! body once `CacheState::is_ready()`, `503 Service Unavailable`
14+
//! otherwise. Kept off the OpenAPI spec because it's purely a
15+
//! compat shim for the existing `@yolean/kafka-keyvalue` Node
16+
//! client, whose `onReady()` polls
17+
//! `KKV_CACHE_HOST_READINESS_ENDPOINT` (default `/q/health/ready`).
1118
//! - `POST /_admin/v1/shutdown` and `POST /_admin/v1/shutdown/{exitcode}` — operator hooks.
1219
//! - `GET /openapi.json` and `GET /openapi.yaml` — auto-generated OpenAPI 3.1 spec.
1320
//! - `GET /docs` — Scalar UI rendering the spec.
1421
//!
15-
//! Readiness: every endpoint under `/cache/v1` returns `503 Service
16-
//! Unavailable` until `CacheState::is_ready()` flips to `true`
17-
//! (every registered mirror has caught up to its bootstrap
18-
//! high-watermark). The flag is sticky — once ready, always ready.
22+
//! Readiness: every endpoint under `/cache/v1` (and the
23+
//! `/q/health/ready` alias) returns `503 Service Unavailable` until
24+
//! `CacheState::is_ready()` flips to `true` (every registered mirror
25+
//! has caught up to its bootstrap high-watermark). The flag is
26+
//! sticky — once ready, always ready.
1927
2028
use std::net::SocketAddr;
2129
use std::sync::Arc;
@@ -101,6 +109,11 @@ pub fn openapi_doc() -> utoipa::openapi::OpenApi {
101109
/// `shutdown_tx` is consumed by `POST /_admin/v1/shutdown[/{exitcode}]`
102110
/// to signal the supervisor that a clean exit is requested.
103111
pub fn build_router(cache: Arc<CacheState>, shutdown_tx: oneshot::Sender<i32>) -> axum::Router {
112+
// Hold an extra clone for the /q/health/ready closure below.
113+
// The main `state.cache` is moved into the OpenAPI router via
114+
// `open_api_router(state)`, so we can't reach it from outside
115+
// afterwards.
116+
let cache_for_ready = Arc::clone(&cache);
104117
let state = AppState {
105118
cache,
106119
shutdown_tx: Arc::new(tokio::sync::Mutex::new(Some(shutdown_tx))),
@@ -126,6 +139,34 @@ pub fn build_router(cache: Arc<CacheState>, shutdown_tx: oneshot::Sender<i32>) -
126139
.into_response()
127140
}),
128141
)
142+
// Drop-in for the Yolean/kafka-keyvalue Quarkus binary's
143+
// `/q/health/ready` SmallRye-Health endpoint. The default
144+
// value of `KKV_CACHE_HOST_READINESS_ENDPOINT` in the
145+
// `@yolean/kafka-keyvalue` Node client is `/q/health/ready`;
146+
// that client's `onReady()` polls it every 3 s and gates
147+
// downstream consumer-pod readiness on a `200`. Returning
148+
// the same `200`/`503` shape here makes mirror-v3 a true
149+
// drop-in: existing consumers work unmodified, no
150+
// `KKV_CACHE_HOST_READINESS_ENDPOINT` override needed.
151+
//
152+
// Kept off the OpenAPI spec — it's purely a compat shim for
153+
// an existing client, not a public surface mirror-v3 wants
154+
// to commit to. The Quarkus `/q/...` path namespace is
155+
// unlikely to collide with anything else mirror-v3 might
156+
// want to add.
157+
.route(
158+
"/q/health/ready",
159+
axum::routing::get(move || {
160+
let cache = Arc::clone(&cache_for_ready);
161+
async move {
162+
if cache.is_ready() {
163+
StatusCode::OK.into_response()
164+
} else {
165+
StatusCode::SERVICE_UNAVAILABLE.into_response()
166+
}
167+
}
168+
}),
169+
)
129170
.merge(axum::Router::from(Scalar::with_url("/docs", api)))
130171
}
131172

‎crates/mirror-cache/tests/handlers.rs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,66 @@ async fn offsets_header_contents_match_snapshot() {
233233
assert_eq!(parsed[1]["partition"], 1);
234234
assert_eq!(parsed[1]["offset"], 3);
235235
}
236+
237+
#[tokio::test]
238+
async fn q_health_ready_returns_503_until_caught_up_then_200() {
239+
// Drop-in for the Yolean/kafka-keyvalue Quarkus binary's
240+
// `/q/health/ready` SmallRye-Health endpoint. The
241+
// `@yolean/kafka-keyvalue` Node client's `onReady()` polls it
242+
// every 3 s; consumer pods that don't see a `200` never become
243+
// Ready themselves. Same readiness gate as `/cache/v1`.
244+
let cache = Arc::new(CacheState::new());
245+
cache.register_mirror("userstate", 2); // needs offsets 0..=1
246+
let app = router_with(Arc::clone(&cache));
247+
248+
let resp = app
249+
.clone()
250+
.oneshot(
251+
Request::get("/q/health/ready")
252+
.body(Body::empty())
253+
.unwrap(),
254+
)
255+
.await
256+
.unwrap();
257+
assert_eq!(resp.status(), StatusCode::SERVICE_UNAVAILABLE);
258+
259+
cache.apply_record("userstate", &rec("userstate", 0, 0, "k0", Some(b"v0")));
260+
cache.apply_record("userstate", &rec("userstate", 0, 1, "k1", Some(b"v1")));
261+
262+
let resp = app
263+
.oneshot(
264+
Request::get("/q/health/ready")
265+
.body(Body::empty())
266+
.unwrap(),
267+
)
268+
.await
269+
.unwrap();
270+
assert_eq!(resp.status(), StatusCode::OK);
271+
// Empty body — Quarkus's SmallRye-Health returns a JSON document,
272+
// but the kkv Node client only checks the status code, so we
273+
// keep the body empty (200 implies ready, no further parsing).
274+
assert!(body_bytes(resp).await.is_empty());
275+
}
276+
277+
#[tokio::test]
278+
async fn q_health_ready_is_not_in_openapi_spec() {
279+
// Compat shim, intentionally undocumented — public surface is
280+
// `/cache/v1` and `/_admin/v1` only.
281+
let cache = Arc::new(CacheState::new());
282+
cache.register_mirror("m", 0);
283+
let app = router_with(Arc::clone(&cache));
284+
let resp = app
285+
.oneshot(
286+
Request::get("/openapi.json")
287+
.body(Body::empty())
288+
.unwrap(),
289+
)
290+
.await
291+
.unwrap();
292+
assert_eq!(resp.status(), StatusCode::OK);
293+
let body = String::from_utf8(body_bytes(resp).await).unwrap();
294+
assert!(
295+
!body.contains("/q/health/ready"),
296+
"/q/health/ready must stay off the OpenAPI spec; got: {body}"
297+
);
298+
}

0 commit comments

Comments
 (0)