22 #include <NdbThread.h>
26 #include "TraceNdbApi.hpp"
27 #include "VerifyNdbApi.hpp"
31 #define NdbSchemaCon CTraceNdbSchemaCon
32 #define NdbSchemaOp CTraceNdbSchemaOp
33 #define NdbConnection CTraceNdbConnection
34 #define NdbOperation CTraceNdbOperation
35 #define NdbIndexOperation CTraceNdbIndexOperation
36 #define NdbRecAttr CTraceNdbRecAttr
37 #define Table CTraceTable
38 #define Index CTraceIndex
39 #define Column CTraceColumn
40 #define NdbDictionary CTraceNdbDictionary
56 NdbMutex* g_pNdbMutexStop = 0;
60 Uint32 g_nAttribute = 1;
63 char* g_szAttribute = 0;
64 bool g_bVerify =
false;
65 bool g_bUseIndex =
false;
71 #define MATRIX_A 0x9908b0df
72 #define UPPER_MASK 0x80000000
73 #define LOWER_MASK 0x7fffffff
75 #define TEMPERING_MASK_B 0x9d2c5680
76 #define TEMPERING_MASK_C 0xefc60000
77 #define TEMPERING_SHIFT_U(y) (y >> 11)
78 #define TEMPERING_SHIFT_S(y) (y << 7)
79 #define TEMPERING_SHIFT_T(y) (y << 15)
80 #define TEMPERING_SHIFT_L(y) (y >> 18)
87 void sgenrand(
unsigned long seed);
88 unsigned long genrand(
void);
93 unsigned long mag01[2];
97 MT19937::MT19937(
void)
106 void MT19937::sgenrand(
unsigned long seed)
108 mt[0]= seed & 0xffffffff;
109 for (mti=1; mti<N; mti++)
110 mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
114 unsigned long MT19937::genrand(
void)
123 for (kk=0;kk<N-M;kk++) {
124 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
125 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
128 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
129 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
131 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
132 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
136 y ^= TEMPERING_SHIFT_U(y);
137 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
138 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
139 y ^= TEMPERING_SHIFT_L(y);
147 void CreateTables(
Ndb* pNdb)
149 for(Uint32 iTable=0; iTable<g_nTable; ++iTable)
154 table.
setName(g_szTable+iTable*4);
157 index.
setName(g_szIndex+iTable*4);
163 columnPK.setTupleKey(
true);
167 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
170 columnAttr.
setName(g_szAttribute+iAttr*4);
171 columnAttr.setTupleKey(
false);
195 int InsertTransaction(
Ndb* pNdb,
const Uint32 iPart,
const bool bIndex)
202 for(Uint32 iTable=0; iTable<g_nTable; ++iTable)
204 for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)
210 Uint32 nPK = iPart*g_nTuple + iTuple;
211 pNdbIndexOperation->
equal(
"PK", nPK);
212 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
214 Uint32 nValue = ((iPart*g_nTable+iTable)*g_nTuple+iTuple)*g_nAttribute+iAttr;
215 pNdbIndexOperation->
setValue(g_szAttribute+iAttr*4, nValue);
222 Uint32 nPK = iPart*g_nTuple + iTuple;
223 pNdbOperation->
equal(
"PK", nPK);
224 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
226 Uint32 nValue = ((iPart*g_nTable+iTable)*g_nTuple+iTuple)*g_nAttribute+iAttr;
227 pNdbOperation->
setValue(g_szAttribute+iAttr*4, nValue);
232 iExec = pNdbConnection->execute_ok(Commit);
243 int UpdateGetAndSetTransaction(
Ndb* pNdb,
const Uint32 iPart,
const bool bIndex)
251 for(Uint32 iTable=0; iTable<g_nTable; ++iTable)
253 for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)
259 Uint32 nPK = iPart*g_nTuple + iTuple;
260 pNdbIndexOperation->
equal(
"PK", nPK);
261 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
263 ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr]
264 = pNdbIndexOperation->
getValue(g_szAttribute+iAttr*4);
271 Uint32 nPK = iPart*g_nTuple + iTuple;
272 pNdbOperation->
equal(
"PK", nPK);
273 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
275 ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr]
276 = pNdbOperation->
getValue(g_szAttribute+iAttr*4);
281 iExec = pNdbConnection->execute_ok(NoCommit);
290 for(Uint32 iTable=0; iTable<g_nTable; ++iTable)
292 for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)
298 Uint32 nPK = iPart*g_nTuple + iTuple;
299 pNdbIndexOperation->
equal(
"PK", nPK);
300 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
303 = ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr];
304 Uint32 nValue = pNdbRecAttr->
u_32_value() + 1;
305 pNdbIndexOperation->
setValue(g_szAttribute+iAttr*4, nValue);
312 Uint32 nPK = iPart*g_nTuple + iTuple;
313 pNdbOperation->
equal(
"PK", nPK);
314 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
317 = ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr];
318 Uint32 nValue = pNdbRecAttr->
u_32_value() + 1;
319 pNdbOperation->
setValue(g_szAttribute+iAttr*4, nValue);
324 iExec = pNdbConnection->
execute(Commit);
331 delete[] ppNdbRecAttr;
336 int UpdateInterpretedTransaction(
Ndb* pNdb,
const Uint32 iPart,
const bool bIndex)
343 for(Uint32 iTable=0; iTable<g_nTable; ++iTable)
345 for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)
351 Uint32 nPK = iPart*g_nTuple + iTuple;
352 pNdbIndexOperation->
equal(
"PK", nPK);
353 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
355 pNdbIndexOperation->
incValue(g_szAttribute+iAttr*4, (Uint32)1);
362 Uint32 nPK = iPart*g_nTuple + iTuple;
363 pNdbOperation->
equal(
"PK", nPK);
364 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
366 pNdbOperation->
incValue(g_szAttribute+iAttr*4, (Uint32)1);
371 iExec = pNdbConnection->execute_ok(Commit);
383 void ReportInconsistency (
const Uint32 iPart,
388 const Uint32 nExpected )
390 ndbout <<
"INCONSISTENCY: ";
391 ndbout <<
"Part " << iPart;
392 ndbout <<
", Table " << iTable;
393 ndbout <<
", Tuple " << iTuple;
394 ndbout <<
", Attr " << iAttr;
395 ndbout <<
", Value " << nValue;
396 ndbout <<
", Expected " << nExpected;
401 int ReadTransaction(
Ndb* pNdb,
const Uint32 iPart,
const bool bIndex)
409 for(Uint32 iTable=0; iTable<g_nTable; ++iTable)
411 for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)
417 Uint32 nPK = iPart*g_nTuple + iTuple;
418 pNdbIndexOperation->
equal(
"PK", nPK);
419 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
421 ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr]
422 = pNdbIndexOperation->
getValue(g_szAttribute+iAttr*4);
429 Uint32 nPK = iPart*g_nTuple + iTuple;
430 pNdbOperation->
equal(
"PK", nPK);
431 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
433 ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr]
434 = pNdbOperation->
getValue(g_szAttribute+iAttr*4);
439 iExec = pNdbConnection->execute_ok(Commit);
446 Uint32 nValue0 = ppNdbRecAttr[0]->
u_32_value();
447 for(Uint32 iTable=0; iTable<g_nTable; ++iTable)
449 for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)
451 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
453 Uint32 nValue = ppNdbRecAttr[(iTable*g_nTuple+iTuple)*g_nAttribute+iAttr]->u_32_value();
454 Uint32 nExpected = nValue0 + (iTable*g_nTuple+iTuple)*g_nAttribute+iAttr;
455 if(nValue!=nExpected)
457 ReportInconsistency(iPart, iTable, iTuple, iAttr, nValue, nExpected);
469 int DeleteTransaction(
Ndb* pNdb,
const Uint32 iPart,
const bool bIndex)
476 for(Uint32 iTable=0; iTable<g_nTable; ++iTable)
478 for(Uint32 iTuple=0; iTuple<g_nTuple; ++iTuple)
484 Uint32 nPK = iPart*g_nTuple + iTuple;
485 pNdbIndexOperation->
equal(
"PK", nPK);
491 Uint32 nPK = iPart*g_nTuple + iTuple;
492 pNdbOperation->
equal(
"PK", nPK);
496 iExec = pNdbConnection->execute_ok(Commit);
508 extern "C" void* ThreadFunc(
void*)
510 Ndb* pNdb =
new Ndb(
"TEST_DB");
515 rndgen.sgenrand((
unsigned long)pNdb);
517 Uint32 nInsertError = 0;
518 Uint32 nInsertCommit = 0;
519 Uint32 nInsertRollback = 0;
520 Uint32 nUpdateGetAndSetError = 0;
521 Uint32 nUpdateGetAndSetCommit = 0;
522 Uint32 nUpdateGetAndSetRollback = 0;
523 Uint32 nReadError = 0;
524 Uint32 nReadCommit = 0;
525 Uint32 nReadRollback = 0;
526 Uint32 nUpdateInterpretedError = 0;
527 Uint32 nUpdateInterpretedCommit = 0;
528 Uint32 nUpdateInterpretedRollback = 0;
529 Uint32 nDeleteError = 0;
530 Uint32 nDeleteCommit = 0;
531 Uint32 nDeleteRollback = 0;
535 for (Uint32 iPart = 0; iPart < g_nPart; iPart++)
537 switch(ReadTransaction(pNdb, iPart,
false))
539 case -1: ++nReadError;
break;
540 case 0: ++nReadCommit;
break;
541 case 1: ++nReadRollback;
break;
546 while(NdbMutex_Trylock(g_pNdbMutexStop))
548 Uint32 iPart = rndgen.genrand() % g_nPart;
549 Uint32 iTrans = rndgen.genrand() % 5;
550 bool bIndex = ((rndgen.genrand() & 1) ?
true :
false);
554 switch(InsertTransaction(pNdb, iPart, bIndex))
556 case -1: ++nInsertError;
break;
557 case 0: ++nInsertCommit;
break;
558 case 1: ++nInsertRollback;
break;
563 switch(UpdateGetAndSetTransaction(pNdb, iPart, bIndex))
565 case -1: ++nUpdateGetAndSetError;
break;
566 case 0: ++nUpdateGetAndSetCommit;
break;
567 case 1: ++nUpdateGetAndSetRollback;
break;
572 switch(ReadTransaction(pNdb, iPart, bIndex))
574 case -1: ++nReadError;
break;
575 case 0: ++nReadCommit;
break;
576 case 1: ++nReadRollback;
break;
581 switch(UpdateInterpretedTransaction(pNdb, iPart, bIndex))
583 case -1: ++nUpdateInterpretedError;
break;
584 case 0: ++nUpdateInterpretedCommit;
break;
585 case 1: ++nUpdateInterpretedRollback;
break;
590 switch(DeleteTransaction(pNdb, iPart, bIndex))
592 case -1: ++nDeleteError;
break;
593 case 0: ++nDeleteCommit;
break;
594 case 1: ++nDeleteRollback;
break;
600 ndbout <<
"I:" << nInsertError <<
":" << nInsertCommit <<
":" << nInsertRollback;
601 ndbout <<
" UG:" << nUpdateGetAndSetError <<
":" << nUpdateGetAndSetCommit <<
":" << nUpdateGetAndSetRollback;
602 ndbout <<
" R:" << nReadError <<
":" << nReadCommit <<
":" << nReadRollback;
603 ndbout <<
" UI:" << nUpdateInterpretedError <<
":" << nUpdateInterpretedCommit <<
":" << nUpdateInterpretedRollback;
604 ndbout <<
" D:" << nDeleteError <<
":" << nDeleteCommit <<
":" << nDeleteRollback << endl;
607 NdbMutex_Unlock(g_pNdbMutexStop);
613 int main(
int argc,
char* argv[])
619 for(
int iArg=1; iArg<argc; ++iArg)
621 if(argv[iArg][0]==
'-')
623 switch(argv[iArg][1])
625 case 'p': g_nPart = atol(argv[iArg]+2);
break;
626 case 'b': g_nTable = atol(argv[iArg]+2);
break;
627 case 'u': g_nTuple = atol(argv[iArg]+2);
break;
628 case 'a': g_nAttribute = atol(argv[iArg]+2);
break;
629 case 'v': g_bVerify =
true;
break;
630 case 't': nThread = atol(argv[iArg]+2);
break;
631 case 's': nSeconds = atol(argv[iArg]+2);
break;
632 case 'i': g_bUseIndex =
true;
break;
637 ndbout <<
" -p" << g_nPart;
638 ndbout <<
" -b" << g_nTable;
639 ndbout <<
" -u" << g_nTuple;
640 ndbout <<
" -a" << g_nAttribute;
643 ndbout <<
" -t" << nThread;
644 ndbout <<
" -s" << nSeconds;
647 g_szTable =
new char[g_nTable*4];
648 for(Uint32 iTable=0; iTable<g_nTable; ++iTable)
650 sprintf(g_szTable+iTable*4,
"T%02d", iTable);
653 g_szIndex =
new char[g_nTable*4];
654 for(Uint32 iIndex=0; iIndex<g_nTable; ++iIndex)
656 sprintf(g_szIndex+iIndex*4,
"I%02d", iIndex);
659 g_szAttribute =
new char[g_nAttribute*4];
660 for(Uint32 iAttr=0; iAttr<g_nAttribute; ++iAttr)
662 sprintf(g_szAttribute+iAttr*4,
"A%02d", iAttr);
666 Ndb* pNdb =
new Ndb(
"TEST_DB");
670 if (!g_bVerify) CreateTables(pNdb);
671 g_pNdbMutexStop = NdbMutex_Create();
672 NdbMutex_Lock(g_pNdbMutexStop);
674 NdbThread_SetConcurrencyLevel(nThread+1);
676 for(Uint32 iThread=0; iThread<nThread; ++iThread)
678 ppNdbThread[iThread] = NdbThread_Create(ThreadFunc, 0, 0,
"ThreadFunc", NDB_THREAD_PRIO_MEAN);
681 NdbSleep_SecSleep(nSeconds);
682 NdbMutex_Unlock(g_pNdbMutexStop);
684 for(Uint32 iThread=0; iThread<nThread; ++iThread)
687 NdbThread_WaitFor(ppNdbThread[iThread], &status);
690 NdbMutex_Destroy(g_pNdbMutexStop);