-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtestPartitioning.cpp
More file actions
1397 lines (1198 loc) · 38.4 KB
/
Copy pathtestPartitioning.cpp
File metadata and controls
1397 lines (1198 loc) · 38.4 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) 2004, 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_Test.hpp>
#include <NDBT_ReturnCodes.h>
#include <HugoTransactions.hpp>
#include <UtilTransactions.hpp>
#include <NdbRestarter.hpp>
static Uint32 max_dks = 0;
static const Uint32 MAX_FRAGS=48 * 8 * 4; // e.g. 48 nodes, 8 frags/node, 4 replicas
static Uint32 frag_ng_mappings[MAX_FRAGS];
static const char* DistTabName= "DistTest";
static const char* DistTabDKeyCol= "DKey";
static const char* DistTabPKey2Col= "PKey2";
static const char* DistTabResultCol= "Result";
static const char* DistIdxName= "ResultIndex";
static
int
run_drop_table(NDBT_Context* ctx, NDBT_Step* step)
{
NdbDictionary::Dictionary* dict = GETNDB(step)->getDictionary();
dict->dropTable(ctx->getTab()->getName());
return 0;
}
static
int
setNativePartitioning(Ndb* ndb, NdbDictionary::Table& tab, int when, void* arg)
{
switch(when){
case 0: // Before
break;
case 1: // After
return 0;
default:
return 0;
}
/* Use rand to choose one of the native partitioning schemes */
const Uint32 rType= rand() % 3;
Uint32 fragType= -1;
switch(rType)
{
case 0 :
fragType = NdbDictionary::Object::DistrKeyHash;
break;
case 1 :
fragType = NdbDictionary::Object::DistrKeyLin;
break;
case 2:
fragType = NdbDictionary::Object::HashMapPartition;
break;
}
ndbout << "Setting fragment type to " << fragType << endl;
tab.setFragmentType((NdbDictionary::Object::FragmentType)fragType);
return 0;
}
static
int
add_distribution_key(Ndb* ndb, NdbDictionary::Table& tab, int when, void* arg)
{
switch(when){
case 0: // Before
break;
case 1: // After
return 0;
default:
return 0;
}
/* Choose a partitioning type */
setNativePartitioning(ndb, tab, when, arg);
int keys = tab.getNoOfPrimaryKeys();
Uint32 dks = (2 * keys + 2) / 3; dks = (dks > max_dks ? max_dks : dks);
for(int i = 0; i<tab.getNoOfColumns(); i++)
if(tab.getColumn(i)->getPrimaryKey() &&
tab.getColumn(i)->getCharset() != 0)
keys--;
Uint32 max = NDB_MAX_NO_OF_ATTRIBUTES_IN_KEY - tab.getNoOfPrimaryKeys();
if(max_dks < max)
max = max_dks;
if(keys <= 1 && max > 0)
{
dks = 1 + (rand() % max);
ndbout_c("%s pks: %d dks: %d", tab.getName(), keys, dks);
while(dks--)
{
NdbDictionary::Column col;
BaseString name;
name.assfmt("PK_DK_%d", dks);
col.setName(name.c_str());
if((rand() % 100) > 50)
{
col.setType(NdbDictionary::Column::Unsigned);
col.setLength(1);
}
else
{
col.setType(NdbDictionary::Column::Varbinary);
col.setLength(1+(rand() % 25));
}
col.setNullable(false);
col.setPrimaryKey(true);
col.setDistributionKey(true);
tab.addColumn(col);
}
}
else
{
for(int i = 0; i<tab.getNoOfColumns(); i++)
{
NdbDictionary::Column* col = tab.getColumn(i);
if(col->getPrimaryKey() && col->getCharset() == 0)
{
if((int)dks >= keys || (rand() % 100) > 50)
{
col->setDistributionKey(true);
dks--;
}
keys--;
}
}
}
ndbout << (NDBT_Table&)tab << endl;
return 0;
}
static
int
setupUDPartitioning(Ndb* ndb, NdbDictionary::Table& tab)
{
/* Following should really be taken from running test system : */
const Uint32 numNodes= ndb->get_ndb_cluster_connection().no_db_nodes();
const Uint32 numReplicas= 2; // Assumption
const Uint32 guessNumNgs= numNodes/2;
const Uint32 numNgs= guessNumNgs?guessNumNgs : 1;
const Uint32 numFragsPerNode= 2 + (rand() % 3);
const Uint32 numPartitions= numReplicas * numNgs * numFragsPerNode;
tab.setFragmentType(NdbDictionary::Table::UserDefined);
tab.setFragmentCount(numPartitions);
for (Uint32 i=0; i<numPartitions; i++)
{
frag_ng_mappings[i]= i % numNgs;
}
tab.setFragmentData(frag_ng_mappings, numPartitions);
return 0;
}
static
int
setUserDefPartitioning(Ndb* ndb, NdbDictionary::Table& tab, int when, void* arg)
{
switch(when){
case 0: // Before
break;
case 1: // After
return 0;
default:
return 0;
}
setupUDPartitioning(ndb, tab);
ndbout << (NDBT_Table&)tab << endl;
return 0;
}
static
int
one_distribution_key(Ndb* ndb, NdbDictionary::Table& tab, int when, void* arg)
{
switch(when){
case 0: // Before
break;
case 1: // After
return 0;
default:
return 0;
}
setNativePartitioning(ndb, tab, when, arg);
int keys = tab.getNoOfPrimaryKeys();
int dist_key_no = rand()% keys;
for(int i = 0; i<tab.getNoOfColumns(); i++)
{
if(tab.getColumn(i)->getPrimaryKey())
{
if (dist_key_no-- == 0)
{
tab.getColumn(i)->setDistributionKey(true);
}
else
{
tab.getColumn(i)->setDistributionKey(false);
}
}
}
ndbout << (NDBT_Table&)tab << endl;
return 0;
}
static
const NdbDictionary::Table*
create_dist_table(Ndb* pNdb,
bool userDefined)
{
NdbDictionary::Dictionary* dict= pNdb->getDictionary();
do {
NdbDictionary::Table tab;
tab.setName(DistTabName);
if (userDefined)
{
setupUDPartitioning(pNdb, tab);
}
else
{
setNativePartitioning(pNdb, tab, 0, 0);
}
NdbDictionary::Column dk;
dk.setName(DistTabDKeyCol);
dk.setType(NdbDictionary::Column::Unsigned);
dk.setLength(1);
dk.setNullable(false);
dk.setPrimaryKey(true);
dk.setPartitionKey(true);
tab.addColumn(dk);
NdbDictionary::Column pk2;
pk2.setName(DistTabPKey2Col);
pk2.setType(NdbDictionary::Column::Unsigned);
pk2.setLength(1);
pk2.setNullable(false);
pk2.setPrimaryKey(true);
pk2.setPartitionKey(false);
tab.addColumn(pk2);
NdbDictionary::Column result;
result.setName(DistTabResultCol);
result.setType(NdbDictionary::Column::Unsigned);
result.setLength(1);
result.setNullable(true);
result.setPrimaryKey(false);
tab.addColumn(result);
dict->dropTable(tab.getName());
if(dict->createTable(tab) == 0)
{
ndbout << (NDBT_Table&)tab << endl;
do {
/* Primary key index */
NdbDictionary::Index idx;
idx.setType(NdbDictionary::Index::OrderedIndex);
idx.setLogging(false);
idx.setTable(DistTabName);
idx.setName("PRIMARY");
idx.addColumnName(DistTabDKeyCol);
idx.addColumnName(DistTabPKey2Col);
dict->dropIndex("PRIMARY",
tab.getName());
if (dict->createIndex(idx) == 0)
{
ndbout << "Primary Index created successfully" << endl;
break;
}
ndbout << "Primary Index create failed with " <<
dict->getNdbError().code <<
" retrying " << endl;
} while (0);
do {
/* Now the index on the result column */
NdbDictionary::Index idx;
idx.setType(NdbDictionary::Index::OrderedIndex);
idx.setLogging(false);
idx.setTable(DistTabName);
idx.setName(DistIdxName);
idx.addColumnName(DistTabResultCol);
dict->dropIndex(idx.getName(),
tab.getName());
if (dict->createIndex(idx) == 0)
{
ndbout << "Index on Result created successfully" << endl;
return dict->getTable(tab.getName());
}
ndbout << "Index create failed with " <<
dict->getNdbError().code << endl;
} while (0);
}
} while (0);
return 0;
};
static int
run_create_table(NDBT_Context* ctx, NDBT_Step* step)
{
/* Create table, optionally with extra distribution keys
* or UserDefined partitioning
*/
max_dks = ctx->getProperty("distributionkey", (unsigned)0);
bool userDefined = ctx->getProperty("UserDefined", (unsigned) 0);
if(NDBT_Tables::createTable(GETNDB(step),
ctx->getTab()->getName(),
false, false,
max_dks?
add_distribution_key:
userDefined?
setUserDefPartitioning :
setNativePartitioning) == NDBT_OK)
{
return NDBT_OK;
}
if(GETNDB(step)->getDictionary()->getNdbError().code == 745)
return NDBT_OK;
return NDBT_FAILED;
}
static int
run_create_table_smart_scan(NDBT_Context* ctx, NDBT_Step* step)
{
if(NDBT_Tables::createTable(GETNDB(step),
ctx->getTab()->getName(),
false, false,
one_distribution_key) == NDBT_OK)
{
return NDBT_OK;
}
if(GETNDB(step)->getDictionary()->getNdbError().code == 745)
return NDBT_OK;
return NDBT_FAILED;
}
static int
run_create_pk_index(NDBT_Context* ctx, NDBT_Step* step){
bool orderedIndex = ctx->getProperty("OrderedIndex", (unsigned)0);
Ndb* pNdb = GETNDB(step);
const NdbDictionary::Table *pTab =
pNdb->getDictionary()->getTable(ctx->getTab()->getName());
if(!pTab)
return NDBT_OK;
bool logged = ctx->getProperty("LoggedIndexes", orderedIndex ? 0 : 1);
BaseString name;
name.assfmt("IND_%s_PK_%c", pTab->getName(), orderedIndex ? 'O' : 'U');
// Create index
if (orderedIndex)
ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "ordered index "
<< name.c_str() << " (";
else
ndbout << "Creating " << ((logged)?"logged ": "temporary ") << "unique index "
<< name.c_str() << " (";
NdbDictionary::Index pIdx(name.c_str());
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 (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;
}
static int run_create_pk_index_drop(NDBT_Context* ctx, NDBT_Step* step){
bool orderedIndex = ctx->getProperty("OrderedIndex", (unsigned)0);
Ndb* pNdb = GETNDB(step);
const NdbDictionary::Table *pTab =
pNdb->getDictionary()->getTable(ctx->getTab()->getName());
if(!pTab)
return NDBT_OK;
BaseString name;
name.assfmt("IND_%s_PK_%c", pTab->getName(), orderedIndex ? 'O' : 'U');
ndbout << "Dropping index " << name.c_str() << " ";
if (pNdb->getDictionary()->dropIndex(name.c_str(), pTab->getName()) != 0){
ndbout << "FAILED!" << endl;
ERR(pNdb->getDictionary()->getNdbError());
return NDBT_FAILED;
} else {
ndbout << "OK!" << endl;
}
return NDBT_OK;
}
static int
run_create_dist_table(NDBT_Context* ctx, NDBT_Step* step)
{
bool userDefined = ctx->getProperty("UserDefined", (unsigned)0);
if(create_dist_table(GETNDB(step),
userDefined))
return NDBT_OK;
return NDBT_FAILED;
}
static int
run_drop_dist_table(NDBT_Context* ctx, NDBT_Step* step)
{
GETNDB(step)->getDictionary()->dropTable(DistTabName);
return NDBT_OK;
}
static int
run_tests(Ndb* p_ndb, HugoTransactions& hugoTrans, int records, Uint32 batchSize = 1)
{
if (hugoTrans.loadTable(p_ndb, records, batchSize) != 0)
{
return NDBT_FAILED;
}
if(hugoTrans.pkReadRecords(p_ndb, records, batchSize) != 0)
{
return NDBT_FAILED;
}
if(hugoTrans.pkUpdateRecords(p_ndb, records, batchSize) != 0)
{
return NDBT_FAILED;
}
if(hugoTrans.pkDelRecords(p_ndb, records, batchSize) != 0)
{
return NDBT_FAILED;
}
if (hugoTrans.loadTable(p_ndb, records, batchSize) != 0)
{
return NDBT_FAILED;
}
if(hugoTrans.scanUpdateRecords(p_ndb, records) != 0)
{
return NDBT_FAILED;
}
Uint32 abort = 23;
for(Uint32 j = 0; j<5; j++){
Uint32 parallelism = (j == 1 ? 1 : j * 3);
ndbout_c("parallelism: %d", parallelism);
if (hugoTrans.scanReadRecords(p_ndb, records, abort, parallelism,
NdbOperation::LM_Read) != 0)
{
return NDBT_FAILED;
}
if (hugoTrans.scanReadRecords(p_ndb, records, abort, parallelism,
NdbOperation::LM_Exclusive) != 0)
{
return NDBT_FAILED;
}
if (hugoTrans.scanReadRecords(p_ndb, records, abort, parallelism,
NdbOperation::LM_CommittedRead) != 0)
{
return NDBT_FAILED;
}
}
if(hugoTrans.clearTable(p_ndb, records) != 0)
{
return NDBT_FAILED;
}
return 0;
}
static int
run_pk_dk(NDBT_Context* ctx, NDBT_Step* step)
{
Ndb* p_ndb = GETNDB(step);
int records = ctx->getNumRecords();
const NdbDictionary::Table *tab =
p_ndb->getDictionary()->getTable(ctx->getTab()->getName());
if(!tab)
return NDBT_OK;
HugoTransactions hugoTrans(*tab);
Uint32 batchSize= ctx->getProperty("BatchSize", (unsigned) 1);
return run_tests(p_ndb, hugoTrans, records, batchSize);
}
int
run_index_dk(NDBT_Context* ctx, NDBT_Step* step)
{
Ndb* p_ndb = GETNDB(step);
int records = ctx->getNumRecords();
const NdbDictionary::Table *pTab =
p_ndb->getDictionary()->getTable(ctx->getTab()->getName());
if(!pTab)
return NDBT_OK;
bool orderedIndex = ctx->getProperty("OrderedIndex", (unsigned)0);
BaseString name;
name.assfmt("IND_%s_PK_%c", pTab->getName(), orderedIndex ? 'O' : 'U');
const NdbDictionary::Index * idx =
p_ndb->getDictionary()->getIndex(name.c_str(), pTab->getName());
if(!idx)
{
ndbout << "Failed to retreive index: " << name.c_str() << endl;
return NDBT_FAILED;
}
Uint32 batchSize= ctx->getProperty("BatchSize", (unsigned) 1);
HugoTransactions hugoTrans(*pTab, idx);
return run_tests(p_ndb, hugoTrans, records, batchSize);
}
static int
run_startHint(NDBT_Context* ctx, NDBT_Step* step)
{
Ndb* p_ndb = GETNDB(step);
int records = ctx->getNumRecords();
const NdbDictionary::Table *tab =
p_ndb->getDictionary()->getTable(ctx->getTab()->getName());
if(!tab)
return NDBT_OK;
HugoTransactions hugoTrans(*tab);
if (hugoTrans.loadTable(p_ndb, records) != 0)
{
return NDBT_FAILED;
}
NdbRestarter restarter;
if(restarter.insertErrorInAllNodes(8050) != 0)
return NDBT_FAILED;
HugoCalculator dummy(*tab);
int result = NDBT_OK;
for(int i = 0; i<records && result == NDBT_OK; i++)
{
char buffer[NDB_MAX_TUPLE_SIZE];
char* start= buffer + (rand() & 7);
char* pos= start;
int k = 0;
Ndb::Key_part_ptr ptrs[NDB_MAX_NO_OF_ATTRIBUTES_IN_KEY+1];
for(int j = 0; j<tab->getNoOfColumns(); j++)
{
if(tab->getColumn(j)->getPartitionKey())
{
//ndbout_c(tab->getColumn(j)->getName());
int sz = tab->getColumn(j)->getSizeInBytes();
Uint32 real_size;
dummy.calcValue(i, j, 0, pos, sz, &real_size);
ptrs[k].ptr = pos;
ptrs[k++].len = real_size;
pos += (real_size + 3) & ~3;
}
}
ptrs[k].ptr = 0;
// Now we have the pk
NdbTransaction* pTrans= p_ndb->startTransaction(tab, ptrs);
HugoOperations ops(*tab);
ops.setTransaction(pTrans);
if(ops.pkReadRecord(p_ndb, i, 1) != NDBT_OK)
{
result = NDBT_FAILED;
break;
}
if(ops.execute_Commit(p_ndb) != 0)
{
result = NDBT_FAILED;
break;
}
ops.closeTransaction(p_ndb);
}
restarter.insertErrorInAllNodes(0);
return result;
}
static int
run_startHint_ordered_index(NDBT_Context* ctx, NDBT_Step* step)
{
Ndb* p_ndb = GETNDB(step);
int records = ctx->getNumRecords();
const NdbDictionary::Table *tab =
p_ndb->getDictionary()->getTable(ctx->getTab()->getName());
if(!tab)
return NDBT_OK;
BaseString name;
name.assfmt("IND_%s_PK_O", tab->getName());
const NdbDictionary::Index * idx =
p_ndb->getDictionary()->getIndex(name.c_str(), tab->getName());
if(!idx)
{
ndbout << "Failed to retreive index: " << name.c_str() << endl;
return NDBT_FAILED;
}
HugoTransactions hugoTrans(*tab, idx);
if (hugoTrans.loadTable(p_ndb, records) != 0)
{
return NDBT_FAILED;
}
NdbRestarter restarter;
if(restarter.insertErrorInAllNodes(8050) != 0)
return NDBT_FAILED;
HugoCalculator dummy(*tab);
int result = NDBT_OK;
for(int i = 0; i<records && result == NDBT_OK; i++)
{
char buffer[NDB_MAX_TUPLE_SIZE];
NdbTransaction* pTrans= NULL;
char* start= buffer + (rand() & 7);
char* pos= start;
int k = 0;
Ndb::Key_part_ptr ptrs[NDB_MAX_NO_OF_ATTRIBUTES_IN_KEY+1];
for(int j = 0; j<tab->getNoOfColumns(); j++)
{
if(tab->getColumn(j)->getPartitionKey())
{
//ndbout_c(tab->getColumn(j)->getName());
int sz = tab->getColumn(j)->getSizeInBytes();
Uint32 real_size;
dummy.calcValue(i, j, 0, pos, sz, &real_size);
ptrs[k].ptr = pos;
ptrs[k++].len = real_size;
pos += (real_size + 3) & ~3;
}
}
ptrs[k].ptr = 0;
// Now we have the pk, start a hinted transaction
pTrans= p_ndb->startTransaction(tab, ptrs);
// Because we pass an Ordered index here, pkReadRecord will
// use an index scan on the Ordered index
HugoOperations ops(*tab, idx);
ops.setTransaction(pTrans);
/* Despite it's name, it will actually perform index scans
* as there is an index.
* Error 8050 will cause an NDBD assertion failure in
* Dbtc::execDIGETPRIMCONF() if TC needs to scan a fragment
* which is not on the TC node
* So for this TC to pass with no failures we need transaction
* hinting and scan partition pruning on equal() to work
* correctly.
* TODO : Get coverage of Index scan which is equal on dist
* key cols, but has an inequality on some other column.
*/
if(ops.pkReadRecord(p_ndb, i, 1) != NDBT_OK)
{
result = NDBT_FAILED;
break;
}
if(ops.execute_Commit(p_ndb) != 0)
{
result = NDBT_FAILED;
break;
}
ops.closeTransaction(p_ndb);
}
restarter.insertErrorInAllNodes(0);
return result;
}
#define CHECK(x, y) {int res= (x); \
if (res != 0) { ndbout << "Assert failed at " \
<< __LINE__ << endl \
<< res << endl \
<< " error : " \
<< (y)->getNdbError().code \
<< endl; \
return NDBT_FAILED; } }
#define CHECKNOTNULL(x, y) { \
if ((x) == NULL) { ndbout << "Assert failed at line " \
<< __LINE__ << endl \
<< " with " \
<< (y)->getNdbError().code \
<< endl; \
return NDBT_FAILED; } }
static int
load_dist_table(Ndb* pNdb, int records, int parts)
{
const NdbDictionary::Table* tab= pNdb->getDictionary()->getTable(DistTabName);
bool userDefined= (tab->getFragmentType() ==
NdbDictionary::Object::UserDefined);
const NdbRecord* distRecord= tab->getDefaultRecord();
CHECKNOTNULL(distRecord, pNdb);
char* buf= (char*) malloc(NdbDictionary::getRecordRowLength(distRecord));
CHECKNOTNULL(buf, pNdb);
/* We insert a number of records with a constrained number of
* values for the distribution key column
*/
for (int r=0; r < records; r++)
{
NdbTransaction* trans= pNdb->startTransaction();
CHECKNOTNULL(trans, pNdb);
{
const int dKeyVal= r % parts;
const Uint32 dKeyAttrid= tab->getColumn(DistTabDKeyCol)->getAttrId();
memcpy(NdbDictionary::getValuePtr(distRecord, buf,
dKeyAttrid),
&dKeyVal, sizeof(dKeyVal));
}
{
const int pKey2Val= r;
const Uint32 pKey2Attrid= tab->getColumn(DistTabPKey2Col)->getAttrId();
memcpy(NdbDictionary::getValuePtr(distRecord, buf,
pKey2Attrid),
&pKey2Val, sizeof(pKey2Val));
}
{
const int resultVal= r*r;
const Uint32 resultValAttrid=
tab->getColumn(DistTabResultCol)->getAttrId();
memcpy(NdbDictionary::getValuePtr(distRecord, buf,
resultValAttrid),
&resultVal, sizeof(resultVal));
// set not NULL
NdbDictionary::setNull(distRecord, buf, resultValAttrid, false);
}
NdbOperation::OperationOptions opts;
opts.optionsPresent= 0;
if (userDefined)
{
/* For user-defined partitioning, we set the partition id
* to be the distribution key value modulo the number
* of partitions in the table
*/
opts.optionsPresent= NdbOperation::OperationOptions::OO_PARTITION_ID;
opts.partitionId= (r%parts) % tab->getFragmentCount();
}
CHECKNOTNULL(trans->insertTuple(distRecord, buf,
NULL, &opts, sizeof(opts)), trans);
if (trans->execute(NdbTransaction::Commit) != 0)
{
NdbError err = trans->getNdbError();
if (err.status == NdbError::TemporaryError)
{
ndbout << err << endl;
NdbSleep_MilliSleep(50);
r--; // just retry
}
else
{
CHECK(-1, trans);
}
}
trans->close();
}
free(buf);
return NDBT_OK;
};
struct PartInfo
{
NdbTransaction* trans;
NdbIndexScanOperation* op;
int dKeyVal;
int valCount;
};
class Ap
{
public:
void* ptr;
Ap(void* _ptr) : ptr(_ptr)
{};
~Ap()
{
if (ptr != 0)
{
free(ptr);
ptr= 0;
}
}
};
static int
dist_scan_body(Ndb* pNdb, int records, int parts, PartInfo* partInfo, bool usePrimary)
{
const NdbDictionary::Table* tab= pNdb->getDictionary()->getTable(DistTabName);
CHECKNOTNULL(tab, pNdb->getDictionary());
const char* indexName= usePrimary ? "PRIMARY" : DistIdxName;
const NdbDictionary::Index* idx= pNdb->getDictionary()->getIndex(indexName,
DistTabName);
CHECKNOTNULL(idx, pNdb->getDictionary());
const NdbRecord* tabRecord= tab->getDefaultRecord();
const NdbRecord* idxRecord= idx->getDefaultRecord();
bool userDefined= (tab->getFragmentType() ==
NdbDictionary::Object::UserDefined);
char* boundBuf= (char*) malloc(NdbDictionary::getRecordRowLength(idx->getDefaultRecord()));
if (usePrimary)
ndbout << "Checking MRR indexscan distribution awareness when distribution key part of bounds" << endl;
else
ndbout << "Checking MRR indexscan distribution awareness when distribution key provided explicitly" << endl;
if (userDefined)
ndbout << "User Defined Partitioning scheme" << endl;
else
ndbout << "Native Partitioning scheme" << endl;
Ap boundAp(boundBuf);
for (int r=0; r < records; r++)
{
int partValue= r % parts;
PartInfo& pInfo= partInfo[partValue];
if (pInfo.trans == NULL)
{
/* Provide the partition key as a hint for this transaction */
if (!userDefined)
{
Ndb::Key_part_ptr keyParts[2];
keyParts[0].ptr= &partValue;
keyParts[0].len= sizeof(partValue);
keyParts[1].ptr= NULL;
keyParts[1].len= 0;
/* To test that bad hinting causes failure, uncomment */
// int badPartVal= partValue+1;
// keyParts[0].ptr= &badPartVal;
CHECKNOTNULL(pInfo.trans= pNdb->startTransaction(tab, keyParts),
pNdb);
}
else
{
/* User Defined partitioning */
Uint32 partId= partValue % tab->getFragmentCount();
CHECKNOTNULL(pInfo.trans= pNdb->startTransaction(tab,
partId),
pNdb);
}
pInfo.valCount= 0;
pInfo.dKeyVal= partValue;
NdbScanOperation::ScanOptions opts;
opts.optionsPresent= NdbScanOperation::ScanOptions::SO_SCANFLAGS;
opts.scan_flags= NdbScanOperation::SF_MultiRange;
// Define the scan operation for this partition.
CHECKNOTNULL(pInfo.op= pInfo.trans->scanIndex(idx->getDefaultRecord(),
tab->getDefaultRecord(),
NdbOperation::LM_Read,
NULL,
NULL,
&opts,
sizeof(opts)),
pInfo.trans);
}
NdbIndexScanOperation* op= pInfo.op;
if (usePrimary)
{
{
int dKeyVal= partValue;
int pKey2Val= r;
/* Scanning the primary index, set bound on the pk */
memcpy(NdbDictionary::getValuePtr(idxRecord,
boundBuf,
tab->getColumn(DistTabDKeyCol)->getAttrId()),
&dKeyVal,
sizeof(dKeyVal));
memcpy(NdbDictionary::getValuePtr(idxRecord,
boundBuf,
tab->getColumn(DistTabPKey2Col)->getAttrId()),
&pKey2Val,
sizeof(pKey2Val));
}
NdbIndexScanOperation::IndexBound ib;
ib.low_key= boundBuf;
ib.low_key_count= 2;
ib.low_inclusive= true;
ib.high_key= ib.low_key;
ib.high_key_count= ib.low_key_count;
ib.high_inclusive= true;
ib.range_no= pInfo.valCount++;
/* No partitioning info for native, PK index scan
* NDBAPI can determine it from PK */
Ndb::PartitionSpec pSpec;
pSpec.type= Ndb::PartitionSpec::PS_NONE;
if (userDefined)
{
/* We'll provide partition info */
pSpec.type= Ndb::PartitionSpec::PS_USER_DEFINED;
pSpec.UserDefined.partitionId= partValue % tab->getFragmentCount();
}
CHECK(op->setBound(idxRecord,
ib,
&pSpec,