20 #include <NdbSchemaCon.hpp>
24 #include <NdbThread.h>
27 #include <NdbTest.hpp>
33 #define NODE_REC // epaulsa: introduces pointer checks to help 'acid' keep core
38 #define VerifyMethodInt(c, m) (ReportMethodInt(c->m, c, #c, #m, __FILE__, __LINE__))
39 #define VerifyMethodPtr(v, c, m) (v=ReportMethodPtr(c->m, c, #v, #c, #m, __FILE__, __LINE__))
40 #define VerifyMethodVoid(c, m) (c->m, ReportMethodVoid(c, #c, #m, __FILE__, __LINE__))
42 int ReportMethodInt(
int iRes,
NdbConnection* pNdbConnection,
const char* szClass,
const char* szMethod,
const char* szFile,
const int iLine)
44 ndbout << szFile <<
"(" << iLine <<
") : ";
45 ndbout << szClass <<
"->" << szMethod <<
" return " << iRes <<
" : ";
57 int ReportMethodInt(
int iRes, C* pC,
const char* szClass,
const char* szMethod,
const char* szFile,
const int iLine)
59 ndbout << szFile <<
"(" << iLine <<
") : ";
60 ndbout << szClass <<
"->" << szMethod <<
" return " << iRes <<
" : ";
61 ndbout << pC->getNdbError();
66 template <
class R,
class C>
67 R* ReportMethodPtr(R* pR, C* pC,
const char* szVariable,
const char* szClass,
const char* szMethod,
const char* szFile,
const int iLine)
69 ndbout << szFile <<
"(" << iLine <<
") : ";
70 ndbout << szVariable <<
" = " << szClass <<
"->" << szMethod <<
" return " << (long)(
void*)pR << " : ";
71 ndbout << pC->getNdbError();
77 void ReportMethodVoid(C* pC, const
char* szClass, const
char* szMethod, const
char* szFile, const
int iLine)
79 ndbout << szFile <<
"(" << iLine <<
") : ";
80 ndbout << szClass <<
"->" << szMethod <<
" : ";
81 ndbout << pC->getNdbError();
89 #define VerifyMethodInt(c, m) (ReportMethodInt(c->m, c, #c, #m, __FILE__, __LINE__))
90 #define VerifyMethodPtr(v, c, m) (v=ReportMethodPtr(c->m, c, #v, #c, #m, __FILE__, __LINE__))
91 #define VerifyMethodVoid(c, m) (c->m, ReportMethodVoid(c, #c, #m, __FILE__, __LINE__))
93 int ReportMethodInt(
int iRes,
NdbConnection* pNdbConnection,
const char* szClass,
const char* szMethod,
const char* szFile,
const int iLine)
96 ndbout << szFile <<
"(" << iLine <<
") : ";
97 ndbout << szClass <<
"->" << szMethod <<
" return " << iRes <<
" : ";
111 int ReportMethodInt(
int iRes, C* pC,
const char* szClass,
const char* szMethod,
const char* szFile,
const int iLine)
114 ndbout << szFile <<
"(" << iLine <<
") : ";
115 ndbout << szClass <<
"->" << szMethod <<
" return " << iRes <<
" : ";
116 ndbout << pC->getNdbError();
122 template <
class R,
class C>
123 R* ReportMethodPtr(R* pR, C* pC,
const char* szVariable,
const char* szClass,
const char* szMethod,
const char* szFile,
const int iLine)
126 ndbout << szFile <<
"(" << iLine <<
") : ";
127 ndbout << szVariable <<
" = " << szClass <<
"->" << szMethod <<
" return " <<
" : ";
128 ndbout << pC->getNdbError();
135 void ReportMethodVoid(C* pC,
const char* szClass,
const char* szMethod,
const char* szFile,
const int iLine)
137 if(pC->getNdbError().code) {
138 ndbout << szFile <<
"(" << iLine <<
") : ";
139 ndbout << szClass <<
"->" << szMethod <<
" : ";
140 ndbout << pC->getNdbError();
151 #define VerifyMethodInt(c, m) (c->m)
152 #define VerifyMethodPtr(v, c, m) (v=(c->m))
153 #define VerifyMethodVoid(c, m) (c->m)
155 int ReportMethodInt(
int iRes,
NdbConnection* pNdbConnection,
const char* szClass,
const char* szMethod,
const char* szFile,
const int iLine)
158 ndbout << szFile <<
"(" << iLine <<
") : ";
159 ndbout << szClass <<
"->" << szMethod <<
" return " << iRes <<
" : ";
177 #define CHK_TR(p) if(!p){ \
178 ndbout <<"startTransaction failed, returning now." << endl ; \
186 const char* c_szWarehouse =
"WAREHOUSE";
187 const char* c_szWarehouseNumber =
"W_ID";
188 const char* c_szWarehouseSum =
"W_SUM";
189 const char* c_szWarehouseCount =
"W_CNT";
190 const char* c_szDistrict =
"DISTRICT";
191 const char* c_szDistrictWarehouseNumber =
"D_W_ID";
192 const char* c_szDistrictNumber =
"D_ID";
193 const char* c_szDistrictSum =
"D_SUM";
194 const char* c_szDistrictCount =
"D_CNT";
196 Uint32 g_nWarehouseCount = 10;
197 Uint32 g_nDistrictPerWarehouse = 10;
198 Uint32 g_nThreadCount = 1;
199 NdbMutex* g_pNdbMutex = 0;
201 extern "C" void* NdbThreadFuncInsert(
void* pArg)
203 myRandom48Init((
long int)NdbTick_CurrentMillisecond());
207 pNdb =
new Ndb(
"TEST_DB");
208 VerifyMethodInt(pNdb, init());
209 VerifyMethodInt(pNdb, waitUntilReady());
211 while(NdbMutex_Trylock(g_pNdbMutex)) {
212 Uint32 nWarehouse = myRandom48(g_nWarehouseCount);
214 VerifyMethodPtr(pNdbConnection, pNdb, startTransaction());
215 CHK_TR(pNdbConnection);
217 VerifyMethodPtr(pNdbOperationW, pNdbConnection, getNdbOperation(c_szWarehouse));
218 VerifyMethodInt(pNdbOperationW, insertTuple());
219 VerifyMethodInt(pNdbOperationW, equal(c_szWarehouseNumber, nWarehouse));
220 VerifyMethodInt(pNdbOperationW, setValue(c_szWarehouseCount, Uint32(1)));
221 Uint32 nWarehouseSum = 0;
222 for(Uint32 nDistrict=0; nDistrict<g_nDistrictPerWarehouse; ++nDistrict) {
224 VerifyMethodPtr(pNdbOperationD, pNdbConnection, getNdbOperation(c_szDistrict));
225 VerifyMethodInt(pNdbOperationD, insertTuple());
226 VerifyMethodInt(pNdbOperationD, equal(c_szDistrictWarehouseNumber, nWarehouse));
227 VerifyMethodInt(pNdbOperationD, equal(c_szDistrictNumber, nDistrict));
228 VerifyMethodInt(pNdbOperationD, setValue(c_szDistrictCount, Uint32(1)));
229 Uint32 nDistrictSum = myRandom48(100);
230 nWarehouseSum += nDistrictSum;
231 VerifyMethodInt(pNdbOperationD, setValue(c_szDistrictSum, nDistrictSum));
233 VerifyMethodInt(pNdbOperationW, setValue(c_szWarehouseSum, nWarehouseSum));
234 int iExec = pNdbConnection->
execute(Commit);
237 if(iExec<0 && iError!=0 && iError!=266 && iError!=630) {
238 ReportMethodInt(iExec, pNdbConnection,
"pNdbConnection",
"execute(Commit)", __FILE__, __LINE__);
245 VerifyMethodVoid(pNdb, closeTransaction(pNdbConnection));
247 ndbout <<
"insert: " << nSucc <<
" succeeded, " << nFail <<
" failed " << endl;
248 NdbMutex_Unlock(g_pNdbMutex);
255 extern "C" void* NdbThreadFuncUpdate(
void* pArg)
257 myRandom48Init((
long int)NdbTick_CurrentMillisecond());
261 pNdb =
new Ndb(
"TEST_DB");
262 VerifyMethodInt(pNdb, init());
263 VerifyMethodInt(pNdb, waitUntilReady());
265 while(NdbMutex_Trylock(g_pNdbMutex)) {
266 Uint32 nWarehouse = myRandom48(g_nWarehouseCount);
268 VerifyMethodPtr(pNdbConnection, pNdb, startTransaction());
269 CHK_TR(pNdbConnection) ;
271 VerifyMethodPtr(pNdbOperationW, pNdbConnection, getNdbOperation(c_szWarehouse));
272 VerifyMethodInt(pNdbOperationW, interpretedUpdateTuple());
273 VerifyMethodInt(pNdbOperationW, equal(c_szWarehouseNumber, nWarehouse));
274 VerifyMethodInt(pNdbOperationW, incValue(c_szWarehouseCount, Uint32(1)));
275 Uint32 nWarehouseSum = 0;
276 for(Uint32 nDistrict=0; nDistrict<g_nDistrictPerWarehouse; ++nDistrict) {
278 VerifyMethodPtr(pNdbOperationD, pNdbConnection, getNdbOperation(c_szDistrict));
279 VerifyMethodInt(pNdbOperationD, interpretedUpdateTuple());
280 VerifyMethodInt(pNdbOperationD, equal(c_szDistrictWarehouseNumber, nWarehouse));
281 VerifyMethodInt(pNdbOperationD, equal(c_szDistrictNumber, nDistrict));
282 VerifyMethodInt(pNdbOperationD, incValue(c_szDistrictCount, Uint32(1)));
283 Uint32 nDistrictSum = myRandom48(100);
284 nWarehouseSum += nDistrictSum;
285 VerifyMethodInt(pNdbOperationD, setValue(c_szDistrictSum, nDistrictSum));
287 VerifyMethodInt(pNdbOperationW, setValue(c_szWarehouseSum, nWarehouseSum));
288 int iExec = pNdbConnection->
execute(Commit);
291 if(iExec<0 && iError!=0 && iError!=266 && iError!=626) {
292 ReportMethodInt(iExec, pNdbConnection,
"pNdbConnection",
"execute(Commit)", __FILE__, __LINE__);
299 VerifyMethodVoid(pNdb, closeTransaction(pNdbConnection));
301 ndbout <<
"update: " << nSucc <<
" succeeded, " << nFail <<
" failed " << endl;
302 NdbMutex_Unlock(g_pNdbMutex);
309 extern "C" void* NdbThreadFuncDelete(
void* pArg)
311 myRandom48Init((
long int)NdbTick_CurrentMillisecond());
315 pNdb =
new Ndb(
"TEST_DB");
316 VerifyMethodInt(pNdb, init());
317 VerifyMethodInt(pNdb, waitUntilReady());
319 while(NdbMutex_Trylock(g_pNdbMutex)) {
320 Uint32 nWarehouse = myRandom48(g_nWarehouseCount);
322 VerifyMethodPtr(pNdbConnection, pNdb, startTransaction());
323 CHK_TR(pNdbConnection) ;
325 VerifyMethodPtr(pNdbOperationW, pNdbConnection, getNdbOperation(c_szWarehouse));
326 VerifyMethodInt(pNdbOperationW, deleteTuple());
327 VerifyMethodInt(pNdbOperationW, equal(c_szWarehouseNumber, nWarehouse));
328 for(Uint32 nDistrict=0; nDistrict<g_nDistrictPerWarehouse; ++nDistrict) {
330 VerifyMethodPtr(pNdbOperationD, pNdbConnection, getNdbOperation(c_szDistrict));
331 VerifyMethodInt(pNdbOperationD, deleteTuple());
332 VerifyMethodInt(pNdbOperationD, equal(c_szDistrictWarehouseNumber, nWarehouse));
333 VerifyMethodInt(pNdbOperationD, equal(c_szDistrictNumber, nDistrict));
335 int iExec = pNdbConnection->
execute(Commit);
338 if(iExec<0 && iError!=0 && iError!=266 && iError!=626) {
339 ReportMethodInt(iExec, pNdbConnection,
"pNdbConnection",
"execute(Commit)", __FILE__, __LINE__);
346 VerifyMethodVoid(pNdb, closeTransaction(pNdbConnection));
348 ndbout <<
"delete: " << nSucc <<
" succeeded, " << nFail <<
" failed " << endl;
349 NdbMutex_Unlock(g_pNdbMutex);
356 extern "C" void* NdbThreadFuncRead(
void* pArg)
358 myRandom48Init((
long int)NdbTick_CurrentMillisecond());
364 pNdb =
new Ndb(
"TEST_DB");
365 VerifyMethodInt(pNdb, init());
366 VerifyMethodInt(pNdb, waitUntilReady());
368 while(NdbMutex_Trylock(g_pNdbMutex)) {
369 Uint32 nWarehouse = myRandom48(g_nWarehouseCount);
371 VerifyMethodPtr(pNdbConnection, pNdb, startTransaction());
372 CHK_TR(pNdbConnection) ;
374 VerifyMethodPtr(pNdbOperationW, pNdbConnection, getNdbOperation(c_szWarehouse));
375 VerifyMethodInt(pNdbOperationW, readTuple());
376 VerifyMethodInt(pNdbOperationW, equal(c_szWarehouseNumber, nWarehouse));
378 VerifyMethodPtr(pNdbRecAttrWSum, pNdbOperationW, getValue(c_szWarehouseSum, 0));
380 VerifyMethodPtr(pNdbRecAttrWCnt, pNdbOperationW, getValue(c_szWarehouseCount, 0));
381 for(Uint32 nDistrict=0; nDistrict<g_nDistrictPerWarehouse; ++nDistrict) {
383 VerifyMethodPtr(pNdbOperationD, pNdbConnection, getNdbOperation(c_szDistrict));
384 VerifyMethodInt(pNdbOperationD, readTuple());
385 VerifyMethodInt(pNdbOperationD, equal(c_szDistrictWarehouseNumber, nWarehouse));
386 VerifyMethodInt(pNdbOperationD, equal(c_szDistrictNumber, nDistrict));
387 VerifyMethodPtr(ppNdbRecAttrDSum[nDistrict], pNdbOperationD, getValue(c_szDistrictSum, 0));
388 VerifyMethodPtr(ppNdbRecAttrDCnt[nDistrict], pNdbOperationD, getValue(c_szDistrictCount, 0));
390 int iExec = pNdbConnection->
execute(Commit);
393 if(iExec<0 && iError!=0 && iError!=266 && iError!=626) {
394 ReportMethodInt(iExec, pNdbConnection,
"pNdbConnection",
"execute(Commit)", __FILE__, __LINE__);
399 for(Uint32 nDistrict=0; nDistrict<g_nDistrictPerWarehouse; ++nDistrict) {
400 nSum += ppNdbRecAttrDSum[nDistrict]->
u_32_value();
401 nCnt += ppNdbRecAttrDCnt[nDistrict]->
u_32_value();
404 || nCnt!=g_nDistrictPerWarehouse*pNdbRecAttrWCnt->
u_32_value()) {
405 ndbout <<
"INCONSISTENT!" << endl;
406 ndbout <<
"iExec==" << iExec << endl;
407 ndbout <<
"iError==" << iError << endl;
409 ndbout << c_szWarehouseSum <<
"==" << pNdbRecAttrWSum->
u_32_value() <<
", ";
410 ndbout << c_szWarehouseCount <<
"==" << pNdbRecAttrWCnt->
u_32_value() << endl;
411 ndbout <<
"nSum==" << nSum <<
", nCnt=" << nCnt << endl;
412 for(Uint32 nDistrict=0; nDistrict<g_nDistrictPerWarehouse; ++nDistrict) {
413 ndbout << c_szDistrictSum <<
"[" << nDistrict <<
"]==" << ppNdbRecAttrDSum[nDistrict]->
u_32_value() <<
", ";
414 ndbout << c_szDistrictCount <<
"[" << nDistrict <<
"]==" << ppNdbRecAttrDCnt[nDistrict]->
u_32_value() << endl;
416 VerifyMethodVoid(pNdb, closeTransaction(pNdbConnection));
417 delete pNdb; pNdb = NULL ;
418 delete[] ppNdbRecAttrDSum; ppNdbRecAttrDSum = NULL ;
419 delete[] ppNdbRecAttrDCnt; ppNdbRecAttrDCnt = NULL ;
420 NDBT_ProgramExit(NDBT_FAILED);
426 VerifyMethodVoid(pNdb, closeTransaction(pNdbConnection));
428 ndbout <<
"read: " << nSucc <<
" succeeded, " << nFail <<
" failed " << endl;
429 NdbMutex_Unlock(g_pNdbMutex);
430 delete pNdb; pNdb = NULL ;
431 delete[] ppNdbRecAttrDSum; ppNdbRecAttrDSum = NULL ;
432 delete[] ppNdbRecAttrDCnt; ppNdbRecAttrDCnt = NULL ;
437 NDB_COMMAND(acid,
"acid",
"acid",
"acid", 65535)
443 for(
int i=1;
i<argc; ++
i) {
444 if(argv[
i][0]==
'-' || argv[
i][0]==
'/') {
446 case 'w': g_nWarehouseCount=atol(argv[i]+2);
break;
447 case 'd': g_nDistrictPerWarehouse=atol(argv[i]+2);
break;
448 case 's': nSeconds=atol(argv[i]+2);
break;
449 case 't': g_nThreadCount=atol(argv[i]+2);
break;
450 default: ndbout <<
"invalid option" << endl;
return 1;
453 ndbout <<
"invalid operand" << endl;
458 ndbout <<
" -w" << g_nWarehouseCount;
459 ndbout <<
" -d" << g_nDistrictPerWarehouse;
460 ndbout <<
" -s" << (int)nSeconds;
461 ndbout <<
" -t" << g_nThreadCount;
465 pNdb =
new Ndb(
"TEST_DB");
466 VerifyMethodInt(pNdb, init());
467 VerifyMethodInt(pNdb, waitUntilReady());
469 NdbSchemaCon* pNdbSchemaCon= NdbSchemaCon::startSchemaTrans(pNdb);
471 ndbout <<
"startSchemaTransaction failed, exiting now" << endl ;
473 NDBT_ProgramExit(NDBT_FAILED) ;
476 VerifyMethodPtr(pNdbSchemaOp, pNdbSchemaCon, getNdbSchemaOp());
477 VerifyMethodInt(pNdbSchemaOp, createTable(
479 (4+4+4+12)*1.02*g_nWarehouseCount/1024+1,
481 (4+14)*g_nWarehouseCount/8/1024+1));
483 VerifyMethodInt(pNdbSchemaOp, createAttribute(c_szWarehouseNumber, TupleKey, 32, 1, UnSigned, MMBased,
false));
484 VerifyMethodInt(pNdbSchemaOp, createAttribute(c_szWarehouseSum, NoKey, 32, 1, UnSigned, MMBased,
false));
485 VerifyMethodInt(pNdbSchemaOp, createAttribute(c_szWarehouseCount, NoKey, 32, 1, UnSigned, MMBased,
false));
486 VerifyMethodInt(pNdbSchemaCon, execute());
487 NdbSchemaCon::closeSchemaTrans(pNdbSchemaCon);
489 pNdbSchemaCon= NdbSchemaCon::startSchemaTrans(pNdb);
490 VerifyMethodPtr(pNdbSchemaOp, pNdbSchemaCon, getNdbSchemaOp());
491 VerifyMethodInt(pNdbSchemaOp, createTable(
493 (4+4+4+4+12)*1.02*g_nWarehouseCount*g_nDistrictPerWarehouse/1024+1,
495 (4+4+14)*g_nWarehouseCount*g_nDistrictPerWarehouse/8/1024+1));
498 VerifyMethodInt(pNdbSchemaOp, createAttribute(c_szDistrictWarehouseNumber, TupleKey, 32, 1, UnSigned, MMBased,
false));
499 VerifyMethodInt(pNdbSchemaOp, createAttribute(c_szDistrictNumber, TupleKey, 32, 1, UnSigned, MMBased,
false));
500 VerifyMethodInt(pNdbSchemaOp, createAttribute(c_szDistrictSum, NoKey, 32, 1, UnSigned, MMBased,
false));
501 VerifyMethodInt(pNdbSchemaOp, createAttribute(c_szDistrictCount, NoKey, 32, 1, UnSigned, MMBased,
false));
502 VerifyMethodInt(pNdbSchemaCon, execute());
503 NdbSchemaCon::closeSchemaTrans(pNdbSchemaCon);
504 g_pNdbMutex = NdbMutex_Create();
505 NdbMutex_Lock(g_pNdbMutex);
508 for(Uint32 nThread=0; nThread<g_nThreadCount; ++nThread) {
509 ppNdbThread[nThread*4+0] = NdbThread_Create(NdbThreadFuncInsert, 0, 65535,
"insert",
510 NDB_THREAD_PRIO_LOW);
511 ppNdbThread[nThread*4+1] = NdbThread_Create(NdbThreadFuncUpdate, 0, 65535,
"update",
512 NDB_THREAD_PRIO_LOW);
513 ppNdbThread[nThread*4+2] = NdbThread_Create(NdbThreadFuncDelete, 0, 65535,
"delete",
514 NDB_THREAD_PRIO_LOW);
515 ppNdbThread[nThread*4+3] = NdbThread_Create(NdbThreadFuncRead, 0, 65535,
"read",
516 NDB_THREAD_PRIO_LOW);
519 NdbSleep_SecSleep(nSeconds);
520 NdbMutex_Unlock(g_pNdbMutex);
523 for(Uint32 nThread=0; nThread<g_nThreadCount; ++nThread) {
524 NdbThread_WaitFor(ppNdbThread[nThread*4+0], &pStatus);
525 NdbThread_WaitFor(ppNdbThread[nThread*4+1], &pStatus);
526 NdbThread_WaitFor(ppNdbThread[nThread*4+2], &pStatus);
527 NdbThread_WaitFor(ppNdbThread[nThread*4+3], &pStatus);
530 NdbMutex_Destroy(g_pNdbMutex);
531 delete[] ppNdbThread;
533 return NDBT_ProgramExit(rc);