Skip to content

Commit

Permalink
feat: [#1128] add new metric UDP total requests aborted
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Dec 13, 2024
1 parent 9499fd8 commit 6ca82e9
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 27 deletions.
3 changes: 3 additions & 0 deletions src/core/services/statistics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ pub async fn get_metrics(tracker: Arc<Tracker>) -> TrackerMetrics {
TrackerMetrics {
torrents_metrics,
protocol_metrics: Metrics {
// TCP
tcp4_connections_handled: stats.tcp4_connections_handled,
tcp4_announces_handled: stats.tcp4_announces_handled,
tcp4_scrapes_handled: stats.tcp4_scrapes_handled,
tcp6_connections_handled: stats.tcp6_connections_handled,
tcp6_announces_handled: stats.tcp6_announces_handled,
tcp6_scrapes_handled: stats.tcp6_scrapes_handled,
// UDP
udp_requests_aborted: stats.udp_requests_aborted,
udp4_requests: stats.udp4_requests,
udp4_connections_handled: stats.udp4_connections_handled,
udp4_announces_handled: stats.udp4_announces_handled,
Expand Down
15 changes: 15 additions & 0 deletions src/core/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub enum Event {
Tcp4Scrape,
Tcp6Announce,
Tcp6Scrape,
Udp4RequestAborted,
Udp4Request,
Udp4Connect,
Udp4Announce,
Expand Down Expand Up @@ -84,6 +85,9 @@ pub struct Metrics {
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
pub tcp6_scrapes_handled: u64,

/// Total number of UDP (UDP tracker) requests aborted.
pub udp_requests_aborted: u64,

/// Total number of UDP (UDP tracker) requests from IPv4 peers.
pub udp4_requests: u64,
/// Total number of UDP (UDP tracker) connections from IPv4 peers.
Expand Down Expand Up @@ -179,6 +183,11 @@ async fn event_handler(event: Event, stats_repository: &Repo) {
stats_repository.increase_tcp6_connections().await;
}

// UDP
Event::Udp4RequestAborted => {
stats_repository.increase_udp_requests_aborted().await;
}

// UDP4
Event::Udp4Request => {
stats_repository.increase_udp4_requests().await;
Expand Down Expand Up @@ -303,6 +312,12 @@ impl Repo {
drop(stats_lock);
}

pub async fn increase_udp_requests_aborted(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp_requests_aborted += 1;
drop(stats_lock);
}

pub async fn increase_udp4_requests(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp4_requests += 1;
Expand Down
60 changes: 36 additions & 24 deletions src/servers/apis/v1/context/stats/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub struct Stats {
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
pub tcp6_scrapes_handled: u64,

/// Total number of UDP (UDP tracker) requests aborted.
pub udp_requests_aborted: u64,

/// Total number of UDP (UDP tracker) requests from IPv4 peers.
pub udp4_requests: u64,
/// Total number of UDP (UDP tracker) connections from IPv4 peers.
Expand Down Expand Up @@ -68,12 +71,15 @@ impl From<TrackerMetrics> for Stats {
seeders: metrics.torrents_metrics.complete,
completed: metrics.torrents_metrics.downloaded,
leechers: metrics.torrents_metrics.incomplete,
// TCP
tcp4_connections_handled: metrics.protocol_metrics.tcp4_connections_handled,
tcp4_announces_handled: metrics.protocol_metrics.tcp4_announces_handled,
tcp4_scrapes_handled: metrics.protocol_metrics.tcp4_scrapes_handled,
tcp6_connections_handled: metrics.protocol_metrics.tcp6_connections_handled,
tcp6_announces_handled: metrics.protocol_metrics.tcp6_announces_handled,
tcp6_scrapes_handled: metrics.protocol_metrics.tcp6_scrapes_handled,
// UDP
udp_requests_aborted: metrics.protocol_metrics.udp_requests_aborted,
udp4_requests: metrics.protocol_metrics.udp4_requests,
udp4_connections_handled: metrics.protocol_metrics.udp4_connections_handled,
udp4_announces_handled: metrics.protocol_metrics.udp4_announces_handled,
Expand Down Expand Up @@ -109,49 +115,55 @@ mod tests {
torrents: 4
},
protocol_metrics: Metrics {
// TCP
tcp4_connections_handled: 5,
tcp4_announces_handled: 6,
tcp4_scrapes_handled: 7,
tcp6_connections_handled: 8,
tcp6_announces_handled: 9,
tcp6_scrapes_handled: 10,
udp4_requests: 11,
udp4_connections_handled: 12,
udp4_announces_handled: 13,
udp4_scrapes_handled: 14,
udp4_responses: 15,
udp4_errors_handled: 16,
udp6_requests: 17,
udp6_connections_handled: 18,
udp6_announces_handled: 19,
udp6_scrapes_handled: 20,
udp6_responses: 21,
udp6_errors_handled: 22
// UDP
udp_requests_aborted: 11,
udp4_requests: 12,
udp4_connections_handled: 13,
udp4_announces_handled: 14,
udp4_scrapes_handled: 15,
udp4_responses: 16,
udp4_errors_handled: 17,
udp6_requests: 18,
udp6_connections_handled: 19,
udp6_announces_handled: 20,
udp6_scrapes_handled: 21,
udp6_responses: 22,
udp6_errors_handled: 23
}
}),
Stats {
torrents: 4,
seeders: 1,
completed: 2,
leechers: 3,
// TCP
tcp4_connections_handled: 5,
tcp4_announces_handled: 6,
tcp4_scrapes_handled: 7,
tcp6_connections_handled: 8,
tcp6_announces_handled: 9,
tcp6_scrapes_handled: 10,
udp4_requests: 11,
udp4_connections_handled: 12,
udp4_announces_handled: 13,
udp4_scrapes_handled: 14,
udp4_responses: 15,
udp4_errors_handled: 16,
udp6_requests: 17,
udp6_connections_handled: 18,
udp6_announces_handled: 19,
udp6_scrapes_handled: 20,
udp6_responses: 21,
udp6_errors_handled: 22
// UDP
udp_requests_aborted: 11,
udp4_requests: 12,
udp4_connections_handled: 13,
udp4_announces_handled: 14,
udp4_scrapes_handled: 15,
udp4_responses: 16,
udp4_errors_handled: 17,
udp6_requests: 18,
udp6_connections_handled: 19,
udp6_announces_handled: 20,
udp6_scrapes_handled: 21,
udp6_responses: 22,
udp6_errors_handled: 23
}
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/servers/apis/v1/context/stats/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
tracker_metrics.protocol_metrics.tcp6_scrapes_handled
));

lines.push(format!(
"udp_requests_aborted {}",
tracker_metrics.protocol_metrics.udp_requests_aborted
));

lines.push(format!("udp4_requests {}", tracker_metrics.protocol_metrics.udp4_requests));
lines.push(format!(
"udp4_connections_handled {}",
Expand Down
7 changes: 6 additions & 1 deletion src/servers/udp/server/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ impl Launcher {
continue;
}

active_requests.force_push(abort_handle, &local_addr).await;
let old_request_aborted = active_requests.force_push(abort_handle, &local_addr).await;

if old_request_aborted {
// Evicted task from active requests buffer was aborted.
tracker.send_stats_event(statistics::Event::Udp4RequestAborted).await;
}
} else {
tokio::task::yield_now().await;

Expand Down
11 changes: 9 additions & 2 deletions src/servers/udp/server/request_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ impl ActiveRequests {
/// 1. Removing finished tasks.
/// 2. Removing the oldest unfinished task if no finished tasks are found.
///
/// Returns `true` if a task was removed, `false` otherwise.
///
/// # Panics
///
/// This method will panic if it cannot make space for adding a new handle.
Expand All @@ -49,17 +51,19 @@ impl ActiveRequests {
///
/// * `abort_handle` - The `AbortHandle` for the UDP request processor task.
/// * `local_addr` - A string slice representing the local address for logging.
pub async fn force_push(&mut self, new_task: AbortHandle, local_addr: &str) {
pub async fn force_push(&mut self, new_task: AbortHandle, local_addr: &str) -> bool {
// Attempt to add the new handle to the buffer.
match self.rb.try_push(new_task) {
Ok(()) => {
// Successfully added the task, no further action needed.
false
}
Err(new_task) => {
// Buffer is full, attempt to make space.

let mut finished: u64 = 0;
let mut unfinished_task = None;
let mut old_task_aborted = false;

for old_task in self.rb.pop_iter() {
// We found a finished tasks ... increase the counter and
Expand Down Expand Up @@ -96,6 +100,7 @@ impl ActiveRequests {
if finished == 0 {
// We make place aborting this task.
old_task.abort();
old_task_aborted = true;

tracing::warn!(
target: UDP_TRACKER_LOG_TARGET,
Expand Down Expand Up @@ -134,7 +139,9 @@ impl ActiveRequests {
if !new_task.is_finished() {
self.rb.try_push(new_task).expect("it should have space for this new task.");
}

old_task_aborted
}
};
}
}
}
3 changes: 3 additions & 0 deletions tests/servers/api/v1/contract/context/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ async fn should_allow_getting_tracker_statistics() {
seeders: 1,
completed: 0,
leechers: 0,
// TCP
tcp4_connections_handled: 0,
tcp4_announces_handled: 0,
tcp4_scrapes_handled: 0,
tcp6_connections_handled: 0,
tcp6_announces_handled: 0,
tcp6_scrapes_handled: 0,
// UDP
udp_requests_aborted: 0,
udp4_requests: 0,
udp4_connections_handled: 0,
udp4_announces_handled: 0,
Expand Down

0 comments on commit 6ca82e9

Please sign in to comment.