testTimeout.cpp 14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <NDBT.hpp>
#include <NDBT_Test.hpp>
#include <HugoTransactions.hpp>
#include <UtilTransactions.hpp>
#include <random.h>
#include <NdbConfig.hpp>
unknown's avatar
unknown committed
23 24
#include <signaldata/DumpStateOrd.hpp>

unknown's avatar
unknown committed
25
#define TIMEOUT (Uint32)3000
unknown's avatar
unknown committed
26
Uint32 g_org_timeout = 3000;
unknown's avatar
unknown committed
27
Uint32 g_org_deadlock = 3000;
unknown's avatar
unknown committed
28 29 30 31

int
setTransactionTimeout(NDBT_Context* ctx, NDBT_Step* step){
  NdbRestarter restarter;
unknown's avatar
unknown committed
32 33
  int timeout = ctx->getProperty("TransactionInactiveTimeout",TIMEOUT);

unknown's avatar
unknown committed
34 35 36 37 38 39 40 41 42
  NdbConfig conf(GETNDB(step)->getNodeId()+1);
  unsigned int nodeId = conf.getMasterNodeId();
  if (!conf.getProperty(nodeId,
			NODE_TYPE_DB, 
			CFG_DB_TRANSACTION_INACTIVE_TIMEOUT,
			&g_org_timeout)){
    return NDBT_FAILED;
  }

unknown's avatar
unknown committed
43
  int val[] = { DumpStateOrd::TcSetApplTransactionTimeout, timeout };
unknown's avatar
unknown committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  if(restarter.dumpStateAllNodes(val, 2) != 0){
    return NDBT_FAILED;
  }
  
  return NDBT_OK;
}

int
resetTransactionTimeout(NDBT_Context* ctx, NDBT_Step* step){
  NdbRestarter restarter;
  
  int val[] = { DumpStateOrd::TcSetApplTransactionTimeout, g_org_timeout };
  if(restarter.dumpStateAllNodes(val, 2) != 0){
    return NDBT_FAILED;
  }
  
  return NDBT_OK;
}
62

unknown's avatar
unknown committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
int
setDeadlockTimeout(NDBT_Context* ctx, NDBT_Step* step){
  NdbRestarter restarter;
  int timeout = ctx->getProperty("TransactionDeadlockTimeout", TIMEOUT);
  
  NdbConfig conf(GETNDB(step)->getNodeId()+1);
  unsigned int nodeId = conf.getMasterNodeId();
  if (!conf.getProperty(nodeId,
			NODE_TYPE_DB, 
			CFG_DB_TRANSACTION_DEADLOCK_TIMEOUT,
			&g_org_deadlock))
    return NDBT_FAILED;
  
  g_err << "Setting timeout: " << timeout << endl;
  int val[] = { DumpStateOrd::TcSetTransactionTimeout, timeout };
  if(restarter.dumpStateAllNodes(val, 2) != 0){
    return NDBT_FAILED;
  }
  
  return NDBT_OK;
}

int
getDeadlockTimeout(NDBT_Context* ctx, NDBT_Step* step){
  NdbRestarter restarter;
  
  Uint32 val = 0;
  NdbConfig conf(GETNDB(step)->getNodeId()+1);
  unsigned int nodeId = conf.getMasterNodeId();
  if (!conf.getProperty(nodeId,
			NODE_TYPE_DB, 
			CFG_DB_TRANSACTION_DEADLOCK_TIMEOUT,
			&val))
    return NDBT_FAILED;

  if (val < 120000)
    val = 120000;
  ctx->setProperty("TransactionDeadlockTimeout", 4*val);
  
  return NDBT_OK;
}

int
resetDeadlockTimeout(NDBT_Context* ctx, NDBT_Step* step){
  NdbRestarter restarter;
  
  int val[] = { DumpStateOrd::TcSetTransactionTimeout, g_org_deadlock };
  if(restarter.dumpStateAllNodes(val, 2) != 0){
    return NDBT_FAILED;
  }
  
  return NDBT_OK;
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

int runLoadTable(NDBT_Context* ctx, NDBT_Step* step){

  int records = ctx->getNumRecords();
  HugoTransactions hugoTrans(*ctx->getTab());
  if (hugoTrans.loadTable(GETNDB(step), records) != 0){
    return NDBT_FAILED;
  }
  return NDBT_OK;
}

int runClearTable(NDBT_Context* ctx, NDBT_Step* step){
  int records = ctx->getNumRecords();
  
  UtilTransactions utilTrans(*ctx->getTab());
  if (utilTrans.clearTable2(GETNDB(step),  records) != 0){
    return NDBT_FAILED;
  }
  return NDBT_OK;
}


#define CHECK(b) if (!(b)) { \
  ndbout << "ERR: "<< step->getName() \
         << " failed on line " << __LINE__ << endl; \
  result = NDBT_FAILED; \
  break; }

unknown's avatar
unknown committed
145 146 147 148 149 150 151 152
int runTimeoutTrans2(NDBT_Context* ctx, NDBT_Step* step){
  int result = NDBT_OK;
  int loops = ctx->getNumLoops();
  int stepNo = step->getStepNo();
  int mul1 = ctx->getProperty("Op1", (Uint32)0);
  int mul2 = ctx->getProperty("Op2", (Uint32)0);
  int records = ctx->getNumRecords();

unknown's avatar
unknown committed
153 154 155 156
  int timeout = ctx->getProperty("TransactionInactiveTimeout",TIMEOUT);

  int minSleep = (int)(timeout * 1.5);
  int maxSleep = timeout * 2;
unknown's avatar
unknown committed
157 158 159 160
  
  HugoOperations hugoOps(*ctx->getTab());
  Ndb* pNdb = GETNDB(step);

unknown's avatar
unknown committed
161
  for (int l = 0; l<loops && !ctx->isTestStopped() && result == NDBT_OK; l++){
unknown's avatar
unknown committed
162 163 164 165 166 167 168
    
    int op1 = 0 + (l + stepNo) * mul1;
    int op2 = 0 + (l + stepNo) * mul2;

    op1 = (op1 % 5);
    op2 = (op2 % 5);

unknown's avatar
unknown committed
169
    ndbout << stepNo << ": TransactionInactiveTimeout="<< timeout
unknown's avatar
unknown committed
170 171 172 173 174 175 176 177 178 179 180 181 182
	   << ", minSleep="<<minSleep
	   << ", maxSleep="<<maxSleep
	   << ", op1=" << op1
	   << ", op2=" << op2 << endl;;
    
    do{
      // Commit transaction
      CHECK(hugoOps.startTransaction(pNdb) == 0);
      
      switch(op1){
      case 0:
	break;
      case 1:
183
	if(hugoOps.pkReadRecord(pNdb, stepNo) != 0){
unknown's avatar
unknown committed
184
	  g_err << stepNo << ": Fail" << __LINE__ << endl;
unknown's avatar
unknown committed
185
	  result = NDBT_FAILED; break;
unknown's avatar
unknown committed
186 187 188
	}
	break;
      case 2:
189
	if(hugoOps.pkUpdateRecord(pNdb, stepNo) != 0){
unknown's avatar
unknown committed
190
	  g_err << stepNo << ": Fail" << __LINE__ << endl;
unknown's avatar
unknown committed
191
	  result = NDBT_FAILED; break;
unknown's avatar
unknown committed
192 193 194
	}
	break;
      case 3:
195
	if(hugoOps.pkDeleteRecord(pNdb, stepNo) != 0){
unknown's avatar
unknown committed
196
	  g_err << stepNo << ": Fail" << __LINE__ << endl;
unknown's avatar
unknown committed
197
	  result = NDBT_FAILED; break;
unknown's avatar
unknown committed
198 199 200
	}
	break;
      case 4:
201
	if(hugoOps.pkInsertRecord(pNdb, stepNo+records+l) != 0){
unknown's avatar
unknown committed
202
	  g_err << stepNo << ": Fail" << __LINE__ << endl;
unknown's avatar
unknown committed
203
	  result = NDBT_FAILED; break;
unknown's avatar
unknown committed
204 205 206
	}
	break;
      }
unknown's avatar
unknown committed
207 208 209 210
      
      if(result != NDBT_OK)
	break;

unknown's avatar
unknown committed
211 212 213
      int res = hugoOps.execute_NoCommit(pNdb);
      if(res != 0){
	g_err << stepNo << ": Fail" << __LINE__ << endl;
unknown's avatar
unknown committed
214
	result = NDBT_FAILED; break;
unknown's avatar
unknown committed
215 216 217 218 219 220 221 222 223 224
      }
      
      int sleep = minSleep + myRandom48(maxSleep-minSleep);   
      ndbout << stepNo << ": Sleeping for "<< sleep << " milliseconds" << endl;
      NdbSleep_MilliSleep(sleep);
      
      switch(op2){
      case 0:
	break;
      case 1:
225
	if(hugoOps.pkReadRecord(pNdb, stepNo) != 0){
unknown's avatar
unknown committed
226
	  g_err << stepNo << ": Fail" << __LINE__ << endl;
unknown's avatar
unknown committed
227
	  result = NDBT_FAILED; break;
unknown's avatar
unknown committed
228 229 230
	}
	break;
      case 2:
231
	if(hugoOps.pkUpdateRecord(pNdb, stepNo) != 0){
unknown's avatar
unknown committed
232
	  g_err << stepNo << ": Fail" << __LINE__ << endl;
unknown's avatar
unknown committed
233
	  result = NDBT_FAILED; break;
unknown's avatar
unknown committed
234 235 236
	}
	break;
      case 3:
237
	if(hugoOps.pkDeleteRecord(pNdb, stepNo) != 0){
unknown's avatar
unknown committed
238
	  g_err << stepNo << ": Fail" << __LINE__ << endl;
unknown's avatar
unknown committed
239
	  result = NDBT_FAILED; break;
unknown's avatar
unknown committed
240 241 242
	}
	break;
      case 4:
243
	if(hugoOps.pkInsertRecord(pNdb, stepNo+2*records+l) != 0){
unknown's avatar
unknown committed
244
	  g_err << stepNo << ": Fail" << __LINE__ << endl;
unknown's avatar
unknown committed
245
	  result = NDBT_FAILED; break;
unknown's avatar
unknown committed
246 247 248 249 250 251
	}
	break;
      }

      // Expect that transaction has timed-out
      res = hugoOps.execute_Commit(pNdb);
252
      if(op1 != 0 && res != 266){
unknown's avatar
unknown committed
253 254
	g_err << stepNo << ": Fail: " << res << "!= 237, op1=" 
	      << op1 << ", op2=" << op2 << endl;
unknown's avatar
unknown committed
255
	result = NDBT_FAILED; break;
unknown's avatar
unknown committed
256 257 258 259 260 261 262 263 264 265
      }
      
    } while(false);
    
    hugoOps.closeTransaction(pNdb);
  }

  return result;
}

266 267 268 269
int runDontTimeoutTrans(NDBT_Context* ctx, NDBT_Step* step){
  int result = NDBT_OK;
  int loops = ctx->getNumLoops();
  int stepNo = step->getStepNo();
unknown's avatar
unknown committed
270

unknown's avatar
unknown committed
271 272 273 274
  int timeout = ctx->getProperty("TransactionInactiveTimeout",TIMEOUT);

  int maxSleep = (int)(timeout * 0.5);
  ndbout << "TransactionInactiveTimeout="<< timeout
275 276 277 278 279 280 281 282 283 284 285
	 << ", maxSleep="<<maxSleep<<endl;


  HugoOperations hugoOps(*ctx->getTab());
  Ndb* pNdb = GETNDB(step);

  for (int l = 0; l < loops && result == NDBT_OK; l++){

    do{
      // Commit transaction
      CHECK(hugoOps.startTransaction(pNdb) == 0);
286
      CHECK(hugoOps.pkReadRecord(pNdb, stepNo) == 0);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
      CHECK(hugoOps.execute_NoCommit(pNdb) == 0);
      
      int sleep = myRandom48(maxSleep);   
      ndbout << "Sleeping for " << sleep << " milliseconds" << endl;
      NdbSleep_MilliSleep(sleep);
      
      // Expect that transaction has NOT timed-out
      CHECK(hugoOps.execute_Commit(pNdb) == 0); 
    
    } while(false);

    hugoOps.closeTransaction(pNdb);
  }
    
  return result;
}

unknown's avatar
unknown committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
int runDeadlockTimeoutTrans(NDBT_Context* ctx, NDBT_Step* step){
  int result = NDBT_OK;
  int loops = ctx->getNumLoops();
  int stepNo = step->getStepNo();

  Uint32 deadlock_timeout;
  NdbConfig conf(GETNDB(step)->getNodeId()+1);
  unsigned int nodeId = conf.getMasterNodeId();
  if (!conf.getProperty(nodeId,
                        NODE_TYPE_DB,
                        CFG_DB_TRANSACTION_DEADLOCK_TIMEOUT,
                        &deadlock_timeout)){
    return NDBT_FAILED;
  }


  int do_sleep = (int)(deadlock_timeout * 0.5);


  HugoOperations hugoOps(*ctx->getTab());
  Ndb* pNdb = GETNDB(step);

  for (int l = 0; l < loops && result == NDBT_OK; l++){

    do{
      // Commit transaction
      CHECK(hugoOps.startTransaction(pNdb) == 0);
      CHECK(hugoOps.pkReadRecord(pNdb, stepNo) == 0);
      CHECK(hugoOps.execute_NoCommit(pNdb) == 0);

      int sleep = deadlock_timeout * 1.5 + myRandom48(do_sleep);
      ndbout << "Sleeping for " << sleep << " milliseconds" << endl;
      NdbSleep_MilliSleep(sleep);

      // Expect that transaction has NOT timed-out
      CHECK(hugoOps.execute_Commit(pNdb) == 0);

    } while(false);

    hugoOps.closeTransaction(pNdb);
  }

  return result;
}

349 350 351 352 353
int runBuddyTransNoTimeout(NDBT_Context* ctx, NDBT_Step* step){
  int result = NDBT_OK;
  int loops = ctx->getNumLoops();
  int records = ctx->getNumRecords();
  int stepNo = step->getStepNo();
unknown's avatar
unknown committed
354 355
  int maxSleep = (int)(TIMEOUT * 0.3);
  ndbout << "TransactionInactiveTimeout="<< TIMEOUT
356 357 358 359 360 361 362 363 364 365 366
	 << ", maxSleep="<<maxSleep<<endl;

  HugoOperations hugoOps(*ctx->getTab());
  Ndb* pNdb = GETNDB(step);

  for (int l = 1; l < loops && result == NDBT_OK; l++){

    do{
      // Start an insert trans
      CHECK(hugoOps.startTransaction(pNdb) == 0);
      int recordNo = records + (stepNo*loops) + l;
367
      CHECK(hugoOps.pkInsertRecord(pNdb, recordNo) == 0);
368 369
      CHECK(hugoOps.execute_NoCommit(pNdb) == 0);
      
unknown's avatar
unknown committed
370
      for (int i = 0; i < 3; i++){
371
	// Perform buddy scan reads
unknown's avatar
unknown committed
372
	CHECK((hugoOps.scanReadRecords(pNdb)) == 0);
373 374
	CHECK(hugoOps.execute_NoCommit(pNdb) == 0); 
	
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	int sleep = myRandom48(maxSleep);   	
	ndbout << "Sleeping for " << sleep << " milliseconds" << endl;
	NdbSleep_MilliSleep(sleep);
      }

      // Expect that transaction has NOT timed-out
      CHECK(hugoOps.execute_Commit(pNdb) == 0); 
    
    } while(false);

    hugoOps.closeTransaction(pNdb);
  }

  return result;
}

unknown's avatar
unknown committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
int 
runError4012(NDBT_Context* ctx, NDBT_Step* step){
  int result = NDBT_OK;
  int loops = ctx->getNumLoops();
  int stepNo = step->getStepNo();
  
  int timeout = ctx->getProperty("TransactionDeadlockTimeout", TIMEOUT);

  HugoOperations hugoOps(*ctx->getTab());
  Ndb* pNdb = GETNDB(step);

  do{
    // Commit transaction
    CHECK(hugoOps.startTransaction(pNdb) == 0);
    CHECK(hugoOps.pkUpdateRecord(pNdb, 0) == 0);
    int ret = hugoOps.execute_NoCommit(pNdb);
    if (ret == 0)
    {
      int sleep = timeout;
      ndbout << "Sleeping for " << sleep << " milliseconds" << endl;
      NdbSleep_MilliSleep(sleep);
      
      // Expect that transaction has NOT timed-out
      CHECK(hugoOps.execute_Commit(pNdb) == 0);
    }
    else
    {
      CHECK(ret == 4012);
    }
  } while(false);
  
  hugoOps.closeTransaction(pNdb);
  
  return result;
}


428 429 430 431 432 433
NDBT_TESTSUITE(testTimeout);
TESTCASE("DontTimeoutTransaction", 
	 "Test that the transaction does not timeout "\
	 "if we sleep during the transaction. Use a sleep "\
	 "value which is smaller than TransactionInactiveTimeout"){
  INITIALIZER(runLoadTable);
unknown's avatar
unknown committed
434
  INITIALIZER(setTransactionTimeout);
435
  STEPS(runDontTimeoutTrans, 1); 
unknown's avatar
unknown committed
436
  FINALIZER(resetTransactionTimeout);
437 438
  FINALIZER(runClearTable);
}
unknown's avatar
unknown committed
439 440 441 442 443 444 445 446 447
TESTCASE("Bug11290",
         "Setting TransactionInactiveTimeout to 0(zero) "\
         "should result in infinite timeout, and not as "\
         "was the bug, a timeout that is equal to the deadlock timeout"){
  TC_PROPERTY("TransactionInactiveTimeout",(Uint32)0);
  INITIALIZER(runLoadTable);
  INITIALIZER(setTransactionTimeout);
  STEPS(runDeadlockTimeoutTrans, 1);
  FINALIZER(resetTransactionTimeout);
448 449 450 451 452 453 454 455
  FINALIZER(runClearTable);
}
TESTCASE("DontTimeoutTransaction5", 
	 "Test that the transaction does not timeout "\
	 "if we sleep during the transaction. Use a sleep "\
	 "value which is smaller than TransactionInactiveTimeout" \
	 "Five simultaneous threads"){
  INITIALIZER(runLoadTable);
unknown's avatar
unknown committed
456
  INITIALIZER(setTransactionTimeout);
457
  STEPS(runDontTimeoutTrans, 5); 
unknown's avatar
unknown committed
458
  FINALIZER(resetTransactionTimeout);
459 460
  FINALIZER(runClearTable);
}
unknown's avatar
unknown committed
461 462 463 464 465
TESTCASE("TimeoutRandTransaction", 
	 "Test that the transaction does timeout "\
	 "if we sleep during the transaction. Use a sleep "\
	 "value which is larger than TransactionInactiveTimeout"){
  INITIALIZER(runLoadTable);
unknown's avatar
unknown committed
466
  INITIALIZER(setTransactionTimeout);
unknown's avatar
unknown committed
467 468 469
  TC_PROPERTY("Op1", 7);
  TC_PROPERTY("Op2", 11);
  STEPS(runTimeoutTrans2, 5);
unknown's avatar
unknown committed
470
  FINALIZER(resetTransactionTimeout);
unknown's avatar
unknown committed
471 472
  FINALIZER(runClearTable);
}
473 474 475 476 477 478 479
TESTCASE("BuddyTransNoTimeout", 
	 "Start a transaction and perform an insert with NoCommit. " \
	 "Start a buddy transaction wich performs long running scans " \
	 "and sleeps. " \
	 "The total sleep time is longer than TransactionInactiveTimeout" \
	 "Commit the first transaction, it should not have timed out."){
  INITIALIZER(runLoadTable);
unknown's avatar
unknown committed
480
  INITIALIZER(setTransactionTimeout);
481
  STEPS(runBuddyTransNoTimeout, 1);
unknown's avatar
unknown committed
482
  FINALIZER(resetTransactionTimeout);
483 484 485 486 487 488 489 490 491 492
  FINALIZER(runClearTable);
}
TESTCASE("BuddyTransNoTimeout5", 
	 "Start a transaction and perform an insert with NoCommit. " \
	 "Start a buddy transaction wich performs long running scans " \
	 "and sleeps. " \
	 "The total sleep time is longer than TransactionInactiveTimeout" \
	 "Commit the first transaction, it should not have timed out." \
	 "Five simultaneous threads"){
  INITIALIZER(runLoadTable);
unknown's avatar
unknown committed
493
  INITIALIZER(setTransactionTimeout);
494
  STEPS(runBuddyTransNoTimeout, 5);
unknown's avatar
unknown committed
495
  FINALIZER(resetTransactionTimeout);
496 497
  FINALIZER(runClearTable);
}
unknown's avatar
unknown committed
498 499 500 501 502 503 504 505 506
TESTCASE("Error4012", ""){
  TC_PROPERTY("TransactionDeadlockTimeout", 120000);
  INITIALIZER(runLoadTable);
  INITIALIZER(getDeadlockTimeout);
  INITIALIZER(setDeadlockTimeout);
  STEPS(runError4012, 2);
  FINALIZER(runClearTable);
}

507 508 509
NDBT_TESTSUITE_END(testTimeout);

int main(int argc, const char** argv){
unknown's avatar
unknown committed
510
  ndb_init();
511 512 513 514
  myRandom48Init(NdbTick_CurrentMillisecond());
  return testTimeout.execute(argc, argv);
}