Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mattyopon_faultray /src /faultray /model /graph.py
| # Copyright (c) 2025-2026 Yutaro Maeda. All rights reserved. | |
| # Licensed under the Apache License 2.0. See LICENSE file for details. | |
| """Dependency graph for infrastructure components.""" | |
| from __future__ import annotations | |
| import json | |
| from collections import deque | |
| from pathlib import Path | |
| import networkx as nx | |
| from .components import SCHEMA_VERSION, Component, ComponentType, Dependency | |
| _MAX_COMPONENTS = 10_000 # DoS guard — prevent unbounded memory growth | |
| _MAX_JSON_BYTES = 10 * 1024 * 1024 # 10 MB — JSON input cap (parity with YAML) | |
| class InfraGraph: | |
| """Directed graph of infrastructure components and their dependencies.""" | |
| def __init__(self) -> None: | |
| self._graph: nx.DiGraph[str] = nx.DiGraph() | |
| self._components: dict[str, Component] = {} | |
| # Memoized get_critical_paths() results, keyed by max_paths. | |
| # Invalidated on every topology mutation. | |
| self._critical_paths_cache: dict[int, list[list[str]]] = {} | |
| def components(self) -> dict[str, Component]: | |
| return self._components | |
| def add_component(self, component: Component) -> None: | |
| if len(self._components) >= _MAX_COMPONENTS: | |
| raise ValueError( | |
| f"Component limit reached ({_MAX_COMPONENTS}). " | |
| "Cannot add more components to this graph." | |
| ) | |
| self._components[component.id] = component | |
| self._graph.add_node(component.id, component=component) | |
| self._critical_paths_cache.clear() | |
| def add_dependency(self, dep: Dependency) -> None: | |
| self._graph.add_edge( | |
| dep.source_id, | |
| dep.target_id, | |
| dependency=dep, | |
| ) | |
| self._critical_paths_cache.clear() | |
| def remove_dependency(self, source_id: str, target_id: str) -> bool: | |
| """Remove the dependency edge from *source_id* to *target_id*. | |
| Returns True if an edge was removed, False if none existed. Use this | |
| instead of mutating the underlying networkx graph directly so that | |
| cached path computations stay consistent. | |
| """ | |
| if not self._graph.has_edge(source_id, target_id): | |
| return False | |
| self._graph.remove_edge(source_id, target_id) | |
| self._critical_paths_cache.clear() | |
| return True | |
| def get_component(self, component_id: str) -> Component | None: | |
| return self._components.get(component_id) | |
| def get_dependents(self, component_id: str) -> list[Component]: | |
| """Get components that depend ON this component (upstream impact).""" | |
| predecessors = self._graph.predecessors(component_id) | |
| return [self._components[pid] for pid in predecessors if pid in self._components] | |
| def get_dependencies(self, component_id: str) -> list[Component]: | |
| """Get components that this component depends on.""" | |
| successors = self._graph.successors(component_id) | |
| return [self._components[sid] for sid in successors if sid in self._components] | |
| def get_dependency_edge(self, source_id: str, target_id: str) -> Dependency | None: | |
| edge = self._graph.edges.get((source_id, target_id)) | |
| if edge: | |
| dep: Dependency | None = edge.get("dependency") | |
| return dep | |
| return None | |
| def all_dependency_edges(self) -> list[Dependency]: | |
| """Return all dependency edge metadata.""" | |
| edges = [] | |
| for u, v, data in self._graph.edges(data=True): | |
| if "dependency" in data: | |
| edges.append(data["dependency"]) | |
| return edges | |
| def get_cascade_path( | |
| self, failed_component_id: str, max_paths: int = 10_000 | |
| ) -> list[list[str]]: | |
| """Find all downstream paths showing how a failure cascades from the component. | |
| Returns simple paths from the failed component to all transitively | |
| affected components (its direct and indirect dependents). Enumeration | |
| stops after *max_paths* paths so dense graphs cannot blow up | |
| exponentially. | |
| """ | |
| if failed_component_id not in self._graph: | |
| return [] | |
| # Walk reverse dependency edges (dependents) with a single DFS instead | |
| # of one all_simple_paths() call per node on a reversed graph copy. | |
| paths: list[list[str]] = [] | |
| stack: list[list[str]] = [[failed_component_id]] | |
| while stack: | |
| path = stack.pop() | |
| for pred in self._graph.predecessors(path[-1]): | |
| if pred in path: # keep paths simple under cyclic graphs | |
| continue | |
| new_path = path + [pred] | |
| paths.append(new_path) | |
| if len(paths) >= max_paths: | |
| return paths | |
| stack.append(new_path) | |
| return paths | |
| def get_all_affected(self, component_id: str) -> set[str]: | |
| """Get all components transitively affected by a failure.""" | |
| affected: set[str] = set() | |
| bfs_queue: deque[str] = deque([component_id]) | |
| while bfs_queue: | |
| current = bfs_queue.popleft() | |
| for dep in self.get_dependents(current): | |
| if dep.id not in affected: | |
| affected.add(dep.id) | |
| bfs_queue.append(dep.id) | |
| return affected | |
| def get_critical_paths(self, max_paths: int = 100) -> list[list[str]]: | |
| """Find the longest dependency chains (most vulnerable to cascade). | |
| Results are memoized per *max_paths* until the topology changes; | |
| resilience scoring calls this repeatedly on an unchanged graph. | |
| """ | |
| cached = self._critical_paths_cache.get(max_paths) | |
| if cached is not None: | |
| return [list(p) for p in cached] | |
| entries = [n for n in self._graph.nodes if self._graph.in_degree(n) == 0] | |
| leaves = [n for n in self._graph.nodes if self._graph.out_degree(n) == 0] | |
| paths: list[list[str]] = [] | |
| capped = False | |
| for node in entries: | |
| for target in leaves: | |
| for path in nx.all_simple_paths(self._graph, node, target): | |
| paths.append(path) | |
| if len(paths) >= max_paths: | |
| capped = True | |
| break | |
| if capped: | |
| break | |
| if capped: | |
| break | |
| paths.sort(key=len, reverse=True) | |
| self._critical_paths_cache[max_paths] = [list(p) for p in paths] | |
| return paths | |
| def resilience_score(self) -> float: | |
| """Calculate overall resilience score (0-100). | |
| Factors: | |
| - Single points of failure (weighted by dependency type) | |
| - Failover and autoscaling reduce SPOF penalty | |
| - Average utilization headroom | |
| - Dependency chain depth | |
| """ | |
| if not self._components: | |
| return 0.0 | |
| score = 100.0 | |
| # Penalize single points of failure, weighted by dependency type | |
| # Cap total SPOF penalty so large infras don't hit 0 | |
| spof_penalty_total = 0.0 | |
| for comp in self._components.values(): | |
| dependents = self.get_dependents(comp.id) | |
| if comp.replicas <= 1 and len(dependents) > 0: | |
| # Weight by dependency type: requires=1.0, optional=0.3, async=0.1 | |
| weighted_deps = 0.0 | |
| for dep_comp in dependents: | |
| edge = self.get_dependency_edge(dep_comp.id, comp.id) | |
| if edge: | |
| dep_type = edge.dependency_type | |
| if dep_type == "requires": | |
| weighted_deps += 1.0 | |
| elif dep_type == "optional": | |
| weighted_deps += 0.3 | |
| else: # async | |
| weighted_deps += 0.1 | |
| else: | |
| weighted_deps += 1.0 | |
| penalty = min(20, weighted_deps * 5) | |
| # Reduce penalty for components with failover or autoscaling | |
| if comp.failover.enabled: | |
| penalty *= 0.3 # failover greatly reduces SPOF risk | |
| if comp.autoscaling.enabled: | |
| penalty *= 0.5 # autoscaling reduces capacity risk | |
| spof_penalty_total += penalty | |
| score -= min(30, spof_penalty_total) # cap at 30 points | |
| # Penalize replicas on the same host (false redundancy) | |
| # Cap total penalty so large infras don't get crushed | |
| host_penalty_total = 0.0 | |
| for comp in self._components.values(): | |
| if comp.replicas >= 2 and comp.host: | |
| dependents = self.get_dependents(comp.id) | |
| if len(dependents) > 0: | |
| host_penalty_total += min(5, len(dependents) * 2) | |
| score -= min(20, host_penalty_total) # cap at 20 points | |
| # Penalize lack of failover on critical components | |
| # Cap total penalty | |
| failover_penalty_total = 0.0 | |
| for comp in self._components.values(): | |
| dependents = self.get_dependents(comp.id) | |
| if len(dependents) > 0 and not comp.failover.enabled: | |
| failover_penalty_total += min(3, len(dependents) * 1) | |
| score -= min(15, failover_penalty_total) # cap at 15 points | |
| # Penalize high utilization (per component, each metric independently) | |
| # Cap total penalty | |
| util_penalty_total = 0.0 | |
| for comp in self._components.values(): | |
| for metric_val in [ | |
| comp.metrics.cpu_percent, | |
| comp.metrics.memory_percent, | |
| comp.metrics.disk_percent, | |
| ]: | |
| if metric_val >= 95: | |
| util_penalty_total += 10 | |
| elif metric_val >= 90: | |
| util_penalty_total += 7 | |
| elif metric_val >= 80: | |
| util_penalty_total += 4 | |
| elif metric_val >= 70: | |
| util_penalty_total += 1 | |
| score -= min(25, util_penalty_total) # cap at 25 points | |
| # Penalize deep dependency chains (cap at 10 points) | |
| critical_paths = self.get_critical_paths() | |
| if critical_paths: | |
| max_depth = len(critical_paths[0]) | |
| if max_depth > 5: | |
| score -= min(10, (max_depth - 5) * 3) | |
| return max(0.0, min(100.0, score)) | |
| def resilience_score_v2(self) -> dict[str, object]: | |
| """Enhanced resilience score with detailed breakdown. | |
| Returns dict with: | |
| - score: float (0-100) | |
| - breakdown: dict with per-category scores | |
| - recommendations: list of improvement suggestions | |
| """ | |
| if not self._components: | |
| return { | |
| "score": 0.0, | |
| "breakdown": { | |
| "redundancy": 0.0, | |
| "circuit_breaker_coverage": 0.0, | |
| "auto_recovery": 0.0, | |
| "dependency_risk": 0.0, | |
| "capacity_headroom": 0.0, | |
| }, | |
| "recommendations": [], | |
| } | |
| recommendations: list[str] = [] | |
| # --- 1. Redundancy Score (0-20) --- | |
| redundancy_scores: list[float] = [] | |
| for comp in self._components.values(): | |
| if comp.replicas >= 2 and comp.failover.enabled: | |
| # Active-Active: multiple replicas with failover | |
| redundancy_scores.append(20.0) | |
| elif comp.replicas >= 2 or comp.failover.enabled: | |
| # Active-Standby: either replicas or failover but not both | |
| redundancy_scores.append(15.0) | |
| else: | |
| # Single instance, no failover | |
| redundancy_scores.append(5.0) | |
| recommendations.append( | |
| f"Component '{comp.id}' has no redundancy (replicas=1, no failover). " | |
| "Consider adding replicas or enabling failover." | |
| ) | |
| redundancy = sum(redundancy_scores) / len(redundancy_scores) if redundancy_scores else 0.0 | |
| # --- 2. Circuit Breaker Coverage (0-20) --- | |
| all_edges = self.all_dependency_edges() | |
| if all_edges: | |
| cb_enabled_count = sum( | |
| 1 for edge in all_edges if edge.circuit_breaker.enabled | |
| ) | |
| cb_ratio = cb_enabled_count / len(all_edges) | |
| circuit_breaker_coverage = cb_ratio * 20.0 | |
| if cb_ratio < 1.0: | |
| uncovered = len(all_edges) - cb_enabled_count | |
| recommendations.append( | |
| f"{uncovered} of {len(all_edges)} dependency edges lack circuit breakers. " | |
| "Enable circuit breakers to prevent cascade failures." | |
| ) | |
| else: | |
| # No dependencies means no risk from missing circuit breakers | |
| circuit_breaker_coverage = 20.0 | |
| # --- 3. Auto-Recovery Score (0-20) --- | |
| recovery_scores: list[float] = [] | |
| for comp in self._components.values(): | |
| has_recovery = comp.autoscaling.enabled or comp.failover.enabled | |
| recovery_scores.append(1.0 if has_recovery else 0.0) | |
| if not has_recovery: | |
| recommendations.append( | |
| f"Component '{comp.id}' has no auto-recovery (no autoscaling or failover). " | |
| "Enable autoscaling or failover for automatic recovery." | |
| ) | |
| if recovery_scores: | |
| recovery_ratio = sum(recovery_scores) / len(recovery_scores) | |
| auto_recovery = recovery_ratio * 20.0 | |
| else: | |
| auto_recovery = 0.0 | |
| # --- 4. Dependency Risk Score (0-20) --- | |
| critical_paths = self.get_critical_paths() | |
| if critical_paths: | |
| max_depth = len(critical_paths[0]) | |
| else: | |
| max_depth = 0 | |
| # Score based on inverse of max depth: depth 1 = 20, depth 10+ = 0 | |
| if max_depth <= 1: | |
| depth_score = 20.0 | |
| elif max_depth >= 10: | |
| depth_score = 0.0 | |
| else: | |
| # Linear interpolation: depth 2 -> ~17.8, depth 5 -> ~11.1, depth 9 -> ~2.2 | |
| depth_score = max(0.0, 20.0 * (1.0 - (max_depth - 1) / 9.0)) | |
| # Penalize 'requires' dependencies without alternatives | |
| requires_without_alt = 0 | |
| for comp in self._components.values(): | |
| deps = self.get_dependencies(comp.id) | |
| for dep_comp in deps: | |
| edge = self.get_dependency_edge(comp.id, dep_comp.id) | |
| if edge and edge.dependency_type == "requires": | |
| # Check if the target has replicas or failover | |
| if dep_comp.replicas <= 1 and not dep_comp.failover.enabled: | |
| requires_without_alt += 1 | |
| if requires_without_alt > 0 and self._components: | |
| alt_penalty = min(10.0, requires_without_alt * 2.0) | |
| depth_score = max(0.0, depth_score - alt_penalty) | |
| recommendations.append( | |
| f"{requires_without_alt} 'requires' dependencies target components " | |
| "without redundancy. Add replicas or failover to critical dependencies." | |
| ) | |
| dependency_risk = depth_score | |
| # --- 5. Capacity Headroom Score (0-20) --- | |
| utilizations: list[float] = [] | |
| for comp in self._components.values(): | |
| utilizations.append(comp.utilization()) | |
| if utilizations: | |
| avg_util = sum(utilizations) / len(utilizations) | |
| # All < 50% = 20, all > 90% = 0, linear interpolation between | |
| if avg_util <= 50.0: | |
| capacity_headroom = 20.0 | |
| elif avg_util >= 90.0: | |
| capacity_headroom = 0.0 | |
| else: | |
| capacity_headroom = 20.0 * (1.0 - (avg_util - 50.0) / 40.0) | |
| for comp in self._components.values(): | |
| util = comp.utilization() | |
| if util > 80.0: | |
| recommendations.append( | |
| f"Component '{comp.id}' has high utilization ({util:.0f}%). " | |
| "Consider scaling up or enabling autoscaling." | |
| ) | |
| else: | |
| capacity_headroom = 20.0 | |
| # --- Total Score --- | |
| total_score = redundancy + circuit_breaker_coverage + auto_recovery + dependency_risk + capacity_headroom | |
| total_score = max(0.0, min(100.0, total_score)) | |
| # Deduplicate recommendations | |
| seen: set[str] = set() | |
| unique_recommendations: list[str] = [] | |
| for rec in recommendations: | |
| if rec not in seen: | |
| seen.add(rec) | |
| unique_recommendations.append(rec) | |
| return { | |
| "score": round(total_score, 1), | |
| "breakdown": { | |
| "redundancy": round(redundancy, 1), | |
| "circuit_breaker_coverage": round(circuit_breaker_coverage, 1), | |
| "auto_recovery": round(auto_recovery, 1), | |
| "dependency_risk": round(dependency_risk, 1), | |
| "capacity_headroom": round(capacity_headroom, 1), | |
| }, | |
| "recommendations": unique_recommendations, | |
| } | |
| def summary(self) -> dict[str, object]: | |
| return { | |
| "total_components": len(self._components), | |
| "total_dependencies": self._graph.number_of_edges(), | |
| "component_types": { | |
| t.value: sum( | |
| 1 for c in self._components.values() if c.type == t | |
| ) | |
| for t in ComponentType | |
| if any(c.type == t for c in self._components.values()) | |
| }, | |
| "resilience_score": round(self.resilience_score(), 1), | |
| } | |
| def to_dict(self) -> dict[str, object]: | |
| return { | |
| "schema_version": SCHEMA_VERSION, | |
| "components": [c.model_dump() for c in self._components.values()], | |
| "dependencies": [ | |
| self._graph.edges[e]["dependency"].model_dump() | |
| for e in self._graph.edges | |
| if "dependency" in self._graph.edges[e] | |
| ], | |
| } | |
| def save(self, path: Path) -> None: | |
| path.write_text(json.dumps(self.to_dict(), indent=2, default=str)) | |
| def load(cls, path: Path) -> InfraGraph: | |
| import logging | |
| _logger = logging.getLogger(__name__) | |
| # Delegate YAML files to the dedicated YAML loader so that callers can | |
| # pass either .json or .yaml/.yml without caring about the format. | |
| if str(path).endswith((".yaml", ".yml")): | |
| from faultray.model.loader import load_yaml | |
| return load_yaml(path) | |
| # DoS guard: cap the input size before parsing, at parity with the YAML | |
| # loader. Without this, untrusted JSON could exhaust memory. | |
| if isinstance(path, str): | |
| path = Path(path) | |
| try: | |
| file_size = path.stat().st_size | |
| except OSError: | |
| file_size = 0 | |
| if file_size > _MAX_JSON_BYTES: | |
| raise ValueError( | |
| f"JSON model file too large ({file_size:,} bytes). " | |
| f"Maximum allowed: {_MAX_JSON_BYTES:,} bytes." | |
| ) | |
| data = json.loads(path.read_text()) | |
| if not isinstance(data, dict): | |
| raise ValueError( | |
| f"Expected a JSON object at the top level, got {type(data).__name__}" | |
| ) | |
| # Handle missing schema_version gracefully | |
| file_version = data.get("schema_version") | |
| if file_version is None: | |
| _logger.warning( | |
| "Model uses schema v1.0, migrating to v%s", SCHEMA_VERSION | |
| ) | |
| elif file_version != SCHEMA_VERSION: | |
| _logger.warning( | |
| "Model uses schema v%s, migrating to v%s", | |
| file_version, | |
| SCHEMA_VERSION, | |
| ) | |
| graph = cls() | |
| raw_components = data.get("components", []) | |
| # _MAX_COMPONENTS is enforced inside add_component (raises before | |
| # building the 10,001st object); a pre-check keeps the error explicit. | |
| if isinstance(raw_components, list) and len(raw_components) > _MAX_COMPONENTS: | |
| raise ValueError( | |
| f"JSON model has too many components " | |
| f"({len(raw_components)} > {_MAX_COMPONENTS})." | |
| ) | |
| for c in raw_components: | |
| graph.add_component(Component(**c)) | |
| # Validate dependency endpoints exist BEFORE adding edges — at parity | |
| # with the YAML loader. add_dependency() would otherwise create dangling | |
| # NetworkX nodes for unknown ids, bypassing the component guard and | |
| # corrupting the graph. | |
| known_ids = set(graph.components.keys()) | |
| for d in data.get("dependencies", []): | |
| dep = Dependency(**d) | |
| if dep.source_id not in known_ids: | |
| raise ValueError( | |
| f"Dependency source '{dep.source_id}' does not match any component id" | |
| ) | |
| if dep.target_id not in known_ids: | |
| raise ValueError( | |
| f"Dependency target '{dep.target_id}' does not match any component id" | |
| ) | |
| graph.add_dependency(dep) | |
| # Reject cycles: the graph must be a DAG (parity with the YAML loader). | |
| if not nx.is_directed_acyclic_graph(graph._graph): | |
| cycles = list(nx.simple_cycles(graph._graph)) | |
| cycle_str = " -> ".join(cycles[0] + [cycles[0][0]]) if cycles else "unknown" | |
| raise ValueError( | |
| f"Circular dependency detected: {cycle_str}. " | |
| f"Infrastructure graph must be a DAG." | |
| ) | |
| return graph | |
Xet Storage Details
- Size:
- 21.7 kB
- Xet hash:
- 5e0988dc59823e65f34e2983eb2501246dc3dc83012eb7e143f7a1e948526594
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.