weizhouapache commented on issue #14957:
URL: https://github.com/apache/cloudstack/issues/14957#issuecomment-7350568835
### Root cause
`RegisterUserKeysCmd.getEntityOwnerId()` calls
`QueryService.searchForAccessibleUsers()` just to check whether the target user
is accessible to the caller:
```java
List<Long> accessibleUsers = _queryService.searchForAccessibleUsers();
if (user != null && accessibleUsers.stream().anyMatch(u -> u ==
user.getId())) { ... }
```
`searchForAccessibleUsers()` loads **every** visible user (no pagination)
and, for each one, does two DB lookups (`findByIdIncludingRemoved` on account
and user) plus `checkCallerRoleTypeAllowedForUserOrAccountOperations`. The cost
is therefore linear in the number of users. `GetUserKeysCmd.getEntityOwnerId()`
only does a single `findById`, which is why `getUserKeys` returns in seconds.
The management server log of a slow request (root admin caller, ~75k key
pairs, user id > 100k) shows two ~105 s gaps, each following a domain-access
check, i.e. `getEntityOwnerId()` is evaluated twice per request. Creating the
key pair itself takes < 0.1 s.
### Reproduction on a test environment
Time of `registerUserKeys` (root admin caller) as the number of active users
grows:
| active users | time |
|---|---|
| 5 | 1.65 s |
| 1005 | 2.90 s |
| 1505 | 4.11 s |
| 2005 | 4.48 s |
| 2505 | 5.51 s |
| 3005 | 5.46 s |
This is roughly 1.3 ms per user, which extrapolates to about 100 s per call
at ~75k users.
For a non-root caller there is an additional cost: `checkRoleEscalation`
(called through `verifyCallerPrivilegeForUserOrAccountOperations`) iterates
over all APIs and took ~5 s in the test environment. This one depends on role
permissions, not on the number of users.
### Proposed fix
Check only the target user in `getEntityOwnerId()` (e.g.
`validateCallingUserHasAccessToDesiredUser(userId)`) instead of listing all
accessible users. The change is small, but it has not been validated against a
large data set yet, and I want to avoid running `checkRoleEscalation` twice for
non-root callers.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]