Add integration tests

This commit is contained in:
Silas Brack 2026-03-07 10:17:52 +01:00
parent d7c9192ebb
commit 68ae92e4bf
6 changed files with 392 additions and 83 deletions

69
src/lib.rs Normal file
View file

@ -0,0 +1,69 @@
pub mod config;
pub mod db;
pub mod error;
pub mod hasher;
pub mod health;
pub mod server;
pub mod volume;
use std::collections::HashSet;
use std::sync::Arc;
use tokio::sync::RwLock;
/// Build the axum Router with all state wired up. Returns the router and
/// a handle to the writer (caller must keep it alive).
pub async fn build_app(config: config::Config) -> axum::Router {
let db_path = &config.database.path;
if let Some(parent) = std::path::Path::new(db_path).parent() {
std::fs::create_dir_all(parent).unwrap_or_else(|e| {
eprintln!("Failed to create database directory: {e}");
std::process::exit(1);
});
}
let (writer, ready_rx) = db::spawn_writer(db_path.to_string());
ready_rx.await.expect("writer failed to initialize");
let num_readers = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(4);
let reads = db::ReadPool::new(db_path, num_readers);
let volume_urls = config.volume_urls();
let ring = Arc::new(RwLock::new(hasher::Ring::new(
&volume_urls,
config.server.virtual_nodes,
)));
let volume_client = volume::VolumeClient::new();
let healthy_volumes: health::HealthyVolumes =
Arc::new(RwLock::new(HashSet::from_iter(volume_urls.clone())));
health::spawn_health_checker(
volume_client.clone(),
volume_urls,
healthy_volumes.clone(),
);
let state = server::AppState {
writer,
reads,
ring,
volume_client,
healthy_volumes,
config: Arc::new(config),
};
axum::Router::new()
.route("/", axum::routing::get(server::list_keys))
.route(
"/{*key}",
axum::routing::get(server::get_key)
.put(server::put_key)
.delete(server::delete_key)
.head(server::head_key),
)
.with_state(state)
}

View file

@ -1,16 +1,5 @@
mod config;
mod db;
mod error;
mod hasher;
mod health;
mod server;
mod volume;
use clap::{Parser, Subcommand};
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Parser)]
#[command(name = "mkv", about = "Distributed key-value store")]
@ -40,13 +29,21 @@ async fn main() {
tracing_subscriber::fmt::init();
let cli = Cli::parse();
let config = config::Config::load(&cli.config).unwrap_or_else(|e| {
let config = mkv::config::Config::load(&cli.config).unwrap_or_else(|e| {
eprintln!("Failed to load config: {e}");
std::process::exit(1);
});
match cli.command {
Commands::Serve => serve(config).await,
Commands::Serve => {
let port = config.server.port;
let app = mkv::build_app(config).await;
let addr = format!("0.0.0.0:{port}");
tracing::info!("Listening on {addr}");
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
Commands::Rebuild => {
eprintln!("rebuild not yet implemented");
std::process::exit(1);
@ -57,73 +54,3 @@ async fn main() {
}
}
}
async fn serve(config: config::Config) {
let db_path = &config.database.path;
// Ensure parent directory exists
if let Some(parent) = std::path::Path::new(db_path).parent() {
std::fs::create_dir_all(parent).unwrap_or_else(|e| {
eprintln!("Failed to create database directory: {e}");
std::process::exit(1);
});
}
let (writer, ready_rx) = db::spawn_writer(db_path.to_string());
ready_rx.await.expect("writer failed to initialize");
let num_readers = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(4);
let reads = db::ReadPool::new(db_path, num_readers);
let volume_urls = config.volume_urls();
let ring = Arc::new(RwLock::new(hasher::Ring::new(
&volume_urls,
config.server.virtual_nodes,
)));
let volume_client = volume::VolumeClient::new();
// Start with all volumes assumed healthy, health checker will update
let healthy_volumes: health::HealthyVolumes =
Arc::new(RwLock::new(HashSet::from_iter(volume_urls.clone())));
health::spawn_health_checker(
volume_client.clone(),
volume_urls.clone(),
healthy_volumes.clone(),
);
let port = config.server.port;
let state = server::AppState {
writer,
reads,
ring,
volume_client,
healthy_volumes,
config: Arc::new(config),
};
tracing::info!("Starting mkv server on port {port}");
tracing::info!(" Readers: {num_readers}");
tracing::info!(" Volumes: {volume_urls:?}");
tracing::info!(" Replication factor: {}", state.config.server.replication_factor);
let app = axum::Router::new()
.route("/", axum::routing::get(server::list_keys))
.route(
"/{*key}",
axum::routing::get(server::get_key)
.put(server::put_key)
.delete(server::delete_key)
.head(server::head_key),
)
.with_state(state);
let addr = format!("0.0.0.0:{port}");
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
tracing::info!("Listening on {addr}");
axum::serve(listener, app).await.unwrap();
}