~sergei.glushchenko/percona-server/55-tp

« back to all changes in this revision

Viewing changes to sql_no_fcache.patch

  • Committer: Oleg Tsarev
  • Date: 2010-12-09 18:30:58 UTC
  • Revision ID: oleg.tsarev@percona.com-20101209183058-1mq1qrgjjkz3qxof
propogate Oleg's patches

Show diffs side-by-side

added added

removed removed

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