27
27
#include <algorithm>
28
28
#include <boost/program_options.hpp>
29
29
#include <drizzled/module/option_map.h>
30
#include "drizzled/util/tokenize.h"
31
30
#include "errmsg.h"
32
31
#include "mysql_protocol.h"
33
32
#include "mysql_password.h"
34
33
#include "options.h"
35
#include "table_function.h"
37
#define PROTOCOL_VERSION 10
39
35
namespace po= boost::program_options;
40
36
using namespace std;
41
37
using namespace drizzled;
43
namespace drizzle_plugin
39
#define PROTOCOL_VERSION 10
43
extern uint32_t global_thread_id;
46
std::vector<std::string> ClientMySQLProtocol::mysql_admin_ip_addresses;
47
46
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
49
static port_constraint port;
50
static timeout_constraint connect_timeout;
51
static timeout_constraint read_timeout;
52
static timeout_constraint write_timeout;
53
static retry_constraint retry_count;
54
static buffer_constraint buffer_length;
48
static uint32_t connect_timeout;
49
static uint32_t read_timeout;
50
static uint32_t write_timeout;
51
static uint32_t retry_count;
52
static uint32_t buffer_length;
53
static char* bind_address;
56
54
static uint32_t random_seed1;
57
55
static uint32_t random_seed2;
58
56
static const uint32_t random_max= 0x3FFFFFFF;
59
57
static const double random_max_double= (double)0x3FFFFFFF;
62
ProtocolCounters *ListenMySQLProtocol::mysql_counters= new ProtocolCounters();
64
ListenMySQLProtocol::~ListenMySQLProtocol()
67
const std::string ListenMySQLProtocol::getHost(void) const
59
const char* ListenMySQLProtocol::getHost(void) const
72
64
in_port_t ListenMySQLProtocol::getPort(void) const
66
return (in_port_t) port;
77
69
plugin::Client *ListenMySQLProtocol::getClient(int fd)
146
135
drizzleclient_net_close(&net);
147
136
drizzleclient_net_end(&net);
148
if (is_admin_connection)
149
counters->adminConnected.decrement();
151
counters->connected.decrement();
155
140
bool ClientMySQLProtocol::authenticate()
157
142
bool connection_is_valid;
158
if (is_admin_connection)
160
counters->adminConnectionCount.increment();
161
counters->adminConnected.increment();
165
counters->connectionCount.increment();
166
counters->connected.increment();
169
144
/* Use "connect_timeout" value during connection phase */
170
drizzleclient_net_set_read_timeout(&net, connect_timeout.get());
171
drizzleclient_net_set_write_timeout(&net, connect_timeout.get());
145
drizzleclient_net_set_read_timeout(&net, connect_timeout);
146
drizzleclient_net_set_write_timeout(&net, connect_timeout);
173
148
connection_is_valid= checkConnection();
175
150
if (connection_is_valid)
177
if (not is_admin_connection and (counters->connected > counters->max_connections))
179
std::string errmsg(ER(ER_CON_COUNT_ERROR));
180
sendError(ER_CON_COUNT_ERROR, errmsg.c_str());
181
counters->failedConnections.increment();
190
154
sendError(session->main_da.sql_errno(), session->main_da.message());
191
counters->failedConnections.increment();
195
158
/* Connect completed, set read/write timeouts back to default */
196
drizzleclient_net_set_read_timeout(&net, read_timeout.get());
197
drizzleclient_net_set_write_timeout(&net, write_timeout.get());
159
drizzleclient_net_set_read_timeout(&net, read_timeout);
160
drizzleclient_net_set_write_timeout(&net, write_timeout);
967
892
random_seed2= (seed_time / 2) % random_max;
969
894
const module::option_map &vm= context.getOptions();
895
if (vm.count("port"))
899
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value of port\n"));
904
if (vm.count("connect-timeout"))
906
if (connect_timeout < 1 || connect_timeout > 300)
908
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for connect_timeout\n"));
913
if (vm.count("read-timeout"))
915
if (read_timeout < 1 || read_timeout > 300)
917
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for read_timeout\n"));
922
if (vm.count("write-timeout"))
924
if (write_timeout < 1 || write_timeout > 300)
926
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for write_timeout\n"));
931
if (vm.count("retry-count"))
933
if (retry_count < 1 || retry_count > 100)
935
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for retry_count"));
940
if (vm.count("buffer-length"))
942
if (buffer_length < 1024 || buffer_length > 1024*1024)
944
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for buffer_length\n"));
949
if (vm.count("bind-address"))
951
bind_address= strdup(vm["bind-address"].as<string>().c_str());
971
959
mysql_password= new plugin::Create_function<MySQLPassword>(MySQLPasswordName);
972
960
context.add(mysql_password);
974
listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
962
listen_obj= new ListenMySQLProtocol("mysql_protocol", true);
975
963
context.add(listen_obj);
976
context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
977
context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
978
context.registerVariable(new sys_var_constrained_value<uint32_t>("read_timeout", read_timeout));
979
context.registerVariable(new sys_var_constrained_value<uint32_t>("write_timeout", write_timeout));
980
context.registerVariable(new sys_var_constrained_value<uint32_t>("retry_count", retry_count));
981
context.registerVariable(new sys_var_constrained_value<uint32_t>("buffer_length", buffer_length));
982
context.registerVariable(new sys_var_const_string_val("bind_address",
983
vm["bind-address"].as<std::string>()));
985
context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters->max_connections));
968
static DRIZZLE_SYSVAR_UINT(port, port, PLUGIN_VAR_RQCMDARG,
969
N_("Port number to use for connection or 0 for default to with MySQL "
971
NULL, NULL, 3306, 0, 65535, 0);
972
static DRIZZLE_SYSVAR_UINT(connect_timeout, connect_timeout,
973
PLUGIN_VAR_RQCMDARG, N_("Connect Timeout."),
974
NULL, NULL, 10, 1, 300, 0);
975
static DRIZZLE_SYSVAR_UINT(read_timeout, read_timeout, PLUGIN_VAR_RQCMDARG,
976
N_("Read Timeout."), NULL, NULL, 30, 1, 300, 0);
977
static DRIZZLE_SYSVAR_UINT(write_timeout, write_timeout, PLUGIN_VAR_RQCMDARG,
978
N_("Write Timeout."), NULL, NULL, 60, 1, 300, 0);
979
static DRIZZLE_SYSVAR_UINT(retry_count, retry_count, PLUGIN_VAR_RQCMDARG,
980
N_("Retry Count."), NULL, NULL, 10, 1, 100, 0);
981
static DRIZZLE_SYSVAR_UINT(buffer_length, buffer_length, PLUGIN_VAR_RQCMDARG,
982
N_("Buffer length."), NULL, NULL, 16384, 1024,
984
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
985
N_("Address to bind to."), NULL, NULL, NULL);
990
987
static void init_options(drizzled::module::option_context &context)
993
po::value<port_constraint>(&port)->default_value(3306),
990
po::value<uint32_t>(&port)->default_value(3306),
994
991
N_("Port number to use for connection or 0 for default to with MySQL "
996
993
context("connect-timeout",
997
po::value<timeout_constraint>(&connect_timeout)->default_value(10),
994
po::value<uint32_t>(&connect_timeout)->default_value(10),
998
995
N_("Connect Timeout."));
999
996
context("read-timeout",
1000
po::value<timeout_constraint>(&read_timeout)->default_value(30),
997
po::value<uint32_t>(&read_timeout)->default_value(30),
1001
998
N_("Read Timeout."));
1002
999
context("write-timeout",
1003
po::value<timeout_constraint>(&write_timeout)->default_value(60),
1000
po::value<uint32_t>(&write_timeout)->default_value(60),
1004
1001
N_("Write Timeout."));
1005
1002
context("retry-count",
1006
po::value<retry_constraint>(&retry_count)->default_value(10),
1003
po::value<uint32_t>(&retry_count)->default_value(10),
1007
1004
N_("Retry Count."));
1008
1005
context("buffer-length",
1009
po::value<buffer_constraint>(&buffer_length)->default_value(16384),
1006
po::value<uint32_t>(&buffer_length)->default_value(16384),
1010
1007
N_("Buffer length."));
1011
1008
context("bind-address",
1012
po::value<string>()->default_value(""),
1009
po::value<string>(),
1013
1010
N_("Address to bind to."));
1014
context("max-connections",
1015
po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters->max_connections)->default_value(1000),
1016
N_("Maximum simultaneous connections."));
1017
context("admin-ip-addresses",
1018
po::value<vector<string> >()->composing()->notifier(&ClientMySQLProtocol::mysql_compose_ip_addresses),
1019
N_("A restrictive IP address list for incoming admin connections."));
1022
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
1024
var->type= SHOW_LONGLONG;
1026
*((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connectionCount;
1030
static int mysql_protocol_connected_count_func(drizzle_show_var *var, char *buff)
1032
var->type= SHOW_LONGLONG;
1034
*((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connected;
1038
static int mysql_protocol_failed_count_func(drizzle_show_var *var, char *buff)
1040
var->type= SHOW_LONGLONG;
1042
*((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->failedConnections;
1046
static st_show_var_func_container mysql_protocol_connection_count=
1047
{ &mysql_protocol_connection_count_func };
1049
static st_show_var_func_container mysql_protocol_connected_count=
1050
{ &mysql_protocol_connected_count_func };
1052
static st_show_var_func_container mysql_protocol_failed_count=
1053
{ &mysql_protocol_failed_count_func };
1055
static drizzle_show_var mysql_protocol_status_variables[]= {
1057
(char*) &mysql_protocol_connection_count, SHOW_FUNC},
1059
(char*) &mysql_protocol_connected_count, SHOW_FUNC},
1060
{"Failed_connections",
1061
(char*) &mysql_protocol_failed_count, SHOW_FUNC},
1062
{NULL, NULL, SHOW_LONGLONG}
1013
static drizzle_sys_var* sys_variables[]= {
1014
DRIZZLE_SYSVAR(port),
1015
DRIZZLE_SYSVAR(connect_timeout),
1016
DRIZZLE_SYSVAR(read_timeout),
1017
DRIZZLE_SYSVAR(write_timeout),
1018
DRIZZLE_SYSVAR(retry_count),
1019
DRIZZLE_SYSVAR(buffer_length),
1020
DRIZZLE_SYSVAR(bind_address),
1065
MysqlProtocolStatus::Generator::Generator(drizzled::Field **fields) :
1066
plugin::TableFunction::Generator(fields)
1068
status_var_ptr= mysql_protocol_status_variables;
1071
bool MysqlProtocolStatus::Generator::populate()
1073
MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
1074
char * const buff= (char *) &buff_data;
1075
drizzle_show_var tmp;
1077
if (status_var_ptr->name)
1079
std::ostringstream oss;
1080
string return_value;
1084
push(status_var_ptr->name);
1086
if (status_var_ptr->type == SHOW_FUNC)
1088
((drizzle_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
1094
value= status_var_ptr->value;
1095
type= status_var_ptr->type;
1101
oss << *(uint64_t*) value;
1102
return_value= oss.str();
1107
if (return_value.length())
1119
} /* namespace drizzle_plugin */
1121
1024
DRIZZLE_DECLARE_PLUGIN
1123
1026
DRIZZLE_VERSION_ID,