-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [#565] new torrent repository implementation usind DashMap
It's not enabled as the deafult repository becuase DashMap does not return the items in order. Some tests fail: ``` output failures: ---- core::services::torrent::tests::searching_for_torrents::should_return_torrents_ordered_by_info_hash stdout ---- thread 'core::services::torrent::tests::searching_for_torrents::should_return_torrents_ordered_by_info_hash' panicked at src/core/services/torrent.rs:303:13: assertion `left == right` failed left: [BasicInfo { info_hash: InfoHash([158, 2, 23, 208, 250, 113, 200, 115, 50, 205, 139, 249, 219, 234, 188, 178, 194, 207, 60, 77]), seeders: 1, completed: 0, leechers: 0 }, BasicInfo { info_hash: InfoHash([3, 132, 5, 72, 100, 58, 242, 167, 182, 58, 159, 92, 188, 163, 72, 188, 113, 80, 202, 58]), seeders: 1, completed: 0, leechers: 0 }] right: [BasicInfo { info_hash: InfoHash([3, 132, 5, 72, 100, 58, 242, 167, 182, 58, 159, 92, 188, 163, 72, 188, 113, 80, 202, 58]), seeders: 1, completed: 0, leechers: 0 }, BasicInfo { info_hash: InfoHash([158, 2, 23, 208, 250, 113, 200, 115, 50, 205, 139, 249, 219, 234, 188, 178, 194, 207, 60, 77]), seeders: 1, completed: 0, leechers: 0 }] note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: core::services::torrent::tests::searching_for_torrents::should_return_torrents_ordered_by_info_hash test result: FAILED. 212 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 1.18s error: test failed, to rerun pass `--lib` ``` On the other hand, to use it, a new data strcuture should be added to the repo: An Index with sorted torrents by Infohash The API uses pagination returning torrents in alphabetically order by InfoHash. Adding such an Index would probably decrease the performace of this repository implementation. And it's performace looks similar to the current SkipMap implementation. SkipMap performace with Aquatic UDP load test: ``` Requests out: 396788.68/second Responses in: 357105.27/second - Connect responses: 176662.91 - Announce responses: 176863.44 - Scrape responses: 3578.91 - Error responses: 0.00 Peers per announce response: 0.00 Announce responses per info hash: - p10: 1 - p25: 1 - p50: 1 - p75: 1 - p90: 2 - p95: 3 - p99: 105 - p99.9: 287 - p100: 351 ``` DashMap performace with Aquatic UDP load test: ``` Requests out: 410658.38/second Responses in: 365892.86/second - Connect responses: 181258.91 - Announce responses: 181005.95 - Scrape responses: 3628.00 - Error responses: 0.00 Peers per announce response: 0.00 Announce responses per info hash: - p10: 1 - p25: 1 - p50: 1 - p75: 1 - p90: 2 - p95: 3 - p99: 104 - p99.9: 295 - p100: 363 ```
- Loading branch information
1 parent
78b46c4
commit 00ee9db
Showing
6 changed files
with
170 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/torrent-repository/src/repository/dash_map_mutex_std.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use std::collections::BTreeMap; | ||
use std::sync::Arc; | ||
|
||
use dashmap::DashMap; | ||
use torrust_tracker_configuration::TrackerPolicy; | ||
use torrust_tracker_primitives::info_hash::InfoHash; | ||
use torrust_tracker_primitives::pagination::Pagination; | ||
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; | ||
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; | ||
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents}; | ||
|
||
use super::Repository; | ||
use crate::entry::{Entry, EntrySync}; | ||
use crate::{EntryMutexStd, EntrySingle}; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct XacrimonDashMap<T> { | ||
pub torrents: DashMap<InfoHash, T>, | ||
} | ||
|
||
impl Repository<EntryMutexStd> for XacrimonDashMap<EntryMutexStd> | ||
where | ||
EntryMutexStd: EntrySync, | ||
EntrySingle: Entry, | ||
{ | ||
fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) { | ||
if let Some(entry) = self.torrents.get(info_hash) { | ||
entry.insert_or_update_peer_and_get_stats(peer) | ||
} else { | ||
let _unused = self.torrents.insert(*info_hash, Arc::default()); | ||
|
||
match self.torrents.get(info_hash) { | ||
Some(entry) => entry.insert_or_update_peer_and_get_stats(peer), | ||
None => (false, SwarmMetadata::zeroed()), | ||
} | ||
} | ||
} | ||
|
||
fn get(&self, key: &InfoHash) -> Option<EntryMutexStd> { | ||
let maybe_entry = self.torrents.get(key); | ||
maybe_entry.map(|entry| entry.clone()) | ||
} | ||
|
||
fn get_metrics(&self) -> TorrentsMetrics { | ||
let mut metrics = TorrentsMetrics::default(); | ||
|
||
for entry in &self.torrents { | ||
let stats = entry.value().lock().expect("it should get a lock").get_stats(); | ||
metrics.complete += u64::from(stats.complete); | ||
metrics.downloaded += u64::from(stats.downloaded); | ||
metrics.incomplete += u64::from(stats.incomplete); | ||
metrics.torrents += 1; | ||
} | ||
|
||
metrics | ||
} | ||
|
||
fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, EntryMutexStd)> { | ||
match pagination { | ||
Some(pagination) => self | ||
.torrents | ||
.iter() | ||
.skip(pagination.offset as usize) | ||
.take(pagination.limit as usize) | ||
.map(|entry| (*entry.key(), entry.value().clone())) | ||
.collect(), | ||
None => self | ||
.torrents | ||
.iter() | ||
.map(|entry| (*entry.key(), entry.value().clone())) | ||
.collect(), | ||
} | ||
} | ||
|
||
fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { | ||
for (info_hash, completed) in persistent_torrents { | ||
if self.torrents.contains_key(info_hash) { | ||
continue; | ||
} | ||
|
||
let entry = EntryMutexStd::new( | ||
EntrySingle { | ||
peers: BTreeMap::default(), | ||
downloaded: *completed, | ||
} | ||
.into(), | ||
); | ||
|
||
self.torrents.insert(*info_hash, entry); | ||
} | ||
} | ||
|
||
fn remove(&self, key: &InfoHash) -> Option<EntryMutexStd> { | ||
self.torrents.remove(key).map(|(_key, value)| value.clone()) | ||
} | ||
|
||
fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) { | ||
for entry in &self.torrents { | ||
entry.value().remove_inactive_peers(current_cutoff); | ||
} | ||
} | ||
|
||
fn remove_peerless_torrents(&self, policy: &TrackerPolicy) { | ||
self.torrents.retain(|_, entry| entry.is_good(policy)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters