Skip to content

sdap: optimize sdap_initgr_common_store() (rfc2307)#8459

Closed
alexey-tikhonov wants to merge 2 commits intoSSSD:masterfrom
alexey-tikhonov:RFC2307-initgroups
Closed

sdap: optimize sdap_initgr_common_store() (rfc2307)#8459
alexey-tikhonov wants to merge 2 commits intoSSSD:masterfrom
alexey-tikhonov:RFC2307-initgroups

Conversation

@alexey-tikhonov
Copy link
Member

When an LDAP user is a member of many groups (rfc2307), handling of
BE_REQ_INITGROUPS takes an extremely long time because:

  1. sdap_add_incomplete_groups() iterates over all groups to create
    missing group entries
  2. sysdb_update_members() iterates over all groups again to add
    the user as a member

This patch eliminates the second iteration by adding user membership
during step 1. The functions sysdb_add_incomplete_group() and
sdap_add_incomplete_groups() now accept an optional user_member
parameter. When provided, the user is added as a member of each group
during group processing (both for newly created incomplete groups and
for already existing groups).

In sdap_initgr_common_store(), when the member type is SYSDB_MEMBER_USER,
the username is provided to sdap_add_incomplete_groups() and
sysdb_update_members() only handles group deletions.

`sdap_add_incomplete_groups()` had two separate steps: first it
iterated the group name list checking each against sysdb to build
a 'missing' list, then for each missing group it scanned the entire
'ldap_groups' array calling to find matching LDAP  attributes.
This resulted in O(N^2) behavior when all groups were missing (i.e.
empty cache).

Replace this with a single O(N) loop that iterates 'ldap_groups'
directly: check sysdb, and if missing create the incomplete entry
immediately.

The 'sysdb_groupnames' parameter is removed as it is not used
anymore.

This patch also has an interesting side effect: it also makes
`sysdb_update_members()` executed in the `sdap_initgr_common_store()`
after `sdap_add_incomplete_groups()` faster. Most probably this
is because previosuly O(N^2) allocations of `groupname` (by
`sdap_get_group_primary_name()`) trashed memory, purging ldb/tdb
data from the cache.

Implementation assisted-by: Claude Code (Opus 4.6)
When an LDAP user is a member of many groups (rfc2307), handling of
BE_REQ_INITGROUPS takes an extremely long time because:
1. `sdap_add_incomplete_groups()` iterates over all groups to create
 missing group entries
2. `sysdb_update_members()` iterates over all groups again to add
 the user as a member

This patch eliminates the second iteration by adding user membership
during step 1. The functions `sysdb_add_incomplete_group()` and
`sdap_add_incomplete_groups()` now accept an optional `user_member`
parameter. When provided, the user is added as a member of each group
during group processing (both for newly created incomplete groups and
for already existing groups).

In `sdap_initgr_common_store()`, when the member type is SYSDB_MEMBER_USER,
the username is provided to `sdap_add_incomplete_groups()` and
`sysdb_update_members()` only handles group deletions.

Implementation assisted-by: Claude Code (Opus 4.6)
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a significant performance optimization for group membership handling in RFC2307 environments. By combining the creation of incomplete groups and the addition of user memberships into a single pass within sdap_add_incomplete_groups, it effectively eliminates a redundant iteration. The code changes are well-structured and the logic is sound. I've identified one potential issue regarding error handling within a transaction that could lead to data inconsistency.

@alexey-tikhonov
Copy link
Member Author

Testing suggest this patch doesn't provide real perf gains.

@alexey-tikhonov alexey-tikhonov added the Performance Performance related patches label Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-to-sssd-2-9 Performance Performance related patches

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant