Skip to content

Commit 5fc97d1

Browse files
authored
syn2mas: Add progress reporting to log and to opentelemetry metrics (#4215)
2 parents 3fc9726 + 9228f20 commit 5fc97d1

File tree

10 files changed

+396
-16
lines changed

10 files changed

+396
-16
lines changed

Diff for: Cargo.lock

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ syn2mas = { path = "./crates/syn2mas", version = "=0.14.1" }
6161
version = "0.14.1"
6262
features = ["axum", "axum-extra", "axum-json", "axum-query", "macros"]
6363

64+
[workspace.dependencies.arc-swap]
65+
version = "1.7.1"
66+
6467
# GraphQL server
6568
[workspace.dependencies.async-graphql]
6669
version = "7.0.15"

Diff for: crates/cli/src/commands/syn2mas.rs

+44-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, process::ExitCode};
1+
use std::{collections::HashMap, process::ExitCode, sync::atomic::Ordering, time::Duration};
22

33
use anyhow::Context;
44
use camino::Utf8PathBuf;
@@ -12,8 +12,10 @@ use mas_storage::SystemClock;
1212
use mas_storage_pg::MIGRATOR;
1313
use rand::thread_rng;
1414
use sqlx::{Connection, Either, PgConnection, postgres::PgConnectOptions, types::Uuid};
15-
use syn2mas::{LockedMasDatabase, MasWriter, SynapseReader, synapse_config};
16-
use tracing::{Instrument, error, info_span, warn};
15+
use syn2mas::{
16+
LockedMasDatabase, MasWriter, Progress, ProgressStage, SynapseReader, synapse_config,
17+
};
18+
use tracing::{Instrument, error, info, info_span, warn};
1719

1820
use crate::util::{DatabaseConnectOptions, database_connection_from_config_with_options};
1921

@@ -248,7 +250,11 @@ impl Options {
248250
#[allow(clippy::disallowed_methods)]
249251
let mut rng = thread_rng();
250252

251-
// TODO progress reporting
253+
let progress = Progress::default();
254+
255+
let occasional_progress_logger_task =
256+
tokio::spawn(occasional_progress_logger(progress.clone()));
257+
252258
let mas_matrix = MatrixConfig::extract(figment)?;
253259
eprintln!("\n\n");
254260
syn2mas::migrate(
@@ -258,11 +264,45 @@ impl Options {
258264
&clock,
259265
&mut rng,
260266
provider_id_mappings,
267+
&progress,
261268
)
262269
.await?;
263270

271+
occasional_progress_logger_task.abort();
272+
264273
Ok(ExitCode::SUCCESS)
265274
}
266275
}
267276
}
268277
}
278+
279+
/// Logs progress every 30 seconds, as a lightweight alternative to a progress
280+
/// bar. For most deployments, the migration will not take 30 seconds so this
281+
/// will not be relevant. In other cases, this will give the operator an idea of
282+
/// what's going on.
283+
async fn occasional_progress_logger(progress: Progress) {
284+
loop {
285+
tokio::time::sleep(Duration::from_secs(30)).await;
286+
match &**progress.get_current_stage() {
287+
ProgressStage::SettingUp => {
288+
info!(name: "progress", "still setting up");
289+
}
290+
ProgressStage::MigratingData {
291+
entity,
292+
migrated,
293+
approx_count,
294+
} => {
295+
let migrated = migrated.load(Ordering::Relaxed);
296+
#[allow(clippy::cast_precision_loss)]
297+
let percent = (f64::from(migrated) / *approx_count as f64) * 100.0;
298+
info!(name: "progress", "migrating {entity}: {migrated}/~{approx_count} (~{percent:.1}%)");
299+
}
300+
ProgressStage::RebuildIndex { index_name } => {
301+
info!(name: "progress", "still waiting for rebuild of index {index_name}");
302+
}
303+
ProgressStage::RebuildConstraint { constraint_name } => {
304+
info!(name: "progress", "still waiting for rebuild of constraint {constraint_name}");
305+
}
306+
}
307+
}
308+
}

Diff for: crates/syn2mas/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ repository.workspace = true
1111

1212
[dependencies]
1313
anyhow.workspace = true
14+
arc-swap.workspace = true
1415
bitflags.workspace = true
1516
camino.workspace = true
1617
figment.workspace = true
@@ -34,6 +35,9 @@ ulid = { workspace = true, features = ["uuid"] }
3435
mas-config.workspace = true
3536
mas-storage.workspace = true
3637

38+
opentelemetry.workspace = true
39+
opentelemetry-semantic-conventions.workspace = true
40+
3741
[dev-dependencies]
3842
mas-storage-pg.workspace = true
3943

Diff for: crates/syn2mas/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ mod mas_writer;
77
mod synapse_reader;
88

99
mod migration;
10+
mod progress;
11+
mod telemetry;
1012

1113
type RandomState = rustc_hash::FxBuildHasher;
1214
type HashMap<K, V> = rustc_hash::FxHashMap<K, V>;
1315

1416
pub use self::{
1517
mas_writer::{MasWriter, checks::mas_pre_migration_checks, locking::LockedMasDatabase},
1618
migration::migrate,
19+
progress::{Progress, ProgressStage},
1720
synapse_reader::{
1821
SynapseReader,
1922
checks::{

0 commit comments

Comments
 (0)