2020-04-10 20:01:48 +00:00
|
|
|
use std::collections::HashMap;
|
2020-04-07 15:00:48 +00:00
|
|
|
use std::hash::Hash as StdHash;
|
|
|
|
use std::hash::Hasher;
|
2020-04-10 20:01:48 +00:00
|
|
|
use std::io::Read;
|
|
|
|
use std::net::{IpAddr, SocketAddr};
|
2020-04-07 14:26:22 +00:00
|
|
|
use std::path::PathBuf;
|
2020-04-10 20:01:48 +00:00
|
|
|
use std::sync::Arc;
|
2020-04-06 17:55:39 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2020-04-06 19:02:15 +00:00
|
|
|
use futures::future::join_all;
|
2020-04-11 16:51:11 +00:00
|
|
|
use futures::select;
|
|
|
|
use futures_util::future::*;
|
2020-04-10 20:01:48 +00:00
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
use tokio::prelude::*;
|
2020-04-11 16:51:11 +00:00
|
|
|
use tokio::sync::watch;
|
2020-04-11 21:53:32 +00:00
|
|
|
use tokio::sync::Mutex;
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
use crate::background::BackgroundRunner;
|
2020-04-06 17:55:39 +00:00
|
|
|
use crate::data::*;
|
2020-04-10 20:01:48 +00:00
|
|
|
use crate::error::Error;
|
2020-04-06 17:55:39 +00:00
|
|
|
use crate::proto::*;
|
2020-04-07 14:26:22 +00:00
|
|
|
use crate::rpc_client::*;
|
2020-04-10 20:01:48 +00:00
|
|
|
use crate::server::Config;
|
2020-04-06 17:55:39 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
pub rpc_client: RpcClient,
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
pub status: watch::Receiver<Arc<Status>>,
|
|
|
|
pub ring: watch::Receiver<Arc<Ring>>,
|
|
|
|
|
|
|
|
update_lock: Mutex<(watch::Sender<Arc<Status>>, watch::Sender<Arc<Ring>>)>,
|
2020-04-11 16:51:11 +00:00
|
|
|
|
|
|
|
pub background: Arc<BackgroundRunner>,
|
2020-04-06 17:55:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Status {
|
|
|
|
pub nodes: HashMap<UUID, NodeStatus>,
|
|
|
|
pub hash: Hash,
|
2020-04-07 15:00:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-04-07 15:00:48 +00:00
|
|
|
pub struct NodeStatus {
|
|
|
|
pub addr: SocketAddr,
|
|
|
|
pub remaining_ping_attempts: usize,
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Ring {
|
|
|
|
pub config: NetworkConfig,
|
|
|
|
pub ring: Vec<RingEntry>,
|
|
|
|
pub n_datacenters: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2020-04-07 15:00:48 +00:00
|
|
|
pub struct RingEntry {
|
2020-04-10 20:01:48 +00:00
|
|
|
pub location: Hash,
|
|
|
|
pub node: UUID,
|
|
|
|
pub datacenter: u64,
|
2020-04-06 17:55:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
impl Status {
|
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);
|
2020-04-11 21:53:32 +00:00
|
|
|
let old_status = self.nodes.insert(
|
2020-04-10 20:01:48 +00:00
|
|
|
info.id.clone(),
|
|
|
|
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-10 20:01:48 +00:00
|
|
|
},
|
|
|
|
);
|
2020-04-06 20:54:03 +00:00
|
|
|
match old_status {
|
|
|
|
None => {
|
2020-04-07 16:10:20 +00:00
|
|
|
eprintln!("Newly pingable node: {}", hex::encode(&info.id));
|
2020-04-06 20:54:03 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
Some(x) => x.addr != addr,
|
|
|
|
}
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
fn recalculate_hash(&mut self) {
|
|
|
|
let mut nodes = self.nodes.iter().collect::<Vec<_>>();
|
2020-04-07 16:10:20 +00:00
|
|
|
nodes.sort_unstable_by_key(|(id, _status)| *id);
|
2020-04-06 20:27:51 +00:00
|
|
|
|
2020-04-06 19:02:15 +00:00
|
|
|
let mut hasher = Sha256::new();
|
2020-04-06 21:10:28 +00:00
|
|
|
eprintln!("Current set of pingable nodes: --");
|
2020-04-06 20:27:51 +00:00
|
|
|
for (id, status) in nodes {
|
2020-04-07 16:10:20 +00:00
|
|
|
eprintln!("{} {}", hex::encode(&id), status.addr);
|
|
|
|
hasher.input(format!("{} {}\n", hex::encode(&id), status.addr));
|
2020-04-06 19:02:15 +00:00
|
|
|
}
|
2020-04-06 21:10:28 +00:00
|
|
|
eprintln!("END --");
|
2020-04-11 21:53:32 +00:00
|
|
|
self.hash
|
2020-04-10 20:01:48 +00:00
|
|
|
.as_slice_mut()
|
|
|
|
.copy_from_slice(&hasher.result()[..]);
|
2020-04-06 19:02:15 +00:00
|
|
|
}
|
2020-04-11 21:53:32 +00:00
|
|
|
}
|
2020-04-06 19:02:15 +00:00
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
impl Ring {
|
2020-04-10 20:01:48 +00:00
|
|
|
fn rebuild_ring(&mut self) {
|
|
|
|
let mut new_ring = vec![];
|
|
|
|
let mut datacenters = vec![];
|
2020-04-07 15:00:48 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
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();
|
2020-04-07 15:00:48 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
if !datacenters.contains(&datacenter) {
|
|
|
|
datacenters.push(datacenter);
|
|
|
|
}
|
2020-04-07 15:00:48 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
for i in 0..config.n_tokens {
|
|
|
|
let location = hash(format!("{} {}", hex::encode(&id), i).as_bytes());
|
2020-04-07 15:00:48 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
new_ring.push(RingEntry {
|
|
|
|
location: location.into(),
|
|
|
|
node: id.clone(),
|
|
|
|
datacenter,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 15:00:48 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
new_ring.sort_unstable_by(|x, y| x.location.cmp(&y.location));
|
|
|
|
self.ring = new_ring;
|
|
|
|
self.n_datacenters = datacenters.len();
|
2020-04-09 16:43:53 +00:00
|
|
|
|
|
|
|
eprintln!("RING: --");
|
|
|
|
for e in self.ring.iter() {
|
|
|
|
eprintln!("{:?}", e);
|
|
|
|
}
|
|
|
|
eprintln!("END --");
|
2020-04-10 20:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-04-08 20:00:41 +00:00
|
|
|
|
|
|
|
self.walk_ring_from_pos(start, n)
|
2020-04-10 20:01:48 +00:00
|
|
|
}
|
2020-04-08 20:00:41 +00:00
|
|
|
|
|
|
|
fn walk_ring_from_pos(&self, start: usize, n: usize) -> Vec<UUID> {
|
2020-04-10 20:01:48 +00:00
|
|
|
let mut ret = vec![];
|
|
|
|
let mut datacenters = vec![];
|
2020-04-07 15:00:48 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
for delta in 0..self.ring.len() {
|
|
|
|
if ret.len() == n {
|
|
|
|
break;
|
|
|
|
}
|
2020-04-07 15:00:48 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
let i = (start + delta) % self.ring.len();
|
2020-04-07 15:00:48 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 15:00:48 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
ret
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|
2020-04-06 17:55:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
fn read_network_config(metadata_dir: &PathBuf) -> Result<NetworkConfig, Error> {
|
2020-04-10 20:01:48 +00:00
|
|
|
let mut path = metadata_dir.clone();
|
|
|
|
path.push("network_config");
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
let mut file = std::fs::OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.open(path.as_path())?;
|
2020-04-10 20:01:48 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
let mut net_config_bytes = vec![];
|
2020-04-09 21:45:07 +00:00
|
|
|
file.read_to_end(&mut net_config_bytes)?;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
let net_config = rmp_serde::decode::from_read_ref(&net_config_bytes[..])?;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
Ok(net_config)
|
|
|
|
}
|
2020-04-06 17:55:39 +00:00
|
|
|
|
|
|
|
impl System {
|
2020-04-11 16:51:11 +00:00
|
|
|
pub fn new(config: Config, id: UUID, background: Arc<BackgroundRunner>) -> Self {
|
2020-04-10 20:01:48 +00:00
|
|
|
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 {
|
2020-04-09 21:45:07 +00:00
|
|
|
members: HashMap::new(),
|
|
|
|
version: 0,
|
|
|
|
}
|
2020-04-10 20:01:48 +00:00
|
|
|
}
|
|
|
|
};
|
2020-04-11 21:53:32 +00:00
|
|
|
let mut status = Status {
|
|
|
|
nodes: HashMap::new(),
|
|
|
|
hash: Hash::default(),
|
|
|
|
};
|
|
|
|
status.recalculate_hash();
|
|
|
|
let (update_status, status) = watch::channel(Arc::new(status));
|
|
|
|
|
|
|
|
let mut ring = Ring {
|
2020-04-10 20:01:48 +00:00
|
|
|
config: net_config,
|
|
|
|
ring: Vec::new(),
|
|
|
|
n_datacenters: 0,
|
|
|
|
};
|
2020-04-11 21:53:32 +00:00
|
|
|
ring.rebuild_ring();
|
|
|
|
let (update_ring, ring) = watch::channel(Arc::new(ring));
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
System {
|
2020-04-06 21:10:28 +00:00
|
|
|
config,
|
|
|
|
id,
|
2020-04-07 14:26:22 +00:00
|
|
|
rpc_client: RpcClient::new(),
|
2020-04-11 21:53:32 +00:00
|
|
|
status,
|
|
|
|
ring,
|
|
|
|
update_lock: Mutex::new((update_status, update_ring)),
|
2020-04-11 16:51:11 +00:00
|
|
|
background,
|
2020-04-06 17:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
async fn save_network_config(self: Arc<Self>) -> Result<(), Error> {
|
2020-04-10 20:01:48 +00:00
|
|
|
let mut path = self.config.metadata_dir.clone();
|
|
|
|
path.push("network_config");
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
let ring = self.ring.borrow().clone();
|
|
|
|
let data = rmp_to_vec_all_named(&ring.config)?;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
let mut f = tokio::fs::File::create(path.as_path()).await?;
|
|
|
|
f.write_all(&data[..]).await?;
|
2020-04-11 16:51:11 +00:00
|
|
|
Ok(())
|
2020-04-10 20:01:48 +00:00
|
|
|
}
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
pub fn make_ping(&self) -> Message {
|
|
|
|
let status = self.status.borrow().clone();
|
|
|
|
let ring = self.ring.borrow().clone();
|
2020-04-10 20:01:48 +00:00
|
|
|
Message::Ping(PingMessage {
|
2020-04-07 16:10:20 +00:00
|
|
|
id: self.id.clone(),
|
2020-04-06 19:02:15 +00:00
|
|
|
rpc_port: self.config.rpc_port,
|
2020-04-11 21:53:32 +00:00
|
|
|
status_hash: status.hash.clone(),
|
|
|
|
config_version: ring.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) {
|
2020-04-11 21:53:32 +00:00
|
|
|
let status = self.status.borrow().clone();
|
|
|
|
let to = status
|
|
|
|
.nodes
|
2020-04-10 20:01:48 +00:00
|
|
|
.keys()
|
|
|
|
.filter(|x| **x != self.id)
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<_>>();
|
2020-04-07 22:39:07 +00:00
|
|
|
rpc_call_many(self.clone(), &to[..], &msg, 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>) {
|
2020-04-10 20:01:48 +00:00
|
|
|
let bootstrap_peers = self
|
|
|
|
.config
|
|
|
|
.bootstrap_peers
|
2020-04-06 22:00:43 +00:00
|
|
|
.iter()
|
|
|
|
.map(|ip| (ip.clone(), None))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
self.clone().ping_nodes(bootstrap_peers).await;
|
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
self.background
|
|
|
|
.clone()
|
|
|
|
.spawn_worker(|stop_signal| self.ping_loop(stop_signal).map(Ok))
|
|
|
|
.await;
|
2020-04-06 22:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn ping_nodes(self: Arc<Self>, peers: Vec<(SocketAddr, Option<UUID>)>) {
|
2020-04-11 21:53:32 +00:00
|
|
|
let ping_msg = self.make_ping();
|
2020-04-10 20:01:48 +00:00
|
|
|
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;
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
let update_locked = self.update_lock.lock().await;
|
|
|
|
let mut status: Status = self.status.borrow().as_ref().clone();
|
|
|
|
let ring = self.ring.borrow().clone();
|
2020-04-06 22:00:43 +00:00
|
|
|
|
|
|
|
let mut has_changes = false;
|
|
|
|
let mut to_advertise = vec![];
|
|
|
|
|
|
|
|
for (id_option, addr, ping_resp) in ping_resps {
|
2020-04-06 19:02:15 +00:00
|
|
|
if let Ok(Message::Ping(info)) = ping_resp {
|
2020-04-11 21:53:32 +00:00
|
|
|
let is_new = status.handle_ping(addr.ip(), &info);
|
2020-04-06 22:00:43 +00:00
|
|
|
if is_new {
|
|
|
|
has_changes = true;
|
2020-04-10 20:01:48 +00:00
|
|
|
to_advertise.push(AdvertisedNode {
|
2020-04-06 22:00:43 +00:00
|
|
|
id: info.id.clone(),
|
|
|
|
addr: addr.clone(),
|
|
|
|
});
|
|
|
|
}
|
2020-04-11 21:53:32 +00:00
|
|
|
if is_new || status.hash != info.status_hash {
|
2020-04-11 16:51:11 +00:00
|
|
|
self.background
|
|
|
|
.spawn_cancellable(self.clone().pull_status(info.id.clone()).map(Ok));
|
2020-04-06 22:00:43 +00:00
|
|
|
}
|
2020-04-11 21:53:32 +00:00
|
|
|
if is_new || ring.config.version < info.config_version {
|
2020-04-11 16:51:11 +00:00
|
|
|
self.background
|
|
|
|
.spawn_cancellable(self.clone().pull_config(info.id.clone()).map(Ok));
|
2020-04-06 22:00:43 +00:00
|
|
|
}
|
|
|
|
} else if let Some(id) = id_option {
|
2020-04-11 21:53:32 +00:00
|
|
|
let remaining_attempts = status
|
|
|
|
.nodes
|
2020-04-10 20:01:48 +00:00
|
|
|
.get(id)
|
|
|
|
.map(|x| x.remaining_ping_attempts)
|
|
|
|
.unwrap_or(0);
|
2020-04-06 22:00:43 +00:00
|
|
|
if remaining_attempts == 0 {
|
2020-04-10 20:01:48 +00:00
|
|
|
eprintln!(
|
|
|
|
"Removing node {} after too many failed pings",
|
|
|
|
hex::encode(&id)
|
|
|
|
);
|
2020-04-11 21:53:32 +00:00
|
|
|
status.nodes.remove(&id);
|
2020-04-06 22:00:43 +00:00
|
|
|
has_changes = true;
|
|
|
|
} else {
|
2020-04-11 21:53:32 +00:00
|
|
|
if let Some(st) = status.nodes.get_mut(id) {
|
2020-04-06 22:00:43 +00:00
|
|
|
st.remaining_ping_attempts = remaining_attempts - 1;
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 19:02:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-06 22:00:43 +00:00
|
|
|
if has_changes {
|
2020-04-11 21:53:32 +00:00
|
|
|
status.recalculate_hash();
|
2020-04-06 22:00:43 +00:00
|
|
|
}
|
2020-04-11 21:53:32 +00:00
|
|
|
if let Err(e) = update_locked.0.broadcast(Arc::new(status)) {
|
|
|
|
eprintln!("In ping_nodes: could not save status update ({})", e);
|
|
|
|
}
|
|
|
|
drop(update_locked);
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2020-04-06 22:00:43 +00:00
|
|
|
if to_advertise.len() > 0 {
|
2020-04-10 20:01:48 +00:00
|
|
|
self.broadcast(Message::AdvertiseNodesUp(to_advertise), PING_TIMEOUT)
|
|
|
|
.await;
|
2020-04-06 22:00:43 +00:00
|
|
|
}
|
2020-04-06 19:02:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub async fn handle_ping(
|
|
|
|
self: Arc<Self>,
|
|
|
|
from: &SocketAddr,
|
|
|
|
ping: &PingMessage,
|
|
|
|
) -> Result<Message, Error> {
|
2020-04-11 21:53:32 +00:00
|
|
|
let update_locked = self.update_lock.lock().await;
|
|
|
|
let mut status: Status = self.status.borrow().as_ref().clone();
|
|
|
|
|
|
|
|
let is_new = status.handle_ping(from.ip(), ping);
|
2020-04-06 20:54:03 +00:00
|
|
|
if is_new {
|
2020-04-11 21:53:32 +00:00
|
|
|
status.recalculate_hash();
|
2020-04-06 20:54:03 +00:00
|
|
|
}
|
2020-04-11 21:53:32 +00:00
|
|
|
let status_hash = status.hash.clone();
|
|
|
|
let config_version = self.ring.borrow().config.version;
|
|
|
|
|
|
|
|
update_locked.0.broadcast(Arc::new(status))?;
|
|
|
|
drop(update_locked);
|
2020-04-06 19:02:15 +00:00
|
|
|
|
2020-04-06 20:27:51 +00:00
|
|
|
if is_new || status_hash != ping.status_hash {
|
2020-04-11 21:53:32 +00:00
|
|
|
self.background
|
|
|
|
.spawn_cancellable(self.clone().pull_status(ping.id.clone()).map(Ok));
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
|
|
|
if is_new || config_version < ping.config_version {
|
2020-04-11 21:53:32 +00:00
|
|
|
self.background
|
|
|
|
.spawn_cancellable(self.clone().pull_config(ping.id.clone()).map(Ok));
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
Ok(self.make_ping())
|
2020-04-06 19:02:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
pub fn handle_pull_status(&self) -> Result<Message, Error> {
|
|
|
|
let status = self.status.borrow().clone();
|
2020-04-06 20:27:51 +00:00
|
|
|
let mut mem = vec![];
|
2020-04-11 21:53:32 +00:00
|
|
|
for (node, status) in status.nodes.iter() {
|
2020-04-10 20:01:48 +00:00
|
|
|
mem.push(AdvertisedNode {
|
2020-04-06 20:27:51 +00:00
|
|
|
id: node.clone(),
|
|
|
|
addr: status.addr.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(Message::AdvertiseNodesUp(mem))
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
pub fn handle_pull_config(&self) -> Result<Message, Error> {
|
|
|
|
let ring = self.ring.borrow().clone();
|
|
|
|
Ok(Message::AdvertiseConfig(ring.config.clone()))
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub async fn handle_advertise_nodes_up(
|
|
|
|
self: Arc<Self>,
|
|
|
|
adv: &[AdvertisedNode],
|
|
|
|
) -> Result<Message, Error> {
|
2020-04-06 22:00:43 +00:00
|
|
|
let mut to_ping = vec![];
|
2020-04-06 20:27:51 +00:00
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
let update_lock = self.update_lock.lock().await;
|
|
|
|
let mut status: Status = self.status.borrow().as_ref().clone();
|
2020-04-06 22:00:43 +00:00
|
|
|
let mut has_changed = false;
|
|
|
|
|
2020-04-06 20:27:51 +00:00
|
|
|
for node in adv.iter() {
|
2020-04-06 22:00:43 +00:00
|
|
|
if node.id == self.id {
|
|
|
|
// learn our own ip address
|
|
|
|
let self_addr = SocketAddr::new(node.addr.ip(), self.config.rpc_port);
|
2020-04-11 21:53:32 +00:00
|
|
|
let old_self = status.nodes.insert(
|
2020-04-10 20:01:48 +00:00
|
|
|
node.id.clone(),
|
|
|
|
NodeStatus {
|
2020-04-06 22:00:43 +00:00
|
|
|
addr: self_addr,
|
|
|
|
remaining_ping_attempts: MAX_FAILED_PINGS,
|
2020-04-10 20:01:48 +00:00
|
|
|
},
|
|
|
|
);
|
2020-04-06 22:00:43 +00:00
|
|
|
has_changed = match old_self {
|
|
|
|
None => true,
|
|
|
|
Some(x) => x.addr != self_addr,
|
|
|
|
};
|
2020-04-11 21:53:32 +00:00
|
|
|
} else if !status.nodes.contains_key(&node.id) {
|
2020-04-06 22:00:43 +00:00
|
|
|
to_ping.push((node.addr.clone(), Some(node.id.clone())));
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-06 22:00:43 +00:00
|
|
|
if has_changed {
|
2020-04-11 21:53:32 +00:00
|
|
|
status.recalculate_hash();
|
2020-04-06 22:00:43 +00:00
|
|
|
}
|
2020-04-11 21:53:32 +00:00
|
|
|
update_lock.0.broadcast(Arc::new(status))?;
|
|
|
|
drop(update_lock);
|
2020-04-06 22:00:43 +00:00
|
|
|
|
|
|
|
if to_ping.len() > 0 {
|
2020-04-11 21:53:32 +00:00
|
|
|
self.background
|
|
|
|
.spawn_cancellable(self.clone().ping_nodes(to_ping).map(Ok));
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Message::Ok)
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub async fn handle_advertise_config(
|
|
|
|
self: Arc<Self>,
|
|
|
|
adv: &NetworkConfig,
|
|
|
|
) -> Result<Message, Error> {
|
2020-04-11 21:53:32 +00:00
|
|
|
let mut ring: Ring = self.ring.borrow().as_ref().clone();
|
|
|
|
let update_lock = self.update_lock.lock().await;
|
|
|
|
|
|
|
|
if adv.version > ring.config.version {
|
|
|
|
ring.config = adv.clone();
|
|
|
|
ring.rebuild_ring();
|
|
|
|
update_lock.1.broadcast(Arc::new(ring))?;
|
|
|
|
drop(update_lock);
|
2020-04-07 16:10:20 +00:00
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
self.background.spawn_cancellable(
|
2020-04-10 20:01:48 +00:00
|
|
|
self.clone()
|
2020-04-11 16:51:11 +00:00
|
|
|
.broadcast(Message::AdvertiseConfig(adv.clone()), PING_TIMEOUT)
|
|
|
|
.map(Ok),
|
2020-04-10 20:01:48 +00:00
|
|
|
);
|
2020-04-11 16:51:11 +00:00
|
|
|
self.background.spawn(self.clone().save_network_config());
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Message::Ok)
|
|
|
|
}
|
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
pub async fn ping_loop(self: Arc<Self>, mut stop_signal: watch::Receiver<bool>) {
|
2020-04-06 20:27:51 +00:00
|
|
|
loop {
|
|
|
|
let restart_at = tokio::time::delay_for(PING_INTERVAL);
|
2020-04-10 20:01:48 +00:00
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
let status = self.status.borrow().clone();
|
|
|
|
let ping_addrs = status
|
|
|
|
.nodes
|
2020-04-10 20:01:48 +00:00
|
|
|
.iter()
|
|
|
|
.filter(|(id, _)| **id != self.id)
|
|
|
|
.map(|(id, status)| (status.addr.clone(), Some(id.clone())))
|
|
|
|
.collect::<Vec<_>>();
|
2020-04-06 20:27:51 +00:00
|
|
|
|
2020-04-06 22:00:43 +00:00
|
|
|
self.clone().ping_nodes(ping_addrs).await;
|
2020-04-06 20:27:51 +00:00
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
select! {
|
|
|
|
_ = restart_at.fuse() => (),
|
|
|
|
must_exit = stop_signal.recv().fuse() => {
|
|
|
|
match must_exit {
|
|
|
|
None | Some(true) => return,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 20:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub fn pull_status(
|
|
|
|
self: Arc<Self>,
|
|
|
|
peer: UUID,
|
|
|
|
) -> impl futures::future::Future<Output = ()> + Send + 'static {
|
2020-04-06 20:54:03 +00:00
|
|
|
async move {
|
2020-04-10 20:01:48 +00:00
|
|
|
let resp = rpc_call(self.clone(), &peer, &Message::PullStatus, PING_TIMEOUT).await;
|
2020-04-06 20:54:03 +00:00
|
|
|
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) {
|
2020-04-10 20:01:48 +00:00
|
|
|
let resp = rpc_call(self.clone(), &peer, &Message::PullConfig, PING_TIMEOUT).await;
|
2020-04-06 20:27:51 +00:00
|
|
|
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
|
|
|
}
|