~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/mysql_protocol.cc

  • Committer: Lee Bieber
  • Date: 2010-12-05 19:04:34 UTC
  • mfrom: (1975.1.4 build)
  • Revision ID: kalebral@gmail.com-20101205190434-9mtyc2cfzbd1hbye
Merge Padraig - Fix for query rewrite plugin.
Merge Monty: more plugin_sysvar removal work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#include "options.h"
34
34
#include "table_function.h"
35
35
 
 
36
#define PROTOCOL_VERSION 10
 
37
 
36
38
namespace po= boost::program_options;
37
39
using namespace std;
38
40
using namespace drizzled;
39
41
 
40
 
#define PROTOCOL_VERSION 10
 
42
namespace drizzle_plugin
 
43
{
41
44
 
42
45
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
43
 
static uint32_t port;
44
 
static uint32_t connect_timeout;
45
 
static uint32_t read_timeout;
46
 
static uint32_t write_timeout;
47
 
static uint32_t retry_count;
48
 
static uint32_t buffer_length;
49
 
static char* bind_address;
 
46
 
 
47
static port_constraint port;
 
48
static timeout_constraint connect_timeout;
 
49
static timeout_constraint read_timeout;
 
50
static timeout_constraint write_timeout;
 
51
static retry_constraint retry_count;
 
52
static buffer_constraint buffer_length;
 
53
 
50
54
static uint32_t random_seed1;
51
55
static uint32_t random_seed2;
52
56
static const uint32_t random_max= 0x3FFFFFFF;
53
57
static const double random_max_double= (double)0x3FFFFFFF;
54
58
 
55
 
static plugin::TableFunction* mysql_status_table_function_ptr= NULL;
56
59
 
57
60
ListenMySQLProtocol::~ListenMySQLProtocol()
58
 
{
59
 
  /* This is strdup'd from the options */
60
 
  free(bind_address);
61
 
}
 
61
{ }
62
62
 
63
 
const char* ListenMySQLProtocol::getHost(void) const
 
63
const std::string ListenMySQLProtocol::getHost(void) const
64
64
{
65
 
  return bind_address;
 
65
  return _hostname;
66
66
}
67
67
 
68
68
in_port_t ListenMySQLProtocol::getPort(void) const
69
69
{
70
 
  return (in_port_t) port;
 
70
  return port.get();
71
71
}
72
72
 
73
73
plugin::Client *ListenMySQLProtocol::getClient(int fd)
77
77
  if (new_fd == -1)
78
78
    return NULL;
79
79
 
80
 
  return new (nothrow) ClientMySQLProtocol(new_fd, using_mysql41_protocol);
 
80
  return new ClientMySQLProtocol(new_fd, _using_mysql41_protocol);
81
81
}
82
82
 
83
83
drizzled::atomic<uint64_t> ClientMySQLProtocol::connectionCount;
84
84
drizzled::atomic<uint64_t> ClientMySQLProtocol::failedConnections;
85
85
drizzled::atomic<uint64_t> ClientMySQLProtocol::connected;
86
86
 
87
 
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol_arg):
88
 
  using_mysql41_protocol(using_mysql41_protocol_arg)
 
87
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol):
 
88
  _using_mysql41_protocol(using_mysql41_protocol)
89
89
{
 
90
  
90
91
  net.vio= 0;
91
92
 
92
93
  if (fd == -1)
93
94
    return;
94
95
 
95
 
  if (drizzleclient_net_init_sock(&net, fd, buffer_length))
 
96
  if (drizzleclient_net_init_sock(&net, fd, buffer_length.get()))
96
97
    throw bad_alloc();
97
98
 
98
 
  drizzleclient_net_set_read_timeout(&net, read_timeout);
99
 
  drizzleclient_net_set_write_timeout(&net, write_timeout);
100
 
  net.retry_count=retry_count;
 
99
  drizzleclient_net_set_read_timeout(&net, read_timeout.get());
 
100
  drizzleclient_net_set_write_timeout(&net, write_timeout.get());
 
101
  net.retry_count=retry_count.get();
101
102
}
102
103
 
103
104
ClientMySQLProtocol::~ClientMySQLProtocol()
154
155
  connected.increment();
155
156
 
156
157
  /* Use "connect_timeout" value during connection phase */
157
 
  drizzleclient_net_set_read_timeout(&net, connect_timeout);
158
 
  drizzleclient_net_set_write_timeout(&net, connect_timeout);
 
158
  drizzleclient_net_set_read_timeout(&net, connect_timeout.get());
 
159
  drizzleclient_net_set_write_timeout(&net, connect_timeout.get());
159
160
 
160
161
  connection_is_valid= checkConnection();
161
162
 
168
169
    return false;
169
170
  }
170
171
  /* Connect completed, set read/write timeouts back to default */
171
 
  drizzleclient_net_set_read_timeout(&net, read_timeout);
172
 
  drizzleclient_net_set_write_timeout(&net, write_timeout);
 
172
  drizzleclient_net_set_read_timeout(&net, read_timeout.get());
 
173
  drizzleclient_net_set_write_timeout(&net, write_timeout.get());
173
174
  return true;
174
175
}
175
176
 
225
226
    (*l_packet)[0]= (unsigned char) COM_SLEEP;
226
227
    *packet_length= 1;
227
228
  }
228
 
  else if (using_mysql41_protocol)
 
229
  else if (_using_mysql41_protocol)
229
230
  {
230
231
    /* Map from MySQL commands to Drizzle commands. */
231
232
    switch ((int)(*l_packet)[0])
365
366
    drizzleclient_net_flush(&net);
366
367
    session->main_da.can_overwrite_status= false;
367
368
  }
368
 
  packet.shrink(buffer_length);
 
369
  packet.shrink(buffer_length.get());
369
370
}
370
371
 
371
372
 
475
476
    int2store(pos, field.charsetnr);
476
477
    int4store(pos+2, field.length);
477
478
 
478
 
    if (using_mysql41_protocol)
 
479
    if (_using_mysql41_protocol)
479
480
    {
480
481
      /* Switch to MySQL field numbering. */
481
482
      switch (field.type)
659
660
 
660
661
    server_capabilites= CLIENT_BASIC_FLAGS;
661
662
 
662
 
    if (using_mysql41_protocol)
 
663
    if (_using_mysql41_protocol)
663
664
      server_capabilites|= CLIENT_PROTOCOL_MYSQL41;
664
665
 
665
666
#ifdef HAVE_COMPRESS
707
708
      return false;
708
709
    }
709
710
  }
710
 
  if (packet.alloc(buffer_length))
 
711
  if (packet.alloc(buffer_length.get()))
711
712
    return false; /* The error is set by alloc(). */
712
713
 
713
714
  client_capabilities= uint2korr(net.read_pos);
899
900
 
900
901
static int init(drizzled::module::Context &context)
901
902
{  
902
 
  mysql_status_table_function_ptr= new MysqlProtocolStatus;
 
903
  context.add(new MysqlProtocolStatus);
903
904
 
904
 
  context.add(mysql_status_table_function_ptr);
905
905
  /* Initialize random seeds for the MySQL algorithm with minimal changes. */
906
906
  time_t seed_time= time(NULL);
907
907
  random_seed1= seed_time % random_max;
908
908
  random_seed2= (seed_time / 2) % random_max;
909
909
 
910
910
  const module::option_map &vm= context.getOptions();
911
 
  if (vm.count("port"))
912
 
  { 
913
 
    if (port > 65535)
914
 
    {
915
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value of port\n"));
916
 
      exit(-1);
917
 
    }
918
 
  }
919
 
 
920
 
  if (vm.count("connect-timeout"))
921
 
  {
922
 
    if (connect_timeout < 1 || connect_timeout > 300)
923
 
    {
924
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for connect_timeout\n"));
925
 
      exit(-1);
926
 
    }
927
 
  }
928
 
 
929
 
  if (vm.count("read-timeout"))
930
 
  {
931
 
    if (read_timeout < 1 || read_timeout > 300)
932
 
    {
933
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for read_timeout\n"));
934
 
      exit(-1);
935
 
    }
936
 
  }
937
 
 
938
 
  if (vm.count("write-timeout"))
939
 
  {
940
 
    if (write_timeout < 1 || write_timeout > 300)
941
 
    {
942
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for write_timeout\n"));
943
 
      exit(-1);
944
 
    }
945
 
  }
946
 
 
947
 
  if (vm.count("retry-count"))
948
 
  {
949
 
    if (retry_count < 1 || retry_count > 100)
950
 
    {
951
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for retry_count"));
952
 
      exit(-1);
953
 
    }
954
 
  }
955
 
 
956
 
  if (vm.count("buffer-length"))
957
 
  {
958
 
    if (buffer_length < 1024 || buffer_length > 1024*1024)
959
 
    {
960
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for buffer_length\n"));
961
 
      exit(-1);
962
 
    }
963
 
  }
964
 
 
965
 
  if (vm.count("bind-address"))
966
 
  {
967
 
    bind_address= strdup(vm["bind-address"].as<string>().c_str());
968
 
  }
969
 
 
970
 
  else
971
 
  {
972
 
    bind_address= NULL;
973
 
  }
974
911
 
975
912
  mysql_password= new plugin::Create_function<MySQLPassword>(MySQLPasswordName);
976
913
  context.add(mysql_password);
977
914
 
978
 
  listen_obj= new ListenMySQLProtocol("mysql_protocol", true);
 
915
  listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
979
916
  context.add(listen_obj); 
 
917
  context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
 
918
  context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
 
919
  context.registerVariable(new sys_var_constrained_value<uint32_t>("read_timeout", read_timeout));
 
920
  context.registerVariable(new sys_var_constrained_value<uint32_t>("write_timeout", write_timeout));
 
921
  context.registerVariable(new sys_var_constrained_value<uint32_t>("retry_count", retry_count));
 
922
  context.registerVariable(new sys_var_constrained_value<uint32_t>("buffer_length", buffer_length));
 
923
  context.registerVariable(new sys_var_const_string_val("bind_address",
 
924
                                                        vm["bind-address"].as<std::string>()));
980
925
 
981
926
  return 0;
982
927
}
983
928
 
984
 
static DRIZZLE_SYSVAR_UINT(port, port, PLUGIN_VAR_RQCMDARG,
985
 
                           N_("Port number to use for connection or 0 for default to with MySQL "
986
 
                              "protocol."),
987
 
                           NULL, NULL, 3306, 0, 65535, 0);
988
 
static DRIZZLE_SYSVAR_UINT(connect_timeout, connect_timeout,
989
 
                           PLUGIN_VAR_RQCMDARG, N_("Connect Timeout."),
990
 
                           NULL, NULL, 10, 1, 300, 0);
991
 
static DRIZZLE_SYSVAR_UINT(read_timeout, read_timeout, PLUGIN_VAR_RQCMDARG,
992
 
                           N_("Read Timeout."), NULL, NULL, 30, 1, 300, 0);
993
 
static DRIZZLE_SYSVAR_UINT(write_timeout, write_timeout, PLUGIN_VAR_RQCMDARG,
994
 
                           N_("Write Timeout."), NULL, NULL, 60, 1, 300, 0);
995
 
static DRIZZLE_SYSVAR_UINT(retry_count, retry_count, PLUGIN_VAR_RQCMDARG,
996
 
                           N_("Retry Count."), NULL, NULL, 10, 1, 100, 0);
997
 
static DRIZZLE_SYSVAR_UINT(buffer_length, buffer_length, PLUGIN_VAR_RQCMDARG,
998
 
                           N_("Buffer length."), NULL, NULL, 16384, 1024,
999
 
                           1024*1024, 0);
1000
 
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
1001
 
                          N_("Address to bind to."), NULL, NULL, NULL);
1002
 
 
1003
929
static void init_options(drizzled::module::option_context &context)
1004
930
{
1005
931
  context("port",
1006
 
          po::value<uint32_t>(&port)->default_value(3306),
 
932
          po::value<port_constraint>(&port)->default_value(3306),
1007
933
          N_("Port number to use for connection or 0 for default to with MySQL "
1008
934
                              "protocol."));
1009
935
  context("connect-timeout",
1010
 
          po::value<uint32_t>(&connect_timeout)->default_value(10),
 
936
          po::value<timeout_constraint>(&connect_timeout)->default_value(10),
1011
937
          N_("Connect Timeout."));
1012
938
  context("read-timeout",
1013
 
          po::value<uint32_t>(&read_timeout)->default_value(30),
 
939
          po::value<timeout_constraint>(&read_timeout)->default_value(30),
1014
940
          N_("Read Timeout."));
1015
941
  context("write-timeout",
1016
 
          po::value<uint32_t>(&write_timeout)->default_value(60),
 
942
          po::value<timeout_constraint>(&write_timeout)->default_value(60),
1017
943
          N_("Write Timeout."));
1018
944
  context("retry-count",
1019
 
          po::value<uint32_t>(&retry_count)->default_value(10),
 
945
          po::value<retry_constraint>(&retry_count)->default_value(10),
1020
946
          N_("Retry Count."));
1021
947
  context("buffer-length",
1022
 
          po::value<uint32_t>(&buffer_length)->default_value(16384),
 
948
          po::value<buffer_constraint>(&buffer_length)->default_value(16384),
1023
949
          N_("Buffer length."));
1024
950
  context("bind-address",
1025
 
          po::value<string>(),
 
951
          po::value<string>()->default_value(""),
1026
952
          N_("Address to bind to."));
1027
953
}
1028
954
 
1029
 
static drizzle_sys_var* sys_variables[]= {
1030
 
  DRIZZLE_SYSVAR(port),
1031
 
  DRIZZLE_SYSVAR(connect_timeout),
1032
 
  DRIZZLE_SYSVAR(read_timeout),
1033
 
  DRIZZLE_SYSVAR(write_timeout),
1034
 
  DRIZZLE_SYSVAR(retry_count),
1035
 
  DRIZZLE_SYSVAR(buffer_length),
1036
 
  DRIZZLE_SYSVAR(bind_address),
1037
 
  NULL
1038
 
};
1039
 
 
1040
955
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
1041
956
{
1042
957
  var->type= SHOW_LONGLONG;
1134
1049
  return false;
1135
1050
}
1136
1051
 
 
1052
} /* namespace drizzle_plugin */
 
1053
 
1137
1054
DRIZZLE_DECLARE_PLUGIN
1138
1055
{
1139
1056
  DRIZZLE_VERSION_ID,
1142
1059
  "Eric Day",
1143
1060
  "MySQL Protocol Module",
1144
1061
  PLUGIN_LICENSE_GPL,
1145
 
  init,             /* Plugin Init */
1146
 
  sys_variables, /* system variables */
1147
 
  init_options    /* config options */
 
1062
  drizzle_plugin::init,             /* Plugin Init */
 
1063
  NULL, /* system variables */
 
1064
  drizzle_plugin::init_options    /* config options */
1148
1065
}
1149
1066
DRIZZLE_DECLARE_PLUGIN_END;