hypercache-io 0.9.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hypercache/__init__.py +61 -0
- hypercache/client.py +456 -0
- hypercache/datapath.py +466 -0
- hypercache/fsspec_fs.py +457 -0
- hypercache/integrations/__init__.py +25 -0
- hypercache/integrations/dcp.py +147 -0
- hypercache/integrations/safetensors_loader.py +247 -0
- hypercache/kv_store.py +151 -0
- hypercache/pipeline.py +182 -0
- hypercache_io-0.9.0b1.dist-info/METADATA +115 -0
- hypercache_io-0.9.0b1.dist-info/RECORD +14 -0
- hypercache_io-0.9.0b1.dist-info/WHEEL +5 -0
- hypercache_io-0.9.0b1.dist-info/entry_points.txt +2 -0
- hypercache_io-0.9.0b1.dist-info/top_level.txt +1 -0
hypercache/__init__.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""
|
|
2
|
+
HyperCache: Host-Based Zero-Copy GPUDirect Storage Cache.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
from hypercache.client import DiscoveredDeviceInfo, HyperCacheClient, LocateResult
|
|
8
|
+
from hypercache.datapath import (
|
|
9
|
+
ChunkLocation,
|
|
10
|
+
DataReader,
|
|
11
|
+
DirectBlockReader,
|
|
12
|
+
GDSReader,
|
|
13
|
+
get_data_reader,
|
|
14
|
+
)
|
|
15
|
+
from hypercache.fsspec_fs import HyperCacheFile, HyperCacheFileSystem
|
|
16
|
+
from hypercache.integrations import (
|
|
17
|
+
HyperCacheDCPStorageReader,
|
|
18
|
+
HyperCacheDCPStorageWriter,
|
|
19
|
+
HyperCachevLLMWeightLoader,
|
|
20
|
+
load_safetensors_state_dict,
|
|
21
|
+
save_safetensors_state_dict,
|
|
22
|
+
)
|
|
23
|
+
from hypercache.kv_store import HyperCacheKVStore
|
|
24
|
+
from hypercache.pipeline import HyperCachePipelinedLoader, pipeline_load
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def preload(
|
|
28
|
+
uri: str,
|
|
29
|
+
socket_path: Optional[str] = None,
|
|
30
|
+
tier: Optional[str] = None,
|
|
31
|
+
pinned: bool = False,
|
|
32
|
+
) -> LocateResult:
|
|
33
|
+
"""
|
|
34
|
+
Convenience function to proactively pre-warm a checkpoint into the local NVMe cache.
|
|
35
|
+
"""
|
|
36
|
+
client = HyperCacheClient(socket_path=socket_path)
|
|
37
|
+
return client.preload(uri, tier=tier, pinned=pinned)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
__all__ = [
|
|
41
|
+
"HyperCacheClient",
|
|
42
|
+
"DiscoveredDeviceInfo",
|
|
43
|
+
"HyperCacheFileSystem",
|
|
44
|
+
"HyperCacheFile",
|
|
45
|
+
"ChunkLocation",
|
|
46
|
+
"DataReader",
|
|
47
|
+
"DirectBlockReader",
|
|
48
|
+
"GDSReader",
|
|
49
|
+
"get_data_reader",
|
|
50
|
+
"preload",
|
|
51
|
+
"HyperCachePipelinedLoader",
|
|
52
|
+
"pipeline_load",
|
|
53
|
+
"HyperCacheKVStore",
|
|
54
|
+
"load_safetensors_state_dict",
|
|
55
|
+
"save_safetensors_state_dict",
|
|
56
|
+
"HyperCachevLLMWeightLoader",
|
|
57
|
+
"HyperCacheDCPStorageWriter",
|
|
58
|
+
"HyperCacheDCPStorageReader",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
__version__ = "0.9.0-beta"
|
hypercache/client.py
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
"""
|
|
2
|
+
HyperCache UDS IPC Client.
|
|
3
|
+
|
|
4
|
+
Facilitates sub-millisecond metadata handshakes with the Rust Control Plane.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import logging
|
|
11
|
+
import socket
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from typing import List, Optional
|
|
14
|
+
|
|
15
|
+
from hypercache.datapath import ChunkLocation
|
|
16
|
+
|
|
17
|
+
import os
|
|
18
|
+
|
|
19
|
+
PRIMARY_SOCKET_PATH = "/run/hypercache/hypercache.sock"
|
|
20
|
+
FALLBACK_SOCKET_PATH = "/tmp/hypercache/hypercache.sock"
|
|
21
|
+
LEGACY_SOCKET_PATH = "/tmp/hypercache.sock"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def get_default_socket_path() -> str:
|
|
25
|
+
"""Discovers and returns the active HyperCache Unix Domain Socket path."""
|
|
26
|
+
if os.path.exists(PRIMARY_SOCKET_PATH):
|
|
27
|
+
return PRIMARY_SOCKET_PATH
|
|
28
|
+
if os.path.exists(FALLBACK_SOCKET_PATH):
|
|
29
|
+
return FALLBACK_SOCKET_PATH
|
|
30
|
+
if os.path.exists(LEGACY_SOCKET_PATH):
|
|
31
|
+
return LEGACY_SOCKET_PATH
|
|
32
|
+
if os.path.exists("/run"):
|
|
33
|
+
return PRIMARY_SOCKET_PATH
|
|
34
|
+
return FALLBACK_SOCKET_PATH
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
DEFAULT_SOCKET_PATH = PRIMARY_SOCKET_PATH
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class LocateResult:
|
|
42
|
+
status: str
|
|
43
|
+
object_size: int
|
|
44
|
+
chunk_size: int
|
|
45
|
+
chunk_layout: List[ChunkLocation]
|
|
46
|
+
error_message: Optional[str] = None
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def is_hit(self) -> bool:
|
|
50
|
+
return self.status == "HIT"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass
|
|
54
|
+
class StageWriteResult:
|
|
55
|
+
status: str
|
|
56
|
+
object_size: int
|
|
57
|
+
chunk_size: int
|
|
58
|
+
chunk_layout: List[ChunkLocation]
|
|
59
|
+
sync_status: str
|
|
60
|
+
error_message: Optional[str] = None
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def is_staged(self) -> bool:
|
|
64
|
+
return self.status in ("WRITESTAGED", "WRITE_STAGED")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclass
|
|
68
|
+
class DiscoveredDeviceInfo:
|
|
69
|
+
path: str
|
|
70
|
+
is_nvme: bool
|
|
71
|
+
is_virtual: bool
|
|
72
|
+
is_partition: bool
|
|
73
|
+
is_rotational: bool
|
|
74
|
+
size_bytes: int
|
|
75
|
+
model: Optional[str] = None
|
|
76
|
+
existing_fs: Optional[str] = None
|
|
77
|
+
numa_node: Optional[int] = None
|
|
78
|
+
|
|
79
|
+
@classmethod
|
|
80
|
+
def from_dict(cls, data: dict) -> DiscoveredDeviceInfo:
|
|
81
|
+
return cls(
|
|
82
|
+
path=str(data.get("path", "")),
|
|
83
|
+
is_nvme=bool(data.get("is_nvme", False)),
|
|
84
|
+
is_virtual=bool(data.get("is_virtual", False)),
|
|
85
|
+
is_partition=bool(data.get("is_partition", False)),
|
|
86
|
+
is_rotational=bool(data.get("is_rotational", False)),
|
|
87
|
+
size_bytes=int(data.get("size_bytes", 0)),
|
|
88
|
+
model=data.get("model"),
|
|
89
|
+
existing_fs=data.get("existing_fs"),
|
|
90
|
+
numa_node=data.get("numa_node"),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class HyperCacheClient:
|
|
95
|
+
"""Client for querying the HyperCache Control Plane daemon via Unix Domain Socket."""
|
|
96
|
+
|
|
97
|
+
def __init__(self, socket_path: Optional[str] = None, timeout: float = 120.0):
|
|
98
|
+
self.socket_path = socket_path or get_default_socket_path()
|
|
99
|
+
self.timeout = timeout
|
|
100
|
+
self._sock: Optional[socket.socket] = None
|
|
101
|
+
|
|
102
|
+
def _connect(self) -> socket.socket:
|
|
103
|
+
if self._sock is not None:
|
|
104
|
+
return self._sock
|
|
105
|
+
|
|
106
|
+
try:
|
|
107
|
+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
108
|
+
sock.settimeout(self.timeout)
|
|
109
|
+
sock.connect(self.socket_path)
|
|
110
|
+
self._sock = sock
|
|
111
|
+
return sock
|
|
112
|
+
except Exception as e:
|
|
113
|
+
logger.error(f"Failed to connect to HyperCache daemon at {self.socket_path}: {e}")
|
|
114
|
+
raise ConnectionError(
|
|
115
|
+
f"HyperCache daemon unreachable at {self.socket_path}. Is the daemon running?"
|
|
116
|
+
) from e
|
|
117
|
+
|
|
118
|
+
def _send_request(self, payload: dict) -> dict:
|
|
119
|
+
sock = self._connect()
|
|
120
|
+
data = (json.dumps(payload) + "\n").encode("utf-8")
|
|
121
|
+
try:
|
|
122
|
+
sock.sendall(data)
|
|
123
|
+
|
|
124
|
+
# Read newline-terminated response
|
|
125
|
+
resp_buf = bytearray()
|
|
126
|
+
while True:
|
|
127
|
+
chunk = sock.recv(4096)
|
|
128
|
+
if not chunk:
|
|
129
|
+
break
|
|
130
|
+
resp_buf.extend(chunk)
|
|
131
|
+
if b"\n" in chunk:
|
|
132
|
+
break
|
|
133
|
+
|
|
134
|
+
if not resp_buf:
|
|
135
|
+
raise ConnectionResetError("Connection closed by server without response")
|
|
136
|
+
|
|
137
|
+
line = bytes(resp_buf).split(b"\n", 1)[0].decode("utf-8")
|
|
138
|
+
return json.loads(line)
|
|
139
|
+
except (socket.error, ConnectionError):
|
|
140
|
+
self.close()
|
|
141
|
+
raise
|
|
142
|
+
|
|
143
|
+
def ping(self) -> bool:
|
|
144
|
+
"""Checks if the control plane is alive."""
|
|
145
|
+
try:
|
|
146
|
+
resp = self._send_request({"action": "ping"})
|
|
147
|
+
return resp.get("status") == "PONG"
|
|
148
|
+
except Exception:
|
|
149
|
+
return False
|
|
150
|
+
|
|
151
|
+
def get_devices(self) -> List[DiscoveredDeviceInfo]:
|
|
152
|
+
"""Queries the control plane for dynamically auto-discovered SSDs/NVMe devices."""
|
|
153
|
+
resp = self._send_request({"action": "get_devices"})
|
|
154
|
+
raw_devices = resp.get("devices", [])
|
|
155
|
+
return [DiscoveredDeviceInfo.from_dict(d) for d in raw_devices]
|
|
156
|
+
|
|
157
|
+
def locate(
|
|
158
|
+
self,
|
|
159
|
+
uri: str,
|
|
160
|
+
auto_ingest: bool = True,
|
|
161
|
+
tier: Optional[str] = None,
|
|
162
|
+
pinned: bool = False,
|
|
163
|
+
) -> LocateResult:
|
|
164
|
+
"""
|
|
165
|
+
Sends a locate request to the control plane for a given URI.
|
|
166
|
+
Returns exact physical chunk routing layout.
|
|
167
|
+
"""
|
|
168
|
+
req = {
|
|
169
|
+
"action": "locate",
|
|
170
|
+
"uri": uri,
|
|
171
|
+
"auto_ingest": auto_ingest,
|
|
172
|
+
"pinned": pinned,
|
|
173
|
+
}
|
|
174
|
+
if tier is not None:
|
|
175
|
+
req["tier"] = tier.lower()
|
|
176
|
+
|
|
177
|
+
resp = self._send_request(req)
|
|
178
|
+
status = resp.get("status", "ERROR")
|
|
179
|
+
|
|
180
|
+
if status == "HIT":
|
|
181
|
+
chunk_layout = [
|
|
182
|
+
ChunkLocation.from_dict(c) for c in resp.get("chunk_layout", [])
|
|
183
|
+
]
|
|
184
|
+
return LocateResult(
|
|
185
|
+
status=status,
|
|
186
|
+
object_size=resp.get("object_size", 0),
|
|
187
|
+
chunk_size=resp.get("chunk_size", 4194304),
|
|
188
|
+
chunk_layout=chunk_layout,
|
|
189
|
+
)
|
|
190
|
+
elif status == "MISS":
|
|
191
|
+
return LocateResult(
|
|
192
|
+
status=status,
|
|
193
|
+
object_size=0,
|
|
194
|
+
chunk_size=0,
|
|
195
|
+
chunk_layout=[],
|
|
196
|
+
)
|
|
197
|
+
else:
|
|
198
|
+
err = resp.get("error_message", "Unknown error from control plane")
|
|
199
|
+
return LocateResult(
|
|
200
|
+
status=status,
|
|
201
|
+
object_size=0,
|
|
202
|
+
chunk_size=0,
|
|
203
|
+
chunk_layout=[],
|
|
204
|
+
error_message=err,
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
def ingest(
|
|
208
|
+
self,
|
|
209
|
+
uri: str,
|
|
210
|
+
tier: Optional[str] = None,
|
|
211
|
+
pinned: bool = False,
|
|
212
|
+
) -> LocateResult:
|
|
213
|
+
"""Forces immediate ingestion and striping of an object into the SSD or RAM pool."""
|
|
214
|
+
req = {
|
|
215
|
+
"action": "ingest",
|
|
216
|
+
"uri": uri,
|
|
217
|
+
"pinned": pinned,
|
|
218
|
+
}
|
|
219
|
+
if tier is not None:
|
|
220
|
+
req["tier"] = tier.lower()
|
|
221
|
+
resp = self._send_request(req)
|
|
222
|
+
status = resp.get("status", "ERROR")
|
|
223
|
+
if status in ("HIT", "INGESTED"):
|
|
224
|
+
chunk_layout = [
|
|
225
|
+
ChunkLocation.from_dict(c) for c in resp.get("chunk_layout", [])
|
|
226
|
+
]
|
|
227
|
+
return LocateResult(
|
|
228
|
+
status="HIT",
|
|
229
|
+
object_size=resp.get("object_size", 0),
|
|
230
|
+
chunk_size=resp.get("chunk_size", 4194304),
|
|
231
|
+
chunk_layout=chunk_layout,
|
|
232
|
+
)
|
|
233
|
+
else:
|
|
234
|
+
err = resp.get("error_message", "Ingestion failed")
|
|
235
|
+
return LocateResult(
|
|
236
|
+
status="ERROR",
|
|
237
|
+
object_size=0,
|
|
238
|
+
chunk_size=0,
|
|
239
|
+
chunk_layout=[],
|
|
240
|
+
error_message=err,
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
def preload(self, uri: str, tier: Optional[str] = None, pinned: bool = False) -> LocateResult:
|
|
244
|
+
"""
|
|
245
|
+
Proactively pre-loads / pre-warms a checkpoint from S3 or NFS into the local NVMe cache.
|
|
246
|
+
"""
|
|
247
|
+
return self.ingest(uri, tier=tier, pinned=pinned)
|
|
248
|
+
|
|
249
|
+
def pin(self, uri: str, pinned: bool = True) -> bool:
|
|
250
|
+
"""Pins or unpins an existing cached object so W-TinyLFU never evicts it."""
|
|
251
|
+
resp = self._send_request({
|
|
252
|
+
"action": "pin",
|
|
253
|
+
"uri": uri,
|
|
254
|
+
"pinned": pinned,
|
|
255
|
+
})
|
|
256
|
+
return resp.get("status") == "PONG"
|
|
257
|
+
|
|
258
|
+
def evict(self, uri: str) -> bool:
|
|
259
|
+
"""Evicts a specific URI from the cache and reclaims its 4 MiB blocks."""
|
|
260
|
+
resp = self._send_request({
|
|
261
|
+
"action": "evict",
|
|
262
|
+
"uri": uri,
|
|
263
|
+
})
|
|
264
|
+
return resp.get("status") in ("EVICTED", "PONG", "OK")
|
|
265
|
+
|
|
266
|
+
def purge(self) -> bool:
|
|
267
|
+
"""Purges all cached objects from the daemon and resets block allocators."""
|
|
268
|
+
resp = self._send_request({"action": "purge"})
|
|
269
|
+
return resp.get("status") in ("PURGED", "PONG", "OK")
|
|
270
|
+
|
|
271
|
+
def get_status(self) -> dict:
|
|
272
|
+
"""Queries live daemon metrics and pool utilization."""
|
|
273
|
+
return self._send_request({"action": "get_status"})
|
|
274
|
+
|
|
275
|
+
def stage_write(
|
|
276
|
+
self,
|
|
277
|
+
uri: str,
|
|
278
|
+
size_bytes: int,
|
|
279
|
+
tier: Optional[str] = None,
|
|
280
|
+
is_leader: bool = False,
|
|
281
|
+
auto_flush_s3: bool = True,
|
|
282
|
+
write_through: bool = False,
|
|
283
|
+
pinned: bool = False,
|
|
284
|
+
) -> StageWriteResult:
|
|
285
|
+
"""
|
|
286
|
+
Stages an asynchronous write-back or synchronous write-through allocation in the control plane.
|
|
287
|
+
Allocates physical chunk layouts across NVMe drives / RAM without blocking.
|
|
288
|
+
"""
|
|
289
|
+
req = {
|
|
290
|
+
"action": "stage_write",
|
|
291
|
+
"uri": uri,
|
|
292
|
+
"size_bytes": size_bytes,
|
|
293
|
+
"is_leader": is_leader,
|
|
294
|
+
"auto_flush_s3": auto_flush_s3,
|
|
295
|
+
"write_through": write_through,
|
|
296
|
+
"pinned": pinned,
|
|
297
|
+
}
|
|
298
|
+
if tier is not None:
|
|
299
|
+
req["tier"] = tier.lower()
|
|
300
|
+
|
|
301
|
+
resp = self._send_request(req)
|
|
302
|
+
status = resp.get("status", "ERROR")
|
|
303
|
+
|
|
304
|
+
if status in ("WRITESTAGED", "WRITE_STAGED"):
|
|
305
|
+
chunk_layout = [
|
|
306
|
+
ChunkLocation.from_dict(c) for c in resp.get("chunk_layout", [])
|
|
307
|
+
]
|
|
308
|
+
return StageWriteResult(
|
|
309
|
+
status=status,
|
|
310
|
+
object_size=resp.get("object_size", size_bytes),
|
|
311
|
+
chunk_size=resp.get("chunk_size", 4194304),
|
|
312
|
+
chunk_layout=chunk_layout,
|
|
313
|
+
sync_status=resp.get("sync_status", "STAGED"),
|
|
314
|
+
)
|
|
315
|
+
else:
|
|
316
|
+
err = resp.get("error_message", "Failed to stage write")
|
|
317
|
+
return StageWriteResult(
|
|
318
|
+
status="ERROR",
|
|
319
|
+
object_size=0,
|
|
320
|
+
chunk_size=0,
|
|
321
|
+
chunk_layout=[],
|
|
322
|
+
sync_status="ERROR",
|
|
323
|
+
error_message=err,
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
def append_write(
|
|
327
|
+
self,
|
|
328
|
+
uri: str,
|
|
329
|
+
size_bytes: int,
|
|
330
|
+
tier: Optional[str] = None,
|
|
331
|
+
) -> StageWriteResult:
|
|
332
|
+
"""
|
|
333
|
+
Incrementally allocates and appends chunk descriptors for a streaming write.
|
|
334
|
+
"""
|
|
335
|
+
req = {
|
|
336
|
+
"action": "append_write",
|
|
337
|
+
"uri": uri,
|
|
338
|
+
"size_bytes": size_bytes,
|
|
339
|
+
}
|
|
340
|
+
if tier is not None:
|
|
341
|
+
req["tier"] = tier.lower()
|
|
342
|
+
|
|
343
|
+
resp = self._send_request(req)
|
|
344
|
+
status = resp.get("status", "ERROR")
|
|
345
|
+
if status in ("WRITESTAGED", "WRITE_STAGED"):
|
|
346
|
+
chunk_layout = [
|
|
347
|
+
ChunkLocation.from_dict(c) for c in resp.get("chunk_layout", [])
|
|
348
|
+
]
|
|
349
|
+
return StageWriteResult(
|
|
350
|
+
status=status,
|
|
351
|
+
object_size=resp.get("object_size", size_bytes),
|
|
352
|
+
chunk_size=resp.get("chunk_size", 4194304),
|
|
353
|
+
chunk_layout=chunk_layout,
|
|
354
|
+
sync_status=resp.get("sync_status", "STAGED"),
|
|
355
|
+
)
|
|
356
|
+
else:
|
|
357
|
+
err = resp.get("error_message", "Failed to append write")
|
|
358
|
+
return StageWriteResult(
|
|
359
|
+
status="ERROR",
|
|
360
|
+
object_size=0,
|
|
361
|
+
chunk_size=0,
|
|
362
|
+
chunk_layout=[],
|
|
363
|
+
sync_status="ERROR",
|
|
364
|
+
error_message=err,
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
def commit_write(
|
|
368
|
+
self,
|
|
369
|
+
uri: str,
|
|
370
|
+
sha256: Optional[str] = None,
|
|
371
|
+
write_through: bool = False,
|
|
372
|
+
) -> bool:
|
|
373
|
+
"""
|
|
374
|
+
Commits a staged write in the control plane, triggering background sync to S3 (write-back)
|
|
375
|
+
or synchronously waiting for authoritative backend flush (write-through).
|
|
376
|
+
"""
|
|
377
|
+
req = {
|
|
378
|
+
"action": "commit_write",
|
|
379
|
+
"uri": uri,
|
|
380
|
+
"write_through": write_through,
|
|
381
|
+
}
|
|
382
|
+
if sha256 is not None:
|
|
383
|
+
req["sha256"] = sha256
|
|
384
|
+
|
|
385
|
+
resp = self._send_request(req)
|
|
386
|
+
status = resp.get("status", "ERROR")
|
|
387
|
+
return status in ("WRITECOMMITTED", "WRITE_COMMITTED")
|
|
388
|
+
|
|
389
|
+
def get_write_status(self, uri: str) -> dict:
|
|
390
|
+
"""
|
|
391
|
+
Queries current sync status (STAGED, COMMITTED, SYNCING_BACKEND, SYNCED_BACKEND).
|
|
392
|
+
"""
|
|
393
|
+
req = {
|
|
394
|
+
"action": "get_write_status",
|
|
395
|
+
"uri": uri,
|
|
396
|
+
}
|
|
397
|
+
resp = self._send_request(req)
|
|
398
|
+
return {
|
|
399
|
+
"status": resp.get("sync_status", "UNKNOWN"),
|
|
400
|
+
"progress_percent": resp.get("sync_progress_percent", 0.0),
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
def record_telemetry(
|
|
404
|
+
self,
|
|
405
|
+
event_type: str,
|
|
406
|
+
uri: str,
|
|
407
|
+
object_size_bytes: int,
|
|
408
|
+
duration_us: int,
|
|
409
|
+
bandwidth_mbps: float,
|
|
410
|
+
client_id: str = "python_benchmark",
|
|
411
|
+
chunk_count: int = 1,
|
|
412
|
+
local_meta_us: int = 0,
|
|
413
|
+
remote_meta_us: int = 0,
|
|
414
|
+
local_ttfb_us: int = 0,
|
|
415
|
+
remote_ttfb_us: int = 0,
|
|
416
|
+
time_saved_ms: int = 0,
|
|
417
|
+
cpu_pct: float = 0.0,
|
|
418
|
+
status: str = "OK",
|
|
419
|
+
) -> bool:
|
|
420
|
+
"""Emits telemetry metrics to the control plane daemon for Prometheus & Grafana."""
|
|
421
|
+
req = {
|
|
422
|
+
"action": "record_telemetry",
|
|
423
|
+
"event_type": event_type,
|
|
424
|
+
"uri": uri,
|
|
425
|
+
"object_size_bytes": object_size_bytes,
|
|
426
|
+
"duration_us": duration_us,
|
|
427
|
+
"bandwidth_mbps": bandwidth_mbps,
|
|
428
|
+
"client_id": client_id,
|
|
429
|
+
"chunk_count": chunk_count,
|
|
430
|
+
"local_meta_us": local_meta_us,
|
|
431
|
+
"remote_meta_us": remote_meta_us,
|
|
432
|
+
"local_ttfb_us": local_ttfb_us,
|
|
433
|
+
"remote_ttfb_us": remote_ttfb_us,
|
|
434
|
+
"time_saved_ms": time_saved_ms,
|
|
435
|
+
"cpu_pct": cpu_pct,
|
|
436
|
+
"status": status,
|
|
437
|
+
}
|
|
438
|
+
try:
|
|
439
|
+
resp = self._send_request(req)
|
|
440
|
+
return resp.get("status") == "PONG"
|
|
441
|
+
except Exception:
|
|
442
|
+
return False
|
|
443
|
+
|
|
444
|
+
def close(self) -> None:
|
|
445
|
+
if self._sock is not None:
|
|
446
|
+
try:
|
|
447
|
+
self._sock.close()
|
|
448
|
+
except Exception:
|
|
449
|
+
pass
|
|
450
|
+
self._sock = None
|
|
451
|
+
|
|
452
|
+
def __enter__(self) -> HyperCacheClient:
|
|
453
|
+
return self
|
|
454
|
+
|
|
455
|
+
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
|
|
456
|
+
self.close()
|