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

common: fix reallocate sometimes failing with >1000 active allocations #1051

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
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
189 changes: 115 additions & 74 deletions packages/indexer-common/src/indexer-management/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,56 +130,75 @@ export class NetworkMonitor {
async allocations(status: AllocationStatus): Promise<Allocation[]> {
try {
this.logger.debug(`Fetch ${status} allocations`)
const result = await this.networkSubgraph.checkedQuery(
gql`
query allocations($indexer: String!, $status: AllocationStatus!) {
allocations(
where: { indexer: $indexer, status: $status }
first: 1000
orderBy: createdAtBlockNumber
orderDirection: asc
let dataRemaining = true
let allocations: Allocation[] = []
let lastId = ''

while (dataRemaining) {
const result = await this.networkSubgraph.checkedQuery(
gql`
query allocations(
$indexer: String!
$status: AllocationStatus!
$lastId: String!
) {
id
indexer {
id
}
allocatedTokens
createdAtEpoch
closedAtEpoch
createdAtBlockHash
subgraphDeployment {
allocations(
where: { indexer: $indexer, status: $status, id_gt: $lastId }
first: 1000
orderBy: id
orderDirection: asc
) {
id
stakedTokens
signalledTokens
queryFeesAmount
indexer {
id
}
allocatedTokens
createdAtEpoch
closedAtEpoch
createdAtBlockHash
subgraphDeployment {
id
stakedTokens
signalledTokens
queryFeesAmount
}
}
}
}
`,
{
indexer: this.indexerOptions.address.toLocaleLowerCase(),
status: status,
},
)
`,
{
indexer: this.indexerOptions.address.toLocaleLowerCase(),
status: status,
lastId,
},
)

if (result.error) {
throw result.error
if (result.error) {
throw result.error
}

if (
!result.data.allocations ||
result.data.length === 0 ||
result.data.allocations.length === 0
) {
dataRemaining = false
} else {
lastId = result.data.allocations.slice(-1)[0].id
allocations = allocations.concat(
result.data.allocations.map(parseGraphQLAllocation),
)
}
}

if (
!result.data.allocations ||
result.data.length === 0 ||
result.data.allocations.length === 0
) {
if (allocations.length === 0) {
this.logger.warn(
`No ${
AllocationStatus[status.toUpperCase() as keyof typeof AllocationStatus]
} allocations found for indexer '${this.indexerOptions.address}'`,
)
return []
}

return result.data.allocations.map(parseGraphQLAllocation)
return allocations
} catch (error) {
const err = indexerError(IndexerErrorCode.IE010, error)
this.logger.error(`Failed to query indexer allocations`, {
Expand Down Expand Up @@ -231,56 +250,78 @@ export class NetworkMonitor {
): Promise<Allocation[]> {
try {
this.logger.debug('Fetch recently closed allocations')
const result = await this.networkSubgraph.checkedQuery(
gql`
query allocations($indexer: String!, $closedAtEpochThreshold: Int!) {
allocations(
where: {
indexer: $indexer
status: Closed
closedAtEpoch_gte: $closedAtEpochThreshold
}
first: 1000
let dataRemaining = true
let allocations: Allocation[] = []
let lastId = ''

while (dataRemaining) {
const result = await this.networkSubgraph.checkedQuery(
gql`
query allocations(
$indexer: String!
$closedAtEpochThreshold: Int!
$lastId: String!
) {
id
indexer {
id
}
allocatedTokens
createdAtEpoch
closedAtEpoch
createdAtBlockHash
subgraphDeployment {
allocations(
where: {
indexer: $indexer
status: Closed
closedAtEpoch_gte: $closedAtEpochThreshold
id_gt: $lastId
}
first: 1000
orderBy: id
orderDirection: desc
) {
id
stakedTokens
signalledTokens
queryFeesAmount
indexer {
id
}
allocatedTokens
createdAtEpoch
closedAtEpoch
createdAtBlockHash
subgraphDeployment {
id
stakedTokens
signalledTokens
queryFeesAmount
}
}
}
}
`,
{
indexer: this.indexerOptions.address.toLocaleLowerCase(),
closedAtEpochThreshold: currentEpoch - range,
},
)
`,
{
indexer: this.indexerOptions.address.toLocaleLowerCase(),
closedAtEpochThreshold: currentEpoch - range,
lastId,
},
)

if (result.error) {
throw result.error
if (result.error) {
throw result.error
}

if (
!result.data.allocations ||
result.data.length === 0 ||
result.data.allocations.length === 0
) {
dataRemaining = false
} else {
lastId = result.data.allocations.slice(-1)[0].id
allocations = allocations.concat(
result.data.allocations.map(parseGraphQLAllocation),
)
}
}

if (
!result.data.allocations ||
result.data.length === 0 ||
result.data.allocations.length === 0
) {
if (allocations.length === 0) {
this.logger.warn(
`No recently closed allocations found for indexer '${this.indexerOptions.address}'`,
)
return []
}

return result.data.allocations.map(parseGraphQLAllocation)
return allocations
} catch (error) {
const err = indexerError(IndexerErrorCode.IE010, error)
this.logger.error(`Failed to query indexer's recently closed allocations`, {
Expand Down
Loading