Skip to content

Commit f17b402

Browse files
committed
common: skip querying active deployments if in MANUAL or poiDisputeMonitoring is disabled
1 parent d393c2d commit f17b402

File tree

4 files changed

+148
-95
lines changed

4 files changed

+148
-95
lines changed

packages/indexer-agent/src/agent.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,32 @@ export class Agent {
324324
},
325325
)
326326

327+
// Skip fetching active deployments if the deployment management mode is manual and POI tracking is disabled
327328
const activeDeployments: Eventual<SubgraphDeploymentID[]> =
328329
sequentialTimerMap(
329-
{ logger, milliseconds: requestIntervalSmall },
330-
() => {
331-
logger.trace('Fetching active deployments')
332-
return this.graphNode.subgraphDeployments()
330+
{ logger, milliseconds: requestIntervalLarge },
331+
async () => {
332+
const deployments = await this.multiNetworks.map(
333+
async ({ network }) => {
334+
if (
335+
this.deploymentManagement === DeploymentManagementMode.AUTO ||
336+
network.networkMonitor.poiDisputeMonitoringEnabled()
337+
) {
338+
logger.trace('Fetching active deployments')
339+
const assignments =
340+
await this.graphNode.subgraphDeploymentsAssignments(
341+
SubgraphStatus.ACTIVE,
342+
)
343+
return assignments.map(assignment => assignment.id)
344+
} else {
345+
logger.info(
346+
"Skipping fetching active deployments fetch since DeploymentManagementMode = 'manual' and POI tracking is disabled",
347+
)
348+
return []
349+
}
350+
},
351+
)
352+
return deployments.values
333353
},
334354
{
335355
onError: error =>

packages/indexer-common/src/graph-node.ts

Lines changed: 118 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { BlockPointer, ChainIndexingStatus, IndexingStatus } from './types'
1212
import pRetry, { Options } from 'p-retry'
1313
import axios, { AxiosInstance } from 'axios'
1414
import fetch from 'isomorphic-fetch'
15-
import { Action } from './indexer-management'
1615

1716
interface indexNode {
1817
id: string
@@ -103,15 +102,11 @@ export class GraphNode {
103102
this.logger.info(`Check if indexing status API is available`)
104103
await pRetry(
105104
async () => {
106-
const deployments = await this.subgraphDeployments()
107-
if (deployments.length < 100) {
108-
this.logger.info(`Successfully connected to indexing status API`, {
109-
currentDeployments: deployments.map((deployment) => deployment.display),
110-
})
105+
if (await this.statusEndpointConnected()) {
106+
this.logger.info(`Successfully connected to indexing status API`, {})
111107
} else {
112-
this.logger.info(`Successfully connected to indexing status API`, {
113-
currentDeploymentCount: deployments.length,
114-
})
108+
this.logger.error(`Failed to connect to indexing status API`)
109+
throw new Error('Indexing status API not available')
115110
}
116111
},
117112
{
@@ -149,97 +144,120 @@ export class GraphNode {
149144
return new URL(deploymentIpfsHash, this.queryBaseURL).toString()
150145
}
151146

152-
public async subgraphDeployments(): Promise<SubgraphDeploymentID[]> {
153-
return (await this.subgraphDeploymentsAssignments(SubgraphStatus.ACTIVE)).map(
154-
(details) => details.id,
155-
)
147+
// Simple query to make sure the status endpoint is connected
148+
public async statusEndpointConnected(): Promise<boolean> {
149+
try {
150+
const result = await this.status
151+
.query(
152+
gql`
153+
query {
154+
__typename
155+
}
156+
`,
157+
undefined,
158+
)
159+
.toPromise()
160+
161+
if (result.error) {
162+
throw result.error
163+
}
164+
165+
return !!result.data
166+
} catch (error) {
167+
this.logger.error(`Failed to query status endpoint`, { error })
168+
return false
169+
}
156170
}
157171

158-
public async subgraphDeploymentAssignmentsForAllocateActions(
172+
public async subgraphDeploymentAssignmentsByDeploymentID(
159173
subgraphStatus: SubgraphStatus,
160-
actions: Action[],
174+
deploymentIDs: string[],
161175
): Promise<SubgraphDeploymentAssignment[]> {
162-
const deploymentIDs = actions.map((action) => action.deploymentID)
163-
164-
const nodeOnlyResult = await this.status
165-
.query(
166-
gql`
167-
query indexingStatuses($subgraphs: [String!]!) {
168-
indexingStatuses(subgraphs: $subgraphs) {
169-
subgraphDeployment: subgraph
170-
node
176+
try {
177+
const nodeOnlyResult = await this.status
178+
.query(
179+
gql`
180+
query indexingStatuses($subgraphs: [String!]!) {
181+
indexingStatuses(subgraphs: $subgraphs) {
182+
subgraphDeployment: subgraph
183+
node
184+
}
171185
}
172-
}
173-
`,
174-
{ subgraphs: deploymentIDs },
175-
)
176-
.toPromise()
186+
`,
187+
{ subgraphs: deploymentIDs },
188+
)
189+
.toPromise()
177190

178-
if (nodeOnlyResult.error) {
179-
throw nodeOnlyResult.error
180-
}
191+
if (nodeOnlyResult.error) {
192+
throw nodeOnlyResult.error
193+
}
181194

182-
const withAssignments: string[] = nodeOnlyResult.data.indexingStatuses
183-
.filter(
184-
(result: { node: string | null }) =>
185-
result.node !== null && result.node !== undefined,
186-
)
187-
.map((result: { subgraphDeployment: string }) => result.subgraphDeployment)
188-
189-
const result = await this.status
190-
.query(
191-
gql`
192-
query indexingStatuses($subgraphs: [String!]!) {
193-
indexingStatuses(subgraphs: $subgraphs) {
194-
subgraphDeployment: subgraph
195-
node
196-
paused
195+
const withAssignments: string[] = nodeOnlyResult.data.indexingStatuses
196+
.filter(
197+
(result: { node: string | null }) =>
198+
result.node !== null && result.node !== undefined,
199+
)
200+
.map((result: { subgraphDeployment: string }) => result.subgraphDeployment)
201+
202+
const result = await this.status
203+
.query(
204+
gql`
205+
query indexingStatuses($subgraphs: [String!]!) {
206+
indexingStatuses(subgraphs: $subgraphs) {
207+
subgraphDeployment: subgraph
208+
node
209+
paused
210+
}
197211
}
198-
}
199-
`,
200-
{ subgraphs: withAssignments },
201-
)
202-
.toPromise()
212+
`,
213+
{ subgraphs: withAssignments },
214+
)
215+
.toPromise()
203216

204-
if (result.error) {
205-
throw result.error
206-
}
217+
if (result.error) {
218+
throw result.error
219+
}
207220

208-
if (!result.data.indexingStatuses || result.data.length === 0) {
209-
this.logger.warn(`No 'indexingStatuses' data returned from index nodes`, {
210-
data: result.data,
211-
})
212-
return []
213-
}
221+
if (!result.data.indexingStatuses || result.data.length === 0) {
222+
this.logger.warn(`No 'indexingStatuses' data returned from index nodes`, {
223+
data: result.data,
224+
})
225+
return []
226+
}
214227

215-
type QueryResult = {
216-
subgraphDeployment: string
217-
node: string | undefined
218-
paused: boolean | undefined
219-
}
228+
type QueryResult = {
229+
subgraphDeployment: string
230+
node: string | undefined
231+
paused: boolean | undefined
232+
}
220233

221-
const results = result.data.indexingStatuses
222-
.filter((status: QueryResult) => {
223-
if (subgraphStatus === SubgraphStatus.ACTIVE) {
224-
return (
225-
status.paused === false ||
226-
(status.paused === undefined && status.node !== 'removed')
227-
)
228-
} else if (subgraphStatus === SubgraphStatus.PAUSED) {
229-
return status.node === 'removed' || status.paused === true
230-
} else if (subgraphStatus === SubgraphStatus.ALL) {
231-
return true
232-
}
233-
})
234-
.map((status: QueryResult) => {
235-
return {
236-
id: new SubgraphDeploymentID(status.subgraphDeployment),
237-
node: status.node,
238-
paused: status.paused ?? status.node === 'removed',
239-
}
240-
})
234+
const results = result.data.indexingStatuses
235+
.filter((status: QueryResult) => {
236+
if (subgraphStatus === SubgraphStatus.ACTIVE) {
237+
return (
238+
status.paused === false ||
239+
(status.paused === undefined && status.node !== 'removed')
240+
)
241+
} else if (subgraphStatus === SubgraphStatus.PAUSED) {
242+
return status.node === 'removed' || status.paused === true
243+
} else if (subgraphStatus === SubgraphStatus.ALL) {
244+
return true
245+
}
246+
})
247+
.map((status: QueryResult) => {
248+
return {
249+
id: new SubgraphDeploymentID(status.subgraphDeployment),
250+
node: status.node,
251+
paused: status.paused ?? status.node === 'removed',
252+
}
253+
})
241254

242-
return results
255+
return results
256+
} catch (error) {
257+
const err = indexerError(IndexerErrorCode.IE018, error)
258+
this.logger.error(`Failed to query indexing status API`, { err })
259+
throw err
260+
}
243261
}
244262

245263
public async subgraphDeploymentsAssignments(
@@ -265,8 +283,11 @@ export class GraphNode {
265283
)
266284
.toPromise()
267285

286+
const deploymentCount = nodeOnlyResult.data?.indexingStatuses?.length ?? 0
268287
this.logger.debug(
269-
`Fetch subgraph deployment assignments took ${Date.now() - startTimeMs}ms`,
288+
`Fetch subgraph deployment assignments (1/2, node only) took ${
289+
Date.now() - startTimeMs
290+
}ms for ${deploymentCount} deployments`,
270291
)
271292

272293
if (nodeOnlyResult.error) {
@@ -313,6 +334,12 @@ export class GraphNode {
313334
paused: boolean | undefined
314335
}
315336

337+
const deploymentCount2 = result.data?.indexingStatuses?.length ?? 0
338+
this.logger.debug(
339+
`Fetch subgraph deployment assignments (2/2, with paused) took ${
340+
Date.now() - startTimeMs
341+
}ms and returned ${deploymentCount}/${deploymentCount2} deployments`,
342+
)
316343
const results = result.data.indexingStatuses
317344
.filter((status: QueryResult) => {
318345
if (subgraphStatus === SubgraphStatus.ACTIVE) {
@@ -557,7 +584,9 @@ export class GraphNode {
557584
try {
558585
const deploymentAssignments =
559586
currentAssignments ??
560-
(await this.subgraphDeploymentsAssignments(SubgraphStatus.ALL))
587+
(await this.subgraphDeploymentAssignmentsByDeploymentID(SubgraphStatus.ALL, [
588+
deployment.ipfsHash,
589+
]))
561590
const matchingAssignment = deploymentAssignments.find(
562591
(deploymentAssignment) => deploymentAssignment.id.ipfsHash == deployment.ipfsHash,
563592
)

packages/indexer-common/src/indexer-management/allocations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ export class AllocationManager {
323323
allocateActions,
324324
})
325325
const currentAssignments =
326-
await this.graphNode.subgraphDeploymentAssignmentsForAllocateActions(
326+
await this.graphNode.subgraphDeploymentAssignmentsByDeploymentID(
327327
SubgraphStatus.ALL,
328-
actions,
328+
actions.map((action) => action.deploymentID!),
329329
)
330330
await pMap(
331331
allocateActions,

packages/indexer-common/src/indexer-management/monitor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export class NetworkMonitor {
5151
private epochSubgraph: EpochSubgraph,
5252
) {}
5353

54+
poiDisputeMonitoringEnabled(): boolean {
55+
return this.indexerOptions.poiDisputeMonitoring
56+
}
57+
5458
async currentEpochNumber(): Promise<number> {
5559
return (await this.contracts.epochManager.currentEpoch()).toNumber()
5660
}

0 commit comments

Comments
 (0)