├── .gitignore
├── README.md
├── database
├── add_fkey_idx.sql
├── create_tables.sql
└── load_data.sql
├── pom.xml
├── src
└── main
│ ├── java
│ └── com
│ │ └── codefutures
│ │ └── tpcc
│ │ ├── AbortedTransactionException.java
│ │ ├── Counter.java
│ │ ├── Delivery.java
│ │ ├── Driver.java
│ │ ├── Load.java
│ │ ├── NamedThreadFactory.java
│ │ ├── NewOrder.java
│ │ ├── OrderStat.java
│ │ ├── Payment.java
│ │ ├── RtHist.java
│ │ ├── Slev.java
│ │ ├── Tpcc.java
│ │ ├── TpccConstants.java
│ │ ├── TpccLoad.java
│ │ ├── TpccLoadConfig.java
│ │ ├── TpccStatements.java
│ │ ├── TpccThread.java
│ │ ├── Util.java
│ │ └── load
│ │ ├── FileLoader.java
│ │ ├── JdbcPreparedStatementLoader.java
│ │ ├── JdbcStatementLoader.java
│ │ ├── Record.java
│ │ └── RecordLoader.java
│ └── resources
│ └── log4j2.xml
└── tpcc.properties
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | .idea
3 | *.iml
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Java TPC-C
2 | ==========
3 |
4 | This project is a Java implementation of the TPC-C benchmark.
5 |
6 | =========
7 | Compiling
8 | =========
9 |
10 | Use this command to compile the code and produce a fat jar.
11 |
12 | ```
13 | mvn package assembly:single
14 | ```
15 |
16 | ========
17 | Database
18 | ========
19 |
20 | To create the tpcc schema in MySQL:
21 |
22 | ```
23 | cd database
24 | mysql -u root
25 | > create database tpcc;
26 | > use tpcc;
27 | > source create_tables.sql
28 | > source add_fkey_idx.sql
29 | ```
30 |
31 | It is possible to load data without the foreign keys and indexes in place and then add those
32 | after loading data to improve loading times.
33 |
34 | =================================
35 | Generating and loading TPC-C data
36 | =================================
37 |
38 | Data can be loaded directly into a MySQL instance and can also be generated to CSV files that
39 | can be loaded into MySQL later using LOAD DATA INFILE.
40 |
41 | In `tpcc.properties` set the MODE to either CSV or JDBC.
42 |
43 | To run the load process:
44 |
45 | ```
46 | java -classpath target/tpcc-1.0.0-SNAPSHOT-jar-with-dependencies.jar com.codefutures.tpcc.TpccLoad
47 | ```
48 |
49 | It is possible to load data into shards where the warehouse ID is used as a shard key. The
50 | SHARDCOUNT and SHARDID properties must be set correctly when generating or loading data.
51 |
52 | This option requires the use of a JDBC driver that supports automatic sharding, such as
53 | dbShards (http://www.dbshards.com).
54 |
55 | ===========================
56 | Running the TPC-C Benchmark
57 | ===========================
58 |
59 | Review the TPC-C settings in `tpcc.properties`, then run this command To run the tpcc benchmarks:
60 |
61 | ```
62 | java -classpath target/tpcc-1.0.0-SNAPSHOT-jar-with-dependencies.jar com.codefutures.tpcc.Tpcc
63 | ```
64 |
65 | Bugs can be reported to support@codefutures.com.
66 |
67 | (c) 2014 CodeFutures Corporation.
68 |
--------------------------------------------------------------------------------
/database/add_fkey_idx.sql:
--------------------------------------------------------------------------------
1 | SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
2 | SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
3 |
4 | CREATE INDEX idx_customer ON customer (c_w_id,c_d_id,c_last,c_first);
5 | CREATE INDEX idx_orders ON orders (o_w_id,o_d_id,o_c_id,o_id);
6 | CREATE INDEX fkey_stock_2 ON stock (s_i_id);
7 | CREATE INDEX fkey_order_line_2 ON order_line (ol_supply_w_id,ol_i_id);
8 |
9 | ALTER TABLE district ADD CONSTRAINT fkey_district_1 FOREIGN KEY(d_w_id) REFERENCES warehouse(w_id);
10 | ALTER TABLE customer ADD CONSTRAINT fkey_customer_1 FOREIGN KEY(c_w_id,c_d_id) REFERENCES district(d_w_id,d_id);
11 | ALTER TABLE history ADD CONSTRAINT fkey_history_2 FOREIGN KEY(h_w_id,h_d_id) REFERENCES district(d_w_id,d_id);
12 | ALTER TABLE new_orders ADD CONSTRAINT fkey_new_orders_1 FOREIGN KEY(no_w_id,no_d_id,no_o_id) REFERENCES orders(o_w_id,o_d_id,o_id);
13 | ALTER TABLE orders ADD CONSTRAINT fkey_orders_1 FOREIGN KEY(o_w_id,o_d_id,o_c_id) REFERENCES customer(c_w_id,c_d_id,c_id);
14 | ALTER TABLE order_line ADD CONSTRAINT fkey_order_line_1 FOREIGN KEY(ol_w_id,ol_d_id,ol_o_id) REFERENCES orders(o_w_id,o_d_id,o_id);
15 | ALTER TABLE stock ADD CONSTRAINT fkey_stock_1 FOREIGN KEY(s_w_id) REFERENCES warehouse(w_id);
16 | ALTER TABLE stock ADD CONSTRAINT fkey_stock_2 FOREIGN KEY(s_i_id) REFERENCES item(i_id);
17 |
18 |
19 | #NOTE: the following FKs are not shard-safe since they can reference a warehouse in another shard
20 | #ALTER TABLE order_line ADD CONSTRAINT fkey_order_line_2 FOREIGN KEY(ol_supply_w_id,ol_i_id) REFERENCES stock(s_w_id,s_i_id);
21 | #ALTER TABLE history ADD CONSTRAINT fkey_history_1 FOREIGN KEY(h_c_w_id,h_c_d_id,h_c_id) REFERENCES customer(c_w_id,c_d_id,c_id);
22 |
23 |
24 | SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
25 | SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
26 |
--------------------------------------------------------------------------------
/database/create_tables.sql:
--------------------------------------------------------------------------------
1 | SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
2 | SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
3 |
4 | drop table if exists warehouse;
5 |
6 | create table warehouse (
7 | w_id smallint not null,
8 | w_name varchar(10),
9 | w_street_1 varchar(20),
10 | w_street_2 varchar(20),
11 | w_city varchar(20),
12 | w_state char(2),
13 | w_zip char(9),
14 | w_tax decimal(4,2),
15 | w_ytd decimal(12,2),
16 | primary key (w_id) ) Engine=InnoDB;
17 |
18 | drop table if exists district;
19 |
20 | create table district (
21 | d_id tinyint not null,
22 | d_w_id smallint not null,
23 | d_name varchar(10),
24 | d_street_1 varchar(20),
25 | d_street_2 varchar(20),
26 | d_city varchar(20),
27 | d_state char(2),
28 | d_zip char(9),
29 | d_tax decimal(4,2),
30 | d_ytd decimal(12,2),
31 | d_next_o_id int,
32 | primary key (d_w_id, d_id) ) Engine=InnoDB;
33 |
34 | drop table if exists customer;
35 |
36 | create table customer (
37 | c_id int not null,
38 | c_d_id tinyint not null,
39 | c_w_id smallint not null,
40 | c_first varchar(16),
41 | c_middle char(2),
42 | c_last varchar(16),
43 | c_street_1 varchar(20),
44 | c_street_2 varchar(20),
45 | c_city varchar(20),
46 | c_state char(2),
47 | c_zip char(9),
48 | c_phone char(16),
49 | c_since datetime,
50 | c_credit char(2),
51 | c_credit_lim bigint,
52 | c_discount decimal(4,2),
53 | c_balance decimal(12,2),
54 | c_ytd_payment decimal(12,2),
55 | c_payment_cnt smallint,
56 | c_delivery_cnt smallint,
57 | c_data text,
58 | PRIMARY KEY(c_w_id, c_d_id, c_id) ) Engine=InnoDB;
59 |
60 | drop table if exists history;
61 |
62 | create table history (
63 | h_c_id int,
64 | h_c_d_id tinyint,
65 | h_c_w_id smallint,
66 | h_d_id tinyint,
67 | h_w_id smallint,
68 | h_date datetime,
69 | h_amount decimal(6,2),
70 | h_data varchar(24) ) Engine=InnoDB;
71 |
72 | drop table if exists new_orders;
73 |
74 | create table new_orders (
75 | no_o_id int not null,
76 | no_d_id tinyint not null,
77 | no_w_id smallint not null,
78 | PRIMARY KEY(no_w_id, no_d_id, no_o_id)) Engine=InnoDB;
79 |
80 | drop table if exists orders;
81 |
82 | create table orders (
83 | o_id int not null,
84 | o_d_id tinyint not null,
85 | o_w_id smallint not null,
86 | o_c_id int,
87 | o_entry_d datetime,
88 | o_carrier_id tinyint,
89 | o_ol_cnt tinyint,
90 | o_all_local tinyint,
91 | PRIMARY KEY(o_w_id, o_d_id, o_id) ) Engine=InnoDB ;
92 |
93 | drop table if exists order_line;
94 |
95 | create table order_line (
96 | ol_o_id int not null,
97 | ol_d_id tinyint not null,
98 | ol_w_id smallint not null,
99 | ol_number tinyint not null,
100 | ol_i_id int,
101 | ol_supply_w_id smallint,
102 | ol_delivery_d datetime,
103 | ol_quantity tinyint,
104 | ol_amount decimal(6,2),
105 | ol_dist_info char(24),
106 | PRIMARY KEY(ol_w_id, ol_d_id, ol_o_id, ol_number) ) Engine=InnoDB ;
107 |
108 | drop table if exists item;
109 |
110 | create table item (
111 | i_id int not null,
112 | i_im_id int,
113 | i_name varchar(24),
114 | i_price decimal(5,2),
115 | i_data varchar(50),
116 | PRIMARY KEY(i_id) ) Engine=InnoDB;
117 |
118 | drop table if exists stock;
119 |
120 | create table stock (
121 | s_i_id int not null,
122 | s_w_id smallint not null,
123 | s_quantity smallint,
124 | s_dist_01 char(24),
125 | s_dist_02 char(24),
126 | s_dist_03 char(24),
127 | s_dist_04 char(24),
128 | s_dist_05 char(24),
129 | s_dist_06 char(24),
130 | s_dist_07 char(24),
131 | s_dist_08 char(24),
132 | s_dist_09 char(24),
133 | s_dist_10 char(24),
134 | s_ytd decimal(8,0),
135 | s_order_cnt smallint,
136 | s_remote_cnt smallint,
137 | s_data varchar(50),
138 | PRIMARY KEY(s_w_id, s_i_id) ) Engine=InnoDB ;
139 |
140 | SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
141 | SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
142 |
143 |
--------------------------------------------------------------------------------
/database/load_data.sql:
--------------------------------------------------------------------------------
1 | LOAD DATA INFILE 'warehouse.txt' INTO TABLE warehouse;
2 | LOAD DATA INFILE 'district.txt' INTO TABLE district;
3 | LOAD DATA INFILE 'customer.txt' INTO TABLE customer;
4 | LOAD DATA INFILE 'history.txt' INTO TABLE history;
5 | LOAD DATA INFILE 'orders.txt' INTO TABLE orders;
6 | LOAD DATA INFILE 'new_orders.txt' INTO TABLE new_orders;
7 | LOAD DATA INFILE 'order_line.txt' INTO TABLE order_line;
8 | LOAD DATA INFILE 'item.txt' INTO TABLE item;
9 | LOAD DATA INFILE 'stock.txt' INTO TABLE stock;
10 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.codefutures
8 | tpcc
9 | tpcc
10 | 1.0.0-SNAPSHOT
11 | jar
12 |
13 |
14 |
15 | Apache License 2.0
16 | http://www.apache.org/licenses/LICENSE-2.0
17 |
18 |
19 |
20 |
21 | CodeFutures
22 | http://www.codefutures.com
23 |
24 |
25 |
26 |
27 |
28 | mysql
29 | mysql-connector-java
30 | 6.0.4
31 |
32 |
33 |
34 | org.slf4j
35 | slf4j-api
36 | 1.7.7
37 |
38 |
39 |
40 | org.apache.logging.log4j
41 | log4j-api
42 | 2.0.2
43 |
44 |
45 |
46 | org.apache.logging.log4j
47 | log4j-core
48 | 2.0.2
49 |
50 |
51 |
52 | org.apache.logging.log4j
53 | log4j-slf4j-impl
54 | 2.0.2
55 |
56 |
57 |
58 | commons-cli
59 | commons-cli
60 | 1.2
61 |
62 |
63 |
64 |
65 |
66 | junit
67 | junit
68 | 4.8.2
69 | test
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 | org.apache.maven.plugins
80 | maven-compiler-plugin
81 |
82 | 1.8
83 | 1.8
84 |
85 |
86 |
87 |
88 | maven-assembly-plugin
89 |
90 |
91 | package
92 |
93 | single
94 |
95 |
96 |
97 |
98 |
99 | jar-with-dependencies
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
--------------------------------------------------------------------------------
/src/main/java/com/codefutures/tpcc/AbortedTransactionException.java:
--------------------------------------------------------------------------------
1 | package com.codefutures.tpcc;
2 |
3 | public class AbortedTransactionException extends Exception {
4 | public AbortedTransactionException() {
5 | super();
6 | }
7 |
8 | public AbortedTransactionException(String message) {
9 | super(message);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/codefutures/tpcc/Counter.java:
--------------------------------------------------------------------------------
1 | package com.codefutures.tpcc;
2 |
3 | public class Counter {
4 |
5 | private long count = 0;
6 |
7 | public Counter() {
8 | }
9 |
10 | public synchronized long increment() {
11 | return ++count;
12 | }
13 |
14 | public synchronized long get() {
15 | return count;
16 | }
17 |
18 | public synchronized long reset() {
19 | long ret = count;
20 | count = 0;
21 | return ret;
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/codefutures/tpcc/Delivery.java:
--------------------------------------------------------------------------------
1 | package com.codefutures.tpcc;
2 |
3 | import java.sql.*;
4 | import java.util.Calendar;
5 | import java.util.Date;
6 |
7 | import org.slf4j.LoggerFactory;
8 | import org.slf4j.Logger;
9 |
10 | public class Delivery implements TpccConstants {
11 | private static final Logger logger = LoggerFactory.getLogger(Driver.class);
12 | private static final boolean DEBUG = logger.isDebugEnabled();
13 | private static final boolean TRACE = logger.isTraceEnabled();
14 |
15 | private TpccStatements pStmts;
16 |
17 | public Delivery(TpccStatements pStmts) {
18 | this.pStmts = pStmts;
19 | }
20 |
21 | public int delivery(int w_id_arg, int o_carrier_id_arg) {
22 | try {
23 | // Start a transaction.
24 | pStmts.setAutoCommit(false);
25 | if (DEBUG) logger.debug("Transaction: Delivery");
26 | int w_id = w_id_arg;
27 | int o_carrier_id = o_carrier_id_arg;
28 | int d_id = 0;
29 | int c_id = 0;
30 | int no_o_id = 0;
31 | float ol_total = 0;
32 |
33 | Calendar calendar = Calendar.getInstance();
34 | Date now = calendar.getTime();
35 | Timestamp currentTimeStamp = new Timestamp(now.getTime());
36 |
37 | for (d_id = 1; d_id <= DIST_PER_WARE; d_id++) {
38 |
39 |
40 | // Get the prepared statement.
41 | //"SELECT COALESCE(MIN(no_o_id),0) FROM new_orders WHERE no_d_id = ? AND no_w_id = ?"
42 | if (TRACE)
43 | logger.trace("SELECT COALESCE(MIN(no_o_id),0) FROM new_orders WHERE no_d_id = " + d_id + " AND no_w_id = " + w_id);
44 | try {
45 | pStmts.getStatement(25).setInt(1, d_id);
46 | pStmts.getStatement(25).setInt(2, w_id);
47 | ResultSet rs = pStmts.getStatement(25).executeQuery();
48 |
49 | if (rs.next()) {
50 | no_o_id = rs.getInt(1);
51 | }
52 |
53 | rs.close();
54 | } catch (SQLException e) {
55 | logger.error("SELECT COALESCE(MIN(no_o_id),0) FROM new_orders WHERE no_d_id = " + d_id + " AND no_w_id = " + w_id, e);
56 | throw new Exception("Delivery Select transaction error", e);
57 | }
58 |
59 | if (no_o_id == 0) {
60 | continue;
61 | } else {
62 | if (DEBUG) logger.debug("No_o_id did not equal 0 -> " + no_o_id);
63 | }
64 |
65 | //Get the prepared statement
66 | //"DELETE FROM new_orders WHERE no_o_id = ? AND no_d_id = ? AND no_w_id = ?"
67 | if (TRACE)
68 | logger.trace("DELETE FROM new_orders WHERE no_o_id = " + no_o_id + " AND no_d_id = " + d_id + " AND no_w_id = " + w_id);
69 | try {
70 | pStmts.getStatement(26).setInt(1, no_o_id);
71 | pStmts.getStatement(26).setInt(2, d_id);
72 | pStmts.getStatement(26).setInt(3, w_id);
73 | pStmts.getStatement(26).executeUpdate();
74 |
75 |
76 | } catch (SQLException e) {
77 | logger.error("DELETE FROM new_orders WHERE no_o_id = " + no_o_id + " AND no_d_id = " + d_id + " AND no_w_id = " + w_id, e);
78 | throw new Exception(" Delivery Delete transaction error", e);
79 | }
80 |
81 |
82 | //Get the prepared statement
83 | //"SELECT o_c_id FROM orders WHERE o_id = ? AND o_d_id = ? AND o_w_id = ?"
84 | if (TRACE)
85 | logger.trace("SELECT o_c_id FROM orders WHERE o_id = " + no_o_id + " AND o_d_id = " + d_id + " AND o_w_id = " + w_id);
86 | try {
87 | pStmts.getStatement(27).setInt(1, no_o_id);
88 | pStmts.getStatement(27).setInt(2, d_id);
89 | pStmts.getStatement(27).setInt(3, w_id);
90 | ResultSet rs = pStmts.getStatement(27).executeQuery();
91 |
92 | if (rs.next()) {
93 | c_id = rs.getInt(1);
94 | }
95 |
96 |
97 | rs.close();
98 | } catch (SQLException e) {
99 | logger.error("SELECT o_c_id FROM orders WHERE o_id = " + no_o_id + " AND o_d_id = " + d_id + " AND o_w_id = " + w_id, e);
100 | throw new Exception(" Delivery Select transaction error", e);
101 | }
102 |
103 | //Get the prepared Statement
104 | //"UPDATE orders SET o_carrier_id = ? WHERE o_id = ? AND o_d_id = ? AND o_w_id = ?"
105 | if (TRACE)
106 | logger.trace("UPDATE orders SET o_carrier_id = " + o_carrier_id + " WHERE o_id = " + no_o_id + " AND o_d_id = " + d_id + " AND o_w_id = " + w_id);
107 | try {
108 | pStmts.getStatement(28).setInt(1, o_carrier_id);
109 | pStmts.getStatement(28).setInt(2, no_o_id);
110 | pStmts.getStatement(28).setInt(3, d_id);
111 | pStmts.getStatement(28).setInt(4, w_id);
112 | pStmts.getStatement(28).executeUpdate();
113 |
114 |
115 | } catch (SQLException e) {
116 | logger.error("UPDATE orders SET o_carrier_id = " + o_carrier_id + " WHERE o_id = " + no_o_id + " AND o_d_id = " + d_id + " AND o_w_id = " + w_id, e);
117 | throw new Exception("Delivery Update transcation error", e);
118 | }
119 |
120 |
121 | //Get the prepared Statement
122 | //"UPDATE order_line SET ol_delivery_d = ? WHERE ol_o_id = ? AND ol_d_id = ? AND ol_w_id = ?"
123 | if (TRACE)
124 | logger.trace("UPDATE order_line SET ol_delivery_d = " + currentTimeStamp.toString() + " WHERE ol_o_id = " + no_o_id + " AND ol_d_id = " + d_id + " AND ol_w_id = " + w_id);
125 | try {
126 | pStmts.getStatement(29).setString(1, currentTimeStamp.toString());
127 | pStmts.getStatement(29).setInt(2, no_o_id);
128 | pStmts.getStatement(29).setInt(3, d_id);
129 | pStmts.getStatement(29).setInt(4, w_id);
130 | pStmts.getStatement(29).executeUpdate();
131 |
132 |
133 | } catch (SQLException e) {
134 | logger.error("UPDATE order_line SET ol_delivery_d = " + currentTimeStamp.toString() + " WHERE ol_o_id = " + no_o_id + " AND ol_d_id = " + d_id + " AND ol_w_id = " + w_id, e);
135 | throw new Exception("Delivery Update transaction error", e);
136 | }
137 |
138 |
139 | //Get the prepared Statement
140 | //"SELECT SUM(ol_amount) FROM order_line WHERE ol_o_id = ? AND ol_d_id = ? AND ol_w_id = ?"
141 | if (TRACE)
142 | logger.trace("SELECT SUM(ol_amount) FROM order_line WHERE ol_o_id = " + no_o_id + " AND ol_d_id = " + d_id + " AND ol_w_id = " + w_id);
143 | try {
144 | pStmts.getStatement(30).setInt(1, no_o_id);
145 | pStmts.getStatement(30).setInt(2, d_id);
146 | pStmts.getStatement(30).setInt(3, w_id);
147 | ResultSet rs = pStmts.getStatement(30).executeQuery();
148 | if (rs.next()) {
149 | ol_total = rs.getFloat(1);
150 | }
151 |
152 |
153 | rs.close();
154 | } catch (SQLException e) {
155 | logger.error("SELECT SUM(ol_amount) FROM order_line WHERE ol_o_id = " + no_o_id + " AND ol_d_id = " + d_id + " AND ol_w_id = " + w_id, e);
156 | throw new Exception("Delivery Select transaction error", e);
157 | }
158 |
159 |
160 | //Get the prepared statement
161 | //"UPDATE customer SET c_balance = c_balance + ? , c_delivery_cnt = c_delivery_cnt + 1 WHERE c_id = ? AND c_d_id = ? AND c_w_id = ?"
162 | if (TRACE)
163 | logger.trace("UPDATE customer SET c_balance = c_balance + " + ol_total + ", c_delivery_cnt = c_delivery_cnt + 1 WHERE c_id = " + c_id + " AND c_d_id = " + d_id + " AND c_w_id = " + w_id);
164 | try {
165 | pStmts.getStatement(31).setFloat(1, ol_total);
166 | pStmts.getStatement(31).setInt(2, c_id);
167 | pStmts.getStatement(31).setInt(3, d_id);
168 | pStmts.getStatement(31).setInt(4, w_id);
169 | pStmts.getStatement(31).executeUpdate();
170 |
171 |
172 | } catch (SQLException e) {
173 | logger.error("UPDATE customer SET c_balance = c_balance + " + ol_total + ", c_delivery_cnt = c_delivery_cnt + 1 WHERE c_id = " + c_id + " AND c_d_id = " + d_id + " AND c_w_id = " + w_id, e);
174 | throw new Exception("Delivery Update transaction error", e);
175 | }
176 | }
177 |
178 | // Commit.
179 | pStmts.commit();
180 |
181 | return 1;
182 |
183 | } catch (Exception e) {
184 | try {
185 | // Rollback if an aborted transaction, they are intentional in some percentage of cases.
186 | pStmts.rollback();
187 | return 0;
188 | } catch (Throwable th) {
189 | throw new RuntimeException("Delivery error", th);
190 | } finally {
191 | logger.error("Delivery error", e);
192 | }
193 | }
194 |
195 | }
196 |
197 | }
198 |
--------------------------------------------------------------------------------
/src/main/java/com/codefutures/tpcc/Driver.java:
--------------------------------------------------------------------------------
1 | package com.codefutures.tpcc;
2 |
3 | import java.sql.Connection;
4 | import java.sql.SQLException;
5 | import java.util.Arrays;
6 | import java.util.concurrent.*;
7 |
8 | import org.slf4j.LoggerFactory;
9 | import org.slf4j.Logger;
10 |
11 |
12 | public class Driver implements TpccConstants {
13 |
14 | private static final Logger logger = LoggerFactory.getLogger(Driver.class);
15 | private static final boolean DEBUG = logger.isDebugEnabled();
16 |
17 | /**
18 | * For debug use only.
19 | */
20 | private static final boolean DETECT_LOCK_WAIT_TIMEOUTS = false;
21 |
22 | /**
23 | * Can be disabled for debug use only.
24 | */
25 | private static final boolean ALLOW_MULTI_WAREHOUSE_TX = true;
26 |
27 | //CHECK: The following variables are externs??
28 | // public int counting_on;
29 | public int num_ware;
30 | public int num_conn;
31 |
32 | public int num_node;
33 | // public int time_count;
34 | // public PrintWriter freport_file;
35 |
36 | // total count for all threads
37 | private int[] success;
38 | private int[] late;
39 | private int[] retry;
40 | private int[] failure;
41 |
42 | // per thread counts
43 | private int[][] success2;
44 | private int[][] late2;
45 | private int[][] retry2;
46 | private int[][] failure2;
47 |
48 | public double[] max_rt = new double[TRANSACTION_COUNT];
49 |
50 | //Private variables
51 | private final int MAX_RETRY = 2000;
52 | private final int RTIME_NEWORD = 5 * 1000;
53 | private final int RTIME_PAYMENT = 5 * 1000;
54 | private final int RTIME_ORDSTAT = 5 * 1000;
55 | private final int RTIME_DELIVERY = 5 * 1000;
56 | private final int RTIME_SLEV = 20 * 1000;
57 |
58 | private Connection conn;
59 | private TpccStatements pStmts;
60 |
61 | // Transaction instances.
62 | private NewOrder newOrder;
63 | private Payment payment;
64 | private OrderStat orderStat;
65 | private Slev slev;
66 | private Delivery delivery;
67 |
68 | /**
69 | * Constructor.
70 | *
71 | * @param conn
72 | */
73 | public Driver(Connection conn, int fetchSize,
74 | int[] success, int[] late, int[] retry, int[] failure,
75 | int[][] success2, int[][] late2, int[][] retry2, int[][] failure2, boolean joins) {
76 | try {
77 | this.conn = conn;
78 |
79 | pStmts = new TpccStatements(conn, fetchSize);
80 |
81 | // Initialize the transactions.
82 | newOrder = new NewOrder(pStmts, joins);
83 | payment = new Payment(pStmts);
84 | orderStat = new OrderStat(pStmts);
85 | slev = new Slev(pStmts);
86 | delivery = new Delivery(pStmts);
87 |
88 | this.success = success;
89 | this.late = late;
90 | this.retry = retry;
91 | this.failure = failure;
92 |
93 | this.success2 = success2;
94 | this.late2 = late2;
95 | this.retry2 = retry2;
96 | this.failure2 = failure2;
97 |
98 | for (int i = 0; i < TRANSACTION_COUNT; i++) {
99 | max_rt[i] = 0.0;
100 | }
101 |
102 | } catch (Throwable th) {
103 | throw new RuntimeException("Error initializing Driver", th);
104 | }
105 | }
106 |
107 | private final Executor exec = Executors.newSingleThreadExecutor();
108 |
109 | public int runTransaction(final int t_num, final int numWare, final int numConn) {
110 |
111 | num_ware = numWare;
112 | num_conn = numConn;
113 |
114 | int count = 0;
115 |
116 | /* Actually, WaitTimes are needed... */
117 | //CHECK: Is activate_transaction handled correctly?
118 | int sequence = Util.seqGet();
119 | while (Tpcc.activate_transaction == 1) {
120 |
121 | try {
122 | if (DEBUG) logger.debug("BEFORE runTransaction: sequence: " + sequence);
123 |
124 | if (DETECT_LOCK_WAIT_TIMEOUTS) {
125 | final int _sequence = sequence;
126 | FutureTask t = new FutureTask