~ignacio-nin/percona-server/5.1-issue26684

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# name       : sql_no_fcache.patch
# introduced : 12
# maintainer : Oleg
#
#!!! notice !!!
# Any small change to this file in the main branch
# should be done or reviewed by the maintainer!
diff -ruN a/client/mysqldump.c b/client/mysqldump.c
--- a/client/mysqldump.c	2010-07-28 16:47:58.264067653 +0400
+++ b/client/mysqldump.c	2010-07-28 16:47:59.604985656 +0400
@@ -132,6 +132,8 @@
 #endif
 static uint opt_protocol= 0;
 
+static my_bool server_supports_sql_no_fcache= FALSE;
+
 /*
 Dynamic_string wrapper functions. In this file use these
 wrappers, they will terminate the process if there is
@@ -1512,6 +1514,17 @@
     /* Don't switch charsets for 4.1 and earlier.  (bug#34192). */
     server_supports_switching_charsets= FALSE;
   } 
+  
+  /* Check to see if we support SQL_NO_FCACHE on this server. */ 
+  if (mysql_query(mysql, "SELECT SQL_NO_FCACHE NOW()") == 0)
+  {
+    MYSQL_RES *res = mysql_store_result(mysql);
+    if (res)
+    {
+      mysql_free_result(res);
+    }
+    server_supports_sql_no_fcache= TRUE;
+  }
   /*
     As we're going to set SQL_MODE, it would be lost on reconnect, so we
     cannot reconnect.
@@ -3175,7 +3188,12 @@
 
     /* now build the query string */
 
-    dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ * INTO OUTFILE '");
+    dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ ");
+    if (server_supports_sql_no_fcache)
+    {
+      dynstr_append_checked(&query_string, "/*!50084 SQL_NO_FCACHE */ ");
+    }
+    dynstr_append_checked(&query_string, "* INTO OUTFILE '");
     dynstr_append_checked(&query_string, filename);
     dynstr_append_checked(&query_string, "'");
 
@@ -3225,7 +3243,12 @@
       check_io(md_result_file);
     }
     
-    dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ * FROM ");
+    dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ ");
+    if (server_supports_sql_no_fcache)
+    {
+      dynstr_append_checked(&query_string, "/*!50084 SQL_NO_FCACHE */ ");
+    }
+    dynstr_append_checked(&query_string, "* FROM ");
     dynstr_append_checked(&query_string, result_table);
 
     if (where)
diff -ruN a/include/flashcache_ioctl.h b/include/flashcache_ioctl.h
--- a/include/flashcache_ioctl.h	1970-01-01 03:00:00.000000000 +0300
+++ b/include/flashcache_ioctl.h	2010-07-28 16:47:59.744079911 +0400
@@ -0,0 +1,53 @@
+/****************************************************************************
+ *  flashcache_ioctl.h
+ *  FlashCache: Device mapper target for block-level disk caching
+ *
+ *  Copyright 2010 Facebook, Inc.
+ *  Author: Mohan Srinivasan (mohan@facebook.com)
+ *
+ *  Based on DM-Cache:
+ *   Copyright (C) International Business Machines Corp., 2006
+ *   Author: Ming Zhao (mingzhao@ufl.edu)
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; under version 2 of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ ****************************************************************************/
+
+#ifndef FLASHCACHE_IOCTL_H
+#define FLASHCACHE_IOCTL_H
+
+#include <linux/types.h>
+
+#define FLASHCACHE_IOCTL 0xfe
+
+enum {
+       FLASHCACHEADDNCPID_CMD=200,
+       FLASHCACHEDELNCPID_CMD,
+       FLASHCACHEDELNCALL_CMD,
+       FLASHCACHEADDWHITELIST_CMD,
+       FLASHCACHEDELWHITELIST_CMD,
+       FLASHCACHEDELWHITELISTALL_CMD,
+};
+
+#define FLASHCACHEADDNCPID     _IOW(FLASHCACHE_IOCTL, FLASHCACHEADDNCPID_CMD, pid_t)
+#define FLASHCACHEDELNCPID     _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELNCPID_CMD, pid_t)
+#define FLASHCACHEDELNCALL     _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELNCALL_CMD, pid_t)
+
+#define FLASHCACHEADDBLACKLIST         FLASHCACHEADDNCPID
+#define FLASHCACHEDELBLACKLIST         FLASHCACHEDELNCPID
+#define FLASHCACHEDELALLBLACKLIST      FLASHCACHEDELNCALL
+
+#define FLASHCACHEADDWHITELIST         _IOW(FLASHCACHE_IOCTL, FLASHCACHEADDWHITELIST_CMD, pid_t)
+#define FLASHCACHEDELWHITELIST         _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELWHITELIST_CMD, pid_t)
+#define FLASHCACHEDELALLWHITELIST      _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELWHITELISTALL_CMD, pid_t)
+
+#endif
diff -ruN a/mysql-test/r/mysqldump.result b/mysql-test/r/mysqldump.result
--- a/mysql-test/r/mysqldump.result	2010-07-28 16:47:58.334083833 +0400
+++ b/mysql-test/r/mysqldump.result	2010-07-28 16:48:14.805318437 +0400
@@ -1832,7 +1832,7 @@
 # Bug#21288 mysqldump segmentation fault when using --where
 #
 create table t1 (a int);
-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)
+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)
 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
 
 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
diff -ruN a/patch_info/sql_no_fcache.info b/patch_info/sql_no_fcache.info
--- a/patch_info/sql_no_fcache.info	1970-01-01 03:00:00.000000000 +0300
+++ b/patch_info/sql_no_fcache.info	2010-07-28 16:47:59.915439165 +0400
@@ -0,0 +1,6 @@
+File=sql_no_fcache.patch
+Name=Support for flashcache including the SQL_NO_FCACHE option that prevents blocks from being cached during a query.
+Version=1.0
+Author=Facebook
+License=GPL
+Comment=
diff -ruN a/sql/lex.h b/sql/lex.h
--- a/sql/lex.h	2010-07-28 16:47:58.575318748 +0400
+++ b/sql/lex.h	2010-07-28 16:48:00.134078469 +0400
@@ -500,6 +500,7 @@
   { "SQL_CACHE",        SYM(SQL_CACHE_SYM)},
   { "SQL_CALC_FOUND_ROWS", SYM(SQL_CALC_FOUND_ROWS)},
   { "SQL_NO_CACHE",	SYM(SQL_NO_CACHE_SYM)},
+  { "SQL_NO_FCACHE",   SYM(SQL_NO_FCACHE_SYM)},
   { "SQL_SMALL_RESULT", SYM(SQL_SMALL_RESULT)},
   { "SQL_THREAD",	SYM(SQL_THREAD)},
   { "SQL_TSI_FRAC_SECOND", SYM(FRAC_SECOND_SYM)},
diff -ruN a/sql/mysqld.cc b/sql/mysqld.cc
--- a/sql/mysqld.cc	2010-07-28 16:47:58.565318871 +0400
+++ b/sql/mysqld.cc	2010-07-28 16:48:03.004544367 +0400
@@ -45,6 +45,11 @@
 #define OPT_NDB_SHM_DEFAULT 0
 #endif
 #endif
+#if defined(__linux__)
+#include <mntent.h>
+#include <sys/statfs.h>
+#include "flashcache_ioctl.h"
+#endif//__linux__
 
 #ifndef DEFAULT_SKIP_THREAD_PRIORITY
 #define DEFAULT_SKIP_THREAD_PRIORITY 0
@@ -593,6 +598,11 @@
 ulong max_connections, max_connect_errors;
 uint  max_user_connections= 0;
 ulonglong denied_connections = 0;
+
+/* flashcache */
+int cachedev_fd;
+my_bool cachedev_enabled= FALSE;
+
 /**
   Limit of the total number of prepared statements in the server.
   Is necessary to protect the server against out-of-memory attacks.
@@ -4391,6 +4401,97 @@
 }
 #endif//DBUG_OFF
 
+#if defined(__linux__)
+/*
+ * Auto detect if we support flash cache on the host system.
+ * This needs to be called before we setuid away from root
+ * to avoid permission problems on opening the device node.
+ */
+static void init_cachedev(void)
+{
+  struct statfs stfs_data_home_dir;
+  struct statfs stfs;
+  struct mntent *ent;
+  pid_t pid = getpid();
+  FILE *mounts;
+  char *error_message= NULL;
+
+  // disabled by default
+  cachedev_fd = -1;
+  cachedev_enabled= FALSE;
+
+  if (!mysql_data_home)
+  {
+    error_message= "mysql_data_home not set";
+    goto epilogue;
+  }
+
+  if (statfs(mysql_data_home, &stfs_data_home_dir) < 0)
+  {
+    error_message= "statfs failed";
+    goto epilogue;
+  }
+
+  mounts = setmntent("/etc/mtab", "r");
+  if (mounts == NULL)
+  {
+    error_message= "setmntent failed";
+    goto epilogue;
+  }
+
+  while ((ent = getmntent(mounts)) != NULL)
+  {
+    if (statfs(ent->mnt_dir, &stfs) < 0)
+      continue;
+    if (memcmp(&stfs.f_fsid, &stfs_data_home_dir.f_fsid, sizeof(fsid_t)) == 0)
+      break;
+  }
+  endmntent(mounts);
+
+  if (ent == NULL)
+  {
+    error_message= "getmntent loop failed";
+    goto epilogue;
+  }
+
+  cachedev_fd = open(ent->mnt_fsname, O_RDONLY);
+  if (cachedev_fd < 0)
+  {
+    error_message= "open flash device failed";
+    goto epilogue;
+  }
+
+  /* cleanup previous whitelistings */
+  if (ioctl(cachedev_fd, FLASHCACHEDELALLWHITELIST, &pid) < 0)
+  {
+    close(cachedev_fd);
+    cachedev_fd = -1;
+    error_message= "ioctl failed";
+  } else {
+    ioctl(cachedev_fd, FLASHCACHEADDWHITELIST, &pid);
+  }
+
+epilogue:
+  sql_print_information("Flashcache bypass: %s",
+      (cachedev_fd > 0) ? "enabled" : "disabled");
+  if (error_message)
+    sql_print_information("Flashcache setup error is : %s\n", error_message);
+  else
+    cachedev_enabled= TRUE;
+
+}
+
+static void cleanup_cachedev(void)
+{
+  pid_t pid = getpid();
+
+  if (cachedev_enabled) {
+    ioctl(cachedev_fd, FLASHCACHEDELWHITELIST, &pid);
+    close(cachedev_fd);
+    cachedev_fd = -1;
+  }
+}
+#endif//__linux__
 
 #ifdef __WIN__
 int win_main(int argc, char **argv)
@@ -4495,6 +4596,10 @@
   test_lc_time_sz();
 #endif
 
+#if defined(__linux__)
+  init_cachedev();
+#endif//__linux__
+
   /*
     We have enough space for fiddling with the argv, continue
   */
@@ -4696,6 +4801,10 @@
   clean_up_mutexes();
   my_end(opt_endinfo ? MY_CHECK_ERROR | MY_GIVE_INFO : 0);
 
+#if defined(__linux__)
+  cleanup_cachedev();
+#endif//__linux__
+
   exit(0);
   return(0);					/* purecov: deadcode */
 }
@@ -7853,6 +7962,7 @@
   {"Delayed_errors",           (char*) &delayed_insert_errors,  SHOW_LONG},
   {"Delayed_insert_threads",   (char*) &delayed_insert_threads, SHOW_LONG_NOFLUSH},
   {"Delayed_writes",           (char*) &delayed_insert_writes,  SHOW_LONG},
+  {"Flashcache_enabled",       (char*) &cachedev_enabled,       SHOW_BOOL },
   {"Flush_commands",           (char*) &refresh_version,        SHOW_LONG_NOFLUSH},
   {"Handler_commit",           (char*) offsetof(STATUS_VAR, ha_commit_count), SHOW_LONG_STATUS},
   {"Handler_delete",           (char*) offsetof(STATUS_VAR, ha_delete_count), SHOW_LONG_STATUS},
diff -ruN a/sql/mysql_priv.h b/sql/mysql_priv.h
--- a/sql/mysql_priv.h	2010-07-28 16:47:58.575318748 +0400
+++ b/sql/mysql_priv.h	2010-07-28 16:48:05.505952089 +0400
@@ -819,6 +819,8 @@
 */
 extern ulong server_id, concurrency;
 
+/* flashcache */
+extern int cachedev_fd;
 
 typedef my_bool (*qc_engine_callback)(THD *thd, char *table_key,
                                       uint key_length,
diff -ruN a/sql/sql_lex.cc b/sql/sql_lex.cc
--- a/sql/sql_lex.cc	2010-07-28 16:47:58.555318714 +0400
+++ b/sql/sql_lex.cc	2010-07-28 16:48:07.794069239 +0400
@@ -306,6 +306,7 @@
   lex->describe= 0;
   lex->subqueries= FALSE;
   lex->context_analysis_only= 0;
+  lex->disable_flashcache= FALSE;
   lex->derived_tables= 0;
   lex->lock_option= TL_READ;
   lex->safe_to_cache_query= 1;
diff -ruN a/sql/sql_lex.h b/sql/sql_lex.h
--- a/sql/sql_lex.h	2010-07-28 16:47:58.575318748 +0400
+++ b/sql/sql_lex.h	2010-07-28 16:48:08.405691834 +0400
@@ -1722,6 +1722,7 @@
 
   uint8 context_analysis_only;
   bool safe_to_cache_query;
+  bool disable_flashcache;
   bool subqueries, ignore;
   st_parsing_options parsing_options;
   Alter_info alter_info;
diff -ruN a/sql/sql_select.cc b/sql/sql_select.cc
--- a/sql/sql_select.cc	2010-07-28 16:47:58.555318714 +0400
+++ b/sql/sql_select.cc	2010-07-28 16:48:13.414069437 +0400
@@ -37,6 +37,12 @@
 #include <hash.h>
 #include <ft_global.h>
 
+#include <sys/syscall.h>
+#include <sys/ioctl.h>
+#if defined(__linux__)
+#include "flashcache_ioctl.h"
+#endif//__linux__
+
 const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 			      "MAYBE_REF","ALL","range","index","fulltext",
 			      "ref_or_null","unique_subquery","index_subquery",
@@ -239,9 +245,20 @@
                    ulong setup_tables_done_option)
 {
   bool res;
+#if defined(__linux__)
+  pid_t pid;
+#endif
   register SELECT_LEX *select_lex = &lex->select_lex;
   DBUG_ENTER("handle_select");
 
+#if defined(__linux__)
+  if(lex->disable_flashcache && cachedev_fd > 0)
+  {
+    pid = syscall(SYS_gettid);
+    ioctl(cachedev_fd, FLASHCACHEADDNCPID, &pid);
+  }
+#endif//__linux__
+ 
   if (select_lex->master_unit()->is_union() || 
       select_lex->master_unit()->fake_select_lex)
     res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
@@ -274,6 +291,12 @@
   if (unlikely(res))
     result->abort();
 
+#if defined(__linux__)
+  if (lex->disable_flashcache && cachedev_fd > 0)
+  {
+    ioctl(cachedev_fd, FLASHCACHEDELNCPID, &pid);
+  }
+#endif//__linux__ 
   DBUG_RETURN(res);
 }
 
diff -ruN a/sql/sql_yacc.yy b/sql/sql_yacc.yy
--- a/sql/sql_yacc.yy	2010-07-28 16:47:58.565318871 +0400
+++ b/sql/sql_yacc.yy	2010-07-28 16:48:14.205317990 +0400
@@ -1164,6 +1164,7 @@
 %token  SQL_CACHE_SYM
 %token  SQL_CALC_FOUND_ROWS
 %token  SQL_NO_CACHE_SYM
+%token  SQL_NO_FCACHE_SYM
 %token  SQL_SMALL_RESULT
 %token  SQL_SYM                       /* SQL-2003-R */
 %token  SQL_THREAD
@@ -6752,6 +6753,10 @@
             Lex->select_lex.options&= ~OPTION_TO_QUERY_CACHE;
             Lex->select_lex.sql_cache= SELECT_LEX::SQL_NO_CACHE;
           }
+	| SQL_NO_FCACHE_SYM
+	  {
+	    Lex->disable_flashcache= TRUE;
+	  }
         | SQL_CACHE_SYM
           {
             /*