-
Notifications
You must be signed in to change notification settings - Fork 247
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
Add domain snap sync algorithm #3027
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bd4829a
Introduce snap sync orchestrator.
shamil-gadelshin 53d1cad
Introduce consensus sync params struct.
shamil-gadelshin aea7712
Export FINALIZATION_DEPTH_IN_SEGMENTS constant.
shamil-gadelshin c65ffa6
Modify wait_for_import function.
shamil-gadelshin 7919e62
Add domain snap sync algorithm.
shamil-gadelshin 0ec127d
Update Cargo.lock
shamil-gadelshin 8d4747d
Fix Cargo.toml for `domain-operator` crate
shamil-gadelshin 38d885b
Update domains/client/domain-operator/src/snap_sync.rs
shamil-gadelshin 3b542b0
Refactor peer discovery code for domain snap sync.
shamil-gadelshin 7fe7136
Refactor wait_for_block_import function
shamil-gadelshin 0f81e12
Merge branch 'main' into add-domain-snap-sync-algorithm
shamil-gadelshin 06a038c
Fix merge changes.
shamil-gadelshin 8ee2d64
Refactor snap sync orchestrator.
shamil-gadelshin 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
106 changes: 106 additions & 0 deletions
106
crates/subspace-service/src/domains/snap_sync_orchestrator.rs
This file contains 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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//! Provides synchronization primitives for consensus and domain chains snap sync. | ||
|
||
use crate::sync_from_dsn::snap_sync::{DefaultTargetBlockProvider, SnapSyncTargetBlockProvider}; | ||
use async_trait::async_trait; | ||
use parking_lot::Mutex; | ||
use std::sync::Arc; | ||
use subspace_core_primitives::BlockNumber; | ||
use tokio::sync::Notify; | ||
use tracing::debug; | ||
|
||
pub(crate) fn create_target_block_provider( | ||
snap_sync_orchestrator: Option<Arc<SnapSyncOrchestrator>>, | ||
) -> Arc<dyn SnapSyncTargetBlockProvider> { | ||
if let Some(snap_sync_orchestrator) = snap_sync_orchestrator { | ||
snap_sync_orchestrator | ||
} else { | ||
Arc::new(DefaultTargetBlockProvider) | ||
} | ||
} | ||
|
||
/// Synchronizes consensus and domain chain snap sync. | ||
pub struct SnapSyncOrchestrator { | ||
notify_consensus_snap_sync_unblocked: Notify, | ||
consensus_snap_sync_block_number: Mutex<Option<BlockNumber>>, | ||
notify_domain_snap_sync_finished: Notify, | ||
domain_snap_sync_finished: Mutex<bool>, | ||
} | ||
|
||
#[async_trait] | ||
impl SnapSyncTargetBlockProvider for SnapSyncOrchestrator { | ||
async fn target_block(&self) -> Option<BlockNumber> { | ||
self.consensus_snap_sync_unblocked().await; | ||
|
||
self.target_consensus_snap_sync_block_number() | ||
} | ||
} | ||
|
||
impl Default for SnapSyncOrchestrator { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl SnapSyncOrchestrator { | ||
/// Constructor | ||
pub fn new() -> Self { | ||
Self { | ||
notify_consensus_snap_sync_unblocked: Notify::new(), | ||
consensus_snap_sync_block_number: Mutex::new(None), | ||
notify_domain_snap_sync_finished: Notify::new(), | ||
domain_snap_sync_finished: Mutex::new(false), | ||
} | ||
} | ||
|
||
/// Returns optional target block for consensus chain snap sync. None means target block is | ||
/// not defined yet. | ||
pub fn target_consensus_snap_sync_block_number(&self) -> Option<BlockNumber> { | ||
*self.consensus_snap_sync_block_number.lock() | ||
} | ||
|
||
/// Wait for the allowing signal for the consensus chain snap sync. | ||
pub async fn consensus_snap_sync_unblocked(&self) { | ||
debug!("Waiting for a signal to start consensus chain snap sync."); | ||
self.notify_consensus_snap_sync_unblocked.notified().await; | ||
debug!("Finished waiting for a signal to start consensus chain snap sync."); | ||
} | ||
|
||
/// Unblocks (allows) consensus chain snap sync with the given target block. | ||
pub fn unblock_consensus_snap_sync(&self, target_block_number: BlockNumber) { | ||
debug!(%target_block_number, "Allowed starting consensus chain snap sync."); | ||
self.consensus_snap_sync_block_number | ||
.lock() | ||
.replace(target_block_number); | ||
|
||
self.notify_consensus_snap_sync_unblocked.notify_waiters(); | ||
} | ||
|
||
/// Returns true if domain snap sync finished. | ||
pub fn domain_snap_sync_finished(&self) -> bool { | ||
*self.domain_snap_sync_finished.lock() | ||
} | ||
|
||
/// Signal that domain snap sync finished. | ||
pub fn mark_domain_snap_sync_finished(&self) { | ||
debug!("Signal that domain snap sync finished."); | ||
*self.domain_snap_sync_finished.lock() = true; | ||
|
||
self.notify_domain_snap_sync_finished.notify_waiters(); | ||
} | ||
|
||
/// Wait for a signal that domain snap sync finished. | ||
pub async fn domain_snap_sync_finished_blocking(&self) { | ||
debug!("Waiting for a signal that domain snap sync finished."); | ||
self.notify_domain_snap_sync_finished.notified().await; | ||
debug!("Finished waiting for a signal that domain snap sync finished."); | ||
} | ||
|
||
/// Unblock all processes (synchronization cancelled). | ||
pub fn unblock_all(&self) { | ||
debug!("Allow all processes (synchronization cancelled)."); | ||
|
||
self.notify_consensus_snap_sync_unblocked.notify_waiters(); | ||
self.notify_domain_snap_sync_finished.notify_waiters(); | ||
*self.domain_snap_sync_finished.lock() = true; | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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
Oops, something went wrong.
Oops, something went wrong.
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.
SubspaceLink::block_importing_notification_stream()
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.
AFAIR, we discussed offline a possibility of changing the current algorithm with sync orchestrator from blocking to reactive approach by utilizing
SubspaceLink::block_importing_notification_stream()
and its ability to acknowledge blocks. I tried to use that approach and deleted several initial synchronization points instead.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.
Why not deleting this one then? My point was that we ideally wouldn't need this orchestrator at all.
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.
Consensus chain snap sync is a part of the more complex domain snap sync process. In this form, it must start after we acquire the correct target block. I removed other blocking orchestrator points after our conversation: for example, we don't need to send signals from consensus snap sync anymore, however, it's not clear to me how to remove the dependency completely. Let's wait until the full solution is merged and return to this, I'm open for a change here.
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.
Can you just have a mutex or oneshot channel or something that is passed down to
subspace-service
that prevents sync as such from starting? I don't think you need to block/unblock it many times anyway, just pause until something happens on domain side and it is not necessarily specific to Snap sync either.What else is this orchestrator needed for?
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.
The current implementation will contain the target block provider that conceals the orchestrator in the full version:
The default non-blocking implementation returns
None,
which is close to what you proposed. I tried to limit the scope of the PR, but it seems the whole solution will provide more context and will be easier to review despite its size.