27
27
* THE POSSIBILITY OF SUCH DAMAGE.
31
#include "drizzled/session.h"
32
#include "drizzled/show.h"
33
#include "drizzled/my_error.h"
32
35
#include "stats_table.h"
34
#include <drizzled/error.h>
35
#include <libmemcached/server.h>
36
#include "sysvar_holder.h"
38
#include <libmemcached/memcached.h>
44
using namespace drizzled;
37
46
#if !defined(HAVE_MEMCACHED_SERVER_FN)
38
47
typedef memcached_server_function memcached_server_fn;
41
namespace drizzle_plugin
45
memcached_return server_function(const memcached_st *ptr,
51
memcached_return server_function(memcached_st *ptr,
46
52
memcached_server_st *server,
49
55
struct server_function_context
51
StatsTableTool::Generator* generator;
52
server_function_context(StatsTableTool::Generator *generator_arg)
53
: generator(generator_arg)
58
plugin::InfoSchemaTable *schema_table;
59
server_function_context(Table *table_arg,
60
plugin::InfoSchemaTable *schema_table_arg)
61
: table(table_arg), schema_table(schema_table_arg)
59
memcached_return server_function(const memcached_st *memc,
66
memcached_return server_function(memcached_st *memc,
60
67
memcached_server_st *server,
63
70
server_function_context *ctx= static_cast<server_function_context *>(context);
65
const char *server_name= memcached_server_name(*memc, *server);
66
in_port_t server_port= memcached_server_port(*memc, *server);
71
const CHARSET_INFO * const scs= system_charset_info;
73
char *server_name= memcached_server_name(memc, *server);
74
in_port_t server_port= memcached_server_port(memc, *server);
68
76
memcached_stat_st stats;
69
77
memcached_return ret= memcached_stat_servername(&stats, NULL,
70
78
server_name, server_port);
72
79
if (ret != MEMCACHED_SUCCESS)
74
81
my_printf_error(ER_UNKNOWN_ERROR, _("Unable get stats from memcached server %s. Got error from memcached_stat_servername()."), MYF(0), server_name);
78
char **list= memcached_stat_get_keys((memcached_st *)memc, &stats, &ret);
85
char **list= memcached_stat_get_keys(memc, &stats, &ret);
81
ctx->generator->push(server_name);
82
ctx->generator->push(static_cast<uint64_t>(server_port));
88
ctx->table->setWriteSet(0);
89
ctx->table->setWriteSet(1);
91
ctx->table->field[0]->store(server_name, strlen(server_name), scs);
92
ctx->table->field[1]->store(server_port);
84
95
for (ptr= list; *ptr; ptr++)
86
char *value= memcached_stat_get_value((memcached_st *)memc, &stats, *ptr, &ret);
87
ctx->generator->push(value);
97
char *value= memcached_stat_get_value(memc, &stats, *ptr, &ret);
99
ctx->table->setWriteSet(col);
100
ctx->table->field[col]->store(value,
107
/* store the actual record now */
108
ctx->schema_table->addRow(ctx->table->record[0], ctx->table->s->reclength);
92
109
return MEMCACHED_SUCCESS;
96
StatsTableTool::StatsTableTool() :
97
plugin::TableFunction("DATA_DICTIONARY", "MEMCACHED_STATS")
100
add_field("PORT_NUMBER", plugin::TableFunction::NUMBER);
101
add_field("PROCESS_ID", plugin::TableFunction::NUMBER);
102
add_field("UPTIME", plugin::TableFunction::NUMBER);
103
add_field("TIME", plugin::TableFunction::NUMBER);
104
add_field("VERSION");
105
add_field("POINTER_SIZE", plugin::TableFunction::NUMBER);
106
add_field("RUSAGE_USER", plugin::TableFunction::NUMBER);
107
add_field("RUSAGE_SYSTEM", plugin::TableFunction::NUMBER);
108
add_field("CURRENT_ITEMS", plugin::TableFunction::NUMBER);
109
add_field("TOTAL_ITEMS", plugin::TableFunction::NUMBER);
110
add_field("BYTES", plugin::TableFunction::NUMBER);
111
add_field("CURRENT_CONNECTIONS", plugin::TableFunction::NUMBER);
112
add_field("TOTAL_CONNECTIONS", plugin::TableFunction::NUMBER);
113
add_field("CONNECTION_STRUCTURES", plugin::TableFunction::NUMBER);
114
add_field("GETS", plugin::TableFunction::NUMBER);
115
add_field("SETS", plugin::TableFunction::NUMBER);
116
add_field("HITS", plugin::TableFunction::NUMBER);
117
add_field("MISSES", plugin::TableFunction::NUMBER);
118
add_field("EVICTIONS", plugin::TableFunction::NUMBER);
119
add_field("BYTES_READ", plugin::TableFunction::NUMBER);
120
add_field("BYTES_WRITTEN", plugin::TableFunction::NUMBER);
121
add_field("LIMIT_MAXBYTES", plugin::TableFunction::NUMBER);
122
add_field("THREADS", plugin::TableFunction::NUMBER);
126
StatsTableTool::Generator::Generator(drizzled::Field **arg) :
127
plugin::TableFunction::Generator(arg)
129
/* This will be set to the real number if we initialize properly below */
134
/* set to NULL if we are not able to init we dont want to call delete on this */
137
drizzled::sys_var *servers_var= drizzled::find_sys_var("memcached_stats_servers");
138
assert(servers_var != NULL);
140
const string servers_string(static_cast<char *>(servers_var.value_ptr(NULL, 0, NULL)));
112
int MemcachedStatsISMethods::fillTable(Session *,
114
plugin::InfoSchemaTable *schema_table)
116
SysvarHolder &sysvar_holder= SysvarHolder::singleton();
117
const string servers_string= sysvar_holder.getServersString();
119
table->restoreRecordAsDefault();
142
120
if (servers_string.empty())
144
122
my_printf_error(ER_UNKNOWN_ERROR, _("No value in MEMCACHED_STATS_SERVERS variable."), MYF(0));
148
memc= memcached_create(NULL);
127
memcached_st *memc= memcached_create(NULL);
149
128
if (memc == NULL)
151
130
my_printf_error(ER_UNKNOWN_ERROR, _("Unable to create memcached struct. Got error from memcached_create()."), MYF(0));
155
134
memcached_server_st *tmp_serv=
157
136
if (tmp_serv == NULL)
159
138
my_printf_error(ER_UNKNOWN_ERROR, _("Unable to create memcached server list. Got error from memcached_servers_parse(%s)."), MYF(0), servers_string.c_str());
139
memcached_free(memc);
163
143
memcached_server_push(memc, tmp_serv);
164
144
memcached_server_list_free(tmp_serv);
166
number_of_hosts= memc->number_of_hosts;
170
StatsTableTool::Generator::~Generator()
174
memcached_free(memc);
179
bool StatsTableTool::Generator::populate()
181
if (host_number == number_of_hosts)
186
server_function_context context(this);
188
memcached_server_function callbacks[1];
146
memcached_server_fn callbacks[1];
189
148
callbacks[0]= server_function;
191
unsigned int iferror;
192
iferror= (*callbacks[0])(memc, &memc->servers[host_number], (void *)&context);
204
} /* namespace drizzle_plugin */
149
server_function_context context(table, schema_table);
151
memcached_server_cursor(memc, callbacks, &context, 1);
153
memcached_free(memc);
158
bool createMemcachedStatsColumns(vector<const plugin::ColumnInfo *> &cols)
161
* Create each column for the memcached stats table.
163
const plugin::ColumnInfo *name_col= new(std::nothrow) plugin::ColumnInfo("NAME",
165
DRIZZLE_TYPE_VARCHAR,
174
const plugin::ColumnInfo *port= new(std::nothrow) plugin::ColumnInfo("PORT_NUMBER",
176
DRIZZLE_TYPE_LONGLONG,
185
const plugin::ColumnInfo *pid= new(std::nothrow) plugin::ColumnInfo("PROCESS_ID",
187
DRIZZLE_TYPE_LONGLONG,
196
const plugin::ColumnInfo *uptime= new(std::nothrow) plugin::ColumnInfo("UPTIME",
198
DRIZZLE_TYPE_LONGLONG,
207
const plugin::ColumnInfo *time= new(std::nothrow) plugin::ColumnInfo("TIME",
209
DRIZZLE_TYPE_LONGLONG,
218
const plugin::ColumnInfo *version= new(std::nothrow) plugin::ColumnInfo("VERSION",
220
DRIZZLE_TYPE_VARCHAR,
229
const plugin::ColumnInfo *ptr_size= new(std::nothrow) plugin::ColumnInfo("POINTER_SIZE",
231
DRIZZLE_TYPE_LONGLONG,
240
const plugin::ColumnInfo *r_user= new(std::nothrow) plugin::ColumnInfo("RUSAGE_USER",
242
DRIZZLE_TYPE_LONGLONG,
251
const plugin::ColumnInfo *r_sys= new(std::nothrow) plugin::ColumnInfo("RUSAGE_SYSTEM",
253
DRIZZLE_TYPE_LONGLONG,
261
const plugin::ColumnInfo *curr_items= new(std::nothrow) plugin::ColumnInfo("CURRENT_ITEMS",
263
DRIZZLE_TYPE_LONGLONG,
272
const plugin::ColumnInfo *total_items= new(std::nothrow) plugin::ColumnInfo("TOTAL_ITEMS",
274
DRIZZLE_TYPE_LONGLONG,
283
const plugin::ColumnInfo *bytes= new(std::nothrow) plugin::ColumnInfo("BYTES",
285
DRIZZLE_TYPE_LONGLONG,
294
const plugin::ColumnInfo *curr_cons= new(std::nothrow) plugin::ColumnInfo("CURRENT_CONNECTIONS",
296
DRIZZLE_TYPE_LONGLONG,
299
"Current Connections");
305
const plugin::ColumnInfo *total_cons= new(std::nothrow) plugin::ColumnInfo("TOTAL_CONNECTIONS",
307
DRIZZLE_TYPE_LONGLONG,
310
"Total Connections");
316
const plugin::ColumnInfo *con_structs= new(std::nothrow) plugin::ColumnInfo("CONNECTION_STRUCTURES",
318
DRIZZLE_TYPE_LONGLONG,
321
"Connection Structures");
327
const plugin::ColumnInfo *cmd_gets= new(std::nothrow) plugin::ColumnInfo("GETS",
329
DRIZZLE_TYPE_LONGLONG,
338
const plugin::ColumnInfo *cmd_sets= new(std::nothrow) plugin::ColumnInfo("SETS",
340
DRIZZLE_TYPE_LONGLONG,
349
const plugin::ColumnInfo *hits= new(std::nothrow) plugin::ColumnInfo("HITS",
351
DRIZZLE_TYPE_LONGLONG,
360
const plugin::ColumnInfo *misses= new(std::nothrow) plugin::ColumnInfo("MISSES",
362
DRIZZLE_TYPE_LONGLONG,
371
const plugin::ColumnInfo *evicts= new(std::nothrow) plugin::ColumnInfo("EVICTIONS",
373
DRIZZLE_TYPE_LONGLONG,
382
const plugin::ColumnInfo *bytes_read= new(std::nothrow) plugin::ColumnInfo("BYTES_READ",
384
DRIZZLE_TYPE_LONGLONG,
393
const plugin::ColumnInfo *bytes_written= new(std::nothrow) plugin::ColumnInfo("BYTES_WRITTEN",
395
DRIZZLE_TYPE_LONGLONG,
404
const plugin::ColumnInfo *lim_max_bytes= new(std::nothrow) plugin::ColumnInfo("LIMIT_MAXBYTES",
406
DRIZZLE_TYPE_LONGLONG,
415
const plugin::ColumnInfo *threads= new(std::nothrow) plugin::ColumnInfo("THREADS",
417
DRIZZLE_TYPE_LONGLONG,
426
cols.push_back(name_col);
427
cols.push_back(port);
429
cols.push_back(uptime);
430
cols.push_back(time);
431
cols.push_back(version);
432
cols.push_back(ptr_size);
433
cols.push_back(r_user);
434
cols.push_back(r_sys);
435
cols.push_back(curr_items);
436
cols.push_back(total_items);
437
cols.push_back(bytes);
438
cols.push_back(curr_cons);
439
cols.push_back(total_cons);
440
cols.push_back(con_structs);
441
cols.push_back(cmd_gets);
442
cols.push_back(cmd_sets);
443
cols.push_back(hits);
444
cols.push_back(misses);
445
cols.push_back(evicts);
446
cols.push_back(bytes_read);
447
cols.push_back(bytes_written);
448
cols.push_back(lim_max_bytes);
449
cols.push_back(threads);
454
class DeleteMemcachedCols
458
inline void operator()(const T *ptr) const
464
void clearMemcachedColumns(vector<const plugin::ColumnInfo *> &cols)
466
for_each(cols.begin(), cols.end(), DeleteMemcachedCols());