1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
|
/* 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; version 2 of the License.
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 <ndb_global.h>
#include <NdbApi.hpp>
#include <NdbSchemaCon.hpp>
#include <NdbCondition.h>
#include <NdbMutex.h>
#include <NdbSleep.h>
#include <NdbThread.h>
#include <NdbTick.h>
const char* const c_szDatabaseName = "TEST_DB";
const char* const c_szTableNameStored = "CCStored";
const char* const c_szTableNameTemp = "CCTemp";
const char* const c_szContextId = "ContextId";
const char* const c_szVersion = "Version";
const char* const c_szLockFlag = "LockFlag";
const char* const c_szLockTime = "LockTime";
const char* const c_szLockTimeUSec = "LockTimeUSec";
const char* const c_szContextData = "ContextData";
const char* g_szTableName = c_szTableNameStored;
#ifdef NDB_WIN32
HANDLE hShutdownEvent = 0;
#else
#include <signal.h>
bool bShutdownEvent = false;
#endif
long g_nMaxContextIdPerThread = 5000;
long g_nNumThreads = 0;
long g_nMaxCallsPerSecond = 0;
long g_nMaxRetry = 50;
bool g_bWriteTuple = false;
bool g_bInsertInitial = false;
bool g_bVerifyInitial = false;
NdbMutex* g_pNdbMutexPrintf = 0;
NdbMutex* g_pNdbMutexIncrement = 0;
long g_nNumCallsProcessed = 0;
NDB_TICKS g_tStartTime = 0;
NDB_TICKS g_tEndTime = 0;
long g_nNumberOfInitialInsert = 0;
long g_nNumberOfInitialVerify = 0;
const long c_nMaxMillisecForAllCall = 5000;
long* g_plCountMillisecForCall = 0;
const long c_nMaxMillisecForAllTrans = 5000;
long* g_plCountMillisecForTrans = 0;
bool g_bReport = false;
bool g_bReportPlus = false;
// data for CALL_CONTEXT and GROUP_RESOURCE
static char STATUS_DATA[]=
"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"
"101112131415161718191A1B1C1D1E1F000102030405060708090A0B0C0D0E0F"
"202122232425262728292A2B2C2D2E2F000102030405060708090A0B0C0D0E0F"
"303132333435363738393A3B3C3D3E3F000102030405060708090A0B0C0D0E0F"
"404142434445464748494A4B4C4D4E4F000102030405060708090A0B0C0D0E0F"
"505152535455565758595A5B5C5D5E5F000102030405060708090A0B0C0D0E0F"
"606162636465666768696A6B6C6D6E6F000102030405060708090A0B0C0D0E0F"
"707172737475767778797A7B7C7D7E7F000102030405060708090A0B0C0D0E0F"
"808182838485868788898A8B8C8D8E8F000102030405060708090A0B0C0D0E0F"
"909192939495969798999A9B9C9D9E9F000102030405060708090A0B0C0D0E0F"
"10010110210310410510610710810910A000102030405060708090A0B0C0D0EF"
"10B10C10D10E10F110111112113114115000102030405060708090A0B0C0D0EF"
"11611711811911A11B11C11D11E11F120000102030405060708090A0B0C0D0EF"
"12112212312412512612712812912A12B000102030405060708090A0B0C0D0EF"
"12C12D12E12F130131132134135136137000102030405060708090A0B0C0D0EF"
"13813913A13B13C13D13E13F140141142000102030405060708090A0B0C0D0EF"
"14314414514614714814914A14B14C14D000102030405060708090A0B0C0D0EF"
"14E14F150151152153154155156157158000102030405060708090A0B0C0D0EF"
"15915A15B15C15D15E15F160161162163000102030405060708090A0B0C0D0EF"
"16416516616716816916A16B16C16D16E000102030405060708090A0B0C0D0EF"
"16F170171172173174175176177178179000102030405060708090A0B0C0D0EF"
"17A17B17C17D17E17F180181182183184000102030405060708090A0B0C0D0EF"
"18518618718818918A18B18C18D18E18F000102030405060708090A0B0C0D0EF"
"19019119219319419519619719819919A000102030405060708090A0B0C0D0EF"
"19B19C19D19E19F200201202203204205000102030405060708090A0B0C0D0EF"
"20620720820920A20B20C20D20F210211000102030405060708090A0B0C0D0EF"
"21221321421521621721821921A21B21C000102030405060708090A0B0C0D0EF"
"21D21E21F220221222223224225226227000102030405060708090A0B0C0D0EF"
"22822922A22B22C22D22E22F230231232000102030405060708090A0B0C0D0EF"
"23323423523623723823923A23B23C23D000102030405060708090A0B0C0D0EF"
"23E23F240241242243244245246247248000102030405060708090A0B0C0D0EF"
"24924A24B24C24D24E24F250251252253000102030405060708090A0B0C0D0EF"
"101112131415161718191A1B1C1D1E1F000102030405060708090A0B0C0D0E0F"
"202122232425262728292A2B2C2D2E2F000102030405060708090A0B0C0D0E0F"
"303132333435363738393A3B3C3D3E3F000102030405060708090A0B0C0D0E0F"
"404142434445464748494A4B4C4D4E4F000102030405060708090A0B0C0D0E0F"
"505152535455565758595A5B5C5D5E5F000102030405060708090A0B0C0D0E0F"
"606162636465666768696A6B6C6D6E6F000102030405060708090A0B0C0D0E0F"
"707172737475767778797A7B7C7D7E7F000102030405060708090A0B0C0D0E0F"
"808182838485868788898A8B8C8D8E8F000102030405060708090A0B0C0D0E0F"
"909192939495969798999A9B9C9D9E9F000102030405060708090A0B0C0D0E0F"
"10010110210310410510610710810910A000102030405060708090A0B0C0D0EF"
"10B10C10D10E10F110111112113114115000102030405060708090A0B0C0D0EF"
"11611711811911A11B11C11D11E11F120000102030405060708090A0B0C0D0EF"
"12112212312412512612712812912A12B000102030405060708090A0B0C0D0EF"
"12C12D12E12F130131132134135136137000102030405060708090A0B0C0D0EF"
"13813913A13B13C13D13E13F140141142000102030405060708090A0B0C0D0EF"
"14314414514614714814914A14B14C14D000102030405060708090A0B0C0D0EF"
"14E14F150151152153154155156157158000102030405060708090A0B0C0D0EF"
"15915A15B15C15D15E15F160161162163000102030405060708090A0B0C0D0EF"
"16416516616716816916A16B16C16D16E000102030405060708090A0B0C0D0EF"
"16F170171172173174175176177178179000102030405060708090A0B0C0D0EF"
"17A17B17C17D17E17F180181182183184000102030405060708090A0B0C0D0EF"
"18518618718818918A18B18C18D18E18F000102030405060708090A0B0C0D0EF"
"19019119219319419519619719819919A000102030405060708090A0B0C0D0EF"
"19B19C19D19E19F200201202203204205000102030405060708090A0B0C0D0EF"
"20620720820920A20B20C20D20F210211000102030405060708090A0B0C0D0EF"
"21221321421521621721821921A21B21C000102030405060708090A0B0C0D0EF"
"21D21E21F220221222223224225226227000102030405060708090A0B0C0D0EF"
"22822922A22B22C22D22E22F230231232000102030405060708090A0B0C0D0EF"
"23323423523623723823923A23B23C23D000102030405060708090A0B0C0D0EF"
"2366890FE1438751097E7F6325DC0E6326F"
"25425525625725825925A25B25C25D25E25F000102030405060708090A0B0C0F";
long g_nStatusDataSize = sizeof(STATUS_DATA);
// Thread function for Call Context Inserts
#ifdef NDB_WIN32
BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType)
{
if(CTRL_C_EVENT == dwCtrlType)
{
SetEvent(hShutdownEvent);
return TRUE;
}
return FALSE;
}
#else
void CtrlCHandler(int)
{
bShutdownEvent = true;
}
#endif
void ReportNdbError(const char* szMsg, const NdbError& err)
{
NdbMutex_Lock(g_pNdbMutexPrintf);
printf("%s: %d: %s\n", szMsg, err.code, (err.message ? err.message : ""));
NdbMutex_Unlock(g_pNdbMutexPrintf);
}
void
ReportCallsPerSecond(long nNumCallsProcessed,
NDB_TICKS tStartTime,
NDB_TICKS tEndTime)
{
NDB_TICKS tElapsed = tEndTime - tStartTime;
long lCallsPerSec;
if(tElapsed>0)
lCallsPerSec = (long)((1000*nNumCallsProcessed)/tElapsed);
else
lCallsPerSec = 0;
NdbMutex_Lock(g_pNdbMutexPrintf);
printf("Time Taken for %ld Calls is %ld msec (= %ld calls/sec)\n",
nNumCallsProcessed, (long)tElapsed, lCallsPerSec);
NdbMutex_Unlock(g_pNdbMutexPrintf);
}
#ifndef NDB_WIN32
void InterlockedIncrement(long* lp) // expensive
{
NdbMutex_Lock(g_pNdbMutexIncrement);
(*lp)++;
NdbMutex_Unlock(g_pNdbMutexIncrement);
}
#endif
void InterlockedIncrementAndReport(void)
{
NdbMutex_Lock(g_pNdbMutexIncrement);
++g_nNumCallsProcessed;
if((g_nNumCallsProcessed%1000)==0)
{
g_tEndTime = NdbTick_CurrentMillisecond();
if(g_tStartTime)
ReportCallsPerSecond(1000, g_tStartTime, g_tEndTime);
g_tStartTime = g_tEndTime;
}
NdbMutex_Unlock(g_pNdbMutexIncrement);
}
void SleepOneCall(void)
{
int iMillisecToSleep;
if(g_nMaxCallsPerSecond>0)
iMillisecToSleep = (1000*g_nNumThreads)/g_nMaxCallsPerSecond;
else
iMillisecToSleep = 50;
if(iMillisecToSleep>0)
NdbSleep_MilliSleep(iMillisecToSleep);
}
int QueryTransaction(Ndb* pNdb,
long iContextId,
long* piVersion,
long* piLockFlag,
long* piLockTime,
long* piLockTimeUSec,
char* pchContextData,
NdbError& err)
{
int iRes = -1;
NdbConnection* pNdbConnection = pNdb->startTransaction(0, (const char*)&iContextId, 4);
if(pNdbConnection)
{
NdbOperation* pNdbOperation = pNdbConnection->getNdbOperation(g_szTableName);
if(pNdbOperation)
{
NdbRecAttr* pNdbRecAttrVersion;
NdbRecAttr* pNdbRecAttrLockFlag;
NdbRecAttr* pNdbRecAttrLockTime;
NdbRecAttr* pNdbRecAttrLockTimeUSec;
NdbRecAttr* pNdbRecAttrContextData;
if(!pNdbOperation->readTuple()
&& !pNdbOperation->equal(c_szContextId, (Int32)iContextId)
&& (pNdbRecAttrVersion=pNdbOperation->getValue(c_szVersion, (char*)piVersion))
&& (pNdbRecAttrLockFlag=pNdbOperation->getValue(c_szLockFlag, (char*)piLockFlag))
&& (pNdbRecAttrLockTime=pNdbOperation->getValue(c_szLockTime, (char*)piLockTime))
&& (pNdbRecAttrLockTimeUSec=pNdbOperation->getValue(c_szLockTimeUSec, (char*)piLockTimeUSec))
&& (pNdbRecAttrContextData=pNdbOperation->getValue(c_szContextData, pchContextData)))
{
if(!pNdbConnection->execute(Commit))
iRes = 0;
else
err = pNdbConnection->getNdbError();
}
else
err = pNdbOperation->getNdbError();
}
else
err = pNdbConnection->getNdbError();
pNdb->closeTransaction(pNdbConnection);
}
else
err = pNdb->getNdbError();
return iRes;
}
int RetryQueryTransaction(Ndb* pNdb,
long iContextId,
long* piVersion,
long* piLockFlag,
long* piLockTime,
long* piLockTimeUSec,
char* pchContextData,
NdbError& err,
int& nRetry)
{
int iRes = -1;
nRetry = 0;
bool bRetry = true;
while(bRetry && nRetry<g_nMaxRetry)
{
if(!QueryTransaction(pNdb, iContextId, piVersion, piLockFlag,
piLockTime, piLockTimeUSec, pchContextData, err))
{
iRes = 0;
bRetry = false;
}
else
{
switch(err.status)
{
case NdbError::TemporaryError:
case NdbError::UnknownResult:
SleepOneCall();
++nRetry;
break;
case NdbError::PermanentError:
default:
bRetry = false;
break;
}
}
}
return iRes;
}
int DeleteTransaction(Ndb* pNdb, long iContextId, NdbError& err)
{
int iRes = -1;
NdbConnection* pNdbConnection = pNdb->startTransaction(0, (const char*)&iContextId, 4);
if(pNdbConnection)
{
NdbOperation* pNdbOperation = pNdbConnection->getNdbOperation(g_szTableName);
if(pNdbOperation)
{
if(!pNdbOperation->deleteTuple()
&& !pNdbOperation->equal(c_szContextId, (Int32)iContextId))
{
if(pNdbConnection->execute(Commit) == 0)
iRes = 0;
else
err = pNdbConnection->getNdbError();
}
else
err = pNdbOperation->getNdbError();
}
else
err = pNdbConnection->getNdbError();
pNdb->closeTransaction(pNdbConnection);
}
else
err = pNdb->getNdbError();
return iRes;
}
int RetryDeleteTransaction(Ndb* pNdb, long iContextId, NdbError& err, int& nRetry)
{
int iRes = -1;
nRetry = 0;
bool bRetry = true;
bool bUnknown = false;
while(bRetry && nRetry<g_nMaxRetry)
{
if(!DeleteTransaction(pNdb, iContextId, err))
{
iRes = 0;
bRetry = false;
}
else
{
switch(err.status)
{
case NdbError::UnknownResult:
bUnknown = true;
++nRetry;
break;
case NdbError::TemporaryError:
bUnknown = false;
SleepOneCall();
++nRetry;
break;
case NdbError::PermanentError:
if(err.code==626 && bUnknown)
iRes = 0;
bRetry = false;
break;
default:
bRetry = false;
break;
}
}
}
return iRes;
}
int InsertTransaction(Ndb* pNdb,
long iContextID,
long iVersion,
long iLockFlag,
long iLockTime,
long iLockTimeUSec,
const char* pchContextData,
NdbError& err)
{
int iRes = -1;
NdbConnection* pNdbConnection = pNdb->startTransaction(0, (const char*)&iContextID, 4);
if(pNdbConnection)
{
NdbOperation* pNdbOperation = pNdbConnection->getNdbOperation(g_szTableName);
if(pNdbOperation)
{
if(!(g_bWriteTuple ? pNdbOperation->writeTuple() : pNdbOperation->insertTuple())
&& !pNdbOperation->equal(c_szContextId, (Int32)iContextID)
&& !pNdbOperation->setValue(c_szVersion, (Int32)iVersion)
&& !pNdbOperation->setValue(c_szLockFlag, (Int32)iLockFlag)
&& !pNdbOperation->setValue(c_szLockTime, (Int32)iLockTime)
&& !pNdbOperation->setValue(c_szLockTimeUSec, (Int32)iLockTimeUSec)
&& !pNdbOperation->setValue(c_szContextData, pchContextData, g_nStatusDataSize))
{
if(!pNdbConnection->execute(Commit))
iRes = 0;
else
err = pNdbConnection->getNdbError();
}
else
err = pNdbOperation->getNdbError();
}
else
err = pNdbConnection->getNdbError();
pNdb->closeTransaction(pNdbConnection);
}
else
err = pNdb->getNdbError();
return iRes;
}
int RetryInsertTransaction(Ndb* pNdb,
long iContextId,
long iVersion,
long iLockFlag,
long iLockTime,
long iLockTimeUSec,
const char* pchContextData,
NdbError& err, int& nRetry)
{
int iRes = -1;
nRetry = 0;
bool bRetry = true;
bool bUnknown = false;
while(bRetry && nRetry<g_nMaxRetry)
{
if(!InsertTransaction(pNdb, iContextId, iVersion, iLockFlag,
iLockTime, iLockTimeUSec, pchContextData, err))
{
iRes = 0;
bRetry = false;
}
else
{
switch(err.status)
{
case NdbError::UnknownResult:
bUnknown = true;
++nRetry;
break;
case NdbError::TemporaryError:
bUnknown = false;
SleepOneCall();
++nRetry;
break;
case NdbError::PermanentError:
if(err.code==630 && bUnknown)
iRes = 0;
bRetry = false;
break;
default:
bRetry = false;
break;
}
}
}
return iRes;
}
int UpdateTransaction(Ndb* pNdb, long iContextId, NdbError& err)
{
int iRes = -1;
NdbConnection* pNdbConnection = pNdb->startTransaction(0, (const char*)&iContextId, 4);
if(pNdbConnection)
{
NdbOperation* pNdbOperation = pNdbConnection->getNdbOperation(g_szTableName);
if(pNdbOperation)
{
if(!pNdbOperation->updateTuple()
&& !pNdbOperation->equal(c_szContextId, (Int32)iContextId)
&& !pNdbOperation->setValue(c_szContextData, STATUS_DATA, g_nStatusDataSize))
{
if(!pNdbConnection->execute(Commit))
iRes = 0;
else
err = pNdbConnection->getNdbError();
}
else
err = pNdbOperation->getNdbError();
}
else
err = pNdbConnection->getNdbError();
pNdb->closeTransaction(pNdbConnection);
}
else
err = pNdb->getNdbError();
return iRes;
}
int RetryUpdateTransaction(Ndb* pNdb, long iContextId, NdbError& err, int& nRetry)
{
int iRes = -1;
nRetry = 0;
bool bRetry = true;
while(bRetry && nRetry<g_nMaxRetry)
{
if(!UpdateTransaction(pNdb, iContextId, err))
{
iRes = 0;
bRetry = false;
}
else
{
switch(err.status)
{
case NdbError::TemporaryError:
case NdbError::UnknownResult:
SleepOneCall();
++nRetry;
break;
case NdbError::PermanentError:
default:
bRetry = false;
break;
}
}
}
return iRes;
}
int InsertInitialRecords(Ndb* pNdb, long nInsert, long nSeed)
{
int iRes = -1;
char szMsg[100];
for(long i=0; i<nInsert; ++i)
{
int iContextID = i+nSeed;
int nRetry = 0;
NdbError err;
memset(&err, 0, sizeof(err));
NDB_TICKS tStartTrans = NdbTick_CurrentMillisecond();
iRes = RetryInsertTransaction(pNdb, iContextID, nSeed, iContextID,
(long)(tStartTrans/1000), (long)((tStartTrans%1000)*1000),
STATUS_DATA, err, nRetry);
NDB_TICKS tEndTrans = NdbTick_CurrentMillisecond();
long lMillisecForThisTrans = (long)(tEndTrans-tStartTrans);
if(nRetry>0)
{
sprintf(szMsg, "insert retried %d times, time %ld msec.",
nRetry, lMillisecForThisTrans);
ReportNdbError(szMsg, err);
}
if(iRes)
{
ReportNdbError("Insert initial record failed", err);
return iRes;
}
InterlockedIncrement(&g_nNumberOfInitialInsert);
}
return iRes;
}
int VerifyInitialRecords(Ndb* pNdb, long nVerify, long nSeed)
{
int iRes = -1;
char* pchContextData = new char[g_nStatusDataSize];
char szMsg[100];
long iPrevLockTime = -1;
long iPrevLockTimeUSec = -1;
for(long i=0; i<nVerify; ++i)
{
int iContextID = i+nSeed;
long iVersion = 0;
long iLockFlag = 0;
long iLockTime = 0;
long iLockTimeUSec = 0;
int nRetry = 0;
NdbError err;
memset(&err, 0, sizeof(err));
NDB_TICKS tStartTrans = NdbTick_CurrentMillisecond();
iRes = RetryQueryTransaction(pNdb, iContextID, &iVersion, &iLockFlag,
&iLockTime, &iLockTimeUSec, pchContextData, err, nRetry);
NDB_TICKS tEndTrans = NdbTick_CurrentMillisecond();
long lMillisecForThisTrans = (long)(tEndTrans-tStartTrans);
if(nRetry>0)
{
sprintf(szMsg, "verify retried %d times, time %ld msec.",
nRetry, lMillisecForThisTrans);
ReportNdbError(szMsg, err);
}
if(iRes)
{
ReportNdbError("Read initial record failed", err);
delete[] pchContextData;
return iRes;
}
if(memcmp(pchContextData, STATUS_DATA, g_nStatusDataSize))
{
sprintf(szMsg, "wrong context data in tuple %d", iContextID);
ReportNdbError(szMsg, err);
delete[] pchContextData;
return -1;
}
if(iVersion!=nSeed
|| iLockFlag!=iContextID
|| iLockTime<iPrevLockTime
|| (iLockTime==iPrevLockTime && iLockTimeUSec<iPrevLockTimeUSec))
{
sprintf(szMsg, "wrong call data in tuple %d", iContextID);
ReportNdbError(szMsg, err);
delete[] pchContextData;
return -1;
}
iPrevLockTime = iLockTime;
iPrevLockTimeUSec = iLockTimeUSec;
InterlockedIncrement(&g_nNumberOfInitialVerify);
}
delete[] pchContextData;
return iRes;
}
void* RuntimeCallContext(void* lpParam)
{
long nNumCallsProcessed = 0;
int nStartingRecordID = *(int*)lpParam;
Ndb* pNdb;
char* pchContextData = new char[g_nStatusDataSize];
char szMsg[100];
int iRes;
const char* szOp;
long iVersion;
long iLockFlag;
long iLockTime;
long iLockTimeUSec;
pNdb = new Ndb("TEST_DB");
if(!pNdb)
{
NdbMutex_Lock(g_pNdbMutexPrintf);
printf("new Ndb failed\n");
NdbMutex_Unlock(g_pNdbMutexPrintf);
delete[] pchContextData;
return 0;
}
if(pNdb->init(1) || pNdb->waitUntilReady())
{
ReportNdbError("init of Ndb failed", pNdb->getNdbError());
delete pNdb;
delete[] pchContextData;
return 0;
}
if(g_bInsertInitial)
{
if(InsertInitialRecords(pNdb, g_nMaxContextIdPerThread, -nStartingRecordID-g_nMaxContextIdPerThread))
{
delete pNdb;
delete[] pchContextData;
return 0;
}
}
if(g_bVerifyInitial)
{
NdbError err;
memset(&err, 0, sizeof(err));
if(VerifyInitialRecords(pNdb, g_nMaxContextIdPerThread, -nStartingRecordID-g_nMaxContextIdPerThread))
{
delete pNdb;
delete[] pchContextData;
return 0;
}
}
if(g_bInsertInitial || g_bVerifyInitial)
{
delete[] pchContextData;
return 0;
}
long nContextID = nStartingRecordID;
#ifdef NDB_WIN32
while(WaitForSingleObject(hShutdownEvent,0) != WAIT_OBJECT_0)
#else
while(!bShutdownEvent)
#endif
{
++nContextID;
nContextID %= g_nMaxContextIdPerThread;
nContextID += nStartingRecordID;
bool bTimeLatency = (nContextID==100);
NDB_TICKS tStartCall = NdbTick_CurrentMillisecond();
for (int i=0; i < 20; i++)
{
int nRetry = 0;
NdbError err;
memset(&err, 0, sizeof(err));
NDB_TICKS tStartTrans = NdbTick_CurrentMillisecond();
switch(i)
{
case 3:
case 6:
case 9:
case 11:
case 12:
case 15:
case 18: // Query Record
szOp = "Read";
iRes = RetryQueryTransaction(pNdb, nContextID, &iVersion, &iLockFlag,
&iLockTime, &iLockTimeUSec, pchContextData, err, nRetry);
break;
case 19: // Delete Record
szOp = "Delete";
iRes = RetryDeleteTransaction(pNdb, nContextID, err, nRetry);
break;
case 0: // Insert Record
szOp = "Insert";
iRes = RetryInsertTransaction(pNdb, nContextID, 1, 1, 1, 1, STATUS_DATA, err, nRetry);
break;
default: // Update Record
szOp = "Update";
iRes = RetryUpdateTransaction(pNdb, nContextID, err, nRetry);
break;
}
NDB_TICKS tEndTrans = NdbTick_CurrentMillisecond();
long lMillisecForThisTrans = (long)(tEndTrans-tStartTrans);
if(g_bReport)
{
assert(lMillisecForThisTrans>=0 && lMillisecForThisTrans<c_nMaxMillisecForAllTrans);
InterlockedIncrement(g_plCountMillisecForTrans+lMillisecForThisTrans);
}
if(nRetry>0)
{
sprintf(szMsg, "%s retried %d times, time %ld msec.",
szOp, nRetry, lMillisecForThisTrans);
ReportNdbError(szMsg, err);
}
else if(bTimeLatency)
{
NdbMutex_Lock(g_pNdbMutexPrintf);
printf("%s = %ld msec.\n", szOp, lMillisecForThisTrans);
NdbMutex_Unlock(g_pNdbMutexPrintf);
}
if(iRes)
{
sprintf(szMsg, "%s failed after %ld calls, terminating thread",
szOp, nNumCallsProcessed);
ReportNdbError(szMsg, err);
delete pNdb;
delete[] pchContextData;
return 0;
}
}
NDB_TICKS tEndCall = NdbTick_CurrentMillisecond();
long lMillisecForThisCall = (long)(tEndCall-tStartCall);
if(g_bReport)
{
assert(lMillisecForThisCall>=0 && lMillisecForThisCall<c_nMaxMillisecForAllCall);
InterlockedIncrement(g_plCountMillisecForCall+lMillisecForThisCall);
}
if(bTimeLatency)
{
NdbMutex_Lock(g_pNdbMutexPrintf);
printf("Total time for call is %ld msec.\n", (long)lMillisecForThisCall);
NdbMutex_Unlock(g_pNdbMutexPrintf);
}
nNumCallsProcessed++;
InterlockedIncrementAndReport();
if(g_nMaxCallsPerSecond>0)
{
int iMillisecToSleep = (1000*g_nNumThreads)/g_nMaxCallsPerSecond;
iMillisecToSleep -= lMillisecForThisCall;
if(iMillisecToSleep>0)
{
NdbSleep_MilliSleep(iMillisecToSleep);
}
}
}
NdbMutex_Lock(g_pNdbMutexPrintf);
printf("Terminating thread after %ld calls\n", nNumCallsProcessed);
NdbMutex_Unlock(g_pNdbMutexPrintf);
delete pNdb;
delete[] pchContextData;
return 0;
}
int CreateCallContextTable(Ndb* pNdb, const char* szTableName, bool bStored)
{
int iRes = -1;
NdbError err;
memset(&err, 0, sizeof(err));
NdbSchemaCon* pNdbSchemaCon = NdbSchemaCon::startSchemaTrans(pNdb);
if(pNdbSchemaCon)
{
NdbSchemaOp* pNdbSchemaOp = pNdbSchemaCon->getNdbSchemaOp();
if(pNdbSchemaOp)
{
if(!pNdbSchemaOp->createTable(szTableName, 8, TupleKey, 2,
All, 6, 78, 80, 1, bStored)
&& !pNdbSchemaOp->createAttribute(c_szContextId, TupleKey, 32, 1, Signed)
&& !pNdbSchemaOp->createAttribute(c_szVersion, NoKey, 32, 1, Signed)
&& !pNdbSchemaOp->createAttribute(c_szLockFlag, NoKey, 32, 1, Signed)
&& !pNdbSchemaOp->createAttribute(c_szLockTime, NoKey, 32, 1, Signed)
&& !pNdbSchemaOp->createAttribute(c_szLockTimeUSec, NoKey, 32, 1, Signed)
&& !pNdbSchemaOp->createAttribute(c_szContextData, NoKey, 8, g_nStatusDataSize, String))
{
if(!pNdbSchemaCon->execute())
iRes = 0;
else
err = pNdbSchemaCon->getNdbError();
}
else
err = pNdbSchemaOp->getNdbError();
}
else
err = pNdbSchemaCon->getNdbError();
NdbSchemaCon::closeSchemaTrans(pNdbSchemaCon);
}
else
err = pNdb->getNdbError();
if(iRes)
{
ReportNdbError("create call context table failed", err);
}
return iRes;
}
void ReportResponseTimeStatistics(const char* szStat, long* plCount, const long lSize)
{
long lCount = 0;
Int64 llSum = 0;
Int64 llSum2 = 0;
long lMin = -1;
long lMax = -1;
for(long l=0; l<lSize; ++l)
{
if(plCount[l]>0)
{
lCount += plCount[l];
llSum += (Int64)l*(Int64)plCount[l];
llSum2 += (Int64)l*(Int64)l*(Int64)plCount[l];
if(lMin==-1 || l<lMin)
{
lMin = l;
}
if(lMax==-1 || l>lMax)
{
lMax = l;
}
}
}
long lAvg = long(llSum/lCount);
double dblVar = ((double)lCount*(double)llSum2 - (double)llSum*(double)llSum)/((double)lCount*(double)(lCount-1));
long lStd = long(sqrt(dblVar));
long lMed = -1;
long l95 = -1;
long lSel = -1;
for(long l=lMin; l<=lMax; ++l)
{
if(plCount[l]>0)
{
lSel += plCount[l];
if(lMed==-1 && lSel>=(lCount/2))
{
lMed = l;
}
if(l95==-1 && lSel>=((lCount*95)/100))
{
l95 = l;
}
if(g_bReportPlus)
{
printf("%ld\t%ld\n", l, plCount[l]);
}
}
}
printf("%s: Count=%ld, Min=%ld, Max=%ld, Avg=%ld, Std=%ld, Med=%ld, 95%%=%ld\n",
szStat, lCount, lMin, lMax, lAvg, lStd, lMed, l95);
}
void ShowHelp(const char* szCmd)
{
printf("%s -t<threads> [-s<seed>] [-b<batch>] [-c<maxcps>] [-m<size>] [-d] [-i] [-v] [-f] [-w] [-r[+]]\n", szCmd);
printf("%s -?\n", szCmd);
puts("-d\t\tcreate the table");
puts("-i\t\tinsert initial records");
puts("-v\t\tverify initial records");
puts("-t<threads>\tnumber of threads making calls");
puts("-s<seed>\toffset for primary key");
puts("-b<batch>\tbatch size per thread");
puts("-c<maxcps>\tmax number of calls per second for this process");
puts("-m<size>\tsize of context data");
puts("-f\t\tno checkpointing and no logging");
puts("-w\t\tuse writeTuple instead of insertTuple");
puts("-r\t\treport response time statistics");
puts("-r+\t\treport response time distribution");
puts("-?\t\thelp");
}
int main(int argc, char* argv[])
{
ndb_init();
int iRes = -1;
g_nNumThreads = 0;
g_nMaxCallsPerSecond = 0;
long nSeed = 0;
bool bStoredTable = true;
bool bCreateTable = false;
g_bWriteTuple = false;
g_bReport = false;
g_bReportPlus = false;
for(int i=1; i<argc; ++i)
{
if(argv[i][0]=='-' || argv[i][0]=='/')
{
switch(argv[i][1])
{
case 't':
g_nNumThreads = atol(argv[i]+2);
break;
case 's':
nSeed = atol(argv[i]+2);
break;
case 'b':
g_nMaxContextIdPerThread = atol(argv[i]+2);
break;
case 'm':
g_nStatusDataSize = atol(argv[i]+2);
if(g_nStatusDataSize>sizeof(STATUS_DATA))
{
g_nStatusDataSize = sizeof(STATUS_DATA);
}
break;
case 'i':
g_bInsertInitial = true;
break;
case 'v':
g_bVerifyInitial = true;
break;
case 'd':
bCreateTable = true;
break;
case 'f':
bStoredTable = false;
break;
case 'w':
g_bWriteTuple = true;
break;
case 'r':
g_bReport = true;
if(argv[i][2]=='+')
{
g_bReportPlus = true;
}
break;
case 'c':
g_nMaxCallsPerSecond = atol(argv[i]+2);
break;
case '?':
default:
ShowHelp(argv[0]);
return -1;
}
}
else
{
ShowHelp(argv[0]);
return -1;
}
}
if(bCreateTable)
puts("-d\tcreate the table");
if(g_bInsertInitial)
printf("-i\tinsert initial records\n");
if(g_bVerifyInitial)
printf("-v\tverify initial records\n");
if(g_nNumThreads>0)
printf("-t%ld\tnumber of threads making calls\n", g_nNumThreads);
if(g_nNumThreads>0)
{
printf("-s%ld\toffset for primary key\n", nSeed);
printf("-b%ld\tbatch size per thread\n", g_nMaxContextIdPerThread);
}
if(g_nMaxCallsPerSecond>0)
printf("-c%ld\tmax number of calls per second for this process\n", g_nMaxCallsPerSecond);
if(!bStoredTable)
puts("-f\tno checkpointing and no logging to disk");
if(g_bWriteTuple)
puts("-w\tuse writeTuple instead of insertTuple");
if(g_bReport)
puts("-r\treport response time statistics");
if(g_bReportPlus)
puts("-r+\treport response time distribution");
if(!bCreateTable && g_nNumThreads<=0)
{
ShowHelp(argv[0]);
return -1;
}
printf("-m%ld\tsize of context data\n", g_nStatusDataSize);
g_szTableName = (bStoredTable ? c_szTableNameStored : c_szTableNameTemp);
#ifdef NDB_WIN32
SetConsoleCtrlHandler(ConsoleCtrlHandler, true);
#else
signal(SIGINT, CtrlCHandler);
#endif
if(g_bReport)
{
g_plCountMillisecForCall = new long[c_nMaxMillisecForAllCall];
memset(g_plCountMillisecForCall, 0, c_nMaxMillisecForAllCall*sizeof(long));
g_plCountMillisecForTrans = new long[c_nMaxMillisecForAllTrans];
memset(g_plCountMillisecForTrans, 0, c_nMaxMillisecForAllTrans*sizeof(long));
}
g_pNdbMutexIncrement = NdbMutex_Create();
g_pNdbMutexPrintf = NdbMutex_Create();
#ifdef NDB_WIN32
hShutdownEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
#endif
Ndb* pNdb = new Ndb(c_szDatabaseName);
if(!pNdb)
{
printf("could not construct ndb\n");
return 1;
}
if(pNdb->init(1) || pNdb->waitUntilReady())
{
ReportNdbError("could not initialize ndb\n", pNdb->getNdbError());
delete pNdb;
return 2;
}
if(bCreateTable)
{
printf("Create CallContext table\n");
if (bStoredTable)
{
if (CreateCallContextTable(pNdb, c_szTableNameStored, true))
{
printf("Create table failed\n");
delete pNdb;
return 3;
}
}
else
{
if (CreateCallContextTable(pNdb, c_szTableNameTemp, false))
{
printf("Create table failed\n");
delete pNdb;
return 3;
}
}
}
if(g_nNumThreads>0)
{
printf("creating %d threads\n", (int)g_nNumThreads);
if(g_bInsertInitial)
{
printf("each thread will insert %ld initial records, total %ld inserts\n",
g_nMaxContextIdPerThread, g_nNumThreads*g_nMaxContextIdPerThread);
}
if(g_bVerifyInitial)
{
printf("each thread will verify %ld initial records, total %ld reads\n",
g_nMaxContextIdPerThread, g_nNumThreads*g_nMaxContextIdPerThread);
}
g_nNumberOfInitialInsert = 0;
g_nNumberOfInitialVerify = 0;
NDB_TICKS tStartTime = NdbTick_CurrentMillisecond();
NdbThread* pThreads[256];
int pnStartingRecordNum[256];
int ij;
for(ij=0;ij<g_nNumThreads;ij++)
{
pnStartingRecordNum[ij] = (ij*g_nMaxContextIdPerThread) + nSeed;
}
for(ij=0;ij<g_nNumThreads;ij++)
{
pThreads[ij] = NdbThread_Create(RuntimeCallContext,
(void**)(pnStartingRecordNum+ij),
0, "RuntimeCallContext", NDB_THREAD_PRIO_LOW);
}
//Wait for the threads to finish
for(ij=0;ij<g_nNumThreads;ij++)
{
void* status;
NdbThread_WaitFor(pThreads[ij], &status);
}
NDB_TICKS tEndTime = NdbTick_CurrentMillisecond();
//Print time taken
printf("Time Taken for %ld Calls is %ld msec (= %ld calls/sec)\n",
g_nNumCallsProcessed,
(long)(tEndTime-tStartTime),
(long)((1000*g_nNumCallsProcessed)/(tEndTime-tStartTime)));
if(g_bInsertInitial)
printf("successfully inserted %ld tuples\n", g_nNumberOfInitialInsert);
if(g_bVerifyInitial)
printf("successfully verified %ld tuples\n", g_nNumberOfInitialVerify);
}
delete pNdb;
#ifdef NDB_WIN32
CloseHandle(hShutdownEvent);
#endif
NdbMutex_Destroy(g_pNdbMutexIncrement);
NdbMutex_Destroy(g_pNdbMutexPrintf);
if(g_bReport)
{
ReportResponseTimeStatistics("Calls", g_plCountMillisecForCall, c_nMaxMillisecForAllCall);
ReportResponseTimeStatistics("Transactions", g_plCountMillisecForTrans, c_nMaxMillisecForAllTrans);
delete[] g_plCountMillisecForCall;
delete[] g_plCountMillisecForTrans;
}
return 0;
}
|