Skip to content

Commit

Permalink
MON-14908 servicegroups and hostgroups are cleaned later
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-christophe81 committed Sep 23, 2022
1 parent b9c1440 commit 108c94c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ class conflict_manager {
absl::flat_hash_map<std::pair<uint64_t, uint16_t>, uint64_t> _severity_cache;
absl::flat_hash_map<std::pair<uint64_t, uint16_t>, uint64_t> _tags_cache;

std::mutex _group_clean_timer_m;
asio::system_timer _group_clean_timer;

std::unordered_set<uint32_t> _hostgroup_cache;
std::unordered_set<uint32_t> _servicegroup_cache;

Expand Down Expand Up @@ -346,6 +349,7 @@ class conflict_manager {
void _load_deleted_instances();
void _load_caches();
void _clean_tables(uint32_t instance_id);
void _clean_group_table();
void _prepare_hg_insupdate_statement();
void _prepare_sg_insupdate_statement();
void _finish_action(int32_t conn, uint32_t action);
Expand Down
3 changes: 3 additions & 0 deletions broker/storage/src/conflict_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ conflict_manager::conflict_manager(database_config const& dbcfg,
_instance_timeout{instance_timeout},
_stats{stats::center::instance().register_conflict_manager()},
_ref_count{0},
_group_clean_timer{pool::io_context()},
_oldest_timestamp{std::numeric_limits<time_t>::max()} {
log_v2::sql()->debug("conflict_manager: class instanciation");
stats::center::instance().update(&ConflictManagerStats::set_loop_timeout,
Expand All @@ -110,6 +111,8 @@ conflict_manager::conflict_manager(database_config const& dbcfg,

conflict_manager::~conflict_manager() {
log_v2::sql()->debug("conflict_manager: destruction");
std::lock_guard<std::mutex> l(_group_clean_timer_m);
_group_clean_timer.cancel();
}

/**
Expand Down
35 changes: 35 additions & 0 deletions broker/storage/src/conflict_manager_sql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ using namespace com::centreon::broker::storage;
* @param[in] instance_id Instance ID to remove.
*/
void conflict_manager::_clean_tables(uint32_t instance_id) {
// no hostgroup and servicegroup clean during this function
{
std::lock_guard<std::mutex> l(_group_clean_timer_m);
_group_clean_timer.cancel();
}

/* Database version. */

_finish_action(-1, -1);
Expand Down Expand Up @@ -168,6 +174,35 @@ void conflict_manager::_clean_tables(uint32_t instance_id) {
_mysql.run_query(query, database::mysql_error::clean_customvariables, false,
conn);
_add_action(conn, actions::custom_variables);

std::lock_guard<std::mutex> l(_group_clean_timer_m);
_group_clean_timer.expires_after(std::chrono::minutes(1));
_group_clean_timer.async_wait([this](const asio::error_code& err) {
if (!err) {
_clean_group_table();
}
});
}

void conflict_manager::_clean_group_table() {
int32_t conn = _mysql.choose_best_connection(-1);
/* Remove host groups. */
log_v2::sql()->debug("conflict_manager: remove empty host groups ");
_mysql.run_query(
"DELETE hg FROM hostgroups AS hg LEFT JOIN hosts_hostgroups AS hhg ON "
"hg.hostgroup_id=hhg.hostgroup_id WHERE hhg.hostgroup_id IS NULL",
database::mysql_error::clean_empty_hostgroups, false, conn);
_add_action(conn, actions::hostgroups);

/* Remove service groups. */
log_v2::sql()->debug("conflict_manager: remove empty service groups");

_mysql.run_query(
"DELETE sg FROM servicegroups AS sg LEFT JOIN services_servicegroups as "
"ssg ON sg.servicegroup_id=ssg.servicegroup_id WHERE ssg.servicegroup_id "
"IS NULL",
database::mysql_error::clean_empty_servicegroups, false, conn);
_add_action(conn, actions::servicegroups);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ class stream : public io::stream {

absl::flat_hash_map<std::pair<uint64_t, uint64_t>, uint64_t> _resource_cache;

std::mutex _group_clean_timer_m;
asio::system_timer _group_clean_timer;

absl::flat_hash_set<uint32_t> _hostgroup_cache;
absl::flat_hash_set<uint32_t> _servicegroup_cache;

Expand Down Expand Up @@ -359,6 +362,7 @@ class stream : public io::stream {
void _load_deleted_instances();
void _load_caches();
void _clean_tables(uint32_t instance_id);
void _clean_group_table();
void _prepare_hg_insupdate_statement();
void _prepare_sg_insupdate_statement();
void _finish_action(int32_t conn, uint32_t action);
Expand Down
5 changes: 5 additions & 0 deletions broker/unified_sql/src/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ stream::stream(const database_config& dbcfg,
_stop_check_queues{false},
_check_queues_stopped{false},
_stats{stats::center::instance().register_conflict_manager()},
_group_clean_timer{pool::io_context()},
_oldest_timestamp{std::numeric_limits<time_t>::max()} {
log_v2::sql()->debug("unified sql: stream class instanciation");
stats::center::instance().execute([stats = _stats,
Expand All @@ -146,6 +147,10 @@ stream::stream(const database_config& dbcfg,
}

stream::~stream() noexcept {
{
std::lock_guard<std::mutex> l(_group_clean_timer_m);
_group_clean_timer.cancel();
}
std::promise<void> p;
asio::post(_queues_timer.get_executor(), [this, &p] {
_queues_timer.cancel();
Expand Down
35 changes: 35 additions & 0 deletions broker/unified_sql/src/stream_sql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ static inline bool is_not_zero(const int64_t& value) {
* @param[in] instance_id Instance ID to remove.
*/
void stream::_clean_tables(uint32_t instance_id) {
// no hostgroup and servicegroup clean during this function
{
std::lock_guard<std::mutex> l(_group_clean_timer_m);
_group_clean_timer.cancel();
}

/* Database version. */

int32_t conn;
Expand Down Expand Up @@ -190,6 +196,35 @@ void stream::_clean_tables(uint32_t instance_id) {
_mysql.run_query(query, database::mysql_error::clean_customvariables, false,
conn);
_add_action(conn, actions::custom_variables);

std::lock_guard<std::mutex> l(_group_clean_timer_m);
_group_clean_timer.expires_after(std::chrono::minutes(1));
_group_clean_timer.async_wait([this](const asio::error_code& err) {
if (!err) {
_clean_group_table();
}
});
}

void stream::_clean_group_table() {
int32_t conn = _mysql.choose_best_connection(-1);
/* Remove host groups. */
log_v2::sql()->debug("unified_sql: remove empty host groups ");
_mysql.run_query(
"DELETE hg FROM hostgroups AS hg LEFT JOIN hosts_hostgroups AS hhg ON "
"hg.hostgroup_id=hhg.hostgroup_id WHERE hhg.hostgroup_id IS NULL",
database::mysql_error::clean_empty_hostgroups, false, conn);
_add_action(conn, actions::hostgroups);

/* Remove service groups. */
log_v2::sql()->debug("unified_sql: remove empty service groups");

_mysql.run_query(
"DELETE sg FROM servicegroups AS sg LEFT JOIN services_servicegroups as "
"ssg ON sg.servicegroup_id=ssg.servicegroup_id WHERE ssg.servicegroup_id "
"IS NULL",
database::mysql_error::clean_empty_servicegroups, false, conn);
_add_action(conn, actions::servicegroups);
}

/**
Expand Down

0 comments on commit 108c94c

Please sign in to comment.