testTransactions.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:14k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. #include <NDBT_Test.hpp>
  14. #include <NDBT_ReturnCodes.h>
  15. #include <HugoTransactions.hpp>
  16. #include <UtilTransactions.hpp>
  17. #include <NdbRestarter.hpp>
  18. #include <signaldata/DumpStateOrd.hpp>
  19. struct OperationTestCase {
  20.   const char * name;
  21.   bool preCond; // start transaction | insert | commit
  22.   // start transaction 1
  23.   const char * op1;
  24.   const int val1;
  25.   // no commit
  26.   // start transaction 2
  27.   const char * op2;
  28.   const int res2;
  29.   const int val2;
  30.   // no commit
  31.   // commit transaction 1
  32.   // commit transaction 2
  33.   
  34.   // start transaction
  35.   // op3 = READ
  36.   const int res3;
  37.   const int val3;
  38.   // commit transaction
  39. };
  40. #define X -1
  41. OperationTestCase matrix[] = {
  42.   { "ReadRead",         true, "READ",   1, "READ",      0, 1,   0, 1 },
  43.   { "ReadReadEx",       true, "READ",   1, "READ-EX", 266, X,   0, 1 },
  44.   { "ReadSimpleRead",   true, "READ",   1, "S-READ",    0, 1,   0, 1 },
  45.   { "ReadDirtyRead",    true, "READ",   1, "D-READ",    0, 1,   0, 1 },
  46.   { "ReadInsert",       true, "READ",   1, "INSERT",  266, X,   0, 1 },
  47.   { "ReadUpdate",       true, "READ",   1, "UPDATE",  266, X,   0, 1 },
  48.   { "ReadDelete",       true, "READ",   1, "DELETE",  266, X,   0, 1 },
  49.   { "ReadScan",         true, "READ",   1, "SCAN",      0, 1,   0, 1 },
  50.   { "ReadScanHl",       true, "READ",   1, "SCAN-HL",   0, 1,   0, 1 },
  51.   { "ReadScanEx",       true, "READ",   1, "SCAN-EX", 274, X,   0, 1 },
  52. #if 0
  53.   { "ReadScanUp",       true, "READ",   1, "SCAN-UP", 266, X,   0, 1 },
  54.   { "ReadScanDe",       true, "READ",   1, "SCAN-DE", 266, X,   0, 1 },
  55. #endif
  56.   { "ScanRead",         true, "SCAN",   1, "READ",      0, 1,   0, 1 },
  57.   { "ScanReadEx",       true, "SCAN",   1, "READ-EX",   0, 1,   0, 1 },
  58.   { "ScanSimpleRead",   true, "SCAN",   1, "S-READ",    0, 1,   0, 1 },
  59.   { "ScanDirtyRead",    true, "SCAN",   1, "D-READ",    0, 1,   0, 1 },
  60.   { "ScanInsert",       true, "SCAN",   1, "INSERT",  630, X,   0, 1 },
  61.   { "ScanUpdate",       true, "SCAN",   1, "UPDATE",    0, 2,   0, 2 },
  62.   { "ScanDelete",       true, "SCAN",   1, "DELETE",    0, X, 626, X },
  63.   { "ScanScan",         true, "SCAN",   1, "SCAN",      0, 1,   0, 1 },
  64.   { "ScanScanHl",       true, "SCAN",   1, "SCAN-HL",   0, 1,   0, 1 },
  65.   { "ScanScanEx",       true, "SCAN",   1, "SCAN-EX",   0, 1,   0, 1 },
  66. #if 0
  67.   { "ScanScanUp",       true, "SCAN",   1, "SCAN-UP", 266, X,   0, 1 },
  68.   { "ScanScanDe",       true, "SCAN",   1, "SCAN-DE", 266, X,   0, 1 },
  69. #endif
  70.   { "ScanHlRead",       true, "SCAN-HL",1, "READ",      0, 1,   0, 1 },
  71.   { "ScanHlReadEx",     true, "SCAN-HL",1, "READ-EX", 266, 1,   0, 1 },
  72.   { "ScanHlSimpleRead", true, "SCAN-HL",1, "S-READ",    0, 1,   0, 1 },
  73.   { "ScanHlDirtyRead",  true, "SCAN-HL",1, "D-READ",    0, 1,   0, 1 },
  74.   { "ScanHlInsert",     true, "SCAN-HL",1, "INSERT",  266, X,   0, 1 },
  75.   { "ScanHlUpdate",     true, "SCAN-HL",1, "UPDATE",  266, 2,   0, 1 },
  76.   { "ScanHlDelete",     true, "SCAN-HL",1, "DELETE",  266, X,   0, 1 },
  77.   { "ScanHlScan",       true, "SCAN-HL",1, "SCAN",      0, 1,   0, 1 },
  78.   { "ScanHlScanHl",     true, "SCAN-HL",1, "SCAN-HL",   0, 1,   0, 1 },
  79.   { "ScanHlScanEx",     true, "SCAN-HL",1, "SCAN-EX", 274, X,   0, 1 },
  80. #if 0
  81.   { "ScanHlScanUp",     true, "SCAN-HL",1, "SCAN-UP", 266, X,   0, 1 },
  82.   { "ScanHlScanDe",     true, "SCAN-HL",1, "SCAN-DE", 266, X,   0, 1 },
  83. #endif
  84.   { "ScanExRead",       true, "SCAN-EX",1, "READ",    266, 1,   0, 1 },
  85.   { "ScanExReadEx",     true, "SCAN-EX",1, "READ-EX", 266, 1,   0, 1 },
  86.   { "ScanExSimpleRead", true, "SCAN-EX",1, "S-READ",  266, 1,   0, 1 },
  87.   { "ScanExDirtyRead",  true, "SCAN-EX",1, "D-READ",    0, 1,   0, 1 },
  88.   { "ScanExInsert",     true, "SCAN-EX",1, "INSERT",  266, X,   0, 1 },
  89.   { "ScanExUpdate",     true, "SCAN-EX",1, "UPDATE",  266, 2,   0, 1 },
  90.   { "ScanExDelete",     true, "SCAN-EX",1, "DELETE",  266, X,   0, 1 },
  91.   { "ScanExScan",       true, "SCAN-EX",1, "SCAN",      0, 1,   0, 1 },
  92.   { "ScanExScanHl",     true, "SCAN-EX",1, "SCAN-HL", 274, X,   0, 1 },
  93.   { "ScanExScanEx",     true, "SCAN-EX",1, "SCAN-EX", 274, X,   0, 1 },
  94. #if 0
  95.   { "ScanExScanUp",     true, "SCAN-EX",1, "SCAN-UP", 266, X,   0, 1 },
  96.   { "ScanExScanDe",     true, "SCAN-EX",1, "SCAN-DE", 266, X,   0, 1 },
  97. #endif
  98.   { "ReadExRead",       true, "READ-EX",1, "READ",    266, X,   0, 1 },
  99.   { "ReadExReadEx",     true, "READ-EX",1, "READ-EX", 266, X,   0, 1 },
  100.   { "ReadExSimpleRead", true, "READ-EX",1, "S-READ",  266, X,   0, 1 },
  101.   { "ReadExDirtyRead",  true, "READ-EX",1, "D-READ",    0, 1,   0, 1 },
  102.   { "ReadExInsert",     true, "READ-EX",1, "INSERT",  266, X,   0, 1 },
  103.   { "ReadExUpdate",     true, "READ-EX",1, "UPDATE",  266, X,   0, 1 },
  104.   { "ReadExDelete",     true, "READ-EX",1, "DELETE",  266, X,   0, 1 },
  105.   { "ReadExScan",       true, "READ-EX",1, "SCAN",      0, 1,   0, 1 },
  106.   { "ReadExScanHl",     true, "READ-EX",1, "SCAN-HL", 274, X,   0, 1 },
  107.   { "ReadExScanEx",     true, "READ-EX",1, "SCAN-EX", 274, X,   0, 1 },
  108. #if 0
  109.   { "ReadExScanUp",     true, "READ-EX",1, "SCAN-UP", 266, X,   0, 1 },
  110.   { "ReadExScanDe",     true, "READ-EX",1, "SCAN-DE", 266, X,   0, 1 },
  111. #endif
  112.   { "InsertRead",      false, "INSERT", 1, "READ",    266, X,   0, 1 },
  113.   { "InsertReadEx",    false, "INSERT", 1, "READ-EX", 266, X,   0, 1 },
  114.   { "InsertSimpleRead",false, "INSERT", 1, "S-READ",  266, X,   0, 1 },
  115.   { "InsertDirtyRead", false, "INSERT", 1, "D-READ",  626, X,   0, 1 },
  116.   { "InsertInsert",    false, "INSERT", 1, "INSERT",  266, X,   0, 1 },
  117.   { "InsertUpdate",    false, "INSERT", 1, "UPDATE",  266, X,   0, 1 },
  118.   { "InsertDelete",    false, "INSERT", 1, "DELETE",  266, X,   0, 1 },
  119.   { "InsertScan",      false, "INSERT", 1, "SCAN",    626, X,   0, 1 },
  120.   { "InsertScanHl",    false, "INSERT", 1, "SCAN-HL", 274, X,   0, 1 },
  121.   { "InsertScanEx",    false, "INSERT", 1, "SCAN-EX", 274, X,   0, 1 },
  122. #if 0
  123.   { "InsertScanUp",    false, "INSERT",   1, "SCAN-UP", 266, X,   0, 1 },
  124.   { "InsertScanDe",    false, "INSERT",   1, "SCAN-DE", 266, X,   0, 1 },
  125. #endif
  126.   { "UpdateRead",       true, "UPDATE", 2, "READ",    266, X,   0, 2 },
  127.   { "UpdateReadEx",     true, "UPDATE", 2, "READ-EX", 266, X,   0, 2 },
  128.   { "UpdateSimpleRead", true, "UPDATE", 2, "S-READ",  266, X,   0, 2 },
  129.   { "UpdateDirtyRead",  true, "UPDATE", 2, "D-READ",    0, 1,   0, 2 },
  130.   { "UpdateInsert",     true, "UPDATE", 2, "INSERT",  266, X,   0, 2 },
  131.   { "UpdateUpdate",     true, "UPDATE", 2, "UPDATE",  266, X,   0, 2 },
  132.   { "UpdateDelete",     true, "UPDATE", 2, "DELETE",  266, X,   0, 2 },
  133.   { "UpdateScan",       true, "UPDATE", 2, "SCAN",      0, 1,   0, 2 },
  134.   { "UpdateScanHl",     true, "UPDATE", 2, "SCAN-HL", 274, X,   0, 2 },
  135.   { "UpdateScanEx",     true, "UPDATE", 2, "SCAN-EX", 274, X,   0, 2 },
  136. #if 0
  137.   { "UpdateScanUp",     true, "UPDATE", 2, "SCAN-UP", 266, X,   0, 2 },
  138.   { "UpdateScanDe",     true, "UPDATE", 2, "SCAN-DE", 266, X,   0, 2 },
  139. #endif
  140.   { "DeleteRead",       true, "DELETE", X, "READ",    266, X, 626, X },
  141.   { "DeleteReadEx",     true, "DELETE", X, "READ-EX", 266, X, 626, X },
  142.   { "DeleteSimpleRead", true, "DELETE", X, "S-READ",  266, X, 626, X },
  143.   { "DeleteDirtyRead",  true, "DELETE", X, "D-READ",    0, 1, 626, X },
  144.   { "DeleteInsert",     true, "DELETE", X, "INSERT",  266, X, 626, X },
  145.   { "DeleteUpdate",     true, "DELETE", X, "UPDATE",  266, X, 626, X },
  146.   { "DeleteDelete",     true, "DELETE", X, "DELETE",  266, X, 626, X },
  147.   { "DeleteScan",       true, "DELETE", X, "SCAN",      0, 1, 626, X },
  148.   { "DeleteScanHl",     true, "DELETE", X, "SCAN-HL", 274, X, 626, X },
  149.   { "DeleteScanEx",     true, "DELETE", X, "SCAN-EX", 274, X, 626, X },
  150. #if 0
  151.   { "DeleteScanUp",     true, "DELETE", X, "SCAN-UP", 266, X, 626, X },
  152.   { "DeleteScanDe",     true, "DELETE", X, "SCAN-DE", 266, X, 626, X }
  153. #endif
  154. };
  155. #define CHECK(a, b) { int x = a; int y = b; if (x != y) { 
  156.   g_err  << "ERR: "<< step->getName() 
  157.          << " failed on line " << __LINE__ << endl << "  " 
  158.          << x << " != " << y << endl;
  159.    result = NDBT_FAILED; 
  160.   break; } }
  161. int
  162. runOp(HugoOperations & hugoOps,
  163.       Ndb * pNdb,
  164.       const char * op,
  165.       int value){
  166. #define C2(x) if(!(x)) {
  167.   g_err  << "ERR: failed on line " << __LINE__ << endl; 
  168.   return NDBT_FAILED; }
  169.   
  170.   if(strcmp(op, "READ") == 0){
  171.     C2(hugoOps.pkReadRecord(pNdb, 1, 1, NdbOperation::LM_Read) == 0);
  172.   } else if(strcmp(op, "READ-EX") == 0){
  173.     C2(hugoOps.pkReadRecord(pNdb, 1, 1, NdbOperation::LM_Exclusive) == 0);  
  174.   } else if(strcmp(op, "S-READ") == 0){
  175.     C2(hugoOps.pkReadRecord(pNdb, 1, 1, NdbOperation::LM_Read) == 0);      
  176.   } else if(strcmp(op, "D-READ") == 0){
  177.     C2(hugoOps.pkReadRecord(pNdb, 1, 1, NdbOperation::LM_CommittedRead) == 0); 
  178.   } else if(strcmp(op, "INSERT") == 0){
  179.     C2(hugoOps.pkInsertRecord(pNdb, 1, 1, value) == 0);      
  180.   } else if(strcmp(op, "UPDATE") == 0){
  181.     C2(hugoOps.pkUpdateRecord(pNdb, 1, 1, value) == 0);      
  182.   } else if(strcmp(op, "DELETE") == 0){
  183.     C2(hugoOps.pkDeleteRecord(pNdb, 1, 1) == 0);      
  184.   } else if(strcmp(op, "SCAN") == 0){
  185.     C2(hugoOps.scanReadRecords(pNdb) == 0);
  186.   } else if(strcmp(op, "SCAN-HL") == 0){
  187.     C2(hugoOps.scanReadRecords(pNdb, NdbScanOperation::LM_Read)== 0);
  188.   } else if(strcmp(op, "SCAN-EX") == 0){
  189.     C2(hugoOps.scanReadRecords(pNdb, NdbScanOperation::LM_Exclusive)== 0);
  190.   } else {
  191.     g_err << __FILE__ << " - " << __LINE__ 
  192.   << ": Unknown operation" << op << endl;
  193.     return NDBT_FAILED;
  194.   }
  195.   
  196.   return NDBT_OK;
  197. }
  198. int
  199. checkVal(HugoOperations & hugoOps,
  200.  const char * op,
  201.  int value,
  202.  int result){
  203.   if(result != 0)
  204.     return NDBT_OK;
  205.   if(strcmp(op, "READ") == 0){
  206.   } else if(strcmp(op, "READ-EX") == 0){
  207.   } else if(strcmp(op, "S-READ") == 0){
  208.   } else if(strcmp(op, "D-READ") == 0){
  209.   } else if(strcmp(op, "SCAN") == 0){
  210.   } else if(strcmp(op, "SCAN-HL") == 0){
  211.   } else if(strcmp(op, "SCAN-EX") == 0){
  212.   } else {
  213.     return NDBT_OK;
  214.   }
  215.   
  216.   return hugoOps.verifyUpdatesValue(value);
  217. }
  218. #define TIMEOUT 100
  219. int
  220. setTransactionTimeout(NDBT_Context* ctx, NDBT_Step* step){
  221.   NdbRestarter restarter;
  222.   int val[] = 
  223.     { DumpStateOrd::TcSetTransactionTimeout, TIMEOUT };
  224.   if(restarter.dumpStateAllNodes(val, 2) != 0){
  225.     return NDBT_FAILED;
  226.   }
  227.   return NDBT_OK;
  228. }
  229. int 
  230. runTwoTrans1(NDBT_Context* ctx, NDBT_Step* step){
  231.   int result = NDBT_OK;
  232.   HugoOperations T1(*ctx->getTab());
  233.   Ndb* pNdb = GETNDB(step);
  234.   
  235.   const char * op1 = ctx->getProperty("op1", "NONE");
  236.   int val1 = ctx->getProperty("val1", ~0);
  237.   do {
  238.     // Insert, read
  239.     CHECK(T1.startTransaction(pNdb), 0);  
  240.     CHECK(runOp(T1, pNdb, op1, val1), 0);
  241.     CHECK(T1.execute_NoCommit(pNdb),  0);
  242.     CHECK(checkVal(T1, op1, val1, 0), 0);
  243.     
  244.     ctx->setProperty("T1-1-Complete", 1);
  245.     while(ctx->getProperty("T2-Complete", (Uint32)0) == 0){
  246.       T1.refresh();
  247.       NdbSleep_MilliSleep(10);
  248.     }
  249.     
  250.     CHECK(T1.execute_Commit(pNdb), 0);
  251.     
  252.   } while(false);
  253.   T1.closeTransaction(pNdb);
  254.   
  255.   if(result != NDBT_OK)
  256.     return result;
  257.   const int res3 = ctx->getProperty("res3", ~0);
  258.   const int val3 = ctx->getProperty("val3", ~0);
  259.   
  260.   do {
  261.     CHECK(T1.startTransaction(pNdb), 0);
  262.     CHECK(runOp(T1, pNdb, "READ", 0), 0);
  263.     CHECK(T1.execute_Commit(pNdb), res3);
  264.     CHECK(checkVal(T1, "READ", val3, res3), 0);
  265.   } while(false);
  266.   T1.closeTransaction(pNdb);
  267.   
  268.   return result;
  269. }
  270. int 
  271. runTwoTrans2(NDBT_Context* ctx, NDBT_Step* step){
  272.   int result = NDBT_OK;
  273.   HugoOperations T2(*ctx->getTab());
  274.   Ndb* pNdb = GETNDB(step);
  275.   
  276.   const char * op2 = ctx->getProperty("op2", "NONE");
  277.   const int res2 = ctx->getProperty("res2", ~0);
  278.   const int val2 = ctx->getProperty("val2", ~0);
  279.   while(ctx->getProperty("T1-1-Complete", (Uint32)0) == 0 && 
  280. !ctx->isTestStopped()){
  281.     NdbSleep_MilliSleep(100);
  282.   }
  283.   if(!ctx->isTestStopped()){
  284.     do {
  285.       CHECK(T2.startTransaction(pNdb), 0);  
  286.       CHECK(runOp(T2, pNdb, op2, val2), 0);
  287.       CHECK(T2.execute_NoCommit(pNdb), res2);
  288.       CHECK(checkVal(T2, op2, val2, res2), 0);
  289.       if(res2 == 0){
  290. CHECK(T2.execute_Commit(pNdb), res2);
  291.       }
  292.     } while(false);
  293.     T2.closeTransaction(pNdb);
  294.   }
  295.   
  296.   ctx->setProperty("T2-Complete", 1);  
  297.   
  298.   return result;
  299. }
  300. int 
  301. runInsertRecord(NDBT_Context* ctx, NDBT_Step* step){
  302.   int result = NDBT_OK;
  303.   HugoOperations hugoOps(*ctx->getTab());
  304.   Ndb* pNdb = GETNDB(step);
  305.   
  306.   do{
  307.     // Insert, insert 
  308.     CHECK(hugoOps.startTransaction(pNdb), 0);  
  309.     CHECK(hugoOps.pkInsertRecord(pNdb, 1, 1, 1), 0);
  310.     CHECK(hugoOps.execute_Commit(pNdb), 0);    
  311.   } while(false);
  312.   
  313.   hugoOps.closeTransaction(pNdb);
  314.   
  315.   return result;
  316. }
  317. int
  318. runClearTable(NDBT_Context* ctx, NDBT_Step* step){
  319.   int records = ctx->getNumRecords();
  320.   
  321.   UtilTransactions utilTrans(*ctx->getTab());
  322.   if (utilTrans.clearTable2(GETNDB(step), records, 240) != 0){
  323.     return NDBT_FAILED;
  324.   }
  325.   return NDBT_OK;
  326. }
  327. int
  328. main(int argc, const char** argv){
  329.   ndb_init();
  330.   NDBT_TestSuite ts("testOperations");
  331.   for(Uint32 i = 0; i<sizeof(matrix)/sizeof(matrix[0]); i++){
  332.     NDBT_TestCaseImpl1 *pt = new NDBT_TestCaseImpl1(&ts, matrix[i].name, "");
  333.     
  334.     pt->addInitializer(new NDBT_Initializer(pt, 
  335.     "runClearTable", 
  336.     runClearTable));
  337.     
  338.     if(matrix[i].preCond){
  339.       pt->addInitializer(new NDBT_Initializer(pt, 
  340.       "runInsertRecord", 
  341.       runInsertRecord));
  342.     }
  343.     
  344.     pt->addInitializer(new NDBT_Initializer(pt, 
  345.     "setTransactionTimeout", 
  346.     setTransactionTimeout));
  347.     
  348.     pt->setProperty("op1", matrix[i].op1);
  349.     pt->setProperty("val1", matrix[i].val1);
  350.     pt->setProperty("op2", matrix[i].op2);
  351.     pt->setProperty("res2", matrix[i].res2);
  352.     pt->setProperty("val2", matrix[i].val2);
  353.     pt->setProperty("res3", matrix[i].res3);
  354.     pt->setProperty("val3", matrix[i].val3);
  355.     pt->addStep(new NDBT_ParallelStep(pt, 
  356.       matrix[i].name,
  357.       runTwoTrans1));
  358.     pt->addStep(new NDBT_ParallelStep(pt, 
  359.       matrix[i].name,
  360.       runTwoTrans2));
  361.     pt->addFinalizer(new NDBT_Finalizer(pt, 
  362. "runClearTable", 
  363. runClearTable));
  364.     
  365.     ts.addTest(pt);
  366.   }
  367.   return ts.execute(argc, argv);
  368. }