2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-07-05 03:37:04 -04:00
|
|
|
|
|
|
|
|
#ifndef DBCONNECTION_H
|
|
|
|
|
#define DBCONNECTION_H
|
|
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "db_ido/i2-db_ido.hpp"
|
2018-01-18 07:50:38 -05:00
|
|
|
#include "db_ido/dbconnection-ti.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "db_ido/dbobject.hpp"
|
|
|
|
|
#include "db_ido/dbquery.hpp"
|
|
|
|
|
#include "base/timer.hpp"
|
2015-03-12 04:39:53 -04:00
|
|
|
#include "base/ringbuffer.hpp"
|
2015-01-27 03:53:07 -05:00
|
|
|
#include <boost/thread/once.hpp>
|
2021-02-02 04:16:04 -05:00
|
|
|
#include <mutex>
|
2013-07-05 03:37:04 -04:00
|
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A database connection.
|
|
|
|
|
*
|
2013-10-14 14:12:42 -04:00
|
|
|
* @ingroup db_ido
|
2013-07-05 03:37:04 -04:00
|
|
|
*/
|
2017-12-31 01:22:16 -05:00
|
|
|
class DbConnection : public ObjectImpl<DbConnection>
|
2013-07-05 03:37:04 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2014-11-02 18:44:04 -05:00
|
|
|
DECLARE_OBJECT(DbConnection);
|
2013-07-05 03:37:04 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static void InitializeDbTimer();
|
2013-08-01 05:14:33 -04:00
|
|
|
|
2021-06-30 12:07:58 -04:00
|
|
|
virtual const char * GetLatestSchemaVersion() const noexcept = 0;
|
2021-06-30 12:11:55 -04:00
|
|
|
virtual const char * GetCompatSchemaVersion() const noexcept = 0;
|
2021-06-30 12:07:58 -04:00
|
|
|
|
2016-08-14 16:24:51 -04: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 02:56:36 -04: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 02:28:00 -05:00
|
|
|
void SetInsertID(const DbType::Ptr& type, const DbReference& objid, const DbReference& dbref);
|
2013-08-02 02:56:36 -04:00
|
|
|
DbReference GetInsertID(const DbObject::Ptr& dbobj) const;
|
2014-01-31 02:28:00 -05:00
|
|
|
DbReference GetInsertID(const DbType::Ptr& type, const DbReference& objid) const;
|
2013-07-05 03:37:04 -04:00
|
|
|
|
2013-11-22 04:13:42 -05:00
|
|
|
void SetObjectActive(const DbObject::Ptr& dbobj, bool active);
|
|
|
|
|
bool GetObjectActive(const DbObject::Ptr& dbobj) const;
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void ClearIDCache();
|
2013-11-20 03:32:30 -05:00
|
|
|
|
2013-08-02 09:45:50 -04: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 10:17:59 -05:00
|
|
|
int GetQueryCount(RingBuffer::SizeType span);
|
2018-01-03 22:25:35 -05:00
|
|
|
virtual int GetPendingQueryCount() const = 0;
|
2015-03-12 04:39:53 -04:00
|
|
|
|
2018-01-11 01:08:09 -05: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 11:13:32 -04:00
|
|
|
|
2013-07-05 03:37:04 -04:00
|
|
|
protected:
|
2018-01-03 23:12:56 -05:00
|
|
|
void OnConfigLoaded() override;
|
|
|
|
|
void Start(bool runtimeCreated) override;
|
|
|
|
|
void Stop(bool runtimeRemoved) override;
|
|
|
|
|
void Resume() override;
|
|
|
|
|
void Pause() override;
|
2013-07-17 08:10:28 -04:00
|
|
|
|
2013-07-22 06:08:49 -04:00
|
|
|
virtual void ExecuteQuery(const DbQuery& query) = 0;
|
2015-12-14 05:36:03 -05:00
|
|
|
virtual void ExecuteMultipleQueries(const std::vector<DbQuery>&) = 0;
|
2013-07-22 06:08:49 -04:00
|
|
|
virtual void ActivateObject(const DbObject::Ptr& dbobj) = 0;
|
|
|
|
|
virtual void DeactivateObject(const DbObject::Ptr& dbobj) = 0;
|
2013-07-05 03:37:04 -04:00
|
|
|
|
2013-10-30 09:07:26 -04:00
|
|
|
virtual void CleanUpExecuteQuery(const String& table, const String& time_column, double max_age);
|
2014-01-31 02:28:00 -05:00
|
|
|
virtual void FillIDCache(const DbType::Ptr& type) = 0;
|
2018-01-03 22:25:35 -05:00
|
|
|
virtual void NewTransaction() = 0;
|
2021-04-20 12:37:09 -04:00
|
|
|
virtual void Disconnect() = 0;
|
2013-09-26 09:22:21 -04:00
|
|
|
|
2015-08-15 14:28:05 -04:00
|
|
|
void UpdateObject(const ConfigObject::Ptr& object);
|
2018-01-03 22:25:35 -05:00
|
|
|
void UpdateAllObjects();
|
2013-07-05 03:37:04 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void PrepareDatabase();
|
2014-01-28 05:09:13 -05:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void IncreaseQueryCount();
|
2015-03-12 04:39:53 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
bool IsIDCacheValid() const;
|
2016-07-15 03:40:39 -04:00
|
|
|
void SetIDCacheValid(bool valid);
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void EnableActiveChangedHandler();
|
2016-08-13 16:54:22 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static void UpdateProgramStatus();
|
2016-05-11 08:03:40 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static int GetSessionToken();
|
2016-07-29 02:40:10 -04:00
|
|
|
|
2020-09-09 12:17:37 -04:00
|
|
|
void IncreasePendingQueries(int count);
|
|
|
|
|
void DecreasePendingQueries(int count);
|
|
|
|
|
|
2020-09-24 08:18:49 -04:00
|
|
|
WorkQueue m_QueryQueue{10000000, 1, LogNotice};
|
|
|
|
|
|
2013-07-05 03:37:04 -04:00
|
|
|
private:
|
2018-01-04 03:43:49 -05:00
|
|
|
bool m_IDCacheValid{false};
|
2016-08-14 16:24:51 -04:00
|
|
|
std::map<std::pair<DbType::Ptr, DbReference>, String> m_ConfigHashes;
|
2013-08-02 02:56:36 -04:00
|
|
|
std::map<DbObject::Ptr, DbReference> m_ObjectIDs;
|
2014-01-31 02:28:00 -05:00
|
|
|
std::map<std::pair<DbType::Ptr, DbReference>, DbReference> m_InsertIDs;
|
2013-11-22 04:13:42 -05:00
|
|
|
std::set<DbObject::Ptr> m_ActiveObjects;
|
2013-08-02 09:45:50 -04:00
|
|
|
std::set<DbObject::Ptr> m_ConfigUpdates;
|
|
|
|
|
std::set<DbObject::Ptr> m_StatusUpdates;
|
2013-10-26 03:41:45 -04:00
|
|
|
Timer::Ptr m_CleanUpTimer;
|
2020-09-09 12:17:37 -04:00
|
|
|
Timer::Ptr m_LogStatsTimer;
|
2013-09-26 09:22:21 -04:00
|
|
|
|
2020-10-08 05:35:59 -04:00
|
|
|
double m_LogStatsTimeout;
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void CleanUpHandler();
|
2020-09-09 12:17:37 -04:00
|
|
|
void LogStatsHandler();
|
2013-09-26 09:22:21 -04:00
|
|
|
|
2013-07-25 03:00:23 -04:00
|
|
|
static Timer::Ptr m_ProgramStatusTimer;
|
2015-01-27 03:53:07 -05:00
|
|
|
static boost::once_flag m_OnceFlag;
|
2013-07-25 03:00:23 -04:00
|
|
|
|
2013-08-08 02:47:29 -04:00
|
|
|
static void InsertRuntimeVariable(const String& key, const Value& value);
|
2015-03-12 04:39:53 -04:00
|
|
|
|
2021-02-02 04:16:04 -05:00
|
|
|
mutable std::mutex m_StatsMutex;
|
2018-01-04 03:43:49 -05:00
|
|
|
RingBuffer m_QueryStats{15 * 60};
|
|
|
|
|
bool m_ActiveChangedHandler{false};
|
2020-09-09 12:17:37 -04:00
|
|
|
|
|
|
|
|
RingBuffer m_InputQueries{10};
|
|
|
|
|
RingBuffer m_OutputQueries{10};
|
|
|
|
|
Atomic<uint_fast64_t> m_PendingQueries{0};
|
2013-07-05 03:37:04 -04:00
|
|
|
};
|
|
|
|
|
|
2013-11-05 06:40:25 -05: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 03:37:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* DBCONNECTION_H */
|