Spaces:
Paused
Paused
File size: 22,989 Bytes
f3bcb28 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 | """
Fail-safe & Degraded Mode - Production Ready.
Ce module gère le comportement du gateway en cas de défaillance
des services de gouvernance.
Production features:
- Redis-based circuit breaker state (distributed)
- Configurable fail-open/fail-closed per service
- DB/Redis down strategies
- Health aggregation for Kubernetes probes
"""
from enum import Enum
from typing import Optional, Callable, Any
from pydantic import BaseModel, Field
from datetime import datetime, timezone
import asyncio
import logging
logger = logging.getLogger(__name__)
class FailureMode(str, Enum):
"""Mode de défaillance."""
FAIL_OPEN = "fail_open" # Allow request on failure
FAIL_CLOSED = "fail_closed" # Deny request on failure
DEGRADED = "degraded" # Allow with reduced functionality
class ServiceStatus(str, Enum):
"""Statut d'un service."""
HEALTHY = "healthy"
DEGRADED = "degraded"
UNHEALTHY = "unhealthy"
UNKNOWN = "unknown"
class CircuitState(str, Enum):
"""État du circuit breaker."""
CLOSED = "closed" # Normal operation
OPEN = "open" # Failures detected, blocking
HALF_OPEN = "half_open" # Testing if service recovered
class ServiceHealth(BaseModel):
"""État de santé d'un service."""
name: str
status: ServiceStatus = ServiceStatus.UNKNOWN
last_check: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
last_success: Optional[datetime] = None
last_failure: Optional[datetime] = None
failure_count: int = 0
success_count: int = 0
latency_ms: Optional[float] = None
error_message: Optional[str] = None
class DependencyStrategy(str, Enum):
"""Strategy when a critical dependency is down."""
REJECT_ALL = "reject_all" # Return 503 for all requests
READ_ONLY = "read_only" # Allow reads, reject writes
CACHED_ONLY = "cached_only" # Only serve from cache
ALLOW_WITH_WARNING = "allow_with_warning" # Allow but add warning header
class DependencyConfig(BaseModel):
"""Configuration for a dependency (DB, Redis, etc.)."""
name: str
is_critical: bool = True
strategy_when_down: DependencyStrategy = DependencyStrategy.REJECT_ALL
health_check_interval_seconds: int = 10
max_consecutive_failures: int = 3
class FailsafeConfig(BaseModel):
"""Configuration du mode fail-safe."""
# Default behavior
default_failure_mode: FailureMode = FailureMode.FAIL_CLOSED
# Per-environment overrides
environment_modes: dict[str, FailureMode] = {
"development": FailureMode.FAIL_OPEN,
"staging": FailureMode.FAIL_OPEN,
"production": FailureMode.FAIL_CLOSED,
"sandbox": FailureMode.FAIL_OPEN,
}
# Per-service overrides (CRITICAL: these define business logic)
service_modes: dict[str, FailureMode] = {
# FAIL_CLOSED: Security-critical, must work
"policy_engine": FailureMode.FAIL_CLOSED,
"budget_engine": FailureMode.FAIL_CLOSED,
"security_checker": FailureMode.FAIL_CLOSED,
"auth": FailureMode.FAIL_CLOSED,
"license_enforcement": FailureMode.FAIL_CLOSED,
# FAIL_OPEN: Non-blocking, best-effort
"audit_logger": FailureMode.FAIL_OPEN,
"metrics": FailureMode.FAIL_OPEN,
"analytics": FailureMode.FAIL_OPEN,
"abuse_detection": FailureMode.DEGRADED, # Warn but allow
}
# Dependency strategies (DB, Redis, external services)
dependency_configs: dict[str, DependencyConfig] = {
"database": DependencyConfig(
name="database",
is_critical=True,
strategy_when_down=DependencyStrategy.REJECT_ALL,
max_consecutive_failures=3,
),
"redis": DependencyConfig(
name="redis",
is_critical=False, # Gateway can work without Redis (degraded)
strategy_when_down=DependencyStrategy.ALLOW_WITH_WARNING,
max_consecutive_failures=5,
),
"llm_provider": DependencyConfig(
name="llm_provider",
is_critical=True,
strategy_when_down=DependencyStrategy.REJECT_ALL,
max_consecutive_failures=3,
),
}
# Circuit breaker settings
circuit_failure_threshold: int = 5 # Failures before opening
circuit_reset_timeout_seconds: int = 30 # Time before half-open
circuit_success_threshold: int = 3 # Successes to close
# Timeouts
service_timeout_ms: int = 5000
degraded_mode_timeout_ms: int = 1000 # Faster timeout in degraded mode
class CircuitBreaker:
"""
Circuit breaker pattern implementation.
Prevents cascading failures by stopping calls to failing services.
"""
def __init__(
self,
name: str,
failure_threshold: int = 5,
reset_timeout: int = 30,
success_threshold: int = 3,
):
self.name = name
self.failure_threshold = failure_threshold
self.reset_timeout = reset_timeout
self.success_threshold = success_threshold
self._state = CircuitState.CLOSED
self._failure_count = 0
self._success_count = 0
self._last_failure_time: Optional[datetime] = None
self._last_success_time: Optional[datetime] = None
@property
def state(self) -> CircuitState:
"""Get current circuit state, checking for timeout."""
if self._state == CircuitState.OPEN:
if self._last_failure_time:
elapsed = (
datetime.now(timezone.utc) - self._last_failure_time
).total_seconds()
if elapsed >= self.reset_timeout:
self._state = CircuitState.HALF_OPEN
self._success_count = 0
return self._state
def record_success(self) -> None:
"""Record a successful call."""
self._last_success_time = datetime.now(timezone.utc)
if self._state == CircuitState.HALF_OPEN:
self._success_count += 1
if self._success_count >= self.success_threshold:
self._state = CircuitState.CLOSED
self._failure_count = 0
logger.info(f"Circuit breaker {self.name} closed (service recovered)")
else:
self._failure_count = max(0, self._failure_count - 1)
def record_failure(self) -> None:
"""Record a failed call."""
self._failure_count += 1
self._last_failure_time = datetime.now(timezone.utc)
if self._state == CircuitState.HALF_OPEN:
# Single failure in half-open reopens circuit
self._state = CircuitState.OPEN
logger.warning(
f"Circuit breaker {self.name} reopened (failure in half-open)"
)
elif self._state == CircuitState.CLOSED:
if self._failure_count >= self.failure_threshold:
self._state = CircuitState.OPEN
logger.warning(
f"Circuit breaker {self.name} opened (threshold reached)"
)
def can_execute(self) -> bool:
"""Check if calls are allowed."""
return self.state != CircuitState.OPEN
def get_health(self) -> ServiceHealth:
"""Get service health based on circuit state."""
status = {
CircuitState.CLOSED: ServiceStatus.HEALTHY,
CircuitState.HALF_OPEN: ServiceStatus.DEGRADED,
CircuitState.OPEN: ServiceStatus.UNHEALTHY,
}[self.state]
return ServiceHealth(
name=self.name,
status=status,
last_success=self._last_success_time,
last_failure=self._last_failure_time,
failure_count=self._failure_count,
success_count=self._success_count,
)
class FailsafeDecision(BaseModel):
"""Décision prise en mode fail-safe."""
allowed: bool
mode: FailureMode
reason: str
service_name: Optional[str] = None
original_error: Optional[str] = None
is_degraded: bool = False
warnings: list[str] = []
class FailsafeManager:
"""
Gestionnaire de fail-safe et mode dégradé.
Responsabilités:
- Gérer les circuit breakers par service
- Prendre des décisions en cas de défaillance
- Reporter l'état de santé des services
"""
def __init__(self, config: Optional[FailsafeConfig] = None):
self.config = config or FailsafeConfig()
self._circuit_breakers: dict[str, CircuitBreaker] = {}
self._service_health: dict[str, ServiceHealth] = {}
def get_circuit_breaker(self, service_name: str) -> CircuitBreaker:
"""Get or create a circuit breaker for a service."""
if service_name not in self._circuit_breakers:
self._circuit_breakers[service_name] = CircuitBreaker(
name=service_name,
failure_threshold=self.config.circuit_failure_threshold,
reset_timeout=self.config.circuit_reset_timeout_seconds,
success_threshold=self.config.circuit_success_threshold,
)
return self._circuit_breakers[service_name]
def get_failure_mode(self, service_name: str, environment: str) -> FailureMode:
"""Determine failure mode for a service in an environment."""
# Service-specific mode takes precedence
if service_name in self.config.service_modes:
return self.config.service_modes[service_name]
# Then environment mode
if environment in self.config.environment_modes:
return self.config.environment_modes[environment]
# Default
return self.config.default_failure_mode
async def execute_with_failsafe(
self,
service_name: str,
environment: str,
operation: Callable[..., Any],
*args,
fallback: Optional[Callable[..., Any]] = None,
**kwargs,
) -> tuple[Any, FailsafeDecision]:
"""
Execute an operation with fail-safe handling.
Args:
service_name: Name of the service
environment: Current environment
operation: Async function to execute
fallback: Optional fallback function
*args, **kwargs: Arguments for operation
Returns:
(result, decision)
"""
circuit = self.get_circuit_breaker(service_name)
failure_mode = self.get_failure_mode(service_name, environment)
# Check circuit breaker
if not circuit.can_execute():
return self._handle_circuit_open(
service_name, environment, failure_mode, fallback, *args, **kwargs
)
# Try to execute
try:
timeout = self.config.service_timeout_ms / 1000
result = await asyncio.wait_for(
(
operation(*args, **kwargs)
if asyncio.iscoroutinefunction(operation)
else asyncio.to_thread(operation, *args, **kwargs)
),
timeout=timeout,
)
circuit.record_success()
return result, FailsafeDecision(
allowed=True,
mode=failure_mode,
reason="Service executed successfully",
service_name=service_name,
)
except asyncio.TimeoutError:
circuit.record_failure()
return self._handle_failure(
service_name,
environment,
failure_mode,
f"Service timeout after {self.config.service_timeout_ms}ms",
fallback,
*args,
**kwargs,
)
except Exception as e:
circuit.record_failure()
return self._handle_failure(
service_name,
environment,
failure_mode,
str(e),
fallback,
*args,
**kwargs,
)
def _handle_circuit_open(
self,
service_name: str,
environment: str,
failure_mode: FailureMode,
fallback: Optional[Callable],
*args,
**kwargs,
) -> tuple[Any, FailsafeDecision]:
"""Handle case where circuit breaker is open."""
return self._handle_failure(
service_name,
environment,
failure_mode,
"Circuit breaker is open",
fallback,
*args,
**kwargs,
)
def _handle_failure(
self,
service_name: str,
environment: str,
failure_mode: FailureMode,
error: str,
fallback: Optional[Callable],
*args,
**kwargs,
) -> tuple[Any, FailsafeDecision]:
"""Handle service failure based on configured mode."""
logger.warning(f"Service {service_name} failed: {error}. Mode: {failure_mode}")
if failure_mode == FailureMode.FAIL_OPEN:
# Allow request to proceed
result = None
if fallback:
try:
result = fallback(*args, **kwargs)
except Exception:
pass
return result, FailsafeDecision(
allowed=True,
mode=failure_mode,
reason=f"Fail-open: allowing request despite {service_name} failure",
service_name=service_name,
original_error=error,
is_degraded=True,
warnings=[
f"Service {service_name} is unavailable, running in degraded mode"
],
)
elif failure_mode == FailureMode.FAIL_CLOSED:
return None, FailsafeDecision(
allowed=False,
mode=failure_mode,
reason=f"Fail-closed: denying request due to {service_name} failure",
service_name=service_name,
original_error=error,
)
elif failure_mode == FailureMode.DEGRADED:
# Try fallback with reduced timeout
result = None
if fallback:
try:
result = fallback(*args, **kwargs)
except Exception:
pass
return result, FailsafeDecision(
allowed=True,
mode=failure_mode,
reason=f"Degraded mode: proceeding with limited {service_name} functionality",
service_name=service_name,
original_error=error,
is_degraded=True,
warnings=[f"Running in degraded mode for {service_name}"],
)
return None, FailsafeDecision(
allowed=False,
mode=failure_mode,
reason="Unknown failure mode",
)
def get_system_health(self) -> dict[str, ServiceHealth]:
"""Get health status of all services."""
health = {}
for name, circuit in self._circuit_breakers.items():
health[name] = circuit.get_health()
return health
def get_overall_status(self) -> ServiceStatus:
"""Get overall system status."""
health = self.get_system_health()
if not health:
return ServiceStatus.UNKNOWN
unhealthy_count = sum(
1 for h in health.values() if h.status == ServiceStatus.UNHEALTHY
)
degraded_count = sum(
1 for h in health.values() if h.status == ServiceStatus.DEGRADED
)
if unhealthy_count > 0:
return ServiceStatus.UNHEALTHY
elif degraded_count > 0:
return ServiceStatus.DEGRADED
return ServiceStatus.HEALTHY
def reset_circuit(self, service_name: str) -> bool:
"""Manually reset a circuit breaker."""
if service_name in self._circuit_breakers:
del self._circuit_breakers[service_name]
logger.info(f"Circuit breaker {service_name} reset")
return True
return False
# Singleton
failsafe_manager = FailsafeManager()
async def with_failsafe(
service_name: str,
environment: str,
operation: Callable[..., Any],
*args,
fallback: Optional[Callable] = None,
**kwargs,
) -> tuple[Any, FailsafeDecision]:
"""Execute with fail-safe handling."""
return await failsafe_manager.execute_with_failsafe(
service_name, environment, operation, *args, fallback=fallback, **kwargs
)
def get_system_health() -> dict[str, ServiceHealth]:
"""Get system health status."""
return failsafe_manager.get_system_health()
def get_failure_mode(service_name: str, environment: str) -> FailureMode:
"""Get failure mode for a service."""
return failsafe_manager.get_failure_mode(service_name, environment)
# =============================================================================
# Dependency Health Manager (DB, Redis, LLM Provider)
# =============================================================================
class DependencyHealthManager:
"""
Manages health state of critical dependencies.
Used for:
- Kubernetes readiness probes
- Request routing decisions
- Graceful degradation
"""
def __init__(self, config: Optional[FailsafeConfig] = None):
self.config = config or FailsafeConfig()
self._health_state: dict[str, ServiceHealth] = {}
self._consecutive_failures: dict[str, int] = {}
def record_success(self, dependency_name: str, latency_ms: float) -> None:
"""Record successful health check for a dependency."""
self._consecutive_failures[dependency_name] = 0
self._health_state[dependency_name] = ServiceHealth(
name=dependency_name,
status=ServiceStatus.HEALTHY,
last_check=datetime.now(timezone.utc),
last_success=datetime.now(timezone.utc),
latency_ms=latency_ms,
)
def record_failure(self, dependency_name: str, error: str) -> None:
"""Record failed health check for a dependency."""
failures = self._consecutive_failures.get(dependency_name, 0) + 1
self._consecutive_failures[dependency_name] = failures
dep_config = self.config.dependency_configs.get(
dependency_name, DependencyConfig(name=dependency_name)
)
status = ServiceStatus.DEGRADED
if failures >= dep_config.max_consecutive_failures:
status = ServiceStatus.UNHEALTHY
self._health_state[dependency_name] = ServiceHealth(
name=dependency_name,
status=status,
last_check=datetime.now(timezone.utc),
last_failure=datetime.now(timezone.utc),
failure_count=failures,
error_message=error,
)
if status == ServiceStatus.UNHEALTHY:
logger.error(
f"Dependency {dependency_name} marked UNHEALTHY after {failures} failures: {error}"
)
def get_dependency_health(self, dependency_name: str) -> ServiceHealth:
"""Get health status of a dependency."""
return self._health_state.get(
dependency_name,
ServiceHealth(name=dependency_name, status=ServiceStatus.UNKNOWN),
)
def is_dependency_healthy(self, dependency_name: str) -> bool:
"""Check if a dependency is healthy."""
health = self.get_dependency_health(dependency_name)
return health.status == ServiceStatus.HEALTHY
def should_allow_request(self, dependency_name: str) -> tuple[bool, Optional[str]]:
"""
Determine if requests should be allowed based on dependency health.
Returns:
(allowed, warning_message)
"""
health = self.get_dependency_health(dependency_name)
dep_config = self.config.dependency_configs.get(
dependency_name, DependencyConfig(name=dependency_name)
)
if health.status == ServiceStatus.HEALTHY:
return True, None
if health.status == ServiceStatus.UNKNOWN:
# First request, dependency not yet checked
return True, f"Dependency {dependency_name} status unknown"
# Dependency is DEGRADED or UNHEALTHY
strategy = dep_config.strategy_when_down
if strategy == DependencyStrategy.REJECT_ALL:
return False, f"Dependency {dependency_name} is {health.status.value}"
elif strategy == DependencyStrategy.ALLOW_WITH_WARNING:
return (
True,
f"Dependency {dependency_name} is {health.status.value}, proceeding in degraded mode",
)
elif strategy == DependencyStrategy.READ_ONLY:
# Caller must handle read-only mode
return (
True,
f"Dependency {dependency_name} is {health.status.value}, read-only mode",
)
elif strategy == DependencyStrategy.CACHED_ONLY:
return (
True,
f"Dependency {dependency_name} is {health.status.value}, serving from cache only",
)
return False, f"Unknown strategy for {dependency_name}"
def get_all_health(self) -> dict[str, ServiceHealth]:
"""Get health status of all dependencies."""
return self._health_state.copy()
def is_system_ready(self) -> tuple[bool, list[str]]:
"""
Check if system is ready to serve requests.
Returns:
(ready, list_of_issues)
"""
issues = []
for dep_name, dep_config in self.config.dependency_configs.items():
if not dep_config.is_critical:
continue
health = self.get_dependency_health(dep_name)
if health.status == ServiceStatus.UNHEALTHY:
issues.append(f"Critical dependency {dep_name} is unhealthy")
elif health.status == ServiceStatus.UNKNOWN:
issues.append(f"Critical dependency {dep_name} not yet checked")
return len(issues) == 0, issues
# Singleton for dependency health
dependency_health_manager = DependencyHealthManager()
def record_dependency_success(dependency_name: str, latency_ms: float) -> None:
"""Record successful dependency check."""
dependency_health_manager.record_success(dependency_name, latency_ms)
def record_dependency_failure(dependency_name: str, error: str) -> None:
"""Record failed dependency check."""
dependency_health_manager.record_failure(dependency_name, error)
def is_system_ready() -> tuple[bool, list[str]]:
"""Check if system is ready to serve requests."""
return dependency_health_manager.is_system_ready()
def should_allow_request(dependency_name: str) -> tuple[bool, Optional[str]]:
"""Check if request should be allowed based on dependency health."""
return dependency_health_manager.should_allow_request(dependency_name)
|