Schedule the Artifact Registry billing state driver and subscribe it to renewals (AR#2019 stack: 4/5)

What does this MR do and why?

This MR schedules the Artifact Registry billing state driver and subscribes it to subscription renewals. It follows S17 (billing state driver spec). It is part 4 of 4 of the stack and sits on the usage-limit arm.

  • Schedule. ArtifactRegistry::BillingStateRunWorker runs both arms every 30 minutes, and ArtifactRegistry::UsageLimitLiftSweepWorker lifts limit blocks every 5 minutes. Each is deduplicated on itself (2-hour and 30-minute TTL), and the cron entries are in ee/config/schedule.yml.
  • Cron gates. Both cron workers return before the driver runs when the new ops flag artifact_registry_billing_state_crons is off. The run worker also returns while artifact_registry_billing_state_driver and artifact_registry_usage_limit_block are both off for every organization. The sweep worker also returns while artifact_registry_usage_limit_block is off for every organization. The sweep acts only on organizations with that flag on.
  • Renewal. ArtifactRegistry::BillingRenewalWorker subscribes to GitlabSubscriptions::RenewedEvent and GitlabSubscriptions::RenewedCloudEvent and runs the suspension arm at once for the billing organization of the renewed namespace. The billing organization is the organization of the namespace, or, for a group in the default organization, the organization that links the group. The subscription fires only when artifact_registry_billing_state_driver is on for that billing organization.
  • Counts. Each worker logs the driver counts with log_hash_metadata_on_done when the job finishes.

Points for the reviewer:

  • One new ops flag. artifact_registry_billing_state_crons is a kill switch for both crons, and it is on by default. The rollout flags are the ones from part 2 and part 3, so with both of them off the crons call nothing and read nothing.
  • Driver service changes. The lift sweep now skips an organization with artifact_registry_usage_limit_block off, because the run lifts the blocks of that organization. The driver reads both flags with Organizations::Organization.actor_from_id, so it no longer loads each organization, and this MR removes NamespaceMapping.each_with_organization.
  • Inline RuboCop disables. Each disable carries its reason on the line:
    • Scalability/CronWorkerContext on both cron workers: each job spans every organization and has no single context.
    • Gitlab/AvoidFeatureGet on both cron workers: the gate only reads whether a rollout flag is on for any organization.
    • CodeReuse/ActiveRecord on find_each in the driver service: a false positive on the block argument.
    • Gitlab/RSpec/AvoidCreateDefaultOrganization in two specs: a linked group must come from the default organization.
  • The 60-minute alert. The alert rule that selects ArtifactRegistry::BillingStateRunWorker is in the follow-up for the alert rules.
S17 rows this MR covers
  • Acceptance 3: a renewal unsuspends without waiting for a run (the worker side; the driver side is in part 2).
  • Acceptance 37: the run and the sweep are each deduplicated on themselves, and not on each other.

The stack

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

References

Related to the issue

Screenshots or screen recordings

n/a: this MR changes no frontend code.

How to set up and validate locally

These steps need a GDK with the Artifact Registry service configured. They were not run for this MR.

  1. In the Rails console, turn on the driver for one organization:

    Feature.enable(:artifact_registry_billing_state_driver, Organizations::Organization.find(<id>))
  2. Move the payer group to a plan without the registry, with a lapse date more than 14 days ago, then run ArtifactRegistry::BillingStateRunWorker.new.perform. Expect a billing state driver: condition call log line with action: :suspend, and the namespace status suspended in the registry.

  3. Renew the subscription of the group. Expect ArtifactRegistry::BillingRenewalWorker to run and call unsuspend.

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.
  • Database. No migration. For a group in the default organization, the renewal worker and its subscription read organization_linked_groups with the existing Organizations::LinkedGroup.organization_id_linked_to. The other queries are the ones that part 2 adds.
  • Performance and reliability. All three workers defer on the database health signal for artifact_registry_namespace_mappings. The run and the sweep are deduplicated until executed, so a job that outlasts its interval drops the next tick.
  • Security. The workers act for no user.
  • Feature flags. One new ops flag, artifact_registry_billing_state_crons, on by default.
Edited by Pawel Rozlach

Merge request reports

Loading
Loading