Skip to content

Commit ff57fc5

Browse files
authored
Merge pull request #2197 from Jamesbarford/feat/queuing-try-commits
Feat; queuing try commits
2 parents 8f1cf93 + 67835be commit ff57fc5

File tree

7 files changed

+229
-6
lines changed

7 files changed

+229
-6
lines changed

database/src/pool.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ pub trait Connection: Send + Sync {
197197
benchmark_request: &BenchmarkRequest,
198198
benchmark_request_status: BenchmarkRequestStatus,
199199
) -> anyhow::Result<()>;
200+
201+
/// Update a Try commit to have a `sha` and `parent_sha`. Will update the
202+
/// status of the request too a ready state.
203+
async fn attach_shas_to_try_benchmark_request(
204+
&self,
205+
pr: u32,
206+
sha: &str,
207+
parent_sha: &str,
208+
) -> anyhow::Result<()>;
200209
}
201210

202211
#[async_trait::async_trait]
@@ -534,4 +543,108 @@ mod tests {
534543
})
535544
.await;
536545
}
546+
547+
#[tokio::test]
548+
async fn updating_try_commits() {
549+
run_postgres_test(|ctx| async {
550+
let db = ctx.db_client();
551+
let db = db.connection().await;
552+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
553+
let pr = 42;
554+
555+
let try_benchmark_request = BenchmarkRequest::create_try(
556+
None,
557+
None,
558+
pr,
559+
time,
560+
BenchmarkRequestStatus::WaitingForArtifacts,
561+
"cranelift",
562+
"",
563+
);
564+
db.insert_benchmark_request(&try_benchmark_request).await;
565+
db.attach_shas_to_try_benchmark_request(pr, "foo", "bar")
566+
.await
567+
.unwrap();
568+
let requests = db
569+
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::ArtifactsReady])
570+
.await
571+
.unwrap();
572+
573+
assert_eq!(requests.len(), 1);
574+
assert_eq!(requests[0].tag(), Some("foo"));
575+
assert_eq!(requests[0].parent_sha(), Some("bar"));
576+
assert_eq!(requests[0].status, BenchmarkRequestStatus::ArtifactsReady);
577+
578+
Ok(ctx)
579+
})
580+
.await;
581+
}
582+
583+
#[tokio::test]
584+
async fn adding_try_commit_to_completed_request() {
585+
run_postgres_test(|ctx| async {
586+
let db = ctx.db_client();
587+
let db = db.connection().await;
588+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
589+
let pr = 42;
590+
591+
let completed_try = BenchmarkRequest::create_try(
592+
Some("sha-2"),
593+
Some("p-sha-1"),
594+
pr,
595+
time,
596+
BenchmarkRequestStatus::Completed,
597+
"cranelift",
598+
"",
599+
);
600+
db.insert_benchmark_request(&completed_try).await;
601+
602+
let try_benchmark_request = BenchmarkRequest::create_try(
603+
None,
604+
None,
605+
pr,
606+
time,
607+
BenchmarkRequestStatus::WaitingForArtifacts,
608+
"cranelift",
609+
"",
610+
);
611+
// deliberately insert twice
612+
db.insert_benchmark_request(&try_benchmark_request).await;
613+
// this one should fail
614+
db.insert_benchmark_request(&try_benchmark_request).await;
615+
db.attach_shas_to_try_benchmark_request(pr, "foo", "bar")
616+
.await
617+
.unwrap();
618+
619+
let requests = db
620+
.get_benchmark_requests_by_status(&[
621+
BenchmarkRequestStatus::WaitingForArtifacts,
622+
BenchmarkRequestStatus::ArtifactsReady,
623+
BenchmarkRequestStatus::InProgress,
624+
BenchmarkRequestStatus::Completed,
625+
])
626+
.await
627+
.unwrap();
628+
629+
assert_eq!(requests.len(), 2);
630+
let completed_try = requests
631+
.iter()
632+
.find(|req| req.status == BenchmarkRequestStatus::Completed);
633+
assert!(completed_try.is_some());
634+
assert_eq!(completed_try.unwrap().pr(), Some(&pr));
635+
assert_eq!(completed_try.unwrap().tag(), Some("sha-2"));
636+
assert_eq!(completed_try.unwrap().parent_sha(), Some("p-sha-1"));
637+
638+
let artifacts_ready_try = requests
639+
.iter()
640+
.find(|req| req.status == BenchmarkRequestStatus::ArtifactsReady);
641+
assert!(artifacts_ready_try.is_some());
642+
assert_eq!(artifacts_ready_try.unwrap().pr(), Some(&pr));
643+
assert_eq!(artifacts_ready_try.unwrap().tag(), Some("foo"));
644+
assert_eq!(artifacts_ready_try.unwrap().parent_sha(), Some("bar"));
645+
646+
Ok(ctx)
647+
})
648+
.await;
649+
}
537650
}

database/src/pool/postgres.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,39 @@ where
15251525

15261526
Ok(())
15271527
}
1528+
1529+
async fn attach_shas_to_try_benchmark_request(
1530+
&self,
1531+
pr: u32,
1532+
sha: &str,
1533+
parent_sha: &str,
1534+
) -> anyhow::Result<()> {
1535+
self.conn()
1536+
.execute(
1537+
"UPDATE
1538+
benchmark_request
1539+
SET
1540+
tag = $1,
1541+
parent_sha = $2,
1542+
status = $3
1543+
WHERE
1544+
pr = $4
1545+
AND commit_type = 'try'
1546+
AND tag IS NULL
1547+
AND status = $5;",
1548+
&[
1549+
&sha,
1550+
&parent_sha,
1551+
&BenchmarkRequestStatus::ArtifactsReady,
1552+
&(pr as i32),
1553+
&BenchmarkRequestStatus::WaitingForArtifacts,
1554+
],
1555+
)
1556+
.await
1557+
.context("failed to execute UPDATE benchmark_request")?;
1558+
1559+
Ok(())
1560+
}
15281561
}
15291562

15301563
fn parse_artifact_id(ty: &str, sha: &str, date: Option<DateTime<Utc>>) -> ArtifactId {

database/src/pool/sqlite.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,15 @@ impl Connection for SqliteConnection {
12821282
) -> anyhow::Result<()> {
12831283
no_queue_implementation_abort!()
12841284
}
1285+
1286+
async fn attach_shas_to_try_benchmark_request(
1287+
&self,
1288+
_pr: u32,
1289+
_sha: &str,
1290+
_parent_sha: &str,
1291+
) -> anyhow::Result<()> {
1292+
no_queue_implementation_abort!()
1293+
}
12851294
}
12861295

12871296
fn parse_artifact_id(ty: &str, sha: &str, date: Option<i64>) -> ArtifactId {

site/src/github.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod client;
22
pub mod comparison_summary;
33

44
use crate::api::github::Commit;
5+
use crate::job_queue::run_new_queue;
56
use crate::load::{MissingReason, SiteCtxt, TryCommit};
67
use std::sync::LazyLock;
78
use std::time::Duration;
@@ -233,6 +234,22 @@ pub async fn rollup_pr_number(
233234
.then_some(issue.number))
234235
}
235236

237+
async fn attach_shas_to_try_benchmark_request(
238+
conn: &dyn database::pool::Connection,
239+
pr: u32,
240+
sha: &str,
241+
parent_sha: &str,
242+
) {
243+
if run_new_queue() {
244+
if let Err(e) = conn
245+
.attach_shas_to_try_benchmark_request(pr, sha, parent_sha)
246+
.await
247+
{
248+
log::error!("Failed to add shas to try commit {}", e);
249+
}
250+
}
251+
}
252+
236253
pub async fn enqueue_shas(
237254
ctxt: &SiteCtxt,
238255
gh_client: &client::Client,
@@ -258,6 +275,15 @@ pub async fn enqueue_shas(
258275
parent_sha: commit_response.parents.remove(0).sha,
259276
};
260277
let conn = ctxt.conn().await;
278+
279+
attach_shas_to_try_benchmark_request(
280+
&*conn,
281+
pr_number,
282+
&try_commit.sha,
283+
&try_commit.parent_sha,
284+
)
285+
.await;
286+
261287
let queued = conn
262288
.pr_attach_commit(
263289
pr_number,

site/src/job_queue.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ use parking_lot::RwLock;
1212
use regex::Regex;
1313
use tokio::time::{self, Duration};
1414

15+
pub fn run_new_queue() -> bool {
16+
std::env::var("RUN_CRON")
17+
.ok()
18+
.and_then(|x| x.parse().ok())
19+
.unwrap_or(false)
20+
}
21+
1522
/// Store the latest master commits or do nothing if all of them are
1623
/// already in the database
1724
async fn create_benchmark_request_master_commits(

site/src/main.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use futures::future::FutureExt;
22
use parking_lot::RwLock;
3+
use site::job_queue::{cron_main, run_new_queue};
34
use site::load;
45
use std::env;
56
use std::sync::Arc;
@@ -33,10 +34,6 @@ async fn main() {
3334
.ok()
3435
.and_then(|x| x.parse().ok())
3536
.unwrap_or(30);
36-
let run_cron_job = env::var("RUN_CRON")
37-
.ok()
38-
.and_then(|x| x.parse().ok())
39-
.unwrap_or(false);
4037

4138
let fut = tokio::task::spawn_blocking(move || {
4239
tokio::task::spawn(async move {
@@ -62,9 +59,9 @@ async fn main() {
6259

6360
let server = site::server::start(ctxt.clone(), port).fuse();
6461

65-
if run_cron_job {
62+
if run_new_queue() {
6663
task::spawn(async move {
67-
site::job_queue::cron_main(ctxt.clone(), queue_update_interval_seconds).await;
64+
cron_main(ctxt.clone(), queue_update_interval_seconds).await;
6865
});
6966
}
7067

site/src/request_handlers/github.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use crate::github::{
33
client, enqueue_shas, parse_homu_comment, rollup_pr_number, unroll_rollup,
44
COMMENT_MARK_TEMPORARY, RUST_REPO_GITHUB_API_URL,
55
};
6+
use crate::job_queue::run_new_queue;
67
use crate::load::SiteCtxt;
78

9+
use database::{BenchmarkRequest, BenchmarkRequestStatus};
810
use hashbrown::HashMap;
911
use std::sync::Arc;
1012

@@ -72,6 +74,29 @@ async fn handle_issue(
7274
Ok(github::Response)
7375
}
7476

77+
/// The try does not have a `sha` or a `parent_sha` but we need to keep a record
78+
/// of this commit existing. We make sure there can only be one `pr` with a
79+
/// status of `WaitingForArtifacts` to ensure we don't have duplicates.
80+
async fn queue_partial_try_benchmark_request(
81+
conn: &dyn database::pool::Connection,
82+
pr: u32,
83+
backends: &str,
84+
) {
85+
// We only want to run this if the new system is running
86+
if run_new_queue() {
87+
let try_request = BenchmarkRequest::create_try(
88+
None,
89+
None,
90+
pr,
91+
chrono::Utc::now(),
92+
BenchmarkRequestStatus::WaitingForArtifacts,
93+
backends,
94+
"",
95+
);
96+
conn.insert_benchmark_request(&try_request).await;
97+
}
98+
}
99+
75100
async fn handle_rust_timer(
76101
ctxt: Arc<SiteCtxt>,
77102
main_client: &client::Client,
@@ -97,6 +122,13 @@ async fn handle_rust_timer(
97122
let msg = match queue {
98123
Ok(cmd) => {
99124
let conn = ctxt.conn().await;
125+
126+
queue_partial_try_benchmark_request(
127+
&*conn,
128+
issue.number,
129+
cmd.params.backends.unwrap_or(""),
130+
)
131+
.await;
100132
conn.queue_pr(
101133
issue.number,
102134
cmd.params.include,
@@ -137,6 +169,12 @@ async fn handle_rust_timer(
137169
{
138170
let conn = ctxt.conn().await;
139171
for command in &valid_build_cmds {
172+
queue_partial_try_benchmark_request(
173+
&*conn,
174+
issue.number,
175+
command.params.backends.unwrap_or(""),
176+
)
177+
.await;
140178
conn.queue_pr(
141179
issue.number,
142180
command.params.include,

0 commit comments

Comments
 (0)