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

Generate GraphQL schema during startup #5164

Merged
merged 2 commits into from
Dec 13, 2024
Merged

Conversation

ogenstad
Copy link
Contributor

@ogenstad ogenstad commented Dec 9, 2024

This PR aims to fix a timing issue when regenerating the GraphQL schema after the branch schema has been updated. I don't completely know the exact conditions that triggers the issue.

It could be that two requests are coming in at the same time before the GraphQL schema has been generated for an updating branch and they step on each others toes, or we start to generate the schema before the SchemaBranch has been properly processed so that the registry for this branch appears to be empty.

After making these changes where we generate the GraphQL schema upon startup and also generate the new schema within the task of refreshing all the branches I haven't been able to reproduce the error any more. On the stable branch it was quite easy to do it before with the procedure described below.

Unfortunately in order to be able to regenerate the GraphQL schema within the registry refresh function I had to introduce a circular import. I'd like to remove that part and change the way the tasks for the scheduler are managed. However, I don't want to do that within a hotfix release. Instead I want to revert back to that within the develop branch.

@github-actions github-actions bot added the group/backend Issue related to the backend (API Server, Git Agent) label Dec 9, 2024
Copy link

codspeed-hq bot commented Dec 9, 2024

CodSpeed Performance Report

Merging #5164 will not alter performance

Comparing pog-generate-graphql-schema (0d43df1) with stable (0ced4f7)

Summary

✅ 10 untouched benchmarks

@ogenstad ogenstad force-pushed the pog-generate-graphql-schema branch from 38e28b0 to d4f22cf Compare December 10, 2024 16:00
@ogenstad
Copy link
Contributor Author

ogenstad commented Dec 11, 2024

Against stable I can get the problem to appear fairly easily by having a script that looks like this:

import asyncio

from infrahub_sdk import InfrahubClient


async def fetch_tags(client: InfrahubClient, iteration: int):
    """Fetch tags and print the result."""
    tags = await client.all(kind="BuiltinTag")
    print(f"iteration: {iteration} - tags: {tags}")


async def main():
    client = InfrahubClient()
    iteration = 0
    while True:
        tasks = [fetch_tags(client, iteration) for _ in range(5)]
        await asyncio.gather(*tasks)
        # await fetch_tags(client=client, iteration=iteration)
        await asyncio.sleep(0.2)
        iteration += 1


if __name__ == "__main__":
    asyncio.run(main())

Then on a fresh installation without having started touched anything else I run this from the command line:

infrahubctl schema load models/base && python ../run.py

Using this script I've not been able to reproduce the issue against any of the commits in this PR. While running the test developed during the offsite I've not personally been able to reproduce the issue with either of the commits but @wvandeun has seen a few errors with the first commit. I think the best option would be to combine them however the second commit introduces a circular import that looks quite ugly now. That could perhaps work as a hotfix solution but for a long term option we could look at implementing the same type of import logic as we have with the prefect workflow to get around these type of circular imports.

The potential error would be something like this:

[{'message': "Unable to find the schema 'InfraDevice' in the registry", 'extensions': {'code': 422}}]

@ogenstad ogenstad force-pushed the pog-generate-graphql-schema branch from d4f22cf to d3273b0 Compare December 13, 2024 07:47
@ogenstad ogenstad force-pushed the pog-generate-graphql-schema branch from d3273b0 to 0d43df1 Compare December 13, 2024 08:26
@ogenstad ogenstad marked this pull request as ready for review December 13, 2024 09:09
@ogenstad ogenstad requested a review from a team December 13, 2024 09:09
@ogenstad ogenstad merged commit 0f28748 into stable Dec 13, 2024
31 checks passed
@ogenstad ogenstad deleted the pog-generate-graphql-schema branch December 13, 2024 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
group/backend Issue related to the backend (API Server, Git Agent)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants