-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcache_client_example.py
More file actions
194 lines (161 loc) · 6.06 KB
/
cache_client_example.py
File metadata and controls
194 lines (161 loc) · 6.06 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
#!/usr/bin/env python3
"""Minimal DLSlimeCache assignment-directory client.
Start NanoCtrl and the cache service first:
nanoctrl start
dlslime-cache start --ctrl http://127.0.0.1:3000 \
--host 127.0.0.1 --port 8765 --memory-size 1G
Then run this client:
python examples/python/cache_client_example.py --url http://127.0.0.1:8765
The client asks the cache service for its PeerAgent, creates a local
PeerAgent, connects over an available RDMA NIC, stores a generated read
manifest to allocate cache slabs, writes bytes into those cache MR offsets,
then queries the same (peer_agent_id, version) back and RDMA-reads the bytes.
Stop the service when finished:
dlslime-cache stop
"""
from __future__ import annotations
import argparse
import ctypes
from dlslime._slime_c import Assignment
from dlslime.cache import CacheClient
from dlslime.cache.types import assignment_from_json, assignment_to_tuple
def first_available_nic(resource: dict | None) -> str:
if resource is None:
return "<unavailable>"
for nic in resource.get("nics") or []:
if nic.get("health", "AVAILABLE") == "UNAVAILABLE":
continue
for port in nic.get("ports") or []:
state = str(port.get("state", "ACTIVE")).upper()
if state not in {"ACTIVE", "UNKNOWN"}:
continue
return "{name}:port{port}:{link}".format(
name=nic.get("name", "<unknown>"),
port=port.get("port", 1),
link=port.get("link_type", "UNKNOWN"),
)
return "<none>"
def make_payload(nbytes: int) -> bytes:
return bytes((i % 251 for i in range(nbytes)))
def check_manifest(stored: dict, queried: dict) -> None:
if queried != stored:
raise RuntimeError(
"manifest mismatch: queried assignment manifest differs from stored manifest"
)
def check_payload(expected: bytes, actual: bytes) -> None:
if actual == expected:
return
if len(actual) != len(expected):
raise RuntimeError(
"payload length mismatch: " f"expected={len(expected)} actual={len(actual)}"
)
mismatch = next(
idx for idx, (left, right) in enumerate(zip(expected, actual)) if left != right
)
raise RuntimeError(
"payload mismatch: "
f"offset={mismatch} expected={expected[mismatch]} actual={actual[mismatch]}"
)
def parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser()
p.add_argument("--url", default="http://127.0.0.1:8765", help="Cache service URL")
p.add_argument("--length", type=int, default=640 * 1024)
p.add_argument("--timeout", type=float, default=60.0)
return p.parse_args()
def main() -> None:
args = parse_args()
payload = make_payload(args.length)
source = ctypes.create_string_buffer(payload, args.length)
readback = ctypes.create_string_buffer(args.length)
client = CacheClient(url=args.url)
server = client.connect_to_server(timeout_sec=args.timeout)
agent = client.peer_agent
stored = None
server_peer = server["peer_agent_id"]
cache_mr_name = server["cache_mr_name"]
print(
"connected:",
f"client_peer_agent_id={agent.alias}",
f"server_peer_agent_id={server_peer}",
f"server_nic={first_available_nic(server.get('resource'))}",
f"cache_mr={cache_mr_name}",
)
source_name = "cache-example-source"
readback_name = "cache-example-readback"
agent.register_memory_region(
source_name,
ctypes.addressof(source),
0,
args.length,
)
readback_handle = agent.register_memory_region(
readback_name,
ctypes.addressof(readback),
0,
args.length,
)
cache_remote_handle = agent.get_handle(cache_mr_name, server_peer)
try:
stored = client.store(
[Assignment(readback_handle, cache_remote_handle, 0, 0, args.length)],
)
print(
"stored:",
f"peer_agent_id={stored['peer_agent_id']}",
f"version={stored['version']}",
f"assignments={len(stored['assignments'])}",
f"total_bytes={stored['total_bytes']}",
f"slabs={stored['slab_ids']}",
)
print("writing bytes into allocated cache slabs...")
write_future = agent.write(
server_peer,
[
(
source_name,
cache_mr_name,
item["target_offset"],
item["source_offset"],
item["length"],
)
for item in stored["assignments"]
],
)
write_future.wait()
queried = client.load(stored["peer_agent_id"], stored["version"])
print(
"queried:",
f"peer_agent_id={queried['peer_agent_id']}",
f"version={queried['version']}",
f"assignments={len(queried['assignments'])}",
f"total_bytes={queried['total_bytes']}",
)
check_manifest(stored, queried)
read_assignments = [
assignment_to_tuple(assignment_from_json(item))
for item in queried["assignments"]
]
print("reading bytes back from cache service...")
read_future = agent.read(server_peer, read_assignments)
read_future.wait()
check_payload(payload, bytes(readback.raw[: args.length]))
print("correctness: ok")
print("slab lengths:", [a["length"] for a in queried["assignments"]])
finally:
cleanup_error = None
if stored is not None:
try:
deleted = client.delete(stored["peer_agent_id"], stored["version"])
print(
"deleted:",
f"peer_agent_id={stored['peer_agent_id']}",
f"version={stored['version']}",
f"deleted={deleted}",
)
except Exception as exc:
cleanup_error = exc
agent.shutdown()
if cleanup_error is not None:
raise cleanup_error
if __name__ == "__main__":
main()