Skip to content

Commit

Permalink
Retry block_pointer_from_number on deploy
Browse files Browse the repository at this point in the history
Add retry to avoid a single failure while making the call prevent the subsgraph from being deployed
Add warning for failed attempts
  • Loading branch information
mangas committed Aug 21, 2023
1 parent d57d9f3 commit e32f7fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
41 changes: 32 additions & 9 deletions core/src/subgraph/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use graph::prelude::{
CreateSubgraphResult, SubgraphAssignmentProvider as SubgraphAssignmentProviderTrait,
SubgraphRegistrar as SubgraphRegistrarTrait, *,
};
use graph::tokio_retry::strategy::jitter;
use graph::tokio_retry::strategy::ExponentialBackoff;
use graph::tokio_retry::Retry;

pub struct SubgraphRegistrar<P, S, SM> {
logger: Logger,
Expand Down Expand Up @@ -518,18 +521,38 @@ async fn resolve_start_block(
.expect("cannot identify minimum start block because there are no data sources")
{
0 => Ok(None),
min_start_block => chain
.block_pointer_from_number(logger, min_start_block - 1)
.await
.map(Some)
.map_err(move |_| {
SubgraphRegistrarError::ManifestValidationError(vec![
SubgraphManifestValidationError::BlockNotFound(min_start_block.to_string()),
])
}),
min_start_block => Retry::spawn(retry_strategy(Some(2)), move || {
chain
.block_pointer_from_number(&logger, min_start_block - 1)
.inspect_err(move |e| warn!(&logger, "Failed to get block number: {}", e))
})
.await
.map(Some)
.map_err(move |_| {
SubgraphRegistrarError::ManifestValidationError(vec![
SubgraphManifestValidationError::BlockNotFound(min_start_block.to_string()),
])
}),
}
}

fn retry_strategy(limit_opt: Option<usize>) -> Box<dyn Iterator<Item = Duration> + Send> {
// Exponential backoff, but with a maximum
let max_delay_ms = 30_000;
let backoff = ExponentialBackoff::from_millis(2)
.max_delay(Duration::from_millis(max_delay_ms))
.map(jitter);

// Apply limit (maximum retry count)
match limit_opt {
Some(limit) => {
// Items are delays *between* attempts,
// so subtract 1 from limit.
Box::new(backoff.take(limit - 1))
}
None => Box::new(backoff),
}
}
/// Resolves the manifest's graft base block
async fn resolve_graft_block(
base: &Graft,
Expand Down
1 change: 1 addition & 0 deletions graph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub use semver;
pub use slog;
pub use stable_hash_legacy;
pub use tokio;
pub use tokio_retry;
pub use tokio_stream;
pub use url;

Expand Down

0 comments on commit e32f7fc

Please sign in to comment.