Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
handle missing blocks as a speacial case of database error
Browse files Browse the repository at this point in the history
  • Loading branch information
lostman committed Sep 13, 2023
1 parent a7c6a2b commit 4faa027
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/fuel-indexer-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub enum WasmIndexerError {
UnableToFetchLogString,
KillSwitch,
DatabaseError,
MissingBlocksError,
GeneralError,
}

Expand All @@ -60,6 +61,7 @@ impl From<u32> for WasmIndexerError {
5 => Self::UnableToFetchLogString,
6 => Self::KillSwitch,
7 => Self::DatabaseError,
8 => Self::MissingBlocksError,
_ => Self::GeneralError,
}
}
Expand Down Expand Up @@ -92,6 +94,9 @@ impl std::fmt::Display for WasmIndexerError {
Self::DatabaseError => {
write!(f, "Failed performing a database operation")
}
Self::MissingBlocksError => {
write!(f, "Some blocks are missing")
}
Self::GeneralError => write!(f, "Some unspecified WASM error occurred."),
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/fuel-indexer/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ pub fn run_executor<T: 'static + Executor + Send + Sync>(
}

if let Err(e) = result {
if let IndexerError::RuntimeError(ref e) = e {
if let Some(&WasmIndexerError::MissingBlocksError) =
e.downcast_ref::<WasmIndexerError>()
{
error!(
"Indexer({indexer_uid}) terminating due to missing blocks."
);
break;
}
}
// Run time metering is deterministic. There is no point in retrying.
if let IndexerError::RunTimeLimitExceededError = e {
error!("Indexer({indexer_uid}) executor run time limit exceeded. Giving up. <('.')>. Consider increasing metering points");
Expand Down
24 changes: 22 additions & 2 deletions packages/fuel-indexer/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn put_object(

if let Err(e) = result {
error!("Failed to put_object: {e}");
return Err(WasmIndexerError::DatabaseError);
return Err(database_operation_failure(e));
};

Ok(())
Expand Down Expand Up @@ -285,12 +285,32 @@ fn put_many_to_many_record(

if let Err(e) = result {
error!("Failed to put_many_to_many_record: {e:?}");
return Err(WasmIndexerError::DatabaseError);
return Err(database_operation_failure(e));
}

Ok(())
}

// Takes care of the exception raised by a trigger to ensure no blocks are
// missed by the indexers.
fn database_operation_failure(e: crate::IndexerError) -> WasmIndexerError {
match e {
crate::IndexerError::SqlxError(e) => {
if let Some(e) = e.as_database_error() {
if let Some(e) = e.try_downcast_ref::<sqlx::postgres::PgDatabaseError>() {
if let Some(source) = e.r#where() {
if source.contains("PL/pgSQL function ensure_block_height_consecutive() line 8 at RAISE") {
return WasmIndexerError::MissingBlocksError
}
}
}
}
WasmIndexerError::DatabaseError
}
_ => WasmIndexerError::DatabaseError,
}
}

/// When called from WASM it will terminate the execution and return the error
/// code.
pub fn early_exit(err_code: u32) -> Result<(), WasmIndexerError> {
Expand Down

0 comments on commit 4faa027

Please sign in to comment.