diff --git a/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/Delivery.java b/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/Delivery.java index adfeddc..8974715 100644 --- a/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/Delivery.java +++ b/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/Delivery.java @@ -12,7 +12,6 @@ public class Delivery { SqlClient sqlClient; - Transaction transaction; RandomGenerator randomGenerator; Profile profile; @@ -102,142 +101,138 @@ public long warehouseId() { return paramsWid; } - void rollback() throws IOException, ServerException, InterruptedException { - try { - transaction.rollback().get(); - } finally { - transaction = null; - } + void rollback(Transaction transaction) throws IOException, ServerException, InterruptedException { + transaction.rollback().get(); } @SuppressWarnings("checkstyle:methodlength") public void transaction(AtomicBoolean stop) throws IOException, ServerException, InterruptedException { while (!stop.get()) { - transaction = sqlClient.createTransaction().get(); - profile.invocation.delivery++; - long dId; - for (dId = 1; dId <= Scale.DISTRICTS; dId++) { - - // "SELECT no_o_id FROM NEW_ORDER WHERE no_d_id = :no_d_id AND no_w_id = :no_w_id ORDER BY no_o_id" - var future1 = transaction.executeQuery(prepared1, - Parameters.of("no_d_id", (long) dId), - Parameters.of("no_w_id", (long) paramsWid)); - try (var resultSet1 = future1.get()) { - if (!resultSet1.nextRow()) { - continue; // noOid is exhausted, it's OK and continue this transaction + try (var transaction = sqlClient.createTransaction().get();) { + profile.invocation.delivery++; + long dId; + for (dId = 1; dId <= Scale.DISTRICTS; dId++) { + + // "SELECT no_o_id FROM NEW_ORDER WHERE no_d_id = :no_d_id AND no_w_id = :no_w_id ORDER BY no_o_id" + var future1 = transaction.executeQuery(prepared1, + Parameters.of("no_d_id", (long) dId), + Parameters.of("no_w_id", (long) paramsWid)); + try (var resultSet1 = future1.get()) { + if (!resultSet1.nextRow()) { + continue; // noOid is exhausted, it's OK and continue this transaction + } + resultSet1.nextColumn(); + noOid = resultSet1.fetchInt8Value(); + } catch (ServerException e) { + profile.ordersTable.delivery++; + break; } - resultSet1.nextColumn(); - noOid = resultSet1.fetchInt8Value(); - } catch (ServerException e) { - profile.ordersTable.delivery++; - break; - } - - try { - // "DELETE FROM NEW_ORDER WHERE no_d_id = :no_d_id AND no_w_id = :no_w_id AND no_o_id = :no_o_id" - var future2 = transaction.executeStatement(prepared2, - Parameters.of("no_d_id", (long) dId), - Parameters.of("no_w_id", (long) paramsWid), - Parameters.of("no_o_id", (long) noOid)); - var result2 = future2.get(); - } catch (ServerException e) { - profile.ordersTable.delivery++; - break; - } - - // "SELECT o_c_id FROM ORDERS WHERE o_id = :o_id AND o_d_id = :o_d_id AND o_w_id = :o_w_id" - var future3 = transaction.executeQuery(prepared3, - Parameters.of("o_id", (long) noOid), - Parameters.of("o_d_id", (long) dId), - Parameters.of("o_w_id", (long) paramsWid)); - try (var resultSet3 = future3.get()) { - if (!resultSet3.nextRow()) { - throw new IOException("no record"); + + try { + // "DELETE FROM NEW_ORDER WHERE no_d_id = :no_d_id AND no_w_id = :no_w_id AND no_o_id = :no_o_id" + var future2 = transaction.executeStatement(prepared2, + Parameters.of("no_d_id", (long) dId), + Parameters.of("no_w_id", (long) paramsWid), + Parameters.of("no_o_id", (long) noOid)); + var result2 = future2.get(); + } catch (ServerException e) { + profile.ordersTable.delivery++; + break; } - resultSet3.nextColumn(); - cId = resultSet3.fetchInt8Value(); - if (resultSet3.nextRow()) { - throw new IOException("found multiple records"); + + // "SELECT o_c_id FROM ORDERS WHERE o_id = :o_id AND o_d_id = :o_d_id AND o_w_id = :o_w_id" + var future3 = transaction.executeQuery(prepared3, + Parameters.of("o_id", (long) noOid), + Parameters.of("o_d_id", (long) dId), + Parameters.of("o_w_id", (long) paramsWid)); + try (var resultSet3 = future3.get()) { + if (!resultSet3.nextRow()) { + throw new IOException("no record"); + } + resultSet3.nextColumn(); + cId = resultSet3.fetchInt8Value(); + if (resultSet3.nextRow()) { + throw new IOException("found multiple records"); + } + } catch (ServerException e) { + profile.ordersTable.delivery++; + break; } - } catch (ServerException e) { - profile.ordersTable.delivery++; - break; - } - - try { - // "UPDATE ORDERS SET o_carrier_id = :o_carrier_id WHERE o_id = :o_id AND o_d_id = :o_d_id AND o_w_id = :o_w_id" - var future4 = transaction.executeStatement(prepared4, - Parameters.of("o_carrier_id", (long) paramsOcarrierId), - Parameters.of("o_id", (long) noOid), - Parameters.of("o_d_id", (long) dId), - Parameters.of("o_w_id", (long) paramsWid)); - var result4 = future4.get(); - } catch (ServerException e) { - profile.ordersTable.delivery++; - break; - } - - try { - // "UPDATE ORDER_LINE SET ol_delivery_d = :ol_delivery_d WHERE ol_o_id = :ol_o_id AND ol_d_id = :ol_d_id AND ol_w_id = :ol_w_id" - var future5 = transaction.executeStatement(prepared5, - Parameters.of("ol_delivery_d", paramsOlDeliveryD), - Parameters.of("ol_o_id", (long) noOid), - Parameters.of("ol_d_id", (long) dId), - Parameters.of("ol_w_id", (long) paramsWid)); - var result5 = future5.get(); - } catch (ServerException e) { - profile.ordersTable.delivery++; - break; - } - - // "SELECT o_c_id FROM ORDERS WHERE o_id = :o_id AND o_d_id = :o_d_id AND o_w_id = :o_w_id" - var future6 = transaction.executeQuery(prepared6, - Parameters.of("ol_o_id", (long) noOid), - Parameters.of("ol_d_id", (long) dId), - Parameters.of("ol_w_id", (long) paramsWid)); - try (var resultSet6 = future6.get()) { - if (!resultSet6.nextRow()) { - continue; + + try { + // "UPDATE ORDERS SET o_carrier_id = :o_carrier_id WHERE o_id = :o_id AND o_d_id = :o_d_id AND o_w_id = :o_w_id" + var future4 = transaction.executeStatement(prepared4, + Parameters.of("o_carrier_id", (long) paramsOcarrierId), + Parameters.of("o_id", (long) noOid), + Parameters.of("o_d_id", (long) dId), + Parameters.of("o_w_id", (long) paramsWid)); + var result4 = future4.get(); + } catch (ServerException e) { + profile.ordersTable.delivery++; + break; } - resultSet6.nextColumn(); - olTotal = resultSet6.fetchFloat8Value(); - if (resultSet6.nextRow()) { - throw new IOException("found multiple records"); + + try { + // "UPDATE ORDER_LINE SET ol_delivery_d = :ol_delivery_d WHERE ol_o_id = :ol_o_id AND ol_d_id = :ol_d_id AND ol_w_id = :ol_w_id" + var future5 = transaction.executeStatement(prepared5, + Parameters.of("ol_delivery_d", paramsOlDeliveryD), + Parameters.of("ol_o_id", (long) noOid), + Parameters.of("ol_d_id", (long) dId), + Parameters.of("ol_w_id", (long) paramsWid)); + var result5 = future5.get(); + } catch (ServerException e) { + profile.ordersTable.delivery++; + break; + } + + // "SELECT o_c_id FROM ORDERS WHERE o_id = :o_id AND o_d_id = :o_d_id AND o_w_id = :o_w_id" + var future6 = transaction.executeQuery(prepared6, + Parameters.of("ol_o_id", (long) noOid), + Parameters.of("ol_d_id", (long) dId), + Parameters.of("ol_w_id", (long) paramsWid)); + try (var resultSet6 = future6.get()) { + if (!resultSet6.nextRow()) { + continue; + } + resultSet6.nextColumn(); + olTotal = resultSet6.fetchFloat8Value(); + if (resultSet6.nextRow()) { + throw new IOException("found multiple records"); + } + } catch (ServerException e) { + profile.ordersTable.delivery++; + break; + } + + try { + // "UPDATE CUSTOMER SET c_balance = c_balance + :ol_total WHERE c_id = :c_id AND c_d_id = :c_d_id AND c_w_id = :c_w_id" + var future7 = transaction.executeStatement(prepared7, + Parameters.of("ol_total", (double) olTotal), + Parameters.of("c_id", (long) cId), + Parameters.of("c_d_id", (long) dId), + Parameters.of("c_w_id", (long) paramsWid)); + var result7 = future7.get(); + } catch (ServerException e) { + profile.customerTable.delivery++; + break; } - } catch (ServerException e) { - profile.ordersTable.delivery++; - break; - } - - try { - // "UPDATE CUSTOMER SET c_balance = c_balance + :ol_total WHERE c_id = :c_id AND c_d_id = :c_d_id AND c_w_id = :c_w_id" - var future7 = transaction.executeStatement(prepared7, - Parameters.of("ol_total", (double) olTotal), - Parameters.of("c_id", (long) cId), - Parameters.of("c_d_id", (long) dId), - Parameters.of("c_w_id", (long) paramsWid)); - var result7 = future7.get(); - } catch (ServerException e) { - profile.customerTable.delivery++; - break; } - } - - if (dId > Scale.DISTRICTS) { // completed 'for (dId = 1; dId <= Scale.DISTRICTS; dId++) {' - try { - transaction.commit().get(); - profile.completion.delivery++; - return; - } catch (ServerException e) { - profile.retryOnCommit.delivery++; - transaction = null; - continue; + + if (dId > Scale.DISTRICTS) { // completed 'for (dId = 1; dId <= Scale.DISTRICTS; dId++) {' + try { + transaction.commit().get(); + profile.completion.delivery++; + return; + } catch (ServerException e) { + profile.retryOnCommit.delivery++; + continue; + } } + + // break in 'for (dId = 1; dId <= Scale.DISTRICTS; dId++) {' + profile.retryOnStatement.delivery++; + rollback(transaction); } - - // break in 'for (dId = 1; dId <= Scale.DISTRICTS; dId++) {' - profile.retryOnStatement.delivery++; - rollback(); } } } diff --git a/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/NewOrder.java b/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/NewOrder.java index e7fea82..c2ad74a 100644 --- a/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/NewOrder.java +++ b/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/NewOrder.java @@ -15,7 +15,6 @@ public class NewOrder { SqlClient sqlClient; - Transaction transaction; RandomGenerator randomGenerator; Profile profile; @@ -179,238 +178,234 @@ public void setParams() { paramsWillRollback = (randomGenerator.uniformWithin(1, 100) == 1); } - void rollback() throws IOException, ServerException, InterruptedException { - try { - transaction.rollback().get(); - } finally { - transaction = null; - } + void rollback(Transaction transaction) throws IOException, ServerException, InterruptedException { + transaction.rollback().get(); } @SuppressWarnings("checkstyle:methodlength") public void transaction(AtomicBoolean stop) throws IOException, ServerException, InterruptedException { while (!stop.get()) { - transaction = sqlClient.createTransaction().get(); - profile.invocation.newOrder++; - total = 0; - - // SELECT w_tax, c_discount, c_last, c_credit FROM WAREHOUSE, CUSTOMER WHERE w_id = :w_id AND c_w_id = w_id AND c_d_id = :c_d_id AND c_id = :c_id; - var future1 = transaction.executeQuery(prepared1, - Parameters.of("w_id", (long) paramsWid), - Parameters.of("c_d_id", (long) paramsDid), - Parameters.of("c_id", (long) paramsCid)); - try (var resultSet1 = future1.get()) { - if (!resultSet1.nextRow()) { - throw new IOException("no record"); - } - resultSet1.nextColumn(); - wTax = resultSet1.fetchFloat8Value(); - resultSet1.nextColumn(); - cDiscount = resultSet1.fetchFloat8Value(); - resultSet1.nextColumn(); - cLast = resultSet1.fetchCharacterValue(); - resultSet1.nextColumn(); - cCredit = resultSet1.fetchCharacterValue(); - if (resultSet1.nextRow()) { - throw new IOException("found multiple records"); - } - } catch (ServerException e) { - profile.retryOnStatement.newOrder++; - profile.customerTable.newOrder++; - rollback(); - continue; - } - - // SELECT d_next_o_id, d_tax FROM DISTRICT WHERE d_w_id = :d_w_id AND d_id = :d_id - var future2 = transaction.executeQuery(prepared2, - Parameters.of("d_w_id", (long) paramsWid), - Parameters.of("d_id", (long) paramsDid)); - try (var resultSet2 = future2.get()) { - if (!resultSet2.nextRow()) { - throw new IOException("no record"); - } - resultSet2.nextColumn(); - dNextOid = resultSet2.fetchInt8Value(); - resultSet2.nextColumn(); - dTax = resultSet2.fetchFloat8Value(); - if (resultSet2.nextRow()) { - throw new IOException("found multiple records"); - } - } catch (ServerException e) { - profile.retryOnStatement.newOrder++; - profile.districtTable.newOrder++; - rollback(); - continue; - } - - try { - // UPDATE DISTRICT SET d_next_o_id = :d_next_o_id WHERE d_w_id = :d_w_id AND d_id = :d_id - var future3 = transaction.executeStatement(prepared3, - Parameters.of("d_next_o_id", (long) (dNextOid + 1)), - Parameters.of("d_w_id", (long) paramsWid), - Parameters.of("d_id", (long) paramsDid)); - var result3 = future3.get(); - } catch (ServerException e) { - profile.retryOnStatement.newOrder++; - profile.districtTable.newOrder++; - rollback(); - continue; - } - - oid = dNextOid; - - try { - // INSERT INTO ORDERS (o_id, o_d_id, o_w_id, o_c_id, o_entry_d, o_ol_cnt, o_all_local) VALUES (:o_id, :o_d_id, :o_w_id, :o_c_id, :o_entry_d, :o_ol_cnt, :o_all_local - var future4 = transaction.executeStatement(prepared4, - Parameters.of("o_id", (long) oid), - Parameters.of("o_d_id", (long) paramsDid), - Parameters.of("o_w_id", (long) paramsWid), - Parameters.of("o_c_id", (long) paramsCid), - Parameters.of("o_entry_d", paramsEntryD), - Parameters.of("o_ol_cnt", (long) paramsOlCnt), - Parameters.of("o_all_local", (long) paramsAllLocal)); - var result4 = future4.get(); - } catch (ServerException e) { - profile.retryOnStatement.newOrder++; - profile.ordersTable.newOrder++; - rollback(); - continue; - } - - try { - // INSERT INTO NEW_ORDER (no_o_id, no_d_id, no_w_id)VALUES (:no_o_id, :no_d_id, :no_w_id - var future5 = transaction.executeStatement(prepared5, - Parameters.of("no_o_id", (long) oid), - Parameters.of("no_d_id", (long) paramsDid), - Parameters.of("no_w_id", (long) paramsWid)); - var result5 = future5.get(); - } catch (ServerException e) { - profile.retryOnStatement.newOrder++; - profile.ordersTable.newOrder++; - rollback(); - continue; - } - - int olNumber; - for (olNumber = 1; olNumber <= paramsOlCnt; olNumber++) { - var olSupplyWid = paramsSupplyWid; - var olIid = paramsItemId[olNumber - 1]; - var olQuantity = paramsQty[olNumber - 1]; - - // SELECT i_price, i_name , i_data FROM ITEM WHERE i_id = :i_id - var future6 = transaction.executeQuery(prepared6, - Parameters.of("i_id", (long) olIid)); - try (var resultSet6 = future6.get()) { - if (!resultSet6.nextRow()) { + try (var transaction = sqlClient.createTransaction().get();) { + profile.invocation.newOrder++; + total = 0; + + // SELECT w_tax, c_discount, c_last, c_credit FROM WAREHOUSE, CUSTOMER WHERE w_id = :w_id AND c_w_id = w_id AND c_d_id = :c_d_id AND c_id = :c_id; + var future1 = transaction.executeQuery(prepared1, + Parameters.of("w_id", (long) paramsWid), + Parameters.of("c_d_id", (long) paramsDid), + Parameters.of("c_id", (long) paramsCid)); + try (var resultSet1 = future1.get()) { + if (!resultSet1.nextRow()) { throw new IOException("no record"); } - resultSet6.nextColumn(); - iPrice = resultSet6.fetchFloat8Value(); - resultSet6.nextColumn(); - iName = resultSet6.fetchCharacterValue(); - resultSet6.nextColumn(); - iData = resultSet6.fetchCharacterValue(); - if (resultSet6.nextRow()) { + resultSet1.nextColumn(); + wTax = resultSet1.fetchFloat8Value(); + resultSet1.nextColumn(); + cDiscount = resultSet1.fetchFloat8Value(); + resultSet1.nextColumn(); + cLast = resultSet1.fetchCharacterValue(); + resultSet1.nextColumn(); + cCredit = resultSet1.fetchCharacterValue(); + if (resultSet1.nextRow()) { throw new IOException("found multiple records"); } } catch (ServerException e) { - break; + profile.retryOnStatement.newOrder++; + profile.customerTable.newOrder++; + rollback(transaction); + continue; } - - // SELECT s_quantity, s_data, s_dist_01, s_dist_02, s_dist_03, s_dist_04, s_dist_05, s_dist_06, s_dist_07, s_dist_08, s_dist_09, s_dist_10 FROM STOCK WHERE s_i_id = :s_i_id AND s_w_id = :s_w_id - var future7 = transaction.executeQuery(prepared7, - Parameters.of("s_i_id", (long) olIid), - Parameters.of("s_w_id", (long) olSupplyWid)); - try (var resultSet7 = future7.get()) { - if (!resultSet7.nextRow()) { + + // SELECT d_next_o_id, d_tax FROM DISTRICT WHERE d_w_id = :d_w_id AND d_id = :d_id + var future2 = transaction.executeQuery(prepared2, + Parameters.of("d_w_id", (long) paramsWid), + Parameters.of("d_id", (long) paramsDid)); + try (var resultSet2 = future2.get()) { + if (!resultSet2.nextRow()) { throw new IOException("no record"); } - resultSet7.nextColumn(); - sQuantity = resultSet7.fetchInt8Value(); - resultSet7.nextColumn(); - sData = resultSet7.fetchCharacterValue(); - for (int i = 0; i < 10; i++) { - resultSet7.nextColumn(); - sDistData[i] = resultSet7.fetchCharacterValue(); - } - if (resultSet7.nextRow()) { + resultSet2.nextColumn(); + dNextOid = resultSet2.fetchInt8Value(); + resultSet2.nextColumn(); + dTax = resultSet2.fetchFloat8Value(); + if (resultSet2.nextRow()) { throw new IOException("found multiple records"); } } catch (ServerException e) { - profile.stockTable.newOrder++; - break; - } - - String olDistInfo = sDistData[(int) paramsDid - 1].substring(0, 24); - stock[olNumber - 1] = sQuantity; - - if (iData.indexOf("original") >= 0 && sData.indexOf("original") >= 0) { - bg[olNumber - 1] = "B"; - } else { - bg[olNumber - 1] = "G"; - } - - if (sQuantity > olQuantity) { - sQuantity = sQuantity - olQuantity; - } else { - sQuantity = sQuantity - olQuantity + 91; + profile.retryOnStatement.newOrder++; + profile.districtTable.newOrder++; + rollback(transaction); + continue; } - + try { - // UPDATE STOCK SET s_quantity = :s_quantity WHERE s_i_id = :s_i_id AND s_w_id = :s_w_id - var future8 = transaction.executeStatement(prepared8, - Parameters.of("s_quantity", (long) sQuantity), - Parameters.of("s_i_id", (long) olIid), - Parameters.of("s_w_id", (long) olSupplyWid)); - var result8 = future8.get(); + // UPDATE DISTRICT SET d_next_o_id = :d_next_o_id WHERE d_w_id = :d_w_id AND d_id = :d_id + var future3 = transaction.executeStatement(prepared3, + Parameters.of("d_next_o_id", (long) (dNextOid + 1)), + Parameters.of("d_w_id", (long) paramsWid), + Parameters.of("d_id", (long) paramsDid)); + var result3 = future3.get(); } catch (ServerException e) { - profile.stockTable.newOrder++; - break; + profile.retryOnStatement.newOrder++; + profile.districtTable.newOrder++; + rollback(transaction); + continue; } - - double olAmount = olQuantity * iPrice * (1 + wTax + dTax) * (1 - cDiscount); - amt[olNumber - 1] = olAmount; - total += olAmount; - + + oid = dNextOid; + try { - // INSERT INTO ORDER_LINE (ol_o_id, ol_d_id, ol_w_id, ol_number, ol_i_id, ol_supply_w_id, ol_quantity, ol_amount, ol_dist_info)VALUES (:ol_o_id, :ol_d_id, :ol_w_id, :ol_number, :ol_i_id, :ol_supply_w_id, :ol_quantity, :ol_amount, :ol_dist_info - var future9 = transaction.executeStatement(prepared9, - Parameters.of("ol_o_id", (long) oid), - Parameters.of("ol_d_id", (long) paramsDid), - Parameters.of("ol_w_id", (long) paramsWid), - Parameters.of("ol_number", (long) olNumber), - Parameters.of("ol_i_id", (long) olIid), - Parameters.of("ol_supply_w_id", (long) olSupplyWid), - Parameters.of("ol_quantity", (long) olQuantity), - Parameters.of("ol_amount", (long) olAmount), - Parameters.of("ol_dist_info", olDistInfo)); - var result9 = future9.get(); + // INSERT INTO ORDERS (o_id, o_d_id, o_w_id, o_c_id, o_entry_d, o_ol_cnt, o_all_local) VALUES (:o_id, :o_d_id, :o_w_id, :o_c_id, :o_entry_d, :o_ol_cnt, :o_all_local + var future4 = transaction.executeStatement(prepared4, + Parameters.of("o_id", (long) oid), + Parameters.of("o_d_id", (long) paramsDid), + Parameters.of("o_w_id", (long) paramsWid), + Parameters.of("o_c_id", (long) paramsCid), + Parameters.of("o_entry_d", paramsEntryD), + Parameters.of("o_ol_cnt", (long) paramsOlCnt), + Parameters.of("o_all_local", (long) paramsAllLocal)); + var result4 = future4.get(); } catch (ServerException e) { + profile.retryOnStatement.newOrder++; profile.ordersTable.newOrder++; - break; - } - } - - if (olNumber > paramsOlCnt) { // completed 'for (olNumber = 1; olNumber <= paramsOlCnt; olNumber++) {' - if (paramsWillRollback) { - rollback(); - profile.newOrderIntentionalRollback++; - return; + rollback(transaction); + continue; } + try { - transaction.commit().get(); - profile.completion.newOrder++; - return; + // INSERT INTO NEW_ORDER (no_o_id, no_d_id, no_w_id)VALUES (:no_o_id, :no_d_id, :no_w_id + var future5 = transaction.executeStatement(prepared5, + Parameters.of("no_o_id", (long) oid), + Parameters.of("no_d_id", (long) paramsDid), + Parameters.of("no_w_id", (long) paramsWid)); + var result5 = future5.get(); } catch (ServerException e) { - profile.retryOnCommit.newOrder++; - transaction = null; + profile.retryOnStatement.newOrder++; + profile.ordersTable.newOrder++; + rollback(transaction); continue; } + + int olNumber; + for (olNumber = 1; olNumber <= paramsOlCnt; olNumber++) { + var olSupplyWid = paramsSupplyWid; + var olIid = paramsItemId[olNumber - 1]; + var olQuantity = paramsQty[olNumber - 1]; + + // SELECT i_price, i_name , i_data FROM ITEM WHERE i_id = :i_id + var future6 = transaction.executeQuery(prepared6, + Parameters.of("i_id", (long) olIid)); + try (var resultSet6 = future6.get()) { + if (!resultSet6.nextRow()) { + throw new IOException("no record"); + } + resultSet6.nextColumn(); + iPrice = resultSet6.fetchFloat8Value(); + resultSet6.nextColumn(); + iName = resultSet6.fetchCharacterValue(); + resultSet6.nextColumn(); + iData = resultSet6.fetchCharacterValue(); + if (resultSet6.nextRow()) { + throw new IOException("found multiple records"); + } + } catch (ServerException e) { + break; + } + + // SELECT s_quantity, s_data, s_dist_01, s_dist_02, s_dist_03, s_dist_04, s_dist_05, s_dist_06, s_dist_07, s_dist_08, s_dist_09, s_dist_10 FROM STOCK WHERE s_i_id = :s_i_id AND s_w_id = :s_w_id + var future7 = transaction.executeQuery(prepared7, + Parameters.of("s_i_id", (long) olIid), + Parameters.of("s_w_id", (long) olSupplyWid)); + try (var resultSet7 = future7.get()) { + if (!resultSet7.nextRow()) { + throw new IOException("no record"); + } + resultSet7.nextColumn(); + sQuantity = resultSet7.fetchInt8Value(); + resultSet7.nextColumn(); + sData = resultSet7.fetchCharacterValue(); + for (int i = 0; i < 10; i++) { + resultSet7.nextColumn(); + sDistData[i] = resultSet7.fetchCharacterValue(); + } + if (resultSet7.nextRow()) { + throw new IOException("found multiple records"); + } + } catch (ServerException e) { + profile.stockTable.newOrder++; + break; + } + + String olDistInfo = sDistData[(int) paramsDid - 1].substring(0, 24); + stock[olNumber - 1] = sQuantity; + + if (iData.indexOf("original") >= 0 && sData.indexOf("original") >= 0) { + bg[olNumber - 1] = "B"; + } else { + bg[olNumber - 1] = "G"; + } + + if (sQuantity > olQuantity) { + sQuantity = sQuantity - olQuantity; + } else { + sQuantity = sQuantity - olQuantity + 91; + } + + try { + // UPDATE STOCK SET s_quantity = :s_quantity WHERE s_i_id = :s_i_id AND s_w_id = :s_w_id + var future8 = transaction.executeStatement(prepared8, + Parameters.of("s_quantity", (long) sQuantity), + Parameters.of("s_i_id", (long) olIid), + Parameters.of("s_w_id", (long) olSupplyWid)); + var result8 = future8.get(); + } catch (ServerException e) { + profile.stockTable.newOrder++; + break; + } + + double olAmount = olQuantity * iPrice * (1 + wTax + dTax) * (1 - cDiscount); + amt[olNumber - 1] = olAmount; + total += olAmount; + + try { + // INSERT INTO ORDER_LINE (ol_o_id, ol_d_id, ol_w_id, ol_number, ol_i_id, ol_supply_w_id, ol_quantity, ol_amount, ol_dist_info)VALUES (:ol_o_id, :ol_d_id, :ol_w_id, :ol_number, :ol_i_id, :ol_supply_w_id, :ol_quantity, :ol_amount, :ol_dist_info + var future9 = transaction.executeStatement(prepared9, + Parameters.of("ol_o_id", (long) oid), + Parameters.of("ol_d_id", (long) paramsDid), + Parameters.of("ol_w_id", (long) paramsWid), + Parameters.of("ol_number", (long) olNumber), + Parameters.of("ol_i_id", (long) olIid), + Parameters.of("ol_supply_w_id", (long) olSupplyWid), + Parameters.of("ol_quantity", (long) olQuantity), + Parameters.of("ol_amount", (long) olAmount), + Parameters.of("ol_dist_info", olDistInfo)); + var result9 = future9.get(); + } catch (ServerException e) { + profile.ordersTable.newOrder++; + break; + } + } + + if (olNumber > paramsOlCnt) { // completed 'for (olNumber = 1; olNumber <= paramsOlCnt; olNumber++) {' + if (paramsWillRollback) { + rollback(transaction); + profile.newOrderIntentionalRollback++; + return; + } + try { + transaction.commit().get(); + profile.completion.newOrder++; + return; + } catch (ServerException e) { + profile.retryOnCommit.newOrder++; + continue; + } + } + + // break in 'for (olNumber = 1; olNumber <= paramsOlCnt; olNumber++) {' + profile.retryOnStatement.newOrder++; + rollback(transaction); } - - // break in 'for (olNumber = 1; olNumber <= paramsOlCnt; olNumber++) {' - profile.retryOnStatement.newOrder++; - rollback(); } } } diff --git a/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/OrderStatus.java b/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/OrderStatus.java index 7aa24b2..ab8a58f 100644 --- a/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/OrderStatus.java +++ b/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/OrderStatus.java @@ -12,7 +12,6 @@ public class OrderStatus { SqlClient sqlClient; - Transaction transaction; RandomGenerator randomGenerator; Profile profile; @@ -114,152 +113,148 @@ public void setParams() { } } - void rollback() throws IOException, ServerException, InterruptedException { - try { - transaction.rollback().get(); - } finally { - transaction = null; - } + void rollback(Transaction transaction) throws IOException, ServerException, InterruptedException { + transaction.rollback().get(); } @SuppressWarnings("checkstyle:methodlength") public void transaction(AtomicBoolean stop) throws IOException, ServerException, InterruptedException { while (!stop.get()) { - transaction = sqlClient.createTransaction().get(); - profile.invocation.orderStatus++; - if (!paramsByName) { + try (var transaction = sqlClient.createTransaction().get();) { + profile.invocation.orderStatus++; + if (!paramsByName) { cId = paramsCid; - } else { + } else { cId = Customer.chooseCustomer(transaction, prepared1, prepared2, paramsWid, paramsDid, paramsClast); if (cId < 0) { profile.retryOnStatement.orderStatus++; profile.customerTable.orderStatus++; - rollback(); + rollback(transaction); continue; } - } - if (cId != 0) { - // "SELECT c_balance, c_first, c_middle, c_last FROM CUSTOMER WHERE c_id = :c_id AND c_d_id = :c_d_id AND c_w_id = :c_w_id" - var future3 = transaction.executeQuery(prepared3, - Parameters.of("c_id", (long) cId), - Parameters.of("c_d_id", (long) paramsDid), - Parameters.of("c_w_id", (long) paramsWid)); - try (var resultSet3 = future3.get()) { - if (!resultSet3.nextRow()) { - throw new IOException("no record"); - } - resultSet3.nextColumn(); - cBalance = resultSet3.fetchFloat8Value(); - resultSet3.nextColumn(); - cFirst = resultSet3.fetchCharacterValue(); - resultSet3.nextColumn(); - cMiddle = resultSet3.fetchCharacterValue(); - resultSet3.nextColumn(); - cLast = resultSet3.fetchCharacterValue(); - if (resultSet3.nextRow()) { - throw new IOException("found multiple records"); - } - } catch (ServerException e) { - profile.retryOnStatement.orderStatus++; - profile.ordersTable.orderStatus++; - rollback(); - continue; - } - - // "SELECT o_id FROM ORDERS WHERE o_w_id = :o_w_id AND o_d_id = :o_d_id AND o_c_id = :o_c_id ORDER by o_id DESC" - var future4 = transaction.executeQuery(prepared4, - Parameters.of("o_d_id", (long) paramsDid), - Parameters.of("o_w_id", (long) paramsWid), - Parameters.of("o_c_id", (long) cId)); - try (var resultSet4 = future4.get()) { - if (!resultSet4.nextRow()) { - throw new IOException("no record"); - } - resultSet4.nextColumn(); - oId = resultSet4.fetchInt8Value(); - // FIXME treat InconsistentIndex - // if (status4.getError().getStatus() == SqlStatus.Status.ERR_INCONSISTENT_INDEX) { - // if (profile.inconsistentIndexCount == 0) { - // System.out.println("inconsistent_index"); - // } - // profile.inconsistentIndexCount++; - // } - } catch (ServerException e) { - profile.retryOnStatement.orderStatus++; - profile.ordersTable.orderStatus++; - rollback(); - continue; } - - // "SELECT o_carrier_id, o_entry_d, o_ol_cnt FROM ORDERS WHERE o_w_id = :o_w_id AND o_d_id = :o_d_id AND o_id = :o_id" - var future5 = transaction.executeQuery(prepared5, - Parameters.of("o_d_id", (long) paramsDid), - Parameters.of("o_w_id", (long) paramsWid), - Parameters.of("o_id", (long) oId)); - try (var resultSet5 = future5.get()) { - if (!resultSet5.nextRow()) { - throw new IOException("no record"); + if (cId != 0) { + // "SELECT c_balance, c_first, c_middle, c_last FROM CUSTOMER WHERE c_id = :c_id AND c_d_id = :c_d_id AND c_w_id = :c_w_id" + var future3 = transaction.executeQuery(prepared3, + Parameters.of("c_id", (long) cId), + Parameters.of("c_d_id", (long) paramsDid), + Parameters.of("c_w_id", (long) paramsWid)); + try (var resultSet3 = future3.get()) { + if (!resultSet3.nextRow()) { + throw new IOException("no record"); + } + resultSet3.nextColumn(); + cBalance = resultSet3.fetchFloat8Value(); + resultSet3.nextColumn(); + cFirst = resultSet3.fetchCharacterValue(); + resultSet3.nextColumn(); + cMiddle = resultSet3.fetchCharacterValue(); + resultSet3.nextColumn(); + cLast = resultSet3.fetchCharacterValue(); + if (resultSet3.nextRow()) { + throw new IOException("found multiple records"); + } + } catch (ServerException e) { + profile.retryOnStatement.orderStatus++; + profile.ordersTable.orderStatus++; + rollback(transaction); + continue; } - resultSet5.nextColumn(); - if (!resultSet5.isNull()) { - oCarrierId = resultSet5.fetchInt8Value(); + + // "SELECT o_id FROM ORDERS WHERE o_w_id = :o_w_id AND o_d_id = :o_d_id AND o_c_id = :o_c_id ORDER by o_id DESC" + var future4 = transaction.executeQuery(prepared4, + Parameters.of("o_d_id", (long) paramsDid), + Parameters.of("o_w_id", (long) paramsWid), + Parameters.of("o_c_id", (long) cId)); + try (var resultSet4 = future4.get()) { + if (!resultSet4.nextRow()) { + throw new IOException("no record"); + } + resultSet4.nextColumn(); + oId = resultSet4.fetchInt8Value(); + // FIXME treat InconsistentIndex + // if (status4.getError().getStatus() == SqlStatus.Status.ERR_INCONSISTENT_INDEX) { + // if (profile.inconsistentIndexCount == 0) { + // System.out.println("inconsistent_index"); + // } + // profile.inconsistentIndexCount++; + // } + } catch (ServerException e) { + profile.retryOnStatement.orderStatus++; + profile.ordersTable.orderStatus++; + rollback(transaction); + continue; } - resultSet5.nextColumn(); - oEntryD = resultSet5.fetchCharacterValue(); - resultSet5.nextColumn(); - oOlCnt = resultSet5.fetchInt8Value(); - if (resultSet5.nextRow()) { - throw new IOException("found multiple records"); + + // "SELECT o_carrier_id, o_entry_d, o_ol_cnt FROM ORDERS WHERE o_w_id = :o_w_id AND o_d_id = :o_d_id AND o_id = :o_id" + var future5 = transaction.executeQuery(prepared5, + Parameters.of("o_d_id", (long) paramsDid), + Parameters.of("o_w_id", (long) paramsWid), + Parameters.of("o_id", (long) oId)); + try (var resultSet5 = future5.get()) { + if (!resultSet5.nextRow()) { + throw new IOException("no record"); + } + resultSet5.nextColumn(); + if (!resultSet5.isNull()) { + oCarrierId = resultSet5.fetchInt8Value(); + } + resultSet5.nextColumn(); + oEntryD = resultSet5.fetchCharacterValue(); + resultSet5.nextColumn(); + oOlCnt = resultSet5.fetchInt8Value(); + if (resultSet5.nextRow()) { + throw new IOException("found multiple records"); + } + } catch (ServerException e) { + profile.retryOnStatement.orderStatus++; + profile.ordersTable.orderStatus++; + rollback(transaction); + continue; } - } catch (ServerException e) { - profile.retryOnStatement.orderStatus++; - profile.ordersTable.orderStatus++; - rollback(); - continue; - } - - // "SELECT ol_i_id, ol_supply_w_id, ol_quantity, ol_amount, ol_delivery_d FROM ORDER_LINE WHERE ol_o_id = :ol_o_id AND ol_d_id = :ol_d_id AND ol_w_id = :ol_w_id" - var future6 = transaction.executeQuery(prepared6, - Parameters.of("ol_o_id", (long) oId), - Parameters.of("ol_d_id", (long) paramsDid), - Parameters.of("ol_w_id", (long) paramsWid)); - try (var resultSet6 = future6.get()) { - int i = 0; - while (resultSet6.nextRow()) { - resultSet6.nextColumn(); - olIid[i] = resultSet6.fetchInt8Value(); - resultSet6.nextColumn(); - olSupplyWid[i] = resultSet6.fetchInt8Value(); - resultSet6.nextColumn(); - olQuantity[i] = resultSet6.fetchInt8Value(); - resultSet6.nextColumn(); - olAmount[i] = resultSet6.fetchFloat8Value(); - resultSet6.nextColumn(); - if (!resultSet6.isNull()) { - olDeliveryD[i] = resultSet6.fetchCharacterValue(); + + // "SELECT ol_i_id, ol_supply_w_id, ol_quantity, ol_amount, ol_delivery_d FROM ORDER_LINE WHERE ol_o_id = :ol_o_id AND ol_d_id = :ol_d_id AND ol_w_id = :ol_w_id" + var future6 = transaction.executeQuery(prepared6, + Parameters.of("ol_o_id", (long) oId), + Parameters.of("ol_d_id", (long) paramsDid), + Parameters.of("ol_w_id", (long) paramsWid)); + try (var resultSet6 = future6.get()) { + int i = 0; + while (resultSet6.nextRow()) { + resultSet6.nextColumn(); + olIid[i] = resultSet6.fetchInt8Value(); + resultSet6.nextColumn(); + olSupplyWid[i] = resultSet6.fetchInt8Value(); + resultSet6.nextColumn(); + olQuantity[i] = resultSet6.fetchInt8Value(); + resultSet6.nextColumn(); + olAmount[i] = resultSet6.fetchFloat8Value(); + resultSet6.nextColumn(); + if (!resultSet6.isNull()) { + olDeliveryD[i] = resultSet6.fetchCharacterValue(); + } + i++; } - i++; + } catch (ServerException e) { + profile.retryOnStatement.orderStatus++; + profile.ordersTable.orderStatus++; + rollback(transaction); + continue; + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println(e + ": ol_o_id = " + oId + ", ol_d_id = " + paramsDid + ", ol_w_id = " + paramsWid); + rollback(transaction); + continue; } + } + + try { + transaction.commit().get(); + profile.completion.orderStatus++; + return; } catch (ServerException e) { - profile.retryOnStatement.orderStatus++; - profile.ordersTable.orderStatus++; - rollback(); - continue; - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println(e + ": ol_o_id = " + oId + ", ol_d_id = " + paramsDid + ", ol_w_id = " + paramsWid); - rollback(); - continue; + profile.retryOnCommit.orderStatus++; } } - - try { - transaction.commit().get(); - profile.completion.orderStatus++; - return; - } catch (ServerException e) { - profile.retryOnCommit.orderStatus++; - transaction = null; - } } } } diff --git a/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/Payment.java b/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/Payment.java index 4cd11f8..78de22b 100644 --- a/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/Payment.java +++ b/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/Payment.java @@ -15,7 +15,6 @@ public class Payment { SqlClient sqlClient; - Transaction transaction; RandomGenerator randomGenerator; Profile profile; @@ -186,258 +185,254 @@ public void setParams() { paramsHdata = randomGenerator.makeAlphaString(12, 24); } - void rollback() throws IOException, ServerException, InterruptedException { - try { - transaction.rollback().get(); - } finally { - transaction = null; - } + void rollback(Transaction transaction) throws IOException, ServerException, InterruptedException { + transaction.rollback().get(); } @SuppressWarnings("checkstyle:methodlength") public void transaction(AtomicBoolean stop) throws IOException, ServerException, InterruptedException { while (!stop.get()) { - transaction = sqlClient.createTransaction().get(); - profile.invocation.payment++; - - try { - // UPDATE WAREHOUSE SET w_ytd = w_ytd + :h_amount WHERE w_id = :w_id - var future1 = transaction.executeStatement(prepared1, - Parameters.of("h_amount", (double) paramsHamount), - Parameters.of("w_id", (long) paramsWid)); - var result1 = future1.get(); - } catch (ServerException e) { - profile.retryOnStatement.payment++; - profile.warehouseTable.payment++; - rollback(); - continue; - } - - // SELECT w_street_1, w_street_2, w_city, w_state, w_zip, w_name FROM WAREHOUSE WHERE w_id = :w_id - var future2 = transaction.executeQuery(prepared2, - Parameters.of("w_id", (long) paramsWid)); - try (var resultSet2 = future2.get()) { - if (!resultSet2.nextRow()) { - throw new IOException("no record"); - } - resultSet2.nextColumn(); - wName = resultSet2.fetchCharacterValue(); - resultSet2.nextColumn(); - wStreet1 = resultSet2.fetchCharacterValue(); - resultSet2.nextColumn(); - wStreet2 = resultSet2.fetchCharacterValue(); - resultSet2.nextColumn(); - wCity = resultSet2.fetchCharacterValue(); - resultSet2.nextColumn(); - wState = resultSet2.fetchCharacterValue(); - resultSet2.nextColumn(); - wZip = resultSet2.fetchCharacterValue(); - if (resultSet2.nextRow()) { - throw new IOException("found multiple records"); - } - } catch (ServerException e) { - profile.retryOnStatement.payment++; - profile.warehouseTable.payment++; - rollback(); - continue; - } - - try { - // UPDATE DISTRICT SET d_ytd = d_ytd + :h_amount WHERE d_w_id = :d_w_id AND d_id = :d_id"; - var future3 = transaction.executeStatement(prepared3, - Parameters.of("h_amount", (double) paramsHamount), - Parameters.of("d_w_id", (long) paramsWid), - Parameters.of("d_id", (long) paramsDid)); - var result3 = future3.get(); - } catch (ServerException e) { - profile.retryOnStatement.payment++; - profile.districtTable.payment++; - rollback(); - continue; - } - - // SELECT d_street_1, d_street_2, d_city, d_state, d_zip, d_name FROM DISTRICT WHERE d_w_id = :d_w_id AND d_id = :d_id - var future4 = transaction.executeQuery(prepared4, - Parameters.of("d_w_id", (long) paramsWid), - Parameters.of("d_id", (long) paramsDid)); - try (var resultSet4 = future4.get()) { - if (!resultSet4.nextRow()) { - throw new IOException("no record"); - } - resultSet4.nextColumn(); - dStreet1 = resultSet4.fetchCharacterValue(); - resultSet4.nextColumn(); - dStreet2 = resultSet4.fetchCharacterValue(); - resultSet4.nextColumn(); - dCity = resultSet4.fetchCharacterValue(); - resultSet4.nextColumn(); - dState = resultSet4.fetchCharacterValue(); - resultSet4.nextColumn(); - dZip = resultSet4.fetchCharacterValue(); - resultSet4.nextColumn(); - dName = resultSet4.fetchCharacterValue(); - if (resultSet4.nextRow()) { - throw new IOException("found multiple records"); - } - } catch (ServerException e) { - profile.retryOnStatement.payment++; - profile.districtTable.payment++; - rollback(); - continue; - } - - if (!paramsByName) { - cId = paramsCid; - } else { - cId = Customer.chooseCustomer(transaction, prepared5, prepared6, paramsWid, paramsDid, paramsClast); - if (cId < 0) { + try (var transaction = sqlClient.createTransaction().get();) { + profile.invocation.payment++; + + try { + // UPDATE WAREHOUSE SET w_ytd = w_ytd + :h_amount WHERE w_id = :w_id + var future1 = transaction.executeStatement(prepared1, + Parameters.of("h_amount", (double) paramsHamount), + Parameters.of("w_id", (long) paramsWid)); + var result1 = future1.get(); + } catch (ServerException e) { profile.retryOnStatement.payment++; - profile.customerTable.payment++; - rollback(); + profile.warehouseTable.payment++; + rollback(transaction); continue; } - } - - // SELECT c_first, c_middle, c_last, c_street_1, c_street_2, c_city, c_state, c_zip, c_phone, c_credit, c_credit_lim, c_discount, c_balance, c_since FROM CUSTOMER WHERE c_w_id = :c_w_id AND c_d_id = :c_d_id AND c_id = :c_id - var future7 = transaction.executeQuery(prepared7, - Parameters.of("c_w_id", (long) paramsWid), - Parameters.of("c_d_id", (long) paramsDid), - Parameters.of("c_id", (long) cId)); - try (var resultSet7 = future7.get()) { - if (!resultSet7.nextRow()) { - throw new IOException("no record"); + + // SELECT w_street_1, w_street_2, w_city, w_state, w_zip, w_name FROM WAREHOUSE WHERE w_id = :w_id + var future2 = transaction.executeQuery(prepared2, + Parameters.of("w_id", (long) paramsWid)); + try (var resultSet2 = future2.get()) { + if (!resultSet2.nextRow()) { + throw new IOException("no record"); + } + resultSet2.nextColumn(); + wName = resultSet2.fetchCharacterValue(); + resultSet2.nextColumn(); + wStreet1 = resultSet2.fetchCharacterValue(); + resultSet2.nextColumn(); + wStreet2 = resultSet2.fetchCharacterValue(); + resultSet2.nextColumn(); + wCity = resultSet2.fetchCharacterValue(); + resultSet2.nextColumn(); + wState = resultSet2.fetchCharacterValue(); + resultSet2.nextColumn(); + wZip = resultSet2.fetchCharacterValue(); + if (resultSet2.nextRow()) { + throw new IOException("found multiple records"); + } + } catch (ServerException e) { + profile.retryOnStatement.payment++; + profile.warehouseTable.payment++; + rollback(transaction); + continue; } - resultSet7.nextColumn(); - cFirst = resultSet7.fetchCharacterValue(); // c_first(0) - resultSet7.nextColumn(); - cMiddle = resultSet7.fetchCharacterValue(); // c_middle(1) - resultSet7.nextColumn(); - cLast = resultSet7.fetchCharacterValue(); // c_last(2) - resultSet7.nextColumn(); - cStreet1 = resultSet7.fetchCharacterValue(); // c_street_1(3) - resultSet7.nextColumn(); - cStreet2 = resultSet7.fetchCharacterValue(); // c_street_1(4) - resultSet7.nextColumn(); - cCity = resultSet7.fetchCharacterValue(); // c_city(5) - resultSet7.nextColumn(); - cState = resultSet7.fetchCharacterValue(); // c_state(6) - resultSet7.nextColumn(); - cZip = resultSet7.fetchCharacterValue(); // c_zip(7) - resultSet7.nextColumn(); - cPhone = resultSet7.fetchCharacterValue(); // c_phone(8) - resultSet7.nextColumn(); - cCredit = resultSet7.fetchCharacterValue(); // c_credit(9) - resultSet7.nextColumn(); - cCreditLim = resultSet7.fetchFloat8Value(); // c_credit_lim(10) - resultSet7.nextColumn(); - cDiscount = resultSet7.fetchFloat8Value(); // c_discount(11) - resultSet7.nextColumn(); - cBalance = resultSet7.fetchFloat8Value(); // c_balance(12) - resultSet7.nextColumn(); - cSince = resultSet7.fetchCharacterValue(); // c_since(13) - if (resultSet7.nextRow()) { - throw new IOException("found multiple records"); + + try { + // UPDATE DISTRICT SET d_ytd = d_ytd + :h_amount WHERE d_w_id = :d_w_id AND d_id = :d_id"; + var future3 = transaction.executeStatement(prepared3, + Parameters.of("h_amount", (double) paramsHamount), + Parameters.of("d_w_id", (long) paramsWid), + Parameters.of("d_id", (long) paramsDid)); + var result3 = future3.get(); + } catch (ServerException e) { + profile.retryOnStatement.payment++; + profile.districtTable.payment++; + rollback(transaction); + continue; } - } catch (ServerException e) { - profile.retryOnStatement.payment++; - profile.customerTable.payment++; - rollback(); - continue; - } - - cBalance += paramsHamount; - - if (cCredit.indexOf("BC") >= 0) { - // SELECT c_data FROM CUSTOMER WHERE c_w_id = :c_w_id AND c_d_id = :c_d_id AND c_id = :c_id - var future8 = transaction.executeQuery(prepared8, - Parameters.of("c_w_id", (long) paramsWid), - Parameters.of("c_d_id", (long) paramsDid), - Parameters.of("c_id", (long) cId)); - try (var resultSet8 = future8.get()) { - if (!resultSet8.nextRow()) { + + // SELECT d_street_1, d_street_2, d_city, d_state, d_zip, d_name FROM DISTRICT WHERE d_w_id = :d_w_id AND d_id = :d_id + var future4 = transaction.executeQuery(prepared4, + Parameters.of("d_w_id", (long) paramsWid), + Parameters.of("d_id", (long) paramsDid)); + try (var resultSet4 = future4.get()) { + if (!resultSet4.nextRow()) { throw new IOException("no record"); } - resultSet8.nextColumn(); - cData = resultSet8.fetchCharacterValue(); - if (resultSet8.nextRow()) { + resultSet4.nextColumn(); + dStreet1 = resultSet4.fetchCharacterValue(); + resultSet4.nextColumn(); + dStreet2 = resultSet4.fetchCharacterValue(); + resultSet4.nextColumn(); + dCity = resultSet4.fetchCharacterValue(); + resultSet4.nextColumn(); + dState = resultSet4.fetchCharacterValue(); + resultSet4.nextColumn(); + dZip = resultSet4.fetchCharacterValue(); + resultSet4.nextColumn(); + dName = resultSet4.fetchCharacterValue(); + if (resultSet4.nextRow()) { throw new IOException("found multiple records"); } } catch (ServerException e) { profile.retryOnStatement.payment++; - profile.customerTable.payment++; - rollback(); + profile.districtTable.payment++; + rollback(transaction); continue; } - - String cNewData = String.format("| %4d %2d %4d %2d %4d $%7.2f ", cId, paramsDid, paramsWid, paramsDid, paramsWid, paramsHamount) + paramsHdate + " " + paramsHdata; - int length = 500 - cNewData.length(); - if (length < cData.length()) { - cNewData += cData.substring(0, length); + + if (!paramsByName) { + cId = paramsCid; } else { - cNewData += cData; + cId = Customer.chooseCustomer(transaction, prepared5, prepared6, paramsWid, paramsDid, paramsClast); + if (cId < 0) { + profile.retryOnStatement.payment++; + profile.customerTable.payment++; + rollback(transaction); + continue; + } } - - try { - // UPDATE CUSTOMER SET c_balance = :c_balance ,c_data = :c_data WHERE c_w_id = :c_w_id AND c_d_id = :c_d_id AND c_id = :c_id - var future9 = transaction.executeStatement(prepared9, - Parameters.of("c_balance", (double) cBalance), - Parameters.of("c_data", cNewData), - Parameters.of("c_w_id", (long) paramsWid), - Parameters.of("c_d_id", (long) paramsDid), - Parameters.of("c_id", (long) cId)); - var result9 = future9.get(); + + // SELECT c_first, c_middle, c_last, c_street_1, c_street_2, c_city, c_state, c_zip, c_phone, c_credit, c_credit_lim, c_discount, c_balance, c_since FROM CUSTOMER WHERE c_w_id = :c_w_id AND c_d_id = :c_d_id AND c_id = :c_id + var future7 = transaction.executeQuery(prepared7, + Parameters.of("c_w_id", (long) paramsWid), + Parameters.of("c_d_id", (long) paramsDid), + Parameters.of("c_id", (long) cId)); + try (var resultSet7 = future7.get()) { + if (!resultSet7.nextRow()) { + throw new IOException("no record"); + } + resultSet7.nextColumn(); + cFirst = resultSet7.fetchCharacterValue(); // c_first(0) + resultSet7.nextColumn(); + cMiddle = resultSet7.fetchCharacterValue(); // c_middle(1) + resultSet7.nextColumn(); + cLast = resultSet7.fetchCharacterValue(); // c_last(2) + resultSet7.nextColumn(); + cStreet1 = resultSet7.fetchCharacterValue(); // c_street_1(3) + resultSet7.nextColumn(); + cStreet2 = resultSet7.fetchCharacterValue(); // c_street_1(4) + resultSet7.nextColumn(); + cCity = resultSet7.fetchCharacterValue(); // c_city(5) + resultSet7.nextColumn(); + cState = resultSet7.fetchCharacterValue(); // c_state(6) + resultSet7.nextColumn(); + cZip = resultSet7.fetchCharacterValue(); // c_zip(7) + resultSet7.nextColumn(); + cPhone = resultSet7.fetchCharacterValue(); // c_phone(8) + resultSet7.nextColumn(); + cCredit = resultSet7.fetchCharacterValue(); // c_credit(9) + resultSet7.nextColumn(); + cCreditLim = resultSet7.fetchFloat8Value(); // c_credit_lim(10) + resultSet7.nextColumn(); + cDiscount = resultSet7.fetchFloat8Value(); // c_discount(11) + resultSet7.nextColumn(); + cBalance = resultSet7.fetchFloat8Value(); // c_balance(12) + resultSet7.nextColumn(); + cSince = resultSet7.fetchCharacterValue(); // c_since(13) + if (resultSet7.nextRow()) { + throw new IOException("found multiple records"); + } } catch (ServerException e) { profile.retryOnStatement.payment++; profile.customerTable.payment++; - rollback(); + rollback(transaction); continue; } - } else { + + cBalance += paramsHamount; + + if (cCredit.indexOf("BC") >= 0) { + // SELECT c_data FROM CUSTOMER WHERE c_w_id = :c_w_id AND c_d_id = :c_d_id AND c_id = :c_id + var future8 = transaction.executeQuery(prepared8, + Parameters.of("c_w_id", (long) paramsWid), + Parameters.of("c_d_id", (long) paramsDid), + Parameters.of("c_id", (long) cId)); + try (var resultSet8 = future8.get()) { + if (!resultSet8.nextRow()) { + throw new IOException("no record"); + } + resultSet8.nextColumn(); + cData = resultSet8.fetchCharacterValue(); + if (resultSet8.nextRow()) { + throw new IOException("found multiple records"); + } + } catch (ServerException e) { + profile.retryOnStatement.payment++; + profile.customerTable.payment++; + rollback(transaction); + continue; + } + + String cNewData = String.format("| %4d %2d %4d %2d %4d $%7.2f ", cId, paramsDid, paramsWid, paramsDid, paramsWid, paramsHamount) + paramsHdate + " " + paramsHdata; + int length = 500 - cNewData.length(); + if (length < cData.length()) { + cNewData += cData.substring(0, length); + } else { + cNewData += cData; + } + + try { + // UPDATE CUSTOMER SET c_balance = :c_balance ,c_data = :c_data WHERE c_w_id = :c_w_id AND c_d_id = :c_d_id AND c_id = :c_id + var future9 = transaction.executeStatement(prepared9, + Parameters.of("c_balance", (double) cBalance), + Parameters.of("c_data", cNewData), + Parameters.of("c_w_id", (long) paramsWid), + Parameters.of("c_d_id", (long) paramsDid), + Parameters.of("c_id", (long) cId)); + var result9 = future9.get(); + } catch (ServerException e) { + profile.retryOnStatement.payment++; + profile.customerTable.payment++; + rollback(transaction); + continue; + } + } else { + try { + // UPDATE CUSTOMER SET c_balance = :c_balance WHERE c_w_id = :c_w_id AND c_d_id = :c_d_id AND c_id = :c_id + var future10 = transaction.executeStatement(prepared10, + Parameters.of("c_balance", (double) cBalance), + Parameters.of("c_w_id", (long) paramsWid), + Parameters.of("c_d_id", (long) paramsDid), + Parameters.of("c_id", (long) cId)); + var result10 = future10.get(); + } catch (ServerException e) { + profile.retryOnStatement.payment++; + profile.customerTable.payment++; + rollback(transaction); + continue; + } + } + + String hData = String.format("%10s%10s ", (wName.length() > 10) ? wName.substring(0, 10) : wName, (dName.length() > 10) ? dName.substring(0, 10) : dName); try { - // UPDATE CUSTOMER SET c_balance = :c_balance WHERE c_w_id = :c_w_id AND c_d_id = :c_d_id AND c_id = :c_id - var future10 = transaction.executeStatement(prepared10, - Parameters.of("c_balance", (double) cBalance), - Parameters.of("c_w_id", (long) paramsWid), - Parameters.of("c_d_id", (long) paramsDid), - Parameters.of("c_id", (long) cId)); - var result10 = future10.get(); + // INSERT INTO HISTORY (h_c_d_id, h_c_w_id, h_c_id, h_d_id, h_w_id, h_date, h_amount, h_data) VALUES (:c_d_id, :c_w_id, :c_id, :d_id, :w_id, :datetime, :h_amount, :h_data) + var future11 = transaction.executeStatement(prepared11, + Parameters.of("c_d_id", (long) paramsDid), + Parameters.of("c_w_id", (long) paramsWid), + + Parameters.of("c_id", (long) cId), + Parameters.of("d_id", (long) paramsDid), + Parameters.of("w_id", (long) paramsWid), + + Parameters.of("datetime", paramsHdate), + Parameters.of("h_amount", (double) paramsHamount), + Parameters.of("h_data", hData.substring(0, 24))); + var result11 = future11.get(); } catch (ServerException e) { profile.retryOnStatement.payment++; - profile.customerTable.payment++; - rollback(); + rollback(transaction); continue; } + + try { + transaction.commit().get(); + profile.completion.payment++; + return; + } catch (ServerException e) { + profile.retryOnCommit.payment++; + } } - - String hData = String.format("%10s%10s ", (wName.length() > 10) ? wName.substring(0, 10) : wName, (dName.length() > 10) ? dName.substring(0, 10) : dName); - try { - // INSERT INTO HISTORY (h_c_d_id, h_c_w_id, h_c_id, h_d_id, h_w_id, h_date, h_amount, h_data) VALUES (:c_d_id, :c_w_id, :c_id, :d_id, :w_id, :datetime, :h_amount, :h_data) - var future11 = transaction.executeStatement(prepared11, - Parameters.of("c_d_id", (long) paramsDid), - Parameters.of("c_w_id", (long) paramsWid), - - Parameters.of("c_id", (long) cId), - Parameters.of("d_id", (long) paramsDid), - Parameters.of("w_id", (long) paramsWid), - - Parameters.of("datetime", paramsHdate), - Parameters.of("h_amount", (double) paramsHamount), - Parameters.of("h_data", hData.substring(0, 24))); - var result11 = future11.get(); - } catch (ServerException e) { - profile.retryOnStatement.payment++; - rollback(); - continue; - } - - try { - transaction.commit().get(); - profile.completion.payment++; - return; - } catch (ServerException e) { - profile.retryOnCommit.payment++; - transaction = null; - } } } } diff --git a/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/StockLevel.java b/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/StockLevel.java index 4846cb2..97ccee9 100644 --- a/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/StockLevel.java +++ b/modules/tpcc/src/main/java/com/tsurugidb/tsubakuro/examples/tpcc/StockLevel.java @@ -12,7 +12,6 @@ public class StockLevel { SqlClient sqlClient; - Transaction transaction; RandomGenerator randomGenerator; Profile profile; @@ -64,70 +63,66 @@ public void setParams() { paramsThreshold = randomGenerator.uniformWithin(10, 20); } - void rollback() throws IOException, ServerException, InterruptedException { - try { - transaction.rollback().get(); - } finally { - transaction = null; - } + void rollback(Transaction transaction) throws IOException, ServerException, InterruptedException { + transaction.rollback().get(); } public void transaction(AtomicBoolean stop) throws IOException, ServerException, InterruptedException { while (!stop.get()) { profile.invocation.stockLevel++; - transaction = sqlClient.createTransaction().get(); + try (var transaction = sqlClient.createTransaction().get();) { - // "SELECT d_next_o_id FROM DISTRICT WHERE d_w_id = :d_w_id AND d_id = :d_id" - var future1 = transaction.executeQuery(prepared1, - Parameters.of("d_w_id", (long) paramsWid), - Parameters.of("d_id", (long) paramsDid)); - try (var resultSet1 = future1.get()) { - if (!resultSet1.nextRow()) { - throw new IOException("no record"); + // "SELECT d_next_o_id FROM DISTRICT WHERE d_w_id = :d_w_id AND d_id = :d_id" + var future1 = transaction.executeQuery(prepared1, + Parameters.of("d_w_id", (long) paramsWid), + Parameters.of("d_id", (long) paramsDid)); + try (var resultSet1 = future1.get()) { + if (!resultSet1.nextRow()) { + throw new IOException("no record"); + } + resultSet1.nextColumn(); + oId = resultSet1.fetchInt8Value(); + if (resultSet1.nextRow()) { + throw new IOException("found multiple records"); + } + } catch (ServerException e) { + profile.retryOnStatement.stockLevel++; + profile.districtTable.stockLevel++; + rollback(transaction); + continue; } - resultSet1.nextColumn(); - oId = resultSet1.fetchInt8Value(); - if (resultSet1.nextRow()) { - throw new IOException("found multiple records"); + + // "SELECT COUNT(DISTINCT s_i_id) FROM ORDER_LINE JOIN STOCK ON s_i_id = ol_i_id WHERE ol_w_id = :ol_w_id AND ol_d_id = :ol_d_id AND ol_o_id < :ol_o_id_high AND ol_o_id >= :ol_o_id_low AND s_w_id = :s_w_id AND s_quantity < :s_quantity" + var future2 = transaction.executeQuery(prepared2, + Parameters.of("ol_w_id", (long) paramsWid), + Parameters.of("ol_d_id", (long) paramsDid), + Parameters.of("ol_o_id_high", (long) oId), + Parameters.of("ol_o_id_low", (long) (oId - OID_RANGE)), + Parameters.of("s_w_id", (long) paramsWid), + Parameters.of("s_quantity", (long) paramsThreshold)); + try (var resultSet2 = future2.get()) { + if (!resultSet2.nextRow()) { + throw new IOException("no record"); + } + resultSet2.nextColumn(); + queryResult = resultSet2.fetchInt8Value(); + if (resultSet2.nextRow()) { + throw new IOException("found multiple records"); + } + } catch (ServerException e) { + profile.retryOnStatement.stockLevel++; + profile.stockTable.stockLevel++; + rollback(transaction); + continue; } - } catch (ServerException e) { - profile.retryOnStatement.stockLevel++; - profile.districtTable.stockLevel++; - rollback(); - continue; - } - - // "SELECT COUNT(DISTINCT s_i_id) FROM ORDER_LINE JOIN STOCK ON s_i_id = ol_i_id WHERE ol_w_id = :ol_w_id AND ol_d_id = :ol_d_id AND ol_o_id < :ol_o_id_high AND ol_o_id >= :ol_o_id_low AND s_w_id = :s_w_id AND s_quantity < :s_quantity" - var future2 = transaction.executeQuery(prepared2, - Parameters.of("ol_w_id", (long) paramsWid), - Parameters.of("ol_d_id", (long) paramsDid), - Parameters.of("ol_o_id_high", (long) oId), - Parameters.of("ol_o_id_low", (long) (oId - OID_RANGE)), - Parameters.of("s_w_id", (long) paramsWid), - Parameters.of("s_quantity", (long) paramsThreshold)); - try (var resultSet2 = future2.get()) { - if (!resultSet2.nextRow()) { - throw new IOException("no record"); + + try { + transaction.commit().get(); + profile.completion.stockLevel++; + return; + } catch (ServerException e) { + profile.retryOnCommit.stockLevel++; } - resultSet2.nextColumn(); - queryResult = resultSet2.fetchInt8Value(); - if (resultSet2.nextRow()) { - throw new IOException("found multiple records"); - } - } catch (ServerException e) { - profile.retryOnStatement.stockLevel++; - profile.stockTable.stockLevel++; - rollback(); - continue; - } - - try { - transaction.commit().get(); - profile.completion.stockLevel++; - return; - } catch (ServerException e) { - profile.retryOnCommit.stockLevel++; - transaction = null; } } }