dont run routing table ticks that require the network until it has started up

This commit is contained in:
Christien Rioux 2024-05-19 10:49:37 -04:00 committed by k8wu
parent b37e2cc3c9
commit e82cdbbfce
8 changed files with 283 additions and 243 deletions

View File

@ -237,7 +237,7 @@ impl AttachmentManager {
} }
// see if we need to restart the network // see if we need to restart the network
if netman.needs_restart() { if netman.network_needs_restart() {
info!("Restarting network"); info!("Restarting network");
restart = true; restart = true;
break; break;

View File

@ -117,8 +117,9 @@ pub(crate) enum NodeContactMethod {
/// Must use outbound relay to reach the node /// Must use outbound relay to reach the node
OutboundRelay(NodeRef), OutboundRelay(NodeRef),
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] #[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
struct NodeContactMethodCacheKey { struct NodeContactMethodCacheKey {
node_ids: TypedKeyGroup,
own_node_info_ts: Timestamp, own_node_info_ts: Timestamp,
target_node_info_ts: Timestamp, target_node_info_ts: Timestamp,
target_node_ref_filter: Option<NodeRefFilter>, target_node_ref_filter: Option<NodeRefFilter>,
@ -305,6 +306,13 @@ impl NetworkManager {
.net .net
.clone() .clone()
} }
fn opt_net(&self) -> Option<Network> {
self.unlocked_inner
.components
.read()
.as_ref()
.map(|x| x.net.clone())
}
fn receipt_manager(&self) -> ReceiptManager { fn receipt_manager(&self) -> ReceiptManager {
self.unlocked_inner self.unlocked_inner
.components .components
@ -512,9 +520,16 @@ impl NetworkManager {
} }
} }
pub fn needs_restart(&self) -> bool { pub fn network_needs_restart(&self) -> bool {
let net = self.net(); self.opt_net()
net.needs_restart() .map(|net| net.needs_restart())
.unwrap_or(false)
}
pub fn network_is_started(&self) -> bool {
self.opt_net()
.and_then(|net| net.is_started())
.unwrap_or(false)
} }
pub fn generate_node_status(&self, _routing_domain: RoutingDomain) -> NodeStatus { pub fn generate_node_status(&self, _routing_domain: RoutingDomain) -> NodeStatus {

View File

@ -72,8 +72,8 @@ pub const MAX_CAPABILITIES: usize = 64;
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
struct NetworkInner { struct NetworkInner {
/// true if the low-level network is running /// Some(true) if the low-level network is running, Some(false) if it is not, None if it is in transit
network_started: bool, network_started: Option<bool>,
/// set if the network needs to be restarted due to a low level configuration change /// set if the network needs to be restarted due to a low level configuration change
/// such as dhcp release or change of address or interfaces being added or removed /// such as dhcp release or change of address or interfaces being added or removed
network_needs_restart: bool, network_needs_restart: bool,
@ -137,7 +137,7 @@ pub(in crate::network_manager) struct Network {
impl Network { impl Network {
fn new_inner() -> NetworkInner { fn new_inner() -> NetworkInner {
NetworkInner { NetworkInner {
network_started: false, network_started: Some(false),
network_needs_restart: false, network_needs_restart: false,
needs_public_dial_info_check: false, needs_public_dial_info_check: false,
network_already_cleared: false, network_already_cleared: false,
@ -675,6 +675,8 @@ impl Network {
#[instrument(level = "debug", err, skip_all)] #[instrument(level = "debug", err, skip_all)]
pub async fn startup(&self) -> EyreResult<()> { pub async fn startup(&self) -> EyreResult<()> {
self.inner.lock().network_started = None;
let startup_func = async {
// initialize interfaces // initialize interfaces
self.unlocked_inner.interfaces.refresh().await?; self.unlocked_inner.interfaces.refresh().await?;
@ -819,7 +821,10 @@ impl Network {
// start listeners // start listeners
if protocol_config.inbound.contains(ProtocolType::UDP) { if protocol_config.inbound.contains(ProtocolType::UDP) {
self.bind_udp_protocol_handlers(&mut editor_public_internet, &mut editor_local_network) self.bind_udp_protocol_handlers(
&mut editor_public_internet,
&mut editor_local_network,
)
.await?; .await?;
} }
if protocol_config.inbound.contains(ProtocolType::WS) { if protocol_config.inbound.contains(ProtocolType::WS) {
@ -862,9 +867,17 @@ impl Network {
editor_public_internet.commit(true).await; editor_public_internet.commit(true).await;
editor_local_network.commit(true).await; editor_local_network.commit(true).await;
info!("network started"); Ok(())
self.inner.lock().network_started = true; };
let res = startup_func.await;
if res.is_err() {
info!("network failed to start");
self.inner.lock().network_started = Some(false);
return res;
}
info!("network started");
self.inner.lock().network_started = Some(true);
Ok(()) Ok(())
} }
@ -872,7 +885,7 @@ impl Network {
self.inner.lock().network_needs_restart self.inner.lock().network_needs_restart
} }
pub fn is_started(&self) -> bool { pub fn is_started(&self) -> Option<bool> {
self.inner.lock().network_started self.inner.lock().network_started
} }

View File

@ -394,6 +394,7 @@ impl NetworkManager {
// Get cache key // Get cache key
let ncm_key = NodeContactMethodCacheKey { let ncm_key = NodeContactMethodCacheKey {
node_ids: target_node_ref.node_ids(),
own_node_info_ts: routing_table.get_own_node_info_ts(routing_domain), own_node_info_ts: routing_table.get_own_node_info_ts(routing_domain),
target_node_info_ts: target_node_ref.node_info_ts(routing_domain), target_node_info_ts: target_node_ref.node_info_ts(routing_domain),
target_node_ref_filter: target_node_ref.filter_ref().cloned(), target_node_ref_filter: target_node_ref.filter_ref().cloned(),

View File

@ -76,15 +76,7 @@ impl NetworkManager {
} }
pub fn get_veilid_state(&self) -> Box<VeilidStateNetwork> { pub fn get_veilid_state(&self) -> Box<VeilidStateNetwork> {
let has_state = self if !self.network_is_started() {
.unlocked_inner
.components
.read()
.as_ref()
.map(|c| c.net.is_started())
.unwrap_or(false);
if !has_state {
return Box::new(VeilidStateNetwork { return Box::new(VeilidStateNetwork {
started: false, started: false,
bps_down: 0.into(), bps_down: 0.into(),

View File

@ -52,7 +52,7 @@ pub const MAX_CAPABILITIES: usize = 64;
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
struct NetworkInner { struct NetworkInner {
network_started: bool, network_started: Option<bool>,
network_needs_restart: bool, network_needs_restart: bool,
protocol_config: ProtocolConfig, protocol_config: ProtocolConfig,
} }
@ -74,7 +74,7 @@ pub(in crate::network_manager) struct Network {
impl Network { impl Network {
fn new_inner() -> NetworkInner { fn new_inner() -> NetworkInner {
NetworkInner { NetworkInner {
network_started: false, network_started: Some(false),
network_needs_restart: false, network_needs_restart: false,
protocol_config: Default::default(), protocol_config: Default::default(),
} }
@ -334,6 +334,8 @@ impl Network {
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
pub async fn startup(&self) -> EyreResult<()> { pub async fn startup(&self) -> EyreResult<()> {
self.inner.lock().network_started = None;
let startup_func = async {
log_net!(debug "starting network"); log_net!(debug "starting network");
// get protocol config // get protocol config
let protocol_config = { let protocol_config = {
@ -395,9 +397,18 @@ impl Network {
// commit routing table edits // commit routing table edits
editor_public_internet.commit(true).await; editor_public_internet.commit(true).await;
Ok(())
};
self.inner.lock().network_started = true; let res = startup_func.await;
log_net!(debug "network started"); if res.is_err() {
info!("network failed to start");
self.inner.lock().network_started = Some(false);
return res;
}
info!("network started");
self.inner.lock().network_started = Some(true);
Ok(()) Ok(())
} }
@ -405,7 +416,7 @@ impl Network {
self.inner.lock().network_needs_restart self.inner.lock().network_needs_restart
} }
pub fn is_started(&self) -> bool { pub fn is_started(&self) -> Option<bool> {
self.inner.lock().network_started self.inner.lock().network_started
} }

View File

@ -287,6 +287,9 @@ impl RoutingTable {
Ok(NodeContactMethod::Direct(v)) => v, Ok(NodeContactMethod::Direct(v)) => v,
Ok(v) => { Ok(v) => {
log_rtab!(warn "invalid contact method for bootstrap, ignoring peer: {:?}", v); log_rtab!(warn "invalid contact method for bootstrap, ignoring peer: {:?}", v);
let _ = routing_table
.network_manager()
.get_node_contact_method(nr.clone());
return; return;
} }
Err(e) => { Err(e) => {

View File

@ -149,6 +149,11 @@ impl RoutingTable {
inner.refresh_cached_entry_counts() inner.refresh_cached_entry_counts()
}; };
// Only do the rest if the network has started
if !self.network_manager().network_is_started() {
return Ok(());
}
let min_peer_count = self.with_config(|c| c.network.dht.min_peer_count as usize); let min_peer_count = self.with_config(|c| c.network.dht.min_peer_count as usize);
// Figure out which tables need bootstrap or peer minimum refresh // Figure out which tables need bootstrap or peer minimum refresh