Skip to content

Chore; Missed database error handling & small refactor #2198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,17 +805,23 @@ pub enum BenchmarkRequestStatus {
Completed,
}

impl fmt::Display for BenchmarkRequestStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl BenchmarkRequestStatus {
pub fn as_str(&self) -> &str {
match self {
BenchmarkRequestStatus::WaitingForArtifacts => write!(f, "waiting_for_artifacts"),
BenchmarkRequestStatus::ArtifactsReady => write!(f, "artifacts_ready"),
BenchmarkRequestStatus::InProgress => write!(f, "in_progress"),
BenchmarkRequestStatus::Completed => write!(f, "completed"),
BenchmarkRequestStatus::WaitingForArtifacts => "waiting_for_artifacts",
BenchmarkRequestStatus::ArtifactsReady => "artifacts_ready",
BenchmarkRequestStatus::InProgress => "in_progress",
BenchmarkRequestStatus::Completed => "completed",
}
}
}

impl fmt::Display for BenchmarkRequestStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}

impl<'a> tokio_postgres::types::FromSql<'a> for BenchmarkRequestStatus {
fn from_sql(
ty: &tokio_postgres::types::Type,
Expand All @@ -825,10 +831,10 @@ impl<'a> tokio_postgres::types::FromSql<'a> for BenchmarkRequestStatus {
let s: &str = <&str as tokio_postgres::types::FromSql>::from_sql(ty, raw)?;

match s {
"waiting_for_artifacts" => Ok(Self::WaitingForArtifacts),
"artifacts_ready" => Ok(Self::ArtifactsReady),
"in_progress" => Ok(Self::InProgress),
"completed" => Ok(Self::Completed),
x if x == Self::WaitingForArtifacts.as_str() => Ok(Self::WaitingForArtifacts),
x if x == Self::ArtifactsReady.as_str() => Ok(Self::ArtifactsReady),
x if x == Self::InProgress.as_str() => Ok(Self::InProgress),
x if x == Self::Completed.as_str() => Ok(Self::Completed),
other => Err(format!("unknown benchmark_request_status '{other}'").into()),
}
}
Expand Down Expand Up @@ -856,30 +862,12 @@ pub enum BenchmarkRequestType {
Release { tag: String },
}

impl BenchmarkRequestType {
pub fn commit_type_str(&self) -> &str {
match self {
BenchmarkRequestType::Try {
sha: _,
parent_sha: _,
pr: _,
} => "try",
BenchmarkRequestType::Master {
sha: _,
parent_sha: _,
pr: _,
} => "master",
BenchmarkRequestType::Release { tag: _ } => "release",
}
}
}

impl fmt::Display for BenchmarkRequestType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BenchmarkRequestType::Try { .. } => write!(f, "try"),
BenchmarkRequestType::Master { .. } => write!(f, "master"),
BenchmarkRequestType::Release { tag: _ } => write!(f, "release"),
BenchmarkRequestType::Release { .. } => write!(f, "release"),
}
}
}
Expand Down Expand Up @@ -975,19 +963,15 @@ impl BenchmarkRequest {
BenchmarkRequestType::Try { pr, .. } | BenchmarkRequestType::Master { pr, .. } => {
Some(pr)
}
BenchmarkRequestType::Release { tag: _ } => None,
BenchmarkRequestType::Release { .. } => None,
}
}

pub fn commit_type(&self) -> &str {
self.commit_type.commit_type_str()
}

pub fn parent_sha(&self) -> Option<&str> {
match &self.commit_type {
BenchmarkRequestType::Try { parent_sha, .. } => parent_sha.as_deref(),
BenchmarkRequestType::Master { parent_sha, .. } => Some(parent_sha),
BenchmarkRequestType::Release { tag: _ } => None,
BenchmarkRequestType::Release { .. } => None,
}
}
}
53 changes: 39 additions & 14 deletions database/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,11 @@ pub trait Connection: Send + Sync {
async fn purge_artifact(&self, aid: &ArtifactId);

/// Add an item to the `benchmark_requests`, if the `benchmark_request`
/// exists it will be ignored
async fn insert_benchmark_request(&self, benchmark_request: &BenchmarkRequest);
/// exists an Error will be returned
async fn insert_benchmark_request(
&self,
benchmark_request: &BenchmarkRequest,
) -> anyhow::Result<()>;

/// Gets the benchmark requests matching the status. Optionally provide the
/// number of days from whence to search from
Expand Down Expand Up @@ -436,12 +439,20 @@ mod tests {
);

let db = db.connection().await;
db.insert_benchmark_request(&master_benchmark_request).await;
db.insert_benchmark_request(&try_benchmark_request).await;
db.insert_benchmark_request(&master_benchmark_request)
.await
.unwrap();
db.insert_benchmark_request(&try_benchmark_request)
.await
.unwrap();
db.insert_benchmark_request(&release_benchmark_request)
.await;
.await
.unwrap();
// duplicate insert
db.insert_benchmark_request(&master_benchmark_request).await;
assert!(db
.insert_benchmark_request(&master_benchmark_request)
.await
.is_err());

Ok(ctx)
})
Expand Down Expand Up @@ -484,10 +495,15 @@ mod tests {
);

let db = db.connection().await;
db.insert_benchmark_request(&master_benchmark_request).await;
db.insert_benchmark_request(&try_benchmark_request).await;
db.insert_benchmark_request(&master_benchmark_request)
.await
.unwrap();
db.insert_benchmark_request(&try_benchmark_request)
.await
.unwrap();
db.insert_benchmark_request(&release_benchmark_request)
.await;
.await
.unwrap();

let requests = db
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::ArtifactsReady])
Expand Down Expand Up @@ -521,7 +537,9 @@ mod tests {
);

let mut db = db.connection().await;
db.insert_benchmark_request(&master_benchmark_request).await;
db.insert_benchmark_request(&master_benchmark_request)
.await
.unwrap();

db.update_benchmark_request_status(
&master_benchmark_request,
Expand Down Expand Up @@ -561,7 +579,9 @@ mod tests {
"cranelift",
"",
);
db.insert_benchmark_request(&try_benchmark_request).await;
db.insert_benchmark_request(&try_benchmark_request)
.await
.unwrap();
db.attach_shas_to_try_benchmark_request(pr, "foo", "bar")
.await
.unwrap();
Expand Down Expand Up @@ -597,7 +617,7 @@ mod tests {
"cranelift",
"",
);
db.insert_benchmark_request(&completed_try).await;
db.insert_benchmark_request(&completed_try).await.unwrap();

let try_benchmark_request = BenchmarkRequest::create_try(
None,
Expand All @@ -609,9 +629,14 @@ mod tests {
"",
);
// deliberately insert twice
db.insert_benchmark_request(&try_benchmark_request).await;
db.insert_benchmark_request(&try_benchmark_request)
.await
.unwrap();
// this one should fail
db.insert_benchmark_request(&try_benchmark_request).await;
assert!(db
.insert_benchmark_request(&try_benchmark_request)
.await
.is_err());
db.attach_shas_to_try_benchmark_request(pr, "foo", "bar")
.await
.unwrap();
Expand Down
15 changes: 9 additions & 6 deletions database/src/pool/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,10 @@ where
.unwrap();
}

async fn insert_benchmark_request(&self, benchmark_request: &BenchmarkRequest) {
async fn insert_benchmark_request(
&self,
benchmark_request: &BenchmarkRequest,
) -> anyhow::Result<()> {
self.conn()
.execute(
r#"
Expand All @@ -1401,22 +1404,22 @@ where
backends,
profiles
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT DO NOTHING;
VALUES ($1, $2, $3, $4, $5, $6, $7, $8);
"#,
&[
&benchmark_request.tag(),
&benchmark_request.parent_sha(),
&benchmark_request.pr().map(|it| *it as i32),
&benchmark_request.commit_type(),
&benchmark_request.status.to_string(),
&benchmark_request.commit_type,
&benchmark_request.status,
&benchmark_request.created_at,
&benchmark_request.backends,
&benchmark_request.profiles,
],
)
.await
.unwrap();
.context("Failed to insert benchmark request")?;
Ok(())
}

async fn get_benchmark_requests_by_status(
Expand Down
5 changes: 4 additions & 1 deletion database/src/pool/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,10 @@ impl Connection for SqliteConnection {
.unwrap();
}

async fn insert_benchmark_request(&self, _benchmark_request: &BenchmarkRequest) {
async fn insert_benchmark_request(
&self,
_benchmark_request: &BenchmarkRequest,
) -> anyhow::Result<()> {
no_queue_implementation_abort!()
}

Expand Down
10 changes: 7 additions & 3 deletions site/src/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ async fn create_benchmark_request_master_commits(
"",
"",
);
conn.insert_benchmark_request(&benchmark).await;
if let Err(e) = conn.insert_benchmark_request(&benchmark).await {
log::error!("Failed to insert master benchmark request: {}", e);
}
}
}
Ok(())
Expand Down Expand Up @@ -130,7 +132,9 @@ async fn create_benchmark_request_releases(
"",
"",
);
conn.insert_benchmark_request(&release_request).await;
if let Err(e) = conn.insert_benchmark_request(&release_request).await {
log::error!("Failed to insert release benchmark request: {}", e);
}
}
}
Ok(())
Expand Down Expand Up @@ -401,7 +405,7 @@ mod tests {
requests: &[BenchmarkRequest],
) {
for request in requests {
conn.insert_benchmark_request(&request).await;
conn.insert_benchmark_request(&request).await.unwrap();
}
}

Expand Down
4 changes: 3 additions & 1 deletion site/src/request_handlers/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ async fn queue_partial_try_benchmark_request(
backends,
"",
);
conn.insert_benchmark_request(&try_request).await;
if let Err(e) = conn.insert_benchmark_request(&try_request).await {
log::error!("Failed to insert try benchmark request: {}", e);
}
}
}

Expand Down
Loading