MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
memcached_mysql.cc
Go to the documentation of this file.
1 /***********************************************************************
2 
3 Copyright (c) 2012, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
8 
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17 
18 ***********************************************************************/
19 
20 /**************************************************/
27 #include "memcached_mysql.h"
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <mysql_version.h>
31 #include "sql_plugin.h"
32 
37 {
38  pthread_t memcached_thread;
39  memcached_context_t memcached_conf;
40 };
41 
43 static char* mci_engine_library = NULL;
44 static char* mci_eng_lib_path = NULL;
45 static char* mci_memcached_option = NULL;
46 static unsigned int mci_r_batch_size = 1048576;
47 static unsigned int mci_w_batch_size = 32;
48 static my_bool mci_enable_binlog = false;
49 
50 static MYSQL_SYSVAR_STR(engine_lib_name, mci_engine_library,
51  PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC,
52  "memcached engine library name", NULL, NULL,
53  "innodb_engine.so");
54 
55 static MYSQL_SYSVAR_STR(engine_lib_path, mci_eng_lib_path,
56  PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC,
57  "memcached engine library path", NULL, NULL, NULL);
58 
59 static MYSQL_SYSVAR_STR(option, mci_memcached_option,
60  PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC,
61  "memcached option string", NULL, NULL, NULL);
62 
63 static MYSQL_SYSVAR_UINT(r_batch_size, mci_r_batch_size,
64  PLUGIN_VAR_READONLY,
65  "read batch commit size", 0, 0, 1,
66  1, 1073741824, 0);
67 
68 static MYSQL_SYSVAR_UINT(w_batch_size, mci_w_batch_size,
69  PLUGIN_VAR_READONLY,
70  "write batch commit size", 0, 0, 1,
71  1, 1048576, 0);
72 
73 static MYSQL_SYSVAR_BOOL(enable_binlog, mci_enable_binlog,
74  PLUGIN_VAR_READONLY,
75  "whether to enable binlog",
76  NULL, NULL, FALSE);
77 
78 static struct st_mysql_sys_var *daemon_memcached_sys_var[] = {
79  MYSQL_SYSVAR(engine_lib_name),
80  MYSQL_SYSVAR(engine_lib_path),
81  MYSQL_SYSVAR(option),
82  MYSQL_SYSVAR(r_batch_size),
83  MYSQL_SYSVAR(w_batch_size),
84  MYSQL_SYSVAR(enable_binlog),
85  0
86 };
87 
88 static int daemon_memcached_plugin_deinit(void *p)
89 {
90  struct st_plugin_int* plugin = (struct st_plugin_int *)p;
91  struct mysql_memcached_context* con = NULL;
92  int loop_count = 0;
93 
94 
95  if (!shutdown_complete()) {
96  shutdown_server();
97  }
98 
99  while (!shutdown_complete() && loop_count < 25) {
100  sleep(2);
101  loop_count++;
102  }
103 
104  if(!shutdown_complete()) {
105  fprintf(stderr," InnoDB_Memcached: Waited for 50 seconds"
106  " for memcached thread to exit. Now force terminating"
107  " the thread\n");
108  }
109 
110  con = (struct mysql_memcached_context*) (plugin->data);
111 
112  pthread_cancel(con->memcached_thread);
113 
114  if (con->memcached_conf.m_engine_library) {
115  my_free(con->memcached_conf.m_engine_library);
116  }
117 
118  my_free(con);
119 
120  return(0);
121 }
122 
123 static int daemon_memcached_plugin_init(void *p)
124 {
125  struct mysql_memcached_context* con;
126  pthread_attr_t attr;
127  struct st_plugin_int* plugin = (struct st_plugin_int *)p;
128 
129  con = (mysql_memcached_context*) my_malloc(sizeof(*con), MYF(0));
130 
131  if (mci_engine_library) {
132  char* lib_path = (mci_eng_lib_path)
133  ? mci_eng_lib_path : opt_plugin_dir;
134  int lib_len = strlen(lib_path)
135  + strlen(mci_engine_library)
136  + strlen(FN_DIRSEP) + 1;
137 
138  con->memcached_conf.m_engine_library = (char*) my_malloc(
139  lib_len, MYF(0));
140 
141  strxmov(con->memcached_conf.m_engine_library, lib_path,
142  FN_DIRSEP, mci_engine_library, NullS);
143  } else {
144  con->memcached_conf.m_engine_library = NULL;
145  }
146 
147  con->memcached_conf.m_mem_option = mci_memcached_option;
148  con->memcached_conf.m_innodb_api_cb = plugin->data;
149  con->memcached_conf.m_r_batch_size = mci_r_batch_size;
150  con->memcached_conf.m_w_batch_size = mci_w_batch_size;
151  con->memcached_conf.m_enable_binlog = mci_enable_binlog;
152 
153  pthread_attr_init(&attr);
154  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
155 
156  /* now create the thread */
157  if (pthread_create(&con->memcached_thread, &attr,
158  daemon_memcached_main,
159  (void *)&con->memcached_conf) != 0)
160  {
161  fprintf(stderr,"Could not create memcached daemon thread!\n");
162  exit(0);
163  }
164 
165  plugin->data= (void *)con;
166 
167  return(0);
168 }
169 
170 struct st_mysql_daemon daemon_memcached_plugin =
171  {MYSQL_DAEMON_INTERFACE_VERSION};
172 
173 mysql_declare_plugin(daemon_memcached)
174 {
175  MYSQL_DAEMON_PLUGIN,
176  &daemon_memcached_plugin,
177  "daemon_memcached",
178  "Oracle Corporation",
179  "Memcached Daemon",
180  PLUGIN_LICENSE_GPL,
181  daemon_memcached_plugin_init, /* Plugin Init */
182  daemon_memcached_plugin_deinit, /* Plugin Deinit */
183  0x0100 /* 1.0 */,
184  NULL, /* status variables */
185  daemon_memcached_sys_var, /* system variables */
186  NULL /* config options */
187 }
188 mysql_declare_plugin_end;