checkpoint

This commit is contained in:
John Smith
2022-04-08 10:17:09 -04:00
parent f7873aba88
commit ddb74d993f
13 changed files with 263 additions and 142 deletions

View File

@@ -37,10 +37,10 @@ pub struct BucketEntry {
min_max_version: Option<(u8, u8)>,
seen_our_dial_info: bool,
last_connection: Option<(ConnectionDescriptor, u64)>,
dial_infos: Vec<DialInfo>,
node_info: NodeInfo,
peer_stats: PeerStats,
latency_stats_accounting: LatencyStatsAccounting,
transfer_stats_accounting: TransferStatsAccounting,
peer_stats: PeerStats,
}
impl BucketEntry {
@@ -51,7 +51,7 @@ impl BucketEntry {
min_max_version: None,
seen_our_dial_info: false,
last_connection: None,
dial_infos: Vec::new(),
node_info: NodeInfo::default(),
latency_stats_accounting: LatencyStatsAccounting::new(),
transfer_stats_accounting: TransferStatsAccounting::new(),
peer_stats: PeerStats {
@@ -60,7 +60,7 @@ impl BucketEntry {
ping_stats: PingStats::default(),
latency: None,
transfer: TransferStatsDownUp::default(),
node_info: None,
status: None,
},
}
}
@@ -105,50 +105,37 @@ impl BucketEntry {
move |e1, e2| Self::cmp_fastest_reliable(cur_ts, e1, e2)
}
pub fn update_dial_infos(&mut self, dial_infos: &[DialInfo]) {
self.dial_infos = dial_infos.to_vec();
self.dial_infos.sort();
pub fn update_node_info(&mut self, node_info: NodeInfo) {
self.node_info = node_info
}
pub fn update_single_dial_info(&mut self, dial_info: &DialInfo) {
let dif = dial_info.make_filter(true);
self.dial_infos.retain(|di| !di.matches_filter(&dif));
self.dial_infos.push(dial_info.clone());
self.dial_infos.sort();
pub fn node_info(&self) -> &NodeInfo {
&self.node_info
}
pub fn first_filtered_dial_info<F>(&self, filter: F) -> Option<DialInfo>
pub fn first_filtered_node_info<F>(&self, filter: F) -> Option<NodeInfo>
where
F: Fn(&DialInfo) -> bool,
{
for di in &self.dial_infos {
if filter(di) {
return Some(di.clone());
}
let out = self.node_info.first_filtered(filter);
if out.dial_infos.is_empty() && out.relay_dial_infos.is_empty() {
None
} else {
Some(out)
}
None
}
pub fn all_filtered_dial_infos<F>(&self, filter: F) -> Vec<DialInfo>
pub fn all_filtered_node_info<F>(&self, filter: F) -> NodeInfo
where
F: Fn(&DialInfo) -> bool,
{
let mut ret = Vec::new();
for di in &self.dial_infos {
if filter(di) {
ret.push(di.clone());
}
}
ret
}
pub fn dial_infos(&self) -> &[DialInfo] {
&self.dial_infos
self.node_info.all_filtered(filter)
}
pub fn get_peer_info(&self, key: DHTKey, scope: PeerScope) -> PeerInfo {
PeerInfo {
node_id: NodeId::new(key),
dial_infos: self.all_filtered_dial_infos(|di| di.matches_peer_scope(scope)),
node_info: self.all_filtered_node_info(|di| di.matches_peer_scope(scope)),
}
}
@@ -182,8 +169,8 @@ impl BucketEntry {
&self.peer_stats
}
pub fn update_node_info(&mut self, node_info: NodeInfo) {
self.peer_stats.node_info = Some(node_info);
pub fn update_node_status(&mut self, status: NodeStatus) {
self.peer_stats.status = Some(status);
}
pub fn set_seen_our_dial_info(&mut self, seen: bool) {
@@ -250,12 +237,8 @@ impl BucketEntry {
// See which ping pattern we are to use
let state = self.state(cur_ts);
// If the current dial info hasn't been recognized then we gotta ping regardless
if !self.seen_our_dial_info && matches!(state, BucketEntryState::Reliable) {
return self.needs_constant_ping(cur_ts, UNRELIABLE_PING_INTERVAL_SECS as u64);
}
// If this entry is our relay node, then we should ping it regularly
else if let Some(relay_node) = relay_node {
// If this entry is our relay node, then we should ping it regularly to keep our association alive
if let Some(relay_node) = relay_node {
if relay_node.node_id() == *node_id {
return self.needs_constant_ping(cur_ts, KEEPALIVE_PING_INTERVAL_SECS as u64);
}

View File

@@ -21,7 +21,7 @@ impl RoutingTable {
.1
.as_ref()
.unwrap()
.first_filtered_dial_info(|di| di.matches_filter(&dial_info_filter1))
.first_filtered_node_info(|di| di.matches_filter(&dial_info_filter1))
.is_some()
},
)),
@@ -39,14 +39,21 @@ impl RoutingTable {
pub fn get_own_peer_info(&self, scope: PeerScope) -> PeerInfo {
let filter = DialInfoFilter::scoped(scope);
let netman = self.network_manager();
let relay_node = netman.relay_node();
PeerInfo {
node_id: NodeId::new(self.node_id()),
dial_infos: self
.all_filtered_dial_info_details(&filter)
.iter()
.map(|did| did.dial_info.clone())
.collect(),
node_info: NodeInfo {
network_class: netman.get_network_class().unwrap_or(NetworkClass::Invalid),
dial_infos: self
.all_filtered_dial_info_details(&filter)
.iter()
.map(|did| did.dial_info.clone())
.collect(),
relay_dial_infos: relay_node
.map(|rn| rn.node_info().dial_infos)
.unwrap_or_default(),
},
}
}

View File

@@ -268,6 +268,7 @@ impl RoutingTable {
e.1.set_seen_our_dial_info(false);
}
}
//
// Release any waiters
let mut new_eventual = Eventual::new();
@@ -420,14 +421,14 @@ impl RoutingTable {
// Shortcut function to add a node to our routing table if it doesn't exist
// and add the dial info we have for it, since that's pretty common
pub fn register_node_with_dial_info(
pub fn register_node_with_node_info(
&self,
node_id: DHTKey,
dial_infos: &[DialInfo],
node_info: NodeInfo,
) -> Result<NodeRef, String> {
let nr = self.create_node_ref(node_id)?;
nr.operate(move |e| -> Result<(), String> {
e.update_dial_infos(dial_infos);
e.update_node_info(node_info);
Ok(())
})?;
@@ -451,21 +452,6 @@ impl RoutingTable {
Ok(nr)
}
// Add a node if it doesn't exist, or update a single dial info on an already registered node
pub fn update_node_with_single_dial_info(
&self,
node_id: DHTKey,
dial_info: &DialInfo,
) -> Result<NodeRef, String> {
let nr = self.create_node_ref(node_id)?;
nr.operate(move |e| -> Result<(), String> {
e.update_single_dial_info(dial_info);
Ok(())
})?;
Ok(nr)
}
fn operate_on_bucket_entry<T, F>(&self, node_id: DHTKey, f: F) -> T
where
F: FnOnce(&mut BucketEntry) -> T,
@@ -487,9 +473,9 @@ impl RoutingTable {
// Ensure it's not dead
if !matches!(entry.state(cur_ts), BucketEntryState::Dead) {
// Ensure we have a node info
if let Some(node_info) = &entry.peer_stats().node_info {
// Ensure network class can relay
if node_info.network_class.can_inbound_relay() {
if let Some(node_status) = &entry.peer_stats().status {
// Ensure the node will relay
if node_status.will_relay {
if let Some(best_inbound_relay) = best_inbound_relay.as_mut() {
if best_inbound_relay.operate(|best| {
BucketEntry::cmp_fastest_reliable(cur_ts, best, entry)
@@ -547,12 +533,12 @@ impl RoutingTable {
// register the node if it's new
let nr = self
.register_node_with_dial_info(p.node_id.key, &p.dial_infos)
.register_node_with_node_info(p.node_id.key, p.node_info.clone())
.map_err(map_to_string)
.map_err(logthru_rtab!(
"couldn't register node {} at {:?}",
p.node_id.key,
&p.dial_infos
&p.node_info
))?;
out.push(nr);
}
@@ -617,10 +603,17 @@ impl RoutingTable {
// Run all bootstrap operations concurrently
let mut unord = FuturesUnordered::new();
for (k, v) in bsmap {
let nr = self
.register_node_with_dial_info(k, &v)
.map_err(logthru_rtab!("Couldn't add bootstrap node: {}", k))?;
log_rtab!(" bootstrapping {} with {:?}", k.encode(), &v);
let nr = self
.register_node_with_node_info(
k,
NodeInfo {
network_class: NetworkClass::Server,
dial_infos: v,
relay_dial_infos: Default::default(),
},
)
.map_err(logthru_rtab!("Couldn't add bootstrap node: {}", k))?;
unord.push(self.reverse_find_node(nr, true));
}
while unord.next().await.is_some() {}

View File

@@ -46,6 +46,14 @@ impl NodeRef {
self.routing_table.operate_on_bucket_entry(self.node_id, f)
}
pub fn node_info(&self) -> NodeInfo {
self.operate(|e| e.node_info().clone())
}
pub fn has_dial_info(&self) -> bool {
self.operate(|e| !e.node_info().dial_infos.is_empty())
}
// Returns if this node has seen and acknowledged our node's dial info yet
pub fn has_seen_our_dial_info(&self) -> bool {
self.operate(|e| e.has_seen_our_dial_info())
@@ -54,12 +62,12 @@ impl NodeRef {
self.operate(|e| e.set_seen_our_dial_info(true));
}
// Returns the best dial info to attempt a connection to this node
pub fn best_dial_info(&self) -> Option<DialInfo> {
// Returns the best node info to attempt a connection to this node
pub fn best_node_info(&self) -> Option<NodeInfo> {
let nm = self.routing_table.network_manager();
let protocol_config = nm.get_protocol_config()?;
self.operate(|e| {
e.first_filtered_dial_info(|di| {
e.first_filtered_node_info(|di| {
// Does it match the dial info filter
if !di.matches_filter(&self.dial_info_filter) {
return false;