-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathndbinfo_sql.cpp
More file actions
445 lines (388 loc) · 13 KB
/
Copy pathndbinfo_sql.cpp
File metadata and controls
445 lines (388 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ndb_global.h>
#include <ndb_opts.h>
#include <util/BaseString.hpp>
#include "../src/kernel/vm/NdbinfoTables.cpp"
static char* opt_ndbinfo_db = (char*)"ndbinfo";
static char* opt_table_prefix = (char*)"ndb$";
static
struct my_option
my_long_options[] =
{
{ "database", 'd',
"Name of the database used by ndbinfo",
(uchar**) &opt_ndbinfo_db, (uchar**) &opt_ndbinfo_db, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "prefix", 256,
"Prefix to use for all virtual tables loaded from NDB",
(uchar**) &opt_table_prefix, (uchar**) &opt_table_prefix, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};
struct view {
const char* name;
const char* sql;
} views[] =
{
#if 0
{ "pools",
"SELECT node_id, b.block_name, block_instance, pool_name, "
"used, total, high, entry_size, cp1.param_name AS param_name1, "
"cp2.param_name AS param_name2, cp3.param_name AS param_name3, "
"cp4.param_name AS param_name4 "
"FROM <NDBINFO_DB>.<TABLE_PREFIX>pools p "
"LEFT JOIN <NDBINFO_DB>.blocks b ON p.block_number = b.block_number "
"LEFT JOIN <NDBINFO_DB>.config_params cp1 ON p.config_param1 = cp1.param_number "
"LEFT JOIN <NDBINFO_DB>.config_params cp2 ON p.config_param2 = cp2.param_number "
"LEFT JOIN <NDBINFO_DB>.config_params cp3 ON p.config_param3 = cp3.param_number "
"LEFT JOIN <NDBINFO_DB>.config_params cp4 ON p.config_param4 = cp4.param_number"
},
#endif
{ "transporters",
"SELECT node_id, remote_node_id, "
" CASE connection_status"
" WHEN 0 THEN \"CONNECTED\""
" WHEN 1 THEN \"CONNECTING\""
" WHEN 2 THEN \"DISCONNECTED\""
" WHEN 3 THEN \"DISCONNECTING\""
" ELSE NULL "
" END AS status "
"FROM <NDBINFO_DB>.<TABLE_PREFIX>transporters"
},
{ "logspaces",
"SELECT node_id, "
" CASE log_type"
" WHEN 0 THEN \"REDO\""
" WHEN 1 THEN \"DD-UNDO\""
" ELSE NULL "
" END AS log_type, "
"log_id, log_part, total, used "
"FROM <NDBINFO_DB>.<TABLE_PREFIX>logspaces"
},
{ "logbuffers",
"SELECT node_id, "
" CASE log_type"
" WHEN 0 THEN \"REDO\""
" WHEN 1 THEN \"DD-UNDO\""
" ELSE \"<unknown>\" "
" END AS log_type, "
"log_id, log_part, total, used "
"FROM <NDBINFO_DB>.<TABLE_PREFIX>logbuffers"
},
{ "resources",
"SELECT node_id, "
" CASE resource_id"
" WHEN 0 THEN \"RESERVED\""
" WHEN 1 THEN \"DISK_OPERATIONS\""
" WHEN 2 THEN \"DISK_RECORDS\""
" WHEN 3 THEN \"DATA_MEMORY\""
" WHEN 4 THEN \"JOBBUFFER\""
" WHEN 5 THEN \"FILE_BUFFERS\""
" WHEN 6 THEN \"TRANSPORTER_BUFFERS\""
" ELSE \"<unknown>\" "
" END AS resource_name, "
"reserved, used, max "
"FROM <NDBINFO_DB>.<TABLE_PREFIX>resources"
},
{ "counters",
"SELECT node_id, b.block_name, block_instance, "
"counter_id, "
"CASE counter_id"
" WHEN 1 THEN \"ATTRINFO\""
" WHEN 2 THEN \"TRANSACTIONS\""
" WHEN 3 THEN \"COMMITS\""
" WHEN 4 THEN \"READS\""
" WHEN 5 THEN \"SIMPLE_READS\""
" WHEN 6 THEN \"WRITES\""
" WHEN 7 THEN \"ABORTS\""
" WHEN 8 THEN \"TABLE_SCANS\""
" WHEN 9 THEN \"RANGE_SCANS\""
" WHEN 10 THEN \"OPERATIONS\""
" WHEN 11 THEN \"READS_RECEIVED\""
" WHEN 12 THEN \"LOCAL_READS_SENT\""
" WHEN 13 THEN \"REMOTE_READS_SENT\""
" WHEN 14 THEN \"READS_NOT_FOUND\""
" WHEN 15 THEN \"TABLE_SCANS_RECEIVED\""
" WHEN 16 THEN \"LOCAL_TABLE_SCANS_SENT\""
" WHEN 17 THEN \"RANGE_SCANS_RECEIVED\""
" WHEN 18 THEN \"LOCAL_RANGE_SCANS_SENT\""
" WHEN 19 THEN \"REMOTE_RANGE_SCANS_SENT\""
" WHEN 20 THEN \"SCAN_BATCHES_RETURNED\""
" WHEN 21 THEN \"SCAN_ROWS_RETURNED\""
" WHEN 22 THEN \"PRUNED_RANGE_SCANS_RECEIVED\""
" WHEN 23 THEN \"CONST_PRUNED_RANGE_SCANS_RECEIVED\""
" ELSE \"<unknown>\" "
" END AS counter_name, "
"val "
"FROM <NDBINFO_DB>.<TABLE_PREFIX>counters c "
"LEFT JOIN <NDBINFO_DB>.blocks b "
"ON c.block_number = b.block_number"
},
{ "nodes",
"SELECT node_id, "
"uptime, "
"CASE status"
" WHEN 0 THEN \"NOTHING\""
" WHEN 1 THEN \"CMVMI\""
" WHEN 2 THEN \"STARTING\""
" WHEN 3 THEN \"STARTED\""
" WHEN 4 THEN \"SINGLEUSER\""
" WHEN 5 THEN \"STOPPING_1\""
" WHEN 6 THEN \"STOPPING_2\""
" WHEN 7 THEN \"STOPPING_3\""
" WHEN 8 THEN \"STOPPING_4\""
" ELSE \"<unknown>\" "
" END AS status, "
"start_phase, "
"config_generation "
"FROM <NDBINFO_DB>.<TABLE_PREFIX>nodes"
},
{ "memoryusage",
"SELECT node_id,"
" pool_name AS memory_type,"
" SUM(used*entry_size) AS used,"
" SUM(used) AS used_pages,"
" SUM(total*entry_size) AS total,"
" SUM(total) AS total_pages "
"FROM <NDBINFO_DB>.<TABLE_PREFIX>pools "
"WHERE block_number IN (248, 254) AND "
" (pool_name = \"Index memory\" OR pool_name = \"Data memory\") "
"GROUP BY node_id, memory_type"
},
{ "diskpagebuffer",
"SELECT node_id, block_instance, "
"pages_written, pages_written_lcp, pages_read, log_waits, "
"page_requests_direct_return, page_requests_wait_queue, page_requests_wait_io "
"FROM <NDBINFO_DB>.<TABLE_PREFIX>diskpagebuffer"
}
};
size_t num_views = sizeof(views)/sizeof(views[0]);
#include "../src/mgmsrv/ConfigInfo.cpp"
static ConfigInfo g_info;
static void fill_config_params(BaseString& sql)
{
const char* separator = "";
const ConfigInfo::ParamInfo* pinfo= NULL;
ConfigInfo::ParamInfoIter param_iter(g_info,
CFG_SECTION_NODE,
NODE_TYPE_DB);
while((pinfo= param_iter.next())) {
if (pinfo->_paramId == 0 || // KEY_INTERNAL
pinfo->_status != ConfigInfo::CI_USED)
continue;
sql.appfmt("%s(%u, \"%s\")", separator, pinfo->_paramId, pinfo->_fname);
separator = ", ";
}
}
#include "../src/common/debugger/BlockNames.cpp"
static void fill_blocks(BaseString& sql)
{
const char* separator = "";
for (BlockNumber i = 0; i < NO_OF_BLOCK_NAMES; i++)
{
const BlockName& bn = BlockNames[i];
sql.appfmt("%s(%u, \"%s\")", separator, bn.number, bn.name);
separator = ", ";
}
}
struct lookup {
const char* name;
const char* columns;
void (*fill)(BaseString&);
} lookups[] =
{
{ "blocks",
"block_number INT UNSIGNED PRIMARY KEY, "
"block_name VARCHAR(512)",
&fill_blocks
},
{ "config_params",
"param_number INT UNSIGNED PRIMARY KEY, "
"param_name VARCHAR(512)",
&fill_config_params
}
};
size_t num_lookups = sizeof(lookups)/sizeof(lookups[0]);
struct replace {
const char* tag;
const char* string;
} replaces[] =
{
{"<TABLE_PREFIX>", opt_table_prefix},
{"<NDBINFO_DB>", opt_ndbinfo_db},
};
size_t num_replaces = sizeof(replaces)/sizeof(replaces[0]);
BaseString replace_tags(const char* str)
{
BaseString result(str);
for (size_t i = 0; i < num_replaces; i++)
{
Vector<BaseString> parts;
const char* p = result.c_str();
const char* tag = replaces[i].tag;
/* Split on <tag> */
const char* first;
while((first = strstr(p, tag)))
{
BaseString part;
part.assign(p, first - p);
parts.push_back(part);
p = first + strlen(tag);
}
parts.push_back(p);
/* Put back together */
BaseString res;
const char* separator = "";
for (unsigned j = 0; j < parts.size(); j++)
{
res.appfmt("%s%s", separator, parts[j].c_str());
separator = replaces[i].string;
}
/* Save result from this loop */
result = res;
}
return result;
}
static void
print_conditional_sql(const BaseString& sql)
{
printf("SET @str=IF(@have_ndbinfo,'%s','SET @dummy = 0');\n",
sql.c_str());
printf("PREPARE stmt FROM @str;\n");
printf("EXECUTE stmt;\n");
printf("DROP PREPARE stmt;\n\n");
}
int main(int argc, char** argv){
BaseString sql;
if ((handle_options(&argc, &argv, my_long_options, NULL)))
return 2;
printf("#\n");
printf("# SQL commands for creating the tables in MySQL Server which\n");
printf("# are used by the NDBINFO storage engine to access system\n");
printf("# information and statistics from MySQL Cluster\n");
printf("#\n");
printf("# Only create objects if NDBINFO is supported\n");
printf("SELECT @have_ndbinfo:= COUNT(*) FROM "
"information_schema.engines WHERE engine='NDBINFO' "
"AND support IN ('YES', 'DEFAULT');\n\n");
printf("# Only create objects if version >= 7.1\n");
sql.assfmt("SELECT @have_ndbinfo:="
" (@@ndbinfo_version >= (7 << 16) | (1 << 8)) || @ndbinfo_skip_version_check");
print_conditional_sql(sql);
printf("# Only create objects if ndbinfo namespace is free\n");
sql.assfmt("SET @@ndbinfo_show_hidden=TRUE");
print_conditional_sql(sql);
sql.assfmt("SELECT @have_ndbinfo:= COUNT(*) = 0"
" FROM information_schema.tables WHERE"
" table_schema = @@ndbinfo_database AND"
" LEFT(table_name, LENGTH(@@ndbinfo_table_prefix)) ="
" @@ndbinfo_table_prefix AND"
" engine != \"ndbinfo\"");
print_conditional_sql(sql);
sql.assfmt("SET @@ndbinfo_show_hidden=default");
print_conditional_sql(sql);
sql.assfmt("CREATE DATABASE IF NOT EXISTS `%s`", opt_ndbinfo_db);
print_conditional_sql(sql);
printf("# Set NDBINFO in offline mode during (re)create of tables\n");
printf("# and views to avoid errors caused by no such table or\n");
printf("# different table definition in NDB\n");
sql.assfmt("SET @@global.ndbinfo_offline=TRUE");
print_conditional_sql(sql);
printf("# Drop any old views in %s\n", opt_ndbinfo_db);
for (size_t i = 0; i < num_views; i++)
{
sql.assfmt("DROP VIEW IF EXISTS %s.%s",
opt_ndbinfo_db, views[i].name);
print_conditional_sql(sql);
}
printf("# Drop any old lookup tables in %s\n", opt_ndbinfo_db);
for (size_t i = 0; i < num_lookups; i++)
{
sql.assfmt("DROP TABLE IF EXISTS %s.%s",
opt_ndbinfo_db, lookups[i].name);
print_conditional_sql(sql);
}
for (int i = 0; i < Ndbinfo::getNumTables(); i++)
{
const Ndbinfo::Table& table = Ndbinfo::getTable(i);
printf("# %s.%s%s\n",
opt_ndbinfo_db, opt_table_prefix, table.m.name);
/* Drop the table if it exists */
sql.assfmt("DROP TABLE IF EXISTS `%s`.`%s%s`",
opt_ndbinfo_db, opt_table_prefix, table.m.name);
print_conditional_sql(sql);
/* Create the table */
sql.assfmt("CREATE TABLE `%s`.`%s%s` (",
opt_ndbinfo_db, opt_table_prefix, table.m.name);
const char* separator = "";
for(int j = 0; j < table.m.ncols ; j++)
{
const Ndbinfo::Column& col = table.col[j];
sql.appfmt("%s", separator);
separator = ",";
sql.appfmt("`%s` ", col.name);
switch(col.coltype)
{
case Ndbinfo::Number:
sql.appfmt("INT UNSIGNED");
break;
case Ndbinfo:: Number64:
sql.appfmt("BIGINT UNSIGNED");
break;
case Ndbinfo::String:
sql.appfmt("VARCHAR(512)");
break;
default:
fprintf(stderr, "unknown coltype: %d\n", col.coltype);
abort();
break;
}
if (col.comment[0] != '\0')
sql.appfmt(" COMMENT \"%s\"", col.comment);
}
sql.appfmt(") COMMENT=\"%s\" ENGINE=NDBINFO", table.m.comment);
print_conditional_sql(sql);
}
for (size_t i = 0; i < num_lookups; i++)
{
lookup l = lookups[i];
printf("# %s.%s\n", opt_ndbinfo_db, l.name);
/* Create lookup table */
sql.assfmt("CREATE TABLE `%s`.`%s` (%s)",
opt_ndbinfo_db, l.name, l.columns);
print_conditional_sql(sql);
/* Insert data */
sql.assfmt("INSERT INTO `%s`.`%s` VALUES ",
opt_ndbinfo_db, l.name);
l.fill(sql);
print_conditional_sql(sql);
}
for (size_t i = 0; i < num_views; i++)
{
view v = views[i];
printf("# %s.%s\n", opt_ndbinfo_db, v.name);
BaseString view_sql = replace_tags(v.sql);
/* Create or replace the view */
BaseString sql;
sql.assfmt("CREATE OR REPLACE DEFINER=`root@localhost` "
"SQL SECURITY INVOKER VIEW `%s`.`%s` AS %s",
opt_ndbinfo_db, v.name, view_sql.c_str());
print_conditional_sql(sql);
}
printf("# Finally turn off offline mode\n");
sql.assfmt("SET @@global.ndbinfo_offline=FALSE");
print_conditional_sql(sql);
return 0;
}