File: memcached_mysql.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (228 lines) | stat: -rw-r--r-- 6,846 bytes parent folder | download
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
/***********************************************************************

Copyright (c) 2011, 2025, Oracle and/or its affiliates.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.

This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation.  The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.

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, version 2.0, for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

***********************************************************************/

/**************************************************//**
@file memcached_mysql.cc
InnoDB Memcached Plugin

Created 04/12/2011 Jimmy Yang
*******************************************************/

#include "memcached_mysql.h"
#include <ctype.h>
#include <mysql_version.h>
#include <stdlib.h>
#include "plugin.h"
#include "sql/sql_error.h"
#include "sql/sql_plugin.h"

/** Configuration info passed to memcached, including
the name of our Memcached InnoDB engine and memcached configure
string to be loaded by memcached. */
struct mysql_memcached_context
{
	pthread_t		memcached_thread;
	memcached_context_t	memcached_conf;
};

/** Variables for configure options */
static char*	mci_engine_library = NULL;
static char*	mci_eng_lib_path = NULL;
static char*	mci_memcached_option = NULL;
static unsigned int mci_r_batch_size = 1048576;
static unsigned int mci_w_batch_size = 32;
static bool	mci_enable_binlog = false;

static MYSQL_SYSVAR_STR(engine_lib_name, mci_engine_library,
			PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC |
                        PLUGIN_VAR_NOPERSIST,
			"memcached engine library name", NULL, NULL,
			"innodb_engine.so");

static MYSQL_SYSVAR_STR(engine_lib_path, mci_eng_lib_path,
			PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC |
                        PLUGIN_VAR_NOPERSIST,
			"memcached engine library path", NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(option, mci_memcached_option,
			PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC |
                        PLUGIN_VAR_NOPERSIST,
			"memcached option string", NULL, NULL, NULL);

static MYSQL_SYSVAR_UINT(r_batch_size, mci_r_batch_size,
			 PLUGIN_VAR_READONLY,
			 "read batch commit size", 0, 0, 1,
			 1, 1073741824, 0);

static MYSQL_SYSVAR_UINT(w_batch_size, mci_w_batch_size,
			 PLUGIN_VAR_READONLY,
			 "write batch commit size", 0, 0, 1,
			 1, 1048576, 0);

static MYSQL_SYSVAR_BOOL(enable_binlog, mci_enable_binlog,
			 PLUGIN_VAR_READONLY,
			 "whether to enable binlog",
			 NULL, NULL, false);

static SYS_VAR *daemon_memcached_sys_var[] = {
	MYSQL_SYSVAR(engine_lib_name),
	MYSQL_SYSVAR(engine_lib_path),
	MYSQL_SYSVAR(option),
	MYSQL_SYSVAR(r_batch_size),
	MYSQL_SYSVAR(w_batch_size),
	MYSQL_SYSVAR(enable_binlog),
	0
};

THD *thd_get_current_thd();  // from sql_class.cc

static void emit_deprecation_message() {
	push_deprecated_warn_no_replacement(thd_get_current_thd(),
		"InnoDB Memcached Plugin");
}

static int daemon_memcached_plugin_deinit(void *p)
{
	struct st_plugin_int*		plugin = (struct st_plugin_int *)p;
	struct mysql_memcached_context*	con = NULL;
	int				loop_count = 0;

	/* If memcached plugin is still initializing, wait for a while.*/
	if(!shutdown_complete()){
		while (!init_complete() && loop_count < 15 ) {
			sleep(1);
			loop_count++;
		}

		if (!init_complete()) {
			fprintf(stderr," InnoDB_Memcached: Memcached plugin is still"
				" initializing. It cannot be shut down now.\n");
			return(0);
		}
	}

	if (!shutdown_complete()) {
		shutdown_server();
	}

	loop_count = 0;
	while (!shutdown_complete() && loop_count < 25) {
		sleep(2);
		loop_count++;
	}

	if(!shutdown_complete()) {
		fprintf(stderr," InnoDB_Memcached: Waited for 50 seconds"
			" for memcached thread to exit. Now force terminating"
			" the thread\n");
	}

	con = (struct mysql_memcached_context*) (plugin->data);

	pthread_cancel(con->memcached_thread);

	if (con->memcached_conf.m_engine_library) {
		my_free(con->memcached_conf.m_engine_library);
	}

	my_free(con);

	return(0);
}

static int daemon_memcached_plugin_init(void *p)
{
	struct mysql_memcached_context*	con;
	pthread_attr_t			attr;
	struct st_plugin_int*		plugin = (struct st_plugin_int *)p;

	emit_deprecation_message();

	con = (mysql_memcached_context*) my_malloc(PSI_INSTRUMENT_ME,
                                                   sizeof(*con), MYF(0));

	if (mci_engine_library) {
		char*	lib_path = (mci_eng_lib_path)
					? mci_eng_lib_path : opt_plugin_dir;
		int	lib_len = strlen(lib_path)
				  + strlen(mci_engine_library)
				  + strlen(FN_DIRSEP) + 1;

		con->memcached_conf.m_engine_library = (char*) my_malloc(
                        PSI_INSTRUMENT_ME,
			lib_len, MYF(0));

		strxmov(con->memcached_conf.m_engine_library, lib_path,
			FN_DIRSEP, mci_engine_library, NullS);
	} else {
		con->memcached_conf.m_engine_library = NULL;
	}

	con->memcached_conf.m_mem_option = mci_memcached_option;
	con->memcached_conf.m_innodb_api_cb = plugin->data;
	con->memcached_conf.m_r_batch_size = mci_r_batch_size;
	con->memcached_conf.m_w_batch_size = mci_w_batch_size;
	con->memcached_conf.m_enable_binlog = mci_enable_binlog;

	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

	/* now create the thread */
	if (pthread_create(&con->memcached_thread, &attr,
			   daemon_memcached_main,
			   (void *)&con->memcached_conf) != 0)
	{
		fprintf(stderr,"Could not create memcached daemon thread!\n");
		exit(0);
	}

	plugin->data= (void *)con;

	return(0);
}

struct st_mysql_daemon daemon_memcached_plugin =
	{MYSQL_DAEMON_INTERFACE_VERSION};

mysql_declare_plugin(daemon_memcached)
{
	MYSQL_DAEMON_PLUGIN,
	&daemon_memcached_plugin,
	"daemon_memcached",
	PLUGIN_AUTHOR_ORACLE,
	"Memcached Daemon",
	PLUGIN_LICENSE_GPL,
	daemon_memcached_plugin_init,	/* Plugin Init */
	NULL,	                        /* Plugin Check uninstall */
	daemon_memcached_plugin_deinit,	/* Plugin Deinit */
	0x0100 /* 1.0 */,
	NULL,				/* status variables */
	daemon_memcached_sys_var,	/* system variables */
	NULL,				/* config options */
	0				/* flags */
}
mysql_declare_plugin_end;