zenoh-ext 1.9.0

Zenoh: extensions to the client API.
Documentation
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
//
// Copyright (c) 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>
//

//! To manage groups and group memberships
use std::{
    collections::HashMap,
    convert::TryInto,
    ops::Add,
    sync::Arc,
    time::{Duration, Instant},
};

use flume::{Receiver, Sender};
use futures::{prelude::*, select};
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
use zenoh::{
    bytes::ZBytesReader,
    internal::{bail, Condition, TaskController},
    key_expr::{keyexpr, KeyExpr, OwnedKeyExpr},
    pubsub::Publisher,
    qos::Priority,
    Error as ZError, Result as ZResult, Session,
};

const GROUP_PREFIX: &str = "zenoh/ext/net/group";
const EVENT_POSTFIX: &str = "evt";
const VIEW_REFRESH_LEASE_RATIO: f32 = 0.75f32;
const DEFAULT_LEASE: Duration = Duration::from_secs(18);
const DEFAULT_PRIORITY: Priority = Priority::DataHigh;

#[zenoh_macros::unstable]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct JoinEvent {
    pub member: Member,
}

#[zenoh_macros::unstable]
#[derive(Serialize, Deserialize, Debug)]
pub struct LeaseExpiredEvent {
    pub mid: OwnedKeyExpr,
}

#[zenoh_macros::unstable]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct LeaveEvent {
    pub mid: OwnedKeyExpr,
}

#[zenoh_macros::unstable]
#[derive(Serialize, Deserialize, Debug)]
pub struct NewLeaderEvent {
    pub mid: OwnedKeyExpr,
}

#[zenoh_macros::unstable]
#[derive(Serialize, Deserialize, Debug)]
struct KeepAliveEvent {
    pub mid: OwnedKeyExpr,
}

#[zenoh_macros::unstable]
#[derive(Serialize, Deserialize, Debug)]
enum GroupNetEvent {
    Join(JoinEvent),
    Leave(LeaveEvent),
    KeepAlive(KeepAliveEvent),
}

/// Events exposed to the user to be informed for relevant changes in the group.
#[zenoh_macros::unstable]
#[derive(Serialize, Deserialize, Debug)]
pub enum GroupEvent {
    Join(JoinEvent),
    Leave(LeaveEvent),
    LeaseExpired(LeaseExpiredEvent),
    NewLeader(NewLeaderEvent),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[zenoh_macros::unstable]
pub enum MemberLiveliness {
    Auto,
    Manual,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[zenoh_macros::unstable]
pub struct Member {
    mid: OwnedKeyExpr,
    info: Option<String>,
    liveliness: MemberLiveliness,
    lease: Duration,
    refresh_ratio: f32,
    #[serde(skip)]
    priority: Priority,
}

impl Member {
    pub fn new<T>(mid: T) -> ZResult<Member>
    where
        T: TryInto<OwnedKeyExpr> + Send,
        <T as TryInto<OwnedKeyExpr>>::Error: Into<ZError>,
    {
        let mid: OwnedKeyExpr = mid.try_into().map_err(|e| e.into())?;
        if mid.is_wild() {
            bail!("Member ID is not allowed to contain wildcards: {}", mid);
        }
        Ok(Member {
            mid,
            info: None,
            liveliness: MemberLiveliness::Auto,
            lease: DEFAULT_LEASE,
            refresh_ratio: VIEW_REFRESH_LEASE_RATIO,
            priority: DEFAULT_PRIORITY,
        })
    }

    pub fn id(&self) -> &keyexpr {
        &self.mid
    }

    pub fn info<T>(mut self, i: T) -> Self
    where
        T: Into<String>,
    {
        self.info = Some(i.into());
        self
    }

    pub fn lease(mut self, d: Duration) -> Self {
        self.lease = d;
        self
    }

    pub fn liveliness(mut self, l: MemberLiveliness) -> Self {
        self.liveliness = l;
        self
    }

    pub fn refresh_ratio(mut self, r: f32) -> Self {
        self.refresh_ratio = r;
        self
    }

    pub fn priority(mut self, p: Priority) -> Self {
        self.priority = p;
        self
    }
}

struct GroupState {
    gid: String,
    local_member: Member,
    members: Mutex<HashMap<OwnedKeyExpr, (Member, Instant)>>,
    group_publisher: Publisher<'static>,
    user_events_tx: Mutex<Option<Sender<GroupEvent>>>,
    cond: Condition,
}

#[zenoh_macros::unstable]
pub struct Group {
    state: Arc<GroupState>,
    task_controller: TaskController,
}

impl Drop for Group {
    fn drop(&mut self) {
        // cancel background tasks
        self.task_controller.terminate_all(Duration::from_secs(10));
    }
}

async fn keep_alive_task(state: Arc<GroupState>) {
    let mid = state.local_member.mid.clone();
    let evt = GroupNetEvent::KeepAlive(KeepAliveEvent { mid });
    let buf = bincode::serialize(&evt).unwrap();
    let period = state
        .local_member
        .lease
        .mul_f32(state.local_member.refresh_ratio);
    loop {
        tokio::time::sleep(period).await;
        tracing::trace!("Sending Keep Alive for: {}", &state.local_member.mid);
        let _ = state.group_publisher.put(buf.clone()).await;
    }
}

async fn watchdog_task(s: Arc<GroupState>, period: Duration) {
    loop {
        tokio::time::sleep(period).await;
        let now = Instant::now();
        let mut ms = s.members.lock().await;
        let expired_members: Vec<OwnedKeyExpr> = ms
            .iter()
            .filter(|e| e.1 .1 < now)
            .map(|e| e.0.clone())
            .collect();

        for e in &expired_members {
            tracing::debug!("Member with lease expired: {}", e);
            ms.remove(e);
        }
        if !expired_members.is_empty() {
            tracing::debug!("Other members list: {:?}", ms.keys());
            drop(ms);
            let u_evt = &*s.user_events_tx.lock().await;
            for e in expired_members {
                if let Some(tx) = u_evt {
                    tx.send(GroupEvent::LeaseExpired(LeaseExpiredEvent { mid: e }))
                        .unwrap()
                }
            }
        }
    }
}

async fn query_handler(z: Arc<Session>, state: Arc<GroupState>) {
    let qres: KeyExpr = format!(
        "{}/{}/{}",
        GROUP_PREFIX, &state.gid, &state.local_member.mid
    )
    .try_into()
    .unwrap();
    tracing::debug!("Started query handler for: {}", &qres);
    let buf = bincode::serialize(&state.local_member).unwrap();
    let queryable = z.declare_queryable(&qres).await.unwrap();

    while let Ok(query) = queryable.recv_async().await {
        tracing::trace!("Serving query for: {}", &qres);
        query.reply(qres.clone(), buf.clone()).await.unwrap();
    }
}

async fn net_event_handler(z: Arc<Session>, state: Arc<GroupState>) {
    let sub = z
        .declare_subscriber(state.group_publisher.key_expr())
        .await
        .unwrap();
    while let Ok(s) = sub.recv_async().await {
        match bincode::deserialize_from::<ZBytesReader, GroupNetEvent>(s.payload().reader()) {
            Ok(evt) => match evt {
                GroupNetEvent::Join(je) => {
                    tracing::debug!("Member join: {:?}", &je.member);
                    let alive_till = Instant::now().add(je.member.lease);
                    let mut ms = state.members.lock().await;
                    ms.insert(je.member.mid.clone(), (je.member.clone(), alive_till));
                    tracing::debug!("Other members list: {:?}", ms.keys());
                    state.cond.notify_all();
                    drop(ms);
                    let u_evt = &*state.user_events_tx.lock().await;
                    if let Some(tx) = u_evt {
                        tx.send(GroupEvent::Join(je)).unwrap()
                    }
                }
                GroupNetEvent::Leave(le) => {
                    tracing::debug!("Member leave: {:?}", &le.mid);
                    let mut ms = state.members.lock().await;
                    ms.remove(&le.mid);
                    tracing::debug!("Other members list: {:?}", ms.keys());
                    drop(ms);
                    let u_evt = &*state.user_events_tx.lock().await;
                    if let Some(tx) = u_evt {
                        tx.send(GroupEvent::Leave(le)).unwrap()
                    }
                }
                GroupNetEvent::KeepAlive(kae) => {
                    tracing::debug!(
                        "KeepAlive from {} ({})",
                        &kae.mid,
                        if kae.mid.ne(&state.local_member.mid) {
                            "other"
                        } else {
                            "myself"
                        }
                    );
                    if kae.mid.ne(&state.local_member.mid) {
                        let mut mm = state.members.lock().await;
                        let v = mm.remove(&kae.mid);
                        match v {
                            Some((m, _)) => {
                                tracing::trace!("Updating leasefor: {:?}", &kae.mid);
                                let alive_till = Instant::now().add(m.lease);
                                mm.insert(m.mid.clone(), (m, alive_till));
                            }
                            None => {
                                tracing::debug!(
                                    "Received Keep Alive from unknown member: {}",
                                    &kae.mid
                                );
                                let qres = format!("{}/{}/{}", GROUP_PREFIX, &state.gid, kae.mid);
                                // @TODO: we could also send this member info
                                let qc = zenoh::query::ConsolidationMode::None;
                                tracing::trace!("Issuing Query for {}", &qres);
                                let receiver = z.get(&qres).consolidation(qc).await.unwrap();

                                while let Ok(reply) = receiver.recv_async().await {
                                    match reply.result() {
                                        Ok(sample) => {
                                            match bincode::deserialize_from::<ZBytesReader, Member>(
                                                sample.payload().reader(),
                                            ) {
                                                Ok(m) => {
                                                    let mut expiry = Instant::now();
                                                    expiry = expiry.add(m.lease);
                                                    tracing::debug!(
                                                        "Received member information: {:?}",
                                                        &m
                                                    );
                                                    mm.insert(kae.mid.clone(), (m.clone(), expiry));
                                                    tracing::debug!(
                                                        "Other members list: {:?}",
                                                        mm.keys()
                                                    );
                                                    // Advertise a JoinEvent
                                                    let u_evt = &*state.user_events_tx.lock().await;
                                                    if let Some(tx) = u_evt {
                                                        let je = JoinEvent { member: m };
                                                        tx.send_async(GroupEvent::Join(je))
                                                            .await
                                                            .unwrap()
                                                    }
                                                }
                                                Err(e) => {
                                                    tracing::warn!(
                                                        "Unable to deserialize the Member info received: {}", e);
                                                }
                                            }
                                        }
                                        Err(e) => {
                                            tracing::warn!("Error received: {:?}", e);
                                        }
                                    }
                                }
                                state.cond.notify_all();
                            }
                        }
                    } else {
                        tracing::trace!("KeepAlive from Local Participant -- Ignoring");
                    }
                }
            },
            Err(e) => {
                tracing::warn!("Failed decoding net-event due to: {:?}", e);
            }
        }
    }
}

impl Group {
    pub async fn join<T>(z: Arc<Session>, group: T, with: Member) -> ZResult<Group>
    where
        T: TryInto<OwnedKeyExpr>,
        <T as TryInto<OwnedKeyExpr>>::Error: Into<ZError>,
    {
        let group: OwnedKeyExpr = group.try_into().map_err(|e| e.into())?;
        if group.is_wild() {
            bail!("Group ID is not allowed to contain wildcards: {}", group);
        }

        let event_expr = format!("{GROUP_PREFIX}/{group}/{EVENT_POSTFIX}");
        let publisher = z
            .declare_publisher(event_expr)
            .priority(with.priority)
            .await
            .unwrap();
        let state = Arc::new(GroupState {
            gid: String::from(group),
            local_member: with.clone(),
            members: Mutex::new(Default::default()),
            group_publisher: publisher,
            user_events_tx: Mutex::new(Default::default()),
            cond: Condition::new(),
        });
        let is_auto_liveliness = matches!(with.liveliness, MemberLiveliness::Auto);

        // announce the member:
        tracing::debug!("Sending Join Message for local member: {:?}", &with);
        let join_evt = GroupNetEvent::Join(JoinEvent { member: with });
        let buf = bincode::serialize(&join_evt).unwrap();
        let _ = state.group_publisher.put(buf).await;

        let task_controller = TaskController::default();
        // If the liveliness is manual it is the user who has to assert it.
        if is_auto_liveliness {
            task_controller.spawn_abortable(keep_alive_task(state.clone()));
        }
        task_controller.spawn_abortable(net_event_handler(z.clone(), state.clone()));
        task_controller.spawn_abortable(query_handler(z.clone(), state.clone()));
        task_controller.spawn_abortable(watchdog_task(state.clone(), Duration::from_secs(1)));
        Ok(Group {
            state,
            task_controller,
        })
    }

    /// Returns a receivers that will allow to receive notifications for group events.
    /// Notice that there can be a single subscription at the time, each call to subscribe
    /// will cancel the previous subscription.
    pub async fn subscribe(&self) -> Receiver<GroupEvent> {
        let (tx, rx) = flume::unbounded();
        *self.state.user_events_tx.lock().await = Some(tx);
        rx
    }

    /// Returns the group identifier.
    pub fn group_id(&self) -> &str {
        &self.state.gid
    }

    /// Returns this member identifier.
    pub fn local_member_id(&self) -> &str {
        &self.state.local_member.mid
    }

    /// Returns the current group view, in other terms the list
    /// of group members.
    pub async fn view(&self) -> Vec<Member> {
        let mut ms: Vec<Member> = self
            .state
            .members
            .lock()
            .await
            .iter()
            .map(|e| e.1 .0.clone())
            .collect();
        ms.push(self.state.local_member.clone());
        ms
    }

    /// Wait for a view size to be established or times out. The resulting selector parameters
    /// indicates whether the desired view size has been established.
    pub async fn wait_for_view_size(&self, size: usize, timeout: Duration) -> bool {
        if self.state.members.lock().await.len() + 1 >= size {
            true
        } else {
            // let s = self.state.clone();
            let f = async {
                loop {
                    let ms = self.state.members.lock().await;
                    if ms.len() + 1 >= size {
                        return true;
                    } else {
                        self.state.cond.wait(ms).await;
                    }
                }
            };
            let r: bool = select! {
                p = f.fuse() => p,
                _ = tokio::time::sleep(timeout).fuse() => false,
            };
            r
        }
    }

    /// Returns the current group size.
    pub async fn size(&self) -> usize {
        let ms = self.state.members.lock().await;
        ms.len() + 1 // with +1 being the local member
    }

    /// Returns the evental leader for this group. Notice that a view change may cause
    /// a change on leader. Thus it is wise to always get the leader after a view change.
    pub async fn leader(&self) -> Member {
        use std::cmp::Ordering;
        let group = self.view().await;
        let mut leader = self.state.local_member.clone();
        for m in group {
            if leader.id().as_str().cmp(m.id().as_str()) == Ordering::Less {
                leader = m
            }
        }
        leader
    }
}