mosaik 0.3.13

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
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
use {
	super::{
		CollectionConfig,
		CollectionFromDef,
		Error,
		READER,
		SyncConfig,
		WRITER,
		When,
		primitives::{Key, StoreId, Version},
	},
	crate::{
		Group,
		GroupId,
		Network,
		PeerId,
		UniqueId,
		collections::sync::{
			Snapshot,
			SnapshotStateMachine,
			SnapshotSync,
			protocol::SnapshotRequest,
		},
		groups::{
			ApplyContext,
			CommandError,
			Cursor,
			LeadershipPreference,
			StateMachine,
		},
		primitives::{EncodeError, Encoded},
	},
	core::{any::type_name, borrow::Borrow, hash::Hash, ops::Range},
	futures::{FutureExt, TryFutureExt},
	serde::{Deserialize, Serialize},
	std::hash::BuildHasherDefault,
	tokio::sync::watch,
};

/// Deterministic hasher for the internal `im::HashSet`, ensuring that
/// iteration order is identical across all nodes for the same set state.
/// Uses `DefaultHasher` (SipHash-1-3) with a fixed zero seed.
type HashSet<T> = im::HashSet<T, BuildHasherDefault<std::hash::DefaultHasher>>;

pub type SetWriter<T> = Set<T, WRITER>;
pub type SetReader<T> = Set<T, READER>;

/// Replicated, unordered, eventually consistent set.
pub struct Set<T: Key, const IS_WRITER: bool = WRITER> {
	when: When,
	group: Group<SetStateMachine<T>>,
	data: watch::Receiver<HashSet<T>>,
}

// read-only access, available to both writers and readers
impl<T: Key, const IS_WRITER: bool> Set<T, IS_WRITER> {
	/// Get the number of elements in the set.
	///
	/// Time: O(1)
	pub fn len(&self) -> usize {
		self.data.borrow().len()
	}

	/// Test whether the set is empty.
	///
	/// Time: O(1)
	pub fn is_empty(&self) -> bool {
		self.data.borrow().is_empty()
	}

	/// Test whether the set contains a given value.
	///
	/// Time: O(log n)
	pub fn contains<Q>(&self, value: &Q) -> bool
	where
		T: Borrow<Q>,
		Q: Hash + Eq + ?Sized,
	{
		self.data.borrow().clone().contains(value)
	}

	/// Test whether this set is a subset of another set.
	///
	/// Time: O(n)
	pub fn is_subset<const W: bool>(&self, other: &Set<T, W>) -> bool {
		self
			.data
			.borrow()
			.clone()
			.is_subset(other.data.borrow().clone())
	}

	/// Get an iterator over the elements of the set.
	pub fn iter(&self) -> impl Iterator<Item = T> {
		let iter_clone = self.data.borrow().clone();
		iter_clone.into_iter()
	}

	/// Returns an observer of the set's state, which can be used to wait for
	/// the set to reach a certain state version before performing an action
	/// or knowing when it is online or offline.
	pub const fn when(&self) -> &When {
		&self.when
	}

	/// The current version of the set's state, which is the version of the
	/// latest committed state.
	pub fn version(&self) -> Version {
		Version(self.group.committed())
	}

	/// The group id of the underlying consensus group for this collection
	/// instance.
	pub fn group_id(&self) -> &GroupId {
		self.group.id()
	}
}

// Mutable operations, only available to writers
impl<T: Key> SetWriter<T> {
	/// Create a new set in writer mode.
	///
	/// The returned writer can be used to modify the set, and it also provides
	/// read access to the set's contents. Writers can be used by multiple
	/// nodes concurrently, and all changes made by any writer will be replicated
	/// to all other writers and readers.
	///
	/// This creates a new set with default synchronization configuration. If you
	/// want to customize the synchronization behavior (e.g. snapshot sync
	/// configuration), use `writer_with_config` instead.
	///
	/// Note that different sync configurations will create different group ids
	/// and the resulting sets will not be able to see each other.
	pub fn writer(network: &Network, store_id: impl Into<StoreId>) -> Self {
		Self::writer_with_config(network, store_id, CollectionConfig::default())
	}

	/// Create a new set in writer mode with the specified configuration.
	pub fn writer_with_config(
		network: &Network,
		store_id: impl Into<StoreId>,
		config: impl Into<CollectionConfig>,
	) -> Self {
		Self::create::<WRITER>(network, store_id, config.into())
	}

	/// Create a new set in writer mode.
	///
	/// This is an alias for the `writer` method.
	pub fn new(network: &Network, store_id: impl Into<StoreId>) -> Self {
		Self::writer(network, store_id)
	}

	/// Create a new set in writer mode with the specified configuration.
	///
	/// This is an alias for the `writer_with_config` method.
	pub fn new_with_config(
		network: &Network,
		store_id: impl Into<StoreId>,
		config: impl Into<CollectionConfig>,
	) -> Self {
		Self::writer_with_config(network, store_id, config)
	}

	/// Discard all elements from the set.
	///
	/// This leaves you with an empty set, and all elements that
	/// were previously inside it are dropped.
	pub fn clear(
		&self,
	) -> impl Future<Output = Result<Version, Error<()>>> + Send + Sync + 'static
	{
		self.execute(
			SetCommand::Clear,
			|_| Error::Offline(()),
			|_, _| unreachable!(),
		)
	}

	/// Insert a value into the set.
	///
	/// If the set already contained this value, the operation is a no-op.
	///
	/// Time: O(log n)
	pub fn insert(
		&self,
		value: T,
	) -> impl Future<Output = Result<Version, Error<T>>> + Send + Sync + 'static
	{
		let value = Encoded(value);
		self.execute(
			SetCommand::Insert { value },
			|cmd| match cmd {
				SetCommand::Insert { value } => Error::Offline(value.0),
				_ => unreachable!(),
			},
			|cmd, e| match cmd {
				SetCommand::Insert { value } => Error::Encoding(value.0, e),
				_ => unreachable!(),
			},
		)
	}

	/// Insert multiple values into the set.
	pub fn extend(
		&self,
		values: impl IntoIterator<Item = T>,
	) -> impl Future<Output = Result<Version, Error<Vec<T>>>> + Send + Sync + 'static
	{
		let entries: Vec<Encoded<T>> = values.into_iter().map(Encoded).collect();

		let is_empty = entries.is_empty();
		let current_version = self.group.committed();
		let fut = self.execute(
			SetCommand::Extend { entries },
			|cmd| match cmd {
				SetCommand::Extend { entries } => {
					Error::Offline(entries.into_iter().map(|e| e.0).collect())
				}
				_ => unreachable!(),
			},
			|cmd, e| match cmd {
				SetCommand::Extend { entries } => {
					Error::Encoding(entries.into_iter().map(|e| e.0).collect(), e)
				}
				_ => unreachable!(),
			},
		);

		async move {
			if is_empty {
				Ok(Version(current_version))
			} else {
				fut.await
			}
		}
	}

	/// Remove a value from the set.
	///
	/// Time: O(log n)
	pub fn remove<Q: Borrow<T>>(
		&self,
		value: &Q,
	) -> impl Future<Output = Result<Version, Error<T>>> + Send + Sync + 'static
	{
		let value = Encoded(value.borrow().clone());
		self.execute(
			SetCommand::RemoveMany {
				values: vec![value],
			},
			|cmd| match cmd {
				SetCommand::RemoveMany { mut values } => {
					Error::Offline(values.remove(0).0)
				}
				_ => unreachable!(),
			},
			|cmd, e| match cmd {
				SetCommand::RemoveMany { mut values } => {
					Error::Encoding(values.remove(0).0, e)
				}
				_ => unreachable!(),
			},
		)
	}

	/// Remove multiple values from the set.
	pub fn remove_many(
		&self,
		values: impl IntoIterator<Item = T>,
	) -> impl Future<Output = Result<Version, Error<Vec<T>>>> + Send + Sync + 'static
	{
		let values: Vec<Encoded<T>> = values.into_iter().map(Encoded).collect();

		let is_empty = values.is_empty();
		let current_version = self.group.committed();

		let fut = self.execute(
			SetCommand::RemoveMany { values },
			|cmd| match cmd {
				SetCommand::RemoveMany { values } => {
					Error::Offline(values.into_iter().map(|v| v.0).collect())
				}
				_ => unreachable!(),
			},
			|cmd, e| match cmd {
				SetCommand::RemoveMany { values } => {
					Error::Encoding(values.into_iter().map(|v| v.0).collect(), e)
				}
				_ => unreachable!(),
			},
		);

		async move {
			if is_empty {
				Ok(Version(current_version))
			} else {
				fut.await
			}
		}
	}
}

// construction
impl<T: Key, const IS_WRITER: bool> Set<T, IS_WRITER> {
	/// Create a new set in reader mode.
	///
	/// The returned reader provides read-only access to the set's contents.
	/// Readers can be used by multiple nodes concurrently, and they will see all
	/// changes made by any writer. However, readers cannot modify the set, and
	/// they will not be able to make any changes themselves. Readers have longer
	/// election timeouts to reduce the likelihood of them being elected as
	/// group leaders, which reduces latency for read operations.
	pub fn reader(
		network: &Network,
		store_id: impl Into<StoreId>,
	) -> SetReader<T> {
		Self::reader_with_config(network, store_id, CollectionConfig::default())
	}

	/// Create a new set in reader mode with the specified configuration.
	pub fn reader_with_config(
		network: &Network,
		store_id: impl Into<StoreId>,
		config: impl Into<CollectionConfig>,
	) -> SetReader<T> {
		Self::create::<READER>(network, store_id, config.into())
	}

	fn create<const W: bool>(
		network: &Network,
		store_id: impl Into<StoreId>,
		config: CollectionConfig,
	) -> Set<T, W> {
		let store_id = store_id.into();
		let machine = SetStateMachine::new(
			store_id, //
			W,
			config.sync,
			network.local().id(),
		);

		let data = machine.data();
		let mut builder = network
			.groups()
			.with_key(store_id)
			.with_state_machine(machine);

		for validator in config.auth {
			builder = builder.require_ticket(validator);
		}

		let group = builder.join();

		Set::<T, W> {
			when: When::new(group.when().clone()),
			group,
			data,
		}
	}
}

impl<T: Key, const WRITER: bool> CollectionFromDef for Set<T, WRITER> {
	type Reader = SetReader<T>;
	type Writer = SetWriter<T>;

	fn reader_with_config(
		network: &Network,
		store_id: StoreId,
		config: CollectionConfig,
	) -> Self::Reader {
		Self::Reader::reader_with_config(network, store_id, config)
	}

	fn writer_with_config(
		network: &Network,
		store_id: StoreId,
		config: CollectionConfig,
	) -> Self::Writer {
		Self::Writer::writer_with_config(network, store_id, config)
	}
}

// internal
impl<T: Key> SetWriter<T> {
	fn execute<TErr>(
		&self,
		command: SetCommand<T>,
		offline_err: impl FnOnce(SetCommand<T>) -> Error<TErr> + Send + Sync + 'static,
		encoding_err: impl FnOnce(SetCommand<T>, EncodeError) -> Error<TErr>
		+ Send
		+ Sync
		+ 'static,
	) -> impl Future<Output = Result<Version, Error<TErr>>> + Send + Sync + 'static
	{
		self
			.group
			.execute(command)
			.map_err(|e| match e {
				CommandError::Offline(mut items) => {
					let command = items.remove(0);
					offline_err(command)
				}
				CommandError::Encoding(mut items, err) => {
					let command = items.remove(0);
					encoding_err(command, err)
				}
				CommandError::GroupTerminated => Error::NetworkDown,
				CommandError::NoCommands => unreachable!(),
			})
			.map(|position| position.map(Version))
	}
}

struct SetStateMachine<T: Key> {
	data: HashSet<T>,
	latest: watch::Sender<HashSet<T>>,
	store_id: StoreId,
	local_id: PeerId,
	state_sync: SnapshotSync<Self>,
	is_writer: bool,
}

impl<T: Key> SetStateMachine<T> {
	pub fn new(
		store_id: StoreId,
		is_writer: bool,
		sync_config: SyncConfig,
		local_id: PeerId,
	) -> Self {
		let data = HashSet::default();
		let state_sync = SnapshotSync::new(sync_config, |request| {
			SetCommand::TakeSnapshot(request)
		});

		let latest = watch::Sender::new(data.clone());

		Self {
			data,
			latest,
			store_id,
			local_id,
			state_sync,
			is_writer,
		}
	}

	pub fn data(&self) -> watch::Receiver<HashSet<T>> {
		self.latest.subscribe()
	}
}

impl<T: Key> StateMachine for SetStateMachine<T> {
	type Command = SetCommand<T>;
	type Query = ();
	type QueryResult = ();
	type StateSync = SnapshotSync<Self>;

	fn apply(&mut self, command: Self::Command, ctx: &dyn ApplyContext) {
		self.apply_batch([command], ctx);
	}

	fn apply_batch(
		&mut self,
		commands: impl IntoIterator<Item = Self::Command>,
		ctx: &dyn ApplyContext,
	) {
		let mut commands_len = 0usize;
		let mut sync_requests = vec![];

		for command in commands {
			match command {
				SetCommand::Clear => {
					self.data.clear();
				}
				SetCommand::Insert { value } => {
					self.data.insert(value.0);
				}
				SetCommand::RemoveMany { values } => {
					for value in values {
						self.data.remove(&value.0);
					}
				}
				SetCommand::Extend { entries } => {
					for value in entries {
						self.data.insert(value.0);
					}
				}
				SetCommand::TakeSnapshot(request) => {
					if request.requested_by != self.local_id
						&& !self.state_sync.is_expired(&request)
					{
						// take note of the snapshot request, we will take the actual
						// snapshot after applying all commands from this batch.
						sync_requests.push(request);
					}
				}
			}
			commands_len += 1;
		}

		self.latest.send_replace(self.data.clone());

		if !sync_requests.is_empty() {
			let snapshot = self.create_snapshot();
			let position = Cursor::new(
				ctx.current_term(),
				ctx.committed().index() + commands_len as u64,
			);

			for request in sync_requests {
				self
					.state_sync
					.serve_snapshot(request, position, snapshot.clone());
			}
		}
	}

	/// The group-key for a set is derived from the store ID and the type of
	/// the set's elements. This ensures that different sets (with different
	/// store IDs or element types) will be in different groups.
	fn signature(&self) -> crate::UniqueId {
		UniqueId::from("mosaik_collections_set")
			.derive(self.store_id)
			.derive(type_name::<T>())
	}

	/// This state machine doesn't support external queries, because all of its
	/// state is observable through the `latest` watch channel. Therefore, the
	/// query method is a no-op.
	fn query(&self, (): Self::Query) {}

	/// This state machine uses the `SnapshotSync` state sync strategy.
	fn state_sync(&self) -> Self::StateSync {
		self.state_sync.clone()
	}

	/// Readers are observers and never assume group leadership.
	fn leadership_preference(&self) -> LeadershipPreference {
		if self.is_writer {
			LeadershipPreference::Normal
		} else {
			LeadershipPreference::Observer
		}
	}
}

impl<T: Key> SnapshotStateMachine for SetStateMachine<T> {
	type Snapshot = SetSnapshot<T>;

	fn create_snapshot(&self) -> Self::Snapshot {
		SetSnapshot {
			data: self.data.clone(),
		}
	}

	fn install_snapshot(&mut self, snapshot: Self::Snapshot) {
		self.data = snapshot.data;
		self.latest.send_replace(self.data.clone());
	}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "T: Key")]
enum SetCommand<T> {
	Clear,
	Insert { value: Encoded<T> },
	RemoveMany { values: Vec<Encoded<T>> },
	Extend { entries: Vec<Encoded<T>> },
	TakeSnapshot(SnapshotRequest),
}

#[derive(Debug, Clone)]
pub struct SetSnapshot<T: Key> {
	data: HashSet<T>,
}

impl<T: Key> Default for SetSnapshot<T> {
	fn default() -> Self {
		Self {
			data: HashSet::default(),
		}
	}
}

impl<T: Key> Snapshot for SetSnapshot<T> {
	type Item = Encoded<T>;

	fn len(&self) -> u64 {
		self.data.len() as u64
	}

	fn iter_range(
		&self,
		range: Range<u64>,
	) -> Option<impl Iterator<Item = Self::Item>> {
		if range.end > self.data.len() as u64 {
			return None;
		}

		Some(
			self
				.data
				.clone()
				.into_iter()
				.skip(range.start as usize)
				.take((range.end - range.start) as usize)
				.map(Encoded),
		)
	}

	fn append(&mut self, items: impl IntoIterator<Item = Self::Item>) {
		self.data.extend(items.into_iter().map(|e| e.0));
	}
}