Add the Artifact Registry billing state driver suspension arm (AR#2019 stack: 2/4)

What does this MR do and why?

This MR adds the suspension arm of the Artifact Registry billing state driver: it suspends a provisioned namespace 14 days after its plan lapses, and unsuspends it when the plan includes the registry again. The driver follows S17 (billing state driver spec). This MR first held the whole change, and it is now part 2 of the 4-part stack below. The activation checks, the usage-limit arm and the workers moved to the other parts.

  • Suspension arm. ArtifactRegistry::BillingStateDriverService reads each namespace live through the registry client, finds the lapse date from the plan of the payer that the billing anchor names (GitLab.com) or from the instance license (Self-Managed and Dedicated), and calls suspend or unsuspend 14 days after the lapse. It has two modes: run for every mapping, and renewal for the mappings it is given. Nothing calls it until part 4.
  • Feature flag. The new wip flag artifact_registry_billing_state_driver, off by default and checked per organization, gates every suspension call.
  • Notice record. One migration adds three nullable columns to artifact_registry_namespace_mappings: suspension_date, lapse_notice_sent_at and suspension_notice_sent_at.
  • Lapse date lookup. SubscriptionHistory.latest_with_hosted_plan_name_uids reads the newest history row of a group on a registry plan, and a post-deploy migration adds the index it needs.
  • Resolver fixes from the part 1 review. UsageLimitResolver now parses the CustomersDot answer as JSON only, and it makes no retry after a timeout (commit 11e3ef35d7f9). These fix two non-blocking findings from the review of part 1.

Points for the reviewer:

  • Recorded departure from the S17 notice record. The driver writes only suspension_date, as the lapse date plus 14 days. S17 sets it to 14 days after the later of the lapse date and the day the lapse notice went out. The notices, that rule, and the writes to the two *_notice_sent_at columns are in the notices follow-up. S17 acceptance 19 to 28 and the notice clause of acceptance 18 are not part of this MR.
  • Part 3 extends this class. The usage-limit arm and its metrics are in part 3, which adds them to this class and its spec. This version of the spec rewords four lines that name the limit arm, and part 3 restores them.
Readings taken where the specs say nothing
  • For the driver, a free trial is not a plan that includes the registry, and ultimate_trial_paid_customer is one.
  • On GitLab.com, a group that left a registry plan takes its lapse date from the newest subscription history row on a registry plan: the earlier of that term end date and the day of the change.
  • On Self-Managed and Dedicated, a lower license that replaces a registry license lapses on the day it starts.
  • On Self-Managed and Dedicated, a lapsed license suspends only after its grace period ends.

The stack

  1. Activation checks and UsageLimitResolver.
  2. This MR: the billing state driver, suspension arm.
  3. Billing state driver, usage-limit arm.
  4. The scheduled workers and the renewal subscription.

The review threads opened on this MR before the split stay here. Threads on files that moved to another part show as outdated.

Database

The SQL below was captured from the code paths through ActiveSupport::Notifications, and all plans ran on a gitlab-production-main Database Lab clone. Production holds 13 rows in artifact_registry_namespace_mappings, and none of them belongs to an organization ID I could name. So the renewal and update plans ran against one seeded row on the clone, with the three new columns added there through ALTER TABLE. The clone was reset afterwards.

Migrations

  • 20261125134512 (regular) adds three nullable columns to artifact_registry_namespace_mappings, with no default and no index.
  • 20261126120517 (post-deploy) adds index_gitlab_subscription_histories_on_namespace_id_and_id on gitlab_subscription_histories (namespace_id, id) through add_concurrent_index, for query 3. The table is not in the LargeTables list of rubocop/rubocop-migrations.yml, and db/docs gives table_size: small, so the index is not built asynchronously. CREATE INDEX CONCURRENTLY on the clone took 7.3 s. The driver is off behind its flag, so the index can wait for the post-deploy step.
Output of db:migrate:up, db:migrate:down and db:migrate:up for 20261126120517
### up
main: == 20261126120517 AddIndexGitlabSubscriptionHistoriesOnNamespaceIdAndId: migrating
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- view_exists?(:postgres_partitions)
main:    -> 0.0601s
main: -- index_exists?(:gitlab_subscription_histories, [:namespace_id, :id], {name: "index_gitlab_subscription_histories_on_namespace_id_and_id", algorithm: :concurrently})
main:    -> 0.0109s
main: -- add_index(:gitlab_subscription_histories, [:namespace_id, :id], {name: "index_gitlab_subscription_histories_on_namespace_id_and_id", algorithm: :concurrently})
main:    -> 0.0040s
main: == 20261126120517 AddIndexGitlabSubscriptionHistoriesOnNamespaceIdAndId: migrated (0.1431s)
### down
main: == 20261126120517 AddIndexGitlabSubscriptionHistoriesOnNamespaceIdAndId: reverting
main: -- transaction_open?(nil)
main:    -> 0.0001s
main: -- view_exists?(:postgres_partitions)
main:    -> 0.0130s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- view_exists?(:postgres_partitions)
main:    -> 0.0020s
main: -- index_name_exists?(:gitlab_subscription_histories, "index_gitlab_subscription_histories_on_namespace_id_and_id")
main:    -> 0.0030s
main: -- remove_index(:gitlab_subscription_histories, {algorithm: :concurrently, name: "index_gitlab_subscription_histories_on_namespace_id_and_id"})
main:    -> 0.0029s
main: == 20261126120517 AddIndexGitlabSubscriptionHistoriesOnNamespaceIdAndId: reverted (0.1655s)
### up
main: == 20261126120517 AddIndexGitlabSubscriptionHistoriesOnNamespaceIdAndId: migrating
main: -- transaction_open?(nil)
main:    -> 0.0001s
main: -- view_exists?(:postgres_partitions)
main:    -> 0.0172s
main: -- index_exists?(:gitlab_subscription_histories, [:namespace_id, :id], {name: "index_gitlab_subscription_histories_on_namespace_id_and_id", algorithm: :concurrently})
main:    -> 0.0104s
main: -- add_index(:gitlab_subscription_histories, [:namespace_id, :id], {name: "index_gitlab_subscription_histories_on_namespace_id_and_id", algorithm: :concurrently})
main:    -> 0.0047s
main: == 20261126120517 AddIndexGitlabSubscriptionHistoriesOnNamespaceIdAndId: migrated (0.1161s)

The run used an empty local database with db/structure.sql loaded, so the timings show the migration path, not the production build time.

1. Mapping rows for a scheduled run

NamespaceMapping.each_with_organization, called once by every BillingStateRunWorker and UsageLimitLiftSweepWorker run (part 4).

SELECT "artifact_registry_namespace_mappings".* FROM "artifact_registry_namespace_mappings" ORDER BY "artifact_registry_namespace_mappings"."id" ASC LIMIT 1000

Later batches add WHERE "artifact_registry_namespace_mappings"."id" > <last_id>. Production has 13 rows, so no second batch exists to explain.

Plan (Seq Scan on artifact_registry_namespace_mappings, 13 rows, 0.6 ms cold, buffers: 3 hit / 1 read)

The organization preload runs once per batch. With more than one organization in the batch, the condition is IN (<organization_ids>).

SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = <organization_id>

Plan (Index Scan on organizations_pkey, 1 row, 1.7 ms cold, buffers: 3 hit / 2 read)

2. Mapping rows for a renewal

BillingRenewalWorker (part 4), once for each renewal event. The existence check runs first, then the same batched read as query 1, filtered on the organization.

SELECT 1 AS one FROM "artifact_registry_namespace_mappings" WHERE "artifact_registry_namespace_mappings"."organization_id" = <organization_id> LIMIT 1

Plan (Index Only Scan on index_artifact_registry_namespace_mappings_on_organization_id, 1 row, 0.04 ms, buffers: 6 hit / 0 read, seeded row)

SELECT "artifact_registry_namespace_mappings".* FROM "artifact_registry_namespace_mappings" WHERE "artifact_registry_namespace_mappings"."organization_id" = <organization_id> ORDER BY "artifact_registry_namespace_mappings"."id" ASC LIMIT 1000

Plan (Index Scan on index_artifact_registry_namespace_mappings_on_organization_id, 1 row, 0.04 ms, buffers: 8 hit / 0 read, seeded row)

3. Last subscription history row on a registry plan

SubscriptionHistory.latest_with_hosted_plan_name_uids, once per mapping in a run, and only when the paying group is on GitLab.com and its current plan does not include the registry. The plan UIDs are the constant set REGISTRY_PLANS.

SELECT "gitlab_subscription_histories".* FROM "gitlab_subscription_histories" WHERE "gitlab_subscription_histories"."namespace_id" = <namespace_id> AND "gitlab_subscription_histories"."hosted_plan_name_uid" IN (3, 4, 5, 6, 7, 10, 11) ORDER BY "gitlab_subscription_histories"."id" DESC LIMIT 1

With the index from 20261126120517: plan (Index Scan Backward on index_gitlab_subscription_histories_on_namespace_id_and_id, 1 row, 0.04 ms warm, buffers: 4 hit / 0 read). The plan ran right after the index build, so the index pages were in cache. An earlier build on a fresh clone gave 3 hit / 1 read in 0.18 ms.

Without the index: plan (Index Scan on i_gitlab_subscription_histories_on_namespace_change_type_plan, then a top-N sort, 924 rows read for 1 returned, 631 ms cold, buffers: 27 hit / 902 read). That index matches only on namespace_id, so Postgres reads every history row of the group and sorts them.

Same query for a second group with 709 history rows
  • Without the index, warm: 709 rows read, 1 ms, buffers: 708 hit. The first, cold run took 632 ms with 703 buffers read, and its plan link was not kept.
  • With the index, warm: Index Scan Backward, 1 row, 0.04 ms, buffers: 4 hit.

4. Suspension date write

BillingStateDriverService#sync_suspension_date, through update_column, when the driver sets or clears suspension_date on a mapping row. Clearing the date sends NULL in place of '<date>'.

UPDATE "artifact_registry_namespace_mappings" SET "suspension_date" = '<date>' WHERE "artifact_registry_namespace_mappings"."id" = <mapping_id>

Plan (Index Scan on artifact_registry_namespace_mappings_pkey, 1 row updated, 0.14 ms, buffers: 15 hit / 0 read, seeded row)

References

Related to the issue

Screenshots or screen recordings

n/a: this MR changes no frontend code.

How to set up and validate locally

This MR has no user-facing change: the driver runs only behind a flag that is off by default, and nothing calls it until part 4.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

  • Quality. All new and changed specs use spec_helper and need the test database, so CI is their first run. Locally, only RuboCop ran. Known gap: no spec asserts that a condition call records no audit event.
  • Database. The migration 20261125134512 adds three nullable columns with no default and no index, and has no data change. It is reversible through change. The post-deploy migration 20261126120517 adds an index on gitlab_subscription_histories (namespace_id, id) and is reversible through down. New queries: NamespaceMapping.each_with_organization (batched find_each with the organization preloaded) and SubscriptionHistory.latest_with_hosted_plan_name_uids (the newest history row of a group on the given plans). The driver writes suspension_date with one update_column on a mapping row when it sets or clears the date. The SQL, the query plans and the migration output are in the Database section.
  • Performance and reliability. A run makes one registry read per mapping row. A failed call is logged and counted, the run continues, and the next run retries it.
  • Security. The driver presents the registry service credential and no user token. Log lines name the organization and the namespace UUID, never a user.
  • Feature flags. artifact_registry_billing_state_driver is new, wip, and off by default.
Spec coverage: each S17 row of the suspension arm and the example that covers it

Source: S17 at AR 7e34ca25.

Row Source Example Runs State
AC1: due + active is suspended by UUID S17 Acceptance 1 D suspends an active namespace by its UUID; SM: D suspends the namespace by its UUID CI (spec_helper) unverified: runs in CI
AC2: not due + suspended is unsuspended S17 Acceptance 2 D unsuspends a suspended namespace by its UUID; SM: D unsuspends a suspended namespace CI (spec_helper) unverified: runs in CI
AC3: renewal unsuspends without waiting for a run S17 Acceptance 3 D the renewal trigger unsuspends the renewed namespace without waiting for the next run; WR runs the renewal step for the mapping..., reads the namespace from a RenewedCloudEvent the same way, subscribes to event (both events) CI (spec_helper) unverified: runs in CI
AC4: not due + active is left alone S17 Acceptance 4 D leaves an active namespace alone (both deployments) CI (spec_helper) unverified: runs in CI
AC5: ultimate_trial_paid_customer not due, back on Premium no call S17 Acceptance 5 D when the payer is on ultimate_trial_paid_customer (2 examples) CI (spec_helper) unverified: runs in CI
AC6: Premium to Free lapse from the history row, earlier of term end and change S17 Acceptance 6 D reading the lapse date from the subscription history (3 examples) CI (spec_helper) unverified: runs in CI
AC7: SM lower license replaces: lapse on its start day S17 Acceptance 7 D lapses on the day the new license starts... CI (spec_helper) unverified: runs in CI
AC8: lapse < 14 days keeps access, per deployment S17 Acceptance 8 D keeps full access for 14 days..., records the suspension date 14 days after... (.com); D makes no suspension call and records the suspension date 14 days after the expiry (SM) CI (spec_helper) unverified: runs in CI
AC9: due + suspended gets no second suspend S17 Acceptance 9 D makes no second suspend call for a suspended namespace CI (spec_helper) unverified: runs in CI
AC10: due + disabled/blocked/deleted/purged suspended anyway S17 Acceptance 10 D when another status masks the suspension (table, plus limit_blocked) CI (spec_helper) unverified: runs in CI
AC11: not due + disabled/blocked/deleted/purged unsuspended anyway S17 Acceptance 11 D when a higher-precedence status masks the suspension (table) CI (spec_helper) unverified: runs in CI
AC12: unreadable plan, no license, unusable anchor S17 Acceptance 12 D when the plan cannot be read; SM D when the instance has no license; D an unusable billing anchor (4 cases) CI (spec_helper) unverified: runs in CI
AC13: SM anchor naming no group reads the license, suspends past grace S17 Acceptance 13; an operator decision D reads the instance license for an anchor that names no existing group, and suspends CI (spec_helper) unverified: runs in CI
AC14: unresolved namespace left alone, row kept S17 Acceptance 14 D leaves it alone, keeps its mapping row, and counts it CI (spec_helper) unverified: runs in CI
AC15: unrecognized status never unsuspended, counted S17 Acceptance 15 D still suspends a namespace whose status it does not recognize..., never unsuspends a namespace whose status... CI (spec_helper) unverified: runs in CI
AC16: failed call logged, counted, run continues, retried; other condition still called (action 404) S17 Acceptance 16 D failures group (read failure table, suspend failure table incl. 404, retry) CI (spec_helper) unverified: runs in CI
AC17: every registry call presents the service credential, no user S17 Acceptance 17 existing ee/spec/lib/artifact_registry/resources/namespaces_spec.rb lines 33, 474-522, 631-634; D builds every client with no user CI (spec_helper) existing examples cover the client; D example unverified: runs in CI
AC18: driver flag off: no suspension call in run or renewal; on for one org only S17 Acceptance 18 (notice clause out of scope for this MR) D the driver feature flag (3 examples) CI (spec_helper) unverified: runs in CI
AC19-AC28: notices, mailer, banner, GraphQL field, accessibility S17 Acceptance 19-28 none n/a not unit acceptance (out of scope for this MR, notice follow-up 1/notice follow-up 2/notice follow-up 3)
EC1: registry unreachable, 429, 5xx S17 Error cases row 1 D read-failure table (503, 429); D suspend-failure table (503) CI (spec_helper) unverified: runs in CI
EC2: service token missing or rejected (401/403) S17 Error cases row 2 D read-failure table (AuthorizationError 401) CI (spec_helper) unverified: runs in CI
EC3: any other non-2xx S17 Error cases row 3 D read-failure table (ApiError 400) CI (spec_helper) unverified: runs in CI
EC4: 404 for the action S17 Error cases row 4 D suspend-failure table (ApiError 404) CI (spec_helper) unverified: runs in CI
EC5: namespace does not resolve S17 Error cases row 5 D when the registry does not resolve the namespace CI (spec_helper) unverified: runs in CI
EC6: unusable billing anchor S17 Error cases row 6 D an unusable billing anchor CI (spec_helper) unverified: runs in CI
EC7: plan cannot be read S17 Error cases row 7 D when the plan cannot be read; SM when the instance has no license CI (spec_helper) unverified: runs in CI
EC8: unknown status value S17 Error cases row 8 D unrecognized-status examples (suspension and limit arms) CI (spec_helper) unverified: runs in CI
EC9: queuing a notice email fails S17 Error cases row 9 none n/a not unit acceptance (notices, out of scope for this MR)
SC1: calls present the service credential, no user S17 Security considerations 1 AC17 row CI (spec_helper) as AC17
SC2: each call logged with organization and namespace UUID, never a user S17 Security considerations 2 D logs each condition call with the organization and the namespace UUID and no user CI (spec_helper) unverified: runs in CI
SC3: no audit event from either condition call S17 Security considerations 3 none n/a gap: no assertion that no audit event is recorded
SC4: driver touches only the two billing conditions, reads only the listed data S17 Security considerations 4 implicit: the client in D is an instance_double that stubs only the four condition methods and namespace, so any other call raises CI (spec_helper) implicit only
SC7: driver adds nothing to the disclosure of the registry S17 Security considerations 7 none n/a no Rails-side assertion possible beyond the condition calls themselves
SC8-SC9: banner disclosure, email recipients S17 Security considerations 8-9 none n/a not unit acceptance (notices, out of scope for this MR)
Notice record: three nullable columns, only suspension_date written an operator decision; S17 The notice record M the billing notice record (2); D records the suspension date..., keeps a suspension date already recorded for the lapse, unsuspends a renewed namespace and clears the recorded suspension date CI (spec_helper) unverified: runs in CI

D driver spec, WR renewal worker spec (part 4), M namespace_mapping_spec.

Still open on the issue: the read-only experience for suspended and limit_blocked namespaces, which waits on the monolith issue, a registry issue and the subscription plan epic.

Edited by Pawel Rozlach

Merge request reports

Loading
Loading