Skip to content

Resolve "Improve performance of `/namespaces/:namespace/exists` API endpoint"

What does this MR do and why?

This is a fix to improve the performance of /namespaces/:namespace/exists API endpoint, related to #361821 (closed)

Current scenario

Imagine having such a group hierarchy:

Screen_Shot_2022-05-23_at_12.20.53_PM

ie, there is a top-level group named hello-gitlab-com, and in that I have a group named test with path hello-gitlab-com/test

And I am trying to create another group named test within hello-gitlab-com again, and this is the API request being made in the groups/new when I type in the group name:

Screen_Shot_2022-05-23_at_12.37.15_PM

The response of this API call has a suggestion for the group path, which is surprisingly test62:

Screen_Shot_2022-05-23_at_12.21.13_PM

test62 is a surprising suggestion, because:

hello-gitlab-com currently only has one group inside it with path test, so why the suffix 62? and why not just suggest test1?

Problem:

It appears that the code produces test62 as the suggestion because groups with path test1 thru test61 exists somewhere in the database, even though they exist in different hierarchies, in totally different groups.

ie, the search is at a global level, even though we want unique paths only at the group level, ie, unique paths within hello-gitlab-com group.

And due to the nature of the uniquify search we have currently, to produce test62 as the suggestion, it has to run 62 different SQL queries, of

Namespace.find_by_path_or_name(s)

where s differs each time (test1 to test61)

This is not performant. For eg, on gitlab.com, if we try to create a group with the path gitlab under one of my groups, it suggests the path of gitlab537, which is running 536 SQL queries!

Screen_Shot_2022-05-23_at_12.33.28_PM

Fix

In this MR, we are fixing this by scoping the area of the search to within the parent group under which the new group needs to be created. With proper scoping like this, the SQL queries being run to suggest a new path reduces considerably, thus speeding up the response from this endpoint.

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

Numbered steps to set up and validate the change are strongly suggested.

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 #361821 (closed)

Edited by Manoj M J

Merge request reports