✨ Priorityqueue: Optionally return items within a priority fairly #3261
+476
−39
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change makes it possible for the priorityqueue to return items fairly across one or more fairness dimensions. These fairness dimensions are extracted from items using a configurable extractor func. Fairness is only ensured within a given priority, as the purpose of priorities is to return higher-priority items before lower-priority items.
An example use-case is to use the namespace as fairness dimension, which then ensures that one very busy namespace can not starve off other namespaces.
An example use-case for more than one dimension are multi-cluster controllers, where fairness should first be achieved across clusters and then across namespaces within each cluster.
By default, no extractor func is configured so this doesn't change any behavior. We can revisit this in the future.
Naively, the whole problem looks like it could be solved by sorting the items in the queue appropriately, but that doesn't really work because:
This change solves the problem by introducing a
fairQueue
structure which holds all items that are ready to be handed out and in the highest priority bracket that has ready items. The priorityqueue then hands out items from its internalfairQueue
and manages its content.The
fairQueue
works by using a slice of all the fairness dinensions it has ever observed and a map of fairness dimension to item slice. Whenever it hands out an item, it iterates over this slice and checks the corresponding map for items until it found an item to hand out.