chore(flags): Add method to build up evaluation levels from a flags DAG
Note that this PR targets the PR #33643.
Problem
In preparation for the upcoming feature where feature flags can depend on other feature flags, we need to consider feature flag dependencies. Typically, this would require that we sort flags topologically, reverse them, and then evaluate them sequentially.
However, our current code evaluates flags in parallel, and we'd like to keep doing that. So what we need to do is build up a multi-root DAG (Directed Acyclic Graph) and evaluate flag dependencies in stages. This PR implements the ability to build those stages (it doesn't do the evaluation in stages yet).
Related to #29016
Changes
This adds a two new methods: build_from_nodes which takes in a collection of nodes and builds up a multi-root graph with all the nodes.
The second new method is evaluation_stages. This returns flags in stages where each stage must be evaluated before the next stage can be evaluated. Here's an example:
Suppose we have these flags (represented by IDs) and their dependencies (represented by arrows):
1
2 -> 3 -> 4
3 -> 4
4
5
6
7 -> 8
8
In other words:
1, 5, 6 - are independent flags (no dependencies, nobody depends on them) 2 depends on 3 3 depends on 4
When we evaluate these flags, we have to do them in the following stages:
[1, 4, 5, 6, 8](leaf nodes and indpendent flags)[3, 7](flags with one dependency)[2](flags with two dependencies)
This allows us to evaluate the flags in each stage in parallel while making sure we always evaluate dependencies before parents.
Did you write or update any docs for this change?
- [ ] I've added or updated the docs
- [ ] I've reached out for help from the docs team
- [x] No docs needed for this change
How did you test this code?
Unit tests