MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
restarts.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 
20 #include "mgmapi.h"
21 #include <string.h>
22 #include <NdbMain.h>
23 #include <OutputStream.hpp>
24 #include <NdbOut.hpp>
25 #include <NdbSleep.h>
26 #include <getarg.h>
27 
28 #include <NdbRestarts.hpp>
29 #include <NDBT.hpp>
30 
31 int main(int argc, const char** argv){
32  ndb_init();
33 
34  const char* _restartName = NULL;
35  int _loops = 1;
36  int _wait = -1;
37  int _maxwait = 120;
38  int _help = 0;
39  int _list = 0;
40  int _random = 0;
41  int _timeout = 0;
42  int _all = 0;
43 
44  struct getargs args[] = {
45  { "seconds", 's', arg_integer, &_wait,
46  "Seconds to wait between each restart(0=random)", "secs" },
47  { "max seconds", 'm', arg_integer, &_maxwait,
48  "Max seconds to wait between each restart. Default is 120 seconds", "msecs" },
49  { "loops", 'l', arg_integer, &_loops, "Number of loops(0=forever)", "loops"},
50  { "timeout", 't', arg_integer, &_timeout, "Timeout waiting for nodes to start", "seconds"},
51  { "random", 'r', arg_flag, &_random, "Select restart case randomly",
52  ""},
53  { "all", 'a', arg_flag, &_all, "Run all restarts",
54  ""},
55  { "list-restarts", '\0', arg_flag, &_list, "List available restarts", ""},
56  { "usage", '?', arg_flag, &_help, "Print help", "" }
57 
58  };
59  int num_args = sizeof(args) / sizeof(args[0]);
60  int optind = 0;
61  char desc[] =
62  "testname\n" \
63  "This program will perform node restart, \n"\
64  "multiple node restart or system-restart.\n"\
65  "Use --list-restarts to see whats available\n";
66 
67  if(getarg(args, num_args, argc, argv, &optind) || _help) {
68  arg_printusage(args, num_args, argv[0], desc);
69  return NDBT_ProgramExit(NDBT_WRONGARGS);
70  }
71 
72  if (_list){
73  NdbRestarts restarts;
74  restarts.listRestarts();
75  return NDBT_ProgramExit(NDBT_OK);
76  }
77 
78  if ((argv[optind] == NULL) && (_random == 0)){
79  arg_printusage(args, num_args, argv[0], desc);
80  return NDBT_ProgramExit(NDBT_WRONGARGS);
81  }
82  _restartName = argv[optind];
83 
84  NdbRestarts restarts;
85 
86  int res = NDBT_OK;
87  int l = 0;
88  while (_loops == 0 || l < _loops){
89 
90  if (_all) {
91  // Execute all restarts, set loops to numRestarts
92  // so that ecvery restart is executed once
93  _loops = restarts.getNumRestarts();
94  res = restarts.executeRestart(l, _timeout);
95  } else if (_random) {
96  int num = rand() % restarts.getNumRestarts();
97  res = restarts.executeRestart(num, _timeout);
98  } else {
99  res = restarts.executeRestart(_restartName, _timeout);
100  }
101  if (res != NDBT_OK)
102  break;
103 
104  if (_wait >= 0){
105  int seconds = _wait;
106  if(seconds==0) {
107  // Create random value, default 120 secs
108  seconds = (rand() % _maxwait) + 1;
109  }
110  g_info << "Waiting for " << seconds << "(" << _maxwait
111  << ") secs " << endl;
112  NdbSleep_SecSleep(seconds);
113  }
114 
115  l++;
116  }
117  return NDBT_ProgramExit(res);
118 }