1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
6 #include "db_ido/i2-db_ido.hpp"
7 #include "db_ido/dbconnection-ti.hpp"
8 #include "db_ido/dbobject.hpp"
9 #include "db_ido/dbquery.hpp"
10 #include "base/timer.hpp"
11 #include "base/ringbuffer.hpp"
12 #include <boost/thread/once.hpp>
13 #include <boost/thread/mutex.hpp>
15 #define IDO_CURRENT_SCHEMA_VERSION "1.14.3"
16 #define IDO_COMPAT_SCHEMA_VERSION "1.14.3"
22 * A database connection.
26 class DbConnection : public ObjectImpl<DbConnection>
29 DECLARE_OBJECT(DbConnection);
31 static void InitializeDbTimer();
33 void SetConfigHash(const DbObject::Ptr& dbobj, const String& hash);
34 void SetConfigHash(const DbType::Ptr& type, const DbReference& objid, const String& hash);
35 String GetConfigHash(const DbObject::Ptr& dbobj) const;
36 String GetConfigHash(const DbType::Ptr& type, const DbReference& objid) const;
38 void SetObjectID(const DbObject::Ptr& dbobj, const DbReference& dbref);
39 DbReference GetObjectID(const DbObject::Ptr& dbobj) const;
41 void SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref);
42 void SetInsertID(const DbType::Ptr& type, const DbReference& objid, const DbReference& dbref);
43 DbReference GetInsertID(const DbObject::Ptr& dbobj) const;
44 DbReference GetInsertID(const DbType::Ptr& type, const DbReference& objid) const;
46 void SetObjectActive(const DbObject::Ptr& dbobj, bool active);
47 bool GetObjectActive(const DbObject::Ptr& dbobj) const;
51 void SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate);
52 bool GetConfigUpdate(const DbObject::Ptr& dbobj) const;
54 void SetStatusUpdate(const DbObject::Ptr& dbobj, bool hasupdate);
55 bool GetStatusUpdate(const DbObject::Ptr& dbobj) const;
57 int GetQueryCount(RingBuffer::SizeType span);
58 virtual int GetPendingQueryCount() const = 0;
60 void ValidateFailoverTimeout(const Lazy<double>& lvalue, const ValidationUtils& utils) final;
61 void ValidateCategories(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) final;
64 void OnConfigLoaded() override;
65 void Start(bool runtimeCreated) override;
66 void Stop(bool runtimeRemoved) override;
67 void Resume() override;
68 void Pause() override;
70 virtual void ExecuteQuery(const DbQuery& query) = 0;
71 virtual void ExecuteMultipleQueries(const std::vector<DbQuery>&) = 0;
72 virtual void ActivateObject(const DbObject::Ptr& dbobj) = 0;
73 virtual void DeactivateObject(const DbObject::Ptr& dbobj) = 0;
75 virtual void CleanUpExecuteQuery(const String& table, const String& time_column, double max_age);
76 virtual void FillIDCache(const DbType::Ptr& type) = 0;
77 virtual void NewTransaction() = 0;
79 void UpdateObject(const ConfigObject::Ptr& object);
80 void UpdateAllObjects();
82 void PrepareDatabase();
84 void IncreaseQueryCount();
86 bool IsIDCacheValid() const;
87 void SetIDCacheValid(bool valid);
89 void EnableActiveChangedHandler();
91 static void UpdateProgramStatus();
93 static int GetSessionToken();
96 bool m_IDCacheValid{false};
97 std::map<std::pair<DbType::Ptr, DbReference>, String> m_ConfigHashes;
98 std::map<DbObject::Ptr, DbReference> m_ObjectIDs;
99 std::map<std::pair<DbType::Ptr, DbReference>, DbReference> m_InsertIDs;
100 std::set<DbObject::Ptr> m_ActiveObjects;
101 std::set<DbObject::Ptr> m_ConfigUpdates;
102 std::set<DbObject::Ptr> m_StatusUpdates;
103 Timer::Ptr m_CleanUpTimer;
105 void CleanUpHandler();
107 static Timer::Ptr m_ProgramStatusTimer;
108 static boost::once_flag m_OnceFlag;
110 static void InsertRuntimeVariable(const String& key, const Value& value);
112 mutable boost::mutex m_StatsMutex;
113 RingBuffer m_QueryStats{15 * 60};
114 bool m_ActiveChangedHandler{false};
117 struct database_error : virtual std::exception, virtual boost::exception { };
119 struct errinfo_database_query_;
120 typedef boost::error_info<struct errinfo_database_query_, std::string> errinfo_database_query;
124 #endif /* DBCONNECTION_H */