Optimize foundational flows sync with batched bulk inserts
Summary
This MR optimizes the Ai::Catalog::Flows::CascadeSyncFoundationalFlowsWorker Sidekiq job performance by implementing batch processing for syncing foundational flows to projects in large group hierarchies.
When cascading foundational flow settings in large groups, the worker processes each project individually with separate database queries, causing timeouts before completing the sync operation. This prevents settings from being properly propagated to all subgroups and projects.
Introduces a new SyncBatchFoundationalFlowsService that processes projects in batches with optimized database operations:
- Adds
sync_projects_optimizedmethod behind Feature flag:optimized_foundational_flows_sync(disabled by default and group based) - Pre-fetches, Pre-loads as much as possible for performance and and to reduces N+1 queries by loading all required data upfront
- Uses bulk
insert_allfor members, item consumers, and flow triggers - Tracks existing records in memory to avoid duplicate inserts
Speedup: 20x faster
References
- Relates to Optimize `Catalog::Flows::CascadeSyncFoundation... (#587126)
- Rollout issue: #588664
Risks ⏰
insert_all for members and project_authorizations can be risky. I'm still verifying
How to test
- Enable all foundational flows and see service accounts, members and flows are in the project
- Go to an issue and try "issue to MR" and see if it works http://gdk.test:3000/gitlab-duo/test/-/issues/1
- Disable the flows and see they are gone. Also disable only a few and disable only a few and see what happens
Query plans
SELECTS from project/project_settings.
- https://postgres.ai/console/gitlab/gitlab-production-main/sessions/48066/commands/144743
- https://postgres.ai/console/gitlab/gitlab-production-main/sessions/48066/commands/144744
- https://postgres.ai/console/gitlab/gitlab-production-main/sessions/48066/commands/144745
- https://postgres.ai/console/gitlab/gitlab-production-main/sessions/48066/commands/144746
INSERTs into project_authorizations/members
- https://postgres.ai/console/gitlab/gitlab-production-main/sessions/48066/commands/144747
- https://postgres.ai/console/gitlab/gitlab-production-main/sessions/48066/commands/144748
Acceptance Criteria
[x] All