|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Streaming DEALER client for bidirectional communication with ROUTER.""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from collections.abc import Awaitable, Callable |
| 8 | + |
| 9 | +import zmq |
| 10 | + |
| 11 | +from aiperf.common.decorators import implements_protocol |
| 12 | +from aiperf.common.enums import CommClientType |
| 13 | +from aiperf.common.factories import CommunicationClientFactory |
| 14 | +from aiperf.common.hooks import background_task, on_stop |
| 15 | +from aiperf.common.messages import Message |
| 16 | +from aiperf.common.protocols import StreamingDealerClientProtocol |
| 17 | +from aiperf.common.utils import yield_to_event_loop |
| 18 | +from aiperf.zmq.zmq_base_client import BaseZMQClient |
| 19 | + |
| 20 | + |
| 21 | +@implements_protocol(StreamingDealerClientProtocol) |
| 22 | +@CommunicationClientFactory.register(CommClientType.STREAMING_DEALER) |
| 23 | +class ZMQStreamingDealerClient(BaseZMQClient): |
| 24 | + """ |
| 25 | + ZMQ DEALER socket client for bidirectional streaming with ROUTER. |
| 26 | +
|
| 27 | + Unlike ZMQDealerRequestClient (request-response pattern), this client is |
| 28 | + designed for streaming scenarios where messages flow bidirectionally without |
| 29 | + request-response pairing. |
| 30 | +
|
| 31 | + The DEALER socket sets an identity which allows the ROUTER to send messages back |
| 32 | + to this specific DEALER instance. |
| 33 | +
|
| 34 | + ASCII Diagram: |
| 35 | + ┌──────────────┐ ┌──────────────┐ |
| 36 | + │ DEALER │◄──── Stream ──────►│ ROUTER │ |
| 37 | + │ (Worker) │ │ (Manager) │ |
| 38 | + │ │ │ │ |
| 39 | + └──────────────┘ └──────────────┘ |
| 40 | +
|
| 41 | + Usage Pattern: |
| 42 | + - DEALER connects to ROUTER with a unique identity |
| 43 | + - DEALER sends messages to ROUTER |
| 44 | + - DEALER receives messages from ROUTER (routed by identity) |
| 45 | + - No request-response pairing - pure streaming |
| 46 | + - Supports concurrent message processing |
| 47 | +
|
| 48 | + Example: |
| 49 | + ```python |
| 50 | + # Create via comms (recommended - handles lifecycle management) |
| 51 | + dealer = comms.create_streaming_dealer_client( |
| 52 | + address=CommAddress.CREDIT_ROUTER, # or "tcp://localhost:5555" |
| 53 | + identity="worker-1", |
| 54 | + ) |
| 55 | +
|
| 56 | + async def handle_message(message: Message) -> None: |
| 57 | + if message.message_type == MessageType.CREDIT_DROP: |
| 58 | + do_some_work(message.credit) |
| 59 | + await dealer.send(CreditReturnMessage(...)) |
| 60 | +
|
| 61 | + dealer.register_receiver(handle_message) |
| 62 | +
|
| 63 | + # Lifecycle managed by comms - initialize/start/stop comms instead |
| 64 | + await comms.initialize() |
| 65 | + await comms.start() |
| 66 | + await dealer.send(WorkerReadyMessage(...)) |
| 67 | + ... |
| 68 | + await dealer.send(WorkerShutdownMessage(...)) |
| 69 | + await comms.stop() |
| 70 | + ``` |
| 71 | + """ |
| 72 | + |
| 73 | + def __init__( |
| 74 | + self, |
| 75 | + address: str, |
| 76 | + identity: str, |
| 77 | + bind: bool = False, |
| 78 | + socket_ops: dict | None = None, |
| 79 | + **kwargs, |
| 80 | + ) -> None: |
| 81 | + """ |
| 82 | + Initialize the streaming DEALER client. |
| 83 | +
|
| 84 | + Args: |
| 85 | + address: The address to connect to (e.g., "tcp://localhost:5555") |
| 86 | + identity: Unique identity for this DEALER (used by ROUTER for routing) |
| 87 | + bind: Whether to bind (True) or connect (False) the socket. |
| 88 | + Usually False for DEALER. |
| 89 | + socket_ops: Additional socket options to set |
| 90 | + **kwargs: Additional arguments passed to BaseZMQClient |
| 91 | + """ |
| 92 | + super().__init__( |
| 93 | + zmq.SocketType.DEALER, |
| 94 | + address, |
| 95 | + bind, |
| 96 | + socket_ops={**(socket_ops or {}), zmq.IDENTITY: identity.encode()}, |
| 97 | + client_id=identity, |
| 98 | + **kwargs, |
| 99 | + ) |
| 100 | + self.identity = identity |
| 101 | + self._receiver_handler: Callable[[Message], Awaitable[None]] | None = None |
| 102 | + |
| 103 | + def register_receiver(self, handler: Callable[[Message], Awaitable[None]]) -> None: |
| 104 | + """ |
| 105 | + Register handler for incoming messages from ROUTER. |
| 106 | +
|
| 107 | + The handler will be called for each message received. |
| 108 | +
|
| 109 | + Args: |
| 110 | + handler: Async function that takes (message: Message) |
| 111 | + """ |
| 112 | + if self._receiver_handler is not None: |
| 113 | + raise ValueError("Receiver handler already registered") |
| 114 | + self._receiver_handler = handler |
| 115 | + self.debug( |
| 116 | + lambda: f"Registered streaming DEALER receiver handler for {self.identity}" |
| 117 | + ) |
| 118 | + |
| 119 | + @on_stop |
| 120 | + async def _clear_receiver(self) -> None: |
| 121 | + """Clear receiver handler on stop.""" |
| 122 | + self._receiver_handler = None |
| 123 | + |
| 124 | + async def send(self, message: Message) -> None: |
| 125 | + """ |
| 126 | + Send message to ROUTER. |
| 127 | +
|
| 128 | + Args: |
| 129 | + message: The message to send |
| 130 | +
|
| 131 | + Raises: |
| 132 | + NotInitializedError: If socket not initialized |
| 133 | + CommunicationError: If send fails |
| 134 | + """ |
| 135 | + await self._check_initialized() |
| 136 | + |
| 137 | + if not isinstance(message, Message): |
| 138 | + raise TypeError( |
| 139 | + f"message must be an instance of Message, got {type(message).__name__}" |
| 140 | + ) |
| 141 | + |
| 142 | + try: |
| 143 | + # DEALER automatically handles framing - use single-frame send |
| 144 | + await self.socket.send(message.to_json_bytes()) |
| 145 | + if self.is_trace_enabled: |
| 146 | + self.trace(f"Sent message: {message}") |
| 147 | + except Exception as e: |
| 148 | + self.exception(f"Failed to send message: {e}") |
| 149 | + raise |
| 150 | + |
| 151 | + @background_task(immediate=True, interval=None) |
| 152 | + async def _streaming_dealer_receiver(self) -> None: |
| 153 | + """ |
| 154 | + Background task for receiving messages from ROUTER. |
| 155 | +
|
| 156 | + Runs continuously until stop is requested. Receives messages with DEALER |
| 157 | + envelope format: [empty_delimiter, message_bytes] or just [message_bytes] |
| 158 | + """ |
| 159 | + self.debug( |
| 160 | + lambda: f"Streaming DEALER receiver task started for {self.identity}" |
| 161 | + ) |
| 162 | + |
| 163 | + while not self.stop_requested: |
| 164 | + try: |
| 165 | + message_bytes = await self.socket.recv() |
| 166 | + if self.is_trace_enabled: |
| 167 | + self.trace(f"Received message: {message_bytes}") |
| 168 | + message = Message.from_json(message_bytes) |
| 169 | + |
| 170 | + if self._receiver_handler: |
| 171 | + self.execute_async(self._receiver_handler(message)) |
| 172 | + else: |
| 173 | + self.warning( |
| 174 | + f"Received {message.message_type} message but no handler registered" |
| 175 | + ) |
| 176 | + |
| 177 | + except zmq.Again: |
| 178 | + self.debug("No data on dealer socket received, yielding to event loop") |
| 179 | + await yield_to_event_loop() |
| 180 | + except Exception as e: |
| 181 | + self.exception(f"Exception receiving messages: {e}") |
| 182 | + await yield_to_event_loop() |
| 183 | + except asyncio.CancelledError: |
| 184 | + self.debug("Streaming DEALER receiver task cancelled") |
| 185 | + raise # re-raise the cancelled error |
| 186 | + |
| 187 | + self.debug( |
| 188 | + lambda: f"Streaming DEALER receiver task stopped for {self.identity}" |
| 189 | + ) |
0 commit comments