Skip to content

Commit e9ed492

Browse files
committed
Store benchmark index in memory, and reload it when a master/release benchmark index is added to the DB
1 parent b420aaf commit e9ed492

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

site/src/job_queue/mod.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ pub fn run_new_queue() -> bool {
2020
}
2121

2222
/// Store the latest master commits or do nothing if all of them are
23-
/// already in the database
23+
/// already in the database.
24+
/// Returns `true` if at least one benchmark request was inserted.
2425
async fn create_benchmark_request_master_commits(
2526
ctxt: &SiteCtxt,
2627
conn: &dyn database::pool::Connection,
2728
index: &BenchmarkRequestIndex,
28-
) -> anyhow::Result<()> {
29+
) -> anyhow::Result<bool> {
2930
let now = Utc::now();
3031

3132
let master_commits = ctxt.get_master_commits();
@@ -38,6 +39,7 @@ async fn create_benchmark_request_master_commits(
3839
// TODO; delete at some point in the future
3940
let cutoff: chrono::DateTime<Utc> = chrono::DateTime::from_str("2025-08-27T00:00:00.000Z")?;
4041

42+
let mut inserted = false;
4143
for master_commit in master_commits {
4244
// We don't want to add masses of obsolete data
4345
if master_commit.time >= cutoff && !index.contains_tag(&master_commit.sha) {
@@ -51,18 +53,21 @@ async fn create_benchmark_request_master_commits(
5153
log::info!("Inserting master benchmark request {benchmark:?}");
5254
if let Err(error) = conn.insert_benchmark_request(&benchmark).await {
5355
log::error!("Failed to insert master benchmark request: {error:?}");
56+
} else {
57+
inserted = true;
5458
}
5559
}
5660
}
57-
Ok(())
61+
Ok(inserted)
5862
}
5963

6064
/// Store the latest release commits or do nothing if all of them are
6165
/// already in the database
66+
/// Returns `true` if at least one benchmark request was inserted.
6267
async fn create_benchmark_request_releases(
6368
conn: &dyn database::pool::Connection,
6469
index: &BenchmarkRequestIndex,
65-
) -> anyhow::Result<()> {
70+
) -> anyhow::Result<bool> {
6671
let releases: String = reqwest::get("https://static.rust-lang.org/manifests.txt")
6772
.await?
6873
.text()
@@ -76,16 +81,19 @@ async fn create_benchmark_request_releases(
7681
.filter_map(parse_release_string)
7782
.take(20);
7883

84+
let mut inserted = false;
7985
for (name, commit_date) in releases {
8086
if commit_date >= cutoff && !index.contains_tag(&name) {
8187
let release_request = BenchmarkRequest::create_release(&name, commit_date);
8288
log::info!("Inserting release benchmark request {release_request:?}");
8389
if let Err(error) = conn.insert_benchmark_request(&release_request).await {
8490
log::error!("Failed to insert release benchmark request: {error}");
91+
} else {
92+
inserted = true;
8593
}
8694
}
8795
}
88-
Ok(())
96+
Ok(inserted)
8997
}
9098

9199
/// Sorts try and master requests that are in the `ArtifactsReady` status and return them in the
@@ -285,15 +293,23 @@ async fn process_benchmark_requests(
285293
async fn cron_enqueue_jobs(site_ctxt: &SiteCtxt) -> anyhow::Result<()> {
286294
let mut conn = site_ctxt.conn().await;
287295

288-
let index = conn.load_benchmark_request_index().await?;
296+
let index = site_ctxt.known_benchmark_requests.load();
289297

298+
let mut requests_inserted = false;
290299
// Put the master commits into the `benchmark_requests` queue
291-
create_benchmark_request_master_commits(site_ctxt, &*conn, &index).await?;
300+
requests_inserted |= create_benchmark_request_master_commits(site_ctxt, &*conn, &index).await?;
292301
// Put the releases into the `benchmark_requests` queue
293-
create_benchmark_request_releases(&*conn, &index).await?;
302+
requests_inserted |= create_benchmark_request_releases(&*conn, &index).await?;
294303
// Enqueue waiting requests and try to complete in-progress ones
295304
process_benchmark_requests(&mut *conn).await?;
296305

306+
// If some change happened, reload the benchmark request index
307+
if requests_inserted {
308+
site_ctxt
309+
.known_benchmark_requests
310+
.store(Arc::new(conn.load_benchmark_request_index().await?));
311+
}
312+
297313
Ok(())
298314
}
299315

site/src/load.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use serde::{Deserialize, Serialize};
1414
use crate::self_profile::SelfProfileCache;
1515
use collector::compile::benchmark::category::Category;
1616
use collector::{Bound, MasterCommit};
17-
use database::Pool;
1817
pub use database::{ArtifactId, Benchmark, Commit};
18+
use database::{BenchmarkRequestIndex, Pool};
1919
use database::{CommitType, Date};
2020

2121
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
@@ -128,6 +128,8 @@ pub struct SiteCtxt {
128128
pub landing_page: ArcSwap<Option<Arc<crate::api::graphs::Response>>>,
129129
/// Index of various common queries
130130
pub index: ArcSwap<database::Index>,
131+
/// Index of all benchmark requests that we have in the DB
132+
pub known_benchmark_requests: ArcSwap<BenchmarkRequestIndex>,
131133
/// Cached master-branch Rust commits
132134
pub master_commits: Arc<ArcSwap<MasterCommitCache>>, // outer Arc enables mutation in background task
133135
/// Cache for self profile data
@@ -160,6 +162,7 @@ impl SiteCtxt {
160162

161163
let mut conn = pool.connection().await;
162164
let index = database::Index::load(&mut *conn).await;
165+
let benchmark_request_index = conn.load_benchmark_request_index().await?;
163166

164167
let config = if let Ok(s) = fs::read_to_string("site-config.toml") {
165168
toml::from_str(&s)?
@@ -177,6 +180,7 @@ impl SiteCtxt {
177180
Ok(Self {
178181
config,
179182
index: ArcSwap::new(Arc::new(index)),
183+
known_benchmark_requests: ArcSwap::new(Arc::new(benchmark_request_index)),
180184
master_commits: Arc::new(ArcSwap::new(Arc::new(master_commits))),
181185
pool,
182186
landing_page: ArcSwap::new(Arc::new(None)),

0 commit comments

Comments
 (0)