-
Notifications
You must be signed in to change notification settings - Fork 68
node: Improve OutOfSync code #2519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
fed-franz
wants to merge
4
commits into
master
Choose a base branch
from
improve-outofsync
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+59
−40
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -196,9 +196,7 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> | |
| self.pool.insert(key, target_block); | ||
| self.remote_peer = peer_addr; | ||
|
|
||
| if let Some(last_request) = self.request_pool_missing_blocks().await { | ||
| self.last_request = last_request | ||
| } | ||
| self.request_pool_missing_blocks().await; | ||
|
|
||
| let (from, to) = &self.range; | ||
| info!(event = "entering out-of-sync", from, to, ?peer_addr); | ||
|
|
@@ -227,13 +225,13 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> | |
| blk: &Block, | ||
| metadata: Option<Metadata>, | ||
| ) -> anyhow::Result<bool> { | ||
| let mut acc = self.acc.write().await; | ||
| let acceptor = self.acc.clone(); | ||
| let mut acc = acceptor.write().await; | ||
| let block_height = blk.header().height; | ||
|
|
||
| if self.attempts == 0 && self.is_timeout_expired() { | ||
| acc.restart_consensus().await; | ||
| // Timeout-ed sync-up | ||
| // Transit back to InSync mode | ||
| // We check the timeout here to prevent the peer from keeping us in | ||
| // outofsync by flooding our node with blocks | ||
| if self.on_sync_timeout().await { | ||
| return Ok(true); | ||
| } | ||
|
|
||
|
|
@@ -242,6 +240,14 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> | |
| return Ok(false); | ||
| } | ||
|
|
||
| // If we receive a block higher than the current target, we "extend" the | ||
| // range of the syncing process | ||
| // | ||
| // NOTE: the block could be coming from a different peer than the one we | ||
| // are currently syncing with. This means the current peer could | ||
| // actually not have blocks up to the new target. We might want to | ||
| // handle this update differently in the future, e.g. by also updating | ||
| // the sync peer. | ||
| if block_height > self.range.1 { | ||
| debug!( | ||
| event = "update sync target", | ||
|
|
@@ -331,7 +337,6 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> | |
| return Ok(false); | ||
| } | ||
|
|
||
| let block_height = blk.header().height; | ||
| let pool_len = self.pool.len(); | ||
|
|
||
| if self.pool.contains_key(&block_height) { | ||
|
|
@@ -346,10 +351,7 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> | |
|
|
||
| // If we almost dequeued all requested blocks (2/3) | ||
| if self.last_request < current_height + (MAX_BLOCKS_TO_REQUEST / 3) { | ||
| if let Some(last_request) = self.request_pool_missing_blocks().await | ||
| { | ||
| self.last_request = last_request | ||
| } | ||
| self.request_pool_missing_blocks().await; | ||
| } | ||
|
|
||
| // if the pool is full, check if the new block has higher priority | ||
|
|
@@ -396,31 +398,35 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> | |
|
|
||
| pub async fn on_heartbeat(&mut self) -> anyhow::Result<bool> { | ||
| if self.is_timeout_expired() { | ||
| if self.attempts == 0 { | ||
| debug!( | ||
| event = "out_of_sync timer expired", | ||
| attempts = self.attempts, | ||
| mode = "out_of_sync" | ||
| ); | ||
| // sync-up has timed out, recover consensus task | ||
| self.acc.write().await.restart_consensus().await; | ||
| return Ok(self.on_sync_timeout().await); | ||
| } | ||
|
|
||
| // sync-up timed out for N attempts | ||
| // Transit back to InSync mode as a fail-over | ||
| return Ok(true); | ||
| } | ||
| Ok(false) | ||
| } | ||
|
|
||
| // Request missing from local_pool blocks | ||
| if let Some(last_request) = self.request_pool_missing_blocks().await | ||
| { | ||
| self.last_request = last_request | ||
| } | ||
| async fn on_sync_timeout(&mut self) -> bool { | ||
| debug!( | ||
| event = "out_of_sync timer expired", | ||
| attempts = self.attempts, | ||
| mode = "out_of_sync" | ||
| ); | ||
|
|
||
| if self.attempts == 0 { | ||
| // sync-up has timed out, recover consensus task | ||
| self.acc.write().await.restart_consensus().await; | ||
|
|
||
| self.start_time = SystemTime::now(); | ||
| self.attempts -= 1; | ||
| // sync-up timed out for N attempts | ||
| // Transit back to InSync mode as a fail-over | ||
| return true; | ||
| } | ||
|
|
||
| Ok(false) | ||
| // Request missing from local_pool blocks | ||
| self.request_pool_missing_blocks().await; | ||
|
|
||
| self.start_time = SystemTime::now(); | ||
| self.attempts -= 1; | ||
|
|
||
| false | ||
| } | ||
|
|
||
| async fn request_missing_block(&self, height: u64) { | ||
|
|
@@ -449,18 +455,23 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> | |
| /// present in the pool and requests them from the `remote_peer`. | ||
| /// | ||
| /// Returns the height of the last block requested, if any. | ||
| async fn request_pool_missing_blocks(&self) -> Option<u64> { | ||
| let mut last_request = None; | ||
| async fn request_pool_missing_blocks(&mut self) { | ||
| let mut inv = Inv::new(0); | ||
|
|
||
| let mut inv_count = 0; | ||
|
|
||
| let mut first_request = None; | ||
| let mut last_request = None; | ||
|
|
||
| for height in self.range.0..=self.range.1 { | ||
| if self.pool.contains_key(&height) { | ||
| // already received | ||
| continue; | ||
| } | ||
| inv.add_block_from_height(height); | ||
| inv_count += 1; | ||
| if first_request.is_none() { | ||
| first_request = Some(height); | ||
| } | ||
| last_request = Some(height); | ||
| if inv_count >= MAX_BLOCKS_TO_REQUEST { | ||
| break; | ||
|
|
@@ -469,8 +480,9 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> | |
|
|
||
| if !inv.inv_list.is_empty() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: since we are at it, can we simply return early if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally in favor! |
||
| debug!( | ||
| event = "request blocks", | ||
| target = last_request.unwrap_or_default(), | ||
| event = "request missing blocks", | ||
| first_request = first_request.unwrap_or_default(), | ||
| last_request = last_request.unwrap_or_default(), | ||
| mode = "out_of_sync", | ||
| ); | ||
|
|
||
|
|
@@ -490,9 +502,9 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> | |
| mode = "out_of_sync", | ||
| ); | ||
| warn!("Unable to request missing blocks {e}"); | ||
| return None; | ||
| } else { | ||
| self.last_request = last_request.unwrap(); | ||
| } | ||
| } | ||
| last_request | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On_sync_timeout try to acquire a write lock from the acceptor (but you already acquired it, so probably it get stuck)
Try this before merge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do!