Skip to content

Commit

Permalink
common: implement deploySubgraph function and its dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Nov 3, 2023
1 parent b45097a commit bd5ba1d
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 30 deletions.
29 changes: 19 additions & 10 deletions packages/indexer-common/src/__tests__/grafting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SubgraphLineageWithStatus,
} from '../grafting'
import { SubgraphDeploymentID } from '@graphprotocol/common-ts'
import { indexerError, IndexerErrorCode } from '../errors'
import { IndexerErrorCode } from '../errors'
import { SubgraphDeploymentDecisionKind } from '../types'

// Create a mock for the fetchSubgraphManifest function
Expand Down Expand Up @@ -173,7 +173,8 @@ describe('determineSubgraphDeploymentDecisions function', () => {
const expected = [
{
deployment: new SubgraphDeploymentID(base1),
deploymentDecision: SubgraphDeploymentDecisionKind.DEPLOY,
kind: SubgraphDeploymentDecisionKind.DEPLOY,
expectedBlockHeight: 1,
},
]
expect(decisions).toEqual(expected)
Expand All @@ -199,7 +200,8 @@ describe('determineSubgraphDeploymentDecisions function', () => {
const expected = [
{
deployment: new SubgraphDeploymentID(base2),
deploymentDecision: SubgraphDeploymentDecisionKind.DEPLOY,
kind: SubgraphDeploymentDecisionKind.DEPLOY,
expectedBlockHeight: 20,
},
]
expect(decisions).toEqual(expected)
Expand All @@ -226,7 +228,8 @@ describe('determineSubgraphDeploymentDecisions function', () => {
const expected = [
{
deployment: new SubgraphDeploymentID(base1),
deploymentDecision: SubgraphDeploymentDecisionKind.REMOVE,
kind: SubgraphDeploymentDecisionKind.REMOVE,
expectedBlockHeight: 10,
},
]
expect(decisions).toEqual(expected)
Expand Down Expand Up @@ -269,15 +272,18 @@ describe('determineSubgraphDeploymentDecisions function', () => {
const expected = [
{
deployment: new SubgraphDeploymentID(base3),
deploymentDecision: SubgraphDeploymentDecisionKind.REMOVE,
kind: SubgraphDeploymentDecisionKind.REMOVE,
expectedBlockHeight: 10,
},
{
deployment: new SubgraphDeploymentID(base2),
deploymentDecision: SubgraphDeploymentDecisionKind.REMOVE,
kind: SubgraphDeploymentDecisionKind.REMOVE,
expectedBlockHeight: 20,
},
{
deployment: new SubgraphDeploymentID(base1),
deploymentDecision: SubgraphDeploymentDecisionKind.DEPLOY,
kind: SubgraphDeploymentDecisionKind.DEPLOY,
expectedBlockHeight: 30,
},
]
expect(decisions).toEqual(expected)
Expand Down Expand Up @@ -330,16 +336,19 @@ describe('determineSubgraphDeploymentDecisions function', () => {
const expected = [
{
deployment: new SubgraphDeploymentID(base4),
deploymentDecision: SubgraphDeploymentDecisionKind.REMOVE,
kind: SubgraphDeploymentDecisionKind.REMOVE,
expectedBlockHeight: 5,
},
// Base 3 is intentionally left out of the result.
{
deployment: new SubgraphDeploymentID(base2),
deploymentDecision: SubgraphDeploymentDecisionKind.REMOVE,
kind: SubgraphDeploymentDecisionKind.REMOVE,
expectedBlockHeight: 20,
},
{
deployment: new SubgraphDeploymentID(base1),
deploymentDecision: SubgraphDeploymentDecisionKind.DEPLOY,
kind: SubgraphDeploymentDecisionKind.DEPLOY,
expectedBlockHeight: 30,
},
]
expect(decisions).toEqual(expected)
Expand Down
2 changes: 1 addition & 1 deletion packages/indexer-common/src/__tests__/subgraph.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DocumentNode, print } from 'graphql'
import {
SubgraphFreshnessChecker,
LoggerInterface,
ProviderInterface,
SubgraphQueryInterface,
} from '../subgraphs'
import { LoggerInterface } from '../types'
import { QueryResult } from '../network-subgraph'
import gql from 'graphql-tag'
import { mergeSelectionSets } from '../utils'
Expand Down
46 changes: 41 additions & 5 deletions packages/indexer-common/src/grafting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { SubgraphDeploymentID } from '@graphprotocol/common-ts'
import { GraphNodeInterface } from './graph-node'
import {
BlockPointer,
LoggerInterface,
SubgraphDeploymentDecision,
SubgraphDeploymentDecisionKind,
SubgraphManifest,
} from './types'
import { indexerError, IndexerErrorCode } from './errors'
import pMap from 'p-map'

// Any type that can return a SubgraphManifest when given a
// SubgraphDeploymentID as input.
type SubgraphManifestResolver = (
export type SubgraphManifestResolver = (
subgraphID: SubgraphDeploymentID,
) => Promise<SubgraphManifest>

Expand Down Expand Up @@ -49,6 +51,10 @@ export interface SubgraphLineageWithStatus extends SubgraphLineage {
bases: GraftSubject[]
}

export interface GraftBaseDeploymentDecision extends SubgraphDeploymentDecision {
expectedBlockHeight: number
}

// Discovers all graft dependencies for a given subgraph.
export async function discoverLineage(
subgraphManifestResolver: SubgraphManifestResolver,
Expand Down Expand Up @@ -114,8 +120,8 @@ export async function getIndexingStatus(

export function determineSubgraphDeploymentDecisions(
subgraphLineage: SubgraphLineageWithStatus,
): SubgraphDeploymentDecision[] {
const deploymentDecisions: SubgraphDeploymentDecision[] = []
): GraftBaseDeploymentDecision[] {
const deploymentDecisions: GraftBaseDeploymentDecision[] = []

// Check lineage size before making any assumptions.
if (!subgraphLineage.bases.length) {
Expand Down Expand Up @@ -143,7 +149,8 @@ export function determineSubgraphDeploymentDecisions(
// Graph Node is not aware of this subgraph deployment. We must deploy it and look no further.
deploymentDecisions.push({
deployment: graft.deployment,
deploymentDecision: SubgraphDeploymentDecisionKind.DEPLOY,
kind: SubgraphDeploymentDecisionKind.DEPLOY,
expectedBlockHeight: graft.block,
})
break
}
Expand All @@ -154,7 +161,8 @@ export function determineSubgraphDeploymentDecisions(
// If so, we can stop syncing it.
deploymentDecisions.push({
deployment: graft.deployment,
deploymentDecision: SubgraphDeploymentDecisionKind.REMOVE,
kind: SubgraphDeploymentDecisionKind.REMOVE,
expectedBlockHeight: graft.block,
})
continue
}
Expand All @@ -169,3 +177,31 @@ export function determineSubgraphDeploymentDecisions(
}
return deploymentDecisions
}

// Queries the Graph Node to get the deployment status of each graft base in the
// subgraph lineage.
export async function queryGraftBaseStatuses(
subgraphLineage: SubgraphLineage,
graphNode: GraphNodeInterface,
parentLogger: LoggerInterface,
concurrency: number = 5,
): Promise<SubgraphLineageWithStatus> {
const logger = parentLogger.child({ function: 'queryGraftBaseStatuses' })
logger.debug('Attempting to resolve graft bases for target subgraph')

// Fetch deployment details for each graft base
logger.debug('Querying Graph-Node for graft bases deployment status')
const graftBasesDeploymentStatus = await pMap(
subgraphLineage.bases,
async (graftBase: GraftBase) => await getIndexingStatus(graftBase, graphNode),
{
stopOnError: true,
concurrency,
},
)

return {
target: subgraphLineage.target,
bases: graftBasesDeploymentStatus,
}
}
13 changes: 10 additions & 3 deletions packages/indexer-common/src/graph-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export const parseGraphQLBlockPointer = (block: any): BlockPointer | null =>

export interface GraphNodeInterface {
indexingStatus(deployments: SubgraphDeploymentID[]): Promise<IndexingStatus[]>
ensure(name: string, deployment: SubgraphDeploymentID): Promise<void>
remove(deployment: SubgraphDeploymentID): Promise<void>
}

export class GraphNode implements GraphNodeInterface {
Expand Down Expand Up @@ -391,11 +393,16 @@ export class GraphNode implements GraphNodeInterface {
} catch (error) {
if (!(error instanceof IndexerError)) {
const errorCode = IndexerErrorCode.IE020
this.logger.error(INDEXER_ERROR_MESSAGES[errorCode], {
const unknownIndexerError = indexerError(IndexerErrorCode.IE020, error)
const payload = {
name,
deployment: deployment.display,
error: indexerError(errorCode, error),
})
error: unknownIndexerError,
}
this.logger.error(INDEXER_ERROR_MESSAGES[errorCode], payload)
throw unknownIndexerError
} else {
throw error
}
}
}
Expand Down
Loading

0 comments on commit bd5ba1d

Please sign in to comment.