zenkey_fleet/bus/producer.rs
1//! Producer-side bring-up discipline (RFC 04 §5, RFC 05 §2.1/§3, RFC 08
2//! §6.1) — the MUSTs every producer owes the bus, as an API that makes the
3//! wrong shapes unrepresentable rather than a checklist that trusts review.
4//!
5//! The conformance review found these uniformly unenforced, with the
6//! in-repo dev tools modeling the violations; this module is the
7//! enforcement seam, and [`crate::bus::serve`]/[`crate::tape::generate`] now ride it
8//! or its rules. It lives beside — not inside — `serve.rs`, deliberately:
9//! `serve` is the *explorer's* mock (answer anything, wildcards welcome),
10//! while this is the *producer's* posture (concrete keys, ordered
11//! presence), and folding the strict API into the permissive one would
12//! give every mock a way to claim it is a producer.
13//!
14//! Three rules, three shapes:
15//!
16//! - **Order** ([`BringUp`]): a producer declares its queryables
17//! (`introspect`, `describe`, its procedures) **first**, and its `alive`
18//! liveliness token **last** — "alive ⇒ callable" (RFC 04 §5): callers
19//! attribute RPC silence against the roster, so a token that precedes
20//! the queryables manufactures false negatives. RFC 08 §6.1's bounded
21//! grace exists to tolerate declarations spawned concurrently at
22//! startup; this API sequences them instead, so there is no race for a
23//! checker to be graceful about. The token is only mintable by
24//! consuming the bring-up: alive-before-queryable does not typecheck.
25//! - **Vocabulary** ([`ReservedError`]): the RFC 05 §3 reserved error
26//! names as an enum, so a responder cannot misspell `error/unsupported`
27//! — and RFC 08 §6.1's conditional-procedure rule (declare always,
28//! answer `error/unsupported`/`error/gated` when gated) is writable
29//! without a string literal.
30//! - **Reply key** ([`Responder::reply`]): a procedure replies on its
31//! **own concrete key**, never by echoing `query.key_expr()` (RFC 05
32//! §2.1: consolidation keeps one reply *per reply key*, so a fleet
33//! echoing a shared wildcard selector consolidates down to one
34//! survivor). The responder holds its declared key and replies on it;
35//! the query's key is never consulted.
36
37use crate::{Error, Result};
38use zenoh::Session;
39use zenoh::handlers::FifoChannelHandler;
40use zenoh::liveliness::LivelinessToken;
41use zenoh::query::{Query, Queryable};
42
43/// The RFC 05 §3 reserved error vocabulary — the names every conforming
44/// caller understands, as a closed enum. Producer-specific names live under
45/// `error/<producer>/…` and are registered like subjects; these six are the
46/// convention's own.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
48pub enum ReservedError {
49 /// `error/invalid-args` — the request payload does not parse or fails
50 /// validation.
51 InvalidArgs,
52 /// `error/unauthorized` — the caller may not invoke this procedure.
53 Unauthorized,
54 /// `error/not-found` — the named entity does not exist here.
55 NotFound,
56 /// `error/unsupported` — the capability is absent from this build
57 /// (RFC 08 §6.1: a conditional procedure still declares, and answers
58 /// with this — "producer present, capability not in this build →
59 /// rebuild").
60 Unsupported,
61 /// `error/busy` — present and capable, but not now.
62 Busy,
63 /// `error/gated` — the capability is built in but disabled here by
64 /// policy or configuration (RFC 05 §3: gated writes keep their gate at
65 /// the server; RFC 08 §6.1: "capability built in, disabled here →
66 /// reconfigure").
67 Gated,
68}
69
70impl ReservedError {
71 /// Every reserved name, for iteration (renderers, doctors).
72 pub const ALL: [ReservedError; 6] = [
73 ReservedError::InvalidArgs,
74 ReservedError::Unauthorized,
75 ReservedError::NotFound,
76 ReservedError::Unsupported,
77 ReservedError::Busy,
78 ReservedError::Gated,
79 ];
80
81 /// The wire name, namespaced like a key (RFC 05 §3).
82 pub fn name(self) -> &'static str {
83 match self {
84 ReservedError::InvalidArgs => "error/invalid-args",
85 ReservedError::Unauthorized => "error/unauthorized",
86 ReservedError::NotFound => "error/not-found",
87 ReservedError::Unsupported => "error/unsupported",
88 ReservedError::Busy => "error/busy",
89 ReservedError::Gated => "error/gated",
90 }
91 }
92
93 /// The RFC 05 §3 error envelope, `{ "error": <name>, "message": … }`,
94 /// serialized as JSON (the RFC shows the envelope as JSON for
95 /// readability; a deployment whose payload default differs encodes the
96 /// same two fields itself).
97 pub fn envelope(self, message: &str) -> Vec<u8> {
98 serde_json::to_vec(&serde_json::json!({
99 "error": self.name(),
100 "message": message,
101 }))
102 .expect("two string fields serialize")
103 }
104}
105
106/// One declared `@rpc` queryable, bound to its own concrete key — the only
107/// key it will ever reply on.
108pub struct Responder {
109 key: String,
110 queryable: Queryable<FifoChannelHandler<Query>>,
111}
112
113impl std::fmt::Debug for Responder {
114 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115 f.debug_struct("Responder")
116 .field("key", &self.key)
117 .finish_non_exhaustive()
118 }
119}
120
121impl Responder {
122 /// The concrete key this responder was declared on — and replies on.
123 pub fn key(&self) -> &str {
124 &self.key
125 }
126
127 /// The next query, or `None` once the queryable is gone.
128 pub async fn next(&self) -> Option<Query> {
129 self.queryable.recv_async().await.ok()
130 }
131
132 /// The queries as a [`Stream`](futures_core::Stream) (#343).
133 ///
134 /// Borrows, which is the point: `reply` and `reply_err` take `&self`, so
135 /// a query pulled from this stream can still be answered while the stream
136 /// is held — the same receive/answer split [`next`](Self::next) has.
137 pub fn stream(&self) -> impl futures_core::Stream<Item = Query> + '_ {
138 self.queryable.stream()
139 }
140
141 /// Reply a value on this responder's **own concrete key** (RFC 05
142 /// §2.1). `query.key_expr()` is deliberately never consulted: echoing
143 /// the query's selector puts a fleet's replies on one shared wildcard
144 /// key, and default consolidation keeps one survivor per reply key.
145 pub async fn reply(
146 &self,
147 query: &Query,
148 payload: Vec<u8>,
149 encoding: Option<&str>,
150 ) -> Result<()> {
151 let reply = query.reply(self.key.clone(), payload);
152 let reply = match encoding {
153 Some(e) => reply.encoding(e),
154 None => reply,
155 };
156 reply.await.map_err(|e| Error::bus("reply", &self.key, e))
157 }
158
159 /// Refuse on Zenoh's reply-error channel with a [`ReservedError`]
160 /// envelope (RFC 05 §3: a value reply always means success; a failure
161 /// always rides `reply_err` — never a success payload carrying
162 /// `ok: false`).
163 pub async fn reply_err(
164 &self,
165 query: &Query,
166 error: ReservedError,
167 message: &str,
168 ) -> Result<()> {
169 query
170 .reply_err(error.envelope(message))
171 .encoding("application/json")
172 .await
173 .map_err(|e| Error::bus("reply_err", &self.key, e))
174 }
175
176 /// Undeclare, acknowledged.
177 pub async fn undeclare(self) -> Result<()> {
178 self.queryable
179 .undeclare()
180 .await
181 .map_err(|e| Error::bus("undeclare", &self.key, e))
182 }
183}
184
185/// The ordered bring-up (RFC 04 §5): queryables first, `alive` last — as
186/// one API in which the wrong order is unrepresentable.
187///
188/// ```no_run
189/// # async fn demo(session: zenoh::Session) -> zenkey_fleet::Result<()> {
190/// let mut up = zenkey_fleet::bus::producer::BringUp::new(&session);
191/// up.serve("v1/h-3fa9c2d41b7e/@rpc/sysinfo/introspect").await?;
192/// up.serve("v1/h-3fa9c2d41b7e/@rpc/sysinfo/describe").await?;
193/// // The token is mintable only by consuming the bring-up: every
194/// // queryable above is declared (awaited, not spawned) before it.
195/// let live = up.alive("v1/h-3fa9c2d41b7e/state/sysinfo/alive").await?;
196/// # let _ = live; Ok(()) }
197/// ```
198///
199/// Keys are written base-relative here because a *producer's* session is
200/// namespaced (the deployment base is session config, RFC 03 §1.1); an
201/// un-namespaced explorer passes full wire keys, which is equally fine —
202/// the API is a string seam either way, concreteness enforced.
203#[derive(Debug)]
204pub struct BringUp<'a> {
205 session: &'a Session,
206 responders: Vec<Responder>,
207}
208
209impl<'a> BringUp<'a> {
210 /// Start a bring-up: nothing is declared yet, and no `alive` token can
211 /// exist before [`alive`](Self::alive) consumes this value.
212 pub fn new(session: &'a Session) -> Self {
213 BringUp {
214 session,
215 responders: Vec::new(),
216 }
217 }
218
219 /// Declare one `@rpc` queryable on the producer's **own concrete key**,
220 /// awaited before return (no spawn race, so RFC 08 §6.1's bounded
221 /// grace has nothing to tolerate). Refused:
222 ///
223 /// - a wildcard key — a producer serves its own concrete keys, and a
224 /// concrete declared key is what makes [`Responder::reply`]'s
225 /// reply-key discipline meaningful (RFC 05 §2.1);
226 /// - `complete` is not a parameter at all: `@rpc` queryables are
227 /// **never** declared complete (RFC 05 §2.1 — one complete queryable
228 /// short-circuits `BestMatching` callers to a single reply).
229 pub async fn serve(&mut self, key: &str) -> Result<&Responder> {
230 let parsed = zenoh::key_expr::KeyExpr::try_from(key.to_string())
231 .map_err(|e| Error::bus("declare queryable", key, e))?;
232 if parsed.is_wild() {
233 // The caller handed us the key; nothing was declared.
234 return Err(Error::unaskable(
235 key.to_string(),
236 "a producer serves its own concrete key, never a wildcard \
237 (RFC 05 §2.1 — replies are attributed by their concrete reply \
238 key)",
239 ));
240 }
241 let queryable = crate::bus::teardown::declared(
242 "declare queryable",
243 key,
244 self.session.declare_queryable(parsed).complete(false),
245 )
246 .await?;
247 self.responders.push(Responder {
248 key: key.to_string(),
249 queryable,
250 });
251 Ok(self.responders.last().expect("just pushed"))
252 }
253
254 /// Declare the `alive` liveliness token — consuming the bring-up, so
255 /// the token structurally cannot precede the queryables ("alive ⇒
256 /// callable", RFC 04 §5).
257 pub async fn alive(self, alive_key: &str) -> Result<LiveProducer> {
258 let token = crate::bus::teardown::declared(
259 "declare alive token",
260 alive_key,
261 self.session
262 .liveliness()
263 .declare_token(alive_key.to_string()),
264 )
265 .await?;
266 Ok(LiveProducer {
267 token: Some(token),
268 responders: self.responders,
269 })
270 }
271
272 /// Finish **without** presence: hand back the declared responders and
273 /// never mint a token. This is the mock/synthetic path ([`crate::
274 /// generate`]'s impersonated producers, RFC 13 §5) — a tool that
275 /// answers for a producer must not also claim its presence. A real
276 /// producer wants [`alive`](Self::alive).
277 pub fn without_alive(self) -> Vec<Responder> {
278 self.responders
279 }
280}
281
282/// A producer that is up: queryables declared, then `alive` — held while
283/// serving.
284#[derive(Debug)]
285pub struct LiveProducer {
286 token: Option<LivelinessToken>,
287 /// The declared responders, in bring-up order — drive them
288 /// ([`Responder::next`]) to serve.
289 pub responders: Vec<Responder>,
290}
291
292impl LiveProducer {
293 /// Retire in the reverse of bring-up: retract `alive` **first** (the
294 /// roster must stop attributing silence to a producer that is going
295 /// away), then undeclare the queryables, acknowledged.
296 ///
297 /// A token that will not retract still bails before the queryables, and
298 /// deliberately: "alive ⇒ callable" (RFC 04 §5) is a claim that outlives
299 /// this call, and stripping callability while presence stands would
300 /// manufacture exactly the false negative the ordering exists to prevent.
301 /// The queryables themselves drain (#346): one that will not undeclare
302 /// must not leave the rest declared, and the failures are reported
303 /// together — the `bus::teardown` shape, shared with the monitor and the
304 /// replayer.
305 pub async fn retire(mut self) -> Result<()> {
306 if let Some(token) = self.token.take() {
307 token
308 .undeclare()
309 .await
310 .map_err(|e| Error::bus("retract alive token", "", e))?;
311 }
312 let declared: Vec<(String, Responder)> = self
313 .responders
314 .drain(..)
315 .map(|r| (r.key.clone(), r))
316 .collect();
317 crate::bus::teardown::drain_undeclare(declared, Responder::undeclare).await
318 }
319}