Preload session associations on merge request, pipeline and link lists

What does this MR do and why?

Ai::DuoWorkflows::Workflow collections reached from a merge request, a pipeline, or a session link connection did not preload the workflow's own associations, so the per-session fields the session widgets select (user, project, and the agentName / aiCatalogItem / mcpEnabled fields that go through ai_catalog_item_version and namespace) queried once per row. Only WorkflowsResolver applied Workflow.with_preloaded_associations.

  • MergeRequest.duoWorkflows (ee/app/graphql/ee/types/merge_request_type.rb) now chains with_preloaded_associations, the scope WorkflowsResolver already uses.
  • Ci::Pipeline.duoWorkflows (PipelineWorkflowsResolver) already batched the workflows per pipeline and handed each node its pipeline's project; it now also preloads user, namespace and ai_catalog_item_version: :item for the whole batch.
  • WorkflowLinksBaseResolver (the duoWorkflowLinks connections on merge requests, pipelines, work items and notes) preloaded the link's two sides; the workflow side now also brings its own associations.

tests.yml also maps the three changed source files to the request specs that cover them (project/merge_request_spec.rb, project/pipelines/duo_workflows_spec.rb, ai/duo_workflows/*_links_spec.rb); the generic rule mapped them to specs named after the source files, which do not exist, so predictive pipelines would skip the covering specs.

Each entry point gets a request spec that selects the per-session fields and asserts the query count stays flat when more sessions are linked (skip_cached: false, because the repeated project load is identical SQL that the query cache would otherwise hide). On master the three new examples fail; the extra queries are one users and one projects load per additional session:

entry point control (1 session) 3 sessions, master 3 sessions, this branch
MergeRequest.duoWorkflows 48 52 does not exceed 48
Ci::Pipeline.duoWorkflows 45 47 does not exceed 45
MergeRequest.duoWorkflowLinks → workflow 47 53 does not exceed 47

Why it matters: the merge request and work item session widgets run these queries on every page view. Kibana, GraphqlController execute, last 24 h, json.db_main_replica_count p10 / p50 / p90:

operation requests / 24 h p10 p50 p90
getDuoAgentSessionsOnMergeRequest 2,307,670 19.5 22 23.7
getDuoAgentSessionsOnWorkItem 471,720 16.5 26.2 29.0
getDuoAgentSessionsOnPipeline 599,702 16.1 19 20

The work item path (WorkItems::Widgets::AiSession#duo_workflows) is deliberately left out of this MR: both files are being changed in !260805 (merged); it gets the same one-line scope once that lands.

Prior art: Workflow.with_preloaded_associations (the association list WorkflowsResolver uses) is reused for the merge request path; the pipeline resolver uses ActiveRecord::Associations::Preloader on the batch the way Types::Ci::JobType and GroupIssuesResolver do, because the workflows there are already loaded through the WorkflowPipeline join.

How to reproduce (on master)

bin/rspec ee/spec/requests/api/graphql/project/merge_request_spec.rb \
  ee/spec/requests/api/graphql/project/pipelines/duo_workflows_spec.rb \
  ee/spec/requests/api/graphql/ai/duo_workflows/merge_request_links_spec.rb \
  -e 'avoids N+1 queries as more sessions are linked' \
  -e 'avoids N+1 queries for per-session fields on one pipeline' \
  -e 'does not N+1 on the session user and project'

Expected with the spec changes of this MR applied on top of master: 3 examples, 3 failures, for example:

Expected a maximum of 48 queries, got 52:
SELECT "projects"."id", … FROM "projects" WHERE "projects"."id" = $1 LIMIT $2   -- (expected: 1, got: 3)
SELECT "users"."id", … FROM "users" WHERE "users"."id" = $1 LIMIT $2           -- (expected: 0, got: 1)
SELECT "users"."id", … FROM "users" WHERE "users"."id" = $1 LIMIT $2           -- (expected: 0, got: 1)

How to verify (on this branch)

# the three examples above
# 3 examples, 0 failures

bin/rspec ee/spec/requests/api/graphql/project/merge_request_spec.rb \
  ee/spec/requests/api/graphql/project/pipelines/duo_workflows_spec.rb \
  ee/spec/requests/api/graphql/ai/duo_workflows/merge_request_links_spec.rb \
  ee/spec/requests/api/graphql/ai/duo_workflows/pipeline_links_spec.rb \
  ee/spec/requests/api/graphql/ai/duo_workflows/work_item_links_spec.rb \
  ee/spec/requests/api/graphql/ai/duo_workflows/note_links_spec.rb \
  ee/spec/graphql/resolvers/ai/duo_workflows/pipeline_workflows_resolver_spec.rb \
  ee/spec/graphql/ee/types/merge_request_type_spec.rb
# 173 examples, 0 failures, 5 pending (the pending ones are the pre-existing granular-token examples)

bundle exec rubocop <changed files>
# no offenses detected

After deploy: the p10 → p90 spread of the three operations above should narrow on https://log.gprd.gitlab.net (GraphqlController execute, json.graphql.operation_name.keyword).

References

Related to #628078 (closed) Related to gitlab-org#23509

Edited by Tian Gao

Merge request reports

Loading
Loading