|
| 1 | +# Discovery |
| 2 | + |
| 3 | +> **Source:** [`cortex.discovery.daemon`](../reference/discovery/daemon.md), |
| 4 | +> [`cortex.discovery.client`](../reference/discovery/client.md), |
| 5 | +> [`cortex.discovery.protocol`](../reference/discovery/protocol.md) |
| 6 | +
|
| 7 | +Discovery is Cortex's control plane: a single long-lived process that maps |
| 8 | +topic names to ZMQ endpoints. It sits off the data path — once a subscriber |
| 9 | +has an endpoint, messages flow publisher → subscriber directly without the |
| 10 | +daemon's involvement. |
| 11 | + |
| 12 | +## Moving parts |
| 13 | + |
| 14 | +```mermaid |
| 15 | +flowchart LR |
| 16 | + subgraph DP[discovery package] |
| 17 | + PR[protocol.py<br/>DiscoveryRequest /<br/>DiscoveryResponse /<br/>TopicInfo] |
| 18 | + DM[daemon.py<br/>DiscoveryDaemon<br/>ZMQ REP loop] |
| 19 | + CL[client.py<br/>DiscoveryClient<br/>ZMQ REQ wrapper] |
| 20 | + end |
| 21 | +
|
| 22 | + CL -- msgpack REQ --> DM |
| 23 | + DM -- msgpack REP --> CL |
| 24 | + PR -.-> DM |
| 25 | + PR -.-> CL |
| 26 | +``` |
| 27 | + |
| 28 | +Everyone agrees on the wire format via `protocol.py`. The daemon runs a |
| 29 | +single-threaded REP loop. The client speaks REQ from every publisher and |
| 30 | +subscriber in the graph. |
| 31 | + |
| 32 | +## Daemon |
| 33 | + |
| 34 | +Implemented in [`DiscoveryDaemon`][cortex.discovery.daemon.DiscoveryDaemon]. |
| 35 | + |
| 36 | +Key behaviors: |
| 37 | + |
| 38 | +- Binds `zmq.REP` at `ipc:///tmp/cortex/discovery.sock` by default. |
| 39 | +- Maintains `_topics: dict[str, TopicInfo]` — **one publisher per topic**. |
| 40 | +- `RCVTIMEO=1000` on the socket so the loop can check `_running` for clean |
| 41 | + Ctrl-C. This also means the daemon is naturally single-request-at-a-time — |
| 42 | + a slow client blocks all others. |
| 43 | + |
| 44 | +### State transitions |
| 45 | + |
| 46 | +```mermaid |
| 47 | +stateDiagram-v2 |
| 48 | + [*] --> Starting |
| 49 | + Starting --> Running: bind OK |
| 50 | + Running --> Running: REGISTER → insert |
| 51 | + Running --> Running: LOOKUP → read |
| 52 | + Running --> Running: UNREGISTER → delete |
| 53 | + Running --> Running: LIST → snapshot |
| 54 | + Running --> Stopping: SIGINT / SHUTDOWN |
| 55 | + Stopping --> [*]: close socket, unlink .sock |
| 56 | +``` |
| 57 | + |
| 58 | +### Registry semantics |
| 59 | + |
| 60 | +| Case | Result | |
| 61 | +| -------------------------------------- | ------------------ | |
| 62 | +| New topic | Insert → OK | |
| 63 | +| Same topic, same `publisher_node` | Overwrite → OK (re-registration) | |
| 64 | +| Same topic, different `publisher_node` | Reject → ALREADY_EXISTS | |
| 65 | +| UNREGISTER missing topic | NOT_FOUND | |
| 66 | + |
| 67 | +## Client |
| 68 | + |
| 69 | +Implemented in [`DiscoveryClient`][cortex.discovery.client.DiscoveryClient]. |
| 70 | + |
| 71 | +Thin REQ wrapper around the protocol. Important operational detail: **REQ |
| 72 | +sockets stick after a timeout** — they block subsequent sends waiting for a |
| 73 | +reply that never came. The client handles this by closing and recreating the |
| 74 | +socket on every timeout (`_reconnect`). Callers don't see it. |
| 75 | + |
| 76 | +### REQ timeout recovery |
| 77 | + |
| 78 | +```mermaid |
| 79 | +flowchart TD |
| 80 | + S[send request] --> W[wait RCVTIMEO] |
| 81 | + W -->|reply| OK[return DiscoveryResponse] |
| 82 | + W -->|timeout| T[zmq.Again] |
| 83 | + T --> C[close REQ socket] |
| 84 | + C --> N[create fresh REQ<br/>same endpoint] |
| 85 | + N -->|attempts < retries| S |
| 86 | + N -->|exhausted| F[raise TimeoutError] |
| 87 | +``` |
| 88 | + |
| 89 | +### Polling helpers |
| 90 | + |
| 91 | +- [`lookup_topic(name)`][cortex.discovery.client.DiscoveryClient.lookup_topic] — |
| 92 | + one-shot, returns `None` on miss. |
| 93 | +- [`wait_for_topic(name, timeout, poll_interval)`][cortex.discovery.client.DiscoveryClient.wait_for_topic] — |
| 94 | + blocking poll loop (time.sleep). |
| 95 | +- [`wait_for_topic_async(name, timeout, poll_interval)`][cortex.discovery.client.DiscoveryClient.wait_for_topic_async] — |
| 96 | + async poll loop (asyncio.sleep). This is what [`Subscriber`][cortex.core.subscriber.Subscriber] |
| 97 | + uses when `wait_for_topic=True`. |
| 98 | + |
| 99 | +## Protocol |
| 100 | + |
| 101 | +Implemented in [`cortex.discovery.protocol`](../reference/discovery/protocol.md). |
| 102 | + |
| 103 | +| Type | Purpose | |
| 104 | +| -------------------------------------------------------------------- | ----------------------------------------- | |
| 105 | +| [`DiscoveryCommand`][cortex.discovery.protocol.DiscoveryCommand] | `REGISTER_TOPIC` / `UNREGISTER_TOPIC` / `LOOKUP_TOPIC` / `LIST_TOPICS` / `SHUTDOWN` | |
| 106 | +| [`DiscoveryStatus`][cortex.discovery.protocol.DiscoveryStatus] | `OK` / `NOT_FOUND` / `ALREADY_EXISTS` / `ERROR` | |
| 107 | +| [`TopicInfo`][cortex.discovery.protocol.TopicInfo] | name, address, message_type, fingerprint, publisher_node | |
| 108 | +| [`DiscoveryRequest`][cortex.discovery.protocol.DiscoveryRequest] | command + optional topic_info / topic_name | |
| 109 | +| [`DiscoveryResponse`][cortex.discovery.protocol.DiscoveryResponse] | status, message, topic_info, topics | |
| 110 | + |
| 111 | +All payloads are msgpack. `TopicInfo` is nested as a packed sub-blob so |
| 112 | +discovery responses stay flat. |
| 113 | + |
| 114 | +## Known limitations |
| 115 | + |
| 116 | +Summarized here, detailed in [critique.md](../critique.md): |
| 117 | + |
| 118 | +- One-publisher-per-topic. |
| 119 | +- No heartbeats or leases — crashed publishers leave stale entries. |
| 120 | +- Single-threaded REP — slow client starves others. |
| 121 | +- `retries=1` in the client is a fencepost; effective retries today is zero. |
| 122 | +- Daemon state lost on restart; publishers do not auto-re-register. |
| 123 | + |
| 124 | +## See also |
| 125 | + |
| 126 | +- [Concepts → Discovery protocol](../concepts/discovery-protocol.md) |
| 127 | +- [Getting started → Running the discovery daemon](../getting-started/discovery-daemon.md) |
| 128 | +- [Critique](../critique.md) |
0 commit comments