Skip to content

perf(keystone): list a project's roles in one request - #158

Open
trondham wants to merge 1 commit into
norcams:masterfrom
trondham:keystone-list-roles
Open

trondham wants to merge 1 commit into
norcams:masterfrom
trondham:keystone-list-roles

Conversation

@trondham

Copy link
Copy Markdown
Contributor

Keystone.list_roles() asked keystone for a project's role assignments and then
fetched the name of every group and every role by id, one request each. That is
two requests per project member on top of the listing: six members cost thirteen
requests, fifty would cost a hundred. The role lookup was the plainer waste,
since a cloud has a handful of distinct roles and the same ones were fetched once
per assignment.

include_names=True makes keystone fill the names in, so this is one request
whatever the size of the project. __get_role_assignments() in the same file
already asks that way, so nothing new is required of keystone, and
python-keystoneclient has carried the parameter since 3.7.0.

Measured on a six member project: thirteen requests and 161ms become one and
15ms.
Small in absolute terms, but it was 2 requests per member and is now
flat.

Also fixes a latent AttributeError

The old loop rebound role from the assignment to the role object and then read
role.group in the branch for a group it had not found:

for role in roles:
    group = self.get_by_id('group', role.group['id']) if hasattr(role, 'group') else None
    role = self.client.roles.get(role.role['id']) if hasattr(role, 'role') else None
    if hasattr(group, 'name'):
        ...
    else:
        self.logger.debug('=> could not find group %s' % (role.group['id']))

So both an assignment held by a user rather than a group, and a group missing
from keystone, ended in AttributeError: 'Assignment' object has no attribute 'group' instead of the debug line that was meant to be printed. Such an
assignment is now skipped and logged.

Compatibility

The return value is unchanged, a list of {'group': name, 'role': name}. All
seven callers use only those two keys or len(): user.py, project.py,
report.py, object.py (twice), report2.py and himlarcli/printer.py.

Testing

Checked against a fake keystone covering the assignment shapes that matter -
a group holding a role, a disabled group, a second role, an assignment on a user,
and one where keystone did not fill the names in - asserting the resulting rows,
that exactly one request is made, and that the per-id roles.get/groups.get
paths are never reached. The old loop was run against the user assignment
separately to confirm it did raise.

pylint -E reports the same three pre-existing findings as master, none in this
function.

Not yet exercised against a live cloud. Worth running a command that calls it
(project.py, object.py or user.py) against a project whose membership is
known before merging.

🤖 Generated with Claude Code

list_roles() asked keystone for the role assignments of a project and
then fetched the name of every group and every role by id, one request
each. That is two requests per project member, on top of the listing:
six members cost thirteen requests, and fifty would cost a hundred.
The role lookup was the plainer waste of the two, since a cloud has a
handful of distinct roles and it fetched the same ones once per
assignment.

include_names makes keystone fill the names in, so this is now one
request whatever the size of the project. The same file already asks
that way in __get_role_assignments(), so nothing new is required of
keystone. Measured on a six member project: thirteen requests and
161ms become one and 15ms.

What callers get back is unchanged - a list of {'group': name, 'role':
name} - and all of them use only those two keys.

It also no longer raises on a role assignment it cannot report. The old
loop rebound 'role' from the assignment to the role object and then
read 'role.group' in the branch for a group it had not found, so both
an assignment held by a user rather than a group, and a group missing
from keystone, ended in AttributeError instead of the debug line that
was meant to be printed. Such an assignment is now skipped and logged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.

1 participant