forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql.rs
More file actions
483 lines (375 loc) · 18.2 KB
/
mysql.rs
File metadata and controls
483 lines (375 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! The `MySQL` database driver.
//!
//! This module provides an implementation of the [`Database`] trait for `MySQL`
//! using the `r2d2_mysql` connection pool. It configures the MySQL connection
//! based on a URL, creates the necessary tables (for torrent metrics, torrent
//! whitelist, and authentication keys), and implements all CRUD operations
//! required by the persistence layer.
use std::str::FromStr;
use std::time::Duration;
use bittorrent_primitives::info_hash::InfoHash;
use r2d2::Pool;
use r2d2_mysql::mysql::prelude::Queryable;
use r2d2_mysql::mysql::{params, Opts, OptsBuilder};
use r2d2_mysql::MySqlConnectionManager;
use torrust_tracker_primitives::{NumberOfDownloads, NumberOfDownloadsBTreeMap};
use super::{Database, Driver, Error, TORRENTS_DOWNLOADS_TOTAL};
use crate::authentication::key::AUTH_KEY_LENGTH;
use crate::authentication::{self, Key};
const DRIVER: Driver = Driver::MySQL;
/// `MySQL` driver implementation.
///
/// This struct encapsulates a connection pool for `MySQL`, built using the
/// `r2d2_mysql` connection manager. It implements the [`Database`] trait to
/// provide persistence operations.
pub(crate) struct Mysql {
pool: Pool<MySqlConnectionManager>,
}
impl Mysql {
/// It instantiates a new `MySQL` database driver.
///
/// Refer to [`databases::Database::new`](crate::core::databases::Database::new).
///
/// # Errors
///
/// Will return `r2d2::Error` if `db_path` is not able to create `MySQL` database.
pub fn new(db_path: &str) -> Result<Self, Error> {
let opts = Opts::from_url(db_path)?;
let builder = OptsBuilder::from_opts(opts);
let manager = MySqlConnectionManager::new(builder);
let pool = r2d2::Pool::builder().build(manager).map_err(|e| (e, DRIVER))?;
Ok(Self { pool })
}
fn load_torrent_aggregate_metric(&self, metric_name: &str) -> Result<Option<NumberOfDownloads>, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let query = conn.exec_first::<u32, _, _>(
"SELECT value FROM torrent_aggregate_metrics WHERE metric_name = :metric_name",
params! { "metric_name" => metric_name },
);
let persistent_torrent = query?;
Ok(persistent_torrent)
}
fn save_torrent_aggregate_metric(&self, metric_name: &str, completed: NumberOfDownloads) -> Result<(), Error> {
const COMMAND : &str = "INSERT INTO torrent_aggregate_metrics (metric_name, value) VALUES (:metric_name, :completed) ON DUPLICATE KEY UPDATE value = VALUES(value)";
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
Ok(conn.exec_drop(COMMAND, params! { metric_name, completed })?)
}
}
impl Database for Mysql {
/// Refer to [`databases::Database::create_database_tables`](crate::core::databases::Database::create_database_tables).
fn create_database_tables(&self) -> Result<(), Error> {
let create_whitelist_table = "
CREATE TABLE IF NOT EXISTS whitelist (
id integer PRIMARY KEY AUTO_INCREMENT,
info_hash VARCHAR(40) NOT NULL UNIQUE
);"
.to_string();
let create_torrents_table = "
CREATE TABLE IF NOT EXISTS torrents (
id integer PRIMARY KEY AUTO_INCREMENT,
info_hash VARCHAR(40) NOT NULL UNIQUE,
completed INTEGER DEFAULT 0 NOT NULL
);"
.to_string();
let create_torrent_aggregate_metrics_table = "
CREATE TABLE IF NOT EXISTS torrent_aggregate_metrics (
id integer PRIMARY KEY AUTO_INCREMENT,
metric_name VARCHAR(50) NOT NULL UNIQUE,
value INTEGER DEFAULT 0 NOT NULL
);"
.to_string();
let create_keys_table = format!(
"
CREATE TABLE IF NOT EXISTS `keys` (
`id` INT NOT NULL AUTO_INCREMENT,
`key` VARCHAR({}) NOT NULL,
`valid_until` INT(10),
PRIMARY KEY (`id`),
UNIQUE (`key`)
);",
i8::try_from(AUTH_KEY_LENGTH).expect("authentication key length should fit within a i8!")
);
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
conn.query_drop(&create_torrents_table)
.expect("Could not create torrents table.");
conn.query_drop(&create_torrent_aggregate_metrics_table)
.expect("Could not create create_torrent_aggregate_metrics_table table.");
conn.query_drop(&create_keys_table).expect("Could not create keys table.");
conn.query_drop(&create_whitelist_table)
.expect("Could not create whitelist table.");
Ok(())
}
/// Refer to [`databases::Database::drop_database_tables`](crate::core::databases::Database::drop_database_tables).
fn drop_database_tables(&self) -> Result<(), Error> {
let drop_whitelist_table = "
DROP TABLE `whitelist`;"
.to_string();
let drop_torrents_table = "
DROP TABLE `torrents`;"
.to_string();
let drop_keys_table = "
DROP TABLE `keys`;"
.to_string();
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
conn.query_drop(&drop_whitelist_table)
.expect("Could not drop `whitelist` table.");
conn.query_drop(&drop_torrents_table)
.expect("Could not drop `torrents` table.");
conn.query_drop(&drop_keys_table).expect("Could not drop `keys` table.");
Ok(())
}
/// Refer to [`databases::Database::load_persistent_torrents`](crate::core::databases::Database::load_persistent_torrents).
fn load_all_torrents_downloads(&self) -> Result<NumberOfDownloadsBTreeMap, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let torrents = conn.query_map(
"SELECT info_hash, completed FROM torrents",
|(info_hash_string, completed): (String, u32)| {
let info_hash = InfoHash::from_str(&info_hash_string).unwrap();
(info_hash, completed)
},
)?;
Ok(torrents.iter().copied().collect())
}
/// Refer to [`databases::Database::load_persistent_torrent`](crate::core::databases::Database::load_persistent_torrent).
fn load_torrent_downloads(&self, info_hash: &InfoHash) -> Result<Option<NumberOfDownloads>, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let query = conn.exec_first::<u32, _, _>(
"SELECT completed FROM torrents WHERE info_hash = :info_hash",
params! { "info_hash" => info_hash.to_hex_string() },
);
let persistent_torrent = query?;
Ok(persistent_torrent)
}
/// Refer to [`databases::Database::save_persistent_torrent`](crate::core::databases::Database::save_persistent_torrent).
fn save_torrent_downloads(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error> {
const COMMAND : &str = "INSERT INTO torrents (info_hash, completed) VALUES (:info_hash_str, :completed) ON DUPLICATE KEY UPDATE completed = VALUES(completed)";
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let info_hash_str = info_hash.to_string();
Ok(conn.exec_drop(COMMAND, params! { info_hash_str, completed })?)
}
/// Refer to [`databases::Database::increase_number_of_downloads`](crate::core::databases::Database::increase_number_of_downloads).
fn increase_downloads_for_torrent(&self, info_hash: &InfoHash) -> Result<(), Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let info_hash_str = info_hash.to_string();
conn.exec_drop(
"UPDATE torrents SET completed = completed + 1 WHERE info_hash = :info_hash_str",
params! { info_hash_str },
)?;
Ok(())
}
/// Refer to [`databases::Database::load_global_number_of_downloads`](crate::core::databases::Database::load_global_number_of_downloads).
fn load_global_downloads(&self) -> Result<Option<NumberOfDownloads>, Error> {
self.load_torrent_aggregate_metric(TORRENTS_DOWNLOADS_TOTAL)
}
/// Refer to [`databases::Database::save_global_number_of_downloads`](crate::core::databases::Database::save_global_number_of_downloads).
fn save_global_downloads(&self, downloaded: NumberOfDownloads) -> Result<(), Error> {
self.save_torrent_aggregate_metric(TORRENTS_DOWNLOADS_TOTAL, downloaded)
}
/// Refer to [`databases::Database::increase_global_number_of_downloads`](crate::core::databases::Database::increase_global_number_of_downloads).
fn increase_global_downloads(&self) -> Result<(), Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let metric_name = TORRENTS_DOWNLOADS_TOTAL;
conn.exec_drop(
"UPDATE torrent_aggregate_metrics SET value = value + 1 WHERE metric_name = :metric_name",
params! { metric_name },
)?;
Ok(())
}
/// Refer to [`databases::Database::load_keys`](crate::core::databases::Database::load_keys).
fn load_keys(&self) -> Result<Vec<authentication::PeerKey>, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let keys = conn.query_map(
"SELECT `key`, valid_until FROM `keys`",
|(key, valid_until): (String, Option<i64>)| match valid_until {
Some(valid_until) => authentication::PeerKey {
key: key.parse::<Key>().unwrap(),
valid_until: Some(Duration::from_secs(valid_until.unsigned_abs())),
},
None => authentication::PeerKey {
key: key.parse::<Key>().unwrap(),
valid_until: None,
},
},
)?;
Ok(keys)
}
/// Refer to [`databases::Database::load_whitelist`](crate::core::databases::Database::load_whitelist).
fn load_whitelist(&self) -> Result<Vec<InfoHash>, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let info_hashes = conn.query_map("SELECT info_hash FROM whitelist", |info_hash: String| {
InfoHash::from_str(&info_hash).unwrap()
})?;
Ok(info_hashes)
}
/// Refer to [`databases::Database::get_info_hash_from_whitelist`](crate::core::databases::Database::get_info_hash_from_whitelist).
fn get_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<Option<InfoHash>, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let select = conn.exec_first::<String, _, _>(
"SELECT info_hash FROM whitelist WHERE info_hash = :info_hash",
params! { "info_hash" => info_hash.to_hex_string() },
)?;
let info_hash = select.map(|f| InfoHash::from_str(&f).expect("Failed to decode InfoHash String from DB!"));
Ok(info_hash)
}
/// Refer to [`databases::Database::add_info_hash_to_whitelist`](crate::core::databases::Database::add_info_hash_to_whitelist).
fn add_info_hash_to_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let info_hash_str = info_hash.to_string();
conn.exec_drop(
"INSERT INTO whitelist (info_hash) VALUES (:info_hash_str)",
params! { info_hash_str },
)?;
Ok(1)
}
/// Refer to [`databases::Database::remove_info_hash_from_whitelist`](crate::core::databases::Database::remove_info_hash_from_whitelist).
fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let info_hash = info_hash.to_string();
conn.exec_drop("DELETE FROM whitelist WHERE info_hash = :info_hash", params! { info_hash })?;
Ok(1)
}
/// Refer to [`databases::Database::get_key_from_keys`](crate::core::databases::Database::get_key_from_keys).
fn get_key_from_keys(&self, key: &Key) -> Result<Option<authentication::PeerKey>, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
let query = conn.exec_first::<(String, Option<i64>), _, _>(
"SELECT `key`, valid_until FROM `keys` WHERE `key` = :key",
params! { "key" => key.to_string() },
);
let key = query?;
Ok(key.map(|(key, opt_valid_until)| match opt_valid_until {
Some(valid_until) => authentication::PeerKey {
key: key.parse::<Key>().unwrap(),
valid_until: Some(Duration::from_secs(valid_until.unsigned_abs())),
},
None => authentication::PeerKey {
key: key.parse::<Key>().unwrap(),
valid_until: None,
},
}))
}
/// Refer to [`databases::Database::add_key_to_keys`](crate::core::databases::Database::add_key_to_keys).
fn add_key_to_keys(&self, auth_key: &authentication::PeerKey) -> Result<usize, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
match auth_key.valid_until {
Some(valid_until) => conn.exec_drop(
"INSERT INTO `keys` (`key`, valid_until) VALUES (:key, :valid_until)",
params! { "key" => auth_key.key.to_string(), "valid_until" => valid_until.as_secs().to_string() },
)?,
None => conn.exec_drop(
"INSERT INTO `keys` (`key`) VALUES (:key)",
params! { "key" => auth_key.key.to_string() },
)?,
}
Ok(1)
}
/// Refer to [`databases::Database::remove_key_from_keys`](crate::core::databases::Database::remove_key_from_keys).
fn remove_key_from_keys(&self, key: &Key) -> Result<usize, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
conn.exec_drop("DELETE FROM `keys` WHERE `key` = :key", params! { "key" => key.to_string() })?;
Ok(1)
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use testcontainers::core::IntoContainerPort;
/*
We run a MySQL container and run all the tests against the same container and database.
Test for this driver are executed with:
`TORRUST_TRACKER_CORE_RUN_MYSQL_DRIVER_TEST=true cargo test`
The `Database` trait is very simple and we only have one driver that needs
a container. In the future we might want to use different approaches like:
- https://github.com/testcontainers/testcontainers-rs/issues/707
- https://www.infinyon.com/blog/2021/04/rust-custom-test-harness/
- https://github.com/torrust/torrust-tracker/blob/develop/src/bin/e2e_tests_runner.rs
If we increase the number of methods or the number or drivers.
*/
use testcontainers::runners::AsyncRunner;
use testcontainers::{ContainerAsync, GenericImage, ImageExt};
use torrust_tracker_configuration::Core;
use super::Mysql;
use crate::databases::driver::tests::run_tests;
use crate::databases::Database;
#[derive(Debug, Default)]
struct StoppedMysqlContainer {}
impl StoppedMysqlContainer {
async fn run(self, config: &MysqlConfiguration) -> Result<RunningMysqlContainer, Box<dyn std::error::Error + 'static>> {
let container = GenericImage::new("mysql", "8.0")
.with_exposed_port(config.internal_port.tcp())
// todo: this does not work
//.with_wait_for(WaitFor::message_on_stdout("ready for connections"))
.with_env_var("MYSQL_ROOT_PASSWORD", config.db_root_password.clone())
.with_env_var("MYSQL_DATABASE", config.database.clone())
.with_env_var("MYSQL_ROOT_HOST", "%")
.start()
.await?;
Ok(RunningMysqlContainer::new(container, config.internal_port))
}
}
struct RunningMysqlContainer {
container: ContainerAsync<GenericImage>,
internal_port: u16,
}
impl RunningMysqlContainer {
fn new(container: ContainerAsync<GenericImage>, internal_port: u16) -> Self {
Self {
container,
internal_port,
}
}
async fn stop(self) {
self.container.stop().await.unwrap();
}
async fn get_host(&self) -> url::Host {
self.container.get_host().await.unwrap()
}
async fn get_host_port_ipv4(&self) -> u16 {
self.container.get_host_port_ipv4(self.internal_port).await.unwrap()
}
}
impl Default for MysqlConfiguration {
fn default() -> Self {
Self {
internal_port: 3306,
database: "torrust_tracker_test".to_string(),
db_user: "root".to_string(),
db_root_password: "test".to_string(),
}
}
}
struct MysqlConfiguration {
pub internal_port: u16,
pub database: String,
pub db_user: String,
pub db_root_password: String,
}
fn core_configuration(host: &url::Host, port: u16, mysql_configuration: &MysqlConfiguration) -> Core {
let mut config = Core::default();
let database = mysql_configuration.database.clone();
let db_user = mysql_configuration.db_user.clone();
let db_password = mysql_configuration.db_root_password.clone();
config.database.path = format!("mysql://{db_user}:{db_password}@{host}:{port}/{database}");
config
}
fn initialize_driver(config: &Core) -> Arc<Box<dyn Database>> {
let driver: Arc<Box<dyn Database>> = Arc::new(Box::new(Mysql::new(&config.database.path).unwrap()));
driver
}
#[tokio::test]
async fn run_mysql_driver_tests() -> Result<(), Box<dyn std::error::Error + 'static>> {
if std::env::var("TORRUST_TRACKER_CORE_RUN_MYSQL_DRIVER_TEST").is_err() {
println!("Skipping the MySQL driver tests.");
return Ok(());
}
let mysql_configuration = MysqlConfiguration::default();
let stopped_mysql_container = StoppedMysqlContainer::default();
let mysql_container = stopped_mysql_container.run(&mysql_configuration).await.unwrap();
let host = mysql_container.get_host().await;
let port = mysql_container.get_host_port_ipv4().await;
let config = core_configuration(&host, port, &mysql_configuration);
let driver = initialize_driver(&config);
run_tests(&driver).await;
mysql_container.stop().await;
Ok(())
}
}