Limit each AI Catalog item to 1000 versions
What does this MR do and why?
This MR limits each AI Catalog item to 1000 versions.
Each save of an agent, flow or third-party flow with a changed definition makes a new version. A restore also makes a new version. A version can be 80 KB, and one GraphQL request can have about 100 aliased saves. Thus one user can add many records to the ai_catalog_item_versions table on the main database quickly.
When an item has 1000 versions, the item refuses a new version. Update and restore return this message as a normal mutation error:
This item has reached the maximum of 1000 versions. To make more changes, create a new item.
The limit is a validation on Ai::Catalog::Item. It runs only when an item that exists already gets a new version. Thus item create, and saves that do not make a version, do not run the check.
The limit also applies to skill publish, because a skill update adds a version to an existing item. At the limit, the publish records that skill as failed and continues with the other skills.
This MR is MR 1 for the issue. MR 2, !269755 (merged), adds a rate limit for third-party flow update and restore.
Why block instead of removing the oldest version
- GitLab.com has 33,791 versions in total. The largest item has 127. A limit of 1000 has no effect on real users.
- If GitLab removes the oldest version, data goes away and the user gets no warning. A project can also pin an old version. A clear error is safer.
- There is no feature flag. To roll back, revert this MR. This is the same as the other Project Bulkhead MRs of the AI Catalog team.
Known limit
Two saves at the same time with different version_bump levels can push an item to about 1002 versions. next_version_number takes no lock. The lock from https://gitlab.com/gitlab-org/gitlab/-/work_items/636701 closes this gap. This MR does not change it.
Database
The check runs one query on each save that makes a version. The query counts at most 1000 records and uses the index_ai_catalog_item_versions_on_ai_catalog_item_id_and_id index:
SELECT COUNT(*) FROM (
SELECT 1 AS one FROM "ai_catalog_item_versions"
WHERE "ai_catalog_item_versions"."ai_catalog_item_id" = 1
LIMIT 1000
) subquery_for_countQuery plan on production data: index-only scan on (ai_catalog_item_id, id), 6 shared buffer hits, 0.093 ms. The item with the most versions has 127.
References
- https://gitlab.com/gitlab-org/gitlab/-/work_items/635191
- https://gitlab.com/gitlab-org/gitlab/-/work_items/636701
Screenshots or screen recordings
Not applicable. This MR adds no UI. The error shows in the existing mutation error display.
How to set up and validate locally
-
Find a third-party flow that has a released latest version, and a user with the Maintainer role in its project.
-
In the Rails console, set the limit to the number of versions that the item has now:
item = Ai::Catalog::Item.find(<id>) Ai::Catalog::Item.const_set(:VERSIONS_LIMIT, item.versions.count) -
In the same console, change the definition:
definition = "injectGatewayToken: false\nimage: alpine:latest\ncommands:\n - echo hello\n" Ai::Catalog::ThirdPartyFlows::UpdateService.new( project: item.project, current_user: User.find(<user_id>), params: { item: item, definition: definition } ).execute.message -
Make sure that the message is the limit error, and that
item.versions.countdid not change.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Related to https://gitlab.com/gitlab-org/gitlab/-/work_items/635191