From 85e1e79de3eec536a1cc2931b5e320bb7a1becb3 Mon Sep 17 00:00:00 2001 From: Jeong Min Oh Date: Tue, 24 Nov 2026 09:21:38 +0900 Subject: [PATCH] Round 41 runtime fixes: DIRECT stash keeps live entries R41-J2-MED1: the DIRECT overflow stash evicted live, unredeemed responses when the count (1024) or byte (256 MiB) budget was exceeded. The handler had already run for them, so the later fetch0 / stream0 failed with a 500 for a correct response. The stash now sweeps only TTL-expired entries; a fresh overflow is refused with a retryable 503 only past the hard ceilings (4096 entries, 1 GiB held), and a re-put over a live token is never refused. The DirectStash and StashedOverflowException javadoc document the retention and the fetch0 Integer.MIN_VALUE contract. R41-IP1-L1: try_on_shared_runtime refuses a hook once the shared runtime build has failed, so cron takes its error path instead of reporting the job accepted. Child-process test added. R41-CJ1-L1: OpenApi::merge ignores an empty webhooks map from the other side (as merge_component_map does). Test added. R41-CJ2-L1, R41-IP0-L1, R41-I4-01: doc and comment corrections only (unescape_schema_name lossy UTF-8, RequestEnvelope::headers single value, tracing test comment). Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_runtime_round41.json | 1 + crates/vespera_core/src/openapi.rs | 4 +- crates/vespera_core/src/openapi/tests.rs | 15 + crates/vespera_core/src/schema/schema_ref.rs | 4 +- crates/vespera_inprocess/src/envelope.rs | 6 + crates/vespera_inprocess/src/runtime.rs | 49 ++- .../tests/tracing_instrumentation.rs | 4 +- .../vespera_jni/src/jni_impl_direct_stash.rs | 405 +++++++++++------- .../devfive/vespera/bridge/DirectStash.java | 14 + .../bridge/StashedOverflowException.java | 3 +- 10 files changed, 332 insertions(+), 173 deletions(-) create mode 100644 .changepacks/changepack_log_runtime_round41.json diff --git a/.changepacks/changepack_log_runtime_round41.json b/.changepacks/changepack_log_runtime_round41.json new file mode 100644 index 000000000..e3c66c584 --- /dev/null +++ b/.changepacks/changepack_log_runtime_round41.json @@ -0,0 +1 @@ +{"changes":{"crates/vespera/Cargo.toml":"Patch","crates/vespera_core/Cargo.toml":"Patch","crates/vespera_inprocess/Cargo.toml":"Patch","crates/vespera_jni/Cargo.toml":"Patch","crates/vespera_macro/Cargo.toml":"Patch","libs/vespera-bridge/build.gradle.kts":"Patch","libs/vespera-bridge-gradle-plugin/build.gradle.kts":"Patch"},"note":"Runtime round 41. vespera_jni: the DIRECT overflow stash never evicts an entry before its 30 s TTL (a live response awaiting redemption was evicted under count or byte pressure, so its fetch or stream failed after the handler had run); only expired entries are swept, and a fresh overflow is refused with a retryable 503 only past the hard ceilings (4096 entries, 1 GiB). vespera_inprocess: try_on_shared_runtime refuses a hook once the shared runtime build has failed (cron reports it instead of claiming it accepted); the RequestEnvelope headers single-value limit is documented. vespera_core: OpenApi::merge ignores an empty webhooks map from the other side; unescape_schema_name doc states the lossy UTF-8 replacement. Docs only: the DirectStash and StashedOverflowException javadoc describe the retention and fetch0 contract.","date":"2026-11-24T00:00:00.000Z"} diff --git a/crates/vespera_core/src/openapi.rs b/crates/vespera_core/src/openapi.rs index 23fa756c7..fe0147032 100644 --- a/crates/vespera_core/src/openapi.rs +++ b/crates/vespera_core/src/openapi.rs @@ -651,7 +651,9 @@ impl OpenApi { // Webhooks: union by name, self-wins on a name conflict. Before the // components move below: an adopted reference reads `path_items`. - if let Some(other_webhooks) = other.webhooks { + // An empty map adds nothing (as `merge_component_map` does), so a + // `Some({})` from the other side does not turn a `None` into `Some({})`. + if let Some(other_webhooks) = non_empty_component_map(other.webhooks) { let self_webhooks = self.webhooks.get_or_insert_with(BTreeMap::new); for (name, mut item) in other_webhooks { if name_clash && let Some(path_items) = path_items { diff --git a/crates/vespera_core/src/openapi/tests.rs b/crates/vespera_core/src/openapi/tests.rs index ad3edd05d..83b73f707 100644 --- a/crates/vespera_core/src/openapi/tests.rs +++ b/crates/vespera_core/src/openapi/tests.rs @@ -828,6 +828,21 @@ fn merge_adopts_webhooks_when_self_has_none() { assert!(base.webhooks.unwrap().contains_key("hook")); } +/// R41-CJ1-L1: an empty `webhooks` map on the other side adds nothing, so a +/// `None` receiver stays `None` (no empty `webhooks` object is serialized). +#[test] +fn merge_ignores_an_empty_webhooks_map_from_other() { + let mut base = create_base_openapi(); + let mut other = create_base_openapi(); + other.webhooks = Some(BTreeMap::new()); + + base.merge(other); + + assert!(base.webhooks.is_none(), "an empty map must not be adopted"); + let value = serde_json::to_value(&base).unwrap(); + assert!(value.get("webhooks").is_none()); +} + /// Round 14: a `3.0.x` document's body is read into 3.1 shapes, so its /// version is read as `3.1.0` too (a hand-built `3.0.x` still serializes). #[rstest] diff --git a/crates/vespera_core/src/schema/schema_ref.rs b/crates/vespera_core/src/schema/schema_ref.rs index cf12238bc..e3485fd78 100644 --- a/crates/vespera_core/src/schema/schema_ref.rs +++ b/crates/vespera_core/src/schema/schema_ref.rs @@ -487,7 +487,9 @@ pub fn escape_schema_name(name: &str) -> String { /// Inverse of [`escape_schema_name`]: percent-decode, then undo the JSON /// Pointer escaping. A `%` not followed by two hex digits, or a `~` that is -/// not part of `~0` / `~1`, is kept as written. +/// not part of `~0` / `~1`, is kept as written. Decoded bytes that are not +/// valid UTF-8 are replaced lossily (each invalid sequence becomes U+FFFD), +/// so the result is always a valid `String`. #[must_use] pub fn unescape_schema_name(segment: &str) -> String { pointer_unescape(&percent_decode(segment.as_bytes())) diff --git a/crates/vespera_inprocess/src/envelope.rs b/crates/vespera_inprocess/src/envelope.rs index 8a1413350..6bbb701ad 100644 --- a/crates/vespera_inprocess/src/envelope.rs +++ b/crates/vespera_inprocess/src/envelope.rs @@ -20,6 +20,12 @@ pub struct RequestEnvelope { /// UTF-8 text arrives as the bytes `63 61 66 E9`, not `63 61 66 C3 A9`. /// A value with a `char` above `U+00FF` keeps its UTF-8 bytes. Send /// ASCII header values when the exact bytes matter. + /// + /// Single value per name: a map cannot carry a repeated header, so when a + /// name repeats the last value wins (serde keeps the last entry). The + /// binary wire API keeps every repeat (`header_read.rs`). Changing this + /// field's type would be a semver break, so the limit is documented here + /// instead. #[serde(default)] pub headers: HashMap, #[serde(default)] diff --git a/crates/vespera_inprocess/src/runtime.rs b/crates/vespera_inprocess/src/runtime.rs index e08548517..5ce662ccb 100644 --- a/crates/vespera_inprocess/src/runtime.rs +++ b/crates/vespera_inprocess/src/runtime.rs @@ -434,11 +434,15 @@ pub fn on_shared_runtime(hook: RuntimeHook) { } /// [`on_shared_runtime`], reporting whether the hook was accepted. `false` -/// once [`shutdown_runtime`] has closed the runtime: the hook is dropped and -/// never runs, so a caller that claimed its work (cron's per-job claims) -/// must release it. +/// once [`shutdown_runtime`] has closed the runtime, and `false` once the +/// runtime build has failed (no flush would ever run the hook): the hook is +/// dropped and never runs, so a caller that claimed its work (cron's per-job +/// claims) must release it. #[must_use] pub fn try_on_shared_runtime(hook: RuntimeHook) -> bool { + if matches!(RUNTIME.get(), Some(None)) { + return false; + } let accepted = lock_hooks().push(hook); if accepted { HOOKS_PENDING.store(true, Ordering::SeqCst); @@ -451,7 +455,8 @@ pub fn try_on_shared_runtime(hook: RuntimeHook) -> bool { accepted } -/// Build the runtime now when hooks wait for it. Returns whether it runs. +/// Build the runtime now when hooks wait for it. Returns whether it runs +/// (`false` as well when the build has already failed). pub fn start_runtime_for_hooks() -> bool { HOOKS_PENDING.load(Ordering::SeqCst) && runtime().is_some() } @@ -563,4 +568,40 @@ mod tests { assert!(!start_runtime_for_hooks()); assert!(!RAN.load(Ordering::SeqCst), "a refused hook never runs"); } + + /// R41-IP1-L1. A runtime build that failed (`RUNTIME` settled as `None` + /// while the gate stays open) refuses a hook: nothing would ever flush it, + /// so the caller (cron) must take its error path, not report it accepted. + /// Child process: it settles the process-global runtime slot. + #[test] + #[cfg_attr(miri, ignore = "re-executes the test binary as a child process")] + fn hooks_are_refused_after_a_failed_build() { + static RAN: AtomicBool = AtomicBool::new(false); + const CHILD_ENV: &str = "VESPERA_RUNTIME_BUILD_FAILED_CHILD"; + if std::env::var_os(CHILD_ENV).is_none() { + crate::test_child::run_isolated( + "runtime::tests::hooks_are_refused_after_a_failed_build", + CHILD_ENV, + &[], + &[], + ); + return; + } + // The build failed: the slot is settled as unbuilt, the gate is open. + assert!( + RUNTIME.set(None).is_ok(), + "fresh process: nothing was built" + ); + assert!( + !try_on_shared_runtime(Box::new(|_| RAN.store(true, Ordering::SeqCst))), + "a failed build refuses the hook" + ); + assert!( + lock_hooks().pending.is_empty(), + "no hook is queued that no flush would take" + ); + assert!(!start_runtime_for_hooks()); + assert!(runtime().is_none()); + assert!(!RAN.load(Ordering::SeqCst), "a refused hook never runs"); + } } diff --git a/crates/vespera_inprocess/tests/tracing_instrumentation.rs b/crates/vespera_inprocess/tests/tracing_instrumentation.rs index a443219bb..06e9674e8 100644 --- a/crates/vespera_inprocess/tests/tracing_instrumentation.rs +++ b/crates/vespera_inprocess/tests/tracing_instrumentation.rs @@ -365,7 +365,9 @@ fn unknown_app_is_a_rejected_request() { }); assert_eq!(status_of(&resp), 404); let span = capture.dispatch_span(); - // The raw client value of an app that did not resolve is never recorded. + // Only the span's `app` field withholds the raw client value of an app + // that did not resolve; the WARN event's detail text (the 404 "no app + // registered with name ..." message) does name it. assert_eq!(field(&span, "app"), ""); assert_eq!(field(&span, "status"), "404"); assert_eq!( diff --git a/crates/vespera_jni/src/jni_impl_direct_stash.rs b/crates/vespera_jni/src/jni_impl_direct_stash.rs index 91400fecd..091be81e6 100644 --- a/crates/vespera_jni/src/jni_impl_direct_stash.rs +++ b/crates/vespera_jni/src/jni_impl_direct_stash.rs @@ -19,14 +19,16 @@ //! never re-runs): an 8-byte big-endian token (never `0`) followed by a //! flags byte ([`FLAG_EXACT`], [`FLAG_STREAM`]). The token is redeemed with //! `fetch0` (copy into a larger buffer), `stream0` (header-first streaming) -//! or released with `discard0`. An unredeemed entry expires after -//! [`STASH_TTL`] (swept on every stash access — put, fetch, stream, -//! discard); at most [`MAX_STASHED`] entries are kept (least recently -//! stashed evicted), and their held bytes stay within [`MAX_STASHED_BYTES`] -//! except for one response larger than that budget, which is kept alone -//! (evicting it would make the handler run again). -//! The byte count covers what an entry holds in memory (its header and the -//! body bytes already read), not the unread rest of a body. +//! or released with `discard0`. An unredeemed entry is kept until it is +//! redeemed or expires after [`STASH_TTL`] (swept on every stash access — +//! put, fetch, stream, discard). A live entry is never evicted early: the +//! handler already ran for it, and evicting it would turn a correct response +//! into a failed redemption. Only a fresh stash is refused, and only past +//! the hard ceilings [`MAX_STASHED`] / [`MAX_STASHED_BYTES`] (after the +//! expired entries are swept); a refused overflow answers a retryable `503` +//! wire. One response larger than the byte ceiling is kept alone when the +//! stash is empty. The byte count covers what an entry holds in memory (its +//! header and the body bytes already read), not the unread rest of a body. //! //! # Shutdown //! @@ -65,13 +67,38 @@ pub const FLAG_EXACT: u8 = 1; pub const FLAG_STREAM: u8 = 2; /// How long an unredeemed stash entry is kept. pub const STASH_TTL: Duration = Duration::from_secs(30); -/// Maximum number of stashed responses (oldest evicted first). -pub const MAX_STASHED: usize = 1024; -/// Maximum bytes the stash holds across its entries (see -/// [`PendingDirectResponse::held_bytes`]), oldest evicted first. Matches the -/// default buffered-response cap. A single response larger than this is -/// kept alone: refusing it would force the handler to run again. -pub const MAX_STASHED_BYTES: usize = 256 * 1024 * 1024; +/// Hard ceiling on the number of stashed responses. Live entries are never +/// evicted early, so this only refuses a fresh stash (a retryable `503`); +/// it is a memory ceiling, not a working budget. +pub const MAX_STASHED: usize = 4096; +/// Hard ceiling on the bytes the stash holds across its entries (see +/// [`PendingDirectResponse::held_bytes`]). A fresh stash that would exceed +/// it is refused, unless the stash is empty (then one response is kept alone: +/// refusing it would force the handler to run again). +pub const MAX_STASHED_BYTES: usize = 1024 * 1024 * 1024; + +/// The ceilings a fresh stash must fit under ([`MAX_STASHED`] / +/// [`MAX_STASHED_BYTES`] in production; tests use small ones). +#[derive(Clone, Copy)] +struct Limits { + entries: usize, + bytes: usize, +} + +const HARD_LIMITS: Limits = Limits { + entries: MAX_STASHED, + bytes: MAX_STASHED_BYTES, +}; + +/// Why a stash was not stored. +#[derive(Debug, PartialEq, Eq)] +enum Refusal { + /// [`clear_stash`] closed the stash (shutdown): the retryable `503` + /// wire of [`runtime_unavailable_wire`]. + Closed, + /// The hard ceilings are reached: a retryable `503` (overload). + Full, +} /// One stashed response: when it was stashed, the response, and the bytes /// it was counted at (what [`Stash::held`] added for it). @@ -81,7 +108,7 @@ struct Stash { next: u64, /// Token → entry. A partially fetched response is re-stashed under its /// old token with a fresh timestamp, so key order is NOT timestamp - /// order: expiry and eviction compare timestamps. + /// order: expiry compares timestamps. entries: BTreeMap, /// Sum of the byte counts of `entries`. held: usize, @@ -108,30 +135,41 @@ impl Stash { Some((displaced_at, displaced)) } - /// Store `pending` under `token` (`None` = a fresh token), evicting the - /// expired entries and the least recently stashed ones that make room - /// within `budget`. Every entry that leaves goes to `evicted`. + /// Store `pending` under `token` (`None` = a fresh token). Expired + /// entries go to `evicted` first; then a fresh stash is refused (the + /// response comes back with the [`Refusal`]) when it would exceed + /// `limits`, while a re-put over a live token always stores (it replaces + /// its own entry, so the count does not grow). A closed stash refuses + /// everything. fn put( &mut self, token: Option, now: Instant, pending: PendingDirectResponse, - budget: usize, + limits: Limits, evicted: &mut Vec<(Instant, PendingDirectResponse)>, - ) -> u64 { + ) -> Result { + if self.closed { + return Err((Refusal::Closed, pending)); + } + self.sweep(now, evicted); let incoming = pending.held_bytes(); - let token = token.unwrap_or_else(|| { + let token = if let Some(token) = token { + // Take the live entry out first: the re-put replaces it. + evicted.extend(self.remove(token)); + token + } else { + let fits = self.entries.len() < limits.entries + && (self.held == 0 || self.held.saturating_add(incoming) <= limits.bytes); + if !fits { + return Err((Refusal::Full, pending)); + } let token = self.next; self.next = self.next.wrapping_add(1).max(1); token - }); - // A re-put displaces its live entry, so take that entry out BEFORE the - // eviction pass: the surplus check then counts only the entries that - // stay, and an unrelated oldest entry is not evicted for nothing. - evicted.extend(self.remove(token)); - self.evict(now, Some((incoming, budget)), evicted); + }; evicted.extend(self.insert(token, now, pending)); - token + Ok(token) } /// Remove `token`'s entry, releasing its counted bytes. @@ -141,16 +179,9 @@ impl Stash { Some((at, pending)) } - /// Move every entry older than [`STASH_TTL`] into `evicted`, then — when - /// `room` is `Some((incoming, budget))` — the least recently stashed - /// ones until one more entry of `incoming` bytes fits both - /// [`MAX_STASHED`] and `budget`. - fn evict( - &mut self, - now: Instant, - room: Option<(usize, usize)>, - evicted: &mut Vec<(Instant, PendingDirectResponse)>, - ) { + /// Move every entry older than [`STASH_TTL`] into `evicted`. The only + /// eviction: a live entry is never dropped before its TTL. + fn sweep(&mut self, now: Instant, evicted: &mut Vec<(Instant, PendingDirectResponse)>) { let expired: Vec = self .entries .iter() @@ -160,20 +191,6 @@ impl Stash { for token in expired { evicted.extend(self.remove(token)); } - let Some((incoming, budget)) = room else { - return; - }; - while self.entries.len() >= MAX_STASHED || self.held.saturating_add(incoming) > budget { - let Some(oldest) = self - .entries - .iter() - .min_by_key(|(_, (at, _, _))| *at) - .map(|(token, _)| *token) - else { - break; - }; - evicted.extend(self.remove(oldest)); - } } } @@ -190,32 +207,25 @@ fn lock_stash() -> std::sync::MutexGuard<'static, Stash> { .unwrap_or_else(std::sync::PoisonError::into_inner) } -/// Store `pending` under `token` (`None` = a fresh token), evicting expired -/// and surplus entries. `None` when the stash is closed (after -/// [`clear_stash`]): the response is dropped and nothing is stored. Evicted -/// responses are dropped AFTER the lock is released (dropping a body may run -/// arbitrary code). -fn stash_put(token: Option, pending: PendingDirectResponse) -> Option { - stash_put_within(token, pending, MAX_STASHED_BYTES) +/// Store `pending` under `token` (`None` = a fresh token), sweeping expired +/// entries first. `Err` when the stash is closed (after [`clear_stash`]) or +/// full (see [`Stash::put`]): the response is dropped and nothing is stored. +/// Swept and refused responses are dropped AFTER the lock is released +/// (dropping a body may run arbitrary code). +fn stash_put(token: Option, pending: PendingDirectResponse) -> Result { + stash_put_within(token, pending, HARD_LIMITS) } -/// [`stash_put`] with an explicit byte budget (tests use a small one). +/// [`stash_put`] with explicit ceilings (tests use small ones). fn stash_put_within( token: Option, pending: PendingDirectResponse, - budget: usize, -) -> Option { - let now = Instant::now(); + limits: Limits, +) -> Result { let mut evicted = Vec::new(); - let token = { - let mut stash = lock_stash(); - if stash.closed { - return None; - } - stash.put(token, now, pending, budget, &mut evicted) - }; + let outcome = lock_stash().put(token, Instant::now(), pending, limits, &mut evicted); drop(evicted); - Some(token) + outcome.map_err(|(refusal, _refused)| refusal) } /// Remove `token`'s entry (if still live) and sweep expired ones, so an @@ -225,7 +235,7 @@ fn stash_take(token: u64) -> Option { let mut evicted = Vec::new(); let removed = { let mut stash = lock_stash(); - stash.evict(Instant::now(), None, &mut evicted); + stash.sweep(Instant::now(), &mut evicted); stash.remove(token) }; drop(evicted); @@ -301,6 +311,12 @@ fn handle_flags(required: usize, exact: bool, prefers_streaming: bool) -> u8 { flags } +/// `503` wire for an overflow the stash refused because it is full (every +/// live entry is still awaiting redemption, so none is evicted). Retryable. +fn stash_full_wire() -> Vec { + vespera_inprocess::error_wire(503, "DIRECT overflow stash is full; retry the request") +} + /// Stash an overflowing response and write its handle into `out`. /// /// Called while the dispatch still holds its runtime lease (inside the @@ -308,7 +324,8 @@ fn handle_flags(required: usize, exact: bool, prefers_streaming: bool) -> u8 { /// runs: the stash refuses entries only once that cleanup has closed it (see /// the module docs). An overflow after that, from a dispatch whose drain /// timed out, is answered with the retryable `503` every other request gets, -/// not a handle to a response nothing would ever release. +/// not a handle to a response nothing would ever release. An overflow the +/// full stash refuses (see the module docs) answers a `503` too. pub fn stash_overflow(out: &mut [u8], pending: PendingDirectResponse) -> jint { let code = pending_code(&pending); let flags = pending_flags(&pending); @@ -316,8 +333,10 @@ pub fn stash_overflow(out: &mut [u8], pending: PendingDirectResponse) -> jint { // No room for a handle: the response cannot be redeemed, drop it. return code; } - let Some(token) = stash_put(None, pending) else { - return write_error_wire(out, &runtime_unavailable_wire()); + let token = match stash_put(None, pending) { + Ok(token) => token, + Err(Refusal::Closed) => return write_error_wire(out, &runtime_unavailable_wire()), + Err(Refusal::Full) => return write_error_wire(out, &stash_full_wire()), }; out[..8].copy_from_slice(&token.to_be_bytes()); out[8] = flags; @@ -456,7 +475,7 @@ pub fn fetch_into(token: jlong, out: &mut [u8]) -> jint { ResumableDirectWrite::Overflow(pending) => { let code = pending_code(&pending); let key = u64::try_from(token).unwrap_or_default(); - stash_put(Some(key), pending).map(|_| code) + stash_put(Some(key), pending).ok().map(|_| code) } } }); @@ -508,14 +527,9 @@ pub extern "system" fn Java_com_devfive_vespera_bridge_DirectStash_discard0<'loc mod tests { use super::*; - /// [`stash_put`] on a stash that is open (the tests reopen it first). + /// [`stash_put`] on the process-global stash (the tests reopen it first). fn put(token: Option, pending: PendingDirectResponse) -> u64 { - stash_put(token, pending).expect("the stash is open") - } - - /// [`stash_put_within`] on a stash that is open. - fn put_within(token: Option, pending: PendingDirectResponse, budget: usize) -> u64 { - stash_put_within(token, pending, budget).expect("the stash is open") + stash_put(token, pending).expect("the stash is open and has room") } /// A materialised `400` (malformed wire, no app needed) that overflows @@ -533,6 +547,21 @@ mod tests { } } + /// Ceilings for a test stash: `entries` count, `bytes` budget. + const fn limits(entries: usize, bytes: usize) -> Limits { + Limits { entries, bytes } + } + + /// A stash that shares no state with the process-global one. + fn local() -> Stash { + Stash { + next: 1, + entries: BTreeMap::new(), + held: 0, + closed: false, + } + } + #[test] fn exact_flag_only_for_an_unclamped_size() { let max = usize::try_from(jint::MAX).unwrap(); @@ -570,37 +599,25 @@ mod tests { assert!(stash_overflow(&mut tiny, pending()) < 0); assert_eq!(tiny, [0xAA; 4]); assert!(lock_stash().entries.is_empty()); - - // Surplus entries evict the oldest. - let first = put(None, pending()); - for _ in 0..MAX_STASHED { - let _ = put(None, pending()); - } - assert!(stash_take(first).is_none(), "the oldest entry was evicted"); - assert_eq!(lock_stash().entries.len(), MAX_STASHED); reset_stash(); - assert!(lock_stash().entries.is_empty()); - // Eviction follows the stash time, not the token: a re-stashed - // (partially fetched) low token outlives newer entries. - let resumed = put(None, pending()); - let newer = put(None, pending()); - let _ = put(Some(resumed), stash_take(resumed).expect("live")); - // Pin the stash times instead of relying on `Instant::now()` ordering: - // `newer` is stashed before the re-stash of `resumed`, so on a clock - // tie the token order must not decide the eviction. - let earlier = Instant::now() - .checked_sub(Duration::from_secs(1)) - .expect("monotonic clock far enough from its origin"); - lock_stash().entries.get_mut(&newer).expect("stashed").0 = earlier; - for _ in 0..MAX_STASHED - 1 { - let _ = put(None, pending()); + // Live entries are never evicted early. At the hard ceiling a fresh + // overflow is refused with a retryable 503 wire, and every entry still + // awaiting redemption stays redeemable (R41-J2-MED1). + let live: Vec = (0..MAX_STASHED).map(|_| put(None, pending())).collect(); + assert_eq!(stashed_count(), MAX_STASHED); + let mut out = vec![0u8; 1024]; + let code = stash_overflow(&mut out, pending()); + let len = usize::try_from(code).expect("a complete 503 wire"); + assert!(wire_status(&out[..len]).contains(r#""status":503"#)); + assert_eq!(stashed_count(), MAX_STASHED, "nothing was evicted"); + for token in live { + assert!( + stash_take(token).is_some(), + "a live entry was evicted under pressure" + ); } - assert!( - stash_take(newer).is_none(), - "least recently stashed evicted" - ); - assert!(stash_take(resumed).is_some(), "re-stashed entry kept"); + assert_eq!(stashed_count(), 0); reset_stash(); // Expired entries are swept by any access, not only by a put. @@ -617,46 +634,27 @@ mod tests { ); reset_stash(); - // Byte budget: the oldest entries go first once the held bytes would - // exceed it, and every removal releases its bytes. - let each = pending().held_bytes(); - assert!(each > 0, "a materialised wire holds its bytes"); - let budget = each * 2; - let first = put_within(None, pending(), budget); - let second = put_within(None, pending(), budget); - assert_eq!(stashed_bytes(), each * 2); - let third = put_within(None, pending(), budget); - assert!( - stash_take(first).is_none(), - "oldest evicted over the budget" - ); - assert_eq!(stashed_bytes(), each * 2); - assert!(stash_take(second).is_some()); - assert!(stash_take(third).is_some()); - assert_eq!(stashed_bytes(), 0, "removals release their bytes"); - // A re-stash over a live token displaces that entry: it leaves the // stash and its bytes are released (it is dropped after the lock). + let each = pending().held_bytes(); + assert!(each > 0, "a materialised wire holds its bytes"); let live = put(None, pending()); let _ = put(Some(live), pending()); assert_eq!(stashed_count(), 1, "the displaced entry left the stash"); assert_eq!(stashed_bytes(), each, "its bytes were released"); - reset_stash(); - - // A response over the whole budget is kept alone (refusing it would - // make its handler run again); the entry before it is evicted. - let over_first = put_within(None, pending(), 0); - let over_second = put_within(None, pending(), 0); - assert!(stash_take(over_first).is_none()); - assert!(stash_take(over_second).is_some(), "kept alone"); - assert_eq!(stashed_bytes(), 0); + assert!(stash_take(live).is_some()); + assert_eq!(stashed_bytes(), 0, "removals release their bytes"); reset_stash(); // Closed by the shutdown's cleanup: nothing is stored (a new entry or a // re-stash), and an overflow answers the retryable 503 wire instead. clear_stash(); - assert!(stash_put(None, pending()).is_none()); - assert!(stash_put(Some(7), pending()).is_none(), "re-stash refused"); + assert_eq!( + stash_put(None, pending()).err(), + Some(Refusal::Closed), + "closed refuses a new entry" + ); + assert_eq!(stash_put(Some(7), pending()).err(), Some(Refusal::Closed)); let mut out = vec![0u8; 1024]; let code = stash_overflow(&mut out, pending()); let len = usize::try_from(code).expect("a complete unavailable wire"); @@ -669,30 +667,29 @@ mod tests { reset_stash(); } - /// Round 38 (CJ3-01). Re-stashing a live token at the cap must displace - /// only that token's entry: the eviction pass counts the entries that - /// stay, so no unrelated oldest entry is evicted for the re-put. Uses a - /// local `Stash`, so it does not race the process-global one. + /// A re-put over a live token always stores (it replaces its own entry, + /// so the count does not grow), even at the ceiling; nothing else leaves. + /// Uses a local `Stash`, so it does not race the process-global one. #[test] - fn re_put_at_the_cap_keeps_every_other_entry() { - let mut stash = Stash { - next: 1, - entries: BTreeMap::new(), - held: 0, - closed: false, - }; + fn re_put_at_the_ceiling_keeps_every_other_entry() { + let mut stash = local(); let now = Instant::now(); + let caps = limits(3, usize::MAX); let mut evicted = Vec::new(); - let tokens: Vec = (0..MAX_STASHED) - .map(|_| stash.put(None, now, pending(), MAX_STASHED_BYTES, &mut evicted)) + let tokens: Vec = (0..3) + .map(|_| { + stash + .put(None, now, pending(), caps, &mut evicted) + .expect("room") + }) .collect(); - assert!(evicted.is_empty(), "filling to the cap evicts nothing"); - - let live = *tokens.last().expect("the cap is not empty"); - let token = stash.put(Some(live), now, pending(), MAX_STASHED_BYTES, &mut evicted); + let live = *tokens.last().expect("the ceiling is not empty"); + let token = stash + .put(Some(live), now, pending(), caps, &mut evicted) + .expect("a re-put is never refused"); assert_eq!(token, live, "the re-put keeps its token"); assert_eq!(evicted.len(), 1, "only the displaced live entry leaves"); - assert_eq!(stash.entries.len(), MAX_STASHED, "no entry was lost"); + assert_eq!(stash.entries.len(), 3, "no entry was lost"); for token in &tokens { assert!( stash.entries.contains_key(token), @@ -701,17 +698,95 @@ mod tests { } } + /// The count ceiling refuses a fresh stash (`Full`) and hands the response + /// back unstored; a live entry is never displaced to make room. + #[test] + fn full_stash_refuses_a_fresh_entry_and_keeps_the_live_ones() { + let mut stash = local(); + let now = Instant::now(); + let caps = limits(2, usize::MAX); + let mut evicted = Vec::new(); + let first = stash + .put(None, now, pending(), caps, &mut evicted) + .expect("room"); + let second = stash + .put(None, now, pending(), caps, &mut evicted) + .expect("room"); + let (refusal, refused) = stash + .put(None, now, pending(), caps, &mut evicted) + .expect_err("the ceiling is reached"); + assert_eq!(refusal, Refusal::Full); + drop(refused); + assert!(evicted.is_empty(), "nothing live was evicted"); + assert!(stash.entries.contains_key(&first)); + assert!(stash.entries.contains_key(&second)); + } + + /// The byte ceiling refuses a fresh stash that would exceed it, while a + /// single response is kept alone when the stash is empty. + #[test] + fn byte_ceiling_refuses_a_fresh_entry_and_keeps_one_alone() { + let mut stash = local(); + let now = Instant::now(); + let mut evicted = Vec::new(); + let each = pending().held_bytes(); + assert!(each > 0, "a materialised wire holds its bytes"); + let caps = limits(usize::MAX, each * 2); + stash + .put(None, now, pending(), caps, &mut evicted) + .expect("room"); + stash + .put(None, now, pending(), caps, &mut evicted) + .expect("room"); + assert_eq!(stash.held, each * 2); + let (refusal, _refused) = stash + .put(None, now, pending(), caps, &mut evicted) + .expect_err("over the byte ceiling"); + assert_eq!(refusal, Refusal::Full); + assert_eq!(stash.entries.len(), 2, "live entries kept"); + assert_eq!(stash.held, each * 2, "a refused entry counts nothing"); + + let mut alone = local(); + let zero = limits(usize::MAX, 0); + alone + .put(None, now, pending(), zero, &mut evicted) + .expect("kept alone in an empty stash"); + assert!( + alone.put(None, now, pending(), zero, &mut evicted).is_err(), + "not alone any more" + ); + assert!(evicted.is_empty()); + } + + /// Expired entries are swept by a put, so they make room under the count + /// ceiling; they are evicted, live ones never are. + #[test] + fn expired_entries_make_room_under_the_ceiling() { + let mut stash = local(); + let now = Instant::now(); + let caps = limits(1, usize::MAX); + let mut evicted = Vec::new(); + let stale = now + .checked_sub(STASH_TTL + Duration::from_secs(1)) + .expect("monotonic clock far enough from its origin"); + let old = stash + .put(None, now, pending(), caps, &mut evicted) + .expect("room"); + stash.entries.get_mut(&old).expect("stashed").0 = stale; + let fresh = stash + .put(None, now, pending(), caps, &mut evicted) + .expect("the expired entry was swept, so there is room"); + assert_eq!(evicted.len(), 1, "the expired entry left"); + assert!(!stash.entries.contains_key(&old)); + assert!(stash.entries.contains_key(&fresh)); + } + /// `Stash::insert` hands the displaced entry back (the caller drops it /// outside the lock) and keeps the held-byte count exact. Uses a local /// `Stash`, so it does not race the process-global one. #[test] fn insert_returns_the_displaced_entry() { - let mut stash = Stash { - next: 1, - entries: BTreeMap::new(), - held: 0, - closed: false, - }; + let mut stash = local(); let now = Instant::now(); let each = pending().held_bytes(); assert!(stash.insert(5, now, pending()).is_none(), "fresh token"); diff --git a/libs/vespera-bridge/src/main/java/com/devfive/vespera/bridge/DirectStash.java b/libs/vespera-bridge/src/main/java/com/devfive/vespera/bridge/DirectStash.java index a6701f55e..09ff5f8bc 100644 --- a/libs/vespera-bridge/src/main/java/com/devfive/vespera/bridge/DirectStash.java +++ b/libs/vespera-bridge/src/main/java/com/devfive/vespera/bridge/DirectStash.java @@ -19,6 +19,20 @@ * stashed under the same token), {@link #stream0} (header-first streaming, * same contract as {@link VesperaBridge#dispatchStreamingWithHeader}) or * released by {@link #discard0}. An unredeemed entry expires after 30 s. + * + *

Retention: the native stash never evicts an entry before its 30 s expiry + * (a handler already ran for it, so evicting it would fail a correct response). + * Only a fresh overflow is refused, and only past the native hard + * ceilings — 4096 held entries or 1 GiB held (one response larger than the + * byte ceiling is kept alone when nothing else is held). A refused overflow + * is answered with a complete, retryable {@code 503} wire instead of a handle, + * so a caller never receives a handle that cannot be redeemed. + * + *

{@link #fetch0} returns {@code Integer.MIN_VALUE} (the + * {@code dispatchDirect} "unrepresentable" sentinel) only while the runtime is + * live and the token is unknown: it expired, was discarded, or was already + * redeemed. After runtime shutdown an unknown token answers the + * runtime-unavailable {@code 503} wire instead. */ final class DirectStash { /** Bytes of the overflow handle written into {@code out}. */ diff --git a/libs/vespera-bridge/src/main/java/com/devfive/vespera/bridge/StashedOverflowException.java b/libs/vespera-bridge/src/main/java/com/devfive/vespera/bridge/StashedOverflowException.java index b157a29b6..c82834bf6 100644 --- a/libs/vespera-bridge/src/main/java/com/devfive/vespera/bridge/StashedOverflowException.java +++ b/libs/vespera-bridge/src/main/java/com/devfive/vespera/bridge/StashedOverflowException.java @@ -8,7 +8,8 @@ * (see {@link DirectStash}): it can be streamed with {@link #streamTo} — the * handler is not re-run — or released with {@link #discard}. * Exactly one of the two must be called (an unredeemed entry expires after - * 30 s). Raised internally by the pooled DIRECT path when the response is + * 30 s; the native stash never evicts it earlier, see {@link DirectStash}). + * Raised internally by the pooled DIRECT path when the response is * an event stream or exceeds the pooled buffer cap; public callers only see * the plain {@link VesperaBridge.BufferTooSmallException} supertype. */