Skip to content

Commit 731fd01

Browse files
committed
Merge torrust#1204: Overhaul core Tracker: extract torrents context (part 2)
046578d refactor: [torrust#1203] use directly the InMemoryTorrentRepository (Jose Celano) 0f1b2fb refactor: [torrust#1203] use InMemoryTorrentRepository directly in core tracker tests (Jose Celano) 2ac68f6 refactor: [torrust#1203] move test (Jose Celano) 94673d6 refactor: [torrust#1203] inline methods in core tracker (Jose Celano) Pull request description: This is part 2 of the refactor initiated [here](torrust#1202). The core `Tracker` after this refactor: ```rust pub struct Tracker { /// The tracker configuration. config: Core, /// The service to check is a torrent is whitelisted. whitelist_authorization: Arc<whitelist::authorization::Authorization>, /// The in-memory torrents repository. in_memory_torrent_repository: Arc<InMemoryTorrentRepository>, /// The persistent torrents repository. db_torrent_repository: Arc<DatabasePersistentTorrentRepository>, } ``` ACKs for top commit: josecelano: ACK 046578d Tree-SHA512: db32af87815375ca570b81392c392dae0b602d12f54543c413d34f6a8dcb69f7e2f56a56e5d552a271d7fe2e4f751277d9f6e204caee297a2e3333f6a7b77e24
2 parents a4277a7 + 046578d commit 731fd01

File tree

17 files changed

+306
-207
lines changed

17 files changed

+306
-207
lines changed

src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
118118
if let Some(http_api_config) = &config.http_api {
119119
if let Some(job) = tracker_apis::start_job(
120120
http_api_config,
121-
app_container.tracker.clone(),
121+
app_container.in_memory_torrent_repository.clone(),
122122
app_container.keys_handler.clone(),
123123
app_container.whitelist_manager.clone(),
124124
app_container.ban_service.clone(),

src/bootstrap/jobs/tracker_apis.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use super::make_rust_tls;
3333
use crate::core::authentication::handler::KeysHandler;
3434
use crate::core::statistics::event::sender::Sender;
3535
use crate::core::statistics::repository::Repository;
36+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
3637
use crate::core::whitelist::manager::WhiteListManager;
37-
use crate::core::{self};
3838
use crate::servers::apis::server::{ApiServer, Launcher};
3939
use crate::servers::apis::Version;
4040
use crate::servers::registar::ServiceRegistrationForm;
@@ -63,7 +63,6 @@ pub struct ApiServerJobStarted();
6363
#[allow(clippy::too_many_arguments)]
6464
#[instrument(skip(
6565
config,
66-
tracker,
6766
keys_handler,
6867
whitelist_manager,
6968
ban_service,
@@ -73,7 +72,7 @@ pub struct ApiServerJobStarted();
7372
))]
7473
pub async fn start_job(
7574
config: &HttpApi,
76-
tracker: Arc<core::Tracker>,
75+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
7776
keys_handler: Arc<KeysHandler>,
7877
whitelist_manager: Arc<WhiteListManager>,
7978
ban_service: Arc<RwLock<BanService>>,
@@ -95,7 +94,7 @@ pub async fn start_job(
9594
start_v1(
9695
bind_to,
9796
tls,
98-
tracker.clone(),
97+
in_memory_torrent_repository.clone(),
9998
keys_handler.clone(),
10099
whitelist_manager.clone(),
101100
ban_service.clone(),
@@ -114,7 +113,6 @@ pub async fn start_job(
114113
#[instrument(skip(
115114
socket,
116115
tls,
117-
tracker,
118116
keys_handler,
119117
whitelist_manager,
120118
ban_service,
@@ -126,7 +124,7 @@ pub async fn start_job(
126124
async fn start_v1(
127125
socket: SocketAddr,
128126
tls: Option<RustlsConfig>,
129-
tracker: Arc<core::Tracker>,
127+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
130128
keys_handler: Arc<KeysHandler>,
131129
whitelist_manager: Arc<WhiteListManager>,
132130
ban_service: Arc<RwLock<BanService>>,
@@ -137,7 +135,7 @@ async fn start_v1(
137135
) -> JoinHandle<()> {
138136
let server = ApiServer::new(Launcher::new(socket, tls))
139137
.start(
140-
tracker,
138+
in_memory_torrent_repository,
141139
keys_handler,
142140
whitelist_manager,
143141
stats_event_sender,
@@ -179,7 +177,7 @@ mod tests {
179177

180178
start_job(
181179
config,
182-
app_container.tracker,
180+
app_container.in_memory_torrent_repository,
183181
app_container.keys_handler,
184182
app_container.whitelist_manager,
185183
app_container.ban_service,

src/core/mod.rs

+53-74
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@ use torrust_tracker_configuration::{AnnouncePolicy, Core, TORRENT_PEERS_LIMIT};
459459
use torrust_tracker_primitives::core::{AnnounceData, ScrapeData};
460460
use torrust_tracker_primitives::peer;
461461
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
462-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
463462

464463
/// The domain layer tracker service.
465464
///
@@ -475,7 +474,7 @@ pub struct Tracker {
475474
config: Core,
476475

477476
/// The service to check is a torrent is whitelisted.
478-
pub whitelist_authorization: Arc<whitelist::authorization::Authorization>,
477+
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
479478

480479
/// The in-memory torrents repository.
481480
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
@@ -619,7 +618,9 @@ impl Tracker {
619618

620619
let stats = self.upsert_peer_and_get_stats(info_hash, peer);
621620

622-
let peers = self.get_peers_for(info_hash, peer, peers_wanted.limit());
621+
let peers = self
622+
.in_memory_torrent_repository
623+
.get_peers_for(info_hash, peer, peers_wanted.limit());
623624

624625
AnnounceData {
625626
peers,
@@ -638,7 +639,7 @@ impl Tracker {
638639

639640
for info_hash in info_hashes {
640641
let swarm_metadata = match self.whitelist_authorization.authorize(info_hash).await {
641-
Ok(()) => self.get_swarm_metadata(info_hash),
642+
Ok(()) => self.in_memory_torrent_repository.get_swarm_metadata(info_hash),
642643
Err(_) => SwarmMetadata::zeroed(),
643644
};
644645
scrape_data.add_file(info_hash, swarm_metadata);
@@ -684,40 +685,6 @@ impl Tracker {
684685
drop(self.db_torrent_repository.save(&info_hash, completed));
685686
}
686687
}
687-
688-
/// It returns the data for a `scrape` response.
689-
fn get_swarm_metadata(&self, info_hash: &InfoHash) -> SwarmMetadata {
690-
self.in_memory_torrent_repository.get_swarm_metadata(info_hash)
691-
}
692-
693-
/// # Context: Tracker
694-
///
695-
/// Get torrent peers for a given torrent and client.
696-
///
697-
/// It filters out the client making the request.
698-
fn get_peers_for(&self, info_hash: &InfoHash, peer: &peer::Peer, limit: usize) -> Vec<Arc<peer::Peer>> {
699-
self.in_memory_torrent_repository.get_peers_for(info_hash, peer, limit)
700-
}
701-
702-
/// # Context: Tracker
703-
///
704-
/// Get torrent peers for a given torrent.
705-
#[must_use]
706-
pub fn get_torrent_peers(&self, info_hash: &InfoHash) -> Vec<Arc<peer::Peer>> {
707-
self.in_memory_torrent_repository.get_torrent_peers(info_hash)
708-
}
709-
710-
/// It calculates and returns the general `Tracker`
711-
/// [`TorrentsMetrics`]
712-
///
713-
/// # Context: Tracker
714-
///
715-
/// # Panics
716-
/// Panics if unable to get the torrent metrics.
717-
#[must_use]
718-
pub fn get_torrents_metrics(&self) -> TorrentsMetrics {
719-
self.in_memory_torrent_repository.get_torrents_metrics()
720-
}
721688
}
722689

723690
#[must_use]
@@ -742,15 +709,17 @@ mod tests {
742709
use bittorrent_primitives::info_hash::fixture::gen_seeded_infohash;
743710
use bittorrent_primitives::info_hash::InfoHash;
744711
use torrust_tracker_configuration::TORRENT_PEERS_LIMIT;
712+
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
745713
use torrust_tracker_primitives::DurationSinceUnixEpoch;
746714
use torrust_tracker_test_helpers::configuration;
747715

748716
use crate::app_test::initialize_tracker_dependencies;
749717
use crate::core::peer::Peer;
750718
use crate::core::services::{initialize_tracker, initialize_whitelist_manager};
751719
use crate::core::torrent::manager::TorrentsManager;
720+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
752721
use crate::core::whitelist::manager::WhiteListManager;
753-
use crate::core::{whitelist, TorrentsMetrics, Tracker};
722+
use crate::core::{whitelist, Tracker};
754723

755724
fn public_tracker() -> Tracker {
756725
let config = configuration::ephemeral_public();
@@ -773,6 +742,29 @@ mod tests {
773742
)
774743
}
775744

745+
fn public_tracker_and_in_memory_torrents_repository() -> (Arc<Tracker>, Arc<InMemoryTorrentRepository>) {
746+
let config = configuration::ephemeral_public();
747+
748+
let (
749+
_database,
750+
_in_memory_whitelist,
751+
whitelist_authorization,
752+
_authentication_service,
753+
in_memory_torrent_repository,
754+
db_torrent_repository,
755+
_torrents_manager,
756+
) = initialize_tracker_dependencies(&config);
757+
758+
let tracker = Arc::new(initialize_tracker(
759+
&config,
760+
&whitelist_authorization,
761+
&in_memory_torrent_repository,
762+
&db_torrent_repository,
763+
));
764+
765+
(tracker, in_memory_torrent_repository)
766+
}
767+
776768
fn whitelisted_tracker() -> (Tracker, Arc<whitelist::authorization::Authorization>, Arc<WhiteListManager>) {
777769
let config = configuration::ephemeral_listed();
778770

@@ -798,7 +790,7 @@ mod tests {
798790
(tracker, whitelist_authorization, whitelist_manager)
799791
}
800792

801-
pub fn tracker_persisting_torrents_in_database() -> (Tracker, Arc<TorrentsManager>) {
793+
pub fn tracker_persisting_torrents_in_database() -> (Tracker, Arc<TorrentsManager>, Arc<InMemoryTorrentRepository>) {
802794
let mut config = configuration::ephemeral_listed();
803795
config.core.tracker_policy.persistent_torrent_completed_stat = true;
804796

@@ -819,7 +811,7 @@ mod tests {
819811
&db_torrent_repository,
820812
);
821813

822-
(tracker, torrents_manager)
814+
(tracker, torrents_manager, in_memory_torrent_repository)
823815
}
824816

825817
fn sample_info_hash() -> InfoHash {
@@ -906,33 +898,16 @@ mod tests {
906898
}
907899
}
908900

909-
#[tokio::test]
910-
async fn should_collect_torrent_metrics() {
911-
let tracker = public_tracker();
912-
913-
let torrents_metrics = tracker.get_torrents_metrics();
914-
915-
assert_eq!(
916-
torrents_metrics,
917-
TorrentsMetrics {
918-
complete: 0,
919-
downloaded: 0,
920-
incomplete: 0,
921-
torrents: 0
922-
}
923-
);
924-
}
925-
926901
#[tokio::test]
927902
async fn it_should_return_the_peers_for_a_given_torrent() {
928-
let tracker = public_tracker();
903+
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
929904

930905
let info_hash = sample_info_hash();
931906
let peer = sample_peer();
932907

933908
let _ = tracker.upsert_peer_and_get_stats(&info_hash, &peer);
934909

935-
let peers = tracker.get_torrent_peers(&info_hash);
910+
let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash);
936911

937912
assert_eq!(peers, vec![Arc::new(peer)]);
938913
}
@@ -957,7 +932,7 @@ mod tests {
957932

958933
#[tokio::test]
959934
async fn it_should_return_74_peers_at_the_most_for_a_given_torrent() {
960-
let tracker = public_tracker();
935+
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
961936

962937
let info_hash = sample_info_hash();
963938

@@ -975,7 +950,7 @@ mod tests {
975950
let _ = tracker.upsert_peer_and_get_stats(&info_hash, &peer);
976951
}
977952

978-
let peers = tracker.get_torrent_peers(&info_hash);
953+
let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash);
979954

980955
assert_eq!(peers.len(), 74);
981956
}
@@ -989,7 +964,9 @@ mod tests {
989964

990965
let _ = tracker.upsert_peer_and_get_stats(&info_hash, &peer);
991966

992-
let peers = tracker.get_peers_for(&info_hash, &peer, TORRENT_PEERS_LIMIT);
967+
let peers = tracker
968+
.in_memory_torrent_repository
969+
.get_peers_for(&info_hash, &peer, TORRENT_PEERS_LIMIT);
993970

994971
assert_eq!(peers, vec![]);
995972
}
@@ -1019,18 +996,20 @@ mod tests {
1019996
let _ = tracker.upsert_peer_and_get_stats(&info_hash, &peer);
1020997
}
1021998

1022-
let peers = tracker.get_peers_for(&info_hash, &excluded_peer, TORRENT_PEERS_LIMIT);
999+
let peers = tracker
1000+
.in_memory_torrent_repository
1001+
.get_peers_for(&info_hash, &excluded_peer, TORRENT_PEERS_LIMIT);
10231002

10241003
assert_eq!(peers.len(), 74);
10251004
}
10261005

10271006
#[tokio::test]
10281007
async fn it_should_return_the_torrent_metrics() {
1029-
let tracker = public_tracker();
1008+
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
10301009

10311010
let _ = tracker.upsert_peer_and_get_stats(&sample_info_hash(), &leecher());
10321011

1033-
let torrent_metrics = tracker.get_torrents_metrics();
1012+
let torrent_metrics = in_memory_torrent_repository.get_torrents_metrics();
10341013

10351014
assert_eq!(
10361015
torrent_metrics,
@@ -1045,7 +1024,7 @@ mod tests {
10451024

10461025
#[tokio::test]
10471026
async fn it_should_get_many_the_torrent_metrics() {
1048-
let tracker = public_tracker();
1027+
let (tracker, in_memory_torrent_repository) = public_tracker_and_in_memory_torrents_repository();
10491028

10501029
let start_time = std::time::Instant::now();
10511030
for i in 0..1_000_000 {
@@ -1054,7 +1033,7 @@ mod tests {
10541033
let result_a = start_time.elapsed();
10551034

10561035
let start_time = std::time::Instant::now();
1057-
let torrent_metrics = tracker.get_torrents_metrics();
1036+
let torrent_metrics = in_memory_torrent_repository.get_torrents_metrics();
10581037
let result_b = start_time.elapsed();
10591038

10601039
assert_eq!(
@@ -1346,24 +1325,24 @@ mod tests {
13461325

13471326
#[tokio::test]
13481327
async fn it_should_authorize_the_announce_and_scrape_actions_on_whitelisted_torrents() {
1349-
let (tracker, _whitelist_authorization, whitelist_manager) = whitelisted_tracker();
1328+
let (_tracker, whitelist_authorization, whitelist_manager) = whitelisted_tracker();
13501329

13511330
let info_hash = sample_info_hash();
13521331

13531332
let result = whitelist_manager.add_torrent_to_whitelist(&info_hash).await;
13541333
assert!(result.is_ok());
13551334

1356-
let result = tracker.whitelist_authorization.authorize(&info_hash).await;
1335+
let result = whitelist_authorization.authorize(&info_hash).await;
13571336
assert!(result.is_ok());
13581337
}
13591338

13601339
#[tokio::test]
13611340
async fn it_should_not_authorize_the_announce_and_scrape_actions_on_not_whitelisted_torrents() {
1362-
let (tracker, _whitelist_authorization, _whitelist_manager) = whitelisted_tracker();
1341+
let (_tracker, whitelist_authorization, _whitelist_manager) = whitelisted_tracker();
13631342

13641343
let info_hash = sample_info_hash();
13651344

1366-
let result = tracker.whitelist_authorization.authorize(&info_hash).await;
1345+
let result = whitelist_authorization.authorize(&info_hash).await;
13671346
assert!(result.is_err());
13681347
}
13691348
}
@@ -1479,7 +1458,7 @@ mod tests {
14791458

14801459
#[tokio::test]
14811460
async fn it_should_persist_the_number_of_completed_peers_for_all_torrents_into_the_database() {
1482-
let (tracker, torrents_manager) = tracker_persisting_torrents_in_database();
1461+
let (tracker, torrents_manager, in_memory_torrent_repository) = tracker_persisting_torrents_in_database();
14831462

14841463
let info_hash = sample_info_hash();
14851464

@@ -1494,7 +1473,7 @@ mod tests {
14941473
assert_eq!(swarm_stats.downloaded, 1);
14951474

14961475
// Remove the newly updated torrent from memory
1497-
let _unused = tracker.in_memory_torrent_repository.remove(&info_hash);
1476+
let _unused = in_memory_torrent_repository.remove(&info_hash);
14981477

14991478
torrents_manager.load_torrents_from_database().unwrap();
15001479

0 commit comments

Comments
 (0)