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