MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
testReconnect.cpp
1 /*
2  Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <NDBT.hpp>
19 #include <NDBT_Test.hpp>
20 #include <DbUtil.hpp>
21 #include <AtrtClient.hpp>
22 
23 #include <NdbRestarts.hpp>
24 
25 
26 int runCreateTable(NDBT_Context* ctx, NDBT_Step* step){
27  DbUtil sql("test");
28 
29  if (!sql.doQuery("CREATE TABLE reconnect ("
30  "pk bigint, "
31  "thread int, "
32  "b varchar(32) NOT NULL, "
33  "PRIMARY KEY(pk, thread)"
34  ") engine = NDB;"))
35  return NDBT_FAILED;
36  return NDBT_OK;
37 }
38 
39 int runDropTable(NDBT_Context* ctx, NDBT_Step* step){
40  DbUtil sql("test");
41 
42  if (!sql.doQuery("DROP TABLE IF EXISTS reconnect"))
43  return NDBT_FAILED;
44  return NDBT_OK;
45 }
46 
47 
48 int runSQLQueries(NDBT_Context* ctx, NDBT_Step* step,
49  const char* query)
50 {
51  int result = -1;
52  DbUtil sql("test");
53 
54  unsigned failed = 0;
55  unsigned i = 0;
56  unsigned shutdown_counter= 0;
57  while (result == -1)
58  {
59  Properties args;
60  args.put("0", i);
61  if (!sql.doQuery(query, args))
62  {
63  switch(sql.last_errno()){
64  case 2006: // MySQL server has gone away(ie. crash)
65  g_err << "Fatal error: " << sql.last_error() << endl;
66  g_err.print("query: %s", query);
67  result = NDBT_FAILED;
68  break;
69  default:
70  // Ignore
71  failed++;
72  break;
73  }
74  }
75  else
76  g_info << query << endl;
77  sql.silent(); // Late, to catch any SQL syntax errors
78  i++;
79 
80  if (ctx->isTestStopped())
81  {
82  // When the test is stopped we run additional queries
83  // that all should work
84 
85  if (shutdown_counter == 0)
86  {
87  shutdown_counter = i;
88  }
89  else
90  {
91  unsigned extra_loops= i - shutdown_counter;
92 
93  if (extra_loops < 10)
94  {
95  // Check that last query suceeded
96  if (sql.last_errno() != 0)
97  {
98  g_err << "Fatal error during shutdown queries: "
99  << sql.last_error() << endl;
100  g_err.print("query: %s", query);
101  result = NDBT_FAILED;
102  }
103  }
104  else
105  {
106  // We are done, signal sucess
107  result= NDBT_OK;
108  }
109  }
110  }
111 
112  }
113  ctx->stopTest();
114  g_info << i - failed << " queries completed and "
115  << failed << " failed" << endl;
116  return result;
117 }
118 
119 
120 int runINSERT(NDBT_Context* ctx, NDBT_Step* step){
121  BaseString query;
122  query.assfmt("INSERT INTO reconnect "
123  "(pk, thread, b) VALUES (?, %d, 'data%d')",
124  step->getStepNo(), step->getStepNo());
125  return runSQLQueries(ctx, step, query.c_str());
126 
127 }
128 
129 
130 int runSELECT(NDBT_Context* ctx, NDBT_Step* step){
131  return runSQLQueries(ctx, step, "SELECT * FROM reconnect");
132 }
133 
134 
135 int runDELETE(NDBT_Context* ctx, NDBT_Step* step){
136  BaseString query;
137  query.assfmt("DELETE from reconnect WHERE thread=%d LIMIT 10",
138  step->getStepNo());
139  return runSQLQueries(ctx, step, query.c_str());
140 }
141 
142 
143 int runRestartCluster(NDBT_Context* ctx, NDBT_Step* step){
144  int result = NDBT_OK;
145  int loops = ctx->getNumLoops();
146  NdbRestarts restarts;
147  int i = 0;
148  int timeout = 240;
149 
150  while(i<loops && result != NDBT_FAILED && !ctx->isTestStopped()){
151 
152  ndbout << "Loop " << i << "/"<< loops <<" started" << endl;
153 
154  if(restarts.executeRestart(ctx, "RestartAllNodesAbort", timeout) != 0){
155  g_err << "Failed to restart all nodes with abort" << endl;
156  result = NDBT_FAILED;
157  break;
158  }
159 
160  NdbSleep_SecSleep(10);
161  i++;
162  }
163  ctx->stopTest();
164  return result;
165 }
166 
167 
168 NDBT_TESTSUITE(testReconnect);
169 TESTCASE("InsertAndRestart",
170  "Run INSERTs while cluster restart"){
171  INITIALIZER(runDropTable);
172  INITIALIZER(runCreateTable);
173  STEP(runINSERT);
174  STEP(runRestartCluster);
175 }
176 TESTCASE("SelectAndRestart",
177  "Run SELECTs while cluster restart"){
178  INITIALIZER(runDropTable);
179  INITIALIZER(runCreateTable);
180  STEP(runSELECT);
181  STEP(runRestartCluster);
182 }
183 TESTCASE("DeleteAndRestart",
184  "Run DELETEs while cluster restart"){
185  INITIALIZER(runDropTable);
186  INITIALIZER(runCreateTable);
187  STEP(runDELETE);
188  STEP(runRestartCluster);
189 }
190 TESTCASE("AllAndRestart",
191  "Run all kind of statements while cluster restart"){
192  INITIALIZER(runDropTable);
193  INITIALIZER(runCreateTable);
194  STEPS(runSELECT, 5);
195  STEPS(runINSERT, 25);
196  STEPS(runDELETE, 2);
197  STEP(runRestartCluster);
198 }
199 
200 NDBT_TESTSUITE_END(testReconnect);
201 
202 int main(int argc, const char** argv){
203  ndb_init();
204  NDBT_TESTSUITE_INSTANCE(testReconnect);
205  return testReconnect.execute(argc, argv);
206 }
207