This commit is contained in:
Silas Brack 2026-03-07 09:53:24 +01:00
parent 8d32777f9f
commit 2a2afa5f69
7 changed files with 534 additions and 12 deletions

View file

@ -1,9 +1,16 @@
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")]
@ -70,27 +77,53 @@ async fn serve(config: config::Config) {
.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 volumes = config.volume_urls();
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: {volumes:?}");
tracing::info!(
" Replication factor: {}",
config.server.replication_factor
);
tracing::info!(" Volumes: {volume_urls:?}");
tracing::info!(" Replication factor: {}", state.config.server.replication_factor);
// TODO: wire up axum routes, volume client, hasher, health checker
let app = axum::Router::new()
.route("/health", axum::routing::get(|| async { "ok" }));
.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();
// Keep these alive (will be used in later phases)
drop(writer);
drop(reads);
}