MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
restarter2.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 <string.h>
21 #include <NdbMain.h>
22 #include <OutputStream.hpp>
23 #include <NdbOut.hpp>
24 #include <NdbSleep.h>
25 #include <getarg.h>
26 
27 #include <NdbRestarter.hpp>
28 #include <NDBT.hpp>
29 
30 int main(int argc, const char** argv){
31  ndb_init();
32 
33  const char* _hostName = NULL;
34  int _loops = 10;
35  int _wait = 15;
36  int _help = 0;
37 #if 0
38  int _crash = 0;
39  int _abort = 0;
40 #endif
41 
42  struct getargs args[] = {
43  { "seconds", 's', arg_integer, &_wait, "Seconds to wait between each restart(0=random)", "secs" },
44  { "loops", 'l', arg_integer, &_loops, "Number of loops", "loops 0=forever"},
45 #if 0
46  // Not yet!
47  { "abort", 'a', arg_flag, &_abort, "Restart abort"},
48  { "crash", 'c', arg_flag, &_crash, "Crash instead of restart"},
49 #endif
50  { "usage", '?', arg_flag, &_help, "Print help", "" }
51 
52  };
53  int num_args = sizeof(args) / sizeof(args[0]);
54  int optind = 0;
55  char desc[] =
56  "hostname:port\n"\
57  "This program will connect to the mgmsrv of a NDB cluster.\n"\
58  "It will wait for all nodes to be started, then restart all nodes\n"\
59  "into nostart state. Then after a random delay it will tell all nodes\n"\
60  "to start. It will do this loop number of times\n";
61 
62  if(getarg(args, num_args, argc, argv, &optind) || _help) {
63  arg_printusage(args, num_args, argv[0], desc);
64  return NDBT_ProgramExit(NDBT_WRONGARGS);
65  }
66  _hostName = argv[optind];
67 
68  NdbRestarter restarter(_hostName);
69 #if 0
70  if(_abort && _crash){
71  g_err << "You can't specify both abort and crash" << endl;
72  arg_printusage(args, num_args, argv[0], desc);
73  return NDBT_ProgramExit(NDBT_WRONGARGS);
74  }
75  if(_abort){
76  restarter.setRestartType(NdbRestarter::AbortRestart);
77  }
78  if(_crash){
79  restarter.setRestartType(NdbRestarter::Crash);
80  }
81 #endif
82 
83  int l = 0;
84  while (_loops == 0 || l<_loops){
85  g_info << "Waiting for cluster to start" << endl;
86  while(restarter.waitClusterStarted(120) != 0){
87  g_warning << "Ndb failed to start in 2 minutes" << endl;
88  }
89 
90  int seconds = _wait;
91  if(seconds==0)
92  seconds = (rand() % 120) + 1; // Create random value max 120 secs
93  g_info << "Waiting for "<<seconds<<" secs" << endl;
94  NdbSleep_SecSleep(seconds);
95 
96  g_info << l << ": restarting all nodes with nostart" << endl;
97  const bool b = (restarter.restartAll(false, true, false) == 0);
98  assert(b);
99 
100  g_info << "Waiting for cluster to enter nostart" << endl;
101  while(restarter.waitClusterNoStart(120) != 0){
102  g_warning << "Ndb failed to enter no start in 2 minutes" << endl;
103  }
104 
105  seconds = _wait;
106  if(seconds==0)
107  seconds = (rand() % 120) + 1; // Create random value max 120 secs
108  g_info << "Waiting for " <<seconds<<" secs" << endl;
109  NdbSleep_SecSleep(seconds);
110 
111  g_info << l << ": Telling all nodes to start" << endl;
112  const bool b2 = (restarter.startAll() == 0);
113  assert(b2);
114 
115  l++;
116  }
117 
118  return NDBT_ProgramExit(NDBT_OK);
119 }