-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure request promotion patterns separate from build key
Refactor BuildKeyPromotionPatterns to make it more generic so it can be used for more than just "build key" ordering. Create a new static / env var for configuring the patterns that will be used reorder requests, so this can be configured independently of the build key ordering configuration. Start conservatively by only promoting "*platform*" requests, and revert the build key configuration back to its original value. Signed-off-by: J Robert Ray <jrray@imageworks.com>
- Loading branch information
J Robert Ray
committed
Aug 22, 2023
1 parent
4f1ac61
commit 96f3950
Showing
6 changed files
with
86 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
crates/spk-solve/crates/package-iterator/src/promotion_patterns.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Sony Pictures Imageworks, et al. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// https://github.com/imageworks/spk | ||
|
||
use glob::Pattern; | ||
|
||
#[cfg(test)] | ||
#[path = "./promotion_patterns_test.rs"] | ||
mod promotion_patterns_test; | ||
|
||
/// A list of glob patterns that can be used to match and reorder other lists. | ||
pub struct PromotionPatterns(Vec<Pattern>); | ||
|
||
impl PromotionPatterns { | ||
/// Parse a comma-separated string into a list of patterns. | ||
pub fn new(comma_separated_patterns: &str) -> Self { | ||
Self( | ||
comma_separated_patterns | ||
.split(',') | ||
.filter_map(|p| Pattern::new(p).ok()) | ||
.collect(), | ||
) | ||
} | ||
|
||
/// Sort the given list by moving any entries that match the list of | ||
/// promoted names to the front, but otherwise preserving the original | ||
/// order. The function `f` is used to extract the name to compare to for | ||
/// each element of the list. | ||
/// | ||
/// Entries that match are ordered based on the order of the patterns, | ||
/// where patterns at a lower index are prioritized. | ||
pub fn promote_names<N, F>(&self, names: &mut [N], f: F) | ||
where | ||
F: Fn(&N) -> &str, | ||
{ | ||
names.sort_by_cached_key(|name| { | ||
self.0 | ||
.iter() | ||
.enumerate() | ||
.find(|(_, pattern)| pattern.matches(f(name))) | ||
.map(|(index, _)| index) | ||
.unwrap_or(usize::MAX) | ||
}) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
crates/spk-solve/crates/package-iterator/src/promotion_patterns_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Sony Pictures Imageworks, et al. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// https://github.com/imageworks/spk | ||
|
||
use rstest::rstest; | ||
|
||
use crate::PromotionPatterns; | ||
|
||
#[rstest] | ||
#[case("gcc", &["a", "b", "gcc"], &["gcc", "a", "b"])] | ||
#[case::pattern_order_matters_1("gcc,python", &["a", "python", "b", "gcc"], &["gcc", "python", "a", "b"])] | ||
#[case::pattern_order_matters_2("python,gcc", &["a", "python", "b", "gcc"], &["python", "gcc", "a", "b"])] | ||
#[case::pattern_glob("*platform*,python,gcc", &["a", "python", "b", "gcc", "spi-platform"], &["spi-platform", "python", "gcc", "a", "b"])] | ||
fn test_promote_names(#[case] patterns: &str, #[case] input: &[&str], #[case] expected: &[&str]) { | ||
let patterns = PromotionPatterns::new(patterns); | ||
let mut subject = input.to_owned(); | ||
patterns.promote_names(subject.as_mut_slice(), |n| n); | ||
assert_eq!(subject, expected) | ||
} |