pub mod algorithms;
pub mod graph;
pub mod ml;
pub mod prevention;
pub mod recovery;
pub mod types;
pub type ResourceId = u64;
pub type TransactionId = u64;
pub use types::{
AdaptiveSensitivity, AdvancedDeadlockConfig, AdvancedDiagnostics, DeadlockDetectionConfig,
DeadlockDetector, DeadlockPerformanceConfig, DeadlockSensitivity, DeliveryMethod,
DetectionEvent, DetectionEventType, DetectionState, DetectionStatus, ExportFormat,
IntegrationSettings, NotificationConfig, NotificationType, PerformanceOptimization,
ResourceLimits, SensitivityMetric,
};
pub use algorithms::{
BackoffStrategy, CacheOptimization, CachePolicy, CombinationStrategy, ConflictResolution,
CycleDetectionMethod, DeadlockCriteria, DeadlockDetectionAlgorithm, ErrorHandling,
GraphOptimization, GraphReductionMethod, ParallelProcessing, PrefetchingStrategy,
PropagationStrategy, ResourceAllocationMethod, ResponseHandling, RetryPolicy, SafeStateMethod,
SynchronizationMethod, TimestampOrdering, WorkDistribution,
};
pub use prevention::{
AllocationPolicy, AvoidanceStrategy, BankersAlgorithmConfig, CircularWaitPrevention,
ConservativeStrategy, DeadlockPrevention, DeadlockPreventionSystem, HoldAndWaitPrevention,
MutualExclusionPrevention, NoPreemptionPrevention, OptimisticStrategy, OrderingStrategy,
PreemptionPolicy, PreemptionStrategy, PreventionPolicy, PreventionStatistics, ResourceOrdering,
TimeoutStrategy, ValidationStatistics, WoundWaitStrategy,
};
pub use ml::{
CombinationStrategy as MLCombinationStrategy, EnsembleMethod, FeatureExtraction, GraphFeature,
MLModelType, ResourceFeature, TemporalFeature,
};
pub use graph::{
ChangeType, DependencyEdge, DependencyGraph, EdgeMetadata, EdgeType, GraphChange, GraphHistory,
GraphMetadata, GraphNode, GraphOptimizationState, GraphProperties, GraphSnapshot,
GraphStatistics, NodeMetadata, NodeState, NodeType, OptimizationOperation, OptimizationRecord,
OptimizationStatistics, PerformanceImpact,
};
pub use crate::pod_coordination::performance::{
DeadlockPerformanceConfig as PerformanceConfig, DeadlockStatistics,
};
pub use recovery::{
ActiveRecovery, CoordinatorSelection, CoordinatorState, DeadlockRecovery,
DeadlockRecoverySystem, DeadlockSeverity, DetectedDeadlock, DistributedRecovery,
DistributedRecoveryStrategy, ExecutionContext, ExecutionRecord, ExecutorCapabilities,
ExecutorPerformance, PhaseRecord, PhaseResult, RecoveryAction, RecoveryConstraint,
RecoveryConstraintType, RecoveryCoordination, RecoveryCoordinator, RecoveryExecutor,
RecoveryExecutorStrategy, RecoveryObjective, RecoveryOptimization,
RecoveryOptimizationAlgorithm, RecoveryPhase, RecoveryProgress, RecoveryRequest,
RecoveryResult, RecoveryStatistics, RecoveryStrategy, RecoveryVerification,
RecoveryVerificationMethod, RollbackMechanism, SelectionCriterion, StateSynchronization,
StrategyStatistics, SynchronizationProtocol, SystemHealth, SystemState,
VerificationSuccessCriteria, VictimSelection, VictimSelectionAlgorithm,
};
pub type DeadlockConfig = DeadlockDetectionConfig;
pub type Statistics = DeadlockStatistics;
pub type RecoveryConfig = DeadlockRecovery;
pub fn create_detector() -> crate::error::Result<DeadlockDetector> {
DeadlockDetector::new()
}
pub fn create_detector_with_config(
config: DeadlockDetectionConfig,
) -> crate::error::Result<DeadlockDetector> {
let mut detector = DeadlockDetector::new()?;
detector.config = config;
Ok(detector)
}
pub fn create_dependency_graph() -> crate::error::Result<DependencyGraph> {
Ok(DependencyGraph::new())
}
pub fn create_recovery_system(
config: DeadlockRecovery,
) -> crate::error::Result<DeadlockRecoverySystem> {
let mut system = DeadlockRecoverySystem::new();
system.recovery = config;
Ok(system)
}
impl DeadlockDetector {
pub fn new() -> crate::error::Result<Self> {
Ok(Self {
config: DeadlockDetectionConfig::default(),
dependency_graph: DependencyGraph::new(),
detection_state: DetectionState {
status: DetectionStatus::Idle,
last_detection: std::time::Instant::now(),
active_deadlocks: 0,
history: Vec::new(),
},
statistics: DeadlockStatistics::default(),
prevention_system: prevention::DeadlockPreventionSystem::new(),
recovery_system: recovery::DeadlockRecoverySystem::new(),
})
}
pub fn detect_deadlocks(&mut self) -> crate::error::Result<Vec<String>> {
self.detection_state.status = DetectionStatus::Running;
self.detection_state.last_detection = std::time::Instant::now();
if !self.config.enable {
self.detection_state.status = DetectionStatus::Idle;
return Ok(Vec::new());
}
match self
.dependency_graph
.find_cycle_with(&self.config.cycle_detection)
{
Some(cycle) => {
self.detection_state.status = DetectionStatus::DeadlockDetected;
self.detection_state.active_deadlocks += 1;
self.statistics.detection_count += 1;
let members = cycle
.iter()
.map(|id| id.to_string())
.collect::<Vec<_>>()
.join("->");
log::warn!("deadlock detected in wait-for graph: {members}");
Ok(vec![format!("deadlock[{members}]")])
}
None => {
self.detection_state.status = DetectionStatus::Idle;
Ok(Vec::new())
}
}
}
pub fn add_dependency(&mut self, source: String, target: String) -> crate::error::Result<()> {
use graph::{DependencyEdge, EdgeMetadata, EdgeType};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
source.hash(&mut hasher);
let source_id = hasher.finish();
let mut hasher = DefaultHasher::new();
target.hash(&mut hasher);
let target_id = hasher.finish();
if !self
.dependency_graph
.nodes
.iter()
.any(|n| n.id == source_id)
{
let node = graph::GraphNode {
id: source_id,
node_type: graph::NodeType::Process,
state: graph::NodeState::Active,
metadata: graph::GraphMetadata::default(),
timestamp: std::time::Instant::now(),
};
self.dependency_graph.add_node(node);
}
if !self
.dependency_graph
.nodes
.iter()
.any(|n| n.id == target_id)
{
let node = graph::GraphNode {
id: target_id,
node_type: graph::NodeType::Resource,
state: graph::NodeState::Active,
metadata: graph::GraphMetadata::default(),
timestamp: std::time::Instant::now(),
};
self.dependency_graph.add_node(node);
}
let edge = DependencyEdge {
from: source_id,
to: target_id,
source: source_id,
target: target_id,
edge_type: EdgeType::WaitsFor,
weight: 1.0,
timestamp: std::time::Instant::now(),
metadata: EdgeMetadata::default(),
};
self.dependency_graph.add_edge(edge);
Ok(())
}
pub fn remove_dependency(&mut self, source: &str, target: &str) -> crate::error::Result<()> {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
source.hash(&mut hasher);
let source_id = hasher.finish();
let mut hasher = DefaultHasher::new();
target.hash(&mut hasher);
let target_id = hasher.finish();
self.dependency_graph
.edges
.retain(|edge| !(edge.source == source_id && edge.target == target_id));
Ok(())
}
pub fn get_statistics(&self) -> &DeadlockStatistics {
&self.statistics
}
pub fn update_config(&mut self, config: DeadlockDetectionConfig) {
self.config = config;
}
}