-
Notifications
You must be signed in to change notification settings - Fork 74
network backoff resolution strategy #2775
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
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
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
Empty file.
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 |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| use std::collections::HashSet; | ||
insipx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| use crate::{ | ||
| d14n::QueryEnvelope, | ||
| protocol::{ | ||
| Envelope, ResolutionError, ResolveDependencies, Resolved, VectorClock, | ||
| types::MissingEnvelope, | ||
| }, | ||
| }; | ||
| use derive_builder::Builder; | ||
| use itertools::Itertools; | ||
| use tracing::warn; | ||
| use xmtp_common::{ExponentialBackoff, Strategy}; | ||
| use xmtp_configuration::MAX_PAGE_SIZE; | ||
| use xmtp_proto::{ | ||
| api::{Client, Query}, | ||
| types::{Cursor, GlobalCursor, Topic}, | ||
| xmtp::xmtpv4::envelopes::OriginatorEnvelope, | ||
| }; | ||
|
|
||
| /// try resolve d14n dependencies based on a backoff strategy | ||
| #[derive(Clone, Debug, Builder)] | ||
| #[builder(setter(strip_option), build_fn(error = "ResolutionError"))] | ||
| pub struct NetworkBackoffResolver<ApiClient> { | ||
| client: ApiClient, | ||
| backoff: ExponentialBackoff, | ||
| } | ||
|
|
||
| impl<ApiClient: Clone> NetworkBackoffResolver<ApiClient> { | ||
| pub fn builder() -> NetworkBackoffResolverBuilder<ApiClient> { | ||
| NetworkBackoffResolverBuilder::default() | ||
| } | ||
| } | ||
|
|
||
| #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] | ||
| #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] | ||
| impl<ApiClient: Client> ResolveDependencies for NetworkBackoffResolver<ApiClient> { | ||
| type ResolvedEnvelope = OriginatorEnvelope; | ||
| /// Resolve dependencies, starting with a list of dependencies. Should try to resolve | ||
| /// all dependents after `dependency`, if `Dependency` is missing as well. | ||
| /// * Once resolved, these dependencies may have missing dependencies of their own. | ||
| /// # Returns | ||
| /// * `HashSet<Self::ResolvedEnvelope>`: The list of envelopes which were resolved. | ||
| async fn resolve( | ||
| &mut self, | ||
| mut missing: HashSet<MissingEnvelope>, | ||
| ) -> Result<Resolved<Self::ResolvedEnvelope>, ResolutionError> { | ||
| let mut attempts = 0; | ||
| let time_spent = xmtp_common::time::Instant::now(); | ||
| let mut resolved = Vec::new(); | ||
| while !missing.is_empty() { | ||
| if let Some(wait_for) = self.backoff.backoff(attempts, time_spent) { | ||
| xmtp_common::time::sleep(wait_for).await; | ||
| attempts += 1; | ||
| } else { | ||
| missing.iter().for_each(|m| { | ||
| warn!( | ||
| "dropping missing dependency {} due to lack of resolution", | ||
| m | ||
| ); | ||
| }); | ||
| return Ok(Resolved { | ||
| envelopes: resolved, | ||
| unresolved: Some(missing), | ||
| }); | ||
| } | ||
| let (topics, lcc) = lcc(&missing); | ||
| let envelopes = QueryEnvelope::builder() | ||
| .topics(topics) | ||
| .last_seen(lcc) | ||
| .limit(MAX_PAGE_SIZE) | ||
| .build()? | ||
| .query(&self.client) | ||
| .await | ||
| .map_err(|e| ResolutionError::Api(Box::new(e)))? | ||
| .envelopes; | ||
| let got = envelopes | ||
| .iter() | ||
| .map(|e| e.cursor()) | ||
| .collect::<Result<HashSet<Cursor>, _>>()?; | ||
| missing.retain(|m| !got.contains(&m.cursor)); | ||
| resolved.extend(envelopes); | ||
| } | ||
| Ok(Resolved { | ||
| envelopes: resolved, | ||
| unresolved: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// Get the LCC and topics from a list of missing envelopes | ||
| fn lcc(missing: &HashSet<MissingEnvelope>) -> (Vec<Topic>, GlobalCursor) { | ||
| // get the lcc by first getting lowest Cursor | ||
| // per topic, then merging the global cursor of every topic into | ||
| // one. | ||
| let (topics, last_seen): (Vec<_>, Vec<GlobalCursor>) = missing | ||
| .iter() | ||
| .into_grouping_map_by(|m| m.topic.clone()) | ||
| .fold(GlobalCursor::default(), |mut acc, _key, val| { | ||
| acc.apply_least(&val.cursor); | ||
| acc | ||
| }) | ||
| .into_iter() | ||
| .unzip(); | ||
| let last_seen = last_seen | ||
| .into_iter() | ||
| .fold(GlobalCursor::default(), |mut acc, clock| { | ||
| acc.merge_least(&clock); | ||
| acc | ||
| }); | ||
| (topics, last_seen) | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.