Skip to content

Commit

Permalink
[ENH] Use figment configuration for chroma-load
Browse files Browse the repository at this point in the history
This mirrors the compactor and query services (and can even merge with
their configs if we so choose).
  • Loading branch information
rescrv committed Dec 2, 2024
1 parent f23c64d commit 22807bb
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions rust/load/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ async-trait = "0.1.83"
axum = "0.7"
chromadb = { git = "https://github.com/rescrv/chromadb-rs", rev = "e364e35c34c660d4e8e862436ea600ddc2f46a1e" }
chrono = "0.4.38"
figment = { version = "0.10.12", features = ["env", "yaml", "test"] }
guacamole = { version = "0.9", default-features = false }
serde.workspace = true

Expand Down
4 changes: 4 additions & 0 deletions rust/load/chroma_load.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load_service:
service_name: chroma-load
otel_endpoint: "http://otel-collector:4317"
port: 3000
47 changes: 47 additions & 0 deletions rust/load/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use figment::providers::{Env, Format, Yaml};
use serde::Deserialize;

const DEFAULT_CONFIG_PATH: &str = "./chroma_config.yaml";

#[derive(Deserialize)]
/// Root config for chroma-load service. Can be part of a larger config file.
pub struct RootConfig {
pub load_service: LoadServiceConfig,
}

impl RootConfig {
pub fn load() -> Self {
Self::load_from_path(DEFAULT_CONFIG_PATH)
}

// NOTE: Copied from ../worker/src/config.rs.
pub fn load_from_path(path: &str) -> Self {
// Unfortunately, figment doesn't support environment variables with underscores. So we have to map and replace them.
// Excluding our own environment variables, which are prefixed with CHROMA_.
let mut f = figment::Figment::from(Env::prefixed("CHROMA_").map(|k| match k {
k if k == "my_member_id" => k.into(),
k => k.as_str().replace("__", ".").into(),
}));
if std::path::Path::new(path).exists() {
f = figment::Figment::from(Yaml::file(path)).merge(f);
}
// Apply defaults - this seems to be the best way to do it.
// https://github.com/SergioBenitez/Figment/issues/77#issuecomment-1642490298
// f = f.join(Serialized::default(
// "worker.num_indexing_threads",
// num_cpus::get(),
// ));
let res = f.extract();
match res {
Ok(config) => config,
Err(e) => panic!("Error loading config: {}", e),
}
}
}

#[derive(Deserialize)]
pub struct LoadServiceConfig {
pub service_name: String,
pub otel_endpoint: String,
pub port: u16,
}
19 changes: 14 additions & 5 deletions rust/load/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ use tower_http::trace::TraceLayer;
use tracing::Instrument;
use uuid::Uuid;

pub mod config;
pub mod data_sets;
pub mod opentelemetry_config;
pub mod rest;
pub mod workloads;

const CONFIG_PATH_ENV_VAR: &str = "CONFIG_PATH";

/////////////////////////////////////////////// Error //////////////////////////////////////////////

#[derive(Debug)]
Expand Down Expand Up @@ -659,10 +662,14 @@ async fn uninhibit(State(state): State<AppState>) -> Result<String, Error> {
}

pub async fn entrypoint() {
opentelemetry_config::init_otel_tracing(
&"chroma-load".to_string(),
&"localhost:4317".to_string(),
);
let config = match std::env::var(CONFIG_PATH_ENV_VAR) {
Ok(config_path) => config::RootConfig::load_from_path(&config_path),
Err(_) => config::RootConfig::load(),
};

let config = config.load_service;

opentelemetry_config::init_otel_tracing(&config.service_name, &config.otel_endpoint);
let load = Arc::new(LoadService::default());
let state = AppState {
load: Arc::clone(&load),
Expand Down Expand Up @@ -690,7 +697,9 @@ pub async fn entrypoint() {
}),
)
.with_state(state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", config.port))
.await
.unwrap();
let runner = tokio::task::spawn(async move { load.run().await });
axum::serve(listener, app).await.unwrap();
runner.abort();
Expand Down
1 change: 1 addition & 0 deletions rust/worker/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl RootConfig {
/// # Notes
/// The environment variables are prefixed with CHROMA_ and are uppercase.
/// Values in the envionment variables take precedence over values in the YAML file.
// NOTE: Copied to ../load/src/config.rs.
pub(crate) fn load_from_path(path: &str) -> Self {
// Unfortunately, figment doesn't support environment variables with underscores. So we have to map and replace them.
// Excluding our own environment variables, which are prefixed with CHROMA_.
Expand Down

0 comments on commit 22807bb

Please sign in to comment.