Skip to content

Commit 5053b1d

Browse files
committed
Make BenchmarkRequest fields private
To ensure that they can't be messed with from the outside.
1 parent 8c99b0b commit 5053b1d

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

database/src/lib.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ pub struct ArtifactCollection {
798798
pub end_time: DateTime<Utc>,
799799
}
800800

801-
#[derive(Debug, Clone, PartialEq)]
801+
#[derive(Debug, Copy, Clone, PartialEq)]
802802
pub enum BenchmarkRequestStatus {
803803
WaitingForArtifacts,
804804
ArtifactsReady,
@@ -886,31 +886,28 @@ impl fmt::Display for BenchmarkRequestType {
886886

887887
#[derive(Debug, Clone, PartialEq)]
888888
pub struct BenchmarkRequest {
889-
pub commit_type: BenchmarkRequestType,
890-
pub created_at: DateTime<Utc>,
891-
pub status: BenchmarkRequestStatus,
892-
pub backends: String,
893-
pub profiles: String,
889+
commit_type: BenchmarkRequestType,
890+
created_at: DateTime<Utc>,
891+
status: BenchmarkRequestStatus,
892+
backends: String,
893+
profiles: String,
894894
}
895895

896896
impl BenchmarkRequest {
897-
pub fn create_release(
898-
tag: &str,
899-
created_at: DateTime<Utc>,
900-
status: BenchmarkRequestStatus,
901-
) -> Self {
897+
/// Create a release benchmark request that is in the `ArtifactsReady` status.
898+
pub fn create_release(tag: &str, created_at: DateTime<Utc>) -> Self {
902899
Self {
903900
commit_type: BenchmarkRequestType::Release {
904901
tag: tag.to_string(),
905902
},
906903
created_at,
907-
status,
904+
status: BenchmarkRequestStatus::ArtifactsReady,
908905
backends: String::new(),
909906
profiles: String::new(),
910907
}
911908
}
912909

913-
/// Create a try request that is waiting for artifacts
910+
/// Create a try request that is in the `WaitingForArtifacts` status.
914911
pub fn create_try_without_artifacts(
915912
pr: u32,
916913
created_at: DateTime<Utc>,
@@ -930,6 +927,7 @@ impl BenchmarkRequest {
930927
}
931928
}
932929

930+
/// Create a master benchmark request that is in the `ArtifactsReady` status.
933931
pub fn create_master(sha: &str, parent_sha: &str, pr: u32, created_at: DateTime<Utc>) -> Self {
934932
Self {
935933
commit_type: BenchmarkRequestType::Master {
@@ -970,6 +968,26 @@ impl BenchmarkRequest {
970968
BenchmarkRequestType::Release { .. } => None,
971969
}
972970
}
971+
972+
pub fn status(&self) -> BenchmarkRequestStatus {
973+
self.status
974+
}
975+
976+
pub fn created_at(&self) -> DateTime<Utc> {
977+
self.created_at
978+
}
979+
980+
pub fn is_master(&self) -> bool {
981+
matches!(self.commit_type, BenchmarkRequestType::Master { .. })
982+
}
983+
984+
pub fn is_try(&self) -> bool {
985+
matches!(self.commit_type, BenchmarkRequestType::Try { .. })
986+
}
987+
988+
pub fn is_release(&self) -> bool {
989+
matches!(self.commit_type, BenchmarkRequestType::Release { .. })
990+
}
973991
}
974992

975993
/// Cached information about benchmark requests in the DB

site/src/job_queue.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use std::{
66

77
use crate::load::{partition_in_place, SiteCtxt};
88
use chrono::{DateTime, NaiveDate, Utc};
9-
use database::{
10-
BenchmarkRequest, BenchmarkRequestIndex, BenchmarkRequestStatus, BenchmarkRequestType,
11-
};
9+
use database::{BenchmarkRequest, BenchmarkRequestIndex, BenchmarkRequestStatus};
1210
use hashbrown::HashSet;
1311
use parking_lot::RwLock;
1412
use regex::Regex;
@@ -126,11 +124,7 @@ async fn create_benchmark_request_releases(
126124

127125
for (name, date_time) in releases {
128126
if date_time >= cutoff && !index.contains_tag(&name) {
129-
let release_request = BenchmarkRequest::create_release(
130-
&name,
131-
date_time,
132-
BenchmarkRequestStatus::ArtifactsReady,
133-
);
127+
let release_request = BenchmarkRequest::create_release(&name, date_time);
134128
if let Err(error) = conn.insert_benchmark_request(&release_request).await {
135129
log::error!("Failed to insert release benchmark request: {error}");
136130
}
@@ -147,11 +141,7 @@ fn sort_benchmark_requests(index: &BenchmarkRequestIndex, request_queue: &mut [B
147141
// Ensure all the items are ready to be sorted, if they are not this is
148142
// undefined behaviour
149143
assert!(request_queue.iter().all(|bmr| {
150-
bmr.status == BenchmarkRequestStatus::ArtifactsReady
151-
&& matches!(
152-
bmr.commit_type,
153-
BenchmarkRequestType::Master { .. } | BenchmarkRequestType::Try { .. }
154-
)
144+
bmr.status() == BenchmarkRequestStatus::ArtifactsReady && (bmr.is_master() || bmr.is_try())
155145
}));
156146

157147
let mut finished = 0;
@@ -180,15 +170,11 @@ fn sort_benchmark_requests(index: &BenchmarkRequestIndex, request_queue: &mut [B
180170
let level = &mut request_queue[finished..][..level_len];
181171
level.sort_unstable_by_key(|bmr| {
182172
(
183-
// Pr number takes priority
173+
// PR number takes priority
184174
*bmr.pr().unwrap_or(&0),
185175
// Order master commits before try commits
186-
match bmr.commit_type {
187-
BenchmarkRequestType::Try { .. } => 1,
188-
BenchmarkRequestType::Master { .. } => 0,
189-
BenchmarkRequestType::Release { .. } => unreachable!(),
190-
},
191-
bmr.created_at,
176+
if bmr.is_master() { 0 } else { 1 },
177+
bmr.created_at(),
192178
)
193179
});
194180
for c in level {
@@ -248,21 +234,21 @@ pub async fn build_queue(
248234
let mut pending = conn.load_pending_benchmark_requests().await?;
249235

250236
// The queue starts with in progress
251-
let mut queue: Vec<BenchmarkRequest> = pending
252-
.extract_if_stable(|request| matches!(request.status, BenchmarkRequestStatus::InProgress));
237+
let mut queue: Vec<BenchmarkRequest> = pending.extract_if_stable(|request| {
238+
matches!(request.status(), BenchmarkRequestStatus::InProgress)
239+
});
253240

254241
// We sort the in-progress ones based on the started date
255-
queue.sort_unstable_by(|a, b| a.created_at.cmp(&b.created_at));
242+
queue.sort_unstable_by(|a, b| a.created_at().cmp(&b.created_at()));
256243

257244
// Add release artifacts ordered by the release tag (1.87.0 before 1.88.0) and `created_at`.
258-
let mut release_artifacts: Vec<BenchmarkRequest> = pending.extract_if_stable(|request| {
259-
matches!(request.commit_type, BenchmarkRequestType::Release { .. })
260-
});
245+
let mut release_artifacts: Vec<BenchmarkRequest> =
246+
pending.extract_if_stable(|request| request.is_release());
261247

262248
release_artifacts.sort_unstable_by(|a, b| {
263249
a.tag()
264250
.cmp(&b.tag())
265-
.then_with(|| a.created_at.cmp(&b.created_at))
251+
.then_with(|| a.created_at().cmp(&b.created_at()))
266252
});
267253

268254
queue.append(&mut release_artifacts);

0 commit comments

Comments
 (0)