Draft: Write the mergeability merge status with a guarded UPDATE

MergeabilityCheckService writes the merge status inside a transaction that locks the MR row to confirm the inputs haven't moved. That holds a Sidekiq PgBouncer connection idle across several round trips, and it's about 25% of MergeRequestMergeabilityCheckWorker's idle-in-transaction time on the main primary. This replaces it with one UPDATE ... WHERE id AND target_branch AND latest_merge_request_diff_id, which gives the same stale-write guarantee with no transaction. It's behind mergeability_check_guarded_merge_status_write.

Resolves #637920. Part of &24375.

Detailed context for AI agents

Why

  • The main non-urgent PgBouncer pool gitlabhq_production_sidekiq (69 server connections) saturates every weekday from about 12:00 to 19:00 UTC.
  • MergeRequestMergeabilityCheckWorker is the largest holder: about 7.8 non-idle backends on the primary, of which 5.0 are idle in transaction.
  • Each in-transaction round trip costs about 5 ms on gprd, because of PgBouncer hops and GVL wait on catchall.
  • Today's write transaction on GDK, warm: SELECT ... FOR UPDATE pick of target_branch and latest_merge_request_diff_id, the assignee-count validation SELECT COUNT(*), the UPDATE, then COMMIT.
  • The new path is one autocommit UPDATE "merge_requests" SET "merge_status" = ? WHERE "id" = ? AND "target_branch" = ? AND "latest_merge_request_diff_id" = ?. It uses the primary key, so there is no plan risk.

How

  • MergeRequest#write_merge_status_if_inputs_current(inputs, mergeable:):
    1. Gets the state-machine transition (mark_as_mergeable_transition / mark_as_unmergeable_transition).
    2. Runs the guarded update_all. If it updates 0 rows, it returns false and the service reports STALE_MERGE_STATUS_DISCARDED, as before.
    3. Runs transition.perform(false), so the real transition callbacks run without a second save. They queue their run_after_commit blocks: the GraphQL mergeRequestMergeStatusUpdated trigger, the AutoMergeProcessWorker enqueue, and the conflict event, notification and todo.
    4. Clears the dirty flag, calls save_merge_data_changes, which writes only when merge_requests_merge_data_dual_write is on, the same as the after_save.
    5. Runs the commit-level work through current_transaction.after_commit. That waits for an outer transaction if the caller has one, and is skipped on rollback.
  • run_merge_status_write_callbacks holds the commit-level work: clear_memoized_shas, the transition _run_after_commit_queue, and expire_etag_cache. EE adds maintain_elasticsearch_update(updated_attributes: [:merge_status]) when the MR is maintained in Elasticsearch, because merge_status is indexed.
  • MergeabilityCheckService#write_merge_status now takes mergeable: instead of a block, and picks the path by flag (actor: project). The old locked path is unchanged, apart from that signature.

Deliberate behaviour differences on the new path

  • No validations run. Only merge_status changes, and batch_mark_as_checking / batch_mark_as_unchecked already bypass them.
  • ensure_metrics! doesn't run. It only bumps merge_request_metrics.updated_at; the row is created with the MR, and the batch methods skip it too.
  • enqueue_keep_around_commit doesn't run. It's a no-op without merge_commit_sha, which unmerged MRs don't have.
  • There is no merge_status IN (check states) guard, unlike the issue's first sketch. The existing spec "when a merge status was already written for the same source and target" documents why: a first-writer-wins guard would turn a self-correcting race into a permanent wrong answer. The UPDATE matches on the inputs only, exactly as the locked check did.

Verification

Run in an rspec slot on the same base:

  • spec/models/merge_request_spec.rb #write_merge_status_if_inputs_current: 13 examples. They cover one UPDATE and no FOR UPDATE, updated_at untouched, the transition callbacks, the ETag, merge_data sync, auto-merge, the conflict notification, discard on a moved target or diff, the not-allowed transition, and deferral and rollback inside a transaction.
  • ee/spec/models/merge_request_spec.rb: the Elasticsearch hook, 2 examples.
  • spec/services/merge_requests/mergeability_check_service_spec.rb: 67 examples. The existing examples now run the new path; a flag-off context covers the locked path.
  • spec/workers/merge_request_mergeability_check_worker_spec.rb and spec/services/merge_requests/mergeability_check_batch_service_spec.rb: 11 examples.

Rollout

  • gitlab_com_derisk, default off, no changelog. The rollout issue isn't created yet.
  • What to watch: the Postgres-side sampler for this endpoint (pg_stat_activity_marginalia_sampler_active_count{endpoint="MergeRequestMergeabilityCheckWorker"}, idle-in-transaction state), plus the dashboard and queries in the epic's internal note.
Edited by Marc Shaw

Merge request reports

Loading
Loading