Merge lp:~dingqi-lxb/percona-server/5.5_log_queries_in_memory into lp:percona-server/5.5

Proposed by 林晓斌 on 2011-12-11
Status: Rejected
Rejected by: 林晓斌 on 2011-12-21
Proposed branch: lp:~dingqi-lxb/percona-server/5.5_log_queries_in_memory
Merge into: lp:percona-server/5.5
Diff against target: 898 lines (+879/-0) (has conflicts)
3 files modified
doc/source/diagnostics/slow_log_in_memory.rst (+74/-0)
patches/series (+4/-0)
patches/slowlog_in_memory.patch (+801/-0)
Text conflict in patches/series
To merge this branch: bzr merge lp:~dingqi-lxb/percona-server/5.5_log_queries_in_memory
Reviewer Review Type Date Requested Status
Oleg Tsarev (community) 2011-12-11 Needs Fixing on 2011-12-11
Review via email: mp+85251@code.launchpad.net

This proposal has been superseded by a proposal from 2011-12-20.

Description of the change

patch for https://blueprints.launchpad.net/percona-server/+spec/log-queries-in-memory.

add a variable 'slowlog_in_memory_audit_max_memory' and global status 'slow_log_current_memory'

If the current_memory grows larger than max_memory, the eldest ones will be wash out.
Same action happens when max_mamory are set to a new value that is less than current_memory.

To post a comment you must log in.
Oleg Tsarev (tsarev) wrote :

1. Please add patch name to the "patches/series" file
2. About innodb_trx_id. Please look to two following cases:
 * log_slow_verbosity doesn't set "innodb"
 * query doesn't use the InnoDB
In first case slow query log will has no information about InnoDB
In second case slow query will have line "# No InnoDB statistics available for this query\n"
In this two cases innodb_trx_id will be incorrect, and you can't use and setup it.

Please handle variable innodb_was_used correctly (at least for innodb_trx_id)

3. Following is very, very bad:
+ if (general_query)
+ {
....
+ }
+ else
+ {
+ this->general_query= "NULL";
+ this->general_query_len= 5;
+ }
You are break SQL semantic. For example query:
SELECT * FROM INSFORMATION_SCHEMA.I_S_SLOW_LOG WHERE QUERY IS NULL
or any join will not work correctly.
You are break SQL language semantic by string "NULL" instead of NULL

Same for Schema.

4. I_S_SLOW_LOG not very good name. Why SLOW_LOG bad?

5. Please add documentation to the doc/

6. Sleep in the test file - bad idea. Please replace this by debug variable query_exec_time.

7. Please add tests to sys_vars suite for the all added variables.

Please fix this, and I will review again.

review: Needs Fixing
Oleg Tsarev (tsarev) wrote :

8.
+ ulonglong utime_of_query= thd->current_utime() - thd->utime_after_lock;
+ if (utime_of_query <= thd->variables.long_query_time)
+ return;

Please look into the slow_extended.patch:

static inline ulonglong get_query_exec_time(THD *thd, ulonglong cur_utime)
...
thd->server_status|= SERVER_QUERY_WAS_SLOW;

Please better use this status variable, instead of calculate query execution time directly.

林晓斌 (dingqi-lxb) wrote :

Thank you. Oleg, I have some items to discuss.

2. About innodb_trx_id. Please look to two following cases
   In these two cases, the variable thd->innodb_trx_id is set to 0. It seems that I have to show it in the result table as 0, is there other result even use innodb_was_used ?

8.About calculating query execution time directly, it is because that I have to get the query_time, the status is not enough.

* Why plugin doesn't uninstall in test?
  You mean that it shows " Plugin is busy and will be uninstalled on shutdown" in result-file? It is the same with the demo for audit plugin "null_audit", the strategy decided by mysqld

Oleg Tsarev (tsarev) wrote :

On Mon, Dec 12, 2011 at 12:05 PM, 林晓斌 <email address hidden> wrote:
> Thank you. Oleg, I have some items to discuss.
>
> 2. About innodb_trx_id. Please look to two following cases
>   In these two cases, the variable thd->innodb_trx_id is set to 0.  It seems that I have to show it in the result table as 0, is there other result even use innodb_was_used ?

I think right decision is "NULL" value instead of 0.
>
> 8.About calculating query execution time directly, it is because that I have to get the query_time, the status is not enough.
>
> * Why plugin doesn't uninstall in test?
>  You mean that it shows " Plugin is busy and will be uninstalled on shutdown" in result-file? It is the same with the demo for audit plugin "null_audit", the strategy decided by mysqld

Ok

> --
> https://code.launchpad.net/~dingqi-lxb/percona-server/5.5_log_queries_in_memory/+merge/85251
> You are reviewing the proposed merge of lp:~dingqi-lxb/percona-server/5.5_log_queries_in_memory into lp:percona-server.

--
Oleg Tsarev, Software Engineer, Percona Inc.

林晓斌 (dingqi-lxb) wrote :

2. About innodb_trx_id. Generally, when the thd->innodb_trx_id is 0, the value of "INNODB_TRX_ID" should show NULL, so the thd->innodb_was_used will not be imported here.

7. Please add tests to sys_vars suite for the all added variables.
   The variable add in this plugin will not be seen in other test-case, it can only be seen the plugin is installed. So, is test-case in sys_vars suits necessary?

Unmerged revisions

200. By 林晓斌 on 2012-01-01

add slowlog_in_memory.patch

199. By 林晓斌 on 2012-01-01

merge from trunk

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'doc/source/diagnostics/slow_log_in_memory.rst'
2--- doc/source/diagnostics/slow_log_in_memory.rst 1970-01-01 00:00:00 +0000
3+++ doc/source/diagnostics/slow_log_in_memory.rst 2011-12-20 02:47:23 +0000
4@@ -0,0 +1,74 @@
5+.. _slow_log_in_memory:
6+
7+=================
8+ Slow Log In Memory
9+=================
10+
11+The feature is used to save slow queries in memory. This feature adds an ``INFORMATION_SCHEMA.SLOW_LOG`` table to show the latest queries, a variable to limit the max memory used, and status to show the status of memory used.
12+
13+There are two plugins in the feature. They can be installed by command ``"install plugin SLOWLOG_IN_MEMORY_AUDIT soname 'slowlog_in_memory.so'"`` and ``"install plugin SLOW_LOG soname 'slowlog_in_memory.so'"``, they work by keeping one list in memory.
14+
15+
16+Other Information
17+=================
18+
19+ * Author/Origin:
20+ *Percona*; *Percona* added an ``INFORMATION_SCHEMA`` table and the :variable:`slowlog_in_memory_audit_max_memory` and the :status:`slow_log_current_memory`.
21+
22+System Variables
23+================
24+
25+.. variable:: slowlog_in_memory_audit_max_memory
26+
27+ :cli: Yes
28+ :conf: Yes
29+ :scope: Global
30+ :dyn: Yes
31+ :vartype: ULONG
32+ :default: 1M
33+ :range: 0 - 1G
34+
35+The variable is used to limit the max-memory used for slow log. If a new slow query will make the `current_memory` bigger than this value, the most eldest items will be discarded, until there is enough memory for new item. When the value is decrease dynamically, it behaves the same.
36+
37+Status Variables
38+================
39+
40+One new status variable was introduced by this feature.
41+
42+.. variable:: slow_log_current_memory
43+
44+ :vartype: ULONG
45+ :scope: Global
46+
47+The variable showes the current memory used for the slow queries. It can never be bigger than the `slowlog_in_memory_audit_max_memory` variable.
48+
49+
50+INFORMATION_SCHEMA Tables
51+=========================
52+
53+.. table:: INFORMATION_SCHEMA.SLOW_LOG
54+
55+ :column EVENT_ID: A global increment id for every record in the table, can be used to identify every one.
56+ :column TIMESTAMP: The timestamp when the query stop.
57+ :column THREAD_ID: The thread_id of the query.
58+ :column DB: The current database() of the query, may be NULL.
59+ :column INNODB_TRX_ID: The Innodb_trx_id if InnoDB is used, may be NULL.
60+ :column ERROR: The errno of the query, 0 means no error.
61+ :column MS_QUERY_TIME: The query_exec_time of the query, in millisecond.
62+ :column QUERY: The query statement.
63+
64+This table holds the lastest slow queries.
65+
66+Example: ::
67+
68+ mysql> select * from information_schema.slow_log \G
69+ *************************** 1. row ***************************
70+ EVENT_ID: 1
71+ TIMESTAMP: 1323753896
72+ THREAD_ID: 2
73+ DB: test
74+ INNODB_TRX_ID: NULL
75+ ERRNO: 0
76+ MS_QUERY_TIME: 3001
77+ QUERY: select sleep(3)
78+
79
80=== modified file 'patches/series'
81--- patches/series 2011-12-16 09:37:26 +0000
82+++ patches/series 2011-12-20 02:47:23 +0000
83@@ -60,5 +60,9 @@
84 subunit.patch
85 bug860910.patch
86 bug45702.patch
87+<<<<<<< TREE
88 group_commit.patch
89 warning_fixes.patch
90+=======
91+slowlog_in_memory.patch
92+>>>>>>> MERGE-SOURCE
93
94=== added file 'patches/slowlog_in_memory.patch'
95--- patches/slowlog_in_memory.patch 1970-01-01 00:00:00 +0000
96+++ patches/slowlog_in_memory.patch 2011-12-20 02:47:23 +0000
97@@ -0,0 +1,801 @@
98+--- a/plugin/slowlog_in_memory/CMakeLists.txt 1970-01-01 08:00:00.000000000 +0800
99++++ b/plugin/slowlog_in_memory/CMakeLists.txt 2011-12-19 14:45:40.000000000 +0800
100+@@ -0,0 +1,17 @@
101++# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
102++#
103++# This program is free software; you can redistribute it and/or modify
104++# it under the terms of the GNU General Public License as published by
105++# the Free Software Foundation; version 2 of the License.
106++#
107++# This program is distributed in the hope that it will be useful,
108++# but WITHOUT ANY WARRANTY; without even the implied warranty of
109++# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
110++# GNU General Public License for more details.
111++#
112++# You should have received a copy of the GNU General Public License
113++# along with this program; if not, write to the Free Software
114++# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
115++
116++MYSQL_ADD_PLUGIN(slowlog_in_memory slowlog_in_memory.cc
117++ MODULE_ONLY MODULE_OUTPUT_NAME "slowlog_in_memory")
118+--- a/plugin/slowlog_in_memory/slowlog_in_memory.cc 1970-01-01 08:00:00.000000000 +0800
119++++ b/plugin/slowlog_in_memory/slowlog_in_memory.cc 2011-12-19 14:45:40.000000000 +0800
120+@@ -0,0 +1,295 @@
121++/* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
122++
123++ This program is free software; you can redistribute it and/or
124++ modify it under the terms of the GNU General Public License
125++ as published by the Free Software Foundation; version 2 of
126++ the License.
127++
128++ This program is distributed in the hope that it will be useful,
129++ but WITHOUT ANY WARRANTY; without even the implied warranty of
130++ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131++ GNU General Public License for more details.
132++
133++ You should have received a copy of the GNU General Public License
134++ along with this program; if not, write to the Free Software
135++ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
136++
137++#include "ut_slow_log_list.h"
138++
139++#if !defined(__attribute__) && (defined(__cplusplus) || !defined(__GNUC__) || __GNUC__ == 2 && __GNUC_MINOR__ < 8)
140++#define __attribute__(A)
141++#endif
142++typedef struct st_mysql_sys_var SYS_VAR;
143++uint max_memory_for_slowlog;
144++
145++/* lock management */
146++
147++slow_log_list *global_slow_log_list= NULL;
148++
149++/*
150++ Initialize the plugin at server start or plugin installation.
151++
152++ SYNOPSIS
153++ slowlog_in_memory_plugin_init()
154++
155++ DESCRIPTION
156++ Does nothing.
157++
158++ RETURN VALUE
159++ 0 success
160++ 1 failure (cannot happen)
161++*/
162++
163++static int slowlog_in_memory_plugin_init(void *arg __attribute__((unused)))
164++{
165++#ifdef HAVE_PSI_INTERFACE
166++ init_semisync_psi_keys();
167++#endif
168++
169++ mysql_mutex_init(key_ss_mutex_LOCK_slow_event_id_,
170++ &LOCK_slow_event_id_, MY_MUTEX_INIT_FAST);
171++ global_slow_log_list= new slow_log_list(max_memory_for_slowlog);
172++
173++ return 0;
174++}
175++
176++
177++/*
178++ Terminate the plugin at server shutdown or plugin deinstallation.
179++
180++ SYNOPSIS
181++ slowlog_in_memory_plugin_deinit()
182++ Does nothing.
183++
184++ RETURN VALUE
185++ 0 success
186++ 1 failure (cannot happen)
187++*/
188++
189++static int slowlog_in_memory_plugin_deinit(void *arg __attribute__((unused)))
190++{
191++ if (global_slow_log_list)
192++ {
193++ delete global_slow_log_list;
194++ global_slow_log_list= NULL;
195++ }
196++
197++ mysql_mutex_destroy(&LOCK_slow_event_id_);
198++ return 0;
199++}
200++
201++/*
202++ Foo
203++
204++ SYNOPSIS
205++ slowlog_in_memory_notify()
206++ thd connection context
207++
208++ DESCRIPTION
209++*/
210++
211++static void slowlog_in_memory_notify(MYSQL_THD thd __attribute__((unused)),
212++ unsigned int event_class,
213++ const void *event)
214++{
215++ if (max_memory_for_slowlog == 0) return;
216++
217++ if (event_class == MYSQL_AUDIT_GENERAL_CLASS)
218++ {
219++ const struct mysql_event_general *event_general=
220++ (const struct mysql_event_general *) event;
221++ if (event_general->event_subclass != MYSQL_AUDIT_GENERAL_STATUS)
222++ return;
223++
224++ ulonglong utime_of_query= thd->current_utime() - thd->utime_after_lock;
225++
226++#ifndef DBUG_OFF
227++ if (thd->variables.query_exec_time != 0)
228++ utime_of_query= thd->lex->sql_command != SQLCOM_SET_OPTION ? thd->variables.query_exec_time : 0;
229++#endif
230++
231++ if (utime_of_query <= thd->variables.long_query_time)
232++ return;
233++
234++ slow_log_t *tmp= new slow_log_t(event_general->general_time, event_general->general_thread_id, thd->db, thd->innodb_trx_id, event_general->general_error_code, utime_of_query/1000, event_general->general_query);
235++ global_slow_log_list->push_back(tmp);
236++ }
237++}
238++
239++
240++/*
241++ Plugin type-specific descriptor
242++*/
243++
244++static struct st_mysql_audit slowlog_in_memory_descriptor=
245++{
246++ MYSQL_AUDIT_INTERFACE_VERSION, /* interface version */
247++ NULL, /* release_thd function */
248++ slowlog_in_memory_notify, /* notify function */
249++ { (unsigned long) MYSQL_AUDIT_GENERAL_CLASSMASK } /* class mask */
250++};
251++
252++static void change_max_memory_for_slowlog(MYSQL_THD thd,
253++ SYS_VAR *var,
254++ void *ptr,
255++ const void *val)
256++{
257++ uint new_len= *(uint *)val;
258++ *(uint *)ptr= new_len;
259++
260++ global_slow_log_list->change_max_memory(new_len);
261++ return;
262++}
263++
264++static MYSQL_SYSVAR_UINT(max_memory, max_memory_for_slowlog,
265++ PLUGIN_VAR_OPCMDARG,
266++ "The max memory that can be allocated for slow log.",
267++ NULL, // check
268++ &change_max_memory_for_slowlog, // update
269++ 1024*1024, 0, 1024*1024*1024, 1);
270++
271++static SYS_VAR* slowlog_in_memory_system_vars[]= {
272++ MYSQL_SYSVAR(max_memory),
273++ NULL,
274++};
275++
276++static uint slow_log_current_memory(MYSQL_THD thd, SHOW_VAR *var, char *buff)
277++{
278++ var->type= SHOW_INT;
279++ var->value= (char*)&global_slow_log_list->current_memory;
280++ return 0;
281++}
282++
283++/* plugin status variables */
284++static SHOW_VAR slowlog_in_memory_status_vars[]= {
285++ {"slow_log_current_memory",
286++ (char*) &slow_log_current_memory,
287++ SHOW_FUNC},
288++ {NULL, NULL, SHOW_LONG},
289++};
290++
291++
292++/*
293++ I_S_SLOW_LOG
294++*/
295++static struct st_mysql_information_schema i_s_slow_log=
296++{
297++ MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION
298++};
299++
300++static ST_FIELD_INFO i_s_slow_log_fields[] =
301++{
302++ {"EVENT_ID", 11, MYSQL_TYPE_LONGLONG, 0, 0, 0, SKIP_OPEN_TABLE},
303++ {"TIMESTAMP", 11, MYSQL_TYPE_LONGLONG, 0, 0, 0, SKIP_OPEN_TABLE},
304++ {"THREAD_ID", 11, MYSQL_TYPE_LONG, 0, 0, 0, SKIP_OPEN_TABLE},
305++ {"DB", NAME_CHAR_LEN, MYSQL_TYPE_STRING, 0, 1, 0, SKIP_OPEN_TABLE},
306++ {"INNODB_TRX_ID", 11, MYSQL_TYPE_LONGLONG, 0, 1, 0, SKIP_OPEN_TABLE},
307++ {"ERRNO", 11, MYSQL_TYPE_LONG, 0, 0, 0, SKIP_OPEN_TABLE},
308++ {"MS_QUERY_TIME", 11, MYSQL_TYPE_LONGLONG, 0, 0, 0, SKIP_OPEN_TABLE},
309++
310++ {"QUERY", 255, MYSQL_TYPE_STRING, 0, 1, 0, SKIP_OPEN_TABLE},
311++ {0, 0, MYSQL_TYPE_STRING, 0, 0, 0, SKIP_OPEN_TABLE}
312++};
313++
314++
315++int i_s_fill_table_slow_log(THD* thd, TABLE_LIST* tables, COND* cond)
316++{
317++ if (!global_slow_log_list) return 0;
318++
319++ TABLE *table= tables->table;
320++
321++ mysql_mutex_lock(&global_slow_log_list->LOCK_slowlog_);
322++
323++ slow_log_t *item= global_slow_log_list->first;
324++ while (item)
325++ {
326++ restore_record(table, s->default_values);
327++
328++ table->field[0]->store(item->event_id, TRUE);
329++ table->field[1]->store(item->timestamp, TRUE);
330++ table->field[2]->store(item->thread_id, TRUE);
331++
332++ if (item->schema)
333++ {
334++ table->field[3]->store(item->schema, item->schema_len-1, system_charset_info);
335++ table->field[3]->set_notnull();
336++ }
337++
338++ if (item->innodb_trx_id)
339++ {
340++ table->field[4]->store(item->innodb_trx_id, TRUE);
341++ table->field[4]->set_notnull();
342++ }
343++
344++ table->field[5]->store(item->cmd_errno, TRUE);
345++ table->field[6]->store(item->mstime_of_query, TRUE);
346++
347++ if (item->general_query)
348++ {
349++ table->field[7]->store(item->general_query, item->general_query_len-1, system_charset_info);
350++ table->field[7]->set_notnull();
351++ }
352++
353++ schema_table_store_record(thd, table);
354++ item= item->next;
355++ }
356++
357++ mysql_mutex_unlock(&global_slow_log_list->LOCK_slowlog_);
358++ return 0;
359++}
360++
361++
362++static int i_s_slow_log_plugin_init(void *arg __attribute__((unused)))
363++{
364++ ST_SCHEMA_TABLE* schema;
365++
366++ schema = (ST_SCHEMA_TABLE*) arg;
367++
368++ schema->fields_info = i_s_slow_log_fields;
369++ schema->fill_table = i_s_fill_table_slow_log;
370++ return 0;
371++}
372++
373++
374++static int i_s_slow_log_plugin_deinit(void *arg __attribute__((unused)))
375++{
376++ return(0);
377++}
378++
379++/*
380++ Plugin library descriptor
381++*/
382++
383++mysql_declare_plugin(slowlog_in_memory)
384++{
385++ MYSQL_AUDIT_PLUGIN, /* type */
386++ &slowlog_in_memory_descriptor, /* descriptor */
387++ "SLOWLOG_IN_MEMORY_AUDIT", /* name */
388++ "Percona", /* author */
389++ "Error code histogram Audit", /* description */
390++ PLUGIN_LICENSE_GPL,
391++ slowlog_in_memory_plugin_init, /* init function (when loaded) */
392++ slowlog_in_memory_plugin_deinit, /* deinit function (when unloaded) */
393++ 0x0001, /* version */
394++ slowlog_in_memory_status_vars, /* status variables */
395++ slowlog_in_memory_system_vars, /* system variables */
396++ NULL,
397++ 0,
398++},
399++{
400++ MYSQL_INFORMATION_SCHEMA_PLUGIN, /* type */
401++ &i_s_slow_log, /* descriptor */
402++ "SLOW_LOG", /* name */
403++ "Percona", /* author */
404++ "latest slow log in memory", /* description */
405++ PLUGIN_LICENSE_GPL,
406++ i_s_slow_log_plugin_init, /* init function (when loaded) */
407++ i_s_slow_log_plugin_deinit, /* deinit function (when unloaded) */
408++ 0x0001, /* version */
409++ NULL, /* status variables */
410++ NULL, /* system variables */
411++ NULL,
412++ 0,
413++}
414++mysql_declare_plugin_end;
415++
416+--- a/plugin/slowlog_in_memory/ut_slow_log_list.h 1970-01-01 08:00:00.000000000 +0800
417++++ b/plugin/slowlog_in_memory/ut_slow_log_list.h 2011-12-19 14:45:40.000000000 +0800
418+@@ -0,0 +1,230 @@
419++#ifndef UT_SLOW_LOG_LIST_H
420++#define UT_SLOW_LOG_LIST_H
421++
422++#define MYSQL_SERVER
423++#include <mysql/plugin_audit.h>
424++#include "unireg.h"
425++#include "sql_show.h"
426++#include "sql_class.h"
427++
428++mysql_mutex_t LOCK_slow_event_id_;
429++
430++#ifdef HAVE_PSI_INTERFACE
431++PSI_mutex_key key_ss_mutex_LOCK_slowlog_;
432++PSI_mutex_key key_ss_mutex_LOCK_slow_event_id_;
433++
434++static PSI_mutex_info all_semisync_mutexes[]=
435++{
436++ {&key_ss_mutex_LOCK_slowlog_, "LOCK_slowlog_", 0},
437++ {&key_ss_mutex_LOCK_slow_event_id_, "LOCK_slow_event_id_", 0}
438++};
439++
440++static void init_semisync_psi_keys(void)
441++{
442++ const char* category= "semisync";
443++ int count;
444++
445++ if (PSI_server == NULL)
446++ return;
447++
448++ count= array_elements(all_semisync_mutexes);
449++ PSI_server->register_mutex(category, all_semisync_mutexes, count);
450++
451++}
452++#endif /* HAVE_PSI_INTERFACE */
453++
454++/* unique event id for each slow query */
455++ulonglong global_event_id= 0;
456++
457++const uint MAX_QUERY_LEN= 255;
458++
459++class slow_log_t
460++{
461++public:
462++ ulonglong event_id;
463++ ulonglong timestamp;
464++ ulong thread_id;
465++ char *schema;
466++ ulonglong innodb_trx_id;
467++ int cmd_errno;
468++ ulonglong mstime_of_query;
469++ char *general_query;
470++
471++ uint schema_len;
472++ uint general_query_len;
473++
474++ slow_log_t *next;
475++
476++ slow_log_t(ulonglong timestamp, ulong thread_id, const char *schema,
477++ ulonglong innodb_trx_id, int cmd_errno, ulonglong mstime_of_query, const char *general_query):next(NULL)
478++ {
479++ this->timestamp= timestamp;
480++ this->thread_id= thread_id;
481++
482++ if (schema)
483++ {
484++ this->schema_len= strlen(schema) + 1;
485++ this->schema= strndup(schema, this->schema_len);
486++ }
487++ else
488++ {
489++ this->schema= NULL;
490++ this->schema_len= 0;
491++ }
492++
493++ this->innodb_trx_id= innodb_trx_id;
494++ this->cmd_errno= cmd_errno;
495++ this->mstime_of_query= mstime_of_query;
496++
497++ if (general_query)
498++ {
499++ this->general_query_len= strlen(general_query) + 1;
500++
501++ if (this->general_query_len > MAX_QUERY_LEN)
502++ {
503++ this->general_query_len= MAX_QUERY_LEN;
504++ }
505++
506++ this->general_query= strndup(general_query, this->general_query_len);
507++ this->general_query[this->general_query_len-1]= '\0';
508++ }
509++ else
510++ {
511++ this->general_query= NULL;
512++ this->general_query_len= 0;
513++ }
514++
515++ this->real_size= sizeof(slow_log_t) + this->schema_len + this->general_query_len;
516++
517++ mysql_mutex_lock(&LOCK_slow_event_id_);
518++ this->event_id= ++global_event_id;
519++ mysql_mutex_unlock(&LOCK_slow_event_id_);
520++ }
521++
522++ ~slow_log_t()
523++ {
524++ if (schema)
525++ {
526++ free(schema);
527++ schema= NULL;
528++ }
529++
530++ if (general_query)
531++ {
532++ free(general_query);
533++ general_query= NULL;
534++ }
535++ }
536++
537++ uint size_of_instance()
538++ {
539++ return real_size;
540++ }
541++
542++ uint real_size;
543++};
544++
545++class slow_log_list
546++{
547++public:
548++ slow_log_t *first, *last;
549++ mysql_mutex_t LOCK_slowlog_;
550++
551++ uint current_memory;
552++
553++ void change_max_memory(uint new_len)
554++ {
555++ mysql_mutex_lock(&LOCK_slowlog_);
556++
557++ max_memory= new_len;
558++
559++ /* if the size is larger than current size, just change the value*/
560++ if (max_memory >= current_memory)
561++ {
562++ mysql_mutex_unlock(&LOCK_slowlog_);
563++ return;
564++ }
565++
566++ while (current_memory > max_memory)
567++ {
568++ pop_front();
569++ }
570++
571++ mysql_mutex_unlock(&LOCK_slowlog_);
572++ }
573++
574++ slow_log_list(uint max_memory)
575++ {
576++ first= NULL;
577++ last= NULL;
578++ current_memory= 0;
579++ this->max_memory= max_memory;
580++
581++ mysql_mutex_init(key_ss_mutex_LOCK_slowlog_,
582++ &LOCK_slowlog_, MY_MUTEX_INIT_FAST);
583++ }
584++
585++ ~slow_log_list()
586++ {
587++ mysql_mutex_lock(&LOCK_slowlog_);
588++ while (first)
589++ {
590++ pop_front();
591++ }
592++ mysql_mutex_unlock(&LOCK_slowlog_);
593++
594++ mysql_mutex_destroy(&LOCK_slowlog_);
595++ }
596++
597++ void push_back(slow_log_t * item)
598++ {
599++ mysql_mutex_lock(&LOCK_slowlog_);
600++
601++ uint size= item->size_of_instance();
602++
603++ if (size > max_memory)
604++ {
605++ sql_print_warning("the insert item size %u > %u discarded", size, max_memory);
606++ mysql_mutex_unlock(&LOCK_slowlog_);
607++ return;
608++ }
609++
610++ while (current_memory + size > max_memory)
611++ {
612++ pop_front();
613++ }
614++
615++ if (!first)
616++ {
617++ first= item;
618++ last= item;
619++ }
620++ else
621++ {
622++ last->next= item;
623++ last= item;
624++ }
625++
626++ current_memory+= size;
627++ mysql_mutex_unlock(&LOCK_slowlog_);
628++ }
629++
630++ /*functions call pop_front should have hold LOCK_slowlog_*/
631++ void pop_front()
632++ {
633++ if (!first) return;
634++
635++ slow_log_t *item= first;
636++ first= first->next;
637++
638++ if (!first) last= NULL;
639++
640++ current_memory-= item->size_of_instance();
641++ delete item;
642++ }
643++
644++private:
645++ uint max_memory;
646++};
647++#endif
648++
649+--- a/mysql-test/include/have_slog_log_in_memory_audit_plugin.inc 1970-01-01 08:00:00.000000000 +0800
650++++ b/mysql-test/include/have_slog_log_in_memory_audit_plugin.inc 2011-12-19 19:13:34.000000000 +0800
651+@@ -0,0 +1,21 @@
652++#
653++# Check if server has support for loading plugins
654++#
655++if (`SELECT @@have_dynamic_loading != 'YES'`) {
656++ --skip Requires dynamic loading
657++}
658++
659++#
660++# Check if the variable SLOW_LOG_IN_MEMORY_AUDIT is set
661++#
662++if (!$SLOW_LOG_IN_MEMORY_AUDIT)
663++{
664++ skip Need slow_log_in_memory plugins;
665++}
666++
667++#
668++# Check if --plugin-dir was setup for slow_log_in_memory
669++#
670++if (`SELECT CONCAT('--plugin-dir=', @@plugin_dir) != '$SLOW_LOG_IN_MEMORY_AUDIT_OPT'`) {
671++ --skip SLOW_LOG plugin requires that --plugin-dir is set to the slow_log_in_memory plugin dir (either the .opt file does not contain \$SLOW_LOG_IN_MEMORY_AUDIT_OPT or another plugin is in use)
672++}
673+--- a/mysql-test/include/plugin.defs 2011-12-19 14:44:49.000000000 +0800
674++++ b/mysql-test/include/plugin.defs 2011-12-19 19:15:18.000000000 +0800
675+@@ -40,3 +40,5 @@
676+ ha_federated storage/federated FEDERATED_PLUGIN
677+ mypluglib plugin/fulltext SIMPLE_PARSER
678+ libdaemon_example plugin/daemon_example DAEMONEXAMPLE
679++slowlog_in_memory plugin/slowlog_in_memory SLOW_LOG
680++slowlog_in_memory plugin/slowlog_in_memory SLOW_LOG_IN_MEMORY_AUDIT
681+--- a/mysql-test/r/slow_log_in_memory.result 1970-01-01 08:00:00.000000000 +0800
682++++ b/mysql-test/r/slow_log_in_memory.result 2011-12-19 19:10:14.000000000 +0800
683+@@ -0,0 +1,67 @@
684++#
685++# feature: slow log show in information_schema.slow_log
686++#
687++install plugin SLOWLOG_IN_MEMORY_AUDIT soname 'slowlog_in_memory.so';
688++install plugin SLOW_LOG soname 'slowlog_in_memory.so';
689++show variables like 'slowlog_in_memory_audit_max_memory';
690++Variable_name Value
691++slowlog_in_memory_audit_max_memory 1048576
692++SET long_query_time=2;
693++SET query_exec_time=2.1;
694++select 1;
695++1
696++1
697++SET query_exec_time=0;
698++SET long_query_time=2;
699++SET query_exec_time=2.1;
700++select 1;
701++1
702++1
703++SET query_exec_time=0;
704++SET long_query_time=2;
705++SET query_exec_time=2.1;
706++select 1;
707++1
708++1
709++SET query_exec_time=0;
710++show global status like 'slow_log_current_memory';
711++Variable_name Value
712++slow_log_current_memory XX
713++select * from information_schema.slow_log;
714++EVENT_ID TIMESTAMP THREAD_ID DB INNODB_TRX_ID ERRNO MS_QUERY_TIME QUERY
715++1 XX 1 test XX 0 2100 select 1
716++2 XX 2 test XX 0 2100 select 1
717++3 XX 3 test XX 0 2100 select 1
718++# decrease max_memory and wash eldest
719++set global slowlog_in_memory_audit_max_memory=240;
720++use information_schema;
721++SET long_query_time=2;
722++SET query_exec_time=2.1;
723++select 1;
724++1
725++1
726++SET query_exec_time=0;
727++show global status like 'slow_log_current_memory';
728++Variable_name Value
729++slow_log_current_memory XX
730++select * from information_schema.slow_log;
731++EVENT_ID TIMESTAMP THREAD_ID DB INNODB_TRX_ID ERRNO MS_QUERY_TIME QUERY
732++3 XX 3 test XX 0 2100 select 1
733++4 XX 2 information_schema XX 0 2100 select 1
734++# set to zero to disable audit
735++set global slowlog_in_memory_audit_max_memory=0;
736++SET long_query_time=2;
737++SET query_exec_time=2.1;
738++select 1;
739++1
740++1
741++SET query_exec_time=0;
742++show global status like 'slow_log_current_memory';
743++Variable_name Value
744++slow_log_current_memory 0
745++select * from information_schema.slow_log;
746++EVENT_ID TIMESTAMP THREAD_ID DB INNODB_TRX_ID ERRNO MS_QUERY_TIME QUERY
747++uninstall plugin SLOW_LOG;
748++uninstall plugin SLOWLOG_IN_MEMORY_AUDIT;
749++Warnings:
750++Warning 1620 Plugin is busy and will be uninstalled on shutdown
751+--- a/mysql-test/suite/sys_vars/r/slow_log_in_memory_audit.result 1970-01-01 08:00:00.000000000 +0800
752++++ b/mysql-test/suite/sys_vars/r/slow_log_in_memory_audit.result 2011-12-19 19:12:14.000000000 +0800
753+@@ -0,0 +1,31 @@
754++install plugin SLOWLOG_IN_MEMORY_AUDIT soname 'slowlog_in_memory.so';
755++install plugin SLOW_LOG soname 'slowlog_in_memory.so';
756++select @@global.slowlog_in_memory_audit_max_memory;
757++@@global.slowlog_in_memory_audit_max_memory
758++1048576
759++show global variables like 'slowlog_in_memory_audit_max_memory';
760++Variable_name Value
761++slowlog_in_memory_audit_max_memory 1048576
762++select * from information_schema.global_variables where variable_name='slowlog_in_memory_audit_max_memory';
763++VARIABLE_NAME VARIABLE_VALUE
764++SLOWLOG_IN_MEMORY_AUDIT_MAX_MEMORY 1048576
765++set global slowlog_in_memory_audit_max_memory=2097152;
766++select @@global.slowlog_in_memory_audit_max_memory;
767++@@global.slowlog_in_memory_audit_max_memory
768++2097152
769++show global variables like 'slowlog_in_memory_audit_max_memory';
770++Variable_name Value
771++slowlog_in_memory_audit_max_memory 2097152
772++set global slowlog_in_memory_audit_max_memory=1.1;
773++ERROR 42000: Incorrect argument type to variable 'slowlog_in_memory_audit_max_memory'
774++set global slowlog_in_memory_audit_max_memory=1e1;
775++ERROR 42000: Incorrect argument type to variable 'slowlog_in_memory_audit_max_memory'
776++set global slowlog_in_memory_audit_max_memory="some text";
777++ERROR 42000: Incorrect argument type to variable 'slowlog_in_memory_audit_max_memory'
778++select @@global.slowlog_in_memory_audit_max_memory;
779++@@global.slowlog_in_memory_audit_max_memory
780++2097152
781++UNINSTALL PLUGIN SLOW_LOG;
782++UNINSTALL PLUGIN SLOWLOG_IN_MEMORY_AUDIT;
783++Warnings:
784++Warning 1620 Plugin is busy and will be uninstalled on shutdown
785+--- a/mysql-test/suite/sys_vars/t/slow_log_in_memory_audit-master.opt 1970-01-01 08:00:00.000000000 +0800
786++++ b/mysql-test/suite/sys_vars/t/slow_log_in_memory_audit-master.opt 2011-12-19 19:12:02.000000000 +0800
787+@@ -0,0 +1 @@
788++$SLOW_LOG_IN_MEMORY_AUDIT_OPT
789+--- a/mysql-test/suite/sys_vars/t/slow_log_in_memory_audit.test 1970-01-01 08:00:00.000000000 +0800
790++++ b/mysql-test/suite/sys_vars/t/slow_log_in_memory_audit.test 2011-12-19 19:12:02.000000000 +0800
791+@@ -0,0 +1,40 @@
792++
793++#
794++# exists as a global only
795++#
796++source include/not_embedded.inc;
797++source include/have_slog_log_in_memory_audit_plugin.inc;
798++# The following is to prevent a mis-match on windows that has the name of of the lib ending with 'dll'
799++--replace_regex /\.dll/.so/
800++install plugin SLOWLOG_IN_MEMORY_AUDIT soname 'slowlog_in_memory.so';
801++install plugin SLOW_LOG soname 'slowlog_in_memory.so';
802++select @@global.slowlog_in_memory_audit_max_memory;
803++
804++show global variables like 'slowlog_in_memory_audit_max_memory';
805++select * from information_schema.global_variables where variable_name='slowlog_in_memory_audit_max_memory';
806++
807++#
808++# show that it's writable
809++#
810++set global slowlog_in_memory_audit_max_memory=2097152;
811++select @@global.slowlog_in_memory_audit_max_memory;
812++show global variables like 'slowlog_in_memory_audit_max_memory';
813++
814++#
815++# incorrect types
816++#
817++--error ER_WRONG_TYPE_FOR_VAR
818++set global slowlog_in_memory_audit_max_memory=1.1;
819++--error ER_WRONG_TYPE_FOR_VAR
820++set global slowlog_in_memory_audit_max_memory=1e1;
821++--error ER_WRONG_TYPE_FOR_VAR
822++set global slowlog_in_memory_audit_max_memory="some text";
823++
824++
825++#
826++# Cleanup
827++#
828++select @@global.slowlog_in_memory_audit_max_memory;
829++UNINSTALL PLUGIN SLOW_LOG;
830++UNINSTALL PLUGIN SLOWLOG_IN_MEMORY_AUDIT;
831++
832+--- a/mysql-test/t/slow_log_in_memory-master.opt 1970-01-01 08:00:00.000000000 +0800
833++++ b/mysql-test/t/slow_log_in_memory-master.opt 2011-12-19 19:10:07.000000000 +0800
834+@@ -0,0 +1 @@
835++$SLOW_LOG_IN_MEMORY_AUDIT_OPT
836+--- a/mysql-test/t/slow_log_in_memory.test 1970-01-01 08:00:00.000000000 +0800
837++++ b/mysql-test/t/slow_log_in_memory.test 2011-12-19 19:10:07.000000000 +0800
838+@@ -0,0 +1,60 @@
839++--source include/have_debug.inc
840++--source include/have_slog_log_in_memory_audit_plugin.inc
841++
842++--echo #
843++--echo # feature: slow log show in information_schema.slow_log
844++--echo #
845++
846++-- source include/restart_mysqld.inc
847++install plugin SLOWLOG_IN_MEMORY_AUDIT soname 'slowlog_in_memory.so';
848++install plugin SLOW_LOG soname 'slowlog_in_memory.so';
849++show variables like 'slowlog_in_memory_audit_max_memory';
850++
851++SET long_query_time=2;
852++SET query_exec_time=2.1;
853++select 1;
854++SET query_exec_time=0;
855++
856++connect (con1,localhost,root,,);
857++
858++SET long_query_time=2;
859++SET query_exec_time=2.1;
860++select 1;
861++SET query_exec_time=0;
862++
863++connect (con2,localhost,root,,);
864++
865++SET long_query_time=2;
866++SET query_exec_time=2.1;
867++select 1;
868++SET query_exec_time=0;
869++--replace_column 2 XX
870++show global status like 'slow_log_current_memory';
871++--replace_column 2 XX 5 XX
872++select * from information_schema.slow_log;
873++
874++--echo # decrease max_memory and wash eldest
875++set global slowlog_in_memory_audit_max_memory=240;
876++connection con1;
877++use information_schema;
878++SET long_query_time=2;
879++SET query_exec_time=2.1;
880++select 1;
881++SET query_exec_time=0;
882++--replace_column 2 XX
883++show global status like 'slow_log_current_memory';
884++--replace_column 2 XX 5 XX
885++select * from information_schema.slow_log;
886++
887++--echo # set to zero to disable audit
888++connection con1;
889++set global slowlog_in_memory_audit_max_memory=0;
890++SET long_query_time=2;
891++SET query_exec_time=2.1;
892++select 1;
893++SET query_exec_time=0;
894++show global status like 'slow_log_current_memory';
895++select * from information_schema.slow_log;
896++
897++uninstall plugin SLOW_LOG;
898++uninstall plugin SLOWLOG_IN_MEMORY_AUDIT;

Subscribers

People subscribed via source and target branches