Skip to content

Commit

Permalink
move router from server to a dependent crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachelint committed Dec 27, 2022
1 parent 6139d89 commit b1a868a
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 264 deletions.
1 change: 0 additions & 1 deletion cluster/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ workspace = true
workspace = true

[dependencies]
analytic_engine = { workspace = true }
async-trait = { workspace = true }
ceresdbproto = { workspace = true }
common_types = { workspace = true }
Expand Down
24 changes: 24 additions & 0 deletions router/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "router"

[package.version]
workspace = true

[package.authors]
workspace = true

[package.edition]
workspace = true

[dependencies]
async-trait = { workspace = true }
ceresdbproto = { workspace = true }
cluster = { workspace = true }
common_types = { workspace = true }
common_util = { workspace = true }
log = { workspace = true }
meta_client = { workspace = true }
serde = { workspace = true }
serde_derive = { workspace = true }
snafu = { workspace = true }
twox-hash = "1.6"
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use meta_client::types::{NodeShard, RouteTablesRequest, RouteTablesResponse};
use snafu::{OptionExt, ResultExt};

use crate::{
config::Endpoint,
route::{hash, OtherNoCause, OtherWithCause, ParseEndpoint, Result, Router},
endpoint::Endpoint, hash, OtherNoCause, OtherWithCause, ParseEndpoint, Result, Router,
};

pub struct ClusterBasedRouter {
Expand Down
67 changes: 67 additions & 0 deletions router/src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.

//! Endpoint definition
use std::str::FromStr;

use ceresdbproto::storage;
use serde_derive::Deserialize;

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct Endpoint {
pub addr: String,
pub port: u16,
}

impl Endpoint {
pub fn new(addr: String, port: u16) -> Self {
Self { addr, port }
}
}

impl ToString for Endpoint {
fn to_string(&self) -> String {
format!("{}:{}", self.addr, self.port)
}
}

impl FromStr for Endpoint {
type Err = Box<dyn std::error::Error + Send + Sync>;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let (addr, raw_port) = match s.rsplit_once(':') {
Some(v) => v,
None => {
let err_msg = "Can't find ':' in the source string".to_string();
return Err(Self::Err::from(err_msg));
}
};
let port = raw_port.parse().map_err(|e| {
let err_msg = format!("Fail to parse port:{}, err:{}", raw_port, e);
Self::Err::from(err_msg)
})?;

Ok(Endpoint {
addr: addr.to_string(),
port,
})
}
}

impl From<Endpoint> for storage::Endpoint {
fn from(endpoint: Endpoint) -> Self {
storage::Endpoint {
ip: endpoint.addr,
port: endpoint.port as u32,
}
}
}

impl From<storage::Endpoint> for Endpoint {
fn from(endpoint_pb: storage::Endpoint) -> Self {
Endpoint {
addr: endpoint_pb.ip,
port: endpoint_pb.port as u16,
}
}
}
File renamed without changes.
11 changes: 6 additions & 5 deletions server/src/route/mod.rs → router/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.

use std::sync::Arc;

use async_trait::async_trait;
use ceresdbproto::storage::{Route, RouteRequest};

pub mod cluster_based;
pub mod endpoint;
pub(crate) mod hash;
pub mod rule_based;

use std::sync::Arc;

use async_trait::async_trait;
use ceresdbproto::storage::{Route, RouteRequest};
pub use cluster_based::ClusterBasedRouter;
use common_util::define_result;
pub use rule_based::{RuleBasedRouter, RuleList};
use snafu::{Backtrace, Snafu};

Expand Down
5 changes: 1 addition & 4 deletions server/src/route/rule_based.rs → router/src/rule_based.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use meta_client::types::ShardId;
use serde_derive::Deserialize;
use snafu::{ensure, OptionExt};

use crate::{
config::Endpoint,
route::{hash, Result, RouteNotFound, Router, ShardNotFound},
};
use crate::{endpoint::Endpoint, hash, Result, RouteNotFound, Router, ShardNotFound};

pub type ShardNodes = HashMap<ShardId, Endpoint>;

Expand Down
3 changes: 1 addition & 2 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ prometheus = { workspace = true }
prometheus-static-metric = { workspace = true }
prost = { workspace = true }
query_engine = { workspace = true }
router = { workspace = true }
serde = { workspace = true }
serde_derive = { workspace = true }
serde_json = { workspace = true }
Expand All @@ -47,8 +48,6 @@ table_engine = { workspace = true }
tokio = { workspace = true }
tokio-stream = { version = "0.1", features = ["net"] }
tonic = { workspace = true }
twox-hash = "1.6"
warp = "0.3"

[dev-dependencies]
sql = { workspace = true, features = ["test"] }
168 changes: 0 additions & 168 deletions server/src/avro_util.rs

This file was deleted.

Loading

0 comments on commit b1a868a

Please sign in to comment.