~posulliv/drizzle/optimizer-style-cleanup

« back to all changes in this revision

Viewing changes to plugin/memcached_stats/stats_table.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-03-15 14:05:26 UTC
  • mfrom: (1237.9.99 staging)
  • Revision ID: osullivan.padraig@gmail.com-20100315140526-opbgwdwn6tfecdkq
MergeĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
 
1
/*
2
2
 * Copyright (c) 2009, Padraig O'Sullivan
3
3
 * All rights reserved.
4
4
 *
28
28
 */
29
29
 
30
30
#include "config.h"
31
 
#include "drizzled/session.h"
32
 
#include "drizzled/show.h"
33
 
#include "drizzled/error.h"
34
31
 
35
32
#include "stats_table.h"
36
33
#include "sysvar_holder.h"
37
34
 
38
 
#include <libmemcached/memcached.h>
39
 
 
40
 
#include <string>
41
 
#include <vector>
 
35
#include "drizzled/error.h"
42
36
 
43
37
using namespace std;
44
38
using namespace drizzled;
52
46
                                  memcached_server_st *server,
53
47
                                  void *context);
54
48
 
 
49
 
55
50
struct server_function_context
56
51
{
57
 
  Table* table;
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)
 
52
  StatsTableTool::Generator* generator; 
 
53
  server_function_context(StatsTableTool::Generator *generator_arg)
 
54
    : generator(generator_arg)
62
55
  {}
63
56
};
64
57
 
 
58
 
65
59
extern "C"
66
 
memcached_return  server_function(const memcached_st *const_memc,
 
60
memcached_return  server_function(const memcached_st *memc,
67
61
                                  memcached_server_st *server,
68
62
                                  void *context)
69
63
{
70
64
  server_function_context *ctx= static_cast<server_function_context *>(context);
71
 
  const CHARSET_INFO * const scs= system_charset_info;
72
 
  memcached_st memc_stack;
73
 
  memcached_st *memc;
74
 
 
75
 
  memc= memcached_clone(&memc_stack, const_memc);
76
 
 
77
 
  if (not memc)
78
 
  {
79
 
    my_printf_error(ER_UNKNOWN_ERROR, _("Unable to allocate memory for memcached_clone()."), MYF(0));
80
 
    return MEMCACHED_FAILURE;
81
 
  }
82
 
    
 
65
 
83
66
  char *server_name= memcached_server_name(memc, *server);
84
67
  in_port_t server_port= memcached_server_port(memc, *server);
85
68
 
86
69
  memcached_stat_st stats;
87
70
  memcached_return ret= memcached_stat_servername(&stats, NULL,
88
71
                                                  server_name, server_port);
 
72
 
89
73
  if (ret != MEMCACHED_SUCCESS)
90
74
  {
91
75
    my_printf_error(ER_UNKNOWN_ERROR, _("Unable get stats from memcached server %s.  Got error from memcached_stat_servername()."), MYF(0), server_name);
92
 
    memcached_free(memc);
93
76
    return ret;
94
77
  }
95
78
 
96
 
  char **list= memcached_stat_get_keys(memc, &stats, &ret);
 
79
  char **list= memcached_stat_get_keys((memcached_st *)memc, &stats, &ret);
97
80
  char **ptr= NULL;
98
 
 
99
 
  ctx->table->setWriteSet(0);
100
 
  ctx->table->setWriteSet(1);
101
 
 
102
 
  ctx->table->field[0]->store(server_name, strlen(server_name), scs);
103
 
  ctx->table->field[1]->store(server_port);
104
 
 
105
 
  uint32_t col= 2;
 
81
 
 
82
  ctx->generator->push(server_name);
 
83
  ctx->generator->push(server_port);
 
84
 
106
85
  for (ptr= list; *ptr; ptr++)
107
86
  {
108
 
    char *value= memcached_stat_get_value(memc, &stats, *ptr, &ret);
109
 
 
110
 
    ctx->table->setWriteSet(col);
111
 
    ctx->table->field[col]->store(value,
112
 
                                  strlen(value),
113
 
                                  scs);
114
 
    col++;
 
87
    char *value= memcached_stat_get_value((memcached_st *)memc, &stats, *ptr, &ret);
 
88
    ctx->generator->push(value);
115
89
    free(value);
116
90
  }
117
91
  free(list);
118
 
  /* store the actual record now */
119
 
  ctx->schema_table->addRow(ctx->table->record[0], ctx->table->s->reclength);
120
 
  memcached_free(memc);
121
92
 
122
93
  return MEMCACHED_SUCCESS;
123
94
}
124
95
 
125
 
int MemcachedStatsISMethods::fillTable(Session *,
126
 
                                       Table *table,
127
 
                                       plugin::InfoSchemaTable *schema_table)
128
 
{
 
96
 
 
97
StatsTableTool::StatsTableTool() :
 
98
  plugin::TableFunction("DATA_DICTIONARY", "MEMCACHED_STATS")
 
99
{
 
100
  add_field("NAME");
 
101
  add_field("PORT_NUMBER", plugin::TableFunction::NUMBER);
 
102
  add_field("PROCESS_ID", plugin::TableFunction::NUMBER);
 
103
  add_field("UPTIME", plugin::TableFunction::NUMBER);
 
104
  add_field("TIME", plugin::TableFunction::NUMBER);
 
105
  add_field("VERSION");
 
106
  add_field("POINTER_SIZE", plugin::TableFunction::NUMBER);
 
107
  add_field("RUSAGE_USER", plugin::TableFunction::NUMBER);
 
108
  add_field("RUSAGE_SYSTEM", plugin::TableFunction::NUMBER);
 
109
  add_field("CURRENT_ITEMS", plugin::TableFunction::NUMBER);
 
110
  add_field("TOTAL_ITEMS", plugin::TableFunction::NUMBER);
 
111
  add_field("BYTES",  plugin::TableFunction::NUMBER);
 
112
  add_field("CURRENT_CONNECTIONS", plugin::TableFunction::NUMBER);
 
113
  add_field("TOTAL_CONNECTIONS", plugin::TableFunction::NUMBER);
 
114
  add_field("CONNECTION_STRUCTURES", plugin::TableFunction::NUMBER);
 
115
  add_field("GETS", plugin::TableFunction::NUMBER);
 
116
  add_field("SETS", plugin::TableFunction::NUMBER);
 
117
  add_field("HITS", plugin::TableFunction::NUMBER);
 
118
  add_field("MISSES", plugin::TableFunction::NUMBER); 
 
119
  add_field("EVICTIONS", plugin::TableFunction::NUMBER);
 
120
  add_field("BYTES_READ", plugin::TableFunction::NUMBER);
 
121
  add_field("BYTES_WRITTEN", plugin::TableFunction::NUMBER);
 
122
  add_field("LIMIT_MAXBYTES", plugin::TableFunction::NUMBER);
 
123
  add_field("THREADS", plugin::TableFunction::NUMBER);
 
124
}
 
125
 
 
126
 
 
127
StatsTableTool::Generator::Generator(Field **arg) :
 
128
  plugin::TableFunction::Generator(arg)
 
129
{
 
130
  /* This will be set to the real number if we initialize properly below */
 
131
  number_of_hosts= 0;
 
132
  
 
133
  host_number= 0;
 
134
 
 
135
  /* set to NULL if we are not able to init we dont want to call delete on this */
 
136
  memc= NULL;
 
137
 
129
138
  SysvarHolder &sysvar_holder= SysvarHolder::singleton();
130
139
  const string servers_string= sysvar_holder.getServersString();
131
140
 
132
 
  table->restoreRecordAsDefault();
133
141
  if (servers_string.empty())
134
142
  {
135
143
    my_printf_error(ER_UNKNOWN_ERROR, _("No value in MEMCACHED_STATS_SERVERS variable."), MYF(0));
136
 
    return 1;
137
 
  } 
138
 
 
 
144
    return; 
 
145
  }
139
146
 
140
 
  memcached_st *memc= memcached_create(NULL);
 
147
  memc= memcached_create(NULL);
141
148
  if (memc == NULL)
142
149
  {
143
150
    my_printf_error(ER_UNKNOWN_ERROR, _("Unable to create memcached struct.  Got error from memcached_create()."), MYF(0));
144
 
    return 1;
 
151
    return;
145
152
  }
146
153
 
147
154
  memcached_server_st *tmp_serv=
149
156
  if (tmp_serv == NULL)
150
157
  {
151
158
    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());
152
 
    memcached_free(memc);
153
 
    return 1; 
 
159
    return;
154
160
  }
155
161
 
156
162
  memcached_server_push(memc, tmp_serv);
157
163
  memcached_server_list_free(tmp_serv);
158
164
 
 
165
  number_of_hosts= memc->number_of_hosts;  
 
166
}
 
167
 
 
168
 
 
169
StatsTableTool::Generator::~Generator()
 
170
{
 
171
  if (memc != NULL)
 
172
  {
 
173
    memcached_free(memc);
 
174
  }
 
175
}
 
176
 
 
177
 
 
178
bool StatsTableTool::Generator::populate()
 
179
{
 
180
  if (host_number == number_of_hosts)
 
181
  {
 
182
    return false;
 
183
  }
 
184
 
 
185
  server_function_context context(this);
 
186
 
159
187
  memcached_server_fn callbacks[1];
160
 
 
161
188
  callbacks[0]= server_function;
162
 
  server_function_context context(table, schema_table);
163
 
 
164
 
  memcached_server_cursor(memc, callbacks, &context, 1);
165
 
 
166
 
  memcached_free(memc);
167
 
 
168
 
  return 0;
169
 
}
170
 
 
171
 
bool createMemcachedStatsColumns(vector<const plugin::ColumnInfo *> &cols)
172
 
{
173
 
  /*
174
 
   * Create each column for the memcached stats table.
175
 
   */
176
 
  const plugin::ColumnInfo *name_col= new(std::nothrow) plugin::ColumnInfo("NAME",
177
 
                                                           32,
178
 
                                                           DRIZZLE_TYPE_VARCHAR,
179
 
                                                           0,
180
 
                                                           0,
181
 
                                                           "Name");
182
 
  if (! name_col)
183
 
  {
184
 
    return true;
185
 
  }
186
 
 
187
 
  const plugin::ColumnInfo *port= new(std::nothrow) plugin::ColumnInfo("PORT_NUMBER",
188
 
                                                       4,
189
 
                                                       DRIZZLE_TYPE_LONGLONG,
190
 
                                                       0,
191
 
                                                       0, 
192
 
                                                       "Port Number");
193
 
  if (! port)
194
 
  {
195
 
    return true;
196
 
  }
197
 
 
198
 
  const plugin::ColumnInfo *pid= new(std::nothrow) plugin::ColumnInfo("PROCESS_ID",
199
 
                                                      4,
200
 
                                                      DRIZZLE_TYPE_LONGLONG,
201
 
                                                      0,
202
 
                                                      0, 
203
 
                                                      "Process ID");
204
 
  if (! pid)
205
 
  {
206
 
    return true;
207
 
  }
208
 
 
209
 
  const plugin::ColumnInfo *uptime= new(std::nothrow) plugin::ColumnInfo("UPTIME",
210
 
                                                         4,
211
 
                                                         DRIZZLE_TYPE_LONGLONG,
212
 
                                                         0,
213
 
                                                         0, 
214
 
                                                         "Uptime");
215
 
  if (! uptime)
216
 
  {
217
 
    return true;
218
 
  }
219
 
 
220
 
  const plugin::ColumnInfo *time= new(std::nothrow) plugin::ColumnInfo("TIME",
221
 
                                                       4,
222
 
                                                       DRIZZLE_TYPE_LONGLONG,
223
 
                                                       0,
224
 
                                                       0, 
225
 
                                                       "Time");
226
 
  if (! time)
227
 
  {
228
 
    return true;
229
 
  }
230
 
 
231
 
  const plugin::ColumnInfo *version= new(std::nothrow) plugin::ColumnInfo("VERSION",
232
 
                                                          8,
233
 
                                                          DRIZZLE_TYPE_VARCHAR,
234
 
                                                          0,
235
 
                                                          0,
236
 
                                                          "Version");
237
 
  if (! version)
238
 
  {
239
 
    return true;
240
 
  }
241
 
 
242
 
  const plugin::ColumnInfo *ptr_size= new(std::nothrow) plugin::ColumnInfo("POINTER_SIZE",
243
 
                                                           4,
244
 
                                                           DRIZZLE_TYPE_LONGLONG,
245
 
                                                           0,
246
 
                                                           0, 
247
 
                                                           "Pointer Size");
248
 
  if (! ptr_size)
249
 
  {
250
 
    return true;
251
 
  }
252
 
 
253
 
  const plugin::ColumnInfo *r_user= new(std::nothrow) plugin::ColumnInfo("RUSAGE_USER",
254
 
                                                         4,
255
 
                                                         DRIZZLE_TYPE_LONGLONG,
256
 
                                                         0,
257
 
                                                         0, 
258
 
                                                         "rusage user");
259
 
  if (! r_user)
260
 
  {
261
 
    return true;
262
 
  }
263
 
 
264
 
  const plugin::ColumnInfo *r_sys= new(std::nothrow) plugin::ColumnInfo("RUSAGE_SYSTEM",
265
 
                                                        4,
266
 
                                                        DRIZZLE_TYPE_LONGLONG,
267
 
                                                        0,
268
 
                                                        0, 
269
 
                                                        "rusage system");
270
 
  if (! r_sys)
271
 
  {
272
 
    return true;
273
 
  }
274
 
  const plugin::ColumnInfo *curr_items= new(std::nothrow) plugin::ColumnInfo("CURRENT_ITEMS",
275
 
                                                             4,
276
 
                                                             DRIZZLE_TYPE_LONGLONG,
277
 
                                                             0,
278
 
                                                             0, 
279
 
                                                             "Current Items");
280
 
  if (! curr_items)
281
 
  {
282
 
    return true;
283
 
  }
284
 
 
285
 
  const plugin::ColumnInfo *total_items= new(std::nothrow) plugin::ColumnInfo("TOTAL_ITEMS",
286
 
                                                              4,
287
 
                                                              DRIZZLE_TYPE_LONGLONG,
288
 
                                                              0,
289
 
                                                              0,
290
 
                                                              "Total Items");
291
 
  if (! total_items)
292
 
  {
293
 
    return true;
294
 
  }
295
 
 
296
 
  const plugin::ColumnInfo *bytes= new(std::nothrow) plugin::ColumnInfo("BYTES",
297
 
                                                        4,
298
 
                                                        DRIZZLE_TYPE_LONGLONG,
299
 
                                                        0,
300
 
                                                        0,
301
 
                                                        "Bytes");
302
 
  if (! bytes)
303
 
  {
304
 
    return true;
305
 
  }
306
 
 
307
 
  const plugin::ColumnInfo *curr_cons= new(std::nothrow) plugin::ColumnInfo("CURRENT_CONNECTIONS",
308
 
                                                            4,
309
 
                                                            DRIZZLE_TYPE_LONGLONG,
310
 
                                                            0,
311
 
                                                            0,
312
 
                                                            "Current Connections");
313
 
  if (! curr_cons)
314
 
  {
315
 
    return true;
316
 
  }
317
 
 
318
 
  const plugin::ColumnInfo *total_cons= new(std::nothrow) plugin::ColumnInfo("TOTAL_CONNECTIONS",
319
 
                                                             4,
320
 
                                                             DRIZZLE_TYPE_LONGLONG,
321
 
                                                             0,
322
 
                                                             0,
323
 
                                                             "Total Connections");
324
 
  if (! total_cons)
325
 
  {
326
 
    return true;
327
 
  }
328
 
 
329
 
  const plugin::ColumnInfo *con_structs= new(std::nothrow) plugin::ColumnInfo("CONNECTION_STRUCTURES",
330
 
                                                              4,
331
 
                                                              DRIZZLE_TYPE_LONGLONG,
332
 
                                                              0,
333
 
                                                              0,
334
 
                                                              "Connection Structures");
335
 
  if (! con_structs)
336
 
  {
337
 
    return true;
338
 
  }
339
 
 
340
 
  const plugin::ColumnInfo *cmd_gets= new(std::nothrow) plugin::ColumnInfo("GETS",
341
 
                                                           4,
342
 
                                                           DRIZZLE_TYPE_LONGLONG,
343
 
                                                           0,
344
 
                                                           0,
345
 
                                                           "Gets");
346
 
  if (! cmd_gets)
347
 
  {
348
 
    return true;
349
 
  }
350
 
 
351
 
  const plugin::ColumnInfo *cmd_sets= new(std::nothrow) plugin::ColumnInfo("SETS",
352
 
                                                           4,
353
 
                                                           DRIZZLE_TYPE_LONGLONG,
354
 
                                                           0,
355
 
                                                           0,
356
 
                                                           "Sets");
357
 
  if (! cmd_sets)
358
 
  {
359
 
    return true;
360
 
  }
361
 
 
362
 
  const plugin::ColumnInfo *hits= new(std::nothrow) plugin::ColumnInfo("HITS",
363
 
                                                       4,
364
 
                                                       DRIZZLE_TYPE_LONGLONG,
365
 
                                                       0,
366
 
                                                       0,
367
 
                                                       "Hits");
368
 
  if (! hits)
369
 
  {
370
 
    return true;
371
 
  }
372
 
 
373
 
  const plugin::ColumnInfo *misses= new(std::nothrow) plugin::ColumnInfo("MISSES",
374
 
                                                         4,
375
 
                                                         DRIZZLE_TYPE_LONGLONG,
376
 
                                                         0,
377
 
                                                         0,
378
 
                                                         "Misses");
379
 
  if (! misses)
380
 
  {
381
 
    return true;
382
 
  }
383
 
 
384
 
  const plugin::ColumnInfo *evicts= new(std::nothrow) plugin::ColumnInfo("EVICTIONS",
385
 
                                                         4,
386
 
                                                         DRIZZLE_TYPE_LONGLONG,
387
 
                                                         0,
388
 
                                                         0,
389
 
                                                         "Evictions");
390
 
  if (! evicts)
391
 
  {
392
 
    return true;
393
 
  }
394
 
 
395
 
  const plugin::ColumnInfo *bytes_read= new(std::nothrow) plugin::ColumnInfo("BYTES_READ",
396
 
                                                             4,
397
 
                                                             DRIZZLE_TYPE_LONGLONG,
398
 
                                                             0,
399
 
                                                             0,
400
 
                                                             "bytes read");
401
 
  if (! bytes_read)
402
 
  {
403
 
    return true;
404
 
  }
405
 
 
406
 
  const plugin::ColumnInfo *bytes_written= new(std::nothrow) plugin::ColumnInfo("BYTES_WRITTEN",
407
 
                                                                4,
408
 
                                                                DRIZZLE_TYPE_LONGLONG,
409
 
                                                                0,
410
 
                                                                0,
411
 
                                                                "bytes written");
412
 
  if (! bytes_written)
413
 
  {
414
 
    return true;
415
 
  }
416
 
 
417
 
  const plugin::ColumnInfo *lim_max_bytes= new(std::nothrow) plugin::ColumnInfo("LIMIT_MAXBYTES",
418
 
                                                                4,
419
 
                                                                DRIZZLE_TYPE_LONGLONG,
420
 
                                                                0,
421
 
                                                                0,
422
 
                                                                "limit maxbytes");
423
 
  if (! lim_max_bytes)
424
 
  {
425
 
    return true;
426
 
  }
427
 
 
428
 
  const plugin::ColumnInfo *threads= new(std::nothrow) plugin::ColumnInfo("THREADS",
429
 
                                                          4,
430
 
                                                          DRIZZLE_TYPE_LONGLONG,
431
 
                                                          0,
432
 
                                                          0,
433
 
                                                          "Threads");
434
 
  if (! threads)
435
 
  {
436
 
    return true;
437
 
  }
438
 
 
439
 
  cols.push_back(name_col);
440
 
  cols.push_back(port);
441
 
  cols.push_back(pid);
442
 
  cols.push_back(uptime);
443
 
  cols.push_back(time);
444
 
  cols.push_back(version);
445
 
  cols.push_back(ptr_size);
446
 
  cols.push_back(r_user);
447
 
  cols.push_back(r_sys);
448
 
  cols.push_back(curr_items);
449
 
  cols.push_back(total_items);
450
 
  cols.push_back(bytes);
451
 
  cols.push_back(curr_cons);
452
 
  cols.push_back(total_cons);
453
 
  cols.push_back(con_structs);
454
 
  cols.push_back(cmd_gets);
455
 
  cols.push_back(cmd_sets);
456
 
  cols.push_back(hits);
457
 
  cols.push_back(misses);
458
 
  cols.push_back(evicts);
459
 
  cols.push_back(bytes_read);
460
 
  cols.push_back(bytes_written);
461
 
  cols.push_back(lim_max_bytes);
462
 
  cols.push_back(threads);
463
 
 
464
 
  return false;
465
 
}
466
 
 
467
 
class DeleteMemcachedCols
468
 
{
469
 
public:
470
 
  template<typename T>
471
 
  inline void operator()(const T *ptr) const
472
 
  {
473
 
    delete ptr;
474
 
  }
475
 
};
476
 
 
477
 
void clearMemcachedColumns(vector<const plugin::ColumnInfo *> &cols)
478
 
{
479
 
  for_each(cols.begin(), cols.end(), DeleteMemcachedCols());
480
 
  cols.clear();
 
189
 
 
190
  unsigned int iferror; 
 
191
  iferror= (*callbacks[0])(memc, &memc->servers[host_number], (void *)&context); 
 
192
 
 
193
  if (iferror)
 
194
  {
 
195
    return false;
 
196
  }
 
197
 
 
198
  host_number++;
 
199
 
 
200
  return true;
481
201
}