Directory sync removes group memberships one at a time in a single long transaction, delaying authentication requests

XMLWordPrintable

    • 1
    • Severity 2 - Major
    • 2
    • RtB - Service ops & Tech entropy

      Issue Summary

      When a full user directory synchronisation finds that many users are no longer members of a group, Embedded Crowd removes those memberships one by one, with all of the removals for that group inside a single database transaction. Each removal runs several Hibernate queries, and each query re-checks every object already loaded in the session, so the work grows quadratically with the number of removals. A removal of tens of thousands of memberships can keep the sync thread at 100% CPU for hours.

      While that transaction is open, its uncommitted deletes hold database locks on the membership table. Any request that needs to write to those tables waits for it. In practice that is authentication: SSO logins, HTTP basic authentication (which may update the user's memberships on login) and the tombstone cleanup job all block until the sync finishes.

      Crowd shipped a batched implementation of membership removal in Crowd 5.1.0. It deletes in small, separately committed batches and does not have the quadratic cost. It is gated behind a dark feature that Bitbucket leaves disabled, so Bitbucket always uses the slow path. Bitbucket's membership DAO and batch-processor wiring already support the batched path; it only needs to be enabled by default, either in Bitbucket or Embedded Crowd.

      I've set the affects version to 9.4.0 as this currently is the oldest supported version of Bitbucket. However the problem predates this version.

      How to recognise it

      Thread dumps show a Caesium-* scheduler thread RUNNABLE at high CPU in Hibernate flush code under the membership removal loop:

        at org.hibernate.event.internal.AbstractFlushingEventListener...
        at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush / onAutoPreFlush
        at org.hibernate.internal.SessionImpl.autoFlushIfRequired / autoPreFlush
        ...
        at com.atlassian.crowd.directory.AbstractInternalDirectory.removeUserFromGroup
        at com.atlassian.crowd.directory.DbCachingRemoteChangeOperations.removeUserMembershipsFromGroupIteratively
        at com.atlassian.crowd.directory.DbCachingRemoteChangeOperations.removeUserMembershipsForGroup
        at com.atlassian.crowd.directory.DirectoryCacheImplUsingChangeOperations.syncUserMembersForGroup
        at com.atlassian.crowd.directory.synchronisation.cache.AbstractCacheRefresher.synchroniseMemberships
        at com.atlassian.crowd.directory.DbCachingRemoteDirectory.synchroniseCache
      

      At the same time, HTTP threads sit in a JDBC socket read inside executeBatch at transaction commit, under com.atlassian.crowd.embedded.core.CrowdServiceImpl.userAuthenticated (SSO) or com.atlassian.stash.internal.crowd.RiotPolice.doAuthenticate (basic auth), and DefaultTombstoneCleanupService.cleanupExpiredTombstones is blocked on a DELETE.

      Steps to Reproduce

      1. Connect a user directory that uses full synchronisation and has a group with a very large number of members.
      2. Let it sync fully, then cause a large number of those memberships to disappear from the remote group.
      3. Wait for the next scheduled synchronisation.
      4. Attempt to log in while it runs.

      Expected Results

      Can log in throughout directory sync.

      Actual Results

      Logins hang until it completes or the node is restarted

      Workaround

      Add the following to JVM_SUPPORT_RECOMMENDED_ARGS in $BITBUCKET_INSTALL/bin/_start-webapp.sh:

       -Datlassian.darkfeature.crowd.sync.delete.user.memberships.batching.enabled=true
      

      This replaces the call to removeGroupMembershipsFromGroupIteratively() seen in the above stack trace with removeGroupMembershipsFromGroupInBulk()

      and if nested groups are enabled in the directory add this too:

      -Datlassian.darkfeature.crowd.sync.nested.groups.group.membership.changes.batching.enabled=true
      

              Assignee:
              Ben Humphreys
              Reporter:
              Ben Humphreys
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated: