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
MYSQL * mysql
const std::string connectionName
std::deque< MariaDBCommandSequencecommandSequenceQueue
MariaDBCommandcurrentCommand = nullptr
bool connected = 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 62 of file MariaDBConnection.cpp.

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

114 {
115 for (MariaDBCommandSequence& mariaDBCommandSequence : commandSequenceQueue) {
116 for (MariaDBCommand* mariaDBCommand : mariaDBCommandSequence.sequence()) {
118 mariaDBCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
119 }
120
121 delete mariaDBCommand;
122 }
123 }
124
125 if (mariaDBClient != nullptr) {
126 mariaDBClient->connectionVanished();
127 }
128
129 mysql_close(mysql);
130 mysql_library_end();
131 }
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 213 of file MariaDBConnection.cpp.

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

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 185 of file MariaDBConnection.cpp.

185 {
186 LOG(DEBUG) << connectionName << " MariaDB completed: " << currentCommand->commandInfo();
187 commandSequenceQueue.front().commandCompleted();
188
189 if (commandSequenceQueue.front().empty()) {
190 commandSequenceQueue.pop_front();
191 }
192
193 delete currentCommand;
194 currentCommand = nullptr;
195 }

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 175 of file MariaDBConnection.cpp.

175 {
176 if (currentCommand != nullptr) {
177 checkStatus(currentCommand->commandContinue(status));
178 } else if ((status & MYSQL_WAIT_READ) != 0 && commandSequenceQueue.empty()) {
179 ReadEventReceiver::disable();
180 WriteEventReceiver::disable();
181 ExceptionalConditionEventReceiver::disable();
182 }
183 }

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 156 of file MariaDBConnection.cpp.

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

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 133 of file MariaDBConnection.cpp.

133 {
134 if (currentCommand == nullptr && commandSequenceQueue.empty()) {
135 commandStartEvent.span();
136 }
137
138 commandSequenceQueue.emplace_back(std::move(commandSequence));
139
140 return commandSequenceQueue.back();
141 }

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 143 of file MariaDBConnection.cpp.

143 {
144 mariaDBCommand->commandStart(mysql, utils::Timeval::currentTime());
145
146 if (mysql_errno(mysql) == 0) {
147 if (mariaDBCommand->commandCompleted()) {
148 }
149 } else {
150 mariaDBCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
151 }
152
153 delete mariaDBCommand;
154 }
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 276 of file MariaDBConnection.cpp.

276 {
277 commandContinue(MYSQL_WAIT_EXCEPT);
278 }

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 288 of file MariaDBConnection.cpp.

288 {
289 commandContinue(MYSQL_WAIT_TIMEOUT);
290 }

References commandContinue().

Here is the call graph for this function:

◆ readEvent()

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

Implements core::eventreceiver::ReadEventReceiver.

Definition at line 268 of file MariaDBConnection.cpp.

268 {
269 commandContinue(MYSQL_WAIT_READ);
270 }

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 280 of file MariaDBConnection.cpp.

280 {
281 commandContinue(MYSQL_WAIT_TIMEOUT);
282 }

References commandContinue().

Here is the call graph for this function:

◆ unmanaged()

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

Definition at line 197 of file MariaDBConnection.cpp.

197 {
198 mariaDBClient = nullptr;
199
200 if (currentCommand == nullptr && commandSequenceQueue.empty()) {
201 if (ReadEventReceiver::isEnabled()) {
202 ReadEventReceiver::disable();
203 }
204 if (WriteEventReceiver::isEnabled()) {
205 WriteEventReceiver::disable();
206 }
207 if (ExceptionalConditionEventReceiver::isEnabled()) {
208 ExceptionalConditionEventReceiver::disable();
209 }
210 }
211 }

References 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 292 of file MariaDBConnection.cpp.

292 {
293 LOG(ERROR) << connectionName << " MariaDB: Lost connection";
294
295 if (mariaDBClient != nullptr) {
296 onStateChanged({});
297 }
298
299 delete this;
300 }

References connectionName, and mariaDBClient.

◆ writeEvent()

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

Implements core::eventreceiver::WriteEventReceiver.

Definition at line 272 of file MariaDBConnection.cpp.

272 {
273 commandContinue(MYSQL_WAIT_WRITE);
274 }

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 284 of file MariaDBConnection.cpp.

284 {
285 commandContinue(MYSQL_WAIT_TIMEOUT);
286 }

References commandContinue().

Here is the call graph for this function:

Member Data Documentation

◆ 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 134 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
private

◆ mysql

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

Definition at line 126 of file MariaDBConnection.h.

◆ onStateChanged

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

Definition at line 136 of file MariaDBConnection.h.


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