Rewrite work item status rows to custom ids when a group converts
What does this MR do and why?
When a group converts from the built-in statuses to custom statuses, WorkItems::Lifecycles::BaseService creates custom copies of the five built-in statuses but leaves existing work_item_current_statuses rows pointing at the built-in ids. Those rows are translated at read time, and the status filter has to check both columns, which is one of the branches behind the timeouts in https://gitlab.com/gitlab-org/gitlab/-/work_items/602041.
!255857 (merged) backfills the rows that already exist. This MR stops new ones from appearing: after a conversion, whether through WorkItems::Lifecycles::CreateService or through WorkItems::Lifecycles::UpdateService (editing the built-in statuses in group settings), the lifecycle service enqueues WorkItems::Statuses::BackfillConvertedStatusIdsWorker for the root group. The worker walks the group's namespace tree with Gitlab::Database::NamespaceEachBatch in batches of 100 and, through WorkItems::Statuses::BackfillConvertedStatusIdsService, rewrites their status rows in passes of 1,000 with the same UPDATE the migration uses, matching on converted_from_system_defined_status_identifier. updated_at is untouched. The worker stops after three minutes and reschedules itself with a namespace cursor, so a group of any size is handled in bounded jobs. The limit is checked after every 1,000-row statement as well. A namespace batch counts as drained only when a pass rewrites nothing, since a pass can come back short while rows remain (a row changed concurrently is skipped), and a batch not drained in time is retried from the same cursor, so the cursor only moves past namespaces whose rows are all rewritten. It is idempotent and does nothing for groups without a custom lifecycle. It is enqueued only when a group converts for the first time, which the base service detects by the absence of custom lifecycles; later status or lifecycle edits never enqueue it. There is no feature flag: a gitlab_com_derisk flag is never enabled on self-managed, which would leave legacy rows behind the backfill for groups converting there. The worker is urgency :low, so a job enqueued from canary during the first deploy only waits until Sidekiq nodes have the class. It allows 20 deploy interruptions instead of the default 3, since it resumes from its cursor. The worker covers the rewrite half only; the missing-rows half (a row for every item that has none) will be added to it alongside the second BBM, #629280 (closed), and the filter change that drops the no-row branch depends on both.
Reviewer note from !255857 (merged): this lets the backfill and the conversion path stay independent. Any group converting between the backfill's enqueue and this worker's deploy can be fixed by running the worker for that group.
References
- Epic: &23612 (closed)
- Closes #630892 (closed)
- Related: #629279 (closed), !255857 (merged)
Screenshots or screen recordings
Not applicable, background job only.
How to set up and validate locally
- Specs:
bin/rspec ee/spec/services/work_items/statuses/backfill_converted_status_ids_service_spec.rb \ ee/spec/workers/work_items/statuses/backfill_converted_status_ids_worker_spec.rb \ ee/spec/services/work_items/lifecycles/create_service_spec.rb \ ee/spec/services/work_items/lifecycles/update_service_spec.rb - Restart Sidekiq so it loads the worker:
gdk restart rails-background-jobs. - In
rails console, pick a top-level group that has never converted and give a few of its issues an explicit built-in status:group = Group.find_by_full_path('<path>') group.custom_lifecycles.exists? # => false issues = WorkItem.where('namespace_traversal_ids[1] = ?', group.id).without_current_status.limit(3).to_a WorkItems::Statuses::CurrentStatus.insert_all(issues.map { |i| { work_item_id: i.id, namespace_id: i.namespace_id, system_defined_status_id: 2, updated_at: 1.year.ago } }) - Open one of those issues in the browser: the status widget shows "In progress".
- Convert the group through the UI: Group > Settings > Issues > Statuses, rename any status and save. Watch the job run:
The
gdk tail rails-background-jobs | grep BackfillConvertedStatusIdsdoneline carriesresult: { rewritten: 3, over_time: false, cursor: nil }. - Reload the issue: it still shows "In progress", now pointing at the group's converted status. Confirm the rows moved column and kept
updated_at:WorkItems::Statuses::CurrentStatus.where(work_item_id: issues.map(&:id)).pluck(:system_defined_status_id, :custom_status_id, :updated_at) # => [[nil, <converted "In progress" id>, <unchanged>], ...] - Rename a status again and watch the log: no new job, the group is already converted.
- Clean up:
WorkItems::Statuses::CurrentStatus.where(work_item_id: issues.map(&:id)).delete_all.
Query plans
Against gitlab-org on Database Lab:
| Query | Plan |
|---|---|
One pass of the service's UPDATE over the gitlab-org/gitlab project namespace (124,975 status rows, 5 still on a built-in id; cold cache) |
https://postgres.ai/console/gitlab/gitlab-production-main/sessions/61306/commands/166343 |
One batch of 100 namespaces from NamespaceEachBatch |
to be added |
Earlier plan of the previous UPDATE shape, for reference: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/58909/commands/164088
MR acceptance checklist
- Tested locally per the steps above.
- Specs added for the worker, the service and the scheduling in both lifecycle services.