nmbrs_runtime/resource_pool.rs
1// Copyright 2024-2026 Jonathan Shook
2// SPDX-License-Identifier: Apache-2.0
3
4//! Generic resource lifecycle and sharing pool — SRD-35 Push A.
5//!
6//! ## What this module provides
7//!
8//! Trait surface and runtime structure for sharing
9//! long-lived resources (CQL sessions, HTTP clients, …)
10//! across phases of one session, decoupled from per-phase
11//! adapter shells. The contract is intentionally generic —
12//! nothing here is CQL-specific.
13//!
14//! ### Core types
15//!
16//! - [`SharedResource`] — the trait every poolable resource
17//! implements. Carries [`SharedResource::can_share`]
18//! (capability declaration: thread-safe + designed for
19//! sharing) and [`SharedResource::can_support_more_load`] (driver's
20//! runtime judgement that another instance would relieve
21//! knowable, substantial contention) — see the SRD-35
22//! load-bearing rules.
23//! - [`ResourceKey`] — value-equality identity. Two keys
24//! compare equal iff their adapter name and fields match
25//! exactly. No derived hash function; the contract is
26//! structural.
27//! - [`ShareCapability`] — strictest sharing the resource
28//! *type* tolerates. Read by the pool at planning time
29//! without an instance.
30//! - [`ResourceSharePolicy`] — user-elevatable isolation
31//! policy. Must satisfy `policy >= capability_floor`.
32//! - [`ResourcePool`] — owns the `(ResourceKey, generation)
33//! → Entry` map, tracks refcounts, emits lifecycle events.
34//!
35//! ## Push A scope
36//!
37//! Push A lays the trait foundation, the pool data
38//! structure, the lifecycle event emission, and a
39//! [`LegacyAdapterResource`] shim that wraps the existing
40//! `Arc<dyn DriverAdapter>` factories under `PerPhase`
41//! policy. Behaviour is byte-identical to today (a fresh
42//! resource per phase) but every phase boundary now emits
43//! the full `resource.{attach,init,detach,close}` event
44//! sequence.
45//!
46//! Push B will migrate the CQL adapter to `Shared` policy
47//! by splitting `CqlAdapter` into a per-phase shell + a
48//! `CassDriverInstance` that implements `SharedResource`
49//! directly with real async `close()`. The pool's
50//! multi-generation machinery is in place but exercised
51//! only by the synthetic mock resource in tests until then.
52
53use std::any::Any;
54use std::collections::{BTreeMap, HashMap};
55use std::future::Future;
56use std::pin::Pin;
57use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
58use std::sync::{Arc, Mutex, Weak};
59use std::time::Instant;
60
61use crate::adapter::DriverAdapter;
62use crate::observer::LogLevel;
63
64// =================================================================
65// Resource key — value-equality identity
66// =================================================================
67
68/// Structural value identity for a shared resource.
69///
70/// Two adapters that build equal `ResourceKey`s receive the
71/// same instance under `Shared` policy. The internal
72/// `BTreeMap` is sorted so equality is deterministic, and
73/// `Hash` is derived structurally for `HashMap` lookup. The
74/// pool never asks adapters to produce a "key digest"; the
75/// contract is value equality on the public type.
76///
77/// Adapters populate `fields` with **only** the params that
78/// distinguish one instance from another. Per-statement and
79/// per-phase shaping (timeouts, trace rates, dynamic
80/// controls) MUST NOT appear in the key — see SRD-35
81/// §"Instance-shaping vs shell-shaping params".
82#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
83pub struct ResourceKey {
84 /// Adapter name (`"cql"`, `"http"`, …) — the
85 /// adapter-name surface, not the engine-specific driver
86 /// identifier (which the adapter folds into the fields
87 /// when meaningful).
88 pub adapter: String,
89 /// Identity-bearing param values, sorted so equality is
90 /// independent of insertion order.
91 pub fields: BTreeMap<String, String>,
92}
93
94impl ResourceKey {
95 /// Construct a key for the given adapter with no fields
96 /// yet. Chain [`Self::with`] to populate.
97 pub fn new(adapter: impl Into<String>) -> Self {
98 Self {
99 adapter: adapter.into(),
100 fields: BTreeMap::new(),
101 }
102 }
103
104 /// Set or replace the value for one identity-bearing
105 /// field, returning `self` for chaining.
106 pub fn with(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
107 self.fields.insert(key.into(), value.into());
108 self
109 }
110
111 /// Render the key for a single line of log output
112 /// (`adapter{k1=v1,k2=v2}`). Stable shape — the
113 /// lifecycle-event surface relies on this format being
114 /// consistent across log consumers.
115 pub fn fmt_for_log(&self) -> String {
116 let mut s = String::with_capacity(64);
117 s.push_str(&self.adapter);
118 s.push('{');
119 let mut first = true;
120 for (k, v) in &self.fields {
121 if !first {
122 s.push(',');
123 }
124 first = false;
125 s.push_str(k);
126 s.push('=');
127 // Don't log secrets. Adapters can prefix
128 // sensitive fields with `_secret_` to redact.
129 if k.starts_with("_secret_") || k == "password" {
130 s.push_str("***");
131 } else {
132 s.push_str(v);
133 }
134 }
135 s.push('}');
136 s
137 }
138
139 /// SRD-104 — the **canonical fingerprint rendering** of this key to a
140 /// stable string, used as the accessor lookup key
141 /// ([`polydat::ResourceAccessor::lookup`]). Unlike [`Self::fmt_for_log`]
142 /// this rendering is **identity-preserving**: two keys that differ
143 /// only in an identity-bearing field (e.g. `password`) MUST render to
144 /// different strings or they would collide in the accessor. A secret
145 /// field (`password`, `_secret_*`) contributes a SHA-256 digest of its
146 /// value rather than the value itself, so the fingerprint keeps its
147 /// identity without ever carrying the cleartext into a kernel binding
148 /// or a diagnostic. The `BTreeMap` makes the field order
149 /// deterministic, so equal keys always render identically.
150 ///
151 /// This is the single rendering shared by both sides of the SRD-104
152 /// bridge: the resource pool renders each live entry's key with it when
153 /// resolving a lookup, and a consumer node builds the same string from
154 /// the fingerprint in scope. Shape: `adapter{k1=v1,k2=v2}`.
155 pub fn render_key(&self) -> String {
156 let mut s = String::with_capacity(64);
157 s.push_str(&self.adapter);
158 s.push('{');
159 let mut first = true;
160 for (k, v) in &self.fields {
161 if !first {
162 s.push(',');
163 }
164 first = false;
165 s.push_str(k);
166 s.push('=');
167 if k.starts_with("_secret_") || k == "password" {
168 use sha2::{Digest, Sha256};
169 let digest = Sha256::digest(v.as_bytes());
170 s.push_str("sha256:");
171 for byte in &digest[..8] {
172 s.push_str(&format!("{byte:02x}"));
173 }
174 } else {
175 s.push_str(v);
176 }
177 }
178 s.push('}');
179 s
180 }
181}
182
183// =================================================================
184// Capability and policy
185// =================================================================
186
187/// Strongest sharing the resource *type* can tolerate.
188/// Declared by the factory at registration time so the pool
189/// can plan the entry layout without configuring or
190/// instantiating any resource. The live instance's
191/// [`SharedResource::can_share`] is a runtime safety net
192/// that aborts the session if it disagrees with this
193/// type-level declaration.
194///
195/// The variants order from most-shared to most-isolated;
196/// `PartialOrd` semantics let the pool check policy ≥
197/// capability with a single comparison.
198#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
199pub enum ShareCapability {
200 /// Safe to share one instance across every adapter
201 /// shell in the workload that produces an equal key.
202 /// CQL, HTTP-with-pool, OpenAPI typically.
203 Shared,
204 /// One instance per scenario subtree. Use when state
205 /// changes during the run that mustn't bleed across
206 /// scenarios (auth tokens, schema versions per
207 /// scenario).
208 PerScenario,
209 /// One instance per phase. Use when each phase
210 /// legitimately wants a fresh state object.
211 PerPhase,
212 /// One instance per fiber. Use only when the resource
213 /// type is `Send` but not `Sync`, or when the
214 /// underlying library mandates single-threaded use.
215 PerFiber,
216}
217
218/// User-selectable isolation. Must satisfy
219/// `policy >= capability_floor`.
220#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
221pub enum ResourceSharePolicy {
222 Shared,
223 PerScenario,
224 PerPhase,
225 PerFiber,
226}
227
228impl ResourceSharePolicy {
229 /// Map a user-string (CLI / workload param) to the
230 /// enum. Used by the SRD-04 umbrella surface
231 /// (`--resource-share <adapter>:<policy>`).
232 pub fn parse(s: &str) -> Result<Self, String> {
233 match s.trim().to_ascii_lowercase().as_str() {
234 "shared" => Ok(Self::Shared),
235 "per-scenario" | "per_scenario" => Ok(Self::PerScenario),
236 "per-phase" | "per_phase" => Ok(Self::PerPhase),
237 "per-fiber" | "per_fiber" => Ok(Self::PerFiber),
238 other => Err(format!(
239 "unknown resource-share policy '{other}' \
240 (expected: shared, per-scenario, per-phase, per-fiber)"
241 )),
242 }
243 }
244}
245
246impl std::fmt::Display for ResourceSharePolicy {
247 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
248 let s = match self {
249 Self::Shared => "shared",
250 Self::PerScenario => "per-scenario",
251 Self::PerPhase => "per-phase",
252 Self::PerFiber => "per-fiber",
253 };
254 f.write_str(s)
255 }
256}
257
258/// Default sharing policy — the strongest sharing the
259/// capability allows. Used when the user gives no override.
260pub fn default_policy_for(cap: ShareCapability) -> ResourceSharePolicy {
261 match cap {
262 ShareCapability::Shared => ResourceSharePolicy::Shared,
263 ShareCapability::PerScenario => ResourceSharePolicy::PerScenario,
264 ShareCapability::PerPhase => ResourceSharePolicy::PerPhase,
265 ShareCapability::PerFiber => ResourceSharePolicy::PerFiber,
266 }
267}
268
269/// Minimum policy compatible with the given capability.
270/// Used by the pool to validate user-supplied policies at
271/// session start.
272pub fn capability_floor(cap: ShareCapability) -> ResourceSharePolicy {
273 match cap {
274 ShareCapability::Shared => ResourceSharePolicy::Shared,
275 ShareCapability::PerScenario => ResourceSharePolicy::PerScenario,
276 ShareCapability::PerPhase => ResourceSharePolicy::PerPhase,
277 ShareCapability::PerFiber => ResourceSharePolicy::PerFiber,
278 }
279}
280
281// =================================================================
282// SharedResource trait
283// =================================================================
284
285/// One async-init-and-close return type used by the
286/// trait. Returns a `Send` future so the pool can hold it
287/// across `.await` points without runtime fuss.
288pub type ResourceFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
289
290/// The trait every poolable resource implements.
291///
292/// `Send + Sync + 'static` is required so the pool can hold
293/// `Arc<dyn SharedResource>` and clone it across fibers.
294///
295/// Default implementations make the trivial case
296/// (always-shareable, never-saturated, no-op init/close)
297/// boilerplate-free — only `resource_key()` is required to
298/// override.
299pub trait SharedResource: Send + Sync + 'static {
300 /// The structural identity of this resource.
301 fn resource_key(&self) -> &ResourceKey;
302
303 /// **Capability** — does this instance support being
304 /// shared by multiple adapter shells concurrently?
305 /// Default `true`. See SRD-35 §"Two trait methods on
306 /// the live resource".
307 fn can_share(&self) -> bool {
308 true
309 }
310
311 /// **Live capacity** — can this instance accept *another*
312 /// concurrent caller right now without substantial
313 /// contention?
314 ///
315 /// `true` (default) → "yes, route the next attach to me;
316 /// I have capacity." `false` → "no, I'm saturated; the
317 /// pool should spawn a sibling for the new attach."
318 /// Parallel naming to `can_share()`: `can_share()` says
319 /// *whether* sharing is structurally possible at all;
320 /// `can_support_more_load()` says *whether one more
321 /// caller is OK at this moment*.
322 ///
323 /// Drivers that override MUST document their decision
324 /// criterion in the type docstring (the operator reading
325 /// a `reason=capacity-declined` lifecycle event needs to
326 /// be able to interpret what triggered it).
327 /// MUST be cheap (atomic read or short metric query);
328 /// MUST NOT block. SRD-35 §"Validity rules" requires
329 /// the body to reflect *current* in-flight load — never
330 /// historical/peak/lifetime metrics. The pool's guard in
331 /// `needs_sibling_spawn` catches the historical-state
332 /// failure mode (driver returns `false` at quiescence)
333 /// and emits a `Warn` event so operators can spot the
334 /// driver bug.
335 fn can_support_more_load(&self) -> bool {
336 true
337 }
338
339 /// Optional async init beyond what construction
340 /// already did. Called by the pool on first attach for
341 /// a key, exactly once per `(key, generation)`.
342 fn init(&self) -> ResourceFuture<'_, Result<(), String>> {
343 Box::pin(async { Ok(()) })
344 }
345
346 /// Symmetric teardown. Called when the entry's
347 /// refcount hits zero. MUST block until network /
348 /// kernel resources are *actually released* — no
349 /// async-Drop races, no sockets in TIME_WAIT being
350 /// counted as closed, no libuv worker thread races.
351 fn close(self: Arc<Self>) -> ResourceFuture<'static, Result<(), String>> {
352 Box::pin(async { Ok(()) })
353 }
354
355 /// Bridge for the Push A legacy-adapter shim. Default
356 /// returns `None`; only [`LegacyAdapterResource`]
357 /// overrides it to surface its wrapped `DriverAdapter`
358 /// handle. Push B retires this entirely once each
359 /// adapter has its own `SharedResource` impl with no
360 /// hidden `DriverAdapter` inside.
361 ///
362 /// Real `SharedResource` implementations should leave
363 /// this at the default — the pool layer is the right
364 /// place to surface domain-specific handles, not the
365 /// trait surface.
366 fn as_legacy_adapter(&self) -> Option<Arc<dyn DriverAdapter>> {
367 None
368 }
369
370 /// SRD-104 — the resource's **accessor payload**: a type-erased handle
371 /// (`Arc<dyn Any + Send + Sync>`) a kernel node can obtain by
372 /// fingerprint through its kernel tree's resource scope. The pool stores it on
373 /// the entry right after a successful init, and its
374 /// [`polydat::ResourceAccessor`] impl hands out clones. Default `None`
375 /// — a resource opts in only when it wants kernels to reach a live
376 /// handle (the first consumer is a CQL session handle, SRD-103); no
377 /// existing adapter is affected.
378 fn accessor_payload(&self) -> Option<Arc<dyn Any + Send + Sync>> {
379 None
380 }
381}
382
383// =================================================================
384// Internal entry — one per (key, generation)
385// =================================================================
386
387/// Pool entry tracking one resource instance and its
388/// refcount state.
389struct Entry {
390 key: ResourceKey,
391 policy: ResourceSharePolicy,
392 generation: usize,
393
394 /// The lazily-constructed resource. `tokio::sync::OnceCell`
395 /// would work too; we use a `Mutex<Option<…>>` because
396 /// Push A doesn't yet need cross-fiber concurrent first-
397 /// attach contention (the pool's outer mutex serialises),
398 /// and the simpler shape avoids pulling tokio into the
399 /// type signature.
400 resource: Mutex<Option<Arc<dyn SharedResource>>>,
401
402 /// Set when init failed. Subsequent attaches return the
403 /// cached error rather than retrying.
404 poisoned: AtomicBool,
405 /// Cached init error message, populated alongside
406 /// `poisoned`.
407 init_error: Mutex<Option<String>>,
408
409 /// Predicted future attaches that haven't yet landed
410 /// on this entry. SRD-35 Push D's pre-map walker
411 /// (`pre_map_pending_uses`) seeds this from the
412 /// scenario tree at session bootstrap; each `attach`
413 /// decrements eagerly. The pool's close trigger fires
414 /// the moment `pending_uses == 0 && live_attaches ==
415 /// 0`, releasing the resource the instant its last
416 /// predicted user is done.
417 pending_uses: AtomicUsize,
418 /// Currently in-use shells. Decremented on detach.
419 live_attaches: AtomicUsize,
420
421 /// Wall-clock instant at which `init()` started — used
422 /// to compute `elapsed_ms` on `init.completed` /
423 /// `init.failed` events.
424 init_started_at: Mutex<Option<Instant>>,
425
426 /// SRD-104 — the resource's type-erased **accessor payload**, populated
427 /// right after a successful init from [`SharedResource::accessor_payload`]
428 /// and returned by the pool's [`polydat::ResourceAccessor`] impl. `None`
429 /// until init succeeds, and `None` for resources that don't opt in (the
430 /// trait default). The payload lives and dies with the entry — no second
431 /// store.
432 accessor_payload: Mutex<Option<Arc<dyn Any + Send + Sync>>>,
433}
434
435impl Entry {
436 fn new(key: ResourceKey, policy: ResourceSharePolicy, generation: usize) -> Self {
437 Self {
438 key,
439 policy,
440 generation,
441 resource: Mutex::new(None),
442 poisoned: AtomicBool::new(false),
443 init_error: Mutex::new(None),
444 pending_uses: AtomicUsize::new(0),
445 live_attaches: AtomicUsize::new(0),
446 init_started_at: Mutex::new(None),
447 accessor_payload: Mutex::new(None),
448 }
449 }
450}
451
452// =================================================================
453// `can_support_more_load()` validity guard
454// =================================================================
455
456/// Decide whether the pool should spawn a sibling
457/// generation for this attach.
458///
459/// Returns `true` only when the active resource genuinely
460/// can't take another caller (`can_support_more_load() ==
461/// false`) AND there's actual concurrent load
462/// (`live_attaches > 0`). Both halves are required:
463///
464/// - `can_support_more_load() == true` → existing instance
465/// has capacity; route the attach there. No spawn.
466/// - `can_support_more_load() == false` AND `live > 0` →
467/// genuine saturation; spawn a sibling.
468/// - `can_support_more_load() == false` AND `live == 0` →
469/// **driver bug**: the resource refused a new caller
470/// while sitting completely idle. SRD-35 §"Validity
471/// rules" forbids this — `can_support_more_load()` must
472/// reflect *current* in-flight load, never historical /
473/// peak / lifetime state. The classic failure mode is a
474/// ring buffer or moving-average metric that filled
475/// during an earlier phase and hasn't decayed; reading
476/// it at quiescence falsely reports saturation. The
477/// guard logs a `Warn` so the operator sees the driver
478/// bug, and routes the attach to the existing idle
479/// instance instead of wasting a sibling.
480///
481/// Push A/B do not yet wire the sibling-spawn path, but
482/// this helper lands now so Push D's spawn logic uses it
483/// from day one. Tests exercise it directly.
484#[allow(dead_code)]
485fn needs_sibling_spawn(entry: &Entry, resource: &dyn SharedResource) -> bool {
486 if resource.can_support_more_load() {
487 // Existing generation has capacity — route there.
488 return false;
489 }
490 let live = entry.live_attaches.load(Ordering::Acquire);
491 if live == 0 {
492 // Driver bug: refused a new attach at quiescence.
493 // Log once per occurrence with the resource key so
494 // the operator can attribute it. Don't spawn — the
495 // existing idle generation is the right answer.
496 crate::diag!(
497 LogLevel::Warn,
498 "{EVENT_FAMILY}.share.suppressed key={} generation={} \
499 reason=quiescent-decline \
500 note=can_support_more_load() returned false with live_attaches=0; \
501 driver may be reading historical state (filled ring buffer, \
502 moving-average that hasn't decayed) instead of in-flight load \
503 (SRD-35 §\"Validity rules\")",
504 entry.key.fmt_for_log(),
505 entry.generation,
506 );
507 return false;
508 }
509 // Genuine saturation: refusing under live load.
510 true
511}
512
513// =================================================================
514// Lifecycle event emission
515// =================================================================
516
517/// Family prefix on every event line emitted by the pool.
518/// Enables log-side filtering with one substring.
519const EVENT_FAMILY: &str = "resource";
520
521/// Emit one lifecycle event line. All events carry `key`,
522/// `generation`, and `policy`; per-event extra fields are
523/// appended to the formatted suffix.
524fn emit_event(level: LogLevel, name: &str, entry: &Entry, extra: &str) {
525 let suffix = if extra.is_empty() {
526 String::new()
527 } else {
528 format!(" {extra}")
529 };
530 crate::diag!(
531 level,
532 "{EVENT_FAMILY}.{name} key={} generation={} policy={}{suffix}",
533 entry.key.fmt_for_log(),
534 entry.generation,
535 entry.policy,
536 );
537}
538
539// =================================================================
540// Resource pool
541// =================================================================
542
543/// One `(ResourceKey, generation)` map for the lifetime of
544/// one session. Owns lazy init, refcount transitions, and
545/// the close trigger.
546///
547/// The pool is `Send + Sync` and intended to be held in an
548/// `Arc` from the session root (executor's `ExecCtx` or
549/// equivalent). Each phase activation calls
550/// [`attach`] for the adapter names it needs;
551/// the returned [`AttachGuard`] holds a clone of the
552/// per-phase `DriverAdapter` shell and detaches on drop.
553pub struct ResourcePool {
554 inner: Mutex<PoolInner>,
555}
556
557struct PoolInner {
558 /// Active entries keyed by `(ResourceKey, generation)`.
559 /// We use a `Vec<Arc<Entry>>` for ordered iteration;
560 /// the `HashMap` is the lookup index.
561 entries_by_key: HashMap<(ResourceKey, usize), Arc<Entry>>,
562
563 /// Cumulative attach count — used for diagnostic
564 /// post-mortems and the `nmbrs describe drivers` output
565 /// that's deferred to a later SRD.
566 total_attaches: usize,
567}
568
569impl ResourcePool {
570 pub fn new() -> Self {
571 Self {
572 inner: Mutex::new(PoolInner {
573 entries_by_key: HashMap::new(),
574 total_attaches: 0,
575 }),
576 }
577 }
578
579 /// Total attach count since pool construction. Useful
580 /// for tests and post-mortem diagnostics.
581 pub fn total_attaches(&self) -> usize {
582 self.inner
583 .lock()
584 .unwrap_or_else(|e| e.into_inner())
585 .total_attaches
586 }
587
588 /// Number of distinct entries currently live in the
589 /// pool. One per `(key, generation)` that's been
590 /// attached at least once and not yet fully drained.
591 pub fn live_entries(&self) -> usize {
592 self.inner
593 .lock()
594 .unwrap_or_else(|e| e.into_inner())
595 .entries_by_key
596 .len()
597 }
598
599 /// Drain every live entry, awaiting each resource's
600 /// `close()` and removing the entry from the map. Call
601 /// at session end so `Shared`-policy entries — which
602 /// the per-attach detach intentionally keeps alive
603 /// across phases — release their network resources
604 /// before the process exits.
605 ///
606 /// Emits `resource.close.started reason=session-end`
607 /// per entry, then `resource.close.completed` /
608 /// `resource.close.failed` after each resource's
609 /// `close()` future resolves. Returns `Ok(())` even if
610 /// individual close calls fail — the failures are
611 /// logged and accounted, but the pool always finishes
612 /// the drain so the executor can move on.
613 pub async fn shutdown(self: &Arc<Self>) {
614 // Snapshot the entries, then drop the lock so the
615 // close futures can run unblocked.
616 let entries: Vec<Arc<Entry>> = {
617 let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
618 inner.entries_by_key.values().cloned().collect()
619 };
620 for entry in entries {
621 // Take the resource out of the slot. If it's
622 // already gone (concurrent close, or never
623 // realised after init failure), skip.
624 let resource = {
625 let mut slot = entry.resource.lock().unwrap_or_else(|e| e.into_inner());
626 slot.take()
627 };
628 let Some(resource) = resource else {
629 self.remove_entry(&entry.key, entry.generation);
630 continue;
631 };
632 let live = entry.live_attaches.load(Ordering::Acquire);
633 let pending = entry.pending_uses.load(Ordering::Acquire);
634 // Note residual counts on the started event so
635 // operators can spot bugs in the pre-map walker
636 // when it lands (Push D): if `pending > 0` at
637 // session end, the walker over-predicted; if
638 // `live > 0`, an attach guard wasn't dropped.
639 emit_event(
640 LogLevel::Debug,
641 "close.started",
642 &entry,
643 &format!("reason=session-end live={live} pending={pending}"),
644 );
645 let started_at = Instant::now();
646 let result = resource.close().await;
647 let elapsed_ms = started_at.elapsed().as_millis() as u64;
648 match result {
649 Ok(()) => emit_event(
650 LogLevel::Debug,
651 "close.completed",
652 &entry,
653 &format!("elapsed_ms={elapsed_ms}"),
654 ),
655 Err(ref e) => emit_event(
656 LogLevel::Warn,
657 "close.failed",
658 &entry,
659 &format!("elapsed_ms={elapsed_ms} error={e:?}"),
660 ),
661 }
662 self.remove_entry(&entry.key, entry.generation);
663 }
664 }
665
666 /// SRD-35 Push D: declare a predicted future attach for
667 /// `key` under `policy`. Called by the pre-map walker
668 /// once per phase that will eventually attach this key
669 /// — the per-key counter accumulates so a `Shared`
670 /// entry attached by 50 phases starts the run with
671 /// `pending_uses = 50`. Each `attach` decrements
672 /// eagerly; the entry's close trigger fires the moment
673 /// `pending == 0 && live == 0`, releasing the resource
674 /// the instant its last user is done rather than holding
675 /// it until session end.
676 ///
677 /// Idempotent for a given `(key, policy)` — the entry is
678 /// created on first call, then increments on subsequent
679 /// calls. The pre-map walker is the only declared caller;
680 /// adapters never invoke this directly.
681 pub fn declare_pending_use(&self, key: ResourceKey, policy: ResourceSharePolicy) {
682 let entry = self.get_or_create_entry(key, policy, 0);
683 entry.pending_uses.fetch_add(1, Ordering::AcqRel);
684 }
685
686 /// SRD-35 Push D: explicit per-key `pending_uses`
687 /// decrement, mirroring [`Self::declare_pending_use`].
688 /// Most callers don't need this — the [`AttachGuard`]
689 /// detach path already decrements via the `attach`-time
690 /// "slot consumed" semantic, and the close trigger
691 /// observes the resulting `pending == 0 && live == 0`
692 /// invariant. Provided so external callers (tests, future
693 /// pre-map invalidation paths) can adjust the counter
694 /// explicitly. Saturating at 0 — calling this on a key
695 /// with no pending uses left is a no-op, not an
696 /// underflow.
697 ///
698 /// Returns `true` when the call drove `pending` to 0
699 /// AND `live` was already 0 (so the entry was eligible
700 /// for close). Doesn't itself trigger close — the close
701 /// path runs on detach. Tests that exercise the
702 /// counter-only lifecycle use the return to assert the
703 /// "eligible for close" gate without setting up a real
704 /// attach.
705 pub fn complete_pending_use(&self, key: &ResourceKey) -> bool {
706 let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
707 let Some(entry) = inner.entries_by_key.get(&(key.clone(), 0)).cloned() else {
708 return false;
709 };
710 drop(inner);
711 let cur = entry.pending_uses.load(Ordering::Acquire);
712 if cur == 0 {
713 return entry.live_attaches.load(Ordering::Acquire) == 0;
714 }
715 let new_pending = entry.pending_uses.fetch_sub(1, Ordering::AcqRel) - 1;
716 new_pending == 0 && entry.live_attaches.load(Ordering::Acquire) == 0
717 }
718
719 /// SRD-35 Push D introspection helper: how many predicted
720 /// future attaches remain on the entry for `key` (gen 0).
721 /// Returns `None` when no entry exists for the key.
722 /// Tests use this to assert pre-map walker correctness
723 /// without poking the pool internals.
724 pub fn pending_uses_for(&self, key: &ResourceKey) -> Option<usize> {
725 let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
726 inner
727 .entries_by_key
728 .get(&(key.clone(), 0))
729 .map(|e| e.pending_uses.load(Ordering::Acquire))
730 }
731
732 /// Look up or create the entry for the given key under
733 /// the given policy. Generation 0 by default; siblings
734 /// (Push D) will create higher generations on
735 /// `can_support_more_load()` recommendation.
736 fn get_or_create_entry(
737 &self,
738 key: ResourceKey,
739 policy: ResourceSharePolicy,
740 generation: usize,
741 ) -> Arc<Entry> {
742 let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
743 let map_key = (key.clone(), generation);
744 if let Some(existing) = inner.entries_by_key.get(&map_key) {
745 return existing.clone();
746 }
747 let entry = Arc::new(Entry::new(key, policy, generation));
748 inner.entries_by_key.insert(map_key, entry.clone());
749 entry
750 }
751
752 /// Remove an entry from the active map. Called when its
753 /// refcount has fully drained.
754 fn remove_entry(&self, key: &ResourceKey, generation: usize) {
755 let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
756 inner.entries_by_key.remove(&(key.clone(), generation));
757 }
758
759 /// SRD-104 — resolve the accessor payload for the live entry whose
760 /// [`ResourceKey`] renders (via [`ResourceKey::render_key`]) to `key`.
761 /// Returns a clone of the stored `Arc<dyn Any>`, or `None` when no live
762 /// entry matches the fingerprint or the matched entry has no payload
763 /// (not opted in, or not yet initialised). This is the pool's half of
764 /// the [`polydat::ResourceAccessor`] bridge; see [`install_accessor`].
765 fn lookup_accessor_payload(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>> {
766 let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
767 for entry in inner.entries_by_key.values() {
768 if entry.key.render_key() == key {
769 return entry
770 .accessor_payload
771 .lock()
772 .unwrap_or_else(|e| e.into_inner())
773 .clone();
774 }
775 }
776 None
777 }
778
779 /// Lazily realise the resource for an entry, emitting
780 /// `resource.init.{started,completed,failed}` events
781 /// around the call. Subsequent calls observe the
782 /// already-populated slot and return the existing
783 /// resource (or the cached error if init was poisoned).
784 async fn ensure_initialized(
785 &self,
786 entry: &Arc<Entry>,
787 first_attach: bool,
788 factory: ResourceFactory<'_>,
789 ) -> Result<Arc<dyn SharedResource>, String> {
790 // Fast path — already realised.
791 if let Some(existing) = entry
792 .resource
793 .lock()
794 .unwrap_or_else(|e| e.into_inner())
795 .clone()
796 {
797 return Ok(existing);
798 }
799 if entry.poisoned.load(Ordering::Acquire) {
800 let err = entry
801 .init_error
802 .lock()
803 .unwrap_or_else(|e| e.into_inner())
804 .clone()
805 .unwrap_or_else(|| "init poisoned".into());
806 return Err(err);
807 }
808
809 let reason = if first_attach {
810 "first-attach"
811 } else {
812 "capacity-declined"
813 };
814 emit_event(
815 LogLevel::Debug,
816 "init.started",
817 entry,
818 &format!("reason={reason}"),
819 );
820 *entry
821 .init_started_at
822 .lock()
823 .unwrap_or_else(|e| e.into_inner()) = Some(Instant::now());
824
825 // Run the factory + the resource's own `init()`.
826 // Errors poison the entry so subsequent attaches
827 // surface the same error rather than retrying.
828 let outcome: Result<Arc<dyn SharedResource>, String> = async {
829 let resource = factory.build().await?;
830 resource.init().await?;
831 Ok(resource)
832 }
833 .await;
834
835 let elapsed_ms = entry
836 .init_started_at
837 .lock()
838 .unwrap_or_else(|e| e.into_inner())
839 .map(|t| t.elapsed().as_millis() as u64)
840 .unwrap_or(0);
841
842 match outcome {
843 Ok(resource) => {
844 *entry.resource.lock().unwrap_or_else(|e| e.into_inner()) = Some(resource.clone());
845 // SRD-104: capture the resource's accessor payload (if any)
846 // onto the entry so the pool's ResourceAccessor impl can hand
847 // it to kernel nodes by fingerprint. `None` for resources that
848 // don't opt in (the trait default) — no-op then.
849 *entry
850 .accessor_payload
851 .lock()
852 .unwrap_or_else(|e| e.into_inner()) = resource.accessor_payload();
853 emit_event(
854 LogLevel::Debug,
855 "init.completed",
856 entry,
857 &format!("elapsed_ms={elapsed_ms}"),
858 );
859 Ok(resource)
860 }
861 Err(e) => {
862 entry.poisoned.store(true, Ordering::Release);
863 *entry.init_error.lock().unwrap_or_else(|e| e.into_inner()) = Some(e.clone());
864 emit_event(
865 LogLevel::Error,
866 "init.failed",
867 entry,
868 &format!("elapsed_ms={elapsed_ms} error={e:?}"),
869 );
870 Err(e)
871 }
872 }
873 }
874}
875
876impl Default for ResourcePool {
877 fn default() -> Self {
878 Self::new()
879 }
880}
881
882/// Boxed, `Send` one-shot closure that builds a shared resource,
883/// returning a [`ResourceFuture`] yielding the resource (or an
884/// error message).
885type ResourceFactoryFn<'a> =
886 Box<dyn FnOnce() -> ResourceFuture<'a, Result<Arc<dyn SharedResource>, String>> + Send + 'a>;
887
888/// Closure-shaped factory for building a resource the first
889/// time its key is attached. Boxed so the pool can hold it
890/// across `.await` points and so callers don't have to type
891/// the future signature inline.
892pub struct ResourceFactory<'a> {
893 inner: ResourceFactoryFn<'a>,
894}
895
896impl<'a> ResourceFactory<'a> {
897 pub fn new<F, Fut>(f: F) -> Self
898 where
899 F: FnOnce() -> Fut + Send + 'a,
900 Fut: Future<Output = Result<Arc<dyn SharedResource>, String>> + Send + 'a,
901 {
902 Self {
903 inner: Box::new(move || Box::pin(f()) as ResourceFuture<'a, _>),
904 }
905 }
906
907 fn build(self) -> ResourceFuture<'a, Result<Arc<dyn SharedResource>, String>> {
908 (self.inner)()
909 }
910}
911
912// =================================================================
913// Public attach / detach surface
914// =================================================================
915
916/// Public attach call. Builds (or reuses) the entry for
917/// `key` under `policy`, lazily realises the resource via
918/// `factory` if this is the first attach, emits
919/// `resource.attach`, and returns a guard that detaches on
920/// drop.
921///
922/// `phase` is a free-form label that lands on the
923/// `resource.attach` / `resource.detach` event lines so
924/// operators can correlate lifecycle events with the
925/// per-phase activation that drove them.
926pub async fn attach(
927 pool: &Arc<ResourcePool>,
928 key: ResourceKey,
929 policy: ResourceSharePolicy,
930 phase: impl Into<String>,
931 factory: ResourceFactory<'_>,
932) -> Result<AttachGuard, String> {
933 let phase = phase.into();
934 let entry = pool.get_or_create_entry(key.clone(), policy, 0);
935 let first_attach = entry
936 .resource
937 .lock()
938 .unwrap_or_else(|e| e.into_inner())
939 .is_none()
940 && !entry.poisoned.load(Ordering::Acquire);
941
942 let resource = pool
943 .ensure_initialized(&entry, first_attach, factory)
944 .await?;
945
946 // Capability check: the live resource's can_share()
947 // must agree with the policy. PerPhase / PerFiber
948 // policies don't require can_share()=true; only
949 // Shared / PerScenario do.
950 let needs_share = matches!(
951 policy,
952 ResourceSharePolicy::Shared | ResourceSharePolicy::PerScenario
953 );
954 if needs_share && !resource.can_share() {
955 return Err(format!(
956 "resource for {} declared can_share()=false but policy is {policy}; \
957 elevate isolation to per-phase or per-fiber, or fix the resource impl",
958 entry.key.fmt_for_log(),
959 ));
960 }
961
962 // Bookkeeping: increment live attach, emit attach event
963 let live = entry.live_attaches.fetch_add(1, Ordering::AcqRel) + 1;
964 let pending_dec = entry.pending_uses.load(Ordering::Acquire);
965 let pending = if pending_dec > 0 {
966 entry.pending_uses.fetch_sub(1, Ordering::AcqRel) - 1
967 } else {
968 0
969 };
970 {
971 let mut inner = pool.inner.lock().unwrap_or_else(|e| e.into_inner());
972 inner.total_attaches += 1;
973 }
974 emit_event(
975 LogLevel::Debug,
976 "attach",
977 &entry,
978 &format!("phase={phase:?} pending={pending} live={live}"),
979 );
980
981 Ok(AttachGuard {
982 pool: Arc::clone(pool),
983 entry,
984 resource,
985 phase,
986 detached: false,
987 })
988}
989
990/// Owns an attached resource for the duration of one phase
991/// activation. Drop emits `resource.detach` and, when the
992/// entry's refcount has fully drained, schedules an async
993/// `close()` (and emits `resource.close.*` events around it).
994///
995/// Holding the guard keeps the resource alive; releasing
996/// it lets the pool tear down the resource if no other
997/// shells reference it.
998///
999/// Implements `Debug` so test helpers like
1000/// `Result::expect_err` accept it as the success type when
1001/// the error path is the one under test.
1002pub struct AttachGuard {
1003 pool: Arc<ResourcePool>,
1004 entry: Arc<Entry>,
1005 resource: Arc<dyn SharedResource>,
1006 phase: String,
1007 detached: bool,
1008}
1009
1010impl std::fmt::Debug for AttachGuard {
1011 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1012 f.debug_struct("AttachGuard")
1013 .field("key", &self.entry.key)
1014 .field("generation", &self.entry.generation)
1015 .field("policy", &self.entry.policy)
1016 .field("phase", &self.phase)
1017 .field("detached", &self.detached)
1018 .finish()
1019 }
1020}
1021
1022impl AttachGuard {
1023 /// Borrow the underlying shared resource. The guard
1024 /// retains ownership; the `Arc` clone is cheap.
1025 pub fn resource(&self) -> Arc<dyn SharedResource> {
1026 Arc::clone(&self.resource)
1027 }
1028
1029 /// Resource key for the attached entry. Useful for log
1030 /// correlation in adapter shells.
1031 pub fn key(&self) -> &ResourceKey {
1032 &self.entry.key
1033 }
1034
1035 /// Explicit detach. Calling this drains the refcount
1036 /// and (if it hits zero) awaits the resource's
1037 /// `close()` synchronously, so the caller observes
1038 /// teardown completion at this point. The guard's
1039 /// `Drop` falls back to a best-effort spawn when
1040 /// `detach()` wasn't called explicitly.
1041 pub async fn detach(mut self) -> Result<(), String> {
1042 self.detach_inner(true).await
1043 }
1044
1045 async fn detach_inner(&mut self, await_close: bool) -> Result<(), String> {
1046 if self.detached {
1047 return Ok(());
1048 }
1049 self.detached = true;
1050
1051 let live = self.entry.live_attaches.fetch_sub(1, Ordering::AcqRel) - 1;
1052 let pending = self.entry.pending_uses.load(Ordering::Acquire);
1053 emit_event(
1054 LogLevel::Debug,
1055 "detach",
1056 &self.entry,
1057 &format!("phase={:?} pending={pending} live={live}", self.phase),
1058 );
1059
1060 // SRD-35 Push D: close trigger is unified across
1061 // every policy. The pool closes an entry the moment
1062 // its `(pending == 0, live == 0)` invariant is
1063 // satisfied — meaning "no more attaches predicted by
1064 // the pre-map AND nothing currently using it."
1065 //
1066 // - `Shared` / `PerScenario`: pre-map seeds
1067 // `pending_uses` to the predicted attach count
1068 // (sum across all phases that produce this key).
1069 // Each `attach` decrements pending eagerly; the
1070 // last phase's detach drives both counters to
1071 // zero and the entry closes immediately —
1072 // releasing network resources before session end
1073 // for keys whose users are all done. If the
1074 // pre-map walker missed a phase (under-predict),
1075 // the entry would close prematurely and be
1076 // re-initialised on the next attach; if it
1077 // over-predicts, `pending` stays > 0 and
1078 // `pool.shutdown()` does the close at session
1079 // end (the conservative default).
1080 //
1081 // - `PerPhase` / `PerFiber`: pre-map intentionally
1082 // skips these — each phase is its own key, so
1083 // `pending` stays 0 throughout. Close fires on
1084 // the first detach when `live` returns to 0.
1085 if live == 0 && pending == 0 {
1086 self.trigger_close(await_close, "refcount-zero").await?;
1087 }
1088 Ok(())
1089 }
1090
1091 async fn trigger_close(&self, await_close: bool, reason: &str) -> Result<(), String> {
1092 // Pull the resource out of the entry slot — only
1093 // the close path holds it after this point, and a
1094 // late attach would observe an empty slot and
1095 // re-init under a fresh generation.
1096 let resource_opt = {
1097 let mut slot = self
1098 .entry
1099 .resource
1100 .lock()
1101 .unwrap_or_else(|e| e.into_inner());
1102 slot.take()
1103 };
1104 let Some(resource) = resource_opt else {
1105 // Entry never realised (factory failed before
1106 // resource was set, or already closed).
1107 self.pool
1108 .remove_entry(&self.entry.key, self.entry.generation);
1109 return Ok(());
1110 };
1111
1112 emit_event(
1113 LogLevel::Debug,
1114 "close.started",
1115 &self.entry,
1116 &format!("reason={reason}"),
1117 );
1118 let started_at = Instant::now();
1119
1120 let entry_for_async = Arc::clone(&self.entry);
1121 let pool_for_async = Arc::clone(&self.pool);
1122 let close_future = resource.close();
1123
1124 if await_close {
1125 let result = close_future.await;
1126 let elapsed_ms = started_at.elapsed().as_millis() as u64;
1127 match result {
1128 Ok(()) => emit_event(
1129 LogLevel::Debug,
1130 "close.completed",
1131 &self.entry,
1132 &format!("elapsed_ms={elapsed_ms}"),
1133 ),
1134 Err(ref e) => emit_event(
1135 LogLevel::Warn,
1136 "close.failed",
1137 &self.entry,
1138 &format!("elapsed_ms={elapsed_ms} error={e:?}"),
1139 ),
1140 }
1141 self.pool
1142 .remove_entry(&self.entry.key, self.entry.generation);
1143 result
1144 } else {
1145 // Drop-path: spawn the close so we don't block
1146 // the dropping thread, but still emit the
1147 // events when it finishes. Errors get logged
1148 // via the events themselves.
1149 tokio::spawn(async move {
1150 let result = close_future.await;
1151 let elapsed_ms = started_at.elapsed().as_millis() as u64;
1152 match result {
1153 Ok(()) => emit_event(
1154 LogLevel::Debug,
1155 "close.completed",
1156 &entry_for_async,
1157 &format!("elapsed_ms={elapsed_ms}"),
1158 ),
1159 Err(ref e) => emit_event(
1160 LogLevel::Warn,
1161 "close.failed",
1162 &entry_for_async,
1163 &format!("elapsed_ms={elapsed_ms} error={e:?}"),
1164 ),
1165 }
1166 pool_for_async.remove_entry(&entry_for_async.key, entry_for_async.generation);
1167 });
1168 Ok(())
1169 }
1170 }
1171}
1172
1173impl Drop for AttachGuard {
1174 fn drop(&mut self) {
1175 if self.detached {
1176 return;
1177 }
1178 // Drop is sync; schedule the detach work onto the
1179 // current tokio runtime if there is one. If no
1180 // runtime is active (test fallback), do the
1181 // synchronous bookkeeping inline and skip the
1182 // close await.
1183 let live = self.entry.live_attaches.fetch_sub(1, Ordering::AcqRel) - 1;
1184 let pending = self.entry.pending_uses.load(Ordering::Acquire);
1185 self.detached = true;
1186 let phase = &self.phase;
1187 emit_event(
1188 LogLevel::Debug,
1189 "detach",
1190 &self.entry,
1191 &format!("phase={phase:?} pending={pending} live={live}"),
1192 );
1193
1194 // SRD-35 Push D unified close gate (mirrors
1195 // `detach_inner`): close as soon as both counters
1196 // hit zero, regardless of policy. Pre-map seeds
1197 // `pending_uses` so Shared/PerScenario entries close
1198 // when their last user drains — not at session end.
1199 if live == 0 && pending == 0 {
1200 // Take the resource out so the close path owns
1201 // it — even if no runtime is active to await
1202 // the close, the synchronous bookkeeping is
1203 // correct.
1204 let resource_opt = {
1205 let mut slot = self
1206 .entry
1207 .resource
1208 .lock()
1209 .unwrap_or_else(|e| e.into_inner());
1210 slot.take()
1211 };
1212 let Some(resource) = resource_opt else {
1213 self.pool
1214 .remove_entry(&self.entry.key, self.entry.generation);
1215 return;
1216 };
1217
1218 // Try to spawn the close on the current
1219 // runtime. If we're outside a runtime
1220 // (tests, dryrun), the resource simply drops
1221 // synchronously here.
1222 let entry = Arc::clone(&self.entry);
1223 let pool = Arc::clone(&self.pool);
1224 emit_event(
1225 LogLevel::Debug,
1226 "close.started",
1227 &self.entry,
1228 "reason=refcount-zero",
1229 );
1230 let started_at = Instant::now();
1231
1232 if let Ok(handle) = tokio::runtime::Handle::try_current() {
1233 let close_future = resource.close();
1234 handle.spawn(async move {
1235 let result = close_future.await;
1236 let elapsed_ms = started_at.elapsed().as_millis() as u64;
1237 match result {
1238 Ok(()) => emit_event(
1239 LogLevel::Debug,
1240 "close.completed",
1241 &entry,
1242 &format!("elapsed_ms={elapsed_ms}"),
1243 ),
1244 Err(ref e) => emit_event(
1245 LogLevel::Warn,
1246 "close.failed",
1247 &entry,
1248 &format!("elapsed_ms={elapsed_ms} error={e:?}"),
1249 ),
1250 }
1251 pool.remove_entry(&entry.key, entry.generation);
1252 });
1253 } else {
1254 // No runtime: drop synchronously, emit
1255 // a synthetic close.completed with
1256 // elapsed_ms=0 so log consumers still see
1257 // a paired close event.
1258 drop(resource);
1259 emit_event(
1260 LogLevel::Debug,
1261 "close.completed",
1262 &self.entry,
1263 "elapsed_ms=0",
1264 );
1265 self.pool
1266 .remove_entry(&self.entry.key, self.entry.generation);
1267 }
1268 }
1269 }
1270}
1271
1272// =================================================================
1273// Legacy adapter wrapper — Push A bridge
1274// =================================================================
1275
1276/// Wraps an `Arc<dyn DriverAdapter>` from the existing
1277/// per-phase factory under `PerPhase` policy. Used until
1278/// each adapter migrates to a real `SharedResource` impl
1279/// (Push B for CQL, Push C for HTTP / OpenAPI / stdout).
1280///
1281/// `can_share()` returns `false` so the pool refuses to
1282/// give a `LegacyAdapterResource` to a second shell — it's
1283/// effectively single-use, matching today's behaviour.
1284///
1285/// Most callers shouldn't construct this directly — use
1286/// [`attach_legacy_adapter`] which handles the wrapping,
1287/// pool registration, and adapter unwrapping in one call.
1288pub struct LegacyAdapterResource {
1289 key: ResourceKey,
1290 adapter: Mutex<Option<Arc<dyn DriverAdapter>>>,
1291}
1292
1293impl LegacyAdapterResource {
1294 pub fn new(key: ResourceKey, adapter: Arc<dyn DriverAdapter>) -> Self {
1295 Self {
1296 key,
1297 adapter: Mutex::new(Some(adapter)),
1298 }
1299 }
1300
1301 /// Borrow the wrapped legacy adapter. Returns `None`
1302 /// after `close()` has consumed it.
1303 pub fn adapter(&self) -> Option<Arc<dyn DriverAdapter>> {
1304 self.adapter
1305 .lock()
1306 .unwrap_or_else(|e| e.into_inner())
1307 .clone()
1308 }
1309}
1310
1311impl SharedResource for LegacyAdapterResource {
1312 fn resource_key(&self) -> &ResourceKey {
1313 &self.key
1314 }
1315
1316 /// Legacy adapters are NOT yet declared shareable —
1317 /// each phase gets its own. Push B / C migrate
1318 /// adapters out of this shim and into real
1319 /// `can_share() = true` impls.
1320 fn can_share(&self) -> bool {
1321 false
1322 }
1323
1324 fn close(self: Arc<Self>) -> ResourceFuture<'static, Result<(), String>> {
1325 Box::pin(async move {
1326 // Drop the adapter Arc — the legacy factory
1327 // doesn't expose an async-close surface, so
1328 // this is best-effort sync teardown.
1329 let _ = self
1330 .adapter
1331 .lock()
1332 .unwrap_or_else(|e| e.into_inner())
1333 .take();
1334 Ok(())
1335 })
1336 }
1337
1338 fn as_legacy_adapter(&self) -> Option<Arc<dyn DriverAdapter>> {
1339 self.adapter()
1340 }
1341}
1342
1343/// Push B sibling of [`LegacyAdapterResource`] — wraps an
1344/// `Arc<dyn DriverAdapter>` but declares `can_share()=true`.
1345/// Used when an adapter has registered a
1346/// [`crate::adapter::SharedDriverRegistration`] opting it
1347/// into pool-shared semantics. The pool caches the
1348/// underlying `Arc<dyn DriverAdapter>` and hands the same
1349/// clone to every phase whose params produce the same
1350/// `ResourceKey`.
1351///
1352/// `close()` drops the wrapped adapter Arc — the actual
1353/// teardown timing is governed by the underlying
1354/// `DriverAdapter`'s `Drop`. Push D will add real async
1355/// `close()` to specific driver instances (e.g. awaiting
1356/// `cass_session_close()`'s `CassFuture` for the
1357/// cassandra-cpp engine).
1358pub struct SharedAdapterResource {
1359 key: ResourceKey,
1360 adapter: Mutex<Option<Arc<dyn DriverAdapter>>>,
1361}
1362
1363impl SharedAdapterResource {
1364 pub fn new(key: ResourceKey, adapter: Arc<dyn DriverAdapter>) -> Self {
1365 Self {
1366 key,
1367 adapter: Mutex::new(Some(adapter)),
1368 }
1369 }
1370
1371 pub fn adapter(&self) -> Option<Arc<dyn DriverAdapter>> {
1372 self.adapter
1373 .lock()
1374 .unwrap_or_else(|e| e.into_inner())
1375 .clone()
1376 }
1377}
1378
1379impl SharedResource for SharedAdapterResource {
1380 fn resource_key(&self) -> &ResourceKey {
1381 &self.key
1382 }
1383
1384 /// Push B: this wrapper declares the adapter shareable.
1385 /// The pool keeps one instance per `ResourceKey` and
1386 /// returns the same `Arc` to every matching attach.
1387 fn can_share(&self) -> bool {
1388 true
1389 }
1390
1391 fn close(self: Arc<Self>) -> ResourceFuture<'static, Result<(), String>> {
1392 Box::pin(async move {
1393 // Take the adapter Arc out of the slot so the
1394 // shutdown handshake runs on a stable owner.
1395 // Drop happens at the end of this scope; the
1396 // engine's Drop runs after `shutdown().await`
1397 // resolves.
1398 let adapter = self
1399 .adapter
1400 .lock()
1401 .unwrap_or_else(|e| e.into_inner())
1402 .take();
1403 if let Some(adapter) = adapter {
1404 adapter.shutdown().await;
1405 }
1406 Ok(())
1407 })
1408 }
1409
1410 fn as_legacy_adapter(&self) -> Option<Arc<dyn DriverAdapter>> {
1411 self.adapter()
1412 }
1413
1414 /// SRD-104 — surface the wrapped adapter's accessor payload (SRD-103's
1415 /// CQL session handle for the `cql` adapter). The pool captures this
1416 /// right after init and hands clones to kernel nodes by fingerprint. The
1417 /// pool-shared wrapper owns no payload itself; it delegates to the
1418 /// adapter, which builds one over its connected session. `None` for
1419 /// adapters that don't opt in (the trait default).
1420 fn accessor_payload(&self) -> Option<Arc<dyn Any + Send + Sync>> {
1421 self.adapter().and_then(|a| a.accessor_payload())
1422 }
1423}
1424
1425/// Push B high-level helper: attach a shareable adapter
1426/// through the pool under `Shared` policy. The first phase
1427/// whose params produce `key` triggers `factory`; every
1428/// subsequent matching phase reuses the same
1429/// `Arc<dyn DriverAdapter>`. Each call returns a fresh
1430/// guard; the pool's refcount drops the entry only when
1431/// every guard is released.
1432pub async fn attach_shared_adapter<F, Fut>(
1433 pool: &Arc<ResourcePool>,
1434 adapter_name: &str,
1435 phase: &str,
1436 key: ResourceKey,
1437 factory: F,
1438) -> Result<(Arc<dyn DriverAdapter>, AttachGuard), String>
1439where
1440 F: FnOnce() -> Fut + Send + 'static,
1441 Fut: Future<Output = Result<Arc<dyn DriverAdapter>, String>> + Send + 'static,
1442{
1443 let key_for_resource = key.clone();
1444 let factory = ResourceFactory::new(move || async move {
1445 let adapter = factory().await?;
1446 let res = SharedAdapterResource::new(key_for_resource, adapter);
1447 Ok(Arc::new(res) as Arc<dyn SharedResource>)
1448 });
1449 let guard = attach(pool, key, ResourceSharePolicy::Shared, phase, factory).await?;
1450 let adapter = guard.resource().as_legacy_adapter().ok_or_else(|| {
1451 format!(
1452 "internal: shared pool resource for adapter '{adapter_name}' did not surface \
1453 a DriverAdapter handle — should be unreachable"
1454 )
1455 })?;
1456 Ok((adapter, guard))
1457}
1458
1459/// Push A high-level helper for the executor: attach a
1460/// legacy adapter through the pool under `PerPhase` policy
1461/// and return both the unwrapped `Arc<dyn DriverAdapter>`
1462/// and the corresponding [`AttachGuard`].
1463///
1464/// The guard MUST outlive any use of the returned adapter
1465/// reference — typically the caller stores it alongside
1466/// the activity and drops it at phase teardown so the
1467/// pool sees the matching `resource.detach`.
1468///
1469/// `factory` builds the adapter the first time the entry
1470/// is realised. The pool only calls it once per
1471/// `(key, generation)` even though `PerPhase` policy
1472/// makes each phase its own key.
1473pub async fn attach_legacy_adapter<F, Fut>(
1474 pool: &Arc<ResourcePool>,
1475 adapter_name: &str,
1476 phase: &str,
1477 key_extras: &[(&str, &str)],
1478 factory: F,
1479) -> Result<(Arc<dyn DriverAdapter>, AttachGuard), String>
1480where
1481 F: FnOnce() -> Fut + Send + 'static,
1482 Fut: Future<Output = Result<Arc<dyn DriverAdapter>, String>> + Send + 'static,
1483{
1484 let mut key = ResourceKey::new(adapter_name);
1485 for (k, v) in key_extras {
1486 key = key.with(*k, *v);
1487 }
1488 let key_for_resource = key.clone();
1489 let factory = ResourceFactory::new(move || async move {
1490 let adapter = factory().await?;
1491 let res = LegacyAdapterResource::new(key_for_resource, adapter);
1492 Ok(Arc::new(res) as Arc<dyn SharedResource>)
1493 });
1494
1495 let guard = attach(pool, key, ResourceSharePolicy::PerPhase, phase, factory).await?;
1496 let resource = guard.resource();
1497 let adapter = resource.as_legacy_adapter().ok_or_else(|| {
1498 format!(
1499 "internal: pool resource for adapter '{adapter_name}' did not surface \
1500 a legacy DriverAdapter handle — Push A path should be unreachable"
1501 )
1502 })?;
1503 Ok((adapter, guard))
1504}
1505
1506// =================================================================
1507// Pre-map pending_uses walker — SRD-35 Push D
1508// =================================================================
1509
1510/// Walk the freshly-pre-mapped scenario tree and seed the
1511/// pool's `pending_uses` counter for every phase that will
1512/// attach a pool-shareable adapter. Called once at session
1513/// bootstrap, after the pre-map pass of
1514/// `crate::executor::execute_tree` (run with `pre_map_only` set)
1515/// returns and before the executor begins running any phase.
1516///
1517/// For each phase node:
1518/// 1. Determine the adapter name — `phase.adapter` override
1519/// (when the phase declares one) wins over the
1520/// session-level `default_driver`.
1521/// 2. Resolve the driver name without instantiating
1522/// anything (`resolve_driver_name`).
1523/// 3. Look up the matching `SharedDriverRegistration`. If
1524/// none registered, the phase rides the legacy
1525/// `PerPhase` path — its key is per-phase-unique, so
1526/// pre-map contributes zero to any shared `pending_uses`
1527/// and the legacy close-on-detach path handles teardown.
1528/// 4. Compute the resource key from the session's
1529/// `merged_params` via the registration's pure
1530/// `resource_key` function. `resource_key` failures are
1531/// hard errors at session bootstrap — surfacing the same
1532/// misconfiguration that `attach_shared_adapter` would
1533/// have hit at runtime, just earlier and in one place.
1534/// 5. Increment the per-key `pending_uses` counter via
1535/// [`ResourcePool::declare_pending_use`].
1536///
1537/// This is a pure read of the scenario tree + phase params;
1538/// no adapters are instantiated. The walker can run before
1539/// `pool.shutdown()` would otherwise be a no-op — and must
1540/// run before the first phase, since pending counts are
1541/// the close trigger that releases shared resources promptly
1542/// when their last user finishes.
1543pub fn pre_map_pending_uses(
1544 pool: &Arc<ResourcePool>,
1545 tree: &crate::scene_tree::SceneTree,
1546 phases: &std::collections::HashMap<String, nmbrs_workload::model::WorkloadPhase>,
1547 default_driver: &str,
1548 merged_params: &std::collections::HashMap<String, String>,
1549) -> Result<(), String> {
1550 for node in tree.dfs_phases() {
1551 // Phase-level adapter override wins over session
1552 // default. Mirrors `executor::run_phase` (line 1903)
1553 // so the predicted key matches the runtime key
1554 // exactly.
1555 let adapter = phases
1556 .get(&node.name)
1557 .and_then(|p| p.adapter.clone())
1558 .unwrap_or_else(|| default_driver.to_string());
1559
1560 // Drive the same resolution the executor uses
1561 // (`<adapter>driver` selector param + DriverImpl
1562 // ranking). `None` here means the adapter has no
1563 // `DriverImpl` registered AND no fallback; skip.
1564 let selector = format!("{adapter}driver");
1565 let Some(driver_name) =
1566 crate::adapter::resolve_driver_name(&adapter, &selector, merged_params)
1567 else {
1568 continue;
1569 };
1570
1571 // Only adapters that opted into pool sharing have a
1572 // `SharedDriverRegistration`. Legacy adapters fall
1573 // through; their `PerPhase` close-on-detach path is
1574 // unaffected.
1575 let Some(reg) = crate::adapter::find_shared_driver(&adapter, driver_name) else {
1576 continue;
1577 };
1578
1579 let key = (reg.resource_key)(merged_params).map_err(|e| {
1580 format!(
1581 "resource pool pre-map: phase '{}' adapter '{}' driver '{}': {}",
1582 node.name, adapter, driver_name, e,
1583 )
1584 })?;
1585 pool.declare_pending_use(key, default_policy_for(reg.share_capability));
1586 }
1587 Ok(())
1588}
1589
1590// =================================================================
1591// SRD-104 — resource-accessor bridge (pool ⇄ polydat)
1592// =================================================================
1593
1594/// The pool the process-global [`polydat::ResourceAccessor`] currently
1595/// resolves against, held as a `Weak` so the process-lifetime bridge never
1596/// keeps a finished session's pool (and its live network resources) alive.
1597/// Re-pointed each session by [`install_accessor`]; empty until the first
1598/// install.
1599static ACTIVE_POOL: Mutex<Weak<ResourcePool>> = Mutex::new(Weak::new());
1600
1601/// The bridge every nmbrs kernel tree resolves resources through
1602/// ([`pool_resources`]). It carries no state itself — it resolves against
1603/// the swappable [`ACTIVE_POOL`] — so a host process that runs several
1604/// sessions (TUI, `metrics watch`) re-points the pool without touching the
1605/// trees built before.
1606struct PoolAccessorView;
1607
1608impl polydat::ResourceAccessor for PoolAccessorView {
1609 fn lookup(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>> {
1610 let pool = ACTIVE_POOL
1611 .lock()
1612 .unwrap_or_else(|e| e.into_inner())
1613 .upgrade()?;
1614 pool.lookup_accessor_payload(key)
1615 }
1616}
1617
1618/// SRD-104 — point the resource bridge at `pool`. Called once at session
1619/// start where the [`ResourcePool`] is created.
1620///
1621/// Every kernel tree resolves through the same bridge ([`pool_resources`]),
1622/// and the pool it resolves against is swappable (`ACTIVE_POOL`), so a
1623/// second session in the same host process re-points the bridge at its own
1624/// pool rather than stranding on the first session's (now-closed) one.
1625/// Idempotent per session.
1626pub fn install_accessor(pool: &Arc<ResourcePool>) {
1627 *ACTIVE_POOL.lock().unwrap_or_else(|e| e.into_inner()) = Arc::downgrade(pool);
1628}
1629
1630/// A resource scope with the pool bridge installed: what every nmbrs
1631/// kernel tree is compiled with at its root
1632/// ([`crate::bindings::compile_scope_kernel`]). A kernel bound under the
1633/// tree joins its scope, so a node anywhere in it that captured
1634/// `ctx.resources()` resolves pool resources by key.
1635pub fn pool_resources() -> polydat::ResourceScope {
1636 polydat::ResourceScope::with_accessor(Arc::new(PoolAccessorView))
1637}
1638
1639// =================================================================
1640// Tests
1641// =================================================================
1642
1643#[cfg(test)]
1644mod tests {
1645 use super::*;
1646 use std::sync::atomic::AtomicU32;
1647
1648 /// Synthetic resource for unit tests — records every
1649 /// lifecycle call so assertions can verify the pool's
1650 /// invariants without touching real network resources.
1651 struct MockResource {
1652 key: ResourceKey,
1653 can_share: bool,
1654 saturated_after_attaches: u32,
1655 attach_count: AtomicU32,
1656 init_calls: AtomicU32,
1657 close_calls: AtomicU32,
1658 init_should_fail: bool,
1659 }
1660
1661 impl MockResource {
1662 fn new(key: ResourceKey) -> Arc<Self> {
1663 Arc::new(Self {
1664 key,
1665 can_share: true,
1666 saturated_after_attaches: u32::MAX,
1667 attach_count: AtomicU32::new(0),
1668 init_calls: AtomicU32::new(0),
1669 close_calls: AtomicU32::new(0),
1670 init_should_fail: false,
1671 })
1672 }
1673
1674 fn with_can_share(key: ResourceKey, can_share: bool) -> Arc<Self> {
1675 Arc::new(Self {
1676 key,
1677 can_share,
1678 saturated_after_attaches: u32::MAX,
1679 attach_count: AtomicU32::new(0),
1680 init_calls: AtomicU32::new(0),
1681 close_calls: AtomicU32::new(0),
1682 init_should_fail: false,
1683 })
1684 }
1685
1686 fn with_init_failure(key: ResourceKey) -> Arc<Self> {
1687 Arc::new(Self {
1688 key,
1689 can_share: true,
1690 saturated_after_attaches: u32::MAX,
1691 attach_count: AtomicU32::new(0),
1692 init_calls: AtomicU32::new(0),
1693 close_calls: AtomicU32::new(0),
1694 init_should_fail: true,
1695 })
1696 }
1697 }
1698
1699 impl SharedResource for MockResource {
1700 fn resource_key(&self) -> &ResourceKey {
1701 &self.key
1702 }
1703 fn can_share(&self) -> bool {
1704 self.can_share
1705 }
1706 fn can_support_more_load(&self) -> bool {
1707 // True (has capacity) until `attach_count`
1708 // crosses the saturation threshold; then
1709 // false (saturated). `u32::MAX` as the
1710 // threshold ⇒ always has capacity, matching
1711 // the trait default.
1712 self.attach_count.load(Ordering::Acquire) < self.saturated_after_attaches
1713 }
1714
1715 fn init(&self) -> ResourceFuture<'_, Result<(), String>> {
1716 self.init_calls.fetch_add(1, Ordering::AcqRel);
1717 let fail = self.init_should_fail;
1718 Box::pin(async move {
1719 if fail {
1720 Err("simulated init failure".into())
1721 } else {
1722 Ok(())
1723 }
1724 })
1725 }
1726
1727 fn close(self: Arc<Self>) -> ResourceFuture<'static, Result<(), String>> {
1728 self.close_calls.fetch_add(1, Ordering::AcqRel);
1729 Box::pin(async { Ok(()) })
1730 }
1731 }
1732
1733 fn key(adapter: &str, vendor: &str) -> ResourceKey {
1734 ResourceKey::new(adapter).with("vendor", vendor)
1735 }
1736
1737 #[test]
1738 fn key_value_equality_is_field_order_independent() {
1739 let a = ResourceKey::new("cql")
1740 .with("hosts", "h1")
1741 .with("port", "9042");
1742 let b = ResourceKey::new("cql")
1743 .with("port", "9042")
1744 .with("hosts", "h1");
1745 assert_eq!(a, b);
1746 assert_eq!(a.fmt_for_log(), b.fmt_for_log());
1747 }
1748
1749 #[test]
1750 fn key_log_format_redacts_password() {
1751 let k = ResourceKey::new("cql")
1752 .with("hosts", "h1")
1753 .with("password", "hunter2");
1754 let s = k.fmt_for_log();
1755 assert!(s.contains("hosts=h1"));
1756 assert!(s.contains("password=***"), "got: {s}");
1757 assert!(!s.contains("hunter2"));
1758 }
1759
1760 #[test]
1761 fn policy_parse_accepts_canonical_and_underscore_forms() {
1762 assert_eq!(
1763 ResourceSharePolicy::parse("shared").unwrap(),
1764 ResourceSharePolicy::Shared
1765 );
1766 assert_eq!(
1767 ResourceSharePolicy::parse("per-phase").unwrap(),
1768 ResourceSharePolicy::PerPhase
1769 );
1770 assert_eq!(
1771 ResourceSharePolicy::parse("per_phase").unwrap(),
1772 ResourceSharePolicy::PerPhase
1773 );
1774 assert_eq!(
1775 ResourceSharePolicy::parse("PER-FIBER").unwrap(),
1776 ResourceSharePolicy::PerFiber
1777 );
1778 ResourceSharePolicy::parse("nope").unwrap_err();
1779 }
1780
1781 #[test]
1782 fn capability_floor_orders_isolation_correctly() {
1783 // Shared is the lowest (most-shared); PerFiber is
1784 // the highest (most-isolated). User-policy must be
1785 // ≥ floor for capability.
1786 assert!(capability_floor(ShareCapability::Shared) <= ResourceSharePolicy::Shared);
1787 assert!(capability_floor(ShareCapability::PerPhase) > ResourceSharePolicy::Shared);
1788 }
1789
1790 #[tokio::test]
1791 async fn shared_resource_survives_detach_until_more_phases_predicted() {
1792 // SRD-35 Push D: the pre-map walker seeds
1793 // `pending_uses` with the predicted total attach
1794 // count. While `pending > 0`, detach MUST NOT close
1795 // — there's still a future phase that will use the
1796 // resource. The 55-phase open/close storm in the
1797 // operator's session.log was a Push A regression
1798 // where pending wasn't seeded; the resource closed
1799 // on the first detach and re-initialised every time.
1800 let pool = Arc::new(ResourcePool::new());
1801 let mock = MockResource::new(key("cql", "cassandra-cpp"));
1802 let mock_for_factory = Arc::clone(&mock);
1803
1804 // Pre-map says: 2 phases will attach this key.
1805 pool.declare_pending_use(mock.resource_key().clone(), ResourceSharePolicy::Shared);
1806 pool.declare_pending_use(mock.resource_key().clone(), ResourceSharePolicy::Shared);
1807 assert_eq!(pool.pending_uses_for(mock.resource_key()), Some(2));
1808
1809 // Phase A: attach + detach. pending drops to 1, so
1810 // close must NOT fire — phase B is still expected.
1811 let guard = attach(
1812 &pool,
1813 mock.resource_key().clone(),
1814 ResourceSharePolicy::Shared,
1815 "phase_A",
1816 ResourceFactory::new(
1817 move || async move { Ok(mock_for_factory as Arc<dyn SharedResource>) },
1818 ),
1819 )
1820 .await
1821 .unwrap();
1822
1823 assert_eq!(mock.init_calls.load(Ordering::Acquire), 1);
1824 guard.detach().await.unwrap();
1825 assert_eq!(
1826 mock.close_calls.load(Ordering::Acquire),
1827 0,
1828 "Shared-policy detach MUST NOT close while another \
1829 phase is still predicted to attach"
1830 );
1831 assert_eq!(pool.pending_uses_for(mock.resource_key()), Some(1));
1832 assert_eq!(pool.live_entries(), 1);
1833
1834 // Phase B: attach + detach. pending drops to 0,
1835 // live drops to 0 — close fires NOW, not at session
1836 // shutdown. Promptly releases the resource the
1837 // moment its last user is done.
1838 let mock_for_factory2 = Arc::clone(&mock);
1839 let guard2 = attach(
1840 &pool,
1841 mock.resource_key().clone(),
1842 ResourceSharePolicy::Shared,
1843 "phase_B",
1844 ResourceFactory::new(move || async move {
1845 Ok(mock_for_factory2 as Arc<dyn SharedResource>)
1846 }),
1847 )
1848 .await
1849 .unwrap();
1850 guard2.detach().await.unwrap();
1851 assert_eq!(
1852 mock.close_calls.load(Ordering::Acquire),
1853 1,
1854 "close MUST fire when pending hits 0 AND live hits 0 — \
1855 that's the SRD-35 Push D close-on-zero contract"
1856 );
1857 assert_eq!(pool.live_entries(), 0, "entry removed once closed");
1858
1859 // Pool shutdown is now a no-op for this key.
1860 pool.shutdown().await;
1861 assert_eq!(
1862 mock.close_calls.load(Ordering::Acquire),
1863 1,
1864 "shutdown() must NOT double-close an entry"
1865 );
1866 }
1867
1868 #[tokio::test]
1869 async fn shared_resource_overpredicted_pending_holds_until_shutdown() {
1870 // Conservative fallback: if the pre-map walker
1871 // over-predicts (predicts N phases but only N-1
1872 // actually attach), the entry stays alive until
1873 // shutdown — better than closing prematurely. The
1874 // residual `pending > 0` at session end is also
1875 // visible on the `close.started reason=session-end`
1876 // line so an operator can spot pre-map bugs.
1877 let pool = Arc::new(ResourcePool::new());
1878 let mock = MockResource::new(key("cql", "cassandra-cpp"));
1879 let mock_for_factory = Arc::clone(&mock);
1880
1881 // Pre-map says 3 phases, only 1 actually runs.
1882 for _ in 0..3 {
1883 pool.declare_pending_use(mock.resource_key().clone(), ResourceSharePolicy::Shared);
1884 }
1885 let guard = attach(
1886 &pool,
1887 mock.resource_key().clone(),
1888 ResourceSharePolicy::Shared,
1889 "phase_A",
1890 ResourceFactory::new(
1891 move || async move { Ok(mock_for_factory as Arc<dyn SharedResource>) },
1892 ),
1893 )
1894 .await
1895 .unwrap();
1896 guard.detach().await.unwrap();
1897
1898 // pending = 2 after one attach — close MUST NOT
1899 // fire mid-run.
1900 assert_eq!(pool.pending_uses_for(mock.resource_key()), Some(2));
1901 assert_eq!(mock.close_calls.load(Ordering::Acquire), 0);
1902 assert_eq!(pool.live_entries(), 1);
1903
1904 pool.shutdown().await;
1905 assert_eq!(
1906 mock.close_calls.load(Ordering::Acquire),
1907 1,
1908 "shutdown() drains residual entries with pending > 0"
1909 );
1910 }
1911
1912 #[tokio::test]
1913 async fn per_phase_resource_closes_on_detach() {
1914 // Counter-test: under `PerPhase` policy, the
1915 // legacy-shim semantics still apply — close fires
1916 // on the detach that drains refcount, no waiting
1917 // for shutdown. The synthetic mock has
1918 // `can_share=true`, so we explicitly use PerPhase
1919 // to exercise this branch.
1920 let pool = Arc::new(ResourcePool::new());
1921 let mock = MockResource::new(key("legacy", "synthetic"));
1922 let mock_for_factory = Arc::clone(&mock);
1923
1924 let guard = attach(
1925 &pool,
1926 mock.resource_key().clone(),
1927 ResourceSharePolicy::PerPhase,
1928 "phase_A",
1929 ResourceFactory::new(
1930 move || async move { Ok(mock_for_factory as Arc<dyn SharedResource>) },
1931 ),
1932 )
1933 .await
1934 .unwrap();
1935
1936 guard.detach().await.unwrap();
1937 assert_eq!(
1938 mock.close_calls.load(Ordering::Acquire),
1939 1,
1940 "PerPhase detach MUST close immediately"
1941 );
1942 assert_eq!(pool.live_entries(), 0);
1943 }
1944
1945 #[tokio::test]
1946 async fn init_failure_poisons_the_entry() {
1947 let pool = Arc::new(ResourcePool::new());
1948 let mock = MockResource::with_init_failure(key("cql", "cassandra-cpp"));
1949 let mock_for_factory = Arc::clone(&mock);
1950
1951 let result = attach(
1952 &pool,
1953 mock.resource_key().clone(),
1954 ResourceSharePolicy::Shared,
1955 "phase_A",
1956 ResourceFactory::new(
1957 move || async move { Ok(mock_for_factory as Arc<dyn SharedResource>) },
1958 ),
1959 )
1960 .await;
1961
1962 let err = result.expect_err("init failure must propagate");
1963 assert!(err.contains("simulated init failure"), "got: {err}");
1964 }
1965
1966 #[tokio::test]
1967 async fn shared_policy_against_can_share_false_resource_errors() {
1968 let pool = Arc::new(ResourcePool::new());
1969 let mock = MockResource::with_can_share(key("legacy", "x"), false);
1970 let mock_for_factory = Arc::clone(&mock);
1971
1972 let result = attach(
1973 &pool,
1974 mock.resource_key().clone(),
1975 ResourceSharePolicy::Shared,
1976 "phase_A",
1977 ResourceFactory::new(
1978 move || async move { Ok(mock_for_factory as Arc<dyn SharedResource>) },
1979 ),
1980 )
1981 .await;
1982
1983 let err = result.expect_err("shared policy on can_share()=false resource must be rejected");
1984 assert!(err.contains("can_share()=false"), "got: {err}");
1985 assert!(
1986 err.contains("per-phase") || err.contains("per-fiber"),
1987 "error must point at the available policies, got: {err}"
1988 );
1989 }
1990
1991 #[tokio::test]
1992 async fn per_phase_policy_with_can_share_false_works() {
1993 let pool = Arc::new(ResourcePool::new());
1994 let mock = MockResource::with_can_share(key("legacy", "x"), false);
1995 let mock_for_factory = Arc::clone(&mock);
1996
1997 let guard = attach(
1998 &pool,
1999 mock.resource_key().clone(),
2000 ResourceSharePolicy::PerPhase,
2001 "phase_A",
2002 ResourceFactory::new(
2003 move || async move { Ok(mock_for_factory as Arc<dyn SharedResource>) },
2004 ),
2005 )
2006 .await
2007 .expect("PerPhase must accept can_share()=false");
2008
2009 // PerPhase + can_share=false is exactly the
2010 // legacy-adapter shape; works without elevation.
2011 guard.detach().await.unwrap();
2012 }
2013
2014 #[tokio::test]
2015 async fn shared_policy_reuses_one_instance_across_attaches() {
2016 let pool = Arc::new(ResourcePool::new());
2017 let mock = MockResource::new(key("cql", "cassandra-cpp"));
2018 let mock_for_factory = Arc::clone(&mock);
2019 let factory_called = Arc::new(AtomicU32::new(0));
2020 let factory_called_clone = Arc::clone(&factory_called);
2021
2022 // SRD-35 Push D: pre-map predicts 3 phases will
2023 // attach this key, but the test only exercises 2.
2024 // The residual pending = 1 keeps the entry alive
2025 // until shutdown — this lets the test assert "Shared
2026 // policy holds across phases" without timing-coupling
2027 // the close trigger to detach order.
2028 for _ in 0..3 {
2029 pool.declare_pending_use(mock.resource_key().clone(), ResourceSharePolicy::Shared);
2030 }
2031
2032 let g1 = attach(
2033 &pool,
2034 mock.resource_key().clone(),
2035 ResourceSharePolicy::Shared,
2036 "phase_A",
2037 ResourceFactory::new(move || {
2038 factory_called_clone.fetch_add(1, Ordering::AcqRel);
2039 let m = Arc::clone(&mock_for_factory);
2040 async move { Ok(m as Arc<dyn SharedResource>) }
2041 }),
2042 )
2043 .await
2044 .unwrap();
2045
2046 // Second attach for the SAME key MUST reuse the
2047 // existing instance — factory MUST NOT be called
2048 // again. Push A's pool reuses by key+generation
2049 // even though the legacy adapter shim doesn't
2050 // typically exercise this; the synthetic test
2051 // proves the contract end-to-end.
2052 let mock_for_factory2 = Arc::clone(&mock);
2053 let factory_called_clone2 = Arc::clone(&factory_called);
2054 let g2 = attach(
2055 &pool,
2056 mock.resource_key().clone(),
2057 ResourceSharePolicy::Shared,
2058 "phase_B",
2059 ResourceFactory::new(move || {
2060 factory_called_clone2.fetch_add(1, Ordering::AcqRel);
2061 let m = Arc::clone(&mock_for_factory2);
2062 async move { Ok(m as Arc<dyn SharedResource>) }
2063 }),
2064 )
2065 .await
2066 .unwrap();
2067
2068 assert_eq!(
2069 factory_called.load(Ordering::Acquire),
2070 1,
2071 "factory must not be called twice for the same key under Shared policy"
2072 );
2073 assert_eq!(
2074 mock.init_calls.load(Ordering::Acquire),
2075 1,
2076 "init() must fire exactly once"
2077 );
2078 assert_eq!(pool.total_attaches(), 2);
2079 assert_eq!(pool.live_entries(), 1);
2080
2081 // Detach both guards. Pre-map predicted 3 phases;
2082 // only 2 attached, so pending stays > 0 and the
2083 // entry survives until session shutdown. The
2084 // session.log bug this contract locks down was the
2085 // resource closing on first detach, defeating
2086 // sharing across the next phase's attach.
2087 g1.detach().await.unwrap();
2088 assert_eq!(
2089 mock.close_calls.load(Ordering::Acquire),
2090 0,
2091 "close MUST NOT fire while another guard is live"
2092 );
2093 g2.detach().await.unwrap();
2094 assert_eq!(
2095 mock.close_calls.load(Ordering::Acquire),
2096 0,
2097 "close MUST NOT fire while pending uses remain — \
2098 the resource stays alive for predicted future phases"
2099 );
2100 assert_eq!(pool.live_entries(), 1, "entry stays cached across phases");
2101
2102 // The session-end shutdown drains the residual.
2103 pool.shutdown().await;
2104 assert_eq!(
2105 mock.close_calls.load(Ordering::Acquire),
2106 1,
2107 "shutdown() drains the entry"
2108 );
2109 assert_eq!(pool.live_entries(), 0);
2110 }
2111
2112 /// Minimal `DriverAdapter` stub used by the legacy-
2113 /// wrapper test below. `map_op` returns an error
2114 /// because the test never exercises the dispenser
2115 /// path — we only need a `DriverAdapter` value to
2116 /// hand to `LegacyAdapterResource::new`.
2117 struct LegacyDummy;
2118 impl crate::adapter::DriverAdapter for LegacyDummy {
2119 fn name(&self) -> &str {
2120 "legacy_dummy"
2121 }
2122 fn map_op<'a>(
2123 &'a self,
2124 _template: &'a nmbrs_workload::model::ParsedOp,
2125 _parent: std::sync::Arc<dyn polydat::Kernel>,
2126 ) -> std::pin::Pin<
2127 Box<
2128 dyn std::future::Future<
2129 Output = Result<Box<dyn crate::adapter::OpDispenser>, String>,
2130 > + Send
2131 + 'a,
2132 >,
2133 > {
2134 Box::pin(async move { Err("dummy".into()) })
2135 }
2136 }
2137
2138 #[test]
2139 fn capacity_decline_at_quiescence_is_caught_as_driver_bug() {
2140 // SRD-35 validity rule: a `can_support_more_load()`
2141 // body that returns FALSE while `live_attaches ==
2142 // 0` is reading historical state, not active load
2143 // (e.g., a ring buffer that filled during a
2144 // previous phase). The pool's guard MUST treat
2145 // this as a driver bug — log a Warn and route the
2146 // attach to the existing idle generation instead
2147 // of wasting a spawn. Real saturation (decline +
2148 // live > 0) MUST trigger the spawn unchanged.
2149 struct Reports {
2150 key: ResourceKey,
2151 has_capacity: bool,
2152 }
2153 impl SharedResource for Reports {
2154 fn resource_key(&self) -> &ResourceKey {
2155 &self.key
2156 }
2157 fn can_support_more_load(&self) -> bool {
2158 self.has_capacity
2159 }
2160 }
2161
2162 let entry = Entry::new(ResourceKey::new("buggy"), ResourceSharePolicy::Shared, 0);
2163
2164 // Has capacity → never spawn, regardless of load.
2165 let calm = Reports {
2166 key: ResourceKey::new("buggy"),
2167 has_capacity: true,
2168 };
2169 assert!(
2170 !needs_sibling_spawn(&entry, &calm),
2171 "no spawn when the resource has capacity"
2172 );
2173
2174 // No capacity at quiescence → driver bug, suppressed.
2175 let buggy = Reports {
2176 key: ResourceKey::new("buggy"),
2177 has_capacity: false,
2178 };
2179 assert_eq!(entry.live_attaches.load(Ordering::Acquire), 0);
2180 assert!(
2181 !needs_sibling_spawn(&entry, &buggy),
2182 "quiescent decline must be suppressed (driver bug)"
2183 );
2184
2185 // No capacity + actual load → genuine saturation, spawn.
2186 entry.live_attaches.fetch_add(3, Ordering::AcqRel);
2187 assert!(
2188 needs_sibling_spawn(&entry, &buggy),
2189 "decline under live load is genuine saturation; spawn sibling"
2190 );
2191
2192 // Has capacity + load → still no spawn.
2193 assert!(
2194 !needs_sibling_spawn(&entry, &calm),
2195 "no spawn when there is capacity, even under load"
2196 );
2197 }
2198
2199 #[tokio::test]
2200 async fn shared_adapter_resource_returns_one_instance_for_one_key() {
2201 // Push B contract: under `Shared` policy, two
2202 // attaches against the same `ResourceKey` must
2203 // produce two guards backed by the SAME wrapped
2204 // `Arc<dyn DriverAdapter>` — the pool's caching is
2205 // what stops 50 phases from opening 50 cluster
2206 // sessions.
2207 let pool = Arc::new(ResourcePool::new());
2208 let adapter: Arc<dyn DriverAdapter> = Arc::new(LegacyDummy);
2209 let key = ResourceKey::new("cql").with("driver", "synthetic");
2210 let factory_call_count = Arc::new(AtomicU32::new(0));
2211
2212 // SRD-35 Push D: simulate the pre-map walker
2213 // declaring 3 future phases. The two attaches below
2214 // consume 2 of those 3, leaving pending = 1 so the
2215 // entry survives until shutdown — the assertion
2216 // shape this test was designed to verify.
2217 for _ in 0..3 {
2218 pool.declare_pending_use(key.clone(), ResourceSharePolicy::Shared);
2219 }
2220
2221 let factory_count_a = Arc::clone(&factory_call_count);
2222 let adapter_a = Arc::clone(&adapter);
2223 let (got_a, guard_a) =
2224 attach_shared_adapter(&pool, "cql", "phase_A", key.clone(), move || {
2225 factory_count_a.fetch_add(1, Ordering::AcqRel);
2226 let a = Arc::clone(&adapter_a);
2227 async move { Ok(a) }
2228 })
2229 .await
2230 .unwrap();
2231
2232 let factory_count_b = Arc::clone(&factory_call_count);
2233 let adapter_b = Arc::clone(&adapter);
2234 let (got_b, guard_b) =
2235 attach_shared_adapter(&pool, "cql", "phase_B", key.clone(), move || {
2236 factory_count_b.fetch_add(1, Ordering::AcqRel);
2237 let a = Arc::clone(&adapter_b);
2238 async move { Ok(a) }
2239 })
2240 .await
2241 .unwrap();
2242
2243 // The factory MUST be called exactly once even
2244 // though two phases attached.
2245 assert_eq!(
2246 factory_call_count.load(Ordering::Acquire),
2247 1,
2248 "Shared policy MUST cache the factory output"
2249 );
2250 assert!(
2251 Arc::ptr_eq(&got_a, &got_b),
2252 "both attaches MUST surface the SAME Arc<dyn DriverAdapter>"
2253 );
2254 assert_eq!(pool.live_entries(), 1);
2255 assert_eq!(pool.total_attaches(), 2);
2256
2257 guard_a.detach().await.unwrap();
2258 assert_eq!(
2259 pool.live_entries(),
2260 1,
2261 "entry stays live while any guard holds it"
2262 );
2263 guard_b.detach().await.unwrap();
2264 assert_eq!(
2265 pool.live_entries(),
2266 1,
2267 "Shared-policy entry stays cached across phases — \
2268 session shutdown is the close trigger, not last detach"
2269 );
2270 pool.shutdown().await;
2271 assert_eq!(pool.live_entries(), 0, "shutdown drains the cached entry");
2272 }
2273
2274 #[test]
2275 fn declare_then_complete_pending_use_drains_to_zero() {
2276 // SRD-35 Push D counter lifecycle: each
2277 // `declare_pending_use` call increments per-key;
2278 // each `complete_pending_use` decrements (saturating
2279 // at 0) and reports whether the entry is now
2280 // eligible for close (`pending == 0 && live == 0`).
2281 // The pool's close trigger sits on `(pending, live)`
2282 // both being zero — this test exercises the
2283 // counter-only side without a real attach to keep
2284 // the assertion shape tight.
2285 let pool = Arc::new(ResourcePool::new());
2286 let k = ResourceKey::new("cql").with("hosts", "h1");
2287 assert_eq!(
2288 pool.pending_uses_for(&k),
2289 None,
2290 "no entry exists before declare"
2291 );
2292
2293 // Pre-map walker declares: 3 phases will use this key.
2294 pool.declare_pending_use(k.clone(), ResourceSharePolicy::Shared);
2295 pool.declare_pending_use(k.clone(), ResourceSharePolicy::Shared);
2296 pool.declare_pending_use(k.clone(), ResourceSharePolicy::Shared);
2297 assert_eq!(pool.pending_uses_for(&k), Some(3));
2298 assert_eq!(
2299 pool.live_entries(),
2300 1,
2301 "declare_pending_use creates the entry on first call"
2302 );
2303
2304 // Drain phases 1 and 2: not yet eligible for close.
2305 let eligible = pool.complete_pending_use(&k);
2306 assert!(!eligible, "pending == 2; not eligible yet");
2307 assert_eq!(pool.pending_uses_for(&k), Some(2));
2308
2309 let eligible = pool.complete_pending_use(&k);
2310 assert!(!eligible, "pending == 1; not eligible yet");
2311 assert_eq!(pool.pending_uses_for(&k), Some(1));
2312
2313 // Phase 3 drains: pending hits 0, live is 0 (no
2314 // real attach in this test) — eligible for close.
2315 let eligible = pool.complete_pending_use(&k);
2316 assert!(
2317 eligible,
2318 "pending == 0 && live == 0 — entry is eligible for close"
2319 );
2320 assert_eq!(pool.pending_uses_for(&k), Some(0));
2321
2322 // Saturation: extra completes are no-ops, not
2323 // underflow. Stays "eligible for close" because the
2324 // invariant still holds.
2325 let eligible = pool.complete_pending_use(&k);
2326 assert!(
2327 eligible,
2328 "saturating decrement: extra completes don't underflow"
2329 );
2330 assert_eq!(pool.pending_uses_for(&k), Some(0));
2331 }
2332
2333 #[tokio::test]
2334 async fn declare_pending_use_paired_with_attach_closes_promptly() {
2335 // SRD-35 Push D end-to-end: pre-map seeds pending,
2336 // attach decrements eagerly, detach observes the
2337 // (pending == 0, live == 0) invariant and closes.
2338 // Verifies the close trigger fires the moment the
2339 // last predicted phase finishes, not at session end.
2340 let pool = Arc::new(ResourcePool::new());
2341 let mock = MockResource::new(key("cql", "cassandra-cpp"));
2342
2343 // Pre-map: exactly 1 phase will use this key.
2344 pool.declare_pending_use(mock.resource_key().clone(), ResourceSharePolicy::Shared);
2345
2346 let mock_for_factory = Arc::clone(&mock);
2347 let guard = attach(
2348 &pool,
2349 mock.resource_key().clone(),
2350 ResourceSharePolicy::Shared,
2351 "phase_A",
2352 ResourceFactory::new(
2353 move || async move { Ok(mock_for_factory as Arc<dyn SharedResource>) },
2354 ),
2355 )
2356 .await
2357 .unwrap();
2358 // After attach, pending dropped from 1 to 0.
2359 assert_eq!(pool.pending_uses_for(mock.resource_key()), Some(0));
2360
2361 guard.detach().await.unwrap();
2362 // Detach drove live to 0; pending was already 0;
2363 // close fired without waiting for shutdown.
2364 assert_eq!(
2365 mock.close_calls.load(Ordering::Acquire),
2366 1,
2367 "Push D: close fires the moment the last predicted \
2368 phase detaches — not at session shutdown"
2369 );
2370 assert_eq!(pool.live_entries(), 0);
2371 }
2372
2373 #[test]
2374 fn render_key_is_identity_preserving_unlike_log_format() {
2375 // SRD-104: `render_key()` is the accessor fingerprint — it must be
2376 // identity-preserving (NOT redact secrets), because two keys that
2377 // differ only in an identity-bearing field would otherwise collide
2378 // in the accessor. `fmt_for_log()` redacts (safe for logs) and so
2379 // deliberately collides; `render_key()` must not.
2380 let a = ResourceKey::new("cql")
2381 .with("hosts", "h1")
2382 .with("password", "p1");
2383 let b = ResourceKey::new("cql")
2384 .with("hosts", "h1")
2385 .with("password", "p2");
2386 assert_eq!(
2387 a.fmt_for_log(),
2388 b.fmt_for_log(),
2389 "log format redacts password → collides (expected)"
2390 );
2391 assert_ne!(
2392 a.render_key(),
2393 b.render_key(),
2394 "render_key must distinguish identity-bearing password"
2395 );
2396 assert!(
2397 !a.render_key().contains("p1") && a.render_key().contains("password=sha256:"),
2398 "the fingerprint must carry a digest of the secret, never the cleartext; got: {}",
2399 a.render_key()
2400 );
2401 // BTreeMap makes field order irrelevant to the rendering.
2402 let c = ResourceKey::new("cql")
2403 .with("password", "p1")
2404 .with("hosts", "h1");
2405 assert_eq!(
2406 a.render_key(),
2407 c.render_key(),
2408 "render_key is field-order independent"
2409 );
2410 }
2411
2412 #[tokio::test]
2413 async fn accessor_payload_populates_and_looks_up_by_render_key() {
2414 // SRD-104 pool-side round-trip: a resource that opts into an
2415 // accessor payload has it captured onto its entry at init, and the
2416 // pool resolves it by the key's canonical `render_key()` rendering
2417 // — the same string a consumer node builds from the fingerprint in
2418 // scope. Exercises the pool half of the bridge directly, without the
2419 // process-global (which would race across parallel tests).
2420 struct WithPayload {
2421 key: ResourceKey,
2422 }
2423 impl SharedResource for WithPayload {
2424 fn resource_key(&self) -> &ResourceKey {
2425 &self.key
2426 }
2427 fn accessor_payload(&self) -> Option<Arc<dyn Any + Send + Sync>> {
2428 Some(Arc::new(4242u64) as Arc<dyn Any + Send + Sync>)
2429 }
2430 }
2431
2432 let pool = Arc::new(ResourcePool::new());
2433 let key = ResourceKey::new("cql")
2434 .with("hosts", "h1")
2435 .with("keyspace", "ks");
2436 let rendered = key.render_key();
2437
2438 // No entry yet → miss.
2439 assert!(
2440 pool.lookup_accessor_payload(&rendered).is_none(),
2441 "no live entry ⇒ None"
2442 );
2443
2444 let key_for_factory = key.clone();
2445 let guard = attach(
2446 &pool,
2447 key.clone(),
2448 ResourceSharePolicy::Shared,
2449 "phase_A",
2450 ResourceFactory::new(move || async move {
2451 Ok(Arc::new(WithPayload {
2452 key: key_for_factory,
2453 }) as Arc<dyn SharedResource>)
2454 }),
2455 )
2456 .await
2457 .unwrap();
2458
2459 // Hit by the canonical rendering; downcast to the concrete payload.
2460 let payload = pool
2461 .lookup_accessor_payload(&rendered)
2462 .expect("payload present after successful init");
2463 let n = payload
2464 .downcast_ref::<u64>()
2465 .expect("payload downcasts to u64");
2466 assert_eq!(*n, 4242);
2467
2468 // Detach with no pending uses drains the entry → miss again.
2469 guard.detach().await.unwrap();
2470 assert!(
2471 pool.lookup_accessor_payload(&rendered).is_none(),
2472 "closed entry ⇒ None"
2473 );
2474 }
2475
2476 #[tokio::test]
2477 async fn default_resource_has_no_accessor_payload() {
2478 // A resource that doesn't override `accessor_payload` (the trait
2479 // default) contributes no payload — the miss policy of the bridge.
2480 let pool = Arc::new(ResourcePool::new());
2481 let mock = MockResource::new(key("cql", "cassandra-cpp"));
2482 let rendered = mock.resource_key().render_key();
2483 let mock_for_factory = Arc::clone(&mock);
2484 let guard = attach(
2485 &pool,
2486 mock.resource_key().clone(),
2487 ResourceSharePolicy::Shared,
2488 "phase_A",
2489 ResourceFactory::new(
2490 move || async move { Ok(mock_for_factory as Arc<dyn SharedResource>) },
2491 ),
2492 )
2493 .await
2494 .unwrap();
2495 assert!(
2496 pool.lookup_accessor_payload(&rendered).is_none(),
2497 "default accessor_payload() ⇒ None even for a live entry"
2498 );
2499 guard.detach().await.unwrap();
2500 }
2501
2502 #[tokio::test]
2503 async fn legacy_adapter_resource_declares_non_shareable() {
2504 // Sanity: the bridge type used by Push A's
2505 // executor wiring declares itself non-shareable,
2506 // forcing PerPhase semantics for any adapter that
2507 // hasn't migrated yet.
2508 let key = ResourceKey::new("legacy");
2509 let adapter: Arc<dyn DriverAdapter> = Arc::new(LegacyDummy);
2510 let wrapped = Arc::new(LegacyAdapterResource::new(key, adapter));
2511 assert!(
2512 !wrapped.can_share(),
2513 "LegacyAdapterResource MUST declare can_share()=false"
2514 );
2515 }
2516}