SNode.C
Loading...
Searching...
No Matches
database::mariadb::MariaDBConnection Class Reference

#include <MariaDBConnection.h>

Inheritance diagram for database::mariadb::MariaDBConnection:
Collaboration diagram for database::mariadb::MariaDBConnection:

Public Member Functions

 MariaDBConnection (MariaDBClient *mariaDBClient, const MariaDBConnectionDetails &connectionDetails, const std::function< void(const MariaDBState &)> &onStateChanged)
 MariaDBConnection (const MariaDBConnection &)=delete
 ~MariaDBConnection () override
MariaDBConnectionoperator= (const MariaDBConnection &)=delete
MariaDBCommandSequenceexecute_async (MariaDBCommandSequence &&commandSequence)
void execute_sync (MariaDBCommand *mariaDBCommand)
void commandStart (const utils::Timeval &currentTime)
void commandContinue (int status)
void commandCompleted ()
void unmanaged ()

Protected Member Functions

void checkStatus (int status)
void readEvent () override
void writeEvent () override
void outOfBandEvent () override
void readTimeout () override
void writeTimeout () override
void outOfBandTimeout () override
void unobservedEvent () override

Private Attributes

MariaDBClientmariaDBClient = nullptr
MYSQL * mysql = nullptr
const std::string connectionName
std::deque< MariaDBCommandSequencecommandSequenceQueue
MariaDBCommandcurrentCommand = nullptr
bool connected = false
bool closing = false
MariaDBCommandStartEvent commandStartEvent
std::function< void(const MariaDBState &)> onStateChanged

Additional Inherited Members

Private Member Functions inherited from core::eventreceiver::ReadEventReceiver
 ReadEventReceiver (const std::string &name, const utils::Timeval &timeout)
Private Member Functions inherited from core::DescriptorEventReceiver
 DescriptorEventReceiver (const std::string &name, DescriptorEventPublisher &descriptorEventPublisher, const utils::Timeval &timeout=TIMEOUT::DISABLE)
int getRegisteredFd () const
bool isEnabled () const
bool isSuspended () const
void setTimeout (const utils::Timeval &timeout)
utils::Timeval getTimeout (const utils::Timeval &currentTime) const
void checkTimeout (const utils::Timeval &currentTime)
bool enable (int fd)
void disable ()
void suspend ()
void resume ()
Private Member Functions inherited from core::EventReceiver
 EventReceiver (const std::string &name)
 EventReceiver (EventReceiver &)=delete
 EventReceiver (EventReceiver &&)=delete
EventReceiveroperator= (EventReceiver &)=delete
EventReceiveroperator= (EventReceiver &&)=delete
virtual void destruct ()
void span ()
void relax ()
const std::string & getName () const
virtual ~EventReceiver ()=default
Private Member Functions inherited from core::eventreceiver::WriteEventReceiver
 WriteEventReceiver (const std::string &name, const utils::Timeval &timeout)
Private Member Functions inherited from core::eventreceiver::ExceptionalConditionEventReceiver
 ExceptionalConditionEventReceiver (const std::string &name, const utils::Timeval &timeout=60)
Static Private Member Functions inherited from core::EventReceiver
static void atNextTick (const std::function< void(void)> &callBack)

Detailed Description

Definition at line 88 of file MariaDBConnection.h.

Constructor & Destructor Documentation

◆ MariaDBConnection() [1/2]

database::mariadb::MariaDBConnection::MariaDBConnection ( MariaDBClient * mariaDBClient,
const MariaDBConnectionDetails & connectionDetails,
const std::function< void(const MariaDBState &)> & onStateChanged )
explicit

Definition at line 63 of file MariaDBConnection.cpp.

70 , connectionName(connectionDetails.connectionName)
71 , commandStartEvent("MariaDBCommandStartEvent", this)
74
75 mysql = mysql_init(nullptr);
76 mysql_options(mysql, MYSQL_OPT_NONBLOCK, nullptr);
77
78 execute_async(std::move(MariaDBCommandSequence().execute_async(new database::mariadb::commands::async::MariaDBConnectCommand(
79 connectionDetails,
80 [this]() {
81 if (mysql_errno(mysql) == 0) {
82 const int fd = mysql_get_socket(mysql);
83
84 if (ReadEventReceiver::enable(fd) && WriteEventReceiver::enable(fd) && ExceptionalConditionEventReceiver::enable(fd)) {
85 ReadEventReceiver::suspend();
86 WriteEventReceiver::suspend();
87 ExceptionalConditionEventReceiver::suspend();
88
89 connected = true;
90 } else {
91 if (ReadEventReceiver::isEnabled()) {
92 ReadEventReceiver::disable();
93 }
94 if (WriteEventReceiver::isEnabled()) {
95 WriteEventReceiver::disable();
96 }
97 if (ExceptionalConditionEventReceiver::isEnabled()) {
98 ExceptionalConditionEventReceiver::disable();
99 }
100
101 LOG(ERROR) << this->connectionName << " MariaDB: Descriptor not registered in SNode.C eventloop";
102 }
103 }
104 },
105 [this]() {
106 LOG(DEBUG) << this->connectionName << " MariaDB connect: success";
107
108 this->onStateChanged({.connected = true});
109 },
110 [this](const std::string& errorString, unsigned int errorNumber) {
111 LOG(WARNING) << this->connectionName << " MariaDB connect: error: " << errorString << " : " << errorNumber;
112
113 this->onStateChanged({.error = errorNumber, .errorMessage = errorString});
114 }))));
115 }
ExceptionalConditionEventReceiver(const std::string &name, const utils::Timeval &timeout=60)
ReadEventReceiver(const std::string &name, const utils::Timeval &timeout)
WriteEventReceiver(const std::string &name, const utils::Timeval &timeout)
MariaDBCommandStartEvent commandStartEvent
MariaDBCommandSequence & execute_async(MariaDBCommandSequence &&commandSequence)
std::function< void(const MariaDBState &)> onStateChanged

References commandStartEvent, connectionName, database::mariadb::MariaDBConnectionDetails::connectionName, core::DescriptorEventReceiver::TIMEOUT::DISABLE, database::mariadb::MariaDBLibrary::ensureInitialized(), database::mariadb::MariaDBCommandSequence::execute_async(), execute_async(), mariaDBClient, database::mariadb::MariaDBCommandSequence::MariaDBCommandSequence(), and MariaDBConnection().

Referenced by MariaDBConnection().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MariaDBConnection() [2/2]

database::mariadb::MariaDBConnection::MariaDBConnection ( const MariaDBConnection & )
delete

◆ ~MariaDBConnection()

database::mariadb::MariaDBConnection::~MariaDBConnection ( )
override

Definition at line 117 of file MariaDBConnection.cpp.

117 {
118 for (MariaDBCommandSequence& mariaDBCommandSequence : commandSequenceQueue) {
119 for (MariaDBCommand* mariaDBCommand : mariaDBCommandSequence.sequence()) {
121 mariaDBCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
122 }
123
124 delete mariaDBCommand;
125 }
126 }
127
128 if (mariaDBClient != nullptr) {
129 mariaDBClient->connectionVanished();
130 }
131
132 if (mysql != nullptr) {
133 mysql_close(mysql);
134 mysql = nullptr;
135 }
136 }
static State state()
Definition SNodeC.cpp:76
std::deque< MariaDBCommandSequence > commandSequenceQueue
@ RUNNING
Definition State.h:51

References database::mariadb::MariaDBClient::connectionVanished(), and mariaDBClient.

Here is the call graph for this function:

Member Function Documentation

◆ checkStatus()

void database::mariadb::MariaDBConnection::checkStatus ( int status)
protected

Definition at line 221 of file MariaDBConnection.cpp.

221 {
222 if (connected) {
223 if ((status & MYSQL_WAIT_READ) != 0) {
224 if (ReadEventReceiver::isSuspended() && ReadEventReceiver::isEnabled()) {
225 ReadEventReceiver::resume();
226 }
227 } else if (!ReadEventReceiver::isSuspended()) {
228 ReadEventReceiver::suspend();
229 }
230
231 if ((status & MYSQL_WAIT_WRITE) != 0) {
232 if (WriteEventReceiver::isSuspended() && WriteEventReceiver::isEnabled()) {
233 WriteEventReceiver::resume();
234 }
235 } else if (!WriteEventReceiver::isSuspended()) {
236 WriteEventReceiver::suspend();
237 }
238
239 if ((status & MYSQL_WAIT_EXCEPT) != 0) {
240 if (ExceptionalConditionEventReceiver::isSuspended() && ExceptionalConditionEventReceiver::isEnabled()) {
241 ExceptionalConditionEventReceiver::resume();
242 }
243 } else if (!ExceptionalConditionEventReceiver::isSuspended()) {
244 ExceptionalConditionEventReceiver::suspend();
245 }
246
247 if ((status & MYSQL_WAIT_TIMEOUT) != 0) {
248 ReadEventReceiver::setTimeout(mysql_get_timeout_value(mysql));
249 // WriteEventReceiver::setTimeout(mysql_get_timeout_value(mysql));
250 // ExceptionalConditionEventReceiver::setTimeout(mysql_get_timeout_value(mysql));
251 } else {
252 ReadEventReceiver::setTimeout(core::DescriptorEventReceiver::TIMEOUT::DEFAULT);
253 // WriteEventReceiver::setTimeout(core::DescriptorEventReceiver::TIMEOUT::DEFAULT);
254 // ExceptionalConditionEventReceiver::setTimeout(core::DescriptorEventReceiver::TIMEOUT::DEFAULT);
255 }
256
257 if (status == 0) {
258 if (mysql_errno(mysql) == 0) {
259 if (currentCommand->commandCompleted()) {
261 }
262 } else {
263 currentCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
265 }
266 commandStartEvent.span();
267 }
268 } else {
269 currentCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
271
272 delete this;
273 }
274 }

References database::mariadb::MariaDBCommand::commandCompleted(), commandCompleted(), database::mariadb::MariaDBCommand::commandError(), commandStartEvent, connected, currentCommand, core::DescriptorEventReceiver::TIMEOUT::DEFAULT, core::DescriptorEventReceiver::isEnabled(), core::DescriptorEventReceiver::isSuspended(), core::DescriptorEventReceiver::resume(), core::DescriptorEventReceiver::setTimeout(), core::EventReceiver::span(), and core::DescriptorEventReceiver::suspend().

Referenced by commandContinue(), and commandStart().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ commandCompleted()

void database::mariadb::MariaDBConnection::commandCompleted ( )

Definition at line 190 of file MariaDBConnection.cpp.

190 {
191 LOG(DEBUG) << connectionName << " MariaDB completed: " << currentCommand->commandInfo();
192 commandSequenceQueue.front().commandCompleted();
193
194 const bool sequenceEmpty = commandSequenceQueue.front().empty();
195
196 delete currentCommand;
197 currentCommand = nullptr;
198
199 if (sequenceEmpty) {
200 commandSequenceQueue.pop_front();
201 }
202 }

References database::mariadb::MariaDBCommand::commandInfo(), connectionName, and currentCommand.

Referenced by checkStatus().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ commandContinue()

void database::mariadb::MariaDBConnection::commandContinue ( int status)

Definition at line 180 of file MariaDBConnection.cpp.

180 {
181 if (currentCommand != nullptr) {
182 checkStatus(currentCommand->commandContinue(status));
183 } else if ((status & MYSQL_WAIT_READ) != 0 && commandSequenceQueue.empty()) {
184 ReadEventReceiver::disable();
185 WriteEventReceiver::disable();
186 ExceptionalConditionEventReceiver::disable();
187 }
188 }

References checkStatus(), database::mariadb::MariaDBCommand::commandContinue(), currentCommand, and core::DescriptorEventReceiver::disable().

Referenced by outOfBandEvent(), outOfBandTimeout(), readEvent(), readTimeout(), writeEvent(), and writeTimeout().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ commandStart()

void database::mariadb::MariaDBConnection::commandStart ( const utils::Timeval & currentTime)

Definition at line 161 of file MariaDBConnection.cpp.

161 {
162 if (!commandSequenceQueue.empty()) {
163 currentCommand = commandSequenceQueue.front().nextCommand();
164
165 LOG(DEBUG) << connectionName << " MariaDB start: " << currentCommand->commandInfo();
166
167 currentCommand->setMariaDBConnection(this);
168 checkStatus(currentCommand->commandStart(mysql, currentTime));
169 } else if (mariaDBClient != nullptr) {
170 if (ReadEventReceiver::isSuspended() && ReadEventReceiver::isEnabled()) {
171 ReadEventReceiver::resume();
172 }
173 } else {
174 ReadEventReceiver::disable();
175 WriteEventReceiver::disable();
176 ExceptionalConditionEventReceiver::disable();
177 }
178 }

References checkStatus(), database::mariadb::MariaDBCommand::commandInfo(), database::mariadb::MariaDBCommand::commandStart(), connectionName, currentCommand, core::DescriptorEventReceiver::disable(), core::DescriptorEventReceiver::isEnabled(), core::DescriptorEventReceiver::isSuspended(), mariaDBClient, core::DescriptorEventReceiver::resume(), and database::mariadb::MariaDBCommand::setMariaDBConnection().

Referenced by database::mariadb::MariaDBCommandStartEvent::onEvent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute_async()

MariaDBCommandSequence & database::mariadb::MariaDBConnection::execute_async ( MariaDBCommandSequence && commandSequence)

Definition at line 138 of file MariaDBConnection.cpp.

138 {
139 if (currentCommand == nullptr && commandSequenceQueue.empty()) {
140 commandStartEvent.span();
141 }
142
143 commandSequenceQueue.emplace_back(std::move(commandSequence));
144
145 return commandSequenceQueue.back();
146 }

References commandStartEvent, and core::EventReceiver::span().

Referenced by database::mariadb::MariaDBClient::execute_async(), and MariaDBConnection().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute_sync()

void database::mariadb::MariaDBConnection::execute_sync ( MariaDBCommand * mariaDBCommand)

Definition at line 148 of file MariaDBConnection.cpp.

148 {
149 mariaDBCommand->commandStart(mysql, utils::Timeval::currentTime());
150
151 if (mysql_errno(mysql) == 0) {
152 if (mariaDBCommand->commandCompleted()) {
153 }
154 } else {
155 mariaDBCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
156 }
157
158 delete mariaDBCommand;
159 }
static Timeval currentTime()
Definition Timeval.cpp:76

References database::mariadb::MariaDBCommand::commandCompleted(), database::mariadb::MariaDBCommand::commandError(), database::mariadb::MariaDBCommand::commandStart(), and utils::Timeval::currentTime().

Referenced by database::mariadb::MariaDBClient::execute_sync().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

MariaDBConnection & database::mariadb::MariaDBConnection::operator= ( const MariaDBConnection & )
delete

◆ outOfBandEvent()

void database::mariadb::MariaDBConnection::outOfBandEvent ( )
overrideprotectedvirtual

Implements core::eventreceiver::ExceptionalConditionEventReceiver.

Definition at line 284 of file MariaDBConnection.cpp.

284 {
285 commandContinue(MYSQL_WAIT_EXCEPT);
286 }

References commandContinue().

Here is the call graph for this function:

◆ outOfBandTimeout()

void database::mariadb::MariaDBConnection::outOfBandTimeout ( )
overrideprotectedvirtual

Reimplemented from core::eventreceiver::ExceptionalConditionEventReceiver.

Definition at line 296 of file MariaDBConnection.cpp.

296 {
297 commandContinue(MYSQL_WAIT_TIMEOUT);
298 }

References commandContinue().

Here is the call graph for this function:

◆ readEvent()

void database::mariadb::MariaDBConnection::readEvent ( )
overrideprotectedvirtual

Implements core::eventreceiver::ReadEventReceiver.

Definition at line 276 of file MariaDBConnection.cpp.

276 {
277 commandContinue(MYSQL_WAIT_READ);
278 }

References commandContinue().

Here is the call graph for this function:

◆ readTimeout()

void database::mariadb::MariaDBConnection::readTimeout ( )
overrideprotectedvirtual

Reimplemented from core::eventreceiver::ReadEventReceiver.

Definition at line 288 of file MariaDBConnection.cpp.

288 {
289 commandContinue(MYSQL_WAIT_TIMEOUT);
290 }

References commandContinue().

Here is the call graph for this function:

◆ unmanaged()

void database::mariadb::MariaDBConnection::unmanaged ( )

Definition at line 204 of file MariaDBConnection.cpp.

204 {
205 mariaDBClient = nullptr;
206 closing = true;
207
208 if (currentCommand == nullptr && commandSequenceQueue.empty()) {
209 if (ReadEventReceiver::isEnabled()) {
210 ReadEventReceiver::disable();
211 }
212 if (WriteEventReceiver::isEnabled()) {
213 WriteEventReceiver::disable();
214 }
215 if (ExceptionalConditionEventReceiver::isEnabled()) {
216 ExceptionalConditionEventReceiver::disable();
217 }
218 }
219 }

References closing, core::DescriptorEventReceiver::disable(), core::DescriptorEventReceiver::isEnabled(), and mariaDBClient.

Referenced by database::mariadb::MariaDBClient::~MariaDBClient().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unobservedEvent()

void database::mariadb::MariaDBConnection::unobservedEvent ( )
overrideprotectedvirtual

Implements core::Observer.

Definition at line 300 of file MariaDBConnection.cpp.

300 {
301 if (!closing) {
302 LOG(ERROR) << connectionName << " MariaDB: Lost connection";
303 }
304
305 if (mariaDBClient != nullptr) {
306 onStateChanged({});
307 }
308
309 delete this;
310 }

References closing, connectionName, and mariaDBClient.

◆ writeEvent()

void database::mariadb::MariaDBConnection::writeEvent ( )
overrideprotectedvirtual

Implements core::eventreceiver::WriteEventReceiver.

Definition at line 280 of file MariaDBConnection.cpp.

280 {
281 commandContinue(MYSQL_WAIT_WRITE);
282 }

References commandContinue().

Here is the call graph for this function:

◆ writeTimeout()

void database::mariadb::MariaDBConnection::writeTimeout ( )
overrideprotectedvirtual

Reimplemented from core::eventreceiver::WriteEventReceiver.

Definition at line 292 of file MariaDBConnection.cpp.

292 {
293 commandContinue(MYSQL_WAIT_TIMEOUT);
294 }

References commandContinue().

Here is the call graph for this function:

Member Data Documentation

◆ closing

bool database::mariadb::MariaDBConnection::closing = false
private

Definition at line 133 of file MariaDBConnection.h.

Referenced by unmanaged(), and unobservedEvent().

◆ commandSequenceQueue

std::deque<MariaDBCommandSequence> database::mariadb::MariaDBConnection::commandSequenceQueue
private

Definition at line 129 of file MariaDBConnection.h.

◆ commandStartEvent

MariaDBCommandStartEvent database::mariadb::MariaDBConnection::commandStartEvent
private

Definition at line 135 of file MariaDBConnection.h.

Referenced by checkStatus(), execute_async(), and MariaDBConnection().

◆ connected

bool database::mariadb::MariaDBConnection::connected = false
private

Definition at line 132 of file MariaDBConnection.h.

Referenced by checkStatus().

◆ connectionName

const std::string database::mariadb::MariaDBConnection::connectionName
private

◆ currentCommand

MariaDBCommand* database::mariadb::MariaDBConnection::currentCommand = nullptr
private

Definition at line 131 of file MariaDBConnection.h.

Referenced by checkStatus(), commandCompleted(), commandContinue(), and commandStart().

◆ mariaDBClient

MariaDBClient* database::mariadb::MariaDBConnection::mariaDBClient = nullptr
private

◆ mysql

MYSQL* database::mariadb::MariaDBConnection::mysql = nullptr
private

Definition at line 126 of file MariaDBConnection.h.

◆ onStateChanged

std::function<void(const MariaDBState&)> database::mariadb::MariaDBConnection::onStateChanged
private

Definition at line 137 of file MariaDBConnection.h.


The documentation for this class was generated from the following files: