Skip to content

Commit 5ffbd3f

Browse files
authored
fix(l1): headers snapsync logs (#5082)
**Motivation** The logs for the headers in snapsync shows inaccurate numbers due to the chunked header download. This pr aims to fix that and show the proper percentage of headers to download. **Description** - Change `METRICS.headers_downloaded` to an `IntCounter` instead of `AtomicU64` - Removed `METRICS.headers_to_download` and replaced the calculation with `METRICS.sync_head_block` - Fixed `METRICS.sync_head_block` to include the proper Works to improve #4950
1 parent 5b1c859 commit 5ffbd3f

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

crates/networking/p2p/metrics.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ pub struct Metrics {
6161
pub current_step: Arc<CurrentStep>,
6262

6363
// Headers
64-
pub headers_to_download: AtomicU64,
65-
pub downloaded_headers: AtomicU64,
64+
pub downloaded_headers: IntCounter,
6665
pub time_to_retrieve_sync_head_block: Arc<Mutex<Option<Duration>>>,
6766
pub headers_download_start_time: Arc<Mutex<Option<SystemTime>>>,
6867

@@ -632,6 +631,16 @@ impl Default for Metrics {
632631
.register(Box::new(storage_leaves_inserted.clone()))
633632
.expect("Failed to register storage_leaves_inserted counter");
634633

634+
let downloaded_headers = IntCounter::new(
635+
"downloaded_headers",
636+
"Total number of headers already download",
637+
)
638+
.expect("Failed to create downloaded_headers counter");
639+
640+
registry
641+
.register(Box::new(downloaded_headers.clone()))
642+
.expect("Failed to register downloaded_headers counter");
643+
635644
let storage_leaves_downloaded = IntCounter::new(
636645
"storage_leaves_downloaded",
637646
"Total number of storage leaves downloaded",
@@ -679,8 +688,7 @@ impl Default for Metrics {
679688
current_step: Arc::new(CurrentStep(AtomicU8::new(0))),
680689

681690
// Headers
682-
headers_to_download: AtomicU64::new(0),
683-
downloaded_headers: AtomicU64::new(0),
691+
downloaded_headers,
684692
time_to_retrieve_sync_head_block: Arc::new(Mutex::new(None)),
685693
headers_download_start_time: Arc::new(Mutex::new(None)),
686694

crates/networking/p2p/network.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,11 @@ pub async fn periodically_show_peer_stats_during_syncing(
198198
let current_header_hash = *METRICS.sync_head_hash.lock().await;
199199

200200
// Headers metrics
201-
let headers_to_download = METRICS.headers_to_download.load(Ordering::Relaxed);
202-
let headers_downloaded = METRICS.downloaded_headers.load(Ordering::Relaxed);
201+
let headers_to_download = METRICS.sync_head_block.load(Ordering::Relaxed);
202+
// We may download more than expected headers due to duplicates
203+
// We just clamp it to the max to avoid showing the user confusing data
204+
let headers_downloaded =
205+
u64::min(METRICS.downloaded_headers.get(), headers_to_download);
203206
let headers_remaining = headers_to_download.saturating_sub(headers_downloaded);
204207
let headers_download_progress = if headers_to_download == 0 {
205208
"0%".to_string()

crates/networking/p2p/peer_handler.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ impl PeerHandler {
190190
.current_step
191191
.set(CurrentStepValue::DownloadingHeaders);
192192

193-
let initial_downloaded_headers = METRICS.downloaded_headers.load(Ordering::Relaxed);
194-
195193
let mut ret = Vec::<BlockHeader>::new();
196194

197195
let mut sync_head_number = 0_u64;
@@ -238,6 +236,9 @@ impl PeerHandler {
238236

239237
retries += 1;
240238
}
239+
METRICS
240+
.sync_head_block
241+
.store(sync_head_number, Ordering::Relaxed);
241242
sync_head_number = sync_head_number.min(start + MAX_HEADER_CHUNK);
242243

243244
let sync_head_number_retrieval_elapsed = sync_head_number_retrieval_start
@@ -248,12 +249,6 @@ impl PeerHandler {
248249

249250
*METRICS.time_to_retrieve_sync_head_block.lock().await =
250251
Some(sync_head_number_retrieval_elapsed);
251-
METRICS
252-
.sync_head_block
253-
.store(sync_head_number, Ordering::Relaxed);
254-
METRICS
255-
.headers_to_download
256-
.store(sync_head_number + 1, Ordering::Relaxed);
257252
*METRICS.sync_head_hash.lock().await = sync_head;
258253

259254
let block_count = sync_head_number + 1 - start;
@@ -307,9 +302,7 @@ impl PeerHandler {
307302

308303
downloaded_count += headers.len() as u64;
309304

310-
METRICS
311-
.downloaded_headers
312-
.fetch_add(headers.len() as u64, Ordering::Relaxed);
305+
METRICS.downloaded_headers.inc_by(headers.len() as u64);
313306

314307
let batch_show = downloaded_count / 10_000;
315308

@@ -393,11 +386,6 @@ impl PeerHandler {
393386
});
394387
}
395388

396-
METRICS.downloaded_headers.store(
397-
initial_downloaded_headers + downloaded_count,
398-
Ordering::Relaxed,
399-
);
400-
401389
let elapsed = start_time.elapsed().unwrap_or_default();
402390

403391
debug!(

0 commit comments

Comments
 (0)