Skip to content

Commit 6ff9305

Browse files
committed
Improve progress bar configuration (and stop it clobbering the logs)
1 parent 1cd080d commit 6ff9305

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
@@ -15,6 +15,7 @@ use sentry_tracing::EventFilter;
1515
use tracing_indicatif::{
1616
IndicatifLayer,
1717
filter::{IndicatifFilter, hide_indicatif_span_fields},
18+
style::ProgressStyle,
1819
};
1920
use tracing_subscriber::{
2021
EnvFilter, Layer, Registry, filter::LevelFilter, fmt::format::DefaultFields,
@@ -74,10 +75,23 @@ async fn try_main() -> anyhow::Result<ExitCode> {
7475
// Display the error if it is something other than the .env file not existing
7576
.or_else(|e| if e.not_found() { Ok(None) } else { Err(e) });
7677

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

141-
// Set up progress bars, used in syn2mas for example
142-
let progress_layer = IndicatifLayer::new()
143-
.with_span_field_formatter(hide_indicatif_span_fields(DefaultFields::new()))
144-
.with_filter(IndicatifFilter::new(false));
145-
146155
let subscriber = Registry::default()
147156
.with(sentry_layer)
148157
.with(telemetry_layer)
149158
.with(filter_layer)
150159
.with(fmt_layer)
151-
.with(progress_layer);
160+
.with(progress_layer.with_filter(IndicatifFilter::new(false)));
152161
subscriber
153162
.try_init()
154163
.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::{Level, Span, error, info, warn};
26-
use tracing_indicatif::{span_ext::IndicatifSpanExt, style::ProgressStyle};
26+
use tracing_indicatif::span_ext::IndicatifSpanExt;
2727
use uuid::Uuid;
2828

2929
use self::{
@@ -550,14 +550,6 @@ impl MasWriter {
550550
constraints_to_restore: &[ConstraintDescription],
551551
) -> Result<(), Error> {
552552
let span = Span::current();
553-
// TODO this style is a quick workaround for showing the message, but
554-
// might not be optimal for other purposes
555-
span.pb_set_style(
556-
&ProgressStyle::with_template(
557-
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}",
558-
)
559-
.unwrap(),
560-
);
561553
span.pb_set_length((indices_to_restore.len() + constraints_to_restore.len()) as u64);
562554

563555
// 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::{Level, Span, info};
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

@@ -141,16 +141,8 @@ pub async fn migrate(
141141
provider_id_mapping: HashMap<String, Uuid>,
142142
) -> Result<(), Error> {
143143
let span = Span::current();
144-
// TODO this style is inconsistent with the child spans; it's just used because
145-
// the default style doesn't seem to include the message?
146-
span.pb_set_style(
147-
&ProgressStyle::with_template(
148-
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}",
149-
)
150-
.unwrap(),
151-
);
152144
span.pb_set_message("counting work");
153-
span.pb_set_length(7);
145+
span.pb_set_length(8);
154146
let counts = synapse.count_rows().await.into_synapse("counting rows")?;
155147

156148
let state = MigrationState {
@@ -233,7 +225,6 @@ async fn migrate_users(
233225
let start = Instant::now();
234226

235227
let span = Span::current();
236-
span.pb_set_style(&ProgressStyle::default_bar());
237228
span.pb_set_length(count_hint as u64);
238229

239230
let mut user_buffer = MasWriteBuffer::new(&mas, MasWriter::write_users);
@@ -320,7 +311,6 @@ async fn migrate_threepids(
320311
let start = Instant::now();
321312

322313
let span = Span::current();
323-
span.pb_set_style(&ProgressStyle::default_bar());
324314
span.pb_set_length(count_hint as u64);
325315

326316
let mut email_buffer = MasWriteBuffer::new(&mas, MasWriter::write_email_threepids);
@@ -416,7 +406,6 @@ async fn migrate_external_ids(
416406
) -> Result<(MasWriter, MigrationState), Error> {
417407
let start = Instant::now();
418408
let span = Span::current();
419-
span.pb_set_style(&ProgressStyle::default_bar());
420409
span.pb_set_length(count_hint as u64);
421410

422411
let mut write_buffer = MasWriteBuffer::new(&mas, MasWriter::write_upstream_oauth_links);
@@ -506,7 +495,6 @@ async fn migrate_devices(
506495
let start = Instant::now();
507496

508497
let span = Span::current();
509-
span.pb_set_style(&ProgressStyle::default_bar());
510498
span.pb_set_length(count_hint as u64);
511499

512500
let mut devices_stream = pin!(synapse.read_devices());
@@ -617,7 +605,6 @@ async fn migrate_unrefreshable_access_tokens(
617605
let start = Instant::now();
618606

619607
let span = Span::current();
620-
span.pb_set_style(&ProgressStyle::default_bar());
621608
span.pb_set_length(count_hint as u64);
622609

623610
let mut token_stream = pin!(synapse.read_unrefreshable_access_tokens());
@@ -743,7 +730,6 @@ async fn migrate_refreshable_token_pairs(
743730
let start = Instant::now();
744731

745732
let span = Span::current();
746-
span.pb_set_style(&ProgressStyle::default_bar());
747733
span.pb_set_length(count_hint as u64);
748734

749735
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
@@ -340,8 +340,13 @@ impl<'conn> SynapseReader<'conn> {
340340
///
341341
/// - An underlying database error
342342
pub async fn count_rows(&mut self) -> Result<SynapseRowCounts, Error> {
343+
// For speed, retrieve a fast estimate from the statistics system
344+
// of Postgres instead. https://wiki.postgresql.org/wiki/Count_estimate
345+
// If the estimates are not up to date, the result might be `-1`, so clamp to 0.
346+
343347
// We don't get to filter out application service users by using this estimate,
344-
// which is a shame, but on a large database this is way faster.
348+
// which is a shame, but on a large database this is way faster than counting
349+
// exactly.
345350
// On matrix.org, counting users and devices properly takes around 1m10s,
346351
// which is unnecessary extra downtime during the migration, just to
347352
// show a more accurate progress bar and size a hash map accurately.
@@ -369,11 +374,6 @@ impl<'conn> SynapseReader<'conn> {
369374
.try_into()
370375
.unwrap_or(usize::MAX);
371376

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

0 commit comments

Comments
 (0)