Kea  2.3.5-git
database_connection.h
Go to the documentation of this file.
1 // Copyright (C) 2015-2023 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef DATABASE_CONNECTION_H
8 #define DATABASE_CONNECTION_H
9 
10 #include <asiolink/io_service.h>
11 #include <cc/data.h>
12 #include <boost/noncopyable.hpp>
13 #include <boost/shared_ptr.hpp>
14 #include <exceptions/exceptions.h>
15 #include <util/reconnect_ctl.h>
16 #include <functional>
17 #include <map>
18 #include <string>
19 
20 namespace isc {
21 namespace db {
22 
24 class NoDatabaseName : public Exception {
25 public:
26  NoDatabaseName(const char* file, size_t line, const char* what) :
27  isc::Exception(file, line, what) {}
28 };
29 
31 class DbOpenError : public Exception {
32 public:
33  DbOpenError(const char* file, size_t line, const char* what) :
34  isc::Exception(file, line, what) {}
35 };
36 
38 class DbOperationError : public Exception {
39 public:
40  DbOperationError(const char* file, size_t line, const char* what) :
41  isc::Exception(file, line, what) {}
42 };
43 
47 public:
48  DbConnectionUnusable(const char* file, size_t line, const char* what) :
49  isc::Exception(file, line, what) {}
50 };
51 
52 
56 class InvalidType : public Exception {
57 public:
58  InvalidType(const char* file, size_t line, const char* what) :
59  isc::Exception(file, line, what) {}
60 };
61 
65 class DbInvalidTimeout : public Exception {
66 public:
67  DbInvalidTimeout(const char* file, size_t line, const char* what) :
68  isc::Exception(file, line, what) {}
69 };
70 
74 class DbInvalidPort : public Exception {
75 public:
76  DbInvalidPort(const char* file, size_t line, const char* what) :
77  isc::Exception(file, line, what) {}
78 };
79 
83 class DbInvalidReadOnly : public Exception {
84 public:
85  DbInvalidReadOnly(const char* file, size_t line, const char* what) :
86  isc::Exception(file, line, what) {}
87 };
88 
90 typedef std::function<bool (util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback;
91 
99 typedef std::function<isc::asiolink::IOServicePtr ()> IOServiceAccessor;
100 
102 typedef boost::shared_ptr<IOServiceAccessor> IOServiceAccessorPtr;
103 
112 class DatabaseConnection : public boost::noncopyable {
113 public:
114 
121  static const time_t MAX_DB_TIME;
122 
124  typedef std::map<std::string, std::string> ParameterMap;
125 
131  DatabaseConnection(const ParameterMap& parameters,
132  DbCallback callback = DbCallback())
133  : parameters_(parameters), callback_(callback), unusable_(false) {
134  }
135 
137  virtual ~DatabaseConnection(){};
138 
143  virtual void makeReconnectCtl(const std::string& timer_name);
144 
149  return (reconnect_ctl_);
150  }
151 
157  std::string getParameter(const std::string& name) const;
158 
168  static ParameterMap parse(const std::string& dbaccess);
169 
178  static std::string redactedAccessString(const ParameterMap& parameters);
179 
186  bool configuredReadOnly() const;
187 
192  static bool invokeDbLostCallback(const util::ReconnectCtlPtr& db_reconnect_ctl);
193 
198  static bool invokeDbRecoveredCallback(const util::ReconnectCtlPtr& db_reconnect_ctl);
199 
204  static bool invokeDbFailedCallback(const util::ReconnectCtlPtr& db_reconnect_ctl);
205 
210  static isc::data::ElementPtr toElement(const ParameterMap& params);
211 
216  static isc::data::ElementPtr toElementDbAccessString(const std::string& dbaccess);
217 
220  static DbCallback db_lost_callback_;
221 
224  static DbCallback db_recovered_callback_;
225 
228  static DbCallback db_failed_callback_;
229 
232  void checkUnusable() {
233  if (unusable_) {
234  isc_throw (DbConnectionUnusable, "Attempt to use an invalid connection");
235  }
236  }
237 
241  bool isUnusable() {
242  return (unusable_);
243  }
244 
245 protected:
247  void markUnusable() { unusable_ = true; }
248 
249 private:
250 
256  ParameterMap parameters_;
257 
258 protected:
259 
261  DbCallback callback_;
262 
263 private:
264 
271  bool unusable_;
272 
274  util::ReconnectCtlPtr reconnect_ctl_;
275 };
276 
277 } // namespace db
278 } // namespace isc
279 
280 #endif // DATABASE_CONNECTION_H
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
DbOpenError(const char *file, size_t line, const char *what)
static DbCallback db_lost_callback_
Optional callback function to invoke if an opened connection is lost.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
NoDatabaseName(const char *file, size_t line, const char *what)
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
Common database connection class.
DbOperationError(const char *file, size_t line, const char *what)
void checkUnusable()
Throws an exception if the connection is not usable.
static const time_t MAX_DB_TIME
Defines maximum value for time that can be reliably stored.
DbInvalidTimeout(const char *file, size_t line, const char *what)
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Invalid type exception.
static DbCallback db_failed_callback_
Optional callback function to invoke if an opened connection recovery failed.
Invalid port number.
Exception thrown on failure to open database.
bool isUnusable()
Flag which indicates if connection is unusable.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Exception thrown if name of database is not specified.
DatabaseConnection(const ParameterMap &parameters, DbCallback callback=DbCallback())
Constructor.
virtual ~DatabaseConnection()
Destructor.
DbConnectionUnusable(const char *file, size_t line, const char *what)
static DbCallback db_recovered_callback_
Optional callback function to invoke if an opened connection recovery succeeded.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
DbInvalidPort(const char *file, size_t line, const char *what)
void markUnusable()
Sets the unusable flag to true.
DbInvalidReadOnly(const char *file, size_t line, const char *what)
Exception thrown when a specific connection has been rendered unusable either through loss of connect...
Invalid &#39;readonly&#39; value specification.
util::ReconnectCtlPtr reconnectCtl()
The reconnect settings.
std::function< isc::asiolink::IOServicePtr()> IOServiceAccessor
Function which returns the IOService that can be used to recover the connection.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
InvalidType(const char *file, size_t line, const char *what)
Exception thrown on failure to execute a database function.
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
DbCallback callback_
The callback used to recover the connection.