1 # name : sql_no_fcache.patch
6 # Any small change to this file in the main branch
7 # should be done or reviewed by the maintainer!
8 --- a/client/mysqldump.c
9 +++ b/client/mysqldump.c
12 static uint opt_protocol= 0;
14 +static my_bool server_supports_sql_no_fcache= FALSE;
17 Dynamic_string wrapper functions. In this file use these
18 wrappers, they will terminate the process if there is
19 @@ -1521,6 +1523,17 @@
20 /* Don't switch charsets for 4.1 and earlier. (bug#34192). */
21 server_supports_switching_charsets= FALSE;
24 + /* Check to see if we support SQL_NO_FCACHE on this server. */
25 + if (mysql_query(mysql, "SELECT SQL_NO_FCACHE NOW()") == 0)
27 + MYSQL_RES *res = mysql_store_result(mysql);
30 + mysql_free_result(res);
32 + server_supports_sql_no_fcache= TRUE;
35 As we're going to set SQL_MODE, it would be lost on reconnect, so we
37 @@ -3184,7 +3197,12 @@
39 /* now build the query string */
41 - dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ * INTO OUTFILE '");
42 + dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ ");
43 + if (server_supports_sql_no_fcache)
45 + dynstr_append_checked(&query_string, "/*!50084 SQL_NO_FCACHE */ ");
47 + dynstr_append_checked(&query_string, "* INTO OUTFILE '");
48 dynstr_append_checked(&query_string, filename);
49 dynstr_append_checked(&query_string, "'");
51 @@ -3234,7 +3252,12 @@
52 check_io(md_result_file);
55 - dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ * FROM ");
56 + dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ ");
57 + if (server_supports_sql_no_fcache)
59 + dynstr_append_checked(&query_string, "/*!50084 SQL_NO_FCACHE */ ");
61 + dynstr_append_checked(&query_string, "* FROM ");
62 dynstr_append_checked(&query_string, result_table);
66 +++ b/include/flashcache_ioctl.h
68 +/****************************************************************************
69 + * flashcache_ioctl.h
70 + * FlashCache: Device mapper target for block-level disk caching
72 + * Copyright 2010 Facebook, Inc.
73 + * Author: Mohan Srinivasan (mohan@facebook.com)
75 + * Based on DM-Cache:
76 + * Copyright (C) International Business Machines Corp., 2006
77 + * Author: Ming Zhao (mingzhao@ufl.edu)
79 + * This program is free software; you can redistribute it and/or modify
80 + * it under the terms of the GNU General Public License as published by
81 + * the Free Software Foundation; under version 2 of the License.
83 + * This program is distributed in the hope that it will be useful,
84 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
85 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
86 + * GNU General Public License for more details.
88 + * You should have received a copy of the GNU General Public License
89 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
90 + ****************************************************************************/
92 +#ifndef FLASHCACHE_IOCTL_H
93 +#define FLASHCACHE_IOCTL_H
95 +#include <linux/types.h>
97 +#define FLASHCACHE_IOCTL 0xfe
100 + FLASHCACHEADDNCPID_CMD=200,
101 + FLASHCACHEDELNCPID_CMD,
102 + FLASHCACHEDELNCALL_CMD,
103 + FLASHCACHEADDWHITELIST_CMD,
104 + FLASHCACHEDELWHITELIST_CMD,
105 + FLASHCACHEDELWHITELISTALL_CMD,
108 +#define FLASHCACHEADDNCPID _IOW(FLASHCACHE_IOCTL, FLASHCACHEADDNCPID_CMD, pid_t)
109 +#define FLASHCACHEDELNCPID _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELNCPID_CMD, pid_t)
110 +#define FLASHCACHEDELNCALL _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELNCALL_CMD, pid_t)
112 +#define FLASHCACHEADDBLACKLIST FLASHCACHEADDNCPID
113 +#define FLASHCACHEDELBLACKLIST FLASHCACHEDELNCPID
114 +#define FLASHCACHEDELALLBLACKLIST FLASHCACHEDELNCALL
116 +#define FLASHCACHEADDWHITELIST _IOW(FLASHCACHE_IOCTL, FLASHCACHEADDWHITELIST_CMD, pid_t)
117 +#define FLASHCACHEDELWHITELIST _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELWHITELIST_CMD, pid_t)
118 +#define FLASHCACHEDELALLWHITELIST _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELWHITELISTALL_CMD, pid_t)
121 --- a/mysql-test/r/mysqldump.result
122 +++ b/mysql-test/r/mysqldump.result
123 @@ -1832,7 +1832,7 @@
124 # Bug#21288 mysqldump segmentation fault when using --where
126 create table t1 (a int);
127 -mysqldump: Couldn't execute 'SELECT /*!40001 SQL_NO_CACHE */ * FROM `t1` WHERE xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' at line 1 (1064)
128 +mysqldump: Couldn't execute 'SELECT /*!40001 SQL_NO_CACHE */ /*!50084 SQL_NO_FCACHE */ * FROM `t1` WHERE xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' at line 1 (1064)
129 mysqldump: Got error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' at line 1 when retrieving data from server
131 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
133 +++ b/patch_info/sql_no_fcache.info
135 +File=sql_no_fcache.patch
136 +Name=Support for flashcache including the SQL_NO_FCACHE option that prevents blocks from being cached during a query.
144 { "SQL_CACHE", SYM(SQL_CACHE_SYM)},
145 { "SQL_CALC_FOUND_ROWS", SYM(SQL_CALC_FOUND_ROWS)},
146 { "SQL_NO_CACHE", SYM(SQL_NO_CACHE_SYM)},
147 + { "SQL_NO_FCACHE", SYM(SQL_NO_FCACHE_SYM)},
148 { "SQL_SMALL_RESULT", SYM(SQL_SMALL_RESULT)},
149 { "SQL_THREAD", SYM(SQL_THREAD)},
150 { "SQL_TSI_FRAC_SECOND", SYM(FRAC_SECOND_SYM)},
154 #define OPT_NDB_SHM_DEFAULT 0
157 +#if defined(__linux__)
159 +#include <sys/statfs.h>
160 +#include "flashcache_ioctl.h"
163 #ifndef DEFAULT_SKIP_THREAD_PRIORITY
164 #define DEFAULT_SKIP_THREAD_PRIORITY 0
166 ulong max_long_data_size;
167 uint max_user_connections= 0;
168 ulonglong denied_connections = 0;
172 +my_bool cachedev_enabled= FALSE;
175 Limit of the total number of prepared statements in the server.
176 Is necessary to protect the server against out-of-memory attacks.
177 @@ -4412,6 +4422,97 @@
181 +#if defined(__linux__)
183 + * Auto detect if we support flash cache on the host system.
184 + * This needs to be called before we setuid away from root
185 + * to avoid permission problems on opening the device node.
187 +static void init_cachedev(void)
189 + struct statfs stfs_data_home_dir;
190 + struct statfs stfs;
191 + struct mntent *ent;
192 + pid_t pid = getpid();
194 + const char *error_message= NULL;
196 + // disabled by default
198 + cachedev_enabled= FALSE;
200 + if (!mysql_data_home)
202 + error_message= "mysql_data_home not set";
206 + if (statfs(mysql_data_home, &stfs_data_home_dir) < 0)
208 + error_message= "statfs failed";
212 + mounts = setmntent("/etc/mtab", "r");
213 + if (mounts == NULL)
215 + error_message= "setmntent failed";
219 + while ((ent = getmntent(mounts)) != NULL)
221 + if (statfs(ent->mnt_dir, &stfs) < 0)
223 + if (memcmp(&stfs.f_fsid, &stfs_data_home_dir.f_fsid, sizeof(fsid_t)) == 0)
230 + error_message= "getmntent loop failed";
234 + cachedev_fd = open(ent->mnt_fsname, O_RDONLY);
235 + if (cachedev_fd < 0)
237 + error_message= "open flash device failed";
241 + /* cleanup previous whitelistings */
242 + if (ioctl(cachedev_fd, FLASHCACHEDELALLWHITELIST, &pid) < 0)
244 + close(cachedev_fd);
246 + error_message= "ioctl failed";
248 + ioctl(cachedev_fd, FLASHCACHEADDWHITELIST, &pid);
252 + sql_print_information("Flashcache bypass: %s",
253 + (cachedev_fd > 0) ? "enabled" : "disabled");
255 + sql_print_information("Flashcache setup error is : %s\n", error_message);
257 + cachedev_enabled= TRUE;
261 +static void cleanup_cachedev(void)
263 + pid_t pid = getpid();
265 + if (cachedev_enabled) {
266 + ioctl(cachedev_fd, FLASHCACHEDELWHITELIST, &pid);
267 + close(cachedev_fd);
274 int win_main(int argc, char **argv)
275 @@ -4516,6 +4617,10 @@
279 +#if defined(__linux__)
284 We have enough space for fiddling with the argv, continue
286 @@ -4717,6 +4822,10 @@
288 my_end(opt_endinfo ? MY_CHECK_ERROR | MY_GIVE_INFO : 0);
290 +#if defined(__linux__)
291 + cleanup_cachedev();
295 return(0); /* purecov: deadcode */
297 @@ -7890,6 +7999,7 @@
298 {"Delayed_errors", (char*) &delayed_insert_errors, SHOW_LONG},
299 {"Delayed_insert_threads", (char*) &delayed_insert_threads, SHOW_LONG_NOFLUSH},
300 {"Delayed_writes", (char*) &delayed_insert_writes, SHOW_LONG},
301 + {"Flashcache_enabled", (char*) &cachedev_enabled, SHOW_BOOL },
302 {"Flush_commands", (char*) &refresh_version, SHOW_LONG_NOFLUSH},
303 {"Handler_commit", (char*) offsetof(STATUS_VAR, ha_commit_count), SHOW_LONG_STATUS},
304 {"Handler_delete", (char*) offsetof(STATUS_VAR, ha_delete_count), SHOW_LONG_STATUS},
305 --- a/sql/mysql_priv.h
306 +++ b/sql/mysql_priv.h
309 extern ulong server_id, concurrency;
312 +extern int cachedev_fd;
314 typedef my_bool (*qc_engine_callback)(THD *thd, char *table_key,
320 lex->subqueries= FALSE;
321 lex->context_analysis_only= 0;
322 + lex->disable_flashcache= FALSE;
323 lex->derived_tables= 0;
324 lex->lock_option= TL_READ;
325 lex->safe_to_cache_query= 1;
328 @@ -1730,6 +1730,7 @@
330 uint8 context_analysis_only;
331 bool safe_to_cache_query;
332 + bool disable_flashcache;
333 bool subqueries, ignore;
334 st_parsing_options parsing_options;
335 Alter_info alter_info;
336 --- a/sql/sql_select.cc
337 +++ b/sql/sql_select.cc
340 #include <ft_global.h>
342 +#include <sys/syscall.h>
343 +#include <sys/ioctl.h>
344 +#if defined(__linux__)
345 +#include "flashcache_ioctl.h"
348 const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
349 "MAYBE_REF","ALL","range","index","fulltext",
350 "ref_or_null","unique_subquery","index_subquery",
352 ulong setup_tables_done_option)
355 +#if defined(__linux__)
358 register SELECT_LEX *select_lex = &lex->select_lex;
359 DBUG_ENTER("handle_select");
361 +#if defined(__linux__)
362 + if(lex->disable_flashcache && cachedev_fd > 0)
364 + pid = syscall(SYS_gettid);
365 + ioctl(cachedev_fd, FLASHCACHEADDNCPID, &pid);
369 if (select_lex->master_unit()->is_union() ||
370 select_lex->master_unit()->fake_select_lex)
371 res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
376 +#if defined(__linux__)
377 + if (lex->disable_flashcache && cachedev_fd > 0)
379 + ioctl(cachedev_fd, FLASHCACHEDELNCPID, &pid);
385 --- a/sql/sql_yacc.yy
386 +++ b/sql/sql_yacc.yy
387 @@ -1166,6 +1166,7 @@
389 %token SQL_CALC_FOUND_ROWS
390 %token SQL_NO_CACHE_SYM
391 +%token SQL_NO_FCACHE_SYM
392 %token SQL_SMALL_RESULT
393 %token SQL_SYM /* SQL-2003-R */
395 @@ -6757,6 +6758,10 @@
396 Lex->select_lex.options&= ~OPTION_TO_QUERY_CACHE;
397 Lex->select_lex.sql_cache= SELECT_LEX::SQL_NO_CACHE;
399 + | SQL_NO_FCACHE_SYM
401 + Lex->disable_flashcache= TRUE;
407 +++ b/mysql-test/r/percona_sql_no_fcache.result
409 +drop table if exists t1;
410 +create table t (a int not null);
411 +insert into t values (1),(2),(3);
412 +SELECT SQL_NO_FCACHE SLEEP(0);
415 +SELECT /*!40001 SQL_NO_CACHE */ /*!50084 SQL_NO_FCACHE */ * FROM t;
422 +++ b/mysql-test/t/percona_sql_no_fcache.test
425 +drop table if exists t1;
428 +create table t (a int not null);
429 +insert into t values (1),(2),(3);
431 +SELECT SQL_NO_FCACHE SLEEP(0);
432 +SELECT /*!40001 SQL_NO_CACHE */ /*!50084 SQL_NO_FCACHE */ * FROM t;