2020-04-06 17:55:39 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::time::Duration;
|
2020-04-06 19:02:15 +00:00
|
|
|
use std::net::{IpAddr, SocketAddr};
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2020-04-06 19:02:15 +00:00
|
|
|
use futures::future::join_all;
|
2020-04-06 17:55:39 +00:00
|
|
|
use hyper::client::Client;
|
|
|
|
use tokio::sync::RwLock;
|
2020-04-06 19:02:15 +00:00
|
|
|
use sha2::{Sha256, Digest};
|
2020-04-06 17:55:39 +00:00
|
|
|
|
|
|
|
use crate::Config;
|
|
|
|
use crate::error::Error;
|
|
|
|
use crate::data::*;
|
|
|
|
use crate::proto::*;
|
|
|
|
use crate::rpc::*;
|
|
|
|
|
|
|
|
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: Client<hyper::client::HttpConnector, hyper::Body>,
|
|
|
|
|
|
|
|
pub members: RwLock<Members>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Members {
|
|
|
|
pub status: HashMap<UUID, NodeStatus>,
|
2020-04-06 19:02:15 +00:00
|
|
|
pub status_hash: Hash,
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2020-04-06 20:27:51 +00:00
|
|
|
pub config: NetworkConfig,
|
2020-04-06 17:55:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 19:02:15 +00:00
|
|
|
impl Members {
|
2020-04-06 20:27:51 +00:00
|
|
|
fn handle_ping(&mut self, ip: IpAddr, info: &PingMessage) -> bool {
|
2020-04-06 20:54:03 +00:00
|
|
|
let addr = SocketAddr::new(ip, info.rpc_port);
|
|
|
|
let old_status = self.status.insert(info.id.clone(),
|
2020-04-06 19:02:15 +00:00
|
|
|
NodeStatus{
|
2020-04-06 20:54:03 +00:00
|
|
|
addr: addr.clone(),
|
2020-04-06 19:02:15 +00:00
|
|
|
remaining_ping_attempts: MAX_FAILED_PINGS,
|
2020-04-06 20:54:03 +00:00
|
|
|
});
|
|
|
|
match old_status {
|
|
|
|
None => {
|
|
|
|
eprintln!("Discovered new node (ping): {}", hex::encode(info.id));
|
|
|
|
true
|
|
|
|
}
|
|
|
|
Some(x) => x.addr != addr,
|
|
|
|
}
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_advertise_node(&mut self, id: &UUID, addr: &SocketAddr) -> bool {
|
|
|
|
if !self.status.contains_key(id) {
|
2020-04-06 20:54:03 +00:00
|
|
|
eprintln!("Discovered new node (advertisment): {}", hex::encode(id));
|
2020-04-06 20:27:51 +00:00
|
|
|
self.status.insert(id.clone(),
|
|
|
|
NodeStatus{
|
|
|
|
addr: addr.clone(),
|
|
|
|
remaining_ping_attempts: MAX_FAILED_PINGS,
|
|
|
|
});
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2020-04-06 19:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn recalculate_status_hash(&mut self) {
|
2020-04-06 20:27:51 +00:00
|
|
|
let mut nodes = self.status.iter().collect::<Vec<_>>();
|
|
|
|
nodes.sort_by_key(|(id, _status)| *id);
|
|
|
|
|
2020-04-06 19:02:15 +00:00
|
|
|
let mut hasher = Sha256::new();
|
2020-04-06 20:27:51 +00:00
|
|
|
for (id, status) in nodes {
|
|
|
|
hasher.input(format!("{} {}\n", hex::encode(id), status.addr));
|
2020-04-06 19:02:15 +00:00
|
|
|
}
|
|
|
|
self.status_hash.copy_from_slice(&hasher.result()[..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 17:55:39 +00:00
|
|
|
pub struct NodeStatus {
|
|
|
|
pub addr: SocketAddr,
|
2020-04-06 19:02:15 +00:00
|
|
|
pub remaining_ping_attempts: usize,
|
2020-04-06 17:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl System {
|
|
|
|
pub fn new(config: Config, id: UUID) -> Self {
|
|
|
|
System{
|
|
|
|
config,
|
|
|
|
id,
|
|
|
|
rpc_client: Client::new(),
|
|
|
|
members: RwLock::new(Members{
|
|
|
|
status: HashMap::new(),
|
2020-04-06 19:02:15 +00:00
|
|
|
status_hash: [0u8; 32],
|
2020-04-06 20:27:51 +00:00
|
|
|
config: NetworkConfig{
|
|
|
|
members: HashMap::new(),
|
|
|
|
version: 0,
|
|
|
|
},
|
2020-04-06 17:55:39 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:02:15 +00:00
|
|
|
pub async fn make_ping(&self) -> Message {
|
2020-04-06 20:27:51 +00:00
|
|
|
let members = self.members.read().await;
|
2020-04-06 19:02:15 +00:00
|
|
|
Message::Ping(PingMessage{
|
|
|
|
id: self.id,
|
|
|
|
rpc_port: self.config.rpc_port,
|
2020-04-06 20:27:51 +00:00
|
|
|
status_hash: members.status_hash.clone(),
|
|
|
|
config_version: members.config.version,
|
2020-04-06 19:02:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:27:51 +00:00
|
|
|
pub async fn broadcast(self: Arc<Self>, msg: Message, timeout: Duration) {
|
|
|
|
let members = self.members.read().await;
|
2020-04-06 20:54:03 +00:00
|
|
|
let to = members.status.keys().filter(|x| **x != self.id).cloned().collect::<Vec<_>>();
|
2020-04-06 20:27:51 +00:00
|
|
|
drop(members);
|
|
|
|
rpc_call_many(self.clone(), &to[..], &msg, None, timeout).await;
|
2020-04-06 17:55:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 19:02:15 +00:00
|
|
|
pub async fn bootstrap(self: Arc<Self>) {
|
|
|
|
let ping_msg = self.make_ping().await;
|
|
|
|
let ping_resps = join_all(
|
|
|
|
self.config.bootstrap_peers.iter().cloned()
|
|
|
|
.map(|to| {
|
|
|
|
let sys = self.clone();
|
|
|
|
let ping_msg_ref = &ping_msg;
|
|
|
|
async move {
|
|
|
|
(to.clone(), rpc_call_addr(sys, &to, ping_msg_ref, PING_TIMEOUT).await)
|
|
|
|
}
|
|
|
|
})).await;
|
|
|
|
|
|
|
|
let mut members = self.members.write().await;
|
|
|
|
for (addr, ping_resp) in ping_resps {
|
|
|
|
if let Ok(Message::Ping(info)) = ping_resp {
|
|
|
|
members.handle_ping(addr.ip(), &info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
members.recalculate_status_hash();
|
|
|
|
drop(members);
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2020-04-06 20:27:51 +00:00
|
|
|
tokio::spawn(self.ping_loop());
|
2020-04-06 19:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn handle_ping(self: Arc<Self>,
|
|
|
|
from: &SocketAddr,
|
|
|
|
ping: &PingMessage)
|
|
|
|
-> Result<Message, Error>
|
|
|
|
{
|
|
|
|
let mut members = self.members.write().await;
|
2020-04-06 20:27:51 +00:00
|
|
|
let is_new = members.handle_ping(from.ip(), ping);
|
2020-04-06 20:54:03 +00:00
|
|
|
if is_new {
|
|
|
|
members.recalculate_status_hash();
|
|
|
|
}
|
2020-04-06 20:27:51 +00:00
|
|
|
let status_hash = members.status_hash.clone();
|
|
|
|
let config_version = members.config.version;
|
2020-04-06 19:02:15 +00:00
|
|
|
drop(members);
|
|
|
|
|
2020-04-06 20:27:51 +00:00
|
|
|
if is_new || status_hash != ping.status_hash {
|
|
|
|
tokio::spawn(self.clone().pull_status(ping.id.clone()));
|
|
|
|
}
|
|
|
|
if is_new || config_version < ping.config_version {
|
|
|
|
tokio::spawn(self.clone().pull_config(ping.id.clone()));
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:02:15 +00:00
|
|
|
Ok(self.make_ping().await)
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:27:51 +00:00
|
|
|
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])
|
2020-04-06 19:02:15 +00:00
|
|
|
-> Result<Message, Error>
|
|
|
|
{
|
2020-04-06 20:27:51 +00:00
|
|
|
let mut propagate = vec![];
|
|
|
|
|
|
|
|
let mut members = self.members.write().await;
|
|
|
|
for node in adv.iter() {
|
|
|
|
let is_new = members.handle_advertise_node(&node.id, &node.addr);
|
|
|
|
if is_new {
|
|
|
|
tokio::spawn(self.clone().pull_status(node.id.clone()));
|
|
|
|
tokio::spawn(self.clone().pull_config(node.id.clone()));
|
|
|
|
propagate.push(node.clone());
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 20:54:03 +00:00
|
|
|
drop(members);
|
2020-04-06 20:27:51 +00:00
|
|
|
|
|
|
|
if propagate.len() > 0 {
|
|
|
|
tokio::spawn(self.clone().broadcast(Message::AdvertiseNodesUp(propagate), PING_TIMEOUT));
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
tokio::spawn(self.clone().broadcast(Message::AdvertiseConfig(adv.clone()), PING_TIMEOUT));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Message::Ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn ping_loop(self: Arc<Self>) {
|
|
|
|
loop {
|
|
|
|
let restart_at = tokio::time::delay_for(PING_INTERVAL);
|
|
|
|
|
|
|
|
let members = self.members.read().await;
|
|
|
|
let ping_addrs = members.status.iter()
|
2020-04-06 20:54:03 +00:00
|
|
|
.filter(|(id, _)| **id != self.id)
|
2020-04-06 20:27:51 +00:00
|
|
|
.map(|(id, status)| (id.clone(), status.addr.clone()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
drop(members);
|
|
|
|
|
|
|
|
let ping_msg = self.make_ping().await;
|
|
|
|
let ping_resps = join_all(
|
|
|
|
ping_addrs.iter()
|
|
|
|
.map(|(id, addr)| {
|
|
|
|
let sys = self.clone();
|
|
|
|
let ping_msg_ref = &ping_msg;
|
|
|
|
async move {
|
|
|
|
(id, addr.clone(), rpc_call_addr(sys, &addr, ping_msg_ref, PING_TIMEOUT).await)
|
|
|
|
}
|
|
|
|
})).await;
|
|
|
|
|
|
|
|
let mut members = self.members.write().await;
|
|
|
|
for (id, addr, ping_resp) in ping_resps {
|
|
|
|
if let Ok(Message::Ping(ping)) = ping_resp {
|
|
|
|
let is_new = members.handle_ping(addr.ip(), &ping);
|
|
|
|
if is_new || members.status_hash != ping.status_hash {
|
|
|
|
tokio::spawn(self.clone().pull_status(ping.id.clone()));
|
|
|
|
}
|
|
|
|
if is_new || members.config.version < ping.config_version {
|
|
|
|
tokio::spawn(self.clone().pull_config(ping.id.clone()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
if let Some(st) = members.status.get_mut(id) {
|
|
|
|
st.remaining_ping_attempts = remaining_attempts - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drop(members);
|
|
|
|
|
|
|
|
restart_at.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:54:03 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-04-06 20:54:03 +00:00
|
|
|
let _: Result<_, _> = self.handle_advertise_config(&config).await;
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
2020-04-06 19:02:15 +00:00
|
|
|
}
|
2020-04-06 17:55:39 +00:00
|
|
|
}
|