Skip to content

Commit 1044b16

Browse files
reivilibresandhose
authored andcommitted
Improve progress bar configuration (and stop it clobbering the logs)
1 parent bc45146 commit 1044b16

File tree

4 files changed

+26
-39
lines changed

4 files changed

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

560552
// 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
@@ -21,7 +21,7 @@ use rand::RngCore;
2121
use thiserror::Error;
2222
use thiserror_ext::ContextInto;
2323
use tracing::{info, Level, Span};
24-
use tracing_indicatif::{span_ext::IndicatifSpanExt, style::ProgressStyle};
24+
use tracing_indicatif::span_ext::IndicatifSpanExt;
2525
use ulid::Ulid;
2626
use uuid::Uuid;
2727

@@ -134,16 +134,8 @@ pub async fn migrate(
134134
provider_id_mapping: HashMap<String, Uuid>,
135135
) -> Result<(), Error> {
136136
let span = Span::current();
137-
// TODO this style is inconsistent with the child spans; it's just used because
138-
// the default style doesn't seem to include the message?
139-
span.pb_set_style(
140-
&ProgressStyle::with_template(
141-
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}",
142-
)
143-
.unwrap(),
144-
);
145137
span.pb_set_message("counting work");
146-
span.pb_set_length(7);
138+
span.pb_set_length(8);
147139
let counts = synapse.count_rows().await.into_synapse("counting rows")?;
148140

149141
let state = MigrationState {
@@ -226,7 +218,6 @@ async fn migrate_users(
226218
let start = Instant::now();
227219

228220
let span = Span::current();
229-
span.pb_set_style(&ProgressStyle::default_bar());
230221
span.pb_set_length(count_hint as u64);
231222

232223
let mut user_buffer = MasWriteBuffer::new(&mas, MasWriter::write_users);
@@ -299,7 +290,6 @@ async fn migrate_threepids(
299290
let start = Instant::now();
300291

301292
let span = Span::current();
302-
span.pb_set_style(&ProgressStyle::default_bar());
303293
span.pb_set_length(count_hint as u64);
304294

305295
let mut email_buffer = MasWriteBuffer::new(&mas, MasWriter::write_email_threepids);
@@ -398,7 +388,6 @@ async fn migrate_external_ids(
398388
) -> Result<(MasWriter, MigrationState), Error> {
399389
let start = Instant::now();
400390
let span = Span::current();
401-
span.pb_set_style(&ProgressStyle::default_bar());
402391
span.pb_set_length(count_hint as u64);
403392

404393
let mut write_buffer = MasWriteBuffer::new(&mas, MasWriter::write_upstream_oauth_links);
@@ -487,7 +476,6 @@ async fn migrate_devices(
487476
let start = Instant::now();
488477

489478
let span = Span::current();
490-
span.pb_set_style(&ProgressStyle::default_bar());
491479
span.pb_set_length(count_hint as u64);
492480

493481
let mut devices_stream = pin!(synapse.read_devices());
@@ -596,7 +584,6 @@ async fn migrate_unrefreshable_access_tokens(
596584
let start = Instant::now();
597585

598586
let span = Span::current();
599-
span.pb_set_style(&ProgressStyle::default_bar());
600587
span.pb_set_length(count_hint as u64);
601588

602589
let mut token_stream = pin!(synapse.read_unrefreshable_access_tokens());
@@ -722,7 +709,6 @@ async fn migrate_refreshable_token_pairs(
722709
let start = Instant::now();
723710

724711
let span = Span::current();
725-
span.pb_set_style(&ProgressStyle::default_bar());
726712
span.pb_set_length(count_hint as u64);
727713

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

crates/syn2mas/src/synapse_reader/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,13 @@ 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.
@@ -367,11 +372,6 @@ impl<'conn> SynapseReader<'conn> {
367372
.try_into()
368373
.unwrap_or(usize::MAX);
369374

370-
// For other rows, we don't particularly care about the number except for
371-
// progress bars, so retrieve a fast estimate from the statistics system
372-
// of Postgres instead. https://wiki.postgresql.org/wiki/Count_estimate
373-
// If the estimates are not up to date, the result might be `-1`, so clamp to 0.
374-
375375
let threepids = sqlx::query_scalar::<_, i64>(
376376
"
377377
SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = 'user_threepids'::regclass;

0 commit comments

Comments
 (0)