N+1 Gitaly calls for CommitIsAncestor in environments
- ZD: https://gitlab.zendesk.com/agent/tickets/123786
- GitLab v11.11.3
From gitaly#1609 (comment 182194790):
Basically for one of most active and oldest projects every MR , even with a single empty commit on top of a latest branch, there are 162
commit_is_ancestor
gitally calls with following...
tracebacks:[ "lib/gitlab/gitaly_client/commit_service.rb:32:in `ancestor?'", "lib/gitlab/git/repository.rb:418:in `ancestor?'", "lib/gitlab/git/rugged_impl/repository.rb:52:in `ancestor?'", "app/models/repository.rb:938:in `ancestor?'", "app/models/deployment.rb:123:in `includes_commit?'", "app/models/environment.rb:108:in `includes_commit?'", "app/finders/environments_finder.rb:36:in `block in execute'", "app/finders/environments_finder.rb:35:in `select!'", "app/finders/environments_finder.rb:35:in `execute'", "app/models/merge_request.rb:1058:in `block in environments_for'", "app/models/merge_request.rb:1068:in `environments_for'", "app/controllers/projects/merge_requests/diffs_controller.rb:23:in `render_diffs'", "app/controllers/projects/merge_requests/diffs_controller.rb:13:in `show'", "lib/gitlab/session.rb:11:in `with_session'", "app/controllers/application_controller.rb:439:in `set_session_storage'", "app/controllers/application_controller.rb:435:in `set_locale'", "ee/lib/gitlab/jira/middleware.rb:17:in `call'" ]
Briefly checked the code. It look for all active envs, where current source branch HEAD is in history (that is the slow part) and then discards all but one.
def render_diffs @environment = @merge_request.environments_for(current_user).last ...
def environments_for(current_user) return [] unless diff_head_commit @environments ||= Hash.new do |h, current_user| envs = EnvironmentsFinder.new(target_project, current_user, ref: target_branch, commit: diff_head_commit, with_tags: true).execute
And worst of all, it seems all that just to show "View on $URL" on some files in diff (I didn't find where it is on a page, just saw in the code). Should it really look up envs using full git history? Maybe comparing commit of a branch would be enough, it should show URL only for envs which deployed this very commit, otherwise "View on $URL" can be quite misleading