Skip to content

Commit

Permalink
all: Introduce a DeploymentCreate struct
Browse files Browse the repository at this point in the history
We were using SubgraphDeploymentEntity for both reading and creating
deployments. But when creating a deployment, a lot of the fields of
SubgraphDeploymentEntity must default to specific values (e.g., latest
block must be null), and using SubgraphDeploymentEntity obscured what could
be set and whatcouldn't be set.
  • Loading branch information
lutter committed Mar 14, 2022
1 parent 612262d commit 640b6bb
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 76 deletions.
4 changes: 2 additions & 2 deletions core/src/subgraph/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use graph::blockchain::Blockchain;
use graph::blockchain::BlockchainKind;
use graph::blockchain::BlockchainMap;
use graph::components::store::{DeploymentId, DeploymentLocator, SubscriptionManager};
use graph::data::subgraph::schema::SubgraphDeploymentEntity;
use graph::data::subgraph::schema::DeploymentCreate;
use graph::data::subgraph::MAX_SPEC_VERSION;
use graph::prelude::{
CreateSubgraphResult, SubgraphAssignmentProvider as SubgraphAssignmentProviderTrait,
Expand Down Expand Up @@ -568,7 +568,7 @@ async fn create_subgraph_version<C: Blockchain, S: SubgraphStore, L: LinkResolve

// Apply the subgraph versioning and deployment operations,
// creating a new subgraph deployment if one doesn't exist.
let deployment = SubgraphDeploymentEntity::new(&manifest, false, start_block)
let deployment = DeploymentCreate::new(&manifest, start_block)
.graft(base_block)
.debug(debug_fork);
deployment_store
Expand Down
2 changes: 1 addition & 1 deletion graph/src/components/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub trait SubgraphStore: Send + Sync + 'static {
&self,
name: SubgraphName,
schema: &Schema,
deployment: SubgraphDeploymentEntity,
deployment: DeploymentCreate,
node_id: NodeId,
network: String,
mode: SubgraphVersionSwitchingMode,
Expand Down
48 changes: 23 additions & 25 deletions graph/src/data/subgraph/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,55 +99,33 @@ impl TryFromValue for SubgraphHealth {
}
}

#[derive(Debug)]
pub struct SubgraphDeploymentEntity {
/// The deployment data that is needed to create a deployment
pub struct DeploymentCreate {
pub manifest: SubgraphManifestEntity,
pub failed: bool,
pub health: SubgraphHealth,
pub synced: bool,
pub fatal_error: Option<SubgraphError>,
pub non_fatal_errors: Vec<SubgraphError>,
pub earliest_block: Option<BlockPtr>,
pub latest_block: Option<BlockPtr>,
pub graft_base: Option<DeploymentHash>,
pub graft_block: Option<BlockPtr>,
pub debug_fork: Option<DeploymentHash>,
pub reorg_count: i32,
pub current_reorg_depth: i32,
pub max_reorg_depth: i32,
}

impl SubgraphDeploymentEntity {
impl DeploymentCreate {
pub fn new(
source_manifest: &SubgraphManifest<impl Blockchain>,
synced: bool,
earliest_block: Option<BlockPtr>,
) -> Self {
Self {
manifest: SubgraphManifestEntity::from(source_manifest),
failed: false,
health: SubgraphHealth::Healthy,
synced,
fatal_error: None,
non_fatal_errors: vec![],
earliest_block: earliest_block.cheap_clone(),
latest_block: earliest_block,
graft_base: None,
graft_block: None,
debug_fork: None,
reorg_count: 0,
current_reorg_depth: 0,
max_reorg_depth: 0,
}
}

pub fn graft(mut self, base: Option<(DeploymentHash, BlockPtr)>) -> Self {
if let Some((subgraph, ptr)) = base {
self.graft_base = Some(subgraph);
self.graft_block = Some(ptr);
// When we graft, the block pointer is only set after copying
// from the base subgraph finished successfully
self.latest_block = None;
}
self
}
Expand All @@ -158,6 +136,26 @@ impl SubgraphDeploymentEntity {
}
}

/// The representation of a subgraph deployment when reading an existing
/// deployment
#[derive(Debug)]
pub struct SubgraphDeploymentEntity {
pub manifest: SubgraphManifestEntity,
pub failed: bool,
pub health: SubgraphHealth,
pub synced: bool,
pub fatal_error: Option<SubgraphError>,
pub non_fatal_errors: Vec<SubgraphError>,
pub earliest_block: Option<BlockPtr>,
pub latest_block: Option<BlockPtr>,
pub graft_base: Option<DeploymentHash>,
pub graft_block: Option<BlockPtr>,
pub debug_fork: Option<DeploymentHash>,
pub reorg_count: i32,
pub current_reorg_depth: i32,
pub max_reorg_depth: i32,
}

#[derive(Debug)]
pub struct SubgraphManifestEntity {
pub spec_version: String,
Expand Down
7 changes: 4 additions & 3 deletions graphql/tests/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[macro_use]
extern crate pretty_assertions;

use graph::data::subgraph::schema::DeploymentCreate;
use graph::data::value::Object;
use graphql_parser::Pos;
use std::iter::FromIterator;
Expand All @@ -23,8 +24,8 @@ use graph::{
futures03::stream::StreamExt, o, q, r, serde_json, slog, BlockPtr, DeploymentHash, Entity,
EntityKey, EntityOperation, FutureExtension, GraphQlRunner as _, Logger, NodeId, Query,
QueryError, QueryExecutionError, QueryResult, QueryStoreManager, QueryVariables, Schema,
SubgraphDeploymentEntity, SubgraphManifest, SubgraphName, SubgraphStore,
SubgraphVersionSwitchingMode, Subscription, SubscriptionError, Value,
SubgraphManifest, SubgraphName, SubgraphStore, SubgraphVersionSwitchingMode, Subscription,
SubscriptionError, Value,
},
semver::Version,
};
Expand Down Expand Up @@ -112,7 +113,7 @@ fn insert_test_entities(
store: &impl SubgraphStore,
manifest: SubgraphManifest<graph_chain_ethereum::Chain>,
) -> DeploymentLocator {
let deployment = SubgraphDeploymentEntity::new(&manifest, false, None);
let deployment = DeploymentCreate::new(&manifest, None);
let name = SubgraphName::new("test/query").unwrap();
let node_id = NodeId::new("test").unwrap();
let deployment = store
Expand Down
28 changes: 11 additions & 17 deletions store/postgres/src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ use diesel::{
sql_query,
sql_types::{Nullable, Text},
};
use graph::data::subgraph::{schema::SubgraphManifestEntity, SubgraphFeature};
use graph::data::subgraph::schema::SubgraphError;
use graph::data::subgraph::{
schema::{DeploymentCreate, SubgraphManifestEntity},
SubgraphFeature,
};
use graph::prelude::{
anyhow, bigdecimal::ToPrimitive, hex, web3::types::H256, BigDecimal, BlockNumber, BlockPtr,
DeploymentHash, DeploymentState, Schema, StoreError,
};
use graph::{data::subgraph::schema::SubgraphError, prelude::SubgraphDeploymentEntity};
use stable_hash::crypto::SetHasher;
use std::str::FromStr;
use std::{collections::BTreeSet, convert::TryFrom, ops::Bound};
Expand Down Expand Up @@ -863,7 +866,7 @@ pub fn drop_metadata(conn: &PgConnection, site: &Site) -> Result<(), StoreError>
pub fn create_deployment(
conn: &PgConnection,
site: &Site,
deployment: SubgraphDeploymentEntity,
deployment: DeploymentCreate,
exists: bool,
replace: bool,
) -> Result<(), StoreError> {
Expand All @@ -881,7 +884,7 @@ pub fn create_deployment(
}
}

let SubgraphDeploymentEntity {
let DeploymentCreate {
manifest:
SubgraphManifestEntity {
spec_version,
Expand All @@ -890,33 +893,24 @@ pub fn create_deployment(
features,
schema,
},
failed,
health: _,
synced,
fatal_error: _,
non_fatal_errors: _,
earliest_block,
latest_block,
graft_base,
graft_block,
debug_fork,
reorg_count: _,
current_reorg_depth: _,
max_reorg_depth: _,
} = deployment;

let deployment_values = (
d::id.eq(site.id),
d::deployment.eq(site.deployment.as_str()),
d::failed.eq(failed),
d::synced.eq(synced),
d::failed.eq(false),
d::synced.eq(false),
d::health.eq(SubgraphHealth::Healthy),
d::fatal_error.eq::<Option<String>>(None),
d::non_fatal_errors.eq::<Vec<String>>(vec![]),
d::earliest_ethereum_block_hash.eq(b(&earliest_block)),
d::earliest_ethereum_block_number.eq(n(&earliest_block)),
d::latest_ethereum_block_hash.eq(b(&latest_block)),
d::latest_ethereum_block_number.eq(n(&latest_block)),
d::latest_ethereum_block_hash.eq(sql("null")),
d::latest_ethereum_block_number.eq(sql("null")),
d::entity_count.eq(sql("0")),
d::graft_base.eq(graft_base.as_ref().map(|s| s.as_str())),
d::graft_block_hash.eq(b(&graft_block)),
Expand Down
4 changes: 2 additions & 2 deletions store/postgres/src/deployment_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::time::Instant;
use graph::components::store::EntityCollection;
use graph::components::subgraph::ProofOfIndexingFinisher;
use graph::constraint_violation;
use graph::data::subgraph::schema::{SubgraphError, POI_OBJECT};
use graph::data::subgraph::schema::{DeploymentCreate, SubgraphError, POI_OBJECT};
use graph::prelude::{
anyhow, debug, info, lazy_static, o, warn, web3, ApiSchema, AttributeNames, BlockNumber,
BlockPtr, CheapClone, DeploymentHash, DeploymentState, Entity, EntityKey, EntityModification,
Expand Down Expand Up @@ -175,7 +175,7 @@ impl DeploymentStore {
pub(crate) fn create_deployment(
&self,
schema: &Schema,
deployment: SubgraphDeploymentEntity,
deployment: DeploymentCreate,
site: Arc<Site>,
graft_base: Option<Arc<Layout>>,
replace: bool,
Expand Down
20 changes: 5 additions & 15 deletions store/postgres/src/subgraph_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ use graph::{
},
constraint_violation,
data::query::QueryTarget,
data::subgraph::status,
data::subgraph::{schema::DeploymentCreate, status},
prelude::StoreEvent,
prelude::SubgraphDeploymentEntity,
prelude::{
anyhow, futures03::future::join_all, lazy_static, o, web3::types::Address, ApiSchema,
BlockNumber, BlockPtr, DeploymentHash, EntityOperation, Logger, NodeId, Schema, StoreError,
Expand Down Expand Up @@ -472,7 +471,7 @@ impl SubgraphStoreInner {
&self,
name: SubgraphName,
schema: &Schema,
deployment: SubgraphDeploymentEntity,
deployment: DeploymentCreate,
node_id: NodeId,
network_name: String,
mode: SubgraphVersionSwitchingMode,
Expand Down Expand Up @@ -585,21 +584,12 @@ impl SubgraphStoreInner {
}

// Transmogrify the deployment into a new one
let deployment = SubgraphDeploymentEntity {
let deployment = DeploymentCreate {
manifest: deployment.manifest,
failed: false,
health: deployment.health,
synced: false,
fatal_error: None,
non_fatal_errors: vec![],
earliest_block: deployment.earliest_block.clone(),
latest_block: None,
graft_base: Some(src.deployment.clone()),
graft_block: Some(block),
debug_fork: deployment.debug_fork,
reorg_count: 0,
current_reorg_depth: 0,
max_reorg_depth: 0,
};

let graft_base = self.layout(&src.deployment)?;
Expand Down Expand Up @@ -652,7 +642,7 @@ impl SubgraphStoreInner {
&self,
name: SubgraphName,
schema: &Schema,
deployment: SubgraphDeploymentEntity,
deployment: DeploymentCreate,
node_id: NodeId,
network_name: String,
mode: SubgraphVersionSwitchingMode,
Expand Down Expand Up @@ -1019,7 +1009,7 @@ impl SubgraphStoreTrait for SubgraphStore {
&self,
name: SubgraphName,
schema: &Schema,
deployment: SubgraphDeploymentEntity,
deployment: DeploymentCreate,
node_id: NodeId,
network_name: String,
mode: SubgraphVersionSwitchingMode,
Expand Down
2 changes: 1 addition & 1 deletion store/postgres/tests/graft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn insert_test_data(store: Arc<DieselSubgraphStore>) -> DeploymentLocator {
};

// Create SubgraphDeploymentEntity
let deployment = SubgraphDeploymentEntity::new(&manifest, false, None);
let deployment = DeploymentCreate::new(&manifest, None);
let name = SubgraphName::new("test/graft").unwrap();
let node_id = NodeId::new("test").unwrap();
let deployment = store
Expand Down
9 changes: 4 additions & 5 deletions store/postgres/tests/store.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use graph::data::graphql::ext::TypeDefinitionExt;
use graph::data::subgraph::schema::DeploymentCreate;
use graph_chain_ethereum::{Mapping, MappingABI};
use graph_mock::MockMetricsRegistry;
use hex_literal::hex;
Expand Down Expand Up @@ -165,7 +166,7 @@ fn insert_test_data(store: Arc<DieselSubgraphStore>) -> DeploymentLocator {
};

// Create SubgraphDeploymentEntity
let deployment = SubgraphDeploymentEntity::new(&manifest, false, None);
let deployment = DeploymentCreate::new(&manifest, None);
let name = SubgraphName::new("test/store").unwrap();
let node_id = NodeId::new("test").unwrap();
let deployment = store
Expand Down Expand Up @@ -1278,17 +1279,15 @@ fn entity_changes_are_fired_and_forwarded_to_subscriptions() {
chain: PhantomData,
};

// Create SubgraphDeploymentEntity
let deployment_entity =
SubgraphDeploymentEntity::new(&manifest, false, Some(TEST_BLOCK_0_PTR.clone()));
let deployment = DeploymentCreate::new(&manifest, Some(TEST_BLOCK_0_PTR.clone()));
let name = SubgraphName::new("test/entity-changes-are-fired").unwrap();
let node_id = NodeId::new("test").unwrap();
let deployment = store
.subgraph_store()
.create_subgraph_deployment(
name,
&schema,
deployment_entity,
deployment,
node_id,
NETWORK_NAME.to_string(),
SubgraphVersionSwitchingMode::Instant,
Expand Down
5 changes: 2 additions & 3 deletions store/postgres/tests/subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use graph::{
server::index_node::VersionInfo,
store::{DeploymentLocator, StatusStore},
},
data::subgraph::schema::SubgraphError,
data::subgraph::schema::SubgraphHealth,
data::subgraph::schema::{DeploymentCreate, SubgraphError},
prelude::EntityChange,
prelude::EntityChangeOperation,
prelude::QueryStoreManager,
prelude::Schema,
prelude::SubgraphDeploymentEntity,
prelude::SubgraphManifest,
prelude::SubgraphName,
prelude::SubgraphVersionSwitchingMode,
Expand Down Expand Up @@ -137,7 +136,7 @@ fn create_subgraph() {
templates: vec![],
chain: PhantomData,
};
let deployment = SubgraphDeploymentEntity::new(&manifest, false, None);
let deployment = DeploymentCreate::new(&manifest, None);
let node_id = NodeId::new("left").unwrap();

let (deployment, events) = tap_store_events(|| {
Expand Down
4 changes: 2 additions & 2 deletions store/test-store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use diesel::{self, PgConnection};
use graph::data::graphql::effort::LoadManager;
use graph::data::query::QueryResults;
use graph::data::query::QueryTarget;
use graph::data::subgraph::schema::SubgraphError;
use graph::data::subgraph::schema::{DeploymentCreate, SubgraphError};
use graph::log;
use graph::prelude::{QueryStoreManager as _, SubgraphStore as _, *};
use graph::semver::Version;
Expand Down Expand Up @@ -166,7 +166,7 @@ pub fn create_subgraph(
chain: PhantomData,
};

let deployment = SubgraphDeploymentEntity::new(&manifest, false, None).graft(base);
let deployment = DeploymentCreate::new(&manifest, None).graft(base);
let name = {
let mut name = subgraph_id.to_string();
name.truncate(32);
Expand Down

0 comments on commit 640b6bb

Please sign in to comment.