MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
testSpj.cpp
1 /*
2  Copyright (c) 2011, 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_Test.hpp>
19 #include <NDBT_ReturnCodes.h>
20 #include <HugoTransactions.hpp>
21 #include <UtilTransactions.hpp>
22 #include <NdbRestarter.hpp>
23 #include <signaldata/DictTabInfo.hpp>
24 #include <Bitmask.hpp>
25 #include <random.h>
26 #include <HugoQueryBuilder.hpp>
27 #include <HugoQueries.hpp>
28 
29 int
30 runLoadTable(NDBT_Context* ctx, NDBT_Step* step)
31 {
32  int records = ctx->getNumRecords();
33  HugoTransactions hugoTrans(*ctx->getTab());
34  if (hugoTrans.loadTable(GETNDB(step), records) != 0){
35  return NDBT_FAILED;
36  }
37  return NDBT_OK;
38 }
39 
40 int
41 runClearTable(NDBT_Context* ctx, NDBT_Step* step)
42 {
43  UtilTransactions utilTrans(*ctx->getTab());
44  if (utilTrans.clearTable(GETNDB(step)) != 0){
45  return NDBT_FAILED;
46  }
47  return NDBT_OK;
48 }
49 
50 static
51 void
52 addMask(NDBT_Context* ctx, Uint32 val, const char * name)
53 {
54  Uint32 oldValue = 0;
55  do
56  {
57  oldValue = ctx->getProperty(name);
58  Uint32 newValue = oldValue | val;
59  if (ctx->casProperty(name, oldValue, newValue) == oldValue)
60  return;
61  NdbSleep_MilliSleep(5);
62  } while (true);
63 }
64 
65 int
66 runLookupJoin(NDBT_Context* ctx, NDBT_Step* step){
67  int loops = ctx->getNumLoops();
68  int joinlevel = ctx->getProperty("JoinLevel", 3);
69  int records = ctx->getNumRecords();
70  int until_stopped = ctx->getProperty("UntilStopped", (Uint32)0);
71  Uint32 stepNo = step->getStepNo();
72 
73  int i = 0;
74  HugoQueryBuilder qb(GETNDB(step), ctx->getTab(), HugoQueryBuilder::O_LOOKUP);
75  qb.setJoinLevel(joinlevel);
76  const NdbQueryDef * query = qb.createQuery(GETNDB(step));
77  HugoQueries hugoTrans(*query);
78  while ((i<loops || until_stopped) && !ctx->isTestStopped())
79  {
80  g_info << i << ": ";
81  if (hugoTrans.runLookupQuery(GETNDB(step), records))
82  {
83  g_info << endl;
84  return NDBT_FAILED;
85  }
86  addMask(ctx, (1 << stepNo), "Running");
87  i++;
88  }
89  g_info << endl;
90  return NDBT_OK;
91 }
92 
93 int
94 runScanJoin(NDBT_Context* ctx, NDBT_Step* step){
95  int loops = ctx->getNumLoops();
96  int joinlevel = ctx->getProperty("JoinLevel", 3);
97  int until_stopped = ctx->getProperty("UntilStopped", (Uint32)0);
98  Uint32 stepNo = step->getStepNo();
99 
100  int i = 0;
101  HugoQueryBuilder qb(GETNDB(step), ctx->getTab(), HugoQueryBuilder::O_SCAN);
102  qb.setJoinLevel(joinlevel);
103  const NdbQueryDef * query = qb.createQuery(GETNDB(step));
104  HugoQueries hugoTrans(* query);
105  while ((i<loops || until_stopped) && !ctx->isTestStopped())
106  {
107  g_info << i << ": ";
108  if (hugoTrans.runScanQuery(GETNDB(step)))
109  {
110  g_info << endl;
111  return NDBT_FAILED;
112  }
113  addMask(ctx, (1 << stepNo), "Running");
114  i++;
115  }
116  g_info << endl;
117  return NDBT_OK;
118 }
119 
120 int
121 runJoin(NDBT_Context* ctx, NDBT_Step* step){
122  int loops = ctx->getNumLoops();
123  int joinlevel = ctx->getProperty("JoinLevel", 3);
124  int records = ctx->getNumRecords();
125  int until_stopped = ctx->getProperty("UntilStopped", (Uint32)0);
126  Uint32 stepNo = step->getStepNo();
127 
128  int i = 0;
129  HugoQueryBuilder qb1(GETNDB(step), ctx->getTab(), HugoQueryBuilder::O_SCAN);
130  HugoQueryBuilder qb2(GETNDB(step), ctx->getTab(), HugoQueryBuilder::O_LOOKUP);
131  qb1.setJoinLevel(joinlevel);
132  qb2.setJoinLevel(joinlevel);
133  const NdbQueryDef * q1 = qb1.createQuery(GETNDB(step));
134  const NdbQueryDef * q2 = qb2.createQuery(GETNDB(step));
135  HugoQueries hugoTrans1(* q1);
136  HugoQueries hugoTrans2(* q2);
137  while ((i<loops || until_stopped) && !ctx->isTestStopped())
138  {
139  g_info << i << ": ";
140  if (hugoTrans1.runScanQuery(GETNDB(step)))
141  {
142  g_info << endl;
143  return NDBT_FAILED;
144  }
145  if (hugoTrans2.runLookupQuery(GETNDB(step), records))
146  {
147  g_info << endl;
148  return NDBT_FAILED;
149  }
150  i++;
151  addMask(ctx, (1 << stepNo), "Running");
152  }
153  g_info << endl;
154  return NDBT_OK;
155 }
156 
157 int
158 runRestarter(NDBT_Context* ctx, NDBT_Step* step)
159 {
160  int result = NDBT_OK;
161  int loops = ctx->getNumLoops();
162  int waitprogress = ctx->getProperty("WaitProgress", (unsigned)0);
163  int randnode = ctx->getProperty("RandNode", (unsigned)0);
164  NdbRestarter restarter;
165  int i = 0;
166  int lastId = 0;
167 
168  if (restarter.getNumDbNodes() < 2){
169  ctx->stopTest();
170  return NDBT_OK;
171  }
172 
173  if(restarter.waitClusterStarted() != 0){
174  g_err << "Cluster failed to start" << endl;
175  return NDBT_FAILED;
176  }
177 
178  loops *= (restarter.getNumDbNodes() > 2 ? 2 : restarter.getNumDbNodes());
179  if (loops < restarter.getNumDbNodes())
180  loops = restarter.getNumDbNodes();
181 
182  NdbSleep_MilliSleep(200);
183  Uint32 running = ctx->getProperty("Running", (Uint32)0);
184  while (running == 0 && !ctx->isTestStopped())
185  {
186  NdbSleep_MilliSleep(100);
187  running = ctx->getProperty("Running", (Uint32)0);
188  }
189 
190  if (ctx->isTestStopped())
191  return NDBT_FAILED;
192 
193  while(i<loops && result != NDBT_FAILED && !ctx->isTestStopped()){
194 
195  int id = lastId % restarter.getNumDbNodes();
196  if (randnode == 1)
197  {
198  id = rand() % restarter.getNumDbNodes();
199  }
200  int nodeId = restarter.getDbNodeId(id);
201  ndbout << "Restart node " << nodeId << endl;
202 
203  if(restarter.restartOneDbNode(nodeId, false, true, true) != 0){
204  g_err << "Failed to restartNextDbNode" << endl;
205  result = NDBT_FAILED;
206  break;
207  }
208 
209  if (restarter.waitNodesNoStart(&nodeId, 1))
210  {
211  g_err << "Failed to waitNodesNoStart" << endl;
212  result = NDBT_FAILED;
213  break;
214  }
215 
216  if (waitprogress)
217  {
218  Uint32 maxwait = 30;
219  ndbout_c("running: 0x%.8x", running);
220  for (Uint32 checks = 0; checks < 3 && !ctx->isTestStopped(); checks++)
221  {
222  ctx->setProperty("Running", (Uint32)0);
223  for (; maxwait != 0 && !ctx->isTestStopped(); maxwait--)
224  {
225  if ((ctx->getProperty("Running", (Uint32)0) & running) == running)
226  goto ok;
227  NdbSleep_SecSleep(1);
228  }
229 
230  if (ctx->isTestStopped())
231  {
232  g_err << "Test stopped while waiting for progress!" << endl;
233  return NDBT_FAILED;
234  }
235 
236  g_err << "No progress made!!" << endl;
237  return NDBT_FAILED;
238  ok:
239  g_err << "Progress made!! " << endl;
240  }
241  }
242 
243  if (restarter.startNodes(&nodeId, 1))
244  {
245  g_err << "Failed to start node" << endl;
246  result = NDBT_FAILED;
247  break;
248  }
249 
250  if(restarter.waitClusterStarted() != 0){
251  g_err << "Cluster failed to start" << endl;
252  result = NDBT_FAILED;
253  break;
254  }
255 
256  if (waitprogress)
257  {
258  Uint32 maxwait = 30;
259  ndbout_c("running: 0x%.8x", running);
260  for (Uint32 checks = 0; checks < 3 && !ctx->isTestStopped(); checks++)
261  {
262  ctx->setProperty("Running", (Uint32)0);
263  for (; maxwait != 0 && !ctx->isTestStopped(); maxwait--)
264  {
265  if ((ctx->getProperty("Running", (Uint32)0) & running) == running)
266  goto ok2;
267  NdbSleep_SecSleep(1);
268  }
269 
270  if (ctx->isTestStopped())
271  {
272  g_err << "Test stopped while waiting for progress!" << endl;
273  return NDBT_FAILED;
274  }
275 
276  g_err << "No progress made!!" << endl;
277  return NDBT_FAILED;
278  ok2:
279  g_err << "Progress made!! " << endl;
280  ctx->setProperty("Running", (Uint32)0);
281  }
282  }
283 
284  lastId++;
285  i++;
286  }
287 
288  ctx->stopTest();
289 
290  return result;
291 }
292 
293 NDBT_TESTSUITE(testSpj);
294 TESTCASE("LookupJoin", ""){
295  INITIALIZER(runLoadTable);
296  STEP(runLookupJoin);
297  VERIFIER(runClearTable);
298 }
299 TESTCASE("ScanJoin", ""){
300  INITIALIZER(runLoadTable);
301  STEP(runScanJoin);
302  FINALIZER(runClearTable);
303 }
304 TESTCASE("MixedJoin", ""){
305  INITIALIZER(runLoadTable);
306  STEPS(runJoin, 6);
307  FINALIZER(runClearTable);
308 }
309 TESTCASE("NF_Join", ""){
310  TC_PROPERTY("UntilStopped", 1);
311  TC_PROPERTY("WaitProgress", 20);
312  INITIALIZER(runLoadTable);
313  //STEPS(runScanJoin, 6);
314  //STEPS(runLookupJoin, 6);
315  STEPS(runJoin, 6);
316  STEP(runRestarter);
317  FINALIZER(runClearTable);
318 }
319 NDBT_TESTSUITE_END(testSpj);
320 
321 
322 int main(int argc, const char** argv){
323  ndb_init();
324  NDBT_TESTSUITE_INSTANCE(testSpj);
325  return testSpj.execute(argc, argv);
326 }
327