Skip to content

Commit

Permalink
Eli/fix logical rate (#14384)
Browse files Browse the repository at this point in the history
logical rate will be every X pull requests instead of using a math
random function. This will guarantee better distribution

---------

Co-authored-by: trunk bot <[email protected]>
  • Loading branch information
EliSchleifer and trunk bot authored Apr 8, 2024
1 parent a804b5d commit 04fa114
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .config/mq.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ max_deps = 7
max_impacted_deps = 2

# rate at which PRs should simulate a logical merge conflict and need to be rejected
logical_conflict_rate = 0.002
logical_conflict_every = 500
logical_conflict_file = "logical-conflict.txt"

[merge]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ mode = "singlequeue"
# Default value: 1
#max_impacted_deps = 1

# Default value: 0.01
#logical_conflict_rate = 0.01
# Default value: 100 (create logical merge conflict every 100 PRs)
#logical_conflict_every = 100

# Default value: "logical-conflict.txt"
#logical_conflict_file = "logical-conflict.txt"
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ pub struct PullRequestConf {
#[config(default = 1)]
pub max_impacted_deps: usize,

#[config(default = 0.01)]
pub logical_conflict_rate: f32,
#[config(default = 100)]
pub logical_conflict_every: u32,

#[config(default = "logical-conflict.txt")]
pub logical_conflict_file: String,
Expand Down
52 changes: 35 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,35 +157,49 @@ fn simulate_test(config: &Conf) -> bool {
random_float > config.test.flake_rate
}

fn maybe_add_logical_merge_conflict(config: &Conf) -> bool {
fn maybe_add_logical_merge_conflict(last_pr: u32, config: &Conf) -> bool {
if config.pullrequest.logical_conflict_file.is_empty()
|| config.pullrequest.logical_conflict_rate == 0.0
|| config.pullrequest.logical_conflict_every == 0
{
return false;
}

// check if we should simulate a logical merge conflict with this pull request
let mut rng = rand::thread_rng();
let random_float = rng.gen_range(0.0..1.0);
if last_pr + 1 % config.pullrequest.logical_conflict_every != 0 {
return false;
}

println!(
"logical conflict rate: {}",
config.pullrequest.logical_conflict_rate
"logical conflict every {} prs",
config.pullrequest.logical_conflict_every
);
if random_float < config.pullrequest.logical_conflict_rate {
// create logical conflict
let filename = &config.pullrequest.logical_conflict_file;
std::fs::write(filename, "simulate logical merge conflict")
.expect("Unable to write logical merge conflict file");

git(&["add", &config.pullrequest.logical_conflict_file]);
return true;
// create logical conflict
let filename = &config.pullrequest.logical_conflict_file;
std::fs::write(filename, "simulate logical merge conflict")
.expect("Unable to write logical merge conflict file");

git(&["add", &config.pullrequest.logical_conflict_file]);
true
}

fn get_last_pr() -> u32 {
let result = try_gh(&["pr", "list", "--limit=1", "--json", "number"]);
if result.is_err() {
return 0;
}
false
let json_str = result.unwrap();

let v: Value = serde_json::from_str(&json_str).expect("Failed to parse JSON");
let last_pr = v
.as_array()
.and_then(|arr| arr.first().cloned())
.expect("Failed to get first item");
last_pr["number"].as_u64().unwrap_or(0) as u32
}

fn create_pull_request(words: &[String], config: &Conf) -> Result<String, String> {
let lc = maybe_add_logical_merge_conflict(config);
fn create_pull_request(words: &[String], last_pr: u32, config: &Conf) -> Result<String, String> {
let lc = maybe_add_logical_merge_conflict(last_pr, config);

let branch_name = format!("change/{}", words.join("-"));
git(&["checkout", "-t", "-b", &branch_name]);
Expand Down Expand Up @@ -281,6 +295,9 @@ fn run() -> anyhow::Result<()> {
// divide by 6 since we run once every 10 minutes
let pull_requests_to_make = (config.pullrequest.requests_per_hour as f32 / 6.0).ceil() as usize;

// get the most recent PR to be created (used for creating logical merge conflicts)
let mut last_pr = get_last_pr();

let mut prs: Vec<String> = Vec::new();

for _ in 0..pull_requests_to_make {
Expand All @@ -300,7 +317,7 @@ fn run() -> anyhow::Result<()> {
let max_impacted_deps = config.pullrequest.max_impacted_deps as u32; // Convert usize to u32
let words = change_file(&filenames, max_impacted_deps); // Use the converted value

let pr_result = create_pull_request(&words, &config);
let pr_result = create_pull_request(&words, last_pr, &config);
if pr_result.is_err() {
println!("problem created pr for {:?}", words);
continue;
Expand All @@ -309,6 +326,7 @@ fn run() -> anyhow::Result<()> {
let pr = pr_result.unwrap();
println!("created pr: {} in {:?}", pr, duration);
prs.push(pr);
last_pr += 1;
}

for pr in &prs {
Expand Down

0 comments on commit 04fa114

Please sign in to comment.