Skip to content

Commit

Permalink
Adjust the priority of sn configs
Browse files Browse the repository at this point in the history
  • Loading branch information
lurenpluto committed Nov 25, 2022
1 parent d65f31a commit 5db6d10
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/component/cyfs-stack-loader/src/stack_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,13 @@ impl StackInfo {
// should not change the device's inner sn_list and pn_list
info!("current device: {}", device_info.device.format_json());

for (id, sn) in cyfs_util::get_default_sn_desc() {
// only use the sn in local config dir
for (id, sn) in cyfs_util::get_local_sn_desc() {
info!("will use sn: {}", id);
init_sn_peers.push(sn.to_owned());
}

for (id, pn) in cyfs_util::get_pn_desc() {
for (id, pn) in cyfs_util::get_local_pn_desc() {
info!("will use pn: {}", id);
init_pn_peers.push(pn.to_owned());
}
Expand Down
4 changes: 2 additions & 2 deletions src/component/cyfs-stack/src/config/sn_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl SNConfigManager {
match self.noc.get_object(&noc_req).await {
Ok(Some(object)) => {
info!("load sn from noc: {}", id);
let list = SNDirParser::parse(&id, &object.object.object_raw)?;
let list = SNDirParser::parse(Some(id), &object.object.object_raw)?;
*self.sn_list.lock().unwrap() = list;

Ok(())
Expand Down Expand Up @@ -211,7 +211,7 @@ impl SNConfigManager {

let object = ret.unwrap();
let id = object.object.object_id();
let list = SNDirParser::parse(&id, &object.object_raw)?;
let list = SNDirParser::parse(Some(&id), &object.object_raw)?;

{
let mut cache = self.coll.get().unwrap().coll().write().unwrap();
Expand Down
12 changes: 12 additions & 0 deletions src/component/cyfs-stack/src/stack/cyfs_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ impl CyfsStackImpl {
tracker.clone(),
router_handlers.clone(),
chunk_manager.clone(),
&sn_config_manager,
)
.await?;

Expand Down Expand Up @@ -704,6 +705,7 @@ impl CyfsStackImpl {
tracker: Box<dyn TrackerCache>,
router_handlers: RouterHandlersManager,
chunk_manager: ChunkManagerRef,
sn_config_manager: &SNConfigManager,
) -> BuckyResult<(StackGuard, BdtNDNEventHandler)> {
let chunk_store = Box::new(ChunkStoreReader::new(
chunk_manager.clone(),
Expand All @@ -730,9 +732,19 @@ impl CyfsStackImpl {
bdt_params.config.interface.udp.sn_only = sn_only;
}

// priority: params sn(always loaded from config dir) > sn config manager(always loaded from meta) > buildin sn
if !params.known_sn.is_empty() {
bdt_params.known_sn = Some(params.known_sn);
} else {
// use sn from sn config manager
let mut sn_list = sn_config_manager.get_sn_list();
if sn_list.is_empty() {
sn_list = cyfs_util::get_default_sn_desc().clone();
}

bdt_params.known_sn = Some(sn_list.into_iter().map(|v| v.1).collect());
}

if !params.known_device.is_empty() {
bdt_params.known_device = Some(params.known_device);
}
Expand Down
53 changes: 36 additions & 17 deletions src/component/cyfs-util/src/util/bdt_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ fn load_device_object(file: &Path) -> Vec<(DeviceId, Device)> {
}
}

// 读取本地的pn配置,在{root}/etc/desc/pn.desc
// 如果函数返回None,就不配置pn
// 如果函数返回Some,就一定将返回值配置成pn
fn load_pn_desc() -> Vec<(DeviceId, Device)> {
// 读取本地的pn配置,在{root}/etc/desc/pn.desc and {root}/etc/desc/pn
fn load_local_pn_desc() -> Vec<(DeviceId, Device)> {
let mut default_pn_file = get_cyfs_root_path();
default_pn_file.push("etc");
default_pn_file.push("desc");
Expand All @@ -119,13 +117,13 @@ fn load_pn_desc() -> Vec<(DeviceId, Device)> {
}
}

fn load_default_sn_desc() -> Vec<(DeviceId, Device)> {
fn load_local_sn_desc() -> Vec<(DeviceId, Device)> {
let mut default_sn_file = get_cyfs_root_path();
default_sn_file.push("etc");
default_sn_file.push("desc");

let dir = default_sn_file.join("sn");
let ret = if dir.is_dir() {
if dir.is_dir() {
load_device_objects_list(&dir)
} else {
default_sn_file.push("sn.desc");
Expand All @@ -134,21 +132,20 @@ fn load_default_sn_desc() -> Vec<(DeviceId, Device)> {
} else {
vec![]
}
};

if ret.len() > 0 {
return ret;
}
}

fn load_default_sn_desc() -> Vec<(DeviceId, Device)> {
let sn_raw = match cyfs_base::get_channel() {
CyfsChannel::Nightly => env!("NIGHTLY_SN_RAW"),
CyfsChannel::Beta => env!("BETA_SN_RAW"),
CyfsChannel::Stable => {
unreachable!()
}
};
let (desc, _) = Device::raw_decode(&hex::decode(sn_raw).unwrap()).unwrap();
vec![(desc.desc().device_id(), desc)]

let object_raw = hex::decode(sn_raw).unwrap();
SNDirParser::parse(None, &object_raw).unwrap()
}

pub fn get_default_known_peers() -> Vec<Device> {
Expand Down Expand Up @@ -201,22 +198,44 @@ pub fn get_device_desc(name: &str) -> BuckyResult<(Device, PrivateKey)> {
}

lazy_static::lazy_static! {
pub static ref LOCAL_SN: Vec<(DeviceId, Device)> = load_local_sn_desc();
pub static ref LOCAL_PN: Vec<(DeviceId, Device)> = load_local_pn_desc();

pub static ref DEFAULT_SN: Vec<(DeviceId, Device)> = load_default_sn_desc();
pub static ref DEFAULT_PN: Vec<(DeviceId, Device)> = load_pn_desc();
}

pub fn get_pn_desc() -> &'static Vec<(DeviceId, Device)> {
&DEFAULT_PN
// get configed pn
pub fn get_local_pn_desc() -> &'static Vec<(DeviceId, Device)> {
&LOCAL_PN
}

pub fn get_local_pn_desc_id_list() -> Vec<DeviceId> {
LOCAL_PN.iter().map(|item| item.0.clone()).collect()
}

// configed sn
pub fn get_local_sn_desc() -> &'static Vec<(DeviceId, Device)> {
&LOCAL_SN
}

pub fn get_pn_desc_id_list() -> Vec<DeviceId> {
DEFAULT_PN.iter().map(|item| item.0.clone()).collect()
pub fn get_local_sn_desc_id_list() -> Vec<DeviceId> {
LOCAL_SN.iter().map(|item| item.0.clone()).collect()
}

// buildin default sn
pub fn get_default_sn_desc() -> &'static Vec<(DeviceId, Device)> {
&DEFAULT_SN
}

pub fn get_default_sn_desc_id_list() -> Vec<DeviceId> {
DEFAULT_SN.iter().map(|item| item.0.clone()).collect()
}

// get local sn, if empty, get the buildin sn
pub fn get_sn_desc() -> &'static Vec<(DeviceId, Device)> {
if LOCAL_SN.len() > 0 {
&LOCAL_SN
} else {
&DEFAULT_SN
}
}
7 changes: 4 additions & 3 deletions src/component/cyfs-util/src/util/sn_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ use cyfs_base::*;
pub struct SNDirParser;

impl SNDirParser {
pub fn parse(id: &ObjectId, object_raw: &[u8]) -> BuckyResult<Vec<(DeviceId, Device)>> {
pub fn parse(id: Option<&ObjectId>, object_raw: &[u8]) -> BuckyResult<Vec<(DeviceId, Device)>> {
let dir = Dir::clone_from_slice(&object_raw)?;
let id = id.cloned().unwrap_or(dir.desc().calculate_id());

let mut sn_list = vec![];
match dir.desc().content().obj_list() {
NDNObjectInfo::ObjList(list) => {
for (path, node) in list.object_map() {
let path = path.trim_start_matches('/');
if path.starts_with("list/") {
let (sn_id, buf) = Self::load_sn_node(id, &dir, &node)?;
let (sn_id, buf) = Self::load_sn_node(&id, &dir, &node)?;
match Device::clone_from_slice(&buf) {
Ok(device) => {
let real_id = device.desc().device_id();
Expand Down Expand Up @@ -176,7 +177,7 @@ mod test {
let dir = SNDirGenerator::gen_from_dir(&None, &root).unwrap();
let object_raw = dir.to_vec().unwrap();
let dir_id = dir.desc().calculate_id();
let list = SNDirParser::parse(&dir_id, object_raw).unwrap();
let list = SNDirParser::parse(Some(&dir_id), &object_raw).unwrap();
for item in list {
info!("got sn item: {}", item.0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/misc/cyfs-monitor/src/case/sn_online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl MonitorRunner for SNOnlineMonitor {

//TODO:需要的时候可以选择和gateway用同一个bdt stack
let mut init_sn_peers = vec![];
let list = cyfs_util::get_default_sn_desc().to_owned();
let list = cyfs_util::get_sn_desc().to_owned();
for (id, sn) in list {
device.body_mut().as_mut().unwrap().content_mut().mut_sn_list().push(id);
init_sn_peers.push(sn);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/cyfs-client/src/named_data_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl NamedCacheClient {

//TODO:需要的时候可以选择和gateway用同一个bdt stack
let mut init_sn_peers = vec![];
let list = cyfs_util::get_default_sn_desc().to_owned();
let list = cyfs_util::get_sn_desc().to_owned();
for (id, sn) in list {
self.desc.get_mut().unwrap().body_mut().as_mut().unwrap().content_mut().mut_sn_list().push(id);
init_sn_peers.push(sn);
Expand Down

0 comments on commit 5db6d10

Please sign in to comment.