~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/mgmapi/ndb_logevent.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <ndb_global.h>
 
17
#include <my_sys.h>
 
18
#include <mgmapi.h>
 
19
 
 
20
#include <NdbOut.hpp>
 
21
#include <Properties.hpp>
 
22
#include <socket_io.h>
 
23
#include <InputStream.hpp>
 
24
 
 
25
#include <debugger/EventLogger.hpp>
 
26
 
 
27
#include "ndb_logevent.hpp"
 
28
 
 
29
extern
 
30
int ndb_mgm_listen_event_internal(NdbMgmHandle, const int filter[], int);
 
31
 
 
32
struct ndb_logevent_error_msg {
 
33
  enum ndb_logevent_handle_error code;
 
34
  const char *msg;
 
35
};
 
36
 
 
37
struct ndb_logevent_error_msg ndb_logevent_error_messages[]= {
 
38
  { NDB_LEH_READ_ERROR,              "Read error" },
 
39
  { NDB_LEH_MISSING_EVENT_SPECIFIER, "Missing event specifier" },
 
40
  { NDB_LEH_UNKNOWN_EVENT_VARIABLE,  "Unknown event variable" },
 
41
  { NDB_LEH_UNKNOWN_EVENT_TYPE,      "Unknown event type" },
 
42
  { NDB_LEH_INTERNAL_ERROR,          "Unknown internal error" },
 
43
  { NDB_LEH_NO_ERROR,0}
 
44
};
 
45
 
 
46
struct ndb_logevent_handle {
 
47
  NDB_SOCKET_TYPE socket;
 
48
  enum ndb_logevent_handle_error m_error;
 
49
};
 
50
 
 
51
extern "C"
 
52
NdbLogEventHandle
 
53
ndb_mgm_create_logevent_handle(NdbMgmHandle mh,
 
54
                               const int filter[])
 
55
{
 
56
  int fd= ndb_mgm_listen_event_internal(mh, filter, 1);
 
57
 
 
58
  if (fd == -1)
 
59
    return 0;
 
60
 
 
61
  NdbLogEventHandle h=
 
62
    (NdbLogEventHandle)my_malloc(sizeof(ndb_logevent_handle),MYF(MY_WME));
 
63
 
 
64
  h->socket= fd;
 
65
 
 
66
  return h;
 
67
}
 
68
 
 
69
extern "C"
 
70
int
 
71
ndb_logevent_get_fd(const NdbLogEventHandle h)
 
72
{
 
73
  return h->socket;
 
74
}
 
75
 
 
76
extern "C"
 
77
void ndb_mgm_destroy_logevent_handle(NdbLogEventHandle * h)
 
78
{
 
79
  if( !h )
 
80
    return;
 
81
 
 
82
  if ( *h )
 
83
    close((*h)->socket);
 
84
 
 
85
  my_free((char*)* h,MYF(MY_ALLOW_ZERO_PTR));
 
86
  * h = 0;
 
87
}
 
88
 
 
89
#define ROW(a,b,c,d) \
 
90
{ NDB_LE_ ## a,  b, c, 0, offsetof(struct ndb_logevent, a.d), \
 
91
  sizeof(((struct ndb_logevent *)0)->a.d) }
 
92
 
 
93
#define ROW_FN(a,b,c,d,e) \
 
94
{ NDB_LE_ ## a,  b, c, e, offsetof(struct ndb_logevent, a.d), \
 
95
  sizeof(((struct ndb_logevent *)0)->a.d) }
 
96
 
 
97
static int ref_to_node(int ref){
 
98
  return ref & 0xFFFF;
 
99
}
 
100
 
 
101
struct Ndb_logevent_body_row ndb_logevent_body[]= {
 
102
 
 
103
  // Connection
 
104
  ROW( Connected,           "node",          1, node),
 
105
 
 
106
  ROW( Disconnected,        "node",          1, node),
 
107
 
 
108
  ROW( CommunicationClosed, "node",          1, node),
 
109
 
 
110
  ROW( CommunicationOpened, "node",          1, node),
 
111
 
 
112
  ROW( ConnectedApiVersion, "node",          1, node),
 
113
  ROW( ConnectedApiVersion, "version",       2, version),
 
114
 
 
115
      /* CHECKPOINT */
 
116
 
 
117
  ROW( GlobalCheckpointStarted, "gci", 1, gci),
 
118
 
 
119
  ROW( GlobalCheckpointCompleted, "gci", 1, gci),
 
120
 
 
121
  ROW( LocalCheckpointStarted, "lci",         1, lci),
 
122
  ROW( LocalCheckpointStarted, "keep_gci",    2, keep_gci),
 
123
  ROW( LocalCheckpointStarted, "restore_gci", 3, restore_gci),
 
124
 
 
125
  ROW( LocalCheckpointCompleted, "lci", 1, lci),
 
126
 
 
127
  ROW( LCPStoppedInCalcKeepGci, "data", 1, data),
 
128
 
 
129
  ROW( LCPFragmentCompleted, "node",        1, node),
 
130
  ROW( LCPFragmentCompleted, "table_id",    2, table_id),
 
131
  ROW( LCPFragmentCompleted, "fragment_id", 3, fragment_id),
 
132
 
 
133
  ROW( UndoLogBlocked, "acc_count", 1, acc_count),
 
134
  ROW( UndoLogBlocked, "tup_count", 2, tup_count),
 
135
 
 
136
      /* STARTUP */
 
137
  ROW( NDBStartStarted, "version", 1, version),
 
138
 
 
139
  ROW( NDBStartCompleted, "version", 1, version),
 
140
 
 
141
//  ROW( STTORRYRecieved),
 
142
 
 
143
  ROW( StartPhaseCompleted, "phase",     1, phase),
 
144
  ROW( StartPhaseCompleted, "starttype", 2, starttype),
 
145
 
 
146
  ROW( CM_REGCONF, "own_id",       1, own_id),
 
147
  ROW( CM_REGCONF, "president_id", 2, president_id),
 
148
  ROW( CM_REGCONF, "dynamic_id",   3, dynamic_id),
 
149
 
 
150
  ROW( CM_REGREF, "own_id",   1, own_id),
 
151
  ROW( CM_REGREF, "other_id", 2, other_id),
 
152
  ROW( CM_REGREF, "cause",    3, cause),
 
153
 
 
154
  ROW( FIND_NEIGHBOURS, "own_id",     1, own_id),
 
155
  ROW( FIND_NEIGHBOURS, "left_id",    3, left_id),
 
156
  ROW( FIND_NEIGHBOURS, "right_id",   3, right_id),
 
157
  ROW( FIND_NEIGHBOURS, "dynamic_id", 4, dynamic_id),
 
158
 
 
159
  ROW( NDBStopStarted, "stoptype", 1, stoptype),
 
160
 
 
161
  ROW( NDBStopCompleted, "action",         1, action),
 
162
  ROW( NDBStopCompleted, "signum",         2, signum),
 
163
 
 
164
  ROW( NDBStopForced, "action",         1, action),
 
165
  ROW( NDBStopForced, "signum",         2, signum),
 
166
  ROW( NDBStopForced, "error",          3, error),
 
167
  ROW( NDBStopForced, "sphase",         4, sphase),
 
168
  ROW( NDBStopForced, "extra",          5, extra),
 
169
 
 
170
//  ROW( NDBStopAborted),
 
171
 
 
172
  ROW( StartREDOLog, "node",           1, node),
 
173
  ROW( StartREDOLog, "keep_gci",       2, keep_gci),
 
174
  ROW( StartREDOLog, "completed_gci",  3, completed_gci),
 
175
  ROW( StartREDOLog, "restorable_gci", 4, restorable_gci),
 
176
 
 
177
  ROW( StartLog, "log_part", 1, log_part),
 
178
  ROW( StartLog, "start_mb", 2, start_mb),
 
179
  ROW( StartLog, "stop_mb",  3, stop_mb),
 
180
  ROW( StartLog, "gci",      4, gci),
 
181
 
 
182
  ROW( UNDORecordsExecuted, "block", 1, block),
 
183
  ROW( UNDORecordsExecuted, "data1", 2, data1),
 
184
  ROW( UNDORecordsExecuted, "data2", 3, data2),
 
185
  ROW( UNDORecordsExecuted, "data3", 4, data3),
 
186
  ROW( UNDORecordsExecuted, "data4", 5, data4),
 
187
  ROW( UNDORecordsExecuted, "data5", 6, data5),
 
188
  ROW( UNDORecordsExecuted, "data6", 7, data6),
 
189
  ROW( UNDORecordsExecuted, "data7", 8, data7),
 
190
  ROW( UNDORecordsExecuted, "data8", 9, data8),
 
191
  ROW( UNDORecordsExecuted, "data9", 10, data9),
 
192
  ROW( UNDORecordsExecuted, "data10", 11, data10),
 
193
  
 
194
      /* NODERESTART */
 
195
//  ROW( NR_CopyDict),
 
196
 
 
197
//  ROW( NR_CopyDistr),
 
198
 
 
199
  ROW( NR_CopyFragsStarted, "dest_node", 1, dest_node),
 
200
 
 
201
  ROW( NR_CopyFragDone, "dest_node",   1, dest_node),
 
202
  ROW( NR_CopyFragDone, "table_id",    2, table_id),
 
203
  ROW( NR_CopyFragDone, "fragment_id", 3, fragment_id),
 
204
 
 
205
  ROW( NR_CopyFragsCompleted, "dest_node", 1, dest_node),
 
206
 
 
207
  ROW( NodeFailCompleted, "block",           1, block), /* 0 = all */
 
208
  ROW( NodeFailCompleted, "failed_node",     2, failed_node),
 
209
  ROW( NodeFailCompleted, "completing_node", 3, completing_node), /* 0 = all */
 
210
 
 
211
  ROW( NODE_FAILREP, "failed_node",   1, failed_node),
 
212
  ROW( NODE_FAILREP, "failure_state", 2, failure_state),
 
213
 
 
214
        /* TODO */
 
215
  ROW( ArbitState,   "code",          1, code),
 
216
  ROW( ArbitState,   "arbit_node",    2, arbit_node),
 
217
  ROW( ArbitState,   "ticket_0",      3, ticket_0),
 
218
  ROW( ArbitState,   "ticket_1",      4, ticket_1),
 
219
 
 
220
        /* TODO */
 
221
  ROW( ArbitResult,  "code",          1, code),
 
222
  ROW( ArbitResult,  "arbit_node",    2, arbit_node),
 
223
  ROW( ArbitResult,  "ticket_0",      3, ticket_0),
 
224
  ROW( ArbitResult,  "ticket_1",      4, ticket_1),
 
225
 
 
226
//  ROW( GCP_TakeoverStarted),
 
227
 
 
228
//  ROW( GCP_TakeoverCompleted),
 
229
 
 
230
//  ROW( LCP_TakeoverStarted),
 
231
 
 
232
  ROW( LCP_TakeoverCompleted, "state", 1, state),
 
233
 
 
234
      /* STATISTIC */
 
235
  ROW( TransReportCounters, "trans_count",       1, trans_count),
 
236
  ROW( TransReportCounters, "commit_count",      2, commit_count),
 
237
  ROW( TransReportCounters, "read_count",        3, read_count),
 
238
  ROW( TransReportCounters, "simple_read_count", 4, simple_read_count),
 
239
  ROW( TransReportCounters, "write_count",       5, write_count),
 
240
  ROW( TransReportCounters, "attrinfo_count",    6, attrinfo_count),
 
241
  ROW( TransReportCounters, "conc_op_count",     7, conc_op_count),
 
242
  ROW( TransReportCounters, "abort_count",       8, abort_count),
 
243
  ROW( TransReportCounters, "scan_count",        9, scan_count),
 
244
  ROW( TransReportCounters, "range_scan_count", 10, range_scan_count),
 
245
 
 
246
  ROW( OperationReportCounters, "ops", 1, ops),
 
247
 
 
248
  ROW( TableCreated, "table_id", 1, table_id),
 
249
 
 
250
  ROW( JobStatistic, "mean_loop_count", 1, mean_loop_count),
 
251
 
 
252
  ROW( SendBytesStatistic, "to_node",         1, to_node),
 
253
  ROW( SendBytesStatistic, "mean_sent_bytes", 2, mean_sent_bytes),
 
254
 
 
255
  ROW( ReceiveBytesStatistic, "from_node",           1, from_node),
 
256
  ROW( ReceiveBytesStatistic, "mean_received_bytes", 2, mean_received_bytes),
 
257
 
 
258
  ROW( MemoryUsage, "gth",          1, gth),
 
259
  ROW( MemoryUsage, "page_size_bytes", 2, page_size_bytes),
 
260
  ROW( MemoryUsage, "pages_used",   3, pages_used),
 
261
  ROW( MemoryUsage, "pages_total",  4, pages_total),
 
262
  ROW( MemoryUsage, "block",        5, block),
 
263
 
 
264
      /* ERROR */
 
265
  ROW( TransporterError, "to_node", 1, to_node),
 
266
  ROW( TransporterError, "code",    2, code),
 
267
 
 
268
  ROW( TransporterWarning, "to_node", 1, to_node),
 
269
  ROW( TransporterWarning, "code",    2, code),
 
270
 
 
271
  ROW( MissedHeartbeat, "node",  1, node),
 
272
  ROW( MissedHeartbeat, "count", 2, count),
 
273
 
 
274
  ROW( DeadDueToHeartbeat, "node", 1, node),
 
275
 
 
276
        /* TODO */
 
277
//  ROW( WarningEvent),
 
278
 
 
279
      /* INFO */
 
280
  ROW( SentHeartbeat, "node", 1, node),
 
281
 
 
282
  ROW( CreateLogBytes, "node", 1, node),
 
283
 
 
284
        /* TODO */
 
285
//  ROW( InfoEvent),
 
286
 
 
287
  ROW( EventBufferStatus, "usage",        1, usage),
 
288
  ROW( EventBufferStatus, "alloc",        2, alloc),
 
289
  ROW( EventBufferStatus, "max",          3, max),
 
290
  ROW( EventBufferStatus, "apply_gci_l",  4, apply_gci_l),
 
291
  ROW( EventBufferStatus, "apply_gci_h",  5, apply_gci_h),
 
292
  ROW( EventBufferStatus, "latest_gci_l", 6, latest_gci_l),
 
293
  ROW( EventBufferStatus, "latest_gci_h", 7, latest_gci_h),
 
294
 
 
295
  // Backup
 
296
  ROW_FN( BackupStarted,    "starting_node", 1, starting_node, ref_to_node),
 
297
  ROW( BackupStarted,       "backup_id",     2, backup_id),
 
298
 
 
299
  ROW_FN(BackupFailedToStart,"starting_node",1, starting_node, ref_to_node),
 
300
  ROW( BackupFailedToStart, "error",         2, error),
 
301
 
 
302
  ROW_FN( BackupCompleted,  "starting_node", 1, starting_node, ref_to_node),
 
303
  ROW( BackupCompleted,     "backup_id",     2, backup_id), 
 
304
  ROW( BackupCompleted,     "start_gci",     3, start_gci),
 
305
  ROW( BackupCompleted,     "stop_gci",      4, stop_gci),
 
306
  ROW( BackupCompleted,     "n_bytes",       5, n_bytes),
 
307
  ROW( BackupCompleted,     "n_records",     6, n_records), 
 
308
  ROW( BackupCompleted,     "n_log_bytes",   7, n_log_bytes),
 
309
  ROW( BackupCompleted,     "n_log_records", 8, n_log_records),
 
310
 
 
311
  ROW_FN( BackupAborted,    "starting_node", 1, starting_node, ref_to_node),
 
312
  ROW( BackupAborted,       "backup_id",     2, backup_id),
 
313
  ROW( BackupAborted,       "error",         3, error),
 
314
 
 
315
  ROW( SingleUser,          "type",          1, type),
 
316
  ROW( SingleUser,          "node_id",       2, node_id),
 
317
  { NDB_LE_ILLEGAL_TYPE, 0, 0, 0, 0, 0}
 
318
};
 
319
 
 
320
struct Ndb_logevent_header_row {
 
321
  const char            *token;    // token to use for text transfer
 
322
  int                    offset;   // offset into struct ndb_logevent
 
323
  int                    size;
 
324
};
 
325
 
 
326
#define ROW2(a,b) \
 
327
{ a, offsetof(struct ndb_logevent, b), \
 
328
  sizeof(((struct ndb_logevent *)0)->b) }
 
329
 
 
330
struct Ndb_logevent_header_row ndb_logevent_header[]= {
 
331
  ROW2( "type",          type),
 
332
  ROW2( "time",          time),
 
333
  ROW2( "source_nodeid", source_nodeid),
 
334
  { 0, 0, 0 }
 
335
};
 
336
 
 
337
static int
 
338
insert_row(const char * pair, Properties & p){
 
339
  BaseString tmp(pair);
 
340
  
 
341
  tmp.trim(" \t\n\r");
 
342
  Vector<BaseString> split;
 
343
  tmp.split(split, ":=", 2);
 
344
  if(split.size() != 2)
 
345
    return -1;
 
346
  p.put(split[0].trim().c_str(), split[1].trim().c_str());
 
347
 
 
348
  return 0;
 
349
}
 
350
 
 
351
static
 
352
int memcpy_atoi(void *dst, const char *str, int sz)
 
353
{
 
354
  switch (sz)
 
355
  {
 
356
  case 1:
 
357
  {
 
358
    Int8 val= atoi(str);
 
359
    memcpy(dst,&val,sz);
 
360
    return 0;
 
361
  }
 
362
  case 2:
 
363
  {
 
364
    Int16 val= atoi(str);
 
365
    memcpy(dst,&val,sz);
 
366
    return 0;
 
367
  }
 
368
  case 4:
 
369
  {
 
370
    Int32 val= atoi(str);
 
371
    memcpy(dst,&val,sz);
 
372
    return 0;
 
373
  }
 
374
  case 8:
 
375
  {
 
376
    Int64 val= atoi(str);
 
377
    memcpy(dst,&val,sz);
 
378
    return 0;
 
379
  }
 
380
  default:
 
381
  {
 
382
    return -1;
 
383
  }
 
384
  }
 
385
}
 
386
 
 
387
extern "C"
 
388
int ndb_logevent_get_next(const NdbLogEventHandle h,
 
389
                          struct ndb_logevent *dst,
 
390
                          unsigned timeout_in_milliseconds)
 
391
{
 
392
  if (timeout_in_milliseconds == 0)
 
393
  {
 
394
    int res;
 
395
    while ((res = ndb_logevent_get_next(h, dst, 60000))==0);
 
396
    return res;
 
397
  }
 
398
 
 
399
  SocketInputStream in(h->socket, timeout_in_milliseconds);
 
400
 
 
401
  Properties p;
 
402
  char buf[256];
 
403
 
 
404
  /* header */
 
405
  while (1) {
 
406
    if (in.gets(buf,sizeof(buf)) == 0)
 
407
    {
 
408
      h->m_error= NDB_LEH_READ_ERROR;
 
409
      return -1;
 
410
    }
 
411
    if ( buf[0] == 0 )
 
412
    {
 
413
      // timed out
 
414
      return 0;
 
415
    }
 
416
 
 
417
    if ( strcmp("log event reply\n", buf) == 0 )
 
418
      break;
 
419
 
 
420
    if ( strcmp("<PING>\n", buf) )
 
421
      ndbout_c("skipped: %s", buf);
 
422
 
 
423
    if(in.timedout())
 
424
        return 0;
 
425
  }
 
426
 
 
427
  /* read name-value pairs into properties object */
 
428
  while (1)
 
429
  {
 
430
    if (in.gets(buf,sizeof(buf)) == 0)
 
431
    {
 
432
      h->m_error= NDB_LEH_READ_ERROR;
 
433
      return -1;
 
434
    }
 
435
    if (in.timedout())
 
436
      return 0;
 
437
 
 
438
    if ( buf[0] == '\n' )
 
439
    {
 
440
      break;
 
441
    }
 
442
    if (insert_row(buf,p))
 
443
    {
 
444
      h->m_error= NDB_LEH_READ_ERROR;
 
445
      return -1;
 
446
    }
 
447
  }
 
448
 
 
449
  int i;
 
450
  const char *val;
 
451
 
 
452
  dst->type= (enum Ndb_logevent_type)-1;
 
453
  /* fill in header info from p*/
 
454
  for (i= 0; ndb_logevent_header[i].token; i++)
 
455
  {
 
456
    if ( p.get(ndb_logevent_header[i].token, &val) == 0 )
 
457
    {
 
458
      ndbout_c("missing: %s\n", ndb_logevent_header[i].token);
 
459
      h->m_error= NDB_LEH_MISSING_EVENT_SPECIFIER;
 
460
      return -1;
 
461
    }
 
462
    if ( memcpy_atoi((char *)dst+ndb_logevent_header[i].offset, val,
 
463
                     ndb_logevent_header[i].size) )
 
464
    {
 
465
      h->m_error= NDB_LEH_INTERNAL_ERROR;
 
466
      return -1;
 
467
    }
 
468
  }
 
469
 
 
470
  Uint32                             level;
 
471
  LogLevel::EventCategory            category;
 
472
  Logger::LoggerLevel                severity;
 
473
  EventLoggerBase::EventTextFunction text_fn;
 
474
 
 
475
  /* fill in rest of header info event_lookup */
 
476
  if (EventLoggerBase::event_lookup(dst->type,category,level,severity,text_fn))
 
477
  {
 
478
    ndbout_c("unknown type: %d\n", dst->type);
 
479
    h->m_error= NDB_LEH_UNKNOWN_EVENT_TYPE;
 
480
    return -1;
 
481
  }
 
482
  dst->category= (enum ndb_mgm_event_category)category;
 
483
  dst->severity= (enum ndb_mgm_event_severity)severity;
 
484
  dst->level=    level;
 
485
 
 
486
  /* fill in header info from p */
 
487
  for (i= 0; ndb_logevent_body[i].token; i++)
 
488
  {
 
489
    if ( ndb_logevent_body[i].type != dst->type )
 
490
      continue;
 
491
    if ( p.get(ndb_logevent_body[i].token, &val) == 0 )
 
492
    {
 
493
      h->m_error= NDB_LEH_UNKNOWN_EVENT_VARIABLE;
 
494
      return -1;
 
495
    }
 
496
    if ( memcpy_atoi((char *)dst+ndb_logevent_body[i].offset, val,
 
497
                     ndb_logevent_body[i].size) )
 
498
    {
 
499
      h->m_error= NDB_LEH_INTERNAL_ERROR;
 
500
      return -1;
 
501
    }
 
502
  }
 
503
  return 1;
 
504
}
 
505
 
 
506
extern "C"
 
507
int ndb_logevent_get_latest_error(const NdbLogEventHandle h)
 
508
{
 
509
  return h->m_error;
 
510
}
 
511
 
 
512
extern "C"
 
513
const char *ndb_logevent_get_latest_error_msg(const NdbLogEventHandle h)
 
514
{
 
515
  for (int i= 0; ndb_logevent_error_messages[i].msg; i++)
 
516
    if (ndb_logevent_error_messages[i].code == h->m_error)
 
517
      return ndb_logevent_error_messages[i].msg;
 
518
  return "<unknown error msg>";
 
519
}