Skip to content

Follow-ups to the tracking cleanup deletion order #1573

Backlog

Description

@FragmentedPacket

The tracking cleanup now deletes unused members in dependency order (#1487, fixing #1477). The review of that pull request listed six changes that can be made in separate pull requests. They are collected here so they are not lost. Which of them to do, and in which order, is not decided yet.

Source: review by ogenstad on #1487, at commit 70101e03. The line links below point at that commit.

Changes to make

  1. Share the client-side ordering between callers. The ordering code that uses the client (_order_for_deletion in both clients, _mandatory_peer_ids and _member_peers, query_groups.py:124-141) lives in the group context. A future client.delete_nodes() would have to copy it or import private helpers. One method per client that reads the schema, queries the peers and returns the order would let delete_unused and later callers share it. That method needs the schema refresh that _order_for_deletion now does when a kind is missing from the cache, because a caller that did not read a group first may have an incomplete cache.
  2. Send fewer and smaller peer query requests. The peer query at query_groups.py:291 sends every id with every page request. With 5,000 members and the default page size of 50, that is 101 requests of about 190 KB each. Splitting the ids into chunks the size of a page, or setting limit=len(ids), avoids this. The async client also sends the query for each kind one after another.
  3. Accept dependencies that are not keys in the graph helpers. dependents_first and strongly_connected_components raise KeyError when a dependency is not a key, for example dependents_first({"a": {"b"}}, order=["a"]). topological_sort accepts such keys. This needs fixing before the helpers are reused for import ordering.
  4. Make dependents_first faster on long chains. It runs topological_sort, which first runs a recursive cycle search and discards the result, then scans every remaining key at each level. A chain of 10,000 nodes costs about 10^8 set operations, and a long chain that contains a cycle can reach Python's recursion limit.
  5. Use one dependency graph for deletion and import. _mandatory_peer_kinds builds the same graph as InfrahubSchemaTopologicalSorter.get_sorted_node_schema(required_relationships_only=True) in transfer/schema_sorter.py, but handles generic peers differently, so a fix to one does not reach the other.
  6. Simplify PeerLookup.relationships to a list of names. DeletionOrder.cardinality_many and the dict[str, bool] form of PeerLookup.relationships exist only so _mandatory_peer_ids can choose an accessor. The node already carries its schema, so relationships could be a list[str].

Not decided yet

  • Whether a public API to delete many nodes in dependency order, such as client.delete_nodes(), should be added, and what its name and behaviour would be.
  • Which branch these changes target.

Activity

  1. added
    type/tech-debt
    on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/tech-debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions