Skip to content

Commit

Permalink
Ftr: protocol layer abstraction design (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
mozhou-tech authored Mar 4, 2023
1 parent be70ed7 commit 64cf21c
Show file tree
Hide file tree
Showing 51 changed files with 715 additions and 66 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"common/logger",
"common/utils",
"common/extention",
"common/base",
"registry/zookeeper",
"registry/nacos",
"metadata",
Expand All @@ -18,7 +19,7 @@ members = [
"remoting/exchange",
"remoting/xds",
"protocol/dubbo2",
"protocol/protocol",
"protocol/base",
"protocol/triple"
]

Expand All @@ -39,8 +40,9 @@ serde_json = "1"
urlencoding = "2.1.2"
logger = {path="./common/logger"}
utils = {path="./common/utils"}
base = {path="./common/base"}
remoting-net = {path="./remoting/net"}
protocol = {path="./protocol/protocol"}
protocol = {path= "protocol/base" }
protocol-dubbo2 = {path="./protocol/dubbo2"}
protocol-triple = {path="./protocol/triple"}
registry-zookeeper = {path="./registry/zookeeper"}
Expand Down
7 changes: 6 additions & 1 deletion application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ dubbo:
version: 1.0.0
group: test
protocol: triple
interface: org.apache.dubbo.sample.tri.Greeter
interface: org.apache.dubbo.sample.tri.Greeter
consumer:
references:
GreeterClientImpl:
url: tri://localhost:20000
protocol: tri
11 changes: 11 additions & 0 deletions common/base/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "base"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
urlencoding.workspace = true
http = "0.2"
logger.workspace = true
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pub const REGISTRY_PROTOCOL: &str = "registry_protocol";
pub const PROTOCOL: &str = "protocol";
pub const REGISTRY: &str = "registry";
Expand Down
26 changes: 26 additions & 0 deletions common/base/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#![cfg_attr(
debug_assertions,
allow(dead_code, unused_imports, unused_variables, unused_mut)
)]
pub mod constants;
pub mod node;
pub mod url;

pub use node::Node;
pub use url::Url;
20 changes: 9 additions & 11 deletions protocol/protocol/src/lib.rs → common/base/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;

use crate::Url;

pub trait Node {
fn get_url(&self) -> Arc<Url>;
fn is_available(&self) -> bool;
fn destroy(&self);

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
fn is_destroyed(&self) -> bool;
}
16 changes: 11 additions & 5 deletions dubbo/src/common/url.rs → common/base/src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ use std::{
fmt::{Display, Formatter},
};

use crate::common::consts::{GROUP_KEY, INTERFACE_KEY, VERSION_KEY};
use crate::constants::{GROUP_KEY, INTERFACE_KEY, VERSION_KEY};
use http::Uri;

#[derive(Debug, Clone, Default, PartialEq)]
pub struct Url {
pub raw_url_string: String,
// value of scheme is different to protocol name, eg. triple -> tri://
// value of scheme is different to base name, eg. triple -> tri://
pub scheme: String,
pub location: String,
pub ip: String,
Expand All @@ -48,7 +48,7 @@ impl Url {
let uri = url
.parse::<http::Uri>()
.map_err(|err| {
tracing::error!("fail to parse url({}), err: {:?}", url, err);
logger::tracing::error!("fail to parse url({}), err: {:?}", url, err);
})
.unwrap();
let query = uri.path_and_query().unwrap().query();
Expand Down Expand Up @@ -157,6 +157,10 @@ impl Url {
pub fn short_url(&self) -> String {
format!("{}://{}:{}", self.scheme, self.ip, self.port)
}

pub fn protocol(&self) -> String {
self.scheme.clone()
}
}

impl Display for Url {
Expand All @@ -179,8 +183,10 @@ impl From<&str> for Url {

#[cfg(test)]
mod tests {
use super::*;
use crate::common::consts::{ANYHOST_KEY, VERSION_KEY};
use crate::{
constants::{ANYHOST_KEY, VERSION_KEY},
url::Url,
};

#[test]
fn test_from_url() {
Expand Down
4 changes: 3 additions & 1 deletion common/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ serde_yaml.workspace = true
serde = { workspace = true, features = ["derive"] }
project-root = "0.2.2"
anyhow.workspace=true
once_cell.workspace = true
once_cell.workspace = true
local-ip-address = "0.5.1"
port-selector = "0.1.6"
72 changes: 72 additions & 0 deletions common/utils/src/host_util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::net::IpAddr;

use port_selector::is_free;

pub use port_selector::Port;

// get local ip for linux/macos/windows
#[allow(dead_code)]
pub(crate) fn local_ip() -> IpAddr {
local_ip_address::local_ip().unwrap()
}

#[allow(dead_code)]
pub(crate) fn is_free_port(port: Port) -> bool {
is_free(port)
}

// scan from the give port
#[allow(dead_code)]
pub(crate) fn scan_free_port(port: Port) -> Port {
for selected_port in port..65535 {
if is_free_port(selected_port) {
return selected_port;
} else {
continue;
}
}
port
}

#[cfg(test)]
mod tests {
use local_ip_address::list_afinet_netifas;

use super::*;

#[test]
fn test_local_ip() {
let ip = local_ip();
println!("ip: {}", ip);
}

#[test]
fn test_local_addresses() {
let network_interfaces = list_afinet_netifas().unwrap();
for (name, ip) in network_interfaces.iter() {
println!("{}:\t{:?}", name, ip);
}
}

#[test]
fn test_scan_free_port() {
let free_port = scan_free_port(7890);
println!("{}", free_port);
}
}
1 change: 1 addition & 0 deletions common/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
pub mod host_util;
pub mod path_util;
pub mod yaml_util;
2 changes: 1 addition & 1 deletion config/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl ProtocolRetrieve for ProtocolConfig {
} else {
let result = self.get_protocol(protocol_key);
if let Some(..) = result {
panic!("default triple protocol dose not defined.")
panic!("default triple base dose not defined.")
} else {
result.unwrap()
}
Expand Down
1 change: 1 addition & 0 deletions dubbo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ aws-smithy-http = "0.54.1"
itertools.workspace = true
urlencoding.workspace = true
lazy_static.workspace = true
base.workspace=true

dubbo-config = { path = "../config", version = "0.3.0" }

Expand Down
2 changes: 1 addition & 1 deletion dubbo/src/cluster/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use std::{
};

use crate::{
common::url::Url,
invocation::{Invocation, RpcInvocation},
registry::{memory_registry::MemoryNotifyListener, BoxRegistry, RegistryWrapper},
};
use base::Url;

/// Directory.
///
Expand Down
2 changes: 1 addition & 1 deletion dubbo/src/cluster/loadbalance/impls/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use base::Url;
use std::{
fmt::{Debug, Formatter},
sync::Arc,
Expand All @@ -22,7 +23,6 @@ use std::{
use crate::{
cluster::loadbalance::types::{LoadBalance, Metadata},
codegen::RpcInvocation,
common::url::Url,
};

pub struct RandomLoadBalance {
Expand Down
2 changes: 1 addition & 1 deletion dubbo/src/cluster/loadbalance/impls/roundrobin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use base::Url;
use std::{
collections::HashMap,
fmt::{Debug, Formatter},
Expand All @@ -26,7 +27,6 @@ use std::{
use crate::{
cluster::loadbalance::types::{LoadBalance, Metadata},
codegen::RpcInvocation,
common::url::Url,
};

pub struct RoundRobinLoadBalance {
Expand Down
3 changes: 2 additions & 1 deletion dubbo/src/cluster/loadbalance/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
* limitations under the License.
*/

use base::Url;
use std::{fmt::Debug, sync::Arc};

use crate::{codegen::RpcInvocation, common::url::Url};
use crate::codegen::RpcInvocation;

pub type BoxLoadBalance = Box<dyn LoadBalance + Send + Sync>;

Expand Down
2 changes: 1 addition & 1 deletion dubbo/src/cluster/support/cluster_invoker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use aws_smithy_http::body::SdkBody;
use std::{str::FromStr, sync::Arc};

use base::Url;
use http::{uri::PathAndQuery, Request};

use crate::{
Expand All @@ -26,7 +27,6 @@ use crate::{
support::DEFAULT_LOADBALANCE,
},
codegen::{Directory, RegistryDirectory, TripleClient},
common::url::Url,
invocation::RpcInvocation,
};

Expand Down
6 changes: 3 additions & 3 deletions dubbo/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use std::{
sync::{Arc, Mutex},
};

use base::Url;
use futures::{future, Future};
use tracing::{debug, info};

use crate::{
common::url::Url,
protocol::{BoxExporter, Protocol},
registry::{
protocol::RegistryProtocol,
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Dubbo {
info!("protocol_url: {:?}", protocol_url);
Url::from_url(&protocol_url)
} else {
return Err(format!("protocol {:?} not exists", service_config.protocol).into());
return Err(format!("base {:?} not exists", service_config.protocol).into());
};
info!("url: {:?}", url);
if url.is_none() {
Expand Down Expand Up @@ -126,7 +126,7 @@ impl Dubbo {
let mut async_vec: Vec<Pin<Box<dyn Future<Output = BoxExporter> + Send>>> = Vec::new();
for (name, items) in self.protocols.iter() {
for url in items.iter() {
info!("protocol: {:?}, service url: {:?}", name, url);
info!("base: {:?}, service url: {:?}", name, url);
let exporter = mem_reg.clone().export(url.to_owned());
async_vec.push(exporter);
//TODO multiple registry
Expand Down
1 change: 0 additions & 1 deletion dubbo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

pub mod cluster;
pub mod codegen;
pub mod common;
pub mod context;
pub mod filter;
mod framework;
Expand Down
2 changes: 1 addition & 1 deletion dubbo/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use async_trait::async_trait;
use aws_smithy_http::body::SdkBody;
use tower_service::Service;

use crate::common::url::Url;
use base::Url;

pub mod server_desc;
pub mod triple;
Expand Down
Loading

0 comments on commit 64cf21c

Please sign in to comment.