Skip to content

Commit 2edc0e3

Browse files
committed
fix(cardano-chain-follower): thread stat name constants to its own file
Signed-off-by: bkioshn <[email protected]>
1 parent 329455f commit 2edc0e3

File tree

11 files changed

+75
-57
lines changed

11 files changed

+75
-57
lines changed

rust/cardano-chain-follower/src/chain_sync.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,11 @@ async fn live_sync_backfill(
414414
/// Call the live sync backfill.
415415
/// This is a helper function to pause and resume the stats thread.
416416
async fn call_live_sync_backfill(
417-
cfg: &ChainSyncConfig, name: &str, update: &MithrilUpdateMessage,
417+
cfg: &ChainSyncConfig, update: &MithrilUpdateMessage,
418418
) -> anyhow::Result<()> {
419-
stats::pause_thread(cfg.chain, name);
419+
stats::pause_thread(cfg.chain, stats::thread::name::LIVE_SYNC_BACKFILL_AND_PURGE);
420420
let result = live_sync_backfill(cfg, update).await;
421-
stats::resume_thread(cfg.chain, name);
421+
stats::resume_thread(cfg.chain, stats::thread::name::LIVE_SYNC_BACKFILL_AND_PURGE);
422422
result
423423
}
424424

@@ -427,14 +427,15 @@ async fn live_sync_backfill_and_purge(
427427
cfg: ChainSyncConfig, mut rx: mpsc::Receiver<MithrilUpdateMessage>,
428428
mut sync_ready: SyncReadyWaiter,
429429
) {
430-
/// Thread name for stats.
431-
const THREAD_NAME: &str = "LiveSyncBackfillAndPurge";
432-
433-
stats::start_thread(cfg.chain, THREAD_NAME, true);
430+
stats::start_thread(
431+
cfg.chain,
432+
stats::thread::name::LIVE_SYNC_BACKFILL_AND_PURGE,
433+
true,
434+
);
434435
// Wait for first Mithril Update advice, which triggers a BACKFILL of the Live Data.
435436
let Some(update) = rx.recv().await else {
436437
error!("Mithril Sync Failed, can not continue chain sync either.");
437-
stats::stop_thread(cfg.chain, THREAD_NAME);
438+
stats::stop_thread(cfg.chain, stats::thread::name::LIVE_SYNC_BACKFILL_AND_PURGE);
438439
return;
439440
};
440441

@@ -449,7 +450,7 @@ async fn live_sync_backfill_and_purge(
449450
// We will re-attempt backfill, until its successful.
450451
// Backfill is atomic, it either fully works, or none of the live-chain is changed.
451452
debug!("Mithril Tip has advanced to: {update:?} : BACKFILL");
452-
while let Err(error) = call_live_sync_backfill(&cfg, THREAD_NAME, &update).await {
453+
while let Err(error) = call_live_sync_backfill(&cfg, &update).await {
453454
error!("Mithril Backfill Sync Failed: {}", error);
454455
sleep(Duration::from_secs(10)).await;
455456
}
@@ -480,7 +481,7 @@ async fn live_sync_backfill_and_purge(
480481
loop {
481482
let Some(update) = rx.recv().await else {
482483
error!("Mithril Sync Failed, can not continue chain sync either.");
483-
stats::stop_thread(cfg.chain, THREAD_NAME);
484+
stats::stop_thread(cfg.chain, stats::thread::name::LIVE_SYNC_BACKFILL_AND_PURGE);
484485
return;
485486
};
486487

rust/cardano-chain-follower/src/chain_sync_config.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ impl ChainSyncConfig {
125125
///
126126
/// `Error`: On error.
127127
pub async fn run(self) -> Result<()> {
128-
/// Thread name for stats.
129-
const THREAD_NAME: &str = "ChainSync";
130128
debug!(
131129
chain = self.chain.to_string(),
132130
"Chain Synchronization Starting"
@@ -154,7 +152,7 @@ impl ChainSyncConfig {
154152

155153
// Wrap inside a panic catcher to detect if the task panics.
156154
let result = panic::catch_unwind(|| {
157-
stats::start_thread(self.chain, THREAD_NAME, true);
155+
stats::start_thread(self.chain, stats::thread::name::CHAIN_SYNC, true);
158156
// Start Chain Sync
159157
tokio::spawn(chain_sync(self.clone(), rx))
160158
});
@@ -167,7 +165,7 @@ impl ChainSyncConfig {
167165
chain = self.chain.to_string(),
168166
"Chain Sync for {} : PANICKED", self.chain
169167
);
170-
stats::stop_thread(self.chain, THREAD_NAME);
168+
stats::stop_thread(self.chain, stats::thread::name::CHAIN_SYNC);
171169
}
172170

173171
// sync_map.insert(chain, handle);

rust/cardano-chain-follower/src/chain_sync_ready.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,10 @@ static SYNC_READY: LazyLock<DashMap<Network, RwLock<SyncReady>>> = LazyLock::new
8585
/// Write Lock the `SYNC_READY` lock for a network.
8686
/// When we are signaled to be ready, set it to true and release the lock.
8787
pub(crate) fn wait_for_sync_ready(chain: Network) -> SyncReadyWaiter {
88-
/// Thread name for stats.
89-
const THREAD_NAME: &str = "WaitForSyncReady";
90-
9188
let (tx, rx) = oneshot::channel::<()>();
9289

9390
tokio::spawn(async move {
94-
stats::start_thread(chain, THREAD_NAME, true);
91+
stats::start_thread(chain, stats::thread::name::WAIT_FOR_SYNC_READY, true);
9592
// We are safe to use `expect` here because the SYNC_READY list is exhaustively
9693
// initialized. Its a Serious BUG if that not True, so panic is OK.
9794
#[allow(clippy::expect_used)]
@@ -105,7 +102,7 @@ pub(crate) fn wait_for_sync_ready(chain: Network) -> SyncReadyWaiter {
105102
if let Ok(()) = rx.await {
106103
status.ready = true;
107104
}
108-
stats::stop_thread(chain, THREAD_NAME);
105+
stats::stop_thread(chain, stats::thread::name::WAIT_FOR_SYNC_READY);
109106
// If the channel closes early, we can NEVER use the Blockchain data.
110107
});
111108

rust/cardano-chain-follower/src/mithril_query.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ pub(crate) type ImmutableBlockIterator = Box<dyn Iterator<Item = FallibleBlock>
1818
pub(crate) async fn make_mithril_iterator(
1919
path: &Path, start: &Point, chain: Network,
2020
) -> Result<ImmutableBlockIterator> {
21-
/// Thread name for stats.
22-
const THREAD_NAME: &str = "MithrilIterator";
23-
2421
let path = path.to_path_buf();
2522
let start = start.clone();
2623
// Initial input
2724
let res = task::spawn_blocking(move || {
28-
stats::start_thread(chain, THREAD_NAME, false);
25+
stats::start_thread(chain, stats::thread::name::MITHRIL_ITERATOR, false);
2926
let result =
3027
pallas_hardano::storage::immutable::read_blocks_from_point(&path, start.clone().into())
3128
.map_err(|error| Error::MithrilSnapshot(Some(error)));
32-
stats::stop_thread(chain, THREAD_NAME);
29+
stats::stop_thread(chain, stats::thread::name::MITHRIL_ITERATOR);
3330
result
3431
})
3532
.await;

rust/cardano-chain-follower/src/mithril_snapshot_config.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,6 @@ impl MithrilSnapshotConfig {
386386

387387
/// Run a Mithril Follower for the given network and configuration.
388388
pub(crate) async fn run(&self) -> Result<mpsc::Receiver<MithrilUpdateMessage>> {
389-
/// Thread name for stats.
390-
const THREAD_NAME: &str = "MithrilSnapshotUpdater";
391-
392389
debug!(
393390
chain = self.chain.to_string(),
394391
"Mithril Auto-update : Starting"
@@ -421,7 +418,11 @@ impl MithrilSnapshotConfig {
421418

422419
// Wrap inside a panic catcher to detect if the task panics.
423420
let result = panic::catch_unwind(|| {
424-
stats::start_thread(self.chain, THREAD_NAME, true);
421+
stats::start_thread(
422+
self.chain,
423+
stats::thread::name::MITHRIL_SNAPSHOT_UPDATER,
424+
true,
425+
);
425426
tokio::spawn(background_mithril_update(self.clone(), tx))
426427
});
427428

@@ -433,7 +434,7 @@ impl MithrilSnapshotConfig {
433434
chain = self.chain.to_string(),
434435
"Background Mithril Update for {} : PANICKED", self.chain
435436
);
436-
stats::stop_thread(self.chain, THREAD_NAME);
437+
stats::stop_thread(self.chain, stats::thread::name::MITHRIL_SNAPSHOT_UPDATER);
437438
}
438439

439440
// sync_map.insert(chain, handle);

rust/cardano-chain-follower/src/mithril_snapshot_iterator.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,21 @@ impl MithrilSnapshotIterator {
192192
/// Get the next block, in a way that is Async friendly.
193193
/// Returns the next block, or None if there are no more blocks.
194194
pub(crate) async fn next(&self) -> Option<MultiEraBlock> {
195-
/// Thread name for stats.
196-
const THREAD_NAME: &str = "MithrilSnapshotIterator::Next";
197-
198195
let inner = self.inner.clone();
199196

200197
let res = task::spawn_blocking(move || {
201198
#[allow(clippy::unwrap_used)] // Unwrap is safe here because the lock can't be poisoned.
202199
let mut inner_iterator = inner.lock().unwrap();
203-
stats::start_thread(inner_iterator.chain, THREAD_NAME, false);
200+
stats::start_thread(
201+
inner_iterator.chain,
202+
stats::thread::name::MITHRIL_ITERATOR_NEXT,
203+
false,
204+
);
204205
let next = inner_iterator.next();
205-
stats::stop_thread(inner_iterator.chain, THREAD_NAME);
206+
stats::stop_thread(
207+
inner_iterator.chain,
208+
stats::thread::name::MITHRIL_ITERATOR_NEXT,
209+
);
206210
next
207211
})
208212
.await;

rust/cardano-chain-follower/src/mithril_snapshot_sync.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -295,19 +295,16 @@ async fn get_mithril_snapshot_and_certificate(
295295
async fn validate_mithril_snapshot(
296296
chain: Network, certificate: &MithrilCertificate, path: &Path,
297297
) -> bool {
298-
/// Thread name for stats.
299-
const THREAD_NAME: &str = "ValidateMithrilSnapshot";
300-
301298
let cert = certificate.clone();
302299
let mithril_path = path.to_path_buf();
303300
match tokio::spawn(async move {
304301
// This can be long running and CPU Intensive.
305302
// So we spawn it off to a background task.
306-
stats::start_thread(chain, THREAD_NAME, true);
303+
stats::start_thread(chain, stats::thread::name::COMPUTE_SNAPSHOT_MSG, true);
307304
let result = MessageBuilder::new()
308305
.compute_snapshot_message(&cert, &mithril_path)
309306
.await;
310-
stats::stop_thread(chain, THREAD_NAME);
307+
stats::stop_thread(chain, stats::thread::name::COMPUTE_SNAPSHOT_MSG);
311308
result
312309
})
313310
.await
@@ -522,11 +519,8 @@ async fn check_snapshot_to_download(
522519
fn background_validate_mithril_snapshot(
523520
chain: Network, certificate: MithrilCertificate, tmp_path: PathBuf,
524521
) -> tokio::task::JoinHandle<bool> {
525-
/// Thread name for stats.
526-
const THREAD_NAME: &str = "BGValidateMithrilSnapshot";
527-
528522
tokio::spawn(async move {
529-
stats::start_thread(chain, THREAD_NAME, true);
523+
stats::start_thread(chain, stats::thread::name::VALIDATE_MITHRIL_SNAPSHOT, true);
530524
debug!(
531525
"Mithril Snapshot background updater for: {} : Check Certificate.",
532526
chain
@@ -550,7 +544,7 @@ fn background_validate_mithril_snapshot(
550544
chain
551545
);
552546

553-
stats::stop_thread(chain, THREAD_NAME);
547+
stats::stop_thread(chain, stats::thread::name::VALIDATE_MITHRIL_SNAPSHOT);
554548
true
555549
})
556550
}

rust/cardano-chain-follower/src/mithril_turbo_downloader.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,20 @@ impl MithrilTurboDownloader {
319319

320320
/// Parallel Download, Extract and Dedup the Mithril Archive.
321321
async fn dl_and_dedup(&self, location: &str, target_dir: &Path) -> MithrilResult<()> {
322-
/// Thread name for stats.
323-
const THREAD_NAME: &str = "MithrilTurboDownloader::DlAndDedup";
324-
325322
// Get a copy of the inner data to use in the sync download task.
326323
let inner = self.inner.clone();
327324
let location = location.to_owned();
328325
let target_dir = target_dir.to_owned();
329326

330327
// This is fully synchronous IO, so do it on a sync thread.
331328
let result = spawn_blocking(move || {
332-
stats::start_thread(inner.cfg.chain, THREAD_NAME, false);
329+
stats::start_thread(
330+
inner.cfg.chain,
331+
stats::thread::name::MITHRIL_DL_DEDUP,
332+
false,
333+
);
333334
let result = inner.dl_and_dedup(&location, &target_dir);
334-
stats::stop_thread(inner.cfg.chain, THREAD_NAME);
335+
stats::stop_thread(inner.cfg.chain, stats::thread::name::MITHRIL_DL_DEDUP);
335336
result
336337
})
337338
.await;

rust/cardano-chain-follower/src/stats/thread.rs renamed to rust/cardano-chain-follower/src/stats/thread/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Thread statistics.
22
3+
pub(crate) mod name;
4+
35
use std::{
46
fmt,
57
sync::{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! Thread names
2+
3+
/// Chain Sync.
4+
pub(crate) const CHAIN_SYNC: &str = "ChainSync";
5+
/// Wait for Sync Ready.
6+
pub(crate) const WAIT_FOR_SYNC_READY: &str = "WaitForSyncReady";
7+
/// Live Sync Backfill and Purge.
8+
pub(crate) const LIVE_SYNC_BACKFILL_AND_PURGE: &str = "LiveSyncBackfillAndPurge";
9+
/// Mithril Iterator.
10+
pub(crate) const MITHRIL_ITERATOR: &str = "MithrilIterator";
11+
/// Background Mithril Snapshot Updater.
12+
pub(crate) const MITHRIL_SNAPSHOT_UPDATER: &str = "MithrilSnapshotUpdater";
13+
/// Mithril Snapshot Iterator Next.
14+
pub(crate) const MITHRIL_ITERATOR_NEXT: &str = "MithrilIteratorNext";
15+
/// Mithril compute snapshot.
16+
pub(crate) const COMPUTE_SNAPSHOT_MSG: &str = "ComputeSnapshotMsg";
17+
/// Background Mithril Snapshot Validator.
18+
pub(crate) const VALIDATE_MITHRIL_SNAPSHOT: &str = "ValidateMithrilSnapshot";
19+
/// Mithril Downloader, Dl and Dedup.
20+
pub(crate) const MITHRIL_DL_DEDUP: &str = "MithrilDlDedup";
21+
/// Parallel Download Processor Worker.
22+
pub(crate) const PARALLEL_DL_WORKER: &str = "ParallelDlWorker";
23+
/// Parallel Download Processor Get Content Length.
24+
pub(crate) const PARALLEL_DL_GET_CONTENT_LENGTH: &str = "ParallelDlGetContentLength";

0 commit comments

Comments
 (0)