-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtestIndex.cpp
More file actions
3707 lines (3206 loc) · 108 KB
/
Copy pathtestIndex.cpp
File metadata and controls
3707 lines (3206 loc) · 108 KB
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
/*
Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <NDBT.hpp>
#include <NDBT_Test.hpp>
#include <HugoTransactions.hpp>
#include <UtilTransactions.hpp>
#include <NdbRestarter.hpp>
#include <NdbRestarts.hpp>
#include <Vector.hpp>
#include <signaldata/DumpStateOrd.hpp>
#include <NodeBitmask.hpp>
#include <NdbSqlUtil.hpp>
#include <BlockNumbers.h>
#define CHECK(b) if (!(b)) { \
g_err << "ERR: "<< step->getName() \
<< " failed on line " << __LINE__ << endl; \
result = NDBT_FAILED; break;\
}
#define CHECKRET(b) if (!(b)) { \
g_err << "ERR: "<< step->getName() \
<< " failed on line " << __LINE__ << endl; \
return NDBT_FAILED; \
}
struct Attrib {
bool indexCreated;
int numAttribs;
int attribs[1024];
Attrib(){
numAttribs = 0;
indexCreated = false;
}
};
class AttribList {
public:
AttribList(){};
~AttribList(){
for(size_t i = 0; i < attriblist.size(); i++){
delete attriblist[i];
}
};
void buildAttribList(const NdbDictionary::Table* pTab);
Vector<Attrib*> attriblist;
};
void AttribList::buildAttribList(const NdbDictionary::Table* pTab){
attriblist.clear();
Attrib* attr;
// Build attrib definitions that describes which attributes to build index
// Try to build strange combinations, not just "all" or all PK's
int i;
for(i = 1; i <= pTab->getNoOfColumns(); i++){
attr = new Attrib;
attr->numAttribs = i;
for(int a = 0; a<i; a++)
attr->attribs[a] = a;
attriblist.push_back(attr);
}
int b = 0;
for(i = pTab->getNoOfColumns()-1; i > 0; i--){
attr = new Attrib;
attr->numAttribs = i;
b++;
for(int a = 0; a<i; a++)
attr->attribs[a] = a+b;
attriblist.push_back(attr);
}
for(i = pTab->getNoOfColumns(); i > 0; i--){
attr = new Attrib;
attr->numAttribs = pTab->getNoOfColumns() - i;
for(int a = 0; a<pTab->getNoOfColumns() - i; a++)
attr->attribs[a] = pTab->getNoOfColumns()-a-1;
attriblist.push_back(attr);
}
for(i = 1; i < pTab->getNoOfColumns(); i++){
attr = new Attrib;
attr->numAttribs = pTab->getNoOfColumns() - i;
for(int a = 0; a<pTab->getNoOfColumns() - i; a++)
attr->attribs[a] = pTab->getNoOfColumns()-a-1;
attriblist.push_back(attr);
}
for(i = 1; i < pTab->getNoOfColumns(); i++){
attr = new Attrib;
attr->numAttribs = 2;
for(int a = 0; a<2; a++){
attr->attribs[a] = i%pTab->getNoOfColumns();
}
attriblist.push_back(attr);
}
// Last
attr = new Attrib;
attr->numAttribs = 1;
attr->attribs[0] = pTab->getNoOfColumns()-1;
attriblist.push_back(attr);
// Last and first
attr = new Attrib;
attr->numAttribs = 2;
attr->attribs[0] = pTab->getNoOfColumns()-1;
attr->attribs[1] = 0;
attriblist.push_back(attr);
// First and last
attr = new Attrib;
attr->numAttribs = 2;
attr->attribs[0] = 0;
attr->attribs[1] = pTab->getNoOfColumns()-1;
attriblist.push_back(attr);
#if 0
for(size_t i = 0; i < attriblist.size(); i++){
ndbout << attriblist[i]->numAttribs << ": " ;
for(int a = 0; a < attriblist[i]->numAttribs; a++)
ndbout << attriblist[i]->attribs[a] << ", ";
ndbout << endl;
}
#endif
}
char idxName[255];
char pkIdxName[255];
static const int SKIP_INDEX = 99;
int create_index(NDBT_Context* ctx, int indxNum,
const NdbDictionary::Table* pTab,
Ndb* pNdb, Attrib* attr, bool logged){
bool orderedIndex = ctx->getProperty("OrderedIndex", (unsigned)0);
bool notOnlyPkId = ctx->getProperty("NotOnlyPkId", (unsigned)0);
int result = NDBT_OK;
HugoCalculator calc(*pTab);
if (attr->numAttribs == 1 &&
calc.isUpdateCol(attr->attribs[0]) == true){
// Don't create index for the Hugo update column
// since it's not unique
return SKIP_INDEX;
}
// Create index
BaseString::snprintf(idxName, 255, "IDC%d", indxNum);
if (orderedIndex)
ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "ordered index "<<idxName << " (";
else
ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "unique index "<<idxName << " (";
ndbout << flush;
NdbDictionary::Index pIdx(idxName);
pIdx.setTable(pTab->getName());
if (orderedIndex)
pIdx.setType(NdbDictionary::Index::OrderedIndex);
else
pIdx.setType(NdbDictionary::Index::UniqueHashIndex);
bool includesOnlyPkIdCols = true;
for (int c = 0; c< attr->numAttribs; c++){
int attrNo = attr->attribs[c];
const NdbDictionary::Column* col = pTab->getColumn(attrNo);
switch(col->getType())
{
case NDB_TYPE_BIT:
case NDB_TYPE_BLOB:
case NDB_TYPE_TEXT:
/* Not supported */
ndbout << col->getName() << " - bad type )" << endl;
return SKIP_INDEX;
default:
break;
}
if (col->getStorageType() == NDB_STORAGETYPE_DISK)
{
ndbout << col->getName() << " - disk based )" << endl;
return SKIP_INDEX;
}
pIdx.addIndexColumn(col->getName());
ndbout << col->getName()<<" ";
if (! (col->getPrimaryKey() ||
calc.isIdCol(attrNo)))
includesOnlyPkIdCols = false;
}
if (notOnlyPkId && includesOnlyPkIdCols)
{
ndbout << " Only PK/id cols included - skipping" << endl;
return SKIP_INDEX;
}
if (!orderedIndex)
{
/**
* For unique indexes we must add PK, otherwise it's not guaranteed
* to be unique
*/
for (int i = 0; i<pTab->getNoOfColumns(); i++)
{
if (pTab->getColumn(i)->getPrimaryKey())
{
for (int j = 0; j<attr->numAttribs; j++)
{
if (attr->attribs[j] == i)
goto next;
}
pIdx.addIndexColumn(pTab->getColumn(i)->getName());
ndbout << pTab->getColumn(i)->getName() << " ";
}
next:
(void)i;
}
}
pIdx.setStoredIndex(logged);
ndbout << ") ";
bool noddl= ctx->getProperty("NoDDL");
if (noddl)
{
const NdbDictionary::Index* idx= pNdb->
getDictionary()->getIndex(pIdx.getName(), pTab->getName());
if (!idx)
{
ndbout << "Failed - Index does not exist and DDL not allowed" << endl;
return NDBT_FAILED;
}
else
{
attr->indexCreated = false;
// TODO : Check index definition is ok
}
}
else
{
if (pNdb->getDictionary()->createIndex(pIdx) != 0){
attr->indexCreated = false;
ndbout << "FAILED!" << endl;
const NdbError err = pNdb->getDictionary()->getNdbError();
ERR(err);
if (err.classification == NdbError::ApplicationError)
return SKIP_INDEX;
if (err.status == NdbError::TemporaryError)
return SKIP_INDEX;
return NDBT_FAILED;
} else {
ndbout << "OK!" << endl;
attr->indexCreated = true;
}
}
return result;
}
int drop_index(int indxNum, Ndb* pNdb,
const NdbDictionary::Table* pTab, Attrib* attr){
int result = NDBT_OK;
if (attr->indexCreated == false)
return NDBT_OK;
BaseString::snprintf(idxName, 255, "IDC%d", indxNum);
// Drop index
ndbout << "Dropping index "<<idxName<<"(" << pTab->getName() << ") ";
if (pNdb->getDictionary()->dropIndex(idxName, pTab->getName()) != 0){
ndbout << "FAILED!" << endl;
ERR(pNdb->getDictionary()->getNdbError());
result = NDBT_FAILED;
} else {
ndbout << "OK!" << endl;
}
return result;
}
int runCreateIndexes(NDBT_Context* ctx, NDBT_Step* step){
int loops = ctx->getNumLoops();
int l = 0;
const NdbDictionary::Table* pTab = ctx->getTab();
Ndb* pNdb = GETNDB(step);
int result = NDBT_OK;
// NOTE If we need to test creating both logged and non logged indexes
// this should be divided into two testcases
// The paramater logged should then be specified
// as a TC_PROPERTY. ex TC_PROPERTY("LoggedIndexes", 1);
// and read into the test step like
bool logged = ctx->getProperty("LoggedIndexes", 1);
AttribList attrList;
attrList.buildAttribList(pTab);
while (l < loops && result == NDBT_OK){
unsigned int i;
for (i = 0; i < attrList.attriblist.size(); i++){
// Try to create index
if (create_index(ctx, i, pTab, pNdb, attrList.attriblist[i], logged) == NDBT_FAILED)
result = NDBT_FAILED;
}
// Now drop all indexes that where created
for (i = 0; i < attrList.attriblist.size(); i++){
// Try to drop index
if (drop_index(i, pNdb, pTab, attrList.attriblist[i]) != NDBT_OK)
result = NDBT_FAILED;
}
l++;
}
return result;
}
int createRandomIndex(NDBT_Context* ctx, NDBT_Step* step){
const NdbDictionary::Table* pTab = ctx->getTab();
Ndb* pNdb = GETNDB(step);
bool logged = ctx->getProperty("LoggedIndexes", 1);
AttribList attrList;
attrList.buildAttribList(pTab);
int retries = 100;
while(retries > 0){
const Uint32 i = rand() % attrList.attriblist.size();
int res = create_index(ctx, i, pTab, pNdb, attrList.attriblist[i],
logged);
if (res == SKIP_INDEX){
retries--;
continue;
}
if (res == NDBT_FAILED){
return NDBT_FAILED;
}
ctx->setProperty("createRandomIndex", i);
// Now drop all indexes that where created
return NDBT_OK;
}
return NDBT_FAILED;
}
int createRandomIndex_Drop(NDBT_Context* ctx, NDBT_Step* step){
Ndb* pNdb = GETNDB(step);
Uint32 i = ctx->getProperty("createRandomIndex");
BaseString::snprintf(idxName, 255, "IDC%d", i);
// Drop index
ndbout << "Dropping index " << idxName << " ";
if (pNdb->getDictionary()->dropIndex(idxName,
ctx->getTab()->getName()) != 0){
ndbout << "FAILED!" << endl;
ERR(pNdb->getDictionary()->getNdbError());
return NDBT_FAILED;
} else {
ndbout << "OK!" << endl;
}
return NDBT_OK;
}
int createPkIndex(NDBT_Context* ctx, NDBT_Step* step){
bool orderedIndex = ctx->getProperty("OrderedIndex", (unsigned)0);
const NdbDictionary::Table* pTab = ctx->getTab();
Ndb* pNdb = GETNDB(step);
bool logged = ctx->getProperty("LoggedIndexes", 1);
bool noddl= ctx->getProperty("NoDDL");
// Create index
BaseString::snprintf(pkIdxName, 255, "IDC_PK_%s", pTab->getName());
if (orderedIndex)
ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "ordered index "
<< pkIdxName << " (";
else
ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "unique index "
<< pkIdxName << " (";
NdbDictionary::Index pIdx(pkIdxName);
pIdx.setTable(pTab->getName());
if (orderedIndex)
pIdx.setType(NdbDictionary::Index::OrderedIndex);
else
pIdx.setType(NdbDictionary::Index::UniqueHashIndex);
for (int c = 0; c< pTab->getNoOfColumns(); c++){
const NdbDictionary::Column * col = pTab->getColumn(c);
if(col->getPrimaryKey()){
pIdx.addIndexColumn(col->getName());
ndbout << col->getName() <<" ";
}
}
pIdx.setStoredIndex(logged);
ndbout << ") ";
if (noddl)
{
const NdbDictionary::Index* idx= pNdb->
getDictionary()->getIndex(pkIdxName, pTab->getName());
if (!idx)
{
ndbout << "Failed - Index does not exist and DDL not allowed" << endl;
ERR(pNdb->getDictionary()->getNdbError());
return NDBT_FAILED;
}
else
{
// TODO : Check index definition is ok
}
}
else
{
if (pNdb->getDictionary()->createIndex(pIdx) != 0){
ndbout << "FAILED!" << endl;
const NdbError err = pNdb->getDictionary()->getNdbError();
ERR(err);
return NDBT_FAILED;
}
}
ndbout << "OK!" << endl;
return NDBT_OK;
}
int createPkIndex_Drop(NDBT_Context* ctx, NDBT_Step* step){
const NdbDictionary::Table* pTab = ctx->getTab();
Ndb* pNdb = GETNDB(step);
bool noddl= ctx->getProperty("NoDDL");
// Drop index
if (!noddl)
{
ndbout << "Dropping index " << pkIdxName << " ";
if (pNdb->getDictionary()->dropIndex(pkIdxName,
pTab->getName()) != 0){
ndbout << "FAILED!" << endl;
ERR(pNdb->getDictionary()->getNdbError());
return NDBT_FAILED;
} else {
ndbout << "OK!" << endl;
}
}
return NDBT_OK;
}
int
runVerifyIndex(NDBT_Context* ctx, NDBT_Step* step){
// Verify that data in index match
// table data
Ndb* pNdb = GETNDB(step);
UtilTransactions utilTrans(*ctx->getTab());
const int batchSize = ctx->getProperty("BatchSize", 16);
const int parallelism = batchSize > 240 ? 240 : batchSize;
do {
if (utilTrans.verifyIndex(pNdb, idxName, parallelism, true) != 0){
g_err << "Inconsistent index" << endl;
return NDBT_FAILED;
}
} while(ctx->isTestStopped() == false);
return NDBT_OK;
}
int
runTransactions1(NDBT_Context* ctx, NDBT_Step* step){
// Verify that data in index match
// table data
Ndb* pNdb = GETNDB(step);
HugoTransactions hugoTrans(*ctx->getTab());
const int batchSize = ctx->getProperty("BatchSize", 50);
int rows = ctx->getNumRecords();
while (ctx->isTestStopped() == false) {
if (hugoTrans.pkUpdateRecords(pNdb, rows, batchSize) != 0){
g_err << "Updated table failed" << endl;
return NDBT_FAILED;
}
ctx->sync_down("PauseThreads");
if(ctx->isTestStopped())
break;
if (hugoTrans.scanUpdateRecords(pNdb, rows, batchSize) != 0){
g_err << "Updated table failed" << endl;
return NDBT_FAILED;
}
ctx->sync_down("PauseThreads");
}
return NDBT_OK;
}
int
runTransactions2(NDBT_Context* ctx, NDBT_Step* step){
// Verify that data in index match
// table data
Ndb* pNdb = GETNDB(step);
HugoTransactions hugoTrans(*ctx->getTab());
const int batchSize = ctx->getProperty("BatchSize", 50);
int rows = ctx->getNumRecords();
while (ctx->isTestStopped() == false) {
#if 1
if (hugoTrans.indexReadRecords(pNdb, pkIdxName, rows, batchSize) != 0){
g_err << "Index read failed" << endl;
return NDBT_FAILED;
}
#endif
ctx->sync_down("PauseThreads");
if(ctx->isTestStopped())
break;
#if 1
if (hugoTrans.indexUpdateRecords(pNdb, pkIdxName, rows, batchSize) != 0){
g_err << "Index update failed" << endl;
return NDBT_FAILED;
}
#endif
ctx->sync_down("PauseThreads");
}
return NDBT_OK;
}
int
runTransactions3(NDBT_Context* ctx, NDBT_Step* step){
// Verify that data in index match
// table data
Ndb* pNdb = GETNDB(step);
HugoTransactions hugoTrans(*ctx->getTab());
UtilTransactions utilTrans(*ctx->getTab());
const int batchSize = ctx->getProperty("BatchSize", 32);
const int parallel = batchSize > 240 ? 240 : batchSize;
int rows = ctx->getNumRecords();
while (ctx->isTestStopped() == false) {
if(hugoTrans.loadTable(pNdb, rows, batchSize, false) != 0){
g_err << "Load table failed" << endl;
return NDBT_FAILED;
}
ctx->sync_down("PauseThreads");
if(ctx->isTestStopped())
break;
if (hugoTrans.pkUpdateRecords(pNdb, rows, batchSize) != 0){
g_err << "Updated table failed" << endl;
return NDBT_FAILED;
}
ctx->sync_down("PauseThreads");
if(ctx->isTestStopped())
break;
if (hugoTrans.indexReadRecords(pNdb, pkIdxName, rows, batchSize) != 0){
g_err << "Index read failed" << endl;
return NDBT_FAILED;
}
ctx->sync_down("PauseThreads");
if(ctx->isTestStopped())
break;
if (hugoTrans.indexUpdateRecords(pNdb, pkIdxName, rows, batchSize) != 0){
g_err << "Index update failed" << endl;
return NDBT_FAILED;
}
ctx->sync_down("PauseThreads");
if(ctx->isTestStopped())
break;
if (hugoTrans.scanUpdateRecords(pNdb, rows, 5, parallel) != 0){
g_err << "Scan updated table failed" << endl;
return NDBT_FAILED;
}
ctx->sync_down("PauseThreads");
if(ctx->isTestStopped())
break;
if(utilTrans.clearTable(pNdb, rows, parallel) != 0){
g_err << "Clear table failed" << endl;
return NDBT_FAILED;
}
ctx->sync_down("PauseThreads");
if(ctx->isTestStopped())
break;
int count = -1;
if(utilTrans.selectCount(pNdb, 64, &count) != 0 || count != 0)
return NDBT_FAILED;
ctx->sync_down("PauseThreads");
}
return NDBT_OK;
}
int runRestarts(NDBT_Context* ctx, NDBT_Step* step){
int result = NDBT_OK;
int loops = ctx->getNumLoops();
NDBT_TestCase* pCase = ctx->getCase();
NdbRestarts restarts;
int i = 0;
int timeout = 240;
int sync_threads = ctx->getProperty("Threads", (unsigned)0);
while(i<loops && result != NDBT_FAILED && !ctx->isTestStopped()){
if(restarts.executeRestart(ctx, "RestartRandomNodeAbort", timeout) != 0){
g_err << "Failed to executeRestart(" <<pCase->getName() <<")" << endl;
result = NDBT_FAILED;
break;
}
ctx->sync_up_and_wait("PauseThreads", sync_threads);
i++;
}
ctx->stopTest();
return result;
}
int runCreateLoadDropIndex(NDBT_Context* ctx, NDBT_Step* step){
int loops = ctx->getNumLoops();
int records = ctx->getNumRecords();
int l = 0;
const NdbDictionary::Table* pTab = ctx->getTab();
Ndb* pNdb = GETNDB(step);
int result = NDBT_OK;
int batchSize = ctx->getProperty("BatchSize", 1);
int parallelism = batchSize > 240? 240: batchSize;
ndbout << "batchSize="<<batchSize<<endl;
bool logged = ctx->getProperty("LoggedIndexes", 1);
HugoTransactions hugoTrans(*pTab);
UtilTransactions utilTrans(*pTab);
AttribList attrList;
attrList.buildAttribList(pTab);
for (unsigned int i = 0; i < attrList.attriblist.size(); i++){
while (l < loops && result == NDBT_OK){
if ((l % 2) == 0){
// Create index first and then load
// Try to create index
if (create_index(ctx, i, pTab, pNdb, attrList.attriblist[i], logged) == NDBT_FAILED){
result = NDBT_FAILED;
}
// Load the table with data
ndbout << "Loading data after" << endl;
CHECK(hugoTrans.loadTable(pNdb, records, batchSize) == 0);
} else {
// Load table then create index
// Load the table with data
ndbout << "Loading data before" << endl;
CHECK(hugoTrans.loadTable(pNdb, records, batchSize) == 0);
// Try to create index
if (create_index(ctx, i, pTab, pNdb, attrList.attriblist[i], logged) == NDBT_FAILED)
result = NDBT_FAILED;
}
// Verify that data in index match
// table data
CHECK(utilTrans.verifyIndex(pNdb, idxName, parallelism) == 0);
// Do it all...
ndbout <<"Doing it all"<<endl;
int count;
ndbout << " pkUpdateRecords" << endl;
CHECK(hugoTrans.pkUpdateRecords(pNdb, records, batchSize) == 0);
CHECK(utilTrans.verifyIndex(pNdb, idxName, parallelism) == 0);
CHECK(hugoTrans.pkUpdateRecords(pNdb, records, batchSize) == 0);
CHECK(utilTrans.verifyIndex(pNdb, idxName, parallelism) == 0);
ndbout << " pkDelRecords half" << endl;
CHECK(hugoTrans.pkDelRecords(pNdb, records/2, batchSize) == 0);
CHECK(utilTrans.verifyIndex(pNdb, idxName, parallelism) == 0);
ndbout << " scanUpdateRecords" << endl;
CHECK(hugoTrans.scanUpdateRecords(pNdb, records/2, parallelism) == 0);
CHECK(utilTrans.verifyIndex(pNdb, idxName, parallelism) == 0);
ndbout << " clearTable" << endl;
CHECK(utilTrans.clearTable(pNdb, records/2, parallelism) == 0);
CHECK(utilTrans.verifyIndex(pNdb, idxName, parallelism) == 0);
CHECK(utilTrans.selectCount(pNdb, 64, &count) == 0);
CHECK(count == 0);
ndbout << " loadTable" << endl;
CHECK(hugoTrans.loadTable(pNdb, records, batchSize) == 0);
CHECK(utilTrans.verifyIndex(pNdb, idxName, parallelism) == 0);
ndbout << " loadTable again" << endl;
CHECK(hugoTrans.loadTable(pNdb, records, batchSize) == 0);
CHECK(utilTrans.verifyIndex(pNdb, idxName, parallelism) == 0);
CHECK(utilTrans.selectCount(pNdb, 64, &count) == 0);
CHECK(count == records);
if ((l % 2) == 0){
// Drop index first and then clear
// Try to create index
if (drop_index(i, pNdb, pTab, attrList.attriblist[i]) != NDBT_OK){
result = NDBT_FAILED;
}
// Clear table
ndbout << "Clearing table after" << endl;
CHECK(hugoTrans.clearTable(pNdb, records, parallelism) == 0);
} else {
// Clear table then drop index
//Clear table
ndbout << "Clearing table before" << endl;
CHECK(hugoTrans.clearTable(pNdb, records, parallelism) == 0);
// Try to drop index
if (drop_index(i, pNdb, pTab, attrList.attriblist[i]) != NDBT_OK)
result = NDBT_FAILED;
}
ndbout << " Done!" << endl;
l++;
}
// Make sure index is dropped
drop_index(i, pNdb, pTab, attrList.attriblist[i]);
}
return result;
}
int runInsertDelete(NDBT_Context* ctx, NDBT_Step* step){
int loops = ctx->getNumLoops();
int records = ctx->getNumRecords();
const NdbDictionary::Table* pTab = ctx->getTab();
Ndb* pNdb = GETNDB(step);
int result = NDBT_OK;
int batchSize = ctx->getProperty("BatchSize", 1);
int parallelism = batchSize > 240? 240: batchSize;
ndbout << "batchSize="<<batchSize<<endl;
bool logged = ctx->getProperty("LoggedIndexes", 1);
HugoTransactions hugoTrans(*pTab);
UtilTransactions utilTrans(*pTab);
AttribList attrList;
attrList.buildAttribList(pTab);
for (unsigned int i = 0; i < attrList.attriblist.size(); i++){
Attrib* attr = attrList.attriblist[i];
// Create index
if (create_index(ctx, i, pTab, pNdb, attr, logged) == NDBT_OK){
int l = 1;
while (l <= loops && result == NDBT_OK){
CHECK(hugoTrans.loadTable(pNdb, records, batchSize) == 0);
CHECK(utilTrans.verifyIndex(pNdb, idxName, parallelism) == 0);
CHECK(utilTrans.clearTable(pNdb, records, parallelism) == 0);
CHECK(utilTrans.verifyIndex(pNdb, idxName, parallelism) == 0);
l++;
}
// Drop index
if (drop_index(i, pNdb, pTab, attr) != NDBT_OK)
result = NDBT_FAILED;
}
}
return result;
}
int tryAddUniqueIndex(Ndb* pNdb,
const NdbDictionary::Table* pTab,
const char* idxName,
HugoCalculator& calc,
int& chosenCol)
{
for(int c = 0; c < pTab->getNoOfColumns(); c++)
{
const NdbDictionary::Column* col = pTab->getColumn(c);
if (!col->getPrimaryKey() &&
!calc.isUpdateCol(c) &&
!col->getNullable() &&
col->getStorageType() != NDB_STORAGETYPE_DISK)
{
chosenCol = c;
break;
}
}
if (chosenCol == -1)
{
return 1;
}
/* Create unique index on chosen column */
const char* colName = pTab->getColumn(chosenCol)->getName();
ndbout << "Creating unique index :" << idxName << " on ("
<< colName << ")" << endl;
NdbDictionary::Index idxDef(idxName);
idxDef.setTable(pTab->getName());
idxDef.setType(NdbDictionary::Index::UniqueHashIndex);
idxDef.addIndexColumn(colName);
idxDef.setStoredIndex(false);
if (pNdb->getDictionary()->createIndex(idxDef) != 0)
{
ndbout << "FAILED!" << endl;
const NdbError err = pNdb->getDictionary()->getNdbError();
ERR(err);
return -1;
}
return 0;
}
int tryInsertUniqueRecord(NDBT_Step* step,
HugoOperations& hugoOps,
int& recordNum)
{
Ndb* pNdb = GETNDB(step);
do
{
CHECKRET(hugoOps.startTransaction(pNdb) == 0);
CHECKRET(hugoOps.pkInsertRecord(pNdb,
recordNum,
1, // NumRecords
0) // UpdatesValue
== 0);
if (hugoOps.execute_Commit(pNdb) != 0)
{
NdbError err = hugoOps.getTransaction()->getNdbError();
hugoOps.closeTransaction(pNdb);
if (err.code == 839)
{
/* Unique constraint violation, try again with
* different record
*/
recordNum++;
continue;
}
else
{
ERR(err);
return NDBT_FAILED;
}
}
hugoOps.closeTransaction(pNdb);
break;
} while (true);
return NDBT_OK;
}
int runConstraintDetails(NDBT_Context* ctx, NDBT_Step* step)
{
const NdbDictionary::Table* pTab = ctx->getTab();
Ndb* pNdb = GETNDB(step);
/* Steps in testcase
* 1) Choose a column to index - not pk or updates column
* 2) Insert a couple of unique rows
* 3) For a number of different batch sizes :
* i) Insert a row with a conflicting values
* ii) Update an existing row with a conflicting value
* Verify :
* - The correct error is received
* - The failing constraint is detected
* - The error details string is as expected.
*/
HugoCalculator calc(*pTab);
/* Choose column to add unique index to */
int chosenCol = -1;
const char* idxName = "constraintCheck";
int rc = tryAddUniqueIndex(pNdb, pTab, idxName, calc, chosenCol);
if (rc)
{
if (rc == 1)
{
ndbout << "No suitable column in this table, skipping" << endl;
return NDBT_OK;
}
return NDBT_FAILED;
}
const NdbDictionary::Index* pIdx =
pNdb->getDictionary()->getIndex(idxName, pTab->getName());
CHECKRET(pIdx != 0);
/* Now insert a couple of rows */
HugoOperations hugoOps(*pTab);
int firstRecordNum = 0;
CHECKRET(tryInsertUniqueRecord(step, hugoOps, firstRecordNum) == NDBT_OK);
int secondRecordNum = firstRecordNum + 1;
CHECKRET(tryInsertUniqueRecord(step, hugoOps, secondRecordNum) == NDBT_OK);
/* Now we'll attempt to insert/update records
* in various sized batches and check the errors which
* are returned
*/
int maxBatchSize = 10;
int recordOffset = secondRecordNum + 1;
char buff[NDB_MAX_TUPLE_SIZE];
Uint32 real_len;
CHECKRET(calc.calcValue(firstRecordNum, chosenCol, 0, &buff[0],
pTab->getColumn(chosenCol)->getSizeInBytes(),
&real_len) != 0);
for (int optype = 0; optype < 2; optype ++)
{
bool useInsert = (optype == 0);
ndbout << "Verifying constraint violation for "
<< (useInsert?"Insert":"Update")
<< " operations" << endl;
for (int batchSize = 1; batchSize <= maxBatchSize; batchSize++)
{
NdbTransaction* trans = pNdb->startTransaction();
CHECKRET(trans != 0);
for (int rows = 0; rows < batchSize; rows ++)
{
int rowId = recordOffset + rows;
NdbOperation* op = trans->getNdbOperation(pTab);
CHECKRET(op != 0);
if (useInsert)
{
CHECKRET(op->insertTuple() == 0);
CHECKRET(hugoOps.setValues(op, rowId, 0) == 0);
/* Now override setValue for the indexed column to cause
* constraint violation
*/
CHECKRET(op->setValue(chosenCol, &buff[0], real_len) == 0);
}
else
{
/* Update value of 'second' row to conflict with
* first
*/
CHECKRET(op->updateTuple() == 0);
CHECKRET(hugoOps.equalForRow(op, secondRecordNum) == 0);
CHECKRET(op->setValue(chosenCol, &buff[0], real_len) == 0);
}
}