MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
testInterpreter.cpp
1 /*
2  Copyright (C) 2003-2006, 2008 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 #include <NDBT.hpp>
20 #include <NDBT_Test.hpp>
21 #include <HugoTransactions.hpp>
22 #include <UtilTransactions.hpp>
23 #include <NdbRestarter.hpp>
24 #include <NdbRestarts.hpp>
25 #include <Vector.hpp>
26 #include <random.h>
27 #include <NdbTick.h>
28 
29 
30 #define CHECK(b) if (!(b)) { \
31  ndbout << "ERR: "<< step->getName() \
32  << " failed on line " << __LINE__ << endl; \
33  result = NDBT_FAILED; \
34  continue; }
35 
36 int runClearTable(NDBT_Context* ctx, NDBT_Step* step){
37  int records = ctx->getNumRecords();
38  int batchSize = ctx->getProperty("BatchSize", 1);
39 
40  HugoTransactions hugoTrans(*ctx->getTab());
41  if (hugoTrans.pkDelRecords(GETNDB(step), records, batchSize) != 0){
42  return NDBT_FAILED;
43  }
44  return NDBT_OK;
45 }
46 
47 int runLoadTable(NDBT_Context* ctx, NDBT_Step* step){
48 
49  int records = ctx->getNumRecords();
50  HugoTransactions hugoTrans(*ctx->getTab());
51  if (hugoTrans.loadTable(GETNDB(step), records) != 0){
52  return NDBT_FAILED;
53  }
54  return NDBT_OK;
55 }
56 
57 int runTestIncValue64(NDBT_Context* ctx, NDBT_Step* step){
58  int records = ctx->getNumRecords();
59  // NDBT_Table* pTab = ctx->getTab();
60  //Ndb* pNdb = GETNDB(step);
61 
62  HugoTransactions hugoTrans(*ctx->getTab());
63  if (hugoTrans.pkInterpretedUpdateRecords(GETNDB(step),
64  records) != 0){
65  return NDBT_FAILED;
66  }
67 
68  // Verify the update
69  if (hugoTrans.pkReadRecords(GETNDB(step),
70  records) != 0){
71  return NDBT_FAILED;
72  }
73 
74  return NDBT_OK;
75 
76 }
77 
78 int runTestIncValue32(NDBT_Context* ctx, NDBT_Step* step){
79  const NdbDictionary::Table * pTab = ctx->getTab();
80  Ndb* pNdb = GETNDB(step);
81 
82  if (strcmp(pTab->getName(), "T1") != 0) {
83  g_err << "runTestBug19537: skip, table != T1" << endl;
84  return NDBT_OK;
85  }
86 
87 
88  NdbConnection* pTrans = pNdb->startTransaction();
89  if (pTrans == NULL){
90  ERR(pNdb->getNdbError());
91  return NDBT_FAILED;
92  }
93 
94  NdbOperation* pOp = pTrans->getNdbOperation(pTab->getName());
95  if (pOp == NULL) {
96  ERR(pTrans->getNdbError());
97  pNdb->closeTransaction(pTrans);
98  return NDBT_FAILED;
99  }
100 
101  int check = pOp->interpretedUpdateTuple();
102  if( check == -1 ) {
103  ERR(pTrans->getNdbError());
104  pNdb->closeTransaction(pTrans);
105  return NDBT_FAILED;
106  }
107 
108 
109  // Primary keys
110  Uint32 pkVal = 1;
111  check = pOp->equal("KOL1", pkVal );
112  if( check == -1 ) {
113  ERR(pTrans->getNdbError());
114  pNdb->closeTransaction(pTrans);
115  return NDBT_FAILED;
116  }
117 
118  // Attributes
119 
120  // Perform initial read of column start value
121  NdbRecAttr* initialVal = pOp->getValue("KOL2");
122  if( initialVal == NULL ) {
123  ERR(pTrans->getNdbError());
124  pNdb->closeTransaction(pTrans);
125  return NDBT_FAILED;
126  }
127 
128  // Update the column
129  Uint32 valToIncWith = 1;
130  check = pOp->incValue("KOL2", valToIncWith);
131  if( check == -1 ) {
132  ERR(pTrans->getNdbError());
133  pNdb->closeTransaction(pTrans);
134  return NDBT_FAILED;
135  }
136 
137  // Perform final read of column after value
138  NdbRecAttr* afterVal = pOp->getValue("KOL2");
139  if( afterVal == NULL ) {
140  ERR(pTrans->getNdbError());
141  pNdb->closeTransaction(pTrans);
142  return NDBT_FAILED;
143  }
144 
145  check = pTrans->execute(Commit);
146  if( check == -1 ) {
147  ERR(pTrans->getNdbError());
148  pNdb->closeTransaction(pTrans);
149  return NDBT_FAILED;
150  }
151 
152  Uint32 oldValue = initialVal->u_32_value();
153  Uint32 newValue = afterVal->u_32_value();
154  Uint32 expectedValue = oldValue + valToIncWith;
155 
156  if (newValue != expectedValue)
157  {
158  g_err << "Failed : Expected " << oldValue << "+" <<
159  valToIncWith << "=" << expectedValue <<
160  " but received " << newValue << endl;
161  pNdb->closeTransaction(pTrans);
162  return NDBT_FAILED;
163  }
164 
165  pNdb->closeTransaction(pTrans);
166 
167 
168  return NDBT_OK;
169 }
170 
171 int runTestBug19537(NDBT_Context* ctx, NDBT_Step* step){
172  const NdbDictionary::Table * pTab = ctx->getTab();
173  Ndb* pNdb = GETNDB(step);
174 
175  if (strcmp(pTab->getName(), "T1") != 0) {
176  g_err << "runTestBug19537: skip, table != T1" << endl;
177  return NDBT_OK;
178  }
179 
180 
181  NdbConnection* pTrans = pNdb->startTransaction();
182  if (pTrans == NULL){
183  ERR(pNdb->getNdbError());
184  return NDBT_FAILED;
185  }
186 
187  NdbOperation* pOp = pTrans->getNdbOperation(pTab->getName());
188  if (pOp == NULL) {
189  ERR(pTrans->getNdbError());
190  pNdb->closeTransaction(pTrans);
191  return NDBT_FAILED;
192  }
193 
194  if (pOp->interpretedUpdateTuple() == -1) {
195  ERR(pOp->getNdbError());
196  pNdb->closeTransaction(pTrans);
197  return NDBT_FAILED;
198  }
199 
200 
201  // Primary keys
202  const Uint32 pkVal = 1;
203  if (pOp->equal("KOL1", pkVal) == -1) {
204  ERR(pTrans->getNdbError());
205  pNdb->closeTransaction(pTrans);
206  return NDBT_FAILED;
207  }
208 
209  // Load 64-bit constant into register 1 and
210  // write from register 1 to 32-bit column KOL2
211  const Uint64 reg_val = 0x0102030405060708ULL;
212  Uint32 reg_ptr32[2];
213  memcpy(reg_ptr32+0, (Uint8*)&reg_val, sizeof(Uint32));
214  memcpy(reg_ptr32+1, ((Uint8*)&reg_val)+4, sizeof(Uint32));
215  if (reg_ptr32[0] == 0x05060708 && reg_ptr32[1] == 0x01020304) {
216  g_err << "runTestBug19537: platform is LITTLE endian" << endl;
217  } else if (reg_ptr32[0] == 0x01020304 && reg_ptr32[1] == 0x05060708) {
218  g_err << "runTestBug19537: platform is BIG endian" << endl;
219  } else {
220  g_err << "runTestBug19537: impossible platform"
221  << hex << " [0]=" << reg_ptr32[0] << " [1]=" <<reg_ptr32[1] << endl;
222  pNdb->closeTransaction(pTrans);
223  return NDBT_FAILED;
224  }
225 
226  if (pOp->load_const_u64(1, reg_val) == -1 ||
227  pOp->write_attr("KOL2", 1) == -1) {
228  ERR(pOp->getNdbError());
229  pNdb->closeTransaction(pTrans);
230  return NDBT_FAILED;
231  }
232 
233  if (pTrans->execute(Commit) == -1) {
234  ERR(pTrans->getNdbError());
235  pNdb->closeTransaction(pTrans);
236  return NDBT_FAILED;
237  }
238 
239  // Read value via a new transaction
240 
241  pTrans = pNdb->startTransaction();
242  if (pTrans == NULL){
243  ERR(pNdb->getNdbError());
244  return NDBT_FAILED;
245  }
246 
247  pOp = pTrans->getNdbOperation(pTab->getName());
248  if (pOp == NULL) {
249  ERR(pTrans->getNdbError());
250  pNdb->closeTransaction(pTrans);
251  return NDBT_FAILED;
252  }
253 
254  Uint32 kol2 = 0x09090909;
255  if (pOp->readTuple() == -1 ||
256  pOp->equal("KOL1", pkVal) == -1 ||
257  pOp->getValue("KOL2", (char*)&kol2) == 0) {
258  ERR(pOp->getNdbError());
259  pNdb->closeTransaction(pTrans);
260  return NDBT_FAILED;
261  }
262 
263  if (pTrans->execute(Commit) == -1) {
264  ERR(pTrans->getNdbError());
265  pNdb->closeTransaction(pTrans);
266  return NDBT_FAILED;
267  }
268 
269  // Expected conversion as in C - truncate to lower (logical) word
270 
271  if (kol2 == 0x01020304) {
272  g_err << "runTestBug19537: the bug manifests itself !" << endl;
273  pNdb->closeTransaction(pTrans);
274  return NDBT_FAILED;
275  }
276 
277  if (kol2 != 0x05060708) {
278  g_err << "runTestBug19537: impossible KOL2 " << hex << kol2 << endl;
279  pNdb->closeTransaction(pTrans);
280  return NDBT_FAILED;
281  }
282 
283  pNdb->closeTransaction(pTrans);
284  return NDBT_OK;
285 }
286 
287 
288 int runTestBug34107(NDBT_Context* ctx, NDBT_Step* step){
289  const NdbDictionary::Table * pTab = ctx->getTab();
290  Ndb* pNdb = GETNDB(step);
291  const Uint32 okSize= 10000;
292  const Uint32 tooBig= 30000;
293 
294  Uint32 codeBuff[tooBig];
295 
296  int i;
297  for (i = 0; i <= 1; i++) {
298  g_info << "bug34107:" << (i == 0 ? " small" : " too big") << endl;
299 
300  NdbConnection* pTrans = pNdb->startTransaction();
301  if (pTrans == NULL){
302  ERR(pNdb->getNdbError());
303  return NDBT_FAILED;
304  }
305 
306  NdbScanOperation* pOp = pTrans->getNdbScanOperation(pTab->getName());
307  if (pOp == NULL) {
308  ERR(pTrans->getNdbError());
309  pNdb->closeTransaction(pTrans);
310  return NDBT_FAILED;
311  }
312 
313  if (pOp->readTuples() == -1) {
314  ERR(pOp->getNdbError());
315  pNdb->closeTransaction(pTrans);
316  return NDBT_FAILED;
317  }
318 
319  /* Test kernel mechanism for dealing with too large program
320  * We need to provide our own program buffer as default
321  * NdbInterpretedCode buffer will not grow larger than
322  * NDB_MAX_SCANFILTER_SIZE
323  */
324 
325  NdbInterpretedCode code(NULL, // Table is irrelevant
326  codeBuff,
327  tooBig); // Size of codeBuff
328 
329  int n = i == 0 ? okSize : tooBig;
330  int k;
331 
332  for (k = 0; k < n; k++) {
333 
334  // inserts 1 word ATTRINFO
335 
336  if (code.interpret_exit_ok() == -1) {
337  ERR(code.getNdbError());
338  pNdb->closeTransaction(pTrans);
339  return NDBT_FAILED;
340  }
341  }
342 
343  if (code.finalise() != 0)
344  {
345  ERR(code.getNdbError());
346  pNdb->closeTransaction(pTrans);
347  return NDBT_FAILED;
348  }
349 
350  if (pOp->setInterpretedCode(&code) != 0)
351  {
352  ERR(pOp->getNdbError());
353  pNdb->closeTransaction(pTrans);
354  return NDBT_FAILED;
355  }
356 
357  if (pTrans->execute(NoCommit) == -1) {
358  ERR(pTrans->getNdbError());
359  pNdb->closeTransaction(pTrans);
360  return NDBT_FAILED;
361  }
362 
363  int ret;
364  while ((ret = pOp->nextResult()) == 0)
365  ;
366  g_info << "ret=" << ret << " err=" << pOp->getNdbError().code << endl;
367 
368  if (i == 0 && ret != 1) {
369  ERR(pTrans->getNdbError());
370  pNdb->closeTransaction(pTrans);
371  return NDBT_FAILED;
372  }
373 
374  if (i == 1 && ret != -1) {
375  g_err << "unexpected big filter success" << endl;
376  pNdb->closeTransaction(pTrans);
377  return NDBT_FAILED;
378  }
379  if (i == 1 && pOp->getNdbError().code != 874) {
380  g_err << "unexpected big filter error code, wanted 874" << endl;
381  ERR(pTrans->getNdbError());
382  pNdb->closeTransaction(pTrans);
383  return NDBT_FAILED;
384  }
385 
386  pNdb->closeTransaction(pTrans);
387  }
388 
389  return NDBT_OK;
390 }
391 
392 static char pkIdxName[256];
393 
394 int
395 createPkIndex(NDBT_Context* ctx, NDBT_Step* step){
396  const NdbDictionary::Table* pTab = ctx->getTab();
397  Ndb* pNdb = GETNDB(step);
398 
399  bool orderedIndex = ctx->getProperty("OrderedIndex", (unsigned)0);
400  bool logged = ctx->getProperty("LoggedIndexes", (Uint32)0);
401  bool noddl= ctx->getProperty("NoDDL");
402 
403  // Create index
404  BaseString::snprintf(pkIdxName, 255, "IDC_PK_%s", pTab->getName());
405  if (orderedIndex)
406  ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "ordered index "
407  << pkIdxName << " (";
408  else
409  ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "unique index "
410  << pkIdxName << " (";
411 
412  NdbDictionary::Index pIdx(pkIdxName);
413  pIdx.setTable(pTab->getName());
414  if (orderedIndex)
416  else
418  for (int c = 0; c< pTab->getNoOfColumns(); c++){
419  const NdbDictionary::Column * col = pTab->getColumn(c);
420  if(col->getPrimaryKey()){
421  pIdx.addIndexColumn(col->getName());
422  ndbout << col->getName() <<" ";
423  }
424  }
425 
426  pIdx.setStoredIndex(logged);
427  ndbout << ") ";
428  if (noddl)
429  {
430  const NdbDictionary::Index* idx= pNdb->
431  getDictionary()->getIndex(pkIdxName, pTab->getName());
432 
433  if (!idx)
434  {
435  ndbout << "Failed - Index does not exist and DDL not allowed" << endl;
436  ERR(pNdb->getDictionary()->getNdbError());
437  return NDBT_FAILED;
438  }
439  else
440  {
441  // TODO : Check index definition is ok
442  }
443  }
444  else
445  {
446  if (pNdb->getDictionary()->createIndex(pIdx) != 0){
447  ndbout << "FAILED!" << endl;
448  const NdbError err = pNdb->getDictionary()->getNdbError();
449  ERR(err);
450  return NDBT_FAILED;
451  }
452  }
453 
454  ndbout << "OK!" << endl;
455  return NDBT_OK;
456 }
457 
458 int
459 createPkIndex_Drop(NDBT_Context* ctx, NDBT_Step* step)
460 {
461  const NdbDictionary::Table* pTab = ctx->getTab();
462  Ndb* pNdb = GETNDB(step);
463 
464  bool noddl= ctx->getProperty("NoDDL");
465 
466  // Drop index
467  if (!noddl)
468  {
469  ndbout << "Dropping index " << pkIdxName << " ";
470  if (pNdb->getDictionary()->dropIndex(pkIdxName,
471  pTab->getName()) != 0){
472  ndbout << "FAILED!" << endl;
473  ERR(pNdb->getDictionary()->getNdbError());
474  return NDBT_FAILED;
475  } else {
476  ndbout << "OK!" << endl;
477  }
478  }
479 
480  return NDBT_OK;
481 }
482 
483 #define CHK_RET_FAILED(x) if (!(x)) { ndbout_c("Failed on line: %u", __LINE__); return NDBT_FAILED; }
484 
485 int
486 runInterpretedUKLookup(NDBT_Context* ctx, NDBT_Step* step)
487 {
488  const NdbDictionary::Table * pTab = ctx->getTab();
489  Ndb* pNdb = GETNDB(step);
490  NdbDictionary::Dictionary * dict = pNdb->getDictionary();
491 
492  const NdbDictionary::Index* pIdx= dict->getIndex(pkIdxName, pTab->getName());
493  CHK_RET_FAILED(pIdx != 0);
494 
495  const NdbRecord * pRowRecord = pTab->getDefaultRecord();
496  CHK_RET_FAILED(pRowRecord != 0);
497  const NdbRecord * pIdxRecord = pIdx->getDefaultRecord();
498  CHK_RET_FAILED(pIdxRecord != 0);
499 
500  const Uint32 len = NdbDictionary::getRecordRowLength(pRowRecord);
501  Uint8 * pRow = new Uint8[len];
502  bzero(pRow, len);
503 
504  HugoCalculator calc(* pTab);
505  calc.equalForRow(pRow, pRowRecord, 0);
506 
507  NdbTransaction* pTrans = pNdb->startTransaction();
508  CHK_RET_FAILED(pTrans != 0);
509 
511  code.interpret_exit_ok();
512  code.finalise();
513 
515  bzero(&opts, sizeof(opts));
516  opts.optionsPresent = NdbOperation::OperationOptions::OO_INTERPRETED;
517  opts.interpretedCode = &code;
518 
519  const NdbOperation * pOp = pTrans->readTuple(pIdxRecord, (char*)pRow,
520  pRowRecord, (char*)pRow,
522  0,
523  &opts,
524  sizeof(opts));
525  CHK_RET_FAILED(pOp);
526  int res = pTrans->execute(Commit, AbortOnError);
527 
528  CHK_RET_FAILED(res == 0);
529 
530  delete [] pRow;
531 
532  return NDBT_OK;
533 }
534 
535 NDBT_TESTSUITE(testInterpreter);
536 TESTCASE("IncValue32",
537  "Test incValue for 32 bit integer\n"){
538  INITIALIZER(runLoadTable);
539  INITIALIZER(runTestIncValue32);
540  FINALIZER(runClearTable);
541 }
542 TESTCASE("IncValue64",
543  "Test incValue for 64 bit integer\n"){
544  INITIALIZER(runLoadTable);
545  INITIALIZER(runTestIncValue64);
546  FINALIZER(runClearTable);
547 }
548 TESTCASE("Bug19537",
549  "Test big-endian write_attr of 32 bit integer\n"){
550  INITIALIZER(runLoadTable);
551  INITIALIZER(runTestBug19537);
552  FINALIZER(runClearTable);
553 }
554 TESTCASE("Bug34107",
555  "Test too big scan filter (error 874)\n"){
556  INITIALIZER(runLoadTable);
557  INITIALIZER(runTestBug34107);
558  FINALIZER(runClearTable);
559 }
560 #if 0
561 TESTCASE("MaxTransactions",
562  "Start transactions until no more can be created\n"){
563  INITIALIZER(runTestMaxTransaction);
564 }
565 TESTCASE("MaxOperations",
566  "Get operations until no more can be created\n"){
567  INITIALIZER(runLoadTable);
568  INITIALIZER(runTestMaxOperations);
569  FINALIZER(runClearTable);
570 }
571 TESTCASE("MaxGetValue",
572  "Call getValue loads of time\n"){
573  INITIALIZER(runLoadTable);
574  INITIALIZER(runTestGetValue);
575  FINALIZER(runClearTable);
576 }
577 TESTCASE("MaxEqual",
578  "Call equal loads of time\n"){
579  INITIALIZER(runTestEqual);
580 }
581 TESTCASE("DeleteNdb",
582  "Make sure that a deleted Ndb object is properly deleted\n"
583  "and removed from transporter\n"){
584  INITIALIZER(runLoadTable);
585  INITIALIZER(runTestDeleteNdb);
586  FINALIZER(runClearTable);
587 }
588 TESTCASE("WaitUntilReady",
589  "Make sure you get an error message when calling waitUntilReady\n"
590  "without an init'ed Ndb\n"){
591  INITIALIZER(runTestWaitUntilReady);
592 }
593 TESTCASE("GetOperationNoTab",
594  "Call getNdbOperation on a table that does not exist\n"){
595  INITIALIZER(runGetNdbOperationNoTab);
596 }
597 TESTCASE("MissingOperation",
598  "Missing operation request(insertTuple) should give an error code\n"){
599  INITIALIZER(runMissingOperation);
600 }
601 TESTCASE("GetValueInUpdate",
602  "Test that it's not possible to perform getValue in an update\n"){
603  INITIALIZER(runLoadTable);
604  INITIALIZER(runGetValueInUpdate);
605  FINALIZER(runClearTable);
606 }
607 TESTCASE("UpdateWithoutKeys",
608  "Test that it's not possible to perform update without setting\n"
609  "PKs"){
610  INITIALIZER(runLoadTable);
611  INITIALIZER(runUpdateWithoutKeys);
612  FINALIZER(runClearTable);
613 }
614 TESTCASE("UpdateWithoutValues",
615  "Test that it's not possible to perform update without setValues\n"){
616  INITIALIZER(runLoadTable);
617  INITIALIZER(runUpdateWithoutValues);
618  FINALIZER(runClearTable);
619 }
620 TESTCASE("NdbErrorOperation",
621  "Test that NdbErrorOperation is properly set"){
622  INITIALIZER(runCheckGetNdbErrorOperation);
623 }
624 #endif
625 TESTCASE("InterpretedUKLookup", "")
626 {
627  INITIALIZER(runLoadTable);
628  INITIALIZER(createPkIndex);
629  INITIALIZER(runInterpretedUKLookup);
630  INITIALIZER(createPkIndex_Drop);
631 }
632 NDBT_TESTSUITE_END(testInterpreter);
633 
634 int main(int argc, const char** argv){
635  ndb_init();
636  // TABLE("T1");
637  NDBT_TESTSUITE_INSTANCE(testInterpreter);
638  return testInterpreter.execute(argc, argv);
639 }
640 
641