Restore preserved states when a deletion is cancelled

What does this MR do and why?

Problem: When you cancel a group's deletion, all subgroups become "active" — even ones that were archived before the deletion was scheduled.

Solution: This MR remembers each subgroup's original state and restores it when deletion is cancelled.

State Transition Overview

stateDiagram-v2
    direction LR
    ancestor_inherited --> archived: archive
    ancestor_inherited --> deletion_scheduled: schedule deletion
    archived --> deletion_scheduled: schedule deletion
    archived --> ancestor_inherited: unarchive
    deletion_scheduled --> ancestor_inherited: cancel (restore)
    deletion_scheduled --> archived: cancel (restore to archived)

Example Scenario

Before this MR:

Parent Group (deletion_scheduled → ancestor_inherited)
├── Subgroup A (was archived before deletion) → ❌ becomes ancestor_inherited
└── Subgroup B (was active before deletion) → ancestor_inherited

After this MR:

Parent Group (deletion_scheduled → ancestor_inherited)
├── Subgroup A (was archived before deletion) → ✅ restored to archived
└── Subgroup B (was active before deletion) → ancestor_inherited

Technical Details

  • Each descendant goes back to its preserved schedule_deletion state, but never below the parent's new state. A descendant with nothing preserved takes the parent's state. The preserved state is then removed.
  • Cancelling an archived group's deletion (deletion_scheduled → archived) is now a reversal, so its deletion_scheduled descendants are restored too, as ADR 003 describes ("Allowed as restore to archived state").

Nothing creates propagation records yet (#599305 (closed)), so this has no user-facing effect until that lands.

Database queries

Restore mode runs these per batch of up to 200 namespace IDs. Timings are from a gitlab-production-main Database Lab thin clone, using a batch of 200 IDs spread across the ID range (133 matched rows). The UPDATE was measured as a single statement covering the whole batch, the worst case. The existing query that removes preserved states is not re-measured.

Lock the batch's rows. Restore now matches only rows still in deletion_scheduled, so a re-run after a partial failure skips descendants it already restored. Measured with 200 randomly chosen deletion_scheduled IDs, so every row matches and is locked (the worst case):

SELECT "namespaces"."id", "namespaces"."state"
FROM "namespaces"
WHERE "namespaces"."state" = 2
  AND "namespaces"."id" IN (...)
ORDER BY "namespaces"."id" ASC
LIMIT 200
FOR NO KEY UPDATE
Limit  (cost=0.57..555.70 rows=1 width=12) (actual time=0.061..1.477 rows=200 loops=1)
  Buffers: shared hit=1395
  ->  LockRows  (cost=0.57..555.70 rows=1 width=12) (actual time=0.060..1.460 rows=200 loops=1)
        Buffers: shared hit=1395
        ->  Index Scan using namespaces_pkey on public.namespaces  (cost=0.57..555.69 rows=1 width=12) (actual time=0.021..1.158 rows=200 loops=1)
              Index Cond: (namespaces.id = ANY ('{...}'::integer[]))
              Filter: (namespaces.state = 2)
              Buffers: shared hit=1000

Read each namespace's preserved state:

SELECT "namespace_details"."namespace_id",
       state_metadata -> 'preserved_states' ->> 'schedule_deletion'
FROM "namespace_details"
WHERE "namespace_details"."namespace_id" IN (...)
Index Scan using namespace_details_pkey on public.namespace_details  (cost=0.57..676.43 rows=200 width=40) (actual time=0.053..1.607 rows=133 loops=1)
  Index Cond: (namespace_id = ANY ('{...}'::bigint[]))
  Buffers: shared hit=933

Move rows to their restored state, one statement per distinct state (at most two: ancestor_inherited and archived):

UPDATE "namespaces" SET "state" = 1 WHERE "namespaces"."id" IN (...)
Update on public.namespaces  (cost=0.57..556.92 rows=0 width=0) (actual time=776.890..776.891 rows=0 loops=1)
  Buffers: shared hit=10059 read=589 dirtied=1082 written=10
  ->  Index Scan using namespaces_pkey on public.namespaces  (cost=0.57..556.92 rows=200 width=8) (actual time=0.054..3.198 rows=133 loops=1)
        Index Cond: (id = ANY ('{...}'::bigint[]))

Known limitations

  • A subgroup that scheduled its own deletion is restored like any other descendant when its parent cancels. Keeping its schedule, and stopping propagation there, is a follow-up noted on #633441 (closed).
  • Descendants in a non-propagated state (for example deletion_in_progress) are skipped, along with their subtrees.

References

How to set up and validate locally

bin/rspec spec/models/concerns/namespaces/stateful/state_precedence_spec.rb
bin/rspec spec/models/concerns/namespaces/stateful_spec.rb \
  spec/services/namespaces/state_propagation_service_spec.rb \
  spec/workers/namespaces/state_propagation_worker_spec.rb

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.

Related to #633441 (closed)

Edited by Shane Maglangit

Merge request reports

Loading
Loading