Skip to content

Commit

Permalink
add data store
Browse files Browse the repository at this point in the history
Signed-off-by: remzi <[email protected]>
  • Loading branch information
HaoYang670 committed Apr 20, 2024
1 parent b0c3185 commit 5850b15
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 27 deletions.
2 changes: 1 addition & 1 deletion rucat_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
rucat_common = {path = "../rucat_common"}
axum = {"version" = "0.7.5"}
axum = {"version" = "0.7.5", features = ["macros"]}
axum-extra = {"version" = "0.9.3", features = ["typed-header"]}
http = {"version" = "1.1.0"}
serde = {"version" = "1.0.197", features = ["derive"]}
Expand Down
38 changes: 26 additions & 12 deletions rucat_server/src/cluster_router.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use axum::{routing::post, Json, Router};
use axum::{extract::State, routing::post, Json, Router};
use serde::{Deserialize, Serialize};

use crate::state::AppState;

#[derive(Clone)]
enum ClusterState {
RUNNING,
ERROR,
Expand All @@ -9,52 +12,63 @@ enum ClusterState {

/// For future
enum ClusterType {
Datafusion,
Ballista,
Rucat,
}

pub(crate) type ClusterId = u8;

pub(crate) struct Cluster<'a> {
name: &'a str,
#[derive(Clone)]
pub struct Cluster {
name: String,
id: ClusterId,
state: ClusterState,
}

impl Cluster {
pub fn get_id(&self) -> ClusterId {
self.id
}
}

#[derive(Debug, Deserialize, Serialize)]
struct CreateClusterRequest {
name: String,
}

/// create a cluster with cluster name in the request body
async fn create_cluster(Json(body): Json<CreateClusterRequest>) -> String {
async fn create_cluster(
State(state): State<AppState<'_>>,
Json(body): Json<CreateClusterRequest>,
) -> String {
format!("Create a cluster with name {}", body.name)
}

async fn delete_cluster() -> () {
async fn delete_cluster(State(state): State<AppState<'_>>) -> () {
todo!()
}

async fn stop_cluster(id: ClusterId) -> () {
async fn stop_cluster(id: ClusterId, State(state): State<AppState<'_>>) -> () {
todo!()
}

async fn start_cluster(id: ClusterId) -> () {
async fn start_cluster(id: ClusterId, State(state): State<AppState<'_>>) -> () {
todo!()
}

async fn restart_cluster(id: ClusterId) -> () {
async fn restart_cluster(id: ClusterId, State(state): State<AppState<'_>>) -> () {
todo!()
}

async fn get_cluster(id: ClusterId) -> () {
async fn get_cluster(id: ClusterId, State(state): State<AppState<'_>>) -> () {
todo!()
}

async fn list_clusters() -> () {
async fn list_clusters(State(state): State<AppState<'_>>) -> () {
todo!()
}

pub fn get_cluster_router() -> Router {
/// Pass the data store endpoint later
pub fn get_cluster_router() -> Router<AppState<'static>> {
Router::new().route("/", post(create_cluster).delete(delete_cluster))
}
12 changes: 0 additions & 12 deletions rucat_server/src/data_store.rs

This file was deleted.

2 changes: 1 addition & 1 deletion rucat_server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod authentication;
pub mod cluster_router;
pub mod data_store;
pub mod state;
10 changes: 9 additions & 1 deletion rucat_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use axum::{middleware, routing::get, Router};
use rucat_common::error::Result;
use rucat_server::{authentication::auth, cluster_router::get_cluster_router};
use rucat_server::{
authentication::auth,
cluster_router::get_cluster_router,
state::{data_store::DataStore, AppState},
};
use tower_http::trace::TraceLayer;

#[tokio::main]
Expand All @@ -24,9 +28,13 @@ fn get_app() -> Router {
"Hello, Rucat!"
}

// create shared state
let app_state = AppState::new(DataStore::new_in_memory());

Router::new()
.route("/", get(root_handler))
.nest("/cluster", get_cluster_router())
.layer(middleware::from_fn(auth))
.layer(TraceLayer::new_for_http())
.with_state(app_state)
}
51 changes: 51 additions & 0 deletions rucat_server/src/state/data_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Datastore to record clusters' infomation
use std::collections::HashMap;

use crate::cluster_router::{Cluster, ClusterId};
use rucat_common::error::Result;

/// Store the metadata of Cluster
/// The lifetime here reprensent that of the endpoint of the SurrealDB.
// DO I need to add a rwlock for it? Or the mutable reference can make sure the r-w policy?
// the problem here is that `Self` is shared between threads.
// Achive the Component in Spring by using Axum::state?
#[derive(Clone)]
pub enum DataStore<'a> {
InMemoryDataStore {
store: HashMap<ClusterId, Cluster>,
},
/// I want to find a distributed database for storing.
SurrealDB {
endpoint: &'a str,
},
}

impl<'a> DataStore<'a> {
pub fn new_in_memory() -> Self {
Self::InMemoryDataStore {
store: HashMap::new(),
}
}

fn add_cluster(&mut self, cluster: Cluster) -> Result<()> {
let id = cluster.get_id();
todo!()
}

fn get_cluster(&self, id: ClusterId) -> Option<&Cluster> {
todo!()
}

fn delete_cluster(&mut self, id: ClusterId) -> Result<()> {
todo!()
}

// the returned reference in Box has the same lifetime as self
fn get_all_clusters(&self) -> Box<dyn Iterator<Item = &Cluster> + '_> {
match self {
DataStore::InMemoryDataStore { store } => Box::new(store.values()),
DataStore::SurrealDB { endpoint: _ } => todo!(),
}
}
}
18 changes: 18 additions & 0 deletions rucat_server/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use self::data_store::DataStore;

pub mod data_store;

#[derive(Clone)]
pub struct AppState<'a> {
data_store: DataStore<'a>,
}

impl<'a> AppState<'a> {
pub fn new(data_store: DataStore<'a>) -> Self {
Self { data_store }
}

pub fn get_data_store(&self) -> &DataStore {
&self.data_store
}
}

0 comments on commit 5850b15

Please sign in to comment.