Loading
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. MergeRequestMergeabilityCheckWorkeris 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 UPDATEpick oftarget_branchandlatest_merge_request_diff_id, the assignee-count validationSELECT COUNT(*), theUPDATE, thenCOMMIT. - 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:):- Gets the state-machine transition (
mark_as_mergeable_transition/mark_as_unmergeable_transition). - Runs the guarded
update_all. If it updates 0 rows, it returns false and the service reportsSTALE_MERGE_STATUS_DISCARDED, as before. - Runs
transition.perform(false), so the real transition callbacks run without a second save. They queue theirrun_after_commitblocks: the GraphQLmergeRequestMergeStatusUpdatedtrigger, theAutoMergeProcessWorkerenqueue, and the conflict event, notification and todo. - Clears the dirty flag, calls
save_merge_data_changes, which writes only whenmerge_requests_merge_data_dual_writeis on, the same as theafter_save. - 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.
- Gets the state-machine transition (
run_merge_status_write_callbacksholds the commit-level work:clear_memoized_shas, the transition_run_after_commit_queue, andexpire_etag_cache. EE addsmaintain_elasticsearch_update(updated_attributes: [:merge_status])when the MR is maintained in Elasticsearch, becausemerge_statusis indexed.MergeabilityCheckService#write_merge_statusnow takesmergeable: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_statuschanges, andbatch_mark_as_checking/batch_mark_as_uncheckedalready bypass them. ensure_metrics!doesn't run. It only bumpsmerge_request_metrics.updated_at; the row is created with the MR, and the batch methods skip it too.enqueue_keep_around_commitdoesn't run. It's a no-op withoutmerge_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 noFOR UPDATE,updated_atuntouched, the transition callbacks, the ETag,merge_datasync, 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.rbandspec/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.
Related
- Same epic: #637921 (closed) (skip the merge-head diff rebuild, the larger share), #637922, #637923.
merge_status_inputs_current?came from 44d6f716 (discard stale merge statuses).
Edited by Marc Shaw