From 4236d20ed68f0141ea3d5abacce14c6e72b104a9 Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Tue, 9 Jul 2024 16:46:42 +0000 Subject: [PATCH] Initial work on template for HostGroups_Manager --- include/Base_HostGroups_Manager.h | 29 ++++++++++++++++++++- include/MySQL_HostGroups_Manager.h | 13 +++++----- include/PgSQL_HostGroups_Manager.h | 11 ++++---- lib/Base_HostGroups_Manager.cpp | 41 ++++++++++++++++++++++++------ lib/MySQL_HostGroups_Manager.cpp | 7 ++++- lib/PgSQL_HostGroups_Manager.cpp | 5 +++- 6 files changed, 84 insertions(+), 22 deletions(-) diff --git a/include/Base_HostGroups_Manager.h b/include/Base_HostGroups_Manager.h index 6881a9e66..ddd5e1b1c 100644 --- a/include/Base_HostGroups_Manager.h +++ b/include/Base_HostGroups_Manager.h @@ -1,5 +1,6 @@ template class BaseSrvList; template class BaseHGC; +template class Base_HostGroups_Manager; class MyHGC; class PgSQL_HGC; @@ -634,7 +635,34 @@ struct srv_opts_t { int64_t max_conns; int32_t use_ssl; }; +#endif // 0 + +template +class Base_HostGroups_Manager { + private: + SQLite3DB *admindb; + SQLite3DB *mydb; + pthread_mutex_t readonly_mutex; + std::set read_only_set1; + std::set read_only_set2; + pthread_mutex_t lock; + PtrArray *MyHostGroups; + std::unordered_mapMyHostGroups_map; + + HGC * MyHGC_find(unsigned int); + HGC * MyHGC_create(unsigned int); + + public: + Base_HostGroups_Manager(); + HGC * MyHGC_lookup(unsigned int); + + friend class MySQL_HostGroups_Manager; + friend class PgSQL_HostGroups_Manager; + +}; + +#if 0 class MySQL_HostGroups_Manager { private: SQLite3DB *admindb; @@ -1101,7 +1129,6 @@ class MySQL_HostGroups_Manager { * @param lock When supplied the function calls 'wrlock()' and 'wrunlock()' functions for accessing the db. */ void update_table_mysql_servers_for_monitor(bool lock=false); - MyHGC * MyHGC_lookup(unsigned int); void MyConn_add_to_pool(MySQL_Connection *); /** diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index 6c21cb7c7..aa0001b70 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -548,15 +548,16 @@ struct srv_opts_t { int32_t use_ssl; }; -class MySQL_HostGroups_Manager { +class MySQL_HostGroups_Manager : public Base_HostGroups_Manager { private: +#if 0 SQLite3DB *admindb; SQLite3DB *mydb; pthread_mutex_t readonly_mutex; std::set read_only_set1; std::set read_only_set2; pthread_mutex_t lock; - +#endif // 0 enum HGM_TABLES { MYSQL_SERVERS_V2 = 0, MYSQL_REPLICATION_HOSTGROUPS, @@ -671,15 +672,16 @@ class MySQL_HostGroups_Manager { uint64_t hgsm_mysql_replication_hostgroups_checksum = 0; +#if 0 PtrArray *MyHostGroups; std::unordered_mapMyHostGroups_map; - +#endif // 0 std::mutex Servers_SSL_Params_map_mutex; std::unordered_map Servers_SSL_Params_map; - +#if 0 MyHGC * MyHGC_find(unsigned int); MyHGC * MyHGC_create(unsigned int); - +#endif // 0 void add(MySrvC *, unsigned int); void purge_mysql_servers_table(); void generate_mysql_servers_table(int *_onlyhg=NULL); @@ -1014,7 +1016,6 @@ class MySQL_HostGroups_Manager { * @param lock When supplied the function calls 'wrlock()' and 'wrunlock()' functions for accessing the db. */ void update_table_mysql_servers_for_monitor(bool lock=false); - MyHGC * MyHGC_lookup(unsigned int); void MyConn_add_to_pool(MySQL_Connection *); /** diff --git a/include/PgSQL_HostGroups_Manager.h b/include/PgSQL_HostGroups_Manager.h index a895aadc1..65f448075 100644 --- a/include/PgSQL_HostGroups_Manager.h +++ b/include/PgSQL_HostGroups_Manager.h @@ -453,15 +453,16 @@ struct PgSQL_srv_opts_t { int32_t use_ssl; }; -class PgSQL_HostGroups_Manager { - private: +class PgSQL_HostGroups_Manager : public Base_HostGroups_Manager { +#if 0 SQLite3DB *admindb; SQLite3DB *mydb; pthread_mutex_t readonly_mutex; std::set read_only_set1; std::set read_only_set2; pthread_mutex_t lock; - +#endif // 0 + private: enum HGM_TABLES { PgSQL_SERVERS_V2 = 0, PgSQL_REPLICATION_HOSTGROUPS, @@ -574,12 +575,13 @@ class PgSQL_HostGroups_Manager { */ uint64_t hgsm_pgsql_replication_hostgroups_checksum = 0; - +#if 0 PtrArray *MyHostGroups; std::unordered_mapMyHostGroups_map; PgSQL_HGC * MyHGC_find(unsigned int); PgSQL_HGC * MyHGC_create(unsigned int); +#endif // 0 void add(PgSQL_SrvC *, unsigned int); void purge_pgsql_servers_table(); @@ -869,7 +871,6 @@ class PgSQL_HostGroups_Manager { * @param lock When supplied the function calls 'wrlock()' and 'wrunlock()' functions for accessing the db. */ void update_table_pgsql_servers_for_monitor(bool lock=false); - PgSQL_HGC * MyHGC_lookup(unsigned int); void MyConn_add_to_pool(PgSQL_Connection *); /** diff --git a/lib/Base_HostGroups_Manager.cpp b/lib/Base_HostGroups_Manager.cpp index a1cdf3c88..d9a9989d4 100644 --- a/lib/Base_HostGroups_Manager.cpp +++ b/lib/Base_HostGroups_Manager.cpp @@ -38,6 +38,16 @@ using std::function; #include "Base_HostGroups_Manager.h" +template Base_HostGroups_Manager::Base_HostGroups_Manager(); +template MyHGC * Base_HostGroups_Manager::MyHGC_find(unsigned int); +template MyHGC * Base_HostGroups_Manager::MyHGC_create(unsigned int); +template MyHGC * Base_HostGroups_Manager::MyHGC_lookup(unsigned int); + +template Base_HostGroups_Manager::Base_HostGroups_Manager(); +template PgSQL_HGC * Base_HostGroups_Manager::MyHGC_find(unsigned int); +template PgSQL_HGC * Base_HostGroups_Manager::MyHGC_create(unsigned int); +template PgSQL_HGC * Base_HostGroups_Manager::MyHGC_lookup(unsigned int); + #if 0 #define SAFE_SQLITE3_STEP(_stmt) do {\ do {\ @@ -623,7 +633,17 @@ hg_metrics_map = std::make_tuple( ) } ); +#endif // 0 + +template +Base_HostGroups_Manager::Base_HostGroups_Manager() { + pthread_mutex_init(&readonly_mutex, NULL); + pthread_mutex_init(&lock, NULL); + admindb=NULL; // initialized only if needed + mydb=new SQLite3DB(); +} +#if 0 MySQL_HostGroups_Manager::MySQL_HostGroups_Manager() { status.client_connections=0; status.client_connections_aborted=0; @@ -2153,6 +2173,7 @@ SQLite3_result * MySQL_HostGroups_Manager::dump_table_mysql(const string& name) return resultset; } +#endif // 0 /** * @brief Create a new MySQL host group container. * @@ -2162,8 +2183,9 @@ SQLite3_result * MySQL_HostGroups_Manager::dump_table_mysql(const string& name) * @param _hid The host group ID for the new container. * @return A pointer to the newly created `MyHGC` instance. */ -MyHGC * MySQL_HostGroups_Manager::MyHGC_create(unsigned int _hid) { - MyHGC *myhgc=new MyHGC(_hid); +template +HGC * Base_HostGroups_Manager::MyHGC_create(unsigned int _hid) { + HGC *myhgc=new HGC(_hid); return myhgc; } @@ -2177,11 +2199,12 @@ MyHGC * MySQL_HostGroups_Manager::MyHGC_create(unsigned int _hid) { * @param _hid The host group ID to search for. * @return A pointer to the found `MyHGC` instance if found; otherwise, a null pointer. */ -MyHGC * MySQL_HostGroups_Manager::MyHGC_find(unsigned int _hid) { +template +HGC * Base_HostGroups_Manager::MyHGC_find(unsigned int _hid) { if (MyHostGroups->len < 100) { // for few HGs, we use the legacy search for (unsigned int i=0; ilen; i++) { - MyHGC *myhgc=(MyHGC *)MyHostGroups->index(i); + HGC *myhgc=(HGC *)MyHostGroups->index(i); if (myhgc->hid==_hid) { return myhgc; } @@ -2190,9 +2213,9 @@ MyHGC * MySQL_HostGroups_Manager::MyHGC_find(unsigned int _hid) { // for a large number of HGs, we use the unordered_map // this search is slower for a small number of HGs, therefore we use // it only for large number of HGs - std::unordered_map::const_iterator it = MyHostGroups_map.find(_hid); + typename std::unordered_map::const_iterator it = MyHostGroups_map.find(_hid); if (it != MyHostGroups_map.end()) { - MyHGC *myhgc = it->second; + HGC *myhgc = it->second; return myhgc; } } @@ -2211,8 +2234,9 @@ MyHGC * MySQL_HostGroups_Manager::MyHGC_find(unsigned int _hid) { * @return A pointer to the found or newly created `MyHGC` instance. * @note The function assertion fails if a newly created container is not found. */ -MyHGC * MySQL_HostGroups_Manager::MyHGC_lookup(unsigned int _hid) { - MyHGC *myhgc=NULL; +template +HGC * Base_HostGroups_Manager::MyHGC_lookup(unsigned int _hid) { + HGC *myhgc=NULL; myhgc=MyHGC_find(_hid); if (myhgc==NULL) { myhgc=MyHGC_create(_hid); @@ -2225,6 +2249,7 @@ MyHGC * MySQL_HostGroups_Manager::MyHGC_lookup(unsigned int _hid) { return myhgc; } +#if 0 void MySQL_HostGroups_Manager::increase_reset_counter() { wrlock(); status.myconnpoll_reset++; diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 28d0980c6..5cf7c3dc8 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -657,13 +657,17 @@ MySQL_HostGroups_Manager::MySQL_HostGroups_Manager() { status.access_denied_max_user_connections=0; status.select_for_update_or_equivalent=0; status.auto_increment_delay_multiplex=0; +#if 0 pthread_mutex_init(&readonly_mutex, NULL); +#endif // 0 pthread_mutex_init(&Group_Replication_Info_mutex, NULL); pthread_mutex_init(&Galera_Info_mutex, NULL); pthread_mutex_init(&AWS_Aurora_Info_mutex, NULL); +#if 0 pthread_mutex_init(&lock, NULL); admindb=NULL; // initialized only if needed mydb=new SQLite3DB(); +#endif // 0 #ifdef DEBUG mydb->open((char *)"file:mem_mydb?mode=memory&cache=shared", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX); #else @@ -2265,6 +2269,7 @@ SQLite3_result * MySQL_HostGroups_Manager::dump_table_mysql(const string& name) return resultset; } +#if 0 /** * @brief Create a new MySQL host group container. * @@ -2310,7 +2315,6 @@ MyHGC * MySQL_HostGroups_Manager::MyHGC_find(unsigned int _hid) { } return NULL; } - /** * @brief Lookup or create a MySQL host group container by host group ID. * @@ -2336,6 +2340,7 @@ MyHGC * MySQL_HostGroups_Manager::MyHGC_lookup(unsigned int _hid) { MyHostGroups_map.emplace(_hid,myhgc); return myhgc; } +#endif // 0 void MySQL_HostGroups_Manager::increase_reset_counter() { wrlock(); diff --git a/lib/PgSQL_HostGroups_Manager.cpp b/lib/PgSQL_HostGroups_Manager.cpp index 304c9cfa9..eade7fa4d 100644 --- a/lib/PgSQL_HostGroups_Manager.cpp +++ b/lib/PgSQL_HostGroups_Manager.cpp @@ -836,10 +836,12 @@ PgSQL_HostGroups_Manager::PgSQL_HostGroups_Manager() { status.access_denied_max_user_connections=0; status.select_for_update_or_equivalent=0; status.auto_increment_delay_multiplex=0; +#if 0 pthread_mutex_init(&readonly_mutex, NULL); pthread_mutex_init(&lock, NULL); admindb=NULL; // initialized only if needed mydb=new SQLite3DB(); +#endif // 0 #ifdef DEBUG mydb->open((char *)"file:mem_mydb?mode=memory&cache=shared", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX); #else @@ -2057,7 +2059,7 @@ SQLite3_result * PgSQL_HostGroups_Manager::dump_table_pgsql(const string& name) return resultset; } - +#if 0 PgSQL_HGC * PgSQL_HostGroups_Manager::MyHGC_create(unsigned int _hid) { PgSQL_HGC *myhgc=new PgSQL_HGC(_hid); return myhgc; @@ -2098,6 +2100,7 @@ PgSQL_HGC * PgSQL_HostGroups_Manager::MyHGC_lookup(unsigned int _hid) { MyHostGroups_map.emplace(_hid,myhgc); return myhgc; } +#endif // 0 void PgSQL_HostGroups_Manager::increase_reset_counter() { wrlock();