|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""TCP one-sided read through two PeerAgent processes. |
| 3 | +
|
| 4 | +The initiator reads bytes directly out of a memory region published by the |
| 5 | +target, then notifies the target so both processes can shut down cleanly. |
| 6 | +
|
| 7 | +Prerequisites: |
| 8 | + 1. Start NanoCtrl: cd NanoCtrl && cargo run --release |
| 9 | + 2. Redis must be reachable. |
| 10 | + 3. dlslime built with BUILD_TCP=ON (default). |
| 11 | +
|
| 12 | +Usage: |
| 13 | + python p2p_tcp_rc_read_peer_agent_two_process.py --role target |
| 14 | + python p2p_tcp_rc_read_peer_agent_two_process.py --role initiator |
| 15 | + python p2p_tcp_rc_read_peer_agent_two_process.py --role target --ctrl_address http://host:4479 |
| 16 | + python p2p_tcp_rc_read_peer_agent_two_process.py --role initiator --ctrl_address http://host:4479 |
| 17 | + python p2p_tcp_rc_read_peer_agent_two_process.py --role target --local_host 10.0.0.2 |
| 18 | + python p2p_tcp_rc_read_peer_agent_two_process.py --role initiator --local_host 10.0.0.3 |
| 19 | +""" |
| 20 | + |
| 21 | +import argparse |
| 22 | +import ctypes |
| 23 | +from typing import Optional |
| 24 | + |
| 25 | +from dlslime import PeerAgent |
| 26 | + |
| 27 | + |
| 28 | +INITIATOR_ALIAS = "tcp_read_initiator" |
| 29 | +TARGET_ALIAS = "tcp_read_target" |
| 30 | +PAYLOAD = b"one-sided-read-via-peer-agent" |
| 31 | +DONE = b"done" |
| 32 | + |
| 33 | + |
| 34 | +def _buffer_from_bytes(data: bytes) -> ctypes.Array: |
| 35 | + buf = ctypes.create_string_buffer(len(data)) |
| 36 | + ctypes.memmove(ctypes.addressof(buf), data, len(data)) |
| 37 | + return buf |
| 38 | + |
| 39 | + |
| 40 | +def run_initiator( |
| 41 | + ctrl_url: str, local_host: Optional[str], local_port: int, connect_timeout: float |
| 42 | +) -> None: |
| 43 | + agent = PeerAgent(ctrl_url=ctrl_url, alias=INITIATOR_ALIAS) |
| 44 | + try: |
| 45 | + buf_a = ctypes.create_string_buffer(64) |
| 46 | + addr_a = ctypes.addressof(buf_a) |
| 47 | + |
| 48 | + # Register the local MR before connect_to so endpoint_info() carries it |
| 49 | + # when the rendezvous fires. Required for one-sided ops on TCP. |
| 50 | + agent.register_memory_region("buf_a", addr_a, 0, 64) |
| 51 | + |
| 52 | + if TARGET_ALIAS not in agent.list_agents(): |
| 53 | + raise RuntimeError(f"target agent {TARGET_ALIAS!r} is not running") |
| 54 | + |
| 55 | + resolved_host = agent._resolve_tcp_local_host(local_host) |
| 56 | + print(f"[initiator] TCP local host: {resolved_host}:{local_port}") |
| 57 | + conn = agent.connect_to( |
| 58 | + TARGET_ALIAS, |
| 59 | + transport="tcp", |
| 60 | + local_host=local_host, |
| 61 | + local_port=local_port, |
| 62 | + ) |
| 63 | + conn.wait(timeout=connect_timeout) |
| 64 | + local_info = conn.endpoint.endpoint_info() |
| 65 | + print( |
| 66 | + f"[initiator] Connected over TCP: {INITIATOR_ALIAS} -> {TARGET_ALIAS} " |
| 67 | + f"(local={local_info.get('host')}:{local_info.get('port')})" |
| 68 | + ) |
| 69 | + |
| 70 | + done_buf = _buffer_from_bytes(DONE) |
| 71 | + |
| 72 | + ep = conn.endpoint |
| 73 | + if not ep.is_connected(): |
| 74 | + raise RuntimeError( |
| 75 | + "TCP endpoint is not connected; make sure both processes publish " |
| 76 | + "a peer-reachable --local_host" |
| 77 | + ) |
| 78 | + peer_info = conn.peer_endpoint_info |
| 79 | + assert peer_info is not None |
| 80 | + remote_mr_info = peer_info.get("mr_info", {}).get("buf_b") |
| 81 | + if remote_mr_info is None: |
| 82 | + raise RuntimeError( |
| 83 | + f"{TARGET_ALIAS!r} is connected but did not publish buf_b; " |
| 84 | + "start the target process first" |
| 85 | + ) |
| 86 | + |
| 87 | + h_local = ep.register_memory_region("buf_a_loc", addr_a, 0, 64) |
| 88 | + h_remote = ep.register_remote_memory_region("buf_b_rem", remote_mr_info) |
| 89 | + |
| 90 | + st = ep.read([(h_local, h_remote, 0, 0, len(PAYLOAD))]).wait() |
| 91 | + assert st == 0, f"read failed: {st}" |
| 92 | + assert ( |
| 93 | + bytes(buf_a[: len(PAYLOAD)]) == PAYLOAD |
| 94 | + ), "initiator did not read bytes" |
| 95 | + print( |
| 96 | + "[initiator] target->initiator one-sided read = " |
| 97 | + f"{bytes(buf_a[: len(PAYLOAD)])!r} ok" |
| 98 | + ) |
| 99 | + |
| 100 | + st = agent.send( |
| 101 | + TARGET_ALIAS, (ctypes.addressof(done_buf), 0, len(DONE)) |
| 102 | + ).wait() |
| 103 | + assert st == 0, f"done send failed: {st}" |
| 104 | + print("[initiator] Sent completion notice.") |
| 105 | + finally: |
| 106 | + agent.shutdown() |
| 107 | + |
| 108 | + |
| 109 | +def run_target( |
| 110 | + ctrl_url: str, local_host: Optional[str], local_port: int, connect_timeout: float |
| 111 | +) -> None: |
| 112 | + agent = PeerAgent(ctrl_url=ctrl_url, alias=TARGET_ALIAS) |
| 113 | + try: |
| 114 | + buf_b = ctypes.create_string_buffer(64) |
| 115 | + addr_b = ctypes.addressof(buf_b) |
| 116 | + |
| 117 | + # Pre-fill the target buffer with the payload the initiator reads out. |
| 118 | + ctypes.memmove(addr_b, PAYLOAD, len(PAYLOAD)) |
| 119 | + |
| 120 | + # Register MRs before connect_to so endpoint_info() carries them when |
| 121 | + # the rendezvous fires. Required for one-sided ops on TCP. |
| 122 | + agent.register_memory_region("buf_b", addr_b, 0, 64) |
| 123 | + |
| 124 | + resolved_host = agent._resolve_tcp_local_host(local_host) |
| 125 | + print(f"[target] TCP local host: {resolved_host}:{local_port}") |
| 126 | + conn = agent.connect_to( |
| 127 | + INITIATOR_ALIAS, |
| 128 | + transport="tcp", |
| 129 | + local_host=local_host, |
| 130 | + local_port=local_port, |
| 131 | + ) |
| 132 | + conn.wait(timeout=connect_timeout) |
| 133 | + local_info = conn.endpoint.endpoint_info() |
| 134 | + print( |
| 135 | + f"[target] Connected over TCP: {TARGET_ALIAS} -> {INITIATOR_ALIAS} " |
| 136 | + f"(local={local_info.get('host')}:{local_info.get('port')})" |
| 137 | + ) |
| 138 | + |
| 139 | + done_buf = ctypes.create_string_buffer(len(DONE)) |
| 140 | + |
| 141 | + if not conn.endpoint.is_connected(): |
| 142 | + raise RuntimeError( |
| 143 | + "TCP endpoint is not connected; make sure both processes publish " |
| 144 | + "a peer-reachable --local_host" |
| 145 | + ) |
| 146 | + print("[target] Published buffer; waiting for initiator completion.") |
| 147 | + |
| 148 | + st = agent.recv( |
| 149 | + INITIATOR_ALIAS, (ctypes.addressof(done_buf), 0, len(DONE)) |
| 150 | + ).wait() |
| 151 | + assert st == 0, f"done recv failed: {st}" |
| 152 | + assert bytes(done_buf[: len(DONE)]) == DONE, "initiator did not send done" |
| 153 | + print("[target] Initiator completed read; shutting down.") |
| 154 | + finally: |
| 155 | + agent.shutdown() |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + parser = argparse.ArgumentParser( |
| 160 | + description="TCP one-sided read through two PeerAgent processes" |
| 161 | + ) |
| 162 | + parser.add_argument( |
| 163 | + "--role", |
| 164 | + choices=("initiator", "target"), |
| 165 | + required=True, |
| 166 | + help="Process role to run", |
| 167 | + ) |
| 168 | + parser.add_argument( |
| 169 | + "--ctrl_address", |
| 170 | + "--ctrl", |
| 171 | + default="http://127.0.0.1:4479", |
| 172 | + help="NanoCtrl URL", |
| 173 | + ) |
| 174 | + parser.add_argument( |
| 175 | + "--local_host", |
| 176 | + default=None, |
| 177 | + help="Local TCP host/IP to publish to the peer; defaults to discovered host", |
| 178 | + ) |
| 179 | + parser.add_argument( |
| 180 | + "--local_port", |
| 181 | + type=int, |
| 182 | + default=0, |
| 183 | + help="Local TCP port to bind; 0 lets the OS choose", |
| 184 | + ) |
| 185 | + parser.add_argument( |
| 186 | + "--connect_timeout", |
| 187 | + type=float, |
| 188 | + default=60.0, |
| 189 | + help="Seconds to wait for the peer-agent TCP connection", |
| 190 | + ) |
| 191 | + args = parser.parse_args() |
| 192 | + if args.role == "initiator": |
| 193 | + run_initiator( |
| 194 | + args.ctrl_address, args.local_host, args.local_port, args.connect_timeout |
| 195 | + ) |
| 196 | + else: |
| 197 | + run_target( |
| 198 | + args.ctrl_address, args.local_host, args.local_port, args.connect_timeout |
| 199 | + ) |
0 commit comments