From e3df7d05b8ab5346c08d5a4d654074225abd68e1 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Mon, 10 Jul 2023 21:52:36 +0800
Subject: [PATCH] fix: simply code a bit, delete an annoying log, and don't set
 frame if we didn't do anything

---
 core/sysmaster/unit/runtime.rs | 70 ++++++++++++++++++----------------
 core/sysmaster/unit/uload.rs   |  2 -
 2 files changed, 38 insertions(+), 34 deletions(-)

diff --git a/core/sysmaster/unit/runtime.rs b/core/sysmaster/unit/runtime.rs
index 5c4f45e..24833a5 100644
--- a/core/sysmaster/unit/runtime.rs
+++ b/core/sysmaster/unit/runtime.rs
@@ -251,7 +251,12 @@ impl UnitRTData {
     }
 
     pub(self) fn dispatch_load_queue(&self) {
-        log::trace!("dispatch load queue");
+        if self.load_queue.borrow().is_empty() {
+            self.dispatch_target_dep_queue();
+            return;
+        }
+
+        log::debug!("Dispatching load queue");
 
         self.reli
             .set_last_frame2(ReliLastFrame::Queue as u32, ReliLastQue::Load as u32);
@@ -260,34 +265,27 @@ impl UnitRTData {
             //unitX pop from the load queue and then no need the ref of load queue
             //the unitX load process will borrow load queue as mut again
             // pop
-            let first_unit = self.load_queue.borrow_mut().pop_front();
-            match first_unit {
+            let unit = match self.load_queue.borrow_mut().pop_front() {
                 None => break,
-                Some(unit) => {
-                    // record + action
-                    self.reli.set_last_unit(unit.id());
-                    match unit.load() {
-                        Ok(()) => {
-                            let load_state = unit.load_state();
-                            if load_state == UnitLoadState::Loaded {
-                                self.push_target_dep_queue(Rc::clone(&unit));
-                            }
-                        }
-                        Err(e) => {
-                            log::error!("load unit [{}] failed: {}", unit.id(), e.to_string());
-                        }
-                    }
-                    self.reli.clear_last_unit();
-                }
+                Some(v) => v,
+            };
+
+            log::debug!("Loading unit: {}", unit.id());
+            self.reli.set_last_unit(unit.id());
+            if let Err(e) = unit.load() {
+                log::error!("Failed to load unit [{}]: {e}", unit.id());
             }
+
+            let load_state = unit.load_state();
+            if load_state == UnitLoadState::Loaded {
+                self.push_target_dep_queue(Rc::clone(&unit));
+            }
+
+            self.reli.clear_last_unit();
         }
-        self.reli.clear_last_frame();
 
-        log::trace!("dispatch target dep queue");
-        self.reli
-            .set_last_frame2(ReliLastFrame::Queue as u32, ReliLastQue::TargetDeps as u32);
-        self.dispatch_target_dep_queue();
         self.reli.clear_last_frame();
+        self.dispatch_target_dep_queue();
     }
 
     pub(self) fn unit_add_dependency(
@@ -307,17 +305,25 @@ impl UnitRTData {
     }
 
     fn dispatch_target_dep_queue(&self) {
+        if self.target_dep_queue.borrow().is_empty() {
+            return;
+        }
+
+        log::debug!("Dispatching target dep queue");
+        self.reli
+            .set_last_frame2(ReliLastFrame::Queue as u32, ReliLastQue::TargetDeps as u32);
+
         loop {
-            let first_unit = self.target_dep_queue.borrow_mut().pop_front();
-            match first_unit {
+            let unit = match self.target_dep_queue.borrow_mut().pop_front() {
                 None => break,
-                Some(unit) => {
-                    self.reli.set_last_unit(unit.id());
-                    dispatch_target_dep_unit(&self.db, &unit);
-                    self.reli.clear_last_unit();
-                }
-            }
+                Some(v) => v,
+            };
+            self.reli.set_last_unit(unit.id());
+            dispatch_target_dep_unit(&self.db, &unit);
+            self.reli.clear_last_unit();
         }
+
+        self.reli.clear_last_frame();
     }
 
     fn push_target_dep_queue(&self, unit: Rc<UnitX>) {
diff --git a/core/sysmaster/unit/uload.rs b/core/sysmaster/unit/uload.rs
index 382b2c0..fa5f616 100644
--- a/core/sysmaster/unit/uload.rs
+++ b/core/sysmaster/unit/uload.rs
@@ -140,9 +140,7 @@ impl UnitLoadData {
 
     pub(self) fn load_unit(&self, name: &str) -> Option<Rc<UnitX>> {
         self.prepare_unit(name).map(|u| {
-            log::debug!("Try to load {name} by dispatching the load queue.");
             self.rt.dispatch_load_queue();
-            log::debug!("The loading state of {name}: {:?}", u.load_state());
             u
         })
     }
-- 
2.33.0

