aboutsummaryrefslogtreecommitdiff
path: root/src/membership.rs
blob: cb8dba99372dd471a62abd270dccefbd635682a7 (plain) (blame)
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
use std::collections::HashMap;
use std::hash::Hash as StdHash;
use std::hash::Hasher;
use std::io::Read;
use std::net::{IpAddr, SocketAddr};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use futures::future::join_all;
use futures::select;
use futures_util::future::*;
use sha2::{Digest, Sha256};
use tokio::prelude::*;
use tokio::sync::watch;
use tokio::sync::RwLock;

use crate::background::BackgroundRunner;
use crate::data::*;
use crate::error::Error;
use crate::proto::*;
use crate::rpc_client::*;
use crate::server::Config;

const PING_INTERVAL: Duration = Duration::from_secs(10);
const PING_TIMEOUT: Duration = Duration::from_secs(2);
const MAX_FAILED_PINGS: usize = 3;

pub struct System {
	pub config: Config,
	pub id: UUID,

	pub rpc_client: RpcClient,

	pub members: RwLock<Members>,

	pub background: Arc<BackgroundRunner>,
}

pub struct Members {
	pub status: HashMap<UUID, NodeStatus>,
	pub status_hash: Hash,

	pub config: NetworkConfig,
	pub ring: Vec<RingEntry>,
	pub n_datacenters: usize,
}

pub struct NodeStatus {
	pub addr: SocketAddr,
	pub remaining_ping_attempts: usize,
}

#[derive(Debug)]
pub struct RingEntry {
	pub location: Hash,
	pub node: UUID,
	pub datacenter: u64,
}

impl Members {
	fn handle_ping(&mut self, ip: IpAddr, info: &PingMessage) -> bool {
		let addr = SocketAddr::new(ip, info.rpc_port);
		let old_status = self.status.insert(
			info.id.clone(),
			NodeStatus {
				addr: addr.clone(),
				remaining_ping_attempts: MAX_FAILED_PINGS,
			},
		);
		match old_status {
			None => {
				eprintln!("Newly pingable node: {}", hex::encode(&info.id));
				true
			}
			Some(x) => x.addr != addr,
		}
	}

	fn recalculate_status_hash(&mut self) {
		let mut nodes = self.status.iter().collect::<Vec<_>>();
		nodes.sort_unstable_by_key(|(id, _status)| *id);

		let mut hasher = Sha256::new();
		eprintln!("Current set of pingable nodes: --");
		for (id, status) in nodes {
			eprintln!("{} {}", hex::encode(&id), status.addr);
			hasher.input(format!("{} {}\n", hex::encode(&id), status.addr));
		}
		eprintln!("END --");
		self.status_hash
			.as_slice_mut()
			.copy_from_slice(&hasher.result()[..]);
	}

	fn rebuild_ring(&mut self) {
		let mut new_ring = vec![];
		let mut datacenters = vec![];

		for (id, config) in self.config.members.iter() {
			let mut dc_hasher = std::collections::hash_map::DefaultHasher::new();
			config.datacenter.hash(&mut dc_hasher);
			let datacenter = dc_hasher.finish();

			if !datacenters.contains(&datacenter) {
				datacenters.push(datacenter);
			}

			for i in 0..config.n_tokens {
				let location = hash(format!("{} {}", hex::encode(&id), i).as_bytes());

				new_ring.push(RingEntry {
					location: location.into(),
					node: id.clone(),
					datacenter,
				})
			}
		}

		new_ring.sort_unstable_by(|x, y| x.location.cmp(&y.location));
		self.ring = new_ring;
		self.n_datacenters = datacenters.len();

		eprintln!("RING: --");
		for e in self.ring.iter() {
			eprintln!("{:?}", e);
		}
		eprintln!("END --");
	}

	pub fn walk_ring(&self, from: &Hash, n: usize) -> Vec<UUID> {
		if n >= self.config.members.len() {
			return self.config.members.keys().cloned().collect::<Vec<_>>();
		}

		let start = match self.ring.binary_search_by(|x| x.location.cmp(from)) {
			Ok(i) => i,
			Err(i) => {
				if i == 0 {
					self.ring.len() - 1
				} else {
					i - 1
				}
			}
		};

		self.walk_ring_from_pos(start, n)
	}

	fn walk_ring_from_pos(&self, start: usize, n: usize) -> Vec<UUID> {
		let mut ret = vec![];
		let mut datacenters = vec![];

		for delta in 0..self.ring.len() {
			if ret.len() == n {
				break;
			}

			let i = (start + delta) % self.ring.len();

			if datacenters.len() == self.n_datacenters && !ret.contains(&self.ring[i].node) {
				ret.push(self.ring[i].node.clone());
			} else if !datacenters.contains(&self.ring[i].datacenter) {
				ret.push(self.ring[i].node.clone());
				datacenters.push(self.ring[i].datacenter);
			}
		}

		ret
	}
}

fn read_network_config(metadata_dir: &PathBuf) -> Result<NetworkConfig, Error> {
	let mut path = metadata_dir.clone();
	path.push("network_config");

	let mut file = std::fs::OpenOptions::new()
		.read(true)
		.open(path.as_path())?;

	let mut net_config_bytes = vec![];
	file.read_to_end(&mut net_config_bytes)?;

	let net_config = rmp_serde::decode::from_read_ref(&net_config_bytes[..])?;

	Ok(net_config)
}

impl System {
	pub fn new(config: Config, id: UUID, background: Arc<BackgroundRunner>) -> Self {
		let net_config = match read_network_config(&config.metadata_dir) {
			Ok(x) => x,
			Err(e) => {
				println!(
					"No valid previous network configuration stored ({}), starting fresh.",
					e
				);
				NetworkConfig {
					members: HashMap::new(),
					version: 0,
				}
			}
		};
		let mut members = Members {
			status: HashMap::new(),
			status_hash: Hash::default(),
			config: net_config,
			ring: Vec::new(),
			n_datacenters: 0,
		};
		members.recalculate_status_hash();
		members.rebuild_ring();
		System {
			config,
			id,
			rpc_client: RpcClient::new(),
			members: RwLock::new(members),
			background,
		}
	}

	async fn save_network_config(self: Arc<Self>) -> Result<(), Error> {
		let mut path = self.config.metadata_dir.clone();
		path.push("network_config");

		let members = self.members.read().await;
		let data =
			rmp_to_vec_all_named(&members.config)?;
		drop(members);

		let mut f = tokio::fs::File::create(path.as_path())
			.await?;
		f.write_all(&data[..])
			.await?;
		Ok(())
	}

	pub async fn make_ping(&self) -> Message {
		let members = self.members.read().await;
		Message::Ping(PingMessage {
			id: self.id.clone(),
			rpc_port: self.config.rpc_port,
			status_hash: members.status_hash.clone(),
			config_version: members.config.version,
		})
	}

	pub async fn broadcast(self: Arc<Self>, msg: Message, timeout: Duration) {
		let members = self.members.read().await;
		let to = members
			.status
			.keys()
			.filter(|x| **x != self.id)
			.cloned()
			.collect::<Vec<_>>();
		drop(members);
		rpc_call_many(self.clone(), &to[..], &msg, timeout).await;
	}

	pub async fn bootstrap(self: Arc<Self>) {
		let bootstrap_peers = self
			.config
			.bootstrap_peers
			.iter()
			.map(|ip| (ip.clone(), None))
			.collect::<Vec<_>>();
		self.clone().ping_nodes(bootstrap_peers).await;

		self.background
			.clone()
			.spawn_worker(|stop_signal| self.ping_loop(stop_signal).map(Ok))
			.await;
	}

	pub async fn ping_nodes(self: Arc<Self>, peers: Vec<(SocketAddr, Option<UUID>)>) {
		let ping_msg = self.make_ping().await;
		let ping_resps = join_all(peers.iter().map(|(addr, id_option)| {
			let sys = self.clone();
			let ping_msg_ref = &ping_msg;
			async move {
				(
					id_option,
					addr.clone(),
					sys.rpc_client.call(&addr, ping_msg_ref, PING_TIMEOUT).await,
				)
			}
		}))
		.await;

		let mut members = self.members.write().await;

		let mut has_changes = false;
		let mut to_advertise = vec![];

		for (id_option, addr, ping_resp) in ping_resps {
			if let Ok(Message::Ping(info)) = ping_resp {
				let is_new = members.handle_ping(addr.ip(), &info);
				if is_new {
					has_changes = true;
					to_advertise.push(AdvertisedNode {
						id: info.id.clone(),
						addr: addr.clone(),
					});
				}
				if is_new || members.status_hash != info.status_hash {
					self.background
						.spawn_cancellable(self.clone().pull_status(info.id.clone()).map(Ok));
				}
				if is_new || members.config.version < info.config_version {
					self.background
						.spawn_cancellable(self.clone().pull_config(info.id.clone()).map(Ok));
				}
			} else if let Some(id) = id_option {
				let remaining_attempts = members
					.status
					.get(id)
					.map(|x| x.remaining_ping_attempts)
					.unwrap_or(0);
				if remaining_attempts == 0 {
					eprintln!(
						"Removing node {} after too many failed pings",
						hex::encode(&id)
					);
					members.status.remove(&id);
					has_changes = true;
				} else {
					if let Some(st) = members.status.get_mut(id) {
						st.remaining_ping_attempts = remaining_attempts - 1;
					}
				}
			}
		}
		if has_changes {
			members.recalculate_status_hash();
		}
		drop(members);

		if to_advertise.len() > 0 {
			self.broadcast(Message::AdvertiseNodesUp(to_advertise), PING_TIMEOUT)
				.await;
		}
	}

	pub async fn handle_ping(
		self: Arc<Self>,
		from: &SocketAddr,
		ping: &PingMessage,
	) -> Result<Message, Error> {
		let mut members = self.members.write().await;
		let is_new = members.handle_ping(from.ip(), ping);
		if is_new {
			members.recalculate_status_hash();
		}
		let status_hash = members.status_hash.clone();
		let config_version = members.config.version;
		drop(members);

		if is_new || status_hash != ping.status_hash {
			self.background.spawn_cancellable(self.clone().pull_status(ping.id.clone()).map(Ok));
		}
		if is_new || config_version < ping.config_version {
			self.background.spawn_cancellable(self.clone().pull_config(ping.id.clone()).map(Ok));
		}

		Ok(self.make_ping().await)
	}

	pub async fn handle_pull_status(&self) -> Result<Message, Error> {
		let members = self.members.read().await;
		let mut mem = vec![];
		for (node, status) in members.status.iter() {
			mem.push(AdvertisedNode {
				id: node.clone(),
				addr: status.addr.clone(),
			});
		}
		Ok(Message::AdvertiseNodesUp(mem))
	}

	pub async fn handle_pull_config(&self) -> Result<Message, Error> {
		let members = self.members.read().await;
		Ok(Message::AdvertiseConfig(members.config.clone()))
	}

	pub async fn handle_advertise_nodes_up(
		self: Arc<Self>,
		adv: &[AdvertisedNode],
	) -> Result<Message, Error> {
		let mut to_ping = vec![];

		let mut members = self.members.write().await;
		let mut has_changed = false;

		for node in adv.iter() {
			if node.id == self.id {
				// learn our own ip address
				let self_addr = SocketAddr::new(node.addr.ip(), self.config.rpc_port);
				let old_self = members.status.insert(
					node.id.clone(),
					NodeStatus {
						addr: self_addr,
						remaining_ping_attempts: MAX_FAILED_PINGS,
					},
				);
				has_changed = match old_self {
					None => true,
					Some(x) => x.addr != self_addr,
				};
			} else if !members.status.contains_key(&node.id) {
				to_ping.push((node.addr.clone(), Some(node.id.clone())));
			}
		}
		if has_changed {
			members.recalculate_status_hash();
		}
		drop(members);

		if to_ping.len() > 0 {
			self.background.spawn_cancellable(self.clone().ping_nodes(to_ping).map(Ok));
		}

		Ok(Message::Ok)
	}

	pub async fn handle_advertise_config(
		self: Arc<Self>,
		adv: &NetworkConfig,
	) -> Result<Message, Error> {
		let mut members = self.members.write().await;
		if adv.version > members.config.version {
			members.config = adv.clone();
			members.rebuild_ring();

			self.background.spawn_cancellable(
				self.clone()
					.broadcast(Message::AdvertiseConfig(adv.clone()), PING_TIMEOUT)
					.map(Ok),
			);
			self.background.spawn(self.clone().save_network_config());
		}

		Ok(Message::Ok)
	}

	pub async fn ping_loop(self: Arc<Self>, mut stop_signal: watch::Receiver<bool>) {
		loop {
			let restart_at = tokio::time::delay_for(PING_INTERVAL);

			let members = self.members.read().await;
			let ping_addrs = members
				.status
				.iter()
				.filter(|(id, _)| **id != self.id)
				.map(|(id, status)| (status.addr.clone(), Some(id.clone())))
				.collect::<Vec<_>>();
			drop(members);

			self.clone().ping_nodes(ping_addrs).await;

			select! {
				_ = restart_at.fuse() => (),
				must_exit = stop_signal.recv().fuse() => {
					match must_exit {
						None | Some(true) => return,
						_ => (),
					}
				}
			}
		}
	}

	pub fn pull_status(
		self: Arc<Self>,
		peer: UUID,
	) -> impl futures::future::Future<Output = ()> + Send + 'static {
		async move {
			let resp = rpc_call(self.clone(), &peer, &Message::PullStatus, PING_TIMEOUT).await;
			if let Ok(Message::AdvertiseNodesUp(nodes)) = resp {
				let _: Result<_, _> = self.handle_advertise_nodes_up(&nodes).await;
			}
		}
	}

	pub async fn pull_config(self: Arc<Self>, peer: UUID) {
		let resp = rpc_call(self.clone(), &peer, &Message::PullConfig, PING_TIMEOUT).await;
		if let Ok(Message::AdvertiseConfig(config)) = resp {
			let _: Result<_, _> = self.handle_advertise_config(&config).await;
		}
	}
}