From 6ed178b3679e4e1d546009a1895feb0b228ab148 Mon Sep 17 00:00:00 2001 From: Huamonarch Date: Wed, 13 May 2026 12:59:16 +0800 Subject: [PATCH] Fix: protect update_history_times snapshot restore from DB failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three fixes in update_history_times(): 1. Wrap DB operations in try-catch — exception no longer skips the snapshot restore, preventing permanent loss of accumulated counts 2. Treat get_history_times() -1 return (DB failure) as skip, not as "record exists" → no more silent UPDATE on non-existent rows 3. Only call update_static and advance last_load_time_ on success, so a failed persist retries on the next cycle instead of waiting another rw_time_ minutes --- eqpalg/algs/exp_times.cc | 79 +++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/eqpalg/algs/exp_times.cc b/eqpalg/algs/exp_times.cc index 7b6cddf..a04bc57 100644 --- a/eqpalg/algs/exp_times.cc +++ b/eqpalg/algs/exp_times.cc @@ -102,14 +102,14 @@ AlarmInfo ExpTimes::mon_proc() { << ",last_load_time_:" << mix_cc::mix_time_t(last_load_time_).to_formatted_time() << ",rw_time:" << rw_time_ << "min" << endl; - update_history_times(); - logger_->Debug() << "exp_type:" << exp_type_ - << ",update_history_times(),shear_times:" - << rule_stat_.shear_times - << ",running_time:" << rule_stat_.running_time - << ",rw_time:" << rw_time_ << "min" << endl; - /*更新时间标记*/ - last_load_time_ = this->now_time_; + if (update_history_times() == 0) { + logger_->Debug() << "exp_type:" << exp_type_ + << ",update_history_times(),shear_times:" + << rule_stat_.shear_times + << ",running_time:" << rule_stat_.running_time + << ",rw_time:" << rw_time_ << "min" << endl; + last_load_time_ = this->now_time_; + } } if (update_times() == 0) { if (check_alarm()) { @@ -255,35 +255,48 @@ int ExpTimes::update_history_times() { << std::to_string(this->act_started_) << ",now_times:" << now_times << ",now_used_time:" << now_used_time << std::endl; - if (get_history_times() == -2) { - // insert into - logger_->Debug() << "首次存入!" << endl; - this->insert_history_times(now_times, now_used_time); - } else { - T_RULE_SAMPLE_1D trs1a; - if (exp_type_ == ExpType::OccTimesAcc) { - this->rule_stat_.current_value = now_times; - exec(update(trs1a) - .set(trs1a.Count() = now_times, trs1a.X1() = 2, - trs1a.Flag() = this->exp_type_) - .where(trs1a.RuleId() == this->rule_id_)); - logger_->Debug() << "update_history_times(),update,now_times:" - << now_times << endl; - } else if (exp_type_ == ExpType::HoldTimeAcc) { - this->rule_stat_.current_value = now_used_time; - exec(update(trs1a) - .set(trs1a.X1() = now_used_time, - trs1a.Count() = 2, - trs1a.Flag() = this->exp_type_) - .where(trs1a.RuleId() == this->rule_id_)); - logger_->Debug() << "update_history_times(),update,now_used_time:" - << now_used_time << endl; + int ret = -1; + try { + int hist_ret = get_history_times(); + if (hist_ret == -1) { + logger_->Error() << "get_history_times() db2 查询失败,跳过本次持久化" + << std::endl; + } else if (hist_ret == -2) { + logger_->Debug() << "首次存入!" << endl; + this->insert_history_times(now_times, now_used_time); + ret = 0; + } else { + T_RULE_SAMPLE_1D trs1a; + if (exp_type_ == ExpType::OccTimesAcc) { + this->rule_stat_.current_value = now_times; + exec(update(trs1a) + .set(trs1a.Count() = now_times, trs1a.X1() = 2, + trs1a.Flag() = this->exp_type_) + .where(trs1a.RuleId() == this->rule_id_)); + logger_->Debug() << "update_history_times(),update,now_times:" + << now_times << endl; + } else if (exp_type_ == ExpType::HoldTimeAcc) { + this->rule_stat_.current_value = now_used_time; + exec(update(trs1a) + .set(trs1a.X1() = now_used_time, + trs1a.Count() = 2, + trs1a.Flag() = this->exp_type_) + .where(trs1a.RuleId() == this->rule_id_)); + logger_->Debug() << "update_history_times(),update,now_used_time:" + << now_used_time << endl; + } + ret = 0; } + if (ret == 0) { + SingletonTemp::GetInstance().update_static(this->rule_id_, true); + } + } catch (const std::exception &e) { + logger_->Error() << "update_history_times() db2 操作异常: " << e.what() + << std::endl; } this->rule_stat_.shear_times = now_times; this->rule_stat_.running_time = now_used_time; - SingletonTemp::GetInstance().update_static(this->rule_id_, true); - return 0; + return ret; } int ExpTimes::insert_history_times(int64_t now_times, double now_used_time) {