github: Batch graphql requests to avoid resource limit errors#271
Open
f0rmiga wants to merge 1 commit into
Open
github: Batch graphql requests to avoid resource limit errors#271f0rmiga wants to merge 1 commit into
f0rmiga wants to merge 1 commit into
Conversation
Github rejects graphql requests that are too large or expensive with errors like "Resource limits for this query exceeded" or "Timeout on validation of query". query_everything() aliases one pullRequests lookup per head ref into a single query, and create/update_pull_requests alias one mutation per PR, so a large enough topic stack makes uploads fail entirely. Mutations hit the limit even sooner because each PR body and review-graph comment lists the whole relative chain, making the payload grow quadratically with stack size. Batch the head ref lookups 10 per query and the PR mutations 5 per request, merging results in order. Stacks below the batch sizes send byte-identical requests to before. Verified against a 41-topic stack in a large monorepo where uploads previously failed: the single query reproducibly errors while the batched version succeeds, including a 2173-ref stress run.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With a large enough topic stack,
revup uploadfails outright with GitHub graphql errors — intermittentlyResource limits for this query exceededorTimeout on validation of query.Two call sites build a single request that grows linearly (or worse) with stack size:
Github.query_everything()aliases onepullRequests(headRefName:)lookup per head ref into one query. Since every topic in a relative chain also adds its relative branch to the lookup list, a 41-topic stack produces ~80+ aliases, each nesting reviews, assignees, labels, timeline items, and comments.create_pull_requests()/update_pull_requests()alias one mutation per PR into one request. These hit the limit even sooner: each PR's body and review-graph comment lists the whole relative chain, so the payload grows quadratically with stack size.Fix
Batch the requests instead of aliasing everything into one:
query_everything()looks up head refs 10 per query (MAX_HEAD_REFS_PER_QUERY); users/labels/teams ride along with the first batch. Results are merged in order, so callers are unaffected.create_pull_requests()/update_pull_requests()send mutations 5 PRs per request (MAX_PRS_PER_MUTATION), smaller because of the body/comment payloads.The public methods keep their signatures (the original bodies became
_implmethods), and stacks below the batch sizes send byte-identical requests to before, so there is no behavior change for small stacks. Batches are sequential; they could be issued concurrently withasyncio.gatherif you prefer — happy to change it.Verification
query_everything()read-only against 2,173 head refs (218 batches) without errors, and smoke-tested this branch's code against the real API.make lint(mypy, ruff, pylint) andmake test(201 passed) are clean.