From 5f76a67ace73f4da5c0579735ce5869f6a356f16 Mon Sep 17 00:00:00 2001 From: istudies Date: Sat, 21 Aug 2021 13:07:55 +0800 Subject: [PATCH 01/16] Optimize the use of synchronized and format code. --- src/LoadData/LoadData.java | 668 +++---- src/LoadData/LoadDataWorker.java | 1473 +++++++------- src/OSCollector/OSCollector.java | 288 ++- src/client/jTPCC.java | 1327 ++++++------- src/client/jTPCCConfig.java | 35 +- src/client/jTPCCConnection.java | 553 +++--- src/client/jTPCCRandom.java | 236 +-- src/client/jTPCCTData.java | 3202 ++++++++++++++---------------- src/client/jTPCCTerminal.java | 560 +++--- src/client/jTPCCUtil.java | 110 +- src/jdbc/ExecJDBC.java | 152 +- 11 files changed, 4086 insertions(+), 4518 deletions(-) diff --git a/src/LoadData/LoadData.java b/src/LoadData/LoadData.java index 46eac10..8355797 100644 --- a/src/LoadData/LoadData.java +++ b/src/LoadData/LoadData.java @@ -7,393 +7,361 @@ * */ -import java.sql.*; -import java.util.*; -import java.io.*; -import java.lang.Integer; - -public class LoadData -{ - private static Properties ini = new Properties(); - private static String db; - private static Properties dbProps; - private static jTPCCRandom rnd; - private static String fileLocation = null; - private static String csvNullValue = null; - - private static int numWarehouses; - private static int numWorkers; - private static int nextJob = 0; - private static Object nextJobLock = new Object(); +import java.io.BufferedWriter; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +public class LoadData { + private static Properties ini = new Properties(); + private static String db; + private static Properties dbProps; + private static jTPCCRandom rnd; + private static String fileLocation = null; + private static String csvNullValue = null; + + private static int numWarehouses; + private static int numWorkers; + private static int nextJob = 0; + private static Object nextJobLock = new Object(); private static LoadDataWorker[] workers; - private static Thread[] workerThreads; - - private static String[] argv; - - private static boolean writeCSV = false; - private static BufferedWriter configCSV = null; - private static BufferedWriter itemCSV = null; - private static BufferedWriter warehouseCSV = null; - private static BufferedWriter districtCSV = null; - private static BufferedWriter stockCSV = null; - private static BufferedWriter customerCSV = null; - private static BufferedWriter historyCSV = null; - private static BufferedWriter orderCSV = null; - private static BufferedWriter orderLineCSV = null; - private static BufferedWriter newOrderCSV = null; + private static Thread[] workerThreads; + + private static String[] argv; + + private static boolean writeCSV = false; + private static BufferedWriter configCSV = null; + private static BufferedWriter itemCSV = null; + private static BufferedWriter warehouseCSV = null; + private static BufferedWriter districtCSV = null; + private static BufferedWriter stockCSV = null; + private static BufferedWriter customerCSV = null; + private static BufferedWriter historyCSV = null; + private static BufferedWriter orderCSV = null; + private static BufferedWriter orderLineCSV = null; + private static BufferedWriter newOrderCSV = null; + + private static final Lock configCSVLock = new ReentrantLock(); + private static final Lock itemCSVLock = new ReentrantLock(); + private static final Lock warehouseCSVLock = new ReentrantLock(); + private static final Lock districtCSVLock = new ReentrantLock(); + private static final Lock stockCSVLock = new ReentrantLock(); + private static final Lock customerCSVLock = new ReentrantLock(); + private static final Lock historyCSVLock = new ReentrantLock(); + private static final Lock orderCSVLock = new ReentrantLock(); + private static final Lock orderLineCSVLock = new ReentrantLock(); + private static final Lock newOrderCSVLock = new ReentrantLock(); + private static final Lock jobLock = new ReentrantLock(); public static void main(String[] args) { - int i; - - System.out.println("Starting BenchmarkSQL LoadData"); - System.out.println(""); - - /* - * Load the Benchmark properties file. - */ - try - { - ini.load(new FileInputStream(System.getProperty("prop"))); - } - catch (IOException e) - { - System.err.println("ERROR: " + e.getMessage()); - System.exit(1); - } - argv = args; - - /* - * Initialize the global Random generator that picks the - * C values for the load. - */ - rnd = new jTPCCRandom(); - - /* - * Load the JDBC driver and prepare the db and dbProps. - */ - try { - Class.forName(iniGetString("driver")); - } - catch (Exception e) - { - System.err.println("ERROR: cannot load JDBC driver - " + - e.getMessage()); - System.exit(1); - } - db = iniGetString("conn"); - dbProps = new Properties(); - dbProps.setProperty("user", iniGetString("user")); - dbProps.setProperty("password", iniGetString("password")); - - /* - * Parse other vital information from the props file. - */ - numWarehouses = iniGetInt("warehouses"); - numWorkers = iniGetInt("loadWorkers", 4); - fileLocation = iniGetString("fileLocation"); - csvNullValue = iniGetString("csvNullValue", "NULL"); - - /* - * If CSV files are requested, open them all. - */ - if (fileLocation != null) - { - writeCSV = true; - - try - { - configCSV = new BufferedWriter(new FileWriter(fileLocation + - "bmsql_config.csv")); - itemCSV = new BufferedWriter(new FileWriter(fileLocation + - "bmsql_item.csv")); - warehouseCSV = new BufferedWriter(new FileWriter(fileLocation + - "bmsql_warehouse.csv")); - districtCSV = new BufferedWriter(new FileWriter(fileLocation + - "bmsql_district.csv")); - stockCSV = new BufferedWriter(new FileWriter(fileLocation + - "bmsql_stock.csv")); - customerCSV = new BufferedWriter(new FileWriter(fileLocation + - "bmsql_customer.csv")); - historyCSV = new BufferedWriter(new FileWriter(fileLocation + - "bmsql_history.csv")); - orderCSV = new BufferedWriter(new FileWriter(fileLocation + - "bmsql_oorder.csv")); - orderLineCSV = new BufferedWriter(new FileWriter(fileLocation + - "bmsql_order_line.csv")); - newOrderCSV = new BufferedWriter(new FileWriter(fileLocation + - "bmsql_new_order.csv")); - } - catch (IOException ie) - { - System.err.println(ie.getMessage()); - System.exit(3); - } - } - - System.out.println(""); - - /* - * Create the number of requested workers and start them. - */ - workers = new LoadDataWorker[numWorkers]; - workerThreads = new Thread[numWorkers]; - for (i = 0; i < numWorkers; i++) - { - Connection dbConn; - - try - { - dbConn = DriverManager.getConnection(db, dbProps); - dbConn.setAutoCommit(false); - if (writeCSV) - workers[i] = new LoadDataWorker(i, csvNullValue, - rnd.newRandom()); - else - workers[i] = new LoadDataWorker(i, dbConn, - rnd.newRandom()); - workerThreads[i] = new Thread(workers[i]); - workerThreads[i].start(); - } - catch (SQLException se) - { - System.err.println("ERROR: " + se.getMessage()); - System.exit(3); - return; - } - - } - - for (i = 0; i < numWorkers; i++) - { - try { - workerThreads[i].join(); - } - catch (InterruptedException ie) - { - System.err.println("ERROR: worker " + i + " - " + - ie.getMessage()); - System.exit(4); - } - } - - /* - * Close the CSV files if we are writing them. - */ - if (writeCSV) - { - try - { - configCSV.close(); - itemCSV.close(); - warehouseCSV.close(); - districtCSV.close(); - stockCSV.close(); - customerCSV.close(); - historyCSV.close(); - orderCSV.close(); - orderLineCSV.close(); - newOrderCSV.close(); - } - catch (IOException ie) - { - System.err.println(ie.getMessage()); - System.exit(3); - } - } + int i; + + System.out.println("Starting BenchmarkSQL LoadData"); + System.out.println(""); + + /* + * Load the Benchmark properties file. + */ + try { + ini.load(new FileInputStream(System.getProperty("prop"))); + } catch (IOException e) { + System.err.println("ERROR: " + e.getMessage()); + System.exit(1); + } + argv = args; + + /* + * Initialize the global Random generator that picks the + * C values for the load. + */ + rnd = new jTPCCRandom(); + + /* + * Load the JDBC driver and prepare the db and dbProps. + */ + try { + Class.forName(iniGetString("driver")); + } catch (Exception e) { + System.err.println("ERROR: cannot load JDBC driver - " + e.getMessage()); + System.exit(1); + } + db = iniGetString("conn"); + dbProps = new Properties(); + dbProps.setProperty("user", iniGetString("user")); + dbProps.setProperty("password", iniGetString("password")); + + /* + * Parse other vital information from the props file. + */ + numWarehouses = iniGetInt("warehouses"); + numWorkers = iniGetInt("loadWorkers", 4); + fileLocation = iniGetString("fileLocation"); + csvNullValue = iniGetString("csvNullValue", "NULL"); + + /* + * If CSV files are requested, open them all. + */ + if (fileLocation != null) { + writeCSV = true; + + try { + configCSV = new BufferedWriter(new FileWriter(fileLocation + "bmsql_config.csv")); + itemCSV = new BufferedWriter(new FileWriter(fileLocation + "bmsql_item.csv")); + warehouseCSV = new BufferedWriter(new FileWriter(fileLocation + "bmsql_warehouse.csv")); + districtCSV = new BufferedWriter(new FileWriter(fileLocation + "bmsql_district.csv")); + stockCSV = new BufferedWriter(new FileWriter(fileLocation + "bmsql_stock.csv")); + customerCSV = new BufferedWriter(new FileWriter(fileLocation + "bmsql_customer.csv")); + historyCSV = new BufferedWriter(new FileWriter(fileLocation + "bmsql_history.csv")); + orderCSV = new BufferedWriter(new FileWriter(fileLocation + "bmsql_oorder.csv")); + orderLineCSV = new BufferedWriter(new FileWriter(fileLocation + "bmsql_order_line.csv")); + newOrderCSV = new BufferedWriter(new FileWriter(fileLocation + "bmsql_new_order.csv")); + } catch (IOException ie) { + System.err.println(ie.getMessage()); + System.exit(3); + } + } + + System.out.println(""); + + /* + * Create the number of requested workers and start them. + */ + workers = new LoadDataWorker[numWorkers]; + workerThreads = new Thread[numWorkers]; + for (i = 0; i < numWorkers; i++) { + Connection dbConn; + + try { + dbConn = DriverManager.getConnection(db, dbProps); + dbConn.setAutoCommit(false); + if (writeCSV) + workers[i] = new LoadDataWorker(i, csvNullValue, rnd.newRandom()); + else + workers[i] = new LoadDataWorker(i, dbConn, rnd.newRandom()); + workerThreads[i] = new Thread(workers[i]); + workerThreads[i].start(); + } catch (SQLException se) { + System.err.println("ERROR: " + se.getMessage()); + System.exit(3); + return; + } + } + + for (i = 0; i < numWorkers; i++) { + try { + workerThreads[i].join(); + } catch (InterruptedException ie) { + System.err.println("ERROR: worker " + i + " - " + ie.getMessage()); + System.exit(4); + } + } + + /* + * Close the CSV files if we are writing them. + */ + if (writeCSV) { + try { + configCSV.close(); + itemCSV.close(); + warehouseCSV.close(); + districtCSV.close(); + stockCSV.close(); + customerCSV.close(); + historyCSV.close(); + orderCSV.close(); + orderLineCSV.close(); + newOrderCSV.close(); + } catch (IOException ie) { + System.err.println(ie.getMessage()); + System.exit(3); + } + } } // End of main() - public static void configAppend(StringBuffer buf) - throws IOException - { - synchronized(configCSV) - { - configCSV.write(buf.toString()); - } - buf.setLength(0); + public static void configAppend(StringBuffer buf) throws IOException { + try { + configCSVLock.lock(); + configCSV.write(buf.toString()); + } finally { + buf.setLength(0); + configCSVLock.unlock(); + } } - public static void itemAppend(StringBuffer buf) - throws IOException - { - synchronized(itemCSV) - { - itemCSV.write(buf.toString()); - } - buf.setLength(0); + public static void itemAppend(StringBuffer buf) throws IOException { + try { + itemCSVLock.lock(); + itemCSV.write(buf.toString()); + } finally { + buf.setLength(0); + itemCSVLock.unlock(); + } } - public static void warehouseAppend(StringBuffer buf) - throws IOException - { - synchronized(warehouseCSV) - { - warehouseCSV.write(buf.toString()); - } - buf.setLength(0); + public static void warehouseAppend(StringBuffer buf) throws IOException { + try { + warehouseCSVLock.lock(); + warehouseCSV.write(buf.toString()); + } finally { + buf.setLength(0); + warehouseCSVLock.unlock(); + } } - public static void districtAppend(StringBuffer buf) - throws IOException - { - synchronized(districtCSV) - { - districtCSV.write(buf.toString()); - } - buf.setLength(0); + public static void districtAppend(StringBuffer buf) throws IOException { + try { + districtCSVLock.lock(); + districtCSV.write(buf.toString()); + } finally { + buf.setLength(0); + districtCSVLock.unlock(); + } } - public static void stockAppend(StringBuffer buf) - throws IOException - { - synchronized(stockCSV) - { - stockCSV.write(buf.toString()); - } - buf.setLength(0); + public static void stockAppend(StringBuffer buf) throws IOException { + try { + stockCSVLock.lock(); + stockCSV.write(buf.toString()); + } finally { + buf.setLength(0); + stockCSVLock.unlock(); + } } - public static void customerAppend(StringBuffer buf) - throws IOException - { - synchronized(customerCSV) - { - customerCSV.write(buf.toString()); - } - buf.setLength(0); + public static void customerAppend(StringBuffer buf) throws IOException { + try { + customerCSVLock.lock(); + customerCSV.write(buf.toString()); + } finally { + buf.setLength(0); + customerCSVLock.unlock(); + } } - public static void historyAppend(StringBuffer buf) - throws IOException - { - synchronized(historyCSV) - { - historyCSV.write(buf.toString()); - } - buf.setLength(0); + public static void historyAppend(StringBuffer buf) throws IOException { + try { + historyCSVLock.lock(); + historyCSV.write(buf.toString()); + } finally { + buf.setLength(0); + historyCSVLock.unlock(); + } } - public static void orderAppend(StringBuffer buf) - throws IOException - { - synchronized(orderCSV) - { - orderCSV.write(buf.toString()); - } - buf.setLength(0); + public static void orderAppend(StringBuffer buf) throws IOException { + try { + orderCSVLock.lock(); + orderCSV.write(buf.toString()); + } finally { + buf.setLength(0); + orderCSVLock.unlock(); + } } - public static void orderLineAppend(StringBuffer buf) - throws IOException - { - synchronized(orderLineCSV) - { - orderLineCSV.write(buf.toString()); - } - buf.setLength(0); + public static void orderLineAppend(StringBuffer buf) throws IOException { + try { + orderLineCSVLock.lock(); + orderLineCSV.write(buf.toString()); + } finally { + buf.setLength(0); + orderLineCSVLock.unlock(); + } } - public static void newOrderAppend(StringBuffer buf) - throws IOException - { - synchronized(newOrderCSV) - { - newOrderCSV.write(buf.toString()); - } - buf.setLength(0); + public static void newOrderAppend(StringBuffer buf) throws IOException { + try { + newOrderCSVLock.lock(); + newOrderCSV.write(buf.toString()); + } finally { + buf.setLength(0); + newOrderCSVLock.unlock(); + } } - public static int getNextJob() - { - int job; - - synchronized(nextJobLock) - { - if (nextJob > numWarehouses) - job = -1; - else - job = nextJob++; - } - - return job; + public static int getNextJob() { + int job; + try { + jobLock.lock(); + if (nextJob > numWarehouses) + job = -1; + else + job = nextJob++; + } finally { + jobLock.unlock(); + } + return job; } - public static int getNumWarehouses() - { - return numWarehouses; + public static int getNumWarehouses() { + return numWarehouses; } - private static String iniGetString(String name) - { - String strVal = null; - - for (int i = 0; i < argv.length - 1; i += 2) - { - if (name.toLowerCase().equals(argv[i].toLowerCase())) - { - strVal = argv[i + 1]; - break; - } - } - - if (strVal == null) - strVal = ini.getProperty(name); - - if (strVal == null) - System.out.println(name + " (not defined)"); - else - if (name.equals("password")) - System.out.println(name + "=***********"); - else - System.out.println(name + "=" + strVal); - return strVal; + private static String iniGetString(String name) { + String strVal = null; + + for (int i = 0; i < argv.length - 1; i += 2) { + if (name.toLowerCase().equals(argv[i].toLowerCase())) { + strVal = argv[i + 1]; + break; + } + } + + if (strVal == null) + strVal = ini.getProperty(name); + + if (strVal == null) + System.out.println(name + " (not defined)"); + else if (name.equals("password")) + System.out.println(name + "=***********"); + else + System.out.println(name + "=" + strVal); + return strVal; } - private static String iniGetString(String name, String defVal) - { - String strVal = null; - - for (int i = 0; i < argv.length - 1; i += 2) - { - if (name.toLowerCase().equals(argv[i].toLowerCase())) - { - strVal = argv[i + 1]; - break; - } - } - - if (strVal == null) - strVal = ini.getProperty(name); - - if (strVal == null) - { - System.out.println(name + " (not defined - using default '" + - defVal + "')"); - return defVal; - } - else - if (name.equals("password")) - System.out.println(name + "=***********"); - else - System.out.println(name + "=" + strVal); - return strVal; + private static String iniGetString(String name, String defVal) { + String strVal = null; + + for (int i = 0; i < argv.length - 1; i += 2) { + if (name.toLowerCase().equals(argv[i].toLowerCase())) { + strVal = argv[i + 1]; + break; + } + } + + if (strVal == null) + strVal = ini.getProperty(name); + + if (strVal == null) { + System.out.println(name + " (not defined - using default '" + + defVal + "')"); + return defVal; + } else if (name.equals("password")) + System.out.println(name + "=***********"); + else + System.out.println(name + "=" + strVal); + return strVal; } - private static int iniGetInt(String name) - { - String strVal = iniGetString(name); + private static int iniGetInt(String name) { + String strVal = iniGetString(name); - if (strVal == null) - return 0; - return Integer.parseInt(strVal); + if (strVal == null) + return 0; + return Integer.parseInt(strVal); } - private static int iniGetInt(String name, int defVal) - { - String strVal = iniGetString(name); + private static int iniGetInt(String name, int defVal) { + String strVal = iniGetString(name); - if (strVal == null) - return defVal; - return Integer.parseInt(strVal); + if (strVal == null) + return defVal; + return Integer.parseInt(strVal); } } diff --git a/src/LoadData/LoadDataWorker.java b/src/LoadData/LoadDataWorker.java index a6dc8f3..510d00f 100644 --- a/src/LoadData/LoadDataWorker.java +++ b/src/LoadData/LoadDataWorker.java @@ -7,223 +7,209 @@ * */ -import java.sql.*; -import java.util.*; -import java.io.*; - -public class LoadDataWorker implements Runnable -{ - private int worker; - private Connection dbConn; - private jTPCCRandom rnd; - - private StringBuffer sb; - private Formatter fmt; - - private boolean writeCSV = false; - private String csvNull = null; - - private PreparedStatement stmtConfig = null; - private PreparedStatement stmtItem = null; - private PreparedStatement stmtWarehouse = null; - private PreparedStatement stmtDistrict = null; - private PreparedStatement stmtStock = null; - private PreparedStatement stmtCustomer = null; - private PreparedStatement stmtHistory = null; - private PreparedStatement stmtOrder = null; - private PreparedStatement stmtOrderLine = null; - private PreparedStatement stmtNewOrder = null; - - private StringBuffer sbConfig = null; - private Formatter fmtConfig = null; - private StringBuffer sbItem = null; - private Formatter fmtItem = null; - private StringBuffer sbWarehouse = null; - private Formatter fmtWarehouse = null; - private StringBuffer sbDistrict = null; - private Formatter fmtDistrict = null; - private StringBuffer sbStock = null; - private Formatter fmtStock = null; - private StringBuffer sbCustomer = null; - private Formatter fmtCustomer = null; - private StringBuffer sbHistory = null; - private Formatter fmtHistory = null; - private StringBuffer sbOrder = null; - private Formatter fmtOrder = null; - private StringBuffer sbOrderLine = null; - private Formatter fmtOrderLine = null; - private StringBuffer sbNewOrder = null; - private Formatter fmtNewOrder = null; - - LoadDataWorker(int worker, String csvNull, jTPCCRandom rnd) - { - this.worker = worker; - this.csvNull = csvNull; - this.rnd = rnd; - - this.sb = new StringBuffer(); - this.fmt = new Formatter(sb); - this.writeCSV = true; - - this.sbConfig = new StringBuffer(); - this.fmtConfig = new Formatter(sbConfig); - this.sbItem = new StringBuffer(); - this.fmtItem = new Formatter(sbItem); - this.sbWarehouse = new StringBuffer(); - this.fmtWarehouse = new Formatter(sbWarehouse); - this.sbDistrict = new StringBuffer(); - this.fmtDistrict = new Formatter(sbDistrict); - this.sbStock = new StringBuffer(); - this.fmtStock = new Formatter(sbStock); - this.sbCustomer = new StringBuffer(); - this.fmtCustomer = new Formatter(sbCustomer); - this.sbHistory = new StringBuffer(); - this.fmtHistory = new Formatter(sbHistory); - this.sbOrder = new StringBuffer(); - this.fmtOrder = new Formatter(sbOrder); - this.sbOrderLine = new StringBuffer(); - this.fmtOrderLine = new Formatter(sbOrderLine); - this.sbNewOrder = new StringBuffer(); - this.fmtNewOrder = new Formatter(sbNewOrder); +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Formatter; + +public class LoadDataWorker implements Runnable { + private int worker; + private Connection dbConn; + private jTPCCRandom rnd; + + private StringBuffer sb; + private Formatter fmt; + + private boolean writeCSV = false; + private String csvNull = null; + + private PreparedStatement stmtConfig = null; + private PreparedStatement stmtItem = null; + private PreparedStatement stmtWarehouse = null; + private PreparedStatement stmtDistrict = null; + private PreparedStatement stmtStock = null; + private PreparedStatement stmtCustomer = null; + private PreparedStatement stmtHistory = null; + private PreparedStatement stmtOrder = null; + private PreparedStatement stmtOrderLine = null; + private PreparedStatement stmtNewOrder = null; + + private StringBuffer sbConfig = null; + private Formatter fmtConfig = null; + private StringBuffer sbItem = null; + private Formatter fmtItem = null; + private StringBuffer sbWarehouse = null; + private Formatter fmtWarehouse = null; + private StringBuffer sbDistrict = null; + private Formatter fmtDistrict = null; + private StringBuffer sbStock = null; + private Formatter fmtStock = null; + private StringBuffer sbCustomer = null; + private Formatter fmtCustomer = null; + private StringBuffer sbHistory = null; + private Formatter fmtHistory = null; + private StringBuffer sbOrder = null; + private Formatter fmtOrder = null; + private StringBuffer sbOrderLine = null; + private Formatter fmtOrderLine = null; + private StringBuffer sbNewOrder = null; + private Formatter fmtNewOrder = null; + + LoadDataWorker(int worker, String csvNull, jTPCCRandom rnd) { + this.worker = worker; + this.csvNull = csvNull; + this.rnd = rnd; + + this.sb = new StringBuffer(); + this.fmt = new Formatter(sb); + this.writeCSV = true; + + this.sbConfig = new StringBuffer(); + this.fmtConfig = new Formatter(sbConfig); + this.sbItem = new StringBuffer(); + this.fmtItem = new Formatter(sbItem); + this.sbWarehouse = new StringBuffer(); + this.fmtWarehouse = new Formatter(sbWarehouse); + this.sbDistrict = new StringBuffer(); + this.fmtDistrict = new Formatter(sbDistrict); + this.sbStock = new StringBuffer(); + this.fmtStock = new Formatter(sbStock); + this.sbCustomer = new StringBuffer(); + this.fmtCustomer = new Formatter(sbCustomer); + this.sbHistory = new StringBuffer(); + this.fmtHistory = new Formatter(sbHistory); + this.sbOrder = new StringBuffer(); + this.fmtOrder = new Formatter(sbOrder); + this.sbOrderLine = new StringBuffer(); + this.fmtOrderLine = new Formatter(sbOrderLine); + this.sbNewOrder = new StringBuffer(); + this.fmtNewOrder = new Formatter(sbNewOrder); } LoadDataWorker(int worker, Connection dbConn, jTPCCRandom rnd) - throws SQLException - { - this.worker = worker; - this.dbConn = dbConn; - this.rnd = rnd; - - this.sb = new StringBuffer(); - this.fmt = new Formatter(sb); - - stmtConfig = dbConn.prepareStatement( - "INSERT INTO bmsql_config (" + - " cfg_name, cfg_value) " + - "VALUES (?, ?)" - ); - stmtItem = dbConn.prepareStatement( - "INSERT INTO bmsql_item (" + - " i_id, i_name, i_price, i_data, i_im_id) " + - "VALUES (?, ?, ?, ?, ?)" - ); - stmtWarehouse = dbConn.prepareStatement( - "INSERT INTO bmsql_warehouse (" + - " w_id, w_ytd, w_tax, w_name, w_street_1, w_street_2, w_city, " + - " w_state, w_zip) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" - ); - stmtStock = dbConn.prepareStatement( - "INSERT INTO bmsql_stock ("+ - " s_w_id, s_i_id, s_quantity, s_ytd, s_order_cnt, s_remote_cnt, 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) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" - ); - stmtDistrict = dbConn.prepareStatement( - "INSERT INTO bmsql_district ("+ - " d_w_id, d_id, d_ytd, d_tax, d_next_o_id, d_name, d_street_1, d_street_2, " + - " d_city, d_state, d_zip) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" - ); - stmtCustomer = dbConn.prepareStatement( - "INSERT INTO bmsql_customer (" + - " c_w_id, c_d_id, c_id, c_discount, c_credit, c_last, c_first, c_credit_lim, " + - " c_balance, c_ytd_payment, c_payment_cnt, c_delivery_cnt, " + - " c_street_1, c_street_2, c_city, c_state, c_zip, " + - " c_phone, c_since, c_middle, c_data) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + - " ?, ?, ?, ?, ?, ?)" - ); - stmtHistory = dbConn.prepareStatement( - "INSERT INTO bmsql_history (" + - " hist_id, h_c_id, h_c_d_id, h_c_w_id, h_d_id, h_w_id, " + - " h_date, h_amount, h_data) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" - ); - stmtOrder = dbConn.prepareStatement( - "INSERT INTO bmsql_oorder (" + - " o_w_id, o_d_id, o_id, o_c_id, " + - " o_carrier_id, o_ol_cnt, o_all_local, o_entry_d) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?)" - ); - stmtOrderLine = dbConn.prepareStatement( - "INSERT INTO bmsql_order_line (" + - " ol_w_id, ol_d_id, ol_o_id, ol_number, ol_i_id, " + - " ol_delivery_d, ol_amount, ol_supply_w_id, ol_quantity, " + - " ol_dist_info) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" - ); - stmtNewOrder = dbConn.prepareStatement( - "INSERT INTO bmsql_new_order (" + - " no_w_id, no_d_id, no_o_id) " + - "VALUES (?, ?, ?)" - ); + throws SQLException { + this.worker = worker; + this.dbConn = dbConn; + this.rnd = rnd; + + this.sb = new StringBuffer(); + this.fmt = new Formatter(sb); + + stmtConfig = dbConn.prepareStatement( + "INSERT INTO bmsql_config (" + + " cfg_name, cfg_value) " + + "VALUES (?, ?)" + ); + stmtItem = dbConn.prepareStatement( + "INSERT INTO bmsql_item (" + + " i_id, i_name, i_price, i_data, i_im_id) " + + "VALUES (?, ?, ?, ?, ?)" + ); + stmtWarehouse = dbConn.prepareStatement( + "INSERT INTO bmsql_warehouse (" + + " w_id, w_ytd, w_tax, w_name, w_street_1, w_street_2, w_city, " + + " w_state, w_zip) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" + ); + stmtStock = dbConn.prepareStatement( + "INSERT INTO bmsql_stock (" + + " s_w_id, s_i_id, s_quantity, s_ytd, s_order_cnt, s_remote_cnt, 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) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + ); + stmtDistrict = dbConn.prepareStatement( + "INSERT INTO bmsql_district (" + + " d_w_id, d_id, d_ytd, d_tax, d_next_o_id, d_name, d_street_1, d_street_2, " + + " d_city, d_state, d_zip) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + ); + stmtCustomer = dbConn.prepareStatement( + "INSERT INTO bmsql_customer (" + + " c_w_id, c_d_id, c_id, c_discount, c_credit, c_last, c_first, c_credit_lim, " + + " c_balance, c_ytd_payment, c_payment_cnt, c_delivery_cnt, " + + " c_street_1, c_street_2, c_city, c_state, c_zip, " + + " c_phone, c_since, c_middle, c_data) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + + " ?, ?, ?, ?, ?, ?)" + ); + stmtHistory = dbConn.prepareStatement( + "INSERT INTO bmsql_history (" + + " hist_id, h_c_id, h_c_d_id, h_c_w_id, h_d_id, h_w_id, " + + " h_date, h_amount, h_data) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" + ); + stmtOrder = dbConn.prepareStatement( + "INSERT INTO bmsql_oorder (" + + " o_w_id, o_d_id, o_id, o_c_id, " + + " o_carrier_id, o_ol_cnt, o_all_local, o_entry_d) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)" + ); + stmtOrderLine = dbConn.prepareStatement( + "INSERT INTO bmsql_order_line (" + + " ol_w_id, ol_d_id, ol_o_id, ol_number, ol_i_id, " + + " ol_delivery_d, ol_amount, ol_supply_w_id, ol_quantity, " + + " ol_dist_info) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + ); + stmtNewOrder = dbConn.prepareStatement( + "INSERT INTO bmsql_new_order (" + + " no_w_id, no_d_id, no_o_id) " + + "VALUES (?, ?, ?)" + ); } /* * run() */ - public void run() - { - int job; - - try - { - while ((job = LoadData.getNextJob()) >= 0) - { - if (job == 0) - { - fmt.format("Worker %03d: Loading ITEM", worker); - System.out.println(sb.toString()); - sb.setLength(0); - - loadItem(); - - fmt.format("Worker %03d: Loading ITEM done", worker); - System.out.println(sb.toString()); - sb.setLength(0); - } - else - { - fmt.format("Worker %03d: Loading Warehouse %6d", - worker, job); - System.out.println(sb.toString()); - sb.setLength(0); - - loadWarehouse(job); - - fmt.format("Worker %03d: Loading Warehouse %6d done", - worker, job); - System.out.println(sb.toString()); - sb.setLength(0); - } - } - - /* - * Close the DB connection if in direct DB mode. - */ - if (!writeCSV) - dbConn.close(); - } - catch (SQLException se) - { - while (se != null) - { - fmt.format("Worker %03d: ERROR: %s", worker, se.getMessage()); - System.err.println(sb.toString()); - sb.setLength(0); - se = se.getNextException(); - } - } - catch (Exception e) - { - fmt.format("Worker %03d: ERROR: %s", worker, e.getMessage()); - System.err.println(sb.toString()); - sb.setLength(0); - e.printStackTrace(); - return; - } + public void run() { + int job; + + try { + while ((job = LoadData.getNextJob()) >= 0) { + if (job == 0) { + fmt.format("Worker %03d: Loading ITEM", worker); + System.out.println(sb.toString()); + sb.setLength(0); + + loadItem(); + + fmt.format("Worker %03d: Loading ITEM done", worker); + System.out.println(sb.toString()); + sb.setLength(0); + } else { + fmt.format("Worker %03d: Loading Warehouse %6d", worker, job); + System.out.println(sb.toString()); + sb.setLength(0); + + loadWarehouse(job); + + fmt.format("Worker %03d: Loading Warehouse %6d done", worker, job); + System.out.println(sb.toString()); + sb.setLength(0); + } + } + + /* + * Close the DB connection if in direct DB mode. + */ + if (!writeCSV) + dbConn.close(); + } catch (SQLException se) { + while (se != null) { + fmt.format("Worker %03d: ERROR: %s", worker, se.getMessage()); + System.err.println(sb.toString()); + sb.setLength(0); + se = se.getNextException(); + } + } catch (Exception e) { + fmt.format("Worker %03d: ERROR: %s", worker, e.getMessage()); + System.err.println(sb.toString()); + sb.setLength(0); + e.printStackTrace(); + return; + } } // End run() /* ---- @@ -232,112 +218,93 @@ public void run() * Load the content of the ITEM table. * ---- */ - private void loadItem() - throws SQLException, IOException - { - int i_id; - - if (writeCSV) - { - /* - * Saving CONFIG information in CSV mode. - */ - fmtConfig.format("warehouses,%d\n", LoadData.getNumWarehouses()); - fmtConfig.format("nURandCLast,%d\n", rnd.getNURandCLast()); - fmtConfig.format("nURandCC_ID,%d\n", rnd.getNURandCC_ID()); - fmtConfig.format("nURandCI_ID,%d\n", rnd.getNURandCI_ID()); - - LoadData.configAppend(sbConfig); - } - else - { - /* - * Saving CONFIG information in DB mode. - */ - stmtConfig.setString(1, "warehouses"); - stmtConfig.setString(2, "" + LoadData.getNumWarehouses()); - stmtConfig.execute(); - - stmtConfig.setString(1, "nURandCLast"); - stmtConfig.setString(2, "" + rnd.getNURandCLast()); - stmtConfig.execute(); - - stmtConfig.setString(1, "nURandCC_ID"); - stmtConfig.setString(2, "" + rnd.getNURandCC_ID()); - stmtConfig.execute(); - - stmtConfig.setString(1, "nURandCI_ID"); - stmtConfig.setString(2, "" + rnd.getNURandCI_ID()); - stmtConfig.execute(); - } - - for (i_id = 1; i_id <= 100000; i_id++) - { - String iData; - - if (i_id != 1 && (i_id - 1) % 100 == 0) - { - if (writeCSV) - { - LoadData.itemAppend(sbItem); - } - else - { - stmtItem.executeBatch(); - stmtItem.clearBatch(); - dbConn.commit(); - } - } - - // Clause 4.3.3.1 for ITEM - if (rnd.nextInt(1, 100) <= 10) - { - int len = rnd.nextInt(26, 50); - int off = rnd.nextInt(0, len - 8); - - iData = rnd.getAString(off, off) + - "ORIGINAL" + - rnd.getAString(len - off - 8, len - off - 8); - } - else - { - iData = rnd.getAString(26, 50); - } - - if (writeCSV) - { - fmtItem.format("%d,%s,%.2f,%s,%d\n", - i_id, - rnd.getAString(14, 24), - ((double)rnd.nextLong(100, 10000)) / 100.0, - iData, - rnd.nextInt(1, 10000)); - - } - else - { - stmtItem.setInt(1, i_id); - stmtItem.setString(2, rnd.getAString(14, 24)); - stmtItem.setDouble(3, ((double)rnd.nextLong(100, 10000)) / 100.0); - stmtItem.setString(4, iData); - stmtItem.setInt(5, rnd.nextInt(1, 10000)); - - stmtItem.addBatch(); - } - } - - if (writeCSV) - { - LoadData.itemAppend(sbItem); - } - else - { - stmtItem.executeBatch(); - stmtItem.clearBatch(); - stmtItem.close(); - - dbConn.commit(); - } + private void loadItem() throws SQLException, IOException { + int i_id; + + if (writeCSV) { + /* + * Saving CONFIG information in CSV mode. + */ + fmtConfig.format("warehouses,%d\n", LoadData.getNumWarehouses()); + fmtConfig.format("nURandCLast,%d\n", rnd.getNURandCLast()); + fmtConfig.format("nURandCC_ID,%d\n", rnd.getNURandCC_ID()); + fmtConfig.format("nURandCI_ID,%d\n", rnd.getNURandCI_ID()); + + LoadData.configAppend(sbConfig); + } else { + /* + * Saving CONFIG information in DB mode. + */ + stmtConfig.setString(1, "warehouses"); + stmtConfig.setString(2, "" + LoadData.getNumWarehouses()); + stmtConfig.execute(); + + stmtConfig.setString(1, "nURandCLast"); + stmtConfig.setString(2, "" + rnd.getNURandCLast()); + stmtConfig.execute(); + + stmtConfig.setString(1, "nURandCC_ID"); + stmtConfig.setString(2, "" + rnd.getNURandCC_ID()); + stmtConfig.execute(); + + stmtConfig.setString(1, "nURandCI_ID"); + stmtConfig.setString(2, "" + rnd.getNURandCI_ID()); + stmtConfig.execute(); + } + + for (i_id = 1; i_id <= 100000; i_id++) { + String iData; + + if (i_id != 1 && (i_id - 1) % 100 == 0) { + if (writeCSV) { + LoadData.itemAppend(sbItem); + } else { + stmtItem.executeBatch(); + stmtItem.clearBatch(); + dbConn.commit(); + } + } + + // Clause 4.3.3.1 for ITEM + if (rnd.nextInt(1, 100) <= 10) { + int len = rnd.nextInt(26, 50); + int off = rnd.nextInt(0, len - 8); + + iData = rnd.getAString(off, off) + + "ORIGINAL" + + rnd.getAString(len - off - 8, len - off - 8); + } else { + iData = rnd.getAString(26, 50); + } + + if (writeCSV) { + fmtItem.format("%d,%s,%.2f,%s,%d\n", + i_id, + rnd.getAString(14, 24), + ((double) rnd.nextLong(100, 10000)) / 100.0, + iData, + rnd.nextInt(1, 10000)); + + } else { + stmtItem.setInt(1, i_id); + stmtItem.setString(2, rnd.getAString(14, 24)); + stmtItem.setDouble(3, ((double) rnd.nextLong(100, 10000)) / 100.0); + stmtItem.setString(4, iData); + stmtItem.setInt(5, rnd.nextInt(1, 10000)); + + stmtItem.addBatch(); + } + } + + if (writeCSV) { + LoadData.itemAppend(sbItem); + } else { + stmtItem.executeBatch(); + stmtItem.clearBatch(); + stmtItem.close(); + + dbConn.commit(); + } } // End loadItem() @@ -347,470 +314,416 @@ private void loadItem() * Load the content of one warehouse. * ---- */ - private void loadWarehouse(int w_id) - throws SQLException, IOException - { - /* - * Load the WAREHOUSE row. - */ - if (writeCSV) - { - fmtWarehouse.format("%d,%.2f,%.4f,%s,%s,%s,%s,%s,%s\n", - w_id, - 300000.0, - ((double)rnd.nextLong(0, 2000)) / 10000.0, - rnd.getAString(6, 10), - rnd.getAString(10, 20), - rnd.getAString(10, 20), - rnd.getAString(10, 20), - rnd.getState(), - rnd.getNString(4, 4) + "11111"); - - LoadData.warehouseAppend(sbWarehouse); - } - else - { - stmtWarehouse.setInt(1, w_id); - stmtWarehouse.setDouble(2, 300000.0); - stmtWarehouse.setDouble(3, ((double)rnd.nextLong(0, 2000)) / 10000.0); - stmtWarehouse.setString(4, rnd.getAString(6, 10)); - stmtWarehouse.setString(5, rnd.getAString(10, 20)); - stmtWarehouse.setString(6, rnd.getAString(10, 20)); - stmtWarehouse.setString(7, rnd.getAString(10, 20)); - stmtWarehouse.setString(8, rnd.getState()); - stmtWarehouse.setString(9, rnd.getNString(4, 4) + "11111"); - - stmtWarehouse.execute(); - } - - /* - * For each WAREHOUSE there are 100,000 STOCK rows. - */ - for (int s_i_id = 1; s_i_id <= 100000; s_i_id++) - { - String sData; - /* - * Load the data in batches of 500 rows. - */ - if (s_i_id != 1 && (s_i_id - 1) % 500 == 0) - { - if (writeCSV) - LoadData.warehouseAppend(sbWarehouse); - else - { - stmtStock.executeBatch(); - stmtStock.clearBatch(); - dbConn.commit(); - } - } - - // Clause 4.3.3.1 for STOCK - if (rnd.nextInt(1, 100) <= 10) - { - int len = rnd.nextInt(26, 50); - int off = rnd.nextInt(0, len - 8); - - sData = rnd.getAString(off, off) + - "ORIGINAL" + - rnd.getAString(len - off - 8, len - off - 8); - } - else - { - sData = rnd.getAString(26, 50); - } - - if (writeCSV) - { - fmtStock.format("%d,%d,%d,%d,%d,%d,%s," + - "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", - w_id, - s_i_id, - rnd.nextInt(10, 100), - 0, - 0, - 0, - sData, - rnd.getAString(24, 24), - rnd.getAString(24, 24), - rnd.getAString(24, 24), - rnd.getAString(24, 24), - rnd.getAString(24, 24), - rnd.getAString(24, 24), - rnd.getAString(24, 24), - rnd.getAString(24, 24), - rnd.getAString(24, 24), - rnd.getAString(24, 24)); - } - else - { - stmtStock.setInt(1, w_id); - stmtStock.setInt(2, s_i_id); - stmtStock.setInt(3, rnd.nextInt(10, 100)); - stmtStock.setInt(4, 0); - stmtStock.setInt(5, 0); - stmtStock.setInt(6, 0); - stmtStock.setString(7, sData); - stmtStock.setString(8, rnd.getAString(24, 24)); - stmtStock.setString(9, rnd.getAString(24, 24)); - stmtStock.setString(10, rnd.getAString(24, 24)); - stmtStock.setString(11, rnd.getAString(24, 24)); - stmtStock.setString(12, rnd.getAString(24, 24)); - stmtStock.setString(13, rnd.getAString(24, 24)); - stmtStock.setString(14, rnd.getAString(24, 24)); - stmtStock.setString(15, rnd.getAString(24, 24)); - stmtStock.setString(16, rnd.getAString(24, 24)); - stmtStock.setString(17, rnd.getAString(24, 24)); - - stmtStock.addBatch(); - } - - } - if (writeCSV) - { - LoadData.stockAppend(sbStock); - } - else - { - stmtStock.executeBatch(); - stmtStock.clearBatch(); - dbConn.commit(); - } - - /* - * For each WAREHOUSE there are 10 DISTRICT rows. - */ - for (int d_id = 1; d_id <= 10; d_id++) - { - if (writeCSV) - { - fmtDistrict.format("%d,%d,%.2f,%.4f,%d,%s,%s,%s,%s,%s,%s\n", - w_id, - d_id, - 30000.0, - ((double)rnd.nextLong(0, 2000)) / 10000.0, - 3001, - rnd.getAString(6, 10), - rnd.getAString(10, 20), - rnd.getAString(10, 20), - rnd.getAString(10, 20), - rnd.getState(), - rnd.getNString(4, 4) + "11111"); - - LoadData.districtAppend(sbDistrict); - } - else - { - stmtDistrict.setInt(1, w_id); - stmtDistrict.setInt(2, d_id); - stmtDistrict.setDouble(3, 30000.0); - stmtDistrict.setDouble(4, ((double)rnd.nextLong(0, 2000)) / 10000.0); - stmtDistrict.setInt(5, 3001); - stmtDistrict.setString(6, rnd.getAString(6, 10)); - stmtDistrict.setString(7, rnd.getAString(10, 20)); - stmtDistrict.setString(8, rnd.getAString(10, 20)); - stmtDistrict.setString(9, rnd.getAString(10, 20)); - stmtDistrict.setString(10, rnd.getState()); - stmtDistrict.setString(11, rnd.getNString(4, 4) + "11111"); - - stmtDistrict.execute(); - } - - /* - * Within each DISTRICT there are 3,000 CUSTOMERs. - */ - for (int c_id = 1; c_id <= 3000; c_id++) - { - // commit district and history when 200 records - if (c_id != 1 && (c_id - 1) % 200 == 0) - { - if (writeCSV){ - LoadData.customerAppend(sbCustomer); - LoadData.historyAppend(sbHistory); - } - else - { - stmtCustomer.executeBatch(); - stmtCustomer.clearBatch(); - dbConn.commit(); - - stmtHistory.executeBatch(); - stmtHistory.clearBatch(); - dbConn.commit(); - } - } - - if (writeCSV) - { - fmtCustomer.format("%d,%d,%d,%.4f,%s,%s,%s," + - "%.2f,%.2f,%.2f,%d,%d," + - "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", - w_id, - d_id, - c_id, - ((double)rnd.nextLong(0, 5000)) / 10000.0, - (rnd.nextInt(1, 100) <= 90) ? "GC" : "BC", - (c_id <= 1000) ? rnd.getCLast(c_id - 1) : rnd.getCLast(), - rnd.getAString(8, 16), - 50000.00, - -10.00, - 10.00, - 1, - 1, - rnd.getAString(10, 20), - rnd.getAString(10, 20), - rnd.getAString(10, 20), - rnd.getState(), - rnd.getNString(4, 4) + "11111", - rnd.getNString(16, 16), - new java.sql.Timestamp(System.currentTimeMillis()).toString(), - "OE", - rnd.getAString(300, 500)); - } - else - { - stmtCustomer.setInt(1, w_id); - stmtCustomer.setInt(2, d_id); - stmtCustomer.setInt(3, c_id); - stmtCustomer.setDouble(4, ((double)rnd.nextLong(0, 5000)) / 10000.0); - if (rnd.nextInt(1, 100) <= 90) - stmtCustomer.setString(5, "GC"); - else - stmtCustomer.setString(5, "BC"); - if (c_id <= 1000) - stmtCustomer.setString(6, rnd.getCLast(c_id - 1)); - else - stmtCustomer.setString(6, rnd.getCLast()); - stmtCustomer.setString(7, rnd.getAString(8, 16)); - stmtCustomer.setDouble(8, 50000.00); - stmtCustomer.setDouble(9, -10.00); - stmtCustomer.setDouble(10, 10.00); - stmtCustomer.setInt(11, 1); - stmtCustomer.setInt(12, 1); - stmtCustomer.setString(13, rnd.getAString(10, 20)); - stmtCustomer.setString(14, rnd.getAString(10, 20)); - stmtCustomer.setString(15, rnd.getAString(10, 20)); - stmtCustomer.setString(16, rnd.getState()); - stmtCustomer.setString(17, rnd.getNString(4, 4) + "11111"); - stmtCustomer.setString(18, rnd.getNString(16, 16)); - stmtCustomer.setTimestamp(19, new java.sql.Timestamp(System.currentTimeMillis())); - stmtCustomer.setString(20, "OE"); - stmtCustomer.setString(21, rnd.getAString(300, 500)); - - stmtCustomer.addBatch(); - } - - /* - * For each CUSTOMER there is one row in HISTORY. - */ - if (writeCSV) - { - fmtHistory.format("%d,%d,%d,%d,%d,%d,%s,%.2f,%s\n", - (w_id - 1) * 30000 + (d_id - 1) * 3000 + c_id, - c_id, - d_id, - w_id, - d_id, - w_id, - new java.sql.Timestamp(System.currentTimeMillis()).toString(), - 10.00, - rnd.getAString(12, 24)); - } - else - { - stmtHistory.setInt(1, (w_id - 1) * 30000 + (d_id - 1) * 3000 + c_id); - stmtHistory.setInt(2, c_id); - stmtHistory.setInt(3, d_id); - stmtHistory.setInt(4, w_id); - stmtHistory.setInt(5, d_id); - stmtHistory.setInt(6, w_id); - stmtHistory.setTimestamp(7, new java.sql.Timestamp(System.currentTimeMillis())); - stmtHistory.setDouble(8, 10.00); - stmtHistory.setString(9, rnd.getAString(12, 24)); - - stmtHistory.addBatch(); - } - } - - if (writeCSV) - { - LoadData.customerAppend(sbCustomer); - LoadData.historyAppend(sbHistory); - } - else - { - stmtCustomer.executeBatch(); - stmtCustomer.clearBatch(); - dbConn.commit(); - stmtHistory.executeBatch(); - stmtHistory.clearBatch(); - dbConn.commit(); - } - - /* - * For the ORDER rows the TPC-C specification demands that they - * are generated using a random permutation of all 3,000 - * customers. To do that we set up an array with all C_IDs - * and then randomly shuffle it. - */ - int randomCID[] = new int[3000]; - for (int i = 0; i < 3000; i++) - randomCID[i] = i + 1; - for (int i = 0; i < 3000; i++) - { - int x = rnd.nextInt(0, 2999); - int y = rnd.nextInt(0, 2999); - int tmp = randomCID[x]; - randomCID[x] = randomCID[y]; - randomCID[y] = tmp; - } - - for (int o_id = 1; o_id <= 3000; o_id++) - { - int o_ol_cnt = rnd.nextInt(5, 15); - - // commit district and history when 100 records - if (o_id != 1 && (o_id - 1) % 100 == 0) - { - if (writeCSV) - { - LoadData.orderAppend(sbOrder); - LoadData.orderLineAppend(sbOrderLine); - LoadData.newOrderAppend(sbNewOrder); - } - else - { - stmtOrder.executeBatch(); - stmtOrder.clearBatch(); - dbConn.commit(); - - stmtOrderLine.executeBatch(); - stmtOrderLine.clearBatch(); - dbConn.commit(); - - stmtNewOrder.executeBatch(); - stmtNewOrder.clearBatch(); - dbConn.commit(); - } - } - - if (writeCSV) - { - fmtOrder.format("%d,%d,%d,%d,%s,%d,%d,%s\n", - w_id, - d_id, - o_id, - randomCID[o_id - 1], - (o_id < 2101) ? rnd.nextInt(1, 10) : csvNull, - o_ol_cnt, - 1, - new java.sql.Timestamp(System.currentTimeMillis()).toString()); - } - else - { - stmtOrder.setInt(1, w_id); - stmtOrder.setInt(2, d_id); - stmtOrder.setInt(3, o_id); - stmtOrder.setInt(4, randomCID[o_id - 1]); - if (o_id < 2101) - stmtOrder.setInt(5, rnd.nextInt(1, 10)); - else - stmtOrder.setNull(5, java.sql.Types.INTEGER); - stmtOrder.setInt(6, o_ol_cnt); - stmtOrder.setInt(7, 1); - stmtOrder.setTimestamp(8, new java.sql.Timestamp(System.currentTimeMillis())); - - stmtOrder.addBatch(); - } - - /* - * Create the ORDER_LINE rows for this ORDER. - */ - for (int ol_number = 1; ol_number <= o_ol_cnt; ol_number++) - { - long now = System.currentTimeMillis(); - - if (writeCSV) - { - fmtOrderLine.format("%d,%d,%d,%d,%d,%s,%.2f,%d,%d,%s\n", - w_id, - d_id, - o_id, - ol_number, - rnd.nextInt(1, 100000), - (o_id < 2101) ? new java.sql.Timestamp(now).toString() : csvNull, - (o_id < 2101) ? 0.00 : ((double)rnd.nextLong(1, 999999)) / 100.0, - w_id, - 5, - rnd.getAString(24, 24)); - } - else - { - stmtOrderLine.setInt(1, w_id); - stmtOrderLine.setInt(2, d_id); - stmtOrderLine.setInt(3, o_id); - stmtOrderLine.setInt(4, ol_number); - stmtOrderLine.setInt(5, rnd.nextInt(1, 100000)); - if (o_id < 2101) - stmtOrderLine.setTimestamp(6, new java.sql.Timestamp(now)); - else - stmtOrderLine.setNull(6, java.sql.Types.TIMESTAMP); - if (o_id < 2101) - stmtOrderLine.setDouble(7, 0.00); - else - stmtOrderLine.setDouble(7, ((double)rnd.nextLong(1, 999999)) / 100.0); - stmtOrderLine.setInt(8, w_id); - stmtOrderLine.setInt(9, 5); - stmtOrderLine.setString(10, rnd.getAString(24, 24)); - - stmtOrderLine.addBatch(); - } - } - - /* - * The last 900 ORDERs are not yet delieverd and have a - * row in NEW_ORDER. - */ - if (o_id >= 2101) - { - if (writeCSV) - { - fmtNewOrder.format("%d,%d,%d\n", - w_id, - d_id, - o_id); - } - else - { - stmtNewOrder.setInt(1, w_id); - stmtNewOrder.setInt(2, d_id); - stmtNewOrder.setInt(3, o_id); - - stmtNewOrder.addBatch(); - } - } - } - - if (writeCSV) - { - LoadData.orderAppend(sbOrder); - LoadData.orderLineAppend(sbOrderLine); - LoadData.newOrderAppend(sbNewOrder); - } - else - { - stmtOrder.executeBatch(); - stmtOrder.clearBatch(); - dbConn.commit(); - stmtOrderLine.executeBatch(); - stmtOrderLine.clearBatch(); - dbConn.commit(); - stmtNewOrder.executeBatch(); - stmtNewOrder.clearBatch(); - dbConn.commit(); - } - } - - if (!writeCSV) - dbConn.commit(); + private void loadWarehouse(int w_id) throws SQLException, IOException { + /* + * Load the WAREHOUSE row. + */ + if (writeCSV) { + fmtWarehouse.format("%d,%.2f,%.4f,%s,%s,%s,%s,%s,%s\n", + w_id, + 300000.0, + ((double) rnd.nextLong(0, 2000)) / 10000.0, + rnd.getAString(6, 10), + rnd.getAString(10, 20), + rnd.getAString(10, 20), + rnd.getAString(10, 20), + rnd.getState(), + rnd.getNString(4, 4) + "11111"); + + LoadData.warehouseAppend(sbWarehouse); + } else { + stmtWarehouse.setInt(1, w_id); + stmtWarehouse.setDouble(2, 300000.0); + stmtWarehouse.setDouble(3, ((double) rnd.nextLong(0, 2000)) / 10000.0); + stmtWarehouse.setString(4, rnd.getAString(6, 10)); + stmtWarehouse.setString(5, rnd.getAString(10, 20)); + stmtWarehouse.setString(6, rnd.getAString(10, 20)); + stmtWarehouse.setString(7, rnd.getAString(10, 20)); + stmtWarehouse.setString(8, rnd.getState()); + stmtWarehouse.setString(9, rnd.getNString(4, 4) + "11111"); + + stmtWarehouse.execute(); + } + + /* + * For each WAREHOUSE there are 100,000 STOCK rows. + */ + for (int s_i_id = 1; s_i_id <= 100000; s_i_id++) { + String sData; + /* + * Load the data in batches of 500 rows. + */ + if (s_i_id != 1 && (s_i_id - 1) % 500 == 0) { + if (writeCSV) + LoadData.warehouseAppend(sbWarehouse); + else { + stmtStock.executeBatch(); + stmtStock.clearBatch(); + dbConn.commit(); + } + } + + // Clause 4.3.3.1 for STOCK + if (rnd.nextInt(1, 100) <= 10) { + int len = rnd.nextInt(26, 50); + int off = rnd.nextInt(0, len - 8); + + sData = rnd.getAString(off, off) + + "ORIGINAL" + + rnd.getAString(len - off - 8, len - off - 8); + } else { + sData = rnd.getAString(26, 50); + } + + if (writeCSV) { + fmtStock.format("%d,%d,%d,%d,%d,%d,%s," + + "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", + w_id, + s_i_id, + rnd.nextInt(10, 100), + 0, + 0, + 0, + sData, + rnd.getAString(24, 24), + rnd.getAString(24, 24), + rnd.getAString(24, 24), + rnd.getAString(24, 24), + rnd.getAString(24, 24), + rnd.getAString(24, 24), + rnd.getAString(24, 24), + rnd.getAString(24, 24), + rnd.getAString(24, 24), + rnd.getAString(24, 24)); + } else { + stmtStock.setInt(1, w_id); + stmtStock.setInt(2, s_i_id); + stmtStock.setInt(3, rnd.nextInt(10, 100)); + stmtStock.setInt(4, 0); + stmtStock.setInt(5, 0); + stmtStock.setInt(6, 0); + stmtStock.setString(7, sData); + stmtStock.setString(8, rnd.getAString(24, 24)); + stmtStock.setString(9, rnd.getAString(24, 24)); + stmtStock.setString(10, rnd.getAString(24, 24)); + stmtStock.setString(11, rnd.getAString(24, 24)); + stmtStock.setString(12, rnd.getAString(24, 24)); + stmtStock.setString(13, rnd.getAString(24, 24)); + stmtStock.setString(14, rnd.getAString(24, 24)); + stmtStock.setString(15, rnd.getAString(24, 24)); + stmtStock.setString(16, rnd.getAString(24, 24)); + stmtStock.setString(17, rnd.getAString(24, 24)); + + stmtStock.addBatch(); + } + + } + if (writeCSV) { + LoadData.stockAppend(sbStock); + } else { + stmtStock.executeBatch(); + stmtStock.clearBatch(); + dbConn.commit(); + } + + /* + * For each WAREHOUSE there are 10 DISTRICT rows. + */ + for (int d_id = 1; d_id <= 10; d_id++) { + if (writeCSV) { + fmtDistrict.format("%d,%d,%.2f,%.4f,%d,%s,%s,%s,%s,%s,%s\n", + w_id, + d_id, + 30000.0, + ((double) rnd.nextLong(0, 2000)) / 10000.0, + 3001, + rnd.getAString(6, 10), + rnd.getAString(10, 20), + rnd.getAString(10, 20), + rnd.getAString(10, 20), + rnd.getState(), + rnd.getNString(4, 4) + "11111"); + + LoadData.districtAppend(sbDistrict); + } else { + stmtDistrict.setInt(1, w_id); + stmtDistrict.setInt(2, d_id); + stmtDistrict.setDouble(3, 30000.0); + stmtDistrict.setDouble(4, ((double) rnd.nextLong(0, 2000)) / 10000.0); + stmtDistrict.setInt(5, 3001); + stmtDistrict.setString(6, rnd.getAString(6, 10)); + stmtDistrict.setString(7, rnd.getAString(10, 20)); + stmtDistrict.setString(8, rnd.getAString(10, 20)); + stmtDistrict.setString(9, rnd.getAString(10, 20)); + stmtDistrict.setString(10, rnd.getState()); + stmtDistrict.setString(11, rnd.getNString(4, 4) + "11111"); + + stmtDistrict.execute(); + } + + /* + * Within each DISTRICT there are 3,000 CUSTOMERs. + */ + for (int c_id = 1; c_id <= 3000; c_id++) { + // commit district and history when 200 records + if (c_id != 1 && (c_id - 1) % 200 == 0) { + if (writeCSV) { + LoadData.customerAppend(sbCustomer); + LoadData.historyAppend(sbHistory); + } else { + stmtCustomer.executeBatch(); + stmtCustomer.clearBatch(); + dbConn.commit(); + + stmtHistory.executeBatch(); + stmtHistory.clearBatch(); + dbConn.commit(); + } + } + + if (writeCSV) { + fmtCustomer.format("%d,%d,%d,%.4f,%s,%s,%s," + + "%.2f,%.2f,%.2f,%d,%d," + + "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", + w_id, + d_id, + c_id, + ((double) rnd.nextLong(0, 5000)) / 10000.0, + (rnd.nextInt(1, 100) <= 90) ? "GC" : "BC", + (c_id <= 1000) ? rnd.getCLast(c_id - 1) : rnd.getCLast(), + rnd.getAString(8, 16), + 50000.00, + -10.00, + 10.00, + 1, + 1, + rnd.getAString(10, 20), + rnd.getAString(10, 20), + rnd.getAString(10, 20), + rnd.getState(), + rnd.getNString(4, 4) + "11111", + rnd.getNString(16, 16), + new java.sql.Timestamp(System.currentTimeMillis()).toString(), + "OE", + rnd.getAString(300, 500)); + } else { + stmtCustomer.setInt(1, w_id); + stmtCustomer.setInt(2, d_id); + stmtCustomer.setInt(3, c_id); + stmtCustomer.setDouble(4, ((double) rnd.nextLong(0, 5000)) / 10000.0); + if (rnd.nextInt(1, 100) <= 90) + stmtCustomer.setString(5, "GC"); + else + stmtCustomer.setString(5, "BC"); + if (c_id <= 1000) + stmtCustomer.setString(6, rnd.getCLast(c_id - 1)); + else + stmtCustomer.setString(6, rnd.getCLast()); + stmtCustomer.setString(7, rnd.getAString(8, 16)); + stmtCustomer.setDouble(8, 50000.00); + stmtCustomer.setDouble(9, -10.00); + stmtCustomer.setDouble(10, 10.00); + stmtCustomer.setInt(11, 1); + stmtCustomer.setInt(12, 1); + stmtCustomer.setString(13, rnd.getAString(10, 20)); + stmtCustomer.setString(14, rnd.getAString(10, 20)); + stmtCustomer.setString(15, rnd.getAString(10, 20)); + stmtCustomer.setString(16, rnd.getState()); + stmtCustomer.setString(17, rnd.getNString(4, 4) + "11111"); + stmtCustomer.setString(18, rnd.getNString(16, 16)); + stmtCustomer.setTimestamp(19, new java.sql.Timestamp(System.currentTimeMillis())); + stmtCustomer.setString(20, "OE"); + stmtCustomer.setString(21, rnd.getAString(300, 500)); + + stmtCustomer.addBatch(); + } + + /* + * For each CUSTOMER there is one row in HISTORY. + */ + if (writeCSV) { + fmtHistory.format("%d,%d,%d,%d,%d,%d,%s,%.2f,%s\n", + (w_id - 1) * 30000 + (d_id - 1) * 3000 + c_id, + c_id, + d_id, + w_id, + d_id, + w_id, + new java.sql.Timestamp(System.currentTimeMillis()).toString(), + 10.00, + rnd.getAString(12, 24)); + } else { + stmtHistory.setInt(1, (w_id - 1) * 30000 + (d_id - 1) * 3000 + c_id); + stmtHistory.setInt(2, c_id); + stmtHistory.setInt(3, d_id); + stmtHistory.setInt(4, w_id); + stmtHistory.setInt(5, d_id); + stmtHistory.setInt(6, w_id); + stmtHistory.setTimestamp(7, new java.sql.Timestamp(System.currentTimeMillis())); + stmtHistory.setDouble(8, 10.00); + stmtHistory.setString(9, rnd.getAString(12, 24)); + + stmtHistory.addBatch(); + } + } + + if (writeCSV) { + LoadData.customerAppend(sbCustomer); + LoadData.historyAppend(sbHistory); + } else { + stmtCustomer.executeBatch(); + stmtCustomer.clearBatch(); + dbConn.commit(); + stmtHistory.executeBatch(); + stmtHistory.clearBatch(); + dbConn.commit(); + } + + /* + * For the ORDER rows the TPC-C specification demands that they + * are generated using a random permutation of all 3,000 + * customers. To do that we set up an array with all C_IDs + * and then randomly shuffle it. + */ + int randomCID[] = new int[3000]; + for (int i = 0; i < 3000; i++) + randomCID[i] = i + 1; + for (int i = 0; i < 3000; i++) { + int x = rnd.nextInt(0, 2999); + int y = rnd.nextInt(0, 2999); + int tmp = randomCID[x]; + randomCID[x] = randomCID[y]; + randomCID[y] = tmp; + } + + for (int o_id = 1; o_id <= 3000; o_id++) { + int o_ol_cnt = rnd.nextInt(5, 15); + + // commit district and history when 100 records + if (o_id != 1 && (o_id - 1) % 100 == 0) { + if (writeCSV) { + LoadData.orderAppend(sbOrder); + LoadData.orderLineAppend(sbOrderLine); + LoadData.newOrderAppend(sbNewOrder); + } else { + stmtOrder.executeBatch(); + stmtOrder.clearBatch(); + dbConn.commit(); + + stmtOrderLine.executeBatch(); + stmtOrderLine.clearBatch(); + dbConn.commit(); + + stmtNewOrder.executeBatch(); + stmtNewOrder.clearBatch(); + dbConn.commit(); + } + } + + if (writeCSV) { + fmtOrder.format("%d,%d,%d,%d,%s,%d,%d,%s\n", + w_id, + d_id, + o_id, + randomCID[o_id - 1], + (o_id < 2101) ? rnd.nextInt(1, 10) : csvNull, + o_ol_cnt, + 1, + new java.sql.Timestamp(System.currentTimeMillis()).toString()); + } else { + stmtOrder.setInt(1, w_id); + stmtOrder.setInt(2, d_id); + stmtOrder.setInt(3, o_id); + stmtOrder.setInt(4, randomCID[o_id - 1]); + if (o_id < 2101) + stmtOrder.setInt(5, rnd.nextInt(1, 10)); + else + stmtOrder.setNull(5, java.sql.Types.INTEGER); + stmtOrder.setInt(6, o_ol_cnt); + stmtOrder.setInt(7, 1); + stmtOrder.setTimestamp(8, new java.sql.Timestamp(System.currentTimeMillis())); + + stmtOrder.addBatch(); + } + + /* + * Create the ORDER_LINE rows for this ORDER. + */ + for (int ol_number = 1; ol_number <= o_ol_cnt; ol_number++) { + long now = System.currentTimeMillis(); + + if (writeCSV) { + fmtOrderLine.format("%d,%d,%d,%d,%d,%s,%.2f,%d,%d,%s\n", + w_id, + d_id, + o_id, + ol_number, + rnd.nextInt(1, 100000), + (o_id < 2101) ? new java.sql.Timestamp(now).toString() : csvNull, + (o_id < 2101) ? 0.00 : ((double) rnd.nextLong(1, 999999)) / 100.0, + w_id, + 5, + rnd.getAString(24, 24)); + } else { + stmtOrderLine.setInt(1, w_id); + stmtOrderLine.setInt(2, d_id); + stmtOrderLine.setInt(3, o_id); + stmtOrderLine.setInt(4, ol_number); + stmtOrderLine.setInt(5, rnd.nextInt(1, 100000)); + if (o_id < 2101) + stmtOrderLine.setTimestamp(6, new java.sql.Timestamp(now)); + else + stmtOrderLine.setNull(6, java.sql.Types.TIMESTAMP); + if (o_id < 2101) + stmtOrderLine.setDouble(7, 0.00); + else + stmtOrderLine.setDouble(7, ((double) rnd.nextLong(1, 999999)) / 100.0); + stmtOrderLine.setInt(8, w_id); + stmtOrderLine.setInt(9, 5); + stmtOrderLine.setString(10, rnd.getAString(24, 24)); + + stmtOrderLine.addBatch(); + } + } + + /* + * The last 900 ORDERs are not yet delieverd and have a + * row in NEW_ORDER. + */ + if (o_id >= 2101) { + if (writeCSV) { + fmtNewOrder.format("%d,%d,%d\n", + w_id, + d_id, + o_id); + } else { + stmtNewOrder.setInt(1, w_id); + stmtNewOrder.setInt(2, d_id); + stmtNewOrder.setInt(3, o_id); + + stmtNewOrder.addBatch(); + } + } + } + + if (writeCSV) { + LoadData.orderAppend(sbOrder); + LoadData.orderLineAppend(sbOrderLine); + LoadData.newOrderAppend(sbNewOrder); + } else { + stmtOrder.executeBatch(); + stmtOrder.clearBatch(); + dbConn.commit(); + stmtOrderLine.executeBatch(); + stmtOrderLine.clearBatch(); + dbConn.commit(); + stmtNewOrder.executeBatch(); + stmtNewOrder.clearBatch(); + dbConn.commit(); + } + } + + if (!writeCSV) + dbConn.commit(); } // End loadWarehouse() } diff --git a/src/OSCollector/OSCollector.java b/src/OSCollector/OSCollector.java index 91d556f..590daf7 100644 --- a/src/OSCollector/OSCollector.java +++ b/src/OSCollector/OSCollector.java @@ -6,174 +6,148 @@ * */ -import org.apache.log4j.*; +import org.apache.log4j.Logger; -import java.lang.*; import java.io.*; -import java.util.*; +import java.util.ArrayList; +import java.util.List; -public class OSCollector -{ - private String script; - private int interval; - private String sshAddress; - private String devices; - private File outputDir; - private Logger log; +public class OSCollector { + private String script; + private int interval; + private String sshAddress; + private String devices; + private File outputDir; + private Logger log; - private CollectData collector = null; - private Thread collectorThread = null; - private boolean endCollection = false; - private Process collProc; + private CollectData collector = null; + private Thread collectorThread = null; + private boolean endCollection = false; + private Process collProc; - private BufferedWriter resultCSVs[]; + private BufferedWriter resultCSVs[]; public OSCollector(String script, int runID, int interval, - String sshAddress, String devices, File outputDir, - Logger log) - { - List cmdLine = new ArrayList(); - String deviceNames[]; - - this.script = script; - this.interval = interval; - this.sshAddress = sshAddress; - this.devices = devices; - this.outputDir = outputDir; - this.log = log; - - if (sshAddress != null) - { - cmdLine.add("ssh"); - // cmdLine.add("-t"); - cmdLine.add(sshAddress); - } - cmdLine.add("python"); - cmdLine.add("-"); - cmdLine.add(Integer.toString(runID)); - cmdLine.add(Integer.toString(interval)); - if (devices != null) - deviceNames = devices.split("[ \t]+"); - else - deviceNames = new String[0]; - - try - { - resultCSVs = new BufferedWriter[deviceNames.length + 1]; - resultCSVs[0] = new BufferedWriter(new FileWriter( - new File(outputDir, "sys_info.csv"))); - for (int i = 0; i < deviceNames.length; i++) - { - cmdLine.add(deviceNames[i]); - resultCSVs[i + 1] = new BufferedWriter(new FileWriter( - new File(outputDir, deviceNames[i] + ".csv"))); - } - } - catch (Exception e) - { - log.error("OSCollector, " + e.getMessage()); - System.exit(1); - } - - try - { - ProcessBuilder pb = new ProcessBuilder(cmdLine); - pb.redirectError(ProcessBuilder.Redirect.INHERIT); - - collProc = pb.start(); - - BufferedReader scriptReader = new BufferedReader(new FileReader(script)); - BufferedWriter scriptWriter = new BufferedWriter( - new OutputStreamWriter(collProc.getOutputStream())); - String line; - while ((line = scriptReader.readLine()) != null) - { - scriptWriter.write(line); - scriptWriter.newLine(); - } - scriptWriter.close(); - scriptReader.close(); - } - catch (Exception e) - { - log.error("OSCollector " + e.getMessage()); - e.printStackTrace(); - System.exit(1); - } - - collector = new CollectData(this); - collectorThread = new Thread(this.collector); - collectorThread.start(); + String sshAddress, String devices, File outputDir, + Logger log) { + List cmdLine = new ArrayList(); + String deviceNames[]; + + this.script = script; + this.interval = interval; + this.sshAddress = sshAddress; + this.devices = devices; + this.outputDir = outputDir; + this.log = log; + + if (sshAddress != null) { + cmdLine.add("ssh"); + // cmdLine.add("-t"); + cmdLine.add(sshAddress); + } + cmdLine.add("python"); + cmdLine.add("-"); + cmdLine.add(Integer.toString(runID)); + cmdLine.add(Integer.toString(interval)); + if (devices != null) + deviceNames = devices.split("[ \t]+"); + else + deviceNames = new String[0]; + + try { + resultCSVs = new BufferedWriter[deviceNames.length + 1]; + resultCSVs[0] = new BufferedWriter(new FileWriter( + new File(outputDir, "sys_info.csv"))); + for (int i = 0; i < deviceNames.length; i++) { + cmdLine.add(deviceNames[i]); + resultCSVs[i + 1] = new BufferedWriter(new FileWriter( + new File(outputDir, deviceNames[i] + ".csv"))); + } + } catch (Exception e) { + log.error("OSCollector, " + e.getMessage()); + System.exit(1); + } + + try { + ProcessBuilder pb = new ProcessBuilder(cmdLine); + pb.redirectError(ProcessBuilder.Redirect.INHERIT); + + collProc = pb.start(); + + BufferedReader scriptReader = new BufferedReader(new FileReader(script)); + BufferedWriter scriptWriter = new BufferedWriter( + new OutputStreamWriter(collProc.getOutputStream())); + String line; + while ((line = scriptReader.readLine()) != null) { + scriptWriter.write(line); + scriptWriter.newLine(); + } + scriptWriter.close(); + scriptReader.close(); + } catch (Exception e) { + log.error("OSCollector " + e.getMessage()); + e.printStackTrace(); + System.exit(1); + } + + collector = new CollectData(this); + collectorThread = new Thread(this.collector); + collectorThread.start(); } - public void stop() - { - endCollection = true; - try - { - collectorThread.join(); - } - catch (InterruptedException ie) - { - log.error("OSCollector, " + ie.getMessage()); - return; - } + public void stop() { + endCollection = true; + try { + collectorThread.join(); + } catch (InterruptedException ie) { + log.error("OSCollector, " + ie.getMessage()); + return; + } } - private class CollectData implements Runnable - { - private OSCollector parent; - - public CollectData(OSCollector parent) - { - this.parent = parent; - } - - public void run() - { - BufferedReader osData; - String line; - int resultIdx = 0; - - osData = new BufferedReader(new InputStreamReader( - parent.collProc.getInputStream())); - - while (!endCollection || resultIdx != 0) - { - try - { - line = osData.readLine(); - if (line == null) - { - log.error("OSCollector, unexpected EOF " + - "while reading from external " + - "helper process"); - break; - } - parent.resultCSVs[resultIdx].write(line); - parent.resultCSVs[resultIdx].newLine(); - parent.resultCSVs[resultIdx].flush(); - if (++resultIdx >= parent.resultCSVs.length) - resultIdx = 0; - } - catch (Exception e) - { - log.error("OSCollector, " + e.getMessage()); - break; - } - } - - try - { - osData.close(); - for (int i = 0; i < parent.resultCSVs.length; i++) - parent.resultCSVs[i].close(); - } - catch (Exception e) - { - log.error("OSCollector, " + e.getMessage()); - } - } + private class CollectData implements Runnable { + private OSCollector parent; + + public CollectData(OSCollector parent) { + this.parent = parent; + } + + public void run() { + BufferedReader osData; + String line; + int resultIdx = 0; + + osData = new BufferedReader(new InputStreamReader( + parent.collProc.getInputStream())); + + while (!endCollection || resultIdx != 0) { + try { + line = osData.readLine(); + if (line == null) { + log.error("OSCollector, unexpected EOF " + + "while reading from external " + + "helper process"); + break; + } + parent.resultCSVs[resultIdx].write(line); + parent.resultCSVs[resultIdx].newLine(); + parent.resultCSVs[resultIdx].flush(); + if (++resultIdx >= parent.resultCSVs.length) + resultIdx = 0; + } catch (Exception e) { + log.error("OSCollector, " + e.getMessage()); + break; + } + } + + try { + osData.close(); + for (int i = 0; i < parent.resultCSVs.length; i++) + parent.resultCSVs[i].close(); + } catch (Exception e) { + log.error("OSCollector, " + e.getMessage()); + } + } } } diff --git a/src/client/jTPCC.java b/src/client/jTPCC.java index 5e7dc9c..fc0e989 100644 --- a/src/client/jTPCC.java +++ b/src/client/jTPCC.java @@ -7,23 +7,29 @@ * */ -import org.apache.log4j.*; +import org.apache.log4j.Logger; +import org.apache.log4j.PropertyConfigurator; import java.io.*; -import java.nio.file.*; -import java.sql.*; -import java.util.*; +import java.nio.file.Files; +import java.sql.Connection; +import java.sql.DriverManager; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Formatter; +import java.util.HashMap; +import java.util.Properties; +import java.util.concurrent.atomic.LongAdder; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.regex.Pattern; -import java.text.*; - -public class jTPCC implements jTPCCConfig -{ - private static org.apache.log4j.Logger log = Logger.getLogger(jTPCC.class); - private static String resultDirName = null; - private static BufferedWriter resultCSV = null; - private static BufferedWriter runInfoCSV = null; - private static int runID = 0; +public class jTPCC implements jTPCCConfig { + private static Logger log = Logger.getLogger(jTPCC.class); + private static String resultDirName = null; + private static BufferedWriter resultCSV = null; + private static BufferedWriter runInfoCSV = null; + private static int runID = 0; private int dbType = DB_UNKNOWN; private int currentlyDisplayedTerminal; @@ -31,11 +37,14 @@ public class jTPCC implements jTPCCConfig private jTPCCTerminal[] terminals; private String[] terminalNames; private boolean terminalsBlockingExit = false; - private long terminalsStarted = 0, sessionCount = 0, transactionCount = 0; - private Object counterLock = new Object(); - - private long newOrderCounter = 0, sessionStartTimestamp, sessionEndTimestamp, sessionNextTimestamp=0, sessionNextKounter=0; - private long sessionEndTargetTime = -1, fastNewOrderCounter, recentTpmC=0, recentTpmTotal=0; + private LongAdder terminalsStarted = new LongAdder(); + private LongAdder transactionCount = new LongAdder(); + private long sessionCount = 0; + + private LongAdder fastNewOrderCounter = new LongAdder(); + private long newOrderCounter = 0, sessionStartTimestamp, + sessionEndTimestamp, sessionNextTimestamp = 0, sessionNextKounter = 0; + private long sessionEndTargetTime = -1, recentTpmC = 0, recentTpmTotal = 0; private boolean signalTerminalsRequestEndSent = false, databaseDriverLoaded = false; private FileOutputStream fileOutputStream; @@ -48,715 +57,647 @@ public class jTPCC implements jTPCCConfig private OSCollector osCollector = null; private HashMap costPerWorkerload; - public static void main(String args[]) - { - PropertyConfigurator.configure("log4j.properties"); - new jTPCC(); + private final Lock terminalsLock = new ReentrantLock(); + private final Lock transactionLock = new ReentrantLock(); + + public static void main(String args[]) { + PropertyConfigurator.configure("log4j.properties"); + new jTPCC(); } - private String getProp (Properties p, String pName) - { - String prop = p.getProperty(pName); - costPerWorkerload = new HashMap(); - log.info("Term-00, " + pName + "=" + prop); - return(prop); + private String getProp(Properties p, String pName) { + String prop = p.getProperty(pName); + costPerWorkerload = new HashMap(); + log.info("Term-00, " + pName + "=" + prop); + return (prop); } - public jTPCC() - { - - // load the ini file - Properties ini = new Properties(); - try { - ini.load( new FileInputStream(System.getProperty("prop"))); - } catch (IOException e) { - errorMessage("Term-00, could not load properties file"); - } - - log.info("Term-00, "); - log.info("Term-00, +-------------------------------------------------------------+"); - log.info("Term-00, BenchmarkSQL v" + JTPCCVERSION); - log.info("Term-00, +-------------------------------------------------------------+"); - log.info("Term-00, (c) 2003, Raul Barbosa"); - log.info("Term-00, (c) 2004-2016, Denis Lussier"); - log.info("Term-00, (c) 2016, Jan Wieck"); - log.info("Term-00, +-------------------------------------------------------------+"); - log.info("Term-00, "); - String iDB = getProp(ini,"db"); - String iDriver = getProp(ini,"driver"); - String iConn = getProp(ini,"conn"); - String iUser = getProp(ini,"user"); - String iPassword = ini.getProperty("password"); - - log.info("Term-00, "); - String iWarehouses = getProp(ini,"warehouses"); - String iTerminals = getProp(ini,"terminals"); - - String iRunTxnsPerTerminal = ini.getProperty("runTxnsPerTerminal"); - String iRunMins = ini.getProperty("runMins"); - if (Integer.parseInt(iRunTxnsPerTerminal) ==0 && Integer.parseInt(iRunMins)!=0){ - log.info("Term-00, runMins" + "=" + iRunMins); - }else if(Integer.parseInt(iRunTxnsPerTerminal) !=0 && Integer.parseInt(iRunMins)==0){ - log.info("Term-00, runTxnsPerTerminal" + "=" + iRunTxnsPerTerminal); - }else{ - errorMessage("Term-00, Must indicate either transactions per terminal or number of run minutes!"); - }; - String limPerMin = getProp(ini,"limitTxnsPerMin"); - String iTermWhseFixed = getProp(ini,"terminalWarehouseFixed"); - log.info("Term-00, "); - String iNewOrderWeight = getProp(ini,"newOrderWeight"); - String iPaymentWeight = getProp(ini,"paymentWeight"); - String iOrderStatusWeight = getProp(ini,"orderStatusWeight"); - String iDeliveryWeight = getProp(ini,"deliveryWeight"); - String iStockLevelWeight = getProp(ini,"stockLevelWeight"); - - log.info("Term-00, "); - String resultDirectory = getProp(ini, "resultDirectory"); - String osCollectorScript = getProp(ini, "osCollectorScript"); - - log.info("Term-00, "); - - if (iDB.equals("firebird")) - dbType = DB_FIREBIRD; - else if (iDB.equals("oracle")) - dbType = DB_ORACLE; - else if (iDB.equals("postgres")) - dbType = DB_POSTGRES; - else if (iDB.equals("mysql")) - dbType = DB_MYSQL; - else - { - log.error("unknown database type '" + iDB + "'"); - return; - } - - if(Integer.parseInt(limPerMin) !=0){ - limPerMin_Terminal = Integer.parseInt(limPerMin)/Integer.parseInt(iTerminals); - } - else{ - limPerMin_Terminal = -1; - } - - - boolean iRunMinsBool=false; - - try - { - String driver = iDriver; - printMessage("Loading database driver: \'" + driver + "\'..."); - Class.forName(iDriver); - databaseDriverLoaded = true; - } - catch(Exception ex) - { - errorMessage("Unable to load the database driver!"); - databaseDriverLoaded = false; - } - - if (databaseDriverLoaded && resultDirectory != null) - { - StringBuffer sb = new StringBuffer(); - Formatter fmt = new Formatter(sb); - Pattern p = Pattern.compile("%t"); - Calendar cal = Calendar.getInstance(); - - String iRunID; - - iRunID = System.getProperty("runID"); - if (iRunID != null) - { - runID = Integer.parseInt(iRunID); - } - - /* - * Split the resultDirectory into strings around - * patterns of %t and then insert date/time formatting - * based on the current time. That way the resultDirectory - * in the properties file can have date/time format - * elements like in result_%tY-%tm-%td to embed the current - * date in the directory name. - */ - String[] parts = p.split(resultDirectory, -1); - sb.append(parts[0]); - for (int i = 1; i < parts.length; i++) - { - fmt.format("%t" + parts[i].substring(0, 1), cal); - sb.append(parts[i].substring(1)); - } - resultDirName = sb.toString(); - File resultDir = new File(resultDirName); - File resultDataDir = new File(resultDir, "data"); - - // Create the output directory structure. - if (!resultDir.mkdir()) - { - log.error("Failed to create directory '" + - resultDir.getPath() + "'"); - System.exit(1); - } - if (!resultDataDir.mkdir()) - { - log.error("Failed to create directory '" + - resultDataDir.getPath() + "'"); - System.exit(1); - } - - // Copy the used properties file into the resultDirectory. - try - { - Files.copy(new File(System.getProperty("prop")).toPath(), - new File(resultDir, "run.properties").toPath()); - } - catch (IOException e) - { - log.error(e.getMessage()); - System.exit(1); - } - log.info("Term-00, copied " + System.getProperty("prop") + - " to " + new File(resultDir, "run.properties").toPath()); - - // Create the runInfo.csv file. - String runInfoCSVName = new File(resultDataDir, "runInfo.csv").getPath(); - try - { - runInfoCSV = new BufferedWriter( - new FileWriter(runInfoCSVName)); - runInfoCSV.write("run,driver,driverVersion,db,sessionStart," + - "runMins," + - "loadWarehouses,runWarehouses,numSUTThreads," + - "limitTxnsPerMin," + - "thinkTimeMultiplier,keyingTimeMultiplier\n"); - } - catch (IOException e) - { - log.error(e.getMessage()); - System.exit(1); - } - log.info("Term-00, created " + runInfoCSVName + " for runID " + - runID); - - // Open the per transaction result.csv file. - String resultCSVName = new File(resultDataDir, "result.csv").getPath(); - try - { - resultCSV = new BufferedWriter(new FileWriter(resultCSVName)); - resultCSV.write("run,elapsed,latency,dblatency," + - "ttype,rbk,dskipped,error\n"); - } - catch (IOException e) - { - log.error(e.getMessage()); - System.exit(1); - } - log.info("Term-00, writing per transaction results to " + - resultCSVName); - - if (osCollectorScript != null) - { - osCollector = new OSCollector(getProp(ini, "osCollectorScript"), - runID, - Integer.parseInt(getProp(ini, "osCollectorInterval")), - getProp(ini, "osCollectorSSHAddr"), - getProp(ini, "osCollectorDevices"), - resultDataDir, log); - } - - log.info("Term-00,"); - } - - if(databaseDriverLoaded) - { - try - { - boolean limitIsTime = iRunMinsBool; - int numTerminals = -1; - int transactionsPerTerminal = -1; - int numWarehouses = -1; - int loadWarehouses = -1; - int newOrderWeightValue = -1, paymentWeightValue = -1, orderStatusWeightValue = -1, deliveryWeightValue = -1, stockLevelWeightValue = -1; - long executionTimeMillis = -1; - boolean terminalWarehouseFixed = true; - long CLoad; - - Properties dbProps = new Properties(); - dbProps.setProperty("user", iUser); - dbProps.setProperty("password", iPassword); - - /* - * Fine tuning of database conneciton parameters if needed. - */ - switch (dbType) - { - case DB_FIREBIRD: - /* - * Firebird needs no_rec_version for our load - * to work. Even with that some "deadlocks" - * occur. Note that the message "deadlock" in - * Firebird can mean something completely different, - * namely that there was a conflicting write to - * a row that could not be resolved. - */ - dbProps.setProperty("TRANSACTION_READ_COMMITTED", - "isc_tpb_read_committed," + - "isc_tpb_no_rec_version," + - "isc_tpb_write," + - "isc_tpb_wait"); - break; - - default: - break; - } - - try { - loadWarehouses = Integer.parseInt(jTPCCUtil.getConfig(iConn, - dbProps, "warehouses")); - CLoad = Long.parseLong(jTPCCUtil.getConfig(iConn, - dbProps, "nURandCLast")); - } catch (Exception e) { - errorMessage(e.getMessage()); - throw e; - } - this.rnd = new jTPCCRandom(CLoad); - log.info("Term-00, C value for C_LAST during load: " + CLoad); - log.info("Term-00, C value for C_LAST this run: " + rnd.getNURandCLast()); - log.info("Term-00, "); - - fastNewOrderCounter = 0; - updateStatusLine(); - - try - { - if (Integer.parseInt(iRunMins) != 0 && Integer.parseInt(iRunTxnsPerTerminal) ==0) - { - iRunMinsBool = true; - } - else if (Integer.parseInt(iRunMins) == 0 && Integer.parseInt(iRunTxnsPerTerminal) !=0) - { - iRunMinsBool = false; - } - else - { - throw new NumberFormatException(); - } - } - catch(NumberFormatException e1) - { - errorMessage("Must indicate either transactions per terminal or number of run minutes!"); - throw new Exception(); - } - - try - { - numWarehouses = Integer.parseInt(iWarehouses); - if(numWarehouses <= 0) - throw new NumberFormatException(); - } - catch(NumberFormatException e1) - { - errorMessage("Invalid number of warehouses!"); - throw new Exception(); - } - if(numWarehouses > loadWarehouses) - { - errorMessage("numWarehouses cannot be greater " + - "than the warehouses loaded in the database"); - throw new Exception(); - } - - try - { - numTerminals = Integer.parseInt(iTerminals); - if(numTerminals <= 0 || numTerminals > 10*numWarehouses) - throw new NumberFormatException(); - } - catch(NumberFormatException e1) - { - errorMessage("Invalid number of terminals!"); - throw new Exception(); - } - - - - if(Long.parseLong(iRunMins) != 0 && Integer.parseInt(iRunTxnsPerTerminal) == 0) - { - try - { - executionTimeMillis = Long.parseLong(iRunMins) * 60000; - if(executionTimeMillis <= 0) - throw new NumberFormatException(); - } - catch(NumberFormatException e1) - { - errorMessage("Invalid number of minutes!"); - throw new Exception(); - } - } - else - { - try - { - transactionsPerTerminal = Integer.parseInt(iRunTxnsPerTerminal); - if(transactionsPerTerminal <= 0) - throw new NumberFormatException(); - } - catch(NumberFormatException e1) - { - errorMessage("Invalid number of transactions per terminal!"); - throw new Exception(); - } - } - - terminalWarehouseFixed = Boolean.parseBoolean(iTermWhseFixed); - - try - { - newOrderWeightValue = Integer.parseInt(iNewOrderWeight); - paymentWeightValue = Integer.parseInt(iPaymentWeight); - orderStatusWeightValue = Integer.parseInt(iOrderStatusWeight); - deliveryWeightValue = Integer.parseInt(iDeliveryWeight); - stockLevelWeightValue = Integer.parseInt(iStockLevelWeight); - - if(newOrderWeightValue < 0 ||paymentWeightValue < 0 || orderStatusWeightValue < 0 || deliveryWeightValue < 0 || stockLevelWeightValue < 0) - throw new NumberFormatException(); - else if(newOrderWeightValue == 0 && paymentWeightValue == 0 && orderStatusWeightValue == 0 && deliveryWeightValue == 0 && stockLevelWeightValue == 0) - throw new NumberFormatException(); - } - catch(NumberFormatException e1) - { - errorMessage("Invalid number in mix percentage!"); - throw new Exception(); - } - - if(newOrderWeightValue + paymentWeightValue + orderStatusWeightValue + deliveryWeightValue + stockLevelWeightValue > 100) - { - errorMessage("Sum of mix percentage parameters exceeds 100%!"); - throw new Exception(); - } - - newOrderCounter = 0; - printMessage("Session started!"); - if(!limitIsTime) - printMessage("Creating " + numTerminals + " terminal(s) with " + transactionsPerTerminal + " transaction(s) per terminal..."); - else - printMessage("Creating " + numTerminals + " terminal(s) with " + (executionTimeMillis/60000) + " minute(s) of execution..."); - if (terminalWarehouseFixed) - printMessage("Terminal Warehouse is fixed"); - else - printMessage("Terminal Warehouse is NOT fixed"); - printMessage("Transaction Weights: " + newOrderWeightValue + "% New-Order, " + paymentWeightValue + "% Payment, " + orderStatusWeightValue + "% Order-Status, " + deliveryWeightValue + "% Delivery, " + stockLevelWeightValue + "% Stock-Level"); - - printMessage("Number of Terminals\t" + numTerminals); - - terminals = new jTPCCTerminal[numTerminals]; - terminalNames = new String[numTerminals]; - terminalsStarted = numTerminals; - try - { - String database = iConn; - String username = iUser; - String password = iPassword; - - int[][] usedTerminals = new int[numWarehouses][10]; - for(int i = 0; i < numWarehouses; i++) - for(int j = 0; j < 10; j++) - usedTerminals[i][j] = 0; - - for(int i = 0; i < numTerminals; i++) - { - int terminalWarehouseID; - int terminalDistrictID; - do - { - terminalWarehouseID = rnd.nextInt(1, numWarehouses); - terminalDistrictID = rnd.nextInt(1, 10); - } - while(usedTerminals[terminalWarehouseID-1][terminalDistrictID-1] == 1); - usedTerminals[terminalWarehouseID-1][terminalDistrictID-1] = 1; - - String terminalName = "Term-" + (i>=9 ? ""+(i+1) : "0"+(i+1)); - Connection conn = null; - printMessage("Creating database connection for " + terminalName + "..."); - conn = DriverManager.getConnection(database, dbProps); - conn.setAutoCommit(false); - - jTPCCTerminal terminal = new jTPCCTerminal - (terminalName, terminalWarehouseID, terminalDistrictID, - conn, dbType, - transactionsPerTerminal, terminalWarehouseFixed, - paymentWeightValue, orderStatusWeightValue, - deliveryWeightValue, stockLevelWeightValue, numWarehouses, limPerMin_Terminal, this); - - terminals[i] = terminal; - terminalNames[i] = terminalName; - printMessage(terminalName + "\t" + terminalWarehouseID); - } - - sessionEndTargetTime = executionTimeMillis; - signalTerminalsRequestEndSent = false; - - - printMessage("Transaction\tWeight"); - printMessage("% New-Order\t" + newOrderWeightValue); - printMessage("% Payment\t" + paymentWeightValue); - printMessage("% Order-Status\t" + orderStatusWeightValue); - printMessage("% Delivery\t" + deliveryWeightValue); - printMessage("% Stock-Level\t" + stockLevelWeightValue); - - printMessage("Transaction Number\tTerminal\tType\tExecution Time (ms)\t\tComment"); - - printMessage("Created " + numTerminals + " terminal(s) successfully!"); - boolean dummvar = true; - - - - // Create Terminals, Start Transactions - sessionStart = getCurrentTime(); - sessionStartTimestamp = System.currentTimeMillis(); - sessionNextTimestamp = sessionStartTimestamp; - if(sessionEndTargetTime != -1) - sessionEndTargetTime += sessionStartTimestamp; - - // Record run parameters in runInfo.csv - if (runInfoCSV != null) - { - try - { - StringBuffer infoSB = new StringBuffer(); - Formatter infoFmt = new Formatter(infoSB); - infoFmt.format("%d,simple,%s,%s,%s,%s,%d,%d,%d,%d,1.0,1.0\n", - runID, JTPCCVERSION, iDB, - new java.sql.Timestamp(sessionStartTimestamp).toString(), - iRunMins, - loadWarehouses, - numWarehouses, - numTerminals, - Integer.parseInt(limPerMin)); - runInfoCSV.write(infoSB.toString()); - runInfoCSV.close(); - } - catch (Exception e) - { - log.error(e.getMessage()); - System.exit(1); - } - } - - synchronized(terminals) - { - printMessage("Starting all terminals..."); - transactionCount = 1; - for(int i = 0; i < terminals.length; i++) - (new Thread(terminals[i])).start(); - - } - - printMessage("All terminals started executing " + sessionStart); - } - - catch(Exception e1) - { - errorMessage("This session ended with errors!"); - printStreamReport.close(); - fileOutputStream.close(); - - throw new Exception(); - } - - } - catch(Exception ex) - { - } - } - updateStatusLine(); + public jTPCC() { + + // load the ini file + Properties ini = new Properties(); + try { + ini.load(new FileInputStream(System.getProperty("prop"))); + } catch (IOException e) { + errorMessage("Term-00, could not load properties file"); + } + + log.info("Term-00, "); + log.info("Term-00, +-------------------------------------------------------------+"); + log.info("Term-00, BenchmarkSQL v" + JTPCCVERSION); + log.info("Term-00, +-------------------------------------------------------------+"); + log.info("Term-00, (c) 2003, Raul Barbosa"); + log.info("Term-00, (c) 2004-2016, Denis Lussier"); + log.info("Term-00, (c) 2016, Jan Wieck"); + log.info("Term-00, +-------------------------------------------------------------+"); + log.info("Term-00, "); + String iDB = getProp(ini, "db"); + String iDriver = getProp(ini, "driver"); + String iConn = getProp(ini, "conn"); + String iUser = getProp(ini, "user"); + String iPassword = ini.getProperty("password"); + + log.info("Term-00, "); + String iWarehouses = getProp(ini, "warehouses"); + String iTerminals = getProp(ini, "terminals"); + + String iRunTxnsPerTerminal = ini.getProperty("runTxnsPerTerminal"); + String iRunMins = ini.getProperty("runMins"); + if (Integer.parseInt(iRunTxnsPerTerminal) == 0 && Integer.parseInt(iRunMins) != 0) { + log.info("Term-00, runMins" + "=" + iRunMins); + } else if (Integer.parseInt(iRunTxnsPerTerminal) != 0 && Integer.parseInt(iRunMins) == 0) { + log.info("Term-00, runTxnsPerTerminal" + "=" + iRunTxnsPerTerminal); + } else { + errorMessage("Term-00, Must indicate either transactions per terminal or number of run minutes!"); + } + ; + String limPerMin = getProp(ini, "limitTxnsPerMin"); + String iTermWhseFixed = getProp(ini, "terminalWarehouseFixed"); + log.info("Term-00, "); + String iNewOrderWeight = getProp(ini, "newOrderWeight"); + String iPaymentWeight = getProp(ini, "paymentWeight"); + String iOrderStatusWeight = getProp(ini, "orderStatusWeight"); + String iDeliveryWeight = getProp(ini, "deliveryWeight"); + String iStockLevelWeight = getProp(ini, "stockLevelWeight"); + + log.info("Term-00, "); + String resultDirectory = getProp(ini, "resultDirectory"); + String osCollectorScript = getProp(ini, "osCollectorScript"); + + log.info("Term-00, "); + + if (iDB.equals("firebird")) + dbType = DB_FIREBIRD; + else if (iDB.equals("oracle")) + dbType = DB_ORACLE; + else if (iDB.equals("postgres")) + dbType = DB_POSTGRES; + else if (iDB.equals("mysql")) + dbType = DB_MYSQL; + else { + log.error("unknown database type '" + iDB + "'"); + return; + } + + if (Integer.parseInt(limPerMin) != 0) { + limPerMin_Terminal = Integer.parseInt(limPerMin) / Integer.parseInt(iTerminals); + } else { + limPerMin_Terminal = -1; + } + + + boolean iRunMinsBool = false; + + try { + String driver = iDriver; + printMessage("Loading database driver: \'" + driver + "\'..."); + Class.forName(iDriver); + databaseDriverLoaded = true; + } catch (Exception ex) { + errorMessage("Unable to load the database driver!"); + databaseDriverLoaded = false; + } + + if (databaseDriverLoaded && resultDirectory != null) { + StringBuffer sb = new StringBuffer(); + Formatter fmt = new Formatter(sb); + Pattern p = Pattern.compile("%t"); + Calendar cal = Calendar.getInstance(); + + String iRunID; + + iRunID = System.getProperty("runID"); + if (iRunID != null) { + runID = Integer.parseInt(iRunID); + } + + /* + * Split the resultDirectory into strings around + * patterns of %t and then insert date/time formatting + * based on the current time. That way the resultDirectory + * in the properties file can have date/time format + * elements like in result_%tY-%tm-%td to embed the current + * date in the directory name. + */ + String[] parts = p.split(resultDirectory, -1); + sb.append(parts[0]); + for (int i = 1; i < parts.length; i++) { + fmt.format("%t" + parts[i].substring(0, 1), cal); + sb.append(parts[i].substring(1)); + } + resultDirName = sb.toString(); + File resultDir = new File(resultDirName); + File resultDataDir = new File(resultDir, "data"); + + // Create the output directory structure. + if (!resultDir.mkdir()) { + log.error("Failed to create directory '" + + resultDir.getPath() + "'"); + System.exit(1); + } + if (!resultDataDir.mkdir()) { + log.error("Failed to create directory '" + + resultDataDir.getPath() + "'"); + System.exit(1); + } + + // Copy the used properties file into the resultDirectory. + try { + Files.copy(new File(System.getProperty("prop")).toPath(), + new File(resultDir, "run.properties").toPath()); + } catch (IOException e) { + log.error(e.getMessage()); + System.exit(1); + } + log.info("Term-00, copied " + System.getProperty("prop") + + " to " + new File(resultDir, "run.properties").toPath()); + + // Create the runInfo.csv file. + String runInfoCSVName = new File(resultDataDir, "runInfo.csv").getPath(); + try { + runInfoCSV = new BufferedWriter( + new FileWriter(runInfoCSVName)); + runInfoCSV.write("run,driver,driverVersion,db,sessionStart," + + "runMins," + + "loadWarehouses,runWarehouses,numSUTThreads," + + "limitTxnsPerMin," + + "thinkTimeMultiplier,keyingTimeMultiplier\n"); + } catch (IOException e) { + log.error(e.getMessage()); + System.exit(1); + } + log.info("Term-00, created " + runInfoCSVName + " for runID " + + runID); + + // Open the per transaction result.csv file. + String resultCSVName = new File(resultDataDir, "result.csv").getPath(); + try { + resultCSV = new BufferedWriter(new FileWriter(resultCSVName)); + resultCSV.write("run,elapsed,latency,dblatency," + + "ttype,rbk,dskipped,error\n"); + } catch (IOException e) { + log.error(e.getMessage()); + System.exit(1); + } + log.info("Term-00, writing per transaction results to " + + resultCSVName); + + if (osCollectorScript != null) { + osCollector = new OSCollector(getProp(ini, "osCollectorScript"), + runID, + Integer.parseInt(getProp(ini, "osCollectorInterval")), + getProp(ini, "osCollectorSSHAddr"), + getProp(ini, "osCollectorDevices"), + resultDataDir, log); + } + + log.info("Term-00,"); + } + + if (databaseDriverLoaded) { + try { + boolean limitIsTime = iRunMinsBool; + int numTerminals = -1; + int transactionsPerTerminal = -1; + int numWarehouses = -1; + int loadWarehouses = -1; + int newOrderWeightValue = -1, paymentWeightValue = -1, orderStatusWeightValue = -1, + deliveryWeightValue = -1, stockLevelWeightValue = -1; + long executionTimeMillis = -1; + boolean terminalWarehouseFixed = true; + long CLoad; + + Properties dbProps = new Properties(); + dbProps.setProperty("user", iUser); + dbProps.setProperty("password", iPassword); + + /* + * Fine tuning of database conneciton parameters if needed. + */ + switch (dbType) { + case DB_FIREBIRD: + /* + * Firebird needs no_rec_version for our load + * to work. Even with that some "deadlocks" + * occur. Note that the message "deadlock" in + * Firebird can mean something completely different, + * namely that there was a conflicting write to + * a row that could not be resolved. + */ + dbProps.setProperty("TRANSACTION_READ_COMMITTED", + "isc_tpb_read_committed," + + "isc_tpb_no_rec_version," + + "isc_tpb_write," + + "isc_tpb_wait"); + break; + + default: + break; + } + + try { + loadWarehouses = Integer.parseInt(jTPCCUtil.getConfig(iConn, + dbProps, "warehouses")); + CLoad = Long.parseLong(jTPCCUtil.getConfig(iConn, + dbProps, "nURandCLast")); + } catch (Exception e) { + errorMessage(e.getMessage()); + throw e; + } + this.rnd = new jTPCCRandom(CLoad); + log.info("Term-00, C value for C_LAST during load: " + CLoad); + log.info("Term-00, C value for C_LAST this run: " + rnd.getNURandCLast()); + log.info("Term-00, "); + + fastNewOrderCounter.add(0); + updateStatusLine(); + + try { + if (Integer.parseInt(iRunMins) != 0 && Integer.parseInt(iRunTxnsPerTerminal) == 0) { + iRunMinsBool = true; + } else if (Integer.parseInt(iRunMins) == 0 && Integer.parseInt(iRunTxnsPerTerminal) != 0) { + iRunMinsBool = false; + } else { + throw new NumberFormatException(); + } + } catch (NumberFormatException e1) { + errorMessage("Must indicate either transactions per terminal or number of run minutes!"); + throw new Exception(); + } + + try { + numWarehouses = Integer.parseInt(iWarehouses); + if (numWarehouses <= 0) + throw new NumberFormatException(); + } catch (NumberFormatException e1) { + errorMessage("Invalid number of warehouses!"); + throw new Exception(); + } + if (numWarehouses > loadWarehouses) { + errorMessage("numWarehouses cannot be greater " + + "than the warehouses loaded in the database"); + throw new Exception(); + } + + try { + numTerminals = Integer.parseInt(iTerminals); + if (numTerminals <= 0 || numTerminals > 10 * numWarehouses) + throw new NumberFormatException(); + } catch (NumberFormatException e1) { + errorMessage("Invalid number of terminals!"); + throw new Exception(); + } + + + if (Long.parseLong(iRunMins) != 0 && Integer.parseInt(iRunTxnsPerTerminal) == 0) { + try { + executionTimeMillis = Long.parseLong(iRunMins) * 60000; + if (executionTimeMillis <= 0) + throw new NumberFormatException(); + } catch (NumberFormatException e1) { + errorMessage("Invalid number of minutes!"); + throw new Exception(); + } + } else { + try { + transactionsPerTerminal = Integer.parseInt(iRunTxnsPerTerminal); + if (transactionsPerTerminal <= 0) + throw new NumberFormatException(); + } catch (NumberFormatException e1) { + errorMessage("Invalid number of transactions per terminal!"); + throw new Exception(); + } + } + + terminalWarehouseFixed = Boolean.parseBoolean(iTermWhseFixed); + + try { + newOrderWeightValue = Integer.parseInt(iNewOrderWeight); + paymentWeightValue = Integer.parseInt(iPaymentWeight); + orderStatusWeightValue = Integer.parseInt(iOrderStatusWeight); + deliveryWeightValue = Integer.parseInt(iDeliveryWeight); + stockLevelWeightValue = Integer.parseInt(iStockLevelWeight); + + if (newOrderWeightValue < 0 || paymentWeightValue < 0 || orderStatusWeightValue < 0 + || deliveryWeightValue < 0 || stockLevelWeightValue < 0) + throw new NumberFormatException(); + else if (newOrderWeightValue == 0 && paymentWeightValue == 0 && orderStatusWeightValue == 0 + && deliveryWeightValue == 0 && stockLevelWeightValue == 0) + throw new NumberFormatException(); + } catch (NumberFormatException e1) { + errorMessage("Invalid number in mix percentage!"); + throw new Exception(); + } + + if (newOrderWeightValue + paymentWeightValue + orderStatusWeightValue + + deliveryWeightValue + stockLevelWeightValue > 100) { + errorMessage("Sum of mix percentage parameters exceeds 100%!"); + throw new Exception(); + } + + newOrderCounter = 0; + printMessage("Session started!"); + if (!limitIsTime) + printMessage("Creating " + numTerminals + " terminal(s) with " + + transactionsPerTerminal + " transaction(s) per terminal..."); + else + printMessage("Creating " + numTerminals + " terminal(s) with " + + (executionTimeMillis / 60000) + " minute(s) of execution..."); + if (terminalWarehouseFixed) + printMessage("Terminal Warehouse is fixed"); + else + printMessage("Terminal Warehouse is NOT fixed"); + printMessage("Transaction Weights: " + newOrderWeightValue + "% New-Order, " + + paymentWeightValue + "% Payment, " + orderStatusWeightValue + "% Order-Status, " + + deliveryWeightValue + "% Delivery, " + stockLevelWeightValue + "% Stock-Level"); + + printMessage("Number of Terminals\t" + numTerminals); + + terminals = new jTPCCTerminal[numTerminals]; + terminalNames = new String[numTerminals]; + terminalsStarted.add(numTerminals); + try { + String database = iConn; + String username = iUser; + String password = iPassword; + + int[][] usedTerminals = new int[numWarehouses][10]; + for (int i = 0; i < numWarehouses; i++) + for (int j = 0; j < 10; j++) + usedTerminals[i][j] = 0; + + for (int i = 0; i < numTerminals; i++) { + int terminalWarehouseID; + int terminalDistrictID; + do { + terminalWarehouseID = rnd.nextInt(1, numWarehouses); + terminalDistrictID = rnd.nextInt(1, 10); + } while (usedTerminals[terminalWarehouseID - 1][terminalDistrictID - 1] == 1); + usedTerminals[terminalWarehouseID - 1][terminalDistrictID - 1] = 1; + + String terminalName = "Term-" + (i >= 9 ? "" + (i + 1) : "0" + (i + 1)); + Connection conn = null; + printMessage("Creating database connection for " + terminalName + "..."); + conn = DriverManager.getConnection(database, dbProps); + conn.setAutoCommit(false); + + jTPCCTerminal terminal = new jTPCCTerminal + (terminalName, terminalWarehouseID, terminalDistrictID, + conn, dbType, + transactionsPerTerminal, terminalWarehouseFixed, + paymentWeightValue, orderStatusWeightValue, + deliveryWeightValue, stockLevelWeightValue, + numWarehouses, limPerMin_Terminal, this); + + terminals[i] = terminal; + terminalNames[i] = terminalName; + printMessage(terminalName + "\t" + terminalWarehouseID); + } + + sessionEndTargetTime = executionTimeMillis; + signalTerminalsRequestEndSent = false; + + printMessage("Transaction\tWeight"); + printMessage("% New-Order\t" + newOrderWeightValue); + printMessage("% Payment\t" + paymentWeightValue); + printMessage("% Order-Status\t" + orderStatusWeightValue); + printMessage("% Delivery\t" + deliveryWeightValue); + printMessage("% Stock-Level\t" + stockLevelWeightValue); + + printMessage("Transaction Number\tTerminal\tType\tExecution Time (ms)\t\tComment"); + + printMessage("Created " + numTerminals + " terminal(s) successfully!"); + boolean dummvar = true; + + // Create Terminals, Start Transactions + sessionStart = getCurrentTime(); + sessionStartTimestamp = System.currentTimeMillis(); + sessionNextTimestamp = sessionStartTimestamp; + if (sessionEndTargetTime != -1) + sessionEndTargetTime += sessionStartTimestamp; + + // Record run parameters in runInfo.csv + if (runInfoCSV != null) { + try { + StringBuffer infoSB = new StringBuffer(); + Formatter infoFmt = new Formatter(infoSB); + infoFmt.format("%d,simple,%s,%s,%s,%s,%d,%d,%d,%d,1.0,1.0\n", + runID, JTPCCVERSION, iDB, + new java.sql.Timestamp(sessionStartTimestamp).toString(), + iRunMins, + loadWarehouses, + numWarehouses, + numTerminals, + Integer.parseInt(limPerMin)); + runInfoCSV.write(infoSB.toString()); + runInfoCSV.close(); + } catch (Exception e) { + log.error(e.getMessage()); + System.exit(1); + } + } + + printMessage("Starting all terminals..."); + transactionCount.add(1); + for (int i = 0; i < terminals.length; i++) + (new Thread(terminals[i])).start(); + + printMessage("All terminals started executing " + sessionStart); + } catch (Exception e1) { + errorMessage("This session ended with errors!"); + printStreamReport.close(); + fileOutputStream.close(); + + throw new Exception(); + } + + } catch (Exception ex) { + } + } + updateStatusLine(); } - private void signalTerminalsRequestEnd(boolean timeTriggered) - { - synchronized(terminals) - { - if(!signalTerminalsRequestEndSent) - { - if(timeTriggered) - printMessage("The time limit has been reached."); - printMessage("Signalling all terminals to stop..."); - signalTerminalsRequestEndSent = true; - - for(int i = 0; i < terminals.length; i++) - if(terminals[i] != null) - terminals[i].stopRunningWhenPossible(); - - printMessage("Waiting for all active transactions to end..."); - } - } + private void signalTerminalsRequestEnd(boolean timeTriggered) { + try { + terminalsLock.lock(); + if (!signalTerminalsRequestEndSent) { + if (timeTriggered) + printMessage("The time limit has been reached."); + printMessage("Signalling all terminals to stop..."); + signalTerminalsRequestEndSent = true; + + for (int i = 0; i < terminals.length; i++) + if (terminals[i] != null) + terminals[i].stopRunningWhenPossible(); + + printMessage("Waiting for all active transactions to end..."); + } + } finally { + terminalsLock.unlock(); + } } - public void signalTerminalEnded(jTPCCTerminal terminal, long countNewOrdersExecuted) - { - synchronized(terminals) - { - boolean found = false; - terminalsStarted--; - for(int i = 0; i < terminals.length && !found; i++) - { - if(terminals[i] == terminal) - { - terminals[i] = null; - terminalNames[i] = "(" + terminalNames[i] + ")"; - newOrderCounter += countNewOrdersExecuted; - found = true; - } - } - } - - if(terminalsStarted == 0) - { - sessionEnd = getCurrentTime(); - sessionEndTimestamp = System.currentTimeMillis(); - sessionEndTargetTime = -1; - printMessage("All terminals finished executing " + sessionEnd); - endReport(); - terminalsBlockingExit = false; - printMessage("Session finished!"); - - // If we opened a per transaction result file, close it. - if (resultCSV != null) - { - try { - resultCSV.close(); - } catch (IOException e) { - log.error(e.getMessage()); - }; - } - - // Stop the OSCollector, if it is active. - if (osCollector != null) - { - osCollector.stop(); - osCollector = null; - } - } + public void signalTerminalEnded(jTPCCTerminal terminal, long countNewOrdersExecuted) { + try { + terminalsLock.lock(); + + terminalsStarted.decrement(); + boolean found = false; + for (int i = 0; i < terminals.length && !found; i++) { + if (terminals[i] == terminal) { + terminals[i] = null; + terminalNames[i] = "(" + terminalNames[i] + ")"; + newOrderCounter += countNewOrdersExecuted; + found = true; + } + } + + } finally { + terminalsLock.unlock(); + } + + if (terminalsStarted.longValue() == 0) { + sessionEnd = getCurrentTime(); + sessionEndTimestamp = System.currentTimeMillis(); + sessionEndTargetTime = -1; + printMessage("All terminals finished executing " + sessionEnd); + endReport(); + terminalsBlockingExit = false; + printMessage("Session finished!"); + + // If we opened a per transaction result file, close it. + if (resultCSV != null) { + try { + resultCSV.close(); + } catch (IOException e) { + log.error(e.getMessage()); + } + } + + // Stop the OSCollector, if it is active. + if (osCollector != null) { + osCollector.stop(); + osCollector = null; + } + } } - public void signalTerminalEndedTransaction(String terminalName, String transactionType, long executionTime, String comment, int newOrder) - { - synchronized (counterLock) - { - transactionCount++; - fastNewOrderCounter += newOrder; - Long counter = costPerWorkerload.get(transactionType); - if (counter == null) { - costPerWorkerload.put(transactionType, Long.valueOf(executionTime)); - } else { - costPerWorkerload.put(transactionType, counter + executionTime); - } - } - - if(sessionEndTargetTime != -1 && System.currentTimeMillis() > sessionEndTargetTime) - { - signalTerminalsRequestEnd(true); - } - - updateStatusLine(); + public void signalTerminalEndedTransaction(String terminalName, String transactionType, + long executionTime, String comment, int newOrder) { + try { + transactionLock.lock(); + transactionCount.increment(); + fastNewOrderCounter.add(newOrder); + Long counter = costPerWorkerload.get(transactionType); + if (counter == null) { + costPerWorkerload.put(transactionType, Long.valueOf(executionTime)); + } else { + costPerWorkerload.put(transactionType, counter + executionTime); + + } + } finally { + transactionLock.unlock(); + } + + if (sessionEndTargetTime != -1 && System.currentTimeMillis() > sessionEndTargetTime) { + signalTerminalsRequestEnd(true); + } + + updateStatusLine(); } - public jTPCCRandom getRnd() - { - return rnd; + public jTPCCRandom getRnd() { + return rnd; } - public void resultAppend(jTPCCTData term) - { - if (resultCSV != null) - { - try - { - resultCSV.write(runID + "," + - term.resultLine(sessionStartTimestamp)); - } - catch (IOException e) - { - log.error("Term-00, " + e.getMessage()); - } - } + public void resultAppend(jTPCCTData term) { + if (resultCSV != null) { + try { + resultCSV.write(runID + "," + + term.resultLine(sessionStartTimestamp)); + } catch (IOException e) { + log.error("Term-00, " + e.getMessage()); + } + } } - private void endReport() - { - long currTimeMillis = System.currentTimeMillis(); - long freeMem = Runtime.getRuntime().freeMemory() / (1024*1024); - long totalMem = Runtime.getRuntime().totalMemory() / (1024*1024); - double tpmC = (6000000*fastNewOrderCounter/(currTimeMillis - sessionStartTimestamp))/100.0; - double tpmTotal = (6000000*transactionCount/(currTimeMillis - sessionStartTimestamp))/100.0; - - System.out.println(""); - log.info("Term-00, "); - log.info("Term-00, "); - log.info("Term-00, Measured tpmC (NewOrders) = " + tpmC); - log.info("Term-00, Measured tpmTOTAL = " + tpmTotal); - log.info("Term-00, Session Start = " + sessionStart ); - log.info("Term-00, Session End = " + sessionEnd); - log.info("Term-00, Transaction Count = " + (transactionCount-1)); - for (String key : costPerWorkerload.keySet()) { - Long value = costPerWorkerload.get(key); - log.info("executeTime[" + key + "]=" + value.toString()); - } + private void endReport() { + long currTimeMillis = System.currentTimeMillis(); + long freeMem = Runtime.getRuntime().freeMemory() / (1024 * 1024); + long totalMem = Runtime.getRuntime().totalMemory() / (1024 * 1024); + double tpmC = (6000000 * fastNewOrderCounter.intValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; + double tpmTotal = (6000000 * transactionCount.intValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; + + System.out.println(""); + log.info("Term-00, "); + log.info("Term-00, "); + log.info("Term-00, Measured tpmC (NewOrders) = " + tpmC); + log.info("Term-00, Measured tpmTOTAL = " + tpmTotal); + log.info("Term-00, Session Start = " + sessionStart); + log.info("Term-00, Session End = " + sessionEnd); + log.info("Term-00, Transaction Count = " + (transactionCount.intValue() - 1)); + for (String key : costPerWorkerload.keySet()) { + Long value = costPerWorkerload.get(key); + log.info("executeTime[" + key + "]=" + value.toString()); + } } - private void printMessage(String message) - { - log.trace("Term-00, " + message); + private void printMessage(String message) { + log.trace("Term-00, " + message); } - private void errorMessage(String message) - { - log.error("Term-00, "+ message); + private void errorMessage(String message) { + log.error("Term-00, " + message); } - private void exit() - { - System.exit(0); + private void exit() { + System.exit(0); } - private String getCurrentTime() - { - return dateFormat.format(new java.util.Date()); + private String getCurrentTime() { + return dateFormat.format(new java.util.Date()); } - private String getFileNameSuffix() - { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); - return dateFormat.format(new java.util.Date()); + private String getFileNameSuffix() { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); + return dateFormat.format(new java.util.Date()); } - synchronized private void updateStatusLine() - { - long currTimeMillis = System.currentTimeMillis(); + private Lock statusLock = new ReentrantLock(); + + private void updateStatusLine() { + long currTimeMillis = System.currentTimeMillis(); + + StringBuilder informativeText = new StringBuilder(""); + Formatter fmt = new Formatter(informativeText); + try { + statusLock.lock(); - if(currTimeMillis > sessionNextTimestamp) - { - StringBuilder informativeText = new StringBuilder(""); - Formatter fmt = new Formatter(informativeText); - double tpmC = (6000000*fastNewOrderCounter/(currTimeMillis - sessionStartTimestamp))/100.0; - double tpmTotal = (6000000*transactionCount/(currTimeMillis - sessionStartTimestamp))/100.0; + if (currTimeMillis > sessionNextTimestamp) { + double tpmC = (6000000 * fastNewOrderCounter.intValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; + double tpmTotal = (6000000 * transactionCount.intValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; - sessionNextTimestamp += 1000; /* update this every seconds */ + sessionNextTimestamp += 1000; /* update this every seconds */ - fmt.format("Term-00, Running Average tpmTOTAL: %.2f", tpmTotal); + fmt.format("Term-00, Running Average tpmTOTAL: %.2f", tpmTotal); - /* XXX What is the meaning of these numbers? */ - recentTpmC = (fastNewOrderCounter - sessionNextKounter) * 12; - recentTpmTotal= (transactionCount-sessionNextKounter)*12; - sessionNextKounter = fastNewOrderCounter; - fmt.format(" Current tpmTOTAL: %d", recentTpmTotal); + /* XXX What is the meaning of these numbers? */ + recentTpmC = (fastNewOrderCounter.intValue() - sessionNextKounter) * 12; + recentTpmTotal = (transactionCount.intValue() - sessionNextKounter) * 12; + sessionNextKounter = fastNewOrderCounter.intValue(); + fmt.format(" Current tpmTOTAL: %d", recentTpmTotal); - long freeMem = Runtime.getRuntime().freeMemory() / (1024*1024); - long totalMem = Runtime.getRuntime().totalMemory() / (1024*1024); - fmt.format(" Memory Usage: %dMB / %dMB ", (totalMem - freeMem), totalMem); + long freeMem = Runtime.getRuntime().freeMemory() / (1024 * 1024); + long totalMem = Runtime.getRuntime().totalMemory() / (1024 * 1024); + fmt.format(" Memory Usage: %dMB / %dMB ", (totalMem - freeMem), totalMem); - System.out.print(informativeText); - for (int count = 0; count < 1+informativeText.length(); count++) - System.out.print("\b"); - } + System.out.print(informativeText); + for (int count = 0; count < 1 + informativeText.length(); count++) + System.out.print("\b"); + } + } finally { + statusLock.unlock(); + } } } diff --git a/src/client/jTPCCConfig.java b/src/client/jTPCCConfig.java index 201a2c8..f0da1dd 100644 --- a/src/client/jTPCCConfig.java +++ b/src/client/jTPCCConfig.java @@ -7,32 +7,31 @@ * */ -import java.text.*; +import java.text.SimpleDateFormat; -public interface jTPCCConfig -{ +public interface jTPCCConfig { public final static String JTPCCVERSION = "5.0"; - public final static int DB_UNKNOWN = 0, - DB_FIREBIRD = 1, - DB_ORACLE = 2, - DB_POSTGRES = 3, - DB_MYSQL = 4; + public final static int DB_UNKNOWN = 0, + DB_FIREBIRD = 1, + DB_ORACLE = 2, + DB_POSTGRES = 3, + DB_MYSQL = 4; - public final static int NEW_ORDER = 1, - PAYMENT = 2, - ORDER_STATUS = 3, - DELIVERY = 4, - STOCK_LEVEL = 5; + public final static int NEW_ORDER = 1, + PAYMENT = 2, + ORDER_STATUS = 3, + DELIVERY = 4, + STOCK_LEVEL = 5; public final static String[] nameTokens = {"BAR", "OUGHT", "ABLE", "PRI", "PRES", "ESE", "ANTI", "CALLY", "ATION", "EING"}; public final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - public final static int configCommitCount = 10000; // commit every n records in LoadData + public final static int configCommitCount = 10000; // commit every n records in LoadData - public final static int configWhseCount = 10; - public final static int configItemCount = 100000; // tpc-c std = 100,000 - public final static int configDistPerWhse = 10; // tpc-c std = 10 - public final static int configCustPerDist = 3000; // tpc-c std = 3,000 + public final static int configWhseCount = 10; + public final static int configItemCount = 100000; // tpc-c std = 100,000 + public final static int configDistPerWhse = 10; // tpc-c std = 10 + public final static int configCustPerDist = 3000; // tpc-c std = 3,000 } diff --git a/src/client/jTPCCConnection.java b/src/client/jTPCCConnection.java index bb36623..b92bbc8 100644 --- a/src/client/jTPCCConnection.java +++ b/src/client/jTPCCConnection.java @@ -9,313 +9,310 @@ * */ -import java.util.*; -import java.sql.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Properties; -public class jTPCCConnection -{ - private Connection dbConn = null; - private int dbType = 0; +public class jTPCCConnection { + private Connection dbConn = null; + private int dbType = 0; - public PreparedStatement stmtNewOrderSelectWhseCust; - public PreparedStatement stmtNewOrderSelectDist; - public PreparedStatement stmtNewOrderUpdateDist; - public PreparedStatement stmtNewOrderInsertOrder; - public PreparedStatement stmtNewOrderInsertNewOrder; - public PreparedStatement stmtNewOrderSelectStock; - public PreparedStatement stmtNewOrderSelectStockBatch[]; - public PreparedStatement stmtNewOrderSelectItem; - public PreparedStatement stmtNewOrderSelectItemBatch[]; - public PreparedStatement stmtNewOrderUpdateStock; - public PreparedStatement stmtNewOrderInsertOrderLine; + public PreparedStatement stmtNewOrderSelectWhseCust; + public PreparedStatement stmtNewOrderSelectDist; + public PreparedStatement stmtNewOrderUpdateDist; + public PreparedStatement stmtNewOrderInsertOrder; + public PreparedStatement stmtNewOrderInsertNewOrder; + public PreparedStatement stmtNewOrderSelectStock; + public PreparedStatement stmtNewOrderSelectStockBatch[]; + public PreparedStatement stmtNewOrderSelectItem; + public PreparedStatement stmtNewOrderSelectItemBatch[]; + public PreparedStatement stmtNewOrderUpdateStock; + public PreparedStatement stmtNewOrderInsertOrderLine; - public PreparedStatement stmtPaymentSelectWarehouse; - public PreparedStatement stmtPaymentSelectDistrict; - public PreparedStatement stmtPaymentSelectCustomerListByLast; - public PreparedStatement stmtPaymentSelectCustomer; - public PreparedStatement stmtPaymentSelectCustomerData; - public PreparedStatement stmtPaymentUpdateWarehouse; - public PreparedStatement stmtPaymentUpdateDistrict; - public PreparedStatement stmtPaymentUpdateCustomer; - public PreparedStatement stmtPaymentUpdateCustomerWithData; - public PreparedStatement stmtPaymentInsertHistory; + public PreparedStatement stmtPaymentSelectWarehouse; + public PreparedStatement stmtPaymentSelectDistrict; + public PreparedStatement stmtPaymentSelectCustomerListByLast; + public PreparedStatement stmtPaymentSelectCustomer; + public PreparedStatement stmtPaymentSelectCustomerData; + public PreparedStatement stmtPaymentUpdateWarehouse; + public PreparedStatement stmtPaymentUpdateDistrict; + public PreparedStatement stmtPaymentUpdateCustomer; + public PreparedStatement stmtPaymentUpdateCustomerWithData; + public PreparedStatement stmtPaymentInsertHistory; - public PreparedStatement stmtOrderStatusSelectCustomerListByLast; - public PreparedStatement stmtOrderStatusSelectCustomer; - public PreparedStatement stmtOrderStatusSelectLastOrder; - public PreparedStatement stmtOrderStatusSelectOrderLine; + public PreparedStatement stmtOrderStatusSelectCustomerListByLast; + public PreparedStatement stmtOrderStatusSelectCustomer; + public PreparedStatement stmtOrderStatusSelectLastOrder; + public PreparedStatement stmtOrderStatusSelectOrderLine; - public PreparedStatement stmtStockLevelSelectLow; + public PreparedStatement stmtStockLevelSelectLow; - public PreparedStatement stmtDeliveryBGSelectOldestNewOrder; - public PreparedStatement stmtDeliveryBGDeleteOldestNewOrder; - public PreparedStatement stmtDeliveryBGSelectOrder; - public PreparedStatement stmtDeliveryBGUpdateOrder; - public PreparedStatement stmtDeliveryBGSelectSumOLAmount; - public PreparedStatement stmtDeliveryBGUpdateOrderLine; - public PreparedStatement stmtDeliveryBGUpdateCustomer; + public PreparedStatement stmtDeliveryBGSelectOldestNewOrder; + public PreparedStatement stmtDeliveryBGDeleteOldestNewOrder; + public PreparedStatement stmtDeliveryBGSelectOrder; + public PreparedStatement stmtDeliveryBGUpdateOrder; + public PreparedStatement stmtDeliveryBGSelectSumOLAmount; + public PreparedStatement stmtDeliveryBGUpdateOrderLine; + public PreparedStatement stmtDeliveryBGUpdateCustomer; public jTPCCConnection(Connection dbConn, int dbType) - throws SQLException - { - this.dbConn = dbConn; - this.dbType = dbType; - stmtNewOrderSelectStockBatch = new PreparedStatement[16]; - String st = "SELECT s_i_id, s_w_id, 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 bmsql_stock " + - " WHERE (s_w_id, s_i_id) in ((?,?)"; - for (int i = 1; i <= 15; i ++) { - String stmtStr = st + ") FOR UPDATE"; - stmtNewOrderSelectStockBatch[i] = dbConn.prepareStatement(stmtStr); - st += ",(?,?)"; - } - stmtNewOrderSelectItemBatch = new PreparedStatement[16]; - st = "SELECT i_id, i_price, i_name, i_data " + - " FROM bmsql_item WHERE i_id in (?"; - for (int i = 1; i <= 15; i ++) { - String stmtStr = st + ")"; - stmtNewOrderSelectItemBatch[i] = dbConn.prepareStatement(stmtStr); - st += ",?"; - } + throws SQLException { + this.dbConn = dbConn; + this.dbType = dbType; + stmtNewOrderSelectStockBatch = new PreparedStatement[16]; + String st = "SELECT s_i_id, s_w_id, 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 bmsql_stock " + + " WHERE (s_w_id, s_i_id) in ((?,?)"; + for (int i = 1; i <= 15; i++) { + String stmtStr = st + ") FOR UPDATE"; + stmtNewOrderSelectStockBatch[i] = dbConn.prepareStatement(stmtStr); + st += ",(?,?)"; + } + stmtNewOrderSelectItemBatch = new PreparedStatement[16]; + st = "SELECT i_id, i_price, i_name, i_data " + + " FROM bmsql_item WHERE i_id in (?"; + for (int i = 1; i <= 15; i++) { + String stmtStr = st + ")"; + stmtNewOrderSelectItemBatch[i] = dbConn.prepareStatement(stmtStr); + st += ",?"; + } - // PreparedStataments for NEW_ORDER - stmtNewOrderSelectWhseCust = dbConn.prepareStatement( - "SELECT c_discount, c_last, c_credit, w_tax " + - " FROM bmsql_customer " + - " JOIN bmsql_warehouse ON (w_id = c_w_id) " + - " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); - stmtNewOrderSelectDist = dbConn.prepareStatement( - "SELECT d_tax, d_next_o_id " + - " FROM bmsql_district " + - " WHERE d_w_id = ? AND d_id = ? " + - " FOR UPDATE"); - stmtNewOrderUpdateDist = dbConn.prepareStatement( - "UPDATE bmsql_district " + - " SET d_next_o_id = d_next_o_id + 1 " + - " WHERE d_w_id = ? AND d_id = ?"); - stmtNewOrderInsertOrder = dbConn.prepareStatement( - "INSERT INTO bmsql_oorder (" + - " o_id, o_d_id, o_w_id, o_c_id, o_entry_d, " + - " o_ol_cnt, o_all_local) " + - "VALUES (?, ?, ?, ?, ?, ?, ?)"); - stmtNewOrderInsertNewOrder = dbConn.prepareStatement( - "INSERT INTO bmsql_new_order (" + - " no_o_id, no_d_id, no_w_id) " + - "VALUES (?, ?, ?)"); - stmtNewOrderSelectStock = dbConn.prepareStatement( - "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 bmsql_stock " + - " WHERE s_w_id = ? AND s_i_id = ? " + - " FOR UPDATE"); - stmtNewOrderSelectItem = dbConn.prepareStatement( - "SELECT i_price, i_name, i_data " + - " FROM bmsql_item " + - " WHERE i_id = ?"); - stmtNewOrderUpdateStock = dbConn.prepareStatement( - "UPDATE bmsql_stock " + - " SET s_quantity = ?, s_ytd = s_ytd + ?, " + - " s_order_cnt = s_order_cnt + 1, " + - " s_remote_cnt = s_remote_cnt + ? " + - " WHERE s_w_id = ? AND s_i_id = ?"); - stmtNewOrderInsertOrderLine = dbConn.prepareStatement( - "INSERT INTO bmsql_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 (?, ?, ?, ?, ?, ?, ?, ?, ?)"); + // PreparedStataments for NEW_ORDER + stmtNewOrderSelectWhseCust = dbConn.prepareStatement( + "SELECT c_discount, c_last, c_credit, w_tax " + + " FROM bmsql_customer " + + " JOIN bmsql_warehouse ON (w_id = c_w_id) " + + " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); + stmtNewOrderSelectDist = dbConn.prepareStatement( + "SELECT d_tax, d_next_o_id " + + " FROM bmsql_district " + + " WHERE d_w_id = ? AND d_id = ? " + + " FOR UPDATE"); + stmtNewOrderUpdateDist = dbConn.prepareStatement( + "UPDATE bmsql_district " + + " SET d_next_o_id = d_next_o_id + 1 " + + " WHERE d_w_id = ? AND d_id = ?"); + stmtNewOrderInsertOrder = dbConn.prepareStatement( + "INSERT INTO bmsql_oorder (" + + " o_id, o_d_id, o_w_id, o_c_id, o_entry_d, " + + " o_ol_cnt, o_all_local) " + + "VALUES (?, ?, ?, ?, ?, ?, ?)"); + stmtNewOrderInsertNewOrder = dbConn.prepareStatement( + "INSERT INTO bmsql_new_order (" + + " no_o_id, no_d_id, no_w_id) " + + "VALUES (?, ?, ?)"); + stmtNewOrderSelectStock = dbConn.prepareStatement( + "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 bmsql_stock " + + " WHERE s_w_id = ? AND s_i_id = ? " + + " FOR UPDATE"); + stmtNewOrderSelectItem = dbConn.prepareStatement( + "SELECT i_price, i_name, i_data " + + " FROM bmsql_item " + + " WHERE i_id = ?"); + stmtNewOrderUpdateStock = dbConn.prepareStatement( + "UPDATE bmsql_stock " + + " SET s_quantity = ?, s_ytd = s_ytd + ?, " + + " s_order_cnt = s_order_cnt + 1, " + + " s_remote_cnt = s_remote_cnt + ? " + + " WHERE s_w_id = ? AND s_i_id = ?"); + stmtNewOrderInsertOrderLine = dbConn.prepareStatement( + "INSERT INTO bmsql_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 (?, ?, ?, ?, ?, ?, ?, ?, ?)"); - // PreparedStatements for PAYMENT - stmtPaymentSelectWarehouse = dbConn.prepareStatement( - "SELECT w_name, w_street_1, w_street_2, w_city, " + - " w_state, w_zip " + - " FROM bmsql_warehouse " + - " WHERE w_id = ? "); - stmtPaymentSelectDistrict = dbConn.prepareStatement( - "SELECT d_name, d_street_1, d_street_2, d_city, " + - " d_state, d_zip " + - " FROM bmsql_district " + - " WHERE d_w_id = ? AND d_id = ?"); - stmtPaymentSelectCustomerListByLast = dbConn.prepareStatement( - "SELECT c_id " + - " FROM bmsql_customer " + - " WHERE c_w_id = ? AND c_d_id = ? AND c_last = ? " + - " ORDER BY c_first"); - stmtPaymentSelectCustomer = dbConn.prepareStatement( - "SELECT c_first, c_middle, c_last, c_street_1, c_street_2, " + - " c_city, c_state, c_zip, c_phone, c_since, c_credit, " + - " c_credit_lim, c_discount, c_balance " + - " FROM bmsql_customer " + - " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ? " + - " FOR UPDATE"); - stmtPaymentSelectCustomerData = dbConn.prepareStatement( - "SELECT c_data " + - " FROM bmsql_customer " + - " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); - stmtPaymentUpdateWarehouse = dbConn.prepareStatement( - "UPDATE bmsql_warehouse " + - " SET w_ytd = w_ytd + ? " + - " WHERE w_id = ?"); - stmtPaymentUpdateDistrict = dbConn.prepareStatement( - "UPDATE bmsql_district " + - " SET d_ytd = d_ytd + ? " + - " WHERE d_w_id = ? AND d_id = ?"); - stmtPaymentUpdateCustomer = dbConn.prepareStatement( - "UPDATE bmsql_customer " + - " SET c_balance = c_balance - ?, " + - " c_ytd_payment = c_ytd_payment + ?, " + - " c_payment_cnt = c_payment_cnt + 1 " + - " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); - stmtPaymentUpdateCustomerWithData = dbConn.prepareStatement( - "UPDATE bmsql_customer " + - " SET c_balance = c_balance - ?, " + - " c_ytd_payment = c_ytd_payment + ?, " + - " c_payment_cnt = c_payment_cnt + 1, " + - " c_data = ? " + - " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); - stmtPaymentInsertHistory = dbConn.prepareStatement( - "INSERT INTO bmsql_history (" + - " h_c_id, h_c_d_id, h_c_w_id, h_d_id, h_w_id, " + - " h_date, h_amount, h_data) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); + // PreparedStatements for PAYMENT + stmtPaymentSelectWarehouse = dbConn.prepareStatement( + "SELECT w_name, w_street_1, w_street_2, w_city, " + + " w_state, w_zip " + + " FROM bmsql_warehouse " + + " WHERE w_id = ? "); + stmtPaymentSelectDistrict = dbConn.prepareStatement( + "SELECT d_name, d_street_1, d_street_2, d_city, " + + " d_state, d_zip " + + " FROM bmsql_district " + + " WHERE d_w_id = ? AND d_id = ?"); + stmtPaymentSelectCustomerListByLast = dbConn.prepareStatement( + "SELECT c_id " + + " FROM bmsql_customer " + + " WHERE c_w_id = ? AND c_d_id = ? AND c_last = ? " + + " ORDER BY c_first"); + stmtPaymentSelectCustomer = dbConn.prepareStatement( + "SELECT c_first, c_middle, c_last, c_street_1, c_street_2, " + + " c_city, c_state, c_zip, c_phone, c_since, c_credit, " + + " c_credit_lim, c_discount, c_balance " + + " FROM bmsql_customer " + + " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ? " + + " FOR UPDATE"); + stmtPaymentSelectCustomerData = dbConn.prepareStatement( + "SELECT c_data " + + " FROM bmsql_customer " + + " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); + stmtPaymentUpdateWarehouse = dbConn.prepareStatement( + "UPDATE bmsql_warehouse " + + " SET w_ytd = w_ytd + ? " + + " WHERE w_id = ?"); + stmtPaymentUpdateDistrict = dbConn.prepareStatement( + "UPDATE bmsql_district " + + " SET d_ytd = d_ytd + ? " + + " WHERE d_w_id = ? AND d_id = ?"); + stmtPaymentUpdateCustomer = dbConn.prepareStatement( + "UPDATE bmsql_customer " + + " SET c_balance = c_balance - ?, " + + " c_ytd_payment = c_ytd_payment + ?, " + + " c_payment_cnt = c_payment_cnt + 1 " + + " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); + stmtPaymentUpdateCustomerWithData = dbConn.prepareStatement( + "UPDATE bmsql_customer " + + " SET c_balance = c_balance - ?, " + + " c_ytd_payment = c_ytd_payment + ?, " + + " c_payment_cnt = c_payment_cnt + 1, " + + " c_data = ? " + + " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); + stmtPaymentInsertHistory = dbConn.prepareStatement( + "INSERT INTO bmsql_history (" + + " h_c_id, h_c_d_id, h_c_w_id, h_d_id, h_w_id, " + + " h_date, h_amount, h_data) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); - // PreparedStatements for ORDER_STATUS - stmtOrderStatusSelectCustomerListByLast = dbConn.prepareStatement( - "SELECT c_id " + - " FROM bmsql_customer " + - " WHERE c_w_id = ? AND c_d_id = ? AND c_last = ? " + - " ORDER BY c_first"); - stmtOrderStatusSelectCustomer = dbConn.prepareStatement( - "SELECT c_first, c_middle, c_last, c_balance " + - " FROM bmsql_customer " + - " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); - stmtOrderStatusSelectLastOrder = dbConn.prepareStatement( - "SELECT o_id, o_entry_d, o_carrier_id " + - " FROM bmsql_oorder " + - " WHERE o_w_id = ? AND o_d_id = ? AND o_c_id = ? " + - " ORDER BY o_id DESC LIMIT 1"); - stmtOrderStatusSelectOrderLine = dbConn.prepareStatement( - "SELECT ol_i_id, ol_supply_w_id, ol_quantity, " + - " ol_amount, ol_delivery_d " + - " FROM bmsql_order_line " + - " WHERE ol_w_id = ? AND ol_d_id = ? AND ol_o_id = ? " + - " ORDER BY ol_w_id, ol_d_id, ol_o_id, ol_number"); + // PreparedStatements for ORDER_STATUS + stmtOrderStatusSelectCustomerListByLast = dbConn.prepareStatement( + "SELECT c_id " + + " FROM bmsql_customer " + + " WHERE c_w_id = ? AND c_d_id = ? AND c_last = ? " + + " ORDER BY c_first"); + stmtOrderStatusSelectCustomer = dbConn.prepareStatement( + "SELECT c_first, c_middle, c_last, c_balance " + + " FROM bmsql_customer " + + " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); + stmtOrderStatusSelectLastOrder = dbConn.prepareStatement( + "SELECT o_id, o_entry_d, o_carrier_id " + + " FROM bmsql_oorder " + + " WHERE o_w_id = ? AND o_d_id = ? AND o_c_id = ? " + + " ORDER BY o_id DESC LIMIT 1"); + stmtOrderStatusSelectOrderLine = dbConn.prepareStatement( + "SELECT ol_i_id, ol_supply_w_id, ol_quantity, " + + " ol_amount, ol_delivery_d " + + " FROM bmsql_order_line " + + " WHERE ol_w_id = ? AND ol_d_id = ? AND ol_o_id = ? " + + " ORDER BY ol_w_id, ol_d_id, ol_o_id, ol_number"); - // PreparedStatements for STOCK_LEVEL - switch (dbType) - { - case jTPCCConfig.DB_POSTGRES: - case jTPCCConfig.DB_MYSQL: - stmtStockLevelSelectLow = dbConn.prepareStatement( - "SELECT count(*) AS low_stock FROM (" + - " SELECT s_w_id, s_i_id, s_quantity " + - " FROM bmsql_stock " + - " WHERE s_w_id = ? AND s_quantity < ? AND s_i_id IN (" + - " SELECT /*+ TIDB_INLJ(bmsql_order_line) */ ol_i_id " + - " FROM bmsql_district " + - " JOIN bmsql_order_line ON ol_w_id = d_w_id " + - " AND ol_d_id = d_id " + - " AND ol_o_id >= d_next_o_id - 20 " + - " AND ol_o_id < d_next_o_id " + - " WHERE d_w_id = ? AND d_id = ? " + - " ) " + - " ) AS L"); - break; + // PreparedStatements for STOCK_LEVEL + switch (dbType) { + case jTPCCConfig.DB_POSTGRES: + case jTPCCConfig.DB_MYSQL: + stmtStockLevelSelectLow = dbConn.prepareStatement( + "SELECT count(*) AS low_stock FROM (" + + " SELECT s_w_id, s_i_id, s_quantity " + + " FROM bmsql_stock " + + " WHERE s_w_id = ? AND s_quantity < ? AND s_i_id IN (" + + " SELECT /*+ TIDB_INLJ(bmsql_order_line) */ ol_i_id " + + " FROM bmsql_district " + + " JOIN bmsql_order_line ON ol_w_id = d_w_id " + + " AND ol_d_id = d_id " + + " AND ol_o_id >= d_next_o_id - 20 " + + " AND ol_o_id < d_next_o_id " + + " WHERE d_w_id = ? AND d_id = ? " + + " ) " + + " ) AS L"); + break; - default: - stmtStockLevelSelectLow = dbConn.prepareStatement( - "SELECT count(*) AS low_stock FROM (" + - " SELECT s_w_id, s_i_id, s_quantity " + - " FROM bmsql_stock " + - " WHERE s_w_id = ? AND s_quantity < ? AND s_i_id IN (" + - " SELECT ol_i_id " + - " FROM bmsql_district " + - " JOIN bmsql_order_line ON ol_w_id = d_w_id " + - " AND ol_d_id = d_id " + - " AND ol_o_id >= d_next_o_id - 20 " + - " AND ol_o_id < d_next_o_id " + - " WHERE d_w_id = ? AND d_id = ? " + - " ) " + - " )"); - break; - } + default: + stmtStockLevelSelectLow = dbConn.prepareStatement( + "SELECT count(*) AS low_stock FROM (" + + " SELECT s_w_id, s_i_id, s_quantity " + + " FROM bmsql_stock " + + " WHERE s_w_id = ? AND s_quantity < ? AND s_i_id IN (" + + " SELECT ol_i_id " + + " FROM bmsql_district " + + " JOIN bmsql_order_line ON ol_w_id = d_w_id " + + " AND ol_d_id = d_id " + + " AND ol_o_id >= d_next_o_id - 20 " + + " AND ol_o_id < d_next_o_id " + + " WHERE d_w_id = ? AND d_id = ? " + + " ) " + + " )"); + break; + } - // PreparedStatements for DELIVERY_BG - stmtDeliveryBGSelectOldestNewOrder = dbConn.prepareStatement( - "SELECT no_o_id " + - " FROM bmsql_new_order " + - " WHERE no_w_id = ? AND no_d_id = ? " + - " ORDER BY no_o_id ASC" + - " LIMIT 1" + - " FOR UPDATE"); - stmtDeliveryBGDeleteOldestNewOrder = dbConn.prepareStatement( - "DELETE FROM bmsql_new_order " + - " WHERE (no_w_id,no_d_id,no_o_id) IN (" + - "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)," + - "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?))"); + // PreparedStatements for DELIVERY_BG + stmtDeliveryBGSelectOldestNewOrder = dbConn.prepareStatement( + "SELECT no_o_id " + + " FROM bmsql_new_order " + + " WHERE no_w_id = ? AND no_d_id = ? " + + " ORDER BY no_o_id ASC" + + " LIMIT 1" + + " FOR UPDATE"); + stmtDeliveryBGDeleteOldestNewOrder = dbConn.prepareStatement( + "DELETE FROM bmsql_new_order " + + " WHERE (no_w_id,no_d_id,no_o_id) IN (" + + "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)," + + "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?))"); - stmtDeliveryBGSelectOrder = dbConn.prepareStatement( - "SELECT o_c_id, o_d_id" + - " FROM bmsql_oorder " + - " WHERE (o_w_id,o_d_id,o_id) IN (" + - "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)," + - "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?))"); + stmtDeliveryBGSelectOrder = dbConn.prepareStatement( + "SELECT o_c_id, o_d_id" + + " FROM bmsql_oorder " + + " WHERE (o_w_id,o_d_id,o_id) IN (" + + "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)," + + "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?))"); - stmtDeliveryBGUpdateOrder = dbConn.prepareStatement( - "UPDATE bmsql_oorder " + - " SET o_carrier_id = ? " + - " WHERE (o_w_id,o_d_id,o_id) IN (" + - "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)," + - "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?))"); + stmtDeliveryBGUpdateOrder = dbConn.prepareStatement( + "UPDATE bmsql_oorder " + + " SET o_carrier_id = ? " + + " WHERE (o_w_id,o_d_id,o_id) IN (" + + "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)," + + "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?))"); - stmtDeliveryBGSelectSumOLAmount = dbConn.prepareStatement( - "SELECT sum(ol_amount) AS sum_ol_amount, ol_d_id" + - " FROM bmsql_order_line " + - " WHERE (ol_w_id,ol_d_id,ol_o_id) IN (" + - "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)," + - "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)" + - ") GROUP BY ol_d_id"); + stmtDeliveryBGSelectSumOLAmount = dbConn.prepareStatement( + "SELECT sum(ol_amount) AS sum_ol_amount, ol_d_id" + + " FROM bmsql_order_line " + + " WHERE (ol_w_id,ol_d_id,ol_o_id) IN (" + + "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)," + + "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)" + + ") GROUP BY ol_d_id"); - stmtDeliveryBGUpdateOrderLine = dbConn.prepareStatement( - "UPDATE bmsql_order_line " + - " SET ol_delivery_d = ? " + - " WHERE (ol_w_id,ol_d_id,ol_o_id) IN (" + - "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)," + - "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?))"); + stmtDeliveryBGUpdateOrderLine = dbConn.prepareStatement( + "UPDATE bmsql_order_line " + + " SET ol_delivery_d = ? " + + " WHERE (ol_w_id,ol_d_id,ol_o_id) IN (" + + "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?)," + + "(?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?))"); - stmtDeliveryBGUpdateCustomer = dbConn.prepareStatement( - "UPDATE bmsql_customer " + - " SET c_balance = c_balance + ?, " + - " c_delivery_cnt = c_delivery_cnt + 1 " + - " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); + stmtDeliveryBGUpdateCustomer = dbConn.prepareStatement( + "UPDATE bmsql_customer " + + " SET c_balance = c_balance + ?, " + + " c_delivery_cnt = c_delivery_cnt + 1 " + + " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); } public jTPCCConnection(String connURL, Properties connProps, int dbType) - throws SQLException - { - this(DriverManager.getConnection(connURL, connProps), dbType); + throws SQLException { + this(DriverManager.getConnection(connURL, connProps), dbType); } public void commit() - throws SQLException - { - try { - dbConn.commit(); - } catch(SQLException e) { - throw new CommitException(); - } + throws SQLException { + try { + dbConn.commit(); + } catch (SQLException e) { + throw new CommitException(); + } } public void rollback() - throws SQLException - { - dbConn.rollback(); + throws SQLException { + dbConn.rollback(); } } diff --git a/src/client/jTPCCRandom.java b/src/client/jTPCCRandom.java index 05a4e75..e5c6fcc 100644 --- a/src/client/jTPCCRandom.java +++ b/src/client/jTPCCRandom.java @@ -9,29 +9,25 @@ */ -import java.io.*; -import java.sql.*; -import java.util.*; -import java.text.*; +import java.util.Random; -public class jTPCCRandom -{ +public class jTPCCRandom { private static final char[] aStringChars = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; private static final String[] cLastTokens = { - "BAR", "OUGHT", "ABLE", "PRI", "PRES", - "ESE", "ANTI", "CALLY", "ATION", "EING"}; + "BAR", "OUGHT", "ABLE", "PRI", "PRES", + "ESE", "ANTI", "CALLY", "ATION", "EING"}; - private static long nURandCLast; - private static long nURandCC_ID; - private static long nURandCI_ID; - private static boolean initialized = false; + private static long nURandCLast; + private static long nURandCC_ID; + private static long nURandCI_ID; + private static boolean initialized = false; - private Random random; + private Random random; /* * jTPCCRandom() @@ -39,17 +35,16 @@ public class jTPCCRandom * Used to create the master jTPCCRandom() instance for loading * the database. See below. */ - jTPCCRandom() - { - if (initialized) - throw new IllegalStateException("Global instance exists"); + jTPCCRandom() { + if (initialized) + throw new IllegalStateException("Global instance exists"); - this.random = new Random(System.nanoTime()); - jTPCCRandom.nURandCLast = nextLong(0, 255); - jTPCCRandom.nURandCC_ID = nextLong(0, 1023); - jTPCCRandom.nURandCI_ID = nextLong(0, 8191); + this.random = new Random(System.nanoTime()); + jTPCCRandom.nURandCLast = nextLong(0, 255); + jTPCCRandom.nURandCC_ID = nextLong(0, 1023); + jTPCCRandom.nURandCI_ID = nextLong(0, 8191); - initialized = true; + initialized = true; } /* @@ -64,35 +59,32 @@ public class jTPCCRandom * C_LAST must be excluded from the possible range during run * time, based on the number used during the load. */ - jTPCCRandom(long CLoad) - { - long delta; - - if (initialized) - throw new IllegalStateException("Global instance exists"); - - this.random = new Random(System.nanoTime()); - jTPCCRandom.nURandCC_ID = nextLong(0, 1023); - jTPCCRandom.nURandCI_ID = nextLong(0, 8191); - - do - { - jTPCCRandom.nURandCLast = nextLong(0, 255); - - delta = Math.abs(jTPCCRandom.nURandCLast - CLoad); - if (delta == 96 || delta == 112) - continue; - if (delta < 65 || delta > 119) - continue; - break; - } while(true); - - initialized = true; + jTPCCRandom(long CLoad) { + long delta; + + if (initialized) + throw new IllegalStateException("Global instance exists"); + + this.random = new Random(System.nanoTime()); + jTPCCRandom.nURandCC_ID = nextLong(0, 1023); + jTPCCRandom.nURandCI_ID = nextLong(0, 8191); + + do { + jTPCCRandom.nURandCLast = nextLong(0, 255); + + delta = Math.abs(jTPCCRandom.nURandCLast - CLoad); + if (delta == 96 || delta == 112) + continue; + if (delta < 65 || delta > 119) + continue; + break; + } while (true); + + initialized = true; } - private jTPCCRandom(jTPCCRandom parent) - { - this.random = new Random(System.nanoTime()); + private jTPCCRandom(jTPCCRandom parent) { + this.random = new Random(System.nanoTime()); } /* @@ -105,9 +97,8 @@ private jTPCCRandom(jTPCCRandom parent) * generate them per instance, but each thread's instance must * inherit those numbers from a global instance. */ - jTPCCRandom newRandom() - { - return new jTPCCRandom(this); + jTPCCRandom newRandom() { + return new jTPCCRandom(this); } @@ -116,9 +107,8 @@ jTPCCRandom newRandom() * * Produce a random number uniformly distributed in [x .. y] */ - public long nextLong(long x, long y) - { - return (long)(random.nextDouble() * (y - x + 1) + x); + public long nextLong(long x, long y) { + return (long) (random.nextDouble() * (y - x + 1) + x); } /* @@ -126,9 +116,8 @@ public long nextLong(long x, long y) * * Produce a random number uniformly distributed in [x .. y] */ - public int nextInt(int x, int y) - { - return (int)(random.nextDouble() * (y - x + 1) + x); + public int nextInt(int x, int y) { + return (int) (random.nextDouble() * (y - x + 1) + x); } /* @@ -145,23 +134,21 @@ public int nextInt(int x, int y) * of UTF8 related trouble by producing alphanumeric only * instead of cartoon style curse-bubbles. */ - public String getAString(long x, long y) - { - String result = new String(); - long len = nextLong(x, y); - long have = 1; - - if (y <= 0) - return result; - - result += aStringChars[(int)nextLong(0, 51)]; - while (have < len) - { - result += aStringChars[(int)nextLong(0, 61)]; - have++; - } - - return result; + public String getAString(long x, long y) { + String result = new String(); + long len = nextLong(x, y); + long have = 1; + + if (y <= 0) + return result; + + result += aStringChars[(int) nextLong(0, 51)]; + while (have < len) { + result += aStringChars[(int) nextLong(0, 61)]; + have++; + } + + return result; } /* @@ -169,19 +156,17 @@ public String getAString(long x, long y) * * Produce a random numeric string of length [x .. y]. */ - public String getNString(long x, long y) - { - String result = new String(); - long len = nextLong(x, y); - long have = 0; - - while (have < len) - { - result += (char)(nextLong((long)'0', (long)'9')); - have++; - } - - return result; + public String getNString(long x, long y) { + String result = new String(); + long len = nextLong(x, y); + long have = 0; + + while (have < len) { + result += (char) (nextLong((long) '0', (long) '9')); + have++; + } + + return result; } /* @@ -189,10 +174,9 @@ public String getNString(long x, long y) * * Produce a non uniform random Item ID. */ - public int getItemID() - { - return (int)((((nextLong(0, 8191) | nextLong(1, 100000)) + nURandCI_ID) - % 100000) + 1); + public int getItemID() { + return (int) ((((nextLong(0, 8191) | nextLong(1, 100000)) + nURandCI_ID) + % 100000) + 1); } /* @@ -200,10 +184,9 @@ public int getItemID() * * Produce a non uniform random Customer ID. */ - public int getCustomerID() - { - return (int)((((nextLong(0, 1023) | nextLong(1, 3000)) + nURandCC_ID) - % 3000) + 1); + public int getCustomerID() { + return (int) ((((nextLong(0, 1023) | nextLong(1, 3000)) + nURandCC_ID) + % 3000) + 1); } /* @@ -211,17 +194,15 @@ public int getCustomerID() * * Produce the syllable representation for C_LAST of [0 .. 999] */ - public String getCLast(int num) - { - String result = new String(); + public String getCLast(int num) { + String result = new String(); - for (int i = 0; i < 3; i++) - { - result = cLastTokens[num % 10] + result; - num /= 10; - } + for (int i = 0; i < 3; i++) { + result = cLastTokens[num % 10] + result; + num /= 10; + } - return result; + return result; } /* @@ -229,38 +210,33 @@ public String getCLast(int num) * * Procude a non uniform random Customer Last Name. */ - public String getCLast() - { - long num; - num = (((nextLong(0, 255) | nextLong(0, 999)) + nURandCLast) % 1000); - return getCLast((int)num); + public String getCLast() { + long num; + num = (((nextLong(0, 255) | nextLong(0, 999)) + nURandCLast) % 1000); + return getCLast((int) num); } - public String getState() - { - String result = new String(); + public String getState() { + String result = new String(); - result += (char)nextInt((int)'A', (int)'Z'); - result += (char)nextInt((int)'A', (int)'Z'); + result += (char) nextInt((int) 'A', (int) 'Z'); + result += (char) nextInt((int) 'A', (int) 'Z'); - return result; + return result; } /* * Methods to retrieve the C values used. */ - public long getNURandCLast() - { - return nURandCLast; + public long getNURandCLast() { + return nURandCLast; } - public long getNURandCC_ID() - { - return nURandCC_ID; + public long getNURandCC_ID() { + return nURandCC_ID; } - public long getNURandCI_ID() - { - return nURandCI_ID; + public long getNURandCI_ID() { + return nURandCI_ID; } } // end jTPCCRandom diff --git a/src/client/jTPCCTData.java b/src/client/jTPCCTData.java index 74872cb..a5497cf 100644 --- a/src/client/jTPCCTData.java +++ b/src/client/jTPCCTData.java @@ -6,233 +6,224 @@ * */ -import org.apache.log4j.*; +import org.apache.log4j.Logger; -import java.util.*; -import java.sql.*; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.Formatter; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Vector; -public class jTPCCTData -{ - protected int numWarehouses = 0; +public class jTPCCTData { + protected int numWarehouses = 0; public final static int - TT_NEW_ORDER = 0, - TT_PAYMENT = 1, - TT_ORDER_STATUS = 2, - TT_STOCK_LEVEL = 3, - TT_DELIVERY = 4, - TT_DELIVERY_BG = 5, - TT_NONE = 6, - TT_DONE = 7; + TT_NEW_ORDER = 0, + TT_PAYMENT = 1, + TT_ORDER_STATUS = 2, + TT_STOCK_LEVEL = 3, + TT_DELIVERY = 4, + TT_DELIVERY_BG = 5, + TT_NONE = 6, + TT_DONE = 7; public final static String transTypeNames[] = { - "NEW_ORDER", "PAYMENT", "ORDER_STATUS", "STOCK_LEVEL", - "DELIVERY", "DELIVERY_BG", "NONE", "DONE"}; - - public int sched_code; - public long sched_fuzz; - public jTPCCTData term_left; - public jTPCCTData term_right; - public int tree_height; - - private int transType; - private long transDue; - private long transStart; - private long transEnd; - private boolean transRbk; - private String transError; - - private int terminalWarehouse = 0; - private int terminalDistrict = 0; - - private NewOrderData newOrder = null; - private PaymentData payment = null; - private OrderStatusData orderStatus = null; - private StockLevelData stockLevel = null; - private DeliveryData delivery = null; - private DeliveryBGData deliveryBG = null; - - private static Object traceLock = new Object(); - - private StringBuffer resultSB = new StringBuffer(); - private Formatter resultFmt = new Formatter(resultSB); - - public void setNumWarehouses(int num) - { - numWarehouses = num; + "NEW_ORDER", "PAYMENT", "ORDER_STATUS", "STOCK_LEVEL", + "DELIVERY", "DELIVERY_BG", "NONE", "DONE"}; + + public int sched_code; + public long sched_fuzz; + public jTPCCTData term_left; + public jTPCCTData term_right; + public int tree_height; + + private int transType; + private long transDue; + private long transStart; + private long transEnd; + private boolean transRbk; + private String transError; + + private int terminalWarehouse = 0; + private int terminalDistrict = 0; + + private NewOrderData newOrder = null; + private PaymentData payment = null; + private OrderStatusData orderStatus = null; + private StockLevelData stockLevel = null; + private DeliveryData delivery = null; + private DeliveryBGData deliveryBG = null; + + private static Object traceLock = new Object(); + + private StringBuffer resultSB = new StringBuffer(); + private Formatter resultFmt = new Formatter(resultSB); + + public void setNumWarehouses(int num) { + numWarehouses = num; } - public void setWarehouse(int warehouse) - { - terminalWarehouse = warehouse; + public void setWarehouse(int warehouse) { + terminalWarehouse = warehouse; } - public int getWarehouse() - { - return terminalWarehouse; + public int getWarehouse() { + return terminalWarehouse; } - public void setDistrict(int district) - { - terminalDistrict = district; + public void setDistrict(int district) { + terminalDistrict = district; } - public int getDistrict() - { - return terminalDistrict; + public int getDistrict() { + return terminalDistrict; } public void execute(Logger log, jTPCCConnection db) - throws Exception - { - transStart = System.currentTimeMillis(); - if (transDue == 0) - transDue = transStart; - - switch (transType) - { - case TT_NEW_ORDER: - executeNewOrder(log, db); - break; - - case TT_PAYMENT: - executePayment(log, db); - break; - - case TT_ORDER_STATUS: - executeOrderStatus(log, db); - break; - - case TT_STOCK_LEVEL: - executeStockLevel(log, db); - break; - - case TT_DELIVERY: - executeDelivery(log, db); - break; - - case TT_DELIVERY_BG: - executeDeliveryBG(log, db); - break; - - default: - throw new Exception("Unknown transType " + transType); - } - - transEnd = System.currentTimeMillis(); + throws Exception { + transStart = System.currentTimeMillis(); + if (transDue == 0) + transDue = transStart; + + switch (transType) { + case TT_NEW_ORDER: + executeNewOrder(log, db); + break; + + case TT_PAYMENT: + executePayment(log, db); + break; + + case TT_ORDER_STATUS: + executeOrderStatus(log, db); + break; + + case TT_STOCK_LEVEL: + executeStockLevel(log, db); + break; + + case TT_DELIVERY: + executeDelivery(log, db); + break; + + case TT_DELIVERY_BG: + executeDeliveryBG(log, db); + break; + + default: + throw new Exception("Unknown transType " + transType); + } + + transEnd = System.currentTimeMillis(); } public void traceScreen(Logger log) - throws Exception - { - StringBuffer sb = new StringBuffer(); - Formatter fmt = new Formatter(sb); - - StringBuffer screenSb[] = new StringBuffer[23]; - Formatter screenFmt[] = new Formatter[23]; - for (int i = 0; i < 23; i++) - { - screenSb[i] = new StringBuffer(); - screenFmt[i] = new Formatter(screenSb[i]); - } - - if (!log.isTraceEnabled()) - return; - - if (transType < TT_NEW_ORDER || transType > TT_DONE) - throw new Exception("Unknown transType " + transType); - - synchronized(traceLock) - { - fmt.format("==== %s %s ==== Terminal %d,%d =================================================", - transTypeNames[transType], - (transEnd == 0) ? "INPUT" : "OUTPUT", - terminalWarehouse, terminalDistrict); - sb.setLength(79); - log.trace(sb.toString()); - sb.setLength(0); - - fmt.format("---- Due: %s", (transDue == 0) ? "N/A" : - new java.sql.Timestamp(transDue).toString()); - log.trace(sb.toString()); - sb.setLength(0); - - fmt.format("---- Start: %s", (transStart == 0) ? "N/A" : - new java.sql.Timestamp(transStart).toString()); - log.trace(sb.toString()); - sb.setLength(0); - - fmt.format("---- End: %s", (transEnd == 0) ? "N/A" : - new java.sql.Timestamp(transEnd).toString()); - log.trace(sb.toString()); - sb.setLength(0); - - if (transError != null) - { - fmt.format("#### ERROR: %s", transError); - log.trace(sb.toString()); - sb.setLength(0); - } - - log.trace("-------------------------------------------------------------------------------"); - - switch (transType) - { - case TT_NEW_ORDER: - traceNewOrder(log, screenFmt); - break; - - case TT_PAYMENT: - tracePayment(log, screenFmt); - break; - - case TT_ORDER_STATUS: - traceOrderStatus(log, screenFmt); - break; - - case TT_STOCK_LEVEL: - traceStockLevel(log, screenFmt); - break; - - case TT_DELIVERY: - traceDelivery(log, screenFmt); - break; - - case TT_DELIVERY_BG: - traceDeliveryBG(log, screenFmt); - break; - - default: - throw new Exception("Unknown transType " + transType); - } - - for (int i = 0; i < 23; i++) - { - if (screenSb[i].length() > 79) - screenSb[i].setLength(79); - log.trace(screenSb[i].toString()); - } - - log.trace("-------------------------------------------------------------------------------"); - log.trace(""); - } + throws Exception { + StringBuffer sb = new StringBuffer(); + Formatter fmt = new Formatter(sb); + + StringBuffer screenSb[] = new StringBuffer[23]; + Formatter screenFmt[] = new Formatter[23]; + for (int i = 0; i < 23; i++) { + screenSb[i] = new StringBuffer(); + screenFmt[i] = new Formatter(screenSb[i]); + } + + if (!log.isTraceEnabled()) + return; + + if (transType < TT_NEW_ORDER || transType > TT_DONE) + throw new Exception("Unknown transType " + transType); + + synchronized (traceLock) { + fmt.format("==== %s %s ==== Terminal %d,%d =================================================", + transTypeNames[transType], + (transEnd == 0) ? "INPUT" : "OUTPUT", + terminalWarehouse, terminalDistrict); + sb.setLength(79); + log.trace(sb.toString()); + sb.setLength(0); + + fmt.format("---- Due: %s", (transDue == 0) ? "N/A" : + new java.sql.Timestamp(transDue).toString()); + log.trace(sb.toString()); + sb.setLength(0); + + fmt.format("---- Start: %s", (transStart == 0) ? "N/A" : + new java.sql.Timestamp(transStart).toString()); + log.trace(sb.toString()); + sb.setLength(0); + + fmt.format("---- End: %s", (transEnd == 0) ? "N/A" : + new java.sql.Timestamp(transEnd).toString()); + log.trace(sb.toString()); + sb.setLength(0); + + if (transError != null) { + fmt.format("#### ERROR: %s", transError); + log.trace(sb.toString()); + sb.setLength(0); + } + + log.trace("-------------------------------------------------------------------------------"); + + switch (transType) { + case TT_NEW_ORDER: + traceNewOrder(log, screenFmt); + break; + + case TT_PAYMENT: + tracePayment(log, screenFmt); + break; + + case TT_ORDER_STATUS: + traceOrderStatus(log, screenFmt); + break; + + case TT_STOCK_LEVEL: + traceStockLevel(log, screenFmt); + break; + + case TT_DELIVERY: + traceDelivery(log, screenFmt); + break; + + case TT_DELIVERY_BG: + traceDeliveryBG(log, screenFmt); + break; + + default: + throw new Exception("Unknown transType " + transType); + } + + for (int i = 0; i < 23; i++) { + if (screenSb[i].length() > 79) + screenSb[i].setLength(79); + log.trace(screenSb[i].toString()); + } + + log.trace("-------------------------------------------------------------------------------"); + log.trace(""); + } } - public String resultLine(long sessionStart) - { - String line; - - resultFmt.format("%d,%d,%d,%s,%d,%d,%d\n", - transEnd - sessionStart, - transEnd - transDue, - transEnd - transStart, - transTypeNames[transType], - (transRbk) ? 1 : 0, - (transType == TT_DELIVERY_BG) ? getSkippedDeliveries() : 0, - (transError == null) ? 0 : 1); - line = resultSB.toString(); - resultSB.setLength(0); - return line; + public String resultLine(long sessionStart) { + String line; + + resultFmt.format("%d,%d,%d,%s,%d,%d,%d\n", + transEnd - sessionStart, + transEnd - transDue, + transEnd - transStart, + transTypeNames[transType], + (transRbk) ? 1 : 0, + (transType == TT_DELIVERY_BG) ? getSkippedDeliveries() : 0, + (transError == null) ? 0 : 1); + line = resultSB.toString(); + resultSB.setLength(0); + return line; } /* ********************************************************************** @@ -240,466 +231,433 @@ public String resultLine(long sessionStart) * ***** NEW_ORDER related methods and subclass. ************************ * ********************************************************************** * *********************************************************************/ - public void generateNewOrder(Logger log, jTPCCRandom rnd, long due) - { - int o_ol_cnt; - int i = 0; - - transType = TT_NEW_ORDER; - transDue = due; - transStart = 0; - transEnd = 0; - transRbk = false; - transError = null; - - newOrder = new NewOrderData(); - payment = null; - orderStatus = null; - stockLevel = null; - delivery = null; - deliveryBG = null; - - newOrder.w_id = terminalWarehouse; // 2.4.1.1 - newOrder.d_id = rnd.nextInt(1, 10); // 2.4.1.2 - newOrder.c_id = rnd.getCustomerID(); - o_ol_cnt = rnd.nextInt(5, 15); // 2.4.1.3 - - while (i < o_ol_cnt) // 2.4.1.5 - { - newOrder.ol_i_id[i] = rnd.getItemID(); - if (rnd.nextInt(1, 100) <= 99) - newOrder.ol_supply_w_id[i] = terminalWarehouse; - else - newOrder.ol_supply_w_id[i] = rnd.nextInt(1, numWarehouses); - newOrder.ol_quantity[i] = rnd.nextInt(1, 10); - newOrder.found[i] = false; - i++; - } - - if (rnd.nextInt(1, 100) == 1) // 2.4.1.4 - { - newOrder.ol_i_id[i - 1] += (rnd.nextInt(1, 9) * 1000000); - transRbk = true; - } - - // Zero out remainint lines - while (i < 15) - { - newOrder.ol_i_id[i] = 0; - newOrder.ol_supply_w_id[i] = 0; - newOrder.ol_quantity[i] = 0; - i++; - } + public void generateNewOrder(Logger log, jTPCCRandom rnd, long due) { + int o_ol_cnt; + int i = 0; + + transType = TT_NEW_ORDER; + transDue = due; + transStart = 0; + transEnd = 0; + transRbk = false; + transError = null; + + newOrder = new NewOrderData(); + payment = null; + orderStatus = null; + stockLevel = null; + delivery = null; + deliveryBG = null; + + newOrder.w_id = terminalWarehouse; // 2.4.1.1 + newOrder.d_id = rnd.nextInt(1, 10); // 2.4.1.2 + newOrder.c_id = rnd.getCustomerID(); + o_ol_cnt = rnd.nextInt(5, 15); // 2.4.1.3 + + while (i < o_ol_cnt) // 2.4.1.5 + { + newOrder.ol_i_id[i] = rnd.getItemID(); + if (rnd.nextInt(1, 100) <= 99) + newOrder.ol_supply_w_id[i] = terminalWarehouse; + else + newOrder.ol_supply_w_id[i] = rnd.nextInt(1, numWarehouses); + newOrder.ol_quantity[i] = rnd.nextInt(1, 10); + newOrder.found[i] = false; + i++; + } + + if (rnd.nextInt(1, 100) == 1) // 2.4.1.4 + { + newOrder.ol_i_id[i - 1] += (rnd.nextInt(1, 9) * 1000000); + transRbk = true; + } + + // Zero out remainint lines + while (i < 15) { + newOrder.ol_i_id[i] = 0; + newOrder.ol_supply_w_id[i] = 0; + newOrder.ol_quantity[i] = 0; + i++; + } } private void executeNewOrder(Logger log, jTPCCConnection db) - throws Exception - { - PreparedStatement stmt; - PreparedStatement insertOrderLineBatch; - ResultSet rs; - - int o_id; - int o_all_local = 1; - long o_entry_d; - int ol_cnt; - double total_amount = 0.0; - - int ol_seq[] = new int[15]; - - // The o_entry_d is now. - o_entry_d = System.currentTimeMillis(); - newOrder.o_entry_d = new java.sql.Timestamp(o_entry_d).toString(); - - /* - * When processing the order lines we must select the STOCK rows - * FOR UPDATE. This is because we must perform business logic - * (the juggling with the S_QUANTITY) here in the application - * and cannot do that in an atomic UPDATE statement while getting - * the original value back at the same time (UPDATE ... RETURNING - * may not be vendor neutral). This can lead to possible deadlocks - * if two transactions try to lock the same two stock rows in - * opposite order. To avoid that we process the order lines in - * the order of the order of ol_supply_w_id, ol_i_id. - */ - for (ol_cnt = 0; ol_cnt < 15 && newOrder.ol_i_id[ol_cnt] != 0; ol_cnt++) - { - ol_seq[ol_cnt] = ol_cnt; - - // While looping we also determine o_all_local. - if (newOrder.ol_supply_w_id[ol_cnt] != newOrder.w_id) - o_all_local = 0; - } - - for (int x = 0; x < ol_cnt - 1; x++) - { - for (int y = x + 1; y < ol_cnt; y++) - { - if (newOrder.ol_supply_w_id[ol_seq[y]] < newOrder.ol_supply_w_id[ol_seq[x]]) - { - int tmp = ol_seq[x]; - ol_seq[x] = ol_seq[y]; - ol_seq[y] = tmp; - } - else if (newOrder.ol_supply_w_id[ol_seq[y]] == newOrder.ol_supply_w_id[ol_seq[x]] && - newOrder.ol_i_id[ol_seq[y]] < newOrder.ol_i_id[ol_seq[x]]) - { - int tmp = ol_seq[x]; - ol_seq[x] = ol_seq[y]; - ol_seq[y] = tmp; - } - } - } - - // The above also provided the output value for o_ol_cnt; - newOrder.o_ol_cnt = ol_cnt; - - try { - // Retrieve the required data from DISTRICT - stmt = db.stmtNewOrderSelectDist; - stmt.setInt(1, newOrder.w_id); - stmt.setInt(2, newOrder.d_id); - rs = stmt.executeQuery(); - if (!rs.next()) - { - rs.close(); - throw new SQLException("District for" + - " W_ID=" + newOrder.w_id + - " D_ID=" + newOrder.d_id + " not found"); - } - newOrder.d_tax = rs.getDouble("d_tax"); - newOrder.o_id = rs.getInt("d_next_o_id"); - o_id = newOrder.o_id; - rs.close(); - - // Retrieve the required data from CUSTOMER and WAREHOUSE - stmt = db.stmtNewOrderSelectWhseCust; - stmt.setInt(1, newOrder.w_id); - stmt.setInt(2, newOrder.d_id); - stmt.setInt(3, newOrder.c_id); - rs = stmt.executeQuery(); - if (!rs.next()) - { - rs.close(); - throw new SQLException("Warehouse or Customer for" + - " W_ID=" + newOrder.w_id + - " D_ID=" + newOrder.d_id + - " C_ID=" + newOrder.c_id + " not found"); - } - newOrder.w_tax = rs.getDouble("w_tax"); - newOrder.c_last = rs.getString("c_last"); - newOrder.c_credit = rs.getString("c_credit"); - newOrder.c_discount = rs.getDouble("c_discount"); - rs.close(); - - // Update the DISTRICT bumping the D_NEXT_O_ID - stmt = db.stmtNewOrderUpdateDist; - stmt.setInt(1, newOrder.w_id); - stmt.setInt(2, newOrder.d_id); - stmt.executeUpdate(); - - // Insert the ORDER row - stmt = db.stmtNewOrderInsertOrder; - stmt.setInt(1, o_id); - stmt.setInt(2, newOrder.d_id); - stmt.setInt(3, newOrder.w_id); - stmt.setInt(4, newOrder.c_id); - stmt.setTimestamp(5, new java.sql.Timestamp(System.currentTimeMillis())); - stmt.setInt(6, ol_cnt); - stmt.setInt(7, o_all_local); - stmt.executeUpdate(); - - // Insert the NEW_ORDER row - stmt = db.stmtNewOrderInsertNewOrder; - stmt.setInt(1, o_id); - stmt.setInt(2, newOrder.d_id); - stmt.setInt(3, newOrder.w_id); - stmt.executeUpdate(); - - // Per ORDER_LINE - insertOrderLineBatch = db.stmtNewOrderInsertOrderLine; - int seq0 = ol_seq[0]; - boolean distinct_item = true; - HashSet itemIds = new HashSet(); - for (int i = 0; i < ol_cnt; i++) - { - int seq = ol_seq[i]; - itemIds.add(Integer.valueOf(newOrder.ol_i_id[seq])); - } - String distName = "s_dist_0" + String.valueOf(newOrder.d_id); - if (newOrder.d_id > 9) { - distName = "s_dist_" + String.valueOf(newOrder.d_id); - } - stmt = db.stmtNewOrderSelectItemBatch[itemIds.size()]; - HashMap itemMap = new HashMap(); - int i_idx = 0; - for (Integer x : itemIds) { - i_idx ++; - stmt.setInt(i_idx, x.intValue()); - } - rs = stmt.executeQuery(); - while (rs.next()) - { - int i_id = rs.getInt("i_id"); - NewOrderItem item = new NewOrderItem(); - item.i_id = i_id; - item.i_price = rs.getDouble("i_price"); - item.i_name = rs.getString("i_name"); - item.i_data = rs.getString("i_data"); - itemMap.put(i_id, item); - } - rs.close(); - for (int i = 0; i < ol_cnt; i++) - { - int seq = ol_seq[i]; - int i_id = newOrder.ol_i_id[seq]; - NewOrderItem item = itemMap.get(i_id); - - if (item == null) { - if (transRbk && (i_id < 1 || - i_id > 100000)) - { - /* - * Clause 2.4.2.3 mandates that the entire - * transaction profile up to here must be executed - * before we can roll back, except for retrieving - * the missing STOCK row and inserting this - * ORDER_LINE row. Note that we haven't updated - * STOCK rows or inserted any ORDER_LINE rows so - * far, we only batched them up. So we must do - * that now in order to satisfy 2.4.2.3. - */ - db.rollback(); - return; - } - // This ITEM should have been there. - throw new Exception("ITEM " + newOrder.ol_i_id[seq] + - " not fount"); - } - } - - stmt = db.stmtNewOrderSelectStockBatch[ol_cnt]; - for (int i = 0; i < ol_cnt; ++i) { - int seq = ol_seq[i]; - stmt.setInt(i * 2 + 1, newOrder.ol_supply_w_id[seq]); - stmt.setInt(i * 2 + 2, newOrder.ol_i_id[seq]); - } - rs = stmt.executeQuery(); - while (rs.next()) { - int i_id = rs.getInt("s_i_id"); - int w_id = rs.getInt("s_w_id"); - NewOrderItem item = itemMap.get(i_id); - - // There may be two item having the same supply warehouse. - for (int i = 0; i < ol_cnt; i ++) { - int seq = ol_seq[i]; - if (newOrder.ol_i_id[seq] == i_id && newOrder.ol_supply_w_id[seq] == w_id) { - newOrder.s_quantity[seq] = rs.getInt("s_quantity"); - newOrder.dist_value[seq] = rs.getString(distName); - newOrder.found[seq] = true; - if (item != null) { - newOrder.ol_amount[seq] = item.i_price * newOrder.ol_quantity[seq]; - if (item.i_data.contains("ORIGINAL") && - rs.getString("s_data").contains("ORIGINAL")) - newOrder.brand_generic[seq] = new String("B"); - else - newOrder.brand_generic[seq] = new String("G"); - } - } - } - } - rs.close(); - - - for (int i = 0; i < ol_cnt; i++) - { - int ol_number = i + 1; - int seq = ol_seq[i]; - if (!newOrder.found[seq]) - { - throw new Exception("STOCK with" + - " S_W_ID=" + newOrder.ol_supply_w_id[seq] + - " S_I_ID=" + newOrder.ol_i_id[seq] + - " not fount"); - } - - - total_amount += newOrder.ol_amount[seq] * - (1.0 - newOrder.c_discount) * - (1.0 + newOrder.w_tax + newOrder.d_tax); - stmt = db.stmtNewOrderUpdateStock; - // Update the STOCK row. - if (newOrder.s_quantity[seq] >= newOrder.ol_quantity[seq] + 10) - stmt.setInt(1, newOrder.s_quantity[seq] - - newOrder.ol_quantity[seq]); - else - stmt.setInt(1, newOrder.s_quantity[seq] + 91); - stmt.setInt(2, newOrder.ol_quantity[seq]); - if (newOrder.ol_supply_w_id[seq] == newOrder.w_id) - stmt.setInt(3, 0); - else - stmt.setInt(3, 1); - stmt.setInt(4, newOrder.ol_supply_w_id[seq]); - stmt.setInt(5, newOrder.ol_i_id[seq]); - stmt.executeUpdate(); - - // Insert the ORDER_LINE row. - insertOrderLineBatch.setInt(1, o_id); - insertOrderLineBatch.setInt(2, newOrder.d_id); - insertOrderLineBatch.setInt(3, newOrder.w_id); - insertOrderLineBatch.setInt(4, ol_number); - insertOrderLineBatch.setInt(5, newOrder.ol_i_id[seq]); - insertOrderLineBatch.setInt(6, newOrder.ol_supply_w_id[seq]); - insertOrderLineBatch.setInt(7, newOrder.ol_quantity[seq]); - insertOrderLineBatch.setDouble(8, newOrder.ol_amount[seq]); - insertOrderLineBatch.setString(9, newOrder.dist_value[seq]); - insertOrderLineBatch.addBatch(); - } - - // All done ... execute the batches. - insertOrderLineBatch.executeBatch(); - insertOrderLineBatch.clearBatch(); - - newOrder.execution_status = new String("Order placed"); - newOrder.total_amount = total_amount; - - db.commit(); - - } - catch (SQLException se) - { - log.error("Unexpected SQLException in NEW_ORDER"); - for (SQLException x = se; x != null; x = x.getNextException()) - log.error(x.getMessage()); - se.printStackTrace(); - - try - { - db.stmtNewOrderInsertOrderLine.clearBatch(); - db.rollback(); - } - catch (SQLException se2) - { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); - } - } - catch (Exception e) - { - try - { - db.stmtNewOrderInsertOrderLine.clearBatch(); - db.rollback(); - } - catch (SQLException se2) - { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); - } - throw e; - } + throws Exception { + PreparedStatement stmt; + PreparedStatement insertOrderLineBatch; + ResultSet rs; + + int o_id; + int o_all_local = 1; + long o_entry_d; + int ol_cnt; + double total_amount = 0.0; + + int ol_seq[] = new int[15]; + + // The o_entry_d is now. + o_entry_d = System.currentTimeMillis(); + newOrder.o_entry_d = new java.sql.Timestamp(o_entry_d).toString(); + + /* + * When processing the order lines we must select the STOCK rows + * FOR UPDATE. This is because we must perform business logic + * (the juggling with the S_QUANTITY) here in the application + * and cannot do that in an atomic UPDATE statement while getting + * the original value back at the same time (UPDATE ... RETURNING + * may not be vendor neutral). This can lead to possible deadlocks + * if two transactions try to lock the same two stock rows in + * opposite order. To avoid that we process the order lines in + * the order of the order of ol_supply_w_id, ol_i_id. + */ + for (ol_cnt = 0; ol_cnt < 15 && newOrder.ol_i_id[ol_cnt] != 0; ol_cnt++) { + ol_seq[ol_cnt] = ol_cnt; + + // While looping we also determine o_all_local. + if (newOrder.ol_supply_w_id[ol_cnt] != newOrder.w_id) + o_all_local = 0; + } + + for (int x = 0; x < ol_cnt - 1; x++) { + for (int y = x + 1; y < ol_cnt; y++) { + if (newOrder.ol_supply_w_id[ol_seq[y]] < newOrder.ol_supply_w_id[ol_seq[x]]) { + int tmp = ol_seq[x]; + ol_seq[x] = ol_seq[y]; + ol_seq[y] = tmp; + } else if (newOrder.ol_supply_w_id[ol_seq[y]] == newOrder.ol_supply_w_id[ol_seq[x]] && + newOrder.ol_i_id[ol_seq[y]] < newOrder.ol_i_id[ol_seq[x]]) { + int tmp = ol_seq[x]; + ol_seq[x] = ol_seq[y]; + ol_seq[y] = tmp; + } + } + } + + // The above also provided the output value for o_ol_cnt; + newOrder.o_ol_cnt = ol_cnt; + + try { + // Retrieve the required data from DISTRICT + stmt = db.stmtNewOrderSelectDist; + stmt.setInt(1, newOrder.w_id); + stmt.setInt(2, newOrder.d_id); + rs = stmt.executeQuery(); + if (!rs.next()) { + rs.close(); + throw new SQLException("District for" + + " W_ID=" + newOrder.w_id + + " D_ID=" + newOrder.d_id + " not found"); + } + newOrder.d_tax = rs.getDouble("d_tax"); + newOrder.o_id = rs.getInt("d_next_o_id"); + o_id = newOrder.o_id; + rs.close(); + + // Retrieve the required data from CUSTOMER and WAREHOUSE + stmt = db.stmtNewOrderSelectWhseCust; + stmt.setInt(1, newOrder.w_id); + stmt.setInt(2, newOrder.d_id); + stmt.setInt(3, newOrder.c_id); + rs = stmt.executeQuery(); + if (!rs.next()) { + rs.close(); + throw new SQLException("Warehouse or Customer for" + + " W_ID=" + newOrder.w_id + + " D_ID=" + newOrder.d_id + + " C_ID=" + newOrder.c_id + " not found"); + } + newOrder.w_tax = rs.getDouble("w_tax"); + newOrder.c_last = rs.getString("c_last"); + newOrder.c_credit = rs.getString("c_credit"); + newOrder.c_discount = rs.getDouble("c_discount"); + rs.close(); + + // Update the DISTRICT bumping the D_NEXT_O_ID + stmt = db.stmtNewOrderUpdateDist; + stmt.setInt(1, newOrder.w_id); + stmt.setInt(2, newOrder.d_id); + stmt.executeUpdate(); + + // Insert the ORDER row + stmt = db.stmtNewOrderInsertOrder; + stmt.setInt(1, o_id); + stmt.setInt(2, newOrder.d_id); + stmt.setInt(3, newOrder.w_id); + stmt.setInt(4, newOrder.c_id); + stmt.setTimestamp(5, new java.sql.Timestamp(System.currentTimeMillis())); + stmt.setInt(6, ol_cnt); + stmt.setInt(7, o_all_local); + stmt.executeUpdate(); + + // Insert the NEW_ORDER row + stmt = db.stmtNewOrderInsertNewOrder; + stmt.setInt(1, o_id); + stmt.setInt(2, newOrder.d_id); + stmt.setInt(3, newOrder.w_id); + stmt.executeUpdate(); + + // Per ORDER_LINE + insertOrderLineBatch = db.stmtNewOrderInsertOrderLine; + int seq0 = ol_seq[0]; + boolean distinct_item = true; + HashSet itemIds = new HashSet(); + for (int i = 0; i < ol_cnt; i++) { + int seq = ol_seq[i]; + itemIds.add(Integer.valueOf(newOrder.ol_i_id[seq])); + } + String distName = "s_dist_0" + String.valueOf(newOrder.d_id); + if (newOrder.d_id > 9) { + distName = "s_dist_" + String.valueOf(newOrder.d_id); + } + stmt = db.stmtNewOrderSelectItemBatch[itemIds.size()]; + HashMap itemMap = new HashMap(); + int i_idx = 0; + for (Integer x : itemIds) { + i_idx++; + stmt.setInt(i_idx, x.intValue()); + } + rs = stmt.executeQuery(); + while (rs.next()) { + int i_id = rs.getInt("i_id"); + NewOrderItem item = new NewOrderItem(); + item.i_id = i_id; + item.i_price = rs.getDouble("i_price"); + item.i_name = rs.getString("i_name"); + item.i_data = rs.getString("i_data"); + itemMap.put(i_id, item); + } + rs.close(); + for (int i = 0; i < ol_cnt; i++) { + int seq = ol_seq[i]; + int i_id = newOrder.ol_i_id[seq]; + NewOrderItem item = itemMap.get(i_id); + + if (item == null) { + if (transRbk && (i_id < 1 || + i_id > 100000)) { + /* + * Clause 2.4.2.3 mandates that the entire + * transaction profile up to here must be executed + * before we can roll back, except for retrieving + * the missing STOCK row and inserting this + * ORDER_LINE row. Note that we haven't updated + * STOCK rows or inserted any ORDER_LINE rows so + * far, we only batched them up. So we must do + * that now in order to satisfy 2.4.2.3. + */ + db.rollback(); + return; + } + // This ITEM should have been there. + throw new Exception("ITEM " + newOrder.ol_i_id[seq] + + " not fount"); + } + } + + stmt = db.stmtNewOrderSelectStockBatch[ol_cnt]; + for (int i = 0; i < ol_cnt; ++i) { + int seq = ol_seq[i]; + stmt.setInt(i * 2 + 1, newOrder.ol_supply_w_id[seq]); + stmt.setInt(i * 2 + 2, newOrder.ol_i_id[seq]); + } + rs = stmt.executeQuery(); + while (rs.next()) { + int i_id = rs.getInt("s_i_id"); + int w_id = rs.getInt("s_w_id"); + NewOrderItem item = itemMap.get(i_id); + + // There may be two item having the same supply warehouse. + for (int i = 0; i < ol_cnt; i++) { + int seq = ol_seq[i]; + if (newOrder.ol_i_id[seq] == i_id && newOrder.ol_supply_w_id[seq] == w_id) { + newOrder.s_quantity[seq] = rs.getInt("s_quantity"); + newOrder.dist_value[seq] = rs.getString(distName); + newOrder.found[seq] = true; + if (item != null) { + newOrder.ol_amount[seq] = item.i_price * newOrder.ol_quantity[seq]; + if (item.i_data.contains("ORIGINAL") && + rs.getString("s_data").contains("ORIGINAL")) + newOrder.brand_generic[seq] = new String("B"); + else + newOrder.brand_generic[seq] = new String("G"); + } + } + } + } + rs.close(); + + + for (int i = 0; i < ol_cnt; i++) { + int ol_number = i + 1; + int seq = ol_seq[i]; + if (!newOrder.found[seq]) { + throw new Exception("STOCK with" + + " S_W_ID=" + newOrder.ol_supply_w_id[seq] + + " S_I_ID=" + newOrder.ol_i_id[seq] + + " not fount"); + } + + + total_amount += newOrder.ol_amount[seq] * + (1.0 - newOrder.c_discount) * + (1.0 + newOrder.w_tax + newOrder.d_tax); + stmt = db.stmtNewOrderUpdateStock; + // Update the STOCK row. + if (newOrder.s_quantity[seq] >= newOrder.ol_quantity[seq] + 10) + stmt.setInt(1, newOrder.s_quantity[seq] - + newOrder.ol_quantity[seq]); + else + stmt.setInt(1, newOrder.s_quantity[seq] + 91); + stmt.setInt(2, newOrder.ol_quantity[seq]); + if (newOrder.ol_supply_w_id[seq] == newOrder.w_id) + stmt.setInt(3, 0); + else + stmt.setInt(3, 1); + stmt.setInt(4, newOrder.ol_supply_w_id[seq]); + stmt.setInt(5, newOrder.ol_i_id[seq]); + stmt.executeUpdate(); + + // Insert the ORDER_LINE row. + insertOrderLineBatch.setInt(1, o_id); + insertOrderLineBatch.setInt(2, newOrder.d_id); + insertOrderLineBatch.setInt(3, newOrder.w_id); + insertOrderLineBatch.setInt(4, ol_number); + insertOrderLineBatch.setInt(5, newOrder.ol_i_id[seq]); + insertOrderLineBatch.setInt(6, newOrder.ol_supply_w_id[seq]); + insertOrderLineBatch.setInt(7, newOrder.ol_quantity[seq]); + insertOrderLineBatch.setDouble(8, newOrder.ol_amount[seq]); + insertOrderLineBatch.setString(9, newOrder.dist_value[seq]); + insertOrderLineBatch.addBatch(); + } + + // All done ... execute the batches. + insertOrderLineBatch.executeBatch(); + insertOrderLineBatch.clearBatch(); + + newOrder.execution_status = new String("Order placed"); + newOrder.total_amount = total_amount; + + db.commit(); + + } catch (SQLException se) { + log.error("Unexpected SQLException in NEW_ORDER"); + for (SQLException x = se; x != null; x = x.getNextException()) + log.error(x.getMessage()); + se.printStackTrace(); + + try { + db.stmtNewOrderInsertOrderLine.clearBatch(); + db.rollback(); + } catch (SQLException se2) { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } + } catch (Exception e) { + try { + db.stmtNewOrderInsertOrderLine.clearBatch(); + db.rollback(); + } catch (SQLException se2) { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } + throw e; + } /* log.info("Reached the point of creating one NEW_ORDER W_ID "+newOrder.w_id+" D_ID "+newOrder.d_id+" C_ID "+newOrder.c_id); System.exit(0); */ } - private void traceNewOrder(Logger log, Formatter fmt[]) - { - fmt[0].format(" New Order"); - - if (transEnd == 0) - { - // NEW_ORDER INPUT screen - fmt[1].format("Warehouse: %6d District: %2d Date:", - newOrder.w_id, newOrder.d_id); - fmt[2].format("Customer: %4d Name: Credit: %%Disc:", - newOrder.c_id); - fmt[3].format("Order Number: Number of Lines: W_tax: D_tax:"); - - fmt[5].format("Supp_W Item_Id Item Name Qty Stock B/G Price Amount"); - - for (int i = 0; i < 15; i++) - { - if (newOrder.ol_i_id[i] != 0) - fmt[6 + i].format("%6d %6d %2d", - newOrder.ol_supply_w_id[i], - newOrder.ol_i_id[i], newOrder.ol_quantity[i]); - else - fmt[6 + i].format("______ ______ __"); - } - - fmt[21].format("Execution Status: Total: $"); - } - else - { - // NEW_ORDER OUTPUT screen - fmt[1].format("Warehouse: %6d District: %2d Date: %19.19s", - newOrder.w_id, newOrder.d_id, newOrder.o_entry_d); - fmt[2].format("Customer: %4d Name: %-16.16s Credit: %2.2s %%Disc: %5.2f", - newOrder.c_id, newOrder.c_last, - newOrder.c_credit, newOrder.c_discount * 100.0); - fmt[3].format("Order Number: %8d Number of Lines: %2d W_tax: %5.2f D_tax: %5.2f", - newOrder.o_id, newOrder.o_ol_cnt, - newOrder.w_tax * 100.0, newOrder.d_tax * 100.0); - - fmt[5].format("Supp_W Item_Id Item Name Qty Stock B/G Price Amount"); - - for (int i = 0; i < 15; i++) - { - if (newOrder.ol_i_id[i] != 0) - fmt[6 + i].format("%6d %6d %-24.24s %2d %3d %1.1s $%6.2f $%7.2f", - newOrder.ol_supply_w_id[i], - newOrder.ol_i_id[i], newOrder.i_name[i], - newOrder.ol_quantity[i], - newOrder.s_quantity[i], - newOrder.brand_generic[i], - newOrder.i_price[i], - newOrder.ol_amount[i]); - } - - fmt[21].format("Execution Status: %-24.24s Total: $%8.2f", - newOrder.execution_status, newOrder.total_amount); - } + private void traceNewOrder(Logger log, Formatter fmt[]) { + fmt[0].format(" New Order"); + + if (transEnd == 0) { + // NEW_ORDER INPUT screen + fmt[1].format("Warehouse: %6d District: %2d Date:", + newOrder.w_id, newOrder.d_id); + fmt[2].format("Customer: %4d Name: Credit: %%Disc:", + newOrder.c_id); + fmt[3].format("Order Number: Number of Lines: W_tax: D_tax:"); + + fmt[5].format("Supp_W Item_Id Item Name Qty Stock B/G Price Amount"); + + for (int i = 0; i < 15; i++) { + if (newOrder.ol_i_id[i] != 0) + fmt[6 + i].format("%6d %6d %2d", + newOrder.ol_supply_w_id[i], + newOrder.ol_i_id[i], newOrder.ol_quantity[i]); + else + fmt[6 + i].format("______ ______ __"); + } + + fmt[21].format("Execution Status: Total: $"); + } else { + // NEW_ORDER OUTPUT screen + fmt[1].format("Warehouse: %6d District: %2d Date: %19.19s", + newOrder.w_id, newOrder.d_id, newOrder.o_entry_d); + fmt[2].format("Customer: %4d Name: %-16.16s Credit: %2.2s %%Disc: %5.2f", + newOrder.c_id, newOrder.c_last, + newOrder.c_credit, newOrder.c_discount * 100.0); + fmt[3].format("Order Number: %8d Number of Lines: %2d W_tax: %5.2f D_tax: %5.2f", + newOrder.o_id, newOrder.o_ol_cnt, + newOrder.w_tax * 100.0, newOrder.d_tax * 100.0); + + fmt[5].format("Supp_W Item_Id Item Name Qty Stock B/G Price Amount"); + + for (int i = 0; i < 15; i++) { + if (newOrder.ol_i_id[i] != 0) + fmt[6 + i].format("%6d %6d %-24.24s %2d %3d %1.1s $%6.2f $%7.2f", + newOrder.ol_supply_w_id[i], + newOrder.ol_i_id[i], newOrder.i_name[i], + newOrder.ol_quantity[i], + newOrder.s_quantity[i], + newOrder.brand_generic[i], + newOrder.i_price[i], + newOrder.ol_amount[i]); + } + + fmt[21].format("Execution Status: %-24.24s Total: $%8.2f", + newOrder.execution_status, newOrder.total_amount); + } + } + + private class NewOrderItem { + /* terminal input data */ + public int i_id; + public double i_price; + public String i_name; + public String i_data; } - private class NewOrderItem { - /* terminal input data */ - public int i_id; - public double i_price; - public String i_name; - public String i_data; - } - - private class NewOrderData - { - /* terminal input data */ - public int w_id; - public int d_id; - public int c_id; - - public int ol_supply_w_id[] = new int[15]; - public int ol_i_id[] = new int[15]; - public int ol_quantity[] = new int[15]; - - /* terminal output data */ - public String c_last; - public String c_credit; - public double c_discount; - public double w_tax; - public double d_tax; - public int o_ol_cnt; - public int o_id; - public String o_entry_d; - public double total_amount; - public String execution_status; - - public String i_name[] = new String[15]; - public int s_quantity[] = new int[15]; - public String brand_generic[] = new String[15]; - public double i_price[] = new double[15]; - public double ol_amount[] = new double[15]; - public String dist_value[] = new String[15]; - public boolean found[] = new boolean[15]; + + private class NewOrderData { + /* terminal input data */ + public int w_id; + public int d_id; + public int c_id; + + public int ol_supply_w_id[] = new int[15]; + public int ol_i_id[] = new int[15]; + public int ol_quantity[] = new int[15]; + + /* terminal output data */ + public String c_last; + public String c_credit; + public double c_discount; + public double w_tax; + public double d_tax; + public int o_ol_cnt; + public int o_id; + public String o_entry_d; + public double total_amount; + public String execution_status; + + public String i_name[] = new String[15]; + public int s_quantity[] = new int[15]; + public String brand_generic[] = new String[15]; + public double i_price[] = new double[15]; + public double ol_amount[] = new double[15]; + public String dist_value[] = new String[15]; + public boolean found[] = new boolean[15]; } /* ********************************************************************** @@ -707,385 +665,348 @@ private class NewOrderData * ***** PAYMENT related methods and subclass. ************************** * ********************************************************************** * *********************************************************************/ - public void generatePayment(Logger log, jTPCCRandom rnd, long due) - { - transType = TT_PAYMENT; - transDue = due; - transStart = 0; - transEnd = 0; - transRbk = false; - transError = null; - - newOrder = null; - payment = new PaymentData(); - orderStatus = null; - stockLevel = null; - delivery = null; - deliveryBG = null; - - payment.w_id = terminalWarehouse; // 2.5.1.1 - payment.d_id = rnd.nextInt(1, 10); // 2.5.1.2 - payment.c_w_id = payment.w_id; - payment.c_d_id = payment.d_id; - if (rnd.nextInt(1, 100) > 85) - { - payment.c_d_id = rnd.nextInt(1, 10); - while (payment.c_w_id == payment.w_id && numWarehouses > 1) - payment.c_w_id = rnd.nextInt(1, numWarehouses); - } - if (rnd.nextInt(1, 100) <= 60) - { - payment.c_last = rnd.getCLast(); - payment.c_id = 0; - } - else - { - payment.c_last = null; - payment.c_id = rnd.getCustomerID(); - } - - // 2.5.1.3 - payment.h_amount = ((double)rnd.nextLong(100, 500000)) / 100.0; + public void generatePayment(Logger log, jTPCCRandom rnd, long due) { + transType = TT_PAYMENT; + transDue = due; + transStart = 0; + transEnd = 0; + transRbk = false; + transError = null; + + newOrder = null; + payment = new PaymentData(); + orderStatus = null; + stockLevel = null; + delivery = null; + deliveryBG = null; + + payment.w_id = terminalWarehouse; // 2.5.1.1 + payment.d_id = rnd.nextInt(1, 10); // 2.5.1.2 + payment.c_w_id = payment.w_id; + payment.c_d_id = payment.d_id; + if (rnd.nextInt(1, 100) > 85) { + payment.c_d_id = rnd.nextInt(1, 10); + while (payment.c_w_id == payment.w_id && numWarehouses > 1) + payment.c_w_id = rnd.nextInt(1, numWarehouses); + } + if (rnd.nextInt(1, 100) <= 60) { + payment.c_last = rnd.getCLast(); + payment.c_id = 0; + } else { + payment.c_last = null; + payment.c_id = rnd.getCustomerID(); + } + + // 2.5.1.3 + payment.h_amount = ((double) rnd.nextLong(100, 500000)) / 100.0; } private void executePayment(Logger log, jTPCCConnection db) - throws Exception - { - PreparedStatement stmt; - ResultSet rs; - Vector c_id_list = new Vector (); - - long h_date = System.currentTimeMillis(); - - try - { - // Update the DISTRICT. - stmt = db.stmtPaymentUpdateDistrict; - stmt.setDouble(1, payment.h_amount); - stmt.setInt(2, payment.w_id); - stmt.setInt(3, payment.d_id); - stmt.executeUpdate(); - - // Select the DISTRICT. - stmt = db.stmtPaymentSelectDistrict; - stmt.setInt(1, payment.w_id); - stmt.setInt(2, payment.d_id); - rs = stmt.executeQuery(); - if (!rs.next()) - { - rs.close(); - throw new Exception("District for" + - " W_ID=" + payment.w_id + - " D_ID=" + payment.d_id + " not found"); - } - payment.d_name = rs.getString("d_name"); - payment.d_street_1 = rs.getString("d_street_1"); - payment.d_street_2 = rs.getString("d_street_2"); - payment.d_city = rs.getString("d_city"); - payment.d_state = rs.getString("d_state"); - payment.d_zip = rs.getString("d_zip"); - rs.close(); - - // Update the WAREHOUSE. - stmt = db.stmtPaymentUpdateWarehouse; - stmt.setDouble(1, payment.h_amount); - stmt.setInt(2, payment.w_id); - stmt.executeUpdate(); - - // Select the WAREHOUSE. - stmt = db.stmtPaymentSelectWarehouse; - stmt.setInt(1, payment.w_id); - rs = stmt.executeQuery(); - if (!rs.next()) - { - rs.close(); - throw new Exception("Warehouse for" + - " W_ID=" + payment.w_id + " not found"); - } - payment.w_name = rs.getString("w_name"); - payment.w_street_1 = rs.getString("w_street_1"); - payment.w_street_2 = rs.getString("w_street_2"); - payment.w_city = rs.getString("w_city"); - payment.w_state = rs.getString("w_state"); - payment.w_zip = rs.getString("w_zip"); - rs.close(); - - // If C_LAST is given instead of C_ID (60%), determine the C_ID. - if (payment.c_last != null) - { - stmt = db.stmtPaymentSelectCustomerListByLast; - stmt.setInt(1, payment.c_w_id); - stmt.setInt(2, payment.c_d_id); - stmt.setString(3, payment.c_last); - rs = stmt.executeQuery(); - while (rs.next()) - c_id_list.add(rs.getInt("c_id")); - rs.close(); - - if (c_id_list.size() == 0) - { - throw new Exception("Customer(s) for" + - " C_W_ID=" + payment.c_w_id + - " C_D_ID=" + payment.c_d_id + - " C_LAST=" + payment.c_last + " not found"); - } - - payment.c_id = c_id_list.get((c_id_list.size() + 1) / 2 - 1); - } - - // Select the CUSTOMER. - stmt = db.stmtPaymentSelectCustomer; - stmt.setInt(1, payment.c_w_id); - stmt.setInt(2, payment.c_d_id); - stmt.setInt(3, payment.c_id); - rs = stmt.executeQuery(); - if (!rs.next()) - { - throw new Exception("Customer for" + - " C_W_ID=" + payment.c_w_id + - " C_D_ID=" + payment.c_d_id + - " C_ID=" + payment.c_id + " not found"); - } - payment.c_first = rs.getString("c_first"); - payment.c_middle = rs.getString("c_middle"); - if (payment.c_last == null) - payment.c_last = rs.getString("c_last"); - payment.c_street_1 = rs.getString("c_street_1"); - payment.c_street_2 = rs.getString("c_street_2"); - payment.c_city = rs.getString("c_city"); - payment.c_state = rs.getString("c_state"); - payment.c_zip = rs.getString("c_zip"); - payment.c_phone = rs.getString("c_phone"); - payment.c_since = rs.getTimestamp("c_since").toString(); - payment.c_credit = rs.getString("c_credit"); - payment.c_credit_lim = rs.getDouble("c_credit_lim"); - payment.c_discount = rs.getDouble("c_discount"); - payment.c_balance = rs.getDouble("c_balance"); - payment.c_data = new String(""); - rs.close(); - - // Update the CUSTOMER. - payment.c_balance -= payment.h_amount; - if (payment.c_credit.equals("GC")) - { - // Customer with good credit, don't update C_DATA. - stmt = db.stmtPaymentUpdateCustomer; - stmt.setDouble(1, payment.h_amount); - stmt.setDouble(2, payment.h_amount); - stmt.setInt(3, payment.c_w_id); - stmt.setInt(4, payment.c_d_id); - stmt.setInt(5, payment.c_id); - stmt.executeUpdate(); - } - else - { - // Customer with bad credit, need to do the C_DATA work. - stmt = db.stmtPaymentSelectCustomerData; - stmt.setInt(1, payment.c_w_id); - stmt.setInt(2, payment.c_d_id); - stmt.setInt(3, payment.c_id); - rs = stmt.executeQuery(); - if (!rs.next()) - { - throw new Exception("Customer.c_data for" + - " C_W_ID=" + payment.c_w_id + - " C_D_ID=" + payment.c_d_id + - " C_ID=" + payment.c_id + " not found"); - } - payment.c_data = rs.getString("c_data"); - rs.close(); - - stmt = db.stmtPaymentUpdateCustomerWithData; - stmt.setDouble(1, payment.h_amount); - stmt.setDouble(2, payment.h_amount); - - StringBuffer sbData = new StringBuffer(); - Formatter fmtData = new Formatter(sbData); - fmtData.format("C_ID=%d C_D_ID=%d C_W_ID=%d " + - "D_ID=%d W_ID=%d H_AMOUNT=%.2f ", - payment.c_id, payment.c_d_id, payment.c_w_id, - payment.d_id, payment.w_id, payment.h_amount); - sbData.append(payment.c_data); - if (sbData.length() > 500) - sbData.setLength(500); - payment.c_data = sbData.toString(); - stmt.setString(3, payment.c_data); - - stmt.setInt(4, payment.c_w_id); - stmt.setInt(5, payment.c_d_id); - stmt.setInt(6, payment.c_id); - stmt.executeUpdate(); - } - - // Insert the HISORY row. - stmt = db.stmtPaymentInsertHistory; - stmt.setInt(1, payment.c_id); - stmt.setInt(2, payment.c_d_id); - stmt.setInt(3, payment.c_w_id); - stmt.setInt(4, payment.d_id); - stmt.setInt(5, payment.w_id); - stmt.setTimestamp(6, new java.sql.Timestamp(h_date)); - stmt.setDouble(7, payment.h_amount); - stmt.setString(8, payment.w_name + " " + payment.d_name); - stmt.executeUpdate(); - - payment.h_date = new java.sql.Timestamp(h_date).toString(); - - db.commit(); - } - catch (SQLException se) - { - log.error("Unexpected SQLException in PAYMENT"); - for (SQLException x = se; x != null; x = x.getNextException()) - log.error(x.getMessage()); - se.printStackTrace(); - - try - { - db.rollback(); - } - catch (SQLException se2) - { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); - } - } - catch (Exception e) - { - try - { - db.rollback(); - } - catch (SQLException se2) - { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); - } - throw e; - } + throws Exception { + PreparedStatement stmt; + ResultSet rs; + Vector c_id_list = new Vector(); + + long h_date = System.currentTimeMillis(); + + try { + // Update the DISTRICT. + stmt = db.stmtPaymentUpdateDistrict; + stmt.setDouble(1, payment.h_amount); + stmt.setInt(2, payment.w_id); + stmt.setInt(3, payment.d_id); + stmt.executeUpdate(); + + // Select the DISTRICT. + stmt = db.stmtPaymentSelectDistrict; + stmt.setInt(1, payment.w_id); + stmt.setInt(2, payment.d_id); + rs = stmt.executeQuery(); + if (!rs.next()) { + rs.close(); + throw new Exception("District for" + + " W_ID=" + payment.w_id + + " D_ID=" + payment.d_id + " not found"); + } + payment.d_name = rs.getString("d_name"); + payment.d_street_1 = rs.getString("d_street_1"); + payment.d_street_2 = rs.getString("d_street_2"); + payment.d_city = rs.getString("d_city"); + payment.d_state = rs.getString("d_state"); + payment.d_zip = rs.getString("d_zip"); + rs.close(); + + // Update the WAREHOUSE. + stmt = db.stmtPaymentUpdateWarehouse; + stmt.setDouble(1, payment.h_amount); + stmt.setInt(2, payment.w_id); + stmt.executeUpdate(); + + // Select the WAREHOUSE. + stmt = db.stmtPaymentSelectWarehouse; + stmt.setInt(1, payment.w_id); + rs = stmt.executeQuery(); + if (!rs.next()) { + rs.close(); + throw new Exception("Warehouse for" + + " W_ID=" + payment.w_id + " not found"); + } + payment.w_name = rs.getString("w_name"); + payment.w_street_1 = rs.getString("w_street_1"); + payment.w_street_2 = rs.getString("w_street_2"); + payment.w_city = rs.getString("w_city"); + payment.w_state = rs.getString("w_state"); + payment.w_zip = rs.getString("w_zip"); + rs.close(); + + // If C_LAST is given instead of C_ID (60%), determine the C_ID. + if (payment.c_last != null) { + stmt = db.stmtPaymentSelectCustomerListByLast; + stmt.setInt(1, payment.c_w_id); + stmt.setInt(2, payment.c_d_id); + stmt.setString(3, payment.c_last); + rs = stmt.executeQuery(); + while (rs.next()) + c_id_list.add(rs.getInt("c_id")); + rs.close(); + + if (c_id_list.size() == 0) { + throw new Exception("Customer(s) for" + + " C_W_ID=" + payment.c_w_id + + " C_D_ID=" + payment.c_d_id + + " C_LAST=" + payment.c_last + " not found"); + } + + payment.c_id = c_id_list.get((c_id_list.size() + 1) / 2 - 1); + } + + // Select the CUSTOMER. + stmt = db.stmtPaymentSelectCustomer; + stmt.setInt(1, payment.c_w_id); + stmt.setInt(2, payment.c_d_id); + stmt.setInt(3, payment.c_id); + rs = stmt.executeQuery(); + if (!rs.next()) { + throw new Exception("Customer for" + + " C_W_ID=" + payment.c_w_id + + " C_D_ID=" + payment.c_d_id + + " C_ID=" + payment.c_id + " not found"); + } + payment.c_first = rs.getString("c_first"); + payment.c_middle = rs.getString("c_middle"); + if (payment.c_last == null) + payment.c_last = rs.getString("c_last"); + payment.c_street_1 = rs.getString("c_street_1"); + payment.c_street_2 = rs.getString("c_street_2"); + payment.c_city = rs.getString("c_city"); + payment.c_state = rs.getString("c_state"); + payment.c_zip = rs.getString("c_zip"); + payment.c_phone = rs.getString("c_phone"); + payment.c_since = rs.getTimestamp("c_since").toString(); + payment.c_credit = rs.getString("c_credit"); + payment.c_credit_lim = rs.getDouble("c_credit_lim"); + payment.c_discount = rs.getDouble("c_discount"); + payment.c_balance = rs.getDouble("c_balance"); + payment.c_data = new String(""); + rs.close(); + + // Update the CUSTOMER. + payment.c_balance -= payment.h_amount; + if (payment.c_credit.equals("GC")) { + // Customer with good credit, don't update C_DATA. + stmt = db.stmtPaymentUpdateCustomer; + stmt.setDouble(1, payment.h_amount); + stmt.setDouble(2, payment.h_amount); + stmt.setInt(3, payment.c_w_id); + stmt.setInt(4, payment.c_d_id); + stmt.setInt(5, payment.c_id); + stmt.executeUpdate(); + } else { + // Customer with bad credit, need to do the C_DATA work. + stmt = db.stmtPaymentSelectCustomerData; + stmt.setInt(1, payment.c_w_id); + stmt.setInt(2, payment.c_d_id); + stmt.setInt(3, payment.c_id); + rs = stmt.executeQuery(); + if (!rs.next()) { + throw new Exception("Customer.c_data for" + + " C_W_ID=" + payment.c_w_id + + " C_D_ID=" + payment.c_d_id + + " C_ID=" + payment.c_id + " not found"); + } + payment.c_data = rs.getString("c_data"); + rs.close(); + + stmt = db.stmtPaymentUpdateCustomerWithData; + stmt.setDouble(1, payment.h_amount); + stmt.setDouble(2, payment.h_amount); + + StringBuffer sbData = new StringBuffer(); + Formatter fmtData = new Formatter(sbData); + fmtData.format("C_ID=%d C_D_ID=%d C_W_ID=%d " + + "D_ID=%d W_ID=%d H_AMOUNT=%.2f ", + payment.c_id, payment.c_d_id, payment.c_w_id, + payment.d_id, payment.w_id, payment.h_amount); + sbData.append(payment.c_data); + if (sbData.length() > 500) + sbData.setLength(500); + payment.c_data = sbData.toString(); + stmt.setString(3, payment.c_data); + + stmt.setInt(4, payment.c_w_id); + stmt.setInt(5, payment.c_d_id); + stmt.setInt(6, payment.c_id); + stmt.executeUpdate(); + } + + // Insert the HISORY row. + stmt = db.stmtPaymentInsertHistory; + stmt.setInt(1, payment.c_id); + stmt.setInt(2, payment.c_d_id); + stmt.setInt(3, payment.c_w_id); + stmt.setInt(4, payment.d_id); + stmt.setInt(5, payment.w_id); + stmt.setTimestamp(6, new java.sql.Timestamp(h_date)); + stmt.setDouble(7, payment.h_amount); + stmt.setString(8, payment.w_name + " " + payment.d_name); + stmt.executeUpdate(); + + payment.h_date = new java.sql.Timestamp(h_date).toString(); + + db.commit(); + } catch (SQLException se) { + log.error("Unexpected SQLException in PAYMENT"); + for (SQLException x = se; x != null; x = x.getNextException()) + log.error(x.getMessage()); + se.printStackTrace(); + + try { + db.rollback(); + } catch (SQLException se2) { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } + } catch (Exception e) { + try { + db.rollback(); + } catch (SQLException se2) { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } + throw e; + } } - private void tracePayment(Logger log, Formatter fmt[]) - { - fmt[0].format(" Payment"); - - if (transEnd == 0) - { - // PAYMENT INPUT screen - fmt[1].format("Date: "); - fmt[3].format("Warehouse: %6d District: %2d", - payment.w_id, payment.d_id); - - if (payment.c_last == null) - { - fmt[8].format("Customer: %4d Cust-Warehouse: %6d Cust-District: %2d", - payment.c_id, payment.c_w_id, payment.c_d_id); - fmt[9].format("Name: ________________ Since:"); - } - else - { - fmt[8].format("Customer: ____ Cust-Warehouse: %6d Cust-District: %2d", - payment.c_w_id, payment.c_d_id); - fmt[9].format("Name: %-16.16s Since:", - payment.c_last); - } - fmt[10].format(" Credit:"); - fmt[11].format(" %%Disc:"); - fmt[12].format(" Phone:"); - - fmt[14].format("Amount Paid: $%7.2f New Cust-Balance:", - payment.h_amount); - fmt[15].format("Credit Limit:"); - fmt[17].format("Cust-Data:"); - } - else - { - // PAYMENT OUTPUT screen - fmt[1].format("Date: %-19.19s", payment.h_date); - fmt[3].format("Warehouse: %6d District: %2d", - payment.w_id, payment.d_id); - fmt[4].format("%-20.20s %-20.20s", - payment.w_street_1, payment.d_street_1); - fmt[5].format("%-20.20s %-20.20s", - payment.w_street_2, payment.d_street_2); - fmt[6].format("%-20.20s %2.2s %5.5s-%4.4s %-20.20s %2.2s %5.5s-%4.4s", - payment.w_city, payment.w_state, - payment.w_zip.substring(0, 5), payment.w_zip.substring(5, 9), - payment.d_city, payment.d_state, - payment.d_zip.substring(0, 5), payment.d_zip.substring(5, 9)); -log.trace("w_zip=" + payment.w_zip + " d_zip=" + payment.d_zip); - - fmt[8].format("Customer: %4d Cust-Warehouse: %6d Cust-District: %2d", - payment.c_id, payment.c_w_id, payment.c_d_id); - fmt[9].format("Name: %-16.16s %2.2s %-16.16s Since: %-10.10s", - payment.c_first, payment.c_middle, payment.c_last, - payment.c_since); - fmt[10].format(" %-20.20s Credit: %2s", - payment.c_street_1, payment.c_credit); - fmt[11].format(" %-20.20s %%Disc: %5.2f", - payment.c_street_2, payment.c_discount * 100.0); - fmt[12].format(" %-20.20s %2.2s %5.5s-%4.4s Phone: %6.6s-%3.3s-%3.3s-%4.4s", - payment.c_city, payment.c_state, - payment.c_zip.substring(0, 5), payment.c_zip.substring(5, 9), - payment.c_phone.substring(0, 6), payment.c_phone.substring(6, 9), - payment.c_phone.substring(9, 12), payment.c_phone.substring(12, 16)); - - fmt[14].format("Amount Paid: $%7.2f New Cust-Balance: $%14.2f", - payment.h_amount, payment.c_balance); - fmt[15].format("Credit Limit: $%13.2f", payment.c_credit_lim); - if (payment.c_data.length() >= 200) - { - fmt[17].format("Cust-Data: %-50.50s", payment.c_data.substring(0, 50)); - fmt[18].format(" %-50.50s", payment.c_data.substring(50, 100)); - fmt[19].format(" %-50.50s", payment.c_data.substring(100, 150)); - fmt[20].format(" %-50.50s", payment.c_data.substring(150, 200)); - } - else - { - fmt[17].format("Cust-Data:"); - } - } + private void tracePayment(Logger log, Formatter fmt[]) { + fmt[0].format(" Payment"); + + if (transEnd == 0) { + // PAYMENT INPUT screen + fmt[1].format("Date: "); + fmt[3].format("Warehouse: %6d District: %2d", + payment.w_id, payment.d_id); + + if (payment.c_last == null) { + fmt[8].format("Customer: %4d Cust-Warehouse: %6d Cust-District: %2d", + payment.c_id, payment.c_w_id, payment.c_d_id); + fmt[9].format("Name: ________________ Since:"); + } else { + fmt[8].format("Customer: ____ Cust-Warehouse: %6d Cust-District: %2d", + payment.c_w_id, payment.c_d_id); + fmt[9].format("Name: %-16.16s Since:", + payment.c_last); + } + fmt[10].format(" Credit:"); + fmt[11].format(" %%Disc:"); + fmt[12].format(" Phone:"); + + fmt[14].format("Amount Paid: $%7.2f New Cust-Balance:", + payment.h_amount); + fmt[15].format("Credit Limit:"); + fmt[17].format("Cust-Data:"); + } else { + // PAYMENT OUTPUT screen + fmt[1].format("Date: %-19.19s", payment.h_date); + fmt[3].format("Warehouse: %6d District: %2d", + payment.w_id, payment.d_id); + fmt[4].format("%-20.20s %-20.20s", + payment.w_street_1, payment.d_street_1); + fmt[5].format("%-20.20s %-20.20s", + payment.w_street_2, payment.d_street_2); + fmt[6].format("%-20.20s %2.2s %5.5s-%4.4s %-20.20s %2.2s %5.5s-%4.4s", + payment.w_city, payment.w_state, + payment.w_zip.substring(0, 5), payment.w_zip.substring(5, 9), + payment.d_city, payment.d_state, + payment.d_zip.substring(0, 5), payment.d_zip.substring(5, 9)); + log.trace("w_zip=" + payment.w_zip + " d_zip=" + payment.d_zip); + + fmt[8].format("Customer: %4d Cust-Warehouse: %6d Cust-District: %2d", + payment.c_id, payment.c_w_id, payment.c_d_id); + fmt[9].format("Name: %-16.16s %2.2s %-16.16s Since: %-10.10s", + payment.c_first, payment.c_middle, payment.c_last, + payment.c_since); + fmt[10].format(" %-20.20s Credit: %2s", + payment.c_street_1, payment.c_credit); + fmt[11].format(" %-20.20s %%Disc: %5.2f", + payment.c_street_2, payment.c_discount * 100.0); + fmt[12].format(" %-20.20s %2.2s %5.5s-%4.4s Phone: %6.6s-%3.3s-%3.3s-%4.4s", + payment.c_city, payment.c_state, + payment.c_zip.substring(0, 5), payment.c_zip.substring(5, 9), + payment.c_phone.substring(0, 6), payment.c_phone.substring(6, 9), + payment.c_phone.substring(9, 12), payment.c_phone.substring(12, 16)); + + fmt[14].format("Amount Paid: $%7.2f New Cust-Balance: $%14.2f", + payment.h_amount, payment.c_balance); + fmt[15].format("Credit Limit: $%13.2f", payment.c_credit_lim); + if (payment.c_data.length() >= 200) { + fmt[17].format("Cust-Data: %-50.50s", payment.c_data.substring(0, 50)); + fmt[18].format(" %-50.50s", payment.c_data.substring(50, 100)); + fmt[19].format(" %-50.50s", payment.c_data.substring(100, 150)); + fmt[20].format(" %-50.50s", payment.c_data.substring(150, 200)); + } else { + fmt[17].format("Cust-Data:"); + } + } } - private class PaymentData - { - /* terminal input data */ - public int w_id; - public int d_id; - public int c_id; - public int c_d_id; - public int c_w_id; - public String c_last; - public double h_amount; - - /* terminal output data */ - public String w_name; - public String w_street_1; - public String w_street_2; - public String w_city; - public String w_state; - public String w_zip; - public String d_name; - public String d_street_1; - public String d_street_2; - public String d_city; - public String d_state; - public String d_zip; - public String c_first; - public String c_middle; - public String c_street_1; - public String c_street_2; - public String c_city; - public String c_state; - public String c_zip; - public String c_phone; - public String c_since; - public String c_credit; - public double c_credit_lim; - public double c_discount; - public double c_balance; - public String c_data; - public String h_date; + private class PaymentData { + /* terminal input data */ + public int w_id; + public int d_id; + public int c_id; + public int c_d_id; + public int c_w_id; + public String c_last; + public double h_amount; + + /* terminal output data */ + public String w_name; + public String w_street_1; + public String w_street_2; + public String w_city; + public String w_state; + public String w_zip; + public String d_name; + public String d_street_1; + public String d_street_2; + public String d_city; + public String d_state; + public String d_zip; + public String c_first; + public String c_middle; + public String c_street_1; + public String c_street_2; + public String c_city; + public String c_state; + public String c_zip; + public String c_phone; + public String c_since; + public String c_credit; + public double c_credit_lim; + public double c_discount; + public double c_balance; + public String c_data; + public String h_date; } /* ********************************************************************** @@ -1093,246 +1014,218 @@ private class PaymentData * ***** ORDER_STATUS related methods and subclass. ********************* * ********************************************************************** * *********************************************************************/ - public void generateOrderStatus(Logger log, jTPCCRandom rnd, long due) - { - transType = TT_ORDER_STATUS; - transDue = due; - transStart = 0; - transEnd = 0; - transRbk = false; - transError = null; - - newOrder = null; - payment = null; - orderStatus = new OrderStatusData(); - stockLevel = null; - delivery = null; - deliveryBG = null; - - orderStatus.w_id = terminalWarehouse; - orderStatus.d_id = rnd.nextInt(1, 10); - if (rnd.nextInt(1, 100) <= 60) - { - orderStatus.c_id = 0; - orderStatus.c_last = rnd.getCLast(); - } - else - { - orderStatus.c_id = rnd.getCustomerID(); - orderStatus.c_last = null; - } + public void generateOrderStatus(Logger log, jTPCCRandom rnd, long due) { + transType = TT_ORDER_STATUS; + transDue = due; + transStart = 0; + transEnd = 0; + transRbk = false; + transError = null; + + newOrder = null; + payment = null; + orderStatus = new OrderStatusData(); + stockLevel = null; + delivery = null; + deliveryBG = null; + + orderStatus.w_id = terminalWarehouse; + orderStatus.d_id = rnd.nextInt(1, 10); + if (rnd.nextInt(1, 100) <= 60) { + orderStatus.c_id = 0; + orderStatus.c_last = rnd.getCLast(); + } else { + orderStatus.c_id = rnd.getCustomerID(); + orderStatus.c_last = null; + } } private void executeOrderStatus(Logger log, jTPCCConnection db) - throws Exception - { - PreparedStatement stmt; - ResultSet rs; - Vector c_id_list = new Vector (); - int ol_idx = 0; - - try - { - // If C_LAST is given instead of C_ID (60%), determine the C_ID. - if (orderStatus.c_last != null) - { - stmt = db.stmtOrderStatusSelectCustomerListByLast; - stmt.setInt(1, orderStatus.w_id); - stmt.setInt(2, orderStatus.d_id); - stmt.setString(3, orderStatus.c_last); - rs = stmt.executeQuery(); - while (rs.next()) - c_id_list.add(rs.getInt("c_id")); - rs.close(); - - if (c_id_list.size() == 0) - { - throw new Exception("Customer(s) for" + - " C_W_ID=" + orderStatus.w_id + - " C_D_ID=" + orderStatus.d_id + - " C_LAST=" + orderStatus.c_last + " not found"); - } - - orderStatus.c_id = c_id_list.get((c_id_list.size() + 1) / 2 - 1); - } - - // Select the CUSTOMER. - stmt = db.stmtOrderStatusSelectCustomer; - stmt.setInt(1, orderStatus.w_id); - stmt.setInt(2, orderStatus.d_id); - stmt.setInt(3, orderStatus.c_id); - rs = stmt.executeQuery(); - if (!rs.next()) - { - throw new Exception("Customer for" + - " C_W_ID=" + orderStatus.w_id + - " C_D_ID=" + orderStatus.d_id + - " C_ID=" + orderStatus.c_id + " not found"); - } - orderStatus.c_first = rs.getString("c_first"); - orderStatus.c_middle = rs.getString("c_middle"); - if (orderStatus.c_last == null) - orderStatus.c_last = rs.getString("c_last"); - orderStatus.c_balance = rs.getDouble("c_balance"); - rs.close(); - - // Select the last ORDER for this customer. - stmt = db.stmtOrderStatusSelectLastOrder; - stmt.setInt(1, orderStatus.w_id); - stmt.setInt(2, orderStatus.d_id); - stmt.setInt(3, orderStatus.c_id); - rs = stmt.executeQuery(); - if (!rs.next()) - { - throw new Exception("Last Order for" + - " W_ID=" + orderStatus.w_id + - " D_ID=" + orderStatus.d_id + - " C_ID=" + orderStatus.c_id + " not found"); - } - orderStatus.o_id = rs.getInt("o_id"); - orderStatus.o_entry_d = rs.getTimestamp("o_entry_d").toString(); - orderStatus.o_carrier_id = rs.getInt("o_carrier_id"); - if (rs.wasNull()) - orderStatus.o_carrier_id = -1; - rs.close(); - - stmt = db.stmtOrderStatusSelectOrderLine; - stmt.setInt(1, orderStatus.w_id); - stmt.setInt(2, orderStatus.d_id); - stmt.setInt(3, orderStatus.o_id); - rs = stmt.executeQuery(); - while (rs.next()) - { - Timestamp ol_delivery_d; - - orderStatus.ol_i_id[ol_idx] = rs.getInt("ol_i_id"); - orderStatus.ol_supply_w_id[ol_idx] = rs.getInt("ol_supply_w_id"); - orderStatus.ol_quantity[ol_idx] = rs.getInt("ol_quantity"); - orderStatus.ol_amount[ol_idx] = rs.getDouble("ol_amount"); - ol_delivery_d = rs.getTimestamp("ol_delivery_d"); - if (ol_delivery_d != null) - orderStatus.ol_delivery_d[ol_idx] = ol_delivery_d.toString(); - else - orderStatus.ol_delivery_d[ol_idx] = null; - ol_idx++; - } - rs.close(); - - while (ol_idx < 15) - { - orderStatus.ol_i_id[ol_idx] = 0; - orderStatus.ol_supply_w_id[ol_idx] = 0; - orderStatus.ol_quantity[ol_idx] = 0; - orderStatus.ol_amount[ol_idx] = 0.0; - orderStatus.ol_delivery_d[ol_idx] = null; - ol_idx++; - } - - db.rollback(); - } - catch (SQLException se) - { - log.error("Unexpected SQLException in ORDER_STATUS"); - for (SQLException x = se; x != null; x = x.getNextException()) - log.error(x.getMessage()); - se.printStackTrace(); - - try - { - db.rollback(); - } - catch (SQLException se2) - { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); - } - } - catch (Exception e) - { - try - { - db.rollback(); - } - catch (SQLException se2) - { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); - } - throw e; - } + throws Exception { + PreparedStatement stmt; + ResultSet rs; + Vector c_id_list = new Vector(); + int ol_idx = 0; + + try { + // If C_LAST is given instead of C_ID (60%), determine the C_ID. + if (orderStatus.c_last != null) { + stmt = db.stmtOrderStatusSelectCustomerListByLast; + stmt.setInt(1, orderStatus.w_id); + stmt.setInt(2, orderStatus.d_id); + stmt.setString(3, orderStatus.c_last); + rs = stmt.executeQuery(); + while (rs.next()) + c_id_list.add(rs.getInt("c_id")); + rs.close(); + + if (c_id_list.size() == 0) { + throw new Exception("Customer(s) for" + + " C_W_ID=" + orderStatus.w_id + + " C_D_ID=" + orderStatus.d_id + + " C_LAST=" + orderStatus.c_last + " not found"); + } + + orderStatus.c_id = c_id_list.get((c_id_list.size() + 1) / 2 - 1); + } + + // Select the CUSTOMER. + stmt = db.stmtOrderStatusSelectCustomer; + stmt.setInt(1, orderStatus.w_id); + stmt.setInt(2, orderStatus.d_id); + stmt.setInt(3, orderStatus.c_id); + rs = stmt.executeQuery(); + if (!rs.next()) { + throw new Exception("Customer for" + + " C_W_ID=" + orderStatus.w_id + + " C_D_ID=" + orderStatus.d_id + + " C_ID=" + orderStatus.c_id + " not found"); + } + orderStatus.c_first = rs.getString("c_first"); + orderStatus.c_middle = rs.getString("c_middle"); + if (orderStatus.c_last == null) + orderStatus.c_last = rs.getString("c_last"); + orderStatus.c_balance = rs.getDouble("c_balance"); + rs.close(); + + // Select the last ORDER for this customer. + stmt = db.stmtOrderStatusSelectLastOrder; + stmt.setInt(1, orderStatus.w_id); + stmt.setInt(2, orderStatus.d_id); + stmt.setInt(3, orderStatus.c_id); + rs = stmt.executeQuery(); + if (!rs.next()) { + throw new Exception("Last Order for" + + " W_ID=" + orderStatus.w_id + + " D_ID=" + orderStatus.d_id + + " C_ID=" + orderStatus.c_id + " not found"); + } + orderStatus.o_id = rs.getInt("o_id"); + orderStatus.o_entry_d = rs.getTimestamp("o_entry_d").toString(); + orderStatus.o_carrier_id = rs.getInt("o_carrier_id"); + if (rs.wasNull()) + orderStatus.o_carrier_id = -1; + rs.close(); + + stmt = db.stmtOrderStatusSelectOrderLine; + stmt.setInt(1, orderStatus.w_id); + stmt.setInt(2, orderStatus.d_id); + stmt.setInt(3, orderStatus.o_id); + rs = stmt.executeQuery(); + while (rs.next()) { + Timestamp ol_delivery_d; + + orderStatus.ol_i_id[ol_idx] = rs.getInt("ol_i_id"); + orderStatus.ol_supply_w_id[ol_idx] = rs.getInt("ol_supply_w_id"); + orderStatus.ol_quantity[ol_idx] = rs.getInt("ol_quantity"); + orderStatus.ol_amount[ol_idx] = rs.getDouble("ol_amount"); + ol_delivery_d = rs.getTimestamp("ol_delivery_d"); + if (ol_delivery_d != null) + orderStatus.ol_delivery_d[ol_idx] = ol_delivery_d.toString(); + else + orderStatus.ol_delivery_d[ol_idx] = null; + ol_idx++; + } + rs.close(); + + while (ol_idx < 15) { + orderStatus.ol_i_id[ol_idx] = 0; + orderStatus.ol_supply_w_id[ol_idx] = 0; + orderStatus.ol_quantity[ol_idx] = 0; + orderStatus.ol_amount[ol_idx] = 0.0; + orderStatus.ol_delivery_d[ol_idx] = null; + ol_idx++; + } + + db.rollback(); + } catch (SQLException se) { + log.error("Unexpected SQLException in ORDER_STATUS"); + for (SQLException x = se; x != null; x = x.getNextException()) + log.error(x.getMessage()); + se.printStackTrace(); + + try { + db.rollback(); + } catch (SQLException se2) { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } + } catch (Exception e) { + try { + db.rollback(); + } catch (SQLException se2) { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } + throw e; + } } - private void traceOrderStatus(Logger log, Formatter fmt[]) - { - fmt[0].format(" Order Status"); - - if (transEnd == 0) - { - // ORDER_STATUS INPUT screen - fmt[1].format("Warehouse: %6d District: %2d", - orderStatus.w_id, orderStatus.d_id); - if (orderStatus.c_last == null) - fmt[2].format("Customer: %4d Name: ________________", - orderStatus.c_id); - else - fmt[2].format("Customer: ____ Name: %-16.16s", - orderStatus.c_last); - fmt[3].format("Cust-Balance:"); - - fmt[5].format("Order-Number: Entry-Date: Carrier-Number:"); - fmt[6].format("Suppy-W Item-Id Qty Amount Delivery-Date"); - } - else - { - // ORDER_STATUS OUTPUT screen - fmt[1].format("Warehouse: %6d District: %2d", - orderStatus.w_id, orderStatus.d_id); - fmt[2].format("Customer: %4d Name: %-16.16s %2.2s %-16.16s", - orderStatus.c_id, orderStatus.c_first, - orderStatus.c_middle, orderStatus.c_last); - fmt[3].format("Cust-Balance: $%13.2f", orderStatus.c_balance); - - if (orderStatus.o_carrier_id >= 0) - fmt[5].format("Order-Number: %8d Entry-Date: %-19.19s Carrier-Number: %2d", - orderStatus.o_id, orderStatus.o_entry_d, orderStatus.o_carrier_id); - else - fmt[5].format("Order-Number: %8d Entry-Date: %-19.19s Carrier-Number:", - orderStatus.o_id, orderStatus.o_entry_d); - fmt[6].format("Suppy-W Item-Id Qty Amount Delivery-Date"); - for (int i = 0; i < 15 && orderStatus.ol_i_id[i] > 0; i++) - { - fmt[7 + i].format(" %6d %6d %3d $%8.2f %-10.10s", - orderStatus.ol_supply_w_id[i], - orderStatus.ol_i_id[i], - orderStatus.ol_quantity[i], - orderStatus.ol_amount[i], - (orderStatus.ol_delivery_d[i] == null) ? "" : - orderStatus.ol_delivery_d[i]); - } - } + private void traceOrderStatus(Logger log, Formatter fmt[]) { + fmt[0].format(" Order Status"); + + if (transEnd == 0) { + // ORDER_STATUS INPUT screen + fmt[1].format("Warehouse: %6d District: %2d", + orderStatus.w_id, orderStatus.d_id); + if (orderStatus.c_last == null) + fmt[2].format("Customer: %4d Name: ________________", + orderStatus.c_id); + else + fmt[2].format("Customer: ____ Name: %-16.16s", + orderStatus.c_last); + fmt[3].format("Cust-Balance:"); + + fmt[5].format("Order-Number: Entry-Date: Carrier-Number:"); + fmt[6].format("Suppy-W Item-Id Qty Amount Delivery-Date"); + } else { + // ORDER_STATUS OUTPUT screen + fmt[1].format("Warehouse: %6d District: %2d", + orderStatus.w_id, orderStatus.d_id); + fmt[2].format("Customer: %4d Name: %-16.16s %2.2s %-16.16s", + orderStatus.c_id, orderStatus.c_first, + orderStatus.c_middle, orderStatus.c_last); + fmt[3].format("Cust-Balance: $%13.2f", orderStatus.c_balance); + + if (orderStatus.o_carrier_id >= 0) + fmt[5].format("Order-Number: %8d Entry-Date: %-19.19s Carrier-Number: %2d", + orderStatus.o_id, orderStatus.o_entry_d, orderStatus.o_carrier_id); + else + fmt[5].format("Order-Number: %8d Entry-Date: %-19.19s Carrier-Number:", + orderStatus.o_id, orderStatus.o_entry_d); + fmt[6].format("Suppy-W Item-Id Qty Amount Delivery-Date"); + for (int i = 0; i < 15 && orderStatus.ol_i_id[i] > 0; i++) { + fmt[7 + i].format(" %6d %6d %3d $%8.2f %-10.10s", + orderStatus.ol_supply_w_id[i], + orderStatus.ol_i_id[i], + orderStatus.ol_quantity[i], + orderStatus.ol_amount[i], + (orderStatus.ol_delivery_d[i] == null) ? "" : + orderStatus.ol_delivery_d[i]); + } + } } - private class OrderStatusData - { - /* terminal input data */ - public int w_id; - public int d_id; - public int c_id; - public String c_last; - - /* terminal output data */ - public String c_first; - public String c_middle; - public double c_balance; - public int o_id; - public String o_entry_d; - public int o_carrier_id; - - public int ol_supply_w_id[] = new int[15]; - public int ol_i_id[] = new int[15]; - public int ol_quantity[] = new int[15]; - public double ol_amount[] = new double[15]; - public String ol_delivery_d[] = new String[15]; + private class OrderStatusData { + /* terminal input data */ + public int w_id; + public int d_id; + public int c_id; + public String c_last; + + /* terminal output data */ + public String c_first; + public String c_middle; + public double c_balance; + public int o_id; + public String o_entry_d; + public int o_carrier_id; + + public int ol_supply_w_id[] = new int[15]; + public int ol_i_id[] = new int[15]; + public int ol_quantity[] = new int[15]; + public double ol_amount[] = new double[15]; + public String ol_delivery_d[] = new String[15]; } /* ********************************************************************** @@ -1340,109 +1233,93 @@ private class OrderStatusData * ***** STOCK_LEVEL related methods and subclass. ********************** * ********************************************************************** * *********************************************************************/ - public void generateStockLevel(Logger log, jTPCCRandom rnd, long due) - { - transType = TT_STOCK_LEVEL; - transDue = due; - transStart = 0; - transEnd = 0; - transRbk = false; - transError = null; - - newOrder = null; - payment = null; - orderStatus = null; - stockLevel = new StockLevelData(); - delivery = null; - deliveryBG = null; - - stockLevel.w_id = terminalWarehouse; - stockLevel.d_id = terminalDistrict; - stockLevel.threshold = rnd.nextInt(10, 20); + public void generateStockLevel(Logger log, jTPCCRandom rnd, long due) { + transType = TT_STOCK_LEVEL; + transDue = due; + transStart = 0; + transEnd = 0; + transRbk = false; + transError = null; + + newOrder = null; + payment = null; + orderStatus = null; + stockLevel = new StockLevelData(); + delivery = null; + deliveryBG = null; + + stockLevel.w_id = terminalWarehouse; + stockLevel.d_id = terminalDistrict; + stockLevel.threshold = rnd.nextInt(10, 20); } private void executeStockLevel(Logger log, jTPCCConnection db) - throws Exception - { - PreparedStatement stmt; - ResultSet rs; - - try - { - stmt = db.stmtStockLevelSelectLow; - stmt.setInt(1, stockLevel.w_id); - stmt.setInt(2, stockLevel.threshold); - stmt.setInt(3, stockLevel.w_id); - stmt.setInt(4, stockLevel.d_id); - rs = stmt.executeQuery(); - if (!rs.next()) - { - throw new Exception("Failed to get low-stock for" + - " W_ID=" + stockLevel.w_id + - " D_ID=" + stockLevel.d_id); - } - stockLevel.low_stock = rs.getInt("low_stock"); - rs.close(); - - db.rollback(); - } - catch (SQLException se) - { - log.error("Unexpected SQLException in STOCK_LEVEL"); - for (SQLException x = se; x != null; x = x.getNextException()) - log.error(x.getMessage()); - se.printStackTrace(); - - try - { - db.rollback(); - } - catch (SQLException se2) - { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); - } - } - catch (Exception e) - { - try - { - db.rollback(); - } - catch (SQLException se2) - { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); - } - throw e; - } + throws Exception { + PreparedStatement stmt; + ResultSet rs; + + try { + stmt = db.stmtStockLevelSelectLow; + stmt.setInt(1, stockLevel.w_id); + stmt.setInt(2, stockLevel.threshold); + stmt.setInt(3, stockLevel.w_id); + stmt.setInt(4, stockLevel.d_id); + rs = stmt.executeQuery(); + if (!rs.next()) { + throw new Exception("Failed to get low-stock for" + + " W_ID=" + stockLevel.w_id + + " D_ID=" + stockLevel.d_id); + } + stockLevel.low_stock = rs.getInt("low_stock"); + rs.close(); + + db.rollback(); + } catch (SQLException se) { + log.error("Unexpected SQLException in STOCK_LEVEL"); + for (SQLException x = se; x != null; x = x.getNextException()) + log.error(x.getMessage()); + se.printStackTrace(); + + try { + db.rollback(); + } catch (SQLException se2) { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } + } catch (Exception e) { + try { + db.rollback(); + } catch (SQLException se2) { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } + throw e; + } } - private void traceStockLevel(Logger log, Formatter fmt[]) - { - fmt[0].format(" Stock-Level"); + private void traceStockLevel(Logger log, Formatter fmt[]) { + fmt[0].format(" Stock-Level"); - fmt[1].format("Warehouse: %6d District: %2d", - stockLevel.w_id, stockLevel.d_id); - fmt[3].format("Stock Level Threshold: %2d", - stockLevel.threshold); + fmt[1].format("Warehouse: %6d District: %2d", + stockLevel.w_id, stockLevel.d_id); + fmt[3].format("Stock Level Threshold: %2d", + stockLevel.threshold); - if (transEnd == 0) - fmt[5].format("Low Stock:"); - else - fmt[5].format("Low Stock: %3d", - stockLevel.low_stock); + if (transEnd == 0) + fmt[5].format("Low Stock:"); + else + fmt[5].format("Low Stock: %3d", + stockLevel.low_stock); } - private class StockLevelData - { - /* terminal input data */ - public int w_id; - public int d_id; - public int threshold; + private class StockLevelData { + /* terminal input data */ + public int w_id; + public int d_id; + public int threshold; - /* terminal output data */ - public int low_stock; + /* terminal output data */ + public int low_stock; } /* ********************************************************************** @@ -1450,89 +1327,81 @@ private class StockLevelData * ***** DELIVERY related methods and subclass. ************************* * ********************************************************************** * *********************************************************************/ - public void generateDelivery(Logger log, jTPCCRandom rnd, long due) - { - transType = TT_DELIVERY; - transDue = due; - transStart = 0; - transEnd = 0; - transRbk = false; - transError = null; - - newOrder = null; - payment = null; - orderStatus = null; - stockLevel = null; - delivery = new DeliveryData(); - deliveryBG = null; - - delivery.w_id = terminalWarehouse; - delivery.o_carrier_id = rnd.nextInt(1, 10); - delivery.execution_status = null; - delivery.deliveryBG = null; + public void generateDelivery(Logger log, jTPCCRandom rnd, long due) { + transType = TT_DELIVERY; + transDue = due; + transStart = 0; + transEnd = 0; + transRbk = false; + transError = null; + + newOrder = null; + payment = null; + orderStatus = null; + stockLevel = null; + delivery = new DeliveryData(); + deliveryBG = null; + + delivery.w_id = terminalWarehouse; + delivery.o_carrier_id = rnd.nextInt(1, 10); + delivery.execution_status = null; + delivery.deliveryBG = null; } - private void executeDelivery(Logger log, jTPCCConnection db) - { - long now = System.currentTimeMillis(); - - /* - * The DELIVERY transaction is different from all the others. - * The foreground transaction, experienced by the user, does - * not perform any interaction with the database. It only queues - * a request to perform such a transaction in the background - * (DeliveryBG). We store that TData object in the delivery - * part for the caller to pick up and queue/execute. - */ - delivery.deliveryBG = new jTPCCTData(); - delivery.deliveryBG.generateDeliveryBG(delivery.w_id, now, - new java.sql.Timestamp(now).toString(), this); - delivery.execution_status = new String("Delivery has been queued"); + private void executeDelivery(Logger log, jTPCCConnection db) { + long now = System.currentTimeMillis(); + + /* + * The DELIVERY transaction is different from all the others. + * The foreground transaction, experienced by the user, does + * not perform any interaction with the database. It only queues + * a request to perform such a transaction in the background + * (DeliveryBG). We store that TData object in the delivery + * part for the caller to pick up and queue/execute. + */ + delivery.deliveryBG = new jTPCCTData(); + delivery.deliveryBG.generateDeliveryBG(delivery.w_id, now, + new java.sql.Timestamp(now).toString(), this); + delivery.execution_status = new String("Delivery has been queued"); } - private void traceDelivery(Logger log, Formatter fmt[]) - { - fmt[0].format(" Delivery"); - fmt[1].format("Warehouse: %6d", delivery.w_id); - fmt[3].format("Carrier Number: %2d", delivery.o_carrier_id); - if (transEnd == 0) - { - fmt[5].format("Execution Status: "); - } - else - { - fmt[5].format("Execution Status: %s", delivery.execution_status); - } + private void traceDelivery(Logger log, Formatter fmt[]) { + fmt[0].format(" Delivery"); + fmt[1].format("Warehouse: %6d", delivery.w_id); + fmt[3].format("Carrier Number: %2d", delivery.o_carrier_id); + if (transEnd == 0) { + fmt[5].format("Execution Status: "); + } else { + fmt[5].format("Execution Status: %s", delivery.execution_status); + } } public jTPCCTData getDeliveryBG() - throws Exception - { - if (transType != TT_DELIVERY) - throw new Exception("Not a DELIVERY"); - if (delivery.deliveryBG == null) - throw new Exception("DELIVERY foreground not executed yet " + - "or background part already consumed"); - - jTPCCTData result = delivery.deliveryBG; - delivery.deliveryBG = null; - return result; + throws Exception { + if (transType != TT_DELIVERY) + throw new Exception("Not a DELIVERY"); + if (delivery.deliveryBG == null) + throw new Exception("DELIVERY foreground not executed yet " + + "or background part already consumed"); + + jTPCCTData result = delivery.deliveryBG; + delivery.deliveryBG = null; + return result; } - private class DeliveryData - { - /* terminal input data */ - public int w_id; - public int o_carrier_id; + private class DeliveryData { + /* terminal input data */ + public int w_id; + public int o_carrier_id; - /* terminal output data */ - public String execution_status; + /* terminal output data */ + public String execution_status; - /* - * executeDelivery() will store the background request - * here for the caller to pick up and process as needed. - */ - public jTPCCTData deliveryBG; + /* + * executeDelivery() will store the background request + * here for the caller to pick up and process as needed. + */ + public jTPCCTData deliveryBG; } /* ********************************************************************** @@ -1541,270 +1410,249 @@ private class DeliveryData * ********************************************************************** * *********************************************************************/ private void generateDeliveryBG(int w_id, long due, String ol_delivery_d, - jTPCCTData parent) { - /* - * The DELIVERY_BG part is created as a result of executing the - * foreground part of the DELIVERY transaction. Because of that - * it inherits certain information from it. - */ - numWarehouses = parent.numWarehouses; - terminalWarehouse = parent.terminalWarehouse; - terminalDistrict = parent.terminalDistrict; - - transType = TT_DELIVERY_BG; - transDue = due; - transStart = 0; - transEnd = 0; - transRbk = false; - transError = null; - - newOrder = null; - payment = null; - orderStatus = null; - stockLevel = null; - delivery = null; - deliveryBG = new DeliveryBGData(); - - deliveryBG.w_id = parent.delivery.w_id; - deliveryBG.o_carrier_id = parent.delivery.o_carrier_id; - deliveryBG.ol_delivery_d = ol_delivery_d; - - deliveryBG.delivered_o_id = new int[10]; - deliveryBG.delivered_c_id = new int[10]; - deliveryBG.sum_ol_amount = new double[10]; - for (int i = 0; i < 10; i++) { - deliveryBG.delivered_o_id[i] = -1; - deliveryBG.sum_ol_amount[i] = -1.0; - deliveryBG.delivered_c_id[i] = -1; - } - } + jTPCCTData parent) { + /* + * The DELIVERY_BG part is created as a result of executing the + * foreground part of the DELIVERY transaction. Because of that + * it inherits certain information from it. + */ + numWarehouses = parent.numWarehouses; + terminalWarehouse = parent.terminalWarehouse; + terminalDistrict = parent.terminalDistrict; + + transType = TT_DELIVERY_BG; + transDue = due; + transStart = 0; + transEnd = 0; + transRbk = false; + transError = null; + + newOrder = null; + payment = null; + orderStatus = null; + stockLevel = null; + delivery = null; + deliveryBG = new DeliveryBGData(); + + deliveryBG.w_id = parent.delivery.w_id; + deliveryBG.o_carrier_id = parent.delivery.o_carrier_id; + deliveryBG.ol_delivery_d = ol_delivery_d; + + deliveryBG.delivered_o_id = new int[10]; + deliveryBG.delivered_c_id = new int[10]; + deliveryBG.sum_ol_amount = new double[10]; + for (int i = 0; i < 10; i++) { + deliveryBG.delivered_o_id[i] = -1; + deliveryBG.sum_ol_amount[i] = -1.0; + deliveryBG.delivered_c_id[i] = -1; + } + } private void executeDeliveryBG(Logger log, jTPCCConnection db) - throws Exception - { - PreparedStatement stmt1; - PreparedStatement stmt2; - ResultSet rs; - int rc; - int d_id; - int o_id; - int c_id; - double sum_ol_amount; - long now = System.currentTimeMillis(); - - try - { - for (d_id = 1; d_id <= 10; d_id++) { - o_id = -1; - - stmt1 = db.stmtDeliveryBGSelectOldestNewOrder; - - /* - * Try to find the oldest undelivered order for this - * DISTRICT. There may not be one, which is a case - * that needs to be reportd. - */ - while (o_id < 0) { - stmt1.setInt(1, deliveryBG.w_id); - stmt1.setInt(2, d_id); - rs = stmt1.executeQuery(); - if (!rs.next()) { - rs.close(); - break; - } - o_id = rs.getInt("no_o_id"); - rs.close(); - /* - * This logic only works in SNAPSHOT isolation - * level. Because we select new_order for update, - * the order must be not selected by other termnial - * as long as this SQL statement return it. - */ - } - - if (o_id < 0) { - // No undelivered NEW_ORDER found for this DISTRICT. - continue; - } - deliveryBG.delivered_o_id[d_id - 1] = o_id; - } - stmt1 = db.stmtDeliveryBGDeleteOldestNewOrder; - for (d_id = 1; d_id <= 10; d_id++) { - stmt1.setInt(d_id * 3 - 2, deliveryBG.w_id); - stmt1.setInt(d_id * 3 - 1, d_id); - stmt1.setInt(d_id * 3, deliveryBG.delivered_o_id[d_id - 1]); - } - stmt1.executeUpdate(); - - /* - * We found out oldest undelivered order for this DISTRICT - * and the NEW_ORDER line has been deleted. Process the - * rest of the DELIVERY_BG. - */ - - // Update the ORDER setting the o_carrier_id. - stmt1 = db.stmtDeliveryBGUpdateOrder; - stmt1.setInt(1, deliveryBG.o_carrier_id); - for (d_id = 1; d_id <= 10; d_id++) { - stmt1.setInt(d_id * 3 -1, deliveryBG.w_id); - stmt1.setInt(d_id * 3, d_id); - stmt1.setInt(d_id * 3 + 1, deliveryBG.delivered_o_id[d_id - 1]); - } - - stmt1.executeUpdate(); - - // Get the o_c_id from the ORDER. - stmt1 = db.stmtDeliveryBGSelectOrder; - for (d_id = 1; d_id <= 10; d_id++) { - stmt1.setInt(d_id * 3 - 2, deliveryBG.w_id); - stmt1.setInt(d_id * 3 - 1, d_id); - stmt1.setInt(d_id * 3, deliveryBG.delivered_o_id[d_id - 1]); - } - rs = stmt1.executeQuery(); - while (rs.next()) - { - d_id = rs.getInt("o_d_id"); - c_id = rs.getInt("o_c_id"); - deliveryBG.delivered_c_id[d_id - 1] = c_id; - } - rs.close(); - for (d_id = 1; d_id <= 10; d_id++) { - o_id = deliveryBG.delivered_o_id[d_id - 1]; - if (o_id >= 0 && deliveryBG.delivered_c_id[d_id - 1] < 0) { - throw new Exception("ORDER in DELIVERY_BG for" + - " O_W_ID=" + deliveryBG.w_id + - " O_D_ID=" + d_id + - " O_ID=" + o_id + " not found"); - } - } - - // Update ORDER_LINE setting the ol_delivery_d. - stmt1 = db.stmtDeliveryBGUpdateOrderLine; - stmt1.setTimestamp(1, new java.sql.Timestamp(now)); - for (d_id = 1; d_id <= 10; d_id++) { - stmt1.setInt(d_id * 3 - 1, deliveryBG.w_id); - stmt1.setInt(d_id * 3, d_id); - stmt1.setInt(d_id * 3 + 1, deliveryBG.delivered_o_id[d_id - 1]); - } - stmt1.executeUpdate(); - - // Select the sum(ol_amount) from ORDER_LINE. - - stmt1 = db.stmtDeliveryBGSelectSumOLAmount; - for (d_id = 1; d_id <= 10; d_id++) { - stmt1.setInt(d_id * 3 - 2, deliveryBG.w_id); - stmt1.setInt(d_id * 3 - 1, d_id); - stmt1.setInt(d_id * 3, deliveryBG.delivered_o_id[d_id - 1]); - } - rs = stmt1.executeQuery(); - - while (rs.next()) - { - d_id = rs.getInt("ol_d_id"); - deliveryBG.sum_ol_amount[d_id - 1] = rs.getDouble("sum_ol_amount"); - } - rs.close(); - - // Update the CUSTOMER. - for (d_id = 1; d_id <= 10; d_id ++) { - o_id = deliveryBG.delivered_o_id[d_id - 1]; - if (o_id < 0) { - continue; - } - double ans = deliveryBG.sum_ol_amount[d_id - 1]; - if (ans < 0) { - throw new Exception("sum(OL_AMOUNT) for ORDER_LINEs with " + - " OL_W_ID=" + deliveryBG.w_id + - " OL_D_ID=" + d_id + - " OL_O_ID=" + o_id + " not found"); - } - c_id = deliveryBG.delivered_c_id[d_id - 1]; - stmt1 = db.stmtDeliveryBGUpdateCustomer; - stmt1.setDouble(1, ans); - stmt1.setInt(2, deliveryBG.w_id); - stmt1.setInt(3, d_id); - stmt1.setInt(4, c_id); - stmt1.executeUpdate(); - // Recored the delivered O_ID in the DELIVERY_BG - } - db.commit(); - } - catch (SQLException se) - { - log.error("Unexpected SQLException in DELIVERY_BG"); - for (SQLException x = se; x != null; x = x.getNextException()) - log.error(x.getMessage()); - se.printStackTrace(); - - try - { - db.rollback(); - } - catch (SQLException se2) - { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); - } - } - catch (Exception e) - { - try - { - db.rollback(); - } - catch (SQLException se2) - { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); - } - throw e; - } + throws Exception { + PreparedStatement stmt1; + PreparedStatement stmt2; + ResultSet rs; + int rc; + int d_id; + int o_id; + int c_id; + double sum_ol_amount; + long now = System.currentTimeMillis(); + + try { + for (d_id = 1; d_id <= 10; d_id++) { + o_id = -1; + + stmt1 = db.stmtDeliveryBGSelectOldestNewOrder; + + /* + * Try to find the oldest undelivered order for this + * DISTRICT. There may not be one, which is a case + * that needs to be reportd. + */ + while (o_id < 0) { + stmt1.setInt(1, deliveryBG.w_id); + stmt1.setInt(2, d_id); + rs = stmt1.executeQuery(); + if (!rs.next()) { + rs.close(); + break; + } + o_id = rs.getInt("no_o_id"); + rs.close(); + /* + * This logic only works in SNAPSHOT isolation + * level. Because we select new_order for update, + * the order must be not selected by other termnial + * as long as this SQL statement return it. + */ + } + + if (o_id < 0) { + // No undelivered NEW_ORDER found for this DISTRICT. + continue; + } + deliveryBG.delivered_o_id[d_id - 1] = o_id; + } + stmt1 = db.stmtDeliveryBGDeleteOldestNewOrder; + for (d_id = 1; d_id <= 10; d_id++) { + stmt1.setInt(d_id * 3 - 2, deliveryBG.w_id); + stmt1.setInt(d_id * 3 - 1, d_id); + stmt1.setInt(d_id * 3, deliveryBG.delivered_o_id[d_id - 1]); + } + stmt1.executeUpdate(); + + /* + * We found out oldest undelivered order for this DISTRICT + * and the NEW_ORDER line has been deleted. Process the + * rest of the DELIVERY_BG. + */ + + // Update the ORDER setting the o_carrier_id. + stmt1 = db.stmtDeliveryBGUpdateOrder; + stmt1.setInt(1, deliveryBG.o_carrier_id); + for (d_id = 1; d_id <= 10; d_id++) { + stmt1.setInt(d_id * 3 - 1, deliveryBG.w_id); + stmt1.setInt(d_id * 3, d_id); + stmt1.setInt(d_id * 3 + 1, deliveryBG.delivered_o_id[d_id - 1]); + } + + stmt1.executeUpdate(); + + // Get the o_c_id from the ORDER. + stmt1 = db.stmtDeliveryBGSelectOrder; + for (d_id = 1; d_id <= 10; d_id++) { + stmt1.setInt(d_id * 3 - 2, deliveryBG.w_id); + stmt1.setInt(d_id * 3 - 1, d_id); + stmt1.setInt(d_id * 3, deliveryBG.delivered_o_id[d_id - 1]); + } + rs = stmt1.executeQuery(); + while (rs.next()) { + d_id = rs.getInt("o_d_id"); + c_id = rs.getInt("o_c_id"); + deliveryBG.delivered_c_id[d_id - 1] = c_id; + } + rs.close(); + for (d_id = 1; d_id <= 10; d_id++) { + o_id = deliveryBG.delivered_o_id[d_id - 1]; + if (o_id >= 0 && deliveryBG.delivered_c_id[d_id - 1] < 0) { + throw new Exception("ORDER in DELIVERY_BG for" + + " O_W_ID=" + deliveryBG.w_id + + " O_D_ID=" + d_id + + " O_ID=" + o_id + " not found"); + } + } + + // Update ORDER_LINE setting the ol_delivery_d. + stmt1 = db.stmtDeliveryBGUpdateOrderLine; + stmt1.setTimestamp(1, new java.sql.Timestamp(now)); + for (d_id = 1; d_id <= 10; d_id++) { + stmt1.setInt(d_id * 3 - 1, deliveryBG.w_id); + stmt1.setInt(d_id * 3, d_id); + stmt1.setInt(d_id * 3 + 1, deliveryBG.delivered_o_id[d_id - 1]); + } + stmt1.executeUpdate(); + + // Select the sum(ol_amount) from ORDER_LINE. + + stmt1 = db.stmtDeliveryBGSelectSumOLAmount; + for (d_id = 1; d_id <= 10; d_id++) { + stmt1.setInt(d_id * 3 - 2, deliveryBG.w_id); + stmt1.setInt(d_id * 3 - 1, d_id); + stmt1.setInt(d_id * 3, deliveryBG.delivered_o_id[d_id - 1]); + } + rs = stmt1.executeQuery(); + + while (rs.next()) { + d_id = rs.getInt("ol_d_id"); + deliveryBG.sum_ol_amount[d_id - 1] = rs.getDouble("sum_ol_amount"); + } + rs.close(); + + // Update the CUSTOMER. + for (d_id = 1; d_id <= 10; d_id++) { + o_id = deliveryBG.delivered_o_id[d_id - 1]; + if (o_id < 0) { + continue; + } + double ans = deliveryBG.sum_ol_amount[d_id - 1]; + if (ans < 0) { + throw new Exception("sum(OL_AMOUNT) for ORDER_LINEs with " + + " OL_W_ID=" + deliveryBG.w_id + + " OL_D_ID=" + d_id + + " OL_O_ID=" + o_id + " not found"); + } + c_id = deliveryBG.delivered_c_id[d_id - 1]; + stmt1 = db.stmtDeliveryBGUpdateCustomer; + stmt1.setDouble(1, ans); + stmt1.setInt(2, deliveryBG.w_id); + stmt1.setInt(3, d_id); + stmt1.setInt(4, c_id); + stmt1.executeUpdate(); + // Recored the delivered O_ID in the DELIVERY_BG + } + db.commit(); + } catch (SQLException se) { + log.error("Unexpected SQLException in DELIVERY_BG"); + for (SQLException x = se; x != null; x = x.getNextException()) + log.error(x.getMessage()); + se.printStackTrace(); + + try { + db.rollback(); + } catch (SQLException se2) { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } + } catch (Exception e) { + try { + db.rollback(); + } catch (SQLException se2) { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } + throw e; + } } - private void traceDeliveryBG(Logger log, Formatter fmt[]) - { - fmt[0].format(" DeliveryBG"); - fmt[1].format("Warehouse: %6d", deliveryBG.w_id); - fmt[2].format("Carrier Number: %2d", deliveryBG.o_carrier_id); - fmt[3].format("Delivery Date: %-19.19s", deliveryBG.ol_delivery_d); - - if (transEnd != 0) - { - for (int d_id = 1; d_id <= 10; d_id++) - { - fmt[4 + d_id].format("District %02d: delivered O_ID: %8d", - d_id, deliveryBG.delivered_o_id[d_id - 1]); - } - } + private void traceDeliveryBG(Logger log, Formatter fmt[]) { + fmt[0].format(" DeliveryBG"); + fmt[1].format("Warehouse: %6d", deliveryBG.w_id); + fmt[2].format("Carrier Number: %2d", deliveryBG.o_carrier_id); + fmt[3].format("Delivery Date: %-19.19s", deliveryBG.ol_delivery_d); + + if (transEnd != 0) { + for (int d_id = 1; d_id <= 10; d_id++) { + fmt[4 + d_id].format("District %02d: delivered O_ID: %8d", + d_id, deliveryBG.delivered_o_id[d_id - 1]); + } + } } - public int[] getDeliveredOrderIDs() - { - return deliveryBG.delivered_o_id; + public int[] getDeliveredOrderIDs() { + return deliveryBG.delivered_o_id; } - public int getSkippedDeliveries() - { - int numSkipped = 0; + public int getSkippedDeliveries() { + int numSkipped = 0; - for (int i = 0; i < 10; i++) - { - if (deliveryBG.delivered_o_id[i] < 0) - numSkipped++; - } + for (int i = 0; i < 10; i++) { + if (deliveryBG.delivered_o_id[i] < 0) + numSkipped++; + } - return numSkipped; + return numSkipped; } - private class DeliveryBGData - { - /* DELIVERY_BG data */ - public int w_id; - public int o_carrier_id; - public String ol_delivery_d; + private class DeliveryBGData { + /* DELIVERY_BG data */ + public int w_id; + public int o_carrier_id; + public String ol_delivery_d; - public int delivered_o_id[]; - public int delivered_c_id[]; - public double sum_ol_amount[]; + public int delivered_o_id[]; + public int delivered_c_id[]; + public double sum_ol_amount[]; } } diff --git a/src/client/jTPCCTerminal.java b/src/client/jTPCCTerminal.java index e75e4c7..8d0778f 100644 --- a/src/client/jTPCCTerminal.java +++ b/src/client/jTPCCTerminal.java @@ -6,18 +6,18 @@ * Copyright (C) 2016, Jan Wieck * */ -import org.apache.log4j.*; -import java.io.*; -import java.sql.*; -import java.sql.Date; -import java.util.*; -import javax.swing.*; +import org.apache.log4j.Logger; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; -public class jTPCCTerminal implements jTPCCConfig, Runnable -{ - private static org.apache.log4j.Logger log = Logger.getLogger(jTPCCTerminal.class); +public class jTPCCTerminal implements jTPCCConfig, Runnable { + private static Logger log = Logger.getLogger(jTPCCTerminal.class); private String terminalName; private Connection conn = null; @@ -42,330 +42,286 @@ public class jTPCCTerminal implements jTPCCConfig, Runnable long terminalStartTime = 0; long transactionEnd = 0; - jTPCCConnection db = null; - int dbType = 0; + jTPCCConnection db = null; + int dbType = 0; public jTPCCTerminal - (String terminalName, int terminalWarehouseID, int terminalDistrictID, - Connection conn, int dbType, - int numTransactions, boolean terminalWarehouseFixed, - int paymentWeight, int orderStatusWeight, - int deliveryWeight, int stockLevelWeight, int numWarehouses, int limPerMin_Terminal, jTPCC parent) throws SQLException - { - this.terminalName = terminalName; - this.conn = conn; - this.dbType = dbType; - this.stmt = conn.createStatement(); - this.stmt.setMaxRows(200); - this.stmt.setFetchSize(100); - - this.stmt1 = conn.createStatement(); - this.stmt1.setMaxRows(1); - - this.terminalWarehouseID = terminalWarehouseID; - this.terminalDistrictID = terminalDistrictID; - this.terminalWarehouseFixed = terminalWarehouseFixed; - this.parent = parent; - this.rnd = parent.getRnd().newRandom(); - this.numTransactions = numTransactions; - this.paymentWeight = paymentWeight; - this.orderStatusWeight = orderStatusWeight; - this.deliveryWeight = deliveryWeight; - this.stockLevelWeight = stockLevelWeight; - this.numWarehouses = numWarehouses; - this.newOrderCounter = 0; - this.limPerMin_Terminal = limPerMin_Terminal; - - this.db = new jTPCCConnection(conn, dbType); - - terminalMessage(""); - terminalMessage("Terminal \'" + terminalName + "\' has WarehouseID=" + terminalWarehouseID + " and DistrictID=" + terminalDistrictID + "."); - terminalStartTime = System.currentTimeMillis(); + (String terminalName, int terminalWarehouseID, int terminalDistrictID, + Connection conn, int dbType, + int numTransactions, boolean terminalWarehouseFixed, + int paymentWeight, int orderStatusWeight, + int deliveryWeight, int stockLevelWeight, int numWarehouses, int limPerMin_Terminal, jTPCC parent) throws SQLException { + this.terminalName = terminalName; + this.conn = conn; + this.dbType = dbType; + this.stmt = conn.createStatement(); + this.stmt.setMaxRows(200); + this.stmt.setFetchSize(100); + + this.stmt1 = conn.createStatement(); + this.stmt1.setMaxRows(1); + + this.terminalWarehouseID = terminalWarehouseID; + this.terminalDistrictID = terminalDistrictID; + this.terminalWarehouseFixed = terminalWarehouseFixed; + this.parent = parent; + this.rnd = parent.getRnd().newRandom(); + this.numTransactions = numTransactions; + this.paymentWeight = paymentWeight; + this.orderStatusWeight = orderStatusWeight; + this.deliveryWeight = deliveryWeight; + this.stockLevelWeight = stockLevelWeight; + this.numWarehouses = numWarehouses; + this.newOrderCounter = 0; + this.limPerMin_Terminal = limPerMin_Terminal; + + this.db = new jTPCCConnection(conn, dbType); + + terminalMessage(""); + terminalMessage("Terminal \'" + terminalName + "\' has WarehouseID=" + + terminalWarehouseID + " and DistrictID=" + terminalDistrictID + "."); + terminalStartTime = System.currentTimeMillis(); } - public void run() - { - executeTransactions(numTransactions); - try - { - printMessage(""); - printMessage("Closing statement and connection..."); - - stmt.close(); - conn.close(); - } - catch(Exception e) - { - printMessage(""); - printMessage("An error occurred!"); - logException(e); - } - - printMessage(""); - printMessage("Terminal \'" + terminalName + "\' finished after " + (transactionCount-1) + " transaction(s)."); - - parent.signalTerminalEnded(this, newOrderCounter); + public void run() { + executeTransactions(numTransactions); + try { + printMessage(""); + printMessage("Closing statement and connection..."); + + stmt.close(); + conn.close(); + } catch (Exception e) { + printMessage(""); + printMessage("An error occurred!"); + logException(e); + } + + printMessage(""); + printMessage("Terminal \'" + terminalName + "\' finished after " + (transactionCount - 1) + " transaction(s)."); + + parent.signalTerminalEnded(this, newOrderCounter); } - public void stopRunningWhenPossible() - { - stopRunningSignal = true; - printMessage(""); - printMessage("Terminal received stop signal!"); - printMessage("Finishing current transaction before exit..."); + public void stopRunningWhenPossible() { + stopRunningSignal = true; + printMessage(""); + printMessage("Terminal received stop signal!"); + printMessage("Finishing current transaction before exit..."); } - private void executeTransactions(int numTransactions) - { - boolean stopRunning = false; - - if(numTransactions != -1) - printMessage("Executing " + numTransactions + " transactions..."); - else - printMessage("Executing for a limited time..."); - - for(int i = 0; (i < numTransactions || numTransactions == -1) && !stopRunning; i++) - { - - long transactionType = rnd.nextLong(1, 100); - int skippedDeliveries = 0, newOrder = 0; - String transactionTypeName; - - long transactionStart = System.currentTimeMillis(); - - /* - * TPC/C specifies that each terminal has a fixed - * "home" warehouse. However, since this implementation - * does not simulate "terminals", but rather simulates - * "application threads", that association is no longer - * valid. In the case of having less clients than - * warehouses (which should be the normal case), it - * leaves the warehouses without a client without any - * significant traffic, changing the overall database - * access pattern significantly. - */ - if(!terminalWarehouseFixed) - terminalWarehouseID = rnd.nextInt(1, numWarehouses); - - if(transactionType <= paymentWeight) - { - jTPCCTData term = new jTPCCTData(); - term.setNumWarehouses(numWarehouses); - term.setWarehouse(terminalWarehouseID); - term.setDistrict(terminalDistrictID); - try - { - term.generatePayment(log, rnd, 0); - term.traceScreen(log); - term.execute(log, db); - parent.resultAppend(term); - term.traceScreen(log); - } - catch (CommitException e) - { - continue; - } - catch (Exception e) - { - log.fatal(e.getMessage()); - e.printStackTrace(); - System.exit(4); - } - transactionTypeName = "Payment"; - } - else if(transactionType <= paymentWeight + stockLevelWeight) - { - jTPCCTData term = new jTPCCTData(); - term.setNumWarehouses(numWarehouses); - term.setWarehouse(terminalWarehouseID); - term.setDistrict(terminalDistrictID); - try - { - term.generateStockLevel(log, rnd, 0); - term.traceScreen(log); - term.execute(log, db); - parent.resultAppend(term); - term.traceScreen(log); - } - catch (CommitException e) - { - continue; - } - catch (Exception e) - { - log.fatal(e.getMessage()); - e.printStackTrace(); - System.exit(4); - } - transactionTypeName = "Stock-Level"; - } - else if(transactionType <= paymentWeight + stockLevelWeight + orderStatusWeight) - { - jTPCCTData term = new jTPCCTData(); - term.setNumWarehouses(numWarehouses); - term.setWarehouse(terminalWarehouseID); - term.setDistrict(terminalDistrictID); - try - { - term.generateOrderStatus(log, rnd, 0); - term.traceScreen(log); - term.execute(log, db); - parent.resultAppend(term); - term.traceScreen(log); - } - catch (CommitException e) - { - continue; - } - catch (Exception e) - { - log.fatal(e.getMessage()); - e.printStackTrace(); - System.exit(4); - } - transactionTypeName = "Order-Status"; - } - else if(transactionType <= paymentWeight + stockLevelWeight + orderStatusWeight + deliveryWeight) - { - jTPCCTData term = new jTPCCTData(); - term.setNumWarehouses(numWarehouses); - term.setWarehouse(terminalWarehouseID); - term.setDistrict(terminalDistrictID); - try - { - term.generateDelivery(log, rnd, 0); - term.traceScreen(log); - term.execute(log, db); - parent.resultAppend(term); - term.traceScreen(log); - - /* - * The old style driver does not have a delivery - * background queue, so we have to execute that - * part here as well. - */ - jTPCCTData bg = term.getDeliveryBG(); - bg.traceScreen(log); - bg.execute(log, db); - parent.resultAppend(bg); - bg.traceScreen(log); - - skippedDeliveries = bg.getSkippedDeliveries(); - } - catch (CommitException e) - { - continue; - } - catch (Exception e) - { - log.fatal(e.getMessage()); - e.printStackTrace(); - System.exit(4); - } - transactionTypeName = "Delivery"; - } - else - { - jTPCCTData term = new jTPCCTData(); - term.setNumWarehouses(numWarehouses); - term.setWarehouse(terminalWarehouseID); - term.setDistrict(terminalDistrictID); - try - { - term.generateNewOrder(log, rnd, 0); - term.traceScreen(log); - term.execute(log, db); - parent.resultAppend(term); - term.traceScreen(log); - } - catch (CommitException e) - { - continue; - } - catch (Exception e) - { - log.fatal(e.getMessage()); - e.printStackTrace(); - System.exit(4); - } - transactionTypeName = "New-Order"; - newOrderCounter++; - newOrder = 1; - } - - long transactionEnd = System.currentTimeMillis(); - - if(!transactionTypeName.equals("Delivery")) - { - parent.signalTerminalEndedTransaction(this.terminalName, transactionTypeName, transactionEnd - transactionStart, null, newOrder); - } - else - { - parent.signalTerminalEndedTransaction(this.terminalName, transactionTypeName, transactionEnd - transactionStart, (skippedDeliveries == 0 ? "None" : "" + skippedDeliveries + " delivery(ies) skipped."), newOrder); - } - - if(limPerMin_Terminal>0){ - long elapse = transactionEnd-transactionStart; - long timePerTx = 60000/limPerMin_Terminal; - - if(elapse 0) { + long elapse = transactionEnd - transactionStart; + long timePerTx = 60000 / limPerMin_Terminal; + + if (elapse < timePerTx) { + try { + long sleepTime = timePerTx - elapse; + Thread.sleep((sleepTime)); + } catch (Exception e) { + } + } + } + if (stopRunningSignal) stopRunning = true; + } } private void error(String type) { - log.error(terminalName + ", TERMINAL=" + terminalName + " TYPE=" + type + " COUNT=" + transactionCount); - System.out.println(terminalName + ", TERMINAL=" + terminalName + " TYPE=" + type + " COUNT=" + transactionCount); + log.error(terminalName + ", TERMINAL=" + terminalName + " TYPE=" + type + " COUNT=" + transactionCount); + System.out.println(terminalName + ", TERMINAL=" + terminalName + " TYPE=" + type + " COUNT=" + transactionCount); } - private void logException(Exception e) - { - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); - e.printStackTrace(printWriter); - printWriter.close(); - log.error(stringWriter.toString()); + private void logException(Exception e) { + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + e.printStackTrace(printWriter); + printWriter.close(); + log.error(stringWriter.toString()); } private void terminalMessage(String message) { - log.trace(terminalName + ", " + message); + log.trace(terminalName + ", " + message); } private void printMessage(String message) { - log.trace(terminalName + ", " + message); + log.trace(terminalName + ", " + message); } - void transRollback () { - try { - conn.rollback(); - } catch(SQLException se) { - log.error(se.getMessage()); - } + void transRollback() { + try { + conn.rollback(); + } catch (SQLException se) { + log.error(se.getMessage()); + } } void transCommit() { - try { - conn.commit(); - } catch(SQLException se) { - log.error(se.getMessage()); - transRollback(); - } + try { + conn.commit(); + } catch (SQLException se) { + log.error(se.getMessage()); + transRollback(); + } } // end transCommit() - } diff --git a/src/client/jTPCCUtil.java b/src/client/jTPCCUtil.java index d43716e..d8d998f 100644 --- a/src/client/jTPCCUtil.java +++ b/src/client/jTPCCUtil.java @@ -9,83 +9,79 @@ */ -import java.io.*; -import java.sql.*; -import java.util.*; -import java.text.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.Properties; -public class jTPCCUtil implements jTPCCConfig -{ - private static Connection dbConn = null; - private static PreparedStatement stmtGetConfig = null; +public class jTPCCUtil implements jTPCCConfig { + private static Connection dbConn = null; + private static PreparedStatement stmtGetConfig = null; - public static String getSysProp(String inSysProperty, String defaultValue) { + public static String getSysProp(String inSysProperty, String defaultValue) { - String outPropertyValue = null; + String outPropertyValue = null; - try { - outPropertyValue = System.getProperty(inSysProperty, defaultValue); - } catch (Exception e) { - System.err.println("Error Reading Required System Property '" + inSysProperty + "'"); - } + try { + outPropertyValue = System.getProperty(inSysProperty, defaultValue); + } catch (Exception e) { + System.err.println("Error Reading Required System Property '" + inSysProperty + "'"); + } - return(outPropertyValue); + return (outPropertyValue); - } // end getSysProp + } // end getSysProp - public static String randomStr(long strLen){ + public static String randomStr(long strLen) { - char freshChar; - String freshString; - freshString=""; + char freshChar; + String freshString; + freshString = ""; - while(freshString.length() < (strLen - 1)){ + while (freshString.length() < (strLen - 1)) { - freshChar= (char)(Math.random()*128); - if(Character.isLetter(freshChar)){ - freshString += freshChar; - } - } + freshChar = (char) (Math.random() * 128); + if (Character.isLetter(freshChar)) { + freshString += freshChar; + } + } - return (freshString); + return (freshString); - } // end randomStr + } // end randomStr - public static String getCurrentTime() - { - return dateFormat.format(new java.util.Date()); + public static String getCurrentTime() { + return dateFormat.format(new java.util.Date()); } - public static String formattedDouble(double d) - { - String dS = ""+d; - return dS.length() > 6 ? dS.substring(0, 6) : dS; + public static String formattedDouble(double d) { + String dS = "" + d; + return dS.length() > 6 ? dS.substring(0, 6) : dS; } public static String getConfig(String db, Properties dbProps, String option) - throws Exception - { - ResultSet rs; - String value; - - if (dbConn == null) - { - dbConn = DriverManager.getConnection(db, dbProps); - stmtGetConfig = dbConn.prepareStatement( - "SELECT cfg_value FROM bmsql_config " + - " WHERE cfg_name = ?"); - } - stmtGetConfig.setString(1, option); - rs = stmtGetConfig.executeQuery(); - if (!rs.next()) - throw new Exception("DB Load configuration parameter '" + - option + "' not found"); - value = rs.getString("cfg_value"); - rs.close(); - - return value; + throws Exception { + ResultSet rs; + String value; + + if (dbConn == null) { + dbConn = DriverManager.getConnection(db, dbProps); + stmtGetConfig = dbConn.prepareStatement( + "SELECT cfg_value FROM bmsql_config " + + " WHERE cfg_name = ?"); + } + stmtGetConfig.setString(1, option); + rs = stmtGetConfig.executeQuery(); + if (!rs.next()) + throw new Exception("DB Load configuration parameter '" + + option + "' not found"); + value = rs.getString("cfg_value"); + rs.close(); + + return value; } } // end jTPCCUtil diff --git a/src/jdbc/ExecJDBC.java b/src/jdbc/ExecJDBC.java index 61e27bc..f6f21ea 100755 --- a/src/jdbc/ExecJDBC.java +++ b/src/jdbc/ExecJDBC.java @@ -7,108 +7,108 @@ * */ -import java.io.*; -import java.sql.*; -import java.util.*; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; public class ExecJDBC { + public static void main(String[] args) { + Connection conn = null; + Statement stmt = null; + String rLine = null; + StringBuffer sql = new StringBuffer(); + try { - public static void main(String[] args) { + Properties ini = new Properties(); + ini.load(new FileInputStream(System.getProperty("prop"))); - Connection conn = null; - Statement stmt = null; - String rLine = null; - StringBuffer sql = new StringBuffer(); + // Register jdbcDriver + Class.forName(ini.getProperty("driver")); - try { + // make connection + conn = DriverManager.getConnection(ini.getProperty("conn"), + ini.getProperty("user"), ini.getProperty("password")); + conn.setAutoCommit(true); - Properties ini = new Properties(); - ini.load( new FileInputStream(System.getProperty("prop"))); + // Create Statement + stmt = conn.createStatement(); - // Register jdbcDriver - Class.forName(ini.getProperty( "driver" )); + // Open inputFile + BufferedReader in = new BufferedReader + (new FileReader(jTPCCUtil.getSysProp("commandFile", null))); - // make connection - conn = DriverManager.getConnection(ini.getProperty("conn"), - ini.getProperty("user"),ini.getProperty("password")); - conn.setAutoCommit(true); + // loop thru input file and concatenate SQL statement fragments + while ((rLine = in.readLine()) != null) { - // Create Statement - stmt = conn.createStatement(); + String line = rLine.trim(); - // Open inputFile - BufferedReader in = new BufferedReader - (new FileReader(jTPCCUtil.getSysProp("commandFile",null))); + if (line.length() != 0) { + if (line.startsWith("--")) { + System.out.println(line); // print comment line + } else { + if (line.endsWith("\\;")) { + sql.append(line.replaceAll("\\\\;", ";")); + sql.append("\n"); + } else { + sql.append(line.replaceAll("\\\\;", ";")); + if (line.endsWith(";")) { + String query = sql.toString(); - // loop thru input file and concatenate SQL statement fragments - while((rLine = in.readLine()) != null) { + execJDBC(stmt, query.substring(0, query.length() - 1)); + sql = new StringBuffer(); + } else { + sql.append("\n"); + } + } + } - String line = rLine.trim(); + } //end if - if (line.length() != 0) { - if (line.startsWith("--")) { - System.out.println(line); // print comment line - } else { - if (line.endsWith("\\;")) - { - sql.append(line.replaceAll("\\\\;", ";")); - sql.append("\n"); - } - else - { - sql.append(line.replaceAll("\\\\;", ";")); - if (line.endsWith(";")) { - String query = sql.toString(); + } //end while - execJDBC(stmt, query.substring(0, query.length() - 1)); - sql = new StringBuffer(); - } else { - sql.append("\n"); - } - } - } + in.close(); - } //end if + } catch (IOException ie) { + System.out.println(ie.getMessage()); - } //end while + } catch (SQLException se) { + System.out.println(se.getMessage()); - in.close(); + } catch (Exception e) { + e.printStackTrace(); - } catch(IOException ie) { - System.out.println(ie.getMessage()); + //exit Cleanly + } finally { + try { + if (conn != null) + conn.close(); + } catch (SQLException se) { + se.printStackTrace(); + } // end finally - } catch(SQLException se) { - System.out.println(se.getMessage()); + } // end try - } catch(Exception e) { - e.printStackTrace(); + } // end main - //exit Cleanly - } finally { - try { - if (conn !=null) - conn.close(); - } catch(SQLException se) { - se.printStackTrace(); - } // end finally - } // end try + static void execJDBC(Statement stmt, String query) { - } // end main + System.out.println(query + ";"); + try { + stmt.execute(query); + } catch (SQLException se) { + System.out.println(se.getMessage()); + } // end try - static void execJDBC(Statement stmt, String query) { - - System.out.println(query + ";"); - - try { - stmt.execute(query); - }catch(SQLException se) { - System.out.println(se.getMessage()); - } // end try - - } // end execJDBCCommand + } // end execJDBCCommand } // end ExecJDBC Class From 592cc9b7a93cd1284321367ed8497f4d29551f73 Mon Sep 17 00:00:00 2001 From: istudies Date: Sat, 21 Aug 2021 14:43:25 +0800 Subject: [PATCH 02/16] Convert StringBuffer and String splicing into StringBuilder --- src/LoadData/LoadData.java | 24 +++++++-------- src/LoadData/LoadDataWorker.java | 50 +++++++++++++++----------------- src/client/jTPCC.java | 29 +++++++++--------- src/client/jTPCCConnection.java | 28 +++++++++--------- 4 files changed, 62 insertions(+), 69 deletions(-) diff --git a/src/LoadData/LoadData.java b/src/LoadData/LoadData.java index 8355797..936e59b 100644 --- a/src/LoadData/LoadData.java +++ b/src/LoadData/LoadData.java @@ -185,7 +185,7 @@ public static void main(String[] args) { } } // End of main() - public static void configAppend(StringBuffer buf) throws IOException { + public static void configAppend(StringBuilder buf) throws IOException { try { configCSVLock.lock(); configCSV.write(buf.toString()); @@ -195,7 +195,7 @@ public static void configAppend(StringBuffer buf) throws IOException { } } - public static void itemAppend(StringBuffer buf) throws IOException { + public static void itemAppend(StringBuilder buf) throws IOException { try { itemCSVLock.lock(); itemCSV.write(buf.toString()); @@ -205,7 +205,7 @@ public static void itemAppend(StringBuffer buf) throws IOException { } } - public static void warehouseAppend(StringBuffer buf) throws IOException { + public static void warehouseAppend(StringBuilder buf) throws IOException { try { warehouseCSVLock.lock(); warehouseCSV.write(buf.toString()); @@ -215,7 +215,7 @@ public static void warehouseAppend(StringBuffer buf) throws IOException { } } - public static void districtAppend(StringBuffer buf) throws IOException { + public static void districtAppend(StringBuilder buf) throws IOException { try { districtCSVLock.lock(); districtCSV.write(buf.toString()); @@ -225,7 +225,7 @@ public static void districtAppend(StringBuffer buf) throws IOException { } } - public static void stockAppend(StringBuffer buf) throws IOException { + public static void stockAppend(StringBuilder buf) throws IOException { try { stockCSVLock.lock(); stockCSV.write(buf.toString()); @@ -235,7 +235,7 @@ public static void stockAppend(StringBuffer buf) throws IOException { } } - public static void customerAppend(StringBuffer buf) throws IOException { + public static void customerAppend(StringBuilder buf) throws IOException { try { customerCSVLock.lock(); customerCSV.write(buf.toString()); @@ -245,7 +245,7 @@ public static void customerAppend(StringBuffer buf) throws IOException { } } - public static void historyAppend(StringBuffer buf) throws IOException { + public static void historyAppend(StringBuilder buf) throws IOException { try { historyCSVLock.lock(); historyCSV.write(buf.toString()); @@ -255,7 +255,7 @@ public static void historyAppend(StringBuffer buf) throws IOException { } } - public static void orderAppend(StringBuffer buf) throws IOException { + public static void orderAppend(StringBuilder buf) throws IOException { try { orderCSVLock.lock(); orderCSV.write(buf.toString()); @@ -265,7 +265,7 @@ public static void orderAppend(StringBuffer buf) throws IOException { } } - public static void orderLineAppend(StringBuffer buf) throws IOException { + public static void orderLineAppend(StringBuilder buf) throws IOException { try { orderLineCSVLock.lock(); orderLineCSV.write(buf.toString()); @@ -275,7 +275,7 @@ public static void orderLineAppend(StringBuffer buf) throws IOException { } } - public static void newOrderAppend(StringBuffer buf) throws IOException { + public static void newOrderAppend(StringBuilder buf) throws IOException { try { newOrderCSVLock.lock(); newOrderCSV.write(buf.toString()); @@ -307,7 +307,7 @@ private static String iniGetString(String name) { String strVal = null; for (int i = 0; i < argv.length - 1; i += 2) { - if (name.toLowerCase().equals(argv[i].toLowerCase())) { + if (name.equalsIgnoreCase(argv[i])) { strVal = argv[i + 1]; break; } @@ -329,7 +329,7 @@ private static String iniGetString(String name, String defVal) { String strVal = null; for (int i = 0; i < argv.length - 1; i += 2) { - if (name.toLowerCase().equals(argv[i].toLowerCase())) { + if (name.equalsIgnoreCase(argv[i])) { strVal = argv[i + 1]; break; } diff --git a/src/LoadData/LoadDataWorker.java b/src/LoadData/LoadDataWorker.java index 510d00f..b0b4851 100644 --- a/src/LoadData/LoadDataWorker.java +++ b/src/LoadData/LoadDataWorker.java @@ -18,7 +18,7 @@ public class LoadDataWorker implements Runnable { private Connection dbConn; private jTPCCRandom rnd; - private StringBuffer sb; + private StringBuilder sb; private Formatter fmt; private boolean writeCSV = false; @@ -35,25 +35,25 @@ public class LoadDataWorker implements Runnable { private PreparedStatement stmtOrderLine = null; private PreparedStatement stmtNewOrder = null; - private StringBuffer sbConfig = null; + private StringBuilder sbConfig = null; private Formatter fmtConfig = null; - private StringBuffer sbItem = null; + private StringBuilder sbItem = null; private Formatter fmtItem = null; - private StringBuffer sbWarehouse = null; + private StringBuilder sbWarehouse = null; private Formatter fmtWarehouse = null; - private StringBuffer sbDistrict = null; + private StringBuilder sbDistrict = null; private Formatter fmtDistrict = null; - private StringBuffer sbStock = null; + private StringBuilder sbStock = null; private Formatter fmtStock = null; - private StringBuffer sbCustomer = null; + private StringBuilder sbCustomer = null; private Formatter fmtCustomer = null; - private StringBuffer sbHistory = null; + private StringBuilder sbHistory = null; private Formatter fmtHistory = null; - private StringBuffer sbOrder = null; + private StringBuilder sbOrder = null; private Formatter fmtOrder = null; - private StringBuffer sbOrderLine = null; + private StringBuilder sbOrderLine = null; private Formatter fmtOrderLine = null; - private StringBuffer sbNewOrder = null; + private StringBuilder sbNewOrder = null; private Formatter fmtNewOrder = null; LoadDataWorker(int worker, String csvNull, jTPCCRandom rnd) { @@ -61,29 +61,29 @@ public class LoadDataWorker implements Runnable { this.csvNull = csvNull; this.rnd = rnd; - this.sb = new StringBuffer(); + this.sb = new StringBuilder(); this.fmt = new Formatter(sb); this.writeCSV = true; - this.sbConfig = new StringBuffer(); + this.sbConfig = new StringBuilder(); this.fmtConfig = new Formatter(sbConfig); - this.sbItem = new StringBuffer(); + this.sbItem = new StringBuilder(); this.fmtItem = new Formatter(sbItem); - this.sbWarehouse = new StringBuffer(); + this.sbWarehouse = new StringBuilder(); this.fmtWarehouse = new Formatter(sbWarehouse); - this.sbDistrict = new StringBuffer(); + this.sbDistrict = new StringBuilder(); this.fmtDistrict = new Formatter(sbDistrict); - this.sbStock = new StringBuffer(); + this.sbStock = new StringBuilder(); this.fmtStock = new Formatter(sbStock); - this.sbCustomer = new StringBuffer(); + this.sbCustomer = new StringBuilder(); this.fmtCustomer = new Formatter(sbCustomer); - this.sbHistory = new StringBuffer(); + this.sbHistory = new StringBuilder(); this.fmtHistory = new Formatter(sbHistory); - this.sbOrder = new StringBuffer(); + this.sbOrder = new StringBuilder(); this.fmtOrder = new Formatter(sbOrder); - this.sbOrderLine = new StringBuffer(); + this.sbOrderLine = new StringBuilder(); this.fmtOrderLine = new Formatter(sbOrderLine); - this.sbNewOrder = new StringBuffer(); + this.sbNewOrder = new StringBuilder(); this.fmtNewOrder = new Formatter(sbNewOrder); } @@ -93,7 +93,7 @@ public class LoadDataWorker implements Runnable { this.dbConn = dbConn; this.rnd = rnd; - this.sb = new StringBuffer(); + this.sb = new StringBuilder(); this.fmt = new Formatter(sb); stmtConfig = dbConn.prepareStatement( @@ -270,9 +270,7 @@ private void loadItem() throws SQLException, IOException { int len = rnd.nextInt(26, 50); int off = rnd.nextInt(0, len - 8); - iData = rnd.getAString(off, off) + - "ORIGINAL" + - rnd.getAString(len - off - 8, len - off - 8); + iData = rnd.getAString(off, off) + "ORIGINAL" + rnd.getAString(len - off - 8, len - off - 8); } else { iData = rnd.getAString(26, 50); } diff --git a/src/client/jTPCC.java b/src/client/jTPCC.java index fc0e989..3d7d136 100644 --- a/src/client/jTPCC.java +++ b/src/client/jTPCC.java @@ -159,7 +159,7 @@ else if (iDB.equals("mysql")) } if (databaseDriverLoaded && resultDirectory != null) { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); Formatter fmt = new Formatter(sb); Pattern p = Pattern.compile("%t"); Calendar cal = Calendar.getInstance(); @@ -497,11 +497,12 @@ else if (newOrderWeightValue == 0 && paymentWeightValue == 0 && orderStatusWeigh } } - printMessage("Starting all terminals..."); - transactionCount.add(1); - for (int i = 0; i < terminals.length; i++) - (new Thread(terminals[i])).start(); - + synchronized (terminals) { + printMessage("Starting all terminals..."); + transactionCount.add(1); + for (int i = 0; i < terminals.length; i++) + (new Thread(terminals[i])).start(); + } printMessage("All terminals started executing " + sessionStart); } catch (Exception e1) { errorMessage("This session ended with errors!"); @@ -541,8 +542,8 @@ public void signalTerminalEnded(jTPCCTerminal terminal, long countNewOrdersExecu try { terminalsLock.lock(); - terminalsStarted.decrement(); boolean found = false; + terminalsStarted.decrement(); for (int i = 0; i < terminals.length && !found; i++) { if (terminals[i] == terminal) { terminals[i] = null; @@ -551,7 +552,6 @@ public void signalTerminalEnded(jTPCCTerminal terminal, long countNewOrdersExecu found = true; } } - } finally { terminalsLock.unlock(); } @@ -586,6 +586,7 @@ public void signalTerminalEndedTransaction(String terminalName, String transacti long executionTime, String comment, int newOrder) { try { transactionLock.lock(); + transactionCount.increment(); fastNewOrderCounter.add(newOrder); Long counter = costPerWorkerload.get(transactionType); @@ -604,7 +605,6 @@ public void signalTerminalEndedTransaction(String terminalName, String transacti } updateStatusLine(); - } public jTPCCRandom getRnd() { @@ -614,8 +614,7 @@ public jTPCCRandom getRnd() { public void resultAppend(jTPCCTData term) { if (resultCSV != null) { try { - resultCSV.write(runID + "," + - term.resultLine(sessionStartTimestamp)); + resultCSV.write(runID + "," + term.resultLine(sessionStartTimestamp)); } catch (IOException e) { log.error("Term-00, " + e.getMessage()); } @@ -667,14 +666,12 @@ private String getFileNameSuffix() { private Lock statusLock = new ReentrantLock(); private void updateStatusLine() { - long currTimeMillis = System.currentTimeMillis(); - - StringBuilder informativeText = new StringBuilder(""); - Formatter fmt = new Formatter(informativeText); try { statusLock.lock(); - + long currTimeMillis = System.currentTimeMillis(); if (currTimeMillis > sessionNextTimestamp) { + StringBuilder informativeText = new StringBuilder(""); + Formatter fmt = new Formatter(informativeText); double tpmC = (6000000 * fastNewOrderCounter.intValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; double tpmTotal = (6000000 * transactionCount.intValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; diff --git a/src/client/jTPCCConnection.java b/src/client/jTPCCConnection.java index b92bbc8..0a3bd00 100644 --- a/src/client/jTPCCConnection.java +++ b/src/client/jTPCCConnection.java @@ -57,29 +57,28 @@ public class jTPCCConnection { public PreparedStatement stmtDeliveryBGUpdateOrderLine; public PreparedStatement stmtDeliveryBGUpdateCustomer; - public jTPCCConnection(Connection dbConn, int dbType) - throws SQLException { + public jTPCCConnection(Connection dbConn, int dbType) throws SQLException { this.dbConn = dbConn; this.dbType = dbType; stmtNewOrderSelectStockBatch = new PreparedStatement[16]; - String st = "SELECT s_i_id, s_w_id, 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 bmsql_stock " + - " WHERE (s_w_id, s_i_id) in ((?,?)"; + StringBuilder sqlSb = new StringBuilder("SELECT s_i_id, s_w_id, s_quantity, s_data, "); + sqlSb.append(" s_dist_01, s_dist_02, s_dist_03, s_dist_04, "); + sqlSb.append(" s_dist_05, s_dist_06, s_dist_07, s_dist_08, "); + sqlSb.append(" s_dist_09, s_dist_10 "); + sqlSb.append(" FROM bmsql_stock "); + sqlSb.append(" WHERE (s_w_id, s_i_id) in ((?,?)"); for (int i = 1; i <= 15; i++) { - String stmtStr = st + ") FOR UPDATE"; + String stmtStr = sqlSb + ") FOR UPDATE"; stmtNewOrderSelectStockBatch[i] = dbConn.prepareStatement(stmtStr); - st += ",(?,?)"; + sqlSb.append(",(?,?)"); } stmtNewOrderSelectItemBatch = new PreparedStatement[16]; - st = "SELECT i_id, i_price, i_name, i_data " + - " FROM bmsql_item WHERE i_id in (?"; + sqlSb = new StringBuilder("SELECT i_id, i_price, i_name, i_data "); + sqlSb.append(" FROM bmsql_item WHERE i_id in (?"); for (int i = 1; i <= 15; i++) { - String stmtStr = st + ")"; + String stmtStr = sqlSb + ")"; stmtNewOrderSelectItemBatch[i] = dbConn.prepareStatement(stmtStr); - st += ",?"; + sqlSb.append(",?"); } // PreparedStataments for NEW_ORDER @@ -245,7 +244,6 @@ public jTPCCConnection(Connection dbConn, int dbType) break; } - // PreparedStatements for DELIVERY_BG stmtDeliveryBGSelectOldestNewOrder = dbConn.prepareStatement( "SELECT no_o_id " + From d65c41109e58851947750306ff335be91fcb18e7 Mon Sep 17 00:00:00 2001 From: istudies Date: Sat, 21 Aug 2021 17:34:10 +0800 Subject: [PATCH 03/16] Fix the problem of negative numbers in tpmC --- src/client/jTPCC.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/client/jTPCC.java b/src/client/jTPCC.java index 3d7d136..a0b9423 100644 --- a/src/client/jTPCC.java +++ b/src/client/jTPCC.java @@ -625,8 +625,8 @@ private void endReport() { long currTimeMillis = System.currentTimeMillis(); long freeMem = Runtime.getRuntime().freeMemory() / (1024 * 1024); long totalMem = Runtime.getRuntime().totalMemory() / (1024 * 1024); - double tpmC = (6000000 * fastNewOrderCounter.intValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; - double tpmTotal = (6000000 * transactionCount.intValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; + double tpmC = (6000000 * fastNewOrderCounter.longValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; + double tpmTotal = (6000000 * transactionCount.longValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; System.out.println(""); log.info("Term-00, "); @@ -635,7 +635,7 @@ private void endReport() { log.info("Term-00, Measured tpmTOTAL = " + tpmTotal); log.info("Term-00, Session Start = " + sessionStart); log.info("Term-00, Session End = " + sessionEnd); - log.info("Term-00, Transaction Count = " + (transactionCount.intValue() - 1)); + log.info("Term-00, Transaction Count = " + (transactionCount.longValue() - 1)); for (String key : costPerWorkerload.keySet()) { Long value = costPerWorkerload.get(key); log.info("executeTime[" + key + "]=" + value.toString()); @@ -672,17 +672,17 @@ private void updateStatusLine() { if (currTimeMillis > sessionNextTimestamp) { StringBuilder informativeText = new StringBuilder(""); Formatter fmt = new Formatter(informativeText); - double tpmC = (6000000 * fastNewOrderCounter.intValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; - double tpmTotal = (6000000 * transactionCount.intValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; + double tpmC = (6000000 * fastNewOrderCounter.longValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; + double tpmTotal = (6000000 * transactionCount.longValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; sessionNextTimestamp += 1000; /* update this every seconds */ fmt.format("Term-00, Running Average tpmTOTAL: %.2f", tpmTotal); /* XXX What is the meaning of these numbers? */ - recentTpmC = (fastNewOrderCounter.intValue() - sessionNextKounter) * 12; - recentTpmTotal = (transactionCount.intValue() - sessionNextKounter) * 12; - sessionNextKounter = fastNewOrderCounter.intValue(); + recentTpmC = (fastNewOrderCounter.longValue() - sessionNextKounter) * 12; + recentTpmTotal = (transactionCount.longValue() - sessionNextKounter) * 12; + sessionNextKounter = fastNewOrderCounter.longValue(); fmt.format(" Current tpmTOTAL: %d", recentTpmTotal); long freeMem = Runtime.getRuntime().freeMemory() / (1024 * 1024); From 4e3b473c44190210f93e787dfa04d36852e241ec Mon Sep 17 00:00:00 2001 From: istudies Date: Sat, 21 Aug 2021 18:27:32 +0800 Subject: [PATCH 04/16] Optimize the use of StringBuffer and replace it with StringBuilder --- src/client/jTPCC.java | 16 ++++++---------- src/client/jTPCCTerminal.java | 8 -------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/client/jTPCC.java b/src/client/jTPCC.java index a0b9423..3388b67 100644 --- a/src/client/jTPCC.java +++ b/src/client/jTPCC.java @@ -348,7 +348,6 @@ else if (iDB.equals("mysql")) throw new Exception(); } - if (Long.parseLong(iRunMins) != 0 && Integer.parseInt(iRunTxnsPerTerminal) == 0) { try { executionTimeMillis = Long.parseLong(iRunMins) * 60000; @@ -479,7 +478,7 @@ else if (newOrderWeightValue == 0 && paymentWeightValue == 0 && orderStatusWeigh // Record run parameters in runInfo.csv if (runInfoCSV != null) { try { - StringBuffer infoSB = new StringBuffer(); + StringBuilder infoSB = new StringBuilder(); Formatter infoFmt = new Formatter(infoSB); infoFmt.format("%d,simple,%s,%s,%s,%s,%d,%d,%d,%d,1.0,1.0\n", runID, JTPCCVERSION, iDB, @@ -497,12 +496,10 @@ else if (newOrderWeightValue == 0 && paymentWeightValue == 0 && orderStatusWeigh } } - synchronized (terminals) { - printMessage("Starting all terminals..."); - transactionCount.add(1); - for (int i = 0; i < terminals.length; i++) - (new Thread(terminals[i])).start(); - } + printMessage("Starting all terminals..."); + transactionCount.add(1); + for (int i = 0; i < terminals.length; i++) + (new Thread(terminals[i])).start(); printMessage("All terminals started executing " + sessionStart); } catch (Exception e1) { errorMessage("This session ended with errors!"); @@ -628,7 +625,6 @@ private void endReport() { double tpmC = (6000000 * fastNewOrderCounter.longValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; double tpmTotal = (6000000 * transactionCount.longValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; - System.out.println(""); log.info("Term-00, "); log.info("Term-00, "); log.info("Term-00, Measured tpmC (NewOrders) = " + tpmC); @@ -670,7 +666,7 @@ private void updateStatusLine() { statusLock.lock(); long currTimeMillis = System.currentTimeMillis(); if (currTimeMillis > sessionNextTimestamp) { - StringBuilder informativeText = new StringBuilder(""); + StringBuilder informativeText = new StringBuilder(); Formatter fmt = new Formatter(informativeText); double tpmC = (6000000 * fastNewOrderCounter.longValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; double tpmTotal = (6000000 * transactionCount.longValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; diff --git a/src/client/jTPCCTerminal.java b/src/client/jTPCCTerminal.java index 8d0778f..26e2b6a 100644 --- a/src/client/jTPCCTerminal.java +++ b/src/client/jTPCCTerminal.java @@ -279,13 +279,11 @@ private void executeTransactions(int numTransactions) { } } - private void error(String type) { log.error(terminalName + ", TERMINAL=" + terminalName + " TYPE=" + type + " COUNT=" + transactionCount); System.out.println(terminalName + ", TERMINAL=" + terminalName + " TYPE=" + type + " COUNT=" + transactionCount); } - private void logException(Exception e) { StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); @@ -294,17 +292,14 @@ private void logException(Exception e) { log.error(stringWriter.toString()); } - private void terminalMessage(String message) { log.trace(terminalName + ", " + message); } - private void printMessage(String message) { log.trace(terminalName + ", " + message); } - void transRollback() { try { conn.rollback(); @@ -313,7 +308,6 @@ void transRollback() { } } - void transCommit() { try { conn.commit(); @@ -322,6 +316,4 @@ void transCommit() { transRollback(); } } // end transCommit() - - } From c86c01bdccc3303adb26242f59f9f4c47954e173 Mon Sep 17 00:00:00 2001 From: istudies Date: Mon, 30 Aug 2021 20:38:21 +0800 Subject: [PATCH 05/16] add batch process running support. --- run/props.mysql | 6 +- run/runBenchmark.sh | 5 +- src/client/jTPCC.java | 27 +++--- src/client/jTPCCRunner.java | 184 ++++++++++++++++++++++++++++++++++++ src/client/jTPCCUtil.java | 14 +++ 5 files changed, 216 insertions(+), 20 deletions(-) create mode 100644 src/client/jTPCCRunner.java diff --git a/run/props.mysql b/run/props.mysql index 7469a42..34224d3 100644 --- a/run/props.mysql +++ b/run/props.mysql @@ -1,17 +1,17 @@ db=mysql driver=com.mysql.jdbc.Driver -conn=jdbc:mysql://localhost:4000/tpcc?useSSL=false&useServerPrepStmts=true&useConfigs=maxPerformance&rewriteBatchedStatements=true +conn=jdbc:mysql://172.16.6.48:3308/benchmarksql?useSSL=false&useServerPrepStmts=true&useConfigs=maxPerformance&rewriteBatchedStatements=true&&zeroDateTimeBehavior=convertToNull user=root password= -warehouses=1 +warehouses=10 loadWorkers=4 terminals=1 //To run specified transactions per terminal- runMins must equal zero runTxnsPerTerminal=0 //To run for specified minutes- runTxnsPerTerminal must equal zero -runMins=10 +runMins=5 //Number of total transactions per minute limitTxnsPerMin=0 diff --git a/run/runBenchmark.sh b/run/runBenchmark.sh index 6f4419e..fddaae9 100755 --- a/run/runBenchmark.sh +++ b/run/runBenchmark.sh @@ -16,6 +16,7 @@ source funcs.sh $1 setCP || exit 1 -myOPTS="-Dprop=$1 -DrunID=${SEQ}" +# options params: -Ddebug, default value: 0 +myOPTS="-Dprop=$1 -DrunID=${SEQ} -Dprocessor=1" -java -cp "$myCP" $myOPTS jTPCC +java -cp "$myCP" $myOPTS jTPCCRunner \ No newline at end of file diff --git a/src/client/jTPCC.java b/src/client/jTPCC.java index 3388b67..36f1220 100644 --- a/src/client/jTPCC.java +++ b/src/client/jTPCC.java @@ -15,10 +15,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Formatter; -import java.util.HashMap; -import java.util.Properties; +import java.util.*; import java.util.concurrent.atomic.LongAdder; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -60,6 +57,8 @@ public class jTPCC implements jTPCCConfig { private final Lock terminalsLock = new ReentrantLock(); private final Lock transactionLock = new ReentrantLock(); + private boolean hasBatch = false; + public static void main(String args[]) { PropertyConfigurator.configure("log4j.properties"); new jTPCC(); @@ -82,15 +81,12 @@ public jTPCC() { errorMessage("Term-00, could not load properties file"); } - log.info("Term-00, "); - log.info("Term-00, +-------------------------------------------------------------+"); - log.info("Term-00, BenchmarkSQL v" + JTPCCVERSION); - log.info("Term-00, +-------------------------------------------------------------+"); - log.info("Term-00, (c) 2003, Raul Barbosa"); - log.info("Term-00, (c) 2004-2016, Denis Lussier"); - log.info("Term-00, (c) 2016, Jan Wieck"); - log.info("Term-00, +-------------------------------------------------------------+"); - log.info("Term-00, "); + if (Integer.parseInt(System.getProperty("batch")) == 1) { + hasBatch = true; + } + if (!hasBatch) { + jTPCCUtil.printTitle(); + } String iDB = getProp(ini, "db"); String iDriver = getProp(ini, "driver"); String iConn = getProp(ini, "conn"); @@ -185,6 +181,7 @@ else if (iDB.equals("mysql")) fmt.format("%t" + parts[i].substring(0, 1), cal); sb.append(parts[i].substring(1)); } + sb.append("_").append(iRunID); resultDirName = sb.toString(); File resultDir = new File(resultDirName); File resultDataDir = new File(resultDir, "data"); @@ -685,9 +682,9 @@ private void updateStatusLine() { long totalMem = Runtime.getRuntime().totalMemory() / (1024 * 1024); fmt.format(" Memory Usage: %dMB / %dMB ", (totalMem - freeMem), totalMem); - System.out.print(informativeText); + System.out.println(informativeText); for (int count = 0; count < 1 + informativeText.length(); count++) - System.out.print("\b"); + System.out.println("\b"); } } finally { statusLock.unlock(); diff --git a/src/client/jTPCCRunner.java b/src/client/jTPCCRunner.java new file mode 100644 index 0000000..7be8829 --- /dev/null +++ b/src/client/jTPCCRunner.java @@ -0,0 +1,184 @@ +import org.apache.log4j.Logger; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.atomic.LongAdder; + +public class jTPCCRunner { + private static Logger log = Logger.getLogger(jTPCC.class); + + private static CountDownLatch latch; + + private static final AtomicReference tpmCSum = new AtomicReference<>(0D); + private static final AtomicReference tpmTotalSum = new AtomicReference<>(0D); + private static final AtomicReference sessionStart = new AtomicReference<>(new Date()); + private static final AtomicReference sessionEnd = new AtomicReference<>(new Date()); + private static final AtomicLong tranCountSum = new AtomicLong(0L); + private static final Map executeTimeMap = new ConcurrentHashMap<>(); + + public static void main(String[] args) throws Exception { + String prop = System.getProperty("prop"); + String runIDStr = System.getProperty("runID"); + String processorStr = System.getProperty("processor"); + String debugStr = System.getProperty("debug"); + + int processor = Integer.parseInt(processorStr); + if (processor <= 0) { + throw new Exception("processor can only be positive"); + } + latch = new CountDownLatch(processor); + + boolean debug = false; + if (null != debugStr && debugStr.equals("1")) { + debug = true; + } + + int runID = Integer.parseInt(runIDStr); + List processList = new ArrayList<>(); + + jTPCCUtil.printTitle(); + for (int i = 0; i < processor; i++) { + runID++; +// System.out.println("&&&&&&& runID: " + runID); + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.redirectErrorStream(true); + processBuilder.command("java", "-cp", ".:../lib/mysql/*:../lib/*:../dist/*", + "-Dprop=" + prop, "-DrunID=" + runID, "-Dbatch=1", "jTPCC"); + processBuilder.redirectErrorStream(true); + + Process process = processBuilder.start(); + processList.add(process); + } + + for (int i = 0; i < processor; i++) { + int finalI = i; + boolean finalDebug = debug; + new Thread(() -> { + Scanner scanner = new Scanner(processList.get(finalI).getInputStream()); + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + if (line.trim().isEmpty()) { + continue; + } + if (finalDebug) { + System.out.println("[Process " + finalI + "] " + line); + } + if (summary(line)) continue; + // Only the first process data is output + if (finalDebug) { + System.out.println("[Process " + finalI + "] " + line); + } else { + if (finalI == 0) { + System.out.println(line); + } + } + } + latch.countDown(); + }).start(); + } + latch.await(); + + printSummary(); + } + + private static void printSummary() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + log.info("Term-00, "); + log.info("Term-00, "); + log.info("Term-00, Measured tpmC (NewOrders) = " + tpmCSum.get()); + log.info("Term-00, Measured tpmTOTAL = " + tpmTotalSum.get()); + log.info("Term-00, Session Start = " + sdf.format(sessionStart.get())); + log.info("Term-00, Session End = " + sdf.format(sessionEnd.get())); + log.info("Term-00, Transaction Count = " + (tranCountSum.longValue() - 1)); + for (String key : executeTimeMap.keySet()) { + long value = executeTimeMap.get(key).longValue(); + log.info(key + "=" + value); + } + } + + private static boolean summary(String line) { + if (line.contains("Measured tpmC (NewOrders)")) { + // Term-00, Measured tpmC (NewOrders) = 12185.26 + String[] lines = line.split("="); + if (lines.length == 2) { + tpmCSum.set(tpmCSum.get() + Double.parseDouble(lines[1].trim())); + } + return true; + } + if (line.contains("Measured tpmTOTAL")) { + // Term-00, Measured tpmTOTAL = 27073.62 + String[] lines = line.split("="); + if (lines.length == 2) { + tpmTotalSum.set(tpmTotalSum.get() + Double.parseDouble(lines[1].trim())); + } + return true; + } + + SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + if (line.contains("Session Start")) { + // Term-00, Session Start = 2021-08-24 21:54:42 + try { + String[] lines = line.split("="); + if (lines.length == 2) { + Date currentDate = SDF.parse(lines[1].trim()); + if (sessionStart.get().after(currentDate)) { + sessionStart.set(currentDate); + } + } + } catch (ParseException e) { + System.out.println(e.getMessage()); + return false; + } + return true; + } + if (line.contains("Session End")) { + // Term-00, Session End = 2021-08-24 22:24:43 + try { + String[] lines = line.split("="); + if (lines.length == 2) { + Date currentDate = SDF.parse(lines[1].trim()); + + if (sessionEnd.get().before(currentDate)) { + sessionEnd.set(currentDate); + } + } + } catch (ParseException e) { + System.out.println(e.getMessage()); + return false; + } + return true; + } + if (line.contains("Transaction Count")) { + // Term-00, Transaction Count = 812757 + String[] lines = line.split("="); + if (lines.length == 2) { + long l = tranCountSum.addAndGet(Long.parseLong(lines[1].trim())); + } + return true; + } + if (line.contains("executeTime")) { + // executeTime[Payment]=84310282 + // executeTime[Order-Status]=266607 + // executeTime[Delivery]=22537610 + // executeTime[Stock-Level]=929640 + // executeTime[New-Order]=7197477 + String[] lines = line.split(" : "); + if (lines.length == 2) { + String execTimeStr = lines[1].trim(); + String[] execTimeEntryStr = execTimeStr.split("="); + if (execTimeEntryStr.length == 2) { + LongAdder execTimeVal = executeTimeMap.computeIfAbsent(execTimeEntryStr[0].trim(), + (x) -> new LongAdder()); + execTimeVal.add(Long.parseLong(execTimeEntryStr[1].trim())); + } + } + return true; + } + return false; + } +} diff --git a/src/client/jTPCCUtil.java b/src/client/jTPCCUtil.java index d8d998f..2673f80 100644 --- a/src/client/jTPCCUtil.java +++ b/src/client/jTPCCUtil.java @@ -9,6 +9,8 @@ */ +import org.apache.log4j.Logger; + import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -16,6 +18,7 @@ import java.util.Properties; public class jTPCCUtil implements jTPCCConfig { + private static Logger log = Logger.getLogger(jTPCC.class); private static Connection dbConn = null; private static PreparedStatement stmtGetConfig = null; @@ -33,6 +36,17 @@ public static String getSysProp(String inSysProperty, String defaultValue) { } // end getSysProp + public static void printTitle() { + log.info("Term-00, "); + log.info("Term-00, +-------------------------------------------------------------+"); + log.info("Term-00, BenchmarkSQL v" + JTPCCVERSION); + log.info("Term-00, +-------------------------------------------------------------+"); + log.info("Term-00, (c) 2003, Raul Barbosa"); + log.info("Term-00, (c) 2004-2016, Denis Lussier"); + log.info("Term-00, (c) 2016, Jan Wieck"); + log.info("Term-00, +-------------------------------------------------------------+"); + log.info("Term-00, "); + } public static String randomStr(long strLen) { From bfe8e640f5cc32da6ec91092783a2a05ca57af59 Mon Sep 17 00:00:00 2001 From: istudies Date: Mon, 30 Aug 2021 21:01:56 +0800 Subject: [PATCH 06/16] add lock and update default value --- run/runBenchmark.sh | 2 +- src/client/jTPCCRunner.java | 56 ++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/run/runBenchmark.sh b/run/runBenchmark.sh index fddaae9..2ea35e8 100755 --- a/run/runBenchmark.sh +++ b/run/runBenchmark.sh @@ -17,6 +17,6 @@ source funcs.sh $1 setCP || exit 1 # options params: -Ddebug, default value: 0 -myOPTS="-Dprop=$1 -DrunID=${SEQ} -Dprocessor=1" +myOPTS="-Dprop=$1 -DrunID=${SEQ} -Dprocessor=10 -Ddebug=0" java -cp "$myCP" $myOPTS jTPCCRunner \ No newline at end of file diff --git a/src/client/jTPCCRunner.java b/src/client/jTPCCRunner.java index 7be8829..aa3222b 100644 --- a/src/client/jTPCCRunner.java +++ b/src/client/jTPCCRunner.java @@ -8,11 +8,14 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.LongAdder; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; public class jTPCCRunner { private static Logger log = Logger.getLogger(jTPCC.class); private static CountDownLatch latch; + private static final Lock lock = new ReentrantLock(); private static final AtomicReference tpmCSum = new AtomicReference<>(0D); private static final AtomicReference tpmTotalSum = new AtomicReference<>(0D); @@ -68,7 +71,7 @@ public static void main(String[] args) throws Exception { if (finalDebug) { System.out.println("[Process " + finalI + "] " + line); } - if (summary(line)) continue; + if (summary(line) && !finalDebug) continue; // Only the first process data is output if (finalDebug) { System.out.println("[Process " + finalI + "] " + line); @@ -104,17 +107,27 @@ private static void printSummary() { private static boolean summary(String line) { if (line.contains("Measured tpmC (NewOrders)")) { // Term-00, Measured tpmC (NewOrders) = 12185.26 - String[] lines = line.split("="); - if (lines.length == 2) { - tpmCSum.set(tpmCSum.get() + Double.parseDouble(lines[1].trim())); + try { + lock.lock(); + String[] lines = line.split("="); + if (lines.length == 2) { + tpmCSum.set(tpmCSum.get() + Double.parseDouble(lines[1].trim())); + } + } finally { + lock.unlock(); } return true; } if (line.contains("Measured tpmTOTAL")) { // Term-00, Measured tpmTOTAL = 27073.62 - String[] lines = line.split("="); - if (lines.length == 2) { - tpmTotalSum.set(tpmTotalSum.get() + Double.parseDouble(lines[1].trim())); + try { + lock.lock(); + String[] lines = line.split("="); + if (lines.length == 2) { + tpmTotalSum.set(tpmTotalSum.get() + Double.parseDouble(lines[1].trim())); + } + } finally { + lock.unlock(); } return true; } @@ -123,6 +136,7 @@ private static boolean summary(String line) { if (line.contains("Session Start")) { // Term-00, Session Start = 2021-08-24 21:54:42 try { + lock.lock(); String[] lines = line.split("="); if (lines.length == 2) { Date currentDate = SDF.parse(lines[1].trim()); @@ -133,12 +147,15 @@ private static boolean summary(String line) { } catch (ParseException e) { System.out.println(e.getMessage()); return false; + } finally { + lock.unlock(); } return true; } if (line.contains("Session End")) { // Term-00, Session End = 2021-08-24 22:24:43 try { + lock.lock(); String[] lines = line.split("="); if (lines.length == 2) { Date currentDate = SDF.parse(lines[1].trim()); @@ -150,6 +167,8 @@ private static boolean summary(String line) { } catch (ParseException e) { System.out.println(e.getMessage()); return false; + } finally { + lock.unlock(); } return true; } @@ -157,7 +176,7 @@ private static boolean summary(String line) { // Term-00, Transaction Count = 812757 String[] lines = line.split("="); if (lines.length == 2) { - long l = tranCountSum.addAndGet(Long.parseLong(lines[1].trim())); + tranCountSum.addAndGet(Long.parseLong(lines[1].trim())); } return true; } @@ -167,15 +186,20 @@ private static boolean summary(String line) { // executeTime[Delivery]=22537610 // executeTime[Stock-Level]=929640 // executeTime[New-Order]=7197477 - String[] lines = line.split(" : "); - if (lines.length == 2) { - String execTimeStr = lines[1].trim(); - String[] execTimeEntryStr = execTimeStr.split("="); - if (execTimeEntryStr.length == 2) { - LongAdder execTimeVal = executeTimeMap.computeIfAbsent(execTimeEntryStr[0].trim(), - (x) -> new LongAdder()); - execTimeVal.add(Long.parseLong(execTimeEntryStr[1].trim())); + try { + lock.lock(); + String[] lines = line.split(" : "); + if (lines.length == 2) { + String execTimeStr = lines[1].trim(); + String[] execTimeEntryStr = execTimeStr.split("="); + if (execTimeEntryStr.length == 2) { + LongAdder execTimeVal = executeTimeMap.computeIfAbsent(execTimeEntryStr[0].trim(), + (x) -> new LongAdder()); + execTimeVal.add(Long.parseLong(execTimeEntryStr[1].trim())); + } } + } finally { + lock.unlock(); } return true; } From b20b0ec233fbc4af99a986af4ada9d1f8d1e29b5 Mon Sep 17 00:00:00 2001 From: istudies Date: Mon, 30 Aug 2021 21:25:03 +0800 Subject: [PATCH 07/16] update terminals value is terminals/processor --- src/client/jTPCC.java | 17 +++++++++++++---- src/client/jTPCCRunner.java | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/client/jTPCC.java b/src/client/jTPCC.java index 36f1220..20dab24 100644 --- a/src/client/jTPCC.java +++ b/src/client/jTPCC.java @@ -15,7 +15,10 @@ import java.sql.Connection; import java.sql.DriverManager; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.Calendar; +import java.util.Formatter; +import java.util.HashMap; +import java.util.Properties; import java.util.concurrent.atomic.LongAdder; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -58,6 +61,7 @@ public class jTPCC implements jTPCCConfig { private final Lock transactionLock = new ReentrantLock(); private boolean hasBatch = false; + private int processor = 1; public static void main(String args[]) { PropertyConfigurator.configure("log4j.properties"); @@ -81,7 +85,12 @@ public jTPCC() { errorMessage("Term-00, could not load properties file"); } - if (Integer.parseInt(System.getProperty("batch")) == 1) { + String processorStr = System.getProperty("processor"); + if (null != processorStr) { + processor = Integer.parseInt(processorStr); + } + String batch = System.getProperty("batch"); + if (null != batch && Integer.parseInt(batch) == 1) { hasBatch = true; } if (!hasBatch) { @@ -136,7 +145,7 @@ else if (iDB.equals("mysql")) } if (Integer.parseInt(limPerMin) != 0) { - limPerMin_Terminal = Integer.parseInt(limPerMin) / Integer.parseInt(iTerminals); + limPerMin_Terminal = Integer.parseInt(limPerMin) / Integer.parseInt(iTerminals) / processor; } else { limPerMin_Terminal = -1; } @@ -337,7 +346,7 @@ else if (iDB.equals("mysql")) } try { - numTerminals = Integer.parseInt(iTerminals); + numTerminals = Integer.parseInt(iTerminals) / processor; if (numTerminals <= 0 || numTerminals > 10 * numWarehouses) throw new NumberFormatException(); } catch (NumberFormatException e1) { diff --git a/src/client/jTPCCRunner.java b/src/client/jTPCCRunner.java index aa3222b..9e9c610 100644 --- a/src/client/jTPCCRunner.java +++ b/src/client/jTPCCRunner.java @@ -51,7 +51,7 @@ public static void main(String[] args) throws Exception { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.redirectErrorStream(true); processBuilder.command("java", "-cp", ".:../lib/mysql/*:../lib/*:../dist/*", - "-Dprop=" + prop, "-DrunID=" + runID, "-Dbatch=1", "jTPCC"); + "-Dprop=" + prop, "-DrunID=" + runID, "-Dprocessor=" + processor, "-Dbatch=1", "jTPCC"); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); From 8060a36491d3da52f0d206dacdcff226ca4f27a5 Mon Sep 17 00:00:00 2001 From: istudies Date: Mon, 30 Aug 2021 21:30:30 +0800 Subject: [PATCH 08/16] update source funcs.sh add current dir --- run/runBenchmark.sh | 2 +- run/runLoader.sh | 2 +- run/runSQL.sh | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/run/runBenchmark.sh b/run/runBenchmark.sh index 2ea35e8..1372a6e 100755 --- a/run/runBenchmark.sh +++ b/run/runBenchmark.sh @@ -12,7 +12,7 @@ fi SEQ=$(expr $(cat "${SEQ_FILE}") + 1) || exit 1 echo "${SEQ}" > "${SEQ_FILE}" -source funcs.sh $1 +source ./funcs.sh $1 setCP || exit 1 diff --git a/run/runLoader.sh b/run/runLoader.sh index 2df4e36..9f32888 100755 --- a/run/runLoader.sh +++ b/run/runLoader.sh @@ -5,7 +5,7 @@ if [ $# -lt 1 ] ; then exit 2 fi -source funcs.sh $1 +source ./funcs.sh $1 shift setCP || exit 1 diff --git a/run/runSQL.sh b/run/runSQL.sh index 3ac1d42..8e5d495 100755 --- a/run/runSQL.sh +++ b/run/runSQL.sh @@ -11,7 +11,7 @@ fi # ---- # Load common functions # ---- -source funcs.sh $1 +source ./funcs.sh $1 # ---- # Determine which SQL file to use. From 8467777455098b48d048907015d15524bb9a5285 Mon Sep 17 00:00:00 2001 From: istudies Date: Tue, 31 Aug 2021 11:02:45 +0800 Subject: [PATCH 09/16] handle 'No operations allowed after statement closed' SQLException --- src/client/jTPCCTData.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/client/jTPCCTData.java b/src/client/jTPCCTData.java index a5497cf..62b8cf1 100644 --- a/src/client/jTPCCTData.java +++ b/src/client/jTPCCTData.java @@ -557,8 +557,14 @@ private void executeNewOrder(Logger log, jTPCCConnection db) db.stmtNewOrderInsertOrderLine.clearBatch(); db.rollback(); } catch (SQLException se2) { - throw new Exception("Unexpected SQLException on rollback: " + - se2.getMessage()); + String sqlState = se2.getSQLState(); + if ("08S01".equals(sqlState) || "40001".equals(sqlState)) { + log.warn("No operations allowed after statement closed. sql state: " + sqlState); + return; + } else { + throw new Exception("Unexpected SQLException on rollback: " + + se2.getMessage()); + } } throw e; } From 954c4a15adf2b915c44190cdb3b8c48c2e91dff2 Mon Sep 17 00:00:00 2001 From: istudies Date: Tue, 31 Aug 2021 16:05:20 +0800 Subject: [PATCH 10/16] add process exit value print and update config terminals default value --- run/props.mysql | 2 +- src/client/jTPCC.java | 2 +- src/client/jTPCCRunner.java | 39 +++++++++++++++++++++++-------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/run/props.mysql b/run/props.mysql index 34224d3..22c4cba 100644 --- a/run/props.mysql +++ b/run/props.mysql @@ -7,7 +7,7 @@ password= warehouses=10 loadWorkers=4 -terminals=1 +terminals=100 //To run specified transactions per terminal- runMins must equal zero runTxnsPerTerminal=0 //To run for specified minutes- runTxnsPerTerminal must equal zero diff --git a/src/client/jTPCC.java b/src/client/jTPCC.java index 20dab24..6f1a4ef 100644 --- a/src/client/jTPCC.java +++ b/src/client/jTPCC.java @@ -348,7 +348,7 @@ else if (iDB.equals("mysql")) try { numTerminals = Integer.parseInt(iTerminals) / processor; if (numTerminals <= 0 || numTerminals > 10 * numWarehouses) - throw new NumberFormatException(); + throw new NumberFormatException("terminals/processor result must be gt 0"); } catch (NumberFormatException e1) { errorMessage("Invalid number of terminals!"); throw new Exception(); diff --git a/src/client/jTPCCRunner.java b/src/client/jTPCCRunner.java index 9e9c610..07cd69a 100644 --- a/src/client/jTPCCRunner.java +++ b/src/client/jTPCCRunner.java @@ -62,26 +62,35 @@ public static void main(String[] args) throws Exception { int finalI = i; boolean finalDebug = debug; new Thread(() -> { - Scanner scanner = new Scanner(processList.get(finalI).getInputStream()); - while (scanner.hasNextLine()) { - String line = scanner.nextLine(); - if (line.trim().isEmpty()) { - continue; - } - if (finalDebug) { - System.out.println("[Process " + finalI + "] " + line); + try { + Process process = processList.get(finalI); + Scanner scanner = new Scanner(process.getInputStream()); + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + if (line.trim().isEmpty()) { + continue; + } + if (finalDebug) { + System.out.println("[Process " + finalI + "] " + line); + } + if (summary(line) && !finalDebug) continue; + // Only the first process data is output + if (finalDebug) { + System.out.println("[Process " + finalI + "] " + line); + } else { + if (finalI == 0) { + System.out.println(line); + } + } } - if (summary(line) && !finalDebug) continue; - // Only the first process data is output if (finalDebug) { - System.out.println("[Process " + finalI + "] " + line); - } else { - if (finalI == 0) { - System.out.println(line); + if (!process.isAlive()) { + System.out.println("[Process " + finalI + "] exit value: " + process.exitValue()); } } + } finally { + latch.countDown(); } - latch.countDown(); }).start(); } latch.await(); From 45a66303474f498827306da0d7607a854a83ad22 Mon Sep 17 00:00:00 2001 From: istudies Date: Tue, 31 Aug 2021 16:26:57 +0800 Subject: [PATCH 11/16] Optimize summary interrupt exit printing --- src/client/jTPCCRunner.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/client/jTPCCRunner.java b/src/client/jTPCCRunner.java index 07cd69a..8316a61 100644 --- a/src/client/jTPCCRunner.java +++ b/src/client/jTPCCRunner.java @@ -99,9 +99,13 @@ public static void main(String[] args) throws Exception { } private static void printSummary() { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); log.info("Term-00, "); log.info("Term-00, "); + if (tpmCSum.get() <= 0 || tpmTotalSum.get() <= 0) { + log.info("Term-00, Interrupt exit."); + return; + } + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); log.info("Term-00, Measured tpmC (NewOrders) = " + tpmCSum.get()); log.info("Term-00, Measured tpmTOTAL = " + tpmTotalSum.get()); log.info("Term-00, Session Start = " + sdf.format(sessionStart.get())); From 180454c2b534ad20a0bc13784d06355694e10c60 Mon Sep 17 00:00:00 2001 From: istudies Date: Tue, 31 Aug 2021 18:13:26 +0800 Subject: [PATCH 12/16] update log title and summary output format --- src/client/jTPCC.java | 14 ++++++++-- src/client/jTPCCRunner.java | 56 ++++++++++++++++++++++++++----------- src/client/jTPCCUtil.java | 15 ---------- 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/client/jTPCC.java b/src/client/jTPCC.java index 6f1a4ef..4cde36b 100644 --- a/src/client/jTPCC.java +++ b/src/client/jTPCC.java @@ -94,7 +94,15 @@ public jTPCC() { hasBatch = true; } if (!hasBatch) { - jTPCCUtil.printTitle(); + log.info("Term-00, "); + log.info("Term-00, +-------------------------------------------------------------+"); + log.info("Term-00, BenchmarkSQL v" + JTPCCVERSION); + log.info("Term-00, +-------------------------------------------------------------+"); + log.info("Term-00, (c) 2003, Raul Barbosa"); + log.info("Term-00, (c) 2004-2016, Denis Lussier"); + log.info("Term-00, (c) 2016, Jan Wieck"); + log.info("Term-00, +-------------------------------------------------------------+"); + log.info("Term-00, "); } String iDB = getProp(ini, "db"); String iDriver = getProp(ini, "driver"); @@ -631,8 +639,8 @@ private void endReport() { double tpmC = (6000000 * fastNewOrderCounter.longValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; double tpmTotal = (6000000 * transactionCount.longValue() / (currTimeMillis - sessionStartTimestamp)) / 100.0; - log.info("Term-00, "); - log.info("Term-00, "); + log.info("Term-00,\t"); + log.info("Term-00,\t"); log.info("Term-00, Measured tpmC (NewOrders) = " + tpmC); log.info("Term-00, Measured tpmTOTAL = " + tpmTotal); log.info("Term-00, Session Start = " + sessionStart); diff --git a/src/client/jTPCCRunner.java b/src/client/jTPCCRunner.java index 8316a61..2c91be4 100644 --- a/src/client/jTPCCRunner.java +++ b/src/client/jTPCCRunner.java @@ -1,5 +1,3 @@ -import org.apache.log4j.Logger; - import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; @@ -12,8 +10,6 @@ import java.util.concurrent.locks.ReentrantLock; public class jTPCCRunner { - private static Logger log = Logger.getLogger(jTPCC.class); - private static CountDownLatch latch; private static final Lock lock = new ReentrantLock(); @@ -24,6 +20,9 @@ public class jTPCCRunner { private static final AtomicLong tranCountSum = new AtomicLong(0L); private static final Map executeTimeMap = new ConcurrentHashMap<>(); + private static final AtomicReference threadName = new AtomicReference<>(""); + private static final SimpleDateFormat logDateSDF = new SimpleDateFormat("HH:mm:ss,SSS"); + public static void main(String[] args) throws Exception { String prop = System.getProperty("prop"); String runIDStr = System.getProperty("runID"); @@ -44,7 +43,20 @@ public static void main(String[] args) throws Exception { int runID = Integer.parseInt(runIDStr); List processList = new ArrayList<>(); - jTPCCUtil.printTitle(); + System.out.printf("%s [main] INFO jTPCC : Term-00,%n", logDateSDF.format(new Date())); + System.out.printf("%s [main] INFO jTPCC : Term-00, +-------------------------------------------------------------+%n", + logDateSDF.format(new Date())); + System.out.printf("%s [main] INFO jTPCC : Term-00, BenchmarkSQL v%s%n", + logDateSDF.format(new Date()), jTPCCConfig.JTPCCVERSION); + System.out.printf("%s [main] INFO jTPCC : Term-00, +-------------------------------------------------------------+%n", + logDateSDF.format(new Date())); + System.out.printf("%s [main] INFO jTPCC : Term-00, (c) 2003, Raul Barbosa%n", logDateSDF.format(new Date())); + System.out.printf("%s [main] INFO jTPCC : Term-00, (c) 2004-2016, Denis Lussier%n", logDateSDF.format(new Date())); + System.out.printf("%s [main] INFO jTPCC : Term-00, (c) 2016, Jan Wieck%n", logDateSDF.format(new Date())); + System.out.printf("%s [main] INFO jTPCC : Term-00, +-------------------------------------------------------------+%n", + logDateSDF.format(new Date())); + System.out.printf("%s [main] INFO jTPCC : Term-00,%n", logDateSDF.format(new Date())); + for (int i = 0; i < processor; i++) { runID++; // System.out.println("&&&&&&& runID: " + runID); @@ -79,6 +91,12 @@ public static void main(String[] args) throws Exception { System.out.println("[Process " + finalI + "] " + line); } else { if (finalI == 0) { + if (line.contains("Term-00,\t")) { + System.out.println("------" + line); + // 17:06:48,261 [Thread-297] INFO jTPCC : Term-00, + String lineRight = line.substring(line.indexOf("[") + 1); + threadName.set(lineRight.substring(0, lineRight.indexOf("]"))); + } System.out.println(line); } } @@ -99,21 +117,27 @@ public static void main(String[] args) throws Exception { } private static void printSummary() { - log.info("Term-00, "); - log.info("Term-00, "); - if (tpmCSum.get() <= 0 || tpmTotalSum.get() <= 0) { - log.info("Term-00, Interrupt exit."); + if (tpmCSum.get() <= 0 || tpmTotalSum.get() <= 0 || "".equals(threadName.get())) { + System.out.println("Interrupt exit..."); return; } - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - log.info("Term-00, Measured tpmC (NewOrders) = " + tpmCSum.get()); - log.info("Term-00, Measured tpmTOTAL = " + tpmTotalSum.get()); - log.info("Term-00, Session Start = " + sdf.format(sessionStart.get())); - log.info("Term-00, Session End = " + sdf.format(sessionEnd.get())); - log.info("Term-00, Transaction Count = " + (tranCountSum.longValue() - 1)); + System.out.printf("%s [%s] INFO jTPCC : Term-00, Measured tpmC (NewOrders) = %s%n", + logDateSDF.format(new Date()), threadName.get(), tpmCSum.get()); + System.out.printf("%s [%s] INFO jTPCC : Term-00, Measured tpmTOTAL = %s%n", + logDateSDF.format(new Date()), threadName.get(), tpmTotalSum.get()); + + SimpleDateFormat sessionDateSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + System.out.printf("%s [%s] INFO jTPCC : Term-00, Session Start = %s%n", + logDateSDF.format(new Date()), threadName.get(), sessionDateSDF.format(sessionStart.get())); + System.out.printf("%s [%s] INFO jTPCC : Term-00, Session End = %s%n", + logDateSDF.format(new Date()), threadName.get(), sessionDateSDF.format(sessionEnd.get())); + System.out.printf("%s [%s] INFO jTPCC : Term-00, Transaction Count = %s%n", + logDateSDF.format(new Date()), threadName.get(), (tranCountSum.longValue() - 1)); + for (String key : executeTimeMap.keySet()) { long value = executeTimeMap.get(key).longValue(); - log.info(key + "=" + value); + System.out.printf("%s [%s] INFO jTPCC : %s=%s%n", + logDateSDF.format(new Date()), threadName.get(), key, value); } } diff --git a/src/client/jTPCCUtil.java b/src/client/jTPCCUtil.java index 2673f80..b2b2964 100644 --- a/src/client/jTPCCUtil.java +++ b/src/client/jTPCCUtil.java @@ -9,8 +9,6 @@ */ -import org.apache.log4j.Logger; - import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -18,7 +16,6 @@ import java.util.Properties; public class jTPCCUtil implements jTPCCConfig { - private static Logger log = Logger.getLogger(jTPCC.class); private static Connection dbConn = null; private static PreparedStatement stmtGetConfig = null; @@ -36,18 +33,6 @@ public static String getSysProp(String inSysProperty, String defaultValue) { } // end getSysProp - public static void printTitle() { - log.info("Term-00, "); - log.info("Term-00, +-------------------------------------------------------------+"); - log.info("Term-00, BenchmarkSQL v" + JTPCCVERSION); - log.info("Term-00, +-------------------------------------------------------------+"); - log.info("Term-00, (c) 2003, Raul Barbosa"); - log.info("Term-00, (c) 2004-2016, Denis Lussier"); - log.info("Term-00, (c) 2016, Jan Wieck"); - log.info("Term-00, +-------------------------------------------------------------+"); - log.info("Term-00, "); - } - public static String randomStr(long strLen) { char freshChar; From 418cd4bf96350eb4d609c7e90b18580c35ed2d19 Mon Sep 17 00:00:00 2001 From: istudies Date: Tue, 31 Aug 2021 18:21:08 +0800 Subject: [PATCH 13/16] remove debug log --- src/client/jTPCCRunner.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/client/jTPCCRunner.java b/src/client/jTPCCRunner.java index 2c91be4..044d59e 100644 --- a/src/client/jTPCCRunner.java +++ b/src/client/jTPCCRunner.java @@ -92,7 +92,6 @@ public static void main(String[] args) throws Exception { } else { if (finalI == 0) { if (line.contains("Term-00,\t")) { - System.out.println("------" + line); // 17:06:48,261 [Thread-297] INFO jTPCC : Term-00, String lineRight = line.substring(line.indexOf("[") + 1); threadName.set(lineRight.substring(0, lineRight.indexOf("]"))); From 889b32dfbfc2d3bc525ac6b2a45743b00a08e6f2 Mon Sep 17 00:00:00 2001 From: istudies Date: Wed, 1 Sep 2021 14:40:17 +0800 Subject: [PATCH 14/16] Async merge logs --- src/client/jTPCCRunner.java | 47 +++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/src/client/jTPCCRunner.java b/src/client/jTPCCRunner.java index 044d59e..6b7ea09 100644 --- a/src/client/jTPCCRunner.java +++ b/src/client/jTPCCRunner.java @@ -1,8 +1,10 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.LongAdder; @@ -23,6 +25,8 @@ public class jTPCCRunner { private static final AtomicReference threadName = new AtomicReference<>(""); private static final SimpleDateFormat logDateSDF = new SimpleDateFormat("HH:mm:ss,SSS"); + private static final String FINISH_FLAG = "==finished=="; + public static void main(String[] args) throws Exception { String prop = System.getProperty("prop"); String runIDStr = System.getProperty("runID"); @@ -43,6 +47,8 @@ public static void main(String[] args) throws Exception { int runID = Integer.parseInt(runIDStr); List processList = new ArrayList<>(); + List> queues = new ArrayList<>(); + System.out.printf("%s [main] INFO jTPCC : Term-00,%n", logDateSDF.format(new Date())); System.out.printf("%s [main] INFO jTPCC : Term-00, +-------------------------------------------------------------+%n", logDateSDF.format(new Date())); @@ -67,18 +73,42 @@ public static void main(String[] args) throws Exception { processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); + processList.add(process); + queues.add(new LinkedBlockingQueue<>()); + } + + boolean finalDebug = debug; + for (int i = 0; i < processor; i++) { + int finalI = i; + + new Thread(() -> { + Process process = processList.get(finalI); + BlockingQueue queue = queues.get(finalI); + Scanner scanner = new Scanner(process.getInputStream()); + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + queue.offer(line); + } + queue.offer(FINISH_FLAG); + if (finalDebug) { + if (!process.isAlive()) { + System.out.println("[Process " + finalI + "] exit value: " + process.exitValue()); + } + } + }).start(); } for (int i = 0; i < processor; i++) { int finalI = i; - boolean finalDebug = debug; new Thread(() -> { try { - Process process = processList.get(finalI); - Scanner scanner = new Scanner(process.getInputStream()); - while (scanner.hasNextLine()) { - String line = scanner.nextLine(); + for (; ; ) { + String line = queues.get(finalI).take(); + if (line.trim().equals(FINISH_FLAG)) { +// System.out.println(FINISH_FLAG); + break; + } if (line.trim().isEmpty()) { continue; } @@ -100,11 +130,8 @@ public static void main(String[] args) throws Exception { } } } - if (finalDebug) { - if (!process.isAlive()) { - System.out.println("[Process " + finalI + "] exit value: " + process.exitValue()); - } - } + } catch (InterruptedException e) { + System.out.printf("%s [main] INFO jTPCC : %s%n", logDateSDF.format(new Date()), e.getMessage()); } finally { latch.countDown(); } From 107971d9aa621fdb44130bee362b7664a84eb763 Mon Sep 17 00:00:00 2001 From: istudies Date: Wed, 1 Sep 2021 17:44:23 +0800 Subject: [PATCH 15/16] update exec command --- src/client/jTPCCRunner.java | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/client/jTPCCRunner.java b/src/client/jTPCCRunner.java index 6b7ea09..921f3c6 100644 --- a/src/client/jTPCCRunner.java +++ b/src/client/jTPCCRunner.java @@ -1,10 +1,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.LongAdder; @@ -66,13 +63,19 @@ public static void main(String[] args) throws Exception { for (int i = 0; i < processor; i++) { runID++; // System.out.println("&&&&&&& runID: " + runID); - ProcessBuilder processBuilder = new ProcessBuilder(); - processBuilder.redirectErrorStream(true); - processBuilder.command("java", "-cp", ".:../lib/mysql/*:../lib/*:../dist/*", - "-Dprop=" + prop, "-DrunID=" + runID, "-Dprocessor=" + processor, "-Dbatch=1", "jTPCC"); - processBuilder.redirectErrorStream(true); - Process process = processBuilder.start(); +// ProcessBuilder processBuilder = new ProcessBuilder(); +// processBuilder.redirectErrorStream(true); +// processBuilder.command("java", "-cp", ".:../lib/mysql/*:../lib/*:../dist/*", +// "-Dprop=" + prop, "-DrunID=" + runID, "-Dprocessor=" + processor, "-Dbatch=1", "jTPCC"); +// processBuilder.redirectErrorStream(true); +// Process process = processBuilder.start(); + + List cmds = Arrays.asList("nohup", "java", "-cp", ".:../lib/mysql/*:../lib/*:../dist/*", + "-Dprop=" + prop, "-DrunID=" + runID, "-Dprocessor=" + processor, "-Dbatch=1", "jTPCC", "&"); + Process process = Runtime.getRuntime().exec(cmds.toArray(new String[0])); + process.waitFor(0, TimeUnit.SECONDS); + Thread.sleep(3000); processList.add(process); queues.add(new LinkedBlockingQueue<>()); From 129d86529ce1d35fa78b92d7abf3c0d2ca72e07e Mon Sep 17 00:00:00 2001 From: istudies Date: Wed, 1 Sep 2021 22:22:00 +0800 Subject: [PATCH 16/16] handle 'Average tpmTOTAL' and 'Current tpmTOTAL' log output --- src/client/jTPCCRunner.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/client/jTPCCRunner.java b/src/client/jTPCCRunner.java index 921f3c6..044fe61 100644 --- a/src/client/jTPCCRunner.java +++ b/src/client/jTPCCRunner.java @@ -129,7 +129,17 @@ public static void main(String[] args) throws Exception { String lineRight = line.substring(line.indexOf("[") + 1); threadName.set(lineRight.substring(0, lineRight.indexOf("]"))); } - System.out.println(line); + if (line.contains("Running Average tpmTOTAL")) { + // Term-00, Running Average tpmTOTAL: 60244.99 Current tpmTOTAL: 801540 Memory Usage: 49MB / 1596MB + String avgTpmTotalStr = line.substring(line.indexOf(":") + 1, line.indexOf("Current")); + double avgTpmTotal = Double.parseDouble(avgTpmTotalStr.trim()) * processor; + String crtTpmTotalStr = line.substring(line.indexOf("Current") + 17, line.indexOf("Memory")); + long crtTpmTotal = Long.parseLong(crtTpmTotalStr.trim()) * processor; + System.out.printf("Term-00, Running Average tpmTOTAL: %s Current tpmTOTAL: %s %s%n", + avgTpmTotal, crtTpmTotal, line.substring(line.indexOf("Memory"))); + } else { + System.out.println(line); + } } } }