Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Sync: Improve major sync detection #11547

Merged
merged 3 commits into from
May 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions client/network/sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,28 @@ where
.map(|p| PeerInfo { best_hash: p.best_hash, best_number: p.best_number })
}

/// Returns the best seen block.
fn best_seen(&self) -> Option<NumberFor<B>> {
let mut best_seens = self.peers.values().map(|p| p.best_number).collect::<Vec<_>>();

if best_seens.is_empty() {
None
} else {
let middle = best_seens.len() / 2;

// Not the "perfect median" when we have an even number of peers.
Some(*best_seens.select_nth_unstable(middle).1)
}
Comment on lines +581 to +590
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be helpful to first remove all peers that have best_number lower than that of the current node?

Otherwise if I read it correctly there is someone that just started syncing, node might suddenly think it is no longer syncing because of median.

Probably related, I see this strange behavior with this PR applied:

2022-06-03 21:02:31 [PrimaryChain] ⚙️  Syncing  1.4 bps, target=#20572 (65 peers), best: #5480 (0xa72b…f990), finalized #0 (0x9ee8…ccf0), ⬇ 280.1kiB/s ⬆ 21.9MiB/s    
2022-06-03 21:02:32 [PrimaryChain] ✨ Imported #5481 (0x68a0…3e21)    
2022-06-03 21:02:32 [PrimaryChain] ✨ Imported #5482 (0x5248…5c7a)    
2022-06-03 21:02:33 [PrimaryChain] ✨ Imported #5483 (0x2aca…23d6)    
2022-06-03 21:02:34 [PrimaryChain] ✨ Imported #5484 (0xa0d0…c8e0)    
2022-06-03 21:02:34 [PrimaryChain] ✨ Imported #5485 (0x67f9…5270)    
2022-06-03 21:02:35 [PrimaryChain] ✨ Imported #5486 (0xedeb…c714)    
2022-06-03 21:02:36 [PrimaryChain] ✨ Imported #5487 (0x47f0…90dd)    
2022-06-03 21:02:36 [PrimaryChain] ✨ Imported #5488 (0x3b8f…619c)    
2022-06-03 21:02:36 [PrimaryChain] ⚙️  Syncing  1.6 bps, target=#20579 (65 peers), best: #5488 (0x3b8f…619c), finalized #0 (0x9ee8…ccf0), ⬇ 164.4kiB/s ⬆ 22.5MiB/s    
2022-06-03 21:02:38 [PrimaryChain] ✨ Imported #5489 (0x8158…21bb)    
2022-06-03 21:02:38 [PrimaryChain] ✨ Imported #5490 (0x9056…9176)    
2022-06-03 21:02:38 [PrimaryChain] ✨ Imported #5491 (0xbefa…f906)    
2022-06-03 21:02:40 [PrimaryChain] ✨ Imported #5492 (0xa44c…550b)    
2022-06-03 21:02:41 [PrimaryChain] ✨ Imported #5493 (0x2c28…88ce)    
2022-06-03 21:02:41 [PrimaryChain] ✨ Imported #5494 (0xd4d2…10ca)    
2022-06-03 21:02:41 [PrimaryChain] ⚙️  Syncing  1.2 bps, target=#20585 (65 peers), best: #5494 (0xd4d2…10ca), finalized #0 (0x9ee8…ccf0), ⬇ 175.7kiB/s ⬆ 23.7MiB/s    
2022-06-03 21:02:42 [PrimaryChain] ✨ Imported #5495 (0x6f99…2e6e)    
2022-06-03 21:02:43 [PrimaryChain] ✨ Imported #5496 (0x7eae…947e)    
2022-06-03 21:02:43 [PrimaryChain] ✨ Imported #5497 (0x21cb…ce60)    
2022-06-03 21:02:44 [PrimaryChain] ✨ Imported #5498 (0xf4be…b8e7)    
2022-06-03 21:02:45 [PrimaryChain] ✨ Imported #5499 (0x8739…3b2b)    
2022-06-03 21:02:45 [PrimaryChain] ✨ Imported #5500 (0x3221…ed82)    
2022-06-03 21:02:46 [PrimaryChain] ✨ Imported #5501 (0x3e74…a3e9)    
2022-06-03 21:02:46 [PrimaryChain] ⚙️  Syncing  1.4 bps, target=#20591 (65 peers), best: #5501 (0x3e74…a3e9), finalized #0 (0x9ee8…ccf0), ⬇ 131.3kiB/s ⬆ 21.4MiB/s    
2022-06-03 21:02:47 [PrimaryChain] ✨ Imported #5502 (0xb99a…387e)    
2022-06-03 21:02:47 [PrimaryChain] ✨ Imported #5503 (0xdb93…e10d)    
2022-06-03 21:02:48 [PrimaryChain] ✨ Imported #5504 (0xc354…cc07)    
2022-06-03 21:02:49 [PrimaryChain] ✨ Imported #5505 (0x93b4…ed49)    
2022-06-03 21:02:49 [PrimaryChain] ✨ Imported #5506 (0x9c0f…9b01)    
2022-06-03 21:02:51 [PrimaryChain] ✨ Imported #5507 (0xdf07…ce7c)    
2022-06-03 21:02:51 [PrimaryChain] ✨ Imported #5508 (0x6d1a…76aa)    
2022-06-03 21:02:51 [PrimaryChain] ✨ Imported #5509 (0x3cd6…f058)    
2022-06-03 21:02:51 [PrimaryChain] ⚙️  Syncing  1.6 bps, target=#20596 (65 peers), best: #5509 (0x3cd6…f058), finalized #0 (0x9ee8…ccf0), ⬇ 130.3kiB/s ⬆ 24.1MiB/s    

I recall Imported # is typically not printed during major sync.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So your node is connected to 33 other nodes that are also syncing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We have over 7000 nodes that are syncing on our network right now, so a bit unusual situation.

}

/// Returns the current sync status.
pub fn status(&self) -> Status<B> {
let best_seen = self.peers.values().map(|p| p.best_number).max();
let best_seen = self.best_seen();
let sync_state = if let Some(n) = best_seen {
// A chain is classified as downloading if the provided best block is
// more than `MAJOR_SYNC_BLOCKS` behind the best queued block.
if n > self.best_queued_number && n - self.best_queued_number > MAJOR_SYNC_BLOCKS.into()
{
// more than `MAJOR_SYNC_BLOCKS` behind the best block.
let best_block = self.client.info().best_number;
if n > best_block && n - best_block > MAJOR_SYNC_BLOCKS.into() {
SyncState::Downloading
} else {
SyncState::Idle
Expand Down