Skip to content

Commit 91f87d6

Browse files
authored
syn2mas: disable logging of slow statements, better access token query perf (#4208)
2 parents b76465e + 19d87e8 commit 91f87d6

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

crates/cli/src/commands/syn2mas.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use sqlx::{Connection, Either, PgConnection, postgres::PgConnectOptions, types::
1515
use syn2mas::{LockedMasDatabase, MasWriter, SynapseReader, synapse_config};
1616
use tracing::{Instrument, error, info_span, warn};
1717

18-
use crate::util::database_connection_from_config;
18+
use crate::util::{DatabaseConnectOptions, database_connection_from_config_with_options};
1919

2020
/// The exit code used by `syn2mas check` and `syn2mas migrate` when there are
2121
/// errors preventing migration.
@@ -114,7 +114,13 @@ impl Options {
114114

115115
let config = DatabaseConfig::extract_or_default(figment)?;
116116

117-
let mut mas_connection = database_connection_from_config(&config).await?;
117+
let mut mas_connection = database_connection_from_config_with_options(
118+
&config,
119+
&DatabaseConnectOptions {
120+
log_slow_statements: false,
121+
},
122+
)
123+
.await?;
118124

119125
MIGRATOR
120126
.run(&mut mas_connection)
@@ -225,7 +231,15 @@ impl Options {
225231
let reader = SynapseReader::new(&mut syn_conn, true).await?;
226232
let mut writer_mas_connections = Vec::with_capacity(NUM_WRITER_CONNECTIONS);
227233
for _ in 0..NUM_WRITER_CONNECTIONS {
228-
writer_mas_connections.push(database_connection_from_config(&config).await?);
234+
writer_mas_connections.push(
235+
database_connection_from_config_with_options(
236+
&config,
237+
&DatabaseConnectOptions {
238+
log_slow_statements: false,
239+
},
240+
)
241+
.await?,
242+
);
229243
}
230244
let writer = MasWriter::new(mas_connection, writer_mas_connections).await?;
231245

crates/cli/src/util.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ pub async fn templates_from_config(
231231

232232
fn database_connect_options_from_config(
233233
config: &DatabaseConfig,
234+
opts: &DatabaseConnectOptions,
234235
) -> Result<PgConnectOptions, anyhow::Error> {
235236
let options = if let Some(uri) = config.uri.as_deref() {
236237
uri.parse()
@@ -315,17 +316,19 @@ fn database_connect_options_from_config(
315316
None => options,
316317
};
317318

318-
let options = options
319-
.log_statements(LevelFilter::Debug)
320-
.log_slow_statements(LevelFilter::Warn, Duration::from_millis(100));
319+
let mut options = options.log_statements(LevelFilter::Debug);
320+
321+
if opts.log_slow_statements {
322+
options = options.log_slow_statements(LevelFilter::Warn, Duration::from_millis(100));
323+
}
321324

322325
Ok(options)
323326
}
324327

325328
/// Create a database connection pool from the configuration
326329
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
327330
pub async fn database_pool_from_config(config: &DatabaseConfig) -> Result<PgPool, anyhow::Error> {
328-
let options = database_connect_options_from_config(config)?;
331+
let options = database_connect_options_from_config(config, &DatabaseConnectOptions::default())?;
329332
PgPoolOptions::new()
330333
.max_connections(config.max_connections.into())
331334
.min_connections(config.min_connections)
@@ -337,12 +340,37 @@ pub async fn database_pool_from_config(config: &DatabaseConfig) -> Result<PgPool
337340
.context("could not connect to the database")
338341
}
339342

343+
pub struct DatabaseConnectOptions {
344+
pub log_slow_statements: bool,
345+
}
346+
347+
impl Default for DatabaseConnectOptions {
348+
fn default() -> Self {
349+
Self {
350+
log_slow_statements: true,
351+
}
352+
}
353+
}
354+
340355
/// Create a single database connection from the configuration
341356
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
342357
pub async fn database_connection_from_config(
343358
config: &DatabaseConfig,
344359
) -> Result<PgConnection, anyhow::Error> {
345-
database_connect_options_from_config(config)?
360+
database_connect_options_from_config(config, &DatabaseConnectOptions::default())?
361+
.connect()
362+
.await
363+
.context("could not connect to the database")
364+
}
365+
366+
/// Create a single database connection from the configuration,
367+
/// with specific options.
368+
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
369+
pub async fn database_connection_from_config_with_options(
370+
config: &DatabaseConfig,
371+
options: &DatabaseConnectOptions,
372+
) -> Result<PgConnection, anyhow::Error> {
373+
database_connect_options_from_config(config, options)?
346374
.connect()
347375
.await
348376
.context("could not connect to the database")

crates/syn2mas/src/synapse_reader/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ impl<'conn> SynapseReader<'conn> {
449449
INNER JOIN devices USING (user_id, device_id)
450450
WHERE at0.puppets_user_id IS NULL AND at0.refresh_token_id IS NULL
451451
452-
UNION
452+
UNION ALL
453453
454454
SELECT
455455
at0.user_id, at0.device_id, at0.token, at0.valid_until_ms, at0.last_validated
@@ -478,7 +478,7 @@ impl<'conn> SynapseReader<'conn> {
478478
SELECT
479479
rt0.user_id, rt0.device_id, at0.token AS access_token, rt0.token AS refresh_token, at0.valid_until_ms, at0.last_validated
480480
FROM refresh_tokens rt0
481-
INNER JOIN devices USING (device_id)
481+
INNER JOIN devices USING (user_id, device_id)
482482
INNER JOIN access_tokens at0 ON at0.refresh_token_id = rt0.id AND at0.user_id = rt0.user_id AND at0.device_id = rt0.device_id
483483
LEFT JOIN access_tokens at1 ON at1.refresh_token_id = rt0.next_token_id
484484
WHERE NOT at1.used OR at1.used IS NULL
@@ -505,7 +505,6 @@ mod test {
505505
},
506506
};
507507

508-
// TODO test me
509508
static MIGRATOR: Migrator = sqlx::migrate!("./test_synapse_migrations");
510509

511510
#[sqlx::test(migrator = "MIGRATOR", fixtures("user_alice"))]

0 commit comments

Comments
 (0)