Kea 2.0.0
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>
15#include <functional>
16#include <map>
17#include <string>
18
19namespace isc {
20namespace db {
21
23class NoDatabaseName : public Exception {
24public:
25 NoDatabaseName(const char* file, size_t line, const char* what) :
26 isc::Exception(file, line, what) {}
27};
28
30class DbOpenError : public Exception {
31public:
32 DbOpenError(const char* file, size_t line, const char* what) :
33 isc::Exception(file, line, what) {}
34};
35
38public:
39 DbOperationError(const char* file, size_t line, const char* what) :
40 isc::Exception(file, line, what) {}
41};
42
46public:
47 DbConnectionUnusable(const char* file, size_t line, const char* what) :
48 isc::Exception(file, line, what) {}
49};
50
51
55class InvalidType : public Exception {
56public:
57 InvalidType(const char* file, size_t line, const char* what) :
58 isc::Exception(file, line, what) {}
59};
60
65public:
66 DbInvalidTimeout(const char* file, size_t line, const char* what) :
67 isc::Exception(file, line, what) {}
68};
69
74public:
75 DbInvalidReadOnly(const char* file, size_t line, const char* what) :
76 isc::Exception(file, line, what) {}
77};
78
80enum class OnFailAction {
84};
85
94public:
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
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
146 retries_left_ = max_retries_;
147 }
148
152 return (action_ == OnFailAction::STOP_RETRY_EXIT);
153 }
154
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
174private:
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
196typedef boost::shared_ptr<ReconnectCtl> ReconnectCtlPtr;
197
199typedef std::function<bool (ReconnectCtlPtr db_reconnect_ctl)> DbCallback;
200
209
211typedef boost::shared_ptr<IOServiceAccessor> IOServiceAccessorPtr;
212
221class DatabaseConnection : public boost::noncopyable {
222public:
223
230 static const time_t MAX_DB_TIME;
231
233 typedef std::map<std::string, std::string> ParameterMap;
234
241 DbCallback callback = DbCallback())
242 : parameters_(parameters), callback_(callback), unusable_(false) {
243 }
244
247
252 virtual void makeReconnectCtl(const std::string& timer_name);
253
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
330
334
338
342 if (unusable_) {
343 isc_throw (DbConnectionUnusable, "Attempt to use an invalid connection");
344 }
345 }
346
350 bool isUnusable() {
351 return (unusable_);
352 }
353
354protected:
356 void markUnusable() { unusable_ = true; }
357
358private:
359
365 ParameterMap parameters_;
366
367protected:
368
371
372private:
373
380 bool unusable_;
381
383 ReconnectCtlPtr reconnect_ctl_;
384};
385
386} // namespace db
387} // namespace isc
388
389#endif // DATABASE_CONNECTION_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Common database connection class.
virtual ~DatabaseConnection()
Destructor.
bool configuredReadOnly() const
Convenience method checking if database should be opened with read only access.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
void markUnusable()
Sets the unusable flag to true.
static bool invokeDbLostCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
static isc::data::ElementPtr toElement(const ParameterMap &params)
Unparse a parameter map.
ReconnectCtlPtr reconnectCtl()
The reconnect settings.
static isc::data::ElementPtr toElementDbAccessString(const std::string &dbaccess)
Unparse an access string.
static DbCallback db_recovered_callback_
Optional callback function to invoke if an opened connection recovery succeeded.
virtual void makeReconnectCtl(const std::string &timer_name)
Instantiates a ReconnectCtl based on the connection's reconnect parameters.
DatabaseConnection(const ParameterMap &parameters, DbCallback callback=DbCallback())
Constructor.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
static bool invokeDbFailedCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
static DbCallback db_failed_callback_
Optional callback function to invoke if an opened connection recovery failed.
void checkUnusable()
Throws an exception if the connection is not usable.
static DbCallback db_lost_callback_
Optional callback function to invoke if an opened connection is lost.
bool isUnusable()
Flag which indicates if connection is unusable.
static const time_t MAX_DB_TIME
Defines maximum value for time that can be reliably stored.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
static bool invokeDbRecoveredCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
DbCallback callback_
The callback used to recover the connection.
Exception thrown when a specific connection has been rendered unusable either through loss of connect...
DbConnectionUnusable(const char *file, size_t line, const char *what)
Invalid 'readonly' value specification.
DbInvalidReadOnly(const char *file, size_t line, const char *what)
DbInvalidTimeout(const char *file, size_t line, const char *what)
Exception thrown on failure to open database.
DbOpenError(const char *file, size_t line, const char *what)
Exception thrown on failure to execute a database function.
DbOperationError(const char *file, size_t line, const char *what)
Invalid type exception.
InvalidType(const char *file, size_t line, const char *what)
Exception thrown if name of database is not specified.
NoDatabaseName(const char *file, size_t line, const char *what)
Warehouses DB reconnect control values.
std::string backendType() const
Returns the type of the caller backend.
unsigned int retriesLeft()
Returns the number for retries remaining.
bool checkRetries()
Decrements the number of retries remaining.
bool exitOnFailure()
Return true if the connection recovery mechanism should shut down the server on failure,...
void resetRetries()
Resets the retries count.
unsigned int maxRetries()
Returns the maximum number of retries allowed.
static OnFailAction onFailActionFromText(const std::string &text)
Convert string to action.
unsigned int retryInterval()
Returns the amount of time to wait between reconnect attempts.
ReconnectCtl(const std::string &backend_type, const std::string &timer_name, unsigned int max_retries, unsigned int retry_interval, OnFailAction action)
Constructor.
bool alterServiceState()
Return true if the connection loss should affect the service, false otherwise.
static std::string onFailActionToText(OnFailAction action)
Convert action to string.
std::string timerName() const
Returns the associated timer name.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
std::function< bool(ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
OnFailAction
Type of action to take on connection loss.
std::function< isc::asiolink::IOServicePtr()> IOServiceAccessor
Function which returns the IOService that can be used to recover the connection.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
Defines the logger used by the top-level component of kea-lfc.