Skip to content

Commit 4b4f310

Browse files
committed
Merge #1542: Overhaul stats: Refactor code related to metrics persistence
bd6e06a refactor: [#1541] remove unused code (Jose Celano) bcf2338 refactor: [#1541] rename type alias PersistentTorrents to NumberOfDownloadsBTreeMap (Jose Celano) a5a80b5 refactor: [#1541] rename type alias PersistentTorrent to NumberOfDownloads (Jose Celano) 0508a6a refactor: [#1541] rename methods (Jose Celano) 7e27d31 refactor: [#1541] move mod (Jose Celano) fdbea0a refactor: [#1541] rename mod (Jose Celano) 4e3e4f5 refactor: [#1541] create folder for mod (Jose Celano) 99adbde refactor: [#1541] rename symbol db_torrent_repository to db_downloads_metric_repository (Jose Celano) 3bc8148 refactor: [#1541] rename DatabasePersistentTorrentRepository to DatabaseDownloadsMetricRepository (Jose Celano) Pull request description: Refactor code related to metrics persistence. ACKs for top commit: josecelano: ACK bd6e06a Tree-SHA512: e445c0b5dbe2c162ee7a3cc26be87f02246f23ccc006e2dcc375e087ccf14dcf99ed1f530ff0722671364ccbd1e5508cfb6556f44dca89bb82664b4fa6cd3f38
2 parents 46df7eb + bd6e06a commit 4b4f310

File tree

37 files changed

+196
-214
lines changed

37 files changed

+196
-214
lines changed

packages/axum-http-tracker-server/src/v1/handlers/announce.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ mod tests {
119119
use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository;
120120
use bittorrent_tracker_core::authentication::service::AuthenticationService;
121121
use bittorrent_tracker_core::databases::setup::initialize_database;
122+
use bittorrent_tracker_core::statistics::persisted::downloads::DatabaseDownloadsMetricRepository;
122123
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
123-
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
124124
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
125125
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
126126
use torrust_tracker_configuration::Configuration;
@@ -156,12 +156,12 @@ mod tests {
156156
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
157157
let authentication_service = Arc::new(AuthenticationService::new(&config.core, &in_memory_key_repository));
158158
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
159-
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
159+
let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database));
160160
let announce_handler = Arc::new(AnnounceHandler::new(
161161
&config.core,
162162
&whitelist_authorization,
163163
&in_memory_torrent_repository,
164-
&db_torrent_repository,
164+
&db_downloads_metric_repository,
165165
));
166166

167167
// HTTP core stats

packages/http-tracker-core/benches/helpers/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use bittorrent_tracker_core::announce_handler::AnnounceHandler;
1414
use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository;
1515
use bittorrent_tracker_core::authentication::service::AuthenticationService;
1616
use bittorrent_tracker_core::databases::setup::initialize_database;
17+
use bittorrent_tracker_core::statistics::persisted::downloads::DatabaseDownloadsMetricRepository;
1718
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
18-
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
1919
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
2020
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
2121
use futures::future::BoxFuture;
@@ -45,7 +45,7 @@ pub fn initialize_core_tracker_services_with_config(config: &Configuration) -> (
4545
let core_config = Arc::new(config.core.clone());
4646
let database = initialize_database(&config.core);
4747
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
48-
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
48+
let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database));
4949
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
5050
let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone()));
5151
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
@@ -55,7 +55,7 @@ pub fn initialize_core_tracker_services_with_config(config: &Configuration) -> (
5555
&config.core,
5656
&whitelist_authorization,
5757
&in_memory_torrent_repository,
58-
&db_torrent_repository,
58+
&db_downloads_metric_repository,
5959
));
6060

6161
// HTTP core stats

packages/http-tracker-core/src/services/announce.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ mod tests {
212212
use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository;
213213
use bittorrent_tracker_core::authentication::service::AuthenticationService;
214214
use bittorrent_tracker_core::databases::setup::initialize_database;
215+
use bittorrent_tracker_core::statistics::persisted::downloads::DatabaseDownloadsMetricRepository;
215216
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
216-
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
217217
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
218218
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
219219
use torrust_tracker_configuration::{Configuration, Core};
@@ -239,7 +239,7 @@ mod tests {
239239
let core_config = Arc::new(config.core.clone());
240240
let database = initialize_database(&config.core);
241241
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
242-
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
242+
let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database));
243243
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
244244
let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone()));
245245
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
@@ -249,7 +249,7 @@ mod tests {
249249
&config.core,
250250
&whitelist_authorization,
251251
&in_memory_torrent_repository,
252-
&db_torrent_repository,
252+
&db_downloads_metric_repository,
253253
));
254254

255255
// HTTP core stats

packages/http-tracker-core/src/services/scrape.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ mod tests {
176176
use bittorrent_tracker_core::authentication::service::AuthenticationService;
177177
use bittorrent_tracker_core::databases::setup::initialize_database;
178178
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
179+
use bittorrent_tracker_core::statistics::persisted::downloads::DatabaseDownloadsMetricRepository;
179180
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
180-
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
181181
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
182182
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
183183
use futures::future::BoxFuture;
@@ -200,15 +200,15 @@ mod tests {
200200
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
201201
let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone()));
202202
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
203-
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
203+
let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database));
204204
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
205205
let authentication_service = Arc::new(AuthenticationService::new(&config.core, &in_memory_key_repository));
206206

207207
let announce_handler = Arc::new(AnnounceHandler::new(
208208
&config.core,
209209
&whitelist_authorization,
210210
&in_memory_torrent_repository,
211-
&db_torrent_repository,
211+
&db_downloads_metric_repository,
212212
));
213213

214214
let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository));

packages/primitives/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ use bittorrent_primitives::info_hash::InfoHash;
1818
/// Duration since the Unix Epoch.
1919
pub type DurationSinceUnixEpoch = Duration;
2020

21-
pub type PersistentTorrent = u32;
22-
pub type PersistentTorrents = BTreeMap<InfoHash, PersistentTorrent>;
21+
pub type NumberOfDownloads = u32;
22+
pub type NumberOfDownloadsBTreeMap = BTreeMap<InfoHash, NumberOfDownloads>;

packages/torrent-repository-benchmarking/src/repository/dash_map_mutex_std.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use dashmap::DashMap;
55
use torrust_tracker_configuration::TrackerPolicy;
66
use torrust_tracker_primitives::pagination::Pagination;
77
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
8-
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
8+
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap};
99

1010
use super::Repository;
1111
use crate::entry::peer_list::PeerList;
@@ -22,7 +22,7 @@ where
2222
EntryMutexStd: EntrySync,
2323
EntrySingle: Entry,
2424
{
25-
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option<PersistentTorrent>) -> bool {
25+
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option<NumberOfDownloads>) -> bool {
2626
// todo: load persistent torrent data if provided
2727

2828
if let Some(entry) = self.torrents.get(info_hash) {
@@ -77,7 +77,7 @@ where
7777
}
7878
}
7979

80-
fn import_persistent(&self, persistent_torrents: &PersistentTorrents) {
80+
fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) {
8181
for (info_hash, completed) in persistent_torrents {
8282
if self.torrents.contains_key(info_hash) {
8383
continue;

packages/torrent-repository-benchmarking/src/repository/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bittorrent_primitives::info_hash::InfoHash;
22
use torrust_tracker_configuration::TrackerPolicy;
33
use torrust_tracker_primitives::pagination::Pagination;
44
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
5-
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
5+
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap};
66

77
pub mod dash_map_mutex_std;
88
pub mod rw_lock_std;
@@ -19,11 +19,11 @@ pub trait Repository<T>: Debug + Default + Sized + 'static {
1919
fn get(&self, key: &InfoHash) -> Option<T>;
2020
fn get_metrics(&self) -> AggregateSwarmMetadata;
2121
fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, T)>;
22-
fn import_persistent(&self, persistent_torrents: &PersistentTorrents);
22+
fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap);
2323
fn remove(&self, key: &InfoHash) -> Option<T>;
2424
fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch);
2525
fn remove_peerless_torrents(&self, policy: &TrackerPolicy);
26-
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, opt_persistent_torrent: Option<PersistentTorrent>) -> bool;
26+
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, opt_persistent_torrent: Option<NumberOfDownloads>) -> bool;
2727
fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata>;
2828
}
2929

@@ -32,15 +32,15 @@ pub trait RepositoryAsync<T>: Debug + Default + Sized + 'static {
3232
fn get(&self, key: &InfoHash) -> impl std::future::Future<Output = Option<T>> + Send;
3333
fn get_metrics(&self) -> impl std::future::Future<Output = AggregateSwarmMetadata> + Send;
3434
fn get_paginated(&self, pagination: Option<&Pagination>) -> impl std::future::Future<Output = Vec<(InfoHash, T)>> + Send;
35-
fn import_persistent(&self, persistent_torrents: &PersistentTorrents) -> impl std::future::Future<Output = ()> + Send;
35+
fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) -> impl std::future::Future<Output = ()> + Send;
3636
fn remove(&self, key: &InfoHash) -> impl std::future::Future<Output = Option<T>> + Send;
3737
fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) -> impl std::future::Future<Output = ()> + Send;
3838
fn remove_peerless_torrents(&self, policy: &TrackerPolicy) -> impl std::future::Future<Output = ()> + Send;
3939
fn upsert_peer(
4040
&self,
4141
info_hash: &InfoHash,
4242
peer: &peer::Peer,
43-
opt_persistent_torrent: Option<PersistentTorrent>,
43+
opt_persistent_torrent: Option<NumberOfDownloads>,
4444
) -> impl std::future::Future<Output = bool> + Send;
4545
fn get_swarm_metadata(&self, info_hash: &InfoHash) -> impl std::future::Future<Output = Option<SwarmMetadata>> + Send;
4646
}

packages/torrent-repository-benchmarking/src/repository/rw_lock_std.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bittorrent_primitives::info_hash::InfoHash;
22
use torrust_tracker_configuration::TrackerPolicy;
33
use torrust_tracker_primitives::pagination::Pagination;
44
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
5-
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
5+
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap};
66

77
use super::Repository;
88
use crate::entry::peer_list::PeerList;
@@ -45,7 +45,7 @@ impl Repository<EntrySingle> for TorrentsRwLockStd
4545
where
4646
EntrySingle: Entry,
4747
{
48-
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option<PersistentTorrent>) -> bool {
48+
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option<NumberOfDownloads>) -> bool {
4949
// todo: load persistent torrent data if provided
5050

5151
let mut db = self.get_torrents_mut();
@@ -92,7 +92,7 @@ where
9292
}
9393
}
9494

95-
fn import_persistent(&self, persistent_torrents: &PersistentTorrents) {
95+
fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) {
9696
let mut torrents = self.get_torrents_mut();
9797

9898
for (info_hash, downloaded) in persistent_torrents {

packages/torrent-repository-benchmarking/src/repository/rw_lock_std_mutex_std.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bittorrent_primitives::info_hash::InfoHash;
44
use torrust_tracker_configuration::TrackerPolicy;
55
use torrust_tracker_primitives::pagination::Pagination;
66
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
7-
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
7+
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap};
88

99
use super::Repository;
1010
use crate::entry::peer_list::PeerList;
@@ -32,7 +32,7 @@ where
3232
EntryMutexStd: EntrySync,
3333
EntrySingle: Entry,
3434
{
35-
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option<PersistentTorrent>) -> bool {
35+
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option<NumberOfDownloads>) -> bool {
3636
// todo: load persistent torrent data if provided
3737

3838
let maybe_entry = self.get_torrents().get(info_hash).cloned();
@@ -87,7 +87,7 @@ where
8787
}
8888
}
8989

90-
fn import_persistent(&self, persistent_torrents: &PersistentTorrents) {
90+
fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) {
9191
let mut torrents = self.get_torrents_mut();
9292

9393
for (info_hash, completed) in persistent_torrents {

packages/torrent-repository-benchmarking/src/repository/rw_lock_std_mutex_tokio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use futures::{Future, FutureExt};
88
use torrust_tracker_configuration::TrackerPolicy;
99
use torrust_tracker_primitives::pagination::Pagination;
1010
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
11-
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
11+
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap};
1212

1313
use super::RepositoryAsync;
1414
use crate::entry::peer_list::PeerList;
@@ -40,7 +40,7 @@ where
4040
&self,
4141
info_hash: &InfoHash,
4242
peer: &peer::Peer,
43-
_opt_persistent_torrent: Option<PersistentTorrent>,
43+
_opt_persistent_torrent: Option<NumberOfDownloads>,
4444
) -> bool {
4545
// todo: load persistent torrent data if provided
4646

@@ -101,7 +101,7 @@ where
101101
metrics
102102
}
103103

104-
async fn import_persistent(&self, persistent_torrents: &PersistentTorrents) {
104+
async fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) {
105105
let mut db = self.get_torrents_mut();
106106

107107
for (info_hash, completed) in persistent_torrents {

0 commit comments

Comments
 (0)