Skip to content

Commit

Permalink
fix(broker/unified_sql): host dependencies where badly inserted.
Browse files Browse the repository at this point in the history
  • Loading branch information
bouda1 committed Feb 28, 2024
1 parent fb7787a commit a7cc092
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 57 deletions.
39 changes: 20 additions & 19 deletions broker/unified_sql/inc/com/centreon/broker/unified_sql/stream.hh
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/*
** Copyright 2019-2022 Centreon
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** For more information : [email protected]
*/
/**
* Copyright 2019-2024 Centreon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : [email protected]
*/

#ifndef CCB_UNIFIED_SQL_STREAM_HH
#define CCB_UNIFIED_SQL_STREAM_HH
#include <absl/container/flat_hash_map.h>
Expand Down Expand Up @@ -324,8 +325,8 @@ class stream : public io::stream {
database::mysql_stmt _flapping_status_insupdate;
database::mysql_stmt _host_check_update;
database::mysql_stmt _pb_host_check_update;
database::mysql_stmt _host_dependency_insupdate;
database::mysql_stmt _pb_host_dependency_insupdate;
database::mysql_stmt _host_exe_dependency_insupdate;
database::mysql_stmt _host_notif_dependency_insupdate;
database::mysql_stmt _host_group_insupdate;
database::mysql_stmt _pb_host_group_insupdate;
database::mysql_stmt _host_group_member_delete;
Expand Down
16 changes: 16 additions & 0 deletions broker/unified_sql/src/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,22 @@ void stream::_init_statements() {
"last_check=?," // 9: last_check
"output=? " // 10: output
"WHERE id=? AND parent_id=?"); // 11, 12: service_id and host_id
const std::string host_exe_dep_query(
"INSERT INTO hosts_hosts_dependencies (dependent_host_id, host_id, "
"dependency_period, execution_failure_options, inherits_parent) "
"SET(?,?,?,?,?) ON DUPLICATE KEY UPDATE "
"dependency_period=VALUES(dependency_period), "
"execution_failure_options=VALUES(execution_failure_options), "
"inherits_parent=VALUES(inherits_parent)");
_host_exe_dependency_insupdate = _mysql.prepare_query(host_exe_dep_query);
const std::string host_notif_dep_query(
"INSERT INTO hosts_hosts_dependencies (dependent_host_id, host_id, "
"dependency_period, notification_failure_options, inherits_parent) "
"SET(?,?,?,?,?) ON DUPLICATE KEY UPDATE "
"dependency_period=VALUES(dependency_period), "
"notification_failure_options=VALUES(notification_failure_options), "
"inherits_parent=VALUES(inherits_parent)");
_host_notif_dependency_insupdate = _mysql.prepare_query(host_notif_dep_query);
if (_store_in_hosts_services) {
if (_bulk_prepared_statement) {
auto hu = std::make_unique<database::mysql_bulk_stmt>(hscr_query);
Expand Down
85 changes: 47 additions & 38 deletions broker/unified_sql/src/stream_sql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1212,19 +1212,29 @@ void stream::_process_host_dependency(const std::shared_ptr<io::data>& d) {
hd.execution_failure_options,
hd.notification_failure_options);

// Prepare queries.
if (!_host_dependency_insupdate.prepared()) {
query_preparator::event_unique unique;
unique.insert("host_id");
unique.insert("dependent_host_id");
query_preparator qp(neb::host_dependency::static_type(), unique);
_host_dependency_insupdate = qp.prepare_insert_or_update(_mysql);
}

// Process object.
_host_dependency_insupdate << hd;
_mysql.run_statement(_host_dependency_insupdate,
database::mysql_error::store_host_dependency, conn);
if (!hd.execution_failure_options.empty()) {
_host_exe_dependency_insupdate.bind_value_as_i32(0, hd.dependent_host_id);
_host_exe_dependency_insupdate.bind_value_as_i32(1, hd.host_id);
_host_exe_dependency_insupdate.bind_value_as_str(2, hd.dependency_period);
_host_exe_dependency_insupdate.bind_value_as_str(
3, hd.execution_failure_options);
_host_exe_dependency_insupdate.bind_value_as_tiny(4, hd.inherits_parent);
_mysql.run_statement(_host_exe_dependency_insupdate,
database::mysql_error::store_host_dependency, conn);
} else if (!hd.notification_failure_options.empty()) {
_host_notif_dependency_insupdate.bind_value_as_i32(0,
hd.dependent_host_id);
_host_notif_dependency_insupdate.bind_value_as_i32(1, hd.host_id);
_host_notif_dependency_insupdate.bind_value_as_str(2,
hd.dependency_period);
_host_notif_dependency_insupdate.bind_value_as_str(
3, hd.notification_failure_options);
_host_notif_dependency_insupdate.bind_value_as_tiny(4,
hd.inherits_parent);
_mysql.run_statement(_host_notif_dependency_insupdate,
database::mysql_error::store_host_dependency, conn);
}
_add_action(conn, actions::host_dependencies);
}
// Delete.
Expand Down Expand Up @@ -1269,33 +1279,32 @@ void stream::_process_pb_host_dependency(const std::shared_ptr<io::data>& d) {
hd.dependent_host_id(), hd.host_id(), hd.execution_failure_options(),
hd.notification_failure_options());

// Prepare queries.
if (!_pb_host_dependency_insupdate.prepared()) {
query_preparator::event_pb_unique unique{
{6, "host_id", io::protobuf_base::invalid_on_zero, 0},
{3, "dependent_host_id", io::protobuf_base::invalid_on_zero, 0}};
query_preparator qp(neb::pb_host_dependency::static_type(), unique);
_pb_host_dependency_insupdate = qp.prepare_insert_or_update_table(
_mysql, "hosts_hosts_dependencies ", /*space is mandatory to avoid
conflict with _process_host_dependency*/
{{3, "dependent_host_id", io::protobuf_base::invalid_on_zero, 0},
{6, "host_id", io::protobuf_base::invalid_on_zero, 0},
{2, "dependency_period", 0,
get_hosts_hosts_dependencies_col_size(
hosts_hosts_dependencies_dependency_period)},
{5, "execution_failure_options", 0,
get_hosts_hosts_dependencies_col_size(
hosts_hosts_dependencies_execution_failure_options)},
{7, "inherits_parent", 0, 0},
{8, "notification_failure_options", 0,
get_hosts_hosts_dependencies_col_size(
hosts_hosts_dependencies_notification_failure_options)}});
}

// Process object.
_pb_host_dependency_insupdate << hd_protobuf;
_mysql.run_statement(_pb_host_dependency_insupdate,
database::mysql_error::store_host_dependency, conn);
if (!hd.execution_failure_options().empty()) {
_host_exe_dependency_insupdate.bind_value_as_i32(0,
hd.dependent_host_id());
_host_exe_dependency_insupdate.bind_value_as_i32(1, hd.host_id());
_host_exe_dependency_insupdate.bind_value_as_str(2,
hd.dependency_period());
_host_exe_dependency_insupdate.bind_value_as_str(
3, hd.execution_failure_options());
_host_exe_dependency_insupdate.bind_value_as_tiny(4,
hd.inherits_parent());
_mysql.run_statement(_host_exe_dependency_insupdate,
database::mysql_error::store_host_dependency, conn);
} else if (!hd.notification_failure_options().empty()) {
_host_notif_dependency_insupdate.bind_value_as_i32(
0, hd.dependent_host_id());
_host_notif_dependency_insupdate.bind_value_as_i32(1, hd.host_id());
_host_notif_dependency_insupdate.bind_value_as_str(
2, hd.dependency_period());
_host_notif_dependency_insupdate.bind_value_as_str(
3, hd.notification_failure_options());
_host_notif_dependency_insupdate.bind_value_as_tiny(4,
hd.inherits_parent());
_mysql.run_statement(_host_notif_dependency_insupdate,
database::mysql_error::store_host_dependency, conn);
}
_add_action(conn, actions::host_dependencies);
}
// Delete.
Expand Down

0 comments on commit a7cc092

Please sign in to comment.