Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Torrent repository with parking_lot crate locks #800

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ hyper = "1"
lazy_static = "1"
log = { version = "0", features = ["release_max_level_info"] }
multimap = "0"
parking_lot = "0.12.1"
percent-encoding = "2"
r2d2 = "0"
r2d2_mysql = "24"
Expand Down Expand Up @@ -78,7 +79,7 @@ url = "2"
uuid = { version = "1", features = ["v4"] }

[package.metadata.cargo-machete]
ignored = ["serde_bytes", "crossbeam-skiplist", "dashmap"]
ignored = ["serde_bytes", "crossbeam-skiplist", "dashmap", "parking_lot"]

[dev-dependencies]
local-ip-address = "0"
Expand Down
1 change: 1 addition & 0 deletions packages/torrent-repository/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ version.workspace = true
crossbeam-skiplist = "0.1"
dashmap = "5.5.3"
futures = "0.3.29"
parking_lot = "0.12.1"
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
torrust-tracker-clock = { version = "3.0.0-alpha.12-develop", path = "../clock" }
torrust-tracker-configuration = { version = "3.0.0-alpha.12-develop", path = "../configuration" }
Expand Down
43 changes: 42 additions & 1 deletion packages/torrent-repository/benches/repository_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ mod helpers;
use criterion::{criterion_group, criterion_main, Criterion};
use torrust_tracker_torrent_repository::{
TorrentsDashMapMutexStd, TorrentsRwLockStd, TorrentsRwLockStdMutexStd, TorrentsRwLockStdMutexTokio, TorrentsRwLockTokio,
TorrentsRwLockTokioMutexStd, TorrentsRwLockTokioMutexTokio, TorrentsSkipMapMutexStd,
TorrentsRwLockTokioMutexStd, TorrentsRwLockTokioMutexTokio, TorrentsSkipMapMutexParkingLot, TorrentsSkipMapMutexStd,
TorrentsSkipMapRwLockParkingLot,
};

use crate::helpers::{asyn, sync};
Expand Down Expand Up @@ -49,6 +50,14 @@ fn add_one_torrent(c: &mut Criterion) {
b.iter_custom(sync::add_one_torrent::<TorrentsSkipMapMutexStd, _>);
});

group.bench_function("SkipMapMutexParkingLot", |b| {
b.iter_custom(sync::add_one_torrent::<TorrentsSkipMapMutexParkingLot, _>);
});

group.bench_function("SkipMapRwLockParkingLot", |b| {
b.iter_custom(sync::add_one_torrent::<TorrentsSkipMapRwLockParkingLot, _>);
});

group.bench_function("DashMapMutexStd", |b| {
b.iter_custom(sync::add_one_torrent::<TorrentsDashMapMutexStd, _>);
});
Expand Down Expand Up @@ -102,6 +111,16 @@ fn add_multiple_torrents_in_parallel(c: &mut Criterion) {
.iter_custom(|iters| sync::add_multiple_torrents_in_parallel::<TorrentsSkipMapMutexStd, _>(&rt, iters, None));
});

group.bench_function("SkipMapMutexParkingLot", |b| {
b.to_async(&rt)
.iter_custom(|iters| sync::add_multiple_torrents_in_parallel::<TorrentsSkipMapMutexParkingLot, _>(&rt, iters, None));
});

group.bench_function("SkipMapRwLockParkingLot", |b| {
b.to_async(&rt)
.iter_custom(|iters| sync::add_multiple_torrents_in_parallel::<TorrentsSkipMapRwLockParkingLot, _>(&rt, iters, None));
});

group.bench_function("DashMapMutexStd", |b| {
b.to_async(&rt)
.iter_custom(|iters| sync::add_multiple_torrents_in_parallel::<TorrentsDashMapMutexStd, _>(&rt, iters, None));
Expand Down Expand Up @@ -156,6 +175,16 @@ fn update_one_torrent_in_parallel(c: &mut Criterion) {
.iter_custom(|iters| sync::update_one_torrent_in_parallel::<TorrentsSkipMapMutexStd, _>(&rt, iters, None));
});

group.bench_function("SkipMapMutexParkingLot", |b| {
b.to_async(&rt)
.iter_custom(|iters| sync::update_one_torrent_in_parallel::<TorrentsSkipMapMutexParkingLot, _>(&rt, iters, None));
});

group.bench_function("SkipMapRwLockParkingLot", |b| {
b.to_async(&rt)
.iter_custom(|iters| sync::update_one_torrent_in_parallel::<TorrentsSkipMapRwLockParkingLot, _>(&rt, iters, None));
});

group.bench_function("DashMapMutexStd", |b| {
b.to_async(&rt)
.iter_custom(|iters| sync::update_one_torrent_in_parallel::<TorrentsDashMapMutexStd, _>(&rt, iters, None));
Expand Down Expand Up @@ -211,6 +240,18 @@ fn update_multiple_torrents_in_parallel(c: &mut Criterion) {
.iter_custom(|iters| sync::update_multiple_torrents_in_parallel::<TorrentsSkipMapMutexStd, _>(&rt, iters, None));
});

group.bench_function("SkipMapMutexParkingLot", |b| {
b.to_async(&rt).iter_custom(|iters| {
sync::update_multiple_torrents_in_parallel::<TorrentsSkipMapMutexParkingLot, _>(&rt, iters, None)
});
});

group.bench_function("SkipMapRwLockParkingLot", |b| {
b.to_async(&rt).iter_custom(|iters| {
sync::update_multiple_torrents_in_parallel::<TorrentsSkipMapRwLockParkingLot, _>(&rt, iters, None)
});
});

group.bench_function("DashMapMutexStd", |b| {
b.to_async(&rt)
.iter_custom(|iters| sync::update_multiple_torrents_in_parallel::<TorrentsDashMapMutexStd, _>(&rt, iters, None));
Expand Down
2 changes: 2 additions & 0 deletions packages/torrent-repository/src/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};

use self::peer_list::PeerList;

pub mod mutex_parking_lot;
pub mod mutex_std;
pub mod mutex_tokio;
pub mod peer_list;
pub mod rw_lock_parking_lot;
pub mod single;

pub trait Entry {
Expand Down
49 changes: 49 additions & 0 deletions packages/torrent-repository/src/entry/mutex_parking_lot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::net::SocketAddr;
use std::sync::Arc;

use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};

use super::{Entry, EntrySync};
use crate::{EntryMutexParkingLot, EntrySingle};

impl EntrySync for EntryMutexParkingLot {
fn get_swarm_metadata(&self) -> SwarmMetadata {
self.lock().get_swarm_metadata()
}

fn is_good(&self, policy: &TrackerPolicy) -> bool {
self.lock().is_good(policy)
}

fn peers_is_empty(&self) -> bool {
self.lock().peers_is_empty()
}

fn get_peers_len(&self) -> usize {
self.lock().get_peers_len()
}

fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
self.lock().get_peers(limit)
}

fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
self.lock().get_peers_for_client(client, limit)
}

fn upsert_peer(&self, peer: &peer::Peer) -> bool {
self.lock().upsert_peer(peer)
}

fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) {
self.lock().remove_inactive_peers(current_cutoff);
}
}

impl From<EntrySingle> for EntryMutexParkingLot {
fn from(entry: EntrySingle) -> Self {
Arc::new(parking_lot::Mutex::new(entry))
}
}
49 changes: 49 additions & 0 deletions packages/torrent-repository/src/entry/rw_lock_parking_lot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::net::SocketAddr;
use std::sync::Arc;

use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};

use super::{Entry, EntrySync};
use crate::{EntryRwLockParkingLot, EntrySingle};

impl EntrySync for EntryRwLockParkingLot {
fn get_swarm_metadata(&self) -> SwarmMetadata {
self.read().get_swarm_metadata()
}

fn is_good(&self, policy: &TrackerPolicy) -> bool {
self.read().is_good(policy)
}

fn peers_is_empty(&self) -> bool {
self.read().peers_is_empty()
}

fn get_peers_len(&self) -> usize {
self.read().get_peers_len()
}

fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
self.read().get_peers(limit)
}

fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
self.read().get_peers_for_client(client, limit)
}

fn upsert_peer(&self, peer: &peer::Peer) -> bool {
self.write().upsert_peer(peer)
}

fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) {
self.write().remove_inactive_peers(current_cutoff);
}
}

impl From<EntrySingle> for EntryRwLockParkingLot {
fn from(entry: EntrySingle) -> Self {
Arc::new(parking_lot::RwLock::new(entry))
}
}
9 changes: 9 additions & 0 deletions packages/torrent-repository/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ use torrust_tracker_clock::clock;
pub mod entry;
pub mod repository;

// Repo Entries

pub type EntrySingle = entry::Torrent;
pub type EntryMutexStd = Arc<std::sync::Mutex<entry::Torrent>>;
pub type EntryMutexTokio = Arc<tokio::sync::Mutex<entry::Torrent>>;
pub type EntryMutexParkingLot = Arc<parking_lot::Mutex<entry::Torrent>>;
pub type EntryRwLockParkingLot = Arc<parking_lot::RwLock<entry::Torrent>>;

// Repos

pub type TorrentsRwLockStd = RwLockStd<EntrySingle>;
pub type TorrentsRwLockStdMutexStd = RwLockStd<EntryMutexStd>;
Expand All @@ -21,6 +27,9 @@ pub type TorrentsRwLockTokioMutexStd = RwLockTokio<EntryMutexStd>;
pub type TorrentsRwLockTokioMutexTokio = RwLockTokio<EntryMutexTokio>;

pub type TorrentsSkipMapMutexStd = CrossbeamSkipList<EntryMutexStd>;
pub type TorrentsSkipMapMutexParkingLot = CrossbeamSkipList<EntryMutexParkingLot>;
pub type TorrentsSkipMapRwLockParkingLot = CrossbeamSkipList<EntryRwLockParkingLot>;

pub type TorrentsDashMapMutexStd = XacrimonDashMap<EntryMutexStd>;

/// This code needs to be copied into each crate.
Expand Down
Loading
Loading