~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/drizzle_protocol/drizzle_protocol.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include "drizzled/internal/my_sys.h"
27
27
#include "drizzled/internal/m_string.h"
28
28
#include <algorithm>
29
 
 
 
29
#include <iostream>
 
30
#include <boost/program_options.hpp>
 
31
#include <drizzled/module/option_map.h>
30
32
#include "pack.h"
31
33
#include "errmsg.h"
32
34
#include "drizzle_protocol.h"
33
35
#include "options.h"
 
36
#include "table_function.h"
34
37
 
35
38
#define PROTOCOL_VERSION 10
36
39
 
37
 
namespace drizzled
38
 
{
39
 
extern uint32_t global_thread_id;
40
 
}
41
 
 
 
40
namespace po= boost::program_options;
42
41
using namespace drizzled;
43
42
using namespace std;
44
43
 
54
53
static uint32_t write_timeout;
55
54
static uint32_t retry_count;
56
55
static uint32_t buffer_length;
57
 
static char* bind_address;
 
56
static char* bind_address= NULL;
 
57
 
 
58
static plugin::TableFunction* drizzle_status_table_function_ptr= NULL;
 
59
 
 
60
ListenDrizzleProtocol::~ListenDrizzleProtocol()
 
61
{
 
62
  /* This is strdup'd from the options */
 
63
  free(bind_address);
 
64
}
58
65
 
59
66
const char* ListenDrizzleProtocol::getHost(void) const
60
67
{
88
95
  return new (nothrow) ClientDrizzleProtocol(new_fd, using_mysql41_protocol);
89
96
}
90
97
 
 
98
drizzled::atomic<uint64_t> ClientDrizzleProtocol::connectionCount;
 
99
drizzled::atomic<uint64_t> ClientDrizzleProtocol::failedConnections;
 
100
drizzled::atomic<uint64_t> ClientDrizzleProtocol::connected;
 
101
 
91
102
ClientDrizzleProtocol::ClientDrizzleProtocol(int fd, bool using_mysql41_protocol_arg):
92
103
  using_mysql41_protocol(using_mysql41_protocol_arg)
93
104
{
146
157
  { 
147
158
    drizzleclient_net_close(&net);
148
159
    drizzleclient_net_end(&net);
 
160
    connected.decrement();
149
161
  }
150
162
}
151
163
 
153
165
{
154
166
  bool connection_is_valid;
155
167
 
 
168
  connectionCount.increment();
 
169
  connected.increment();
 
170
 
156
171
  /* Use "connect_timeout" value during connection phase */
157
172
  drizzleclient_net_set_read_timeout(&net, connect_timeout);
158
173
  drizzleclient_net_set_write_timeout(&net, connect_timeout);
164
179
  else
165
180
  {
166
181
    sendError(session->main_da.sql_errno(), session->main_da.message());
 
182
    failedConnections.increment();
167
183
    return false;
168
184
  }
169
185
 
663
679
    server_capabilites|= CLIENT_COMPRESS;
664
680
#endif /* HAVE_COMPRESS */
665
681
 
666
 
    end= buff + strlen(VERSION);
 
682
    end= buff + strlen(PANDORA_RELEASE_VERSION);
667
683
    if ((end - buff) >= SERVER_VERSION_LENGTH)
668
684
      end= buff + (SERVER_VERSION_LENGTH - 1);
669
 
    memcpy(buff, VERSION, end - buff);
 
685
    memcpy(buff, PANDORA_RELEASE_VERSION, end - buff);
670
686
    *end= 0;
671
687
    end++;
672
688
 
673
 
    int4store((unsigned char*) end, global_thread_id);
 
689
    int4store((unsigned char*) end, session->variables.pseudo_thread_id);
674
690
    end+= 4;
675
691
 
676
692
    /* We don't use scramble anymore. */
812
828
  drizzleclient_net_write(&net, buff, 5);
813
829
}
814
830
 
815
 
static ListenDrizzleProtocol *listen_obj= NULL;
816
 
 
817
 
static int init(plugin::Registry &registry)
818
 
{
819
 
  listen_obj= new ListenDrizzleProtocol("drizzle_protocol", false);
820
 
  registry.add(listen_obj); 
821
 
  return 0;
822
 
}
823
 
 
824
 
static int deinit(plugin::Registry &registry)
825
 
{
826
 
  registry.remove(listen_obj);
827
 
  delete listen_obj;
 
831
static int init(module::Context &context)
 
832
{
 
833
  drizzle_status_table_function_ptr= new DrizzleProtocolStatus;
 
834
 
 
835
  context.add(drizzle_status_table_function_ptr);
 
836
 
 
837
  const module::option_map &vm= context.getOptions();
 
838
  if (vm.count("port"))
 
839
  { 
 
840
    if (port > 65535)
 
841
    {
 
842
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value of port\n"));
 
843
      exit(-1);
 
844
    }
 
845
  }
 
846
 
 
847
  if (vm.count("connect-timeout"))
 
848
  {
 
849
    if (connect_timeout < 1 || connect_timeout > 300)
 
850
    {
 
851
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for connect_timeout\n"));
 
852
      exit(-1);
 
853
    }
 
854
  }
 
855
 
 
856
  if (vm.count("read-timeout"))
 
857
  {
 
858
    if (read_timeout < 1 || read_timeout > 300)
 
859
    {
 
860
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for read_timeout\n"));
 
861
      exit(-1);
 
862
    }
 
863
  }
 
864
 
 
865
  if (vm.count("write-timeout"))
 
866
  {
 
867
    if (write_timeout < 1 || write_timeout > 300)
 
868
    {
 
869
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for write_timeout\n"));
 
870
      exit(-1);
 
871
    }
 
872
  }
 
873
 
 
874
  if (vm.count("retry-count"))
 
875
  {
 
876
    if (retry_count < 1 || retry_count > 100)
 
877
    {
 
878
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for retry_count\n"));
 
879
      exit(-1);
 
880
    }
 
881
  }
 
882
 
 
883
  if (vm.count("buffer-length"))
 
884
  {
 
885
    if (buffer_length < 1024 || buffer_length > 1024*1024)
 
886
    {
 
887
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for buffer_length\n"));
 
888
      exit(-1);
 
889
    }
 
890
  }
 
891
 
 
892
  if (vm.count("bind-address"))
 
893
  {
 
894
    bind_address= strdup(vm["bind-address"].as<string>().c_str());
 
895
  }
 
896
 
 
897
  else
 
898
  {
 
899
    bind_address= NULL;
 
900
  }
 
901
  
 
902
  context.add(new ListenDrizzleProtocol("drizzle_protocol", false)); 
828
903
  return 0;
829
904
}
830
905
 
849
924
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
850
925
                          N_("Address to bind to."), NULL, NULL, NULL);
851
926
 
 
927
static void init_options(drizzled::module::option_context &context)
 
928
{
 
929
  context("port",
 
930
          po::value<uint32_t>(&port)->default_value(0),
 
931
          N_("Port number to use for connection or 0 for "
 
932
                              "default to, in order of "
 
933
                              "preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
 
934
                              "built-in default (4427)."));
 
935
  context("connect-timeout",
 
936
          po::value<uint32_t>(&connect_timeout)->default_value(10),
 
937
          N_("Connect Timeout."));
 
938
  context("read-timeout",
 
939
          po::value<uint32_t>(&read_timeout)->default_value(30),
 
940
          N_("Read Timeout."));
 
941
  context("write-timeout",
 
942
          po::value<uint32_t>(&write_timeout)->default_value(60),
 
943
          N_("Write Timeout."));
 
944
  context("retry-count",
 
945
          po::value<uint32_t>(&retry_count)->default_value(10),
 
946
          N_("Retry Count."));
 
947
  context("buffer-length",
 
948
          po::value<uint32_t>(&buffer_length)->default_value(16384),
 
949
          N_("Buffer length."));
 
950
  context("bind-address",
 
951
          po::value<string>(),
 
952
          N_("Address to bind to."));
 
953
}
 
954
 
852
955
static drizzle_sys_var* sys_variables[]= {
853
956
  DRIZZLE_SYSVAR(port),
854
957
  DRIZZLE_SYSVAR(connect_timeout),
860
963
  NULL
861
964
};
862
965
 
 
966
static int drizzle_protocol_connection_count_func(drizzle_show_var *var, char *buff)
 
967
{
 
968
  var->type= SHOW_LONGLONG;
 
969
  var->value= buff;
 
970
  *((uint64_t *)buff)= ClientDrizzleProtocol::connectionCount;
 
971
  return 0;
 
972
}
 
973
 
 
974
static int drizzle_protocol_connected_count_func(drizzle_show_var *var, char *buff)
 
975
{
 
976
  var->type= SHOW_LONGLONG;
 
977
  var->value= buff;
 
978
  *((uint64_t *)buff)= ClientDrizzleProtocol::connected;
 
979
  return 0;
 
980
}
 
981
 
 
982
static int drizzle_protocol_failed_count_func(drizzle_show_var *var, char *buff)
 
983
{
 
984
  var->type= SHOW_LONGLONG;
 
985
  var->value= buff;
 
986
  *((uint64_t *)buff)= ClientDrizzleProtocol::failedConnections;
 
987
  return 0;
 
988
}
 
989
 
 
990
static st_show_var_func_container drizzle_protocol_connection_count=
 
991
  { &drizzle_protocol_connection_count_func };
 
992
 
 
993
static st_show_var_func_container drizzle_protocol_connected_count=
 
994
  { &drizzle_protocol_connected_count_func };
 
995
 
 
996
static st_show_var_func_container drizzle_protocol_failed_count=
 
997
  { &drizzle_protocol_failed_count_func };
 
998
 
 
999
static drizzle_show_var drizzle_protocol_status_variables[]= {
 
1000
  {"Connections",
 
1001
  (char*) &drizzle_protocol_connection_count, SHOW_FUNC},
 
1002
  {"Connected",
 
1003
  (char*) &drizzle_protocol_connected_count, SHOW_FUNC},
 
1004
  {"Failed_connections",
 
1005
  (char*) &drizzle_protocol_failed_count, SHOW_FUNC},
 
1006
  {NULL, NULL, SHOW_LONGLONG}
 
1007
};
 
1008
 
 
1009
DrizzleProtocolStatus::Generator::Generator(drizzled::Field **fields) :
 
1010
  plugin::TableFunction::Generator(fields)
 
1011
{
 
1012
  status_var_ptr= drizzle_protocol_status_variables;
 
1013
}
 
1014
 
 
1015
bool DrizzleProtocolStatus::Generator::populate()
 
1016
{
 
1017
  MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
 
1018
  char * const buff= (char *) &buff_data;
 
1019
  drizzle_show_var tmp;
 
1020
 
 
1021
  if (status_var_ptr->name)
 
1022
  {
 
1023
    std::ostringstream oss;
 
1024
    string return_value;
 
1025
    const char *value;
 
1026
    int type;
 
1027
 
 
1028
    push(status_var_ptr->name);
 
1029
 
 
1030
    if (status_var_ptr->type == SHOW_FUNC)
 
1031
    {
 
1032
      ((mysql_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
 
1033
      value= buff;
 
1034
      type= tmp.type;
 
1035
    }
 
1036
    else
 
1037
    {
 
1038
      value= status_var_ptr->value;
 
1039
      type= status_var_ptr->type;
 
1040
    }
 
1041
 
 
1042
    switch(type)
 
1043
    {
 
1044
    case SHOW_LONGLONG:
 
1045
      oss << *(uint64_t*) value;
 
1046
      return_value= oss.str();
 
1047
      break;
 
1048
    default:
 
1049
      assert(0);
 
1050
    }
 
1051
    if (return_value.length())
 
1052
      push(return_value);
 
1053
    else
 
1054
      push(" ");
 
1055
 
 
1056
    status_var_ptr++;
 
1057
 
 
1058
    return true;
 
1059
  }
 
1060
  return false;
 
1061
}
 
1062
 
863
1063
} /* namespace drizzle_protocol */
864
1064
 
865
 
DRIZZLE_PLUGIN(drizzle_protocol::init, drizzle_protocol::deinit, drizzle_protocol::sys_variables);
 
1065
DRIZZLE_PLUGIN(drizzle_protocol::init, drizzle_protocol::sys_variables, drizzle_protocol::init_options);