zenoh-flow 0.5.0-alpha.2

Zenoh-Flow: a Zenoh-based data flow programming framework for computations that span from the cloud to the device.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//
// Copyright (c) 2021 - 2023 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//

#![allow(clippy::manual_async_fn)]
use std::collections::HashMap;
use std::convert::TryFrom;

use crate::model::descriptor::{
    FlattenDataFlowDescriptor, OperatorDescriptor, SinkDescriptor, SourceDescriptor,
};
use crate::model::record::DataFlowRecord;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use uuid::Uuid;
use zenoh::prelude::ZenohId;

use self::dataflow::loader::LoaderConfig;
use crate::runtime::dataflow::loader::Loader;
use crate::types::{ControlMessage, FlowId, RuntimeId};
use crate::zferror;
use crate::zfresult::ErrorKind;
use crate::{DaemonResult, Result as ZFResult};
use uhlc::{Timestamp, HLC};
use zenoh::Session;
use zrpc::zrpcresult::{ZRPCError, ZRPCResult};
use zrpc_macros::zservice;

pub mod dataflow;
pub mod resources;
pub mod worker_pool;

/// The context of a Zenoh Flow runtime.
/// This is shared across all the instances in a runtime.
/// It allows sharing the `zenoh::Session`, the `Loader`,
/// the `HLC` and other relevant singletons.
#[derive(Clone)]
pub struct RuntimeContext {
    pub session: Arc<Session>,
    pub loader: Arc<Loader>,
    pub hlc: Arc<HLC>,
    pub runtime_name: RuntimeId,
    pub runtime_uuid: ZenohId,
    pub shared_memory_element_size: usize,
    pub shared_memory_elements: usize,
    pub shared_memory_backoff: u64,
    pub use_shm: bool,
}

/// The context of a Zenoh Flow graph instance.
#[derive(Clone)]
pub struct InstanceContext {
    pub flow_id: FlowId,
    pub instance_id: Uuid,
    pub runtime: RuntimeContext,
}

/// This function maps a [`FlattenDataFlowDescriptor`](`FlattenDataFlowDescriptor`) into
/// the infrastructure.
/// The initial implementation simply maps all missing mapping
/// to the provided runtime.
///
/// # Errors
/// An error variant is returned in case of:
/// - unable to map node to infrastructure
pub async fn map_to_infrastructure(
    mut descriptor: FlattenDataFlowDescriptor,
    runtime: &str,
) -> ZFResult<FlattenDataFlowDescriptor> {
    log::debug!("[Dataflow mapping] Begin mapping for: {}", descriptor.flow);

    let runtime_id: Arc<str> = runtime.into();

    // Initial "stupid" mapping, if an operator is not mapped, we map to the local runtime.
    // function is async because it could involve other nodes.
    let mut mapping = descriptor.mapping.clone().map_or(HashMap::new(), |m| m);

    for o in &descriptor.operators {
        mapping
            .entry(o.id.clone())
            .or_insert_with(|| runtime_id.clone());
    }

    for o in &descriptor.sources {
        mapping
            .entry(o.id.clone())
            .or_insert_with(|| runtime_id.clone());
    }

    for o in &descriptor.sinks {
        mapping
            .entry(o.id.clone())
            .or_insert_with(|| runtime_id.clone());
    }
    log::trace!(
        "[Dataflow mapping] Mapping for: {} is {:?}",
        descriptor.flow,
        mapping
    );
    descriptor.mapping = Some(mapping);
    Ok(descriptor)
}

// Runtime related types, maybe can be moved.

/// Runtime Status, either Ready or Not Ready.
/// When Ready it is able to accept commands and instantiate graphs.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "lowercase")]
pub enum RuntimeStatusKind {
    Ready,
    NotReady,
}

/// The Runtime information.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RuntimeInfo {
    pub id: ZenohId,
    pub name: Arc<str>,
    pub tags: Vec<String>,
    pub status: RuntimeStatusKind,
    // Do we need/want also RAM usage?
}

/// The detailed runtime status.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RuntimeStatus {
    pub id: ZenohId,
    pub running_flows: usize,
    pub running_operators: usize,
    pub running_sources: usize,
    pub running_sinks: usize,
    pub running_connectors: usize,
}

/// Wrapper for Zenoh kind.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "lowercase")]
pub enum ZenohConfigKind {
    Peer,
    Client,
}

impl std::fmt::Display for ZenohConfigKind {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            ZenohConfigKind::Peer => write!(f, "peer"),
            ZenohConfigKind::Client => write!(f, "client"),
        }
    }
}

impl TryFrom<zenoh::config::whatami::WhatAmI> for ZenohConfigKind {
    type Error = crate::zfresult::Error;
    fn try_from(value: zenoh::config::whatami::WhatAmI) -> Result<Self, Self::Error> {
        match value {
            zenoh::config::whatami::WhatAmI::Client => Ok(Self::Client),
            zenoh::config::whatami::WhatAmI::Peer => Ok(Self::Peer),
            _ => Err(zferror!(ErrorKind::MissingConfiguration).into()),
        }
    }
}

#[allow(clippy::from_over_into)]
impl Into<zenoh::config::whatami::WhatAmI> for ZenohConfigKind {
    fn into(self) -> zenoh::config::whatami::WhatAmI {
        match self {
            Self::Peer => zenoh::config::whatami::WhatAmI::Peer,
            Self::Client => zenoh::config::whatami::WhatAmI::Client,
        }
    }
}

/// The runtime configuration.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RuntimeConfig {
    pub pid_file: String, //Where the PID file resides
    pub path: String,     //Where the libraries are downloaded/located
    pub name: String,
    pub uuid: ZenohId,
    pub loader: LoaderConfig,
}

/// The type of [`Job`](`Job`) to be executed by the workers
///
/// [^note]: This enum is not exhaustive yet, it will evolve in the future
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum JobKind {
    CreateInstance(FlattenDataFlowDescriptor, Uuid),
    DeleteInstance(Uuid),
    Instantiate(FlattenDataFlowDescriptor, Uuid),
    Teardown(Uuid),
    StartInstance(Uuid),
    StopInstance(Uuid),
    StartNode(Uuid, String),
    StopNode(Uuid, String),
}

/// The status of a [`Job`](`Job`), associated with a timestamp from when the status change happenend
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum JobStatus {
    Submitted(Timestamp),
    Started(Timestamp),
    Done(Timestamp),
    Failed(Timestamp, String),
}

/// All the needed information to run a Job
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Job {
    id: Uuid,
    job: JobKind,
    status: JobStatus,
    assignee: Option<usize>,
}

impl Job {
    fn new_instantiate(
        dfd: FlattenDataFlowDescriptor,
        instance_id: Uuid,
        id: Uuid,
        ts: Timestamp,
    ) -> Self {
        Self {
            id,
            job: JobKind::Instantiate(dfd, instance_id),
            status: JobStatus::Submitted(ts),
            assignee: None,
        }
    }

    fn new_create(
        dfd: FlattenDataFlowDescriptor,
        instance_id: Uuid,
        id: Uuid,
        ts: Timestamp,
    ) -> Self {
        Self {
            id,
            job: JobKind::CreateInstance(dfd, instance_id),
            status: JobStatus::Submitted(ts),
            assignee: None,
        }
    }

    fn new_teardown(fid: Uuid, id: Uuid, ts: Timestamp) -> Self {
        Self {
            id,
            job: JobKind::Teardown(fid),
            status: JobStatus::Submitted(ts),
            assignee: None,
        }
    }

    fn new_delete(fid: Uuid, id: Uuid, ts: Timestamp) -> Self {
        Self {
            id,
            job: JobKind::DeleteInstance(fid),
            status: JobStatus::Submitted(ts),
            assignee: None,
        }
    }

    fn new_start(fid: Uuid, id: Uuid, ts: Timestamp) -> Self {
        Self {
            id,
            job: JobKind::StartInstance(fid),
            status: JobStatus::Submitted(ts),
            assignee: None,
        }
    }

    fn new_stop(fid: Uuid, id: Uuid, ts: Timestamp) -> Self {
        Self {
            id,
            job: JobKind::StopInstance(fid),
            status: JobStatus::Submitted(ts),
            assignee: None,
        }
    }

    fn new_start_node(fid: Uuid, node_id: String, id: Uuid, ts: Timestamp) -> Self {
        Self {
            id,
            job: JobKind::StartNode(fid, node_id),
            status: JobStatus::Submitted(ts),
            assignee: None,
        }
    }

    fn new_stop_node(fid: Uuid, node_id: String, id: Uuid, ts: Timestamp) -> Self {
        Self {
            id,
            job: JobKind::StopNode(fid, node_id),
            status: JobStatus::Submitted(ts),
            assignee: None,
        }
    }

    pub fn get_id(&self) -> &Uuid {
        &self.id
    }

    pub fn get_kind(&self) -> &JobKind {
        &self.job
    }

    pub fn get_status(&self) -> &JobStatus {
        &self.status
    }

    pub fn get_assigne(&self) -> &Option<usize> {
        &self.assignee
    }

    pub fn set_status(&mut self, status: JobStatus) {
        self.status = status;
    }

    pub fn assign(&mut self, assignee: usize) {
        self.assignee.replace(assignee);
    }

    pub fn started(&mut self, assignee: usize, ts: Timestamp) {
        self.assignee.replace(assignee);
        self.status = JobStatus::Started(ts);
    }

    pub fn done(&mut self, ts: Timestamp) {
        self.status = JobStatus::Done(ts);
    }

    pub fn failed(&mut self, ts: Timestamp, error_description: String) {
        self.status = JobStatus::Failed(ts, error_description)
    }
}

/// The interface the Daemon expose to a client
/// (eg. the cli, or, the mgmt API)[^note]
/// The service is exposed using zenoh-rpc, the server and client
/// are generated automatically.
///
/// [^note]: We may split this interface in the future.
#[zservice(
    timeout_s = 60,
    prefix = "zf/daemon",
    service_uuid = "11111111111111111111111111111111"
)]
pub trait DaemonInterface {
    /// Creates an instance of the given [`FlattenDataFlowDescriptor`][^note].
    ///
    /// This function:
    /// 1) Generates the instance `Uuid`
    /// 2) Maps the flow into the infrastructure
    /// 3) Creates the associated record
    /// 4) Stores the record in Zenoh
    /// 5) Prepares all the involved runtimes to host the data flow instance
    ///
    /// Returns the [`Uuid`] associated with the instance.
    ///
    /// [^note]: When the registry will be in place it will take the Flow identifier as parameter
    ///
    /// # Errors
    ///
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - unable to map
    /// - unable to prepare nodes
    async fn create_instance(&self, flow: FlattenDataFlowDescriptor) -> DaemonResult<Uuid>;
    //TODO: workaround - it should just take the ID of the flow (when
    // the registry will be in place)

    /// Deletes the given instance.
    ///
    /// This function:
    /// 1) Cleans the instance nodes from all the involved runtimes.
    /// 2) Deletes the record from zenoh
    ///
    /// # Errors
    ///
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - instance not stopped
    /// - unable to clean
    /// - zenoh error
    async fn delete_instance(&self, instance_id: Uuid) -> DaemonResult<DataFlowRecord>;

    /// Instantiates the given [`FlattenDataFlowDescriptor`][^note].
    ///
    /// The instance contains an [`Uuid`] that identifies it uniquely.
    /// The actual instantiation process runs asynchronously in the runtime.
    ///
    /// Returns the [`Uuid`] associated with the instance.
    ///
    /// It is equivalent to calling `create_instance` and then `start_instance`.
    ///
    /// [^note]: When the registry will be in place it will take the Flow identifier as parameter.
    ///
    /// # Errors
    ///
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - unable to instantiate
    async fn instantiate(&self, flow: FlattenDataFlowDescriptor) -> DaemonResult<Uuid>;
    //TODO: workaround - it should just take the ID of the flow (when
    // the registry will be in place)

    /// Sends a teardown request for the given instance identified by the [`Uuid`].
    ///
    /// Note that the request is asynchronous, the runtime that receives the request will return
    /// immediately, but the teardown process will run asynchronously in the runtime.
    ///
    /// It is equivalent to calling `stop_instance` and then `delete_instance`.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - unable to teardown
    /// - instance not found
    async fn teardown(&self, instance_id: Uuid) -> DaemonResult<DataFlowRecord>;

    /// Starts the instance on all involved nodes.
    ///
    /// It first starts all the nodes and then the sources.
    ///
    /// # Errors
    ///
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - instance not found
    /// - instance already started
    async fn start_instance(&self, instance_id: Uuid) -> DaemonResult<()>;

    /// Stops the instance on all involved nodes.
    ///
    /// It first stops the sources then the other nodes.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - unable to clean
    async fn stop_instance(&self, instance_id: Uuid) -> DaemonResult<DataFlowRecord>;

    /// Starts the given graph node for the given instance.
    /// A graph node can be a source, a sink, a connector, or an operator.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - record not found
    /// - node already started
    /// - node not found
    async fn start_node(&self, instance_id: Uuid, node: String) -> DaemonResult<()>;

    /// Stops the given graph node from the given instance.
    /// A graph node can be a source, a sink, a connector, or an operator.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - instance not found
    /// - node not found
    /// - node already stopped
    async fn stop_node(&self, instance_id: Uuid, node: String) -> DaemonResult<()>;

    // FIXME A source now has several outputs.

    // /// Start a recording for the given source.
    // ///
    // /// # Errors
    // /// An error variant is returned in case of:
    // /// - error on zenoh-rpc
    // /// - record not found
    // /// - record already started
    // /// - source not found
    // /// - node is not a source
    // async fn start_record(&self, instance_id: Uuid, source_id: NodeId) -> DaemonResult<String>;

    // /// Stops the recording for the given source.
    // ///
    // /// # Errors
    // /// An error variant is returned in case of:
    // /// - error on zenoh-rpc
    // /// - record not found
    // /// - record already stopped
    // /// - source not found
    // /// - node is not a source
    // async fn stop_record(&self, instance_id: Uuid, source_id: NodeId) -> DaemonResult<String>;

    // /// Starts the replay for the given source.
    // /// The replay creates a new node that has the same port and links as the
    // /// source is replaying.
    // ///
    // /// # Errors
    // /// An error variant is returned in case of:
    // /// - error on zenoh-rpc
    // /// - record not found
    // /// - replay already started
    // /// - source not found
    // /// - node is not a source
    // async fn start_replay(
    //     &self,
    //     instance_id: Uuid,
    //     source_id: NodeId,
    //     key_expr: String,
    // ) -> DaemonResult<NodeId>;

    // /// Stops the replay for the given source.
    // /// This stops and removes the replay node from the graph.
    // ///
    // /// # Errors
    // /// An error variant is returned in case of:
    // /// - error on zenoh-rpc
    // /// - record not found
    // /// - replay already stopped
    // /// - source not found
    // /// - node is not a source
    // async fn stop_replay(
    //     &self,
    //     instance_id: Uuid,
    //     source_id: NodeId,
    //     replay_id: NodeId,
    // ) -> DaemonResult<NodeId>;

    // /// Gets the state of the given graph node for the given instance.
    // /// A graph node can be a source, a sink, a connector, or an operator.
    // /// The node state represents the current state of the node:
    // /// `enum NodeState { Running, Stopped, Error(err) }`
    // async fn get_node_state(&self, instance_id: Uuid, node: String) -> DaemonResult<NodeState>;
}

/// The interface the Daemon expose to other daemons.
/// The service is exposed using zenoh-rpc, the server and client
/// are generated automatically.
///
#[zservice(
    timeout_s = 600,
    prefix = "zf/daemon",
    service_uuid = "22222222222222222222222222222222"
)]
pub trait DaemonInterfaceInternal {
    /// Prepares the runtime host the instance identified by the [`Uuid`].
    ///
    /// Preparing a runtime means, fetch the operators/source/sinks libraries,
    /// create the needed structures in memory, the links.
    /// Once everything is prepared the runtime should return the [`DataFlowRecord`]
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - unable to prepare
    async fn prepare(&self, instance_id: Uuid) -> DaemonResult<DataFlowRecord>;

    /// Cleans the "remains" of the given instance: unload the libraries, drop data structures and
    /// destroy links.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - unable to clean
    async fn clean(&self, instance_id: Uuid) -> DaemonResult<DataFlowRecord>;

    /// Starts the sinks, connectors, and operators for the given instance.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - instance not found
    /// - instance already started
    async fn start(&self, instance_id: Uuid) -> DaemonResult<()>;

    /// Starts the sources for the given instance.
    /// Note that this should be called only after the `start(instance)` has returned
    /// successfully otherwise data may be lost.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - instance not found
    /// - sources already started
    async fn start_sources(&self, instance_id: Uuid) -> DaemonResult<()>;

    /// Stops the sinks, connectors, and operators for the given instance.
    ///
    /// Note that this should be called after the `stop_sources(instance)` has returned
    /// successfully otherwise data may be lost.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - instance not found
    /// - instance already stopped
    async fn stop(&self, instance_id: Uuid) -> DaemonResult<()>;

    /// Stops the sources for the given instance.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - instance not found
    /// - sources already stopped
    async fn stop_sources(&self, instance_id: Uuid) -> DaemonResult<()>;

    /// Sends the `message` to `node` for the given instance.
    ///
    /// This is useful for sending out-of-band notification to a node (eg. in the case of deadline
    /// miss notification).
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    /// - instance not found
    async fn notify_runtime(
        &self,
        instance_id: Uuid,
        runtime: String,
        message: ControlMessage,
    ) -> DaemonResult<()>;

    /// Checks the compatibility for the given `operator`.
    ///
    /// Compatibility is based on tags and some machine characteristics (eg. CPU architecture, OS).
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    async fn check_operator_compatibility(
        &self,
        operator: OperatorDescriptor,
    ) -> DaemonResult<bool>;

    /// Checks the compatibility for the given `source`.
    ///
    /// Compatibility is based on tags and some machine characteristics (eg. CPU architecture, OS)
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    async fn check_source_compatibility(&self, source: SourceDescriptor) -> DaemonResult<bool>;

    /// Checks the compatibility for the given `sink`.
    ///
    /// Compatibility is based on tags and some machine characteristics (eg. CPU architecture, OS)
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - error on zenoh-rpc
    async fn check_sink_compatibility(&self, sink: SinkDescriptor) -> DaemonResult<bool>;
}