-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathhub.py
More file actions
170 lines (144 loc) · 6.65 KB
/
Copy pathhub.py
File metadata and controls
170 lines (144 loc) · 6.65 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
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
"""Async Hub mode - aggregates data from multiple nodes"""
import asyncio
import logging
import json
import websockets
from datetime import datetime
from . import config
logger = logging.getLogger(__name__)
class Hub:
"""Aggregates GPU data from multiple nodes"""
def __init__(self, node_urls):
self.node_urls = node_urls
self.nodes = {} # node_name -> {client, data, status, last_update}
self.url_to_node = {} # url -> node_name mapping
self.running = False
self._connection_started = False
# Initialize nodes as offline
for url in node_urls:
self.nodes[url] = {
'url': url,
'websocket': None,
'data': None,
'status': 'offline',
'last_update': None
}
self.url_to_node[url] = url
async def _connect_all_nodes(self):
"""Connect to all nodes in background with retries"""
# Wait a bit for Docker network to be ready
await asyncio.sleep(2)
# Connect to all nodes concurrently
tasks = [self._connect_node_with_retry(url) for url in self.node_urls]
await asyncio.gather(*tasks, return_exceptions=True)
async def _connect_node_with_retry(self, url):
"""Connect to a node with retry logic"""
max_retries = 5
retry_delay = 2
for attempt in range(max_retries):
try:
await self._connect_node(url)
return # Success
except Exception as e:
if attempt < max_retries - 1:
logger.warning(f'Connection attempt {attempt + 1}/{max_retries} failed for {url}: {str(e)}, retrying in {retry_delay}s...')
await asyncio.sleep(retry_delay)
else:
logger.error(f'Failed to connect to node {url} after {max_retries} attempts: {str(e)}')
async def _connect_node(self, url):
"""Connect to a node using native WebSocket"""
while self.running:
try:
# Convert HTTP URL to WebSocket URL
ws_url = url.replace('http://', 'ws://').replace('https://', 'wss://') + '/socket.io/'
logger.info(f'Connecting to node WebSocket: {ws_url}')
async with websockets.connect(ws_url) as websocket:
logger.info(f'Connected to node: {url}')
# Mark node as online
node_name = self.url_to_node.get(url, url)
self.nodes[node_name] = {
'url': url,
'websocket': websocket,
'data': None,
'status': 'online',
'last_update': datetime.now().isoformat()
}
# Listen for data from the node
async for message in websocket:
try:
data = json.loads(message)
# Extract node name from data or use URL as fallback
node_name = data.get('node_name', url)
# Update URL to node mapping
self.url_to_node[url] = node_name
# Update node entry with received data
self.nodes[node_name] = {
'url': url,
'websocket': websocket,
'data': data,
'status': 'online',
'last_update': datetime.now().isoformat()
}
except json.JSONDecodeError as e:
logger.error(f'Failed to parse message from {url}: {e}')
except Exception as e:
logger.error(f'Error processing message from {url}: {e}')
except websockets.exceptions.ConnectionClosed:
logger.warning(f'WebSocket connection closed for node: {url}')
# Mark node as offline
node_name = self.url_to_node.get(url, url)
if node_name in self.nodes:
self.nodes[node_name]['status'] = 'offline'
logger.info(f'Marked node {node_name} as offline')
except Exception as e:
logger.error(f'Failed to connect to node {url}: {e}')
# Mark node as offline
node_name = self.url_to_node.get(url, url)
if node_name in self.nodes:
self.nodes[node_name]['status'] = 'offline'
logger.info(f'Marked node {node_name} as offline')
# Wait before retrying connection
if self.running:
await asyncio.sleep(5)
async def get_cluster_data(self):
"""Get aggregated data from all nodes"""
nodes = {}
total_gpus = 0
online_nodes = 0
for node_name, node_info in self.nodes.items():
if node_info['status'] == 'online' and node_info['data']:
nodes[node_name] = {
'status': 'online',
'gpus': node_info['data'].get('gpus', {}),
'processes': node_info['data'].get('processes', []),
'system': node_info['data'].get('system', {}),
'last_update': node_info['last_update']
}
total_gpus += len(node_info['data'].get('gpus', {}))
online_nodes += 1
else:
nodes[node_name] = {
'status': 'offline',
'gpus': {},
'processes': [],
'system': {},
'last_update': node_info.get('last_update')
}
return {
'mode': 'hub',
'nodes': nodes,
'cluster_stats': {
'total_nodes': len(self.nodes),
'online_nodes': online_nodes,
'total_gpus': total_gpus
}
}
async def shutdown(self):
"""Disconnect from all nodes"""
self.running = False
for node_info in self.nodes.values():
if node_info.get('websocket'):
try:
await node_info['websocket'].close()
except:
pass