Skip to content

Commit 95916cf

Browse files
reivilibresandhose
authored andcommitted
Improve progress bar configuration (and stop it clobbering the logs)
1 parent e72135e commit 95916cf

File tree

4 files changed

+32
-43
lines changed

4 files changed

+32
-43
lines changed

crates/cli/src/main.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use mas_config::{ConfigurationSection, TelemetryConfig};
1414
use sentry_tracing::EventFilter;
1515
use tracing_indicatif::{
1616
filter::{hide_indicatif_span_fields, IndicatifFilter},
17+
style::ProgressStyle,
1718
IndicatifLayer,
1819
};
1920
use tracing_subscriber::{
@@ -72,10 +73,23 @@ async fn try_main() -> anyhow::Result<ExitCode> {
7273
// Display the error if it is something other than the .env file not existing
7374
.or_else(|e| if e.not_found() { Ok(None) } else { Err(e) });
7475

76+
// Set up progress bars, used in syn2mas for example
77+
let progress_layer = IndicatifLayer::new()
78+
.with_span_field_formatter(hide_indicatif_span_fields(DefaultFields::new()))
79+
// TODO this progress style would likely benefit from being tweaked,
80+
// but we want to use a custom one to show both a bar and the message at the same time.
81+
.with_progress_style(
82+
ProgressStyle::with_template(
83+
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>10}/{len:10} {msg}",
84+
)
85+
.unwrap(),
86+
);
87+
7588
// Setup logging
7689
// This writes logs to stderr
77-
let output = std::io::stderr();
78-
let with_ansi = output.is_terminal();
90+
let with_ansi = std::io::stderr().is_terminal(); // TODO not sure this is the best way
91+
let output = progress_layer.get_stderr_writer();
92+
7993
let (log_writer, _guard) = tracing_appender::non_blocking(output);
8094
let fmt_layer = tracing_subscriber::fmt::layer()
8195
.with_writer(log_writer)
@@ -136,17 +150,12 @@ async fn try_main() -> anyhow::Result<ExitCode> {
136150
.with_filter(LevelFilter::INFO)
137151
});
138152

139-
// Set up progress bars, used in syn2mas for example
140-
let progress_layer = IndicatifLayer::new()
141-
.with_span_field_formatter(hide_indicatif_span_fields(DefaultFields::new()))
142-
.with_filter(IndicatifFilter::new(false));
143-
144153
let subscriber = Registry::default()
145154
.with(sentry_layer)
146155
.with(telemetry_layer)
147156
.with(filter_layer)
148157
.with(fmt_layer)
149-
.with(progress_layer);
158+
.with(progress_layer.with_filter(IndicatifFilter::new(false)));
150159
subscriber
151160
.try_init()
152161
.context("could not initialize logging")?;

crates/syn2mas/src/mas_writer/mod.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use thiserror::Error;
2323
use thiserror_ext::{Construct, ContextInto};
2424
use tokio::sync::mpsc::{self, Receiver, Sender};
2525
use tracing::{error, info, warn, Level, Span};
26-
use tracing_indicatif::{span_ext::IndicatifSpanExt, style::ProgressStyle};
26+
use tracing_indicatif::span_ext::IndicatifSpanExt;
2727
use uuid::Uuid;
2828

2929
use self::{
@@ -546,14 +546,6 @@ impl<'conn> MasWriter<'conn> {
546546
constraints_to_restore: &[ConstraintDescription],
547547
) -> Result<(), Error> {
548548
let span = Span::current();
549-
// TODO this style is a quick workaround for showing the message, but
550-
// might not be optimal for other purposes
551-
span.pb_set_style(
552-
&ProgressStyle::with_template(
553-
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}",
554-
)
555-
.unwrap(),
556-
);
557549
span.pb_set_length((indices_to_restore.len() + constraints_to_restore.len()) as u64);
558550

559551
// First restore all indices. The order is not important as far as I know.

crates/syn2mas/src/migration.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rand::RngCore;
3030
use thiserror::Error;
3131
use thiserror_ext::ContextInto;
3232
use tracing::{info, Level, Span};
33-
use tracing_indicatif::{span_ext::IndicatifSpanExt, style::ProgressStyle};
33+
use tracing_indicatif::span_ext::IndicatifSpanExt;
3434
use ulid::Ulid;
3535
use uuid::Uuid;
3636

@@ -107,16 +107,8 @@ pub async fn migrate(
107107
provider_id_mapping: &HashMap<String, Uuid>,
108108
) -> Result<(), Error> {
109109
let span = Span::current();
110-
// TODO this style is inconsistent with the child spans; it's just used because
111-
// the default style doesn't seem to include the message?
112-
span.pb_set_style(
113-
&ProgressStyle::with_template(
114-
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}",
115-
)
116-
.unwrap(),
117-
);
118110
span.pb_set_message("counting work");
119-
span.pb_set_length(7);
111+
span.pb_set_length(8);
120112
let counts = synapse.count_rows().await.into_synapse("counting rows")?;
121113

122114
span.pb_set_message("migrating user rows");
@@ -255,7 +247,6 @@ async fn migrate_users(
255247
let start = Instant::now();
256248

257249
let span = Span::current();
258-
span.pb_set_style(&ProgressStyle::default_bar());
259250
span.pb_set_length(user_count_hint as u64);
260251

261252
let mut user_buffer = MasWriteBuffer::new(mas, MasWriter::write_users);
@@ -323,7 +314,6 @@ async fn migrate_threepids(
323314
let start = Instant::now();
324315

325316
let span = Span::current();
326-
span.pb_set_style(&ProgressStyle::default_bar());
327317
span.pb_set_length(count_hint);
328318

329319
let mut email_buffer = MasWriteBuffer::new(mas, MasWriter::write_email_threepids);
@@ -426,7 +416,6 @@ async fn migrate_external_ids(
426416
let start = Instant::now();
427417

428418
let span = Span::current();
429-
span.pb_set_style(&ProgressStyle::default_bar());
430419
span.pb_set_length(count_hint);
431420

432421
let mut write_buffer = MasWriteBuffer::new(mas, MasWriter::write_upstream_oauth_links);
@@ -519,7 +508,6 @@ async fn migrate_devices(
519508
let start = Instant::now();
520509

521510
let span = Span::current();
522-
span.pb_set_style(&ProgressStyle::default_bar());
523511
span.pb_set_length(count_hint);
524512

525513
let mut devices_stream = pin!(synapse.read_devices());
@@ -627,7 +615,6 @@ async fn migrate_unrefreshable_access_tokens(
627615
let start = Instant::now();
628616

629617
let span = Span::current();
630-
span.pb_set_style(&ProgressStyle::default_bar());
631618
span.pb_set_length(count_hint);
632619

633620
let mut token_stream = pin!(synapse.read_unrefreshable_access_tokens());
@@ -752,7 +739,6 @@ async fn migrate_refreshable_token_pairs(
752739
let start = Instant::now();
753740

754741
let span = Span::current();
755-
span.pb_set_style(&ProgressStyle::default_bar());
756742
span.pb_set_length(count_hint);
757743

758744
let mut token_stream = pin!(synapse.read_refreshable_token_pairs());

crates/syn2mas/src/synapse_reader/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,33 +338,35 @@ impl<'conn> SynapseReader<'conn> {
338338
///
339339
/// - An underlying database error
340340
pub async fn count_rows(&mut self) -> Result<SynapseRowCounts, Error> {
341+
// For speed, retrieve a fast estimate from the statistics system
342+
// of Postgres instead. https://wiki.postgresql.org/wiki/Count_estimate
343+
// If the estimates are not up to date, the result might be `-1`, so clamp to 0.
344+
341345
// We don't get to filter out application service users by using this estimate,
342-
// which is a shame, but on a large database this is way faster.
346+
// which is a shame, but on a large database this is way faster than counting
347+
// exactly.
343348
// On matrix.org, counting users and devices properly takes around 1m10s,
344349
// which is unnecessary extra downtime during the migration, just to
345350
// show a more accurate progress bar and size a hash map accurately.
346-
let users: i64 = sqlx::query_scalar(
351+
let users: i64 = sqlx::query_scalar::<_, i64>(
347352
"
348353
SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = 'users'::regclass;
349354
",
350355
)
351356
.fetch_one(&mut *self.txn)
352357
.await
353-
.into_database("estimating count of users")?;
358+
.into_database("estimating count of users")?
359+
.max(0);
354360

355-
let devices = sqlx::query_scalar(
361+
let devices = sqlx::query_scalar::<_, i64>(
356362
"
357363
SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = 'devices'::regclass;
358364
",
359365
)
360366
.fetch_one(&mut *self.txn)
361367
.await
362-
.into_database("estimating count of devices")?;
363-
364-
// For other rows, we don't particularly care about the number except for
365-
// progress bars, so retrieve a fast estimate from the statistics system
366-
// of Postgres instead. https://wiki.postgresql.org/wiki/Count_estimate
367-
// If the estimates are not up to date, the result might be `-1`, so clamp to 0.
368+
.into_database("estimating count of devices")?
369+
.max(0);
368370

369371
let threepids = sqlx::query_scalar::<_, i64>(
370372
"

0 commit comments

Comments
 (0)