Skip to content

Commit 8c99b0b

Browse files
committed
Add function for loading pending benchmark requests from the DB
1 parent 10904fc commit 8c99b0b

File tree

5 files changed

+200
-174
lines changed

5 files changed

+200
-174
lines changed

database/src/lib.rs

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use chrono::offset::TimeZone;
22
use chrono::{DateTime, Utc};
3-
use hashbrown::HashMap;
3+
use hashbrown::{HashMap, HashSet};
44
use intern::intern;
55
use serde::{Deserialize, Serialize};
66
use std::fmt;
@@ -806,18 +806,18 @@ pub enum BenchmarkRequestStatus {
806806
Completed { completed_at: DateTime<Utc> },
807807
}
808808

809-
const WAITING_FOR_ARTIFACTS_STR: &str = "waiting_for_artifacts";
810-
const ARTIFACTS_READY_STR: &str = "artifacts_ready";
811-
const IN_PROGRESS_STR: &str = "in_progress";
812-
const COMPLETED_STR: &str = "completed";
809+
const BENCHMARK_REQUEST_STATUS_WAITING_FOR_ARTIFACTS_STR: &str = "waiting_for_artifacts";
810+
const BENCHMARK_REQUEST_STATUS_ARTIFACTS_READY_STR: &str = "artifacts_ready";
811+
const BENCHMARK_REQUEST_STATUS_IN_PROGRESS_STR: &str = "in_progress";
812+
const BENCHMARK_REQUEST_STATUS_COMPLETED_STR: &str = "completed";
813813

814814
impl BenchmarkRequestStatus {
815815
pub(crate) fn as_str(&self) -> &str {
816816
match self {
817-
Self::WaitingForArtifacts => WAITING_FOR_ARTIFACTS_STR,
818-
Self::ArtifactsReady => ARTIFACTS_READY_STR,
819-
Self::InProgress => IN_PROGRESS_STR,
820-
Self::Completed { .. } => COMPLETED_STR,
817+
Self::WaitingForArtifacts => BENCHMARK_REQUEST_STATUS_WAITING_FOR_ARTIFACTS_STR,
818+
Self::ArtifactsReady => BENCHMARK_REQUEST_STATUS_ARTIFACTS_READY_STR,
819+
Self::InProgress => BENCHMARK_REQUEST_STATUS_IN_PROGRESS_STR,
820+
Self::Completed { .. } => BENCHMARK_REQUEST_STATUS_COMPLETED_STR,
821821
}
822822
}
823823

@@ -826,10 +826,10 @@ impl BenchmarkRequestStatus {
826826
completion_date: Option<DateTime<Utc>>,
827827
) -> anyhow::Result<Self> {
828828
match text {
829-
WAITING_FOR_ARTIFACTS_STR => Ok(Self::WaitingForArtifacts),
830-
ARTIFACTS_READY_STR => Ok(Self::ArtifactsReady),
831-
IN_PROGRESS_STR => Ok(Self::InProgress),
832-
COMPLETED_STR => Ok(Self::Completed {
829+
BENCHMARK_REQUEST_STATUS_WAITING_FOR_ARTIFACTS_STR => Ok(Self::WaitingForArtifacts),
830+
BENCHMARK_REQUEST_STATUS_ARTIFACTS_READY_STR => Ok(Self::ArtifactsReady),
831+
BENCHMARK_REQUEST_STATUS_IN_PROGRESS_STR => Ok(Self::InProgress),
832+
BENCHMARK_REQUEST_STATUS_COMPLETED_STR => Ok(Self::Completed {
833833
completed_at: completion_date.ok_or_else(|| {
834834
anyhow!("No completion date for a completed BenchmarkRequestStatus")
835835
})?,
@@ -930,25 +930,17 @@ impl BenchmarkRequest {
930930
}
931931
}
932932

933-
pub fn create_master(
934-
sha: &str,
935-
parent_sha: &str,
936-
pr: u32,
937-
created_at: DateTime<Utc>,
938-
status: BenchmarkRequestStatus,
939-
backends: &str,
940-
profiles: &str,
941-
) -> Self {
933+
pub fn create_master(sha: &str, parent_sha: &str, pr: u32, created_at: DateTime<Utc>) -> Self {
942934
Self {
943935
commit_type: BenchmarkRequestType::Master {
944936
pr,
945937
sha: sha.to_string(),
946938
parent_sha: parent_sha.to_string(),
947939
},
948940
created_at,
949-
status,
950-
backends: backends.to_string(),
951-
profiles: profiles.to_string(),
941+
status: BenchmarkRequestStatus::ArtifactsReady,
942+
backends: String::new(),
943+
profiles: String::new(),
952944
}
953945
}
954946

@@ -979,3 +971,24 @@ impl BenchmarkRequest {
979971
}
980972
}
981973
}
974+
975+
/// Cached information about benchmark requests in the DB
976+
/// FIXME: only store non-try requests here
977+
pub struct BenchmarkRequestIndex {
978+
/// Tags (SHA or release name) of all known benchmark requests
979+
all: HashSet<String>,
980+
/// Tags (SHA or release name) of all benchmark requests in the completed status
981+
completed: HashSet<String>,
982+
}
983+
984+
impl BenchmarkRequestIndex {
985+
/// Do we already have a benchmark request for the passed `tag`?
986+
pub fn contains_tag(&self, tag: &str) -> bool {
987+
self.all.contains(tag)
988+
}
989+
990+
/// Return tags of already completed benchmark requests.
991+
pub fn completed_requests(&self) -> &HashSet<String> {
992+
&self.completed
993+
}
994+
}

database/src/pool.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
ArtifactCollection, ArtifactId, ArtifactIdNumber, BenchmarkRequest, BenchmarkRequestStatus,
3-
CodegenBackend, CompileBenchmark, Target,
2+
ArtifactCollection, ArtifactId, ArtifactIdNumber, BenchmarkRequest, BenchmarkRequestIndex,
3+
BenchmarkRequestStatus, CodegenBackend, CompileBenchmark, Target,
44
};
55
use crate::{CollectionId, Index, Profile, QueuedCommit, Scenario, Step};
66
use chrono::{DateTime, Utc};
@@ -187,18 +187,18 @@ pub trait Connection: Send + Sync {
187187
benchmark_request: &BenchmarkRequest,
188188
) -> anyhow::Result<()>;
189189

190-
/// Gets the benchmark requests matching the status. Optionally provide the
191-
/// number of days from whence to search from
192-
async fn get_benchmark_requests_by_status(
193-
&self,
194-
statuses: &[BenchmarkRequestStatus],
195-
) -> anyhow::Result<Vec<BenchmarkRequest>>;
190+
/// Load all known benchmark request SHAs and all completed benchmark requests.
191+
async fn load_benchmark_request_index(&self) -> anyhow::Result<BenchmarkRequestIndex>;
192+
193+
/// Load all pending benchmark requests, i.e. those that have artifacts ready, but haven't
194+
/// been completed yet. Pending statuses are `ArtifactsReady` and `InProgress`.
195+
async fn load_pending_benchmark_requests(&self) -> anyhow::Result<Vec<BenchmarkRequest>>;
196196

197197
/// Update the status of a `benchmark_request`
198198
async fn update_benchmark_request_status(
199199
&mut self,
200-
benchmark_request: &BenchmarkRequest,
201-
benchmark_request_status: BenchmarkRequestStatus,
200+
request: &BenchmarkRequest,
201+
status: BenchmarkRequestStatus,
202202
) -> anyhow::Result<()>;
203203

204204
/// Update a Try commit to have a `sha` and `parent_sha`. Will update the
@@ -420,7 +420,7 @@ mod tests {
420420
"",
421421
);
422422

423-
let try_benchmark_request = BenchmarkRequest::create_try(
423+
let try_benchmark_request = BenchmarkRequest::create_try_without_artifacts(
424424
Some("b-sha-2"),
425425
Some("parent-sha-2"),
426426
32,
@@ -474,7 +474,7 @@ mod tests {
474474
"",
475475
);
476476

477-
let try_benchmark_request = BenchmarkRequest::create_try(
477+
let try_benchmark_request = BenchmarkRequest::create_try_without_artifacts(
478478
Some("b-sha-2"),
479479
Some("parent-sha-2"),
480480
32,
@@ -502,7 +502,7 @@ mod tests {
502502
.unwrap();
503503

504504
let requests = db
505-
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::ArtifactsReady])
505+
.load_benchmark_request_index(&[BenchmarkRequestStatus::ArtifactsReady])
506506
.await
507507
.unwrap();
508508

@@ -545,7 +545,7 @@ mod tests {
545545
.unwrap();
546546

547547
let requests = db
548-
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::InProgress])
548+
.load_benchmark_request_index(&[BenchmarkRequestStatus::InProgress])
549549
.await
550550
.unwrap();
551551

@@ -566,7 +566,7 @@ mod tests {
566566
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
567567
let pr = 42;
568568

569-
let try_benchmark_request = BenchmarkRequest::create_try(
569+
let try_benchmark_request = BenchmarkRequest::create_try_without_artifacts(
570570
None,
571571
None,
572572
pr,
@@ -582,7 +582,7 @@ mod tests {
582582
.await
583583
.unwrap();
584584
let requests = db
585-
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::ArtifactsReady])
585+
.load_benchmark_request_index(&[BenchmarkRequestStatus::ArtifactsReady])
586586
.await
587587
.unwrap();
588588

@@ -604,7 +604,7 @@ mod tests {
604604
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
605605
let pr = 42;
606606

607-
let completed_try = BenchmarkRequest::create_try(
607+
let completed_try = BenchmarkRequest::create_try_without_artifacts(
608608
Some("sha-2"),
609609
Some("p-sha-1"),
610610
pr,
@@ -615,7 +615,7 @@ mod tests {
615615
);
616616
db.insert_benchmark_request(&completed_try).await.unwrap();
617617

618-
let try_benchmark_request = BenchmarkRequest::create_try(
618+
let try_benchmark_request = BenchmarkRequest::create_try_without_artifacts(
619619
None,
620620
None,
621621
pr,
@@ -638,7 +638,7 @@ mod tests {
638638
.unwrap();
639639

640640
let requests = db
641-
.get_benchmark_requests_by_status(&[
641+
.load_benchmark_request_index(&[
642642
BenchmarkRequestStatus::WaitingForArtifacts,
643643
BenchmarkRequestStatus::ArtifactsReady,
644644
BenchmarkRequestStatus::InProgress,

0 commit comments

Comments
 (0)