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
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
use {
	crate::{
		NetworkId,
		PeerId,
		groups::{
			ApplyContext,
			Bonds,
			ConsensusConfig,
			Cursor,
			GroupId,
			Index,
			StateMachine,
			StateSync,
			StateSyncProvider,
			Storage,
			SyncContext,
			SyncProviderContext,
			SyncSessionContext,
			Term,
			config::GroupConfig,
			raft::Message,
			state::{WorkerCommand, WorkerRaftCommand, WorkerState},
		},
		primitives::{EncodeError, ShortFmtExt},
	},
	core::{mem::MaybeUninit, task::Poll},
	std::sync::Arc,
	tokio::sync::{mpsc::error::SendError, oneshot},
};

/// State that is shared across all raft roles.
pub struct Shared<S, M>
where
	S: Storage<M::Command>,
	M: StateMachine,
{
	/// The group state that is shared between the long-running group worker and
	/// the external world.
	pub group: Arc<WorkerState<M>>,

	/// The underlying storage for the log entries of this group state machine.
	pub storage: S,

	/// The instance of the state machine of this group that applies committed
	/// log entries and responds to queries.
	pub state_machine: M,

	/// The last vote casted by the local node in leader elections.
	pub last_vote: Option<(Term, PeerId)>,

	/// Wakers for tasks that are waiting for changes in the shared state, such
	/// as leadership changes or log commitment.
	pub wakers: Vec<std::task::Waker>,

	/// State machine-specific state sync provider.
	///
	/// This is used to create new state sync sessions for followers and one
	/// state sync provider per group instance for all roles.
	pub sync: M::StateSync,

	/// State machine-specific state sync provider.
	///
	/// Applicable to all roles.
	pub sync_provider: MaybeUninit<<M::StateSync as StateSync>::Provider>,

	/// The index and term of the latest replicated and committed log entry that
	/// has been applied to the state machine.
	committed: Cursor,
}

impl<S, M> Shared<S, M>
where
	S: Storage<M::Command>,
	M: StateMachine,
{
	pub(super) fn new(
		group: Arc<WorkerState<M>>,
		storage: S,
		state_machine: M,
	) -> Self {
		let mut instance = Self {
			group,
			last_vote: None,
			storage,
			sync: state_machine.state_sync(),
			wakers: Vec::new(),
			sync_provider: MaybeUninit::uninit(),
			state_machine,
			committed: Cursor::default(),
		};

		let provider = instance.sync.create_provider(&instance);
		instance.sync_provider.write(provider);

		instance
	}

	/// Returns the group configuration for this consensus group.
	pub fn config(&self) -> &GroupConfig {
		&self.group.config
	}

	/// Returns the timing intervals configuration for this consensus group.
	pub fn consensus(&self) -> &ConsensusConfig {
		self.config().consensus()
	}

	/// Returns the list of active bonds for this consensus group.
	pub fn bonds(&self) -> &Bonds<M> {
		&self.group.bonds
	}

	/// Returns the local ID of this node in the consensus group.
	pub fn local_id(&self) -> PeerId {
		self.group.local_id()
	}

	/// Returns the group ID of this consensus group.
	pub fn group_id(&self) -> &GroupId {
		self.group.group_id()
	}

	/// Returns the network ID of this consensus group.
	pub fn network_id(&self) -> &NetworkId {
		self.group.network_id()
	}

	/// Returns the index of the last committed log entry that has been replicated
	/// to a quorum and applied to the state machine.
	pub const fn committed(&self) -> Cursor {
		self.committed
	}

	/// Optionally handles an incoming state sync message at the handler level
	pub fn sync_provider_receive(
		&mut self,
		message: <M::StateSync as StateSync>::Message,
		sender: PeerId,
	) -> Result<(), <M::StateSync as StateSync>::Message> {
		// SAFETY: The `sync_provider` field is initialized in the constructor of
		// `Shared` and is never mutated after that, so it is safe to assume that
		// it is always initialized when accessed through this method.
		let provider: *mut _ = unsafe { self.sync_provider.assume_init_mut() };

		// SAFETY: There is no public API on `StateSyncContext` that allows the
		// mutation of the `sync_provider` field of `Shared`.
		let provider = unsafe { &mut *provider };

		// Forward the message to the state sync provider. If the provider consumes
		// the message, then it will not be forwarded to the state sync session of
		// the follower.
		provider.receive(message, sender, self)
	}

	/// Creates a new state synchronization session for a lagging follower that is
	/// about to enter the state catch-up process.
	pub fn create_sync_session(
		&mut self,
		position: Cursor,
		leader_commit: Index,
		entries: Vec<(M::Command, Term)>,
	) -> <M::StateSync as StateSync>::Session {
		let ptr: *mut M::StateSync = &raw mut self.sync;

		// SAFETY: There is no public API on `StateSyncContext` that allows the
		// mutation of the `sync` field of `Shared`.
		let sync = unsafe { &mut *ptr };
		sync.create_session(self, position, leader_commit, entries)
	}

	/// Queries the state sync provider for the log index up to which it is safe
	/// to prune log entries, and prunes them if the provider indicates it is
	/// safe to do so.
	///
	/// This should be called after the committed index advances, as pruning is
	/// only meaningful for committed entries that the provider has determined
	/// are no longer needed for state synchronization.
	pub fn prune_safe_prefix(&mut self) {
		// SAFETY: The `sync_provider` field is initialized in the constructor of
		// `Shared` and is never mutated after that, so it is safe to assume that
		// it is always initialized when accessed through this method.
		let provider: *mut _ = unsafe { self.sync_provider.assume_init_mut() };

		// SAFETY: There is no public API on `StateSyncContext` that allows the
		// mutation of the `sync_provider` field of `Shared`.
		let provider = unsafe { &mut *provider };

		if !self.committed().index().is_zero()
			&& let Some(up_to) = provider.safe_to_prune_prefix(self)
		{
			self
				.storage
				.prune_prefix(up_to.min(self.committed().index().prev()));
		}
	}

	/// Updates the leader information in the group state.
	pub fn update_leader(&self, leader: Option<PeerId>) {
		if leader.is_some() {
			let labels = [
				("network", self.network_id().short().to_string()),
				("group", self.group_id().short().to_string()),
			];
			metrics::counter!("mosaik.groups.raft.leader.changes", &labels)
				.increment(1);
		}
		self.group.when.update_leader(leader);
	}

	/// Updates the online status of the local node in the group state to
	/// online. See [`When::is_online`] for more details on what it means for a
	/// node to be online.
	pub fn set_online(&self) {
		self.group.when.set_online_status(true);
	}

	/// Updates the online status of the local node in the group state to
	/// offline. See [`When::is_offline`] for more details on what it means for a
	/// node to be offline.
	pub fn set_offline(&self) {
		self.group.when.set_online_status(false);
	}

	/// Updates the committed index in the group public api observers. This should
	/// be called whenever the committed index of the log advances.
	pub fn update_committed(&mut self, committed: Cursor) {
		let labels = [
			("network", self.network_id().short().to_string()),
			("group", self.group_id().short().to_string()),
		];
		metrics::gauge!("mosaik.groups.raft.committed", &labels)
			.set(u64::from(committed.index()) as f64);
		self.group.when.update_committed(committed.index());

		// SAFETY: The `sync_provider` field is initialized in the constructor of
		// `Shared` and is never mutated after that, so it is safe to assume that
		// it is always initialized when accessed through this method.
		let provider: *mut _ = unsafe { self.sync_provider.assume_init_mut() };

		// SAFETY: There is no public API on `StateSyncContext` that allows the
		// mutation of the `sync_provider` field of `Shared`.
		let provider = unsafe { &mut *provider };

		// Notify the state sync provider that the committed index has advanced.
		provider.committed(committed, self);
	}

	/// Updates the log position in the group public api observers. This should be
	/// called whenever the local node's log position changes, which can happen
	/// when new log entries are appended to the log or when the log is truncated.
	pub fn update_log_pos(&self, log_pos: Cursor) {
		self.group.when.update_log_pos(log_pos);
	}

	/// Called when we receive a `RequestVote` message from a candidate. This
	/// checks if we have already voted for another candidate in the same term.
	pub fn can_vote(&self, term: Term, candidate: PeerId) -> bool {
		let Some((last_term, last_candidate)) = self.last_vote else {
			// If we haven't casted any vote yet, we can vote for the candidate.
			return true;
		};

		if last_term < term {
			// If the incoming vote request is for a higher term than the last
			// vote we casted, we can vote for the new candidate.
			return true;
		}

		if last_term == term && last_candidate == candidate {
			// If the incoming vote request is for the same term and candidate as
			// the last vote we casted, we can vote for the candidate again. This does
			// not constitute equivocation.
			return true;
		}

		// In all other cases, we should not vote for the candidate.
		false
	}

	/// Commits log entries up to the given index and applies them to the state
	/// machine. This should only be called with an index that has been replicated
	/// to a majority of followers, which is guaranteed by the Raft leader before
	/// calling this method.
	///
	/// Returns the index of the latest committed entry after this operation,
	/// which may be less than the given index if it goes beyond the end of the
	/// log.
	pub fn commit_up_to(&mut self, index: Index) -> Cursor {
		let committed = self.committed();
		let range = committed.index().next()..=index;
		let commands = self.storage.get_range(&range);
		let log_position = self.storage.last();

		// Partition commands into consecutive batches by term and apply each
		// batch with the correct `current_term` in the apply context.
		let mut iter = commands.into_iter().peekable();

		while iter.peek().is_some() {
			let batch_term = iter.peek().unwrap().0;
			let batch: Vec<_> = core::iter::from_fn(|| {
				if iter.peek().map(|(t, _, _)| *t) == Some(batch_term) {
					iter.next()
				} else {
					None
				}
			})
			.collect();

			let batch_size = batch.len() as u64;
			let ctx = LocalApplyContext {
				prev_committed: self.committed,
				log_position,
				term: batch_term,
				group_id: *self.group_id(),
				network_id: *self.network_id(),
			};

			self
				.state_machine
				.apply_batch(batch.into_iter().map(|(_, _, cmd)| cmd), &ctx);

			self.committed =
				Cursor::new(batch_term, self.committed.index() + batch_size);
		}

		self.committed
	}

	/// Polls the state sync provider for any pending work it needs to do on every
	/// tick of the raft worker loop.
	pub fn poll_state_sync_provider(
		&mut self,
		cx: &mut std::task::Context<'_>,
	) -> Poll<()> {
		// SAFETY: The `sync_provider` field is initialized in the constructor of
		// `Shared` and is never mutated after that, so it is safe to assume that
		// it is always initialized when accessed through this method.
		let provider: *mut _ = unsafe { self.sync_provider.assume_init_mut() };

		// SAFETY: There is no public API on `StateSyncContext` that allows the
		// mutation of the `sync_provider` field of `Shared`.
		let provider = unsafe { &mut *provider };
		provider.poll(cx, self)
	}

	/// Records the fact that we casted a vote for the given candidate in this
	/// term. This is used to prevent us from voting for multiple candidates in
	/// the same term.
	pub const fn save_vote(&mut self, term: Term, candidate: PeerId) {
		self.last_vote = Some((term, candidate));
	}

	pub fn add_waker(&mut self, waker: std::task::Waker) {
		self.wakers.push(waker);
	}

	pub fn wake_all(&mut self) {
		for waker in self.wakers.drain(..) {
			waker.wake();
		}
	}
}

impl<S, M> crate::primitives::sealed::Sealed for Shared<S, M>
where
	S: Storage<M::Command>,
	M: StateMachine,
{
}

impl<S, M> SyncContext<M::StateSync> for Shared<S, M>
where
	S: Storage<M::Command>,
	M: StateMachine,
{
	fn state_machine(&self) -> &<M::StateSync as StateSync>::Machine {
		&self.state_machine
	}

	fn state_machine_mut(&mut self) -> &mut <M::StateSync as StateSync>::Machine {
		&mut self.state_machine
	}

	fn log(&self) -> &dyn Storage<M::Command> {
		&self.storage
	}

	fn log_mut(&mut self) -> &mut dyn Storage<M::Command> {
		&mut self.storage
	}

	fn committed(&self) -> Cursor {
		self.committed
	}

	fn local_id(&self) -> PeerId {
		self.group.local_id()
	}

	fn group_id(&self) -> GroupId {
		*self.group.group_id()
	}

	fn network_id(&self) -> NetworkId {
		*self.group.network_id()
	}

	fn send_to(
		&mut self,
		peer: PeerId,
		message: <M::StateSync as StateSync>::Message,
	) -> Result<(), EncodeError> {
		self
			.group
			.bonds
			.send_raft_to(Message::StateSync(message), peer)
	}

	fn broadcast(
		&mut self,
		message: <M::StateSync as StateSync>::Message,
	) -> Result<Vec<PeerId>, EncodeError> {
		self.group.bonds.broadcast_raft(Message::StateSync(message))
	}

	fn bonds(&self) -> Bonds<M> {
		self.group.bonds.clone()
	}
}

impl<S, M> SyncSessionContext<M::StateSync> for Shared<S, M>
where
	S: Storage<M::Command>,
	M: StateMachine,
{
	fn leader(&self) -> PeerId {
		self
			.group
			.when
			.current_leader()
			.expect("leader always known on lagging followers during sync sessions")
	}

	fn set_committed(&mut self, position: Cursor) {
		self.committed = position;
		self.group.when.update_committed(position.index());
	}
}

impl<S, M> SyncProviderContext<M::StateSync> for Shared<S, M>
where
	S: Storage<M::Command>,
	M: StateMachine,
{
	fn leader(&self) -> Option<PeerId> {
		self.group.when.current_leader()
	}

	fn feed_command(&mut self, command: M::Command) -> Result<(), M::Command> {
		if self.is_leader() {
			let (unused, _) = oneshot::channel();
			if let Err(SendError(WorkerCommand::Raft(WorkerRaftCommand::Feed(
				commands,
				_,
			)))) =
				self
					.group
					.cmd_tx
					.send(WorkerCommand::Raft(WorkerRaftCommand::Feed(
						vec![command],
						unused,
					))) {
				return Err(
					commands
						.into_iter()
						.next()
						.expect("just sent this command, so it must be here"),
				);
			}
			Ok(())
		} else {
			Err(command)
		}
	}
}

#[derive(Debug)]
struct LocalApplyContext {
	prev_committed: Cursor,
	log_position: Cursor,
	term: Term,
	group_id: GroupId,
	network_id: crate::NetworkId,
}

impl ApplyContext for LocalApplyContext {
	fn committed(&self) -> Cursor {
		self.prev_committed
	}

	fn log_position(&self) -> Cursor {
		self.log_position
	}

	fn current_term(&self) -> Term {
		self.term
	}

	fn group_id(&self) -> &GroupId {
		&self.group_id
	}

	fn network_id(&self) -> &crate::NetworkId {
		&self.network_id
	}
}