MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
restarter.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 <NdbRestarter.hpp>
29 #include <NdbRestarts.hpp>
30 #include <NDBT.hpp>
31 
32 int main(int argc, const char** argv){
33  ndb_init();
34 
35  const char* _hostName = NULL;
36  int _loops = 10;
37  int _wait = 15;
38  int _help = 0;
39  int _error_insert = 0;
40  int _initial = 0;
41  int _master = 0;
42  int _maxwait = 120;
43  int _multiple = 0;
44 
45  struct getargs args[] = {
46  { "seconds", 's', arg_integer, &_wait,
47  "Seconds to wait between each restart(0=random)", "secs" },
48  { "max seconds", 'm', arg_integer, &_maxwait,
49  "Max seconds to wait between each restart. Default is 120 seconds",
50  "msecs" },
51  { "loops", 'l', arg_integer, &_loops,
52  "Number of loops(0=forever)", "loops"},
53  { "initial", 'i', arg_flag, &_initial, "Initial node restart"},
54  { "error-insert", 'e', arg_flag, &_error_insert, "Use error insert"},
55  { "master", 'm', arg_flag, &_master,
56  "Restart the master"},
57  { "multiple", 'x', arg_flag, &_multiple,
58  "Multiple random node restarts. OBS! Even and odd node Ids must be separated into each node group"},
59  { "usage", '?', arg_flag, &_help, "Print help", "" }
60 
61  };
62  int num_args = sizeof(args) / sizeof(args[0]);
63  int optind = 0;
64  char desc[] =
65  "hostname:port\n"\
66  "This program will connect to the mgmsrv of a NDB cluster.\n"\
67  "It will then wait for all nodes to be started, then restart node(s)\n"\
68  "and wait for all to restart inbetween. It will do this \n"\
69  "loop number of times\n";
70 
71  if(getarg(args, num_args, argc, argv, &optind) || _help) {
72  arg_printusage(args, num_args, argv[0], desc);
73  return NDBT_ProgramExit(NDBT_WRONGARGS);
74  }
75  _hostName = argv[optind];
76 
77 
78  NdbRestarts restarts(_hostName);
79  NdbRestarter restarter(_hostName);
80 
81  const char* restartName = "";
82  if (_multiple){
83  if (_master){
84  restartName = "TwoMasterNodeFailure";
85  }
86  else {
87  // Restart 50 percent of nodes
88  restartName = "FiftyPercentFail";
89  }
90  }
91  else if (_master){
92  restartName = "RestartMasterNodeError";
93  }else {
94  if (_error_insert)
95  restartName = "RestartRandomNodeError";
96  else if (_initial)
97  restartName = "RestartRandomNodeInitial";
98  else
99  restartName = "RestartRandomNode";
100  }
101 
102  ndbout << "Performing " << restartName << endl;
103 
104  int result = NDBT_OK;
105  int l = 0;
106  while (_loops == 0 || l<_loops){
107 
108  g_info << "Waiting for cluster to start" << endl;
109  while (restarter.waitClusterStarted(1) != 0){
110  //g_warning << "Ndb failed to start in 2 minutes" << endl;
111  }
112 
113  int seconds = _wait;
114  if(seconds==0) {
115  // Create random value, default 120 secs
116  seconds = (rand() % _maxwait) + 1;
117  }
118  g_info << "Waiting for " << seconds << "(" << _maxwait
119  << ") secs " << endl;
120  NdbSleep_SecSleep(seconds);
121 
122  g_info << l << ": Restarting node(s) " << endl;
123 
124  if (restarts.executeRestart(restartName) != 0){
125  result = NDBT_FAILED;
126  break;
127  }
128 
129  l++;
130  }
131  return NDBT_ProgramExit(NDBT_OK);
132 }