Skip to content

Commit 6ca82e9

Browse files
committed
feat: [#1128] add new metric UDP total requests aborted
1 parent 9499fd8 commit 6ca82e9

File tree

7 files changed

+77
-27
lines changed

7 files changed

+77
-27
lines changed

src/core/services/statistics/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ pub async fn get_metrics(tracker: Arc<Tracker>) -> TrackerMetrics {
6767
TrackerMetrics {
6868
torrents_metrics,
6969
protocol_metrics: Metrics {
70+
// TCP
7071
tcp4_connections_handled: stats.tcp4_connections_handled,
7172
tcp4_announces_handled: stats.tcp4_announces_handled,
7273
tcp4_scrapes_handled: stats.tcp4_scrapes_handled,
7374
tcp6_connections_handled: stats.tcp6_connections_handled,
7475
tcp6_announces_handled: stats.tcp6_announces_handled,
7576
tcp6_scrapes_handled: stats.tcp6_scrapes_handled,
77+
// UDP
78+
udp_requests_aborted: stats.udp_requests_aborted,
7679
udp4_requests: stats.udp4_requests,
7780
udp4_connections_handled: stats.udp4_connections_handled,
7881
udp4_announces_handled: stats.udp4_announces_handled,

src/core/statistics.rs

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub enum Event {
4444
Tcp4Scrape,
4545
Tcp6Announce,
4646
Tcp6Scrape,
47+
Udp4RequestAborted,
4748
Udp4Request,
4849
Udp4Connect,
4950
Udp4Announce,
@@ -84,6 +85,9 @@ pub struct Metrics {
8485
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
8586
pub tcp6_scrapes_handled: u64,
8687

88+
/// Total number of UDP (UDP tracker) requests aborted.
89+
pub udp_requests_aborted: u64,
90+
8791
/// Total number of UDP (UDP tracker) requests from IPv4 peers.
8892
pub udp4_requests: u64,
8993
/// Total number of UDP (UDP tracker) connections from IPv4 peers.
@@ -179,6 +183,11 @@ async fn event_handler(event: Event, stats_repository: &Repo) {
179183
stats_repository.increase_tcp6_connections().await;
180184
}
181185

186+
// UDP
187+
Event::Udp4RequestAborted => {
188+
stats_repository.increase_udp_requests_aborted().await;
189+
}
190+
182191
// UDP4
183192
Event::Udp4Request => {
184193
stats_repository.increase_udp4_requests().await;
@@ -303,6 +312,12 @@ impl Repo {
303312
drop(stats_lock);
304313
}
305314

315+
pub async fn increase_udp_requests_aborted(&self) {
316+
let mut stats_lock = self.stats.write().await;
317+
stats_lock.udp_requests_aborted += 1;
318+
drop(stats_lock);
319+
}
320+
306321
pub async fn increase_udp4_requests(&self) {
307322
let mut stats_lock = self.stats.write().await;
308323
stats_lock.udp4_requests += 1;

src/servers/apis/v1/context/stats/resources.rs

+36-24
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pub struct Stats {
3434
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
3535
pub tcp6_scrapes_handled: u64,
3636

37+
/// Total number of UDP (UDP tracker) requests aborted.
38+
pub udp_requests_aborted: u64,
39+
3740
/// Total number of UDP (UDP tracker) requests from IPv4 peers.
3841
pub udp4_requests: u64,
3942
/// Total number of UDP (UDP tracker) connections from IPv4 peers.
@@ -68,12 +71,15 @@ impl From<TrackerMetrics> for Stats {
6871
seeders: metrics.torrents_metrics.complete,
6972
completed: metrics.torrents_metrics.downloaded,
7073
leechers: metrics.torrents_metrics.incomplete,
74+
// TCP
7175
tcp4_connections_handled: metrics.protocol_metrics.tcp4_connections_handled,
7276
tcp4_announces_handled: metrics.protocol_metrics.tcp4_announces_handled,
7377
tcp4_scrapes_handled: metrics.protocol_metrics.tcp4_scrapes_handled,
7478
tcp6_connections_handled: metrics.protocol_metrics.tcp6_connections_handled,
7579
tcp6_announces_handled: metrics.protocol_metrics.tcp6_announces_handled,
7680
tcp6_scrapes_handled: metrics.protocol_metrics.tcp6_scrapes_handled,
81+
// UDP
82+
udp_requests_aborted: metrics.protocol_metrics.udp_requests_aborted,
7783
udp4_requests: metrics.protocol_metrics.udp4_requests,
7884
udp4_connections_handled: metrics.protocol_metrics.udp4_connections_handled,
7985
udp4_announces_handled: metrics.protocol_metrics.udp4_announces_handled,
@@ -109,49 +115,55 @@ mod tests {
109115
torrents: 4
110116
},
111117
protocol_metrics: Metrics {
118+
// TCP
112119
tcp4_connections_handled: 5,
113120
tcp4_announces_handled: 6,
114121
tcp4_scrapes_handled: 7,
115122
tcp6_connections_handled: 8,
116123
tcp6_announces_handled: 9,
117124
tcp6_scrapes_handled: 10,
118-
udp4_requests: 11,
119-
udp4_connections_handled: 12,
120-
udp4_announces_handled: 13,
121-
udp4_scrapes_handled: 14,
122-
udp4_responses: 15,
123-
udp4_errors_handled: 16,
124-
udp6_requests: 17,
125-
udp6_connections_handled: 18,
126-
udp6_announces_handled: 19,
127-
udp6_scrapes_handled: 20,
128-
udp6_responses: 21,
129-
udp6_errors_handled: 22
125+
// UDP
126+
udp_requests_aborted: 11,
127+
udp4_requests: 12,
128+
udp4_connections_handled: 13,
129+
udp4_announces_handled: 14,
130+
udp4_scrapes_handled: 15,
131+
udp4_responses: 16,
132+
udp4_errors_handled: 17,
133+
udp6_requests: 18,
134+
udp6_connections_handled: 19,
135+
udp6_announces_handled: 20,
136+
udp6_scrapes_handled: 21,
137+
udp6_responses: 22,
138+
udp6_errors_handled: 23
130139
}
131140
}),
132141
Stats {
133142
torrents: 4,
134143
seeders: 1,
135144
completed: 2,
136145
leechers: 3,
146+
// TCP
137147
tcp4_connections_handled: 5,
138148
tcp4_announces_handled: 6,
139149
tcp4_scrapes_handled: 7,
140150
tcp6_connections_handled: 8,
141151
tcp6_announces_handled: 9,
142152
tcp6_scrapes_handled: 10,
143-
udp4_requests: 11,
144-
udp4_connections_handled: 12,
145-
udp4_announces_handled: 13,
146-
udp4_scrapes_handled: 14,
147-
udp4_responses: 15,
148-
udp4_errors_handled: 16,
149-
udp6_requests: 17,
150-
udp6_connections_handled: 18,
151-
udp6_announces_handled: 19,
152-
udp6_scrapes_handled: 20,
153-
udp6_responses: 21,
154-
udp6_errors_handled: 22
153+
// UDP
154+
udp_requests_aborted: 11,
155+
udp4_requests: 12,
156+
udp4_connections_handled: 13,
157+
udp4_announces_handled: 14,
158+
udp4_scrapes_handled: 15,
159+
udp4_responses: 16,
160+
udp4_errors_handled: 17,
161+
udp6_requests: 18,
162+
udp6_connections_handled: 19,
163+
udp6_announces_handled: 20,
164+
udp6_scrapes_handled: 21,
165+
udp6_responses: 22,
166+
udp6_errors_handled: 23
155167
}
156168
);
157169
}

src/servers/apis/v1/context/stats/responses.rs

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
4747
tracker_metrics.protocol_metrics.tcp6_scrapes_handled
4848
));
4949

50+
lines.push(format!(
51+
"udp_requests_aborted {}",
52+
tracker_metrics.protocol_metrics.udp_requests_aborted
53+
));
54+
5055
lines.push(format!("udp4_requests {}", tracker_metrics.protocol_metrics.udp4_requests));
5156
lines.push(format!(
5257
"udp4_connections_handled {}",

src/servers/udp/server/launcher.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ impl Launcher {
166166
continue;
167167
}
168168

169-
active_requests.force_push(abort_handle, &local_addr).await;
169+
let old_request_aborted = active_requests.force_push(abort_handle, &local_addr).await;
170+
171+
if old_request_aborted {
172+
// Evicted task from active requests buffer was aborted.
173+
tracker.send_stats_event(statistics::Event::Udp4RequestAborted).await;
174+
}
170175
} else {
171176
tokio::task::yield_now().await;
172177

src/servers/udp/server/request_buffer.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ impl ActiveRequests {
4141
/// 1. Removing finished tasks.
4242
/// 2. Removing the oldest unfinished task if no finished tasks are found.
4343
///
44+
/// Returns `true` if a task was removed, `false` otherwise.
45+
///
4446
/// # Panics
4547
///
4648
/// This method will panic if it cannot make space for adding a new handle.
@@ -49,17 +51,19 @@ impl ActiveRequests {
4951
///
5052
/// * `abort_handle` - The `AbortHandle` for the UDP request processor task.
5153
/// * `local_addr` - A string slice representing the local address for logging.
52-
pub async fn force_push(&mut self, new_task: AbortHandle, local_addr: &str) {
54+
pub async fn force_push(&mut self, new_task: AbortHandle, local_addr: &str) -> bool {
5355
// Attempt to add the new handle to the buffer.
5456
match self.rb.try_push(new_task) {
5557
Ok(()) => {
5658
// Successfully added the task, no further action needed.
59+
false
5760
}
5861
Err(new_task) => {
5962
// Buffer is full, attempt to make space.
6063

6164
let mut finished: u64 = 0;
6265
let mut unfinished_task = None;
66+
let mut old_task_aborted = false;
6367

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

100105
tracing::warn!(
101106
target: UDP_TRACKER_LOG_TARGET,
@@ -134,7 +139,9 @@ impl ActiveRequests {
134139
if !new_task.is_finished() {
135140
self.rb.try_push(new_task).expect("it should have space for this new task.");
136141
}
142+
143+
old_task_aborted
137144
}
138-
};
145+
}
139146
}
140147
}

tests/servers/api/v1/contract/context/stats.rs

+3
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ async fn should_allow_getting_tracker_statistics() {
3434
seeders: 1,
3535
completed: 0,
3636
leechers: 0,
37+
// TCP
3738
tcp4_connections_handled: 0,
3839
tcp4_announces_handled: 0,
3940
tcp4_scrapes_handled: 0,
4041
tcp6_connections_handled: 0,
4142
tcp6_announces_handled: 0,
4243
tcp6_scrapes_handled: 0,
44+
// UDP
45+
udp_requests_aborted: 0,
4346
udp4_requests: 0,
4447
udp4_connections_handled: 0,
4548
udp4_announces_handled: 0,

0 commit comments

Comments
 (0)