testDataBuffers.cpp 16.2 KB
Newer Older
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
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/*
 * testDataBuffers
 *
 * Test getValue() of byte arrays:
 * - using application buffers of different alignments and sizes
 * - using NdbApi allocated small (<32) and big (>=32) buffers
 *
 * Verifies fixes to tickets 189 and 206.
 *
 * Options: see printusage() below.
 *
 * Creates tables TB00 to TB15
 */

31 32
#include <ndb_global.h>

33 34 35 36
#include <NdbMain.h>
#include <NdbOut.hpp>
#include <NdbApi.hpp>
#include <NdbTest.hpp>
37
#include <NdbSchemaCon.hpp>
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
// limits
static int const MaxAttr = 64;
static int const MaxOper = 1000;
static int const MaxSize = 10000;
static int const MaxOff = 64;		// max offset to add to data buffer
static int const MaxData = MaxSize + MaxOff + 100;

// options
static int attrcnt = 25;
static int existok = 0;
static bool kontinue = false;
static int loopcnt = 1;
static int opercnt = 100;		// also does this many scans
static int randomizer = 171317;
static int sizelim = 500;
static int xverbose = 0;

static void printusage() {
    ndbout
	<< "usage: testDataBuffers options [default/max]"
	<< endl
	<< "NOTE: too large combinations result in NDB error"
	<< endl
	<< "-a N  number of attributes (including the key) [25/64]"
	<< endl
	<< "-e    no error if table exists (assumed to have same structure)"
	<< endl
	<< "-k    on error continue with next test case"
	<< endl
	<< "-l N  number of loops to run, 0 means infinite [1]"
	<< endl
	<< "-o N  number of operations (rows in each table) [100/1000]"
	<< endl
	<< "-r N  source of randomness (big number (prime)) [171317]"
	<< endl
	<< "-s N  array size limit (rounded up in some tests) [500/10000]"
	<< endl
	<< "-x    extremely verbose"
	<< endl
	<< "Tables: TB00 .. TB15"
	<< endl
	;
}

static Ndb* ndb = 0;
static NdbSchemaCon* tcon = 0;
static NdbSchemaOp* top = 0;
static NdbConnection* con = 0;
static NdbOperation* op = 0;
unknown's avatar
unknown committed
87 88
static NdbScanOperation* sop = 0;
static NdbResultSet* rs = 0;
89 90 91 92 93 94 95

static int
ndberror(char const* fmt, ...)
{
    va_list ap;
    char buf[200];
    va_start(ap, fmt);
96
    BaseString::vsnprintf(buf, sizeof(buf), fmt, ap);
97 98 99 100 101 102 103
    va_end(ap);
    ndbout << buf << " --" << endl;
    if (ndb)
      ndbout << "ndb : " << ndb->getNdbError() << endl;
    if (tcon)
	ndbout << "tcon: " << tcon->getNdbError() << endl;
    if (top)
104
	ndbout << "top: " << top->getNdbError() << endl;
105 106 107 108 109 110 111 112 113 114 115 116 117
    if (con)
	ndbout << "con : " << con->getNdbError() << endl;
    if (op)
	ndbout << "op  : " << op->getNdbError() << endl;
    return -1;
}

static int
chkerror(char const* fmt, ...)
{
    va_list ap;
    char buf[200];
    va_start(ap, fmt);
118
    BaseString::vsnprintf(buf, sizeof(buf), fmt, ap);
119 120 121 122 123 124 125
    va_end(ap);
    ndbout << "*** check failed: " << buf << " ***" << endl;
    return -1;
}

// alignment of addresses and data sizes

126
static bool isAligned(UintPtr x)
127 128 129 130 131
{
    return ((x & 3) == 0);
}
static bool isAligned(char* p)
{
132
    return isAligned(UintPtr(p));
133
}
134
static unsigned toAligned(UintPtr x)
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
{
    while (! isAligned(x))
	x++;
    return x;
}
static char* toAligned(char* p)
{
    while (! isAligned(p))
	p++;
    return p;
}

// byte value for key k column i byte j
static int byteVal(int k, int i, int j)
{
    return '0' + (k + i + j)  % 10;
}

// tables

static char tab[20] = "";

static struct col {
    char aAttrName[20];
    AttrType aAttrType;
    int aAttrSize;
    int aArraySize;
    KeyType aTupleKey;
    bool nullable;
    NdbRecAttr* aRa;
    char* buf;
    int bufsiz;
    char data[MaxData];
} ccol[MaxAttr];

static int key = 0;

// independent test bits
static bool alignAddr;		// align our buffer addresses to 4x
static bool alignSize;		// align data sizes to 4x
static bool useBuf;		// use our buffers for output
static bool noRandom;		// do not randomize sizes and offsets
static int testbits = 4;

static int
makeSize(int i)
{
    int n;
    if (noRandom)
	n = i;
    else
	n = i * randomizer;
    n %= sizelim;
    if (n <= 0)
	n = 1;
    if (alignSize)
	n = toAligned(n);
    return n;
}

static int
makeOff(int k)
{
    int n;
    if (alignAddr)
	n = 0;
    else if (noRandom)
	n = k;
    else
	n = k * randomizer;
    n %= MaxOff;
    if (n < 0)
	n = -n;
    return n;
}

static int
212
testcase(Ndb_cluster_connection&cc, int flag)
213 214 215 216 217 218 219 220 221 222 223 224 225
{
    ndbout << "--- case " << flag << " ---" << endl;
    sprintf(tab, "TB%02d", flag);

    alignAddr = ! (flag & 1);
    ndbout << (alignAddr ? "align addresses" : "mis-align addresses") << endl;
    alignSize = ! (flag & 2);
    ndbout << (alignSize ? "align data sizes" : "mis-align data sizes") << endl;
    useBuf = ! (flag & 4);
    ndbout << (useBuf ? "use our buffers" : "use ndbapi buffers") << endl;
    noRandom = ! (flag & 8);
    ndbout << (noRandom ? "simple sizes" : "randomize sizes") << endl;

226
    int smax = 0, stot = 0, i;
227 228
    if (xverbose)
      ndbout << "- define table " << tab << endl;
229
    for (i = 0; i < attrcnt; i++) {
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
	col& c = ccol[i];
	memset(&c, 0, sizeof(c));
	sprintf(c.aAttrName, "C%d", i);
	if (i == 0) {
	    c.aAttrType = UnSigned;
	    c.aAttrSize = 32;
	    c.aArraySize = 1;
	    c.aTupleKey = TupleKey;
	    c.nullable = false;
	} else {
	    c.aAttrType = String;
	    c.aAttrSize = 8;
	    c.aArraySize = makeSize(i);
	    if (smax < c.aArraySize)
		smax = c.aArraySize;
	    stot += c.aArraySize;
	    c.aTupleKey = NoKey;
	    c.nullable = true;
	    if (xverbose)
	      ndbout << "-- column " << i << " size=" << c.aArraySize << endl;
	}
	c.buf = toAligned(c.data);
	c.bufsiz = sizeof(c.data) - (c.buf - c.data);
    }
    ndbout << "tab=" << tab << " cols=" << attrcnt
	<< " size max=" << smax << " tot=" << stot << endl;

257
    ndb = new Ndb(&cc, "TEST_DB");
258 259 260 261 262
    if (ndb->init() != 0)
	return ndberror("init");
    if (ndb->waitUntilReady(30) < 0)
	return ndberror("waitUntilReady");

263
    if ((tcon = NdbSchemaCon::startSchemaTrans(ndb)) == 0)
264 265 266 267 268
	return ndberror("startSchemaTransaction");
    if ((top = tcon->getNdbSchemaOp()) == 0)
	return ndberror("getNdbSchemaOp");
    if (top->createTable(tab) < 0)
	return ndberror("createTable");
269
    for (i = 0; i < attrcnt; i++) {
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
	col& c = ccol[i];
	if (top->createAttribute(
	    c.aAttrName,
	    c.aTupleKey,
	    c.aAttrSize,
	    c.aArraySize,
	    c.aAttrType,
	    MMBased,
	    c.nullable
	) < 0)
	    return ndberror("createAttribute col=%d", i);
    }
    if (tcon->execute() < 0) {
	if (! (tcon->getNdbError().code == 721 && existok))
	    return ndberror("execute");
	ndbout << "using " << tab << endl;
    } else {
	ndbout << "created " << tab << endl;
    }
    top = 0;
    tcon = 0;

    if (xverbose)
      ndbout << "- delete" << endl;
    int delcnt = 0;
    for (key = 0; key < opercnt; key++) {
	if ((con = ndb->startTransaction()) == 0)
	    return ndberror("startTransaction key=%d", key);
	if ((op = con->getNdbOperation(tab)) == 0)
	    return ndberror("getNdbOperation key=%d", key);
	if (op->deleteTuple() < 0)
	    return ndberror("deleteTuple key=%d", key);
302
	for (i = 0; i < attrcnt; i++) {
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
	    col& c = ccol[i];
	    if (i == 0) {
		if (op->equal(c.aAttrName, (char*)&key, sizeof(key)) < 0)
		    return ndberror("equal key=%d", key);
	    } else {
	    }
	}
	if (con->execute(Commit) < 0) {
	  if (con->getNdbError().code != 626)
	    return ndberror("execute key=%d", key);
	} else {
	    delcnt++;
	}
	ndb->closeTransaction(con);
    }
    con = 0;
    op = 0;
    ndbout << "deleted " << delcnt << endl;

    if (xverbose)
      ndbout << "- insert" << endl;
    for (key = 0; key < opercnt; key++) {
	int off = makeOff(key);
	if ((con = ndb->startTransaction()) == 0)
	    return ndberror("startTransaction key=%d", key);
	if ((op = con->getNdbOperation(tab)) == 0)
	    return ndberror("getNdbOperation key=%d", key);
	if (op->insertTuple() < 0)
	    return ndberror("insertTuple key=%d", key);
332
	for (i = 0; i < attrcnt; i++) {
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
	    col& c = ccol[i];
	    if (i == 0) {
		if (op->equal(c.aAttrName, (char*)&key, sizeof(key)) < 0)
		    return ndberror("equal key=%d", key);
	    } else {
		memset(c.buf, 'A', c.bufsiz);
		for (int j = 0; j < c.aArraySize; j++)
		    c.buf[j + off] = byteVal(key, i, j);
		if (op->setValue(c.aAttrName, c.buf + off, c.aArraySize) < 0)
		    return ndberror("setValue key=%d col=%d", key, i);
	    }
	}
	if (con->execute(Commit) < 0)
	    return ndberror("execute key=%d", key);
	ndb->closeTransaction(con);
    }
    con = 0;
    op = 0;
    ndbout << "inserted " << key << endl;

    if (xverbose)
      ndbout << "- select" << endl;
    for (key = 0; key < opercnt; key++) {
	int off = makeOff(key);
	if (xverbose)
	  ndbout << "-- key " << key << " off=" << off << endl;
	if ((con = ndb->startTransaction()) == 0)
	    return ndberror("startTransaction key=%d", key);
	if ((op = con->getNdbOperation(tab)) == 0)
	    return ndberror("getNdbOperation key=%d", key);
	if (op->readTuple() < 0)
	    return ndberror("readTuple key=%d", key);
365
	for (i = 0; i < attrcnt; i++) {
366 367 368 369 370 371 372 373
	    col& c = ccol[i];
	    if (i == 0) {
		if (op->equal(c.aAttrName, (char*)&key, sizeof(key)) < 0)
		    return ndberror("equal key=%d", key);
	    } else {
		if (xverbose) {
		  char tmp[20];
		  if (useBuf)
374
		    sprintf(tmp, "0x%p", c.buf + off);
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		  else
		    strcpy(tmp, "ndbapi");
		  ndbout << "--- column " << i << " addr=" << tmp << endl;
		}
		memset(c.buf, 'B', c.bufsiz);
		if (useBuf) {
		    if (op->getValue(c.aAttrName, c.buf + off) < 0)
			return ndberror("getValue key=%d col=%d", key, i);
		} else {
		    if ((c.aRa = op->getValue(c.aAttrName)) == 0)
			return ndberror("getValue key=%d col=%d", key, i);
		}
	    }
	}
	if (con->execute(Commit) != 0)
	    return ndberror("execute key=%d", key);
391
	for (i = 0; i < attrcnt; i++) {
392 393 394
	    col& c = ccol[i];
	    if (i == 0) {
	    } else if (useBuf) {
395 396
                int j;
		for (j = 0; j < off; j++) {
397 398 399 400 401
		    if (c.buf[j] != 'B') {
			return chkerror("mismatch before key=%d col=%d pos=%d ok=%02x bad=%02x",
			    key, i, j, 'B', c.buf[j]);
		    }
		}
402
		for (j = 0; j < c.aArraySize; j++) {
403 404 405 406 407
		    if (c.buf[j + off] != byteVal(key, i, j)) {
			return chkerror("mismatch key=%d col=%d pos=%d ok=%02x bad=%02x",
			    key, i, j, byteVal(key, i, j), c.buf[j]);
		    }
		}
408
		for (j = c.aArraySize + off; j < c.bufsiz; j++) {
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
		    if (c.buf[j] != 'B') {
			return chkerror("mismatch after key=%d col=%d pos=%d ok=%02x bad=%02x",
			    key, i, j, 'B', c.buf[j]);
		    }
		}
	    } else {
		char* buf = c.aRa->aRef();
		if (buf == 0)
		    return ndberror("null aRef key=%d col%d", key, i);
		for (int j = 0; j < c.aArraySize; j++) {
		    if (buf[j] != byteVal(key, i, j)) {
			return chkerror("mismatch key=%d col=%d pos=%d ok=%02x bad=%02x",
			    key, i, j, byteVal(key, i, j), buf[j]);
		    }
		}
	    }
	}
	ndb->closeTransaction(con);
    }
    con = 0;
    op = 0;
    ndbout << "selected " << key << endl;

    if (xverbose)
      ndbout << "- scan" << endl;
    char found[MaxOper];
435 436
    int k;
    for (k = 0; k < opercnt; k++)
437 438 439 440 441 442 443 444
	found[k] = 0;
    for (key = 0; key < opercnt; key++) {
	int off = makeOff(key);
	if (xverbose)
	  ndbout << "-- key " << key << " off=" << off << endl;
	int newkey = 0;
	if ((con = ndb->startTransaction()) == 0)
	    return ndberror("startTransaction key=%d", key);
unknown's avatar
unknown committed
445
	if ((op = sop = con->getNdbScanOperation(tab)) == 0)
446 447 448
	  return ndberror("getNdbOperation key=%d", key);
	if (sop->readTuples(1))
	  return ndberror("openScanRead key=%d", key);
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	{
	    col& c = ccol[0];
	    if (op->load_const_u32(1, key) < 0)
		return ndberror("load_const_u32");
	    if (op->read_attr(c.aAttrName, 2) < 0)
		return ndberror("read_attr");
	    if (op->branch_eq(1, 2, 0) < 0)
		return ndberror("branch_eq");
	    if (op->interpret_exit_nok() < 0)
		return ndberror("interpret_exit_nok");
	    if (op->def_label(0) < 0)
		return ndberror("def_label");
	    if (op->interpret_exit_ok() < 0)
		return ndberror("interpret_exit_ok");
	}
464
	for (i = 0; i < attrcnt; i++) {
465 466 467 468 469 470 471 472
	    col& c = ccol[i];
	    if (i == 0) {
		if (op->getValue(c.aAttrName, (char*)&newkey) < 0)
		    return ndberror("getValue key=%d col=%d", key, i);
	    } else {
		if (xverbose) {
		  char tmp[20];
		  if (useBuf)
473
		    sprintf(tmp, "0x%p", c.buf + off);
474 475 476 477 478 479 480 481 482 483 484 485 486 487
		  else
		    strcpy(tmp, "ndbapi");
		  ndbout << "--- column " << i << " addr=" << tmp << endl;
		}
		memset(c.buf, 'C', c.bufsiz);
		if (useBuf) {
		    if (op->getValue(c.aAttrName, c.buf + off) < 0)
			return ndberror("getValue key=%d col=%d", key, i);
		} else {
		    if ((c.aRa = op->getValue(c.aAttrName)) == 0)
			return ndberror("getValue key=%d col=%d", key, i);
		}
	    }
	}
unknown's avatar
unknown committed
488
	if (con->execute(NoCommit) < 0)
489 490
	    return ndberror("executeScan key=%d", key);
	int ret, cnt = 0;
491
	while ((ret = sop->nextResult()) == 0) {
492 493
	    if (key != newkey)
		return ndberror("unexpected key=%d newkey=%d", key, newkey);
494
	    for (i = 1; i < attrcnt; i++) {
495 496
		col& c = ccol[i];
		if (useBuf) {
497 498
                    int j;
		    for (j = 0; j < off; j++) {
499 500 501 502 503
			if (c.buf[j] != 'C') {
			    return chkerror("mismatch before key=%d col=%d pos=%d ok=%02x bad=%02x",
				key, i, j, 'C', c.buf[j]);
			}
		    }
504
		    for (j = 0; j < c.aArraySize; j++) {
505 506 507 508 509
			if (c.buf[j + off] != byteVal(key, i, j)) {
			    return chkerror("mismatch key=%d col=%d pos=%d ok=%02x bad=%02x",
				key, i, j, byteVal(key, i, j), c.buf[j]);
			}
		    }
510
		    for (j = c.aArraySize + off; j < c.bufsiz; j++) {
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
			if (c.buf[j] != 'C') {
			    return chkerror("mismatch after key=%d col=%d pos=%d ok=%02x bad=%02x",
				key, i, j, 'C', c.buf[j]);
			}
		    }
		} else {
		    char* buf = c.aRa->aRef();
		    if (buf == 0)
			return ndberror("null aRef key=%d col%d", key, i);
		    for (int j = 0; j < c.aArraySize; j++) {
			if (buf[j] != byteVal(key, i, j)) {
			    return chkerror("mismatch key=%d col=%d pos=%d ok=%02x bad=%02x",
				key, i, j, byteVal(key, i, j), buf[j]);
			}
		    }
		}
	    }
	    cnt++;
	}
	if (ret < 0)
	    return ndberror("nextScanResult key=%d", key);
	if (cnt != 1)
	    return ndberror("scan key=%d found %d", key, cnt);
	found[key] = 1;
	ndb->closeTransaction(con);
    }
    con = 0;
    op = 0;
539
    for (k = 0; k < opercnt; k++)
540 541 542 543 544 545 546 547 548 549 550
	if (! found[k])
	    return ndberror("key %d not found", k);
    ndbout << "scanned " << key << endl;

    ndb = 0;
    ndbout << "done" << endl;
    return 0;
}

NDB_COMMAND(testDataBuffers, "testDataBuffers", "testDataBuffers", "testDataBuffers", 65535)
{
551
    int i;
unknown's avatar
unknown committed
552
    ndb_init();
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
    while (++argv, --argc > 0) {
	char const* p = argv[0];
	if (*p++ != '-' || strlen(p) != 1)
	    goto wrongargs;
	switch (*p) {
	case 'a':
	    if (++argv, --argc > 0) {
		attrcnt = atoi(argv[0]);
		if (1 <= attrcnt && attrcnt <= MaxAttr)
		    break;
	    }
	    goto wrongargs;
	case 'e':
	    existok = 1;
	    break;
	case 'k':
	    kontinue = true;
	    break;
	case 'l':
	    if (++argv, --argc > 0) {
		loopcnt = atoi(argv[0]);
		if (0 <= loopcnt)
		    break;
	    }
	    goto wrongargs;
	case 'o':
	    if (++argv, --argc > 0) {
		opercnt = atoi(argv[0]);
		if (0 <= opercnt && opercnt <= MaxOper)
		    break;
	    }
	    goto wrongargs;
	case 'r':
	    if (++argv, --argc > 0) {
		randomizer = atoi(argv[0]);
		if (1 <= randomizer)
		    break;
	    }
	    goto wrongargs;
	case 's':
	    if (++argv, --argc > 0) {
		sizelim = atoi(argv[0]);
		if (1 <= sizelim && sizelim <= MaxSize)
		    break;
	    }
	    goto wrongargs;
	case 'x':
	    xverbose = 1;
	    break;
	default:
	wrongargs:
	    printusage();
	    return NDBT_ProgramExit(NDBT_WRONGARGS);
	}
    }
    unsigned ok = true;
609 610 611 612 613 614 615

    Ndb_cluster_connection con;
    if(con.connect(12, 5, 1))
    {
      return NDBT_ProgramExit(NDBT_FAILED);
    }

616
    for (i = 1; 0 == loopcnt || i <= loopcnt; i++) {
617 618
	ndbout << "=== loop " << i << " ===" << endl;
	for (int flag = 0; flag < (1<<testbits); flag++) {
619
	    if (testcase(con, flag) < 0) {
620 621 622 623 624 625 626 627 628 629 630
		ok = false;
		if (! kontinue)
		    goto out;
	    }
	}
    }
out:
    return NDBT_ProgramExit(ok ? NDBT_OK : NDBT_FAILED);
}

// vim: set sw=4: