-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #784: Performance optimization: create a new torrent repository…
… using `DashMap` 4030fd1 fix: torrent repository tests. DashMap is not ordered (Jose Celano) 1e76c17 chore: add dashmap cargo dep to cargp machete (Jose Celano) 00ee9db feat: [#565] new torrent repository implementation usind DashMap (Jose Celano) 78b46c4 chore(deps): add cargo dependency: dashmap (Jose Celano) Pull request description: Relates to: - #778 - #567 (comment) This PR adds a new torrent repository implementation where the outer collection for torrents uses a [DashMap](https://docs.rs/dashmap/latest/dashmap/). This is something @mickvandijke was working on this [PR](#645). There have been many changes. @da2ce7 has extracted a [package for the repositories](https://github.com/torrust/torrust-tracker/tree/develop/packages/torrent-repository). This PR adds a new repo using DashMap to the new package. However, it does not implement some of the extra features @mickvandijke added to the other [PR](#645). For example, it does not limit memory consumption. None of the other repos have that feature, so I suggest merging this PR and implementing that feature in the future for all repos. ### Why The current repository used in production is the one using a [SkipMap](https://docs.rs/crossbeam-skiplist/latest/crossbeam_skiplist/struct.SkipMap.html) data structure. DashMap was the first type we considered to allow adding new torrents in parallels. The implementation with DashMap has not been merged because it does not guarantee the order when you iterate over the torrents. The tracker API returns torrents ordered by InfoHash. To avoid breaking the API that PR would need to add a new data structure (kind of Index) to keep the torrent list ordered. This implementation helps us run performance tests with this option without spending much time fixing its limitations. It looks like the performance is similar to the SkiMap, so we don't need to use it in production now. We only need to keep it for benchmarking in the future if other versions are better. ### Benchmarking Running the Aquatic UDP load test, the DashMap looks slightly better. SkipMap: ```output 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 with initial capacity 0 (best result. On average is lower, similar to SkipMap): ```output 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 ``` With Criterion: ![image](https://github.com/torrust/torrust-tracker/assets/58816/1e1fca2d-821d-4e2c-adf8-49e055758bd0) ![image](https://github.com/torrust/torrust-tracker/assets/58816/fcb9a32f-c4f1-4ffd-9797-4a74fc000336) ![image](https://github.com/torrust/torrust-tracker/assets/58816/efe2d788-31ac-4997-82f6-d022aa8f79a0) ![image](https://github.com/torrust/torrust-tracker/assets/58816/f64a4e5a-a363-48cd-87d9-78162cda11d4) ### Conclusion From my point of view, other [performance optimisations](#774) have more potential than this, considering this implementation is not finished. The changes needed to finish this implementation will probably decrease the performance obtained in this benchmarking. In the future, we can review this option if we change the [tracker API behaviour to getting all torrents](#775). ACKs for top commit: josecelano: ACK 4030fd1 Tree-SHA512: 1a6c56f3aecb34fc40c401597efe1663c3cc66903f2d27bf8f2bbc6b058080d487a89b705c224657aaa6059f1c2a8597583e636e30727f9293bb43f460441415
- Loading branch information
Showing
10 changed files
with
185 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,7 @@ | |
"Weidendorfer", | ||
"Werror", | ||
"whitespaces", | ||
"Xacrimon", | ||
"XBTT", | ||
"Xdebug", | ||
"Xeon", | ||
|
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
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
Oops, something went wrong.