-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_simulator.py
More file actions
76 lines (65 loc) · 2.7 KB
/
Copy pathnode_simulator.py
File metadata and controls
76 lines (65 loc) · 2.7 KB
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
"""
Node failure simulation system for COSMEON distributed storage.
Manages simulated node failures that affect API responses and file reconstruction.
"""
from typing import Set, Dict, Any
from datetime import datetime
import threading
class NodeFailureSimulator:
"""Manages simulated node failures across the system"""
def __init__(self):
self._failed_nodes: Set[str] = set()
self._lock = threading.Lock()
self._failure_history: Dict[str, datetime] = {}
def simulate_failure(self, node_id: str) -> bool:
"""Simulate a node failure"""
with self._lock:
if node_id not in self._failed_nodes:
self._failed_nodes.add(node_id)
self._failure_history[node_id] = datetime.utcnow()
print(f"[SIMULATOR] Node {node_id} marked as FAILED")
return True
return False
def restore_node(self, node_id: str) -> bool:
"""Restore a failed node"""
with self._lock:
if node_id in self._failed_nodes:
self._failed_nodes.remove(node_id)
if node_id in self._failure_history:
del self._failure_history[node_id]
print(f"[SIMULATOR] Node {node_id} RESTORED")
return True
return False
def is_node_failed(self, node_id: str) -> bool:
"""Check if a node is currently failed"""
with self._lock:
return node_id in self._failed_nodes
def get_failed_nodes(self) -> Set[str]:
"""Get all currently failed nodes"""
with self._lock:
return self._failed_nodes.copy()
def get_online_nodes(self, all_nodes: list) -> list:
"""Filter out failed nodes from a list of all nodes"""
with self._lock:
return [node for node in all_nodes if node not in self._failed_nodes]
def get_failure_info(self) -> Dict[str, Any]:
"""Get detailed failure information"""
with self._lock:
return {
"failed_nodes": list(self._failed_nodes),
"failure_count": len(self._failed_nodes),
"failure_history": {
node: timestamp.isoformat()
for node, timestamp in self._failure_history.items()
}
}
def clear_all_failures(self) -> int:
"""Clear all simulated failures"""
with self._lock:
count = len(self._failed_nodes)
self._failed_nodes.clear()
self._failure_history.clear()
print(f"[SIMULATOR] Cleared {count} simulated failures")
return count
# Global instance
node_simulator = NodeFailureSimulator()