diff --git a/CHANGELOG.md b/CHANGELOG.md index 4956bd2cc..9dfa94cbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ### Fixes +*lua* + +The Lua cache should no more disappear. + *tcp* The flush() method from tcp\_connection could get stuck in cases of retention. diff --git a/bam/src/configuration/applier/ba.cc b/bam/src/configuration/applier/ba.cc index d97f722d9..1e48c558d 100644 --- a/bam/src/configuration/applier/ba.cc +++ b/bam/src/configuration/applier/ba.cc @@ -227,6 +227,7 @@ std::shared_ptr applier::ba::_ba_service( s->host_id = host_id; s->service_id = service_id; s->service_description = fmt::format("ba_{}", ba_id); + s->display_name = s->service_description; s->last_update = time(nullptr); return s; } diff --git a/core/src/config/applier/endpoint.cc b/core/src/config/applier/endpoint.cc index 02d6ad9e4..719581085 100644 --- a/core/src/config/applier/endpoint.cc +++ b/core/src/config/applier/endpoint.cc @@ -1,5 +1,5 @@ /* -** Copyright 2011-2012,2015,2017 Centreon +** Copyright 2011-2012,2015,2017-2021 Centreon ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. diff --git a/lua/src/connector.cc b/lua/src/connector.cc index 5f8fb99df..dde8492ff 100644 --- a/lua/src/connector.cc +++ b/lua/src/connector.cc @@ -18,7 +18,6 @@ #include "com/centreon/broker/lua/connector.hh" #include -#include #include "com/centreon/broker/exceptions/msg.hh" #include "com/centreon/broker/lua/stream.hh" diff --git a/lua/src/macro_cache.cc b/lua/src/macro_cache.cc index 5c181d97b..7738ad7d9 100644 --- a/lua/src/macro_cache.cc +++ b/lua/src/macro_cache.cc @@ -442,34 +442,7 @@ void macro_cache::write(std::shared_ptr const& data) { void macro_cache::_process_instance(std::shared_ptr const& data) { std::shared_ptr const& in = std::static_pointer_cast(data); - uint32_t poller_id = in->poller_id; - - std::unordered_set hosts_removed; - for (auto it = _hosts.begin(), end = _hosts.end(); it != end;) { - if (it->second->poller_id == poller_id) { - hosts_removed.insert(it->second->host_id); - it = _hosts.erase(it); - } else - ++it; - } - - for (uint64_t id : hosts_removed) { - auto it(_host_group_members.lower_bound({id, 0})); - while (it != _host_group_members.end() && it->first.first == id) { - it = _host_group_members.erase(it); - } - } - - std::unordered_set > services_removed; - for (auto it(_services.begin()), end(_services.end()); it != end;) { - if (hosts_removed.count(it->second->host_id)) { - services_removed.insert(it->first); - it = _services.erase(it); - } else - ++it; - } - - _instances[poller_id] = in; + _instances[in->poller_id] = in; } /** @@ -482,7 +455,10 @@ void macro_cache::_process_host(std::shared_ptr const& data) { std::static_pointer_cast(data); logging::debug(logging::medium) << "lua: processing host '" << h->host_name << "' of id " << h->host_id; - _hosts[h->host_id] = h; + if (h->enabled) + _hosts[h->host_id] = h; + else + _hosts.erase(h->host_id); } /** @@ -529,7 +505,10 @@ void macro_cache::_process_service(std::shared_ptr const& data) { << "lua: processing service (" << s->host_id << ", " << s->service_id << ") " << "(description: " << s->service_description << ")"; - _services[{s->host_id, s->service_id}] = s; + if (s->enabled) + _services[{s->host_id, s->service_id}] = s; + else + _services.erase({s->host_id, s->service_id}); } /** diff --git a/lua/test/lua.cc b/lua/test/lua.cc index d13caf901..667a936c7 100644 --- a/lua/test/lua.cc +++ b/lua/test/lua.cc @@ -1002,85 +1002,6 @@ TEST_F(LuaTest, ServiceGroupCacheTest) { RemoveFile("/tmp/log"); } -// When a query for service groups is made -// And the cache does know about them -// Then an array is returned by the lua method. -TEST_F(LuaTest, SetNewInstance) { - std::map conf; - std::string filename("/tmp/cache_test.lua"); - std::shared_ptr sg(new neb::service_group); - sg->id = 16; - sg->name = "centreon1"; - _cache->write(sg); - sg.reset(new neb::service_group); - sg->id = 17; - sg->name = "centreon2"; - _cache->write(sg); - std::shared_ptr hst(new neb::host); - hst->host_id = 22; - hst->host_name = "host_centreon"; - hst->poller_id = 3; - _cache->write(hst); - std::shared_ptr hg(new neb::host_group); - hg->id = 19; - hg->name = "hg1"; - _cache->write(hg); - std::shared_ptr svc(new neb::service); - svc->service_id = 17; - svc->host_id = 22; - svc->host_name = "host_centreon"; - svc->service_description = "service_description"; - _cache->write(svc); - std::shared_ptr hmember(new neb::host_group_member); - hmember->host_id = 22; - hmember->poller_id = 3; - hmember->enabled = true; - hmember->group_id = 19; - hmember->group_name = "hg1"; - _cache->write(hmember); - std::shared_ptr member( - new neb::service_group_member); - member->host_id = 22; - member->service_id = 17; - member->poller_id = 3; - member->enabled = false; - member->group_id = 16; - member->group_name = "seize"; - _cache->write(member); - member.reset(new neb::service_group_member); - member->host_id = 22; - member->service_id = 17; - member->poller_id = 3; - member->enabled = true; - member->group_id = 17; - member->group_name = "dix-sept"; - _cache->write(member); - - std::shared_ptr ib(new neb::instance); - ib->broker_id = 42; - ib->engine = "engine name"; - ib->is_running = true; - ib->poller_id = 3; - ib->name = "MyPoller"; - _cache->write(ib); - - CreateScript(filename, - "function init(conf)\n" - " broker_log:set_parameters(3, '/tmp/log')\n" - " local s = broker_cache:get_service_description(22, 17)\n" - " broker_log:info(1, 'service description ' .. tostring(s))\n" - "end\n\n" - "function write(d)\n" - "end\n"); - std::unique_ptr binding(new luabinding(filename, conf, *_cache)); - std::string lst(ReadFile("/tmp/log")); - - ASSERT_NE(std::string::npos, lst.find("service description nil")); - - RemoveFile(filename); - RemoveFile("/tmp/log"); -} - // When a query for bvs containing a ba is made // And the cache does know about them // Then an array with bvs id is returned by the lua method. diff --git a/neb/CMakeLists.txt b/neb/CMakeLists.txt index 181e155ec..6fc66fa32 100644 --- a/neb/CMakeLists.txt +++ b/neb/CMakeLists.txt @@ -109,7 +109,6 @@ add_library("${NEB}" SHARED # Main source. "${SRC_DIR}/broker.cc" "${SRC_DIR}/downtime_scheduler.cc" - "${SRC_DIR}/node_cache.cc" "${SRC_DIR}/node_id.cc" "${SRC_DIR}/timeperiod_serializable.cc" "${SRC_DIR}/downtime_serializable.cc" @@ -117,7 +116,6 @@ add_library("${NEB}" SHARED # Inc "${INC_DIR}/com/centreon/broker/neb/downtime_scheduler.hh" - "${INC_DIR}/com/centreon/broker/neb/node_cache.hh" "${INC_DIR}/com/centreon/broker/neb/node_id.hh" "${INC_DIR}/com/centreon/broker/neb/timeperiod_serializable.hh" "${INC_DIR}/com/centreon/broker/neb/downtime_serializable.hh" diff --git a/neb/inc/com/centreon/broker/neb/node_cache.hh b/neb/inc/com/centreon/broker/neb/node_cache.hh deleted file mode 100644 index a10d015de..000000000 --- a/neb/inc/com/centreon/broker/neb/node_cache.hh +++ /dev/null @@ -1,75 +0,0 @@ -/* -** Copyright 2015 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 : contact@centreon.com -*/ - -#ifndef CCB_NEB_NODE_CACHE_HH -#define CCB_NEB_NODE_CACHE_HH - -#include -#include -#include -#include "com/centreon/broker/io/endpoint.hh" -#include "com/centreon/broker/misc/pair.hh" -#include "com/centreon/broker/namespace.hh" -#include "com/centreon/broker/neb/host.hh" -#include "com/centreon/broker/neb/host_status.hh" -#include "com/centreon/broker/neb/node_id.hh" -#include "com/centreon/broker/neb/service.hh" -#include "com/centreon/broker/neb/service_status.hh" -#include "com/centreon/broker/persistent_cache.hh" - -CCB_BEGIN() - -namespace neb { -/** - * @class node_cache node_cache.hh "com/centreon/broker/neb/node_cache.hh" - * @brief Cache node data. - */ -class node_cache { - public: - node_cache(); - node_cache(node_cache const& other); - ~node_cache(); - node_cache& operator=(node_cache const& other); - - void write(std::shared_ptr const& d); - void serialize(std::shared_ptr cache); - node_id get_node_by_names(std::string const& host_name, - std::string const& service_description); - unsigned short get_current_state(node_id id); - neb::host_status* get_host_status(node_id id); - neb::service_status* get_service_status(node_id id); - - private: - std::unordered_map _hosts; - std::unordered_map _services; - std::unordered_map _host_statuses; - std::unordered_map _service_statuses; - std::unordered_map, node_id> - _names_to_node; - std::unordered_map _previous_statuses; - - void _process_host(neb::host const& hst); - void _process_service(neb::service const& svc); - void _process_host_status(neb::host_status const& hst); - void _process_service_status(neb::service_status const& sst); -}; -} // namespace neb - -CCB_END() - -#endif // !CCB_NEB_NODE_CACHE_HH diff --git a/neb/src/node_cache.cc b/neb/src/node_cache.cc deleted file mode 100644 index 18a33a6b8..000000000 --- a/neb/src/node_cache.cc +++ /dev/null @@ -1,238 +0,0 @@ -/* -** Copyright 2015 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 : contact@centreon.com -*/ - -#include "com/centreon/broker/neb/node_cache.hh" -#include -#include "com/centreon/broker/logging/logging.hh" - -using namespace com::centreon::broker; -using namespace com::centreon::broker::neb; - -/** - * Default constructor. - */ -node_cache::node_cache() {} - -/** - * Copy constructor. - * - * @param[in] other Object to copy. - */ -node_cache::node_cache(node_cache const& other) - : _hosts(other._hosts), - _services(other._services), - _host_statuses(other._host_statuses), - _service_statuses(other._service_statuses), - _names_to_node(other._names_to_node) {} - -/** - * Destructor. - */ -node_cache::~node_cache() {} - -/** - * Assignment operator. - * - * @param[in] other Object to copy. - * - * @return This object. - */ -node_cache& node_cache::operator=(node_cache const& other) { - if (this != &other) { - _hosts = other._hosts; - _services = other._services; - _host_statuses = other._host_statuses; - _service_statuses = other._service_statuses; - _names_to_node = other._names_to_node; - } - return (*this); -} - -/** - * Write data to the node cache. - * - * @param[in] d The data. - */ -void node_cache::write(std::shared_ptr const& d) { - if (!d) - return; - - switch (d->type()) { - case neb::host::static_type(): - _process_host(*std::static_pointer_cast(d)); - break; - case neb::service::static_type(): - _process_service(*std::static_pointer_cast(d)); - break; - case neb::host_status::static_type(): - _process_host_status( - *std::static_pointer_cast(d)); - break; - case neb::service_status::static_type(): - _process_service_status( - *std::static_pointer_cast(d)); - break; - } -} - -/** - * Serialize the node cache. - * - * @param[in] cache The cache. - */ -void node_cache::serialize(std::shared_ptr cache) { - if (cache == nullptr) - return; - for (std::unordered_map::const_iterator - it = _hosts.begin(), - end = _hosts.end(); - it != end; ++it) - cache->add(std::make_shared(it->second)); - for (std::unordered_map::const_iterator - it = _services.begin(), - end = _services.end(); - it != end; ++it) - cache->add(std::make_shared(it->second)); - for (std::unordered_map::const_iterator - it = _host_statuses.begin(), - end = _host_statuses.end(); - it != end; ++it) - cache->add(std::make_shared(it->second)); - for (std::unordered_map::const_iterator - it = _service_statuses.begin(), - end = _service_statuses.end(); - it != end; ++it) - cache->add(std::make_shared(it->second)); -} - -/** - * Get a node by its names. - * - * @param[in] host_name The host name. - * @param[in] service_description The service description, or empty. - * - * @return The node id. - */ -node_id node_cache::get_node_by_names(std::string const& host_name, - std::string const& service_description) { - std::unordered_map, - node_id>::const_iterator found{ - _names_to_node.find(std::make_pair(host_name, service_description))}; - if (found != _names_to_node.end()) - return found->second; - else - return node_id(); -} - -/** - * Get the current state of a node, or zero. - * - * @param[in] id The id of the node. - * - * @return Current state, or zero. - */ -unsigned short node_cache::get_current_state(node_id id) { - if (id.is_host()) { - std::unordered_map::const_iterator found{ - _host_statuses.find(id)}; - return found != _host_statuses.end() ? found->second.last_hard_state : 0; - } else { - std::unordered_map::const_iterator found{ - _service_statuses.find(id)}; - return found != _service_statuses.end() ? found->second.last_hard_state : 0; - } -} - -/** - * Get a host status, or null. - * - * @param[in] id The id. - * - * @return A host status, or null. - */ -neb::host_status* node_cache::get_host_status(node_id id) { - std::unordered_map::iterator found{ - _host_statuses.find(id)}; - return found != _host_statuses.end() ? &found->second : nullptr; -} - -/** - * Get a service status, or null. - * - * @param[in] id The id. - * - * @return A service status, or null. - */ -neb::service_status* node_cache::get_service_status(node_id id) { - std::unordered_map::iterator found{ - _service_statuses.find(id)}; - return found != _service_statuses.end() ? &found->second : nullptr; -} - -/** - * Process a host event. - * - * @param[in] hst The host event. - */ -void node_cache::_process_host(neb::host const& hst) { - logging::debug(logging::medium) - << "node events: processing host declaration for (" << hst.host_id << ")"; - _hosts[node_id(hst.host_id)] = hst; - _names_to_node[std::make_pair(hst.host_name, "")] = node_id(hst.host_id); -} - -/** - * Process a service event. - * - * @param[in] svc The service event. - */ -void node_cache::_process_service(neb::service const& svc) { - logging::debug(logging::medium) - << "node events: processing service declaration for (" << svc.host_id - << ", " << svc.service_id << ")"; - _services[node_id(svc.host_id, svc.service_id)] = svc; - _names_to_node[std::make_pair(svc.host_name, svc.service_description)] = - node_id(svc.host_id, svc.service_id); -} - -/** - * Process a host status event. - * - * @param[in] hst Host status event. - */ -void node_cache::_process_host_status(neb::host_status const& hst) { - logging::debug(logging::medium) - << "node events: processing host status for (" << hst.host_id << ")"; - node_id id(hst.host_id); - _host_statuses[id] = hst; - return; -} - -/** - * Process a service status event. - * - * @param[in] sst Service status event. - */ -void node_cache::_process_service_status(neb::service_status const& sst) { - logging::debug(logging::medium) - << "node events: processing service status for (" << sst.host_id << ", " - << sst.service_id << ")"; - node_id id(sst.host_id, sst.service_id); - _service_statuses[id] = sst; - return; -}