Skip to content

Commit f64dbe8

Browse files
committed
Logic for creating the benchmark_request queue and queuing the next
benchmark_request
1 parent c670a43 commit f64dbe8

File tree

8 files changed

+878
-40
lines changed

8 files changed

+878
-40
lines changed

database/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ csv = "1"
2626
x509-cert = { version = "0.2.5", features = ["pem"] }
2727

2828
intern = { path = "../intern" }
29+
uuid = { version = "1.16.0", features = ["v4"] }
2930

3031
[dev-dependencies]
3132
uuid = { version = "1.16.0", features = ["v4"] }

database/src/lib.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ pub mod interpolate;
1313
pub mod metric;
1414
pub mod pool;
1515
pub mod selector;
16-
17-
#[cfg(test)]
18-
mod tests;
16+
pub mod tests;
1917

2018
pub use pool::{Connection, Pool};
2119

@@ -799,7 +797,7 @@ pub struct ArtifactCollection {
799797
pub end_time: DateTime<Utc>,
800798
}
801799

802-
#[derive(Debug)]
800+
#[derive(Debug, Clone, PartialEq)]
803801
pub enum BenchmarkRequestStatus {
804802
WaitingForArtifacts,
805803
ArtifactsReady,
@@ -818,12 +816,34 @@ impl fmt::Display for BenchmarkRequestStatus {
818816
}
819817
}
820818

821-
#[derive(Debug)]
819+
impl<'a> tokio_postgres::types::FromSql<'a> for BenchmarkRequestStatus {
820+
fn from_sql(
821+
ty: &tokio_postgres::types::Type,
822+
raw: &'a [u8],
823+
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
824+
// Decode raw bytes into &str with Postgres' own text codec
825+
let s: &str = <&str as tokio_postgres::types::FromSql>::from_sql(ty, raw)?;
826+
827+
match s {
828+
"waiting_for_artifacts" => Ok(Self::WaitingForArtifacts),
829+
"artifacts_ready" => Ok(Self::ArtifactsReady),
830+
"in_progress" => Ok(Self::InProgress),
831+
"completed" => Ok(Self::Completed),
832+
other => Err(format!("unknown benchmark_request_status '{other}'").into()),
833+
}
834+
}
835+
836+
fn accepts(ty: &tokio_postgres::types::Type) -> bool {
837+
<&str as tokio_postgres::types::FromSql>::accepts(ty)
838+
}
839+
}
840+
841+
#[derive(Debug, Clone, PartialEq)]
822842
pub enum BenchmarkRequestType {
823843
/// A Try commit
824844
Try {
825845
sha: String,
826-
parent_sha: String,
846+
parent_sha: Option<String>,
827847
pr: u32,
828848
},
829849
/// A Master commit
@@ -864,7 +884,7 @@ impl fmt::Display for BenchmarkRequestType {
864884
}
865885
}
866886

867-
#[derive(Debug)]
887+
#[derive(Debug, Clone, PartialEq)]
868888
pub struct BenchmarkRequest {
869889
pub commit_type: BenchmarkRequestType,
870890
pub created_at: DateTime<Utc>,
@@ -896,7 +916,7 @@ impl BenchmarkRequest {
896916

897917
pub fn create_try(
898918
sha: &str,
899-
parent_sha: &str,
919+
parent_sha: Option<&str>,
900920
pr: u32,
901921
created_at: DateTime<Utc>,
902922
status: BenchmarkRequestStatus,
@@ -907,7 +927,7 @@ impl BenchmarkRequest {
907927
commit_type: BenchmarkRequestType::Try {
908928
pr,
909929
sha: sha.to_string(),
910-
parent_sha: parent_sha.to_string(),
930+
parent_sha: parent_sha.map(|it| it.to_string()),
911931
},
912932
created_at,
913933
completed_at: None,
@@ -964,8 +984,11 @@ impl BenchmarkRequest {
964984

965985
pub fn parent_sha(&self) -> Option<&str> {
966986
match &self.commit_type {
967-
BenchmarkRequestType::Try { parent_sha, .. }
968-
| BenchmarkRequestType::Master { parent_sha, .. } => Some(parent_sha),
987+
BenchmarkRequestType::Try { parent_sha, .. } => match parent_sha {
988+
Some(parent_sha) => Some(parent_sha),
989+
_ => None,
990+
},
991+
BenchmarkRequestType::Master { parent_sha, .. } => Some(parent_sha),
969992
BenchmarkRequestType::Release { tag: _ } => None,
970993
}
971994
}

database/src/pool.rs

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::selector::CompileTestCase;
22
use crate::{
3-
ArtifactCollection, ArtifactId, ArtifactIdNumber, BenchmarkRequest, CodegenBackend,
4-
CompileBenchmark, Target,
3+
ArtifactCollection, ArtifactId, ArtifactIdNumber, BenchmarkRequest, BenchmarkRequestStatus,
4+
CodegenBackend, CompileBenchmark, Target,
55
};
66
use crate::{CollectionId, Index, Profile, QueuedCommit, Scenario, Step};
77
use chrono::{DateTime, Utc};
@@ -195,6 +195,20 @@ pub trait Connection: Send + Sync {
195195
&self,
196196
artifact_row_id: &ArtifactIdNumber,
197197
) -> anyhow::Result<HashSet<CompileTestCase>>;
198+
199+
/// Gets the benchmark requests matching the status. Optionally provide the
200+
/// number of days from whence to search from
201+
async fn get_benchmark_requests_by_status(
202+
&self,
203+
statuses: &[BenchmarkRequestStatus],
204+
) -> anyhow::Result<Vec<BenchmarkRequest>>;
205+
206+
/// Update the status of a `benchmark_request`
207+
async fn update_benchmark_request_status(
208+
&mut self,
209+
benchmark_request: &BenchmarkRequest,
210+
benchmark_request_status: BenchmarkRequestStatus,
211+
) -> anyhow::Result<()>;
198212
}
199213

200214
#[async_trait::async_trait]
@@ -406,7 +420,7 @@ mod tests {
406420

407421
let try_benchmark_request = BenchmarkRequest::create_try(
408422
"b-sha-2",
409-
"parent-sha-2",
423+
Some("parent-sha-2"),
410424
32,
411425
time,
412426
BenchmarkRequestStatus::ArtifactsReady,
@@ -494,4 +508,100 @@ mod tests {
494508
})
495509
.await;
496510
}
511+
512+
#[tokio::test]
513+
async fn get_benchmark_requests_by_status() {
514+
// Ensure we get back the requests matching the status with no date
515+
// limit
516+
run_postgres_test(|ctx| async {
517+
let db = ctx.db_client();
518+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
519+
let master_benchmark_request = BenchmarkRequest::create_master(
520+
"a-sha-1",
521+
"parent-sha-1",
522+
42,
523+
time,
524+
BenchmarkRequestStatus::ArtifactsReady,
525+
"llvm",
526+
"",
527+
);
528+
529+
let try_benchmark_request = BenchmarkRequest::create_try(
530+
"b-sha-2",
531+
Some("parent-sha-2"),
532+
32,
533+
time,
534+
BenchmarkRequestStatus::Completed,
535+
"cranelift",
536+
"",
537+
);
538+
539+
let release_benchmark_request = BenchmarkRequest::create_release(
540+
"1.8.0",
541+
time,
542+
BenchmarkRequestStatus::ArtifactsReady,
543+
"cranelift,llvm",
544+
"",
545+
);
546+
547+
let db = db.connection().await;
548+
db.insert_benchmark_request(&master_benchmark_request).await;
549+
db.insert_benchmark_request(&try_benchmark_request).await;
550+
db.insert_benchmark_request(&release_benchmark_request)
551+
.await;
552+
553+
let requests = db
554+
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::ArtifactsReady])
555+
.await
556+
.unwrap();
557+
558+
assert_eq!(requests.len(), 2);
559+
assert_eq!(requests[0].status, BenchmarkRequestStatus::ArtifactsReady);
560+
assert_eq!(requests[1].status, BenchmarkRequestStatus::ArtifactsReady);
561+
562+
Ok(ctx)
563+
})
564+
.await;
565+
}
566+
567+
#[tokio::test]
568+
async fn update_benchmark_request_status() {
569+
// Insert one item into the database, change the status and then
570+
// get the item back out again to ensure it has changed status
571+
run_postgres_test(|ctx| async {
572+
let db = ctx.db_client();
573+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
574+
let master_benchmark_request = BenchmarkRequest::create_master(
575+
"a-sha-1",
576+
"parent-sha-1",
577+
42,
578+
time,
579+
BenchmarkRequestStatus::ArtifactsReady,
580+
"llvm",
581+
"",
582+
);
583+
584+
let mut db = db.connection().await;
585+
db.insert_benchmark_request(&master_benchmark_request).await;
586+
587+
db.update_benchmark_request_status(
588+
&master_benchmark_request,
589+
BenchmarkRequestStatus::InProgress,
590+
)
591+
.await
592+
.unwrap();
593+
594+
let requests = db
595+
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::InProgress])
596+
.await
597+
.unwrap();
598+
599+
assert_eq!(requests.len(), 1);
600+
assert_eq!(requests[0].tag(), master_benchmark_request.tag());
601+
assert_eq!(requests[0].status, BenchmarkRequestStatus::InProgress);
602+
603+
Ok(ctx)
604+
})
605+
.await;
606+
}
497607
}

0 commit comments

Comments
 (0)