Skip to content

Commit 39fab1a

Browse files
authored
Merge pull request #2163 from Jamesbarford/feat/queue-cron
Feat - cron queue for master commits
2 parents d7d0647 + a145daa commit 39fab1a

File tree

8 files changed

+372
-10
lines changed

8 files changed

+372
-10
lines changed

database/src/lib.rs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,3 +798,175 @@ pub struct ArtifactCollection {
798798
pub duration: Duration,
799799
pub end_time: DateTime<Utc>,
800800
}
801+
802+
#[derive(Debug)]
803+
pub enum BenchmarkRequestStatus {
804+
WaitingForArtifacts,
805+
WaitingForParent,
806+
InProgress,
807+
Completed,
808+
}
809+
810+
impl fmt::Display for BenchmarkRequestStatus {
811+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
812+
match self {
813+
BenchmarkRequestStatus::WaitingForArtifacts => write!(f, "waiting_for_artifacts"),
814+
BenchmarkRequestStatus::WaitingForParent => write!(f, "waiting_for_parent"),
815+
BenchmarkRequestStatus::InProgress => write!(f, "in_progress"),
816+
BenchmarkRequestStatus::Completed => write!(f, "completed"),
817+
}
818+
}
819+
}
820+
821+
#[derive(Debug)]
822+
pub enum BenchmarkRequestType {
823+
/// A Try commit
824+
Try {
825+
sha: String,
826+
parent_sha: String,
827+
pr: u32,
828+
},
829+
/// A Master commit
830+
Master {
831+
sha: String,
832+
parent_sha: String,
833+
pr: u32,
834+
},
835+
/// A release only has a tag
836+
Release { tag: String },
837+
}
838+
839+
impl BenchmarkRequestType {
840+
pub fn commit_type_str(&self) -> &str {
841+
match self {
842+
BenchmarkRequestType::Try {
843+
sha: _,
844+
parent_sha: _,
845+
pr: _,
846+
} => "try",
847+
BenchmarkRequestType::Master {
848+
sha: _,
849+
parent_sha: _,
850+
pr: _,
851+
} => "master",
852+
BenchmarkRequestType::Release { tag: _ } => "release",
853+
}
854+
}
855+
}
856+
857+
impl fmt::Display for BenchmarkRequestType {
858+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
859+
match self {
860+
BenchmarkRequestType::Try { .. } => write!(f, "try"),
861+
BenchmarkRequestType::Master { .. } => write!(f, "master"),
862+
BenchmarkRequestType::Release { tag: _ } => write!(f, "release"),
863+
}
864+
}
865+
}
866+
867+
#[derive(Debug)]
868+
pub struct BenchmarkRequest {
869+
pub commit_type: BenchmarkRequestType,
870+
pub created_at: DateTime<Utc>,
871+
pub completed_at: Option<DateTime<Utc>>,
872+
pub status: BenchmarkRequestStatus,
873+
pub backends: String,
874+
pub profiles: String,
875+
}
876+
877+
impl BenchmarkRequest {
878+
pub fn create_release(
879+
tag: &str,
880+
created_at: DateTime<Utc>,
881+
status: BenchmarkRequestStatus,
882+
backends: &str,
883+
profiles: &str,
884+
) -> Self {
885+
Self {
886+
commit_type: BenchmarkRequestType::Release {
887+
tag: tag.to_string(),
888+
},
889+
created_at,
890+
completed_at: None,
891+
status,
892+
backends: backends.to_string(),
893+
profiles: profiles.to_string(),
894+
}
895+
}
896+
897+
pub fn create_try(
898+
sha: &str,
899+
parent_sha: &str,
900+
pr: u32,
901+
created_at: DateTime<Utc>,
902+
status: BenchmarkRequestStatus,
903+
backends: &str,
904+
profiles: &str,
905+
) -> Self {
906+
Self {
907+
commit_type: BenchmarkRequestType::Try {
908+
pr,
909+
sha: sha.to_string(),
910+
parent_sha: parent_sha.to_string(),
911+
},
912+
created_at,
913+
completed_at: None,
914+
status,
915+
backends: backends.to_string(),
916+
profiles: profiles.to_string(),
917+
}
918+
}
919+
920+
pub fn create_master(
921+
sha: &str,
922+
parent_sha: &str,
923+
pr: u32,
924+
created_at: DateTime<Utc>,
925+
status: BenchmarkRequestStatus,
926+
backends: &str,
927+
profiles: &str,
928+
) -> Self {
929+
Self {
930+
commit_type: BenchmarkRequestType::Master {
931+
pr,
932+
sha: sha.to_string(),
933+
parent_sha: parent_sha.to_string(),
934+
},
935+
created_at,
936+
completed_at: None,
937+
status,
938+
backends: backends.to_string(),
939+
profiles: profiles.to_string(),
940+
}
941+
}
942+
943+
/// Get either the `sha` for a `try` or `master` commit or a `tag` for a
944+
/// `release`
945+
pub fn tag(&self) -> &str {
946+
match &self.commit_type {
947+
BenchmarkRequestType::Try { sha, .. } | BenchmarkRequestType::Master { sha, .. } => sha,
948+
BenchmarkRequestType::Release { tag } => tag,
949+
}
950+
}
951+
952+
pub fn pr(&self) -> Option<&u32> {
953+
match &self.commit_type {
954+
BenchmarkRequestType::Try { pr, .. } | BenchmarkRequestType::Master { pr, .. } => {
955+
Some(pr)
956+
}
957+
BenchmarkRequestType::Release { tag: _ } => None,
958+
}
959+
}
960+
961+
pub fn commit_type(&self) -> &str {
962+
self.commit_type.commit_type_str()
963+
}
964+
965+
pub fn parent_sha(&self) -> Option<&str> {
966+
match &self.commit_type {
967+
BenchmarkRequestType::Try { parent_sha, .. }
968+
| BenchmarkRequestType::Master { parent_sha, .. } => Some(parent_sha),
969+
BenchmarkRequestType::Release { tag: _ } => None,
970+
}
971+
}
972+
}

database/src/pool.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
2-
ArtifactCollection, ArtifactId, ArtifactIdNumber, CodegenBackend, CompileBenchmark, Target,
2+
ArtifactCollection, ArtifactId, ArtifactIdNumber, BenchmarkRequest, CodegenBackend,
3+
CompileBenchmark, Target,
34
};
45
use crate::{CollectionId, Index, Profile, QueuedCommit, Scenario, Step};
56
use chrono::{DateTime, Utc};
@@ -178,6 +179,10 @@ pub trait Connection: Send + Sync {
178179

179180
/// Removes all data associated with the given artifact.
180181
async fn purge_artifact(&self, aid: &ArtifactId);
182+
183+
/// Add an item to the `benchmark_requests`, if the `benchmark_request`
184+
/// exists it will be ignored
185+
async fn insert_benchmark_request(&self, benchmark_request: &BenchmarkRequest);
181186
}
182187

183188
#[async_trait::async_trait]
@@ -301,7 +306,10 @@ mod tests {
301306
use std::str::FromStr;
302307

303308
use super::*;
304-
use crate::{tests::run_db_test, Commit, CommitType, Date};
309+
use crate::{
310+
tests::{run_db_test, run_postgres_test},
311+
BenchmarkRequestStatus, Commit, CommitType, Date,
312+
};
305313

306314
/// Create a Commit
307315
fn create_commit(commit_sha: &str, time: chrono::DateTime<Utc>, r#type: CommitType) -> Commit {
@@ -370,4 +378,50 @@ mod tests {
370378
})
371379
.await;
372380
}
381+
382+
#[tokio::test]
383+
async fn insert_benchmark_requests() {
384+
run_postgres_test(|ctx| async {
385+
let db = ctx.db_client();
386+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
387+
let master_benchmark_request = BenchmarkRequest::create_master(
388+
"a-sha-1",
389+
"parent-sha-1",
390+
42,
391+
time,
392+
BenchmarkRequestStatus::WaitingForParent,
393+
"llvm",
394+
"",
395+
);
396+
397+
let try_benchmark_request = BenchmarkRequest::create_try(
398+
"b-sha-2",
399+
"parent-sha-2",
400+
32,
401+
time,
402+
BenchmarkRequestStatus::WaitingForParent,
403+
"cranelift",
404+
"",
405+
);
406+
407+
let release_benchmark_request = BenchmarkRequest::create_release(
408+
"1.8.0",
409+
time,
410+
BenchmarkRequestStatus::WaitingForParent,
411+
"cranelift,llvm",
412+
"",
413+
);
414+
415+
let db = db.connection().await;
416+
db.insert_benchmark_request(&master_benchmark_request).await;
417+
db.insert_benchmark_request(&try_benchmark_request).await;
418+
db.insert_benchmark_request(&release_benchmark_request)
419+
.await;
420+
// duplicate insert
421+
db.insert_benchmark_request(&master_benchmark_request).await;
422+
423+
Ok(ctx)
424+
})
425+
.await;
426+
}
373427
}

database/src/pool/postgres.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::pool::{Connection, ConnectionManager, ManagedConnection, Transaction};
22
use crate::{
3-
ArtifactCollection, ArtifactId, ArtifactIdNumber, Benchmark, CodegenBackend, CollectionId,
4-
Commit, CommitType, CompileBenchmark, Date, Index, Profile, QueuedCommit, Scenario, Target,
3+
ArtifactCollection, ArtifactId, ArtifactIdNumber, Benchmark, BenchmarkRequest, CodegenBackend,
4+
CollectionId, Commit, CommitType, CompileBenchmark, Date, Index, Profile, QueuedCommit,
5+
Scenario, Target,
56
};
67
use anyhow::Context as _;
78
use chrono::{DateTime, TimeZone, Utc};
@@ -285,6 +286,21 @@ static MIGRATIONS: &[&str] = &[
285286
alter table pstat_series drop constraint test_case;
286287
alter table pstat_series add constraint test_case UNIQUE(crate, profile, scenario, backend, target, metric);
287288
"#,
289+
r#"
290+
CREATE TABLE IF NOT EXISTS benchmark_request (
291+
id SERIAL PRIMARY KEY,
292+
tag TEXT NOT NULL UNIQUE,
293+
parent_sha TEXT,
294+
commit_type TEXT NOT NULL,
295+
pr INTEGER,
296+
created_at TIMESTAMPTZ NOT NULL,
297+
completed_at TIMESTAMPTZ,
298+
status TEXT NOT NULL,
299+
backends TEXT NOT NULL,
300+
profiles TEXT NOT NULL
301+
);
302+
CREATE INDEX IF NOT EXISTS benchmark_request_status_idx on benchmark_request (status) WHERE status != 'completed';
303+
"#,
288304
];
289305

290306
#[async_trait::async_trait]
@@ -1365,6 +1381,38 @@ where
13651381
.await
13661382
.unwrap();
13671383
}
1384+
1385+
async fn insert_benchmark_request(&self, benchmark_request: &BenchmarkRequest) {
1386+
self.conn()
1387+
.execute(
1388+
r#"
1389+
INSERT INTO benchmark_request(
1390+
tag,
1391+
parent_sha,
1392+
pr,
1393+
commit_type,
1394+
status,
1395+
created_at,
1396+
backends,
1397+
profiles
1398+
)
1399+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
1400+
ON CONFLICT DO NOTHING;
1401+
"#,
1402+
&[
1403+
&benchmark_request.tag(),
1404+
&benchmark_request.parent_sha(),
1405+
&benchmark_request.pr().map(|it| *it as i32),
1406+
&benchmark_request.commit_type(),
1407+
&benchmark_request.status.to_string(),
1408+
&benchmark_request.created_at,
1409+
&benchmark_request.backends,
1410+
&benchmark_request.profiles,
1411+
],
1412+
)
1413+
.await
1414+
.unwrap();
1415+
}
13681416
}
13691417

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

database/src/pool/sqlite.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::pool::{Connection, ConnectionManager, ManagedConnection, Transaction};
22
use crate::{
3-
ArtifactCollection, ArtifactId, Benchmark, CodegenBackend, CollectionId, Commit, CommitType,
4-
CompileBenchmark, Date, Profile, Target,
3+
ArtifactCollection, ArtifactId, Benchmark, BenchmarkRequest, CodegenBackend, CollectionId,
4+
Commit, CommitType, CompileBenchmark, Date, Profile, Target,
55
};
66
use crate::{ArtifactIdNumber, Index, QueuedCommit};
77
use chrono::{DateTime, TimeZone, Utc};
@@ -1252,6 +1252,10 @@ impl Connection for SqliteConnection {
12521252
)
12531253
.unwrap();
12541254
}
1255+
1256+
async fn insert_benchmark_request(&self, _benchmark_request: &BenchmarkRequest) {
1257+
panic!("Queueing for SQLite has not been implemented, if you are wanting to test the queueing functionality please use postgres. Presuming you have docker installed, at the root of the repo you can run `make start-postgres` to spin up a postgres database.");
1258+
}
12551259
}
12561260

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

database/src/tests/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ impl TestContext {
113113
}
114114
}
115115

116-
/// Runs a test against an actual database.
117-
/// Checks both Postgres and SQLite.
118-
pub(crate) async fn run_db_test<F, Fut>(f: F)
116+
/// Runs a test against an actual postgres database.
117+
pub(crate) async fn run_postgres_test<F, Fut>(f: F)
119118
where
120119
F: Fn(TestContext) -> Fut,
121120
Fut: Future<Output = anyhow::Result<TestContext>>,
@@ -138,7 +137,16 @@ where
138137
);
139138
}
140139
}
140+
}
141141

142+
/// Runs a test against an actual database.
143+
/// Checks both Postgres and SQLite.
144+
pub(crate) async fn run_db_test<F, Fut>(f: F)
145+
where
146+
F: Fn(TestContext) -> Fut + Clone,
147+
Fut: Future<Output = anyhow::Result<TestContext>>,
148+
{
149+
run_postgres_test(f.clone()).await;
142150
// SQLite
143151
eprintln!("Running test with SQLite");
144152
let ctx = TestContext::new_sqlite().await;

0 commit comments

Comments
 (0)