Skip to content

Commit 85709f2

Browse files
authored
Merge pull request #1858 from sysown/v2.0.0-merge-tb
V2.0.0 merge tb
2 parents 40bf4f6 + 0a60cfd commit 85709f2

10 files changed

+413
-93
lines changed

include/MySQL_HostGroups_Manager.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ class MySQL_HostGroups_Manager {
506506
unsigned long long frontend_init_db;
507507
unsigned long long frontend_set_names;
508508
unsigned long long frontend_use_db;
509+
unsigned long long access_denied_wrong_password;
510+
unsigned long long access_denied_max_connections;
511+
unsigned long long access_denied_max_user_connections;
509512
} status;
510513
wqueue<MySQL_Connection *> queue;
511514
MySQL_HostGroups_Manager();
@@ -564,8 +567,8 @@ class MySQL_HostGroups_Manager {
564567
SQLite3_result *SQL3_Get_ConnPool_Stats();
565568
void increase_reset_counter();
566569

567-
void add_mysql_errors(int hostgroup, char *hostname, int port, char *username, char *schemaname, int err_no, char *last_error);
568-
570+
void add_mysql_errors(int hostgroup, char *hostname, int port, char *username, char *address, char *schemaname, int err_no, char *last_error);
571+
SQLite3_result *get_mysql_errors(bool);
569572
};
570573

571574
#endif /* __CLASS_MYSQL_HOSTGROUPS_MANAGER_H */

include/MySQL_Thread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ class MySQL_Threads_Handler
383383
bool query_digests;
384384
bool query_digests_lowercase;
385385
bool query_digests_normalize_digest_text;
386+
bool query_digests_track_hostname;
386387
bool default_reconnect;
387388
bool have_compress;
388389
bool have_ssl;

include/proxysql_structs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ __thread bool mysql_thread___commands_stats;
644644
__thread bool mysql_thread___query_digests;
645645
__thread bool mysql_thread___query_digests_lowercase;
646646
__thread bool mysql_thread___query_digests_normalize_digest_text;
647+
__thread bool mysql_thread___query_digests_track_hostname;
647648
__thread int mysql_thread___query_digests_max_digest_length;
648649
__thread int mysql_thread___query_digests_max_query_length;
649650
__thread bool mysql_thread___default_reconnect;
@@ -764,6 +765,7 @@ extern __thread bool mysql_thread___commands_stats;
764765
extern __thread bool mysql_thread___query_digests;
765766
extern __thread bool mysql_thread___query_digests_lowercase;
766767
extern __thread bool mysql_thread___query_digests_normalize_digest_text;
768+
extern __thread bool mysql_thread___query_digests_track_hostname;
767769
extern __thread int mysql_thread___query_digests_max_digest_length;
768770
extern __thread int mysql_thread___query_digests_max_query_length;
769771
extern __thread bool mysql_thread___default_reconnect;

include/query_processor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class Query_Processor {
230230
char * get_digest_text(SQP_par_t *qp);
231231
uint64_t get_digest(SQP_par_t *qp);
232232

233-
void update_query_digest(SQP_par_t *qp, int hid, MySQL_Connection_userinfo *ui, unsigned long long t, unsigned long long n, MySQL_STMT_Global_info *_stmt_info);
233+
void update_query_digest(SQP_par_t *qp, int hid, MySQL_Connection_userinfo *ui, unsigned long long t, unsigned long long n, MySQL_STMT_Global_info *_stmt_info, MySQL_Session *sess);
234234

235235
unsigned long long query_parser_update_counters(MySQL_Session *sess, enum MYSQL_COM_QUERY_command c, SQP_par_t *qp, unsigned long long t);
236236

lib/MySQL_HostGroups_Manager.cpp

Lines changed: 232 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,108 @@ class MySrvC;
4040
class MySrvList;
4141
class MyHGC;
4242

43+
/*
44+
class HGM_query_errors_stats {
45+
public:
46+
int hid;
47+
char *hostname;
48+
int port;
49+
char *username;
50+
char *schemaname;
51+
int error_no;
52+
unsigned int count_star;
53+
time_t first_seen;
54+
time_t last_seen;
55+
char *last_error;
56+
HGM_query_errors_stats(int _h, char *_hn, int _p, char *u, char *s, int e, char *le) {
57+
hid=_h;
58+
hostname=strdup(_hn);
59+
port=_p;
60+
username=strdup(u);
61+
schemaname=strdup(s);
62+
error_no=e;
63+
last_error=strdup(le);
64+
count_star=0;
65+
first_seen=0;
66+
last_seen=0;
67+
}
68+
void add_time(unsigned long long n, char *le) {
69+
count_star++;
70+
if (first_seen==0) {
71+
first_seen=n;
72+
}
73+
last_seen=n;
74+
if (strcmp(last_error,le)){
75+
free(last_error);
76+
last_error=strdup(le);
77+
}
78+
}
79+
~HGM_query_errors_stats() {
80+
if (hostname) {
81+
free(hostname);
82+
hostname=NULL;
83+
}
84+
if (username) {
85+
free(username);
86+
username=NULL;
87+
}
88+
if (schemaname) {
89+
free(schemaname);
90+
schemaname=NULL;
91+
}
92+
if (last_error) {
93+
free(last_error);
94+
last_error=NULL;
95+
}
96+
}
97+
char **get_row() {
98+
char buf[128];
99+
char **pta=(char **)malloc(sizeof(char *)*10);
100+
sprintf(buf,"%d",hid);
101+
pta[0]=strdup(buf);
102+
assert(hostname);
103+
pta[1]=strdup(hostname);
104+
sprintf(buf,"%d",port);
105+
pta[2]=strdup(buf);
106+
assert(username);
107+
pta[3]=strdup(username);
108+
assert(schemaname);
109+
pta[4]=strdup(schemaname);
110+
sprintf(buf,"%d",error_no);
111+
pta[5]=strdup(buf);
112+
113+
sprintf(buf,"%u",count_star);
114+
pta[6]=strdup(buf);
115+
116+
time_t __now;
117+
time(&__now);
118+
unsigned long long curtime=monotonic_time();
119+
time_t seen_time;
120+
121+
seen_time= __now - curtime/1000000 + first_seen/1000000;
122+
sprintf(buf,"%ld", seen_time);
123+
pta[7]=strdup(buf);
124+
125+
seen_time= __now - curtime/1000000 + last_seen/1000000;
126+
sprintf(buf,"%ld", seen_time);
127+
pta[8]=strdup(buf);
128+
129+
assert(last_error);
130+
pta[9]=strdup(last_error);
131+
return pta;
132+
}
133+
void free_row(char **pta) {
134+
int i;
135+
for (i=0;i<10;i++) {
136+
assert(pta[i]);
137+
free(pta[i]);
138+
}
139+
free(pta);
140+
}
141+
};
142+
143+
*/
144+
43145
//static struct ev_async * gtid_ev_async;
44146

45147
static pthread_mutex_t ev_loop_mutex;
@@ -842,6 +944,9 @@ MySQL_HostGroups_Manager::MySQL_HostGroups_Manager() {
842944
status.frontend_init_db=0;
843945
status.frontend_set_names=0;
844946
status.frontend_use_db=0;
947+
status.access_denied_wrong_password=0;
948+
status.access_denied_max_connections=0;
949+
status.access_denied_max_user_connections=0;
845950
pthread_mutex_init(&readonly_mutex, NULL);
846951
pthread_mutex_init(&Group_Replication_Info_mutex, NULL);
847952
pthread_mutex_init(&Galera_Info_mutex, NULL);
@@ -2392,7 +2497,7 @@ void MySQL_HostGroups_Manager::replication_lag_action(int _hid, char *address, u
23922497
// ||
23932498
(current_replication_lag>=0 && ((unsigned int)current_replication_lag > mysrvc->max_replication_lag))
23942499
) {
2395-
proxy_warning("Shunning server %s:%d with replication lag of %d second\n", address, port, current_replication_lag);
2500+
proxy_warning("Shunning server %s:%d from HG %u with replication lag of %d second\n", address, port, myhgc->hid, current_replication_lag);
23962501
mysrvc->status=MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG;
23972502
}
23982503
} else {
@@ -2403,7 +2508,7 @@ void MySQL_HostGroups_Manager::replication_lag_action(int _hid, char *address, u
24032508
(current_replication_lag==-2) // see issue 959
24042509
) {
24052510
mysrvc->status=MYSQL_SERVER_STATUS_ONLINE;
2406-
proxy_warning("Re-enabling server %s:%d with replication lag of %d second\n", address, port, current_replication_lag);
2511+
proxy_warning("Re-enabling server %s:%d from HG %u with replication lag of %d second\n", address, port, myhgc->hid, current_replication_lag);
24072512
}
24082513
}
24092514
}
@@ -4371,13 +4476,14 @@ class MySQL_Errors_stats {
43714476
char *hostname;
43724477
int port;
43734478
char *username;
4479+
char *client_address;
43744480
char *schemaname;
43754481
int err_no;
43764482
char *last_error;
43774483
time_t first_seen;
43784484
time_t last_seen;
43794485
unsigned long long count_star;
4380-
MySQL_Errors_stats(int hostgroup_, char *hostname_, int port_, char *username_, char *schemaname_, int err_no_, char *last_error_, time_t tn) {
4486+
MySQL_Errors_stats(int hostgroup_, char *hostname_, int port_, char *username_, char *address_, char *schemaname_, int err_no_, char *last_error_, time_t tn) {
43814487
hostgroup = hostgroup_;
43824488
if (hostname_) {
43834489
hostname = strdup(hostname_);
@@ -4390,6 +4496,11 @@ class MySQL_Errors_stats {
43904496
} else {
43914497
username = strdup((char *)"");
43924498
}
4499+
if (address_) {
4500+
client_address = strdup(address_);
4501+
} else {
4502+
client_address = strdup((char *)"");
4503+
}
43934504
if (schemaname_) {
43944505
schemaname = strdup(schemaname_);
43954506
} else {
@@ -4405,9 +4516,88 @@ class MySQL_Errors_stats {
44054516
first_seen = tn;
44064517
count_star = 1;
44074518
}
4519+
~MySQL_Errors_stats() {
4520+
if (hostname) {
4521+
free(hostname);
4522+
hostname=NULL;
4523+
}
4524+
if (username) {
4525+
free(username);
4526+
username=NULL;
4527+
}
4528+
if (client_address) {
4529+
free(client_address);
4530+
client_address=NULL;
4531+
}
4532+
if (schemaname) {
4533+
free(schemaname);
4534+
schemaname=NULL;
4535+
}
4536+
if (last_error) {
4537+
free(last_error);
4538+
last_error=NULL;
4539+
}
4540+
}
4541+
char **get_row() {
4542+
char buf[128];
4543+
char **pta=(char **)malloc(sizeof(char *)*11);
4544+
sprintf(buf,"%d",hostgroup);
4545+
pta[0]=strdup(buf);
4546+
assert(hostname);
4547+
pta[1]=strdup(hostname);
4548+
sprintf(buf,"%d",port);
4549+
pta[2]=strdup(buf);
4550+
assert(username);
4551+
pta[3]=strdup(username);
4552+
assert(client_address);
4553+
pta[4]=strdup(client_address);
4554+
assert(schemaname);
4555+
pta[5]=strdup(schemaname);
4556+
sprintf(buf,"%d",err_no);
4557+
pta[6]=strdup(buf);
4558+
4559+
sprintf(buf,"%llu",count_star);
4560+
pta[7]=strdup(buf);
4561+
4562+
time_t __now;
4563+
time(&__now);
4564+
unsigned long long curtime=monotonic_time();
4565+
time_t seen_time;
4566+
4567+
seen_time= __now - curtime/1000000 + first_seen/1000000;
4568+
sprintf(buf,"%ld", seen_time);
4569+
pta[8]=strdup(buf);
4570+
4571+
seen_time= __now - curtime/1000000 + last_seen/1000000;
4572+
sprintf(buf,"%ld", seen_time);
4573+
pta[9]=strdup(buf);
4574+
4575+
assert(last_error);
4576+
pta[10]=strdup(last_error);
4577+
return pta;
4578+
}
4579+
void add_time(unsigned long long n, char *le) {
4580+
count_star++;
4581+
if (first_seen==0) {
4582+
first_seen=n;
4583+
}
4584+
last_seen=n;
4585+
if (strcmp(last_error,le)){
4586+
free(last_error);
4587+
last_error=strdup(le);
4588+
}
4589+
}
4590+
void free_row(char **pta) {
4591+
int i;
4592+
for (i=0;i<11;i++) {
4593+
assert(pta[i]);
4594+
free(pta[i]);
4595+
}
4596+
free(pta);
4597+
}
44084598
};
44094599

4410-
void MySQL_HostGroups_Manager::add_mysql_errors(int hostgroup, char *hostname, int port, char *username, char *schemaname, int err_no, char *last_error) {
4600+
void MySQL_HostGroups_Manager::add_mysql_errors(int hostgroup, char *hostname, int port, char *username, char *address, char *schemaname, int err_no, char *last_error) {
44114601
SpookyHash myhash;
44124602
uint64_t hash1;
44134603
uint64_t hash2;
@@ -4426,6 +4616,10 @@ void MySQL_HostGroups_Manager::add_mysql_errors(int hostgroup, char *hostname, i
44264616
myhash.Update(username,strlen(username));
44274617
}
44284618
myhash.Update(rand_del,rand_del_len);
4619+
if (address) {
4620+
myhash.Update(address,strlen(address));
4621+
}
4622+
myhash.Update(rand_del,rand_del_len);
44294623
if (schemaname) {
44304624
myhash.Update(schemaname,strlen(schemaname));
44314625
}
@@ -4442,15 +4636,48 @@ void MySQL_HostGroups_Manager::add_mysql_errors(int hostgroup, char *hostname, i
44424636
if (it != mysql_errors_umap.end()) {
44434637
// found
44444638
mes=(MySQL_Errors_stats *)it->second;
4639+
mes->add_time(tn, last_error);
4640+
/*
44454641
mes->last_seen = tn;
44464642
if (strcmp(mes->last_error,last_error)) {
44474643
free(mes->last_error);
44484644
mes->last_error = strdup(last_error);
44494645
mes->count_star++;
44504646
}
4647+
*/
44514648
} else {
4452-
mes = new MySQL_Errors_stats(hostgroup, hostname, port, username, schemaname, err_no, last_error, tn);
4649+
mes = new MySQL_Errors_stats(hostgroup, hostname, port, username, address, schemaname, err_no, last_error, tn);
44534650
mysql_errors_umap.insert(std::make_pair(hash1,(void *)mes));
44544651
}
44554652
pthread_mutex_unlock(&mysql_errors_mutex);
44564653
}
4654+
4655+
SQLite3_result * MySQL_HostGroups_Manager::get_mysql_errors(bool reset) {
4656+
SQLite3_result *result=new SQLite3_result(11);
4657+
pthread_mutex_lock(&mysql_errors_mutex);
4658+
result->add_column_definition(SQLITE_TEXT,"hid");
4659+
result->add_column_definition(SQLITE_TEXT,"hostname");
4660+
result->add_column_definition(SQLITE_TEXT,"port");
4661+
result->add_column_definition(SQLITE_TEXT,"username");
4662+
result->add_column_definition(SQLITE_TEXT,"client_address");
4663+
result->add_column_definition(SQLITE_TEXT,"schemaname");
4664+
result->add_column_definition(SQLITE_TEXT,"err_no");
4665+
result->add_column_definition(SQLITE_TEXT,"count_star");
4666+
result->add_column_definition(SQLITE_TEXT,"first_seen");
4667+
result->add_column_definition(SQLITE_TEXT,"last_seen");
4668+
result->add_column_definition(SQLITE_TEXT,"last_error");
4669+
for (std::unordered_map<uint64_t, void *>::iterator it=mysql_errors_umap.begin(); it!=mysql_errors_umap.end(); ++it) {
4670+
MySQL_Errors_stats *mes=(MySQL_Errors_stats *)it->second;
4671+
char **pta=mes->get_row();
4672+
result->add_row(pta);
4673+
mes->free_row(pta);
4674+
if (reset) {
4675+
delete mes;
4676+
}
4677+
}
4678+
if (reset) {
4679+
mysql_errors_umap.erase(mysql_errors_umap.begin(),mysql_errors_umap.end());
4680+
}
4681+
pthread_mutex_unlock(&mysql_errors_mutex);
4682+
return result;
4683+
}

0 commit comments

Comments
 (0)