]> granicus.if.org Git - icinga2/blob - lib/db_ido/dbconnection.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / db_ido / dbconnection.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef DBCONNECTION_H
4 #define DBCONNECTION_H
5
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>
14
15 #define IDO_CURRENT_SCHEMA_VERSION "1.14.3"
16 #define IDO_COMPAT_SCHEMA_VERSION "1.14.3"
17
18 namespace icinga
19 {
20
21 /**
22  * A database connection.
23  *
24  * @ingroup db_ido
25  */
26 class DbConnection : public ObjectImpl<DbConnection>
27 {
28 public:
29         DECLARE_OBJECT(DbConnection);
30
31         static void InitializeDbTimer();
32
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;
37
38         void SetObjectID(const DbObject::Ptr& dbobj, const DbReference& dbref);
39         DbReference GetObjectID(const DbObject::Ptr& dbobj) const;
40
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;
45
46         void SetObjectActive(const DbObject::Ptr& dbobj, bool active);
47         bool GetObjectActive(const DbObject::Ptr& dbobj) const;
48
49         void ClearIDCache();
50
51         void SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate);
52         bool GetConfigUpdate(const DbObject::Ptr& dbobj) const;
53
54         void SetStatusUpdate(const DbObject::Ptr& dbobj, bool hasupdate);
55         bool GetStatusUpdate(const DbObject::Ptr& dbobj) const;
56
57         int GetQueryCount(RingBuffer::SizeType span);
58         virtual int GetPendingQueryCount() const = 0;
59
60         void ValidateFailoverTimeout(const Lazy<double>& lvalue, const ValidationUtils& utils) final;
61         void ValidateCategories(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) final;
62
63 protected:
64         void OnConfigLoaded() override;
65         void Start(bool runtimeCreated) override;
66         void Stop(bool runtimeRemoved) override;
67         void Resume() override;
68         void Pause() override;
69
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;
74
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;
78
79         void UpdateObject(const ConfigObject::Ptr& object);
80         void UpdateAllObjects();
81
82         void PrepareDatabase();
83
84         void IncreaseQueryCount();
85
86         bool IsIDCacheValid() const;
87         void SetIDCacheValid(bool valid);
88
89         void EnableActiveChangedHandler();
90
91         static void UpdateProgramStatus();
92
93         static int GetSessionToken();
94
95 private:
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;
104
105         void CleanUpHandler();
106
107         static Timer::Ptr m_ProgramStatusTimer;
108         static boost::once_flag m_OnceFlag;
109
110         static void InsertRuntimeVariable(const String& key, const Value& value);
111
112         mutable boost::mutex m_StatsMutex;
113         RingBuffer m_QueryStats{15 * 60};
114         bool m_ActiveChangedHandler{false};
115 };
116
117 struct database_error : virtual std::exception, virtual boost::exception { };
118
119 struct errinfo_database_query_;
120 typedef boost::error_info<struct errinfo_database_query_, std::string> errinfo_database_query;
121
122 }
123
124 #endif /* DBCONNECTION_H */