Skip to content

Commit 0c6d87c

Browse files
committed
Merge torrust#1229: Overhaul core Tracker: refactor statistics module
fd8b57a refactor: [torrust#1228] remove deprecated unified HTTP and UDP stats (Jose Celano) 5576938 refactor: [torrust#1228] get metrics from HTTP and UDP Tracker Core Stats (Jose Celano) f33665d refactor: [torrust#1228] start using the udp tracker stats (Jose Celano) 5f08b2e refactor: [torrust#1228] start using the http tracker stats (Jose Celano) 39cbeda refactor: [torrust#1228] add new UDP and HTTP stats services to AppContainer (Jose Celano) 700c912 docs: update tracker core docs (Jose Celano) f99534a refactor: [torrust#1228] split statistics mod into UDO and HTTP statistics (Jose Celano) 9318842 refactor: [torrust#1228] move statistics back from tracker-core to main lib (Jose Celano) 0ad88b6 refactor: [torrust#1228] move type from tracker-core to main lib (Jose Celano) Pull request description: Overhaul core Tracker: refactor statistics module. Statistics have been split into three parts: - HTTP stats - UDP stats - API stats: the API uses both HTTP metrics and UDP metrics. I have created a temporary directory (`src/packages/`) with the new packages. We need to create crates for those packages and move them to the `src/packages/` dir. I didn't want to do that in this PR. ``` $ tree src/packages/ src/packages/ ├── http_tracker_core │ ├── mod.rs │ └── statistics │ ├── event │ │ ├── handler.rs │ │ ├── listener.rs │ │ ├── mod.rs │ │ └── sender.rs │ ├── keeper.rs │ ├── metrics.rs │ ├── mod.rs │ ├── repository.rs │ ├── services.rs │ └── setup.rs ├── mod.rs ├── tracker_api_core │ ├── mod.rs │ └── statistics │ ├── metrics.rs │ ├── mod.rs │ └── services.rs └── udp_tracker_core ├── mod.rs └── statistics ├── event │ ├── handler.rs │ ├── listener.rs │ ├── mod.rs │ └── sender.rs ├── keeper.rs ├── metrics.rs ├── mod.rs ├── repository.rs ├── services.rs └── setup.rs 9 directories, 27 files ``` The app layers are a little bit different from what I described initially in the issue: Initial design: ``` Main Torrust Tracker | Axum HTTP tracker server (`packages\axum-http-tracker`. This hasn't been extracted yet) | HTTP tracker protocol (`packages\http-protocol`) | Core tracker (`packages\tracker-core`) ``` Final design: ``` Main Torrust Tracker | Axum HTTP tracker server (`packages\axum-http-tracker`. This hasn't been extracted yet) | HTTP tracker core (`src\packages\http_tracker_core`) | HTTP tracker protocol (`packages\http-protocol`) | Core tracker (`packages\tracker-core`) ``` I din't want to put stats in the `packages\http-protocol` because they are no part of the official HTTP tracker protocols. And I didn't want to put it in the axum server because it's code that can be re-used in other server implementations (using other frameworks). ACKs for top commit: josecelano: ACK fd8b57a Tree-SHA512: 75adfaef2bb7f356b2a135183f9eb0306e91fa6dce3e4195ed21dda261139267bcc7cbb7589093dd67c2800ddce5aa172a7ebba19870794436db53890c5a32ba
2 parents ec92a4c + fd8b57a commit 0c6d87c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1291
-720
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/tracker-core/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ bittorrent-http-protocol = { version = "3.0.0-develop", path = "../http-protocol
2020
bittorrent-primitives = "0.1.0"
2121
chrono = { version = "0", default-features = false, features = ["clock"] }
2222
derive_more = { version = "1", features = ["as_ref", "constructor", "from"] }
23-
futures = "0"
2423
r2d2 = "0"
2524
r2d2_mysql = "25"
2625
r2d2_sqlite = { version = "0", features = ["bundled"] }

packages/tracker-core/src/lib.rs

+1-56
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
//!
347347
//! Services are domain services on top of the core tracker domain. Right now there are two types of service:
348348
//!
349-
//! - For statistics: [`crate::core::statistics::services`]
349+
//! - For statistics: [`crate::packages::statistics::services`]
350350
//! - For torrents: [`crate::core::torrent::services`]
351351
//!
352352
//! Services usually format the data inside the tracker to make it easier to consume by other parts.
@@ -370,60 +370,6 @@
370370
//! To learn more about tracker authentication, refer to the following modules :
371371
//!
372372
//! - [`authentication`] module.
373-
//! - [`core`](crate::core) module.
374-
//! - [`http`](crate::servers::http) module.
375-
//!
376-
//! # Statistics
377-
//!
378-
//! The `Tracker` keeps metrics for some events:
379-
//!
380-
//! ```rust,no_run
381-
//! pub struct Metrics {
382-
//! // IP version 4
383-
//!
384-
//! // HTTP tracker
385-
//! pub tcp4_connections_handled: u64,
386-
//! pub tcp4_announces_handled: u64,
387-
//! pub tcp4_scrapes_handled: u64,
388-
//!
389-
//! // UDP tracker
390-
//! pub udp4_connections_handled: u64,
391-
//! pub udp4_announces_handled: u64,
392-
//! pub udp4_scrapes_handled: u64,
393-
//!
394-
//! // IP version 6
395-
//!
396-
//! // HTTP tracker
397-
//! pub tcp6_connections_handled: u64,
398-
//! pub tcp6_announces_handled: u64,
399-
//! pub tcp6_scrapes_handled: u64,
400-
//!
401-
//! // UDP tracker
402-
//! pub udp6_connections_handled: u64,
403-
//! pub udp6_announces_handled: u64,
404-
//! pub udp6_scrapes_handled: u64,
405-
//! }
406-
//! ```
407-
//!
408-
//! The metrics maintained by the `Tracker` are:
409-
//!
410-
//! - `connections_handled`: number of connections handled by the tracker
411-
//! - `announces_handled`: number of `announce` requests handled by the tracker
412-
//! - `scrapes_handled`: number of `scrape` handled requests by the tracker
413-
//!
414-
//! > **NOTICE**: as the HTTP tracker does not have an specific `connection` request like the UDP tracker, `connections_handled` are
415-
//! > increased on every `announce` and `scrape` requests.
416-
//!
417-
//! The tracker exposes an event sender API that allows the tracker users to send events. When a higher application service handles a
418-
//! `connection` , `announce` or `scrape` requests, it notifies the `Tracker` by sending statistics events.
419-
//!
420-
//! For example, the HTTP tracker would send an event like the following when it handles an `announce` request received from a peer using IP version 4.
421-
//!
422-
//! ```text
423-
//! stats_event_sender.send_stats_event(statistics::event::Event::Tcp4Announce).await
424-
//! ```
425-
//!
426-
//! Refer to [`statistics`] module for more information about statistics.
427373
//!
428374
//! # Persistence
429375
//!
@@ -442,7 +388,6 @@ pub mod authentication;
442388
pub mod databases;
443389
pub mod error;
444390
pub mod scrape_handler;
445-
pub mod statistics;
446391
pub mod torrent;
447392
pub mod whitelist;
448393

packages/tracker-core/src/statistics/mod.rs

-32
This file was deleted.

packages/tracker-core/src/statistics/services.rs

-55
This file was deleted.

src/bootstrap/app.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use bittorrent_tracker_core::authentication::key::repository::persisted::Databas
2020
use bittorrent_tracker_core::authentication::service;
2121
use bittorrent_tracker_core::databases::setup::initialize_database;
2222
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
23-
use bittorrent_tracker_core::statistics;
2423
use bittorrent_tracker_core::torrent::manager::TorrentsManager;
2524
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
2625
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
@@ -36,6 +35,7 @@ use tracing::instrument;
3635
use super::config::initialize_configuration;
3736
use crate::bootstrap;
3837
use crate::container::AppContainer;
38+
use crate::packages::{http_tracker_core, udp_tracker_core};
3939
use crate::servers::udp::server::banning::BanService;
4040
use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;
4141
use crate::shared::crypto::ephemeral_instance_keys;
@@ -90,9 +90,19 @@ pub fn initialize_global_services(configuration: &Configuration) {
9090
#[instrument(skip())]
9191
pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
9292
let core_config = Arc::new(configuration.core.clone());
93-
let (stats_event_sender, stats_repository) = statistics::setup::factory(configuration.core.tracker_usage_statistics);
94-
let stats_event_sender = Arc::new(stats_event_sender);
95-
let stats_repository = Arc::new(stats_repository);
93+
94+
// HTTP stats
95+
let (http_stats_event_sender, http_stats_repository) =
96+
http_tracker_core::statistics::setup::factory(configuration.core.tracker_usage_statistics);
97+
let http_stats_event_sender = Arc::new(http_stats_event_sender);
98+
let http_stats_repository = Arc::new(http_stats_repository);
99+
100+
// UDP stats
101+
let (udp_stats_event_sender, udp_stats_repository) =
102+
udp_tracker_core::statistics::setup::factory(configuration.core.tracker_usage_statistics);
103+
let udp_stats_event_sender = Arc::new(udp_stats_event_sender);
104+
let udp_stats_repository = Arc::new(udp_stats_repository);
105+
96106
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));
97107
let database = initialize_database(configuration);
98108
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
@@ -133,8 +143,10 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
133143
authentication_service,
134144
whitelist_authorization,
135145
ban_service,
136-
stats_event_sender,
137-
stats_repository,
146+
http_stats_event_sender,
147+
udp_stats_event_sender,
148+
http_stats_repository,
149+
udp_stats_repository,
138150
whitelist_manager,
139151
in_memory_torrent_repository,
140152
db_torrent_repository,

src/container.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use bittorrent_tracker_core::authentication::handler::KeysHandler;
55
use bittorrent_tracker_core::authentication::service::AuthenticationService;
66
use bittorrent_tracker_core::databases::Database;
77
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
8-
use bittorrent_tracker_core::statistics::event::sender::Sender;
9-
use bittorrent_tracker_core::statistics::repository::Repository;
108
use bittorrent_tracker_core::torrent::manager::TorrentsManager;
119
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
1210
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
@@ -15,6 +13,7 @@ use bittorrent_tracker_core::whitelist::manager::WhitelistManager;
1513
use tokio::sync::RwLock;
1614
use torrust_tracker_configuration::{Core, HttpApi, HttpTracker, UdpTracker};
1715

16+
use crate::packages::{http_tracker_core, udp_tracker_core};
1817
use crate::servers::udp::server::banning::BanService;
1918

2019
pub struct AppContainer {
@@ -26,8 +25,10 @@ pub struct AppContainer {
2625
pub authentication_service: Arc<AuthenticationService>,
2726
pub whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
2827
pub ban_service: Arc<RwLock<BanService>>,
29-
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,
30-
pub stats_repository: Arc<Repository>,
28+
pub http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
29+
pub udp_stats_event_sender: Arc<Option<Box<dyn udp_tracker_core::statistics::event::sender::Sender>>>,
30+
pub http_stats_repository: Arc<http_tracker_core::statistics::repository::Repository>,
31+
pub udp_stats_repository: Arc<udp_tracker_core::statistics::repository::Repository>,
3132
pub whitelist_manager: Arc<WhitelistManager>,
3233
pub in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
3334
pub db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
@@ -40,7 +41,7 @@ pub struct UdpTrackerContainer {
4041
pub announce_handler: Arc<AnnounceHandler>,
4142
pub scrape_handler: Arc<ScrapeHandler>,
4243
pub whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
43-
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,
44+
pub udp_stats_event_sender: Arc<Option<Box<dyn udp_tracker_core::statistics::event::sender::Sender>>>,
4445
pub ban_service: Arc<RwLock<BanService>>,
4546
}
4647

@@ -53,7 +54,7 @@ impl UdpTrackerContainer {
5354
announce_handler: app_container.announce_handler.clone(),
5455
scrape_handler: app_container.scrape_handler.clone(),
5556
whitelist_authorization: app_container.whitelist_authorization.clone(),
56-
stats_event_sender: app_container.stats_event_sender.clone(),
57+
udp_stats_event_sender: app_container.udp_stats_event_sender.clone(),
5758
ban_service: app_container.ban_service.clone(),
5859
}
5960
}
@@ -65,7 +66,7 @@ pub struct HttpTrackerContainer {
6566
pub announce_handler: Arc<AnnounceHandler>,
6667
pub scrape_handler: Arc<ScrapeHandler>,
6768
pub whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
68-
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,
69+
pub http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
6970
pub authentication_service: Arc<AuthenticationService>,
7071
}
7172

@@ -78,7 +79,7 @@ impl HttpTrackerContainer {
7879
announce_handler: app_container.announce_handler.clone(),
7980
scrape_handler: app_container.scrape_handler.clone(),
8081
whitelist_authorization: app_container.whitelist_authorization.clone(),
81-
stats_event_sender: app_container.stats_event_sender.clone(),
82+
http_stats_event_sender: app_container.http_stats_event_sender.clone(),
8283
authentication_service: app_container.authentication_service.clone(),
8384
}
8485
}
@@ -91,8 +92,8 @@ pub struct HttpApiContainer {
9192
pub keys_handler: Arc<KeysHandler>,
9293
pub whitelist_manager: Arc<WhitelistManager>,
9394
pub ban_service: Arc<RwLock<BanService>>,
94-
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,
95-
pub stats_repository: Arc<Repository>,
95+
pub http_stats_repository: Arc<http_tracker_core::statistics::repository::Repository>,
96+
pub udp_stats_repository: Arc<udp_tracker_core::statistics::repository::Repository>,
9697
}
9798

9899
impl HttpApiContainer {
@@ -105,8 +106,8 @@ impl HttpApiContainer {
105106
keys_handler: app_container.keys_handler.clone(),
106107
whitelist_manager: app_container.whitelist_manager.clone(),
107108
ban_service: app_container.ban_service.clone(),
108-
stats_event_sender: app_container.stats_event_sender.clone(),
109-
stats_repository: app_container.stats_repository.clone(),
109+
http_stats_repository: app_container.http_stats_repository.clone(),
110+
udp_stats_repository: app_container.udp_stats_repository.clone(),
110111
}
111112
}
112113
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ pub mod app;
494494
pub mod bootstrap;
495495
pub mod console;
496496
pub mod container;
497-
pub mod core;
497+
pub mod packages;
498498
pub mod servers;
499499
pub mod shared;
500500

File renamed without changes.

0 commit comments

Comments
 (0)