1use std::{
2 collections::{BTreeMap, BTreeSet},
3 path::PathBuf,
4 time::Duration,
5};
6
7use crate::{
8 BuildId, DataflowId, SessionId,
9 common::{DaemonId, GitSource},
10 descriptor::{Descriptor, ResolvedNode},
11 id::{DataId, NodeId, OperatorId},
12};
13
14#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
20pub struct StateCatchUpEntry {
21 pub sequence: u64,
22 pub operation: StateCatchUpOperation,
23}
24
25#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
27pub enum StateCatchUpOperation {
28 SetParam {
29 node_id: NodeId,
30 key: String,
31 value: serde_json::Value,
32 },
33 DeleteParam {
34 node_id: NodeId,
35 key: String,
36 },
37}
38
39pub use crate::common::Timestamped;
40
41#[derive(Debug, serde::Serialize, serde::Deserialize)]
42#[non_exhaustive]
43pub enum RegisterResult {
44 #[non_exhaustive]
50 Ok {
51 daemon_id: DaemonId,
53 #[serde(default)]
88 peer_zenoh_endpoints: Vec<String>,
89 },
90 Err(String),
91}
92
93impl RegisterResult {
94 pub fn ok(daemon_id: DaemonId, peer_zenoh_endpoints: Vec<String>) -> Self {
96 Self::Ok {
97 daemon_id,
98 peer_zenoh_endpoints,
99 }
100 }
101
102 pub fn to_result(self) -> eyre::Result<DaemonId> {
104 self.into_parts().map(|(daemon_id, _)| daemon_id)
105 }
106
107 pub fn into_parts(self) -> eyre::Result<(DaemonId, Vec<String>)> {
110 match self {
111 RegisterResult::Ok {
112 daemon_id,
113 peer_zenoh_endpoints,
114 } => Ok((daemon_id, peer_zenoh_endpoints)),
115 RegisterResult::Err(err) => Err(eyre::eyre!(err)),
116 }
117 }
118}
119
120#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
124pub enum ResolveMachineReply {
125 ResolveMachineResult {
127 found: bool,
128 address: Option<std::net::SocketAddr>,
132 },
133}
134
135#[allow(clippy::large_enum_variant)]
136#[derive(Debug, serde::Deserialize, serde::Serialize)]
137pub enum DaemonCoordinatorEvent {
138 Build(BuildDataflowNodes),
139 Spawn(SpawnDataflowNodes),
140 AllNodesReady {
141 dataflow_id: DataflowId,
142 exited_before_subscribe: Vec<NodeId>,
143 },
144 StopDataflow {
145 dataflow_id: DataflowId,
146 grace_duration: Option<Duration>,
147 #[serde(default)]
148 force: bool,
149 },
150 ReloadDataflow {
151 dataflow_id: DataflowId,
152 node_id: NodeId,
153 operator_id: Option<OperatorId>,
154 },
155 Logs {
156 dataflow_id: DataflowId,
157 node_id: NodeId,
158 tail: Option<usize>,
159 },
160 RestartNode {
161 dataflow_id: DataflowId,
162 node_id: NodeId,
163 grace_duration: Option<Duration>,
164 },
165 StopNode {
166 dataflow_id: DataflowId,
167 node_id: NodeId,
168 grace_duration: Option<Duration>,
169 },
170 SetParam {
171 dataflow_id: DataflowId,
172 node_id: NodeId,
173 key: String,
174 value: serde_json::Value,
175 },
176 DeleteParam {
177 dataflow_id: DataflowId,
178 node_id: NodeId,
179 key: String,
180 },
181 Destroy,
182 Heartbeat,
183 PeerDaemonDisconnected {
184 daemon_id: DaemonId,
185 },
186 AddNode {
189 dataflow_id: DataflowId,
190 node: crate::descriptor::ResolvedNode,
191 uv: bool,
192 },
193 RemoveNode {
195 dataflow_id: DataflowId,
196 node_id: NodeId,
197 grace_duration: Option<Duration>,
198 },
199 ReplaceNode {
214 dataflow_id: DataflowId,
215 node: crate::descriptor::ResolvedNode,
216 unresolved_node: crate::descriptor::Node,
217 uv: bool,
218 grace_duration: Option<Duration>,
219 },
220 AddMapping {
222 dataflow_id: DataflowId,
223 source_node: NodeId,
224 source_output: DataId,
225 target_node: NodeId,
226 target_input: DataId,
227 },
228 RemoveMapping {
230 dataflow_id: DataflowId,
231 source_node: NodeId,
232 source_output: DataId,
233 target_node: NodeId,
234 target_input: DataId,
235 },
236 #[non_exhaustive]
242 StartTopicDebugStream {
243 dataflow_id: DataflowId,
244 outputs: Vec<(NodeId, DataId)>,
245 subscription_id: uuid::Uuid,
246 },
247 StopTopicDebugStream {
250 dataflow_id: DataflowId,
251 subscription_id: uuid::Uuid,
252 },
253 StateCatchUp {
256 dataflow_id: DataflowId,
257 entries: Vec<StateCatchUpEntry>,
259 },
260}
261
262impl DaemonCoordinatorEvent {
263 pub fn start_topic_debug_stream(
266 dataflow_id: DataflowId,
267 outputs: Vec<(NodeId, DataId)>,
268 subscription_id: uuid::Uuid,
269 ) -> Self {
270 Self::StartTopicDebugStream {
271 dataflow_id,
272 outputs,
273 subscription_id,
274 }
275 }
276}
277
278#[derive(Debug, serde::Deserialize, serde::Serialize)]
279pub struct BuildDataflowNodes {
280 pub build_id: BuildId,
281 pub session_id: SessionId,
282 pub local_working_dir: Option<PathBuf>,
290 pub git_sources: BTreeMap<NodeId, GitSource>,
291 pub prev_git_sources: BTreeMap<NodeId, GitSource>,
292 #[serde(serialize_with = "crate::descriptor::serialize_without_path_fallback")]
293 pub dataflow_descriptor: Descriptor,
294 pub nodes_on_machine: BTreeSet<NodeId>,
295 pub uv: bool,
296}
297
298#[derive(Debug, serde::Deserialize, serde::Serialize)]
299pub struct SpawnDataflowNodes {
300 pub build_id: Option<BuildId>,
301 pub session_id: SessionId,
302 pub dataflow_id: DataflowId,
303 pub local_working_dir: Option<PathBuf>,
311 pub nodes: BTreeMap<NodeId, ResolvedNode>,
312 #[serde(serialize_with = "crate::descriptor::serialize_without_path_fallback")]
313 pub dataflow_descriptor: Descriptor,
314 pub spawn_nodes: BTreeSet<NodeId>,
315 pub uv: bool,
316 pub write_events_to: Option<PathBuf>,
317 #[serde(default, skip_serializing_if = "Option::is_none")]
319 pub artifact_base_url: Option<String>,
320}
321
322#[cfg(test)]
323mod register_result_tests {
324 use super::*;
325
326 #[test]
331 fn a_reply_without_peer_endpoints_decodes_as_no_peers() {
332 let legacy = r#"{"Ok":{"daemon_id":{"machine_id":"A","uuid":"00000000-0000-0000-0000-000000000001"}}}"#;
333 let decoded: RegisterResult =
334 serde_json::from_str(legacy).expect("legacy register reply must stay decodable");
335 let (_, peers) = decoded.into_parts().expect("legacy reply is Ok");
336 assert!(peers.is_empty());
337 }
338
339 #[test]
340 fn peer_endpoints_round_trip() {
341 let peers = vec!["tcp/10.0.2.100:5456".to_string()];
342 let encoded = serde_json::to_string(&RegisterResult::Ok {
343 daemon_id: DaemonId::new(Some("A".to_string())),
344 peer_zenoh_endpoints: peers.clone(),
345 })
346 .expect("serialize");
347 let decoded: RegisterResult = serde_json::from_str(&encoded).expect("deserialize");
348 assert_eq!(decoded.into_parts().expect("ok").1, peers);
349 }
350
351 #[test]
354 fn an_error_reply_is_an_error_through_both_accessors() {
355 assert!(RegisterResult::Err("nope".into()).to_result().is_err());
356 assert!(RegisterResult::Err("nope".into()).into_parts().is_err());
357 }
358}
359
360#[cfg(test)]
361mod path_fallback_tests {
362 use super::*;
363 use crate::descriptor::{CoreNodeKind, CustomNode, Node};
364
365 #[test]
366 fn daemon_wire_descriptor_stays_legacy_while_resolved_node_keeps_fallback() {
367 let id: NodeId = "module.worker".parse().unwrap();
368 let mut node = Node::new(id.clone());
369 node.path = Some("modules/worker".into());
370 node.path_fallback = Some("worker".into());
371 let descriptor = Descriptor::new(vec![node]);
372 let mut custom = CustomNode::new("modules/worker".into());
373 custom.path_fallback = Some("worker".into());
374 let resolved = ResolvedNode::new(id.clone(), CoreNodeKind::Custom(custom));
375 let spawn = SpawnDataflowNodes {
376 build_id: None,
377 session_id: SessionId::generate(),
378 dataflow_id: DataflowId::from(uuid::Uuid::new_v4()),
379 local_working_dir: None,
380 nodes: BTreeMap::from([(id.clone(), resolved)]),
381 dataflow_descriptor: descriptor.clone(),
382 spawn_nodes: BTreeSet::from([id]),
383 uv: false,
384 write_events_to: None,
385 artifact_base_url: None,
386 };
387 let build = BuildDataflowNodes {
388 build_id: BuildId::generate(),
389 session_id: SessionId::generate(),
390 local_working_dir: None,
391 git_sources: BTreeMap::new(),
392 prev_git_sources: BTreeMap::new(),
393 dataflow_descriptor: descriptor.clone(),
394 nodes_on_machine: BTreeSet::new(),
395 uv: false,
396 };
397 let spawn_json = serde_json::to_value(&spawn).unwrap();
398 let build_json = serde_json::to_value(&build).unwrap();
399 let mut legacy = descriptor;
400 legacy.nodes[0].path_fallback = None;
401 let legacy = serde_json::to_value(legacy).unwrap();
402 assert_eq!(spawn_json["dataflow_descriptor"], legacy);
403 assert_eq!(build_json["dataflow_descriptor"], legacy);
404 let decoded: SpawnDataflowNodes = serde_json::from_value(spawn_json).unwrap();
405 let CoreNodeKind::Custom(custom) = &decoded.nodes.values().next().unwrap().kind else {
406 panic!("expected a custom node");
407 };
408 assert_eq!(custom.path_fallback.as_deref(), Some("worker"));
409 }
410}