Horizon Official Technical Documentation
DatabaseProcess Class Reference

#include <Server.hpp>

+ Inheritance diagram for DatabaseProcess:
+ Collaboration diagram for DatabaseProcess:

Public Member Functions

 DatabaseProcess ()
 
 DatabaseProcess (Kernel *kernel)
 
 ~DatabaseProcess ()
 
void initialize (int segment_number=1) override
 
void reinitialize ()
 
void initialize (boost::asio::io_context &io_context, int segment_number, std::string host, int port, std::string user, std::string pass, std::string database)
 
void finalize () override
 
std::shared_ptr< boost::mysql::tcp_ssl_connection > get_connection ()
 
bool is_initialized () override
 
bool is_finalized () override
 
- Public Member Functions inherited from KernelComponent
 KernelComponent (Kernel *kernel, Horizon::System::runtime_module_type module_type)
 
virtual void initialize (int segment_number=1)
 
virtual void finalize ()
 
virtual bool is_initialized ()
 
virtual bool is_finalized ()
 
void set_segment_number (int64_t segment_number)
 
int64_t get_segment_number ()
 
void system_routine_queue_push (std::shared_ptr< Horizon::System::RuntimeContext > context)
 
void system_routine_queue_push (std::shared_ptr< Horizon::System::RuntimeContextChain > context)
 
void system_routine_process_queue ()
 
void system_routine_register (Horizon::System::runtime_module_type module_t, Horizon::System::runtime_synchronization_method sync_t, std::shared_ptr< Horizon::System::RuntimeContext > context)
 
const std::string get_uuid_string ()
 
const std::string get_type_string ()
 
Horizon::System::SystemRoutineManagerget_system_routine_manager ()
 
Kernelget_kernel ()
 
void set_thread_cpu_id (int cpu_id)
 
int get_thread_cpu_id ()
 
void set_thread_update_rate (double rate)
 
double get_thread_update_rate ()
 
void set_total_execution_time (int time)
 
int get_total_execution_time ()
 
void calculate_and_set_cpu_load ()
 

Protected Attributes

boost::asio::io_context * _io_context
 
std::shared_ptr< boost::asio::ssl::context > _ssl_ctx {nullptr}
 
std::shared_ptr< boost::mysql::tcp_ssl_connection > _connection {nullptr}
 
std::string _host
 
int _port
 
std::string _user
 
std::string _pass
 
std::string _database
 
std::atomic< bool > _is_initialized {false}
 
std::atomic< bool > _is_finalized {false}
 

Constructor & Destructor Documentation

◆ DatabaseProcess() [1/2]

DatabaseProcess::DatabaseProcess ( )
inline
KernelComponent(Kernel *kernel, Horizon::System::runtime_module_type module_type)
Definition: Server.hpp:194
@ RUNTIME_DATABASE
Definition: System.hpp:88

◆ DatabaseProcess() [2/2]

DatabaseProcess::DatabaseProcess ( Kernel kernel)
inline

◆ ~DatabaseProcess()

DatabaseProcess::~DatabaseProcess ( )
inline
377 {
378 _connection.reset();
379 _ssl_ctx.reset();
380 }
std::shared_ptr< boost::mysql::tcp_ssl_connection > _connection
Definition: Server.hpp:419
std::shared_ptr< boost::asio::ssl::context > _ssl_ctx
Definition: Server.hpp:418

References _connection, and _ssl_ctx.

Member Function Documentation

◆ finalize()

void DatabaseProcess::finalize ( )
inlineoverridevirtual

Reimplemented from KernelComponent.

402 {
403 // Close connection object.
404 if (_connection != nullptr) {
405 _connection->close();
406 }
407
408 _is_finalized.exchange(true);
409 }
std::atomic< bool > _is_finalized
Definition: Server.hpp:426

References _connection, and _is_finalized.

◆ get_connection()

std::shared_ptr< boost::mysql::tcp_ssl_connection > DatabaseProcess::get_connection ( )
254 {
255 try {
256 boost::mysql::results results;
257 _connection->execute("SELECT 'Hello World!'", results);
258 } catch (const boost::mysql::error_with_diagnostics &e) {
259 HLog(error) << "Database connection error: " << e.what();
260 HLog(error) << "Reconnecting...";
261 reinitialize();
262 }
263 return _connection;
264}
#define HLog(type)
Definition: Logger.hpp:122
void reinitialize()
Definition: Server.hpp:389

References _connection, HLog, and reinitialize().

+ Here is the call graph for this function:

◆ initialize() [1/2]

void DatabaseProcess::initialize ( boost::asio::io_context &  io_context,
int  segment_number,
std::string  host,
int  port,
std::string  user,
std::string  pass,
std::string  database 
)
267{
268 set_segment_number(segment_number);
269
270 try {
271 _io_context = &io_context;
272 _ssl_ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tls_client);
273 _connection = std::make_shared<boost::mysql::tcp_ssl_connection>(_io_context->get_executor(), *_ssl_ctx);
274 boost::asio::ip::tcp::resolver resolver(_io_context->get_executor());
275 auto endpoints = resolver.resolve(host, std::to_string(port));
276 boost::mysql::handshake_params params(user, pass, database);
277 _connection->connect(*endpoints.begin(), params);
278 _is_initialized.exchange(true);
279 _host = host;
280 _port = port;
281 _user = user;
282 _pass = pass;
283 _database = database;
284#if WIN32
285 DWORD cpu = GetCurrentProcessorNumber();
286 if (get_thread_cpu_id() != (int) cpu)
288#elif __linux__
289 int cpu = sched_getcpu();
290 if (get_thread_cpu_id() != cpu)
292#endif
294 } catch (boost::mysql::error_with_diagnostics &error) {
295 HLog(error) << error.what();
296 }
297}
std::string _host
Definition: Server.hpp:420
std::string _user
Definition: Server.hpp:422
std::string _pass
Definition: Server.hpp:423
int _port
Definition: Server.hpp:421
boost::asio::io_context * _io_context
Definition: Server.hpp:417
std::atomic< bool > _is_initialized
Definition: Server.hpp:425
std::string _database
Definition: Server.hpp:424
void set_thread_cpu_id(int cpu_id)
Definition: Server.hpp:239
void calculate_and_set_cpu_load()
Definition: Server.hpp:257
void set_segment_number(int64_t segment_number)
Definition: Server.hpp:206
int get_thread_cpu_id()
Definition: Server.hpp:240

References _connection, _database, _host, _io_context, _is_initialized, _pass, _port, _ssl_ctx, _user, KernelComponent::calculate_and_set_cpu_load(), KernelComponent::get_thread_cpu_id(), HLog, KernelComponent::set_segment_number(), and KernelComponent::set_thread_cpu_id().

+ Here is the call graph for this function:

◆ initialize() [2/2]

void DatabaseProcess::initialize ( int  segment_number = 1)
inlineoverridevirtual

Reimplemented from KernelComponent.

383 {
384 set_segment_number(segment_number);
385 HLog(error) << "Database not configured";
386 _is_initialized.exchange(true);
387 }

References _is_initialized, HLog, and KernelComponent::set_segment_number().

Referenced by BOOST_AUTO_TEST_CASE(), and reinitialize().

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

◆ is_finalized()

bool DatabaseProcess::is_finalized ( )
inlineoverridevirtual

Reimplemented from KernelComponent.

414{ return _is_finalized.load(); }

References _is_finalized.

◆ is_initialized()

bool DatabaseProcess::is_initialized ( )
inlineoverridevirtual

Reimplemented from KernelComponent.

413{ return _is_initialized.load(); }

References _is_initialized.

◆ reinitialize()

void DatabaseProcess::reinitialize ( )
inline
390 {
391 _connection.reset();
392 _ssl_ctx.reset();
393 _is_initialized.exchange(false);
394 _is_finalized.exchange(false);
395 if (_io_context != nullptr)
397 }
void initialize(int segment_number=1) override
Definition: Server.hpp:382
int64_t get_segment_number()
Definition: Server.hpp:207

References _connection, _database, _host, _io_context, _is_finalized, _is_initialized, _pass, _port, _ssl_ctx, _user, KernelComponent::get_segment_number(), and initialize().

Referenced by get_connection().

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

Member Data Documentation

◆ _connection

std::shared_ptr<boost::mysql::tcp_ssl_connection> DatabaseProcess::_connection {nullptr}
protected

◆ _database

std::string DatabaseProcess::_database
protected

Referenced by initialize(), and reinitialize().

◆ _host

std::string DatabaseProcess::_host
protected

Referenced by initialize(), and reinitialize().

◆ _io_context

boost::asio::io_context* DatabaseProcess::_io_context
protected

Referenced by initialize(), and reinitialize().

◆ _is_finalized

std::atomic<bool> DatabaseProcess::_is_finalized {false}
protected

◆ _is_initialized

std::atomic<bool> DatabaseProcess::_is_initialized {false}
protected

◆ _pass

std::string DatabaseProcess::_pass
protected

Referenced by initialize(), and reinitialize().

◆ _port

int DatabaseProcess::_port
protected

Referenced by initialize(), and reinitialize().

◆ _ssl_ctx

std::shared_ptr<boost::asio::ssl::context> DatabaseProcess::_ssl_ctx {nullptr}
protected

◆ _user

std::string DatabaseProcess::_user
protected

Referenced by initialize(), and reinitialize().


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