20 package com.mysql.cluster.benchmark.tws;
22 import com.mysql.ndbjtie.ndbapi.Ndb_cluster_connection;
23 import com.mysql.ndbjtie.ndbapi.Ndb;
24 import com.mysql.ndbjtie.ndbapi.NdbDictionary.Dictionary;
25 import com.mysql.ndbjtie.ndbapi.NdbDictionary.TableConst;
26 import com.mysql.ndbjtie.ndbapi.NdbError;
27 import com.mysql.ndbjtie.ndbapi.NdbTransaction;
28 import com.mysql.ndbjtie.ndbapi.NdbOperation;
30 import java.nio.ByteBuffer;
31 import java.nio.ByteOrder;
32 import java.nio.CharBuffer;
33 import java.nio.charset.Charset;
34 import java.nio.charset.CodingErrorAction;
35 import java.nio.charset.CharsetEncoder;
36 import java.nio.charset.CharsetDecoder;
37 import java.nio.charset.CoderResult;
38 import java.nio.charset.CharacterCodingException;
41 class NdbjtieLoad
extends TwsLoad {
44 protected String mgmdConnect;
52 protected int ndbOpLockMode;
55 protected TableConst table_t0;
58 protected ByteBuffer bb;
61 static protected final ByteOrder bo = ByteOrder.nativeOrder();
62 static protected final Charset cs;
63 static protected final CharsetEncoder csEncoder;
64 static protected final CharsetDecoder csDecoder;
67 cs = Charset.forName(
"ISO-8859-1");
68 csDecoder = cs.newDecoder();
69 csEncoder = cs.newEncoder();
73 .onMalformedInput(CodingErrorAction.REPORT)
74 .onUnmappableCharacter(CodingErrorAction.REPORT);
76 .onMalformedInput(CodingErrorAction.REPORT)
77 .onUnmappableCharacter(CodingErrorAction.REPORT);
88 protected void initProperties() {
90 out.print(
"setting ndb properties ...");
92 final StringBuilder
msg =
new StringBuilder();
93 final String eol = System.getProperty(
"line.separator");
96 mgmdConnect = driver.props.getProperty(
"ndb.mgmdConnect",
"localhost");
97 assert mgmdConnect != null;
100 catalog = driver.props.getProperty(
"ndb.catalog",
"crunddb");
101 assert catalog != null;
104 schema = driver.props.getProperty(
"ndb.schema",
"def");
105 assert schema != null;
107 if (msg.length() == 0) {
108 out.println(
" [ok]");
111 out.print(msg.toString());
115 descr =
"ndbjtie(" + mgmdConnect +
")";
118 protected void printProperties() {
119 out.println(
"ndb.mgmdConnect: \"" + mgmdConnect +
"\"");
120 out.println(
"ndb.catalog: \"" + catalog +
"\"");
121 out.println(
"ndb.schema: \"" + schema +
"\"");
124 public void init() throws Exception {
129 Driver.loadSystemLibrary(
"ndbclient");
132 out.print(
"creating cluster connection ...");
136 out.println(
" [ok: mgmd@" + mgmdConnect +
"]");
139 public void close() throws Exception {
143 out.print(
"closing cluster connection ...");
147 out.println(
" [ok]");
156 public void initConnection() {
157 assert (mgmd != null);
158 assert (ndb == null);
161 out.println(
"initializing ndbjtie resources ...");
164 out.print(
"connecting to cluster ...");
166 final int retries = 0;
168 final int verbose = 1;
170 if (mgmd.connect(retries, delay, verbose) != 0) {
171 final String msg = (
"mgmd@" + mgmdConnect
172 +
" was not ready within "
173 + (retries * delay) +
"s.");
175 throw new RuntimeException(
"!!! " + msg);
177 out.println(
" [ok: " + mgmdConnect +
"]");
180 out.print(
"waiting for data nodes ...");
182 final int initial_wait = 10;
183 final int final_wait = 0;
185 if (mgmd.wait_until_ready(initial_wait, final_wait) < 0) {
186 final String msg = (
"data nodes were not ready within "
187 + (initial_wait + final_wait) +
"s.");
189 throw new RuntimeException(msg);
191 out.println(
" [ok]");
194 out.print(
"connecting to database ...");
195 ndb =
Ndb.create(mgmd, catalog, schema);
196 final int max_no_tx = 10;
198 if (ndb.init(max_no_tx) != 0) {
199 String msg =
"Error caught: " + ndb.getNdbError().message();
200 throw new RuntimeException(msg);
202 out.println(
" [ok: " + catalog +
"." + schema +
"]");
204 metaData =
new MetaData(ndb);
206 metaData.initNdbjtieModel();
208 initNdbjtieBuffers();
210 out.print(
"using lock mode for reads ...");
213 switch (driver.lockMode) {
216 lm =
"LM_CommittedRead";
228 lm =
"LM_CommittedRead";
231 out.println(
" [ok: " + lm +
"]");
234 public void closeConnection() {
235 assert (ndb != null);
238 out.println(
"releasing ndbjtie resources ...");
240 closeNdbjtieBuffers();
244 out.print(
"closing database connection ...");
248 out.println(
" [ok]");
252 protected void closeNdbjtieModel() {
253 assert (ndb != null);
254 assert (table_t0 != null);
255 assert (metaData.getColumn(0) != null);
257 out.print(
"clearing metadata cache...");
263 out.println(
" [ok]");
266 public void initNdbjtieBuffers() {
267 assert (metaData.getColumn(0) != null);
270 out.print(
"allocating buffers...");
273 bb = ByteBuffer.allocateDirect(metaData.getRowWidth() * driver.nRows);
278 out.println(
" [ok]");
281 protected void closeNdbjtieBuffers() {
282 assert (metaData.getColumn(0) != null);
285 out.print(
"releasing buffers...");
290 out.println(
" [ok]");
296 public void runOperations() {
298 out.println(
"running NDB JTie operations ..."
299 +
" [nRows=" + driver.nRows +
"]");
301 if (driver.doSingle) {
302 if (driver.doInsert) runNdbjtieInsert(
TwsDriver.XMode.SINGLE);
303 if (driver.doLookup) runNdbjtieLookup(
TwsDriver.XMode.SINGLE);
304 if (driver.doUpdate) runNdbjtieUpdate(
TwsDriver.XMode.SINGLE);
305 if (driver.doDelete) runNdbjtieDelete(
TwsDriver.XMode.SINGLE);
308 if (driver.doInsert) runNdbjtieInsert(
TwsDriver.XMode.BULK);
309 if (driver.doLookup) runNdbjtieLookup(
TwsDriver.XMode.BULK);
310 if (driver.doUpdate) runNdbjtieUpdate(
TwsDriver.XMode.BULK);
311 if (driver.doDelete) runNdbjtieDelete(
TwsDriver.XMode.BULK);
313 if (driver.doBatch) {
314 if (driver.doInsert) runNdbjtieInsert(
TwsDriver.XMode.BATCH);
315 if (driver.doLookup) runNdbjtieLookup(
TwsDriver.XMode.BATCH);
316 if (driver.doUpdate) runNdbjtieUpdate(
TwsDriver.XMode.BATCH);
317 if (driver.doDelete) runNdbjtieDelete(
TwsDriver.XMode.BATCH);
330 for(
int i = 0;
i < driver.nRows;
i++) {
331 ndbjtieBeginTransaction();
333 ndbjtieCommitTransaction();
334 ndbjtieCloseTransaction();
337 ndbjtieBeginTransaction();
338 for(
int i = 0;
i < driver.nRows;
i++) {
342 ndbjtieExecuteTransaction();
344 ndbjtieCommitTransaction();
345 ndbjtieCloseTransaction();
351 protected void initTable() {
352 if(table_t0 == null) {
354 final Dictionary dict = ndb.getDictionary();
356 if ((table_t0 = dict.getTable(
"mytable")) == null)
357 throw new RuntimeException(TwsUtils.toStr(dict.getNdbError()));
361 protected void ndbjtieInsert(
int c0) {
366 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
368 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
372 final CharBuffer str = CharBuffer.wrap(Integer.toString(i));
376 ndbjtieTranscode(bb, str);
377 if (op.
equal(metaData.getAttr(0), bb) != 0)
378 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
379 bb.position(bb.position() + metaData.getColumnWidth(0));
382 ndbjtieTranscode(bb, str);
383 if (op.
setValue(metaData.getAttr(1), bb) != 0)
384 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
385 bb.position(bb.position() + metaData.getColumnWidth(1));
387 if (op.
setValue(metaData.getAttr(2),
i) != 0)
388 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
390 if (op.
setValue(metaData.getAttr(3),
i) != 0)
391 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
394 if (op.
setValue(metaData.getAttr(4), null) != 0)
395 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
398 ndbjtieTranscode(bb, str);
399 if (op.
setValue(metaData.getAttr(5), bb) != 0)
400 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
401 bb.position(bb.position() + metaData.getColumnWidth(5));
404 ndbjtieTranscode(bb, str);
405 if (op.
setValue(metaData.getAttr(6), bb) != 0)
406 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
407 bb.position(bb.position() + metaData.getColumnWidth(6));
410 ndbjtieTranscode(bb, str);
411 if (op.
setValue(metaData.getAttr(7), bb) != 0)
412 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
413 bb.position(bb.position() + metaData.getColumnWidth(7));
416 ndbjtieTranscode(bb, str);
417 if (op.
setValue(metaData.getAttr(8), bb) != 0)
418 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
419 bb.position(bb.position() + metaData.getColumnWidth(8));
422 if (op.
setValue(metaData.getAttr(9), null) != 0)
423 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
425 if (op.
setValue(metaData.getAttr(10), null) != 0)
426 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
428 if (op.
setValue(metaData.getAttr(11), null) != 0)
429 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
431 if (op.
setValue(metaData.getAttr(12), null) != 0)
432 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
434 if (op.
setValue(metaData.getAttr(13), null) != 0)
435 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
437 if (op.
setValue(metaData.getAttr(14), null) != 0)
438 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
439 }
catch (CharacterCodingException e) {
440 throw new RuntimeException(e);
447 final String name =
"lookup_" +
mode.toString().toLowerCase();
451 for(
int i = 0; i < driver.nRows; i++) {
452 ndbjtieBeginTransaction();
454 ndbjtieCommitTransaction();
456 ndbjtieCloseTransaction();
459 ndbjtieBeginTransaction();
460 for(
int i = 0; i < driver.nRows; i++) {
464 ndbjtieExecuteTransaction();
466 ndbjtieCommitTransaction();
467 for(
int i = 0; i < driver.nRows; i++) {
470 ndbjtieCloseTransaction();
476 protected void ndbjtieLookup(
int c0) {
480 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
482 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
484 int p = bb.position();
487 final CharBuffer str = CharBuffer.wrap(Integer.toString(c0));
491 ndbjtieTranscode(bb, str);
492 if (op.
equal(metaData.getAttr(0), bb) != 0)
493 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
494 bb.position(p += metaData.getColumnWidth(0));
495 }
catch (CharacterCodingException e) {
496 throw new RuntimeException(e);
500 for(
int i = 1; i < metaData.getColumnCount(); i++) {
501 if (op.
getValue(metaData.getAttr(i), bb) == null)
502 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
503 bb.position(p += metaData.getColumnWidth(i));
507 protected void ndbjtieRead(
int c0) {
514 int p = bb.position();
515 bb.position(p += metaData.getColumnWidth(0));
522 ndbjtieTranscode(bb);
523 bb.position(p += metaData.getColumnWidth(1));
525 bb.asIntBuffer().get();
526 bb.position(p += metaData.getColumnWidth(2));
527 bb.asIntBuffer().get();
528 bb.position(p += metaData.getColumnWidth(3));
529 bb.asIntBuffer().get();
530 bb.position(p += metaData.getColumnWidth(4));
533 for(
int i = 5; i < metaData.getColumnCount(); i++) {
534 ndbjtieTranscode(bb);
535 bb.position(p += metaData.getColumnWidth(i));
537 }
catch (CharacterCodingException e) {
538 throw new RuntimeException(e);
545 final String name =
"update_" +
mode.toString().toLowerCase();
549 for(
int i = 0; i < driver.nRows; i++) {
550 ndbjtieBeginTransaction();
552 ndbjtieCommitTransaction();
553 ndbjtieCloseTransaction();
556 ndbjtieBeginTransaction();
557 for(
int i = 0; i < driver.nRows; i++) {
561 ndbjtieExecuteTransaction();
563 ndbjtieCommitTransaction();
564 ndbjtieCloseTransaction();
570 protected void ndbjtieUpdate(
int c0) {
571 final CharBuffer str0 = CharBuffer.wrap(Integer.toString(c0));
573 final CharBuffer str1 = CharBuffer.wrap(Integer.toString(r));
578 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
580 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
586 ndbjtieTranscode(bb, str0);
587 if (op.
equal(metaData.getAttr(0), bb) != 0)
588 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
589 bb.position(bb.position() + metaData.getColumnWidth(0));
592 ndbjtieTranscode(bb, str1);
593 if (op.
setValue(metaData.getAttr(1), bb) != 0)
594 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
595 bb.position(bb.position() + metaData.getColumnWidth(1));
597 if (op.
setValue(metaData.getAttr(2), r) != 0)
598 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
600 if (op.
setValue(metaData.getAttr(3), r) != 0)
601 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
603 for(
int i = 5; i < metaData.getColumnCount(); i++) {
605 ndbjtieTranscode(bb, str1);
606 if (op.
setValue(metaData.getAttr(i), bb) != 0)
607 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
608 bb.position(bb.position() + metaData.getColumnWidth(i));
611 }
catch (CharacterCodingException e) {
612 throw new RuntimeException(e);
619 final String name =
"delete_" +
mode.toString().toLowerCase();
623 for(
int i = 0; i < driver.nRows; i++) {
624 ndbjtieBeginTransaction();
626 ndbjtieCommitTransaction();
627 ndbjtieCloseTransaction();
630 ndbjtieBeginTransaction();
631 for(
int i = 0; i < driver.nRows; i++) {
635 ndbjtieExecuteTransaction();
637 ndbjtieCommitTransaction();
638 ndbjtieCloseTransaction();
644 protected void ndbjtieDelete(
int c0) {
648 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
650 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
652 int p = bb.position();
656 final CharBuffer str = CharBuffer.wrap(Integer.toString(c0));
660 ndbjtieTranscode(bb, str);
661 if (op.
equal(metaData.getAttr(0), bb) != 0)
662 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
663 bb.position(p += metaData.getColumnWidth(0));
664 }
catch (CharacterCodingException e) {
665 throw new RuntimeException(e);
671 protected void ndbjtieBeginTransaction() {
679 final TableConst
table = null;
680 final ByteBuffer keyData = null;
681 final int keyLen = 0;
682 if ((tx = ndb.startTransaction(table, keyData, keyLen)) == null)
683 throw new RuntimeException(TwsUtils.toStr(ndb.getNdbError()));
686 protected void ndbjtieExecuteTransaction() {
693 if (tx.execute(execType, abortOption, force) != 0
695 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
698 protected void ndbjtieCommitTransaction() {
705 if (tx.execute(execType, abortOption, force) != 0
707 throw new RuntimeException(TwsUtils.toStr(tx.getNdbError()));
713 protected void ndbjtieCloseTransaction() {
717 ndb.closeTransaction(tx);
723 protected CharBuffer ndbjtieTranscode(ByteBuffer from)
724 throws CharacterCodingException {
726 final int p = from.position();
729 final int l = from.get();
730 assert ((0 <= l) && (l < 256));
733 from.limit(from.position() + l);
736 final CharBuffer
to = csDecoder.decode(from);
737 assert (!from.hasRemaining());
740 from.limit(from.capacity());
742 assert (to.position() == 0);
743 assert (to.limit() == to.capacity());
747 protected void ndbjtieTranscode(ByteBuffer to, CharBuffer from)
748 throws CharacterCodingException {
750 final int p = to.position();
757 final boolean endOfInput =
true;
758 final CoderResult cr = csEncoder.encode(from, to, endOfInput);
759 if (!cr.isUnderflow())
761 assert (!from.hasRemaining());
764 final int l = (to.position() - p) - 1;
765 assert (0 <= l && l < 256);