Skip to content

Commit a7e72f6

Browse files
Satya Valluriinikep
Satya Valluri
authored andcommitted
Add server_cpu and warnings to query response attributes
Summary: This diff adds server CPU time to query response attributes. The key of the attribute is "server_cpu" and the value is the stringified version of the cpu time. This diff also adds warnings information to query response attributes. The key of the attribute is "warnings" and the value is the string that serializes the warnings information. Since there could be more than one warnings for a statement, the value is a list of pairs included with in brackets separated by commas. The first value of the pair is the error number (code) and the second value of the pair is the message text and these are separated by a comma. The following example shows the query response attribute for 'warnings' where two warnings are raised for the statement: **1265,Data truncated for column 'c1' at row 1),(1264,Out of range value for column 'c2' at row 1)** The variable 'response_attrs_contain_warnings' controls this feature and is disabled by default. If the query raises any errors then the response OK packet is not set to the client. So in this case putting warnings into response attributes is skipped. Reviewed By: george-reynya Differential Revision: D26598990
1 parent 467a581 commit a7e72f6

20 files changed

+1099
-0
lines changed

Diff for: include/mysql/components/services/psi_statement_bits.h

+8
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,14 @@ typedef struct PSI_digest_locker *(*digest_start_v1_t)(
586586
typedef void (*digest_end_v1_t)(struct PSI_digest_locker *locker,
587587
const struct sql_digest_storage *digest);
588588

589+
/**
590+
Set a statement event cpu time.
591+
@param locker the statement locker
592+
@param cpu_time the cpu time, in microseconds
593+
*/
594+
typedef unsigned long long (*get_statement_cpu_time_v1_t)(
595+
struct PSI_statement_locker *locker);
596+
589597
/**
590598
Acquire a sp share instrumentation.
591599
@param object_type type of stored program

Diff for: include/mysql/components/services/psi_statement_service.h

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ digest_start_v1_t digest_start;
117117
/** @sa digest_end_v1_t. */
118118
digest_end_v1_t digest_end;
119119

120+
/** @sa get_statement_cpu_time_v1_t. */
121+
get_statement_cpu_time_v1_t get_statement_cpu_time;
120122
/** @sa get_sp_share_v1_t. */
121123
get_sp_share_v1_t get_sp_share;
122124
/** @sa release_sp_share_v1_t. */
@@ -218,6 +220,8 @@ digest_start_v1_t digest_start;
218220
/** @sa digest_end_v1_t. */
219221
digest_end_v1_t digest_end;
220222

223+
/** @sa get_statement_cpu_time_v1_t. */
224+
get_statement_cpu_time_v1_t get_statement_cpu_time;
221225
/** @sa get_sp_share_v1_t. */
222226
get_sp_share_v1_t get_sp_share;
223227
/** @sa release_sp_share_v1_t. */

Diff for: include/mysql/psi/mysql_statement.h

+17
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ struct CHARSET_INFO;
227227
} while (0)
228228
#endif
229229

230+
#ifdef HAVE_PSI_STATEMENT_INTERFACE
231+
#define MYSQL_GET_STATEMENT_CPU_TIME(LOCKER) \
232+
inline_mysql_get_statement_cpu_time(LOCKER)
233+
#else
234+
#define MYSQL_GET_STATEMENT_CPU_TIME(LOCKER) 0
235+
#endif
236+
230237
static inline void inline_mysql_statement_register(
231238
#ifdef HAVE_PSI_STATEMENT_INTERFACE
232239
const char *category, PSI_statement_info *info, int count
@@ -386,6 +393,16 @@ static inline void inline_mysql_end_statement(
386393
}
387394
#endif
388395

396+
static inline unsigned long long inline_mysql_get_statement_cpu_time(
397+
PSI_statement_locker *locker) {
398+
unsigned long long cpu_time = 0;
399+
400+
if (likely(locker != nullptr)) {
401+
cpu_time = PSI_STATEMENT_CALL(get_statement_cpu_time)(locker);
402+
}
403+
return cpu_time;
404+
}
405+
389406
/** @} (end of group psi_api_statement) */
390407

391408
#endif

Diff for: include/mysql/psi/psi_abi_statement_v1.h.pp

+3
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@
190190
struct PSI_statement_locker *locker);
191191
typedef void (*digest_end_v1_t)(struct PSI_digest_locker *locker,
192192
const struct sql_digest_storage *digest);
193+
typedef unsigned long long (*get_statement_cpu_time_v1_t)(
194+
struct PSI_statement_locker *locker);
193195
typedef struct PSI_sp_share *(*get_sp_share_v1_t)(
194196
unsigned int object_type, const char *schema_name,
195197
unsigned int schema_name_length, const char *object_name,
@@ -253,6 +255,7 @@
253255
set_prepared_stmt_text_v1_t set_prepared_stmt_text;
254256
digest_start_v1_t digest_start;
255257
digest_end_v1_t digest_end;
258+
get_statement_cpu_time_v1_t get_statement_cpu_time;
256259
get_sp_share_v1_t get_sp_share;
257260
release_sp_share_v1_t release_sp_share;
258261
start_sp_v1_t start_sp;

Diff for: include/mysql/psi/psi_statement.h

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ struct PSI_statement_service_v2 {
154154
/** @sa digest_end_v1_t. */
155155
digest_end_v1_t digest_end;
156156

157+
/** @sa get_statement_cpu_time_v1_t. */
158+
get_statement_cpu_time_v1_t get_statement_cpu_time;
157159
/** @sa get_sp_share_v1_t. */
158160
get_sp_share_v1_t get_sp_share;
159161
/** @sa release_sp_share_v1_t. */

Diff for: include/pfs_statement_provider.h

+2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ PSI_digest_locker *pfs_digest_start_v2(PSI_statement_locker *locker);
166166
void pfs_digest_end_v2(PSI_digest_locker *locker,
167167
const sql_digest_storage *digest);
168168

169+
unsigned long long pfs_get_statement_cpu_time_v2(PSI_statement_locker *locker);
170+
169171
#endif /* WITH_LOCK_ORDER */
170172
#endif /* MYSQL_DYNAMIC_PLUGIN */
171173
#endif /* MYSQL_SERVER || PFS_DIRECT_CALL */

Diff for: mysql-test/r/mysqld--help-notwin.result

+18
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,22 @@ The following options may be given as the first argument:
16061606
pair - 'hlc_ts' is the key and the value is the
16071607
stringified HLC timestamp. Note that HLC should be
16081608
enabled by setting enable_binlog_hlc
1609+
--response-attrs-contain-server-cpu
1610+
If this is enabled, then the server CPU time of the query
1611+
is sent back to clients as part of OK packet in session
1612+
response attribute. Server CPU time is sent as a
1613+
key-value pair - 'server_cpu' is the key and the value is
1614+
the stringified server CPU time.
1615+
--response-attrs-contain-warnings-bytes[=#]
1616+
Specifies the size of the warnings information (specified
1617+
in bytes) that can be included in the query response
1618+
attributes. The warnings are sent as a key-value pair -
1619+
'warnings' is the key and the value is a list of pairs
1620+
included with in brackets separated by commas. The first
1621+
value of the pair is the error number (code) and the
1622+
second value of the pair is the message text and these
1623+
are separated by a comma. The default value is 0 which
1624+
disables this feature
16091625
--rocksdb[=name] Enable or disable ROCKSDB plugin. Possible values are ON,
16101626
OFF, FORCE (don't start if the plugin fails to load).
16111627
--rocksdb-access-hint-on-compaction-start=#
@@ -3107,6 +3123,8 @@ require-secure-transport FALSE
31073123
reset-period-status-vars FALSE
31083124
reset-seconds-behind-master TRUE
31093125
response-attrs-contain-hlc FALSE
3126+
response-attrs-contain-server-cpu FALSE
3127+
response-attrs-contain-warnings-bytes 0
31103128
rocksdb ON
31113129
rocksdb-access-hint-on-compaction-start 1
31123130
rocksdb-active-compaction-stats ON

Diff for: mysql-test/r/session_attrs_contain_server_cpu.result

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
# Test server_cpu query response attribute
3+
4+
use test;
5+
create table mytbl (a int, b int, c int, d int, e int);
6+
insert into mytbl values (1, 2, 3, 4, 5);
7+
insert into mytbl values (1, 2, 3, 4, 5);
8+
insert into mytbl values (1, 2, 3, 4, 5);
9+
insert into mytbl values (1, 2, 3, 4, 5);
10+
insert into mytbl select * from mytbl;
11+
insert into mytbl select * from mytbl;
12+
insert into mytbl select * from mytbl;
13+
insert into mytbl select * from mytbl;
14+
insert into mytbl select * from mytbl;
15+
insert into mytbl select * from mytbl;
16+
insert into mytbl select * from mytbl;
17+
insert into mytbl select * from mytbl;
18+
insert into mytbl select * from mytbl;
19+
insert into mytbl select * from mytbl;
20+
SET @@session.session_track_response_attributes = on;
21+
# Case 1: session level variable is default, i.e. FALSE
22+
set @@session.response_attrs_contain_server_cpu = default;
23+
select * from mytbl;
24+
Did not find server_cpu in query response attributes
25+
# Case 2: session level variable is TRUE
26+
set @@session.response_attrs_contain_server_cpu = true;
27+
select * from mytbl;
28+
Found valid value for server_cpu in query response attributes
29+
set @@session.response_attrs_contain_server_cpu = default;
30+
# Case 4: global level variable is TRUE but session is FALSE
31+
set @@global.response_attrs_contain_server_cpu = true;
32+
select a,b from mytbl;
33+
Did not find server_cpu in query response attributes
34+
set @@global.response_attrs_contain_server_cpu = default;
35+
36+
# Test multistatement query
37+
38+
use test;
39+
# Case 1: session level variable is default, i.e. FALSE
40+
set @@session.response_attrs_contain_server_cpu = default;
41+
select * from mytbl where a > 1;
42+
select * from mytbl where b > 1;
43+
select * from mytbl where c > 1;
44+
select * from mytbl where d > 1;
45+
select * from mytbl where e > 1;
46+
||||
47+
Did not find server_cpu in query response attributes
48+
# Case 2: session level variable is TRUE
49+
set @@session.response_attrs_contain_server_cpu = true;
50+
select * from mytbl where a > 1;
51+
select * from mytbl where b > 1;
52+
select * from mytbl where c > 1;
53+
select * from mytbl where d > 1;
54+
select * from mytbl where e > 1;
55+
||||
56+
Found valid value for server_cpu in query response attributes
57+
set @@session.response_attrs_contain_server_cpu = default;
58+
# Case 4: global level variable is TRUE but session is FALSE
59+
set @@global.response_attrs_contain_server_cpu = true;
60+
select * from mytbl where a > 1;
61+
select * from mytbl where b > 1;
62+
select * from mytbl where c > 1;
63+
select * from mytbl where d > 1;
64+
select * from mytbl where e > 1;
65+
||||
66+
Did not find server_cpu in query response attributes
67+
set @@global.response_attrs_contain_server_cpu = default;
68+
use test;
69+
drop table if exists mytbl;
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
# Test server_cpu query response attribute
3+
4+
use test;
5+
create table mytbl (c1 char(8) PRIMARY KEY, c2 int);
6+
SET @@session.session_track_response_attributes = on;
7+
# Test 1: session level variable is default, i.e. 0
8+
There should be no warnings present in the response
9+
set @@session.response_attrs_contain_warnings_bytes = default;
10+
explain select * from mytbl;
11+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
12+
1 SIMPLE mytbl NULL ALL NULL NULL NULL NULL 1 100.00 NULL
13+
Warnings:
14+
Note 1003 /* select#1 */ select `test`.`mytbl`.`c1` AS `c1`,`test`.`mytbl`.`c2` AS `c2` from `test`.`mytbl`
15+
[X] Did not find warnings in query response attributes
16+
# Test 2: session level variable is 1 MB
17+
There should be two warnings present in the response
18+
set @@session.response_attrs_contain_warnings_bytes = 1048576;
19+
explain select * from mytbl;
20+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
21+
1 SIMPLE mytbl NULL ALL NULL NULL NULL NULL 1 100.00 NULL
22+
Warnings:
23+
Note 1003 /* select#1 */ select `test`.`mytbl`.`c1` AS `c1`,`test`.`mytbl`.`c2` AS `c2` from `test`.`mytbl`
24+
[^] Warnings in query response attributes: (1003,/* select#1 */ select `test`.`mytbl`.`c1` AS `c1`,`test`.`mytbl`.`c2` AS `c2` from `test`.`mytbl`)
25+
# Test 5: session level variable is 5 bytes
26+
Warnings should be truncated to just message codes in the
27+
response since they exceed the bytes limit
28+
set @@session.response_attrs_contain_warnings_bytes = 5;
29+
explain select * from mytbl;
30+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
31+
1 SIMPLE mytbl NULL ALL NULL NULL NULL NULL 1 100.00 NULL
32+
Warnings:
33+
Note 1003 /* select#1 */ select `test`.`mytbl`.`c1` AS `c1`,`test`.`mytbl`.`c2` AS `c2` from `test`.`mytbl`
34+
[^] Warnings in query response attributes: (1003,)
35+
# Test 6: session level variable is 0, i.e., disabled
36+
There should be no warnings present in the response
37+
set @@session.response_attrs_contain_warnings_bytes = 0;
38+
explain select * from mytbl;
39+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
40+
1 SIMPLE mytbl NULL ALL NULL NULL NULL NULL 1 100.00 NULL
41+
Warnings:
42+
Note 1003 /* select#1 */ select `test`.`mytbl`.`c1` AS `c1`,`test`.`mytbl`.`c2` AS `c2` from `test`.`mytbl`
43+
[X] Did not find warnings in query response attributes
44+
Set variables to raise an additional warning for long running queries
45+
set @@GLOBAL.write_cpu_limit_milliseconds=1;
46+
set @@GLOBAL.write_control_level=NOTE;
47+
set @@GLOBAL.write_time_check_batch=3;
48+
set @@global.debug= '+d,dbug.force_long_running_query';
49+
# Test 7: session level variable is default, i.e. 0
50+
There should be no warnings present in the response
51+
set @@session.response_attrs_contain_warnings_bytes = default;
52+
insert into mytbl values('a1a1', -1), ('a2a2', -1), ('a3a3', -1);
53+
Warnings:
54+
Note 50091 Write CPU time exceeded (controlled by write_cpu_limit_milliseconds)
55+
[X] Did not find warnings in query response attributes
56+
# Test 8: session level variable is 1 MB
57+
There should be two warnings present in the response
58+
set @@session.response_attrs_contain_warnings_bytes = 1048576;
59+
insert into mytbl values('b1b1', -1), ('b2b2', -1), ('b3b3', -1);
60+
Warnings:
61+
Note 50091 Write CPU time exceeded (controlled by write_cpu_limit_milliseconds)
62+
[^] Warnings in query response attributes: (50091,Write CPU time exceeded (controlled by write_cpu_limit_milliseconds))
63+
# Test 9: session level variable is 5 bytes
64+
Warnings should be truncated to just message codes in the
65+
response since they exceed the bytes limit
66+
set @@session.response_attrs_contain_warnings_bytes = 5;
67+
insert into mytbl values('c1c1', -1), ('c2c2', -1), ('c3c3', -1);
68+
Warnings:
69+
Note 50091 Write CPU time exceeded (controlled by write_cpu_limit_milliseconds)
70+
[^] Warnings in query response attributes: (50091,)
71+
# Test 10: session level variable is 0, i.e., disabled
72+
There should be no warnings present in the response
73+
set @@session.response_attrs_contain_warnings_bytes = 0;
74+
insert into mytbl values('d1d1', -1), ('d2d2', -1), ('d3d3', -1);
75+
Warnings:
76+
Note 50091 Write CPU time exceeded (controlled by write_cpu_limit_milliseconds)
77+
[X] Did not find warnings in query response attributes
78+
set @@global.debug= '-d,dbug.force_long_running_query';
79+
set @@GLOBAL.write_control_level=OFF;
80+
set @@GLOBAL.write_cpu_limit_milliseconds=0;
81+
set @@GLOBAL.write_time_check_batch=0;
82+
set @@session.response_attrs_contain_warnings_bytes = default;
83+
use test;
84+
drop table if exists mytbl;

0 commit comments

Comments
 (0)