Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor packages: review UDP events #1328

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/axum-tracker-api-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ torrust-tracker-api-core = { version = "3.0.0-develop", path = "../tracker-api-c
torrust-tracker-clock = { version = "3.0.0-develop", path = "../clock" }
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
torrust-udp-tracker-server = { version = "3.0.0-develop", path = "../udp-tracker-server" }
tower = { version = "0", features = ["timeout"] }
tower-http = { version = "0", features = ["compression-full", "cors", "propagate-header", "request-id", "trace"] }
tracing = "0"
Expand Down
3 changes: 3 additions & 0 deletions packages/axum-tracker-api-server/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use torrust_tracker_api_client::connection_info::{ConnectionInfo, Origin};
use torrust_tracker_api_core::container::TrackerHttpApiCoreContainer;
use torrust_tracker_configuration::{logging, Configuration};
use torrust_tracker_primitives::peer;
use torrust_udp_tracker_server::container::UdpTrackerServerContainer;

use crate::server::{ApiServer, Launcher, Running, Stopped};

Expand Down Expand Up @@ -175,11 +176,13 @@ impl EnvContainer {
let http_tracker_core_container =
HttpTrackerCoreContainer::initialize_from(&tracker_core_container, &http_tracker_config);
let udp_tracker_core_container = UdpTrackerCoreContainer::initialize_from(&tracker_core_container, &udp_tracker_config);
let udp_tracker_server_container = UdpTrackerServerContainer::initialize(&core_config);

let tracker_http_api_core_container = TrackerHttpApiCoreContainer::initialize_from(
&tracker_core_container,
&http_tracker_core_container,
&udp_tracker_core_container,
&udp_tracker_server_container,
&http_api_config,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@ pub async fn get_stats_handler(
Arc<RwLock<BanService>>,
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
)>,
params: Query<QueryParams>,
) -> Response {
let metrics = get_metrics(state.0.clone(), state.1.clone(), state.2.clone(), state.3.clone()).await;
let metrics = get_metrics(
state.0.clone(),
state.1.clone(),
state.2.clone(),
state.3.clone(),
state.4.clone(),
)
.await;

match params.0.format {
Some(format) => match format {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApi
http_api_container.in_memory_torrent_repository.clone(),
http_api_container.ban_service.clone(),
http_api_container.http_stats_repository.clone(),
http_api_container.udp_stats_repository.clone(),
http_api_container.udp_core_stats_repository.clone(),
http_api_container.udp_server_stats_repository.clone(),
)),
)
}
1 change: 1 addition & 0 deletions packages/tracker-api-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bittorrent-udp-tracker-core = { version = "3.0.0-develop", path = "../udp-tracke
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
torrust-udp-tracker-server = { version = "3.0.0-develop", path = "../udp-tracker-server" }

[dev-dependencies]
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }
13 changes: 11 additions & 2 deletions packages/tracker-api-core/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bittorrent_udp_tracker_core::services::banning::BanService;
use bittorrent_udp_tracker_core::{self};
use tokio::sync::RwLock;
use torrust_tracker_configuration::{Core, HttpApi, HttpTracker, UdpTracker};
use torrust_udp_tracker_server::container::UdpTrackerServerContainer;

pub struct TrackerHttpApiCoreContainer {
// todo: replace with TrackerCoreContainer
Expand All @@ -23,7 +24,10 @@ pub struct TrackerHttpApiCoreContainer {

// todo: replace with UdpTrackerCoreContainer
pub ban_service: Arc<RwLock<BanService>>,
pub udp_stats_repository: Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,
pub udp_core_stats_repository: Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,

// todo: replace with UdpTrackerServerContainer
pub udp_server_stats_repository: Arc<torrust_udp_tracker_server::statistics::repository::Repository>,

pub http_api_config: Arc<HttpApi>,
}
Expand All @@ -39,11 +43,13 @@ impl TrackerHttpApiCoreContainer {
let tracker_core_container = Arc::new(TrackerCoreContainer::initialize(core_config));
let http_tracker_core_container = HttpTrackerCoreContainer::initialize_from(&tracker_core_container, http_tracker_config);
let udp_tracker_core_container = UdpTrackerCoreContainer::initialize_from(&tracker_core_container, udp_tracker_config);
let udp_tracker_server_container = UdpTrackerServerContainer::initialize(core_config);

Self::initialize_from(
&tracker_core_container,
&http_tracker_core_container,
&udp_tracker_core_container,
&udp_tracker_server_container,
http_api_config,
)
}
Expand All @@ -53,6 +59,7 @@ impl TrackerHttpApiCoreContainer {
tracker_core_container: &Arc<TrackerCoreContainer>,
http_tracker_core_container: &Arc<HttpTrackerCoreContainer>,
udp_tracker_core_container: &Arc<UdpTrackerCoreContainer>,
udp_tracker_server_container: &Arc<UdpTrackerServerContainer>,
http_api_config: &Arc<HttpApi>,
) -> Arc<TrackerHttpApiCoreContainer> {
Arc::new(TrackerHttpApiCoreContainer {
Expand All @@ -64,7 +71,9 @@ impl TrackerHttpApiCoreContainer {
http_stats_repository: http_tracker_core_container.http_stats_repository.clone(),

ban_service: udp_tracker_core_container.ban_service.clone(),
udp_stats_repository: udp_tracker_core_container.udp_stats_repository.clone(),
udp_core_stats_repository: udp_tracker_core_container.udp_core_stats_repository.clone(),

udp_server_stats_repository: udp_tracker_server_container.udp_server_stats_repository.clone(),

http_api_config: http_api_config.clone(),
})
Expand Down
53 changes: 31 additions & 22 deletions packages/tracker-api-core/src/statistics/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::sync::Arc;

use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
use bittorrent_udp_tracker_core::services::banning::BanService;
use bittorrent_udp_tracker_core::{self, statistics};
use bittorrent_udp_tracker_core::{self, statistics as udp_core_statistics};
use tokio::sync::RwLock;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_udp_tracker_server::statistics as udp_server_statistics;

use crate::statistics::metrics::Metrics;

Expand All @@ -27,12 +28,14 @@ pub async fn get_metrics(
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
ban_service: Arc<RwLock<BanService>>,
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
udp_stats_repository: Arc<statistics::repository::Repository>,
udp_core_stats_repository: Arc<udp_core_statistics::repository::Repository>,
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
) -> TrackerMetrics {
let torrents_metrics = in_memory_torrent_repository.get_torrents_metrics();
let udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
let http_stats = http_stats_repository.get_stats().await;
let udp_stats = udp_stats_repository.get_stats().await;
let udp_core_stats = udp_core_stats_repository.get_stats().await;
let udp_server_stats = udp_server_stats_repository.get_stats().await;

TrackerMetrics {
torrents_metrics,
Expand All @@ -46,26 +49,26 @@ pub async fn get_metrics(
tcp6_announces_handled: http_stats.tcp6_announces_handled,
tcp6_scrapes_handled: http_stats.tcp6_scrapes_handled,
// UDP
udp_requests_aborted: udp_stats.udp_requests_aborted,
udp_requests_banned: udp_stats.udp_requests_banned,
udp_requests_aborted: udp_server_stats.udp_requests_aborted,
udp_requests_banned: udp_server_stats.udp_requests_banned,
udp_banned_ips_total: udp_banned_ips_total as u64,
udp_avg_connect_processing_time_ns: udp_stats.udp_avg_connect_processing_time_ns,
udp_avg_announce_processing_time_ns: udp_stats.udp_avg_announce_processing_time_ns,
udp_avg_scrape_processing_time_ns: udp_stats.udp_avg_scrape_processing_time_ns,
udp_avg_connect_processing_time_ns: udp_server_stats.udp_avg_connect_processing_time_ns,
udp_avg_announce_processing_time_ns: udp_server_stats.udp_avg_announce_processing_time_ns,
udp_avg_scrape_processing_time_ns: udp_server_stats.udp_avg_scrape_processing_time_ns,
// UDPv4
udp4_requests: udp_stats.udp4_requests,
udp4_connections_handled: udp_stats.udp4_connections_handled,
udp4_announces_handled: udp_stats.udp4_announces_handled,
udp4_scrapes_handled: udp_stats.udp4_scrapes_handled,
udp4_responses: udp_stats.udp4_responses,
udp4_errors_handled: udp_stats.udp4_errors_handled,
udp4_requests: udp_server_stats.udp4_requests,
udp4_connections_handled: udp_core_stats.udp4_connections_handled,
udp4_announces_handled: udp_core_stats.udp4_announces_handled,
udp4_scrapes_handled: udp_core_stats.udp4_scrapes_handled,
udp4_responses: udp_server_stats.udp4_responses,
udp4_errors_handled: udp_server_stats.udp4_errors_handled,
// UDPv6
udp6_requests: udp_stats.udp6_requests,
udp6_connections_handled: udp_stats.udp6_connections_handled,
udp6_announces_handled: udp_stats.udp6_announces_handled,
udp6_scrapes_handled: udp_stats.udp6_scrapes_handled,
udp6_responses: udp_stats.udp6_responses,
udp6_errors_handled: udp_stats.udp6_errors_handled,
udp6_requests: udp_server_stats.udp6_requests,
udp6_connections_handled: udp_core_stats.udp6_connections_handled,
udp6_announces_handled: udp_core_stats.udp6_announces_handled,
udp6_scrapes_handled: udp_core_stats.udp6_scrapes_handled,
udp6_responses: udp_server_stats.udp6_responses,
udp6_errors_handled: udp_server_stats.udp6_errors_handled,
},
}
}
Expand Down Expand Up @@ -97,21 +100,27 @@ mod tests {
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));

// HTTP stats
// HTTP core stats
let (_http_stats_event_sender, http_stats_repository) =
bittorrent_http_tracker_core::statistics::setup::factory(config.core.tracker_usage_statistics);
let http_stats_repository = Arc::new(http_stats_repository);

// UDP stats
// UDP core stats
let (_udp_stats_event_sender, udp_stats_repository) =
bittorrent_udp_tracker_core::statistics::setup::factory(config.core.tracker_usage_statistics);
let udp_stats_repository = Arc::new(udp_stats_repository);

// UDP server stats
let (_udp_server_stats_event_sender, udp_server_stats_repository) =
torrust_udp_tracker_server::statistics::setup::factory(config.core.tracker_usage_statistics);
let udp_server_stats_repository = Arc::new(udp_server_stats_repository);

let tracker_metrics = get_metrics(
in_memory_torrent_repository.clone(),
ban_service.clone(),
http_stats_repository.clone(),
udp_stats_repository.clone(),
udp_server_stats_repository.clone(),
)
.await;

Expand Down
14 changes: 7 additions & 7 deletions packages/udp-tracker-core/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub struct UdpTrackerCoreContainer {
pub whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,

pub udp_tracker_config: Arc<UdpTracker>,
pub udp_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
pub udp_stats_repository: Arc<statistics::repository::Repository>,
pub udp_core_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
pub udp_core_stats_repository: Arc<statistics::repository::Repository>,
pub ban_service: Arc<RwLock<BanService>>,
}

Expand All @@ -35,10 +35,10 @@ impl UdpTrackerCoreContainer {
tracker_core_container: &Arc<TrackerCoreContainer>,
udp_tracker_config: &Arc<UdpTracker>,
) -> Arc<UdpTrackerCoreContainer> {
let (udp_stats_event_sender, udp_stats_repository) =
let (udp_core_stats_event_sender, udp_core_stats_repository) =
statistics::setup::factory(tracker_core_container.core_config.tracker_usage_statistics);
let udp_stats_event_sender = Arc::new(udp_stats_event_sender);
let udp_stats_repository = Arc::new(udp_stats_repository);
let udp_core_stats_event_sender = Arc::new(udp_core_stats_event_sender);
let udp_core_stats_repository = Arc::new(udp_core_stats_repository);

let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));

Expand All @@ -49,8 +49,8 @@ impl UdpTrackerCoreContainer {
whitelist_authorization: tracker_core_container.whitelist_authorization.clone(),

udp_tracker_config: udp_tracker_config.clone(),
udp_stats_event_sender: udp_stats_event_sender.clone(),
udp_stats_repository: udp_stats_repository.clone(),
udp_core_stats_event_sender: udp_core_stats_event_sender.clone(),
udp_core_stats_repository: udp_core_stats_repository.clone(),
ban_service: ban_service.clone(),
})
}
Expand Down
18 changes: 9 additions & 9 deletions packages/udp-tracker-core/src/services/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ mod tests {

#[tokio::test]
async fn a_connect_response_should_contain_the_same_transaction_id_as_the_connect_request() {
let (udp_stats_event_sender, _udp_stats_repository) = statistics::setup::factory(false);
let udp_stats_event_sender = Arc::new(udp_stats_event_sender);
let (udp_core_stats_event_sender, _udp_core_stats_repository) = statistics::setup::factory(false);
let udp_core_stats_event_sender = Arc::new(udp_core_stats_event_sender);

let response = handle_connect(sample_ipv4_remote_addr(), &udp_stats_event_sender, sample_issue_time()).await;
let response = handle_connect(sample_ipv4_remote_addr(), &udp_core_stats_event_sender, sample_issue_time()).await;

assert_eq!(
response,
Expand All @@ -68,10 +68,10 @@ mod tests {

#[tokio::test]
async fn a_connect_response_should_contain_a_new_connection_id() {
let (udp_stats_event_sender, _udp_stats_repository) = statistics::setup::factory(false);
let udp_stats_event_sender = Arc::new(udp_stats_event_sender);
let (udp_core_stats_event_sender, _udp_core_stats_repository) = statistics::setup::factory(false);
let udp_core_stats_event_sender = Arc::new(udp_core_stats_event_sender);

let response = handle_connect(sample_ipv4_remote_addr(), &udp_stats_event_sender, sample_issue_time()).await;
let response = handle_connect(sample_ipv4_remote_addr(), &udp_core_stats_event_sender, sample_issue_time()).await;

assert_eq!(
response,
Expand All @@ -81,10 +81,10 @@ mod tests {

#[tokio::test]
async fn a_connect_response_should_contain_a_new_connection_id_ipv6() {
let (udp_stats_event_sender, _udp_stats_repository) = statistics::setup::factory(false);
let udp_stats_event_sender = Arc::new(udp_stats_event_sender);
let (udp_core_stats_event_sender, _udp_core_stats_repository) = statistics::setup::factory(false);
let udp_core_stats_event_sender = Arc::new(udp_core_stats_event_sender);

let response = handle_connect(sample_ipv6_remote_addr(), &udp_stats_event_sender, sample_issue_time()).await;
let response = handle_connect(sample_ipv6_remote_addr(), &udp_core_stats_event_sender, sample_issue_time()).await;

assert_eq!(
response,
Expand Down
Loading
Loading