Kea  1.9.9-git
database_connection.h
Go to the documentation of this file.
1 // Copyright (C) 2015-2021 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 <functional>
16 #include <map>
17 #include <string>
18 
19 namespace isc {
20 namespace db {
21 
23 class NoDatabaseName : public Exception {
24 public:
25  NoDatabaseName(const char* file, size_t line, const char* what) :
26  isc::Exception(file, line, what) {}
27 };
28 
30 class DbOpenError : public Exception {
31 public:
32  DbOpenError(const char* file, size_t line, const char* what) :
33  isc::Exception(file, line, what) {}
34 };
35 
37 class DbOperationError : public Exception {
38 public:
39  DbOperationError(const char* file, size_t line, const char* what) :
40  isc::Exception(file, line, what) {}
41 };
42 
46 public:
47  DbConnectionUnusable(const char* file, size_t line, const char* what) :
48  isc::Exception(file, line, what) {}
49 };
50 
51 
55 class InvalidType : public Exception {
56 public:
57  InvalidType(const char* file, size_t line, const char* what) :
58  isc::Exception(file, line, what) {}
59 };
60 
64 class DbInvalidTimeout : public Exception {
65 public:
66  DbInvalidTimeout(const char* file, size_t line, const char* what) :
67  isc::Exception(file, line, what) {}
68 };
69 
73 class DbInvalidReadOnly : public Exception {
74 public:
75  DbInvalidReadOnly(const char* file, size_t line, const char* what) :
76  isc::Exception(file, line, what) {}
77 };
78 
80 enum class OnFailAction {
84 };
85 
93 class ReconnectCtl {
94 public:
102  ReconnectCtl(const std::string& backend_type, const std::string& timer_name,
103  unsigned int max_retries, unsigned int retry_interval,
104  OnFailAction action) :
105  backend_type_(backend_type), timer_name_(timer_name),
106  max_retries_(max_retries), retries_left_(max_retries),
107  retry_interval_(retry_interval), action_(action) {}
108 
110  std::string backendType() const {
111  return (backend_type_);
112  }
113 
117  std::string timerName() const {
118  return (timer_name_);
119  }
120 
125  bool checkRetries() {
126  return (retries_left_ ? --retries_left_ : false);
127  }
128 
130  unsigned int maxRetries() {
131  return (max_retries_);
132  }
133 
135  unsigned int retriesLeft() {
136  return (retries_left_);
137  }
138 
140  unsigned int retryInterval() {
141  return (retry_interval_);
142  }
143 
145  void resetRetries() {
146  retries_left_ = max_retries_;
147  }
148 
152  return (action_ == OnFailAction::STOP_RETRY_EXIT);
153  }
154 
157  bool exitOnFailure() {
158  return ((action_ == OnFailAction::STOP_RETRY_EXIT) ||
159  (action_ == OnFailAction::SERVE_RETRY_EXIT));
160  }
161 
166  static std::string onFailActionToText(OnFailAction action);
167 
172  static OnFailAction onFailActionFromText(const std::string& text);
173 
174 private:
175 
177  const std::string backend_type_;
178 
180  std::string timer_name_;
181 
183  unsigned int max_retries_;
184 
186  unsigned int retries_left_;
187 
189  unsigned int retry_interval_;
190 
192  OnFailAction action_;
193 };
194 
196 typedef boost::shared_ptr<ReconnectCtl> ReconnectCtlPtr;
197 
199 typedef std::function<bool (ReconnectCtlPtr db_reconnect_ctl)> DbCallback;
200 
208 typedef std::function<isc::asiolink::IOServicePtr ()> IOServiceAccessor;
209 
211 typedef boost::shared_ptr<IOServiceAccessor> IOServiceAccessorPtr;
212 
221 class DatabaseConnection : public boost::noncopyable {
222 public:
223 
230  static const time_t MAX_DB_TIME;
231 
233  typedef std::map<std::string, std::string> ParameterMap;
234 
240  DatabaseConnection(const ParameterMap& parameters,
241  DbCallback callback = DbCallback())
242  : parameters_(parameters), callback_(callback), unusable_(false) {
243  }
244 
246  virtual ~DatabaseConnection(){};
247 
252  virtual void makeReconnectCtl(const std::string& timer_name);
253 
257  ReconnectCtlPtr reconnectCtl() {
258  return (reconnect_ctl_);
259  }
260 
266  std::string getParameter(const std::string& name) const;
267 
277  static ParameterMap parse(const std::string& dbaccess);
278 
287  static std::string redactedAccessString(const ParameterMap& parameters);
288 
295  bool configuredReadOnly() const;
296 
301  static bool invokeDbLostCallback(const ReconnectCtlPtr& db_reconnect_ctl);
302 
307  static bool invokeDbRecoveredCallback(const ReconnectCtlPtr& db_reconnect_ctl);
308 
313  static bool invokeDbFailedCallback(const ReconnectCtlPtr& db_reconnect_ctl);
314 
319  static isc::data::ElementPtr toElement(const ParameterMap& params);
320 
325  static isc::data::ElementPtr toElementDbAccessString(const std::string& dbaccess);
326 
329  static DbCallback db_lost_callback_;
330 
333  static DbCallback db_recovered_callback_;
334 
337  static DbCallback db_failed_callback_;
338 
341  void checkUnusable() {
342  if (unusable_) {
343  isc_throw (DbConnectionUnusable, "Attempt to use an invalid connection");
344  }
345  }
346 
350  bool isUnusable() {
351  return (unusable_);
352  }
353 
354 protected:
356  void markUnusable() { unusable_ = true; }
357 
358 private:
359 
365  ParameterMap parameters_;
366 
367 protected:
368 
370  DbCallback callback_;
371 
372 private:
373 
380  bool unusable_;
381 
383  ReconnectCtlPtr reconnect_ctl_;
384 };
385 
386 } // namespace db
387 } // namespace isc
388 
389 #endif // DATABASE_CONNECTION_H
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.
virtual void makeReconnectCtl(const std::string &timer_name)
Instantiates a ReconnectCtl based on the connection's reconnect parameters.
bool checkRetries()
Decrements the number of retries remaining.
void resetRetries()
Resets the retries count.
Warehouses DB reconnect control values.
bool exitOnFailure()
Return true if the connection recovery mechanism should shut down the server on failure, false otherwise.
unsigned int retriesLeft()
Returns the number for retries remaining.
NoDatabaseName(const char *file, size_t line, const char *what)
ReconnectCtlPtr reconnectCtl()
The reconnect settings.
std::string backendType() const
Returns the type of the caller backend.
static isc::data::ElementPtr toElement(const ParameterMap &params)
Unparse a parameter map.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
static std::string onFailActionToText(OnFailAction action)
Convert action to string.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
unsigned int maxRetries()
Returns the maximum number of retries allowed.
ReconnectCtl(const std::string &backend_type, const std::string &timer_name, unsigned int max_retries, unsigned int retry_interval, OnFailAction action)
Constructor.
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)
Invalid type exception.
static DbCallback db_failed_callback_
Optional callback function to invoke if an opened connection recovery failed.
Exception thrown on failure to open database.
bool isUnusable()
Flag which indicates if connection is unusable.
bool configuredReadOnly() const
Convenience method checking if database should be opened with read only access.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
static isc::data::ElementPtr toElementDbAccessString(const std::string &dbaccess)
Unparse an access string.
Exception thrown if name of database is not specified.
DatabaseConnection(const ParameterMap &parameters, DbCallback callback=DbCallback())
Constructor.
OnFailAction
Type of action to take on connection loss.
virtual ~DatabaseConnection()
Destructor.
DbConnectionUnusable(const char *file, size_t line, const char *what)
std::string timerName() const
Returns the associated timer name.
static DbCallback db_recovered_callback_
Optional callback function to invoke if an opened connection recovery succeeded.
bool alterServiceState()
Return true if the connection loss should affect the service, false otherwise.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static bool invokeDbFailedCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::function< bool(ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
static OnFailAction onFailActionFromText(const std::string &text)
Convert string to action.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
static bool invokeDbRecoveredCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
static bool invokeDbLostCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
unsigned int retryInterval()
Returns the amount of time to wait between reconnect attempts.
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 'readonly' value specification.
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)
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
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.