Skip to content

Commit

Permalink
Merge pull request #752 from Mark-Simulacrum/debug-crater
Browse files Browse the repository at this point in the history
Reduce scans of experiment_crates to find next crate
  • Loading branch information
Mark-Simulacrum authored Dec 8, 2024
2 parents 3305c70 + 5e54664 commit 6ba3a90
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod tokens;
mod try_builds;

use crate::config::Config;
use crate::crates::Crate;
use crate::db::Database;
use crate::prelude::*;
use crate::server::agents::Agents;
Expand All @@ -20,8 +21,10 @@ use crate::server::tokens::{BotTokens, Tokens};
use http::{header::HeaderValue, Response};
use hyper::Body;
use metrics::Metrics;
use std::collections::VecDeque;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::time::Instant;
use warp::Filter;

lazy_static! {
Expand All @@ -47,6 +50,7 @@ pub struct Data {
pub db: Database,
pub reports_worker: reports::ReportsWorker,
pub record_progress_worker: routes::agent::RecordProgressThread,
pub uncompleted_cache: Arc<Mutex<VecDeque<(Instant, Crate)>>>,
pub acl: ACL,
pub metrics: Metrics,
}
Expand Down Expand Up @@ -94,6 +98,7 @@ pub fn run(config: Config, bind: SocketAddr) -> Fallible<()> {
reports_worker: reports::ReportsWorker::new(),
acl,
metrics,
uncompleted_cache: Arc::new(Mutex::new(VecDeque::new())),
};

let mutex = Arc::new(Mutex::new(data.clone()));
Expand Down
34 changes: 28 additions & 6 deletions src/server/routes/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,46 @@ fn endpoint_next_experiment(
Ok(ApiResponse::Success { result }.into_response()?)
}

fn endpoint_next_crate(
fn endpoint_next_crate_inner(
experiment: String,
data: Arc<Data>,
_auth: AuthDetails,
) -> Fallible<Response<Body>> {
) -> Fallible<Option<crate::crates::Crate>> {
let result: Option<crate::crates::Crate> =
if let Some(ex) = Experiment::get(&data.db, &experiment)? {
let mut crates = ex.get_uncompleted_crates(&data.db, Some(1))?;
while let Some(next) = data.uncompleted_cache.lock().unwrap().pop_front() {
if next.0.elapsed() <= std::time::Duration::from_secs(60) {
return Ok(Some(next.1));
}
}

let mut crates = ex.get_uncompleted_crates(&data.db, Some(100))?;
if crates.is_empty() {
None
} else {
Some(crates.remove(0))
let now = std::time::Instant::now();
let ret = crates.pop().unwrap();
data.uncompleted_cache
.lock()
.unwrap()
.extend(crates.into_iter().map(|c| (now, c)));
Some(ret)
}
} else {
None
};

Ok(ApiResponse::Success { result }.into_response()?)
Ok(result)
}

fn endpoint_next_crate(
experiment: String,
data: Arc<Data>,
_auth: AuthDetails,
) -> Fallible<Response<Body>> {
Ok(ApiResponse::Success {
result: endpoint_next_crate_inner(experiment, data)?,
}
.into_response()?)
}

#[derive(Clone)]
Expand Down

0 comments on commit 6ba3a90

Please sign in to comment.