diff --git a/include/PgSQL_HostGroups_Manager.h b/include/PgSQL_HostGroups_Manager.h index 72edb4cc7..36572215c 100644 --- a/include/PgSQL_HostGroups_Manager.h +++ b/include/PgSQL_HostGroups_Manager.h @@ -253,76 +253,6 @@ class PgSQL_HGC: public BaseHGC { PgSQL_SrvC *get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_lag_ms, PgSQL_Session *sess); }; -#if 0 -class PgSQL_Group_Replication_Info { - public: - int writer_hostgroup; - int backup_writer_hostgroup; - int reader_hostgroup; - int offline_hostgroup; - int max_writers; - int max_transactions_behind; - char *comment; - bool active; - int writer_is_also_reader; - bool __active; - bool need_converge; // this is set to true on LOAD PgSQL SERVERS TO RUNTIME . This ensure that checks wil take an action - int current_num_writers; - int current_num_backup_writers; - int current_num_readers; - int current_num_offline; - PgSQL_Group_Replication_Info(int w, int b, int r, int o, int mw, int mtb, bool _a, int _w, char *c); - bool update(int b, int r, int o, int mw, int mtb, bool _a, int _w, char *c); - ~PgSQL_Group_Replication_Info(); -}; - -class PgSQL_Galera_Info { - public: - int writer_hostgroup; - int backup_writer_hostgroup; - int reader_hostgroup; - int offline_hostgroup; - int max_writers; - int max_transactions_behind; - char *comment; - bool active; - int writer_is_also_reader; - bool __active; - bool need_converge; // this is set to true on LOAD PgSQL SERVERS TO RUNTIME . This ensure that checks wil take an action - int current_num_writers; - int current_num_backup_writers; - int current_num_readers; - int current_num_offline; - PgSQL_Galera_Info(int w, int b, int r, int o, int mw, int mtb, bool _a, int _w, char *c); - bool update(int b, int r, int o, int mw, int mtb, bool _a, int _w, char *c); - ~PgSQL_Galera_Info(); -}; - -class PgSQL_AWS_Aurora_Info { - public: - int writer_hostgroup; - int reader_hostgroup; - int aurora_port; - int max_lag_ms; - int add_lag_ms; - int min_lag_ms; - int lag_num_checks; - int check_interval_ms; - int check_timeout_ms; - int writer_is_also_reader; - int new_reader_weight; - // TODO - // add intermediary status value, for example the last check time - char * domain_name; - char * comment; - bool active; - bool __active; - PgSQL_AWS_Aurora_Info(int w, int r, int _port, char *_end_addr, int maxl, int al, int minl, int lnc, int ci, int ct, bool _a, int wiar, int nrw, char *c); - bool update(int r, int _port, char *_end_addr, int maxl, int al, int minl, int lnc, int ci, int ct, bool _a, int wiar, int nrw, char *c); - ~PgSQL_AWS_Aurora_Info(); -}; -#endif // 0 - struct PgSQL_p_hg_counter { enum metric { servers_table_version = 0, @@ -642,12 +572,6 @@ class PgSQL_HostGroups_Manager : public Base_HostGroups_Manager { * @brief Update the prometheus "connection_pool" counters. */ void p_update_connection_pool(); -#if 0 - /** - * @brief Update the "stats_pgsql_gtid_executed" counters. - */ - void p_update_pgsql_gtid_executed(); -#endif // 0 void p_update_connection_pool_update_counter( const std::string& endpoint_id, const std::map& labels, @@ -927,12 +851,6 @@ class PgSQL_HostGroups_Manager : public Base_HostGroups_Manager { void set_server_current_latency_us(char *hostname, int port, unsigned int _current_latency_us); unsigned long long Get_Memory_Stats(); -#if 0 - SQLite3_result * get_stats_pgsql_gtid_executed(); - void generate_pgsql_gtid_executed_tables(); - bool gtid_exists(PgSQL_SrvC *mysrvc, char * gtid_uuid, uint64_t gtid_trxid); -#endif // 0 - SQLite3_result *SQL3_Get_ConnPool_Stats(); void increase_reset_counter(); diff --git a/lib/PgSQL_HostGroups_Manager.cpp b/lib/PgSQL_HostGroups_Manager.cpp index 2ece340d0..5b95db553 100644 --- a/lib/PgSQL_HostGroups_Manager.cpp +++ b/lib/PgSQL_HostGroups_Manager.cpp @@ -1704,106 +1704,6 @@ uint64_t PgSQL_HostGroups_Manager::get_pgsql_servers_checksum(SQLite3_result* ru return table_resultset_checksum[HGM_TABLES::PgSQL_SERVERS]; } -#if 0 -bool PgSQL_HostGroups_Manager::gtid_exists(PgSQL_SrvC *mysrvc, char * gtid_uuid, uint64_t gtid_trxid) { - bool ret = false; - pthread_rwlock_rdlock(>id_rwlock); - std::string s1 = mysrvc->address; - s1.append(":"); - s1.append(std::to_string(mysrvc->port)); - std::unordered_map ::iterator it2; - it2 = gtid_map.find(s1); - PgSQL_GTID_Server_Data *gtid_is=NULL; - if (it2!=gtid_map.end()) { - gtid_is=it2->second; - if (gtid_is) { - if (gtid_is->active == true) { - ret = gtid_is->gtid_exists(gtid_uuid,gtid_trxid); - } - } - } - //proxy_info("Checking if server %s has GTID %s:%lu . %s\n", s1.c_str(), gtid_uuid, gtid_trxid, (ret ? "YES" : "NO")); - pthread_rwlock_unlock(>id_rwlock); - return ret; -} - -void PgSQL_HostGroups_Manager::generate_pgsql_gtid_executed_tables() { - pthread_rwlock_wrlock(>id_rwlock); - // first, set them all as active = false - std::unordered_map::iterator it = gtid_map.begin(); - while(it != gtid_map.end()) { - PgSQL_GTID_Server_Data * gtid_si = it->second; - if (gtid_si) { - gtid_si->active = false; - } - it++; - } - - // NOTE: We are required to lock while iterating over 'MyHostGroups'. Otherwise race conditions could take place, - // e.g. servers could be purged by 'purge_pgsql_servers_table' and invalid memory be accessed. - wrlock(); - for (unsigned int i=0; ilen; i++) { - PgSQL_HGC *myhgc=(PgSQL_HGC *)MyHostGroups->index(i); - PgSQL_SrvC *mysrvc=NULL; - for (unsigned int j=0; jmysrvs->servers->len; j++) { - mysrvc=myhgc->mysrvs->idx(j); - if (mysrvc->gtid_port) { - std::string s1 = mysrvc->address; - s1.append(":"); - s1.append(std::to_string(mysrvc->port)); - std::unordered_map ::iterator it2; - it2 = gtid_map.find(s1); - PgSQL_GTID_Server_Data *gtid_is=NULL; - if (it2!=gtid_map.end()) { - gtid_is=it2->second; - if (gtid_is == NULL) { - gtid_map.erase(it2); - } - } - if (gtid_is) { - gtid_is->active = true; - } else if (mysrvc->status != MYSQL_SERVER_STATUS_OFFLINE_HARD) { - // we didn't find it. Create it - /* - struct ev_io *watcher = (struct ev_io *)malloc(sizeof(struct ev_io)); - gtid_is = new PgSQL_GTID_Server_Data(watcher, mysrvc->address, mysrvc->port, mysrvc->gtid_port); - gtid_map.emplace(s1,gtid_is); - */ - struct ev_io * c = NULL; - c = new_connector(mysrvc->address, mysrvc->gtid_port, mysrvc->port); - if (c) { - gtid_is = (PgSQL_GTID_Server_Data *)c->data; - gtid_map.emplace(s1,gtid_is); - //pthread_mutex_lock(&ev_loop_mutex); - ev_io_start(PgHGM->gtid_ev_loop,c); - //pthread_mutex_unlock(&ev_loop_mutex); - } - } - } - } - } - wrunlock(); - std::vector to_remove; - it = gtid_map.begin(); - while(it != gtid_map.end()) { - PgSQL_GTID_Server_Data * gtid_si = it->second; - if (gtid_si && gtid_si->active == false) { - to_remove.push_back(it->first); - } - it++; - } - for (std::vector::iterator it3=to_remove.begin(); it3!=to_remove.end(); ++it3) { - it = gtid_map.find(*it3); - PgSQL_GTID_Server_Data * gtid_si = it->second; - ev_io_stop(PgHGM->gtid_ev_loop, gtid_si->w); - close(gtid_si->w->fd); - free(gtid_si->w); - gtid_map.erase(*it3); - } - pthread_rwlock_unlock(>id_rwlock); -} -#endif // 0 - void PgSQL_HostGroups_Manager::purge_pgsql_servers_table() { for (unsigned int i=0; ilen; i++) { PgSQL_HGC *myhgc=(PgSQL_HGC *)MyHostGroups->index(i); @@ -2057,49 +1957,6 @@ 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; -} - -PgSQL_HGC * PgSQL_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++) { - PgSQL_HGC *myhgc=(PgSQL_HGC *)MyHostGroups->index(i); - if (myhgc->hid==_hid) { - return myhgc; - } - } - } else { - // 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); - if (it != MyHostGroups_map.end()) { - PgSQL_HGC *myhgc = it->second; - return myhgc; - } - } - return NULL; -} - -PgSQL_HGC * PgSQL_HostGroups_Manager::MyHGC_lookup(unsigned int _hid) { - PgSQL_HGC *myhgc=NULL; - myhgc=MyHGC_find(_hid); - if (myhgc==NULL) { - myhgc=MyHGC_create(_hid); - } else { - return myhgc; - } - assert(myhgc); - MyHostGroups->add(myhgc); - MyHostGroups_map.emplace(_hid,myhgc); - return myhgc; -} -#endif // 0 - void PgSQL_HostGroups_Manager::increase_reset_counter() { wrlock(); status.myconnpoll_reset++; @@ -4237,10 +4094,6 @@ void PgSQL_HostGroups_Manager::p_update_metrics() { // Update the *connection_pool* metrics this->p_update_connection_pool(); -#if 0 - // Update the *gtid_executed* metrics - this->p_update_pgsql_gtid_executed(); -#endif // 0 } SQLite3_result * PgSQL_HostGroups_Manager::SQL3_Get_ConnPool_Stats() { @@ -4321,186 +4174,6 @@ unsigned long long PgSQL_HostGroups_Manager::Get_Memory_Stats() { return intsize; } -#if 0 -PgSQL_Group_Replication_Info::PgSQL_Group_Replication_Info(int w, int b, int r, int o, int mw, int mtb, bool _a, int _w, char *c) { - comment=NULL; - if (c) { - comment=strdup(c); - } - writer_hostgroup=w; - backup_writer_hostgroup=b; - reader_hostgroup=r; - offline_hostgroup=o; - max_writers=mw; - max_transactions_behind=mtb; - active=_a; - writer_is_also_reader=_w; - current_num_writers=0; - current_num_backup_writers=0; - current_num_readers=0; - current_num_offline=0; - __active=true; - need_converge=true; -} - -PgSQL_Group_Replication_Info::~PgSQL_Group_Replication_Info() { - if (comment) { - free(comment); - comment=NULL; - } -} - -bool PgSQL_Group_Replication_Info::update(int b, int r, int o, int mw, int mtb, bool _a, int _w, char *c) { - bool ret=false; - __active=true; - if (backup_writer_hostgroup!=b) { - backup_writer_hostgroup=b; - ret=true; - } - if (reader_hostgroup!=r) { - reader_hostgroup=r; - ret=true; - } - if (offline_hostgroup!=o) { - offline_hostgroup=o; - ret=true; - } - if (max_writers!=mw) { - max_writers=mw; - ret=true; - } - if (max_transactions_behind!=mtb) { - max_transactions_behind=mtb; - ret=true; - } - if (active!=_a) { - active=_a; - ret=true; - } - if (writer_is_also_reader!=_w) { - writer_is_also_reader=_w; - ret=true; - } - // for comment we don't change return value - if (comment) { - if (c) { - if (strcmp(comment,c)) { - free(comment); - comment=strdup(c); - } - } else { - free(comment); - comment=NULL; - } - } else { - if (c) { - comment=strdup(c); - } - } - return ret; -} - -void PgSQL_HostGroups_Manager::p_update_pgsql_gtid_executed() { - pthread_rwlock_wrlock(>id_rwlock); - - std::unordered_map::iterator it = gtid_map.begin(); - while(it != gtid_map.end()) { - PgSQL_GTID_Server_Data* gtid_si = it->second; - std::string address {}; - std::string port {}; - std::string endpoint_id {}; - - if (gtid_si) { - address = std::string(gtid_si->address); - port = std::to_string(gtid_si->pgsql_port); - } else { - std::string s = it->first; - std::size_t found = s.find_last_of(":"); - address = s.substr(0, found); - port = s.substr(found + 1); - } - endpoint_id = address + ":" + port; - - const auto& gitd_id_counter = this->status.p_gtid_executed_map.find(endpoint_id); - prometheus::Counter* gtid_counter = nullptr; - - if (gitd_id_counter == this->status.p_gtid_executed_map.end()) { - auto& gitd_counter = - this->status.p_dyn_counter_array[PgSQL_p_hg_dyn_counter::gtid_executed]; - - gtid_counter = std::addressof(gitd_counter->Add({ - { "hostname", address }, - { "port", port }, - })); - - this->status.p_gtid_executed_map.insert( - { - endpoint_id, - gtid_counter - } - ); - } else { - gtid_counter = gitd_id_counter->second; - } - - if (gtid_si) { - const auto& cur_executed_gtid = gtid_counter->Value(); - gtid_counter->Increment(gtid_si->events_read - cur_executed_gtid); - } - - it++; - } - - pthread_rwlock_unlock(>id_rwlock); -} - -SQLite3_result * PgSQL_HostGroups_Manager::get_stats_pgsql_gtid_executed() { - const int colnum = 4; - SQLite3_result * result = new SQLite3_result(colnum); - result->add_column_definition(SQLITE_TEXT,"hostname"); - result->add_column_definition(SQLITE_TEXT,"port"); - result->add_column_definition(SQLITE_TEXT,"gtid_executed"); - result->add_column_definition(SQLITE_TEXT,"events"); - int k; - pthread_rwlock_wrlock(>id_rwlock); - std::unordered_map::iterator it = gtid_map.begin(); - while(it != gtid_map.end()) { - PgSQL_GTID_Server_Data * gtid_si = it->second; - char buf[64]; - char **pta=(char **)malloc(sizeof(char *)*colnum); - if (gtid_si) { - pta[0]=strdup(gtid_si->address); - sprintf(buf,"%d", (int)gtid_si->pgsql_port); - pta[1]=strdup(buf); - //sprintf(buf,"%d", mysrvc->port); - string s1 = gtid_executed_to_string(gtid_si->gtid_executed); - pta[2]=strdup(s1.c_str()); - sprintf(buf,"%llu", gtid_si->events_read); - pta[3]=strdup(buf); - } else { - std::string s = it->first; - std::size_t found=s.find_last_of(":"); - std::string host=s.substr(0,found); - std::string port=s.substr(found+1); - pta[0]=strdup(host.c_str()); - pta[1]=strdup(port.c_str()); - pta[2]=strdup((char *)"NULL"); - pta[3]=strdup((char *)"0"); - } - result->add_row(pta); - for (k=0; k