Coverage for src/taipanstack/resilience/adaptive/orchestrator.py: 100%

206 statements  

« prev     ^ index     » next       coverage.py v7.15.1, created at 2026-11-23 17:43 +0000

1""" 

2Resilience Orchestrator — compose multiple patterns into a pipeline. 

3 

4Provides a fluent builder to combine bulkhead, circuit breaker, 

5retry, timeout, and fallback into a single execution pipeline. 

6 

7Execution order: bulkhead → circuit breaker → retry → timeout → fn → fallback. 

8""" 

9 

10from __future__ import annotations 

11 

12import asyncio 

13import logging 

14import math 

15from collections.abc import Awaitable, Callable 

16from typing import Generic, ParamSpec, TypeVar, cast 

17 

18from taipanstack.core.result import Err, Ok, Result 

19from taipanstack.resilience.adaptive.adaptive_breaker import AdaptiveCircuitBreaker 

20from taipanstack.resilience.adaptive.adaptive_retry import AdaptiveRetry 

21from taipanstack.resilience.adaptive.bulkhead import Bulkhead, BulkheadFullError 

22from taipanstack.resilience.circuit_breaker import ( 

23 CircuitBreaker, 

24 CircuitBreakerError, 

25) 

26from taipanstack.resilience.retry import RetryConfig, calculate_delay 

27 

28logger = logging.getLogger("taipanstack.resilience.adaptive.orchestrator") 

29 

30T = TypeVar("T") 

31E = TypeVar("E", bound=Exception) 

32P = ParamSpec("P") 

33 

34 

35class ResilienceOrchestrator(Generic[T]): 

36 """Compose resilience patterns into a single pipeline. 

37 

38 Provides a fluent builder API to add patterns in order. 

39 Execution proceeds through each configured layer. 

40 

41 Args: 

42 name: Pipeline name for logging. 

43 

44 Example: 

45 >>> orch = ( 

46 ... ResilienceOrchestrator("api") 

47 ... .with_bulkhead(max_concurrent=5) 

48 ... .with_circuit_breaker(breaker) 

49 ... .with_retry(RetryConfig(max_attempts=3)) 

50 ... .with_timeout(10.0) 

51 ... .with_fallback({"status": "cached"}) 

52 ... ) 

53 >>> result = await orch.execute(call_api, endpoint) 

54 

55 """ 

56 

57 def __init__(self, name: str = "default") -> None: 

58 """Initialize the orchestrator. 

59 

60 Args: 

61 name: Pipeline name. 

62 

63 """ 

64 self.name = name 

65 self._bulkhead: Bulkhead | None = None 

66 self._breaker: CircuitBreaker | None = None 

67 self._adaptive_breaker: AdaptiveCircuitBreaker | None = None 

68 self._retry_config: RetryConfig | None = None 

69 self._adaptive_retry: AdaptiveRetry | None = None 

70 self._timeout: float | None = None 

71 self._fallback_value: T | object = _SENTINEL 

72 

73 @staticmethod 

74 def _validate_timeout_param(timeout: float) -> None: 

75 """Validate timeout parameter.""" 

76 if ( 

77 not isinstance(timeout, (int, float)) 

78 or not math.isfinite(timeout) 

79 or timeout < 0 

80 ): 

81 raise ValueError("timeout must be a finite non-negative number") 

82 

83 def with_bulkhead( 

84 self, 

85 max_concurrent: int = 10, 

86 max_queue: int = 50, 

87 timeout: float = 30.0, 

88 ) -> ResilienceOrchestrator[T]: 

89 """Add a bulkhead concurrency limiter. 

90 

91 Args: 

92 max_concurrent: Max concurrent executions. 

93 max_queue: Max queued callers. 

94 timeout: Permit acquisition timeout. 

95 

96 Returns: 

97 self for chaining. 

98 

99 """ 

100 self._validate_timeout_param(timeout) 

101 self._bulkhead = Bulkhead( 

102 f"{self.name}-bulkhead", 

103 max_concurrent=max_concurrent, 

104 max_queue=max_queue, 

105 timeout=timeout, 

106 ) 

107 return self 

108 

109 def with_circuit_breaker( 

110 self, 

111 breaker: CircuitBreaker | AdaptiveCircuitBreaker, 

112 ) -> ResilienceOrchestrator[T]: 

113 """Add a circuit breaker. 

114 

115 Args: 

116 breaker: Standard or adaptive circuit breaker. 

117 

118 Returns: 

119 self for chaining. 

120 

121 """ 

122 if isinstance(breaker, AdaptiveCircuitBreaker): 

123 self._adaptive_breaker = breaker 

124 self._breaker = None 

125 else: 

126 self._breaker = breaker 

127 return self 

128 

129 def with_retry( 

130 self, 

131 config: RetryConfig | AdaptiveRetry, 

132 ) -> ResilienceOrchestrator[T]: 

133 """Add retry logic. 

134 

135 Args: 

136 config: Standard retry config or adaptive retry. 

137 

138 Returns: 

139 self for chaining. 

140 

141 """ 

142 if isinstance(config, AdaptiveRetry): 

143 self._adaptive_retry = config 

144 self._retry_config = config.to_retry_config() 

145 else: 

146 self._retry_config = config 

147 return self 

148 

149 def with_timeout(self, seconds: float) -> ResilienceOrchestrator[T]: 

150 """Add a timeout. 

151 

152 Args: 

153 seconds: Maximum execution time. 

154 

155 Returns: 

156 self for chaining. 

157 

158 """ 

159 self._validate_timeout_param(seconds) 

160 self._timeout = seconds 

161 return self 

162 

163 def with_fallback(self, value: T) -> ResilienceOrchestrator[T]: 

164 """Add a fallback value for failures. 

165 

166 Args: 

167 value: Value to return on failure. 

168 

169 Returns: 

170 self for chaining. 

171 

172 """ 

173 self._fallback_value = value 

174 return self 

175 

176 async def _acquire_bulkhead(self, bh: Bulkhead) -> Result[None, Exception]: 

177 """Attempt to acquire a bulkhead permit, handling timeouts and errors.""" 

178 return await bh._acquire_permit() 

179 

180 async def _attempt_bulkhead_acquire( 

181 self, 

182 bh: Bulkhead, 

183 ) -> Result[None, Exception]: 

184 """Attempt to queue and acquire a bulkhead permit.""" 

185 bh._queued += 1 

186 try: 

187 acquire_result = await self._acquire_bulkhead(bh) 

188 if isinstance(acquire_result, Err): 

189 return Err(acquire_result.unwrap_err()) 

190 return Ok(None) 

191 finally: 

192 bh._queued -= 1 

193 

194 async def _execute_bulkhead_inner( 

195 self, 

196 bh: Bulkhead, 

197 fn: Callable[P, Awaitable[T]], 

198 *args: P.args, 

199 **kwargs: P.kwargs, 

200 ) -> Result[T, Exception]: 

201 """Execute the inner function while holding the bulkhead permit.""" 

202 bh._active += 1 

203 try: 

204 return await self._execute_inner(fn, *args, **kwargs) 

205 except Exception as exc: 

206 return self._apply_fallback(Err(exc)) 

207 finally: 

208 bh._active -= 1 

209 bh._semaphore.release() 

210 

211 async def _execute_with_bulkhead( 

212 self, 

213 bh: Bulkhead, 

214 fn: Callable[P, Awaitable[T]], 

215 *args: P.args, 

216 **kwargs: P.kwargs, 

217 ) -> Result[T, Exception]: 

218 """Execute through the bulkhead layer.""" 

219 if bh.queued >= bh._max_queue: 

220 result: Result[T, Exception] = Err( 

221 BulkheadFullError(bh.name, bh._max_concurrent, bh._max_queue), 

222 ) 

223 return self._apply_fallback(result) 

224 

225 acquire_result = await self._attempt_bulkhead_acquire(bh) 

226 if isinstance(acquire_result, Err): 

227 return self._apply_fallback(cast(Result[T, Exception], acquire_result)) 

228 

229 return await self._execute_bulkhead_inner(bh, fn, *args, **kwargs) 

230 

231 async def execute( 

232 self, 

233 fn: Callable[P, Awaitable[T]], 

234 *args: P.args, 

235 **kwargs: P.kwargs, 

236 ) -> Result[T, Exception]: 

237 """Execute the function through the resilience pipeline. 

238 

239 Order: bulkhead → circuit breaker → retry → timeout → fn → fallback. 

240 

241 Args: 

242 fn: Async callable to execute. 

243 *args: Positional arguments. 

244 **kwargs: Keyword arguments. 

245 

246 Returns: 

247 ``Ok(result)`` on success, ``Err`` on failure. 

248 

249 """ 

250 # Layer 1: Bulkhead — use semaphore directly to avoid double-wrapping 

251 if self._bulkhead is not None: 

252 return await self._execute_with_bulkhead( 

253 self._bulkhead, 

254 fn, 

255 *args, 

256 **kwargs, 

257 ) 

258 

259 try: 

260 return await self._execute_inner(fn, *args, **kwargs) 

261 except Exception as exc: 

262 return self._apply_fallback(Err(exc)) 

263 

264 def _evaluate_adaptive_breaker(self) -> Err[Exception] | None: 

265 if ( 

266 self._adaptive_breaker is not None 

267 and not self._adaptive_breaker.should_allow() 

268 ): 

269 return Err( 

270 CircuitBreakerError( 

271 f"Circuit '{self._adaptive_breaker.name}' is open", 

272 state=self._adaptive_breaker.state, 

273 ), 

274 ) 

275 return None 

276 

277 def _evaluate_standard_breaker(self) -> Err[Exception] | None: 

278 if self._breaker is not None and not self._breaker._should_attempt(): 

279 return Err( 

280 CircuitBreakerError( 

281 f"Circuit '{self._breaker.name}' is open", 

282 state=self._breaker.state, 

283 ), 

284 ) 

285 return None 

286 

287 def _evaluate_circuit_breaker(self) -> Err[Exception] | None: 

288 """Check if execution is allowed by the circuit breaker.""" 

289 if self._adaptive_breaker is not None: 

290 return self._evaluate_adaptive_breaker() 

291 return self._evaluate_standard_breaker() 

292 

293 def _record_breaker_success(self) -> None: 

294 if self._adaptive_breaker is not None: 

295 self._adaptive_breaker.record_success() 

296 elif self._breaker is not None: 

297 self._breaker._record_success() 

298 

299 def _record_success_outcome(self, attempt: int) -> None: 

300 """Record a successful execution outcome.""" 

301 self._record_breaker_success() 

302 if self._adaptive_retry is not None: 

303 self._adaptive_retry.record_outcome(attempt, True, 0.0) 

304 

305 def _record_breaker_failure(self, error: Exception) -> None: 

306 if self._adaptive_breaker is not None: 

307 self._adaptive_breaker.record_failure(error) 

308 elif self._breaker is not None: 

309 self._breaker._record_failure(error) 

310 

311 def _record_failure_outcome(self, error: Exception, attempt: int) -> None: 

312 """Record a failed execution outcome.""" 

313 self._record_breaker_failure(error) 

314 if self._adaptive_retry is not None: 

315 self._adaptive_retry.record_outcome(attempt, False, 0.0) 

316 

317 def _calculate_retry_delay(self, attempt: int) -> float: 

318 """Calculate the retry delay for the given attempt.""" 

319 if self._adaptive_retry is not None: 

320 return self._adaptive_retry.get_delay(attempt) 

321 if self._retry_config is not None: 

322 return calculate_delay(attempt, self._retry_config) 

323 return 0.0 

324 

325 async def _execute_inner( 

326 self, 

327 fn: Callable[P, Awaitable[T]], 

328 *args: P.args, 

329 **kwargs: P.kwargs, 

330 ) -> Result[T, Exception]: 

331 """Execute through breaker → retry → timeout → fn layers.""" 

332 max_attempts = ( 

333 self._retry_config.max_attempts if self._retry_config is not None else 1 

334 ) 

335 return await self._execute_with_retries(max_attempts, fn, *args, **kwargs) 

336 

337 async def _handle_retry_failure( 

338 self, 

339 error: Exception, 

340 attempt: int, 

341 max_attempts: int, 

342 ) -> bool: 

343 self._record_failure_outcome(error, attempt) 

344 if self._retry_config is not None and attempt < max_attempts: 

345 delay = self._calculate_retry_delay(attempt) 

346 await asyncio.sleep(min(delay, 3600.0)) 

347 return True 

348 return False 

349 

350 def _handle_circuit_breaker_open( 

351 self, 

352 cb_err: Err[Exception], 

353 attempt: int, 

354 ) -> Result[T, Exception]: 

355 """Handle circuit breaker evaluation logic.""" 

356 if attempt == 1: 

357 return self._apply_fallback(cb_err) 

358 return cb_err 

359 

360 async def _execute_single_attempt( 

361 self, 

362 attempt: int, 

363 fn: Callable[P, Awaitable[T]], 

364 *args: P.args, 

365 **kwargs: P.kwargs, 

366 ) -> Result[T, Exception]: 

367 """Execute a single attempt including timeout.""" 

368 result = await self._execute_with_timeout(fn, *args, **kwargs) 

369 if isinstance(result, Ok): 

370 self._record_success_outcome(attempt) 

371 return result 

372 

373 def _check_circuit_breaker_for_attempt( 

374 self, 

375 attempt: int, 

376 ) -> Result[T, Exception] | None: 

377 """Evaluate circuit breaker for the current attempt.""" 

378 cb_err = self._evaluate_circuit_breaker() 

379 if cb_err is not None: 

380 return self._handle_circuit_breaker_open(cb_err, attempt) 

381 return None 

382 

383 def _check_cb_res( 

384 self, cb_res: Result[T, Exception] | None 

385 ) -> tuple[bool, Exception] | Result[T, Exception] | None: 

386 if cb_res is not None: 

387 if isinstance(cb_res, Err): 

388 return False, cb_res.err_value 

389 return cb_res 

390 return None 

391 

392 async def _process_retry_attempt( 

393 self, 

394 attempt: int, 

395 max_attempts: int, 

396 fn: Callable[P, Awaitable[T]], 

397 *args: P.args, 

398 **kwargs: P.kwargs, 

399 ) -> Result[T, Exception] | tuple[bool, Exception]: 

400 """Process a single retry attempt including circuit breaker check.""" 

401 cb_res = self._check_circuit_breaker_for_attempt(attempt) 

402 cb_check = self._check_cb_res(cb_res) 

403 if cb_check is not None: 

404 return cb_check 

405 

406 result = await self._execute_single_attempt(attempt, fn, *args, **kwargs) 

407 if isinstance(result, Ok): 

408 return result 

409 

410 last_error = result.err_value 

411 should_continue = await self._handle_retry_failure( 

412 last_error, attempt, max_attempts 

413 ) 

414 return should_continue, last_error 

415 

416 def _evaluate_retry_outcome( 

417 self, outcome: Result[T, Exception] | tuple[bool, Exception] 

418 ) -> tuple[bool, Exception, Result[T, Exception] | None]: 

419 if not isinstance(outcome, tuple): 

420 return True, RuntimeError("Success"), outcome 

421 

422 should_continue, last_error = outcome 

423 return not should_continue, last_error, None 

424 

425 async def _handle_retry_loop_outcome( 

426 self, 

427 attempt: int, 

428 max_attempts: int, 

429 fn: Callable[P, Awaitable[T]], 

430 *args: P.args, 

431 **kwargs: P.kwargs, 

432 ) -> tuple[bool, Exception, Result[T, Exception] | None]: 

433 outcome = await self._process_retry_attempt( 

434 attempt, max_attempts, fn, *args, **kwargs 

435 ) 

436 return self._evaluate_retry_outcome(outcome) 

437 

438 def _check_retry_loop_break( 

439 self, 

440 is_finished: bool, 

441 result: Result[T, Exception] | None, 

442 ) -> bool: 

443 return result is not None or is_finished 

444 

445 async def _execute_retry_loop( 

446 self, 

447 max_attempts: int, 

448 fn: Callable[P, Awaitable[T]], 

449 *args: P.args, 

450 **kwargs: P.kwargs, 

451 ) -> tuple[Result[T, Exception] | None, Exception]: 

452 last_error: Exception = RuntimeError("Execution failed") 

453 

454 for attempt in range(1, max_attempts + 1): 

455 is_finished, last_error, result = await self._handle_retry_loop_outcome( 

456 attempt, max_attempts, fn, *args, **kwargs 

457 ) 

458 if self._check_retry_loop_break(is_finished, result): 

459 return result, last_error 

460 

461 return None, last_error 

462 

463 async def _execute_with_retries( 

464 self, 

465 max_attempts: int, 

466 fn: Callable[P, Awaitable[T]], 

467 *args: P.args, 

468 **kwargs: P.kwargs, 

469 ) -> Result[T, Exception]: 

470 result, last_error = await self._execute_retry_loop( 

471 max_attempts, fn, *args, **kwargs 

472 ) 

473 if result is not None: 

474 return result 

475 return self._apply_fallback(Err(last_error)) 

476 

477 async def _run_fn_with_timeout( 

478 self, 

479 fn: Callable[P, Awaitable[T]], 

480 *args: P.args, 

481 **kwargs: P.kwargs, 

482 ) -> T | Result[T, Exception]: 

483 if self._timeout is not None: 

484 return await asyncio.wait_for( 

485 fn(*args, **kwargs), 

486 timeout=self._timeout, 

487 ) 

488 return await fn(*args, **kwargs) 

489 

490 def _process_timeout_success_result( 

491 self, 

492 result: T | Result[T, Exception], 

493 ) -> Result[T, Exception]: 

494 """Process successful results from timeout execution.""" 

495 if isinstance(result, (Ok, Err)): 

496 return cast(Result[T, Exception], result) 

497 return Ok(result) 

498 

499 async def _execute_with_timeout( 

500 self, 

501 fn: Callable[P, Awaitable[T]], 

502 *args: P.args, 

503 **kwargs: P.kwargs, 

504 ) -> Result[T, Exception]: 

505 """Execute fn with optional timeout. 

506 

507 Args: 

508 fn: Async callable. 

509 *args: Positional arguments. 

510 **kwargs: Keyword arguments. 

511 

512 Returns: 

513 ``Ok(result)`` or ``Err``. 

514 

515 """ 

516 try: 

517 result = await self._run_fn_with_timeout(fn, *args, **kwargs) 

518 return self._process_timeout_success_result(result) 

519 except TimeoutError: 

520 return Err( 

521 TimeoutError( 

522 f"Pipeline '{self.name}' timed out after {self._timeout}s" 

523 ), 

524 ) 

525 except Exception as exc: 

526 return Err(exc) 

527 

528 def _apply_fallback( 

529 self, 

530 result: Result[T, Exception], 

531 ) -> Result[T, Exception]: 

532 """Apply fallback if configured and result is Err. 

533 

534 Args: 

535 result: The result to potentially replace. 

536 

537 Returns: 

538 Original result or ``Ok(fallback_value)``. 

539 

540 """ 

541 if isinstance(result, Err) and self._fallback_value is not _SENTINEL: 

542 return Ok(cast(T, self._fallback_value)) 

543 return result 

544 

545 

546# Sentinel for distinguishing "no fallback" from "fallback=None" 

547_SENTINEL = object()