Skip to content

Commit b68cdfc

Browse files
committed
Logic for creating the benchmark_request queue and queuing the next
benchmark_request
1 parent b34ad29 commit b68cdfc

File tree

9 files changed

+879
-41
lines changed

9 files changed

+879
-41
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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,6 +1,6 @@
11
use crate::{
2-
ArtifactCollection, ArtifactId, ArtifactIdNumber, BenchmarkRequest, CodegenBackend,
3-
CompileBenchmark, Target,
2+
ArtifactCollection, ArtifactId, ArtifactIdNumber, BenchmarkRequest, BenchmarkRequestStatus,
3+
CodegenBackend, CompileBenchmark, Target,
44
};
55
use crate::{CollectionId, Index, Profile, QueuedCommit, Scenario, Step};
66
use chrono::{DateTime, Utc};
@@ -183,6 +183,20 @@ pub trait Connection: Send + Sync {
183183
/// Add an item to the `benchmark_requests`, if the `benchmark_request`
184184
/// exists it will be ignored
185185
async fn insert_benchmark_request(&self, benchmark_request: &BenchmarkRequest);
186+
187+
/// Gets the benchmark requests matching the status. Optionally provide the
188+
/// number of days from whence to search from
189+
async fn get_benchmark_requests_by_status(
190+
&self,
191+
statuses: &[BenchmarkRequestStatus],
192+
) -> anyhow::Result<Vec<BenchmarkRequest>>;
193+
194+
/// Update the status of a `benchmark_request`
195+
async fn update_benchmark_request_status(
196+
&mut self,
197+
benchmark_request: &BenchmarkRequest,
198+
benchmark_request_status: BenchmarkRequestStatus,
199+
) -> anyhow::Result<()>;
186200
}
187201

188202
#[async_trait::async_trait]
@@ -396,7 +410,7 @@ mod tests {
396410

397411
let try_benchmark_request = BenchmarkRequest::create_try(
398412
"b-sha-2",
399-
"parent-sha-2",
413+
Some("parent-sha-2"),
400414
32,
401415
time,
402416
BenchmarkRequestStatus::ArtifactsReady,
@@ -424,4 +438,100 @@ mod tests {
424438
})
425439
.await;
426440
}
441+
442+
#[tokio::test]
443+
async fn get_benchmark_requests_by_status() {
444+
// Ensure we get back the requests matching the status with no date
445+
// limit
446+
run_postgres_test(|ctx| async {
447+
let db = ctx.db_client();
448+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
449+
let master_benchmark_request = BenchmarkRequest::create_master(
450+
"a-sha-1",
451+
"parent-sha-1",
452+
42,
453+
time,
454+
BenchmarkRequestStatus::ArtifactsReady,
455+
"llvm",
456+
"",
457+
);
458+
459+
let try_benchmark_request = BenchmarkRequest::create_try(
460+
"b-sha-2",
461+
Some("parent-sha-2"),
462+
32,
463+
time,
464+
BenchmarkRequestStatus::Completed,
465+
"cranelift",
466+
"",
467+
);
468+
469+
let release_benchmark_request = BenchmarkRequest::create_release(
470+
"1.8.0",
471+
time,
472+
BenchmarkRequestStatus::ArtifactsReady,
473+
"cranelift,llvm",
474+
"",
475+
);
476+
477+
let db = db.connection().await;
478+
db.insert_benchmark_request(&master_benchmark_request).await;
479+
db.insert_benchmark_request(&try_benchmark_request).await;
480+
db.insert_benchmark_request(&release_benchmark_request)
481+
.await;
482+
483+
let requests = db
484+
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::ArtifactsReady])
485+
.await
486+
.unwrap();
487+
488+
assert_eq!(requests.len(), 2);
489+
assert_eq!(requests[0].status, BenchmarkRequestStatus::ArtifactsReady);
490+
assert_eq!(requests[1].status, BenchmarkRequestStatus::ArtifactsReady);
491+
492+
Ok(ctx)
493+
})
494+
.await;
495+
}
496+
497+
#[tokio::test]
498+
async fn update_benchmark_request_status() {
499+
// Insert one item into the database, change the status and then
500+
// get the item back out again to ensure it has changed status
501+
run_postgres_test(|ctx| async {
502+
let db = ctx.db_client();
503+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
504+
let master_benchmark_request = BenchmarkRequest::create_master(
505+
"a-sha-1",
506+
"parent-sha-1",
507+
42,
508+
time,
509+
BenchmarkRequestStatus::ArtifactsReady,
510+
"llvm",
511+
"",
512+
);
513+
514+
let mut db = db.connection().await;
515+
db.insert_benchmark_request(&master_benchmark_request).await;
516+
517+
db.update_benchmark_request_status(
518+
&master_benchmark_request,
519+
BenchmarkRequestStatus::InProgress,
520+
)
521+
.await
522+
.unwrap();
523+
524+
let requests = db
525+
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::InProgress])
526+
.await
527+
.unwrap();
528+
529+
assert_eq!(requests.len(), 1);
530+
assert_eq!(requests[0].tag(), master_benchmark_request.tag());
531+
assert_eq!(requests[0].status, BenchmarkRequestStatus::InProgress);
532+
533+
Ok(ctx)
534+
})
535+
.await;
536+
}
427537
}

0 commit comments

Comments
 (0)