]> git.pld-linux.org Git - packages/mysql.git/blame - sql_no_fcache.patch
fox for bug #62472 (mysqlhotcopy errors on databases containing MyISAM)
[packages/mysql.git] / sql_no_fcache.patch
CommitLineData
b4e1fa2c
AM
1# name : sql_no_fcache.patch
2# introduced : 12
3# maintainer : Oleg
4#
5#!!! notice !!!
6# Any small change to this file in the main branch
7# should be done or reviewed by the maintainer!
db82db79
AM
8--- a/client/mysqldump.c
9+++ b/client/mysqldump.c
734d6226 10@@ -143,6 +143,8 @@
b4e1fa2c 11 static uint opt_protocol= 0;
df1b5770 12 static char *opt_plugin_dir= 0, *opt_default_auth= 0;
b4e1fa2c
AM
13
14+static my_bool server_supports_sql_no_fcache= FALSE;
15+
16 /*
17 Dynamic_string wrapper functions. In this file use these
18 wrappers, they will terminate the process if there is
734d6226 19@@ -1496,6 +1498,17 @@
b4e1fa2c
AM
20 /* Don't switch charsets for 4.1 and earlier. (bug#34192). */
21 server_supports_switching_charsets= FALSE;
22 }
23+
24+ /* Check to see if we support SQL_NO_FCACHE on this server. */
25+ if (mysql_query(mysql, "SELECT SQL_NO_FCACHE NOW()") == 0)
26+ {
27+ MYSQL_RES *res = mysql_store_result(mysql);
28+ if (res)
29+ {
30+ mysql_free_result(res);
31+ }
32+ server_supports_sql_no_fcache= TRUE;
33+ }
34 /*
35 As we're going to set SQL_MODE, it would be lost on reconnect, so we
36 cannot reconnect.
734d6226 37@@ -3177,7 +3190,12 @@
b4e1fa2c
AM
38
39 /* now build the query string */
40
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)
44+ {
45+ dynstr_append_checked(&query_string, "/*!50084 SQL_NO_FCACHE */ ");
46+ }
47+ dynstr_append_checked(&query_string, "* INTO OUTFILE '");
48 dynstr_append_checked(&query_string, filename);
49 dynstr_append_checked(&query_string, "'");
50
734d6226 51@@ -3227,7 +3245,12 @@
b4e1fa2c
AM
52 check_io(md_result_file);
53 }
54
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)
58+ {
59+ dynstr_append_checked(&query_string, "/*!50084 SQL_NO_FCACHE */ ");
60+ }
61+ dynstr_append_checked(&query_string, "* FROM ");
62 dynstr_append_checked(&query_string, result_table);
63
64 if (where)
db82db79
AM
65--- /dev/null
66+++ b/include/flashcache_ioctl.h
b4e1fa2c
AM
67@@ -0,0 +1,53 @@
68+/****************************************************************************
69+ * flashcache_ioctl.h
70+ * FlashCache: Device mapper target for block-level disk caching
71+ *
72+ * Copyright 2010 Facebook, Inc.
73+ * Author: Mohan Srinivasan (mohan@facebook.com)
74+ *
75+ * Based on DM-Cache:
76+ * Copyright (C) International Business Machines Corp., 2006
77+ * Author: Ming Zhao (mingzhao@ufl.edu)
78+ *
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.
82+ *
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.
87+ *
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+ ****************************************************************************/
91+
92+#ifndef FLASHCACHE_IOCTL_H
93+#define FLASHCACHE_IOCTL_H
94+
95+#include <linux/types.h>
96+
97+#define FLASHCACHE_IOCTL 0xfe
98+
99+enum {
100+ FLASHCACHEADDNCPID_CMD=200,
101+ FLASHCACHEDELNCPID_CMD,
102+ FLASHCACHEDELNCALL_CMD,
103+ FLASHCACHEADDWHITELIST_CMD,
104+ FLASHCACHEDELWHITELIST_CMD,
105+ FLASHCACHEDELWHITELISTALL_CMD,
106+};
107+
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)
111+
112+#define FLASHCACHEADDBLACKLIST FLASHCACHEADDNCPID
113+#define FLASHCACHEDELBLACKLIST FLASHCACHEDELNCPID
114+#define FLASHCACHEDELALLBLACKLIST FLASHCACHEDELNCALL
115+
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)
119+
120+#endif
db82db79
AM
121--- /dev/null
122+++ b/patch_info/sql_no_fcache.info
b4e1fa2c
AM
123@@ -0,0 +1,6 @@
124+File=sql_no_fcache.patch
125+Name=Support for flashcache including the SQL_NO_FCACHE option that prevents blocks from being cached during a query.
126+Version=1.0
127+Author=Facebook
128+License=GPL
129+Comment=
db82db79
AM
130--- a/sql/lex.h
131+++ b/sql/lex.h
b4e1fa2c
AM
132@@ -516,6 +516,7 @@
133 { "SQL_CACHE", SYM(SQL_CACHE_SYM)},
134 { "SQL_CALC_FOUND_ROWS", SYM(SQL_CALC_FOUND_ROWS)},
135 { "SQL_NO_CACHE", SYM(SQL_NO_CACHE_SYM)},
136+ { "SQL_NO_FCACHE", SYM(SQL_NO_FCACHE_SYM)},
137 { "SQL_SMALL_RESULT", SYM(SQL_SMALL_RESULT)},
138 { "SQL_THREAD", SYM(SQL_THREAD)},
139 { "SQL_TSI_SECOND", SYM(SECOND_SYM)},
db82db79
AM
140--- a/sql/mysqld.h
141+++ b/sql/mysqld.h
29ffd636 142@@ -195,6 +195,8 @@
b4e1fa2c 143 extern char language[FN_REFLEN];
adf0fb13
AM
144 extern "C" MYSQL_PLUGIN_IMPORT ulong server_id;
145 extern ulong concurrency;
b4e1fa2c
AM
146+/* flashcache */
147+extern int cachedev_fd;
148 extern time_t server_start_time, flush_status_time;
149 extern char *opt_mysql_tmpdir, mysql_charsets_dir[];
150 extern int mysql_unpacked_real_data_home_len;
db82db79
AM
151--- a/sql/mysqld.cc
152+++ b/sql/mysqld.cc
b4e1fa2c
AM
153@@ -86,6 +86,11 @@
154 #ifdef HAVE_SYS_PRCTL_H
155 #include <sys/prctl.h>
156 #endif
157+#if defined(__linux__)
158+#include <mntent.h>
159+#include <sys/statfs.h>
160+#include "flashcache_ioctl.h"
161+#endif//__linux__
162
163 #include <thr_alarm.h>
164 #include <ft_global.h>
29ffd636 165@@ -491,6 +496,11 @@
b4e1fa2c 166 ulong binlog_cache_use= 0, binlog_cache_disk_use= 0;
d8778560 167 ulong binlog_stmt_cache_use= 0, binlog_stmt_cache_disk_use= 0;
b4e1fa2c
AM
168 ulong max_connections, max_connect_errors;
169+
170+/* flashcache */
171+int cachedev_fd;
172+my_bool cachedev_enabled= FALSE;
173+
11822e22
AM
174 /*
175 Maximum length of parameter value which can be set through
176 mysql_send_long_data() call.
29ffd636 177@@ -4102,6 +4112,97 @@
b4e1fa2c
AM
178 #define decrement_handler_count()
179 #endif /* defined(_WIN32) || defined(HAVE_SMEM) */
180
181+#if defined(__linux__)
182+/*
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.
186+ */
187+static void init_cachedev(void)
188+{
189+ struct statfs stfs_data_home_dir;
190+ struct statfs stfs;
191+ struct mntent *ent;
192+ pid_t pid = getpid();
193+ FILE *mounts;
194+ const char *error_message= NULL;
195+
196+ // disabled by default
197+ cachedev_fd = -1;
198+ cachedev_enabled= FALSE;
199+
200+ if (!mysql_data_home)
201+ {
202+ error_message= "mysql_data_home not set";
203+ goto epilogue;
204+ }
205+
206+ if (statfs(mysql_data_home, &stfs_data_home_dir) < 0)
207+ {
208+ error_message= "statfs failed";
209+ goto epilogue;
210+ }
211+
212+ mounts = setmntent("/etc/mtab", "r");
213+ if (mounts == NULL)
214+ {
215+ error_message= "setmntent failed";
216+ goto epilogue;
217+ }
218+
219+ while ((ent = getmntent(mounts)) != NULL)
220+ {
221+ if (statfs(ent->mnt_dir, &stfs) < 0)
222+ continue;
223+ if (memcmp(&stfs.f_fsid, &stfs_data_home_dir.f_fsid, sizeof(fsid_t)) == 0)
224+ break;
225+ }
226+ endmntent(mounts);
227+
228+ if (ent == NULL)
229+ {
230+ error_message= "getmntent loop failed";
231+ goto epilogue;
232+ }
233+
234+ cachedev_fd = open(ent->mnt_fsname, O_RDONLY);
235+ if (cachedev_fd < 0)
236+ {
237+ error_message= "open flash device failed";
238+ goto epilogue;
239+ }
240+
241+ /* cleanup previous whitelistings */
242+ if (ioctl(cachedev_fd, FLASHCACHEDELALLWHITELIST, &pid) < 0)
243+ {
244+ close(cachedev_fd);
245+ cachedev_fd = -1;
246+ error_message= "ioctl failed";
247+ } else {
248+ ioctl(cachedev_fd, FLASHCACHEADDWHITELIST, &pid);
249+ }
250+
251+epilogue:
252+ sql_print_information("Flashcache bypass: %s",
253+ (cachedev_fd > 0) ? "enabled" : "disabled");
254+ if (error_message)
255+ sql_print_information("Flashcache setup error is : %s\n", error_message);
256+ else
257+ cachedev_enabled= TRUE;
258+
259+}
260+
261+static void cleanup_cachedev(void)
262+{
263+ pid_t pid = getpid();
264+
265+ if (cachedev_enabled) {
266+ ioctl(cachedev_fd, FLASHCACHEDELWHITELIST, &pid);
267+ close(cachedev_fd);
268+ cachedev_fd = -1;
269+ }
270+}
271+#endif//__linux__
272
273 #ifndef EMBEDDED_LIBRARY
274 #ifndef DBUG_OFF
29ffd636 275@@ -4363,6 +4464,10 @@
b4e1fa2c
AM
276 test_lc_time_sz();
277 #endif
278
279+#if defined(__linux__)
280+ init_cachedev();
281+#endif//__linux__
282+
283 /*
284 We have enough space for fiddling with the argv, continue
285 */
29ffd636 286@@ -4574,6 +4679,10 @@
b4e1fa2c
AM
287 }
288 #endif
289 clean_up(1);
290+#if defined(__linux__)
291+ cleanup_cachedev();
292+#endif//__linux__
293+
294 mysqld_exit(0);
295 }
296
29ffd636 297@@ -6421,6 +6530,7 @@
b4e1fa2c
AM
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},
db82db79
AM
305--- a/sql/sql_lex.cc
306+++ b/sql/sql_lex.cc
13ceb006 307@@ -390,6 +390,7 @@
b4e1fa2c
AM
308 lex->describe= 0;
309 lex->subqueries= FALSE;
d8778560 310 lex->context_analysis_only= 0;
b4e1fa2c
AM
311+ lex->disable_flashcache= FALSE;
312 lex->derived_tables= 0;
313 lex->safe_to_cache_query= 1;
314 lex->leaf_tables_insert= 0;
db82db79
AM
315--- a/sql/sql_lex.h
316+++ b/sql/sql_lex.h
29ffd636 317@@ -2346,6 +2346,7 @@
d8778560
AM
318
319 enum enum_yes_no_unknown tx_chain, tx_release;
b4e1fa2c
AM
320 bool safe_to_cache_query;
321+ bool disable_flashcache;
322 bool subqueries, ignore;
323 st_parsing_options parsing_options;
324 Alter_info alter_info;
db82db79
AM
325--- a/sql/sql_select.cc
326+++ b/sql/sql_select.cc
b4e1fa2c
AM
327@@ -55,6 +55,12 @@
328
329 #define PREV_BITS(type,A) ((type) (((type) 1 << (A)) -1))
330
331+#include <sys/syscall.h>
332+#include <sys/ioctl.h>
333+#if defined(__linux__)
334+#include "flashcache_ioctl.h"
335+#endif//__linux__
336+
337 const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
338 "MAYBE_REF","ALL","range","index","fulltext",
339 "ref_or_null","unique_subquery","index_subquery",
340@@ -266,8 +272,17 @@
341 ulong setup_tables_done_option)
342 {
343 bool res;
344+ pid_t pid;
345 register SELECT_LEX *select_lex = &lex->select_lex;
346 DBUG_ENTER("handle_select");
347+#if defined(__linux__)
348+ if(lex->disable_flashcache && cachedev_fd > 0)
349+ {
350+ pid = syscall(SYS_gettid);
351+ ioctl(cachedev_fd, FLASHCACHEADDNCPID, &pid);
352+ }
353+#endif//__linux__
354+
355 MYSQL_SELECT_START(thd->query());
356
357 if (select_lex->master_unit()->is_union() ||
358@@ -302,6 +317,12 @@
359 if (unlikely(res))
360 result->abort_result_set();
361
362+#if defined(__linux__)
363+ if (lex->disable_flashcache && cachedev_fd > 0)
364+ {
365+ ioctl(cachedev_fd, FLASHCACHEDELNCPID, &pid);
366+ }
367+#endif//__linux__
368 MYSQL_SELECT_DONE((int) res, (ulong) thd->limit_found_rows);
369 DBUG_RETURN(res);
370 }
db82db79
AM
371--- a/sql/sql_yacc.yy
372+++ b/sql/sql_yacc.yy
373@@ -1284,6 +1284,7 @@
b4e1fa2c
AM
374 %token SQL_CACHE_SYM
375 %token SQL_CALC_FOUND_ROWS
376 %token SQL_NO_CACHE_SYM
377+%token SQL_NO_FCACHE_SYM
378 %token SQL_SMALL_RESULT
379 %token SQL_SYM /* SQL-2003-R */
380 %token SQL_THREAD
29ffd636 381@@ -7362,6 +7363,10 @@
b4e1fa2c
AM
382 Lex->select_lex.sql_cache= SELECT_LEX::SQL_NO_CACHE;
383 }
384 }
385+ | SQL_NO_FCACHE_SYM
386+ {
387+ Lex->disable_flashcache= TRUE;
388+ }
389 | SQL_CACHE_SYM
390 {
391 /*
48b678b4
AM
392--- /dev/null
393+++ b/mysql-test/r/percona_sql_no_fcache.result
394@@ -0,0 +1,12 @@
395+drop table if exists t1;
396+create table t (a int not null);
397+insert into t values (1),(2),(3);
398+SELECT SQL_NO_FCACHE SLEEP(0);
399+SLEEP(0)
400+0
401+SELECT /*!40001 SQL_NO_CACHE */ /*!50084 SQL_NO_FCACHE */ * FROM t;
402+a
403+1
404+2
405+3
406+DROP TABLE t;
407--- /dev/null
408+++ b/mysql-test/t/percona_sql_no_fcache.test
409@@ -0,0 +1,11 @@
410+--disable_warnings
411+drop table if exists t1;
412+--enable_warnings
413+
414+create table t (a int not null);
415+insert into t values (1),(2),(3);
416+
417+SELECT SQL_NO_FCACHE SLEEP(0);
418+SELECT /*!40001 SQL_NO_CACHE */ /*!50084 SQL_NO_FCACHE */ * FROM t;
419+
420+DROP TABLE t;
This page took 0.146418 seconds and 4 git commands to generate.