use std::sync::Arc;
use std::sync::atomic::{AtomicI32, AtomicUsize, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use rand::Rng;
use tracing::trace;
use crate::selector::server_stat_man::ServerStatMan;
use crate::selector::uri_selector::UriSelector;
const MAX_TIMEOUT_SECS: u64 = 60;
const LOW_LOWEST_SPEED_LIMIT: u64 = 4096;
const MIN_TESTED_SERVERS: usize = 3;
const MAX_RETEST_COUNTER: u32 = 8;
const BEST_MIRROR_RANGE_PCT: f64 = 0.25;
#[cfg(test)]
fn extract_host(uri: &str) -> Option<String> {
crate::selector::feedback_uri_selector::extract_host_and_protocol(uri).map(|(h, _)| h)
}
fn extract_host_and_protocol(uri: &str) -> Option<(String, String)> {
crate::selector::feedback_uri_selector::extract_host_and_protocol(uri)
}
fn extract_hosts(uris: &[String]) -> Vec<(usize, String, String)> {
uris.iter()
.enumerate()
.filter_map(|(i, u)| extract_host_and_protocol(u).map(|(h, p)| (i, h, p)))
.collect()
}
pub struct AdaptiveUriSelector {
stat_man: Arc<ServerStatMan>,
uris: Vec<String>,
nb_server_to_evaluate: AtomicI32,
nb_connections: AtomicI32,
timeout_secs: AtomicUsize,
}
impl AdaptiveUriSelector {
pub fn new(stat_man: Arc<ServerStatMan>) -> Self {
Self {
stat_man,
uris: Vec::new(),
nb_server_to_evaluate: AtomicI32::new(
crate::constants::DEFAULT_NB_SERVER_TO_EVALUATE as i32,
),
nb_connections: AtomicI32::new(crate::constants::DEFAULT_NB_CONNECTIONS as i32),
timeout_secs: AtomicUsize::new(0),
}
}
pub fn new_with_uris(stat_man: Arc<ServerStatMan>, uris: Vec<String>) -> Self {
Self {
stat_man,
uris,
nb_server_to_evaluate: AtomicI32::new(
crate::constants::DEFAULT_NB_SERVER_TO_EVALUATE as i32,
),
nb_connections: AtomicI32::new(crate::constants::DEFAULT_NB_CONNECTIONS as i32),
timeout_secs: AtomicUsize::new(0),
}
}
pub fn set_uris(&mut self, uris: Vec<String>) {
self.uris = uris;
}
pub fn get_uris(&self) -> &[String] {
&self.uris
}
pub fn set_nb_connections(&self, n: i32) {
self.nb_connections.store(n, Ordering::Relaxed);
}
pub fn set_nb_evaluate(&self, n: i32) {
self.nb_server_to_evaluate.store(n, Ordering::Relaxed);
}
pub fn set_timeout_secs(&self, secs: usize) {
self.timeout_secs.store(secs, Ordering::Relaxed);
}
pub fn stat_man(&self) -> &Arc<ServerStatMan> {
&self.stat_man
}
fn select_one(&self, uris: &[String], used_hosts: &[(usize, String)]) -> Option<usize> {
if uris.is_empty() {
return None;
}
let nb_conn = self.nb_connections.fetch_add(1, Ordering::Relaxed);
if uris.len() == 1 {
return Some(0);
}
let hosts = extract_hosts(uris);
if hosts.is_empty() {
return Some(0);
}
let nb_tested = self.get_nb_tested_servers(&hosts);
if nb_tested < MIN_TESTED_SERVERS
&& let Some(idx) = self.get_first_not_tested(&hosts)
{
trace!(idx, "AdaptiveURISelector: choosing first non-tested mirror");
self.nb_server_to_evaluate.fetch_sub(1, Ordering::Relaxed);
return Some(idx);
}
let nb_eval = self.nb_server_to_evaluate.load(Ordering::Relaxed);
if nb_eval > 0 && nb_conn > 1 {
self.nb_server_to_evaluate.fetch_sub(1, Ordering::Relaxed);
if let Some(idx) = self.get_first_not_tested(&hosts) {
trace!(
idx,
nb_conn, "AdaptiveURISelector: choosing non-tested mirror"
);
return Some(idx);
}
if let Some(idx) = self.get_first_to_test_uri(&hosts) {
trace!(idx, nb_conn, "AdaptiveURISelector: choosing re-test mirror");
return Some(idx);
}
return self.get_best_mirror(&hosts, used_hosts);
}
self.get_best_mirror(&hosts, used_hosts)
}
fn get_first_not_tested(&self, hosts: &[(usize, String, String)]) -> Option<usize> {
for (idx, host, protocol) in hosts {
if self
.stat_man
.find_stat_by_protocol(host, protocol)
.is_none()
{
return Some(*idx);
}
}
None
}
fn get_first_to_test_uri(&self, hosts: &[(usize, String, String)]) -> Option<usize> {
let now_secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
for (idx, host, protocol) in hosts {
let stat = match self.stat_man.find_stat_by_protocol(host, protocol) {
Some(s) => s,
None => continue,
};
let counter = stat.get_counter();
if counter > MAX_RETEST_COUNTER {
continue;
}
let retest_hours = 1u64.checked_shl(counter).unwrap_or(u64::MAX) * 24;
let last_updated = stat.get_last_updated();
if last_updated > 0 && now_secs.saturating_sub(last_updated) > retest_hours * 3600 {
return Some(*idx);
}
}
None
}
fn get_nb_tested_servers(&self, hosts: &[(usize, String, String)]) -> usize {
hosts
.iter()
.filter(|(_, host, protocol)| {
self.stat_man
.find_stat_by_protocol(host, protocol)
.is_some()
})
.count()
}
fn get_best_mirror(
&self,
hosts: &[(usize, String, String)],
used_hosts: &[(usize, String)],
) -> Option<usize> {
let max_speed = self.get_max_download_speed(hosts);
let min_speed = (max_speed as f64 * (1.0 - BEST_MIRROR_RANGE_PCT)) as u64;
let mut bests = self.get_uris_by_speed(hosts, min_speed);
let used_set: std::collections::HashSet<&str> =
used_hosts.iter().map(|(_, h)| h.as_str()).collect();
bests.retain(|idx| {
hosts
.get(*idx)
.is_some_and(|(_, h, _)| !used_set.contains(h.as_str()))
});
if bests.is_empty() {
bests = self.get_uris_by_speed(hosts, 0);
bests.retain(|idx| {
hosts
.get(*idx)
.is_some_and(|(_, h, _)| !used_set.contains(h.as_str()))
});
}
if bests.is_empty() {
bests = self.get_uris_by_speed(hosts, 0);
}
if bests.len() == 1 {
Some(bests[0])
} else if bests.len() < 2 {
self.get_max_download_speed_uri(hosts)
} else {
let idx = self.select_random_uri(&bests);
Some(idx)
}
}
fn get_max_download_speed(&self, hosts: &[(usize, String, String)]) -> u64 {
hosts.iter().fold(0u64, |max, (_, host, protocol)| {
if let Some(stat) = self.stat_man.find_stat_by_protocol(host, protocol) {
let speed = stat.get_single_avg_speed().max(stat.get_multi_avg_speed());
max.max(speed)
} else {
max
}
})
}
fn get_max_download_speed_uri(&self, hosts: &[(usize, String, String)]) -> Option<usize> {
let mut max_speed: i64 = -1;
let mut best_idx: Option<usize> = None;
for (idx, host, protocol) in hosts {
if let Some(stat) = self.stat_man.find_stat_by_protocol(host, protocol) {
let single = stat.get_single_avg_speed() as i64;
let multi = stat.get_multi_avg_speed() as i64;
if single > max_speed {
max_speed = single;
best_idx = Some(*idx);
}
if multi > max_speed {
max_speed = multi;
best_idx = Some(*idx);
}
}
}
best_idx
}
fn get_uris_by_speed(&self, hosts: &[(usize, String, String)], min: u64) -> Vec<usize> {
hosts
.iter()
.filter_map(|(idx, host, protocol)| {
let stat = self.stat_man.find_stat_by_protocol(host, protocol)?;
if stat.get_single_avg_speed() > min || stat.get_multi_avg_speed() > min {
Some(*idx)
} else {
None
}
})
.collect()
}
fn select_random_uri(&self, indices: &[usize]) -> usize {
let mut rng = rand::thread_rng();
let pos = rng.gen_range(0..indices.len());
indices[pos]
}
pub fn adjust_lowest_speed_limit(&self, uris: &[String], lowest_limit: u64) -> u64 {
if lowest_limit == 0 {
return 0;
}
let hosts = extract_hosts(uris);
let max = self.get_max_download_speed(&hosts);
if max > 0 && lowest_limit > max / 4 {
max / 4
} else if max == 0 && lowest_limit > LOW_LOWEST_SPEED_LIMIT {
LOW_LOWEST_SPEED_LIMIT
} else {
0
}
}
pub fn may_retry_with_increased_timeout(&self) -> Option<Vec<String>> {
let current = self.timeout_secs.load(Ordering::Relaxed) as u64;
let new_timeout = current * 2;
if new_timeout >= MAX_TIMEOUT_SECS {
return None;
}
self.timeout_secs
.store(new_timeout as usize, Ordering::Relaxed);
Some(Vec::new())
}
pub fn reset_counters(&self) {
self.nb_connections.store(1, Ordering::Relaxed);
for stat in self.stat_man.get_all_stats() {
stat.reset_counter();
}
}
pub fn report_success(&self, uri_idx: usize, speed: u64, is_multi: bool) {
if let Some(uri) = self.uris.get(uri_idx)
&& let Some((host, protocol)) = extract_host_and_protocol(uri)
{
self.stat_man
.update_with_protocol(&host, &protocol, speed, is_multi);
if let Some(stat) = self.stat_man.find_stat_by_protocol(&host, &protocol) {
stat.reset_status();
}
}
}
pub fn report_failure_with_code(&self, uri_idx: usize, error_code: u16) {
if let Some(uri) = self.uris.get(uri_idx)
&& let Some((host, protocol)) = extract_host_and_protocol(uri)
{
self.stat_man.get_or_create_with_protocol(&host, &protocol);
self.stat_man
.mark_failure_with_protocol(&host, &protocol, error_code);
}
}
pub fn report_failure_default(&self, uri_idx: usize) {
self.report_failure_with_code(uri_idx, 500);
}
}
impl UriSelector for AdaptiveUriSelector {
fn select(&self, uris: &[String], used_hosts: &[(usize, String)]) -> Option<usize> {
self.select_one(uris, used_hosts)
}
fn tune_command(&self, uris: &[String], _speed: u64) {
let limit = self.adjust_lowest_speed_limit(uris, 0);
if limit > 0 {
tracing::debug!("AdaptiveURISelector tuning lowest-speed-limit to {}", limit);
}
}
fn reset(&self) {
self.reset_counters();
}
fn report_failure(&mut self, uri_idx: usize) {
self.report_failure_with_code(uri_idx, 500);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::selector::server_stat_man::ServerStatMan;
include!("adaptive_uri_selector_tests.rs");
}