mirror of
https://gitlab.com/veilid/veilid.git
synced 2025-04-19 15:25:54 -04:00
[ci skip] pick random relay from percentile rather than always the fastest
This commit is contained in:
parent
867ea95974
commit
792e05a187
@ -43,7 +43,9 @@ pub const RELAY_MANAGEMENT_INTERVAL_SECS: u32 = 1;
|
||||
/// How frequently we optimize relays
|
||||
pub const RELAY_OPTIMIZATION_INTERVAL_SECS: u32 = 10;
|
||||
/// What percentile to keep our relays optimized to
|
||||
pub const RELAY_OPTIMIZATION_PERCENTILE: f32 = 75.0;
|
||||
pub const RELAY_OPTIMIZATION_PERCENTILE: f32 = 66.0;
|
||||
/// What percentile to choose our relays from (must be greater than RELAY_OPTIMIZATION_PERCENTILE)
|
||||
pub const RELAY_SELECTION_PERCENTILE: f32 = 85.0;
|
||||
|
||||
/// How frequently we tick the private route management routine
|
||||
pub const PRIVATE_ROUTE_MANAGEMENT_INTERVAL_SECS: u32 = 1;
|
||||
@ -1146,6 +1148,18 @@ impl RoutingTable {
|
||||
inner.find_fastest_node(cur_ts, filter, metric)
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self, filter, metric), ret)]
|
||||
pub fn find_random_fast_node(
|
||||
&self,
|
||||
cur_ts: Timestamp,
|
||||
filter: impl Fn(&BucketEntryInner) -> bool,
|
||||
percentile: f32,
|
||||
metric: impl Fn(&LatencyStats) -> TimestampDuration,
|
||||
) -> Option<NodeRef> {
|
||||
let inner = self.inner.read();
|
||||
inner.find_random_fast_node(cur_ts, filter, percentile, metric)
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self, filter, metric), ret)]
|
||||
pub fn get_node_speed_percentile(
|
||||
&self,
|
||||
|
@ -1445,6 +1445,54 @@ impl RoutingTableInner {
|
||||
fastest_node.map(|e| NodeRef::new(self.registry(), e))
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self, filter, metric), ret)]
|
||||
pub fn find_random_fast_node(
|
||||
&self,
|
||||
cur_ts: Timestamp,
|
||||
filter: impl Fn(&BucketEntryInner) -> bool,
|
||||
percentile: f32,
|
||||
metric: impl Fn(&LatencyStats) -> TimestampDuration,
|
||||
) -> Option<NodeRef> {
|
||||
// Go through all entries and find all entries that matches filter function
|
||||
let mut all_filtered_nodes: Vec<Arc<BucketEntry>> = Vec::new();
|
||||
|
||||
// Iterate all known nodes for candidates
|
||||
self.with_entries(cur_ts, BucketEntryState::Unreliable, |rti, entry| {
|
||||
let entry2 = entry.clone();
|
||||
entry.with(rti, |_rti, e| {
|
||||
// Filter this node
|
||||
if filter(e) {
|
||||
all_filtered_nodes.push(entry2);
|
||||
}
|
||||
});
|
||||
// Don't end early, iterate through all entries
|
||||
Option::<()>::None
|
||||
});
|
||||
|
||||
// Sort by fastest tm90 reliable
|
||||
all_filtered_nodes.sort_by(|a, b| {
|
||||
a.with(self, |rti, ea| {
|
||||
b.with(rti, |_rti, eb| {
|
||||
BucketEntryInner::cmp_fastest_reliable(cur_ts, ea, eb, &metric)
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
if all_filtered_nodes.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let max_index =
|
||||
(((all_filtered_nodes.len() - 1) as f32) * (100.0 - percentile) / 100.0) as u32;
|
||||
let chosen_index = (get_random_u32() % (max_index + 1)) as usize;
|
||||
|
||||
// Return the chosen node node
|
||||
Some(NodeRef::new(
|
||||
self.registry(),
|
||||
all_filtered_nodes[chosen_index].clone(),
|
||||
))
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self, filter, metric), ret)]
|
||||
pub fn get_node_relative_performance(
|
||||
&self,
|
||||
|
@ -202,7 +202,12 @@ impl RoutingTable {
|
||||
}
|
||||
if !got_outbound_relay {
|
||||
// Find a node in our routing table that is an acceptable inbound relay
|
||||
if let Some(nr) = self.find_fastest_node(cur_ts, &relay_node_filter, |ls| ls.tm90) {
|
||||
if let Some(nr) = self.find_random_fast_node(
|
||||
cur_ts,
|
||||
&relay_node_filter,
|
||||
RELAY_SELECTION_PERCENTILE,
|
||||
|ls| ls.tm90,
|
||||
) {
|
||||
veilid_log!(self debug "Inbound relay node selected: {}", nr);
|
||||
editor.set_relay_node(Some(nr));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user