~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/memcached_stats/analysis_table.cc

  • Committer: Brian Aker
  • Date: 2009-11-12 16:13:04 UTC
  • mfrom: (1211.1.7 staging)
  • Revision ID: brian@gaz-20091112161304-opamiauv36fg0n6u
Rollup of Brian, Padraig, and Stewart patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* 
2
 
 * Copyright (C) 2009, Padraig O'Sullivan
 
2
 * Copyright (c) 2009, Padraig O'Sullivan
3
3
 * All rights reserved.
4
4
 *
5
5
 * Redistribution and use in source and binary forms, with or without
27
27
 * THE POSSIBILITY OF SUCH DAMAGE.
28
28
 */
29
29
 
30
 
#include "config.h"
 
30
#include "drizzled/server_includes.h"
 
31
#include "drizzled/session.h"
 
32
#include "drizzled/show.h"
31
33
 
32
34
#include "analysis_table.h"
33
35
#include "sysvar_holder.h"
34
36
 
35
 
#include "drizzled/error.h"
36
 
 
37
37
#include <libmemcached/memcached.h>
38
 
#include <libmemcached/server.h>
39
 
 
40
 
namespace drizzle_plugin
41
 
{
42
 
 
43
 
AnalysisTableTool::AnalysisTableTool() :
44
 
  plugin::TableFunction("DATA_DICTIONARY", "MEMCACHED_ANALYSIS")
45
 
{
46
 
  add_field("SERVERS_ANALYZED", plugin::TableFunction::NUMBER);
47
 
  add_field("AVERAGE_ITEM_SIZE", plugin::TableFunction::NUMBER);
48
 
  add_field("NODE_WITH_MOST_MEM_CONSUMPTION");
49
 
  add_field("USED_BYTES", plugin::TableFunction::NUMBER);
50
 
  add_field("NODE_WITH_LEAST_FREE_SPACE");
51
 
  add_field("FREE_BYTES", plugin::TableFunction::NUMBER);
52
 
  add_field("NODE_WITH_LONGEST_UPTIME");
53
 
  add_field("LONGEST_UPTIME", plugin::TableFunction::NUMBER);
54
 
  add_field("POOL_WIDE_HIT_RATIO", plugin::TableFunction::NUMBER); 
55
 
}
56
 
 
57
 
AnalysisTableTool::Generator::Generator(Field **arg) :
58
 
  plugin::TableFunction::Generator(arg)
59
 
{
60
 
  is_done= false;
61
 
}
62
 
 
63
 
bool AnalysisTableTool::Generator::populate()
64
 
{
65
 
  if (is_done)
66
 
  {
67
 
    return false;
68
 
  }
69
 
  is_done= true;
70
 
 
71
 
  drizzled::sys_var *servers_var= drizzled::find_sys_var("memcached_stats_servers");
72
 
  assert(servers_var != NULL);
73
 
 
74
 
  const string servers_string(static_cast<char *>(servers_var.value_ptr(NULL, 0, NULL)));
75
 
 
76
 
  if (servers_string.empty()) 
77
 
  {       
78
 
    my_printf_error(ER_UNKNOWN_ERROR, _("No value in MEMCACHED_STATS_SERVERS variable."), MYF(0));
79
 
    return false;
80
 
  }    
 
38
 
 
39
#include <string>
 
40
#include <vector>
 
41
 
 
42
using namespace std;
 
43
using namespace drizzled;
 
44
 
 
45
int MemcachedAnalysisISMethods::fillTable(Session *session,
 
46
                                          TableList *tables)
 
47
{
 
48
  const CHARSET_INFO * const scs= system_charset_info;
 
49
  Table *table= tables->table;
 
50
  SysvarHolder &sysvar_holder= SysvarHolder::singleton();
 
51
  const string servers_string= sysvar_holder.getServersString();
81
52
 
82
53
  memcached_return rc;
83
54
  memcached_st *serv= memcached_create(NULL);
93
64
  if (server_count > 1)
94
65
  {
95
66
    memcached_analysis_st *report= memcached_analyze(serv, stats, &rc);
96
 
 
97
 
    push(static_cast<uint64_t>(server_count));
98
 
    push(static_cast<uint64_t>(report->average_item_size));
99
 
    push(memcached_server_name(serv, servers[report->most_consumed_server]));
100
 
    push(report->most_used_bytes);
101
 
    push(memcached_server_name(serv, servers[report->least_free_server]));
102
 
    push(report->least_remaining_bytes);
103
 
    push(memcached_server_name(serv, servers[report->oldest_server]));
104
 
    push(static_cast<uint64_t>(report->longest_uptime));
105
 
    push(static_cast<int64_t>(report->pool_hit_ratio));
 
67
    table->restoreRecordAsDefault();
 
68
 
 
69
    table->field[0]->store(server_count);
 
70
    table->field[1]->store(report->average_item_size);
 
71
 
 
72
    table->field[2]->store(memcached_server_name(serv, 
 
73
                                                 servers[report->most_consumed_server]),
 
74
                           64,
 
75
                           scs);
 
76
    table->field[3]->store(report->most_used_bytes);
 
77
    table->field[4]->store(memcached_server_name(serv, 
 
78
                                                 servers[report->least_free_server]),
 
79
                           64,
 
80
                           scs);
 
81
    table->field[5]->store(report->least_remaining_bytes);
 
82
    table->field[6]->store(memcached_server_name(serv, 
 
83
                                                 servers[report->oldest_server]),
 
84
                           64,
 
85
                           scs);
 
86
    table->field[7]->store(report->longest_uptime);
 
87
    table->field[8]->store(report->pool_hit_ratio);
 
88
 
 
89
    /* store the actual record now */
 
90
    if (schema_table_store_record(session, table))
 
91
    {
 
92
      return 1;
 
93
    }
106
94
    free(report);
107
 
  } 
 
95
  }
108
96
 
109
97
  memcached_stat_free(serv, stats);
110
98
  memcached_free(serv);
111
 
 
112
 
  return true;
113
 
}
114
 
 
115
 
} /* namespace drizzle_plugin */
 
99
  return 0;
 
100
}
 
101
 
 
102
bool createMemcachedAnalysisColumns(vector<const plugin::ColumnInfo *> &cols)
 
103
{
 
104
  /*
 
105
   * Create each column for the memcached analysis table.
 
106
   */
 
107
  const plugin::ColumnInfo *num_analyzed= new(std::nothrow) plugin::ColumnInfo("SERVERS_ANALYZED",
 
108
                                                               4,
 
109
                                                               DRIZZLE_TYPE_LONGLONG,
 
110
                                                               0,
 
111
                                                               0, 
 
112
                                                               "Num of Servers Analyzed",
 
113
                                                               SKIP_OPEN_TABLE);
 
114
  if (! num_analyzed)
 
115
  {
 
116
    return true;
 
117
  }
 
118
 
 
119
  const plugin::ColumnInfo *avg_size= new(std::nothrow) plugin::ColumnInfo("AVERAGE_ITEM_SIZE",
 
120
                                                           4,
 
121
                                                           DRIZZLE_TYPE_LONGLONG,
 
122
                                                           0,
 
123
                                                           0, 
 
124
                                                           "Average Item Size",
 
125
                                                           SKIP_OPEN_TABLE);
 
126
  if (! avg_size)
 
127
  {
 
128
    return true;
 
129
  }
 
130
 
 
131
  const plugin::ColumnInfo *mem_node= new(std::nothrow) plugin::ColumnInfo("NODE_WITH_MOST_MEM_CONSUMPTION",
 
132
                                                           32,
 
133
                                                           DRIZZLE_TYPE_VARCHAR,
 
134
                                                           0,
 
135
                                                           0,
 
136
                                                           "Node with Most Memory Consumption",
 
137
                                                           SKIP_OPEN_TABLE);
 
138
  if (! mem_node)
 
139
  {
 
140
    return true;
 
141
  }
 
142
 
 
143
  const plugin::ColumnInfo *used_bytes= new(std::nothrow) plugin::ColumnInfo("USED_BYTES",
 
144
                                                             4,
 
145
                                                             DRIZZLE_TYPE_LONGLONG,
 
146
                                                             0,
 
147
                                                             0,
 
148
                                                             "Used Bytes",
 
149
                                                             SKIP_OPEN_TABLE);
 
150
  if (! used_bytes)
 
151
  {
 
152
    return true;
 
153
  }
 
154
 
 
155
  const plugin::ColumnInfo *free_node= new(std::nothrow) plugin::ColumnInfo("NODE_WITH_LEAST_FREE_SPACE",
 
156
                                                            32,
 
157
                                                            DRIZZLE_TYPE_VARCHAR,
 
158
                                                            0,
 
159
                                                            0,
 
160
                                                            "Node with Least Free Space",
 
161
                                                            SKIP_OPEN_TABLE);
 
162
  if (! free_node)
 
163
  {
 
164
    return true;
 
165
  }
 
166
 
 
167
  const plugin::ColumnInfo *free_bytes= new(std::nothrow) plugin::ColumnInfo("FREE_BYTES",
 
168
                                                             4,
 
169
                                                             DRIZZLE_TYPE_LONGLONG,
 
170
                                                             0,
 
171
                                                             0,
 
172
                                                             "Free Bytes",
 
173
                                                             SKIP_OPEN_TABLE);
 
174
  if (! free_bytes)
 
175
  {
 
176
    return true;
 
177
  }
 
178
 
 
179
  const plugin::ColumnInfo *up_node= new(std::nothrow) plugin::ColumnInfo("NODE_WITH_LONGEST_UPTIME",
 
180
                                                          32,
 
181
                                                          DRIZZLE_TYPE_VARCHAR,
 
182
                                                          0,
 
183
                                                          0,
 
184
                                                          "Node with Longest Uptime",
 
185
                                                          SKIP_OPEN_TABLE);
 
186
  if (! up_node)
 
187
  {
 
188
    return true;
 
189
  }
 
190
 
 
191
  const plugin::ColumnInfo *uptime= new(std::nothrow) plugin::ColumnInfo("LONGEST_UPTIME",
 
192
                                                         4,
 
193
                                                         DRIZZLE_TYPE_LONGLONG,
 
194
                                                         0,
 
195
                                                         0,
 
196
                                                         "Longest Uptime",
 
197
                                                         SKIP_OPEN_TABLE);
 
198
  if (! uptime)
 
199
  {
 
200
    return true;
 
201
  }
 
202
 
 
203
  const plugin::ColumnInfo *hit_ratio= new(std::nothrow) plugin::ColumnInfo("POOL_WIDE_HIT_RATIO",
 
204
                                                            4,
 
205
                                                            DRIZZLE_TYPE_LONGLONG,
 
206
                                                            0,
 
207
                                                            0,
 
208
                                                            "Pool-wide Hit Ratio",
 
209
                                                            SKIP_OPEN_TABLE);
 
210
  if (! hit_ratio)
 
211
  {
 
212
    return true;
 
213
  }
 
214
 
 
215
 
 
216
  cols.push_back(num_analyzed);
 
217
  cols.push_back(avg_size);
 
218
  cols.push_back(mem_node);
 
219
  cols.push_back(used_bytes);
 
220
  cols.push_back(free_node);
 
221
  cols.push_back(free_bytes);
 
222
  cols.push_back(up_node);
 
223
  cols.push_back(uptime);
 
224
  cols.push_back(hit_ratio);
 
225
 
 
226
  return false;
 
227
}
 
228