1 /* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
15 
16 #include <ndb_global.h>
17 #include <ndb_opts.h>
18 #include <NdbApi.hpp>
19 #include <NdbTest.hpp>
20 #include <my_sys.h>
21 #include <ndb_version.h>
22 
23 // version >= 5.1 required
24 
25 #if !defined(min) || !defined(max)
26 #define min(x, y) ((x) < (y) ? (x) : (y))
27 #define max(x, y) ((x) > (y) ? (x) : (y))
28 #endif
29 
30 /*
31  * Test composite operations on same PK via events.  The merge of event
32  * data can happen in 2 places:
33  *
34  * 1) In TUP at commit, the detached triggers report a single composite
35  * operation and its post/pre data
36  *
37  * 2) In event API version >= 5.1 separate commits within same GCI are
38  * optionally merged.  This is required to read blob data via NdbBlob.
39  *
40  * In this test merge is on by default.
41  *
42  * Option --separate-events disables GCI merge and implies --no-blobs.
43  * This is used to test basic events functionality.
44  *
45  * Option --no-blobs omits blob attributes.  This is used to test GCI
46  * merge without getting into blob bugs.
47  *
48  * Option --no-multiops allows 1 operation per commit.  This avoids TUP
49  * and blob multi-operation bugs.
50  *
51  * There are other -no-* options, each added to isolate a specific bug.
52  *
53  * There are 5 ways (ignoring NUL operand) to compose 2 ops:
54  *
55  * INS o DEL = NUL
56  * INS o UPD = INS
57  * DEL o INS = UPD
58  * UPD o DEL = DEL
59  * UPD o UPD = UPD
60  *
61  * Event merge in NDB API handles idempotent INS o INS and DEL o DEL
62  * which are possible on NF (node failure).  This test does not handle
63  * them when --separate-events is used.
64  */
65 
66 struct Opts {
67   my_bool abort_on_error;
68   int loglevel;
69   uint loop;
70   uint maxops;
71   uint maxpk;
72   my_bool no_blobs;
73   my_bool no_implicit_nulls;
74   my_bool no_missing_update;
75   my_bool no_multiops;
76   my_bool no_nulls;
77   my_bool one_blob;
78   const char* opstring;
79   uint seed;
80   int maxtab;
81   my_bool separate_events;
82   uint tweak; // whatever's useful
83   my_bool use_table;
84 };
85 
86 static Opts g_opts;
87 static const uint g_maxpk = 1000;
88 static const uint g_maxtab = 100;
89 static const uint g_maxopstringpart = 100;
90 static const char* g_opstringpart[g_maxopstringpart];
91 static uint g_opstringparts = 0;
92 static uint g_loop = 0;
93 
94 static Ndb_cluster_connection* g_ncc = 0;
95 static Ndb* g_ndb = 0;
96 static NdbDictionary::Dictionary* g_dic = 0;
97 static NdbTransaction* g_con = 0;
98 static NdbOperation* g_op = 0;
99 static NdbScanOperation* g_scan_op = 0;
100 
101 static const uint g_charlen = 5;
102 static const char* g_charval = "abcdefgh";
103 static const char* g_csname = "latin1_swedish_ci";
104 
105 static uint g_blobinlinesize = 256;
106 static uint g_blobpartsize = 2000;
107 static uint g_blobstripesize = 2;
108 static const uint g_maxblobsize = 100000;
109 
110 static NdbEventOperation* g_evt_op = 0;
111 static NdbBlob* g_bh = 0;
112 
113 static uint
urandom()114 urandom()
115 {
116   uint r = (uint)random();
117   return r;
118 }
119 
120 static uint
urandom(uint m)121 urandom(uint m)
122 {
123   if (m == 0)
124     return 0;
125   uint r = urandom();
126   r = r % m;
127   return r;
128 }
129 
130 static bool
urandom(uint per,uint cent)131 urandom(uint per, uint cent)
132 {
133   return urandom(cent) < per;
134 }
135 
136 static int& g_loglevel = g_opts.loglevel; // default log level
137 
138 #define chkdb(x) \
139   do { if (x) break; ndbout << "line " << __LINE__ << " FAIL " << #x << endl; errdb(); if (g_opts.abort_on_error) abort(); return -1; } while (0)
140 
141 #define chkrc(x) \
142   do { if (x) break; ndbout << "line " << __LINE__ << " FAIL " << #x << endl; if (g_opts.abort_on_error) abort(); return -1; } while (0)
143 
144 #define reqrc(x) \
145   do { if (x) break; ndbout << "line " << __LINE__ << " ASSERT " << #x << endl; abort(); } while (0)
146 
147 #define ll0(x) \
148   do { if (g_loglevel < 0) break; ndbout << x << endl; } while (0)
149 
150 #define ll1(x) \
151   do { if (g_loglevel < 1) break; ndbout << x << endl; } while (0)
152 
153 #define ll2(x) \
154   do { if (g_loglevel < 2) break; ndbout << x << endl; } while (0)
155 
156 #define ll3(x) \
157   do { if (g_loglevel < 3) break; ndbout << x << endl; } while (0)
158 
159 static void
errdb()160 errdb()
161 {
162   uint any = 0;
163   // g_ncc return no error...
164   if (g_ndb != 0) {
165     const NdbError& e = g_ndb->getNdbError();
166     if (e.code != 0)
167       ll0(++any << " ndb: error " << e);
168   }
169   if (g_dic != 0) {
170     const NdbError& e = g_dic->getNdbError();
171     if (e.code != 0)
172       ll0(++any << " dic: error " << e);
173   }
174   if (g_con != 0) {
175     const NdbError& e = g_con->getNdbError();
176     if (e.code != 0)
177       ll0(++any << " con: error " << e);
178   }
179   if (g_op != 0) {
180     const NdbError& e = g_op->getNdbError();
181     if (e.code != 0)
182       ll0(++any << " op: error " << e);
183   }
184   if (g_scan_op != 0) {
185     const NdbError& e = g_scan_op->getNdbError();
186     if (e.code != 0)
187       ll0(++any << " scan_op: error " << e);
188   }
189   if (g_evt_op != 0) {
190     const NdbError& e = g_evt_op->getNdbError();
191     if (e.code != 0)
192       ll0(++any << " evt_op: error " << e);
193   }
194   if (g_bh != 0) {
195     const NdbError& e = g_bh->getNdbError();
196     if (e.code != 0)
197       ll0(++any << " bh: error " << e);
198   }
199   if (! any)
200     ll0("unknown db error");
201 }
202 
203 struct Col {
204   uint no;
205   const char* name;
206   NdbDictionary::Column::Type type;
207   bool pk;
208   bool nullable;
209   uint length;
210   uint size;
isblobCol211   bool isblob() const {
212     return
213       type == NdbDictionary::Column::Text ||
214       type == NdbDictionary::Column::Blob;
215   }
216 };
217 
218 static const Col g_col[] = {
219   { 0, "pk1", NdbDictionary::Column::Unsigned, true, false, 1, 4 },
220   { 1, "pk2", NdbDictionary::Column::Char, true, false,  g_charlen, g_charlen },
221   { 2, "seq", NdbDictionary::Column::Unsigned,  false, false, 1, 4 },
222   { 3, "cc1", NdbDictionary::Column::Char, false, true, g_charlen, g_charlen },
223   { 4, "tx1", NdbDictionary::Column::Text, false, true, 0, 0 },
224   { 5, "tx2", NdbDictionary::Column::Text, false, true, 0, 0 },
225   { 6, "bl1", NdbDictionary::Column::Blob, false, true, 0, 0 } // tinyblob
226 };
227 
228 static const uint g_maxcol = sizeof(g_col)/sizeof(g_col[0]);
229 static const uint g_blobcols = 3;
230 
231 static uint
ncol()232 ncol()
233 {
234   uint n = g_maxcol;
235   if (g_opts.no_blobs)
236     n -= g_blobcols;
237   else if (g_opts.one_blob)
238     n -= (g_blobcols - 2);
239   return n;
240 }
241 
242 static const Col&
getcol(uint i)243 getcol(uint i)
244 {
245   if (i < ncol())
246     return g_col[i];
247   assert(false);
248   return g_col[0];
249 }
250 
251 static const Col&
getcol(const char * name)252 getcol(const char* name)
253 {
254   uint i;
255   for (i = 0; i < ncol(); i++)
256     if (strcmp(g_col[i].name, name) == 0)
257       break;
258   return getcol(i);
259 }
260 
261 struct Tab {
262   char tabname[20];
263   const Col* col;
264   const NdbDictionary::Table* tab;
265   char evtname[20];
266   const NdbDictionary::Event* evt;
TabTab267   Tab(uint idx) :
268     col(g_col),
269     tab(0),
270     evt(0)
271   {
272     sprintf(tabname, "tem%d", idx);
273     sprintf(evtname, "tem%dev", idx);
274   }
275 };
276 
277 static Tab* g_tablst[g_maxtab];
278 
279 static uint
maxtab()280 maxtab()
281 {
282   return g_opts.maxtab;
283 }
284 
285 static Tab&
tab(uint i)286 tab(uint i)
287 {
288   assert(i < maxtab() && g_tablst[i] != 0);
289   return *g_tablst[i];
290 }
291 
292 static int
createtable(Tab & t)293 createtable(Tab& t)
294 {
295   ll2("createtable: " << t.tabname);
296   t.tab = 0;
297   NdbDictionary::Table tab(t.tabname);
298   tab.setLogging(false);
299   CHARSET_INFO* cs;
300   chkrc((cs = get_charset_by_name(g_csname, MYF(0))) != 0);
301   uint i;
302   for (i = 0; i < ncol(); i++) {
303     const Col& c = t.col[i];
304     NdbDictionary::Column col(c.name);
305     col.setType(c.type);
306     col.setPrimaryKey(c.pk);
307     if (! c.pk)
308       col.setNullable(true);
309     col.setLength(c.length);
310     switch (c.type) {
311     case NdbDictionary::Column::Unsigned:
312       break;
313     case NdbDictionary::Column::Char:
314       col.setLength(c.length);
315       col.setCharset(cs);
316       break;
317     case NdbDictionary::Column::Text:
318       col.setInlineSize(g_blobinlinesize);
319       col.setPartSize(g_blobpartsize);
320       col.setStripeSize(g_blobstripesize);
321       col.setCharset(cs);
322       break;
323     case NdbDictionary::Column::Blob:
324       col.setInlineSize(g_blobinlinesize);
325       col.setPartSize(0);
326       col.setStripeSize(0);
327       break;
328     default:
329       assert(false);
330       break;
331     }
332     tab.addColumn(col);
333   }
334   g_dic = g_ndb->getDictionary();
335   if (! g_opts.use_table) {
336     if (g_dic->getTable(t.tabname) != 0)
337       chkdb(g_dic->dropTable(t.tabname) == 0);
338     chkdb(g_dic->createTable(tab) == 0);
339   }
340   chkdb((t.tab = g_dic->getTable(t.tabname)) != 0);
341   g_dic = 0;
342   if (! g_opts.use_table) {
343     // extra row for GCI probe
344     chkdb((g_con = g_ndb->startTransaction()) != 0);
345     chkdb((g_op = g_con->getNdbOperation(t.tabname)) != 0);
346     chkdb(g_op->insertTuple() == 0);
347     Uint32 pk1;
348     char pk2[g_charlen + 1];
349     pk1 = g_maxpk;
350     sprintf(pk2, "%-*u", g_charlen, pk1);
351     chkdb(g_op->equal("pk1", (char*)&pk1) == 0);
352     chkdb(g_op->equal("pk2", (char*)&pk2[0]) == 0);
353     chkdb(g_con->execute(Commit) == 0);
354     g_ndb->closeTransaction(g_con);
355     g_op = 0;
356     g_con = 0;
357   }
358   return 0;
359 }
360 
361 static int
createtables()362 createtables()
363 {
364   ll1("createtables");
365   for (uint i = 0; i < maxtab(); i++)
366     chkrc(createtable(tab(i)) == 0);
367   return 0;
368 }
369 
370 static int
droptable(Tab & t)371 droptable(Tab& t)
372 {
373   ll2("droptable: " << t.tabname);
374   if (! g_opts.use_table) {
375     g_dic = g_ndb->getDictionary();
376     chkdb(g_dic->dropTable(t.tabname) == 0);
377     t.tab = 0;
378     g_dic = 0;
379   }
380   return 0;
381 }
382 
383 static int
droptables()384 droptables()
385 {
386   ll1("droptables");
387   for (uint i = 0; i < maxtab(); i++)
388     chkrc(droptable(tab(i)) == 0);
389   return 0;
390 }
391 
392 static int
createevent(Tab & t)393 createevent(Tab& t)
394 {
395   ll2("createevent: " << t.evtname);
396   t.evt = 0;
397   g_dic = g_ndb->getDictionary();
398   NdbDictionary::Event evt(t.evtname);
399   assert(t.tab != 0);
400   evt.setTable(*t.tab);
401   evt.addTableEvent(NdbDictionary::Event::TE_ALL);
402   uint i;
403   for (i = 0; i < ncol(); i++) {
404     const Col& c = g_col[i];
405     evt.addEventColumn(c.name);
406   }
407   evt.setReport(NdbDictionary::Event::ER_UPDATED);
408   evt.mergeEvents(! g_opts.separate_events);
409 #if 0 // XXX random bugs
410   if (g_dic->getEvent(t.evtname) != 0)
411     chkdb(g_dic->dropEvent(t.evtname) == 0);
412 #else
413   (void)g_dic->dropEvent(t.evtname);
414   chkdb(g_dic->createEvent(evt) == 0);
415 #endif
416   chkdb((t.evt = g_dic->getEvent(t.evtname)) != 0);
417   g_dic = 0;
418   return 0;
419 }
420 
421 static int
createevents()422 createevents()
423 {
424   ll1("createevents");
425   for (uint i = 0; i < maxtab(); i++)
426     chkrc(createevent(tab(i)) == 0);
427   return 0;
428 }
429 
430 static int
dropevent(Tab & t,bool force=false)431 dropevent(Tab& t, bool force = false)
432 {
433   ll2("dropevent: " << t.evtname);
434   g_dic = g_ndb->getDictionary();
435   chkdb(g_dic->dropEvent(t.evtname) == 0 || force);
436   t.evt = 0;
437   g_dic = 0;
438   return 0;
439 }
440 
441 static int
dropevents(bool force=false)442 dropevents(bool force = false)
443 {
444   ll1("dropevents");
445   for (uint i = 0; i < maxtab(); i++) {
446     if (force && g_tablst[i] == 0)
447       continue;
448     chkrc(dropevent(tab(i), force) == 0 || force);
449   }
450   return 0;
451 }
452 
453 struct Data {
454   struct Txt { char* val; uint len; };
455   union Ptr { Uint32* u32; char* ch; Txt* txt; void* v; };
456   Uint32 pk1;
457   char pk2[g_charlen + 1];
458   Uint32 seq;
459   char cc1[g_charlen + 1];
460   Txt tx1;
461   Txt tx2;
462   Txt bl1;
463   Ptr ptr[g_maxcol];
464   int ind[g_maxcol]; // -1 = no data, 1 = NULL, 0 = not NULL
465   uint noop; // bit: omit in NdbOperation (implicit NULL INS or no UPD)
466   uint ppeq; // bit: post/pre data value equal in GCI data[0]/data[1]
initData467   void init() {
468     uint i;
469     pk1 = 0;
470     memset(pk2, 0, sizeof(pk2));
471     seq = 0;
472     memset(cc1, 0, sizeof(cc1));
473     tx1.val = tx2.val = bl1.val = 0;
474     tx1.len = tx2.len = bl1.len = 0;
475     ptr[0].u32 = &pk1;
476     ptr[1].ch = pk2;
477     ptr[2].u32 = &seq;
478     ptr[3].ch = cc1;
479     ptr[4].txt = &tx1;
480     ptr[5].txt = &tx2;
481     ptr[6].txt = &bl1;
482     for (i = 0; i < g_maxcol; i++)
483       ind[i] = -1;
484     noop = 0;
485     ppeq = 0;
486   }
freememData487   void freemem() {
488     delete [] tx1.val;
489     delete [] tx2.val;
490     delete [] bl1.val;
491     tx1.val = tx2.val = bl1.val = 0;
492     tx1.len = tx2.len = bl1.len = 0;
493   }
494 };
495 
496 static int
cmpcol(const Col & c,const Data & d1,const Data & d2)497 cmpcol(const Col& c, const Data& d1, const Data& d2)
498 {
499   uint i = c.no;
500   if (d1.ind[i] != d2.ind[i])
501     return 1;
502   if (d1.ind[i] == 0) {
503     switch (c.type) {
504     case NdbDictionary::Column::Unsigned:
505       if (*d1.ptr[i].u32 != *d2.ptr[i].u32)
506         return 1;
507       break;
508     case NdbDictionary::Column::Char:
509       if (memcmp(d1.ptr[i].ch, d2.ptr[i].ch, c.size) != 0)
510         return 1;
511       break;
512     case NdbDictionary::Column::Text:
513     case NdbDictionary::Column::Blob:
514       {
515         const Data::Txt& t1 = *d1.ptr[i].txt;
516         const Data::Txt& t2 = *d2.ptr[i].txt;
517         if (t1.len != t2.len)
518           return 1;
519         if (memcmp(t1.val, t2.val, t1.len) != 0)
520           return 1;
521       }
522       break;
523     default:
524       assert(false);
525       break;
526     }
527   }
528   return 0;
529 }
530 
531 static NdbOut&
operator <<(NdbOut & out,const Data & d)532 operator<<(NdbOut& out, const Data& d)
533 {
534   uint i;
535   for (i = 0; i < ncol(); i++) {
536     const Col& c = getcol(i);
537     out << (i == 0 ? "" : " ") << c.name;
538     out << (! (d.noop & (1 << i)) ? "=" : ":");
539     if (d.ind[i] == -1)
540       continue;
541     if (d.ind[i] == 1) {
542       out << "NULL";
543       continue;
544     }
545     switch (c.type) {
546     case NdbDictionary::Column::Unsigned:
547       out << *d.ptr[i].u32;
548       break;
549     case NdbDictionary::Column::Char:
550       {
551         char buf[g_charlen + 1];
552         memcpy(buf, d.ptr[i].ch, g_charlen);
553         uint n = g_charlen;
554         while (1) {
555           buf[n] = 0;
556           if (n == 0 || buf[n - 1] != 0x20)
557             break;
558           n--;
559         }
560         out << "'" << buf << "'";
561       }
562       break;
563     case NdbDictionary::Column::Text:
564     case NdbDictionary::Column::Blob:
565       {
566         Data::Txt& txt = *d.ptr[i].txt;
567         bool first = true;
568         uint j = 0;
569         while (j < txt.len) {
570           char c[2];
571           c[0] = txt.val[j++];
572           c[1] = 0;
573           uint m = 1;
574           while (j < txt.len && txt.val[j] == c[0])
575             j++, m++;
576           if (! first)
577             out << "+";
578           first = false;
579           out << m << c;
580         }
581       }
582       break;
583     default:
584       assert(false);
585       break;
586     }
587   }
588   return out;
589 }
590 
591 // some random os may define these
592 #undef UNDEF
593 #undef INS
594 #undef DEL
595 #undef UPD
596 #undef NUL
597 
598 static const uint g_optypes = 3; // real ops 0-2
599 
600 /*
601  * Represents single or composite operation or received event.  The
602  * post/pre data is either computed here for operations or received from
603  * the event.
604  */
605 struct Op { // single or composite
606   enum Kind { OP = 1, EV = 2 };
607   enum Type { UNDEF = -1, INS, DEL, UPD, NUL };
608   Kind kind;
609   Type type;
610   Op* next_op; // within one commit
611   Op* next_com; // next commit chain
612   Op* next_gci; // groups commit chains (unless --separate-events)
613   Op* next_ev; // next event
614   Op* next_free; // free list
615   bool free; // on free list
616   uint num_op;
617   uint num_com;
618   Data data[2]; // 0-post 1-pre
619   bool match; // matched to event
620   Uint32 gci; // defined for com op and event
initOp621   void init(Kind a_kind, Type a_type = UNDEF) {
622     kind = a_kind;
623     assert(kind == OP || kind == EV);
624     type = a_type;
625     next_op = next_com = next_gci = next_ev = next_free = 0;
626     free = false;
627     num_op = num_com = 0;
628     data[0].init();
629     data[1].init();
630     match = false;
631     gci = 0;
632   }
freememOp633   void freemem() {
634     data[0].freemem();
635     data[1].freemem();
636   }
637 };
638 
639 static NdbOut&
operator <<(NdbOut & out,Op::Type optype)640 operator<<(NdbOut& out, Op::Type optype)
641 {
642   switch (optype) {
643   case Op::INS:
644     out << "INS";
645     break;
646   case Op::DEL:
647     out << "DEL";
648     break;
649   case Op::UPD:
650     out << "UPD";
651     break;
652   case Op::NUL:
653     out << "NUL";
654     break;
655   default:
656     out << (int)optype;
657     break;
658   }
659   return out;
660 }
661 
662 static NdbOut&
operator <<(NdbOut & out,const Op & op)663 operator<<(NdbOut& out, const Op& op)
664 {
665   out << op.type;
666   out << " " << op.data[0];
667   out << " [" << op.data[1] << "]";
668   if (op.gci != 0)
669     out << " gci:" << op.gci;
670   return out;
671 }
672 
673 static int
seteventtype(Op * ev,NdbDictionary::Event::TableEvent te)674 seteventtype(Op* ev, NdbDictionary::Event::TableEvent te)
675 {
676   Op::Type optype = Op::UNDEF;
677   switch (te) {
678   case NdbDictionary::Event::TE_INSERT:
679     optype = Op::INS;
680     break;
681   case NdbDictionary::Event::TE_DELETE:
682     optype = Op::DEL;
683     break;
684   case NdbDictionary::Event::TE_UPDATE:
685     optype = Op::UPD;
686     break;
687   default:
688     ll0("EVT: " << *ev << ": bad event type " << hex << (uint)te);
689     return -1;
690   }
691   ev->type = optype;
692   return 0;
693 }
694 
695 struct Counter { // debug aid
696   const char* name;
697   uint count;
CounterCounter698   Counter(const char* a_name) : name(a_name), count(0) {
699   }
operator <<(NdbOut & out,const Counter & counter)700   friend class NdbOut& operator<<(NdbOut& out, const Counter& counter) {
701     out << counter.name << "(" << counter.count << ")";
702     return out;
703   }
operator uintCounter704   operator uint() {
705     return count;
706   }
operator ++Counter707   Counter operator ++(int) {
708     ll3(*this << "++");
709     Counter tmp = *this;
710     count++;
711     return tmp;
712   }
operator --Counter713   Counter operator --(int) {
714     ll3(*this << "--");
715     assert(count != 0);
716     Counter tmp = *this;
717     count--;
718     return tmp;
719   }
720 };
721 
722 static Op* g_opfree = 0;
723 static uint g_freeops = 0;
724 static uint g_usedops = 0;
725 static uint g_gciops = 0;
726 static uint g_maxcom = 10; // max ops per commit
727 static uint g_seq = 0;
728 static Op* g_rec_ev;
729 static uint g_num_ev = 0;
730 
731 static const uint g_maxgcis = 500; // max GCIs seen during 1 loop
732 
733 // operation data per table and each loop
734 struct Run : public Tab {
735   bool skip; // no ops in current loop
736   NdbEventOperation* evt_op;
737   uint gcicnt; // number of CGIs seen in current loop
738   Uint64 gcinum[g_maxgcis];
739   Uint32 gcievtypes[g_maxgcis][2]; // 0-getGCIEventOperations 1-nextEvent
740   uint tableops; // real table ops in this loop
741   uint blobops; // approx blob part ops in this loop
742   uint gciops; // commit chains or (after mergeops) gci chains
743   Op* pk_op[g_maxpk]; // GCI chain of ops per PK
744   Op* pk_ev[g_maxpk]; // events per PK
745   uint ev_pos[g_maxpk]; // counts events
746   NdbRecAttr* ev_ra[2][g_maxcol]; // 0-post 1-pre
747   NdbBlob* ev_bh[2][g_maxcol]; // 0-post 1-pre
RunRun748   Run(uint idx) :
749     Tab(idx)
750   {
751     reset();
752   }
resetRun753   void reset()
754   {
755     int i, j;
756     skip = false;
757     evt_op = 0;
758     gcicnt = 0;
759     for (i = 0; i < g_maxgcis; i++) {
760       gcinum[i] = (Uint64)0;
761       gcievtypes[i][0] = gcievtypes[i][1] = (Uint32)0;
762     }
763     tableops = 0;
764     blobops = 0;
765     gciops = 0;
766     for (i = 0; i < g_maxpk; i++) {
767       pk_op[i] = 0;
768       pk_ev[i] = 0;
769       ev_pos[i] = 0;
770     }
771     for (j = 0; i < 2; j ++) {
772       for (i = 0; i < g_maxcol; i++) {
773         ev_ra[j][i] = 0;
774         ev_bh[j][i] = 0;
775       }
776     }
777   }
addgciRun778   int addgci(Uint64 gci)
779   {
780     assert(gcicnt < g_maxgcis);
781     chkrc(gcicnt == 0 || gcinum[gcicnt - 1] < gci);
782     gcinum[gcicnt++] = gci;
783     return 0;
784   }
addevtypesRun785   void addevtypes(Uint64 gci, Uint32 evtypes, uint i)
786   {
787     assert(gcicnt != 0 && gci == gcinum[gcicnt - 1]);
788     assert(evtypes != 0);
789     assert(i < 2);
790     gcievtypes[gcicnt - 1][i] |= evtypes;
791   }
792 };
793 
794 static Run* g_runlst[g_maxtab];
795 
796 static uint
maxrun()797 maxrun()
798 {
799   return maxtab();
800 }
801 
802 static Run&
run(uint i)803 run(uint i)
804 {
805   assert(i < maxrun() && g_runlst[i] != 0);
806   return *g_runlst[i];
807 }
808 
809 static void
initrun()810 initrun()
811 {
812   uint i;
813   for (i = 0; i < maxrun(); i++)
814     g_tablst[i] = g_runlst[i] = new Run(i);
815 }
816 
817 static Op*
getop(Op::Kind a_kind,Op::Type a_type=Op::UNDEF)818 getop(Op::Kind a_kind, Op::Type a_type = Op::UNDEF)
819 {
820   if (g_opfree == 0) {
821     assert(g_freeops == 0);
822     Op* op = new Op;
823     assert(op != 0);
824     op->next_free = g_opfree; // 0
825     g_opfree = op;
826     op->free = true;
827     g_freeops++;
828   }
829   Op* op = g_opfree;
830   g_opfree = op->next_free;
831   assert(g_freeops != 0);
832   g_freeops--;
833   g_usedops++;
834   op->init(a_kind, a_type);
835   op->free = false;
836   ll3("getop: " << op);
837   return op;
838 }
839 
840 static void
freeop(Op * op)841 freeop(Op* op)
842 {
843   ll3("freeop: " << op);
844   assert(! op->free);
845   op->freemem();
846   op->free = true;
847   op->next_free = g_opfree;
848   g_opfree = op;
849   g_freeops++;
850   assert(g_usedops != 0);
851   g_usedops--;
852 }
853 
854 static void
resetmem(Run & r)855 resetmem(Run& r)
856 {
857   ll2("resetmem");
858   Uint32 pk1;
859   for (pk1 = 0; pk1 < g_opts.maxpk; pk1++)
860     r.ev_pos[pk1] = 0;
861   // leave g_seq
862   for (pk1 = 0; pk1 < g_opts.maxpk; pk1++) {
863     if (r.pk_op[pk1] != 0) {
864       Op* tot_op = r.pk_op[pk1];
865       while (tot_op->next_gci != 0) {
866         Op* gci_op = tot_op->next_gci;
867         while (gci_op->next_com != 0) {
868           Op* com_op = gci_op->next_com;
869           while (com_op->next_op != 0) {
870             Op* op = com_op->next_op;
871             com_op->next_op = op->next_op;
872             freeop(op);
873           }
874           gci_op->next_com = com_op->next_com;
875           freeop(com_op);
876         }
877         tot_op->next_gci = gci_op->next_gci;
878         freeop(gci_op);
879       }
880       freeop(tot_op);
881       r.pk_op[pk1] = 0;
882     }
883     if (r.pk_ev[pk1] != 0) {
884       Op* tot_op = r.pk_ev[pk1];
885       while (tot_op->next_ev != 0) {
886         Op* ev = tot_op->next_ev;
887         tot_op->next_ev = ev->next_ev;
888         freeop(ev);
889       }
890       freeop(tot_op);
891       r.pk_ev[pk1] = 0;
892     }
893   }
894   r.reset();
895 }
896 
897 static void
resetmem()898 resetmem()
899 {
900   if (g_rec_ev != 0) {
901     freeop(g_rec_ev);
902     g_rec_ev = 0;
903   }
904   for (uint i = 0; i < maxrun(); i++)
905     resetmem(run(i));
906   assert(g_usedops == 0);
907   g_gciops = g_num_ev = 0;
908 }
909 
910 static void
deleteops()911 deleteops() // for memleak checks
912 {
913   while (g_opfree != 0) {
914     Op* tmp_op = g_opfree;
915     g_opfree = g_opfree->next_free;
916     delete tmp_op;
917     g_freeops--;
918   }
919   assert(g_freeops == 0);
920 }
921 
922 struct Comp {
923   Op::Type t1, t2, t3;
924 };
925 
926 static Comp
927 g_comp[] = {
928   { Op::INS, Op::DEL, Op::NUL },
929   { Op::INS, Op::UPD, Op::INS },
930   { Op::DEL, Op::INS, Op::UPD },
931   { Op::UPD, Op::DEL, Op::DEL },
932   { Op::UPD, Op::UPD, Op::UPD }
933 };
934 
935 static const uint g_ncomp = sizeof(g_comp)/sizeof(g_comp[0]);
936 
937 static int
checkop(const Op * op,Uint32 & pk1)938 checkop(const Op* op, Uint32& pk1)
939 {
940   Op::Type optype = op->type;
941   assert(optype != Op::UNDEF);
942   if (optype == Op::NUL)
943     return 0;
944   chkrc(optype == Op::INS || optype == Op::DEL || optype == Op::UPD);
945   const Data& d0 = op->data[0];
946   const Data& d1 = op->data[1];
947   {
948     const Col& c = getcol("pk1");
949     chkrc(d0.ind[c.no] == 0);
950     pk1 = d0.pk1;
951     chkrc(pk1 < g_opts.maxpk);
952   }
953   uint i;
954   for (i = 0; i < ncol(); i++) {
955     const Col& c = getcol(i);
956     const int ind0 = d0.ind[i];
957     const int ind1 = d1.ind[i];
958     // the rules are the rules..
959     if (c.pk) {
960       chkrc(ind0 == 0); // always PK in post data
961       if (optype == Op::INS)
962         chkrc(ind1 == -1);
963       if (optype == Op::DEL)
964         chkrc(ind1 == -1); // no PK in pre data
965       if (optype == Op::UPD)
966         chkrc(ind1 == 0);
967     }
968     if (! c.pk) {
969       if (optype == Op::INS)
970         chkrc(ind0 >= 0 && ind1 == -1);
971       if (optype == Op::DEL)
972         chkrc(ind0 == -1 && ind1 >= 0); // always non-PK in pre data
973       if (optype == Op::UPD)
974         chkrc(ind0 == -1 || ind1 >= 0); // update must have pre data
975     }
976     if (! c.nullable) {
977       chkrc(ind0 <= 0 && ind1 <= 0);
978     }
979     if (c.isblob()) {
980       // blob values must be from allowed chars
981       int j;
982       for (j = 0; j < 2; j++) {
983         const Data& d = op->data[j];
984         if (d.ind[i] == 0) {
985           const Data::Txt& txt = *d.ptr[i].txt;
986           int k;
987           for (k = 0; k < txt.len; k++) {
988             chkrc(strchr(g_charval, txt.val[k]) != 0);
989           }
990         }
991       }
992     }
993   }
994   return 0;
995 }
996 
997 static Comp*
comptype(Op::Type t1,Op::Type t2)998 comptype(Op::Type t1, Op::Type t2) // only non-NUL
999 {
1000   uint i;
1001   for (i = 0; i < g_ncomp; i++)
1002     if (g_comp[i].t1 == t1 && g_comp[i].t2 == t2)
1003       return &g_comp[i];
1004   return 0;
1005 }
1006 
1007 static void
copycol(const Col & c,const Data & d1,Data & d3)1008 copycol(const Col& c, const Data& d1, Data& d3)
1009 {
1010   uint i = c.no;
1011   if ((d3.ind[i] = d1.ind[i]) == 0) {
1012     if (! c.isblob()) {
1013       memmove(d3.ptr[i].v, d1.ptr[i].v, c.size);
1014     } else {
1015       Data::Txt& t1 = *d1.ptr[i].txt;
1016       Data::Txt& t3 = *d3.ptr[i].txt;
1017       delete [] t3.val;
1018       t3.val = new char [t1.len];
1019       t3.len = t1.len;
1020       memcpy(t3.val, t1.val, t1.len);
1021     }
1022   }
1023 }
1024 
1025 static void
copydata(const Data & d1,Data & d3,bool pk,bool nonpk)1026 copydata(const Data& d1, Data& d3, bool pk, bool nonpk)
1027 {
1028   uint i;
1029   for (i = 0; i < ncol(); i++) {
1030     const Col& c = g_col[i];
1031     if (c.pk && pk || ! c.pk && nonpk)
1032       copycol(c, d1, d3);
1033   }
1034 }
1035 
1036 static void
compdata(const Data & d1,const Data & d2,Data & d3,bool pk,bool nonpk)1037 compdata(const Data& d1, const Data& d2, Data& d3, bool pk, bool nonpk)
1038 {
1039   uint i;
1040   for (i = 0; i < ncol(); i++) {
1041     const Col& c = g_col[i];
1042     if (c.pk && pk || ! c.pk && nonpk) {
1043       const Data* d = 0;
1044       if (d1.ind[i] == -1 && d2.ind[i] == -1)
1045         d3.ind[i] = -1;
1046       else if (d1.ind[i] == -1 && d2.ind[i] != -1)
1047         d = &d2;
1048       else if (d1.ind[i] != -1 && d2.ind[i] == -1)
1049         d = &d1;
1050       else
1051         d = &d2;
1052       if (d != 0)
1053         copycol(c, *d, d3);
1054     }
1055   }
1056 }
1057 
1058 static void
copyop(const Op * op1,Op * op3)1059 copyop(const Op* op1, Op* op3)
1060 {
1061   op3->type = op1->type;
1062   copydata(op1->data[0], op3->data[0], true, true);
1063   copydata(op1->data[1], op3->data[1], true, true);
1064   op3->gci = op1->gci;
1065   Uint32 pk1_tmp;
1066   reqrc(checkop(op3, pk1_tmp) == 0);
1067 }
1068 
1069 static int
compop(const Op * op1,const Op * op2,Op * op3)1070 compop(const Op* op1, const Op* op2, Op* op3) // op1 o op2 = op3
1071 {
1072   assert(op1->type != Op::UNDEF && op2->type != Op::UNDEF);
1073   Comp* comp;
1074   if (op2->type == Op::NUL) {
1075     copyop(op1, op3);
1076     return 0;
1077   }
1078   if (op1->type == Op::NUL) {
1079     copyop(op2, op3);
1080     return 0;
1081   }
1082   Op::Kind kind =
1083     op1->kind == Op::OP && op2->kind == Op::OP ? Op::OP : Op::EV;
1084   Op* res_op = getop(kind);
1085   chkrc((comp = comptype(op1->type, op2->type)) != 0);
1086   res_op->type = comp->t3;
1087   if (res_op->type == Op::INS) {
1088     // INS o UPD
1089     compdata(op1->data[0], op2->data[0], res_op->data[0], true, true);
1090     // pre = undef
1091   }
1092   if (res_op->type == Op::DEL) {
1093     // UPD o DEL
1094     copydata(op2->data[0], res_op->data[0], true, false); // PK
1095     copydata(op1->data[1], res_op->data[1], false, true); // non-PK
1096   }
1097   if (res_op->type == Op::UPD && op1->type == Op::DEL) {
1098     // DEL o INS
1099     copydata(op2->data[0], res_op->data[0], true, true);
1100     copydata(op1->data[0], res_op->data[1], true, false); // PK
1101     copydata(op1->data[1], res_op->data[1], false, true); // non-PK
1102   }
1103   if (res_op->type == Op::UPD && op1->type == Op::UPD) {
1104     // UPD o UPD
1105     compdata(op1->data[0], op2->data[0], res_op->data[0], true, true);
1106     compdata(op2->data[1], op1->data[1], res_op->data[1], true, true);
1107   }
1108   assert(op1->gci == op2->gci);
1109   res_op->gci = op2->gci;
1110   Uint32 pk1_tmp;
1111   reqrc(checkop(res_op, pk1_tmp) == 0);
1112   copyop(res_op, op3);
1113   freeop(res_op);
1114   return 0;
1115 }
1116 
1117 static int
createeventop(Run & r)1118 createeventop(Run& r)
1119 {
1120   ll2("createeventop: " << r.tabname);
1121   chkdb((r.evt_op = g_ndb->createEventOperation(r.evtname)) != 0);
1122   r.evt_op->mergeEvents(! g_opts.separate_events); // not yet inherited
1123   uint i;
1124   for (i = 0; i < ncol(); i++) {
1125     const Col& c = g_col[i];
1126     Data (&d)[2] = g_rec_ev->data;
1127     if (! c.isblob()) {
1128       chkdb((r.ev_ra[0][i] = r.evt_op->getValue(c.name, (char*)d[0].ptr[i].v)) != 0);
1129       chkdb((r.ev_ra[1][i] = r.evt_op->getPreValue(c.name, (char*)d[1].ptr[i].v)) != 0);
1130     } else {
1131       chkdb((r.ev_bh[0][i] = r.evt_op->getBlobHandle(c.name)) != 0);
1132       chkdb((r.ev_bh[1][i] = r.evt_op->getPreBlobHandle(c.name)) != 0);
1133     }
1134   }
1135   return 0;
1136 }
1137 
1138 static int
createeventop()1139 createeventop()
1140 {
1141   ll1("createeventop");
1142   for (uint i = 0; i < maxrun(); i++)
1143     chkrc(createeventop(run(i)) == 0);
1144   return 0;
1145 }
1146 
1147 static int
executeeventop(Run & r)1148 executeeventop(Run& r)
1149 {
1150   ll2("executeeventop: " << r.tabname);
1151   chkdb(r.evt_op->execute() == 0);
1152   return 0;
1153 }
1154 
1155 static int
executeeventop()1156 executeeventop()
1157 {
1158   ll1("executeeventop");
1159   for (uint i = 0; i < maxrun(); i++)
1160     chkrc(executeeventop(run(i)) == 0);
1161   return 0;
1162 }
1163 
1164 static int
dropeventop(Run & r,bool force=false)1165 dropeventop(Run& r, bool force = false)
1166 {
1167   ll2("dropeventop: " << r.tabname);
1168   if (r.evt_op != 0) {
1169     chkdb(g_ndb->dropEventOperation(r.evt_op) == 0 || force);
1170     r.evt_op = 0;
1171   }
1172   return 0;
1173 }
1174 
1175 static int
dropeventops(bool force=false)1176 dropeventops(bool force = false)
1177 {
1178   ll1("dropeventops");
1179   for (uint i = 0; i < maxrun(); i++) {
1180     if (force && g_runlst[i] == 0)
1181       continue;
1182     chkrc(dropeventop(run(i), force) == 0 || force);
1183   }
1184   return 0;
1185 }
1186 
1187 // wait for event to be installed and for GCIs to pass
1188 static int
waitgci(uint ngci)1189 waitgci(uint ngci)
1190 {
1191   ll1("waitgci " << ngci);
1192   Uint32 gci[2];
1193   uint i = 0;
1194   while (1) {
1195     chkdb((g_con = g_ndb->startTransaction()) != 0);
1196     { // forced to exec a dummy op
1197       Tab& t = tab(0); // use first table
1198       Uint32 pk1;
1199       char pk2[g_charlen + 1];
1200       pk1 = g_maxpk;
1201       sprintf(pk2, "%-*u", g_charlen, pk1);
1202       chkdb((g_op = g_con->getNdbOperation(t.tabname)) != 0);
1203       chkdb(g_op->readTuple() == 0);
1204       chkdb(g_op->equal("pk1", (char*)&pk1) == 0);
1205       chkdb(g_op->equal("pk2", (char*)&pk2[0]) == 0);
1206       chkdb(g_con->execute(Commit) == 0);
1207       g_op = 0;
1208     }
1209     gci[i] = g_con->getGCI();
1210     g_ndb->closeTransaction(g_con);
1211     g_con = 0;
1212     if (i == 1 && gci[0] + ngci <= gci[1]) {
1213       ll1("waitgci: " << gci[0] << " " << gci[1]);
1214       break;
1215     }
1216     i = 1;
1217     sleep(1);
1218   }
1219   return 0;
1220 }
1221 
1222 // scan table and set current tot_op for each pk1
1223 static int
scantable(Run & r)1224 scantable(Run& r)
1225 {
1226   ll2("scantable: " << r.tabname);
1227   NdbRecAttr* ra[g_maxcol];
1228   NdbBlob* bh[g_maxcol];
1229   Op* rec_op = getop(Op::OP);
1230   Data& d0 = rec_op->data[0];
1231   chkdb((g_con = g_ndb->startTransaction()) != 0);
1232   chkdb((g_scan_op = g_con->getNdbScanOperation(r.tabname)) != 0);
1233   chkdb(g_scan_op->readTuples() == 0);
1234   uint i;
1235   for (i = 0; i < ncol(); i++) {
1236     const Col& c = getcol(i);
1237     if (! c.isblob()) {
1238       chkdb((ra[i] = g_scan_op->getValue(c.name, (char*)d0.ptr[i].v)) != 0);
1239     } else {
1240       chkdb((bh[i] = g_scan_op->getBlobHandle(c.name)) != 0);
1241     }
1242   }
1243   chkdb(g_con->execute(NoCommit) == 0);
1244   int ret;
1245   while ((ret = g_scan_op->nextResult()) == 0) {
1246     Uint32 pk1 = d0.pk1;
1247     if (pk1 >= g_opts.maxpk)
1248       continue;
1249     rec_op->type = Op::INS;
1250     for (i = 0; i < ncol(); i++) {
1251       const Col& c = getcol(i);
1252       int ind;
1253       if (! c.isblob()) {
1254         ind = ra[i]->isNULL();
1255       } else {
1256         int ret;
1257         ret = bh[i]->getDefined(ind);
1258         assert(ret == 0);
1259         if (ind == 0) {
1260           Data::Txt& txt = *d0.ptr[i].txt;
1261           Uint64 len64;
1262           ret = bh[i]->getLength(len64);
1263           assert(ret == 0);
1264           txt.len = (uint)len64;
1265           delete [] txt.val;
1266           txt.val = new char [txt.len];
1267           memset(txt.val, 'X', txt.len);
1268           Uint32 len = txt.len;
1269           ret = bh[i]->readData(txt.val, len);
1270           assert(ret == 0 && len == txt.len);
1271           // to see the data, have to execute...
1272           chkdb(g_con->execute(NoCommit) == 0);
1273           assert(memchr(txt.val, 'X', txt.len) == 0);
1274         }
1275       }
1276       assert(ind >= 0);
1277       d0.ind[i] = ind;
1278     }
1279     assert(r.pk_op[pk1] == 0);
1280     Op* tot_op = r.pk_op[pk1] = getop(Op::OP);
1281     copyop(rec_op, tot_op);
1282     tot_op->type = Op::INS;
1283   }
1284   chkdb(ret == 1);
1285   g_ndb->closeTransaction(g_con);
1286   g_scan_op = 0;
1287   g_con = 0;
1288   freeop(rec_op);
1289   return 0;
1290 }
1291 
1292 static int
scantable()1293 scantable()
1294 {
1295   ll1("scantable");
1296   for (uint i = 0; i < maxrun(); i++)
1297     chkrc(scantable(run(i)) == 0);
1298   return 0;
1299 }
1300 
1301 static void
makedata(const Col & c,Data & d,Uint32 pk1,Op::Type optype)1302 makedata(const Col& c, Data& d, Uint32 pk1, Op::Type optype)
1303 {
1304   uint i = c.no;
1305   if (c.pk) {
1306     switch (c.type) {
1307     case NdbDictionary::Column::Unsigned:
1308       {
1309         Uint32* p = d.ptr[i].u32;
1310         *p = pk1;
1311       }
1312       break;
1313     case NdbDictionary::Column::Char:
1314       {
1315         char* p = d.ptr[i].ch;
1316         sprintf(p, "%-*u", g_charlen, pk1);
1317       }
1318       break;
1319     default:
1320       assert(false);
1321       break;
1322     }
1323     d.ind[i] = 0;
1324   } else if (optype == Op::DEL) {
1325     ;
1326   } else if (i == getcol("seq").no) {
1327     d.seq = g_seq++;
1328     d.ind[i] = 0;
1329   } else if (optype == Op::INS && ! g_opts.no_implicit_nulls && c.nullable && urandom(10, 100)) {
1330     d.noop |= (1 << i);
1331     d.ind[i] = 1; // implicit NULL value is known
1332   } else if (optype == Op::UPD && ! g_opts.no_missing_update && urandom(10, 100)) {
1333     d.noop |= (1 << i);
1334     d.ind[i] = -1; // fixed up in caller
1335   } else if (! g_opts.no_nulls && c.nullable && urandom(10, 100)) {
1336     d.ind[i] = 1;
1337   } else {
1338     switch (c.type) {
1339     case NdbDictionary::Column::Unsigned:
1340       {
1341         Uint32* p = d.ptr[i].u32;
1342         uint u = urandom();
1343         *p = u;
1344       }
1345       break;
1346     case NdbDictionary::Column::Char:
1347       {
1348         char* p = d.ptr[i].ch;
1349         uint u = urandom(g_charlen);
1350         if (u == 0)
1351           u = urandom(g_charlen); // 2x bias for non-empty
1352         uint j;
1353         for (j = 0; j < g_charlen; j++) {
1354           uint v = urandom(strlen(g_charval));
1355           p[j] = j < u ? g_charval[v] : 0x20;
1356         }
1357       }
1358       break;
1359     case NdbDictionary::Column::Text:
1360     case NdbDictionary::Column::Blob:
1361       {
1362         const bool tinyblob = (c.type == NdbDictionary::Column::Blob);
1363         Data::Txt& txt = *d.ptr[i].txt;
1364         delete [] txt.val;
1365         txt.val = 0;
1366         if (g_opts.tweak & 1) {
1367           uint u = g_blobinlinesize + (tinyblob ? 0 : g_blobpartsize);
1368           uint v = (g_opts.tweak & 2) ? 0 : urandom(strlen(g_charval));
1369           txt.val = new char [u];
1370           txt.len = u;
1371           memset(txt.val, g_charval[v], u);
1372           break;
1373         }
1374         uint u = urandom(tinyblob ? g_blobinlinesize : g_maxblobsize);
1375         u = urandom(u); // 4x bias for smaller blobs
1376         u = urandom(u);
1377         txt.val = new char [u];
1378         txt.len = u;
1379         uint j = 0;
1380         while (j < u) {
1381           assert(u > 0);
1382           uint k = 1 + urandom(u - 1);
1383           if (k > u - j)
1384             k = u - j;
1385           uint v = urandom(strlen(g_charval));
1386           memset(&txt.val[j], g_charval[v], k);
1387           j += k;
1388         }
1389       }
1390       break;
1391     default:
1392       assert(false);
1393       break;
1394     }
1395     d.ind[i] = 0;
1396   }
1397 }
1398 
1399 static void
makeop(const Op * prev_op,Op * op,Uint32 pk1,Op::Type optype)1400 makeop(const Op* prev_op, Op* op, Uint32 pk1, Op::Type optype)
1401 {
1402   op->type = optype;
1403   const Data& dp = prev_op->data[0];
1404   Data& d0 = op->data[0];
1405   Data& d1 = op->data[1];
1406   uint i;
1407   for (i = 0; i < ncol(); i++) {
1408     const Col& c = getcol(i);
1409     makedata(c, d0, pk1, optype);
1410     if (optype == Op::INS) {
1411       d1.ind[i] = -1;
1412     } else if (optype == Op::DEL) {
1413       assert(dp.ind[i] >= 0);
1414       if (c.pk)
1415         d1.ind[i] = -1;
1416       else
1417         copycol(c, dp, d1);
1418     } else if (optype == Op::UPD) {
1419       assert(dp.ind[i] >= 0);
1420       if (d0.ind[i] == -1) // not updating this col
1421         copycol(c, dp, d0); // must keep track of data
1422       copycol(c, dp, d1);
1423     } else {
1424       assert(false);
1425     }
1426   }
1427   Uint32 pk1_tmp = ~(Uint32)0;
1428   reqrc(checkop(op, pk1_tmp) == 0);
1429   reqrc(pk1 == pk1_tmp);
1430 }
1431 
1432 static uint
approxblobops(Op * op)1433 approxblobops(Op* op)
1434 {
1435   uint avg_blob_size = g_maxblobsize / 4; // see makedata()
1436   uint avg_blob_ops = avg_blob_size / 2000;
1437   uint n = 0;
1438   if (! g_opts.no_blobs) {
1439     n += avg_blob_ops;
1440     if (! g_opts.one_blob)
1441       n += avg_blob_ops;
1442     if (op->type == Op::UPD)
1443       n *= 2;
1444   }
1445   return n;
1446 }
1447 
1448 static void
makeops(Run & r)1449 makeops(Run& r)
1450 {
1451   ll1("makeops: " << r.tabname);
1452   Uint32 pk1 = 0;
1453   while (1) {
1454     if (g_opts.opstring == 0) {
1455       if (r.tableops + r.blobops >= g_opts.maxops) // use up ops
1456         break;
1457       pk1 = urandom(g_opts.maxpk);
1458     } else {
1459       if (pk1 >= g_opts.maxpk) // use up pks
1460         break;
1461     }
1462     ll2("makeops: pk1=" << pk1);
1463     // total op on the pk so far
1464     // optype either NUL=initial/deleted or INS=created
1465     Op* tot_op = r.pk_op[pk1];
1466     if (tot_op == 0)
1467       tot_op = r.pk_op[pk1] = getop(Op::OP, Op::NUL);
1468     assert(tot_op->type == Op::NUL || tot_op->type == Op::INS);
1469     // add new commit chain to end
1470     Op* last_gci = tot_op;
1471     while (last_gci->next_gci != 0)
1472       last_gci = last_gci->next_gci;
1473     Op* gci_op = getop(Op::OP, Op::NUL);
1474     last_gci->next_gci = gci_op;
1475     Op* com_op = getop(Op::OP, Op::NUL);
1476     gci_op->next_com = com_op;
1477     // length of random chain
1478     uint len = ~0;
1479     if (g_opts.opstring == 0) {
1480       len = 1 + urandom(g_maxcom - 1);
1481       len = 1 + urandom(len - 1); // 2x bias for short chain
1482     }
1483     uint n = 0;
1484     while (1) {
1485       // random or from current g_opts.opstring part
1486       Op::Type optype;
1487       if (g_opts.opstring == 0) {
1488         if (n == len)
1489           break;
1490         do {
1491           optype = (Op::Type)urandom(g_optypes);
1492         } while (tot_op->type == Op::NUL &&
1493                  (optype == Op::DEL || optype == Op::UPD) ||
1494                  tot_op->type == Op::INS && optype == Op::INS);
1495       } else {
1496         const char* str = g_opstringpart[g_loop % g_opstringparts];
1497         uint m = strlen(str);
1498         uint k = tot_op->num_com + tot_op->num_op;
1499         assert(k < m);
1500         char c = str[k];
1501         if (c == 'c') {
1502           if (k + 1 == m)
1503             pk1 += 1;
1504           break;
1505         }
1506         const char* p = "idu";
1507         const char* q = strchr(p, c);
1508         assert(q != 0);
1509         optype = (Op::Type)(q - p);
1510       }
1511       Op* op = getop(Op::OP);
1512       makeop(tot_op, op, pk1, optype);
1513       r.tableops++;
1514       r.blobops += approxblobops(op);
1515       // add to end
1516       Op* last_op = com_op;
1517       while (last_op->next_op != 0)
1518         last_op = last_op->next_op;
1519       last_op->next_op = op;
1520       // merge into chain head and total op
1521       reqrc(compop(com_op, op, com_op) == 0);
1522       reqrc(compop(tot_op, op, tot_op) == 0);
1523       assert(tot_op->type == Op::NUL || tot_op->type == Op::INS);
1524       // counts
1525       com_op->num_op += 1;
1526       tot_op->num_op += 1;
1527       n++;
1528     }
1529     // copy to gci level
1530     copyop(com_op, gci_op);
1531     tot_op->num_com += 1;
1532     r.gciops += 1;
1533     g_gciops += 1;
1534   }
1535   ll1("makeops: " << r.tabname << ": com recs = " << r.gciops);
1536 }
1537 
1538 static void
selecttables()1539 selecttables()
1540 {
1541   uint i;
1542   for (i = 0; i < maxrun(); i++)
1543     run(i).skip = false;
1544   if (g_opts.opstring != 0) {
1545     ll1("using all tables due to fixed ops");
1546     return;
1547   }
1548   for (i = 0; i + 1 < maxrun(); i++)
1549     run(urandom(maxrun())).skip = true;
1550   uint cnt = 0;
1551   for (i = 0; i < maxrun(); i++) {
1552     if (! run(i).skip) {
1553       ll2("use table " << run(i).tabname);
1554       cnt++;
1555     }
1556   }
1557   ll1("use " << cnt << "/" << maxrun() << " tables in this loop");
1558 }
1559 
1560 static void
makeops()1561 makeops()
1562 {
1563   selecttables();
1564   for (uint i = 0; i < maxrun(); i++)
1565     if (! run(i).skip)
1566       makeops(run(i));
1567   ll1("makeops: used recs = " << g_usedops << " com recs = " << g_gciops);
1568 }
1569 
1570 static int
addndbop(Run & r,Op * op)1571 addndbop(Run& r, Op* op)
1572 {
1573   chkdb((g_op = g_con->getNdbOperation(r.tabname)) != 0);
1574   switch (op->type) {
1575   case Op::INS:
1576     chkdb(g_op->insertTuple() == 0);
1577     break;
1578   case Op::DEL:
1579     chkdb(g_op->deleteTuple() == 0);
1580     break;
1581   case Op::UPD:
1582     chkdb(g_op->updateTuple() == 0);
1583     break;
1584   default:
1585     assert(false);
1586     break;
1587   }
1588   uint i;
1589   for (i = 0; i < ncol(); i++) {
1590     const Col& c = getcol(i);
1591     const Data& d = op->data[0];
1592     if (! c.pk)
1593       continue;
1594     chkdb(g_op->equal(c.name, (const char*)d.ptr[i].v) == 0);
1595   }
1596   if (op->type != Op::DEL) {
1597     for (i = 0; i < ncol(); i++) {
1598       const Col& c = getcol(i);
1599       const Data& d = op->data[0];
1600       if (c.pk)
1601         continue;
1602       if (d.noop & (1 << i))
1603         continue;
1604       assert(d.ind[i] >= 0);
1605       if (! c.isblob()) {
1606         if (d.ind[i] == 0)
1607           chkdb(g_op->setValue(c.name, (const char*)d.ptr[i].v) == 0);
1608         else
1609           chkdb(g_op->setValue(c.name, (const char*)0) == 0);
1610       } else {
1611         const Data::Txt& txt = *d.ptr[i].txt;
1612         g_bh = g_op->getBlobHandle(c.name);
1613         if (d.ind[i] == 0)
1614           chkdb(g_bh->setValue(txt.val, txt.len) == 0);
1615         else
1616           chkdb(g_bh->setValue(0, 0) == 0);
1617         g_bh = 0;
1618       }
1619     }
1620   }
1621   g_op = 0;
1622   return 0;
1623 }
1624 
1625 static int
runops()1626 runops()
1627 {
1628   ll1("runops");
1629   Op* gci_op[g_maxtab][g_maxpk];
1630   uint left = 0; // number of table pks with ops
1631   int i;
1632   Uint32 pk1;
1633   for (i = 0; i < maxrun(); i++) {
1634     Run& r = run(i);
1635     for (pk1 = 0; pk1 < g_opts.maxpk; pk1++) {
1636       gci_op[i][pk1] = 0;
1637       // total op on the pk
1638       Op* tot_op = r.pk_op[pk1];
1639       if (tot_op == 0)
1640         continue;
1641       if (tot_op->next_gci == 0) {
1642         assert(g_loop != 0 && tot_op->type == Op::INS);
1643         continue;
1644       }
1645       // first commit chain
1646       assert(tot_op->next_gci != 0);
1647       gci_op[i][pk1] = tot_op->next_gci;
1648       left++;
1649     }
1650   }
1651   while (left != 0) {
1652     i = urandom(maxrun());
1653     pk1 = urandom(g_opts.maxpk);
1654     if (gci_op[i][pk1] == 0)
1655       continue;
1656     Run& r = run(i);
1657     // do the ops in one transaction
1658     chkdb((g_con = g_ndb->startTransaction()) != 0);
1659     Op* com_op = gci_op[i][pk1]->next_com;
1660     assert(com_op != 0);
1661     // first op in chain
1662     Op* op = com_op->next_op;
1663     assert(op != 0);
1664     while (op != 0) {
1665       ll2("runops:" << *op);
1666       chkrc(addndbop(r, op) == 0);
1667       op = op->next_op;
1668     }
1669     chkdb(g_con->execute(Commit) == 0);
1670     gci_op[i][pk1]->gci = com_op->gci = g_con->getGCI();
1671     ll2("commit: " << run(i).tabname << " gci=" << com_op->gci);
1672     g_ndb->closeTransaction(g_con);
1673     g_con = 0;
1674     // next chain
1675     gci_op[i][pk1] = gci_op[i][pk1]->next_gci;
1676     if (gci_op[i][pk1] == 0) {
1677       assert(left != 0);
1678       left--;
1679     }
1680   }
1681   assert(left == 0);
1682   return 0;
1683 }
1684 
1685 // move com chains with same gci under same gci entry
1686 static void
mergeops(Run & r)1687 mergeops(Run& r)
1688 {
1689   ll2("mergeops: " << r.tabname);
1690   uint mergecnt = 0;
1691   Uint32 pk1;
1692   for (pk1 = 0; pk1 < g_opts.maxpk; pk1++) {
1693     Op* tot_op = r.pk_op[pk1];
1694     if (tot_op == 0)
1695       continue;
1696     Op* gci_op = tot_op->next_gci;
1697     if (gci_op == 0) {
1698       assert(g_loop != 0 && tot_op->type == Op::INS);
1699       continue;
1700     }
1701     while (gci_op != 0) {
1702       Op* com_op = gci_op->next_com;
1703       assert(com_op != 0 && com_op->next_com == 0);
1704       assert(gci_op->gci == com_op->gci);
1705       Op* last_com = com_op;
1706       Op* gci_op2 = gci_op->next_gci;
1707       while (gci_op2 != 0 && gci_op->gci == gci_op2->gci) {
1708         // move link to com level
1709         last_com = last_com->next_com = gci_op2->next_com;
1710         // merge to gci
1711         reqrc(compop(gci_op, gci_op2, gci_op) == 0);
1712         // move to next and discard
1713         Op* tmp_op = gci_op2;
1714         gci_op2 = gci_op2->next_gci;
1715         freeop(tmp_op);
1716         mergecnt++;
1717         assert(r.gciops != 0 && g_gciops != 0);
1718         r.gciops--;
1719         g_gciops--;
1720       }
1721       gci_op = gci_op->next_gci = gci_op2;
1722     }
1723   }
1724   ll1("mergeops: " << r.tabname << ": gci recs = " << r.gciops);
1725 }
1726 
1727 static void
mergeops()1728 mergeops()
1729 {
1730   for (uint i = 0; i < maxrun(); i++)
1731     mergeops(run(i));
1732   ll1("mergeops: used recs = " << g_usedops << " gci recs = " << g_gciops);
1733 }
1734 
1735 // set bit for equal post/pre data in UPD, for use in event match
1736 static void
cmppostpre(Run & r)1737 cmppostpre(Run& r)
1738 {
1739   ll2("cmppostpre: " << r.tabname);
1740   Uint32 pk1;
1741   for (pk1 = 0; pk1 < g_opts.maxpk; pk1++) {
1742     Op* tot_op = r.pk_op[pk1];
1743     Op* gci_op = tot_op ? tot_op->next_gci : 0;
1744     while (gci_op != 0) {
1745       if (gci_op->type == Op::UPD) {
1746         Data (&d)[2] = gci_op->data;
1747         uint i;
1748         for (i = 0; i < ncol(); i++) {
1749           const Col& c = getcol(i);
1750           bool eq =
1751             d[0].ind[i] == 1 && d[1].ind[i] == 1 ||
1752             d[0].ind[i] == 0 && d[1].ind[i] == 0 && cmpcol(c, d[0], d[1]) == 0;
1753           if (eq) {
1754             d[0].ppeq |= (1 << i);
1755             d[1].ppeq |= (1 << i);
1756           }
1757         }
1758       }
1759       gci_op = gci_op->next_gci;
1760     }
1761   }
1762 }
1763 
1764 static void
cmppostpre()1765 cmppostpre()
1766 {
1767   ll1("cmppostpre");
1768   for (uint i = 0; i < maxrun(); i++)
1769     cmppostpre(run(i));
1770 }
1771 
1772 static int
findevent(const NdbEventOperation * evt_op)1773 findevent(const NdbEventOperation* evt_op)
1774 {
1775   uint i;
1776   for (i = 0; i < maxrun(); i++) {
1777     if (run(i).evt_op == evt_op)
1778       break;
1779   }
1780   chkrc(i < maxrun());
1781   return i;
1782 }
1783 
1784 static void
geteventdata(Run & r)1785 geteventdata(Run& r)
1786 {
1787   Data (&d)[2] = g_rec_ev->data;
1788   int i, j;
1789   for (j = 0; j < 2; j++) {
1790     for (i = 0; i < ncol(); i++) {
1791       const Col& c = getcol(i);
1792       int ind, ret;
1793       if (! c.isblob()) {
1794         NdbRecAttr* ra = r.ev_ra[j][i];
1795         ind = ra->isNULL();
1796       } else {
1797         NdbBlob* bh = r.ev_bh[j][i];
1798         ret = bh->getDefined(ind);
1799         assert(ret == 0);
1800         if (ind == 0) { // value was returned and is not NULL
1801           Data::Txt& txt = *d[j].ptr[i].txt;
1802           Uint64 len64;
1803           ret = bh->getLength(len64);
1804           assert(ret == 0);
1805           txt.len = (uint)len64;
1806           delete [] txt.val;
1807           txt.val = new char [txt.len];
1808           memset(txt.val, 'X', txt.len);
1809           Uint32 len = txt.len;
1810           ret = bh->readData(txt.val, len);
1811           assert(ret == 0 && len == txt.len);
1812         }
1813       }
1814       d[j].ind[i] = ind;
1815     }
1816   }
1817 }
1818 
1819 static int
addgcievents(Uint64 gci)1820 addgcievents(Uint64 gci)
1821 {
1822   ll1("getgcieventops");
1823   uint count = 0;
1824   uint seen_current = 0;
1825   Uint32 iter = 0;
1826   while (1) {
1827     Uint32 evtypes = 0;
1828     const NdbEventOperation* evt_op =
1829       g_ndb->getGCIEventOperations(&iter, &evtypes);
1830     if (evt_op == 0)
1831       break;
1832     // evt_op->getGCI() is not defined yet
1833     int i;
1834     chkrc((i = findevent(evt_op)) != -1);
1835     run(i).addevtypes(gci, evtypes, 0);
1836     seen_current += (g_evt_op == evt_op);
1837     count++;
1838   }
1839   chkrc(seen_current == 1);
1840   ll1("addgcievents: " << count);
1841   return 0;
1842 }
1843 
1844 static int
runevents()1845 runevents()
1846 {
1847   ll1("runevents");
1848   uint mspoll = 1000;
1849   uint npoll = 6; // strangely long delay
1850   ll1("poll " << npoll);
1851   Uint64 gci = (Uint64)0;
1852   while (npoll != 0) {
1853     npoll--;
1854     int ret;
1855     ret = g_ndb->pollEvents(mspoll);
1856     if (ret <= 0)
1857       continue;
1858     while (1) {
1859       g_rec_ev->init(Op::EV);
1860       g_evt_op = g_ndb->nextEvent();
1861       if (g_evt_op == 0)
1862         break;
1863       Uint64 newgci = g_evt_op->getGCI();
1864       assert(newgci != 0);
1865       g_rec_ev->gci = newgci;
1866       if (gci != newgci) {
1867         ll1("new gci: " << gci << " -> " << newgci);
1868         gci = newgci;
1869         // add slot in each tab|e
1870         uint i;
1871         for (i = 0; i < maxtab(); i++)
1872           chkrc(run(i).addgci(gci) == 0);
1873         chkrc(addgcievents(gci) == 0);
1874       }
1875       int i;
1876       chkrc((i = findevent(g_evt_op)) != -1);
1877       Run& r = run(i);
1878       NdbDictionary::Event::TableEvent evtype = g_evt_op->getEventType();
1879       chkrc(seteventtype(g_rec_ev, evtype) == 0);
1880       r.addevtypes(gci, (Uint32)evtype, 1);
1881       geteventdata(r);
1882       ll2("runevents: EVT: " << *g_rec_ev);
1883       // check basic sanity
1884       Uint32 pk1 = ~(Uint32)0;
1885       chkrc(checkop(g_rec_ev, pk1) == 0);
1886       // add to events
1887       Op* tot_ev = r.pk_ev[pk1];
1888       if (tot_ev == 0)
1889         tot_ev = r.pk_ev[pk1] = getop(Op::EV);
1890       Op* last_ev = tot_ev;
1891       while (last_ev->next_ev != 0)
1892         last_ev = last_ev->next_ev;
1893       // copy and add
1894       Op* ev = getop(Op::EV);
1895       copyop(g_rec_ev, ev);
1896       g_rec_ev->freemem();
1897       last_ev->next_ev = ev;
1898       g_num_ev++;
1899     }
1900   }
1901   ll1("runevents: used ops = " << g_usedops << " events = " << g_num_ev);
1902   return 0;
1903 }
1904 
1905 static int
cmpopevdata(const Data & d1,const Data & d2)1906 cmpopevdata(const Data& d1, const Data& d2)
1907 {
1908   uint i;
1909   for (i = 0; i < ncol(); i++) {
1910     const Col& c = getcol(i);
1911     if (cmpcol(c, d1, d2) != 0) {
1912       if ((d1.ppeq & (1 << i)) && d2.ind[i] == -1)
1913         ; // post/pre data equal and no event data returned is OK
1914       else
1915         return 1;
1916     }
1917   }
1918   return 0;
1919 }
1920 
1921 // compare operation to event data
1922 static int
cmpopevdata(const Data (& d1)[2],const Data (& d2)[2])1923 cmpopevdata(const Data (&d1)[2], const Data (&d2)[2])
1924 {
1925   if (cmpopevdata(d1[0], d2[0]) != 0)
1926     return 1;
1927   if (cmpopevdata(d1[1], d2[1]) != 0)
1928     return 1;
1929   return 0;
1930 }
1931 
1932 static int
matchevent(Run & r,Op * ev)1933 matchevent(Run& r, Op* ev)
1934 {
1935   Data (&d2)[2] = ev->data;
1936   // get PK
1937   Uint32 pk1 = d2[0].pk1;
1938   chkrc(pk1 < g_opts.maxpk);
1939   // on error repeat and print details
1940   uint loop = 0;
1941   while (loop <= 1) {
1942     int g_loglevel = loop == 0 ? g_opts.loglevel : 2;
1943     ll1("matchevent: " << r.tabname << ": pk1=" << pk1 << " type=" << ev->type);
1944     ll2("EVT: " << *ev);
1945     Op* tot_op = r.pk_op[pk1];
1946     Op* gci_op = tot_op ? tot_op->next_gci : 0;
1947     uint pos = 0;
1948     bool ok = false;
1949     while (gci_op != 0) {
1950       ll2("GCI: " << *gci_op);
1951       // print details
1952       Op* com_op = gci_op->next_com;
1953       assert(com_op != 0);
1954       while (com_op != 0) {
1955         ll2("COM: " << *com_op);
1956         Op* op = com_op->next_op;
1957         assert(op != 0);
1958         while (op != 0) {
1959           ll2("OP : " << *op);
1960           op = op->next_op;
1961         }
1962         com_op = com_op->next_com;
1963       }
1964       // match against GCI op
1965       if (gci_op->type != Op::NUL) {
1966         const Data (&d1)[2] = gci_op->data;
1967         if (cmpopevdata(d1, d2) == 0) {
1968           bool tmpok = true;
1969           if (gci_op->type != ev->type) {
1970             ll2("***: wrong type " << gci_op->type << " != " << ev->type);
1971             tmpok = false;
1972           }
1973           if (gci_op->match) {
1974             ll2("***: duplicate match");
1975             tmpok = false;
1976           }
1977           if (pos != r.ev_pos[pk1]) {
1978             ll2("***: wrong pos " << pos << " != " << r.ev_pos[pk1]);
1979             tmpok = false;
1980           }
1981           if (gci_op->gci != ev->gci) {
1982             ll2("***: wrong gci " << gci_op->gci << " != " << ev->gci);
1983             tmpok = false;
1984           }
1985           if (tmpok) {
1986             ok = gci_op->match = true;
1987             ll2("match");
1988           }
1989         }
1990         pos++;
1991       }
1992       gci_op = gci_op->next_gci;
1993     }
1994     if (ok) {
1995       ll2("matchevent: match");
1996       return 0;
1997     }
1998     ll0("matchevent: ERROR: no match");
1999     if (g_loglevel >= 2)
2000       return -1;
2001     loop++;
2002   }
2003   return 0;
2004 }
2005 
2006 static int
matchevents(Run & r)2007 matchevents(Run& r)
2008 {
2009   ll1("matchevents: " << r.tabname);
2010   uint nomatch = 0;
2011   Uint32 pk1;
2012   for (pk1 = 0; pk1 < g_opts.maxpk; pk1++) {
2013     Op* tot_ev = r.pk_ev[pk1];
2014     if (tot_ev == 0)
2015       continue;
2016     Op* ev = tot_ev->next_ev;
2017     while (ev != 0) {
2018       if (matchevent(r, ev) < 0)
2019         nomatch++;
2020       r.ev_pos[pk1]++;
2021       ev = ev->next_ev;
2022     }
2023   }
2024   chkrc(nomatch == 0);
2025   return 0;
2026 }
2027 
2028 static int
matchevents()2029 matchevents()
2030 {
2031   ll1("matchevents");
2032   for (uint i = 0; i < maxrun(); i++)
2033     chkrc(matchevents(run(i)) == 0);
2034   return 0;
2035 }
2036 
2037 static int
matchops(Run & r)2038 matchops(Run& r)
2039 {
2040   ll1("matchops: " << r.tabname);
2041   uint nomatch = 0;
2042   Uint32 pk1;
2043   for (pk1 = 0; pk1 < g_opts.maxpk; pk1++) {
2044     Op* tot_op = r.pk_op[pk1];
2045     if (tot_op == 0)
2046       continue;
2047     Op* gci_op = tot_op->next_gci;
2048     while (gci_op != 0) {
2049       if (gci_op->type == Op::NUL) {
2050         ll2("GCI: " << *gci_op << " [skip NUL]");
2051       } else if (gci_op->match) {
2052         ll2("GCI: " << *gci_op << " [match OK]");
2053       } else {
2054         ll0("GCI: " << *gci_op);
2055         Op* com_op = gci_op->next_com;
2056         assert(com_op != 0);
2057         ll0("COM: " << *com_op);
2058         Op* op = com_op->next_op;
2059         assert(op != 0);
2060         while (op != 0) {
2061           ll0("OP : " << *op);
2062           op = op->next_op;
2063         }
2064         ll0("no matching event");
2065         nomatch++;
2066       }
2067       gci_op = gci_op->next_gci;
2068     }
2069   }
2070   chkrc(nomatch == 0);
2071   return 0;
2072 }
2073 
2074 static int
matchops()2075 matchops()
2076 {
2077   ll1("matchops");
2078   for (uint i = 0; i < maxrun(); i++)
2079     chkrc(matchops(run(i)) == 0);
2080   return 0;
2081 }
2082 
2083 static int
matchgcievents(Run & r)2084 matchgcievents(Run& r)
2085 {
2086   ll1("matchgcievents: " << r.tabname);
2087   uint i;
2088   for (i = 0; i < r.gcicnt; i++) {
2089     Uint32 t0 = r.gcievtypes[i][0];
2090     Uint32 t1 = r.gcievtypes[i][1];
2091     ll1("gci: " << r.gcinum[i] << hex << " report: " << t0 << " seen: " << t1);
2092 
2093     if (r.skip)
2094       chkrc(t0 == 0 && t1 == 0);
2095     if (t0 == 0 && t1 == 0)
2096       continue;
2097 
2098     // check if not reported event op seen
2099     chkrc(t0 != 0);
2100     // check if not reported event type seen
2101     chkrc((~t0 & t1) == 0);
2102 
2103     // the other way does not work under merge
2104     if (g_opts.separate_events) {
2105       // check if reported event op not seen
2106       chkrc(t1 != 0);
2107       // check if reported event type not seen
2108       chkrc((t0 & ~t1) == 0);
2109     }
2110   }
2111   return 0;
2112 }
2113 
2114 static int
matchgcievents()2115 matchgcievents()
2116 {
2117   ll1("matchgcievents");
2118   for (uint i = 0; i < maxrun(); i++)
2119     chkrc(matchgcievents(run(i)) == 0);
2120   return 0;
2121 }
2122 
2123 static void
setseed(int n)2124 setseed(int n)
2125 {
2126   uint seed;
2127   if (n == -1) {
2128     if (g_opts.seed == 0)
2129       return;
2130     if (g_opts.seed != -1)
2131       seed = (uint)g_opts.seed;
2132     else
2133       seed = 1 + (ushort)getpid();
2134   } else {
2135     if (g_opts.seed != 0)
2136       return;
2137     seed = n;
2138   }
2139   ll0("seed=" << seed);
2140   srandom(seed);
2141 }
2142 
2143 static int
runtest()2144 runtest()
2145 {
2146   setseed(-1);
2147   initrun();
2148   chkrc(createtables() == 0);
2149   chkrc(createevents() == 0);
2150   for (g_loop = 0; g_opts.loop == 0 || g_loop < g_opts.loop; g_loop++) {
2151     ll0("=== loop " << g_loop << " ===");
2152     setseed(g_loop);
2153     resetmem();
2154     chkrc(scantable() == 0); // alternative: save tot_op for loop > 0
2155     makeops();
2156     g_rec_ev = getop(Op::EV);
2157     chkrc(createeventop() == 0);
2158     chkrc(executeeventop() == 0);
2159     chkrc(waitgci(3) == 0);
2160     chkrc(runops() == 0);
2161     if (! g_opts.separate_events)
2162       mergeops();
2163     cmppostpre();
2164     chkrc(runevents() == 0);
2165     ll0("counts: gci ops = " << g_gciops << " ev ops = " << g_num_ev);
2166     chkrc(matchevents() == 0);
2167     chkrc(matchops() == 0);
2168     chkrc(matchgcievents() == 0);
2169     chkrc(dropeventops() == 0);
2170     // time erases everything..
2171     chkrc(waitgci(1) == 0);
2172   }
2173   chkrc(dropevents() == 0);
2174   chkrc(droptables() == 0);
2175   resetmem();
2176   deleteops();
2177   return 0;
2178 }
2179 
2180 NDB_STD_OPTS_VARS;
2181 
2182 static struct my_option
2183 my_long_options[] =
2184 {
2185   NDB_STD_OPTS("test_event_merge"),
2186   { "abort-on-error", 1001, "Do abort() on any error",
2187     &g_opts.abort_on_error, &g_opts.abort_on_error, 0,
2188     GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
2189   { "loglevel", 1002, "Logging level in this program 0-3 (default 0)",
2190     &g_opts.loglevel, &g_opts.loglevel, 0,
2191     GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
2192   { "loop", 1003, "Number of test loops (default 5, 0=forever)",
2193     &g_opts.loop, &g_opts.loop, 0,
2194     GET_INT, REQUIRED_ARG, 5, 0, 0, 0, 0, 0 },
2195   { "maxops", 1004, "Approx number of PK operations per table (default 1000)",
2196     &g_opts.maxops, &g_opts.maxops, 0,
2197     GET_UINT, REQUIRED_ARG, 1000, 0, 0, 0, 0, 0 },
2198   { "maxpk", 1005, "Number of different PK values (default 10, max 1000)",
2199     &g_opts.maxpk, &g_opts.maxpk, 0,
2200     GET_UINT, REQUIRED_ARG, 10, 0, 0, 0, 0, 0 },
2201   { "maxtab", 1006, "Number of tables (default 10, max 100)",
2202     &g_opts.maxtab, &g_opts.maxtab, 0,
2203     GET_INT, REQUIRED_ARG, 10, 0, 0, 0, 0, 0 },
2204   { "no-blobs", 1007, "Omit blob attributes (5.0: true)",
2205     &g_opts.no_blobs, &g_opts.no_blobs, 0,
2206     GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
2207   { "no-implicit-nulls", 1008, "Insert must include all attrs"
2208                                " i.e. no implicit NULLs",
2209     &g_opts.no_implicit_nulls, &g_opts.no_implicit_nulls, 0,
2210     GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
2211   { "no-missing-update", 1009, "Update must include all non-PK attrs",
2212     &g_opts.no_missing_update, &g_opts.no_missing_update, 0,
2213     GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
2214   { "no-multiops", 1010, "Allow only 1 operation per commit",
2215     &g_opts.no_multiops, &g_opts.no_multiops, 0,
2216     GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
2217   { "no-nulls", 1011, "Create no NULL values",
2218     &g_opts.no_nulls, &g_opts.no_nulls, 0,
2219     GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
2220   { "one-blob", 1012, "Only one blob attribute (default 2)",
2221     &g_opts.one_blob, &g_opts.one_blob, 0,
2222     GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
2223   { "opstring", 1013, "Operations to run e.g. idiucdc (c is commit) or"
2224                       " iuuc:uudc (the : separates loops)",
2225     &g_opts.opstring, &g_opts.opstring, 0,
2226     GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
2227   { "seed", 1014, "Random seed (0=loop number, default -1=random)",
2228     &g_opts.seed, &g_opts.seed, 0,
2229     GET_INT, REQUIRED_ARG, -1, 0, 0, 0, 0, 0 },
2230   { "separate-events", 1015, "Do not combine events per GCI (5.0: true)",
2231     &g_opts.separate_events, &g_opts.separate_events, 0,
2232     GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
2233   { "tweak", 1016, "Whatever the source says",
2234     &g_opts.tweak, &g_opts.tweak, 0,
2235     GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
2236   { "use-table", 1017, "Use existing tables",
2237     &g_opts.use_table, &g_opts.use_table, 0,
2238     GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
2239   { 0, 0, 0,
2240     0, 0, 0,
2241     GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 }
2242 };
2243 
2244 static void
usage()2245 usage()
2246 {
2247   my_print_help(my_long_options);
2248 }
2249 
2250 static int
checkopts()2251 checkopts()
2252 {
2253   if (g_opts.separate_events) {
2254     g_opts.no_blobs = true;
2255   }
2256   if (g_opts.no_multiops) {
2257     g_maxcom = 1;
2258   }
2259   if (g_opts.opstring != 0) {
2260     uint len = strlen(g_opts.opstring);
2261     char* str = new char [len + 1];
2262     memcpy(str, g_opts.opstring, len + 1);
2263     char* s = str;
2264     while (1) {
2265       g_opstringpart[g_opstringparts++] = s;
2266       s = strchr(s, ':');
2267       if (s == 0)
2268         break;
2269       *s++ = 0;
2270     }
2271     uint i;
2272     for (i = 0; i < g_opstringparts; i++) {
2273       const char* s = g_opstringpart[i];
2274       while (*s != 0) {
2275         if (strchr("iduc", *s++) == 0) {
2276           ll0("opstring chars are i,d,u,c");
2277           return -1;
2278         }
2279       }
2280       if (s == g_opstringpart[i] || s[-1] != 'c') {
2281         ll0("opstring chain must end in 'c'");
2282         return -1;
2283       }
2284     }
2285   }
2286   if (g_opts.no_nulls) {
2287     g_opts.no_implicit_nulls = true;
2288   }
2289   if (g_opts.maxpk > g_maxpk ||
2290       g_opts.maxtab > g_maxtab) {
2291     return -1;
2292   }
2293   return 0;
2294 }
2295 
2296 static int
doconnect()2297 doconnect()
2298 {
2299   g_ncc = new Ndb_cluster_connection();
2300   chkdb(g_ncc->connect(30) == 0);
2301   g_ndb = new Ndb(g_ncc, "TEST_DB");
2302   chkdb(g_ndb->init() == 0 && g_ndb->waitUntilReady(30) == 0);
2303   return 0;
2304 }
2305 
2306 int
main(int argc,char ** argv)2307 main(int argc, char** argv)
2308 {
2309   ndb_init();
2310   const char* progname =
2311     strchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
2312   uint i;
2313   ndbout << progname;
2314   for (i = 1; i < argc; i++)
2315     ndbout << " " << argv[i];
2316   ndbout << endl;
2317   int ret;
2318   ret = handle_options(&argc, &argv, my_long_options, ndb_std_get_one_option);
2319   if (ret != 0 || argc != 0 || checkopts() != 0)
2320     return NDBT_ProgramExit(NDBT_WRONGARGS);
2321   if (doconnect() == 0 && runtest() == 0) {
2322     delete g_ndb;
2323     delete g_ncc;
2324     return NDBT_ProgramExit(NDBT_OK);
2325   }
2326   dropeventops(true);
2327   dropevents(true);
2328   delete g_ndb;
2329   delete g_ncc;
2330   return NDBT_ProgramExit(NDBT_FAILED);
2331 }
2332