Skip to content

Refactor notification recipients builder for watchers

What does this MR do?

This MR improves performance issues discussed in #326045 (closed). It uses the same logic that was used in !57688 (merged) as they do more or less the same thing.

Note: One notable difference from the existing behaviour is that it doesn't remove group notification settings where the project also has project notification settings. However, we seem to be loading the correct notification setting when we actually sending the notification so I didn't account for that here to keep things simple as the performance impact of that seems low. See here for the related code.

More relavant specs around this feature can be found in notification_service_spec.

Database Queries

We currently handle this with multiple queries and join them outside of database. See the existing approach. We're replacing them with one query to avoid returning many rows only to feed back into the query to obtain notification settings we are after.

Project Watchers

explain SELECT
  "users".*
FROM
  "users"
WHERE
  "users"."id" IN (
    SELECT
      "notification_settings"."user_id"
    FROM
      (
        (
          SELECT
            "notification_settings"."user_id"
          FROM
            "notification_settings"
          WHERE
            "notification_settings"."source_id" = 278964
            AND "notification_settings"."source_type" = 'Project'
            AND (
              (
                "notification_settings"."level" = 3
                AND EXISTS (
                  SELECT
                    true
                  FROM
                    "notification_settings" "notification_settings_2"
                  WHERE
                    "notification_settings_2"."user_id" = "notification_settings"."user_id"
                    AND "notification_settings_2"."source_id" IS NULL
                    AND "notification_settings_2"."source_type" IS NULL
                    AND "notification_settings_2"."level" = 2
                )
              )
              OR "notification_settings"."level" = 2
            )
        )
        UNION
          (
            SELECT
              "notification_settings"."user_id"
            FROM
              "notification_settings"
            WHERE
              "notification_settings"."source_type" = 'Namespace'
              AND "notification_settings"."source_id" IN (9970)
              AND (
                (
                  "notification_settings"."level" = 3
                  AND EXISTS (
                    SELECT
                      true
                    FROM
                      "notification_settings" "notification_settings_2"
                    WHERE
                      "notification_settings_2"."user_id" = "notification_settings"."user_id"
                      AND "notification_settings_2"."source_id" IS NULL
                      AND "notification_settings_2"."source_type" IS NULL
                      AND "notification_settings_2"."level" = 2
                  )
                )
                OR "notification_settings"."level" = 2
              )
          )
      ) notification_settings
  )

explain: ~66ms https://explain.depesz.com/s/6Z44

Group Watchers

explain SELECT
  "users".*
FROM
  "users"
WHERE
  "users"."id" IN (
    SELECT
      "notification_settings"."user_id"
    FROM
      "notification_settings"
    WHERE
      "notification_settings"."source_type" = 'Namespace'
      AND "notification_settings"."source_id" IN (9970)
      AND (
        (
          "notification_settings"."level" = 3
          AND EXISTS (
            SELECT
              true
            FROM
              "notification_settings" "notification_settings_2"
            WHERE
              "notification_settings_2"."user_id" = "notification_settings"."user_id"
              AND "notification_settings_2"."source_id" IS NULL
              AND "notification_settings_2"."source_type" IS NULL
              AND "notification_settings_2"."level" = 2
          )
        )
        OR "notification_settings"."level" = 2
      )
  )

explain: ~60ms https://explain.depesz.com/s/8DOE

Screenshots (strongly suggested)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team

Related to #326045 (closed)

Edited by Sincheol (David) Kim

Merge request reports