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

fix: paginate allocation queries #862

Merged
merged 2 commits into from
Feb 29, 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
103 changes: 71 additions & 32 deletions packages/indexer-service/src/allocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ export const monitorEligibleAllocations = ({

const currentEpoch = currentEpochResult.data.graphNetwork.currentEpoch

const result = await networkSubgraph.query(
gql`
query allocations($indexer: String!, $closedAtEpochThreshold: Int!) {
indexer(id: $indexer) {
activeAllocations: totalAllocations(
where: { status: Active }
orderDirection: desc
let lastId = ''
const activeAllocations = []
for (;;) {
const result = await networkSubgraph.query(
gql`
query allocations($indexer: String!, $lastId: String!) {
allocations(
where: { indexer: $indexer, id_gt: $lastId, status: Active }
orderBy: id
orderDirection: asc
first: 1000
) {
id
Expand All @@ -85,9 +88,43 @@ export const monitorEligibleAllocations = ({
queryFeesAmount
}
}
recentlyClosedAllocations: totalAllocations(
where: { status: Closed, closedAtEpoch_gte: $closedAtEpochThreshold }
orderDirection: desc
}
`,
{
indexer: indexer.toLowerCase(),
lastId,
},
)

if (result.error) {
throw result.error
}
if (result.data.allocations.length == 0) {
break
}
activeAllocations.push(...result.data.allocations)
lastId = result.data.allocations.slice(-1)[0].id
}

lastId = ''
const recentlyClosedAllocations = []
for (;;) {
const result = await networkSubgraph.query(
gql`
query allocations(
$indexer: String!
$lastId: String!
$closedAtEpochThreshold: Int!
) {
allocations(
where: {
indexer: $indexer
id_gt: $lastId
status: Closed
closedAtEpoch_gte: $closedAtEpochThreshold
}
orderBy: id
orderDirection: asc
first: 1000
) {
id
Expand All @@ -106,31 +143,32 @@ export const monitorEligibleAllocations = ({
}
}
}
}
`,
{
indexer: indexer.toLowerCase(),
closedAtEpochThreshold: currentEpoch - 1, // allocation can be closed within the last epoch or later
},
)

if (result.error) {
throw result.error
}
`,
{
indexer: indexer.toLowerCase(),
lastId,
closedAtEpochThreshold: currentEpoch - 1, // allocation can be closed within the last epoch or later
},
)

if (!result.data) {
throw new Error(`No data / indexer not found on chain`)
if (result.error) {
throw result.error
}
if (result.data.allocations.length == 0) {
break
}
recentlyClosedAllocations.push(...result.data.allocations)
lastId = result.data.allocations.slice(-1)[0].id
}

if (!result.data.indexer) {
throw new Error(`Indexer not found on chain`)
const allocations = [...activeAllocations, ...recentlyClosedAllocations]

if (allocations.length == 0) {
throw new Error(`No data / indexer not found on chain`)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return [
...result.data.indexer.activeAllocations,
...result.data.indexer.recentlyClosedAllocations,
].map(x => parseGraphQLAllocation(x, protocolNetwork))
return allocations.map(x => parseGraphQLAllocation(x, protocolNetwork))
} catch (err) {
logger.warn(`Failed to query indexer allocations, keeping existing`, {
allocations: currentAllocations.map(allocation => allocation.id),
Expand Down Expand Up @@ -180,7 +218,7 @@ export const ensureAttestationSigners = ({
const logger = parentLogger.child({ component: 'AttestationSignerCache' })

const cache: AttestationSignerCache = new LRUCache(null, {
maxlen: 1000,
maxlen: 5000,
})

const signers = allocations.map(async allocations => {
Expand Down Expand Up @@ -225,8 +263,9 @@ export const ensureAttestationSigners = ({
})

signers.pipe(signers => {
logger.info(`Cached attestation signers`, {
allocations: [...signers.keys()],
const attestationSigners = [...signers.keys()]
logger.info(`Cached ${attestationSigners.length} attestation signers`, {
allocations: attestationSigners,
})
})

Expand Down
Loading