2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
#ifndef DBCONNECTION_H
|
|
|
|
#define DBCONNECTION_H
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "db_ido/i2-db_ido.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "db_ido/dbconnection-ti.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "db_ido/dbobject.hpp"
|
|
|
|
#include "db_ido/dbquery.hpp"
|
|
|
|
#include "base/timer.hpp"
|
2015-03-12 09:39:53 +01:00
|
|
|
#include "base/ringbuffer.hpp"
|
2015-01-27 09:53:07 +01:00
|
|
|
#include <boost/thread/once.hpp>
|
2015-03-12 09:39:53 +01:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2017-08-09 17:41:43 +02:00
|
|
|
#define IDO_CURRENT_SCHEMA_VERSION "1.14.3"
|
2017-11-07 16:18:06 +01:00
|
|
|
#define IDO_COMPAT_SCHEMA_VERSION "1.14.3"
|
2015-03-12 09:35:26 +01:00
|
|
|
|
2013-07-05 09:37:04 +02:00
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A database connection.
|
|
|
|
*
|
2013-10-14 20:12:42 +02:00
|
|
|
* @ingroup db_ido
|
2013-07-05 09:37:04 +02:00
|
|
|
*/
|
2017-12-31 07:22:16 +01:00
|
|
|
class DbConnection : public ObjectImpl<DbConnection>
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-11-03 00:44:04 +01:00
|
|
|
DECLARE_OBJECT(DbConnection);
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static void InitializeDbTimer();
|
2013-08-01 11:14:33 +02:00
|
|
|
|
2016-08-14 22:24:51 +02:00
|
|
|
void SetConfigHash(const DbObject::Ptr& dbobj, const String& hash);
|
|
|
|
void SetConfigHash(const DbType::Ptr& type, const DbReference& objid, const String& hash);
|
|
|
|
String GetConfigHash(const DbObject::Ptr& dbobj) const;
|
|
|
|
String GetConfigHash(const DbType::Ptr& type, const DbReference& objid) const;
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
void SetObjectID(const DbObject::Ptr& dbobj, const DbReference& dbref);
|
|
|
|
DbReference GetObjectID(const DbObject::Ptr& dbobj) const;
|
|
|
|
|
|
|
|
void SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref);
|
2014-01-31 08:28:00 +01:00
|
|
|
void SetInsertID(const DbType::Ptr& type, const DbReference& objid, const DbReference& dbref);
|
2013-08-02 08:56:36 +02:00
|
|
|
DbReference GetInsertID(const DbObject::Ptr& dbobj) const;
|
2014-01-31 08:28:00 +01:00
|
|
|
DbReference GetInsertID(const DbType::Ptr& type, const DbReference& objid) const;
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2013-11-22 10:13:42 +01:00
|
|
|
void SetObjectActive(const DbObject::Ptr& dbobj, bool active);
|
|
|
|
bool GetObjectActive(const DbObject::Ptr& dbobj) const;
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ClearIDCache();
|
2013-11-20 09:32:30 +01:00
|
|
|
|
2013-08-02 15:45:50 +02:00
|
|
|
void SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate);
|
|
|
|
bool GetConfigUpdate(const DbObject::Ptr& dbobj) const;
|
|
|
|
|
|
|
|
void SetStatusUpdate(const DbObject::Ptr& dbobj, bool hasupdate);
|
|
|
|
bool GetStatusUpdate(const DbObject::Ptr& dbobj) const;
|
|
|
|
|
2017-11-13 16:17:59 +01:00
|
|
|
int GetQueryCount(RingBuffer::SizeType span);
|
2018-01-04 04:25:35 +01:00
|
|
|
virtual int GetPendingQueryCount() const = 0;
|
2015-03-12 09:39:53 +01:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
void ValidateFailoverTimeout(const Lazy<double>& lvalue, const ValidationUtils& utils) final;
|
|
|
|
void ValidateCategories(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) final;
|
2014-08-15 17:13:32 +02:00
|
|
|
|
2013-07-05 09:37:04 +02:00
|
|
|
protected:
|
2018-01-04 05:12:56 +01:00
|
|
|
void OnConfigLoaded() override;
|
|
|
|
void Start(bool runtimeCreated) override;
|
|
|
|
void Stop(bool runtimeRemoved) override;
|
|
|
|
void Resume() override;
|
|
|
|
void Pause() override;
|
2013-07-17 14:10:28 +02:00
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
virtual void ExecuteQuery(const DbQuery& query) = 0;
|
2015-12-14 11:36:03 +01:00
|
|
|
virtual void ExecuteMultipleQueries(const std::vector<DbQuery>&) = 0;
|
2013-07-22 12:08:49 +02:00
|
|
|
virtual void ActivateObject(const DbObject::Ptr& dbobj) = 0;
|
|
|
|
virtual void DeactivateObject(const DbObject::Ptr& dbobj) = 0;
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2013-10-30 14:07:26 +01:00
|
|
|
virtual void CleanUpExecuteQuery(const String& table, const String& time_column, double max_age);
|
2014-01-31 08:28:00 +01:00
|
|
|
virtual void FillIDCache(const DbType::Ptr& type) = 0;
|
2018-01-04 04:25:35 +01:00
|
|
|
virtual void NewTransaction() = 0;
|
2013-09-26 15:22:21 +02:00
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
void UpdateObject(const ConfigObject::Ptr& object);
|
2018-01-04 04:25:35 +01:00
|
|
|
void UpdateAllObjects();
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void PrepareDatabase();
|
2014-01-28 11:09:13 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IncreaseQueryCount();
|
2015-03-12 09:39:53 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool IsIDCacheValid() const;
|
2016-07-15 09:40:39 +02:00
|
|
|
void SetIDCacheValid(bool valid);
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void EnableActiveChangedHandler();
|
2016-08-13 22:54:22 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static void UpdateProgramStatus();
|
2016-05-11 14:03:40 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static int GetSessionToken();
|
2016-07-29 08:40:10 +02:00
|
|
|
|
2013-07-05 09:37:04 +02:00
|
|
|
private:
|
2018-01-04 09:43:49 +01:00
|
|
|
bool m_IDCacheValid{false};
|
2016-08-14 22:24:51 +02:00
|
|
|
std::map<std::pair<DbType::Ptr, DbReference>, String> m_ConfigHashes;
|
2013-08-02 08:56:36 +02:00
|
|
|
std::map<DbObject::Ptr, DbReference> m_ObjectIDs;
|
2014-01-31 08:28:00 +01:00
|
|
|
std::map<std::pair<DbType::Ptr, DbReference>, DbReference> m_InsertIDs;
|
2013-11-22 10:13:42 +01:00
|
|
|
std::set<DbObject::Ptr> m_ActiveObjects;
|
2013-08-02 15:45:50 +02:00
|
|
|
std::set<DbObject::Ptr> m_ConfigUpdates;
|
|
|
|
std::set<DbObject::Ptr> m_StatusUpdates;
|
2013-10-26 09:41:45 +02:00
|
|
|
Timer::Ptr m_CleanUpTimer;
|
2013-09-26 15:22:21 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void CleanUpHandler();
|
2013-09-26 15:22:21 +02:00
|
|
|
|
2013-07-25 09:00:23 +02:00
|
|
|
static Timer::Ptr m_ProgramStatusTimer;
|
2015-01-27 09:53:07 +01:00
|
|
|
static boost::once_flag m_OnceFlag;
|
2013-07-25 09:00:23 +02:00
|
|
|
|
2013-08-08 08:47:29 +02:00
|
|
|
static void InsertRuntimeVariable(const String& key, const Value& value);
|
2015-03-12 09:39:53 +01:00
|
|
|
|
|
|
|
mutable boost::mutex m_StatsMutex;
|
2018-01-04 09:43:49 +01:00
|
|
|
RingBuffer m_QueryStats{15 * 60};
|
|
|
|
bool m_ActiveChangedHandler{false};
|
2013-07-05 09:37:04 +02:00
|
|
|
};
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
struct database_error : virtual std::exception, virtual boost::exception { };
|
|
|
|
|
|
|
|
struct errinfo_database_query_;
|
|
|
|
typedef boost::error_info<struct errinfo_database_query_, std::string> errinfo_database_query;
|
|
|
|
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DBCONNECTION_H */
|