Add the workforce unit read API behind a flag

What does this MR do and why?

Adds the read API for workforce units, so the DAP Impact dashboard can list the units a user can select.

Stacked on !270006 (merged), which adds the unit tables and models. Retarget to master after it merges. Until then the diff shows only this change.

  • Feature flag workforce_unit_filter (type wip, actors: user and tenant root group).
  • Ability read_workforce_hierarchy on the tenant root group. Maintainer+ have it. It needs the workforce_hierarchies licensed feature (Ultimate) and the workforce_hierarchies SaaS feature (GitLab.com only in V1), and it is prevented when the flag is off for both the user and the root group.
  • Workforce::HierarchiesFinder: hierarchies of the current organization that the user can read. Candidates are the tenant root groups the user reaches by membership or group share, or all root groups for admins in admin mode. The ability decides, so listing and fetching follow the same rule.
  • Query.workforceHierarchies, a connection, with WorkforceHierarchy (id, group, units(type:)) and WorkforceUnit (id, name, type, parentId). All fields are marked experiment:. units(type:) is a flat keyset connection ordered by ID, up to 1,000 units per page. type is required, so every page uses the (workforce_hierarchy_id, unit_type, id) index. The query count does not grow with the number of hierarchies: the finder preloads group policies, subscriptions and routes, and units for every hierarchy on the page loads in one JOIN LATERAL query (Gitlab::Graphql::Loaders::Workforce::UnitsLoader, a LazyRelationLoader), with the page limit applied per hierarchy. parentId is the second-to-last entry of traversal_ids.
  • No members, external identifiers, HR person names or managers are exposed.

Resolves https://gitlab.com/gitlab-org/gitlab/-/work_items/639642. Part of https://gitlab.com/groups/gitlab-org/-/work_items/24536.

Database

No migrations. The new queries run on the tables from !270006 (merged), which are not in production yet, so Database Lab cannot plan them. These plans are local EXPLAIN (ANALYZE, BUFFERS), warm cache. Synthetic data: four tenants with 10,000, 10,000, 1,000,000 and 10,000 units (the last one inserted after the large one, so its IDs sit above 1,020,000).

Query Time Plan
units connection, first page of all four tenants in one query (4,004 rows) 0.98 ms, 114 buffers https://explain.depesz.com/s/3BMl
units connection, later page of all four tenants (after ID 520,000) 1.0 ms, 67 buffers https://explain.depesz.com/s/JZsR
Workforce::HierarchiesFinder candidates, admin in admin mode or organization owner (synthetic: 2M top-level groups, 7 hierarchies) 0.08 ms https://explain.depesz.com/s/oKgs
Workforce::HierarchiesFinder candidates, any other user, 201 memberships, root groups with few members 4.8 ms cold, 69 buffers https://explain.depesz.com/s/3Z16
Same, user with 84,002 memberships at every access level 0.70 ms, 64 buffers https://explain.depesz.com/s/Tb9q
Same, 201 memberships, one hierarchy's root group with 150,000 members 0.66 ms, 63 buffers https://explain.depesz.com/s/7Ax3
Same, 84,002 memberships, one root group with 150,000 members 0.24 ms, 62 buffers https://explain.depesz.com/s/GKaL

The admin query joins workforce_hierarchies to namespaces (Workforce::Hierarchy.for_top_level_groups_in), so it starts from the few hierarchy rows and never scans the group table. Admins in admin mode and organization owners get it unfiltered, since both are Owner of every group in the organization (Group#max_member_access_for_user).

Any other user gets the same join, kept to hierarchies whose root group they are connected to by any membership (user.group_members: no access requests, Guest and up) or by a group share (Workforce::Hierarchy.with_membership_for). The role is no longer checked in SQL: the read_workforce_hierarchy ability decides afterwards, so listing and fetching by ID follow the same rule. Each hierarchy row gets one correlated EXISTS on members and one on group_group_links joined to members. With root groups of a few members the planner probes members by source_id (index_members_on_source_and_type_and_access_level); once a root group has 150,000 members it switches to idx_members_on_user_and_source_and_source_type_and_member_role (user_id, source_id), one index probe per hierarchy. Both stay under 70 buffers for a user with 84,002 memberships, so the cost follows the number of hierarchies, not the user's memberships.

Before this change the scope kept only Maintainer+ memberships and shares (with_maintainer_access_for, plan https://explain.depesz.com/s/Lp5o, 0.27 ms warm, 39 buffers, superseded). The earlier User#authorized_groups query (85 s cold for 84k memberships, https://explain.depesz.com/s/nRmG) and the group_id IN admin query (https://explain.depesz.com/s/VrOY) are superseded too. All finder plans are local EXPLAIN (ANALYZE, BUFFERS) on a synthetic database, because the tables are not in production; the 84,002-membership user and the 150,000-member root group were added for this change.

Both units plans use the full page size (LIMIT 1001 per hierarchy). The hierarchy ID reaches the lateral subquery as a parameter, so every tenant, the 1M-unit one included, gets an index scan on index_workforce_units_on_hierarchy_id_type_and_id with a per-hierarchy Limit. The earlier per-hierarchy queries, where the planner walked the primary key for the large tenant (513 buffer hits on its first page: https://explain.depesz.com/s/adAK, https://explain.depesz.com/s/TinH, https://explain.depesz.com/s/5w3O), are superseded. Each hierarchy's connection applies the cursor to the shared batch, so the cursor predicate repeats once per hierarchy on the page; it stays in the index condition and does not change the plan.

Raw SQL for the units queries

First page, all tenants on the page:

SELECT "workforce_units".* FROM (VALUES (4), (3), (2), (1)) AS workforce_hierarchies(id) JOIN LATERAL (SELECT "workforce_units".* FROM "workforce_units" WHERE "workforce_units"."unit_type" = 'supervisory' AND "workforce_units"."workforce_hierarchy_id" = "workforce_hierarchies"."id" ORDER BY "workforce_units"."id" ASC LIMIT 1001) AS workforce_units ON true;

Later page (after ID 520,000):

SELECT "workforce_units".* FROM (VALUES (4), (3), (2), (1)) AS workforce_hierarchies(id) JOIN LATERAL (SELECT "workforce_units".* FROM "workforce_units" WHERE "workforce_units"."unit_type" = 'supervisory' AND ("workforce_units"."id" > 520000) AND ("workforce_units"."id" > 520000) AND ("workforce_units"."id" > 520000) AND ("workforce_units"."id" > 520000) AND "workforce_units"."workforce_hierarchy_id" = "workforce_hierarchies"."id" ORDER BY "workforce_units"."id" ASC LIMIT 1001) AS workforce_units ON true;
Raw SQL for the finder queries

Admin in admin mode (organization_id 1):

SELECT "workforce_hierarchies".* FROM "workforce_hierarchies" INNER JOIN "namespaces" ON "namespaces"."id" = "workforce_hierarchies"."group_id" AND "namespaces"."type" = 'Group' WHERE "namespaces"."type" = 'Group' AND "namespaces"."organization_id" = 1 AND "namespaces"."parent_id" IS NULL;

Any other user (user id 987654), after:

SELECT "workforce_hierarchies".* FROM "workforce_hierarchies" INNER JOIN "namespaces" ON "namespaces"."id" = "workforce_hierarchies"."group_id" AND "namespaces"."type" = 'Group' WHERE "namespaces"."type" = 'Group' AND "namespaces"."organization_id" = 1 AND "namespaces"."parent_id" IS NULL AND (EXISTS (SELECT "members"."id", "members"."access_level", "members"."source_id", "members"."source_type", "members"."user_id", "members"."notification_level", "members"."type", "members"."created_at", "members"."updated_at", "members"."created_by_id", "members"."invite_email", "members"."invite_token", "members"."invite_accepted_at", "members"."requested_at", "members"."expires_at", "members"."ldap", "members"."override", "members"."state", "members"."invite_email_success", "members"."member_namespace_id", "members"."member_role_id", "members"."expiry_notified_at", "members"."request_accepted_at" FROM "members" WHERE "members"."source_type" = 'Namespace' AND "members"."type" = 'GroupMember' AND "members"."user_id" = 987654 AND "members"."requested_at" IS NULL AND (access_level >= 10) AND (members.source_id = workforce_hierarchies.group_id)) OR EXISTS (SELECT "group_group_links".* FROM "group_group_links" WHERE (group_group_links.shared_group_id = workforce_hierarchies.group_id) AND EXISTS (SELECT "members"."id", "members"."access_level", "members"."source_id", "members"."source_type", "members"."user_id", "members"."notification_level", "members"."type", "members"."created_at", "members"."updated_at", "members"."created_by_id", "members"."invite_email", "members"."invite_token", "members"."invite_accepted_at", "members"."requested_at", "members"."expires_at", "members"."ldap", "members"."override", "members"."state", "members"."invite_email_success", "members"."member_namespace_id", "members"."member_role_id", "members"."expiry_notified_at", "members"."request_accepted_at" FROM "members" WHERE "members"."source_type" = 'Namespace' AND "members"."type" = 'GroupMember' AND "members"."user_id" = 987654 AND "members"."requested_at" IS NULL AND (access_level >= 10) AND (members.source_id = group_group_links.shared_with_group_id))))

Before (with_maintainer_access_for, Maintainer+ only):

SELECT "workforce_hierarchies".* FROM "workforce_hierarchies" INNER JOIN "namespaces" ON "namespaces"."id" = "workforce_hierarchies"."group_id" AND "namespaces"."type" = 'Group' WHERE "namespaces"."type" = 'Group' AND "namespaces"."organization_id" = 1 AND "namespaces"."parent_id" IS NULL AND (EXISTS (SELECT "members"."id", "members"."access_level", "members"."source_id", "members"."source_type", "members"."user_id", "members"."notification_level", "members"."type", "members"."created_at", "members"."updated_at", "members"."created_by_id", "members"."invite_email", "members"."invite_token", "members"."invite_accepted_at", "members"."requested_at", "members"."expires_at", "members"."ldap", "members"."override", "members"."state", "members"."invite_email_success", "members"."member_namespace_id", "members"."member_role_id", "members"."expiry_notified_at", "members"."request_accepted_at" FROM "members" WHERE "members"."type" = 'GroupMember' AND "members"."source_type" = 'Namespace' AND "members"."requested_at" IS NULL AND "members"."user_id" = 987654 AND "members"."access_level" >= 40 AND (members.source_id = workforce_hierarchies.group_id)) OR EXISTS (SELECT "group_group_links".* FROM "group_group_links" WHERE "group_group_links"."group_access" IN (50, 40) AND (group_group_links.shared_group_id = workforce_hierarchies.group_id) AND EXISTS (SELECT "members"."id", "members"."access_level", "members"."source_id", "members"."source_type", "members"."user_id", "members"."notification_level", "members"."type", "members"."created_at", "members"."updated_at", "members"."created_by_id", "members"."invite_email", "members"."invite_token", "members"."invite_accepted_at", "members"."requested_at", "members"."expires_at", "members"."ldap", "members"."override", "members"."state", "members"."invite_email_success", "members"."member_namespace_id", "members"."member_role_id", "members"."expiry_notified_at", "members"."request_accepted_at" FROM "members" WHERE "members"."type" = 'GroupMember' AND "members"."source_type" = 'Namespace' AND "members"."requested_at" IS NULL AND "members"."user_id" = 987654 AND "members"."access_level" >= 40 AND (members.source_id = group_group_links.shared_with_group_id))))

How to set up and validate locally

  1. Seed units with the development fixture from the base MR, with a licence for the tenant group.

  2. Enable the flag: Feature.enable(:workforce_unit_filter).

  3. As a Maintainer of the tenant group, query:

    { workforceHierarchies { nodes { id group { fullPath } units { nodes { id name type parentId } } } } }

Specs, including granular personal access tokens: ee/spec/policies/workforce, ee/spec/finders/workforce, ee/spec/requests/api/graphql/workforce.

Screenshots or screen recordings

Not applicable: no UI change.

Edited by Chandra Saripaka

Merge request reports

Loading
Loading