From 2fa65554f167eda086156ab5f2caf899d5402be8 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 21 Oct 2021 13:45:52 +0200 Subject: [PATCH] Persist peer list to file --- src/rpc/system.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/rpc/system.rs b/src/rpc/system.rs index 3a81bc3b..3031063d 100644 --- a/src/rpc/system.rs +++ b/src/rpc/system.rs @@ -69,6 +69,7 @@ pub struct System { pub id: Uuid, persist_config: Persister, + persist_peer_list: Persister>, local_status: ArcSwap, node_status: RwLock>, @@ -148,6 +149,7 @@ impl System { info!("Node public key: {}", hex::encode(&node_key.public_key())); let persist_config = Persister::new(&config.metadata_dir, "network_config"); + let persist_peer_list = Persister::new(&config.metadata_dir, "peer_list"); let net_config = match persist_config.load() { Ok(x) => x, @@ -189,6 +191,7 @@ impl System { let sys = Arc::new(System { id: netapp.id.into(), persist_config, + persist_peer_list, local_status: ArcSwap::new(Arc::new(local_status)), node_status: RwLock::new(HashMap::new()), netapp: netapp.clone(), @@ -424,13 +427,12 @@ impl System { let mut ping_list = self.bootstrap_peers.clone(); - /* - *TODO bring this back: persisted list of peers - if let Ok(peers) = self.persist_status.load_async().await { - ping_list.extend(peers.iter().map(|x| (x.addr, Some(x.id)))); + // Add peer list from list stored on disk + if let Ok(peers) = self.persist_peer_list.load_async().await { + ping_list.extend(peers.iter().map(|(id, addr)| ((*id).into(), *addr))) } - */ + // Fetch peer list from Consul if let Some((consul_host, consul_service_name)) = &consul_config { match get_consul_nodes(consul_host, consul_service_name).await { Ok(node_list) => { @@ -447,6 +449,16 @@ impl System { } } + let peer_list = self + .fullmesh + .get_peer_list() + .iter() + .map(|n| (n.id.into(), n.addr)) + .collect::>(); + if let Err(e) = self.persist_peer_list.save_async(&peer_list).await { + warn!("Could not save peer list to file: {}", e); + } + self.background.spawn(self.clone().advertise_to_consul()); let restart_at = tokio::time::sleep(DISCOVERY_INTERVAL);