Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

feat: better server-side errors #9

Merged
merged 1 commit into from
May 13, 2023
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt", "json"] }

[profile.release]
debug = true
debug = 1
#lto = true # Enable link-time optimization
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabling this for now because it has errors with rocksdb 0.18 (due to bz_internal_error duplication when linking). It is already fixed (more info) but need to update sourmash-bio/sourmash#2230 to use the newer rocksdb version.

71 changes: 54 additions & 17 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{borrow::Cow, net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};

use axum::{
body::Bytes,
body::{BoxBody, Bytes},
error_handling::HandleErrorLayer,
extract::{ContentLengthLimit, Extension},
http::StatusCode,
response::IntoResponse,
http::{header, StatusCode},
response::{IntoResponse, Response},
routing::{get_service, post},
Router,
};
Expand Down Expand Up @@ -63,6 +63,11 @@ async fn main() {
traces_sample_rate: 1.0,
enable_profiling: true,
profiles_sample_rate: 1.0,
environment: Some(
std::env::var("MASTIFF_ENVIRONMENT")
.unwrap_or("development".into())
.into(),
),
Comment on lines +66 to +70
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sentry uses debug/release to decide if it is development/production, but rocksdb is very slow in debug mode. Making it explicit here and defaulting to development unless MASTIFF_ENVIRONMENT is defined (which it is in production).

..Default::default()
},
));
Expand Down Expand Up @@ -135,16 +140,18 @@ impl State {
let threshold = self.threshold;
let template = self.template.clone();

let (matches, query_size) = tokio::task::spawn_blocking(move || {
let Ok((matches, query_size)) = tokio::task::spawn_blocking(move || {
if let Some(Sketch::MinHash(mh)) = query.select_sketch(&template) {
let counter = db.counter_for_query(mh);
let matches = db.matches_from_counter(counter, threshold);
(matches, mh.size() as f64)
Ok((matches, mh.size() as f64))
} else {
todo!()
Err("Could not extract compatible sketch to compare")
}
})
.await?;
.await? else {
return Err("Could not extract compatible sketch to compare".into())
};

let mut csv = vec!["SRA accession,containment".into()];
csv.extend(matches.into_iter().map(|(path, size)| {
Expand All @@ -157,22 +164,52 @@ impl State {
}));
Ok(csv)
}

fn parse_sig(&self, raw_data: &[u8]) -> Result<Signature, BoxError> {
let sig = Signature::from_reader(raw_data)?.swap_remove(0);
if sig.select_sketch(&self.template).is_none() {
Err(format!(
"Could not extract compatible sketch to compare. Expected k={}",
&self.template.ksize(),
)
.into())
} else {
Ok(sig)
}
}
}

async fn search(
ContentLengthLimit(bytes): ContentLengthLimit<Bytes, { 1024 * 5_000 }>, // ~5mb
Extension(state): Extension<SharedState>,
//) -> Result<Json<serde_json::Value>, StatusCode> {
) -> Result<String, StatusCode> {
let sig = parse_sig(&bytes).unwrap();
let matches = state.search(sig).await.unwrap();

Ok(matches.join("\n"))
}

fn parse_sig(raw_data: &[u8]) -> Result<Signature, BoxError> {
let sig = Signature::from_reader(raw_data)?.swap_remove(0);
Ok(sig)
) -> Response<BoxBody> {
let sig = match state.parse_sig(&bytes) {
Ok(sig) => sig,
Err(e) => {
return {
(
StatusCode::BAD_REQUEST,
format!("Error parsing signature: {e}"),
)
.into_response()
}
}
};

match state.search(sig).await {
Ok(matches) => (
StatusCode::OK,
[(header::CONTENT_TYPE, "text/plain; charset=utf-8")],
matches.join("\n"),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {e}"),
)
.into_response(),
}
}

async fn handle_static_serve_error(error: std::io::Error) -> impl IntoResponse {
Expand Down
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.