Skip to content

Commit 3182938

Browse files
committed
move userstat tables to a plugin
1 parent db8af31 commit 3182938

16 files changed

+413
-388
lines changed

mysql-test/r/information_schema.result

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,18 @@ TABLE_CONSTRAINTS TABLE_CONSTRAINTS
129129
TABLE_PRIVILEGES TABLE_PRIVILEGES
130130
TABLE_STATISTICS TABLE_STATISTICS
131131
TRIGGERS TRIGGERS
132+
t1 t1
133+
t2 t2
134+
t3 t3
135+
t4 t4
136+
t5 t5
132137
table_stats table_stats
133138
tables_priv tables_priv
134139
time_zone time_zone
135140
time_zone_leap_second time_zone_leap_second
136141
time_zone_name time_zone_name
137142
time_zone_transition time_zone_transition
138143
time_zone_transition_type time_zone_transition_type
139-
t1 t1
140-
t4 t4
141-
t2 t2
142-
t3 t3
143-
t5 t5
144144
select c,table_name from v1
145145
left join information_schema.TABLES v2 on (v1.c=v2.table_name)
146146
where v1.c like "t%";
@@ -151,18 +151,18 @@ TABLE_CONSTRAINTS TABLE_CONSTRAINTS
151151
TABLE_PRIVILEGES TABLE_PRIVILEGES
152152
TABLE_STATISTICS TABLE_STATISTICS
153153
TRIGGERS TRIGGERS
154+
t1 t1
155+
t2 t2
156+
t3 t3
157+
t4 t4
158+
t5 t5
154159
table_stats table_stats
155160
tables_priv tables_priv
156161
time_zone time_zone
157162
time_zone_leap_second time_zone_leap_second
158163
time_zone_name time_zone_name
159164
time_zone_transition time_zone_transition
160165
time_zone_transition_type time_zone_transition_type
161-
t1 t1
162-
t4 t4
163-
t2 t2
164-
t3 t3
165-
t5 t5
166166
select c, v2.table_name from v1
167167
right join information_schema.TABLES v2 on (v1.c=v2.table_name)
168168
where v1.c like "t%";
@@ -173,18 +173,18 @@ TABLE_CONSTRAINTS TABLE_CONSTRAINTS
173173
TABLE_PRIVILEGES TABLE_PRIVILEGES
174174
TABLE_STATISTICS TABLE_STATISTICS
175175
TRIGGERS TRIGGERS
176+
t1 t1
177+
t2 t2
178+
t3 t3
179+
t4 t4
180+
t5 t5
176181
table_stats table_stats
177182
tables_priv tables_priv
178183
time_zone time_zone
179184
time_zone_leap_second time_zone_leap_second
180185
time_zone_name time_zone_name
181186
time_zone_transition time_zone_transition
182187
time_zone_transition_type time_zone_transition_type
183-
t1 t1
184-
t4 t4
185-
t2 t2
186-
t3 t3
187-
t5 t5
188188
select table_name from information_schema.TABLES
189189
where table_schema = "mysqltest" and table_name like "t%";
190190
table_name

mysql-test/t/information_schema.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,17 @@ create view v1 (c) as
5555
--sorted_result
5656
select * from v1;
5757

58+
--sorted_result
5859
select c,table_name from v1
5960
inner join information_schema.TABLES v2 on (v1.c=v2.table_name)
6061
where v1.c like "t%";
6162

63+
--sorted_result
6264
select c,table_name from v1
6365
left join information_schema.TABLES v2 on (v1.c=v2.table_name)
6466
where v1.c like "t%";
6567

68+
--sorted_result
6669
select c, v2.table_name from v1
6770
right join information_schema.TABLES v2 on (v1.c=v2.table_name)
6871
where v1.c like "t%";
@@ -368,17 +371,20 @@ drop view vo;
368371
select TABLE_NAME,TABLE_TYPE,ENGINE
369372
from information_schema.tables
370373
where table_schema='information_schema' limit 2;
374+
--sorted_result
371375
show tables from information_schema like "T%";
372376

373377
--error ER_DBACCESS_DENIED_ERROR
374378
create database information_schema;
375379
use information_schema;
380+
--sorted_result
376381
show full tables like "T%";
377382
--error ER_DBACCESS_DENIED_ERROR
378383
create table t1(a int);
379384
use test;
380385
show tables;
381386
use information_schema;
387+
--sorted_result
382388
show tables like "T%";
383389

384390
#

mysql-test/t/information_schema_db.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ drop function if exists f2;
1515
--enable_warnings
1616

1717
--replace_result 'Tables_in_INFORMATION_SCHEMA (T%)' 'Tables_in_information_schema (T%)'
18+
--sorted_result
1819
show tables from INFORMATION_SCHEMA like 'T%';
1920
create database `inf%`;
2021
create database mbase;

plugin/userstat/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/sql)
2+
3+
MYSQL_ADD_PLUGIN(USERSTAT userstat.cc MANDATORY)
4+

plugin/userstat/client_stats.cc

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
static ST_FIELD_INFO client_stats_fields[]=
2+
{
3+
{"CLIENT", LIST_PROCESS_HOST_LEN, MYSQL_TYPE_STRING, 0, 0, "Client", 0},
4+
{"TOTAL_CONNECTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Total_connections", 0},
5+
{"CONCURRENT_CONNECTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Concurrent_connections", 0},
6+
{"CONNECTED_TIME", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Connected_time", 0},
7+
{"BUSY_TIME", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_DOUBLE, 0, 0, "Busy_time", 0},
8+
{"CPU_TIME", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_DOUBLE, 0, 0, "Cpu_time", 0},
9+
{"BYTES_RECEIVED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Bytes_received", 0},
10+
{"BYTES_SENT", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Bytes_sent", 0},
11+
{"BINLOG_BYTES_WRITTEN", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Binlog_bytes_written", 0},
12+
{"ROWS_READ", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_read", 0},
13+
{"ROWS_SENT", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_sent", 0},
14+
{"ROWS_DELETED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_deleted", 0},
15+
{"ROWS_INSERTED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_inserted", 0},
16+
{"ROWS_UPDATED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_updated", 0},
17+
{"SELECT_COMMANDS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Select_commands", 0},
18+
{"UPDATE_COMMANDS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Update_commands", 0},
19+
{"OTHER_COMMANDS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Other_commands", 0},
20+
{"COMMIT_TRANSACTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Commit_transactions", 0},
21+
{"ROLLBACK_TRANSACTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rollback_transactions", 0},
22+
{"DENIED_CONNECTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Denied_connections", 0},
23+
{"LOST_CONNECTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Lost_connections", 0},
24+
{"ACCESS_DENIED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Access_denied", 0},
25+
{"EMPTY_QUERIES", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Empty_queries", 0},
26+
{"MAX_STATEMENT_TIME_EXCEEDED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Max_statement_time_exceeded",SKIP_OPEN_TABLE},
27+
{0, 0, MYSQL_TYPE_STRING, 0, 0, 0, 0}
28+
};
29+
30+
static int send_user_stats(THD* thd, HASH *all_user_stats, TABLE *table)
31+
{
32+
mysql_mutex_lock(&LOCK_global_user_client_stats);
33+
for (uint i= 0; i < all_user_stats->records; i++)
34+
{
35+
uint j= 0;
36+
USER_STATS *user_stats= (USER_STATS*) my_hash_element(all_user_stats, i);
37+
38+
table->field[j++]->store(user_stats->user, user_stats->user_name_length,
39+
system_charset_info);
40+
table->field[j++]->store((longlong)user_stats->total_connections,TRUE);
41+
table->field[j++]->store((longlong)user_stats->concurrent_connections, TRUE);
42+
table->field[j++]->store((longlong)user_stats->connected_time, TRUE);
43+
table->field[j++]->store((double)user_stats->busy_time);
44+
table->field[j++]->store((double)user_stats->cpu_time);
45+
table->field[j++]->store((longlong)user_stats->bytes_received, TRUE);
46+
table->field[j++]->store((longlong)user_stats->bytes_sent, TRUE);
47+
table->field[j++]->store((longlong)user_stats->binlog_bytes_written, TRUE);
48+
table->field[j++]->store((longlong)user_stats->rows_read, TRUE);
49+
table->field[j++]->store((longlong)user_stats->rows_sent, TRUE);
50+
table->field[j++]->store((longlong)user_stats->rows_deleted, TRUE);
51+
table->field[j++]->store((longlong)user_stats->rows_inserted, TRUE);
52+
table->field[j++]->store((longlong)user_stats->rows_updated, TRUE);
53+
table->field[j++]->store((longlong)user_stats->select_commands, TRUE);
54+
table->field[j++]->store((longlong)user_stats->update_commands, TRUE);
55+
table->field[j++]->store((longlong)user_stats->other_commands, TRUE);
56+
table->field[j++]->store((longlong)user_stats->commit_trans, TRUE);
57+
table->field[j++]->store((longlong)user_stats->rollback_trans, TRUE);
58+
table->field[j++]->store((longlong)user_stats->denied_connections, TRUE);
59+
table->field[j++]->store((longlong)user_stats->lost_connections, TRUE);
60+
table->field[j++]->store((longlong)user_stats->access_denied_errors, TRUE);
61+
table->field[j++]->store((longlong)user_stats->empty_queries, TRUE);
62+
table->field[j++]->store((longlong)user_stats->max_statement_time_exceeded, TRUE);
63+
if (schema_table_store_record(thd, table))
64+
{
65+
mysql_mutex_unlock(&LOCK_global_user_client_stats);
66+
return 1;
67+
}
68+
}
69+
mysql_mutex_unlock(&LOCK_global_user_client_stats);
70+
return 0;
71+
}
72+
73+
static int client_stats_fill(THD* thd, TABLE_LIST* tables, COND* cond)
74+
{
75+
if (check_global_access(thd, SUPER_ACL | PROCESS_ACL, true))
76+
return 0;
77+
78+
return send_user_stats(thd, &global_client_stats, tables->table);
79+
}
80+
81+
static int client_stats_reset()
82+
{
83+
mysql_mutex_lock(&LOCK_global_user_client_stats);
84+
free_global_client_stats();
85+
init_global_client_stats();
86+
mysql_mutex_unlock(&LOCK_global_user_client_stats);
87+
return 0;
88+
}
89+
90+
static int client_stats_init(void *p)
91+
{
92+
ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p;
93+
schema->fields_info= client_stats_fields;
94+
schema->fill_table= client_stats_fill;
95+
schema->reset_table= client_stats_reset;
96+
return 0;
97+
}

plugin/userstat/index_stats.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
static ST_FIELD_INFO index_stats_fields[]=
2+
{
3+
{"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table_schema",SKIP_OPEN_TABLE},
4+
{"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table_name",SKIP_OPEN_TABLE},
5+
{"INDEX_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Index_name",SKIP_OPEN_TABLE},
6+
{"ROWS_READ", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_read",SKIP_OPEN_TABLE},
7+
{0, 0, MYSQL_TYPE_STRING, 0, 0, 0,0}
8+
};
9+
10+
static int index_stats_fill(THD *thd, TABLE_LIST *tables, COND *cond)
11+
{
12+
TABLE *table= tables->table;
13+
14+
mysql_mutex_lock(&LOCK_global_index_stats);
15+
for (uint i= 0; i < global_index_stats.records; i++)
16+
{
17+
INDEX_STATS *index_stats =
18+
(INDEX_STATS*) my_hash_element(&global_index_stats, i);
19+
TABLE_LIST tmp_table;
20+
char *index_name;
21+
size_t schema_name_length, table_name_length, index_name_length;
22+
23+
bzero((char*) &tmp_table,sizeof(tmp_table));
24+
tmp_table.db= index_stats->index;
25+
tmp_table.table_name= strend(index_stats->index)+1;
26+
tmp_table.grant.privilege= 0;
27+
if (check_access(thd, SELECT_ACL, tmp_table.db,
28+
&tmp_table.grant.privilege, NULL, 0, 1) ||
29+
check_grant(thd, SELECT_ACL, &tmp_table, 1, UINT_MAX, 1))
30+
continue;
31+
32+
index_name= strend(tmp_table.table_name)+1;
33+
schema_name_length= (tmp_table.table_name - index_stats->index) -1;
34+
table_name_length= (index_name - tmp_table.table_name)-1;
35+
index_name_length= (index_stats->index_name_length - schema_name_length -
36+
table_name_length - 3);
37+
38+
table->field[0]->store(tmp_table.db, schema_name_length,
39+
system_charset_info);
40+
table->field[1]->store(tmp_table.table_name, table_name_length,
41+
system_charset_info);
42+
table->field[2]->store(index_name, index_name_length, system_charset_info);
43+
table->field[3]->store((longlong)index_stats->rows_read, TRUE);
44+
45+
if (schema_table_store_record(thd, table))
46+
{
47+
mysql_mutex_unlock(&LOCK_global_index_stats);
48+
return 1;
49+
}
50+
}
51+
mysql_mutex_unlock(&LOCK_global_index_stats);
52+
return 0;
53+
}
54+
55+
static int index_stats_reset()
56+
{
57+
mysql_mutex_lock(&LOCK_global_index_stats);
58+
free_global_index_stats();
59+
init_global_index_stats();
60+
mysql_mutex_unlock(&LOCK_global_index_stats);
61+
return 0;
62+
}
63+
64+
static int index_stats_init(void *p)
65+
{
66+
ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p;
67+
schema->fields_info= index_stats_fields;
68+
schema->fill_table= index_stats_fill;
69+
schema->reset_table= index_stats_reset;
70+
return 0;
71+
}
72+

plugin/userstat/table_stats.cc

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
static ST_FIELD_INFO table_stats_fields[]=
2+
{
3+
{"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table_schema",SKIP_OPEN_TABLE},
4+
{"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table_name",SKIP_OPEN_TABLE},
5+
{"ROWS_READ", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_read",SKIP_OPEN_TABLE},
6+
{"ROWS_CHANGED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_changed",SKIP_OPEN_TABLE},
7+
{"ROWS_CHANGED_X_INDEXES", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_changed_x_#indexes",SKIP_OPEN_TABLE},
8+
{0, 0, MYSQL_TYPE_STRING, 0, 0, 0, 0}
9+
};
10+
11+
static int table_stats_fill(THD *thd, TABLE_LIST *tables, COND *cond)
12+
{
13+
TABLE *table= tables->table;
14+
15+
mysql_mutex_lock(&LOCK_global_table_stats);
16+
for (uint i= 0; i < global_table_stats.records; i++)
17+
{
18+
char *end_of_schema;
19+
TABLE_STATS *table_stats=
20+
(TABLE_STATS*)my_hash_element(&global_table_stats, i);
21+
TABLE_LIST tmp_table;
22+
size_t schema_length, table_name_length;
23+
24+
end_of_schema= strend(table_stats->table);
25+
schema_length= (size_t) (end_of_schema - table_stats->table);
26+
table_name_length= strlen(table_stats->table + schema_length + 1);
27+
28+
bzero((char*) &tmp_table,sizeof(tmp_table));
29+
tmp_table.db= table_stats->table;
30+
tmp_table.table_name= end_of_schema+1;
31+
tmp_table.grant.privilege= 0;
32+
if (check_access(thd, SELECT_ACL, tmp_table.db,
33+
&tmp_table.grant.privilege, NULL, 0, 1) ||
34+
check_grant(thd, SELECT_ACL, &tmp_table, 1, UINT_MAX,
35+
1))
36+
continue;
37+
38+
table->field[0]->store(table_stats->table, schema_length,
39+
system_charset_info);
40+
table->field[1]->store(table_stats->table + schema_length+1,
41+
table_name_length, system_charset_info);
42+
table->field[2]->store((longlong)table_stats->rows_read, TRUE);
43+
table->field[3]->store((longlong)table_stats->rows_changed, TRUE);
44+
table->field[4]->store((longlong)table_stats->rows_changed_x_indexes,
45+
TRUE);
46+
if (schema_table_store_record(thd, table))
47+
{
48+
mysql_mutex_unlock(&LOCK_global_table_stats);
49+
return 1;
50+
}
51+
}
52+
mysql_mutex_unlock(&LOCK_global_table_stats);
53+
return 0;
54+
}
55+
56+
static int table_stats_reset()
57+
{
58+
mysql_mutex_lock(&LOCK_global_table_stats);
59+
free_global_table_stats();
60+
init_global_table_stats();
61+
mysql_mutex_unlock(&LOCK_global_table_stats);
62+
return 0;
63+
}
64+
65+
static int table_stats_init(void *p)
66+
{
67+
ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p;
68+
schema->fields_info= table_stats_fields;
69+
schema->fill_table= table_stats_fill;
70+
schema->reset_table= table_stats_reset;
71+
return 0;
72+
}
73+

0 commit comments

Comments
 (0)