Skip to content

Commit

Permalink
feat(kvsd): impl table boot process
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Sep 19, 2024
1 parent 72b07b6 commit 9a047bb
Show file tree
Hide file tree
Showing 28 changed files with 1,301 additions and 17 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions crates/synd_kvsd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ name = "synd-kvsd"
path = "src/main.rs"

[dependencies]
synd-o11y = { path = "../synd_o11y", version = "0.1.8" }
synd-stdx = { path = "../synd_stdx", version = "0.1.0", features = ["color", "humantime", "byte", "conf"] }
synd-kvsd-protocol = { path = "../synd_kvsd_protocol", version = "0.1.0" }
synd-o11y = { path = "../synd_o11y", version = "0.1.8" }
synd-stdx = { path = "../synd_stdx", version = "0.1.0", features = ["color", "humantime", "byte", "conf"] }

chrono = { workspace = true }
clap = { workspace = true, features = ["derive", "env", "std"] }
crc32fast = "1.4.2"
serde = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "io-util", "net", "signal", "fs", "sync", "macros", "time"] }
Expand Down
14 changes: 14 additions & 0 deletions crates/synd_kvsd/src/authn/credential.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::borrow::Cow;

pub(crate) enum Credential<'a> {
Password(Password<'a>),
}

pub(crate) trait Provider {
fn credential(&self) -> Credential;
}

pub(crate) struct Password<'a> {
pub(crate) username: Cow<'a, str>,
pub(crate) password: Cow<'a, str>,
}
4 changes: 4 additions & 0 deletions crates/synd_kvsd/src/authn/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// TODO: remove
#![expect(dead_code)]
pub(crate) mod credential;
pub(crate) mod principal;
16 changes: 16 additions & 0 deletions crates/synd_kvsd/src/authn/principal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mod user;
pub(crate) use user::User;

#[derive(Debug, Clone)]
pub(crate) enum Principal {
AnonymousUser,
// TODO: remove
#[expect(dead_code)]
User(User),
}

impl Principal {
pub(crate) fn is_authenticated(&self) -> bool {
matches!(self, Principal::User(_))
}
}
5 changes: 5 additions & 0 deletions crates/synd_kvsd/src/authn/principal/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[derive(Debug, Clone)]
pub(crate) struct User {
#[expect(dead_code)]
pub(crate) name: String,
}
34 changes: 27 additions & 7 deletions crates/synd_kvsd/src/boot/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
// TODO: remove
use std::path::PathBuf;

use thiserror::Error;
use tokio::sync::mpsc;

use crate::boot::provision::{ProvisionError, Provisioner};
use crate::{
boot::provision::{ProvisionError, Provisioner},
middleware::Dispatcher,
table::{Table, TableRef},
};

mod provision;

#[derive(Error, Debug)]
pub enum BootError {
#[error("failed to provision: {0}")]
Provision(#[from] ProvisionError),
#[error("tablel: {message}")]
Table { message: String },
}

pub struct Boot {
Expand All @@ -23,16 +31,28 @@ impl Boot {
}
}

pub fn boot(self) -> Result<(), BootError> {
pub async fn boot(self) -> Result<(), BootError> {
let prov = Provisioner::new(self.root_dir).provision()?;
let mut dispatcher = Dispatcher::new();

// Create dispatcher
for (_namespace, _table_dir) in prov.table_dirs()? {
// Create table from path
// register table in dispatcher
for (namespace, table_dir) in prov.table_dirs()? {
let table = Table::try_from_dir(table_dir)
.await
.map_err(|err| BootError::Table {
message: err.to_string(),
})?;
// TODO: configure buffer size
let (tx, _) = mpsc::channel(1024);
let table_ref = TableRef {
namespace,
name: table.name().into(),
};
dispatcher.add_table(table_ref, tx);

// TODO: abstract async runtime
// tokio::spawn(table.run(rx));
}
// Walk table direcotries
// Create Dispatcher
// Create Middleware
// Create Kvsd
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/synd_kvsd/src/boot/provision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use synd_stdx::fs::{self, fsimpl};
use thiserror::Error;
use tracing::debug;

use crate::types::Namespace;
use crate::table::Namespace;
use state::{Provisioned, Unprovisioned};

mod state {
Expand Down
5 changes: 5 additions & 0 deletions crates/synd_kvsd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ pub mod cli;
pub mod config;
pub mod error;
pub mod types;

mod authn;
mod middleware;
mod table;
mod uow;
2 changes: 1 addition & 1 deletion crates/synd_kvsd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn main() {
}
};

Boot::new(config.root_dir()).boot().unwrap();
Boot::new(config.root_dir()).boot().await.unwrap();

// 7. Spawn Kvsd
// 8. Run Server
Expand Down
37 changes: 37 additions & 0 deletions crates/synd_kvsd/src/middleware/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::collections::HashMap;

use tokio::sync::mpsc;

use crate::{
table::{Namespace, TableRef},
uow::UnitOfWork,
};

pub(crate) struct Dispatcher {
// TODO: use TableName
table: HashMap<Namespace, HashMap<String, mpsc::Sender<UnitOfWork>>>,
}

impl Dispatcher {
pub(crate) fn new() -> Self {
Self {
table: HashMap::new(),
}
}

pub(crate) fn add_table(&mut self, table_ref: TableRef<'_>, sender: mpsc::Sender<UnitOfWork>) {
self.table
.entry(table_ref.namespace)
.or_default()
.insert(table_ref.name.into(), sender);
}

/*
fn lookup_table(&self, namespace: &str, table: &str) -> Result<&mpsc::Sender<UnitOfWork>> {
self.table
.get(namespace)
.and_then(|tables| tables.get(table))
.ok_or_else(|| ErrorKind::TableNotFound(format!("{}/{}", namespace, table)).into())
}
*/
}
2 changes: 2 additions & 0 deletions crates/synd_kvsd/src/middleware/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod dispatcher;
pub(crate) use dispatcher::Dispatcher;
Loading

0 comments on commit 9a047bb

Please sign in to comment.