From c9069670a9e0d146a9ae55155a86b624577d69c3 Mon Sep 17 00:00:00 2001 From: licunlong Date: Tue, 12 Dec 2023 17:16:54 +0800 Subject: [PATCH 1/6] feature: adjust with reliability framework --- core/coms/mount/src/comm.rs | 12 ++++++++++++ core/coms/mount/src/config.rs | 31 ++++++++++++++++++++++++++++--- core/coms/mount/src/mng.rs | 20 ++++++++++++++++++++ core/coms/mount/src/rentry.rs | 31 +++++++++++++++++++++++++++++-- core/coms/mount/src/unit.rs | 13 ++++++++----- 5 files changed, 97 insertions(+), 10 deletions(-) diff --git a/core/coms/mount/src/comm.rs b/core/coms/mount/src/comm.rs index d0068d86..be8a1bdd 100644 --- a/core/coms/mount/src/comm.rs +++ b/core/coms/mount/src/comm.rs @@ -15,6 +15,8 @@ //! * Get the attributes of the unit object //! * Call relation: mount_ unit->mount_ mng->mount_ comm +use crate::rentry::SectionMount; + use super::rentry::{MountRe, MountState}; use core::rel::Reliability; use core::unit::{UmIf, UnitBase}; @@ -60,6 +62,16 @@ impl MountUnitComm { self.owner().map_or_else(|| "None".to_string(), |u| u.id()) } + pub(super) fn rentry_conf_insert(&self, mount: &SectionMount) { + if let Some(u) = self.owner() { + self.rentry().conf_insert(&u.id(), mount) + } + } + + pub(super) fn rentry_conf_get(&self) -> Option { + self.owner().map(|u| self.rentry().conf_get(&u.id()))? + } + pub(super) fn rentry_mng_insert(&self, state: MountState) { self.rentry().mng_insert(&self.get_owner_id(), state) } diff --git a/core/coms/mount/src/config.rs b/core/coms/mount/src/config.rs index 074deaff..15932c89 100644 --- a/core/coms/mount/src/config.rs +++ b/core/coms/mount/src/config.rs @@ -10,9 +10,9 @@ // NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. // See the Mulan PSL v2 for more details. // - -use core::error::Result; +#![allow(non_snake_case)] use core::unit::KillContext; +use core::{error::Result, rel::ReStation}; use std::{cell::RefCell, path::PathBuf, rc::Rc, str::FromStr}; use nix::sys::signal::Signal; @@ -21,11 +21,16 @@ use unit_parser::prelude::UnitConfig; use crate::{comm::MountUnitComm, rentry::SectionMount}; #[derive(UnitConfig, Default)] -#[allow(non_snake_case)] pub(super) struct MountConfigData { pub Mount: SectionMount, } +impl MountConfigData { + pub(self) fn new(Mount: SectionMount) -> MountConfigData { + MountConfigData { Mount } + } +} + #[allow(unused)] pub(super) struct MountConfig { // associated objects @@ -38,6 +43,26 @@ pub(super) struct MountConfig { mount_parameters_from_mountinfo: RefCell, } +impl ReStation for MountConfig { + // no input, no compensate + + // data + fn db_map(&self, reload: bool) { + if reload { + return; + } + if let Some(conf) = self.comm.rentry_conf_get() { + self.data.replace(MountConfigData::new(conf)); + } + } + + fn db_insert(&self) { + self.comm.rentry_conf_insert(&self.data.borrow().Mount); + } + + // reload: no external connections, no entry +} + #[derive(Clone)] pub(super) struct MountParameters { pub what: String, diff --git a/core/coms/mount/src/mng.rs b/core/coms/mount/src/mng.rs index 5d9842b1..a12e27b9 100644 --- a/core/coms/mount/src/mng.rs +++ b/core/coms/mount/src/mng.rs @@ -84,6 +84,20 @@ impl ReStation for MountMng { self.comm.rentry_mng_insert(self.state()); } + fn entry_coldplug(&self) { + if [MountState::Mounting, MountState::Unmounting].contains(&self.state()) { + if let Err(e) = self.enable_timer(self.timeout_usec()) { + log::error!("Failed to reenable timer: {}", e); + } + } + } + + fn entry_clear(&self) { + if let Err(e) = self.disable_timer() { + log::error!("Failed to disable timer: {}", e); + } + } + // reload: no external connections, no entry } @@ -350,6 +364,12 @@ impl MountMng { Ok(0) } + pub(super) fn disable_timer(&self) -> Result { + let events = self.comm.um().events(); + events.del_source(self.timer())?; + Ok(0) + } + pub(super) fn dispatch_timer(&self) -> i32 { log::info!("Dispatch timeout event for {}", self.comm.get_owner_id()); match self.state() { diff --git a/core/coms/mount/src/rentry.rs b/core/coms/mount/src/rentry.rs index d6142ef8..db07447e 100644 --- a/core/coms/mount/src/rentry.rs +++ b/core/coms/mount/src/rentry.rs @@ -22,6 +22,7 @@ use std::{collections::HashMap, path::PathBuf, rc::Rc}; const RELI_DB_HMOUNT_MNG: &str = "mntmng"; const RELI_DB_HMOUNTM_FRAME: &str = "mntm-frame"; +const RELI_DB_HMOUNTM_CONFIG: &str = "mntconf"; const RELI_LAST_KEY: u32 = 0; // singleton //Mount contains two states: unmounted Dead and mounted Mounted. The corresponding unit status is inactive and active @@ -80,6 +81,19 @@ bitflags! { } } +#[derive(Clone, Debug, Serialize, Deserialize)] +struct MountReConf { + mount: SectionMount, +} + +impl MountReConf { + fn new(mount: &SectionMount) -> MountReConf { + MountReConf { + mount: mount.clone(), + } + } +} + #[derive(Clone, Debug, Serialize, Deserialize)] struct MountReMng { state: MountState, @@ -99,6 +113,8 @@ pub(super) enum MountReFrame { struct MountReDb(ReDb); pub(super) struct MountRe { + conf: Rc>, // RELI_DB_ESERVICE_CONF; key: unit_id, data: config; + // database: multi-instance(N) mng: Rc>, // RELI_DB_HMOUNT_MNG; key: unit_id, data: state; @@ -110,11 +126,22 @@ impl MountRe { pub(super) fn new(relir: &Rc) -> MountRe { let mng = Rc::new(MountReDb(ReDb::new(relir, RELI_DB_HMOUNT_MNG))); let frame = Rc::new(MountReDb(ReDb::new(relir, RELI_DB_HMOUNTM_FRAME))); - let rentry = MountRe { mng, frame }; + let conf = Rc::new(MountReDb(ReDb::new(relir, RELI_DB_HMOUNTM_CONFIG))); + let rentry = MountRe { conf, mng, frame }; rentry.register(relir); rentry } + pub(super) fn conf_insert(&self, unit_id: &str, service: &SectionMount) { + let conf = MountReConf::new(service); + self.conf.0.insert(unit_id.to_string(), conf); + } + + pub(super) fn conf_get(&self, unit_id: &str) -> Option { + let conf = self.conf.0.get(&unit_id.to_string()); + conf.map(|c| c.mount) + } + pub(super) fn mng_insert(&self, unit_id: &str, state: MountState) { let mng = MountReMng::new(state); self.mng.0.insert(unit_id.to_string(), mng); @@ -192,7 +219,7 @@ impl ReDbTable for MountReDb { } } -#[derive(UnitSection, Default)] +#[derive(UnitSection, Default, Clone, Serialize, Deserialize, Debug)] #[allow(non_snake_case)] pub struct SectionMount { #[entry(default = String::new())] diff --git a/core/coms/mount/src/unit.rs b/core/coms/mount/src/unit.rs index 96ca0942..86b1fc55 100644 --- a/core/coms/mount/src/unit.rs +++ b/core/coms/mount/src/unit.rs @@ -29,8 +29,8 @@ use std::rc::Rc; struct MountUnit { comm: Rc, - mng: Rc, config: Rc, + mng: Rc, exec_ctx: Rc, } @@ -39,22 +39,25 @@ impl ReStation for MountUnit { // data fn db_map(&self, reload: bool) { + self.config.db_map(reload); + if !reload { + self.parse().unwrap(); + } self.mng.db_map(reload); } fn db_insert(&self) { + self.config.db_insert(); self.mng.db_insert(); } // reload: no external connections, entry-only fn entry_coldplug(&self) { - // rebuild external connections, like: timer, ... - // do nothing now + self.mng.entry_coldplug(); } fn entry_clear(&self) { - // release external connection, like: timer, ... - // do nothing now + self.mng.entry_clear(); } } -- Gitee From 2d2c4b07b54f9b649c0cdd6dbeb730e0af4e8e49 Mon Sep 17 00:00:00 2001 From: licunlong Date: Mon, 18 Dec 2023 10:59:12 +0800 Subject: [PATCH 2/6] fix: MountTimer is enabled automatically when we mount, no need to enable it explictly --- core/coms/mount/src/mng.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/core/coms/mount/src/mng.rs b/core/coms/mount/src/mng.rs index a12e27b9..06a0e8c0 100644 --- a/core/coms/mount/src/mng.rs +++ b/core/coms/mount/src/mng.rs @@ -85,11 +85,7 @@ impl ReStation for MountMng { } fn entry_coldplug(&self) { - if [MountState::Mounting, MountState::Unmounting].contains(&self.state()) { - if let Err(e) = self.enable_timer(self.timeout_usec()) { - log::error!("Failed to reenable timer: {}", e); - } - } + // nothing to do. } fn entry_clear(&self) { -- Gitee From 55b6689f66f91cccc09310b5a532b15893b6f47b Mon Sep 17 00:00:00 2001 From: licunlong Date: Mon, 18 Dec 2023 11:01:14 +0800 Subject: [PATCH 3/6] feature: add description for source, useful when debugging --- core/coms/mount/src/mng.rs | 4 ++++ core/sysmaster/src/job/entry.rs | 4 ++++ core/sysmaster/src/manager/alive_timer.rs | 5 +++++ libs/event/src/source.rs | 5 +++++ 4 files changed, 18 insertions(+) diff --git a/core/coms/mount/src/mng.rs b/core/coms/mount/src/mng.rs index 06a0e8c0..de00ea52 100644 --- a/core/coms/mount/src/mng.rs +++ b/core/coms/mount/src/mng.rs @@ -771,6 +771,10 @@ impl Source for MountTimer { let data: u64 = unsafe { std::mem::transmute(self) }; data } + + fn description(&self) -> String { + String::from("MountTimer") + } } // #[cfg(test)] diff --git a/core/sysmaster/src/job/entry.rs b/core/sysmaster/src/job/entry.rs index c62878fd..164486c8 100644 --- a/core/sysmaster/src/job/entry.rs +++ b/core/sysmaster/src/job/entry.rs @@ -149,6 +149,10 @@ impl Source for JobTimer { fn priority(&self) -> i8 { 0i8 } + + fn description(&self) -> String { + String::from("JobTimer") + } } #[derive(Clone)] diff --git a/core/sysmaster/src/manager/alive_timer.rs b/core/sysmaster/src/manager/alive_timer.rs index a325483a..b1333aa8 100644 --- a/core/sysmaster/src/manager/alive_timer.rs +++ b/core/sysmaster/src/manager/alive_timer.rs @@ -129,4 +129,9 @@ impl Source for AliveTimerData { let data: u64 = unsafe { std::mem::transmute(self) }; data } + + fn description(&self) -> String { + String::from("AliveTimer") + } + } diff --git a/libs/event/src/source.rs b/libs/event/src/source.rs index ed06ea90..b9d36a58 100644 --- a/libs/event/src/source.rs +++ b/libs/event/src/source.rs @@ -72,6 +72,11 @@ pub trait Source { /// The code of callback fn dispatch(&self, event: &Events) -> i32; + + /// The short description of this source + fn description(&self) -> String { + String::from("default") + } } // for HashSet -- Gitee From 35c9b3b85987f2b42e57cd1deecf4d84b025a9f2 Mon Sep 17 00:00:00 2001 From: licunlong Date: Mon, 18 Dec 2023 11:02:21 +0800 Subject: [PATCH 4/6] refactor: reduce cyclomatic complexity, no functional change --- libs/event/src/events.rs | 58 ++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/libs/event/src/events.rs b/libs/event/src/events.rs index 613a010a..1ef6afe0 100644 --- a/libs/event/src/events.rs +++ b/libs/event/src/events.rs @@ -515,16 +515,19 @@ impl EventsData { EventType::TimerRealtimeAlarm, EventType::TimerBoottimeAlarm, ] { - if let Some(next) = self.timer.next(&et) { - if self.timer.timerid(&et) >= next { - if !self.flush_timer(&et) { - return false; - } + let next = match self.timer.next(&et) { + None => continue, + Some(v) => v, + }; + if self.timer.timerid(&et) < next { + continue; + } + if !self.flush_timer(&et) { + return false; + } - while let Some(source) = self.timer.pop(&et) { - self.pending_push(source, 0); - } - } + while let Some(source) = self.timer.pop(&et) { + self.pending_push(source, 0); } } @@ -542,23 +545,26 @@ impl EventsData { EventType::TimerBoottimeAlarm, ] { self.timer.now(); - if let Some(next) = self.timer.next(&et) { - if self.timer.timerid(&et) >= next { - while let Some(source) = self.timer.pop(&et) { - self.pending_push(source, 0); - } - ret = true; - } else { - let new_value = self.timer.timer_stored(next); - let mut old_value = MaybeUninit::::zeroed(); - unsafe { - libc::timerfd_settime( - self.timerfd.get(&et).unwrap().as_raw_fd(), - libc::TFD_TIMER_ABSTIME, - &new_value, - old_value.as_mut_ptr(), - ); - } + let next = match self.timer.next(&et) { + None => continue, + Some(v) => v, + }; + + if self.timer.timerid(&et) >= next { + while let Some(source) = self.timer.pop(&et) { + self.pending_push(source, 0); + } + ret = true; + } else { + let new_value = self.timer.timer_stored(next); + let mut old_value = MaybeUninit::::zeroed(); + unsafe { + libc::timerfd_settime( + self.timerfd.get(&et).unwrap().as_raw_fd(), + libc::TFD_TIMER_ABSTIME, + &new_value, + old_value.as_mut_ptr(), + ); } } } -- Gitee From 6a9aefb5096548069e767e8726791ca0d0ff5701 Mon Sep 17 00:00:00 2001 From: licunlong Date: Mon, 18 Dec 2023 11:03:39 +0800 Subject: [PATCH 5/6] fix: we should compare the token() not priority() when remove a timer source This fix the bug, that if one timer is deleted, all the same type timers are deleted too. --- libs/event/src/timer.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libs/event/src/timer.rs b/libs/event/src/timer.rs index c5f020c2..bd2e4490 100644 --- a/libs/event/src/timer.rs +++ b/libs/event/src/timer.rs @@ -11,7 +11,6 @@ // See the Mulan PSL v2 for more details. use std::{ - cmp::Ordering, collections::{BinaryHeap, HashMap}, mem, rc::Rc, @@ -212,7 +211,7 @@ impl TimerInner { // let v = self.data.; let mut tmp = BinaryHeap::::new(); for clock_data in self.data.iter() { - if clock_data.source().cmp(&source) != Ordering::Equal { + if !clock_data.source().eq(&source) { tmp.push(clock_data.clone()); } } -- Gitee From c2ac63b5e14dad2d279af1c250ebe1af103b9112 Mon Sep 17 00:00:00 2001 From: licunlong Date: Mon, 18 Dec 2023 15:46:49 +0800 Subject: [PATCH 6/6] fix: cargo fmt --- core/sysmaster/src/manager/alive_timer.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/core/sysmaster/src/manager/alive_timer.rs b/core/sysmaster/src/manager/alive_timer.rs index b1333aa8..35d24e6c 100644 --- a/core/sysmaster/src/manager/alive_timer.rs +++ b/core/sysmaster/src/manager/alive_timer.rs @@ -133,5 +133,4 @@ impl Source for AliveTimerData { fn description(&self) -> String { String::from("AliveTimer") } - } -- Gitee