-
-
Notifications
You must be signed in to change notification settings - Fork 347
hydra-evaluator: Rewrite in Rust #1608
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
Open
Ericson2314
wants to merge
1
commit into
master
Choose a base branch
from
no-c-plus-plus
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| [package] | ||
| name = "hydra-evaluator" | ||
| version.workspace = true | ||
| edition = "2024" | ||
| license = "GPL-3.0" | ||
| rust-version.workspace = true | ||
|
|
||
| [dependencies] | ||
| anyhow.workspace = true | ||
| clap = { workspace = true, features = [ "derive" ] } | ||
| fs-err.workspace = true | ||
| futures.workspace = true | ||
| sqlx = { workspace = true, features = [ "runtime-tokio", "tls-rustls-ring-webpki", "postgres" ] } | ||
| tokio = { workspace = true, features = [ "full" ] } | ||
| tokio-stream.workspace = true | ||
| tracing.workspace = true | ||
|
|
||
| db = { path = "../crates/db" } | ||
| hydra-tracing = { path = "../crates/tracing" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| { | ||
| lib, | ||
| version, | ||
|
|
||
| rustPlatform, | ||
|
|
||
| nixComponents, | ||
| protobuf, | ||
| pkg-config, | ||
| rust-jemalloc-sys, | ||
| }: | ||
|
|
||
| rustPlatform.buildRustPackage { | ||
| pname = "hydra-evaluator"; | ||
| inherit version; | ||
|
|
||
| src = lib.fileset.toSource { | ||
| root = ../..; | ||
| fileset = lib.fileset.unions [ | ||
| ../../Cargo.toml | ||
| ../../Cargo.lock | ||
| ../../.cargo | ||
| ../../.sqlx | ||
| ../../subprojects/hydra-evaluator/Cargo.toml | ||
| ../../subprojects/hydra-evaluator/src | ||
| ../../subprojects/crates | ||
| # For unit tests which want to spin up a fresh database | ||
| ../../subprojects/hydra/sql/hydra.sql | ||
| ../../subprojects/proto | ||
| ]; | ||
| }; | ||
|
|
||
| cargoLock = { | ||
| lockFile = ../../Cargo.lock; | ||
| outputHashes = { | ||
| "harmonia-store-core-0.0.0-alpha.0" = "sha256-g7JJGrjWnnzBtxtxLaqL/wKehPBZAHh8C7U7ALYW6o0="; | ||
| }; | ||
| }; | ||
|
|
||
| # Drop the other Rust binary crates from the workspace; their sources | ||
| # are excluded from the fileset above, so cargo would otherwise fail | ||
| # trying to load their (absent) manifests. | ||
| postPatch = '' | ||
| sed -i '/hydra-evaluator/!{/"subprojects\/hydra-/d;}' Cargo.toml | ||
| ''; | ||
|
|
||
| buildAndTestSubdir = "subprojects/hydra-evaluator"; | ||
|
|
||
| nativeBuildInputs = [ | ||
| pkg-config | ||
| protobuf | ||
| ]; | ||
|
|
||
| buildInputs = [ | ||
| nixComponents.nix-main | ||
| protobuf | ||
| rust-jemalloc-sys | ||
| ]; | ||
|
|
||
| # FIXME: get these passing in a prod build | ||
| doCheck = false; | ||
|
|
||
| meta.description = "Hydra evaluator (Rust)"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| use anyhow::Context as _; | ||
| use sqlx::postgres::{PgConnectOptions, PgSslMode}; | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) struct HydraConfig { | ||
| options: HashMap<String, String>, | ||
| } | ||
|
|
||
| impl HydraConfig { | ||
| pub(crate) fn load() -> Self { | ||
| let mut options = HashMap::new(); | ||
|
|
||
| let path = match std::env::var("HYDRA_CONFIG") { | ||
| Ok(p) if !p.is_empty() => p, | ||
| _ => return Self { options }, | ||
| }; | ||
|
|
||
| let contents = match fs_err::read_to_string(&path) { | ||
| Ok(c) => c, | ||
| Err(e) => { | ||
| tracing::warn!("could not read HYDRA_CONFIG at {path}: {e}"); | ||
| return Self { options }; | ||
| } | ||
| }; | ||
|
|
||
| for line in contents.lines() { | ||
| // Strip comments | ||
| let line = match line.find('#') { | ||
| Some(pos) => &line[..pos], | ||
| None => line, | ||
| }; | ||
| let line = line.trim(); | ||
|
|
||
| let Some(eq) = line.find('=') else { | ||
| continue; | ||
| }; | ||
|
|
||
| let key = line[..eq].trim(); | ||
| let value = line[eq + 1..].trim(); | ||
|
|
||
| if key.is_empty() { | ||
| continue; | ||
| } | ||
|
|
||
| options.insert(key.to_owned(), value.to_owned()); | ||
| } | ||
|
|
||
| Self { options } | ||
| } | ||
|
|
||
| pub(crate) fn get_int(&self, key: &str, default: u64) -> u64 { | ||
| self.options | ||
| .get(key) | ||
| .and_then(|v| v.parse().ok()) | ||
| .unwrap_or(default) | ||
| } | ||
| } | ||
|
|
||
| /// Parse a `HYDRA_DBI` environment variable into `PgConnectOptions`. | ||
| /// | ||
| /// Accepts strings like `dbi:Pg:dbname=hydra;host=localhost;port=5432`. | ||
| pub(crate) fn parse_hydra_dbi() -> anyhow::Result<PgConnectOptions> { | ||
| let dbi = std::env::var("HYDRA_DBI").unwrap_or_else(|_| "dbi:Pg:dbname=hydra;".to_owned()); | ||
| parse_dbi(&dbi) | ||
| } | ||
|
|
||
| fn parse_dbi(dbi: &str) -> anyhow::Result<PgConnectOptions> { | ||
| let params = dbi | ||
| .strip_prefix("dbi:Pg:") | ||
| .or_else(|| dbi.strip_prefix("DBI:Pg:")) | ||
| .context("$HYDRA_DBI does not denote a PostgreSQL database")?; | ||
|
|
||
| let mut opts = PgConnectOptions::new(); | ||
|
|
||
| for pair in params.split(';').filter(|s| !s.is_empty()) { | ||
| let (key, value) = pair | ||
| .split_once('=') | ||
| .with_context(|| format!("invalid DBI parameter: {pair}"))?; | ||
| match key.trim() { | ||
| "dbname" => opts = opts.database(value.trim()), | ||
| "host" => opts = opts.host(value.trim()), | ||
| "port" => { | ||
| opts = opts.port( | ||
| value | ||
| .trim() | ||
| .parse() | ||
| .with_context(|| format!("invalid port: {value}"))?, | ||
| ); | ||
| } | ||
| "user" => opts = opts.username(value.trim()), | ||
| "password" => opts = opts.password(value.trim()), | ||
| "application_name" => opts = opts.application_name(value.trim()), | ||
| // The C++ evaluator used libpq which understood these | ||
| // natively; without explicit handling TLS-required | ||
| // deployments would fail. | ||
| "sslmode" => { | ||
| let mode = match value.trim() { | ||
| "disable" => PgSslMode::Disable, | ||
| "allow" => PgSslMode::Allow, | ||
| "prefer" => PgSslMode::Prefer, | ||
| "require" => PgSslMode::Require, | ||
| "verify-ca" => PgSslMode::VerifyCa, | ||
| "verify-full" => PgSslMode::VerifyFull, | ||
| v => anyhow::bail!("invalid sslmode: {v}"), | ||
| }; | ||
| opts = opts.ssl_mode(mode); | ||
| } | ||
| "sslrootcert" => opts = opts.ssl_root_cert(value.trim()), | ||
| "sslcert" => opts = opts.ssl_client_cert(value.trim()), | ||
| "sslkey" => opts = opts.ssl_client_key(value.trim()), | ||
| // Warn rather than bail on unknown parameters, to avoid | ||
| // breaking on libpq keywords we haven't mapped yet. | ||
| other => { | ||
| tracing::warn!("ignoring unsupported DBI parameter: {other}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok(opts) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn parse_simple_dbi() { | ||
| parse_dbi("dbi:Pg:dbname=hydra;host=localhost;port=5432").unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_dbi_uppercase() { | ||
| parse_dbi("DBI:Pg:dbname=testdb;").unwrap(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be in the db crate?