Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RHCLOUD-36102] - Slow loading of data from roles endpoint perfo… #1354

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

EvanCasey13
Copy link
Contributor

@EvanCasey13 EvanCasey13 commented Nov 29, 2024

…rmance fixes

Link(s) to Jira

https://issues.redhat.com/browse/RHCLOUD-36102

Description of Intent of Change(s)

I used django silk to see how many queries were being executed for each request to the roles endpoint, initially this was 33 queries. This was happening as the role list functionality for the endpoint was using generic listing functionality provided by ListModelMixin from django.

I overwrite this listing functionality and implemented filtering, pagination etc again for the roles endpoint which reduced this from 33 to 3 queries overall.

Local Testing

How can the feature be exercised?
How can the bug be exploited and fix confirmed?
Is any special local setup required?

Checklist

  • if API spec changes are required, is the spec updated?
  • are there any pre/post merge actions required? if so, document here.
  • are theses changes covered by unit tests?
  • if warranted, are documentation changes accounted for?
  • does this require migration changes?
    • if yes, are they backwards compatible?
  • is there known, direct impact to dependent teams/components?
    • if yes, how will this be handled?

Secure Coding Practices Checklist Link

Secure Coding Practices Checklist

  • Input Validation
  • Output Encoding
  • Authentication and Password Management
  • Session Management
  • Access Control
  • Cryptographic Practices
  • Error Handling and Logging
  • Data Protection
  • Communication Security
  • System Configuration
  • Database Security
  • File Management
  • Memory Management
  • General Coding Practices

@EvanCasey13 EvanCasey13 changed the title [WIP][RHCLOUD-36102] - Slow loading of data from roles endpoint perfo… [RHCLOUD-36102] - Slow loading of data from roles endpoint perfo… Dec 9, 2024
Copy link
Contributor

@astrozzc astrozzc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the extra query you got from django silk, this seems to be n+1 query issue.
I think you can improve the performance by using prefetch related in the get_role_queryset method. We just need to include the permission lookup and external relation lookup there as well.

Comment on lines 103 to 104
groups_in_count = serializers.SerializerMethodField()
groups_in = serializers.SerializerMethodField()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task is to solve the slowness of the endpoint, but not requiring these two new fields to return. Are they required to be added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking more into it I can probably remove these from the serializer and add them dynamically from policies based on the query param passed

@@ -324,7 +328,200 @@ def list(self, request, *args, **kwargs):
}

"""
return super().list(request=request, args=args, kwargs=kwargs)
public_tenant = Tenant.objects.get(tenant_name="public")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might have to coordinate with this: #1368 (comment)

@@ -1210,7 +1209,7 @@ def test_list_role_with_additional_fields_principal_success(self):
self.assertEqual(len(response.data.get("data")), 5)

role = response.data.get("data")[0]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed😉

@@ -1118,6 +1116,7 @@ def test_list_role_with_groups_in_fields_for_admin_scope_success(self):

response_data = response.data.get("data")
for iterRole in response_data:
print(iterRole.get("groups_in"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is for debugging and can be removed

)

# Dynamic annotations
if "groups_in_count" in add_fields_split:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the groups_in_count, groups_in, or without are not mutual exclusive, so no if else needed here.

And obtain_groups_in might still needed here? There are some logic about scope and admin has to be included there

@EvanCasey13 EvanCasey13 marked this pull request as draft December 12, 2024 16:32
@EvanCasey13 EvanCasey13 marked this pull request as ready for review December 19, 2024 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants