1
2 // vim:sw=2:ai
3
4 /*
5 * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved.
6 * See COPYRIGHT.txt for details.
7 */
8
9 #include <memory>
10 #include <string>
11 #include <stdio.h>
12
13 #include "config.hpp"
14 #include "hstcpsvr.hpp"
15 #include "string_util.hpp"
16 #include "mysql_incl.hpp"
17
18 #define DBG_LOG \
19 if (dena::verbose_level >= 100) { \
20 fprintf(stderr, "%s %p\n", __PRETTY_FUNCTION__, this); \
21 }
22 #define DBG_DO(x) if (dena::verbose_level >= 100) { x; }
23
24 #define DBG_DIR(x)
25
26 using namespace dena;
27
28 static char *handlersocket_address = 0;
29 static char *handlersocket_port = 0;
30 static char *handlersocket_port_wr = 0;
31 static unsigned int handlersocket_epoll = 1;
32 static unsigned int handlersocket_threads = 32;
33 static unsigned int handlersocket_threads_wr = 1;
34 static unsigned int handlersocket_timeout = 30;
35 static unsigned int handlersocket_backlog = 32768;
36 static unsigned int handlersocket_sndbuf = 0;
37 static unsigned int handlersocket_rcvbuf = 0;
38 static unsigned int handlersocket_readsize = 0;
39 static unsigned int handlersocket_accept_balance = 0;
40 static unsigned int handlersocket_wrlock_timeout = 0;
41 static char *handlersocket_plain_secret = 0;
42 static char *handlersocket_plain_secret_wr = 0;
43
44 struct daemon_handlersocket_data {
45 hstcpsvr_ptr hssvr_rd;
46 hstcpsvr_ptr hssvr_wr;
47 };
48
49 static int
daemon_handlersocket_init(void * p)50 daemon_handlersocket_init(void *p)
51 {
52 config conf;
53 conf["use_epoll"] = handlersocket_epoll ? "1" : "0";
54 if (handlersocket_address) {
55 conf["host"] = handlersocket_address;
56 }
57 if (handlersocket_port) {
58 conf["port"] = handlersocket_port;
59 }
60 /*
61 * unix domain socket
62 * conf["host"] = "/";
63 * conf["port"] = "/tmp/handlersocket";
64 */
65 if (handlersocket_threads > 0) {
66 conf["num_threads"] = to_stdstring(handlersocket_threads);
67 } else {
68 conf["num_threads"] = "1";
69 }
70 conf["timeout"] = to_stdstring(handlersocket_timeout);
71 conf["listen_backlog"] = to_stdstring(handlersocket_backlog);
72 conf["sndbuf"] = to_stdstring(handlersocket_sndbuf);
73 conf["rcvbuf"] = to_stdstring(handlersocket_rcvbuf);
74 conf["readsize"] = to_stdstring(handlersocket_readsize);
75 conf["accept_balance"] = to_stdstring(handlersocket_accept_balance);
76 conf["wrlock_timeout"] = to_stdstring(handlersocket_wrlock_timeout);
77 std::auto_ptr<daemon_handlersocket_data> ap(new daemon_handlersocket_data);
78 if (handlersocket_port != 0 && handlersocket_port_wr != handlersocket_port) {
79 conf["port"] = handlersocket_port;
80 if (handlersocket_plain_secret) {
81 conf["plain_secret"] = handlersocket_plain_secret;
82 }
83 ap->hssvr_rd = hstcpsvr_i::create(conf);
84 ap->hssvr_rd->start_listen();
85 } else {
86 DENA_VERBOSE(10, fprintf(stderr, "handlersocket: not listening "
87 "for reads\n"));
88 }
89 if (handlersocket_port_wr != 0) {
90 if (handlersocket_threads_wr > 0) {
91 conf["num_threads"] = to_stdstring(handlersocket_threads_wr);
92 }
93 conf["port"] = handlersocket_port_wr;
94 conf["for_write"] = "1";
95 conf["plain_secret"] = "";
96 if (handlersocket_plain_secret_wr) {
97 conf["plain_secret"] = handlersocket_plain_secret_wr;
98 }
99 ap->hssvr_wr = hstcpsvr_i::create(conf);
100 ap->hssvr_wr->start_listen();
101 } else {
102 DENA_VERBOSE(10, fprintf(stderr, "handlersocket: not listening "
103 "for writes\n"));
104 }
105 st_plugin_int *const plugin = static_cast<st_plugin_int *>(p);
106 plugin->data = ap.release();
107 DENA_VERBOSE(10, fprintf(stderr, "handlersocket: initialized\n"));
108 return 0;
109 }
110
111 static int
daemon_handlersocket_deinit(void * p)112 daemon_handlersocket_deinit(void *p)
113 {
114 DENA_VERBOSE(10, fprintf(stderr, "handlersocket: terminated\n"));
115 st_plugin_int *const plugin = static_cast<st_plugin_int *>(p);
116 daemon_handlersocket_data *ptr =
117 static_cast<daemon_handlersocket_data *>(plugin->data);
118 delete ptr;
119 return 0;
120 }
121
122 static struct st_mysql_daemon daemon_handlersocket_plugin = {
123 MYSQL_DAEMON_INTERFACE_VERSION
124 };
125
126 static MYSQL_SYSVAR_UINT(verbose, dena::verbose_level, 0,
127 "0..10000", 0, 0, 10 /* default */, 0, 10000, 0);
128 static MYSQL_SYSVAR_UINT(epoll, handlersocket_epoll, PLUGIN_VAR_READONLY,
129 "0..1", 0, 0, 1 /* default */, 0, 1, 0);
130 static MYSQL_SYSVAR_STR(address, handlersocket_address,
131 PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC, "", NULL, NULL, NULL);
132 static MYSQL_SYSVAR_STR(port, handlersocket_port,
133 PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC, "", NULL, NULL, NULL);
134 static MYSQL_SYSVAR_STR(port_wr, handlersocket_port_wr,
135 PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC, "", NULL, NULL, NULL);
136 static MYSQL_SYSVAR_UINT(threads, handlersocket_threads, PLUGIN_VAR_READONLY,
137 "1..3000", 0, 0, 16 /* default */, 1, 3000, 0);
138 static MYSQL_SYSVAR_UINT(threads_wr, handlersocket_threads_wr,
139 PLUGIN_VAR_READONLY, "1..3000", 0, 0, 1 /* default */, 1, 3000, 0);
140 static MYSQL_SYSVAR_UINT(timeout, handlersocket_timeout, PLUGIN_VAR_READONLY,
141 "30..3600", 0, 0, 300 /* default */, 30, 3600, 0);
142 static MYSQL_SYSVAR_UINT(backlog, handlersocket_backlog, PLUGIN_VAR_READONLY,
143 "5..1000000", 0, 0, 32768 /* default */, 5, 1000000, 0);
144 static MYSQL_SYSVAR_UINT(sndbuf, handlersocket_sndbuf, PLUGIN_VAR_READONLY,
145 "0..16777216", 0, 0, 0 /* default */, 0, 16777216, 0);
146 static MYSQL_SYSVAR_UINT(rcvbuf, handlersocket_rcvbuf, PLUGIN_VAR_READONLY,
147 "0..16777216", 0, 0, 0 /* default */, 0, 16777216, 0);
148 static MYSQL_SYSVAR_UINT(readsize, handlersocket_readsize, PLUGIN_VAR_READONLY,
149 "0..16777216", 0, 0, 0 /* default */, 0, 16777216, 0);
150 static MYSQL_SYSVAR_UINT(accept_balance, handlersocket_accept_balance,
151 PLUGIN_VAR_READONLY, "0..10000", 0, 0, 0 /* default */, 0, 10000, 0);
152 static MYSQL_SYSVAR_UINT(wrlock_timeout, handlersocket_wrlock_timeout,
153 PLUGIN_VAR_READONLY, "0..3600", 0, 0, 12 /* default */, 0, 3600, 0);
154 static MYSQL_SYSVAR_STR(plain_secret, handlersocket_plain_secret,
155 PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC, "", NULL, NULL, NULL);
156 static MYSQL_SYSVAR_STR(plain_secret_wr, handlersocket_plain_secret_wr,
157 PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC, "", NULL, NULL, NULL);
158
159
160 /* warning: type-punning to incomplete type might break strict-aliasing
161 * rules */
162 static struct st_mysql_sys_var *daemon_handlersocket_system_variables[] = {
163 MYSQL_SYSVAR(verbose),
164 MYSQL_SYSVAR(address),
165 MYSQL_SYSVAR(port),
166 MYSQL_SYSVAR(port_wr),
167 MYSQL_SYSVAR(epoll),
168 MYSQL_SYSVAR(threads),
169 MYSQL_SYSVAR(threads_wr),
170 MYSQL_SYSVAR(timeout),
171 MYSQL_SYSVAR(backlog),
172 MYSQL_SYSVAR(sndbuf),
173 MYSQL_SYSVAR(rcvbuf),
174 MYSQL_SYSVAR(readsize),
175 MYSQL_SYSVAR(accept_balance),
176 MYSQL_SYSVAR(wrlock_timeout),
177 MYSQL_SYSVAR(plain_secret),
178 MYSQL_SYSVAR(plain_secret_wr),
179 0
180 };
181
182 static SHOW_VAR hs_status_variables[] = {
183 {"table_open", (char*) &open_tables_count, SHOW_LONGLONG},
184 {"table_close", (char*) &close_tables_count, SHOW_LONGLONG},
185 {"table_lock", (char*) &lock_tables_count, SHOW_LONGLONG},
186 {"table_unlock", (char*) &unlock_tables_count, SHOW_LONGLONG},
187 #if 0
188 {"index_exec", (char*) &index_exec_count, SHOW_LONGLONG},
189 #endif
190 {NullS, NullS, SHOW_LONG}
191 };
192
show_hs_vars(THD * thd,SHOW_VAR * var,char * buff)193 static int show_hs_vars(THD *thd, SHOW_VAR *var, char *buff)
194 {
195 var->type= SHOW_ARRAY;
196 var->value= (char *) &hs_status_variables;
197 return 0;
198 }
199
200 static SHOW_VAR daemon_handlersocket_status_variables[] = {
201 {"Hs", (char*) show_hs_vars, SHOW_FUNC},
202 {NullS, NullS, SHOW_LONG}
203 };
204
205
mysql_declare_plugin(handlersocket)206 mysql_declare_plugin(handlersocket)
207 {
208 MYSQL_DAEMON_PLUGIN,
209 &daemon_handlersocket_plugin,
210 "handlersocket",
211 "higuchi dot akira at dena dot jp",
212 "",
213 PLUGIN_LICENSE_BSD,
214 daemon_handlersocket_init,
215 daemon_handlersocket_deinit,
216 0x0100 /* 1.0 */,
217 daemon_handlersocket_status_variables,
218 daemon_handlersocket_system_variables,
219 NULL,
220 0,
221 }
222 mysql_declare_plugin_end;
223
224