Skip to content

Resolve "Deployments->Environments displays wrong commit refs/tags"

What does this MR do and why?

This MR updates environment.last_deployment to return the deployment that was finished last, instead of the deployment with the highest id.

Customer Impact

After investigation on a read-only production replica of gitlab.com with the following script

Environment.all.each_batch(of: 1000) do |batch|
    batch.each do |e|
        if e.deployments.success.count && e.deployments.success.ordered.first&.id != e.deployments.success.distinct_on_environment.first&.id
            success_changed_envs.push(e.id)
        end
    end
end
puts "Success Changes #{success_changed_envs.count}"

I found 6 last_deployment that would be different.

  • 2 had nil for finished_at
  • The other 4 had finished_at time differences of 0.243551, 35.376646, 39.283417 and 710.027255 seconds.

This seems like this change has fairly low customer impact and in each case, changes last_deployment to the actually last deployment instead of just the latest id

Performance Impact

I found the environment with the highest deployment count on gitlab.com (11115 deployments) then compared the old vs new SQL statement performance on postgres.ai

Old (success.distinct_on_environment): https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/9464/commands/33572

New (success.ordered): https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/9464/commands/33573

The new method is a slight performance improvement (~15.7ms to ~4.7ms)

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

From the Rails Console

  1. Feature.enable(:env_last_deployment_by_finished_at)
  2. e = Environment.last # or pick a random environment with deployments
  3. e.deployments.success.last.dup.update(finished_at: 1.year.ago)
  4. e.reload.last_deployment - should not be the newly created deployment in step 3

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #341840 (closed)

Edited by Allen Cook

Merge request reports