-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathcluster.py
More file actions
326 lines (274 loc) · 10.5 KB
/
cluster.py
File metadata and controls
326 lines (274 loc) · 10.5 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
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
#
# Copyright (c) 2022-2026, NVIDIA CORPORATION. All rights reserved.
#
from __future__ import annotations # pylint: disable=unused-variable
import logging
from typing import Dict, List, Optional, Union
import requests
from aistore.sdk.const import (
HTTP_METHOD_GET,
ACT_LIST,
QPARAM_WHAT,
QPARAM_PRIMARY_READY_REB,
QPARAM_PROVIDER,
QPARAM_LOG_SEV,
QPARAM_ALL_LOGS,
HEADER_NODE_ID,
URL_PATH_ETL,
URL_PATH_REVERSE,
URL_PATH_BUCKETS,
URL_PATH_HEALTH,
URL_PATH_DAEMON,
URL_PATH_CLUSTER,
WHAT_SMAP,
WHAT_ALL_XACT_STATUS,
WHAT_ALL_RUNNING_STATUS,
WHAT_NODE_STATS_AND_STATUS,
WHAT_LOG,
)
from aistore.sdk.enums import LogSeverity, NodeFilter
from aistore.sdk.etl.etl_const import ETL_STAGE_RUNNING
from aistore.sdk.provider import Provider
from aistore.sdk.types import (
BucketModel,
JobStatus,
JobQuery,
ETLInfo,
ActionMsg,
Smap,
)
from aistore.sdk.request_client import RequestClient
logger = logging.getLogger("cluster")
# pylint: disable=unused-variable
class Cluster:
"""
A class representing a cluster bound to an AIS client.
"""
# pylint: disable=duplicate-code
def __init__(self, client: RequestClient):
self._client = client
@property
def client(self):
"""Client this cluster uses to make requests"""
return self._client
def get_info(self) -> Smap:
"""
Returns state of AIS cluster, including the detailed information about its nodes.
Returns:
aistore.sdk.types.Smap: Smap containing cluster information
Raises:
requests.RequestException: "There was an ambiguous exception that occurred while handling..."
requests.ConnectionError: Connection error
requests.ConnectionTimeout: Timed out connecting to AIStore
requests.ReadTimeout: Timed out waiting response from AIStore
"""
return self._get_smap()
def get_primary_url(self) -> str:
"""
Returns: URL of primary proxy
"""
return self._get_smap().proxy_si.public_net.direct_url
def list_buckets(self, provider: Union[str, Provider] = Provider.AIS):
"""
Returns list of buckets in AIStore cluster.
Args:
provider (str or Provider, optional): Provider of bucket (one of "ais", "aws", "gcp", ...).
Defaults to "ais". Empty provider returns buckets of all providers.
Returns:
List[BucketModel]: A list of buckets
Raises:
requests.RequestException: "There was an ambiguous exception that occurred while handling..."
requests.ConnectionError: Connection error
requests.ConnectionTimeout: Timed out connecting to AIStore
requests.ReadTimeout: Timed out waiting response from AIStore
"""
params = {QPARAM_PROVIDER: Provider.parse(provider).value}
action = ActionMsg(action=ACT_LIST).model_dump()
return self.client.request_deserialize(
HTTP_METHOD_GET,
path=URL_PATH_BUCKETS,
res_model=List[BucketModel],
json=action,
params=params,
)
def list_jobs_status(self, job_kind="", target_id="") -> List[JobStatus]:
"""
List the status of jobs on the cluster
Args:
job_kind (str, optional): Only show jobs of a particular type
target_id (str, optional): Limit to jobs on a specific target node
Returns:
List of JobStatus objects
"""
res = self._client.request_deserialize(
HTTP_METHOD_GET,
path=URL_PATH_CLUSTER,
res_model=Optional[List[JobStatus]],
json=JobQuery(kind=job_kind, target=target_id).as_dict(),
params={QPARAM_WHAT: WHAT_ALL_XACT_STATUS},
)
if res is None:
return []
return res
def list_running_jobs(self, job_kind="", target_id="") -> List[str]:
"""
List the currently running jobs on the cluster
Args:
job_kind (str, optional): Only show jobs of a particular type
target_id (str, optional): Limit to jobs on a specific target node
Returns:
List of jobs in the format job_kind[job_id]
"""
return self._client.request_deserialize(
HTTP_METHOD_GET,
path=URL_PATH_CLUSTER,
res_model=List[str],
json=JobQuery(kind=job_kind, target=target_id, active=True).as_dict(),
params={QPARAM_WHAT: WHAT_ALL_RUNNING_STATUS},
)
def list_etls(self, stages: Optional[List[str]] = None) -> List[ETLInfo]:
"""
Lists ETLs filtered by their stages.
Args:
stages (List[str], optional): List of stages to filter ETLs by. Defaults to ["running"].
Returns:
List[ETLInfo]: A list of details on ETLs matching the specified stages
"""
if stages is None:
stages = [ETL_STAGE_RUNNING]
res = self._client.request_deserialize(
HTTP_METHOD_GET, path=URL_PATH_ETL, res_model=List[ETLInfo]
)
return [info for info in res if info.stage in stages]
def is_ready(self) -> bool:
"""
Checks if cluster is ready or still setting up.
Returns:
bool: True if cluster is ready, or false if cluster is still setting up
"""
# compare with AIS Go API (api/cluster.go) for additional supported options
params = {QPARAM_PRIMARY_READY_REB: "true"}
try:
resp = self._client.request(
HTTP_METHOD_GET,
path=URL_PATH_HEALTH,
endpoint=self.get_primary_url(),
params=params,
)
return resp.ok
except Exception as err:
logger.debug(err)
return False
def get_performance(self) -> Dict:
"""
Retrieves the raw performance and status data from each target node in the AIStore cluster.
Returns:
Dict: A dictionary where each key is the ID of a target node and each value is the
raw AIS performance/status JSON returned by that node (for more information,
see https://aistore.nvidia.com/docs/monitoring-metrics#target-metrics).
Raises:
requests.RequestException: If there's an ambiguous exception while processing the request
requests.ConnectionError: If there's a connection error with the cluster
requests.ConnectionTimeout: If the connection to the cluster times out
requests.ReadTimeout: If the timeout is reached while awaiting a response from the cluster
"""
performance_data = {}
targets = self._get_targets()
params = {QPARAM_WHAT: WHAT_NODE_STATS_AND_STATUS}
for target_id in targets:
headers = {HEADER_NODE_ID: target_id}
resp = self.client.request(
HTTP_METHOD_GET,
path=f"{URL_PATH_REVERSE}/{URL_PATH_DAEMON}",
params=params,
headers=headers,
)
resp.raise_for_status()
performance_data[target_id] = resp.json()
return performance_data
def _get_smap(self):
return self.client.request_deserialize(
HTTP_METHOD_GET,
path=URL_PATH_DAEMON,
res_model=Smap,
params={QPARAM_WHAT: WHAT_SMAP},
)
def _get_targets(self):
tmap = self._get_smap().tmap
return list(tmap.keys())
def get_uuid(self) -> str:
"""
Returns: UUID of AIStore Cluster
"""
return self._get_smap().uuid
def get_node_log(
self, node_id: str, severity: LogSeverity = LogSeverity.INFO
) -> str:
"""
Get the current log from a specific cluster node (target or proxy).
Args:
node_id (str): Daemon ID of the node (e.g., "hHQZBnBQ").
severity (LogSeverity): Log severity level (default: LogSeverity.INFO).
Returns:
str: Current log content as text.
"""
resp = self._request_log(node_id, severity.value)
return resp.text
def get_node_log_archive(
self, node_id: str, severity: LogSeverity = LogSeverity.INFO
) -> bytes:
"""
Download a TAR.GZ archive of all rotated logs from a specific node.
Args:
node_id (str): Daemon ID of the node (e.g., "hHQZBnBQ").
severity (LogSeverity): Log severity level (default: LogSeverity.INFO).
Returns:
bytes: TAR.GZ archive containing all rotated log files.
Loaded into memory; typically ~5MB compressed per node.
TODO: stream the archive instead of loading into memory for large logs.
"""
resp = self._request_log(node_id, severity.value, all_logs=True)
return resp.content
def get_cluster_logs(
self,
severity: LogSeverity = LogSeverity.INFO,
role: NodeFilter = NodeFilter.TARGET,
) -> Dict[str, str]:
"""
Get logs from all nodes of a given role.
Args:
severity (LogSeverity): Log severity level (default: LogSeverity.INFO).
role (NodeFilter): Node filter (default: NodeFilter.TARGET).
Returns:
Dict[str, str]: Mapping of node ID to log content.
"""
smap = self._get_smap()
nodes = {}
if role in (NodeFilter.TARGET, NodeFilter.ALL):
nodes.update(smap.tmap)
if role in (NodeFilter.PROXY, NodeFilter.ALL):
nodes.update(smap.pmap)
result = {}
for node_id in nodes:
result[node_id] = self.get_node_log(node_id, severity=severity)
return result
def _request_log(
self, node_id: str, severity: str, all_logs: bool = False
) -> requests.Response:
"""
Make the HTTP request to fetch a node's log.
Args:
node_id (str): Daemon ID of the node.
severity (str): Log severity string value.
all_logs (bool): If True, fetch all rotated logs as TAR.GZ archive.
"""
params = {QPARAM_WHAT: WHAT_LOG, QPARAM_LOG_SEV: severity}
if all_logs:
params[QPARAM_ALL_LOGS] = "true"
headers = {HEADER_NODE_ID: node_id}
return self.client.request(
HTTP_METHOD_GET,
path=f"{URL_PATH_REVERSE}/{URL_PATH_DAEMON}",
params=params,
headers=headers,
)