Skip to content
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

Add new table to track subgraph features #4679

Merged
merged 4 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions chain/arweave/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
fn manifest_idx(&self) -> u32 {
unreachable!("arweave does not support dynamic data sources")
}

fn kind(&self) -> &str {
&self.kind
}
}

#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions chain/cosmos/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
fn manifest_idx(&self) -> u32 {
unimplemented!("{}", TEMPLATE_ERROR);
}

fn kind(&self) -> &str {
&self.kind
}
}

#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions chain/ethereum/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,10 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
fn manifest_idx(&self) -> u32 {
self.manifest_idx
}

fn kind(&self) -> &str {
&self.kind
}
}

#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions chain/near/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
fn manifest_idx(&self) -> u32 {
unreachable!("near does not support dynamic data sources")
}

fn kind(&self) -> &str {
&self.kind
}
}

#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions chain/substreams/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ impl blockchain::DataSourceTemplate<Chain> for NoopDataSourceTemplate {
fn manifest_idx(&self) -> u32 {
todo!()
}

fn kind(&self) -> &str {
unimplemented!("{}", TEMPLATE_ERROR);
}
}

#[async_trait]
Expand Down
1 change: 1 addition & 0 deletions core/src/subgraph/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ async fn create_subgraph_version<C: Blockchain, S: SubgraphStore>(
name,
&manifest.schema,
deployment,
manifest.deployment_features(),
node_id,
network_name,
version_switching_mode,
Expand Down
4 changes: 4 additions & 0 deletions graph/src/blockchain/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ impl<C: Blockchain> DataSourceTemplate<C> for MockDataSourceTemplate {
fn manifest_idx(&self) -> u32 {
todo!()
}

fn kind(&self) -> &str {
todo!()
}
}

#[derive(Clone, Default, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions graph/src/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ pub trait DataSourceTemplate<C: Blockchain>: Send + Sync + Debug {
fn runtime(&self) -> Option<Arc<Vec<u8>>>;
fn name(&self) -> &str;
fn manifest_idx(&self) -> u32;
fn kind(&self) -> &str;
}

#[async_trait]
Expand Down
3 changes: 2 additions & 1 deletion graph/src/components/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::components::server::index_node::VersionInfo;
use crate::components::transaction_receipt;
use crate::components::versions::ApiVersion;
use crate::data::query::Trace;
use crate::data::subgraph::status;
use crate::data::subgraph::{status, DeploymentFeatures};
use crate::data::value::Object;
use crate::data::{query::QueryTarget, subgraph::schema::*};
use crate::schema::{ApiSchema, InputSchema};
Expand Down Expand Up @@ -70,6 +70,7 @@ pub trait SubgraphStore: Send + Sync + 'static {
name: SubgraphName,
schema: &InputSchema,
deployment: DeploymentCreate,
deployment_features: DeploymentFeatures,
node_id: NodeId,
network: String,
mode: SubgraphVersionSwitchingMode,
Expand Down
57 changes: 56 additions & 1 deletion graph/src/data/subgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ pub use features::{SubgraphFeature, SubgraphFeatureValidationError};

use anyhow::{anyhow, Context, Error};
use futures03::{future::try_join3, stream::FuturesOrdered, TryStreamExt as _};
use itertools::Itertools;
use semver::Version;
use serde::{de, ser};
use serde_yaml;
use slog::Logger;
use stable_hash::{FieldAddress, StableHash};
use stable_hash_legacy::SequenceNumber;
use std::{
collections::{BTreeSet, HashMap},
collections::{BTreeSet, HashMap, HashSet},
marker::PhantomData,
};
use thiserror::Error;
Expand Down Expand Up @@ -497,6 +498,15 @@ impl Graft {
}
}

#[derive(Clone, Debug)]
pub struct DeploymentFeatures {
pub id: DeploymentHash,
pub spec_version: String,
pub api_versions: Vec<String>,
pub features: Vec<String>,
pub data_source_kinds: Vec<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BaseSubgraphManifest<C, S, D, T> {
Expand Down Expand Up @@ -663,6 +673,51 @@ impl<C: Blockchain> SubgraphManifest<C> {
.chain(self.data_sources.iter().map(|source| source.api_version()))
}

pub fn deployment_features(&self) -> DeploymentFeatures {
let mut api_versions = self
.api_versions()
.map(|v| v.to_string())
.collect::<HashSet<_>>();

let template_api_versions = self
.templates
.iter()
.map(|t| t.api_version().to_string())
.collect::<HashSet<_>>();

api_versions.extend(template_api_versions);

let features = self
.features
.iter()
.map(|f| f.to_string())
.collect::<Vec<_>>();

let spec_version = self.spec_version.to_string();

let mut data_source_kinds = self
.data_sources
.iter()
.map(|ds| ds.kind().to_string())
.collect::<HashSet<_>>();

let data_source_template_kinds = self
.templates
.iter()
.map(|t| t.kind().to_string())
.collect::<Vec<_>>();

data_source_kinds.extend(data_source_template_kinds);

DeploymentFeatures {
id: self.id.clone(),
api_versions: api_versions.into_iter().collect_vec(),
features,
spec_version,
data_source_kinds: data_source_kinds.into_iter().collect_vec(),
}
}

pub fn runtimes(&self) -> impl Iterator<Item = Arc<Vec<u8>>> + '_ {
self.templates
.iter()
Expand Down
7 changes: 7 additions & 0 deletions graph/src/data_source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ impl<C: Blockchain> DataSourceTemplate<C> {
Self::Offchain(ds) => ds.manifest_idx,
}
}

pub fn kind(&self) -> String {
match self {
Self::Onchain(ds) => ds.kind().to_string(),
Self::Offchain(ds) => ds.kind.to_owned(),
}
}
}

#[derive(Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE subgraph_features;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Creates a new table subgraph_features
create table if not exists subgraphs.subgraph_features (
id text primary key,
spec_version text not null,
api_versions text [] not null,
features text [] not null DEFAULT '{}',
data_sources text [] not null DEFAULT '{}'
);
34 changes: 34 additions & 0 deletions store/postgres/src/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ table! {
}
}

table! {
subgraphs.subgraph_features (id) {
id -> Text,
spec_version -> Text,
api_versions -> Array<Text>,
features -> Array<Text>,
data_sources -> Array<Text>,
}
incrypto32 marked this conversation as resolved.
Show resolved Hide resolved
}

table! {
subgraphs.subgraph_version (vid) {
vid -> BigInt,
Expand Down Expand Up @@ -1098,6 +1108,30 @@ impl<'a> Connection<'a> {
}
}

pub fn create_deployment_features(
&self,
id: String,
spec_version: String,
features: Vec<String>,
api_versions: Vec<String>,
data_source_kinds: Vec<String>,
) -> Result<(), StoreError> {
use subgraph_features as f;

let conn = self.conn.as_ref();
insert_into(f::table)
.values((
f::id.eq(id),
f::spec_version.eq(spec_version),
f::api_versions.eq(api_versions),
f::features.eq(features),
f::data_sources.eq(data_source_kinds),
))
.on_conflict_do_nothing()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets overwrite on conflict:

    .on_conflict(f::id)
    .do_update()

.execute(conn)?;
Ok(())
}

pub fn assign_subgraph(
&self,
site: &Site,
Expand Down
27 changes: 25 additions & 2 deletions store/postgres/src/subgraph_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use graph::{
},
constraint_violation,
data::query::QueryTarget,
data::subgraph::{schema::DeploymentCreate, status},
data::subgraph::{schema::DeploymentCreate, status, DeploymentFeatures},
prelude::StoreEvent,
prelude::{
anyhow, futures03::future::join_all, lazy_static, o, web3::types::Address, ApiVersion,
Expand Down Expand Up @@ -503,6 +503,7 @@ impl SubgraphStoreInner {
name: SubgraphName,
schema: &InputSchema,
deployment: DeploymentCreate,
features: DeploymentFeatures,
node_id: NodeId,
network_name: String,
mode: SubgraphVersionSwitchingMode,
Expand Down Expand Up @@ -571,10 +572,20 @@ impl SubgraphStoreInner {
// FIXME: This simultaneously holds a `primary_conn` and a shard connection, which can
// potentially deadlock.
let pconn = self.primary_conn()?;

pconn.transaction(|| -> Result<_, StoreError> {
// Create subgraph, subgraph version, and assignment
let changes =
pconn.create_subgraph_version(name, &site, node_id, mode, exists_and_synced)?;

pconn.create_deployment_features(
features.id.to_string(),
features.spec_version,
features.features,
features.api_versions,
features.data_source_kinds,
)?;

let event = StoreEvent::new(changes);
pconn.send_store_event(&self.sender, &event)?;
Ok(())
Expand Down Expand Up @@ -686,11 +697,21 @@ impl SubgraphStoreInner {
name: SubgraphName,
schema: &InputSchema,
deployment: DeploymentCreate,
deployment_features: DeploymentFeatures,
node_id: NodeId,
network_name: String,
mode: SubgraphVersionSwitchingMode,
) -> Result<DeploymentLocator, StoreError> {
self.create_deployment_internal(name, schema, deployment, node_id, network_name, mode, true)
self.create_deployment_internal(
name,
schema,
deployment,
deployment_features,
node_id,
network_name,
mode,
true,
)
}

pub(crate) fn send_store_event(&self, event: &StoreEvent) -> Result<(), StoreError> {
Expand Down Expand Up @@ -1240,6 +1261,7 @@ impl SubgraphStoreTrait for SubgraphStore {
name: SubgraphName,
schema: &InputSchema,
deployment: DeploymentCreate,
deployment_features: DeploymentFeatures,
node_id: NodeId,
network_name: String,
mode: SubgraphVersionSwitchingMode,
Expand All @@ -1248,6 +1270,7 @@ impl SubgraphStoreTrait for SubgraphStore {
name,
schema,
deployment,
deployment_features,
node_id,
network_name,
mode,
Expand Down
1 change: 1 addition & 0 deletions store/test-store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ pub async fn create_subgraph(
name,
&schema,
deployment,
manifest.deployment_features(),
NODE_ID.clone(),
NETWORK_NAME.to_string(),
SubgraphVersionSwitchingMode::Instant,
Expand Down
1 change: 1 addition & 0 deletions store/test-store/tests/graph/entity_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ async fn insert_test_data(store: Arc<DieselSubgraphStore>) -> DeploymentLocator
name,
&LOAD_RELATED_SUBGRAPH,
deployment,
manifest.deployment_features(),
node_id,
NETWORK_NAME.to_string(),
SubgraphVersionSwitchingMode::Instant,
Expand Down
1 change: 1 addition & 0 deletions store/test-store/tests/graphql/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ async fn insert_test_entities(
name,
&manifest.schema,
deployment,
manifest.deployment_features(),
node_id,
NETWORK_NAME.to_string(),
SubgraphVersionSwitchingMode::Instant,
Expand Down
1 change: 1 addition & 0 deletions store/test-store/tests/postgres/graft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ async fn insert_test_data(store: Arc<DieselSubgraphStore>) -> DeploymentLocator
name,
&TEST_SUBGRAPH_SCHEMA,
deployment,
manifest.deployment_features(),
node_id,
"fake_network".to_string(),
SubgraphVersionSwitchingMode::Instant,
Expand Down
2 changes: 2 additions & 0 deletions store/test-store/tests/postgres/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ async fn insert_test_data(store: Arc<DieselSubgraphStore>) -> DeploymentLocator
name,
&TEST_SUBGRAPH_SCHEMA,
deployment,
manifest.deployment_features(),
node_id,
NETWORK_NAME.to_string(),
SubgraphVersionSwitchingMode::Instant,
Expand Down Expand Up @@ -1270,6 +1271,7 @@ fn entity_changes_are_fired_and_forwarded_to_subscriptions() {
name,
&schema,
deployment,
manifest.deployment_features(),
node_id,
NETWORK_NAME.to_string(),
SubgraphVersionSwitchingMode::Instant,
Expand Down
1 change: 1 addition & 0 deletions store/test-store/tests/postgres/subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ fn create_subgraph() {
name,
&schema,
deployment,
manifest.deployment_features(),
node_id,
NETWORK_NAME.to_string(),
mode,
Expand Down
1 change: 1 addition & 0 deletions store/test-store/tests/postgres/writable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ async fn insert_test_data(store: Arc<DieselSubgraphStore>) -> DeploymentLocator
name,
&TEST_SUBGRAPH_SCHEMA,
deployment,
manifest.deployment_features(),
node_id,
NETWORK_NAME.to_string(),
SubgraphVersionSwitchingMode::Instant,
Expand Down