mosaik 0.3.17

A Rust runtime for building self-organizing, leaderless distributed systems.
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
use {
	crate::{
		Datum,
		PeerId,
		discovery::{Catalog, Discovery, rtt::PeerInfo},
		network::LocalNode,
		primitives::{Digest, ShortFmtExt},
		streams::{
			Consumer,
			Streams,
			consumer::{builder::ConsumerConfig, receiver::Receiver},
			status::{ActiveChannelsMap, ChannelConditions, State, Stats, When},
		},
	},
	core::pin::Pin,
	futures::{Stream, StreamExt, stream::SelectAll},
	std::{collections::HashMap, sync::Arc},
	tokio::{
		sync::{mpsc, watch},
		task::JoinSet,
	},
	tokio_stream::wrappers::WatchStream,
	tokio_util::sync::CancellationToken,
};

/// Worker task that manages the state of one consumer for a specific datum type
/// `D` on the local node.
///
/// Notes:
/// - Individual worker tasks are spawned for each subscribed producer peer.
pub(super) struct ConsumerWorker<D: Datum> {
	/// The consumer-specific configuration as assembled by
	/// `Network::streams().consumer()`. If some configuration values are not
	/// set, they default to the values from `Streams` config.
	config: Arc<ConsumerConfig>,

	/// A handle to the local node that is used to establish new connections.
	local: LocalNode,

	/// The discovery system handle used to monitor known peers that are
	/// producing the desired stream and also to trigger catalog syncs when
	/// the consumer is connecting to a producer that does not recognize it.
	discovery: Discovery,

	/// Channel for sending received data to the consumer handle.
	data_tx: mpsc::UnboundedSender<(D, usize)>,

	/// Triggered when the consumer handle is dropped.
	cancel: CancellationToken,

	/// Active receive workers for connected producer peers.
	/// This value can be observed to get the current snapshot of connected
	/// producers.
	active: watch::Sender<ActiveChannelsMap>,

	/// Aggregated status streams from all active receiver workers.
	status_rx: StateUpdatesStream,

	/// Cancellation tokens for individual receiver workers, keyed by the same
	/// `sub_id` used in the `active` map. Cancelling a token terminates its
	/// corresponding receiver.
	receiver_cancels: HashMap<Digest, CancellationToken>,

	/// Sets the online status of the consumer when the configured online
	/// conditions are met.
	online: watch::Sender<bool>,

	/// A future that resolves when the consumer meets the configured online
	/// conditions.
	online_when: ChannelConditions,

	/// Futures that resolve when a producer's ticket expires.
	ticket_expiries: JoinSet<Digest>,

	/// Pending RTT probes for newly discovered producers that have no
	/// RTT data yet. When a probe completes, the result is fed back to
	/// the discovery system, which triggers a catalog update that
	/// re-evaluates `require()` with RTT data available.
	rtt_probes: JoinSet<()>,

	/// Pre-computed metrics labels for this consumer.
	metrics_labels: [(&'static str, String); 2],
}

impl<D: Datum> ConsumerWorker<D> {
	/// Spawns a new receive worker task and returns the consumer handle that can
	/// receive data and query status.
	///
	/// The worker handle will terminate when the returned consumer is dropped.
	pub fn spawn(config: ConsumerConfig, streams: &Streams) -> Consumer<D> {
		let config = Arc::new(config);
		let local = streams.local.clone();
		let cancel = local.termination().child_token();
		let active = watch::Sender::new(ActiveChannelsMap::new());
		let online = watch::Sender::new(false);
		let (data_tx, data_rx) = mpsc::unbounded_channel();

		let when = When::new(active.subscribe(), online.subscribe());
		let online_when = (config.online_when)(when.subscribed());

		online.send_replace(online_when.is_condition_met());

		let metrics_labels = [
			("stream", config.stream_id.short().to_string()),
			("network", local.network_id().short().to_string()),
		];

		let worker = Self {
			local,
			data_tx,
			config: Arc::clone(&config),
			discovery: streams.discovery.clone(),
			cancel: cancel.clone(),
			active: active.clone(),
			status_rx: StateUpdatesStream::new(),
			receiver_cancels: HashMap::new(),
			online: online.clone(),
			online_when,
			ticket_expiries: JoinSet::new(),
			rtt_probes: JoinSet::new(),
			metrics_labels,
		};

		let consumer_labels = worker.metrics_labels.clone();
		tokio::spawn(worker.run());

		let network_label = [
			("network", streams.local.network_id().short().to_string()),
			("stream", config.stream_id.short().to_string()),
		];
		Consumer {
			config: Arc::clone(&config),
			chan: data_rx,
			stats: Stats::new_connected(network_label),
			status: When::new(active.subscribe(), online.subscribe()),
			metrics_labels: consumer_labels,
			_abort: cancel.drop_guard(),
		}
	}
}

impl<D: Datum> ConsumerWorker<D> {
	async fn run(mut self) {
		// Get a watch handle for the discovery catalog
		// and mark it as changed to trigger an initial producers lookup in the
		// current state of the catalog.
		let mut catalog = self.discovery.catalog_watch();
		catalog.mark_changed();

		loop {
			tokio::select! {
				// Triggered when the consumer is dropped or the network is shutting down
				() = self.cancel.cancelled() => {
					self.on_terminated();
					break;
				}

				// Triggered when the online conditions for this consumer
				// are met.
				() = &mut self.online_when => {
					self.on_online();
				}

				// Triggered when new peers are discovered or existing peers are updated
				_ = catalog.changed() => {
					// mark the latest catalog snapshot as seen and trigger peers scan
					let snapshot = catalog.borrow_and_update().clone();
					self.on_catalog_update(snapshot);
				}

				// Triggered when one of the active receiver workers updates its state
				Some((state, peer_id)) = self.status_rx.next() => {
					self.on_receiver_state_update(peer_id, state);
				}

				// Triggered when a producer's ticket expires.
				Some(Ok(sub_id)) = self.ticket_expiries.join_next() => {
					self.on_ticket_expired(sub_id);
				}

				// Drive RTT probe tasks (results are fed back to
				// discovery, triggering a catalog update that
				// re-evaluates require() with RTT data).
				Some(_) = self.rtt_probes.join_next() => {}
			}
		}
	}

	/// Handles updates to the discovery catalog.
	#[expect(
		clippy::needless_pass_by_value,
		reason = "Catalog is cheaply cloneable and we don't want to hold a lock \
		          on the watcher while processing"
	)]
	fn on_catalog_update(&mut self, latest: Catalog) {
		// identify all producers that are producing the desired stream id
		// and satisfy the user-provided additional eligibility criteria.
		let producers = latest
			.peers()
			.filter(|peer| peer.streams().contains(&self.config.stream_id));

		for producer in producers {
			// for each discovered producer, create a receive worker if it is not
			// already in the active list of connected producers.
			let sub_id = Digest::from_bytes(*producer.id().as_bytes());
			if !self.active.borrow().contains_key(&sub_id) {
				tracing::trace!(
					stream_id = %self.config.stream_id.short(),
					producer = %producer.id().short(),
					network = %producer.network_id().short(),
					"discovered new stream producer"
				);

				// If no RTT data exists for this producer, trigger an RTT ping probe
				// before evaluating require(). The probe feeds its result back to the
				// discovery system, which triggers a catalog update that re-evaluates
				// with RTT data available. This avoids wasteful connections to
				// peers that will fail RTT requirements.
				if self.discovery.rtt_tracker().get(producer.id()).is_none() {
					let local = self.local.clone();
					let discovery = self.discovery.clone();
					let addr = producer.address().clone();
					self.rtt_probes.spawn(async move {
						if let Ok((entry, rtt)) = local.ping(addr, None).await
							&& let Some(rtt) = rtt
						{
							discovery.rtt_tracker().record_sample(*entry.id(), rtt);
							discovery.feed(entry);
						}
					});
					continue;
				}

				let info =
					PeerInfo::from_tracker(producer, self.discovery.rtt_tracker());
				if !(self.config.require)(&info) {
					tracing::debug!(
						stream_id = %self.config.stream_id.short(),
						producer_id = %producer.id().short(),
						network = %producer.network_id().short(),
						"skipping ineligible producer"
					);
					continue;
				}

				// Validate the producer's ticket against all validators
				let Ok(ticket_expiration) =
					producer.validate_tickets(&self.config.ticket_validators)
				else {
					tracing::debug!(
						stream_id = %self.config.stream_id.short(),
						producer_id = %producer.id().short(),
						network = %producer.network_id().short(),
						"skipping unauthorized producer"
					);
					continue;
				};

				// create a per-receiver cancellation token so the consumer worker
				// can terminate this specific receiver independently
				let receiver_cancel = self.cancel.child_token();

				// spawn a new receiver worker for this producer and track its status
				let channel_info = Receiver::spawn(
					producer.clone(),
					&self.local,
					&self.discovery,
					&receiver_cancel,
					&self.data_tx,
					Arc::clone(&self.config),
				);

				// subscribe to the receiver's status updates
				let peer_id = *producer.id();
				self.status_rx.push(
					WatchStream::new(channel_info.state.clone())
						.map(move |state| (state, peer_id))
						.boxed(),
				);

				// track the active receiver handle, when workers terminate they will
				// transition into the terminated state and be removed from the active
				// list
				self.active.send_modify(|active| {
					active.insert(sub_id, channel_info);
				});
				let labels = self.metrics_labels.as_slice();
				metrics::gauge!("mosaik.streams.consumers.active", labels)
					.increment(1.0);
				metrics::gauge!("mosaik.streams.consumer.producers", labels)
					.set(self.active.borrow().len() as f64);
				self.receiver_cancels.insert(sub_id, receiver_cancel);

				// Schedule ticket expiry timer if the ticket has an expiration
				if let Some(duration) = ticket_expiration.and_then(|e| e.remaining()) {
					self.ticket_expiries.spawn(async move {
						tokio::time::sleep(duration).await;
						sub_id
					});
				}
			}
		}

		self.disconnect_ineligible_producers(&latest);
	}

	/// Re-evaluates the `require` predicate and ticket validity for all
	/// active connections. Disconnects receivers whose producers no
	/// longer satisfy the eligibility criteria or left the catalog.
	fn disconnect_ineligible_producers(&mut self, latest: &Catalog) {
		let to_disconnect: Vec<(Digest, PeerId)> = self
			.active
			.borrow()
			.iter()
			.filter_map(|(sub_id, info)| {
				let peer_id = *info.producer_id();
				let dominated = latest.get(&peer_id).is_none_or(|entry| {
					let info =
						PeerInfo::from_tracker(entry, self.discovery.rtt_tracker());
					!entry.streams().contains(&self.config.stream_id)
						|| !(self.config.require)(&info)
						|| entry
							.validate_tickets(&self.config.ticket_validators)
							.is_err()
				});
				dominated.then_some((*sub_id, peer_id))
			})
			.collect();

		for (sub_id, peer_id) in &to_disconnect {
			tracing::info!(
				producer_id = %peer_id.short(),
				stream_id = %self.config.stream_id.short(),
				"disconnecting ineligible producer"
			);

			if let Some(cancel) = self.receiver_cancels.remove(sub_id) {
				cancel.cancel();
			}
			self
				.active
				.send_if_modified(|active| active.remove(sub_id).is_some());
		}

		if !to_disconnect.is_empty() && !self.online_when.is_condition_met() {
			tracing::trace!(
				stream_id = %self.config.stream_id.short(),
				producers = %self.active.borrow().len(),
				"consumer is offline",
			);
			self.online.send_replace(false);
		}
	}

	/// Triggered when a producer's ticket expires. Disconnects the receiver
	/// so that a fresh ticket is validated on the next catalog update.
	fn on_ticket_expired(&mut self, sub_id: Digest) {
		if !self.active.borrow().contains_key(&sub_id) {
			return;
		}

		tracing::debug!(
			stream_id = %self.config.stream_id.short(),
			"producer ticket expired; disconnecting",
		);

		if let Some(cancel) = self.receiver_cancels.remove(&sub_id) {
			cancel.cancel();
		}
		self
			.active
			.send_if_modified(|active| active.remove(&sub_id).is_some());

		if !self.online_when.is_condition_met() {
			self.online.send_replace(false);
		}
	}

	/// Handles state updates from remote receiver workers.
	fn on_receiver_state_update(&mut self, peer_id: PeerId, state: State) {
		if state == State::Terminated {
			// The receiver has unrecoverably terminated, remove it from the active
			// list and clean up its cancellation token
			let sub_id = Digest::from_bytes(*peer_id.as_bytes());

			self
				.active
				.send_if_modified(|active| active.remove(&sub_id).is_some());
			self.receiver_cancels.remove(&sub_id);
			let labels = self.metrics_labels.as_slice();
			metrics::gauge!("mosaik.streams.consumers.active", labels).decrement(1.0);
			metrics::gauge!("mosaik.streams.consumer.producers", labels)
				.set(self.active.borrow().len() as f64);

			tracing::info!(
				producer_id = %peer_id.short(),
				stream_id = %self.config.stream_id.short(),
				criteria = ?self.config.criteria,
				"connection with producer terminated"
			);

			if !self.online_when.is_condition_met() {
				tracing::trace!(
					stream_id = %self.config.stream_id.short(),
					producers = %self.active.borrow().len(),
					"consumer is offline",
				);
				self.online.send_replace(false);
			}
		}
	}

	/// Triggered when the online conditions for this consumer are met.
	fn on_online(&self) {
		tracing::trace!(
			stream_id = %self.config.stream_id.short(),
			producers = %self.active.borrow().len(),
			"consumer is online",
		);

		self.online.send_if_modified(|status| {
			if *status {
				false
			} else {
				*status = true;
				true
			}
		});
	}

	/// Gracefully closes all connections with remote producers.
	fn on_terminated(&mut self) {
		// terminate all active receiver workers
		let producers_count = self.active.borrow().len();
		self.active.send_replace(ActiveChannelsMap::default());
		self.receiver_cancels.clear();

		tracing::debug!(
			stream_id = %self.config.stream_id.short(),
			producers_count = producers_count,
			criteria = ?self.config.criteria,
			"consumer terminated"
		);
	}
}

type StateUpdatesStream =
	SelectAll<Pin<Box<dyn Stream<Item = (State, PeerId)> + Send>>>;