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

« back to all changes in this revision

Viewing changes to storage/ndb/test/ndbapi/testMgm.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 <NDBT.hpp>
 
17
#include <NDBT_Test.hpp>
 
18
#include <HugoTransactions.hpp>
 
19
#include <UtilTransactions.hpp>
 
20
#include <NdbRestarter.hpp>
 
21
#include <Vector.hpp>
 
22
#include <random.h>
 
23
#include <mgmapi.h>
 
24
#include <mgmapi_debug.h>
 
25
#include <ndb_logevent.h>
 
26
#include <InputStream.hpp>
 
27
#include <signaldata/EventReport.hpp>
 
28
 
 
29
int runLoadTable(NDBT_Context* ctx, NDBT_Step* step){
 
30
 
 
31
  int records = ctx->getNumRecords();
 
32
  HugoTransactions hugoTrans(*ctx->getTab());
 
33
  if (hugoTrans.loadTable(GETNDB(step), records) != 0){
 
34
    return NDBT_FAILED;
 
35
  }
 
36
  return NDBT_OK;
 
37
}
 
38
 
 
39
int runClearTable(NDBT_Context* ctx, NDBT_Step* step){
 
40
  int records = ctx->getNumRecords();
 
41
  
 
42
  UtilTransactions utilTrans(*ctx->getTab());
 
43
  if (utilTrans.clearTable2(GETNDB(step),  records) != 0){
 
44
    return NDBT_FAILED;
 
45
  }
 
46
  return NDBT_OK;
 
47
}
 
48
 
 
49
 
 
50
int create_index_on_pk(Ndb* pNdb, const char* tabName){
 
51
  int result  = NDBT_OK;
 
52
 
 
53
  const NdbDictionary::Table * tab = NDBT_Table::discoverTableFromDb(pNdb,
 
54
                                                                     tabName);
 
55
 
 
56
  // Create index      
 
57
  const char* idxName = "IDX_ON_PK";
 
58
  ndbout << "Create: " <<idxName << "( ";
 
59
  NdbDictionary::Index pIdx(idxName);
 
60
  pIdx.setTable(tabName);
 
61
  pIdx.setType(NdbDictionary::Index::UniqueHashIndex);
 
62
  for (int c = 0; c< tab->getNoOfPrimaryKeys(); c++){    
 
63
    pIdx.addIndexColumn(tab->getPrimaryKey(c));
 
64
    ndbout << tab->getPrimaryKey(c)<<" ";
 
65
  }
 
66
  
 
67
  ndbout << ") ";
 
68
  if (pNdb->getDictionary()->createIndex(pIdx) != 0){
 
69
    ndbout << "FAILED!" << endl;
 
70
    const NdbError err = pNdb->getDictionary()->getNdbError();
 
71
    ERR(err);
 
72
    result = NDBT_FAILED;
 
73
  } else {
 
74
    ndbout << "OK!" << endl;
 
75
  }
 
76
  return result;
 
77
}
 
78
 
 
79
int drop_index_on_pk(Ndb* pNdb, const char* tabName){
 
80
  int result = NDBT_OK;
 
81
  const char* idxName = "IDX_ON_PK";
 
82
  ndbout << "Drop: " << idxName;
 
83
  if (pNdb->getDictionary()->dropIndex(idxName, tabName) != 0){
 
84
    ndbout << "FAILED!" << endl;
 
85
    const NdbError err = pNdb->getDictionary()->getNdbError();
 
86
    ERR(err);
 
87
    result = NDBT_FAILED;
 
88
  } else {
 
89
    ndbout << "OK!" << endl;
 
90
  }
 
91
  return result;
 
92
}
 
93
 
 
94
 
 
95
#define CHECK(b) if (!(b)) { \
 
96
  g_err << "ERR: "<< step->getName() \
 
97
         << " failed on line " << __LINE__ << endl; \
 
98
  result = NDBT_FAILED; \
 
99
  continue; } 
 
100
 
 
101
int runTestSingleUserMode(NDBT_Context* ctx, NDBT_Step* step){
 
102
  int result = NDBT_OK;
 
103
  int loops = ctx->getNumLoops();
 
104
  int records = ctx->getNumRecords();
 
105
  Ndb* pNdb = GETNDB(step);
 
106
  NdbRestarter restarter;
 
107
  char tabName[255];
 
108
  strncpy(tabName, ctx->getTab()->getName(), 255);
 
109
  ndbout << "tabName="<<tabName<<endl;
 
110
  
 
111
  int i = 0;
 
112
  int count;
 
113
  HugoTransactions hugoTrans(*ctx->getTab());
 
114
  UtilTransactions utilTrans(*ctx->getTab());
 
115
  while (i<loops && result == NDBT_OK) {
 
116
    g_info << i << ": ";
 
117
    int timeout = 120;
 
118
    // Test that the single user mode api can do everything     
 
119
    CHECK(restarter.enterSingleUserMode(pNdb->getNodeId()) == 0);
 
120
    CHECK(restarter.waitClusterSingleUser(timeout) == 0); 
 
121
    CHECK(hugoTrans.loadTable(pNdb, records, 128) == 0);
 
122
    CHECK(hugoTrans.pkReadRecords(pNdb, records) == 0);
 
123
    CHECK(hugoTrans.pkUpdateRecords(pNdb, records) == 0);
 
124
    CHECK(utilTrans.selectCount(pNdb, 64, &count) == 0);
 
125
    CHECK(count == records);
 
126
    CHECK(hugoTrans.pkDelRecords(pNdb, records/2) == 0);
 
127
    CHECK(hugoTrans.scanReadRecords(pNdb, records/2, 0, 64) == 0);
 
128
    CHECK(utilTrans.selectCount(pNdb, 64, &count) == 0);
 
129
    CHECK(count == (records/2));
 
130
    CHECK(utilTrans.clearTable(pNdb, records/2) == 0);
 
131
    CHECK(restarter.exitSingleUserMode() == 0);
 
132
    CHECK(restarter.waitClusterStarted(timeout) == 0); 
 
133
 
 
134
    // Test create index in single user mode
 
135
    CHECK(restarter.enterSingleUserMode(pNdb->getNodeId()) == 0);
 
136
    CHECK(restarter.waitClusterSingleUser(timeout) == 0); 
 
137
    CHECK(create_index_on_pk(pNdb, tabName) == 0);
 
138
    CHECK(hugoTrans.loadTable(pNdb, records, 128) == 0);
 
139
    CHECK(hugoTrans.pkReadRecords(pNdb, records) == 0);
 
140
    CHECK(hugoTrans.pkUpdateRecords(pNdb, records) == 0);
 
141
    CHECK(utilTrans.selectCount(pNdb, 64, &count) == 0);
 
142
    CHECK(count == records);
 
143
    CHECK(hugoTrans.pkDelRecords(pNdb, records/2) == 0);
 
144
    CHECK(drop_index_on_pk(pNdb, tabName) == 0);          
 
145
    CHECK(restarter.exitSingleUserMode() == 0);
 
146
    CHECK(restarter.waitClusterStarted(timeout) == 0); 
 
147
 
 
148
    // Test recreate index in single user mode
 
149
    CHECK(create_index_on_pk(pNdb, tabName) == 0);
 
150
    CHECK(hugoTrans.loadTable(pNdb, records, 128) == 0);
 
151
    CHECK(utilTrans.selectCount(pNdb, 64, &count) == 0);
 
152
    CHECK(restarter.enterSingleUserMode(pNdb->getNodeId()) == 0);
 
153
    CHECK(restarter.waitClusterSingleUser(timeout) == 0); 
 
154
    CHECK(drop_index_on_pk(pNdb, tabName) == 0);        
 
155
    CHECK(utilTrans.selectCount(pNdb, 64, &count) == 0);
 
156
    CHECK(create_index_on_pk(pNdb, tabName) == 0);
 
157
    CHECK(restarter.exitSingleUserMode() == 0);
 
158
    CHECK(restarter.waitClusterStarted(timeout) == 0); 
 
159
    CHECK(drop_index_on_pk(pNdb, tabName) == 0);
 
160
 
 
161
    CHECK(utilTrans.clearTable(GETNDB(step),  records) == 0);
 
162
 
 
163
    ndbout << "Restarting cluster" << endl;
 
164
    CHECK(restarter.restartAll() == 0);
 
165
    CHECK(restarter.waitClusterStarted(timeout) == 0);
 
166
    CHECK(pNdb->waitUntilReady(timeout) == 0);
 
167
 
 
168
    i++;
 
169
 
 
170
  }
 
171
  return result;
 
172
}
 
173
 
 
174
int runTestApiSession(NDBT_Context* ctx, NDBT_Step* step)
 
175
{
 
176
  char *mgm= ctx->getRemoteMgm();
 
177
  Uint64 session_id= 0;
 
178
 
 
179
  NdbMgmHandle h;
 
180
  h= ndb_mgm_create_handle();
 
181
  ndb_mgm_set_connectstring(h, mgm);
 
182
  ndb_mgm_connect(h,0,0,0);
 
183
  int s= ndb_mgm_get_fd(h);
 
184
  session_id= ndb_mgm_get_session_id(h);
 
185
  ndbout << "MGM Session id: " << session_id << endl;
 
186
  write(s,"get",3);
 
187
  ndb_mgm_disconnect(h);
 
188
  ndb_mgm_destroy_handle(&h);
 
189
 
 
190
  struct NdbMgmSession sess;
 
191
  int slen= sizeof(struct NdbMgmSession);
 
192
 
 
193
  h= ndb_mgm_create_handle();
 
194
  ndb_mgm_set_connectstring(h, mgm);
 
195
  ndb_mgm_connect(h,0,0,0);
 
196
 
 
197
  NdbSleep_SecSleep(1);
 
198
 
 
199
  if(ndb_mgm_get_session(h,session_id,&sess,&slen))
 
200
  {
 
201
    ndbout << "Failed, session still exists" << endl;
 
202
    ndb_mgm_disconnect(h);
 
203
    ndb_mgm_destroy_handle(&h);
 
204
    return NDBT_FAILED;
 
205
  }
 
206
  else
 
207
  {
 
208
    ndbout << "SUCCESS: session is gone" << endl;
 
209
    ndb_mgm_disconnect(h);
 
210
    ndb_mgm_destroy_handle(&h);
 
211
    return NDBT_OK;
 
212
  }
 
213
}
 
214
 
 
215
int runTestApiConnectTimeout(NDBT_Context* ctx, NDBT_Step* step)
 
216
{
 
217
  char *mgm= ctx->getRemoteMgm();
 
218
  int result= NDBT_FAILED;
 
219
  int cc= 0;
 
220
  int mgmd_nodeid= 0;
 
221
  ndb_mgm_reply reply;
 
222
 
 
223
  NdbMgmHandle h;
 
224
  h= ndb_mgm_create_handle();
 
225
  ndb_mgm_set_connectstring(h, mgm);
 
226
 
 
227
  ndbout << "TEST connect timeout" << endl;
 
228
 
 
229
  ndb_mgm_set_timeout(h, 3000);
 
230
 
 
231
  struct timeval tstart, tend;
 
232
  int secs;
 
233
  timerclear(&tstart);
 
234
  timerclear(&tend);
 
235
  gettimeofday(&tstart,NULL);
 
236
 
 
237
  ndb_mgm_connect(h,0,0,0);
 
238
 
 
239
  gettimeofday(&tend,NULL);
 
240
 
 
241
  secs= tend.tv_sec - tstart.tv_sec;
 
242
  ndbout << "Took about: " << secs <<" seconds"<<endl;
 
243
 
 
244
  if(secs < 4)
 
245
    result= NDBT_OK;
 
246
  else
 
247
    goto done;
 
248
 
 
249
  ndb_mgm_set_connectstring(h, mgm);
 
250
 
 
251
  ndbout << "TEST connect timeout" << endl;
 
252
 
 
253
  ndb_mgm_destroy_handle(&h);
 
254
 
 
255
  h= ndb_mgm_create_handle();
 
256
  ndb_mgm_set_connectstring(h, "1.1.1.1");
 
257
 
 
258
  ndbout << "TEST connect timeout (invalid host)" << endl;
 
259
 
 
260
  ndb_mgm_set_timeout(h, 3000);
 
261
 
 
262
  timerclear(&tstart);
 
263
  timerclear(&tend);
 
264
  gettimeofday(&tstart,NULL);
 
265
 
 
266
  ndb_mgm_connect(h,0,0,0);
 
267
 
 
268
  gettimeofday(&tend,NULL);
 
269
 
 
270
  secs= tend.tv_sec - tstart.tv_sec;
 
271
  ndbout << "Took about: " << secs <<" seconds"<<endl;
 
272
 
 
273
  if(secs < 4)
 
274
    result= NDBT_OK;
 
275
  else
 
276
    result= NDBT_FAILED;
 
277
 
 
278
done:
 
279
  ndb_mgm_disconnect(h);
 
280
  ndb_mgm_destroy_handle(&h);
 
281
 
 
282
  return result;
 
283
}
 
284
 
 
285
int runTestApiTimeoutBasic(NDBT_Context* ctx, NDBT_Step* step)
 
286
{
 
287
  char *mgm= ctx->getRemoteMgm();
 
288
  int result= NDBT_FAILED;
 
289
  int cc= 0;
 
290
  int mgmd_nodeid= 0;
 
291
  ndb_mgm_reply reply;
 
292
 
 
293
  NdbMgmHandle h;
 
294
  h= ndb_mgm_create_handle();
 
295
  ndb_mgm_set_connectstring(h, mgm);
 
296
 
 
297
  ndbout << "TEST timout check_connection" << endl;
 
298
  int errs[] = { 1, 2, 3, -1};
 
299
 
 
300
  for(int error_ins_no=0; errs[error_ins_no]!=-1; error_ins_no++)
 
301
  {
 
302
    int error_ins= errs[error_ins_no];
 
303
    ndbout << "trying error " << error_ins << endl;
 
304
    ndb_mgm_connect(h,0,0,0);
 
305
 
 
306
    if(ndb_mgm_check_connection(h) < 0)
 
307
    {
 
308
      result= NDBT_FAILED;
 
309
      goto done;
 
310
    }
 
311
 
 
312
    mgmd_nodeid= ndb_mgm_get_mgmd_nodeid(h);
 
313
    if(mgmd_nodeid==0)
 
314
    {
 
315
      ndbout << "Failed to get mgmd node id to insert error" << endl;
 
316
      result= NDBT_FAILED;
 
317
      goto done;
 
318
    }
 
319
 
 
320
    reply.return_code= 0;
 
321
 
 
322
    if(ndb_mgm_insert_error(h, mgmd_nodeid, error_ins, &reply)< 0)
 
323
    {
 
324
      ndbout << "failed to insert error " << endl;
 
325
      result= NDBT_FAILED;
 
326
      goto done;
 
327
    }
 
328
 
 
329
    ndb_mgm_set_timeout(h,2500);
 
330
 
 
331
    cc= ndb_mgm_check_connection(h);
 
332
    if(cc < 0)
 
333
      result= NDBT_OK;
 
334
    else
 
335
      result= NDBT_FAILED;
 
336
 
 
337
    if(ndb_mgm_is_connected(h))
 
338
    {
 
339
      ndbout << "FAILED: still connected" << endl;
 
340
      result= NDBT_FAILED;
 
341
    }
 
342
  }
 
343
 
 
344
  ndbout << "TEST get_mgmd_nodeid" << endl;
 
345
  ndb_mgm_connect(h,0,0,0);
 
346
 
 
347
  if(ndb_mgm_insert_error(h, mgmd_nodeid, 0, &reply)< 0)
 
348
  {
 
349
    ndbout << "failed to remove inserted error " << endl;
 
350
    result= NDBT_FAILED;
 
351
    goto done;
 
352
  }
 
353
 
 
354
  cc= ndb_mgm_get_mgmd_nodeid(h);
 
355
  ndbout << "got node id: " << cc << endl;
 
356
  if(cc==0)
 
357
  {
 
358
    ndbout << "FAILED: didn't get node id" << endl;
 
359
    result= NDBT_FAILED;
 
360
  }
 
361
  else
 
362
    result= NDBT_OK;
 
363
 
 
364
  ndbout << "TEST end_session" << endl;
 
365
  ndb_mgm_connect(h,0,0,0);
 
366
 
 
367
  if(ndb_mgm_insert_error(h, mgmd_nodeid, 4, &reply)< 0)
 
368
  {
 
369
    ndbout << "FAILED: insert error 1" << endl;
 
370
    result= NDBT_FAILED;
 
371
    goto done;
 
372
  }
 
373
 
 
374
  cc= ndb_mgm_end_session(h);
 
375
  if(cc==0)
 
376
  {
 
377
    ndbout << "FAILED: success in calling end_session" << endl;
 
378
    result= NDBT_FAILED;
 
379
  }
 
380
  else if(ndb_mgm_get_latest_error(h)!=ETIMEDOUT)
 
381
  {
 
382
    ndbout << "FAILED: Incorrect error code (" << ndb_mgm_get_latest_error(h)
 
383
           << " != expected " << ETIMEDOUT << ") desc: "
 
384
           << ndb_mgm_get_latest_error_desc(h)
 
385
           << " line: " << ndb_mgm_get_latest_error_line(h)
 
386
           << " msg: " << ndb_mgm_get_latest_error_msg(h)
 
387
           << endl;
 
388
    result= NDBT_FAILED;
 
389
  }
 
390
  else
 
391
    result= NDBT_OK;
 
392
 
 
393
  if(ndb_mgm_is_connected(h))
 
394
  {
 
395
    ndbout << "FAILED: is still connected after error" << endl;
 
396
    result= NDBT_FAILED;
 
397
  }
 
398
done:
 
399
  ndb_mgm_disconnect(h);
 
400
  ndb_mgm_destroy_handle(&h);
 
401
 
 
402
  return result;
 
403
}
 
404
 
 
405
int runTestApiGetStatusTimeout(NDBT_Context* ctx, NDBT_Step* step)
 
406
{
 
407
  char *mgm= ctx->getRemoteMgm();
 
408
  int result= NDBT_OK;
 
409
  int cc= 0;
 
410
  int mgmd_nodeid= 0;
 
411
 
 
412
  NdbMgmHandle h;
 
413
  h= ndb_mgm_create_handle();
 
414
  ndb_mgm_set_connectstring(h, mgm);
 
415
 
 
416
  int errs[] = { 0, 5, 6, 7, 8, 9, -1 };
 
417
 
 
418
  for(int error_ins_no=0; errs[error_ins_no]!=-1; error_ins_no++)
 
419
  {
 
420
    int error_ins= errs[error_ins_no];
 
421
    ndb_mgm_connect(h,0,0,0);
 
422
 
 
423
    if(ndb_mgm_check_connection(h) < 0)
 
424
    {
 
425
      result= NDBT_FAILED;
 
426
      goto done;
 
427
    }
 
428
 
 
429
    mgmd_nodeid= ndb_mgm_get_mgmd_nodeid(h);
 
430
    if(mgmd_nodeid==0)
 
431
    {
 
432
      ndbout << "Failed to get mgmd node id to insert error" << endl;
 
433
      result= NDBT_FAILED;
 
434
      goto done;
 
435
    }
 
436
 
 
437
    ndb_mgm_reply reply;
 
438
    reply.return_code= 0;
 
439
 
 
440
    if(ndb_mgm_insert_error(h, mgmd_nodeid, error_ins, &reply)< 0)
 
441
    {
 
442
      ndbout << "failed to insert error " << error_ins << endl;
 
443
      result= NDBT_FAILED;
 
444
    }
 
445
 
 
446
    ndbout << "trying error: " << error_ins << endl;
 
447
 
 
448
    ndb_mgm_set_timeout(h,2500);
 
449
 
 
450
    struct ndb_mgm_cluster_state *cl= ndb_mgm_get_status(h);
 
451
 
 
452
    if(cl!=NULL)
 
453
      free(cl);
 
454
 
 
455
    /*
 
456
     * For whatever strange reason,
 
457
     * get_status is okay with not having the last enter there.
 
458
     * instead of "fixing" the api, let's have a special case
 
459
     * so we don't break any behaviour
 
460
     */
 
461
 
 
462
    if(error_ins!=0 && error_ins!=9 && cl!=NULL)
 
463
    {
 
464
      ndbout << "FAILED: got a ndb_mgm_cluster_state back" << endl;
 
465
      result= NDBT_FAILED;
 
466
    }
 
467
 
 
468
    if(error_ins!=0 && error_ins!=9 && ndb_mgm_is_connected(h))
 
469
    {
 
470
      ndbout << "FAILED: is still connected after error" << endl;
 
471
      result= NDBT_FAILED;
 
472
    }
 
473
 
 
474
    if(error_ins!=0 && error_ins!=9 && ndb_mgm_get_latest_error(h)!=ETIMEDOUT)
 
475
    {
 
476
      ndbout << "FAILED: Incorrect error code (" << ndb_mgm_get_latest_error(h)
 
477
             << " != expected " << ETIMEDOUT << ") desc: "
 
478
             << ndb_mgm_get_latest_error_desc(h)
 
479
             << " line: " << ndb_mgm_get_latest_error_line(h)
 
480
             << " msg: " << ndb_mgm_get_latest_error_msg(h)
 
481
             << endl;
 
482
      result= NDBT_FAILED;
 
483
    }
 
484
  }
 
485
 
 
486
done:
 
487
  ndb_mgm_disconnect(h);
 
488
  ndb_mgm_destroy_handle(&h);
 
489
 
 
490
  return result;
 
491
}
 
492
 
 
493
int runTestMgmApiGetConfigTimeout(NDBT_Context* ctx, NDBT_Step* step)
 
494
{
 
495
  char *mgm= ctx->getRemoteMgm();
 
496
  int result= NDBT_OK;
 
497
  int mgmd_nodeid= 0;
 
498
 
 
499
  NdbMgmHandle h;
 
500
  h= ndb_mgm_create_handle();
 
501
  ndb_mgm_set_connectstring(h, mgm);
 
502
 
 
503
  int errs[] = { 0, 1, 2, 3, -1 };
 
504
 
 
505
  for(int error_ins_no=0; errs[error_ins_no]!=-1; error_ins_no++)
 
506
  {
 
507
    int error_ins= errs[error_ins_no];
 
508
    ndb_mgm_connect(h,0,0,0);
 
509
 
 
510
    if(ndb_mgm_check_connection(h) < 0)
 
511
    {
 
512
      result= NDBT_FAILED;
 
513
      goto done;
 
514
    }
 
515
 
 
516
    mgmd_nodeid= ndb_mgm_get_mgmd_nodeid(h);
 
517
    if(mgmd_nodeid==0)
 
518
    {
 
519
      ndbout << "Failed to get mgmd node id to insert error" << endl;
 
520
      result= NDBT_FAILED;
 
521
      goto done;
 
522
    }
 
523
 
 
524
    ndb_mgm_reply reply;
 
525
    reply.return_code= 0;
 
526
 
 
527
    if(ndb_mgm_insert_error(h, mgmd_nodeid, error_ins, &reply)< 0)
 
528
    {
 
529
      ndbout << "failed to insert error " << error_ins << endl;
 
530
      result= NDBT_FAILED;
 
531
    }
 
532
 
 
533
    ndbout << "trying error: " << error_ins << endl;
 
534
 
 
535
    ndb_mgm_set_timeout(h,2500);
 
536
 
 
537
    struct ndb_mgm_configuration *c= ndb_mgm_get_configuration(h,0);
 
538
 
 
539
    if(c!=NULL)
 
540
      free(c);
 
541
 
 
542
    if(error_ins!=0 && c!=NULL)
 
543
    {
 
544
      ndbout << "FAILED: got a ndb_mgm_configuration back" << endl;
 
545
      result= NDBT_FAILED;
 
546
    }
 
547
 
 
548
    if(error_ins!=0 && ndb_mgm_is_connected(h))
 
549
    {
 
550
      ndbout << "FAILED: is still connected after error" << endl;
 
551
      result= NDBT_FAILED;
 
552
    }
 
553
 
 
554
    if(error_ins!=0 && ndb_mgm_get_latest_error(h)!=ETIMEDOUT)
 
555
    {
 
556
      ndbout << "FAILED: Incorrect error code (" << ndb_mgm_get_latest_error(h)
 
557
             << " != expected " << ETIMEDOUT << ") desc: "
 
558
             << ndb_mgm_get_latest_error_desc(h)
 
559
             << " line: " << ndb_mgm_get_latest_error_line(h)
 
560
             << " msg: " << ndb_mgm_get_latest_error_msg(h)
 
561
             << endl;
 
562
      result= NDBT_FAILED;
 
563
    }
 
564
  }
 
565
 
 
566
done:
 
567
  ndb_mgm_disconnect(h);
 
568
  ndb_mgm_destroy_handle(&h);
 
569
 
 
570
  return result;
 
571
}
 
572
 
 
573
int runTestMgmApiEventTimeout(NDBT_Context* ctx, NDBT_Step* step)
 
574
{
 
575
  char *mgm= ctx->getRemoteMgm();
 
576
  int result= NDBT_OK;
 
577
  int mgmd_nodeid= 0;
 
578
 
 
579
  NdbMgmHandle h;
 
580
  h= ndb_mgm_create_handle();
 
581
  ndb_mgm_set_connectstring(h, mgm);
 
582
 
 
583
  int errs[] = { 10000, 0, -1 };
 
584
 
 
585
  for(int error_ins_no=0; errs[error_ins_no]!=-1; error_ins_no++)
 
586
  {
 
587
    int error_ins= errs[error_ins_no];
 
588
    ndb_mgm_connect(h,0,0,0);
 
589
 
 
590
    if(ndb_mgm_check_connection(h) < 0)
 
591
    {
 
592
      result= NDBT_FAILED;
 
593
      goto done;
 
594
    }
 
595
 
 
596
    mgmd_nodeid= ndb_mgm_get_mgmd_nodeid(h);
 
597
    if(mgmd_nodeid==0)
 
598
    {
 
599
      ndbout << "Failed to get mgmd node id to insert error" << endl;
 
600
      result= NDBT_FAILED;
 
601
      goto done;
 
602
    }
 
603
 
 
604
    ndb_mgm_reply reply;
 
605
    reply.return_code= 0;
 
606
 
 
607
    if(ndb_mgm_insert_error(h, mgmd_nodeid, error_ins, &reply)< 0)
 
608
    {
 
609
      ndbout << "failed to insert error " << error_ins << endl;
 
610
      result= NDBT_FAILED;
 
611
    }
 
612
 
 
613
    ndbout << "trying error: " << error_ins << endl;
 
614
 
 
615
    ndb_mgm_set_timeout(h,2500);
 
616
 
 
617
    int filter[] = { 15, NDB_MGM_EVENT_CATEGORY_BACKUP,
 
618
                     1, NDB_MGM_EVENT_CATEGORY_STARTUP,
 
619
                     0 };
 
620
    int fd= ndb_mgm_listen_event(h, filter);
 
621
 
 
622
    if(fd==NDB_INVALID_SOCKET)
 
623
    {
 
624
      ndbout << "FAILED: could not listen to event" << endl;
 
625
      result= NDBT_FAILED;
 
626
    }
 
627
 
 
628
    Uint32 theData[25];
 
629
    EventReport *fake_event = (EventReport*)theData;
 
630
    fake_event->setEventType(NDB_LE_NDBStopForced);
 
631
    fake_event->setNodeId(42);
 
632
    theData[2]= 0;
 
633
    theData[3]= 0;
 
634
    theData[4]= 0;
 
635
    theData[5]= 0;
 
636
 
 
637
    ndb_mgm_report_event(h, theData, 6);
 
638
 
 
639
    char *tmp= 0;
 
640
    char buf[512];
 
641
    SocketInputStream in(fd,2000);
 
642
    for(int i=0; i<20; i++)
 
643
    {
 
644
      if((tmp = in.gets(buf, sizeof(buf))))
 
645
      {
 
646
//        const char ping_token[]="<PING>";
 
647
//        if(memcmp(ping_token,tmp,sizeof(ping_token)-1))
 
648
          if(tmp && strlen(tmp))
 
649
            ndbout << tmp;
 
650
      }
 
651
      else
 
652
      {
 
653
        if(in.timedout())
 
654
        {
 
655
          ndbout << "TIMED OUT READING EVENT at iteration " << i << endl;
 
656
          break;
 
657
        }
 
658
      }
 
659
    }
 
660
 
 
661
    /*
 
662
     * events go through a *DIFFERENT* socket than the NdbMgmHandle
 
663
     * so we should still be connected (and be able to check_connection)
 
664
     *
 
665
     */
 
666
 
 
667
    if(ndb_mgm_check_connection(h) && !ndb_mgm_is_connected(h))
 
668
    {
 
669
      ndbout << "FAILED: is still connected after error" << endl;
 
670
      result= NDBT_FAILED;
 
671
    }
 
672
 
 
673
    ndb_mgm_disconnect(h);
 
674
  }
 
675
 
 
676
done:
 
677
  ndb_mgm_disconnect(h);
 
678
  ndb_mgm_destroy_handle(&h);
 
679
 
 
680
  return result;
 
681
}
 
682
 
 
683
int runTestMgmApiStructEventTimeout(NDBT_Context* ctx, NDBT_Step* step)
 
684
{
 
685
  char *mgm= ctx->getRemoteMgm();
 
686
  int result= NDBT_OK;
 
687
  int mgmd_nodeid= 0;
 
688
 
 
689
  NdbMgmHandle h;
 
690
  h= ndb_mgm_create_handle();
 
691
  ndb_mgm_set_connectstring(h, mgm);
 
692
 
 
693
  int errs[] = { 10000, 0, -1 };
 
694
 
 
695
  for(int error_ins_no=0; errs[error_ins_no]!=-1; error_ins_no++)
 
696
  {
 
697
    int error_ins= errs[error_ins_no];
 
698
    ndb_mgm_connect(h,0,0,0);
 
699
 
 
700
    if(ndb_mgm_check_connection(h) < 0)
 
701
    {
 
702
      result= NDBT_FAILED;
 
703
      goto done;
 
704
    }
 
705
 
 
706
    mgmd_nodeid= ndb_mgm_get_mgmd_nodeid(h);
 
707
    if(mgmd_nodeid==0)
 
708
    {
 
709
      ndbout << "Failed to get mgmd node id to insert error" << endl;
 
710
      result= NDBT_FAILED;
 
711
      goto done;
 
712
    }
 
713
 
 
714
    ndb_mgm_reply reply;
 
715
    reply.return_code= 0;
 
716
 
 
717
    if(ndb_mgm_insert_error(h, mgmd_nodeid, error_ins, &reply)< 0)
 
718
    {
 
719
      ndbout << "failed to insert error " << error_ins << endl;
 
720
      result= NDBT_FAILED;
 
721
    }
 
722
 
 
723
    ndbout << "trying error: " << error_ins << endl;
 
724
 
 
725
    ndb_mgm_set_timeout(h,2500);
 
726
 
 
727
    int filter[] = { 15, NDB_MGM_EVENT_CATEGORY_BACKUP,
 
728
                     1, NDB_MGM_EVENT_CATEGORY_STARTUP,
 
729
                     0 };
 
730
    NdbLogEventHandle le_handle= ndb_mgm_create_logevent_handle(h, filter);
 
731
 
 
732
    struct ndb_logevent le;
 
733
    for(int i=0; i<20; i++)
 
734
    {
 
735
      if(error_ins==0 || (error_ins!=0 && i<5))
 
736
      {
 
737
        Uint32 theData[25];
 
738
        EventReport *fake_event = (EventReport*)theData;
 
739
        fake_event->setEventType(NDB_LE_NDBStopForced);
 
740
        fake_event->setNodeId(42);
 
741
        theData[2]= 0;
 
742
        theData[3]= 0;
 
743
        theData[4]= 0;
 
744
        theData[5]= 0;
 
745
 
 
746
        ndb_mgm_report_event(h, theData, 6);
 
747
      }
 
748
      int r= ndb_logevent_get_next(le_handle, &le, 2500);
 
749
      if(r>0)
 
750
      {
 
751
        ndbout << "Receieved event" << endl;
 
752
      }
 
753
      else if(r<0)
 
754
      {
 
755
        ndbout << "ERROR" << endl;
 
756
      }
 
757
      else // no event
 
758
      {
 
759
        ndbout << "TIMED OUT READING EVENT at iteration " << i << endl;
 
760
        if(error_ins==0)
 
761
          result= NDBT_FAILED;
 
762
        else
 
763
          result= NDBT_OK;
 
764
        break;
 
765
      }
 
766
    }
 
767
 
 
768
    /*
 
769
     * events go through a *DIFFERENT* socket than the NdbMgmHandle
 
770
     * so we should still be connected (and be able to check_connection)
 
771
     *
 
772
     */
 
773
 
 
774
    if(ndb_mgm_check_connection(h) && !ndb_mgm_is_connected(h))
 
775
    {
 
776
      ndbout << "FAILED: is still connected after error" << endl;
 
777
      result= NDBT_FAILED;
 
778
    }
 
779
 
 
780
    ndb_mgm_disconnect(h);
 
781
  }
 
782
 
 
783
done:
 
784
  ndb_mgm_disconnect(h);
 
785
  ndb_mgm_destroy_handle(&h);
 
786
 
 
787
  return result;
 
788
}
 
789
 
 
790
NDBT_TESTSUITE(testMgm);
 
791
TESTCASE("SingleUserMode", 
 
792
         "Test single user mode"){
 
793
  INITIALIZER(runTestSingleUserMode);
 
794
  FINALIZER(runClearTable);
 
795
}
 
796
TESTCASE("ApiSessionFailure",
 
797
         "Test failures in MGMAPI session"){
 
798
  INITIALIZER(runTestApiSession);
 
799
 
 
800
}
 
801
TESTCASE("ApiConnectTimeout",
 
802
         "Connect timeout tests for MGMAPI"){
 
803
  INITIALIZER(runTestApiConnectTimeout);
 
804
 
 
805
}
 
806
TESTCASE("ApiTimeoutBasic",
 
807
         "Basic timeout tests for MGMAPI"){
 
808
  INITIALIZER(runTestApiTimeoutBasic);
 
809
 
 
810
}
 
811
TESTCASE("ApiGetStatusTimeout",
 
812
         "Test timeout for MGMAPI getStatus"){
 
813
  INITIALIZER(runTestApiGetStatusTimeout);
 
814
 
 
815
}
 
816
TESTCASE("ApiGetConfigTimeout",
 
817
         "Test timeouts for mgmapi get_configuration"){
 
818
  INITIALIZER(runTestMgmApiGetConfigTimeout);
 
819
 
 
820
}
 
821
TESTCASE("ApiMgmEventTimeout",
 
822
         "Test timeouts for mgmapi get_configuration"){
 
823
  INITIALIZER(runTestMgmApiEventTimeout);
 
824
 
 
825
}
 
826
TESTCASE("ApiMgmStructEventTimeout",
 
827
         "Test timeouts for mgmapi get_configuration"){
 
828
  INITIALIZER(runTestMgmApiStructEventTimeout);
 
829
 
 
830
}
 
831
NDBT_TESTSUITE_END(testMgm);
 
832
 
 
833
int main(int argc, const char** argv){
 
834
  ndb_init();
 
835
  myRandom48Init(NdbTick_CurrentMillisecond());
 
836
  return testMgm.execute(argc, argv);
 
837
}
 
838