Skip to content

Commit e7c8333

Browse files
committed
refactor: Make unfail methods return an outcome enum
1 parent b849773 commit e7c8333

File tree

6 files changed

+62
-31
lines changed

6 files changed

+62
-31
lines changed

core/src/subgraph/instance_manager.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ where
625625
// There's no point in calling it if we have no current or parent block
626626
// pointers, because there would be: no block to revert to or to search
627627
// errors from (first execution).
628-
ctx.inputs
628+
let _outcome = ctx
629+
.inputs
629630
.store
630631
.unfail_deterministic_error(&current_ptr, &parent_ptr)?;
631632
}
@@ -652,7 +653,8 @@ where
652653
if should_try_unfail_non_deterministic {
653654
// If the deployment head advanced, we can unfail
654655
// the non-deterministic error (if there's any).
655-
ctx.inputs
656+
let _outcome = ctx
657+
.inputs
656658
.store
657659
.unfail_non_deterministic_error(&block_ptr)?;
658660

graph/src/components/store.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,12 @@ pub enum EntityOperation {
749749
Remove { key: EntityKey },
750750
}
751751

752+
#[derive(Debug, PartialEq)]
753+
pub enum UnfailOutcome {
754+
Noop,
755+
Unfailed,
756+
}
757+
752758
#[derive(Error, Debug)]
753759
pub enum StoreError {
754760
#[error("store error: {0}")]
@@ -1022,11 +1028,14 @@ pub trait WritableStore: Send + Sync + 'static {
10221028
&self,
10231029
current_ptr: &BlockPtr,
10241030
parent_ptr: &BlockPtr,
1025-
) -> Result<(), StoreError>;
1031+
) -> Result<UnfailOutcome, StoreError>;
10261032

10271033
/// If a non-deterministic error happened and the current deployment head is past the error
10281034
/// block range, this function unfails the subgraph and deletes the error.
1029-
fn unfail_non_deterministic_error(&self, current_ptr: &BlockPtr) -> Result<(), StoreError>;
1035+
fn unfail_non_deterministic_error(
1036+
&self,
1037+
current_ptr: &BlockPtr,
1038+
) -> Result<UnfailOutcome, StoreError>;
10301039

10311040
/// Set subgraph status to failed with the given error as the cause.
10321041
async fn fail_subgraph(&self, error: SubgraphError) -> Result<(), StoreError>;
@@ -1208,11 +1217,15 @@ impl WritableStore for MockStore {
12081217
unimplemented!()
12091218
}
12101219

1211-
fn unfail_deterministic_error(&self, _: &BlockPtr, _: &BlockPtr) -> Result<(), StoreError> {
1220+
fn unfail_deterministic_error(
1221+
&self,
1222+
_: &BlockPtr,
1223+
_: &BlockPtr,
1224+
) -> Result<UnfailOutcome, StoreError> {
12121225
unimplemented!()
12131226
}
12141227

1215-
fn unfail_non_deterministic_error(&self, _: &BlockPtr) -> Result<(), StoreError> {
1228+
fn unfail_non_deterministic_error(&self, _: &BlockPtr) -> Result<UnfailOutcome, StoreError> {
12161229
unimplemented!()
12171230
}
12181231

graph/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ pub mod prelude {
123123
EntityChangeOperation, EntityCollection, EntityFilter, EntityKey, EntityLink,
124124
EntityModification, EntityOperation, EntityOrder, EntityQuery, EntityRange, EntityWindow,
125125
EthereumCallCache, ParentLink, PoolWaitStats, QueryStore, QueryStoreManager, StoreError,
126-
StoreEvent, StoreEventStream, StoreEventStreamBox, SubgraphStore, WindowAttribute,
127-
BLOCK_NUMBER_MAX, SUBSCRIPTION_THROTTLE_INTERVAL,
126+
StoreEvent, StoreEventStream, StoreEventStreamBox, SubgraphStore, UnfailOutcome,
127+
WindowAttribute, BLOCK_NUMBER_MAX, SUBSCRIPTION_THROTTLE_INTERVAL,
128128
};
129129
pub use crate::components::subgraph::{
130130
BlockState, DataSourceTemplateInfo, HostMetrics, RuntimeHost, RuntimeHostBuilder,

store/postgres/src/deployment_store.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use graph::prelude::{
3131
anyhow, debug, info, lazy_static, o, warn, web3, ApiSchema, AttributeNames, BlockNumber,
3232
BlockPtr, CheapClone, DeploymentHash, DeploymentState, Entity, EntityKey, EntityModification,
3333
EntityQuery, Error, Logger, QueryExecutionError, Schema, StopwatchMetrics, StoreError,
34-
StoreEvent, Value, BLOCK_NUMBER_MAX,
34+
StoreEvent, UnfailOutcome, Value, BLOCK_NUMBER_MAX,
3535
};
3636
use graph_graphql::prelude::api_schema;
3737
use web3::types::Address;
@@ -1185,7 +1185,7 @@ impl DeploymentStore {
11851185
site: Arc<Site>,
11861186
current_ptr: &BlockPtr,
11871187
parent_ptr: &BlockPtr,
1188-
) -> Result<(), StoreError> {
1188+
) -> Result<UnfailOutcome, StoreError> {
11891189
let conn = &self.get_conn()?;
11901190
let deployment_id = &site.deployment;
11911191

@@ -1194,12 +1194,12 @@ impl DeploymentStore {
11941194
let subgraph_error = match detail::fatal_error(conn, deployment_id)? {
11951195
Some(fatal_error) => fatal_error,
11961196
// If the subgraph is not failed then there is nothing to do.
1197-
None => return Ok(()),
1197+
None => return Ok(UnfailOutcome::Noop),
11981198
};
11991199

12001200
// Confidence check
12011201
if !subgraph_error.deterministic {
1202-
return Ok(()); // Nothing to do
1202+
return Ok(UnfailOutcome::Noop); // Nothing to do
12031203
}
12041204

12051205
use deployment::SubgraphHealth::*;
@@ -1231,6 +1231,8 @@ impl DeploymentStore {
12311231

12321232
// Unfail the deployment.
12331233
deployment::update_deployment_status(conn, deployment_id, prev_health, None)?;
1234+
1235+
Ok(UnfailOutcome::Unfailed)
12341236
}
12351237
// Found error, but not for deployment head, we don't need to
12361238
// revert the block operations.
@@ -1244,6 +1246,8 @@ impl DeploymentStore {
12441246
"error_block_hash" => format!("0x{}", hex::encode(&hash_bytes)),
12451247
"deployment_head" => format!("{}", current_ptr.hash),
12461248
);
1249+
1250+
Ok(UnfailOutcome::Noop)
12471251
}
12481252
// Same as branch above, if you find this warning in the logs,
12491253
// something is wrong, this shouldn't happen.
@@ -1252,10 +1256,10 @@ impl DeploymentStore {
12521256
"subgraph_id" => deployment_id,
12531257
"error_id" => &subgraph_error.id,
12541258
);
1255-
}
1256-
};
12571259

1258-
Ok(())
1260+
Ok(UnfailOutcome::Noop)
1261+
}
1262+
}
12591263
})
12601264
}
12611265

@@ -1273,7 +1277,7 @@ impl DeploymentStore {
12731277
&self,
12741278
site: Arc<Site>,
12751279
current_ptr: &BlockPtr,
1276-
) -> Result<(), StoreError> {
1280+
) -> Result<UnfailOutcome, StoreError> {
12771281
let conn = &self.get_conn()?;
12781282
let deployment_id = &site.deployment;
12791283

@@ -1282,12 +1286,12 @@ impl DeploymentStore {
12821286
let subgraph_error = match detail::fatal_error(conn, deployment_id)? {
12831287
Some(fatal_error) => fatal_error,
12841288
// If the subgraph is not failed then there is nothing to do.
1285-
None => return Ok(()),
1289+
None => return Ok(UnfailOutcome::Noop),
12861290
};
12871291

12881292
// Confidence check
12891293
if subgraph_error.deterministic {
1290-
return Ok(()); // Nothing to do
1294+
return Ok(UnfailOutcome::Noop); // Nothing to do
12911295
}
12921296

12931297
match subgraph_error.block_range {
@@ -1314,7 +1318,7 @@ impl DeploymentStore {
13141318
// Delete the fatal error.
13151319
deployment::delete_error(conn, &subgraph_error.id)?;
13161320

1317-
Ok(())
1321+
Ok(UnfailOutcome::Unfailed)
13181322
}
13191323
// NOOP, the deployment head is still before where non-deterministic error happened.
13201324
block_range => {
@@ -1328,7 +1332,7 @@ impl DeploymentStore {
13281332
"error_block_hash" => subgraph_error.block_hash.as_ref().map(|hash| format!("0x{}", hex::encode(hash))),
13291333
);
13301334

1331-
Ok(())
1335+
Ok(UnfailOutcome::Noop)
13321336
}
13331337
}
13341338
})

store/postgres/src/subgraph_store.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use graph::{
2424
anyhow, futures03::future::join_all, lazy_static, o, web3::types::Address, ApiSchema,
2525
BlockPtr, DeploymentHash, Entity, EntityKey, EntityModification, Error, Logger, NodeId,
2626
Schema, StopwatchMetrics, StoreError, SubgraphName, SubgraphStore as SubgraphStoreTrait,
27-
SubgraphVersionSwitchingMode,
27+
SubgraphVersionSwitchingMode, UnfailOutcome,
2828
},
2929
slog::{error, warn},
3030
util::{backoff::ExponentialBackoff, timed_cache::TimedCache},
@@ -1192,14 +1192,17 @@ impl WritableStoreTrait for WritableStore {
11921192
&self,
11931193
current_ptr: &BlockPtr,
11941194
parent_ptr: &BlockPtr,
1195-
) -> Result<(), StoreError> {
1195+
) -> Result<UnfailOutcome, StoreError> {
11961196
self.retry("unfail_deterministic_error", || {
11971197
self.writable
11981198
.unfail_deterministic_error(self.site.clone(), current_ptr, parent_ptr)
11991199
})
12001200
}
12011201

1202-
fn unfail_non_deterministic_error(&self, current_ptr: &BlockPtr) -> Result<(), StoreError> {
1202+
fn unfail_non_deterministic_error(
1203+
&self,
1204+
current_ptr: &BlockPtr,
1205+
) -> Result<UnfailOutcome, StoreError> {
12031206
self.retry("unfail_non_deterministic_error", || {
12041207
self.writable
12051208
.unfail_non_deterministic_error(self.site.clone(), current_ptr)

store/postgres/tests/subgraph.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use graph::{
1313
prelude::SubgraphManifest,
1414
prelude::SubgraphName,
1515
prelude::SubgraphVersionSwitchingMode,
16+
prelude::UnfailOutcome,
1617
prelude::{futures03, StoreEvent},
1718
prelude::{CheapClone, DeploymentHash, NodeId, SubgraphStore as _},
1819
semver::Version,
@@ -630,11 +631,12 @@ fn fail_unfail_deterministic_error() {
630631
assert_eq!(Some(1), vi.latest_ethereum_block_number);
631632

632633
// Unfail the subgraph.
633-
writable
634+
let outcome = writable
634635
.unfail_deterministic_error(&BLOCKS[1], &BLOCKS[0])
635636
.unwrap();
636637

637638
// We don't have fatal errors anymore and the block got reverted.
639+
assert_eq!(outcome, UnfailOutcome::Unfailed);
638640
assert!(!query_store.has_non_fatal_errors(None).await.unwrap());
639641
let vi = get_version_info(&store, NAME);
640642
assert_eq!(&*NAME, vi.deployment_id.as_str());
@@ -702,11 +704,12 @@ fn fail_unfail_deterministic_error_noop() {
702704
.expect("can get writable");
703705

704706
// Run unfail with no errors results in NOOP.
705-
writable
707+
let outcome = writable
706708
.unfail_deterministic_error(&BLOCKS[1], &BLOCKS[0])
707709
.unwrap();
708710

709711
// Nothing to unfail, state continues the same.
712+
assert_eq!(outcome, UnfailOutcome::Noop);
710713
assert_eq!(count(), 0);
711714
let vi = get_version_info(&store, NAME);
712715
assert_eq!(&*NAME, vi.deployment_id.as_str());
@@ -732,12 +735,13 @@ fn fail_unfail_deterministic_error_noop() {
732735
assert_eq!(Some(1), vi.latest_ethereum_block_number);
733736

734737
// Running unfail_deterministic_error against a NON-deterministic error will do nothing.
735-
writable
738+
let outcome = writable
736739
.unfail_deterministic_error(&BLOCKS[1], &BLOCKS[0])
737740
.unwrap();
738741

739742
// State continues the same, nothing happened.
740743
// Neither the block got reverted or error deleted.
744+
assert_eq!(outcome, UnfailOutcome::Noop);
741745
assert_eq!(count(), 1);
742746
let vi = get_version_info(&store, NAME);
743747
assert_eq!(&*NAME, vi.deployment_id.as_str());
@@ -757,12 +761,13 @@ fn fail_unfail_deterministic_error_noop() {
757761

758762
// Running unfail_deterministic_error won't do anything,
759763
// the hashes won't match and there's nothing to revert.
760-
writable
764+
let outcome = writable
761765
.unfail_deterministic_error(&BLOCKS[1], &BLOCKS[0])
762766
.unwrap();
763767

764768
// State continues the same.
765769
// Neither the block got reverted or error deleted.
770+
assert_eq!(outcome, UnfailOutcome::Noop);
766771
assert_eq!(count(), 2);
767772
let vi = get_version_info(&store, NAME);
768773
assert_eq!(&*NAME, vi.deployment_id.as_str());
@@ -848,9 +853,10 @@ fn fail_unfail_non_deterministic_error() {
848853
assert_eq!(Some(1), vi.latest_ethereum_block_number);
849854

850855
// Unfail the subgraph and delete the fatal error.
851-
writable.unfail_non_deterministic_error(&BLOCKS[1]).unwrap();
856+
let outcome = writable.unfail_non_deterministic_error(&BLOCKS[1]).unwrap();
852857

853858
// We don't have fatal errors anymore and the subgraph is healthy.
859+
assert_eq!(outcome, UnfailOutcome::Unfailed);
854860
assert_eq!(count(), 0);
855861
let vi = get_version_info(&store, NAME);
856862
assert_eq!(&*NAME, vi.deployment_id.as_str());
@@ -918,9 +924,10 @@ fn fail_unfail_non_deterministic_error_noop() {
918924
.expect("can get writable");
919925

920926
// Running unfail without any errors will do nothing.
921-
writable.unfail_non_deterministic_error(&BLOCKS[1]).unwrap();
927+
let outcome = writable.unfail_non_deterministic_error(&BLOCKS[1]).unwrap();
922928

923929
// State continues the same, nothing happened.
930+
assert_eq!(outcome, UnfailOutcome::Noop);
924931
assert_eq!(count(), 0);
925932
let vi = get_version_info(&store, NAME);
926933
assert_eq!(&*NAME, vi.deployment_id.as_str());
@@ -946,9 +953,10 @@ fn fail_unfail_non_deterministic_error_noop() {
946953
assert_eq!(Some(1), vi.latest_ethereum_block_number);
947954

948955
// Running unfail_non_deterministic_error will be NOOP, the error is deterministic.
949-
writable.unfail_non_deterministic_error(&BLOCKS[1]).unwrap();
956+
let outcome = writable.unfail_non_deterministic_error(&BLOCKS[1]).unwrap();
950957

951958
// Nothing happeened, state continues the same.
959+
assert_eq!(outcome, UnfailOutcome::Noop);
952960
assert_eq!(count(), 1);
953961
let vi = get_version_info(&store, NAME);
954962
assert_eq!(&*NAME, vi.deployment_id.as_str());
@@ -967,9 +975,10 @@ fn fail_unfail_non_deterministic_error_noop() {
967975
writable.fail_subgraph(error).await.unwrap();
968976

969977
// Since the block range of the block won't match the deployment head, this will be NOOP.
970-
writable.unfail_non_deterministic_error(&BLOCKS[1]).unwrap();
978+
let outcome = writable.unfail_non_deterministic_error(&BLOCKS[1]).unwrap();
971979

972980
// State continues the same besides a new error added to the database.
981+
assert_eq!(outcome, UnfailOutcome::Noop);
973982
assert_eq!(count(), 2);
974983
let vi = get_version_info(&store, NAME);
975984
assert_eq!(&*NAME, vi.deployment_id.as_str());

0 commit comments

Comments
 (0)