Use boost::multi_index instead of a priority queue.

This commit is contained in:
Gunnar Beutner 2012-08-04 13:49:25 +02:00
parent 85d912feb5
commit 1de42d08f2
3 changed files with 35 additions and 18 deletions

View File

@ -59,14 +59,17 @@ void CheckerComponent::CheckTimerHandler(void)
double now = Utility::GetTime();
long tasks = 0;
while (!m_Services.empty()) {
CheckerComponent::ServiceMultiSet::iterator it = m_Services.begin();
while (!m_IdleServices.empty()) {
typedef nth_index<ServiceSet, 1>::type CheckTimeView;
CheckTimeView& idx = boost::get<1>(m_IdleServices);
CheckTimeView::iterator it = idx.begin();
Service::Ptr service = *it;
if (service->GetNextCheck() > now)
break;
m_Services.erase(it);
idx.erase(it);
Logger::Write(LogDebug, "checker", "Executing service check for '" + service->GetName() + "'");
@ -130,11 +133,11 @@ void CheckerComponent::CheckCompletedHandler(const Service::Ptr& service, const
/* remove the service from the list of pending services; if it's not in the
* list this was a manual (i.e. forced) check and we must not re-add the
* service to the services list because it's already there. */
CheckerComponent::ServiceMultiSet::iterator it;
CheckerComponent::ServiceSet::iterator it;
it = m_PendingServices.find(service);
if (it != m_PendingServices.end()) {
m_PendingServices.erase(it);
m_Services.insert(service);
m_IdleServices.insert(service);
}
Logger::Write(LogDebug, "checker", "Check finished for service '" + service->GetName() + "'");
@ -145,7 +148,7 @@ void CheckerComponent::ResultTimerHandler(void)
Logger::Write(LogDebug, "checker", "ResultTimerHandler entered.");
stringstream msgbuf;
msgbuf << "Pending services: " << m_PendingServices.size() << "; Idle services: " << m_Services.size();
msgbuf << "Pending services: " << m_PendingServices.size() << "; Idle services: " << m_IdleServices.size();
Logger::Write(LogInformation, "checker", msgbuf.str());
}
@ -159,9 +162,9 @@ void CheckerComponent::CheckerChangedHandler(const Service::Ptr& service)
service->UpdateNextCheck();
m_Services.insert(service);
m_IdleServices.insert(service);
} else {
m_Services.erase(service);
m_IdleServices.erase(service);
m_PendingServices.erase(service);
}
}
@ -174,7 +177,7 @@ void CheckerComponent::ServiceRemovedHandler(const DynamicObject::Ptr& object)
if (!service)
return;
m_Services.erase(service);
m_IdleServices.erase(service);
m_PendingServices.erase(service);
}

View File

@ -23,12 +23,13 @@
namespace icinga
{
struct ServiceNextCheckLessComparer
struct ServiceNextCheckExtractor
{
public:
bool operator()(const Service::Ptr& a, const Service::Ptr& b)
typedef double result_type;
double operator()(const Service::Ptr& service)
{
return a->GetNextCheck() > b->GetNextCheck();
return service->GetNextCheck();
}
};
@ -41,7 +42,13 @@ public:
typedef shared_ptr<CheckerComponent> Ptr;
typedef weak_ptr<CheckerComponent> WeakPtr;
typedef multiset<Service::Ptr, ServiceNextCheckLessComparer> ServiceMultiSet;
typedef multi_index_container<
Service::Ptr,
indexed_by<
ordered_unique<identity<Service::Ptr> >,
ordered_non_unique<ServiceNextCheckExtractor>
>
> ServiceSet;
virtual void Start(void);
virtual void Stop(void);
@ -49,8 +56,8 @@ public:
private:
VirtualEndpoint::Ptr m_Endpoint;
ServiceMultiSet m_Services;
ServiceMultiSet m_PendingServices;
ServiceSet m_IdleServices;
ServiceSet m_PendingServices;
Timer::Ptr m_CheckTimer;

View File

@ -30,9 +30,16 @@
#include <i2-icinga.h>
#include <i2-cib.h>
#include <queue>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
using std::priority_queue;
using boost::multi_index_container;
using boost::multi_index::indexed_by;
using boost::multi_index::identity;
using boost::multi_index::ordered_unique;
using boost::multi_index::ordered_non_unique;
using boost::multi_index::nth_index;
#include "checkercomponent.h"