From e640e7da928221c5c67a9e13b5766c5ea3706589 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Fri, 8 Nov 2024 18:42:23 +0530 Subject: [PATCH 01/22] init --- benchmarks/tpcc/pom.xml | 7 + .../tpcc/AbstractBenchmarkRunner.java | 27 +- .../pgadapter/tpcc/BenchmarkApplication.java | 29 ++- .../tpcc/HibernateBenchmarkRunner.java | 137 ++++++++++ .../google/cloud/pgadapter/tpcc/Metrics.java | 83 ++++++ .../tpcc/config/TpccConfiguration.java | 8 +- .../pgadapter/tpcc/entities/Customer.java | 241 ++++++++++++++++++ .../pgadapter/tpcc/entities/District.java | 170 ++++++++++++ .../pgadapter/tpcc/entities/DistrictId.java | 60 +++++ .../pgadapter/tpcc/entities/History.java | 94 +++++++ .../cloud/pgadapter/tpcc/entities/Item.java | 68 +++++ .../pgadapter/tpcc/entities/NewOrder.java | 42 +++ .../cloud/pgadapter/tpcc/entities/Order.java | 87 +++++++ .../pgadapter/tpcc/entities/OrderLine.java | 47 ++++ .../cloud/pgadapter/tpcc/entities/Stock.java | 198 ++++++++++++++ .../pgadapter/tpcc/entities/Warehouse.java | 142 +++++++++++ .../src/main/resources/application.properties | 18 +- .../tpcc/src/main/resources/hibernate.cfg.xml | 39 +++ 18 files changed, 1481 insertions(+), 16 deletions(-) create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java create mode 100644 benchmarks/tpcc/src/main/resources/hibernate.cfg.xml diff --git a/benchmarks/tpcc/pom.xml b/benchmarks/tpcc/pom.xml index 3dd44ca7b..b00879af2 100644 --- a/benchmarks/tpcc/pom.xml +++ b/benchmarks/tpcc/pom.xml @@ -63,6 +63,13 @@ google-cloud-spanner-pgadapter 0.39.0 + + + com.google.cloud + google-cloud-spanner-hibernate-dialect + 3.4.1 + + io.opentelemetry opentelemetry-sdk diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java index 0ec89b173..79e464043 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java @@ -16,12 +16,15 @@ import com.google.cloud.pgadapter.tpcc.config.PGAdapterConfiguration; import com.google.cloud.pgadapter.tpcc.config.SpannerConfiguration; import com.google.cloud.pgadapter.tpcc.config.TpccConfiguration; +import com.google.cloud.spanner.AbortedException; import com.google.cloud.spanner.Dialect; import com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException; +import com.google.common.base.Stopwatch; import java.io.IOException; import java.math.BigDecimal; import java.sql.SQLException; import java.sql.Timestamp; +import java.time.Duration; import java.time.LocalDateTime; import java.util.List; import java.util.Objects; @@ -91,20 +94,31 @@ private void runTransactions() throws SQLException, InterruptedException { while (!Thread.interrupted()) { try { int transaction = random.nextInt(23); + Stopwatch stopwatch = Stopwatch.createStarted(); if (transaction < 10) { newOrder(); + Duration executionDuration = stopwatch.elapsed(); + metrics.recordNewOrderLatency(executionDuration.toMillis()); statistics.incNewOrder(); } else if (transaction < 20) { payment(); + Duration executionDuration = stopwatch.elapsed(); + metrics.recordPaymentLatency(executionDuration.toMillis()); statistics.incPayment(); } else if (transaction < 21) { orderStatus(); + Duration executionDuration = stopwatch.elapsed(); + metrics.recordOrderStatusLatency(executionDuration.toMillis()); statistics.incOrderStatus(); } else if (transaction < 22) { delivery(); + Duration executionDuration = stopwatch.elapsed(); + metrics.recordDeliveryLatency(executionDuration.toMillis()); statistics.incDelivery(); } else if (transaction < 23) { stockLevel(); + Duration executionDuration = stopwatch.elapsed(); + metrics.recordStockLevelLatency(executionDuration.toMillis()); statistics.incStockLevel(); } else { LOG.info("No transaction"); @@ -122,6 +136,9 @@ private void runTransactions() throws SQLException, InterruptedException { } else if (exception instanceof JdbcAbortedException) { LOG.debug("Transaction aborted by Cloud Spanner via Spanner JDBC"); statistics.incAborted(); + } else if (exception instanceof AbortedException) { + LOG.debug("Transaction aborted by Cloud Spanner via Spanner client"); + statistics.incAborted(); } else { LOG.warn("Transaction failed", exception); statistics.incFailed(); @@ -147,7 +164,7 @@ private long getOtherWarehouseId(long currentId) { } } - private void newOrder() throws SQLException { + public void newOrder() throws SQLException { LOG.debug("Executing new_order"); long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); @@ -288,7 +305,7 @@ private void newOrder() throws SQLException { executeStatement("commit"); } - private void payment() throws SQLException { + public void payment() throws SQLException { LOG.debug("Executing payment"); long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); @@ -455,7 +472,7 @@ private void payment() throws SQLException { executeStatement("commit"); } - private void orderStatus() throws SQLException { + public void orderStatus() throws SQLException { LOG.debug("Executing order_status"); long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); @@ -544,7 +561,7 @@ private void orderStatus() throws SQLException { executeStatement("commit"); } - private void delivery() throws SQLException { + public void delivery() throws SQLException { LOG.debug("Executing delivery"); long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); @@ -606,7 +623,7 @@ private void delivery() throws SQLException { executeStatement("commit"); } - private void stockLevel() throws SQLException { + public void stockLevel() throws SQLException { LOG.debug("Executing stock_level"); long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java index 996e859f4..354764aeb 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java @@ -36,12 +36,15 @@ import io.opentelemetry.sdk.metrics.export.MetricExporter; import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; import java.io.IOException; -import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import org.hibernate.SessionFactory; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; @@ -96,6 +99,8 @@ public void run(String... args) throws Exception { pgAdapterConfiguration.getMinSessions(), tpccConfiguration.getBenchmarkThreads()), Math.max( pgAdapterConfiguration.getMaxSessions(), tpccConfiguration.getBenchmarkThreads())); + StandardServiceRegistry registry = null; + SessionFactory sessionFactory = null; try { if (tpccConfiguration.isLoadData()) { boolean isClientLibGSQLRunner = @@ -134,6 +139,11 @@ public void run(String... args) throws Exception { Statistics statistics = new Statistics(tpccConfiguration); ExecutorService executor = Executors.newFixedThreadPool(tpccConfiguration.getBenchmarkThreads()); + + if (tpccConfiguration.getBenchmarkRunner().equals(TpccConfiguration.HIBERNATE_RUNNER)) { + registry = new StandardServiceRegistryBuilder().configure().build(); + sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); + } for (int i = 0; i < tpccConfiguration.getBenchmarkThreads(); i++) { if (tpccConfiguration .getBenchmarkRunner() @@ -187,6 +197,19 @@ public void run(String... args) throws Exception { spannerConfiguration, metrics, Dialect.GOOGLE_STANDARD_SQL)); + } else if (tpccConfiguration + .getBenchmarkRunner() + .equals(TpccConfiguration.HIBERNATE_RUNNER)) { + statistics.setRunnerName("Hibernate benchmark"); + executor.submit( + new HibernateBenchmarkRunner( + statistics, + sessionFactory, + tpccConfiguration, + pgAdapterConfiguration, + spannerConfiguration, + metrics, + Dialect.GOOGLE_STANDARD_SQL)); } } @@ -211,6 +234,10 @@ public void run(String... args) throws Exception { server.stopServer(); server.awaitTerminated(); } + if (sessionFactory != null) { + sessionFactory.close(); + registry.close(); + } } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java new file mode 100644 index 000000000..23ab81de2 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -0,0 +1,137 @@ +package com.google.cloud.pgadapter.tpcc; + +import com.google.cloud.pgadapter.tpcc.config.PGAdapterConfiguration; +import com.google.cloud.pgadapter.tpcc.config.SpannerConfiguration; +import com.google.cloud.pgadapter.tpcc.config.TpccConfiguration; +import com.google.cloud.pgadapter.tpcc.entities.District; +import com.google.cloud.pgadapter.tpcc.entities.DistrictId; +import com.google.cloud.spanner.Dialect; +import java.io.IOException; +import java.sql.SQLException; +import java.util.List; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; + +public class HibernateBenchmarkRunner extends AbstractBenchmarkRunner { + + private final SessionFactory sessionFactory; // SessionFactory injected + private final ThreadLocal sessionThreadLocal = new ThreadLocal<>(); + + HibernateBenchmarkRunner( + Statistics statistics, + SessionFactory sessionFactory, // Injected SessionFactory + TpccConfiguration tpccConfiguration, + PGAdapterConfiguration pgAdapterConfiguration, + SpannerConfiguration spannerConfiguration, + Metrics metrics, + Dialect dialect) { + super( + statistics, + tpccConfiguration, + pgAdapterConfiguration, + spannerConfiguration, + metrics, + dialect); + this.sessionFactory = sessionFactory; + } + + @Override + void setup() throws SQLException, IOException { + sessionThreadLocal.set(sessionFactory.openSession()); // Open session here + } + + @Override + void teardown() throws SQLException { + Session session = sessionThreadLocal.get(); + if (session != null) { + session.close(); + sessionThreadLocal.remove(); + } + } + + @Override + public void newOrder() throws SQLException { + Session session = sessionThreadLocal.get(); + Transaction tx = session.beginTransaction(); + + // Execute the "select 1" query + try { + District district = session.get(District.class, new DistrictId(0l, 0l)); + System.out.println(district); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + tx.commit(); + } + + @Override + public void payment() throws SQLException { + Session session = sessionThreadLocal.get(); + Transaction tx = session.beginTransaction(); + + // Execute the "select 1" query + try { + Integer result = session.createSelectionQuery("select 2", Integer.class).getSingleResult(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + tx.commit(); + } + + @Override + public void orderStatus() throws SQLException { + Session session = sessionThreadLocal.get(); + Transaction tx = session.beginTransaction(); + + // Execute the "select 1" query + try { + Integer result = session.createSelectionQuery("select 3", Integer.class).getSingleResult(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + tx.commit(); + } + + @Override + public void delivery() throws SQLException { + Session session = sessionThreadLocal.get(); + Transaction tx = session.beginTransaction(); + + // Execute the "select 1" query + try { + Integer result = session.createSelectionQuery("select 4", Integer.class).getSingleResult(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + tx.commit(); + } + + public void stockLevel() throws SQLException { + Session session = sessionThreadLocal.get(); + Transaction tx = session.beginTransaction(); + + // Execute the "select 1" query + try { + Integer result = session.createSelectionQuery("select 5", Integer.class).getSingleResult(); + } catch (Exception e) { + } + tx.commit(); + } + + @Override + void executeStatement(String sql) throws SQLException {} + + @Override + Object[] paramQueryRow(String sql, Object[] params) throws SQLException { + return new Object[0]; + } + + @Override + void executeParamStatement(String sql, Object[] params) throws SQLException {} + + @Override + List executeParamQuery(String sql, Object[] params) throws SQLException { + return null; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/Metrics.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/Metrics.java index 0b760f872..f71c3ae34 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/Metrics.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/Metrics.java @@ -23,10 +23,33 @@ public class Metrics { static final String INSTRUMENTATION_SCOPE = "cloud.google.com/java"; static final String ROUNDTRIP_LATENCY = "spanner/benchmarker/roundtrip_latencies"; + static final String ROUNDTRIP_LATENCY_DESCRIPTION = "Latency when the benchmark runner sends out a query and gets a response"; + static final String TPCC_NEWORDER_LATENCY = "spanner/benchmarker/tpcc_neworder_latencies"; + + static final String TPCC_PAYMENT_LATENCY = "spanner/benchmarker/tpcc_payment_latencies"; + + static final String TPCC_ORDERSTATUS_LATENCY = "spanner/benchmarker/tpcc_orderstatus_latencies"; + + static final String TPCC_DELIVERY_LATENCY = "spanner/benchmarker/tpcc_delivery_latencies"; + + static final String TPCC_STOCKLEVEL_LATENCY = "spanner/benchmarker/tpcc_stocklevel_latencies"; + + static final String TPCC_TRANSACTION_DESCRIPTON = "Latency of TPCC transaction"; + private final LongHistogram latencies; + + private final LongHistogram newOrderLatencies; + + private final LongHistogram paymentLatencies; + + private final LongHistogram orderStatusLatencies; + + private final LongHistogram deliveryLatencies; + + private final LongHistogram stockLevelLatencies; private final Attributes attributes; public Metrics(OpenTelemetry openTelemetry, Attributes attributes) { @@ -44,10 +67,70 @@ public Metrics(OpenTelemetry openTelemetry, Attributes attributes) { .setUnit("ms") .setExplicitBucketBoundariesAdvice(RPC_MILLIS_BUCKET_BOUNDARIES) .build(); + newOrderLatencies = + meter + .histogramBuilder(TPCC_NEWORDER_LATENCY) + .ofLongs() + .setDescription(TPCC_TRANSACTION_DESCRIPTON) + .setUnit("ms") + .setExplicitBucketBoundariesAdvice(RPC_MILLIS_BUCKET_BOUNDARIES) + .build(); + paymentLatencies = + meter + .histogramBuilder(TPCC_PAYMENT_LATENCY) + .ofLongs() + .setDescription(TPCC_TRANSACTION_DESCRIPTON) + .setUnit("ms") + .setExplicitBucketBoundariesAdvice(RPC_MILLIS_BUCKET_BOUNDARIES) + .build(); + orderStatusLatencies = + meter + .histogramBuilder(TPCC_ORDERSTATUS_LATENCY) + .ofLongs() + .setDescription(TPCC_TRANSACTION_DESCRIPTON) + .setUnit("ms") + .setExplicitBucketBoundariesAdvice(RPC_MILLIS_BUCKET_BOUNDARIES) + .build(); + deliveryLatencies = + meter + .histogramBuilder(TPCC_DELIVERY_LATENCY) + .ofLongs() + .setDescription(TPCC_TRANSACTION_DESCRIPTON) + .setUnit("ms") + .setExplicitBucketBoundariesAdvice(RPC_MILLIS_BUCKET_BOUNDARIES) + .build(); + stockLevelLatencies = + meter + .histogramBuilder(TPCC_STOCKLEVEL_LATENCY) + .ofLongs() + .setDescription(TPCC_TRANSACTION_DESCRIPTON) + .setUnit("ms") + .setExplicitBucketBoundariesAdvice(RPC_MILLIS_BUCKET_BOUNDARIES) + .build(); this.attributes = attributes; } public void recordLatency(long value) { latencies.record(value, attributes); } + + public void recordNewOrderLatency(long value) { + newOrderLatencies.record(value, attributes); + } + + public void recordPaymentLatency(long value) { + paymentLatencies.record(value, attributes); + } + + public void recordOrderStatusLatency(long value) { + orderStatusLatencies.record(value, attributes); + } + + public void recordDeliveryLatency(long value) { + deliveryLatencies.record(value, attributes); + } + + public void recordStockLevelLatency(long value) { + stockLevelLatencies.record(value, attributes); + } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/TpccConfiguration.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/TpccConfiguration.java index af0ac14ad..532777772 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/TpccConfiguration.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/TpccConfiguration.java @@ -25,9 +25,15 @@ public class TpccConfiguration { public static final String SPANNER_JDBC_RUNNER = "spanner_jdbc"; public static final String CLIENT_LIB_PG_RUNNER = "client_lib_pg"; public static final String CLIENT_LIB_GSQL_RUNNER = "client_lib_gsql"; + + public static final String HIBERNATE_RUNNER = "hibernate"; public static final Set RUNNERS = Set.of( - PGADAPTER_JDBC_RUNNER, SPANNER_JDBC_RUNNER, CLIENT_LIB_PG_RUNNER, CLIENT_LIB_GSQL_RUNNER); + PGADAPTER_JDBC_RUNNER, + SPANNER_JDBC_RUNNER, + CLIENT_LIB_PG_RUNNER, + CLIENT_LIB_GSQL_RUNNER, + HIBERNATE_RUNNER); private boolean loadData; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java new file mode 100644 index 000000000..ffb7afc07 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java @@ -0,0 +1,241 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinColumns; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import java.math.BigDecimal; +import java.sql.Timestamp; + +@Entity +@Table(name = "customer") +public class Customer { + + @Id + @Column(name = "c_id") + private Long cId; + + @ManyToOne + @JoinColumns({ + @JoinColumn(name = "w_id", referencedColumnName = "w_id"), + @JoinColumn(name = "d_id", referencedColumnName = "d_id") + }) + private District district; + + @Column(name = "c_first", length = 16) + private String cFirst; + + @Column(name = "c_middle", length = 2) + private String cMiddle; + + @Column(name = "c_last", length = 16) + private String cLast; + + @Column(name = "c_street_1", length = 20) + private String cStreet1; + + @Column(name = "c_street_2", length = 20) + private String cStreet2; + + @Column(name = "c_city", length = 20) + private String cCity; + + @Column(name = "c_state", length = 2) + private String cState; + + @Column(name = "c_zip", length = 9) + private String cZip; + + @Column(name = "c_phone", length = 16) + private String cPhone; + + @Column(name = "c_since") + private Timestamp cSince; + + @Column(name = "c_credit", length = 2) + private String cCredit; + + @Column(name = "c_credit_lim") + private Long cCreditLim; + + @Column(name = "c_discount", precision = 12, scale = 4) + private BigDecimal cDiscount; + + @Column(name = "c_balance", precision = 12, scale = 4) + private BigDecimal cBalance; + + @Column(name = "c_ytd_payment", precision = 12, scale = 4) + private BigDecimal cYtdPayment; + + @Column(name = "c_payment_cnt") + private Long cPaymentCnt; + + @Column(name = "c_delivery_cnt") + private Long cDeliveryCnt; + + @Column(name = "c_data", columnDefinition = "TEXT") + private String cData; + + public Long getcId() { + return cId; + } + + public void setcId(Long cId) { + this.cId = cId; + } + + public District getDistrict() { + return district; + } + + public void setDistrict(District district) { + this.district = district; + } + + public String getcFirst() { + return cFirst; + } + + public void setcFirst(String cFirst) { + this.cFirst = cFirst; + } + + public String getcMiddle() { + return cMiddle; + } + + public void setcMiddle(String cMiddle) { + this.cMiddle = cMiddle; + } + + public String getcLast() { + return cLast; + } + + public void setcLast(String cLast) { + this.cLast = cLast; + } + + public String getcStreet1() { + return cStreet1; + } + + public void setcStreet1(String cStreet1) { + this.cStreet1 = cStreet1; + } + + public String getcStreet2() { + return cStreet2; + } + + public void setcStreet2(String cStreet2) { + this.cStreet2 = cStreet2; + } + + public String getcCity() { + return cCity; + } + + public void setcCity(String cCity) { + this.cCity = cCity; + } + + public String getcState() { + return cState; + } + + public void setcState(String cState) { + this.cState = cState; + } + + public String getcZip() { + return cZip; + } + + public void setcZip(String cZip) { + this.cZip = cZip; + } + + public String getcPhone() { + return cPhone; + } + + public void setcPhone(String cPhone) { + this.cPhone = cPhone; + } + + public Timestamp getcSince() { + return cSince; + } + + public void setcSince(Timestamp cSince) { + this.cSince = cSince; + } + + public String getcCredit() { + return cCredit; + } + + public void setcCredit(String cCredit) { + this.cCredit = cCredit; + } + + public Long getcCreditLim() { + return cCreditLim; + } + + public void setcCreditLim(Long cCreditLim) { + this.cCreditLim = cCreditLim; + } + + public BigDecimal getcDiscount() { + return cDiscount; + } + + public void setcDiscount(BigDecimal cDiscount) { + this.cDiscount = cDiscount; + } + + public BigDecimal getcBalance() { + return cBalance; + } + + public void setcBalance(BigDecimal cBalance) { + this.cBalance = cBalance; + } + + public BigDecimal getcYtdPayment() { + return cYtdPayment; + } + + public void setcYtdPayment(BigDecimal cYtdPayment) { + this.cYtdPayment = cYtdPayment; + } + + public Long getcPaymentCnt() { + return cPaymentCnt; + } + + public void setcPaymentCnt(Long cPaymentCnt) { + this.cPaymentCnt = cPaymentCnt; + } + + public Long getcDeliveryCnt() { + return cDeliveryCnt; + } + + public void setcDeliveryCnt(Long cDeliveryCnt) { + this.cDeliveryCnt = cDeliveryCnt; + } + + public String getcData() { + return cData; + } + + public void setcData(String cData) { + this.cData = cData; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java new file mode 100644 index 000000000..5e31be758 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java @@ -0,0 +1,170 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import jakarta.persistence.Column; +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import java.math.BigDecimal; + +@Entity +@Table(name = "district") +public class District { + + @EmbeddedId private DistrictId id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false) + private Warehouse warehouse; + + @Column(name = "d_name", length = 10) + private String dName; + + @Column(name = "d_street_1", length = 20) + private String dStreet1; + + @Column(name = "d_street_2", length = 20) + private String dStreet2; + + @Column(name = "d_city", length = 20) + private String dCity; + + @Column(name = "d_state", length = 2) + private String dState; + + @Column(name = "d_zip", length = 9) + private String dZip; + + @Column(name = "d_tax", precision = 12, scale = 4) + private BigDecimal dTax; + + @Column(name = "d_ytd", precision = 12, scale = 4) + private BigDecimal dYtd; + + @Column(name = "d_next_o_id") + private Long dNextOId; + + public DistrictId getId() { + return id; + } + + public void setId(DistrictId id) { + this.id = id; + } + + public Warehouse getWarehouse() { + return warehouse; + } + + public void setWarehouse(Warehouse warehouse) { + this.warehouse = warehouse; + } + + public String getdName() { + return dName; + } + + public void setdName(String dName) { + this.dName = dName; + } + + public String getdStreet1() { + return dStreet1; + } + + public void setdStreet1(String dStreet1) { + this.dStreet1 = dStreet1; + } + + public String getdStreet2() { + return dStreet2; + } + + public void setdStreet2(String dStreet2) { + this.dStreet2 = dStreet2; + } + + public String getdCity() { + return dCity; + } + + public void setdCity(String dCity) { + this.dCity = dCity; + } + + public String getdState() { + return dState; + } + + public void setdState(String dState) { + this.dState = dState; + } + + public String getdZip() { + return dZip; + } + + public void setdZip(String dZip) { + this.dZip = dZip; + } + + public BigDecimal getdTax() { + return dTax; + } + + public void setdTax(BigDecimal dTax) { + this.dTax = dTax; + } + + public BigDecimal getdYtd() { + return dYtd; + } + + public void setdYtd(BigDecimal dYtd) { + this.dYtd = dYtd; + } + + public Long getdNextOId() { + return dNextOId; + } + + public void setdNextOId(Long dNextOId) { + this.dNextOId = dNextOId; + } + + @Override + public String toString() { + return "District{" + + "id=" + + id + + ", warehouse=" + + warehouse + + ", dName='" + + dName + + '\'' + + ", dStreet1='" + + dStreet1 + + '\'' + + ", dStreet2='" + + dStreet2 + + '\'' + + ", dCity='" + + dCity + + '\'' + + ", dState='" + + dState + + '\'' + + ", dZip='" + + dZip + + '\'' + + ", dTax=" + + dTax + + ", dYtd=" + + dYtd + + ", dNextOId=" + + dNextOId + + '}'; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java new file mode 100644 index 000000000..8f090ffe7 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java @@ -0,0 +1,60 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import com.google.common.base.Objects; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; + +@Embeddable +public class DistrictId implements java.io.Serializable { + private static final long serialVersionUID = 1L; + + @Column(name = "w_id") + private Long dId; + + @Column(name = "d_id") + private Long wId; + + public DistrictId() {} + + public DistrictId(Long dId, Long wId) { + this.dId = dId; + this.wId = wId; + } + + public Long getdId() { + return dId; + } + + public void setdId(Long dId) { + this.dId = dId; + } + + public Long getwId() { + return wId; + } + + public void setwId(Long wId) { + this.wId = wId; + } + + @Override + public String toString() { + return "DistrictId{" + "dId=" + dId + ", wId=" + wId + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof DistrictId that)) { + return false; + } + return Objects.equal(dId, that.dId) && Objects.equal(wId, that.wId); + } + + @Override + public int hashCode() { + return Objects.hashCode(dId, wId); + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java new file mode 100644 index 000000000..fffb4cd7a --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java @@ -0,0 +1,94 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinColumns; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import java.math.BigDecimal; +import java.sql.Timestamp; + +@Entity +@Table(name = "history") +public class History { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; // You might want to add a surrogate key + + @ManyToOne + @JoinColumns({ + @JoinColumn(name = "c_id", referencedColumnName = "c_id"), + @JoinColumn(name = "d_id", referencedColumnName = "d_id"), + @JoinColumn(name = "w_id", referencedColumnName = "w_id") + }) + private Customer customer; + + @ManyToOne + @JoinColumns({ + @JoinColumn(name = "h_w_id", referencedColumnName = "w_id"), + @JoinColumn(name = "h_d_id", referencedColumnName = "d_id") + }) + private District district; + + @Column(name = "h_date") + private Timestamp hDate; + + @Column(name = "h_amount", precision = 12, scale = 4) + private BigDecimal hAmount; + + @Column(name = "h_data", length = 24) + private String hData; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + + public District getDistrict() { + return district; + } + + public void setDistrict(District district) { + this.district = district; + } + + public Timestamp gethDate() { + return hDate; + } + + public void sethDate(Timestamp hDate) { + this.hDate = hDate; + } + + public BigDecimal gethAmount() { + return hAmount; + } + + public void sethAmount(BigDecimal hAmount) { + this.hAmount = hAmount; + } + + public String gethData() { + return hData; + } + + public void sethData(String hData) { + this.hData = hData; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java new file mode 100644 index 000000000..1b16a02af --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java @@ -0,0 +1,68 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import java.math.BigDecimal; + +@Entity +@Table(name = "item") +public class Item { + + @Id + @Column(name = "i_id") + private Long iId; + + @Column(name = "i_im_id") + private Long iImId; + + @Column(name = "i_name", length = 24) + private String iName; + + @Column(name = "i_price", precision = 12, scale = 4) + private BigDecimal iPrice; + + @Column(name = "i_data", length = 50) + private String iData; + + public Long getiId() { + return iId; + } + + public void setiId(Long iId) { + this.iId = iId; + } + + public Long getiImId() { + return iImId; + } + + public void setiImId(Long iImId) { + this.iImId = iImId; + } + + public String getiName() { + return iName; + } + + public void setiName(String iName) { + this.iName = iName; + } + + public BigDecimal getiPrice() { + return iPrice; + } + + public void setiPrice(BigDecimal iPrice) { + this.iPrice = iPrice; + } + + public String getiData() { + return iData; + } + + public void setiData(String iData) { + this.iData = iData; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java new file mode 100644 index 000000000..4b4bc33b4 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java @@ -0,0 +1,42 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinColumns; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; + +@Entity +@Table(name = "new_orders") +public class NewOrder { + + @Id + @Column(name = "o_id") + private Long oId; + + @ManyToOne + @JoinColumns({ + @JoinColumn(name = "w_id", referencedColumnName = "w_id"), + @JoinColumn(name = "d_id", referencedColumnName = "d_id"), + @JoinColumn(name = "c_id", referencedColumnName = "c_id") + }) + private Order order; + + public Long getoId() { + return oId; + } + + public void setoId(Long oId) { + this.oId = oId; + } + + public Order getOrder() { + return order; + } + + public void setOrder(Order order) { + this.order = order; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java new file mode 100644 index 000000000..dee06eb25 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java @@ -0,0 +1,87 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinColumns; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import java.sql.Timestamp; + +@Entity +@Table(name = "orders") +public class Order { + + @Id + @Column(name = "o_id") + private Long oId; + + @ManyToOne + @JoinColumns({ + @JoinColumn(name = "w_id", referencedColumnName = "w_id"), + @JoinColumn(name = "d_id", referencedColumnName = "d_id"), + @JoinColumn(name = "c_id", referencedColumnName = "c_id") + }) + private Customer customer; + + @Column(name = "o_entry_d") + private Timestamp oEntryD; + + @Column(name = "o_carrier_id") + private Long oCarrierId; + + @Column(name = "o_ol_cnt") + private Long oOlCnt; + + @Column(name = "o_all_local") + private Long oAllLocal; + + public Long getoId() { + return oId; + } + + public void setoId(Long oId) { + this.oId = oId; + } + + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + + public Timestamp getoEntryD() { + return oEntryD; + } + + public void setoEntryD(Timestamp oEntryD) { + this.oEntryD = oEntryD; + } + + public Long getoCarrierId() { + return oCarrierId; + } + + public void setoCarrierId(Long oCarrierId) { + this.oCarrierId = oCarrierId; + } + + public Long getoOlCnt() { + return oOlCnt; + } + + public void setoOlCnt(Long oOlCnt) { + this.oOlCnt = oOlCnt; + } + + public Long getoAllLocal() { + return oAllLocal; + } + + public void setoAllLocal(Long oAllLocal) { + this.oAllLocal = oAllLocal; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java new file mode 100644 index 000000000..b589c2650 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java @@ -0,0 +1,47 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import jakarta.persistence.*; +import java.math.BigDecimal; +import java.sql.Timestamp; + +@Entity +@Table(name = "order_line") +public class OrderLine { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; // You might want to add a surrogate key + + @ManyToOne + @JoinColumns({ + @JoinColumn(name = "w_id", referencedColumnName = "w_id"), + @JoinColumn(name = "d_id", referencedColumnName = "d_id"), + @JoinColumn(name = "c_id", referencedColumnName = "c_id"), + @JoinColumn(name = "o_id", referencedColumnName = "o_id") + }) + private Order order; + + @Column(name = "ol_number") + private Long olNumber; + + @ManyToOne + @JoinColumns({ + @JoinColumn(name = "ol_supply_w_id", referencedColumnName = "w_id"), + @JoinColumn(name = "ol_i_id", referencedColumnName = "s_i_id") + }) + private Stock stock; + + @Column(name = "ol_delivery_d") + private Timestamp olDeliveryD; + + @Column(name = "ol_quantity") + private Long olQuantity; + + @Column(name = "ol_amount", precision = 12, scale = 4) + private BigDecimal olAmount; + + @Column(name = "ol_dist_info", length = 24) + private String olDistInfo; + + // Getters and setters +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java new file mode 100644 index 000000000..23c4ea0a9 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java @@ -0,0 +1,198 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import jakarta.persistence.*; +import java.math.BigDecimal; + +@Entity +@Table(name = "stock") +public class Stock { + + @Id + @Column(name = "s_i_id") + private Long sIId; + + @ManyToOne + @JoinColumn(name = "w_id", referencedColumnName = "w_id") + private Warehouse warehouse; + + @Column(name = "s_quantity") + private Long sQuantity; + + @Column(name = "s_dist_01", length = 24) + private String sDist01; + + @Column(name = "s_dist_02", length = 24) + private String sDist02; + + @Column(name = "s_dist_03", length = 24) + private String sDist03; + + @Column(name = "s_dist_04", length = 24) + private String sDist04; + + @Column(name = "s_dist_05", length = 24) + private String sDist05; + + @Column(name = "s_dist_06", length = 24) + private String sDist06; + + @Column(name = "s_dist_07", length = 24) + private String sDist07; + + @Column(name = "s_dist_08", length = 24) + private String sDist08; + + @Column(name = "s_dist_09", length = 24) + private String sDist09; + + @Column(name = "s_dist_10", length = 24) + private String sDist10; + + @Column(name = "s_ytd", precision = 12, scale = 4) + private BigDecimal sYtd; + + @Column(name = "s_order_cnt") + private Long sOrderCnt; + + @Column(name = "s_remote_cnt") + private Long sRemoteCnt; + + @Column(name = "s_data", length = 50) + private String sData; + + public Long getsIId() { + return sIId; + } + + public void setsIId(Long sIId) { + this.sIId = sIId; + } + + public Warehouse getWarehouse() { + return warehouse; + } + + public void setWarehouse(Warehouse warehouse) { + this.warehouse = warehouse; + } + + public Long getsQuantity() { + return sQuantity; + } + + public void setsQuantity(Long sQuantity) { + this.sQuantity = sQuantity; + } + + public String getsDist01() { + return sDist01; + } + + public void setsDist01(String sDist01) { + this.sDist01 = sDist01; + } + + public String getsDist02() { + return sDist02; + } + + public void setsDist02(String sDist02) { + this.sDist02 = sDist02; + } + + public String getsDist03() { + return sDist03; + } + + public void setsDist03(String sDist03) { + this.sDist03 = sDist03; + } + + public String getsDist04() { + return sDist04; + } + + public void setsDist04(String sDist04) { + this.sDist04 = sDist04; + } + + public String getsDist05() { + return sDist05; + } + + public void setsDist05(String sDist05) { + this.sDist05 = sDist05; + } + + public String getsDist06() { + return sDist06; + } + + public void setsDist06(String sDist06) { + this.sDist06 = sDist06; + } + + public String getsDist07() { + return sDist07; + } + + public void setsDist07(String sDist07) { + this.sDist07 = sDist07; + } + + public String getsDist08() { + return sDist08; + } + + public void setsDist08(String sDist08) { + this.sDist08 = sDist08; + } + + public String getsDist09() { + return sDist09; + } + + public void setsDist09(String sDist09) { + this.sDist09 = sDist09; + } + + public String getsDist10() { + return sDist10; + } + + public void setsDist10(String sDist10) { + this.sDist10 = sDist10; + } + + public BigDecimal getsYtd() { + return sYtd; + } + + public void setsYtd(BigDecimal sYtd) { + this.sYtd = sYtd; + } + + public Long getsOrderCnt() { + return sOrderCnt; + } + + public void setsOrderCnt(Long sOrderCnt) { + this.sOrderCnt = sOrderCnt; + } + + public Long getsRemoteCnt() { + return sRemoteCnt; + } + + public void setsRemoteCnt(Long sRemoteCnt) { + this.sRemoteCnt = sRemoteCnt; + } + + public String getsData() { + return sData; + } + + public void setsData(String sData) { + this.sData = sData; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java new file mode 100644 index 000000000..ab1495471 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java @@ -0,0 +1,142 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import java.math.BigDecimal; + +@Entity +@Table(name = "warehouse") +public class Warehouse { + + @Id + @Column(name = "w_id") + private Long wId; + + @Column(name = "w_name", length = 10) + private String wName; + + @Column(name = "w_street_1", length = 20) + private String wStreet1; + + @Column(name = "w_street_2", length = 20) + private String wStreet2; + + @Column(name = "w_city", length = 20) + private String wCity; + + @Column(name = "w_state", length = 2) + private String wState; + + @Column(name = "w_zip", length = 9) + private String wZip; + + @Column(name = "w_tax", precision = 12, scale = 4) + private BigDecimal wTax; + + @Column(name = "w_ytd", precision = 12, scale = 4) + private BigDecimal wYtd; + + public Long getwId() { + return wId; + } + + public void setwId(Long wId) { + this.wId = wId; + } + + public String getwName() { + return wName; + } + + public void setwName(String wName) { + this.wName = wName; + } + + public String getwStreet1() { + return wStreet1; + } + + public void setwStreet1(String wStreet1) { + this.wStreet1 = wStreet1; + } + + public String getwStreet2() { + return wStreet2; + } + + public void setwStreet2(String wStreet2) { + this.wStreet2 = wStreet2; + } + + public String getwCity() { + return wCity; + } + + public void setwCity(String wCity) { + this.wCity = wCity; + } + + public String getwState() { + return wState; + } + + public void setwState(String wState) { + this.wState = wState; + } + + public String getwZip() { + return wZip; + } + + public void setwZip(String wZip) { + this.wZip = wZip; + } + + public BigDecimal getwTax() { + return wTax; + } + + public void setwTax(BigDecimal wTax) { + this.wTax = wTax; + } + + public BigDecimal getwYtd() { + return wYtd; + } + + public void setwYtd(BigDecimal wYtd) { + this.wYtd = wYtd; + } + + @Override + public String toString() { + return "Warehouse{" + + "wId=" + + wId + + ", wName='" + + wName + + '\'' + + ", wStreet1='" + + wStreet1 + + '\'' + + ", wStreet2='" + + wStreet2 + + '\'' + + ", wCity='" + + wCity + + '\'' + + ", wState='" + + wState + + '\'' + + ", wZip='" + + wZip + + '\'' + + ", wTax=" + + wTax + + ", wYtd=" + + wYtd + + '}'; + } +} diff --git a/benchmarks/tpcc/src/main/resources/application.properties b/benchmarks/tpcc/src/main/resources/application.properties index 57e67f567..e4e839783 100644 --- a/benchmarks/tpcc/src/main/resources/application.properties +++ b/benchmarks/tpcc/src/main/resources/application.properties @@ -8,13 +8,13 @@ logging.threshold.console=OFF # Load the initial data set. Enable this on one client to fill the database. tpcc.load-data=false -tpcc.load-data-threads=32 +tpcc.load-data-threads=1 # Truncates all existing data before loading new data. This setting only has any effect when # tpcc.load-data also has been enabled. -tpcc.truncate-before-load=false +tpcc.truncate-before-load=true # Choose to use the runner (pgadapter/spanner_jdbc/client_lib_pg) -tpcc.benchmark-runner=pgadapter +tpcc.benchmark-runner=spanner_jdbc # Run the benchmark. tpcc.run-benchmark=true @@ -22,7 +22,7 @@ tpcc.run-benchmark=true tpcc.benchmark-threads=8 # The duration that the benchmark should run in ISO-8601 notation. # E.g. PT60S (60 seconds), PT10M (10 minutes), PT2H (2 hours), P1D (1 day) -tpcc.benchmark-duration=PT300S +tpcc.benchmark-duration=PT10M # The number of warehouses in the database. This is commonly known as the 'factor'. Increasing this # number also increases the number of dependent records (districts, customers, orders, ...). @@ -43,9 +43,9 @@ tpcc.use-read-only-transactions=false tpcc.lock-scanned-ranges=false # Change these to match your Cloud Spanner PostgreSQL-dialect database. -spanner.project=my-project -spanner.instance=my-instance -spanner.database=my-database +spanner.project=span-cloud-testing +spanner.instance=hengfeng-pg-tpcc +spanner.database=tpcc-gsql # --- IN-PROCESS PGADAPTER --- # @@ -54,13 +54,13 @@ spanner.database=my-database # Set this to true to instruct the benchmark runner to start a PGAdapter instance in-process with # the benchmark application. -pgadapter.in-process=true +pgadapter.in-process=false pgadapter.num-channels=32 # Set this if you want the in-process PGAdapter instance to use a specific service account # credentials file. # Leave unset if the application should use the APPLICATION_DEFAULT_CREDENTIALS. -pgadapter.credentials=/path/to/credentials.json +pgadapter.credentials= # Set this to true to disable automatic retries of aborted transactions by PGAdapter. Disabling this # will propagate all aborted transaction errors to the application, and the transaction will be diff --git a/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml b/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml new file mode 100644 index 000000000..ceb57755f --- /dev/null +++ b/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,39 @@ + + + + + + + + com.google.cloud.spanner.jdbc.JdbcDriver + + + + jdbc:cloudspanner:/projects/google.com:span-test-1/instances/bench-1/databases/tpcc + + + + com.google.cloud.spanner.hibernate.SpannerDialect + + + true + + + none + + + + + + + + + + + + + + \ No newline at end of file From 4ac47cb6f2500663c8e97cd4842fc8ac9fd363ed Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Wed, 20 Nov 2024 11:40:01 +0530 Subject: [PATCH 02/22] hibernate runner --- .../tpcc/AbstractBenchmarkRunner.java | 12 +- .../pgadapter/tpcc/BenchmarkApplication.java | 2 +- .../tpcc/HibernateBenchmarkRunner.java | 449 ++++++++++++++++-- .../pgadapter/tpcc/entities/Customer.java | 235 ++++----- .../pgadapter/tpcc/entities/CustomerId.java | 80 ++++ .../pgadapter/tpcc/entities/District.java | 33 -- .../pgadapter/tpcc/entities/History.java | 60 +-- .../pgadapter/tpcc/entities/HistoryId.java | 124 +++++ .../pgadapter/tpcc/entities/NewOrder.java | 28 +- .../cloud/pgadapter/tpcc/entities/Order.java | 93 ++-- .../pgadapter/tpcc/entities/OrderId.java | 93 ++++ .../pgadapter/tpcc/entities/OrderLine.java | 113 ++++- .../pgadapter/tpcc/entities/OrderLineId.java | 114 +++++ .../cloud/pgadapter/tpcc/entities/Stock.java | 204 ++++---- .../pgadapter/tpcc/entities/StockId.java | 63 +++ .../pgadapter/tpcc/entities/Warehouse.java | 29 -- .../src/main/resources/application.properties | 18 +- .../tpcc/src/main/resources/hibernate.cfg.xml | 20 +- 18 files changed, 1336 insertions(+), 434 deletions(-) create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java index 79e464043..5d4f22944 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java @@ -41,7 +41,7 @@ abstract class AbstractBenchmarkRunner implements Runnable { private final Statistics statistics; - private final TpccConfiguration tpccConfiguration; + final TpccConfiguration tpccConfiguration; // We have to define the following configuration in the abstract class. // If we define them in inherited classes, they will be null in threads. @@ -96,22 +96,22 @@ private void runTransactions() throws SQLException, InterruptedException { int transaction = random.nextInt(23); Stopwatch stopwatch = Stopwatch.createStarted(); if (transaction < 10) { - newOrder(); + stockLevel(); Duration executionDuration = stopwatch.elapsed(); metrics.recordNewOrderLatency(executionDuration.toMillis()); statistics.incNewOrder(); } else if (transaction < 20) { - payment(); + stockLevel(); Duration executionDuration = stopwatch.elapsed(); metrics.recordPaymentLatency(executionDuration.toMillis()); statistics.incPayment(); } else if (transaction < 21) { - orderStatus(); + stockLevel(); Duration executionDuration = stopwatch.elapsed(); metrics.recordOrderStatusLatency(executionDuration.toMillis()); statistics.incOrderStatus(); } else if (transaction < 22) { - delivery(); + stockLevel(); Duration executionDuration = stopwatch.elapsed(); metrics.recordDeliveryLatency(executionDuration.toMillis()); statistics.incDelivery(); @@ -152,7 +152,7 @@ private void runTransactions() throws SQLException, InterruptedException { } } - private long getOtherWarehouseId(long currentId) { + long getOtherWarehouseId(long currentId) { if (tpccConfiguration.getWarehouses() == 1) { return currentId; } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java index 354764aeb..a871c8603 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java @@ -217,7 +217,7 @@ public void run(String... args) throws Exception { while (watch.elapsed().compareTo(tpccConfiguration.getBenchmarkDuration()) <= 0) { //noinspection BusyWait Thread.sleep(1_000L); - statistics.print(watch.elapsed()); + //statistics.print(watch.elapsed()); } executor.shutdownNow(); if (!executor.awaitTermination(60L, TimeUnit.SECONDS)) { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index 23ab81de2..ea4e11ecd 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -3,24 +3,49 @@ import com.google.cloud.pgadapter.tpcc.config.PGAdapterConfiguration; import com.google.cloud.pgadapter.tpcc.config.SpannerConfiguration; import com.google.cloud.pgadapter.tpcc.config.TpccConfiguration; +import com.google.cloud.pgadapter.tpcc.entities.Customer; +import com.google.cloud.pgadapter.tpcc.entities.CustomerId; import com.google.cloud.pgadapter.tpcc.entities.District; import com.google.cloud.pgadapter.tpcc.entities.DistrictId; +import com.google.cloud.pgadapter.tpcc.entities.History; +import com.google.cloud.pgadapter.tpcc.entities.HistoryId; +import com.google.cloud.pgadapter.tpcc.entities.Item; +import com.google.cloud.pgadapter.tpcc.entities.NewOrder; +import com.google.cloud.pgadapter.tpcc.entities.Order; +import com.google.cloud.pgadapter.tpcc.entities.OrderId; +import com.google.cloud.pgadapter.tpcc.entities.OrderLine; +import com.google.cloud.pgadapter.tpcc.entities.OrderLineId; +import com.google.cloud.pgadapter.tpcc.entities.Stock; +import com.google.cloud.pgadapter.tpcc.entities.StockId; +import com.google.cloud.pgadapter.tpcc.entities.Warehouse; import com.google.cloud.spanner.Dialect; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.JoinType; +import jakarta.persistence.criteria.Root; +import jakarta.persistence.criteria.Subquery; import java.io.IOException; +import java.math.BigDecimal; import java.sql.SQLException; +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; +import java.util.Random; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; +import org.hibernate.query.Query; public class HibernateBenchmarkRunner extends AbstractBenchmarkRunner { - private final SessionFactory sessionFactory; // SessionFactory injected + private final SessionFactory sessionFactory; private final ThreadLocal sessionThreadLocal = new ThreadLocal<>(); + private final Random random = new Random(); HibernateBenchmarkRunner( Statistics statistics, - SessionFactory sessionFactory, // Injected SessionFactory + SessionFactory sessionFactory, TpccConfiguration tpccConfiguration, PGAdapterConfiguration pgAdapterConfiguration, SpannerConfiguration spannerConfiguration, @@ -38,7 +63,7 @@ public class HibernateBenchmarkRunner extends AbstractBenchmarkRunner { @Override void setup() throws SQLException, IOException { - sessionThreadLocal.set(sessionFactory.openSession()); // Open session here + sessionThreadLocal.set(sessionFactory.openSession()); } @Override @@ -53,70 +78,440 @@ void teardown() throws SQLException { @Override public void newOrder() throws SQLException { Session session = sessionThreadLocal.get(); - Transaction tx = session.beginTransaction(); - // Execute the "select 1" query + long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); + long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); + long customerId = Long.reverse(random.nextInt(tpccConfiguration.getCustomersPerDistrict())); + + int orderLineCount = random.nextInt(5, 16); + long[] itemIds = new long[orderLineCount]; + long[] supplyWarehouses = new long[orderLineCount]; + int[] quantities = new int[orderLineCount]; + int rollback = random.nextInt(100); + int allLocal = 1; + + for (int line = 0; line < orderLineCount; line++) { + if (rollback == 1 && line == orderLineCount - 1) { + itemIds[line] = Long.reverse(Long.MAX_VALUE); + } else { + itemIds[line] = Long.reverse(random.nextInt(tpccConfiguration.getItemCount())); + } + if (random.nextInt(100) == 50) { + supplyWarehouses[line] = getOtherWarehouseId(warehouseId); + allLocal = 0; + } else { + supplyWarehouses[line] = warehouseId; + } + quantities[line] = random.nextInt(1, 10); + } + + Transaction tx = session.beginTransaction(); try { - District district = session.get(District.class, new DistrictId(0l, 0l)); - System.out.println(district); + Customer customer = session.get(Customer.class, new CustomerId(customerId, districtId, warehouseId)); + District district = customer.getDistrict(); + Warehouse warehouse = district.getWarehouse(); + + // Update district next order ID + long districtNextOrderId = district.getdNextOId(); + district.setdNextOId(districtNextOrderId + 1); + + // Create new Order and NewOrder entities + Order order = new Order(); + OrderId orderId = new OrderId(districtNextOrderId, customerId, districtId, warehouseId); + order.setId(orderId); + order.setEntryD(new Timestamp(System.currentTimeMillis())); + order.setOlCnt(orderLineCount); + order.setAllLocal(allLocal); + order.setCustomer(customer); + + + NewOrder newOrder = new NewOrder(); + newOrder.setId(orderId); + newOrder.setOrder(order); + + + // Create and process order lines + List orderLines = new ArrayList<>(); + for (int line = 0; line < orderLineCount; line++) { + long orderLineItemId = itemIds[line]; + + Item item = session.get(Item.class, orderLineItemId); + if (item == null) { + //item not found, rollback(1% chance) + tx.rollback(); + return; + } + + Stock stock = session.get(Stock.class, new StockId(orderLineItemId, supplyWarehouses[line])); + String[] stockDistrict = { + stock.getDist01(), + stock.getDist02(), + stock.getDist03(), + stock.getDist04(), + stock.getDist05(), + stock.getDist06(), + stock.getDist07(), + stock.getDist08(), + stock.getDist09(), + stock.getDist10() + }; + String orderLineDistrictInfo = + stockDistrict[(int) (Long.reverse(districtId) % stockDistrict.length)]; + + long orderLineQuantity = quantities[line]; + long stockQuantity = stock.getQuantity(); + if (stockQuantity > orderLineQuantity) { + stockQuantity = stockQuantity - orderLineQuantity; + } else { + stockQuantity = stockQuantity - orderLineQuantity + 91; + } + stock.setQuantity(stockQuantity); // Update stock quantity + + // Calculate order line amount + BigDecimal totalTax = warehouse.getwTax().add(district.getdTax()).add(BigDecimal.ONE); + BigDecimal discountFactor = BigDecimal.ONE.subtract(customer.getDiscount()); + BigDecimal orderLineAmount = + BigDecimal.valueOf(orderLineQuantity) + .multiply(item.getiPrice()) + .multiply(totalTax) + .multiply(discountFactor); + + // Create and add order line to the list + OrderLine orderLine = new OrderLine(); + orderLine.setId(new OrderLineId(districtNextOrderId, customerId, districtId, warehouseId, line)); + orderLine.setOlIId(orderLineItemId); + orderLine.setOlSupplyWId(supplyWarehouses[line]); + orderLine.setOlQuantity(orderLineQuantity); + orderLine.setOlAmount(orderLineAmount); + orderLine.setOlDistInfo(orderLineDistrictInfo); + orderLine.setOrder(order); // Set the order for the order line + orderLines.add(orderLine); + } + + // Add order lines to the order + order.setOrderLines(orderLines); + session.persist(order); + session.persist(newOrder); + + tx.commit(); } catch (Exception e) { - System.out.println(e.getMessage()); + if (tx != null) { + tx.rollback(); + } + e.printStackTrace(); } - tx.commit(); } @Override public void payment() throws SQLException { Session session = sessionThreadLocal.get(); - Transaction tx = session.beginTransaction(); + long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); + long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); + long customerId = Long.reverse(random.nextInt(tpccConfiguration.getCustomersPerDistrict())); + BigDecimal amount = BigDecimal.valueOf(random.nextInt(1, 5000)); + + long customerWarehouseId; + long customerDistrictId; + String lastName = LastNameGenerator.generateLastName(this.random, Long.MAX_VALUE); + boolean byName; - // Execute the "select 1" query + if (random.nextInt(100) < 60) { + byName = true; + } else { + byName = false; + } + if (random.nextInt(100) < 85) { + customerWarehouseId = warehouseId; + customerDistrictId = districtId; + } else { + customerWarehouseId = getOtherWarehouseId(warehouseId); + customerDistrictId = + Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); + } + Transaction tx = session.beginTransaction(); try { - Integer result = session.createSelectionQuery("select 2", Integer.class).getSingleResult(); + // update warehouse YTD amount + Warehouse warehouse = session.get(Warehouse.class, warehouseId); + warehouse.setwYtd(warehouse.getwYtd().add(amount)); + + // Update district YTD amount + District district = session.get(District.class, new DistrictId(districtId, warehouseId)); + district.setdYtd(district.getdYtd().add(amount)); + + if (byName) { + CriteriaBuilder cb = session.getCriteriaBuilder(); + // Count customers with the given last name + CriteriaQuery countQuery = cb.createQuery(Long.class); + Root countRoot = countQuery.from(Customer.class); + countQuery.select(cb.count(countRoot)); + countQuery.where( + cb.equal(countRoot.get("id").get("wId"), customerWarehouseId), + cb.equal(countRoot.get("id").get("dId"), customerDistrictId), + cb.equal(countRoot.get("last"), lastName) + ); + int nameCount = session.createQuery(countQuery).getSingleResult().intValue(); + if (nameCount % 2 == 0) { + nameCount++; + } + + // Retrieve customer ID (using nameCount as a pseudo-random offset) + CriteriaQuery idQuery = cb.createQuery(Long.class); + Root idRoot = idQuery.from(Customer.class); + idQuery.select(idRoot.get("id").get("cId")); + idQuery.where( + cb.equal(idRoot.get("id").get("wId"), customerWarehouseId), + cb.equal(idRoot.get("id").get("dId"), customerDistrictId), + cb.equal(idRoot.get("last"), lastName) + ); + idQuery.orderBy(cb.asc(idRoot.get("first"))); + + List results = session.createQuery(idQuery).getResultList(); + for (int counter = 0; counter < Math.min(nameCount, results.size()); counter++) { + customerId = results.get(counter); + } + } + + // Update customer + Customer customer = session.get(Customer.class, new CustomerId(customerId, customerDistrictId, customerWarehouseId)); + if (customer != null) { + customer.setBalance(customer.getBalance().subtract(amount)); + customer.setYtdPayment(customer.getYtdPayment().add(amount)); + + if ("BC".equals(customer.getCredit())) { + String customerData = customer.getData(); + String newCustomerData = String.format( + "| %4d %2d %4d %2d %4d $%7.2f %12s %24s", + customerId, + customerDistrictId, + customerWarehouseId, + districtId, + warehouseId, + amount, + LocalDateTime.now(), + customerData); + if (newCustomerData.length() > 500) { + newCustomerData = newCustomerData.substring(0, 500); + } + customer.setData(newCustomerData); + } + + // Insert history + History history = new History(); + history.setId(new HistoryId(customerId, customerDistrictId, customerWarehouseId, districtId, warehouseId, new Timestamp(System.currentTimeMillis()))); + history.setAmount(amount); + history.setData(String.format("%10s %10s", warehouse.getwName(), district.getdName())); + session.persist(history); + } + + tx.commit(); } catch (Exception e) { - System.out.println(e.getMessage()); + if (tx != null) { + tx.rollback(); + } + e.printStackTrace(); } - tx.commit(); } @Override public void orderStatus() throws SQLException { Session session = sessionThreadLocal.get(); - Transaction tx = session.beginTransaction(); - // Execute the "select 1" query + long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); + long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); + long customerId = Long.reverse(random.nextInt(tpccConfiguration.getCustomersPerDistrict())); + + String lastName = LastNameGenerator.generateLastName(this.random, Long.MAX_VALUE); + boolean byName = random.nextInt(100) < 60; + + Transaction tx = session.beginTransaction(); try { - Integer result = session.createSelectionQuery("select 3", Integer.class).getSingleResult(); + Customer customer = null; + if (byName) { + CriteriaBuilder cb = session.getCriteriaBuilder(); + + // Count customers with the given last name + CriteriaQuery countQuery = cb.createQuery(Long.class); + Root countRoot = countQuery.from(Customer.class); + countQuery.select(cb.count(countRoot)); + + countQuery.where( + cb.equal(countRoot.get("id").get("wId"), warehouseId), + cb.equal(countRoot.get("id").get("dId"), districtId), + cb.equal(countRoot.get("last"), lastName) + ); + int nameCount = session.createQuery(countQuery).getSingleResult().intValue(); + + if (nameCount % 2 == 0) { + nameCount++; + } + + // retrieve customer + CriteriaQuery customerQuery = cb.createQuery(Customer.class); + Root idRoot = customerQuery.from(Customer.class); + customerQuery.where( + cb.equal(idRoot.get("id").get("wId"), warehouseId), + cb.equal(idRoot.get("id").get("dId"), districtId), + cb.equal(idRoot.get("last"), lastName) + ); + customerQuery.orderBy(cb.asc(idRoot.get("first"))); + + List results = session.createQuery(customerQuery).getResultList(); + for (int counter = 0; counter < Math.min(nameCount, results.size()); counter++) { + customer = results.get(counter); + customerId = customer.getId().getcId(); + } + } else { + customer = session.get(Customer.class, new CustomerId(customerId, warehouseId, districtId)); + } + + if (customer != null) { + // Fetch the latest order for the customer + CriteriaBuilder cb = session.getCriteriaBuilder(); + CriteriaQuery orderQuery = cb.createQuery(Order.class); + Root orderRoot = orderQuery.from(Order.class); + orderQuery.where( + cb.equal(orderRoot.get("id").get("wId"), warehouseId), + cb.equal(orderRoot.get("id").get("dId"), districtId), + cb.equal(orderRoot.get("id").get("cId"), customerId) + ); + orderQuery.orderBy(cb.desc(orderRoot.get("id").get("oId"))); + + Order order = session.createQuery(orderQuery) + .setMaxResults(1) + .getSingleResult(); + + List orderLines = order.getOrderLines(); + for (OrderLine orderLine : orderLines) { + Long id = orderLine.getOlIId(); + } + } + tx.commit(); } catch (Exception e) { - System.out.println(e.getMessage()); + if (tx != null) { + tx.rollback(); + } + e.printStackTrace(); } - tx.commit(); } + @Override public void delivery() throws SQLException { Session session = sessionThreadLocal.get(); + long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); + long carrierId = Long.reverse(random.nextInt(10)); Transaction tx = session.beginTransaction(); - - // Execute the "select 1" query try { - Integer result = session.createSelectionQuery("select 4", Integer.class).getSingleResult(); + for (long district = 0L; district < tpccConfiguration.getDistrictsPerWarehouse(); district++) { + // find the oldest new_order in district + long districtId = Long.reverse(district); + CriteriaBuilder cb = session.getCriteriaBuilder(); + CriteriaQuery query = cb.createQuery(NewOrder.class); + Root root = query.from(NewOrder.class); + query.where( + cb.equal(root.get("id").get("dId"), districtId), + cb.equal(root.get("id").get("wId"), warehouseId) + ); + query.orderBy(cb.asc(root.get("id").get("oId"))); + NewOrder newOrder = session.createQuery(query) + .setMaxResults(1) + .getSingleResult(); + + + if (newOrder != null) { + // Get the Order and Customer entities + Order order = newOrder.getOrder(); + Customer customer = order.getCustomer(); + // mark newOrder for removal + session.remove(newOrder); + + // Update the corresponding order with the carrier ID + order.setCarrierId(carrierId); + + // Update the delivery date in the order lines + for (OrderLine orderLine : order.getOrderLines()) { + Timestamp t = new Timestamp(System.currentTimeMillis()); + orderLine.setOlDeliveryD(t); + } + + // Calculate the sum of order line amounts + BigDecimal sumOrderLineAmount = order.getOrderLines().stream() + .map(OrderLine::getOlAmount) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + // Update the customer's balance and delivery count + customer.setBalance(customer.getBalance().add(sumOrderLineAmount)); + customer.setDeliveryCnt(customer.getDeliveryCnt() + 1); + } + } + tx.commit(); } catch (Exception e) { - System.out.println(e.getMessage()); + if (tx != null) { + tx.rollback(); + } + e.printStackTrace(); } - tx.commit(); } public void stockLevel() throws SQLException { + long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); + long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); + int level = random.nextInt(10, 21); + Session session = sessionThreadLocal.get(); Transaction tx = session.beginTransaction(); - // Execute the "select 1" query try { - Integer result = session.createSelectionQuery("select 5", Integer.class).getSingleResult(); + CriteriaBuilder cb = session.getCriteriaBuilder(); + + // Retrieve the next available order number for the district + CriteriaQuery nextOrderQuery = cb.createQuery(Long.class); + Root districtRoot = nextOrderQuery.from(District.class); + nextOrderQuery.select(districtRoot.get("dNextOId")); + nextOrderQuery.where( + cb.equal(districtRoot.get("id").get("dId"), districtId), + cb.equal(districtRoot.get("id").get("wId"), warehouseId) + ); + + long nextOrderId = session.createQuery(nextOrderQuery).getSingleResult(); + + CriteriaQuery query = cb.createQuery(Long.class); + Root orderLine = query.from(OrderLine.class); + Root stock = query.from(Stock.class); + + query.select(cb.countDistinct(stock.get("id").get("sIId"))); + + query.where( + cb.equal(orderLine.get("id").get("wId"), warehouseId), + cb.equal(orderLine.get("id").get("dId"), districtId), + cb.lt(orderLine.get("id").get("oId"), nextOrderId), + cb.greaterThanOrEqualTo(orderLine.get("id").get("oId"), nextOrderId - 20), + cb.equal(stock.get("id").get("wId"), warehouseId), + cb.equal(orderLine.get("olIId"), stock.get("id").get("sIId")), + cb.lt(stock.get("quantity"), level) + ); + List result = session.createQuery(query).getResultList(); + + // Iterate through the order lines and count stock items with quantity below threshold + for (Long orderLineItemId : result) { + CriteriaQuery stockQuery = cb.createQuery(Long.class); + Root stockRoot = stockQuery.from(Stock.class); + stockQuery.select(cb.count(stockRoot)); + stockQuery.where( + cb.equal(stockRoot.get("id").get("wId"), warehouseId), + cb.equal(stockRoot.get("id").get("sIId"), orderLineItemId), + cb.lt(stockRoot.get("quantity"), level) + ); + long stockCount = session.createQuery(stockQuery).getSingleResult(); + } + + tx.commit(); } catch (Exception e) { + if (tx != null) { + tx.rollback(); + } + e.printStackTrace(); } - tx.commit(); } @Override diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java index ffb7afc07..a23c1a5a2 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java @@ -1,10 +1,13 @@ package com.google.cloud.pgadapter.tpcc.entities; +import jakarta.persistence.Basic; import jakarta.persistence.Column; +import jakarta.persistence.EmbeddedId; import jakarta.persistence.Entity; -import jakarta.persistence.Id; +import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumns; +import jakarta.persistence.Lob; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import java.math.BigDecimal; @@ -14,77 +17,78 @@ @Table(name = "customer") public class Customer { - @Id - @Column(name = "c_id") - private Long cId; + @EmbeddedId + private CustomerId id; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ - @JoinColumn(name = "w_id", referencedColumnName = "w_id"), - @JoinColumn(name = "d_id", referencedColumnName = "d_id") + @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false), + @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false) }) private District district; - @Column(name = "c_first", length = 16) - private String cFirst; + @Column(name = "c_first") + private String first; - @Column(name = "c_middle", length = 2) - private String cMiddle; + @Column(name = "c_middle") + private String middle; - @Column(name = "c_last", length = 16) - private String cLast; + @Column(name = "c_last") + private String last; - @Column(name = "c_street_1", length = 20) - private String cStreet1; + @Column(name = "c_street_1") + private String street1; - @Column(name = "c_street_2", length = 20) - private String cStreet2; + @Column(name = "c_street_2") + private String street2; - @Column(name = "c_city", length = 20) - private String cCity; + @Column(name = "c_city") + private String city; - @Column(name = "c_state", length = 2) - private String cState; + @Column(name = "c_state") + private String state; - @Column(name = "c_zip", length = 9) - private String cZip; + @Column(name = "c_zip") + private String zip; - @Column(name = "c_phone", length = 16) - private String cPhone; + @Column(name = "c_phone") + private String phone; @Column(name = "c_since") - private Timestamp cSince; + private Timestamp since; - @Column(name = "c_credit", length = 2) - private String cCredit; + @Column(name = "c_credit") + private String credit; @Column(name = "c_credit_lim") - private Long cCreditLim; + private Long creditLim; - @Column(name = "c_discount", precision = 12, scale = 4) - private BigDecimal cDiscount; + @Column(name = "c_discount") + private BigDecimal discount; - @Column(name = "c_balance", precision = 12, scale = 4) - private BigDecimal cBalance; + @Column(name = "c_balance") + private BigDecimal balance; - @Column(name = "c_ytd_payment", precision = 12, scale = 4) - private BigDecimal cYtdPayment; + @Column(name = "c_ytd_payment") + private BigDecimal ytdPayment; @Column(name = "c_payment_cnt") - private Long cPaymentCnt; + private Long paymentCnt; @Column(name = "c_delivery_cnt") - private Long cDeliveryCnt; + private Long deliveryCnt; - @Column(name = "c_data", columnDefinition = "TEXT") - private String cData; + @Lob + @Basic(fetch = FetchType.LAZY) + @Column(name = "c_data") + private String data; - public Long getcId() { - return cId; + public CustomerId getId() { + return id; } - public void setcId(Long cId) { - this.cId = cId; + public void setId(CustomerId id) { + this.id = id; } public District getDistrict() { @@ -95,147 +99,148 @@ public void setDistrict(District district) { this.district = district; } - public String getcFirst() { - return cFirst; + public String getFirst() { + return first; } - public void setcFirst(String cFirst) { - this.cFirst = cFirst; + public void setFirst(String first) { + this.first = first; } - public String getcMiddle() { - return cMiddle; + public String getMiddle() { + return middle; } - public void setcMiddle(String cMiddle) { - this.cMiddle = cMiddle; + public void setMiddle(String middle) { + this.middle = middle; } - public String getcLast() { - return cLast; + public String getLast() { + return last; } - public void setcLast(String cLast) { - this.cLast = cLast; + public void setLast(String last) { + this.last = last; } - public String getcStreet1() { - return cStreet1; + public String getStreet1() { + return street1; } - public void setcStreet1(String cStreet1) { - this.cStreet1 = cStreet1; + public void setStreet1(String street1) { + this.street1 = street1; } - public String getcStreet2() { - return cStreet2; + public String getStreet2() { + return street2; } - public void setcStreet2(String cStreet2) { - this.cStreet2 = cStreet2; + public void setStreet2(String street2) { + this.street2 = street2; } - public String getcCity() { - return cCity; + public String getCity() { + return city; } - public void setcCity(String cCity) { - this.cCity = cCity; + public void setCity(String city) { + this.city = city; } - public String getcState() { - return cState; + public String getState() { + return state; } - public void setcState(String cState) { - this.cState = cState; + public void setState(String state) { + this.state = state; } - public String getcZip() { - return cZip; + public String getZip() { + return zip; } - public void setcZip(String cZip) { - this.cZip = cZip; + public void setZip(String zip) { + this.zip = zip; } - public String getcPhone() { - return cPhone; + public String getPhone() { + return phone; } - public void setcPhone(String cPhone) { - this.cPhone = cPhone; + public void setPhone(String phone) { + this.phone = phone; } - public Timestamp getcSince() { - return cSince; + public Timestamp getSince() { + return since; } - public void setcSince(Timestamp cSince) { - this.cSince = cSince; + public void setSince(Timestamp since) { + this.since = since; } - public String getcCredit() { - return cCredit; + public String getCredit() { + return credit; } - public void setcCredit(String cCredit) { - this.cCredit = cCredit; + public void setCredit(String credit) { + this.credit = credit; } - public Long getcCreditLim() { - return cCreditLim; + public Long getCreditLim() { + return creditLim; } - public void setcCreditLim(Long cCreditLim) { - this.cCreditLim = cCreditLim; + public void setCreditLim(Long creditLim) { + this.creditLim = creditLim; } - public BigDecimal getcDiscount() { - return cDiscount; + public BigDecimal getDiscount() { + return discount; } - public void setcDiscount(BigDecimal cDiscount) { - this.cDiscount = cDiscount; + public void setDiscount(BigDecimal discount) { + this.discount = discount; } - public BigDecimal getcBalance() { - return cBalance; + public BigDecimal getBalance() { + return balance; } - public void setcBalance(BigDecimal cBalance) { - this.cBalance = cBalance; + public void setBalance(BigDecimal balance) { + this.balance = balance; } - public BigDecimal getcYtdPayment() { - return cYtdPayment; + public BigDecimal getYtdPayment() { + return ytdPayment; } - public void setcYtdPayment(BigDecimal cYtdPayment) { - this.cYtdPayment = cYtdPayment; + public void setYtdPayment(BigDecimal ytdPayment) { + this.ytdPayment = ytdPayment; } - public Long getcPaymentCnt() { - return cPaymentCnt; + public Long getPaymentCnt() { + return paymentCnt; } - public void setcPaymentCnt(Long cPaymentCnt) { - this.cPaymentCnt = cPaymentCnt; + public void setPaymentCnt(Long paymentCnt) { + this.paymentCnt = paymentCnt; } - public Long getcDeliveryCnt() { - return cDeliveryCnt; + public Long getDeliveryCnt() { + return deliveryCnt; } - public void setcDeliveryCnt(Long cDeliveryCnt) { - this.cDeliveryCnt = cDeliveryCnt; + public void setDeliveryCnt(Long deliveryCnt) { + this.deliveryCnt = deliveryCnt; } - public String getcData() { - return cData; + public String getData() { + return data; } - public void setcData(String cData) { - this.cData = cData; + public void setData(String data) { + this.data = data; } + } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java new file mode 100644 index 000000000..0cbe1a8dc --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java @@ -0,0 +1,80 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import com.google.common.base.Objects; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import java.io.Serializable; + +@Embeddable +public class CustomerId implements Serializable { + + @Column(name = "c_id") + private Long cId; + + @Column(name = "d_id") + private Long dId; + + @Column(name = "w_id") + private Long wId; + + public CustomerId(){ + + } + + public CustomerId(long customerId, long districtId, long warehouseId) { + this.cId = customerId; + this.dId = districtId; + this.wId = warehouseId; + } + + public Long getcId() { + return cId; + } + + public void setcId(Long cId) { + this.cId = cId; + } + + public Long getdId() { + return dId; + } + + public void setdId(Long dId) { + this.dId = dId; + } + + public Long getwId() { + return wId; + } + + public void setwId(Long wId) { + this.wId = wId; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof CustomerId that)) { + return false; + } + return Objects.equal(cId, that.cId) + && Objects.equal(dId, that.dId) + && Objects.equal(wId, that.wId); + } + + @Override + public int hashCode() { + return Objects.hashCode(cId, dId, wId); + } + + @Override + public String toString() { + return "CustomerId{" + + "cId=" + cId + + ", dId=" + dId + + ", wId=" + wId + + '}'; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java index 5e31be758..4b5098af5 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java @@ -134,37 +134,4 @@ public void setdNextOId(Long dNextOId) { this.dNextOId = dNextOId; } - @Override - public String toString() { - return "District{" - + "id=" - + id - + ", warehouse=" - + warehouse - + ", dName='" - + dName - + '\'' - + ", dStreet1='" - + dStreet1 - + '\'' - + ", dStreet2='" - + dStreet2 - + '\'' - + ", dCity='" - + dCity - + '\'' - + ", dState='" - + dState - + '\'' - + ", dZip='" - + dZip - + '\'' - + ", dTax=" - + dTax - + ", dYtd=" - + dYtd - + ", dNextOId=" - + dNextOId - + '}'; - } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java index fffb4cd7a..e18bde5b8 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java @@ -1,7 +1,9 @@ package com.google.cloud.pgadapter.tpcc.entities; import jakarta.persistence.Column; +import jakarta.persistence.EmbeddedId; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @@ -16,39 +18,35 @@ @Table(name = "history") public class History { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; // You might want to add a surrogate key + @EmbeddedId + private HistoryId id; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ - @JoinColumn(name = "c_id", referencedColumnName = "c_id"), - @JoinColumn(name = "d_id", referencedColumnName = "d_id"), - @JoinColumn(name = "w_id", referencedColumnName = "w_id") + @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false), + @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false), + @JoinColumn(name = "c_id", referencedColumnName = "c_id", insertable = false, updatable = false) }) private Customer customer; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ - @JoinColumn(name = "h_w_id", referencedColumnName = "w_id"), - @JoinColumn(name = "h_d_id", referencedColumnName = "d_id") + @JoinColumn(name = "h_w_id", referencedColumnName = "w_id", insertable = false, updatable = false), + @JoinColumn(name = "h_d_id", referencedColumnName = "d_id", insertable = false, updatable = false) }) private District district; - @Column(name = "h_date") - private Timestamp hDate; + @Column(name = "h_amount") + private BigDecimal amount; - @Column(name = "h_amount", precision = 12, scale = 4) - private BigDecimal hAmount; + @Column(name = "h_data") + private String data; - @Column(name = "h_data", length = 24) - private String hData; - - public Long getId() { + public HistoryId getId() { return id; } - public void setId(Long id) { + public void setId(HistoryId id) { this.id = id; } @@ -68,27 +66,19 @@ public void setDistrict(District district) { this.district = district; } - public Timestamp gethDate() { - return hDate; - } - - public void sethDate(Timestamp hDate) { - this.hDate = hDate; - } - - public BigDecimal gethAmount() { - return hAmount; + public BigDecimal getAmount() { + return amount; } - public void sethAmount(BigDecimal hAmount) { - this.hAmount = hAmount; + public void setAmount(BigDecimal amount) { + this.amount = amount; } - public String gethData() { - return hData; + public String getData() { + return data; } - public void sethData(String hData) { - this.hData = hData; + public void setData(String data) { + this.data = data; } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java new file mode 100644 index 000000000..19d4891ae --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java @@ -0,0 +1,124 @@ +package com.google.cloud.pgadapter.tpcc.entities; + + +import com.google.common.base.Objects; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import java.io.Serializable; +import java.sql.Timestamp; + +@Embeddable +public class HistoryId implements Serializable { + + @Column(name = "c_id") + private Long cId; + + @Column(name = "d_id") + private Long dId; + + @Column(name = "w_id") + private Long wId; + + @Column(name = "h_d_id") + private Long hDId; + + @Column(name = "h_w_id") + private Long hWId; + + @Column(name = "h_date") + private Timestamp hDate; + + public HistoryId() { + } + + public HistoryId(long customerId, long customerDistrictId, long customerWarehouseId, + long districtId, long warehouseId, Timestamp timestamp) { + this.cId = customerId; + this.dId = customerDistrictId; + this.wId = customerWarehouseId; + this.hDId = districtId; + this.hWId = warehouseId; + this.hDate = timestamp; + } + + public Long getcId() { + return cId; + } + + public void setcId(Long cId) { + this.cId = cId; + } + + public Long getdId() { + return dId; + } + + public void setdId(Long dId) { + this.dId = dId; + } + + public Long getwId() { + return wId; + } + + public void setwId(Long wId) { + this.wId = wId; + } + + public Long gethDId() { + return hDId; + } + + public void sethDId(Long hDId) { + this.hDId = hDId; + } + + public Long gethWId() { + return hWId; + } + + public void sethWId(Long hWId) { + this.hWId = hWId; + } + + public Timestamp gethDate() { + return hDate; + } + + public void sethDate(Timestamp hDate) { + this.hDate = hDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof HistoryId historyId)) { + return false; + } + return Objects.equal(cId, historyId.cId) + && Objects.equal(dId, historyId.dId) + && Objects.equal(wId, historyId.wId) + && Objects.equal(hDId, historyId.hDId) + && Objects.equal(hWId, historyId.hWId) + && Objects.equal(hDate, historyId.hDate); + } + + @Override + public int hashCode() { + return Objects.hashCode(cId, dId, wId, hDId, hWId, hDate); + } + + @Override + public String toString() { + return "HistoryId{" + + "cId=" + cId + + ", dId=" + dId + + ", wId=" + wId + + ", hDId=" + hDId + + ", hWId=" + hWId + + ", hDate=" + hDate + + '}'; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java index 4b4bc33b4..de0c7b49e 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java @@ -1,35 +1,35 @@ package com.google.cloud.pgadapter.tpcc.entities; -import jakarta.persistence.Column; +import jakarta.persistence.EmbeddedId; import jakarta.persistence.Entity; -import jakarta.persistence.Id; +import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumns; -import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToOne; import jakarta.persistence.Table; @Entity @Table(name = "new_orders") public class NewOrder { - @Id - @Column(name = "o_id") - private Long oId; + @EmbeddedId + private OrderId id; - @ManyToOne + @OneToOne(fetch = FetchType.LAZY) @JoinColumns({ - @JoinColumn(name = "w_id", referencedColumnName = "w_id"), - @JoinColumn(name = "d_id", referencedColumnName = "d_id"), - @JoinColumn(name = "c_id", referencedColumnName = "c_id") + @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false), + @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false), + @JoinColumn(name = "c_id", referencedColumnName = "c_id", insertable = false, updatable = false), + @JoinColumn(name = "o_id", referencedColumnName = "o_id", insertable = false, updatable = false) }) private Order order; - public Long getoId() { - return oId; + public OrderId getId() { + return id; } - public void setoId(Long oId) { - this.oId = oId; + public void setId(OrderId id) { + this.id = id; } public Order getOrder() { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java index dee06eb25..ee32e2167 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java @@ -1,87 +1,102 @@ package com.google.cloud.pgadapter.tpcc.entities; +import jakarta.persistence.CascadeType; import jakarta.persistence.Column; +import jakarta.persistence.EmbeddedId; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumns; import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import java.sql.Timestamp; +import java.util.List; @Entity @Table(name = "orders") public class Order { + @EmbeddedId + private OrderId id; - @Id - @Column(name = "o_id") - private Long oId; - @ManyToOne + @OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.ALL) + private List orderLines; + + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + + @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ - @JoinColumn(name = "w_id", referencedColumnName = "w_id"), - @JoinColumn(name = "d_id", referencedColumnName = "d_id"), - @JoinColumn(name = "c_id", referencedColumnName = "c_id") + @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false), + @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false), + @JoinColumn(name = "c_id", referencedColumnName = "c_id", insertable = false, updatable = false) }) private Customer customer; + public List getOrderLines() { + return orderLines; + } + + public void setOrderLines(List orderLines) { + this.orderLines = orderLines; + } + @Column(name = "o_entry_d") - private Timestamp oEntryD; + private Timestamp entryD; @Column(name = "o_carrier_id") - private Long oCarrierId; + private Long carrierId; @Column(name = "o_ol_cnt") - private Long oOlCnt; + private Integer olCnt; @Column(name = "o_all_local") - private Long oAllLocal; - - public Long getoId() { - return oId; - } - - public void setoId(Long oId) { - this.oId = oId; - } + private Integer allLocal; - public Customer getCustomer() { - return customer; + public OrderId getId() { + return id; } - public void setCustomer(Customer customer) { - this.customer = customer; + public void setId(OrderId id) { + this.id = id; } - public Timestamp getoEntryD() { - return oEntryD; + public Timestamp getEntryD() { + return entryD; } - public void setoEntryD(Timestamp oEntryD) { - this.oEntryD = oEntryD; + public void setEntryD(Timestamp entryD) { + this.entryD = entryD; } - public Long getoCarrierId() { - return oCarrierId; + public Long getCarrierId() { + return carrierId; } - public void setoCarrierId(Long oCarrierId) { - this.oCarrierId = oCarrierId; + public void setCarrierId(Long carrierId) { + this.carrierId = carrierId; } - public Long getoOlCnt() { - return oOlCnt; + public Integer getOlCnt() { + return olCnt; } - public void setoOlCnt(Long oOlCnt) { - this.oOlCnt = oOlCnt; + public void setOlCnt(Integer olCnt) { + this.olCnt = olCnt; } - public Long getoAllLocal() { - return oAllLocal; + public Integer getAllLocal() { + return allLocal; } - public void setoAllLocal(Long oAllLocal) { - this.oAllLocal = oAllLocal; + public void setAllLocal(Integer allLocal) { + this.allLocal = allLocal; } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java new file mode 100644 index 000000000..08b159214 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java @@ -0,0 +1,93 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import com.google.common.base.Objects; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import java.io.Serializable; + +@Embeddable +public class OrderId implements Serializable { + + @Column(name = "o_id") + private Long oId; + + @Column(name = "c_id") + private Long cId; + + @Column(name = "d_id") + private Long dId; + + @Column(name = "w_id") + private Long wId; + + public OrderId() { + } + + public OrderId(long newOrderId, long customerId, long districtId, long warehouseId) { + this.oId = newOrderId; + this.cId = customerId; + this.dId = districtId; + this.wId = warehouseId; + } + + public Long getoId() { + return oId; + } + + public void setoId(Long oId) { + this.oId = oId; + } + + public Long getcId() { + return cId; + } + + public void setcId(Long cId) { + this.cId = cId; + } + + public Long getdId() { + return dId; + } + + public void setdId(Long dId) { + this.dId = dId; + } + + public Long getwId() { + return wId; + } + + public void setwId(Long wId) { + this.wId = wId; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof OrderId that)) { + return false; + } + return Objects.equal(oId, that.oId) + && Objects.equal(cId, that.cId) + && Objects.equal(dId, that.dId) + && Objects.equal(wId, that.wId); + } + + @Override + public int hashCode() { + return Objects.hashCode(oId, cId, dId, wId); + } + + @Override + public String toString() { + return "OrderId{" + + "oId=" + oId + + ", cId=" + cId + + ", dId=" + dId + + ", wId=" + wId + + '}'; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java index b589c2650..dbc734f52 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java @@ -7,41 +7,112 @@ @Entity @Table(name = "order_line") public class OrderLine { + @EmbeddedId + private OrderLineId id; - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; // You might want to add a surrogate key + @Column(name = "ol_i_id", insertable = false, updatable = false) + private Long olIId; - @ManyToOne + @Column(name = "ol_supply_w_id", insertable = false, updatable = false) + private Long olSupplyWId; + + @Column(name = "ol_delivery_d") + private Timestamp olDeliveryD; + + @Column(name = "ol_quantity") + private Long olQuantity; + + @Column(name = "ol_amount") + private BigDecimal olAmount; + + @Column(name = "ol_dist_info") + private String olDistInfo; + + @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ - @JoinColumn(name = "w_id", referencedColumnName = "w_id"), - @JoinColumn(name = "d_id", referencedColumnName = "d_id"), - @JoinColumn(name = "c_id", referencedColumnName = "c_id"), - @JoinColumn(name = "o_id", referencedColumnName = "o_id") + @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable=false, updatable=false), + @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable=false, updatable=false), + @JoinColumn(name = "c_id", referencedColumnName = "c_id", insertable=false, updatable=false), + @JoinColumn(name = "o_id", referencedColumnName = "o_id", insertable=false, updatable=false) }) private Order order; - @Column(name = "ol_number") - private Long olNumber; - - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ @JoinColumn(name = "ol_supply_w_id", referencedColumnName = "w_id"), @JoinColumn(name = "ol_i_id", referencedColumnName = "s_i_id") }) private Stock stock; - @Column(name = "ol_delivery_d") - private Timestamp olDeliveryD; + public OrderLineId getId() { + return id; + } - @Column(name = "ol_quantity") - private Long olQuantity; + public void setId(OrderLineId id) { + this.id = id; + } - @Column(name = "ol_amount", precision = 12, scale = 4) - private BigDecimal olAmount; + public Long getOlIId() { + return olIId; + } - @Column(name = "ol_dist_info", length = 24) - private String olDistInfo; + public void setOlIId(Long olIId) { + this.olIId = olIId; + } + + public Long getOlSupplyWId() { + return olSupplyWId; + } + + public void setOlSupplyWId(Long olSupplyWId) { + this.olSupplyWId = olSupplyWId; + } + + public Timestamp getOlDeliveryD() { + return olDeliveryD; + } + + public void setOlDeliveryD(Timestamp olDeliveryD) { + this.olDeliveryD = olDeliveryD; + } + + public Long getOlQuantity() { + return olQuantity; + } + + public void setOlQuantity(Long olQuantity) { + this.olQuantity = olQuantity; + } + + public BigDecimal getOlAmount() { + return olAmount; + } + + public void setOlAmount(BigDecimal olAmount) { + this.olAmount = olAmount; + } + + public String getOlDistInfo() { + return olDistInfo; + } + + public void setOlDistInfo(String olDistInfo) { + this.olDistInfo = olDistInfo; + } + + public Order getOrder() { + return order; + } + + public void setOrder(Order order) { + this.order = order; + } + + public Stock getStock() { + return stock; + } - // Getters and setters + public void setStock(Stock stock) { + this.stock = stock; + } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java new file mode 100644 index 000000000..e69cc1ce5 --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java @@ -0,0 +1,114 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import com.google.common.base.Objects; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; + +@Embeddable +public class OrderLineId implements java.io.Serializable { + + private static final long serialVersionUID = 1L; + + @Column(name = "o_id") + private Long oId; + + @Column(name = "c_id") + private Long cId; + + @Column(name = "d_id") + private Long dId; + + @Column(name = "w_id") + private Long wId; + + @Column(name = "ol_number") + private Long olNumber; + + public OrderLineId() { + } + + public OrderLineId(long districtNextOrderId, long customerId, long districtId, long warehouseId, + long l) { + this.oId = districtNextOrderId; + this.cId = customerId; + this.dId = districtId; + this.wId = warehouseId; + this.olNumber = l; + } + + public Long getoId() { + return oId; + } + + public void setoId(Long oId) { + this.oId = oId; + } + + public Long getcId() { + return cId; + } + + public void setcId(Long cId) { + this.cId = cId; + } + + public Long getdId() { + return dId; + } + + public void setdId(Long dId) { + this.dId = dId; + } + + public Long getwId() { + return wId; + } + + public void setwId(Long wId) { + this.wId = wId; + } + + public Long getOlNumber() { + return olNumber; + } + + public void setOlNumber(Long olNumber) { + this.olNumber = olNumber; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof OrderLineId that)) { + return false; + } + return Objects.equal(oId, that.oId) + && Objects.equal(cId, that.cId) + && Objects.equal(dId, that.dId) + && Objects.equal(wId, that.wId) + && Objects.equal(olNumber, that.olNumber); + } + + @Override + public int hashCode() { + return Objects.hashCode(oId, cId, dId, wId, olNumber); + } + + @Override + public String toString() { + return "OrderLineId{" + + "oId=" + + oId + + ", cId=" + + cId + + ", dId=" + + dId + + ", wId=" + + wId + + ", olNumber=" + + olNumber + + '}'; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java index 23c4ea0a9..92f62c226 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java @@ -7,65 +7,68 @@ @Table(name = "stock") public class Stock { - @Id - @Column(name = "s_i_id") - private Long sIId; + @EmbeddedId + private StockId id; - @ManyToOne - @JoinColumn(name = "w_id", referencedColumnName = "w_id") + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "w_id", insertable = false, updatable = false) private Warehouse warehouse; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "s_i_id", insertable = false, updatable = false) + private Item item; + @Column(name = "s_quantity") - private Long sQuantity; + private Long quantity; - @Column(name = "s_dist_01", length = 24) - private String sDist01; + @Column(name = "s_dist_01") + private String dist01; - @Column(name = "s_dist_02", length = 24) - private String sDist02; + @Column(name = "s_dist_02") + private String dist02; - @Column(name = "s_dist_03", length = 24) - private String sDist03; + @Column(name = "s_dist_03") + private String dist03; - @Column(name = "s_dist_04", length = 24) - private String sDist04; + @Column(name = "s_dist_04") + private String dist04; - @Column(name = "s_dist_05", length = 24) - private String sDist05; + @Column(name = "s_dist_05") + private String dist05; - @Column(name = "s_dist_06", length = 24) - private String sDist06; + @Column(name = "s_dist_06") + private String dist06; - @Column(name = "s_dist_07", length = 24) - private String sDist07; + @Column(name = "s_dist_07") + private String dist07; - @Column(name = "s_dist_08", length = 24) - private String sDist08; + @Column(name = "s_dist_08") + private String dist08; - @Column(name = "s_dist_09", length = 24) - private String sDist09; + @Column(name = "s_dist_09") + private String dist09; - @Column(name = "s_dist_10", length = 24) - private String sDist10; + @Column(name = "s_dist_10") + private String dist10; - @Column(name = "s_ytd", precision = 12, scale = 4) - private BigDecimal sYtd; + @Column(name = "s_ytd") + private BigDecimal ytd; @Column(name = "s_order_cnt") - private Long sOrderCnt; + private Long orderCnt; @Column(name = "s_remote_cnt") - private Long sRemoteCnt; + private Long remoteCnt; - @Column(name = "s_data", length = 50) - private String sData; + @Column(name = "s_data") + private String data; - public Long getsIId() { - return sIId; + public StockId getId() { + return id; } - public void setsIId(Long sIId) { - this.sIId = sIId; + public void setId(StockId id) { + this.id = id; } public Warehouse getWarehouse() { @@ -76,123 +79,132 @@ public void setWarehouse(Warehouse warehouse) { this.warehouse = warehouse; } - public Long getsQuantity() { - return sQuantity; + public Item getItem() { + return item; + } + + public void setItem(Item item) { + this.item = item; } - public void setsQuantity(Long sQuantity) { - this.sQuantity = sQuantity; + public Long getQuantity() { + return quantity; } - public String getsDist01() { - return sDist01; + public void setQuantity(Long quantity) { + this.quantity = quantity; } - public void setsDist01(String sDist01) { - this.sDist01 = sDist01; + public String getDist01() { + return dist01; } - public String getsDist02() { - return sDist02; + public void setDist01(String dist01) { + this.dist01 = dist01; } - public void setsDist02(String sDist02) { - this.sDist02 = sDist02; + public String getDist02() { + return dist02; } - public String getsDist03() { - return sDist03; + public void setDist02(String dist02) { + this.dist02 = dist02; } - public void setsDist03(String sDist03) { - this.sDist03 = sDist03; + public String getDist03() { + return dist03; } - public String getsDist04() { - return sDist04; + public void setDist03(String dist03) { + this.dist03 = dist03; } - public void setsDist04(String sDist04) { - this.sDist04 = sDist04; + public String getDist04() { + return dist04; } - public String getsDist05() { - return sDist05; + public void setDist04(String dist04) { + this.dist04 = dist04; } - public void setsDist05(String sDist05) { - this.sDist05 = sDist05; + public String getDist05() { + return dist05; } - public String getsDist06() { - return sDist06; + public void setDist05(String dist05) { + this.dist05 = dist05; } - public void setsDist06(String sDist06) { - this.sDist06 = sDist06; + public String getDist06() { + return dist06; } - public String getsDist07() { - return sDist07; + public void setDist06(String dist06) { + this.dist06 = dist06; } - public void setsDist07(String sDist07) { - this.sDist07 = sDist07; + public String getDist07() { + return dist07; } - public String getsDist08() { - return sDist08; + public void setDist07(String dist07) { + this.dist07 = dist07; } - public void setsDist08(String sDist08) { - this.sDist08 = sDist08; + public String getDist08() { + return dist08; } - public String getsDist09() { - return sDist09; + public void setDist08(String dist08) { + this.dist08 = dist08; } - public void setsDist09(String sDist09) { - this.sDist09 = sDist09; + public String getDist09() { + return dist09; } - public String getsDist10() { - return sDist10; + public void setDist09(String dist09) { + this.dist09 = dist09; } - public void setsDist10(String sDist10) { - this.sDist10 = sDist10; + public String getDist10() { + return dist10; } - public BigDecimal getsYtd() { - return sYtd; + public void setDist10(String dist10) { + this.dist10 = dist10; } - public void setsYtd(BigDecimal sYtd) { - this.sYtd = sYtd; + public BigDecimal getYtd() { + return ytd; } - public Long getsOrderCnt() { - return sOrderCnt; + public void setYtd(BigDecimal ytd) { + this.ytd = ytd; } - public void setsOrderCnt(Long sOrderCnt) { - this.sOrderCnt = sOrderCnt; + public Long getOrderCnt() { + return orderCnt; } - public Long getsRemoteCnt() { - return sRemoteCnt; + public void setOrderCnt(Long orderCnt) { + this.orderCnt = orderCnt; } - public void setsRemoteCnt(Long sRemoteCnt) { - this.sRemoteCnt = sRemoteCnt; + public Long getRemoteCnt() { + return remoteCnt; } - public String getsData() { - return sData; + public void setRemoteCnt(Long remoteCnt) { + this.remoteCnt = remoteCnt; } - public void setsData(String sData) { - this.sData = sData; + public String getData() { + return data; } + + public void setData(String data) { + this.data = data; + } + } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java new file mode 100644 index 000000000..4a5954d1e --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java @@ -0,0 +1,63 @@ +package com.google.cloud.pgadapter.tpcc.entities; + +import com.google.common.base.Objects; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import java.io.Serializable; + +@Embeddable +public class StockId implements Serializable { + + private static final long serialVersionUID = 1L; + + @Column(name = "s_i_id") + private Long sIId; + + @Column(name = "w_id") + private Long wId; + + public StockId() { + } + + public StockId(long orderLineItemId, long supplyWarehouse) { + this.sIId = orderLineItemId; + this.wId = supplyWarehouse; + } + + public Long getsIId() { + return sIId; + } + + public void setsIId(Long sIId) { + this.sIId = sIId; + } + + public Long getwId() { + return wId; + } + + public void setwId(Long wId) { + this.wId = wId; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof StockId stockId)) { + return false; + } + return Objects.equal(sIId, stockId.sIId) && Objects.equal(wId, stockId.wId); + } + + @Override + public int hashCode() { + return Objects.hashCode(sIId, wId); + } + + @Override + public String toString() { + return "StockId{" + "sIId=" + sIId + ", wId=" + wId + '}'; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java index ab1495471..cc107c16c 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java @@ -110,33 +110,4 @@ public void setwYtd(BigDecimal wYtd) { this.wYtd = wYtd; } - @Override - public String toString() { - return "Warehouse{" - + "wId=" - + wId - + ", wName='" - + wName - + '\'' - + ", wStreet1='" - + wStreet1 - + '\'' - + ", wStreet2='" - + wStreet2 - + '\'' - + ", wCity='" - + wCity - + '\'' - + ", wState='" - + wState - + '\'' - + ", wZip='" - + wZip - + '\'' - + ", wTax=" - + wTax - + ", wYtd=" - + wYtd - + '}'; - } } diff --git a/benchmarks/tpcc/src/main/resources/application.properties b/benchmarks/tpcc/src/main/resources/application.properties index e4e839783..42e80e5ff 100644 --- a/benchmarks/tpcc/src/main/resources/application.properties +++ b/benchmarks/tpcc/src/main/resources/application.properties @@ -8,13 +8,13 @@ logging.threshold.console=OFF # Load the initial data set. Enable this on one client to fill the database. tpcc.load-data=false -tpcc.load-data-threads=1 +tpcc.load-data-threads=32 # Truncates all existing data before loading new data. This setting only has any effect when # tpcc.load-data also has been enabled. -tpcc.truncate-before-load=true +tpcc.truncate-before-load=false # Choose to use the runner (pgadapter/spanner_jdbc/client_lib_pg) -tpcc.benchmark-runner=spanner_jdbc +tpcc.benchmark-runner=pgadapter # Run the benchmark. tpcc.run-benchmark=true @@ -22,7 +22,7 @@ tpcc.run-benchmark=true tpcc.benchmark-threads=8 # The duration that the benchmark should run in ISO-8601 notation. # E.g. PT60S (60 seconds), PT10M (10 minutes), PT2H (2 hours), P1D (1 day) -tpcc.benchmark-duration=PT10M +tpcc.benchmark-duration=PT300S # The number of warehouses in the database. This is commonly known as the 'factor'. Increasing this # number also increases the number of dependent records (districts, customers, orders, ...). @@ -43,9 +43,9 @@ tpcc.use-read-only-transactions=false tpcc.lock-scanned-ranges=false # Change these to match your Cloud Spanner PostgreSQL-dialect database. -spanner.project=span-cloud-testing -spanner.instance=hengfeng-pg-tpcc -spanner.database=tpcc-gsql +spanner.project=my-project +spanner.instance=my-instance +spanner.database=my-database # --- IN-PROCESS PGADAPTER --- # @@ -54,13 +54,13 @@ spanner.database=tpcc-gsql # Set this to true to instruct the benchmark runner to start a PGAdapter instance in-process with # the benchmark application. -pgadapter.in-process=false +pgadapter.in-process=true pgadapter.num-channels=32 # Set this if you want the in-process PGAdapter instance to use a specific service account # credentials file. # Leave unset if the application should use the APPLICATION_DEFAULT_CREDENTIALS. -pgadapter.credentials= +pgadapter.credentials=/Users/rayudualp/Downloads/span-cloud-testing-5084c5e7353d.json # Set this to true to disable automatic retries of aborted transactions by PGAdapter. Disabling this # will propagate all aborted transaction errors to the application, and the transaction will be diff --git a/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml b/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml index ceb57755f..19443f04c 100644 --- a/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml +++ b/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml @@ -12,28 +12,30 @@ - jdbc:cloudspanner:/projects/google.com:span-test-1/instances/bench-1/databases/tpcc + jdbc:cloudspanner:/projects/span-cloud-testing/instances/hengfeng-pg-tpcc/databases/tpcc-gsql com.google.cloud.spanner.hibernate.SpannerDialect - true + false none + 64 + - - - - - - - + + + + + + + \ No newline at end of file From 89b333c4fcd96944b2e93f93333ea52c6f1149d3 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Wed, 20 Nov 2024 12:03:59 +0530 Subject: [PATCH 03/22] fix --- .../cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java | 6 +++--- .../cloud/pgadapter/tpcc/BenchmarkApplication.java | 2 +- .../cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java index 5d4f22944..eb1d0bc9a 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java @@ -96,17 +96,17 @@ private void runTransactions() throws SQLException, InterruptedException { int transaction = random.nextInt(23); Stopwatch stopwatch = Stopwatch.createStarted(); if (transaction < 10) { - stockLevel(); + newOrder(); Duration executionDuration = stopwatch.elapsed(); metrics.recordNewOrderLatency(executionDuration.toMillis()); statistics.incNewOrder(); } else if (transaction < 20) { - stockLevel(); + payment(); Duration executionDuration = stopwatch.elapsed(); metrics.recordPaymentLatency(executionDuration.toMillis()); statistics.incPayment(); } else if (transaction < 21) { - stockLevel(); + delivery(); Duration executionDuration = stopwatch.elapsed(); metrics.recordOrderStatusLatency(executionDuration.toMillis()); statistics.incOrderStatus(); diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java index a871c8603..354764aeb 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java @@ -217,7 +217,7 @@ public void run(String... args) throws Exception { while (watch.elapsed().compareTo(tpccConfiguration.getBenchmarkDuration()) <= 0) { //noinspection BusyWait Thread.sleep(1_000L); - //statistics.print(watch.elapsed()); + statistics.print(watch.elapsed()); } executor.shutdownNow(); if (!executor.awaitTermination(60L, TimeUnit.SECONDS)) { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index ea4e11ecd..87df4dd30 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -198,7 +198,7 @@ public void newOrder() throws SQLException { if (tx != null) { tx.rollback(); } - e.printStackTrace(); + //e.printStackTrace(); } } @@ -308,7 +308,7 @@ public void payment() throws SQLException { if (tx != null) { tx.rollback(); } - e.printStackTrace(); + //e.printStackTrace(); } } @@ -390,7 +390,7 @@ public void orderStatus() throws SQLException { if (tx != null) { tx.rollback(); } - e.printStackTrace(); + //e.printStackTrace(); } } @@ -449,7 +449,7 @@ public void delivery() throws SQLException { if (tx != null) { tx.rollback(); } - e.printStackTrace(); + //e.printStackTrace(); } } @@ -510,7 +510,7 @@ public void stockLevel() throws SQLException { if (tx != null) { tx.rollback(); } - e.printStackTrace(); + //e.printStackTrace(); } } From b4fecd6842fda494fc4404afc1172147e43d0574 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Wed, 20 Nov 2024 12:18:04 +0530 Subject: [PATCH 04/22] fix --- .../google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java index eb1d0bc9a..a521c084d 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java @@ -106,12 +106,12 @@ private void runTransactions() throws SQLException, InterruptedException { metrics.recordPaymentLatency(executionDuration.toMillis()); statistics.incPayment(); } else if (transaction < 21) { - delivery(); + orderStatus(); Duration executionDuration = stopwatch.elapsed(); metrics.recordOrderStatusLatency(executionDuration.toMillis()); statistics.incOrderStatus(); } else if (transaction < 22) { - stockLevel(); + delivery(); Duration executionDuration = stopwatch.elapsed(); metrics.recordDeliveryLatency(executionDuration.toMillis()); statistics.incDelivery(); From bbddeadb2da0bf329086df39f6bbd9865cade57f Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Wed, 20 Nov 2024 12:54:58 +0530 Subject: [PATCH 05/22] fix --- .../cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index 87df4dd30..1a6638fcb 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -198,7 +198,7 @@ public void newOrder() throws SQLException { if (tx != null) { tx.rollback(); } - //e.printStackTrace(); + throw new RuntimeException(e); } } @@ -308,7 +308,7 @@ public void payment() throws SQLException { if (tx != null) { tx.rollback(); } - //e.printStackTrace(); + throw new RuntimeException(e); } } @@ -390,7 +390,7 @@ public void orderStatus() throws SQLException { if (tx != null) { tx.rollback(); } - //e.printStackTrace(); + throw new RuntimeException(e); } } @@ -449,7 +449,7 @@ public void delivery() throws SQLException { if (tx != null) { tx.rollback(); } - //e.printStackTrace(); + throw new RuntimeException(e); } } @@ -510,7 +510,7 @@ public void stockLevel() throws SQLException { if (tx != null) { tx.rollback(); } - //e.printStackTrace(); + throw new RuntimeException(e); } } From aa63a2b5fc41aacea966df207469b1a212591ad2 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Wed, 20 Nov 2024 13:37:01 +0530 Subject: [PATCH 06/22] fix --- .../com/google/cloud/pgadapter/tpcc/entities/DistrictId.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java index 8f090ffe7..b6223e840 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java @@ -9,10 +9,10 @@ public class DistrictId implements java.io.Serializable { private static final long serialVersionUID = 1L; @Column(name = "w_id") - private Long dId; + private Long wId; @Column(name = "d_id") - private Long wId; + private Long dId; public DistrictId() {} From 7679ce66f26c4962c0440e439ea6c005200993b3 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Thu, 21 Nov 2024 12:20:52 +0530 Subject: [PATCH 07/22] batch --- .../google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java | 3 ++- benchmarks/tpcc/src/main/resources/hibernate.cfg.xml | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index 1a6638fcb..4b0565881 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -475,6 +475,7 @@ public void stockLevel() throws SQLException { long nextOrderId = session.createQuery(nextOrderQuery).getSingleResult(); + // Retrieve items from the last 20 orders CriteriaQuery query = cb.createQuery(Long.class); Root orderLine = query.from(OrderLine.class); Root stock = query.from(Stock.class); @@ -492,7 +493,7 @@ public void stockLevel() throws SQLException { ); List result = session.createQuery(query).getResultList(); - // Iterate through the order lines and count stock items with quantity below threshold + // Iterate through the items and check stock items with quantity below threshold for (Long orderLineItemId : result) { CriteriaQuery stockQuery = cb.createQuery(Long.class); Root stockRoot = stockQuery.from(Stock.class); diff --git a/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml b/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml index 19443f04c..d14db4379 100644 --- a/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml +++ b/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml @@ -25,6 +25,7 @@ none 64 + 20 From 15d1d414fbbeaddc3e95653fecaf0df12f3ddc69 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Fri, 22 Nov 2024 14:54:28 +0530 Subject: [PATCH 08/22] pr review improvements --- .../pgadapter/tpcc/BenchmarkApplication.java | 15 +- .../tpcc/HibernateBenchmarkRunner.java | 141 ++++++++++-------- .../tpcc/config/HibernateConfiguration.java | 52 +++++++ .../pgadapter/tpcc/entities/Customer.java | 15 +- .../pgadapter/tpcc/entities/CustomerId.java | 13 ++ .../pgadapter/tpcc/entities/District.java | 15 +- .../pgadapter/tpcc/entities/DistrictId.java | 13 ++ .../pgadapter/tpcc/entities/History.java | 13 ++ .../pgadapter/tpcc/entities/HistoryId.java | 14 +- .../cloud/pgadapter/tpcc/entities/Item.java | 13 ++ .../pgadapter/tpcc/entities/NewOrder.java | 15 +- .../cloud/pgadapter/tpcc/entities/Order.java | 13 ++ .../pgadapter/tpcc/entities/OrderId.java | 13 ++ .../pgadapter/tpcc/entities/OrderLine.java | 13 ++ .../pgadapter/tpcc/entities/OrderLineId.java | 13 ++ .../cloud/pgadapter/tpcc/entities/Stock.java | 13 ++ .../pgadapter/tpcc/entities/StockId.java | 13 ++ .../pgadapter/tpcc/entities/Warehouse.java | 13 ++ .../src/main/resources/application.properties | 6 + .../tpcc/src/main/resources/hibernate.cfg.xml | 3 +- 20 files changed, 348 insertions(+), 71 deletions(-) create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java index 354764aeb..5a443bab5 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java @@ -15,6 +15,7 @@ import com.google.cloud.opentelemetry.metric.GoogleCloudMetricExporter; import com.google.cloud.opentelemetry.metric.MetricConfiguration; +import com.google.cloud.pgadapter.tpcc.config.HibernateConfiguration; import com.google.cloud.pgadapter.tpcc.config.PGAdapterConfiguration; import com.google.cloud.pgadapter.tpcc.config.SpannerConfiguration; import com.google.cloud.pgadapter.tpcc.config.TpccConfiguration; @@ -69,13 +70,16 @@ public static void main(String[] args) { private final TpccConfiguration tpccConfiguration; + private final HibernateConfiguration hibernateConfiguration; + public BenchmarkApplication( SpannerConfiguration spannerConfiguration, PGAdapterConfiguration pgAdapterConfiguration, - TpccConfiguration tpccConfiguration) { + TpccConfiguration tpccConfiguration, HibernateConfiguration hibernateConfiguration) { this.spannerConfiguration = spannerConfiguration; this.pgAdapterConfiguration = pgAdapterConfiguration; this.tpccConfiguration = tpccConfiguration; + this.hibernateConfiguration = hibernateConfiguration; } @Override @@ -141,7 +145,12 @@ public void run(String... args) throws Exception { Executors.newFixedThreadPool(tpccConfiguration.getBenchmarkThreads()); if (tpccConfiguration.getBenchmarkRunner().equals(TpccConfiguration.HIBERNATE_RUNNER)) { - registry = new StandardServiceRegistryBuilder().configure().build(); + registry = new StandardServiceRegistryBuilder() + .configure() + .applySetting("hibernate.show_sql", hibernateConfiguration.isShowSql()) + .applySetting("hibernate.jdbc.batch_size", hibernateConfiguration.getBatchSize()) + .applySetting("hibernate.connection.pool_size", hibernateConfiguration.getPoolSize()) + .build(); sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); } for (int i = 0; i < tpccConfiguration.getBenchmarkThreads(); i++) { @@ -217,7 +226,7 @@ public void run(String... args) throws Exception { while (watch.elapsed().compareTo(tpccConfiguration.getBenchmarkDuration()) <= 0) { //noinspection BusyWait Thread.sleep(1_000L); - statistics.print(watch.elapsed()); + //statistics.print(watch.elapsed()); } executor.shutdownNow(); if (!executor.awaitTermination(60L, TimeUnit.SECONDS)) { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index 4b0565881..e6c61804d 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc; import com.google.cloud.pgadapter.tpcc.config.PGAdapterConfiguration; @@ -6,7 +19,6 @@ import com.google.cloud.pgadapter.tpcc.entities.Customer; import com.google.cloud.pgadapter.tpcc.entities.CustomerId; import com.google.cloud.pgadapter.tpcc.entities.District; -import com.google.cloud.pgadapter.tpcc.entities.DistrictId; import com.google.cloud.pgadapter.tpcc.entities.History; import com.google.cloud.pgadapter.tpcc.entities.HistoryId; import com.google.cloud.pgadapter.tpcc.entities.Item; @@ -21,9 +33,7 @@ import com.google.cloud.spanner.Dialect; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; -import jakarta.persistence.criteria.JoinType; import jakarta.persistence.criteria.Root; -import jakarta.persistence.criteria.Subquery; import java.io.IOException; import java.math.BigDecimal; import java.sql.SQLException; @@ -35,12 +45,10 @@ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; -import org.hibernate.query.Query; public class HibernateBenchmarkRunner extends AbstractBenchmarkRunner { private final SessionFactory sessionFactory; - private final ThreadLocal sessionThreadLocal = new ThreadLocal<>(); private final Random random = new Random(); HibernateBenchmarkRunner( @@ -62,23 +70,13 @@ public class HibernateBenchmarkRunner extends AbstractBenchmarkRunner { } @Override - void setup() throws SQLException, IOException { - sessionThreadLocal.set(sessionFactory.openSession()); - } + void setup() throws SQLException, IOException {} @Override - void teardown() throws SQLException { - Session session = sessionThreadLocal.get(); - if (session != null) { - session.close(); - sessionThreadLocal.remove(); - } - } + void teardown() throws SQLException {} @Override public void newOrder() throws SQLException { - Session session = sessionThreadLocal.get(); - long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); long customerId = Long.reverse(random.nextInt(tpccConfiguration.getCustomersPerDistrict())); @@ -104,7 +102,7 @@ public void newOrder() throws SQLException { } quantities[line] = random.nextInt(1, 10); } - + Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { Customer customer = session.get(Customer.class, new CustomerId(customerId, districtId, warehouseId)); @@ -124,12 +122,10 @@ public void newOrder() throws SQLException { order.setAllLocal(allLocal); order.setCustomer(customer); - NewOrder newOrder = new NewOrder(); newOrder.setId(orderId); newOrder.setOrder(order); - // Create and process order lines List orderLines = new ArrayList<>(); for (int line = 0; line < orderLineCount; line++) { @@ -199,12 +195,13 @@ public void newOrder() throws SQLException { tx.rollback(); } throw new RuntimeException(e); + } finally { + session.close(); } } @Override public void payment() throws SQLException { - Session session = sessionThreadLocal.get(); long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); long customerId = Long.reverse(random.nextInt(tpccConfiguration.getCustomersPerDistrict())); @@ -228,16 +225,9 @@ public void payment() throws SQLException { customerDistrictId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); } + Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { - // update warehouse YTD amount - Warehouse warehouse = session.get(Warehouse.class, warehouseId); - warehouse.setwYtd(warehouse.getwYtd().add(amount)); - - // Update district YTD amount - District district = session.get(District.class, new DistrictId(districtId, warehouseId)); - district.setdYtd(district.getdYtd().add(amount)); - if (byName) { CriteriaBuilder cb = session.getCriteriaBuilder(); // Count customers with the given last name @@ -271,51 +261,57 @@ public void payment() throws SQLException { } } - // Update customer + // Update customer balance and Ytd amount Customer customer = session.get(Customer.class, new CustomerId(customerId, customerDistrictId, customerWarehouseId)); - if (customer != null) { - customer.setBalance(customer.getBalance().subtract(amount)); - customer.setYtdPayment(customer.getYtdPayment().add(amount)); - - if ("BC".equals(customer.getCredit())) { - String customerData = customer.getData(); - String newCustomerData = String.format( - "| %4d %2d %4d %2d %4d $%7.2f %12s %24s", - customerId, - customerDistrictId, - customerWarehouseId, - districtId, - warehouseId, - amount, - LocalDateTime.now(), - customerData); - if (newCustomerData.length() > 500) { - newCustomerData = newCustomerData.substring(0, 500); - } - customer.setData(newCustomerData); - } + customer.setBalance(customer.getBalance().subtract(amount)); + customer.setYtdPayment(customer.getYtdPayment().add(amount)); - // Insert history - History history = new History(); - history.setId(new HistoryId(customerId, customerDistrictId, customerWarehouseId, districtId, warehouseId, new Timestamp(System.currentTimeMillis()))); - history.setAmount(amount); - history.setData(String.format("%10s %10s", warehouse.getwName(), district.getdName())); - session.persist(history); + // Update district YTD amount + District district = customer.getDistrict(); + district.setdYtd(district.getdYtd().add(amount)); + + // update warehouse YTD amount + Warehouse warehouse = district.getWarehouse(); + warehouse.setwYtd(warehouse.getwYtd().add(amount)); + + if ("BC".equals(customer.getCredit())) { + String customerData = customer.getData(); + String newCustomerData = String.format( + "| %4d %2d %4d %2d %4d $%7.2f %12s %24s", + customerId, + customerDistrictId, + customerWarehouseId, + districtId, + warehouseId, + amount, + LocalDateTime.now(), + customerData); + if (newCustomerData.length() > 500) { + newCustomerData = newCustomerData.substring(0, 500); + } + customer.setData(newCustomerData); } + // Insert history + History history = new History(); + history.setId(new HistoryId(customerId, customerDistrictId, customerWarehouseId, districtId, warehouseId, new Timestamp(System.currentTimeMillis()))); + history.setAmount(amount); + history.setData(String.format("%10s %10s", warehouse.getwName(), district.getdName())); + session.persist(history); + tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } throw new RuntimeException(e); + } finally { + session.close(); } } @Override public void orderStatus() throws SQLException { - Session session = sessionThreadLocal.get(); - long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); long customerId = Long.reverse(random.nextInt(tpccConfiguration.getCustomersPerDistrict())); @@ -323,6 +319,7 @@ public void orderStatus() throws SQLException { String lastName = LastNameGenerator.generateLastName(this.random, Long.MAX_VALUE); boolean byName = random.nextInt(100) < 60; + Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { Customer customer = null; @@ -391,15 +388,17 @@ public void orderStatus() throws SQLException { tx.rollback(); } throw new RuntimeException(e); + } finally { + session.close(); } } @Override public void delivery() throws SQLException { - Session session = sessionThreadLocal.get(); long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); long carrierId = Long.reverse(random.nextInt(10)); + Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { for (long district = 0L; district < tpccConfiguration.getDistrictsPerWarehouse(); district++) { @@ -428,17 +427,30 @@ public void delivery() throws SQLException { // Update the corresponding order with the carrier ID order.setCarrierId(carrierId); - // Update the delivery date in the order lines + // // Update the delivery date in the order lines for (OrderLine orderLine : order.getOrderLines()) { Timestamp t = new Timestamp(System.currentTimeMillis()); orderLine.setOlDeliveryD(t); } + // // Update the delivery date in the order lines using Criteria API + // CriteriaUpdate updateQuery = cb.createCriteriaUpdate(OrderLine.class); + // Root updateRoot = updateQuery.from(OrderLine.class); + // updateQuery.set(updateRoot.get("olDeliveryD"), cb.currentTimestamp()); + // updateQuery.where(cb.equal(updateRoot.get("order"), order)); + // session.createMutationQuery(updateQuery).executeUpdate(); + // Calculate the sum of order line amounts BigDecimal sumOrderLineAmount = order.getOrderLines().stream() .map(OrderLine::getOlAmount) .reduce(BigDecimal.ZERO, BigDecimal::add); + // CriteriaQuery sumQuery = cb.createQuery(BigDecimal.class); + // Root orderLine = sumQuery.from(OrderLine.class); + // sumQuery.select(cb.sum(orderLine.get("olAmount"))); + // sumQuery.where(cb.equal(orderLine.get("order"), order)); + // BigDecimal sumOrderLineAmount = session.createQuery(sumQuery).getSingleResult(); + // Update the customer's balance and delivery count customer.setBalance(customer.getBalance().add(sumOrderLineAmount)); customer.setDeliveryCnt(customer.getDeliveryCnt() + 1); @@ -450,6 +462,8 @@ public void delivery() throws SQLException { tx.rollback(); } throw new RuntimeException(e); + } finally { + session.close(); } } @@ -458,9 +472,8 @@ public void stockLevel() throws SQLException { long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); int level = random.nextInt(10, 21); - Session session = sessionThreadLocal.get(); + Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); - try { CriteriaBuilder cb = session.getCriteriaBuilder(); @@ -512,6 +525,8 @@ public void stockLevel() throws SQLException { tx.rollback(); } throw new RuntimeException(e); + } finally { + session.close(); } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java new file mode 100644 index 000000000..71137980a --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java @@ -0,0 +1,52 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.cloud.pgadapter.tpcc.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "hibernate") +public class HibernateConfiguration { + + private boolean showSql; + + private int batchSize; + + private int poolSize; + + public boolean isShowSql() { + return showSql; + } + + public void setShowSql(boolean showSql) { + this.showSql = showSql; + } + + public int getBatchSize() { + return batchSize; + } + + public void setBatchSize(int batchSize) { + this.batchSize = batchSize; + } + + public int getPoolSize() { + return poolSize; + } + + public void setPoolSize(int poolSize) { + this.poolSize = poolSize; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java index a23c1a5a2..d4d03cced 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import jakarta.persistence.Basic; @@ -20,7 +33,7 @@ public class Customer { @EmbeddedId private CustomerId id; - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.EAGER) @JoinColumns({ @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false), @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java index 0cbe1a8dc..d4f954367 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import com.google.common.base.Objects; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java index 4b5098af5..f2b4793d5 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import jakarta.persistence.Column; @@ -15,7 +28,7 @@ public class District { @EmbeddedId private DistrictId id; - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false) private Warehouse warehouse; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java index b6223e840..48ec357a9 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/DistrictId.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import com.google.common.base.Objects; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java index e18bde5b8..09250aa5b 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import jakarta.persistence.Column; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java index 19d4891ae..190489231 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java @@ -1,6 +1,18 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; - import com.google.common.base.Objects; import jakarta.persistence.Column; import jakarta.persistence.Embeddable; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java index 1b16a02af..a99be9b60 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import jakarta.persistence.Column; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java index de0c7b49e..047d374cc 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import jakarta.persistence.EmbeddedId; @@ -15,7 +28,7 @@ public class NewOrder { @EmbeddedId private OrderId id; - @OneToOne(fetch = FetchType.LAZY) + @OneToOne(fetch = FetchType.EAGER) @JoinColumns({ @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false), @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false), diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java index ee32e2167..c8c97595d 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import jakarta.persistence.CascadeType; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java index 08b159214..806d33aad 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import com.google.common.base.Objects; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java index dbc734f52..09b247d09 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import jakarta.persistence.*; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java index e69cc1ce5..27e19ac1c 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import com.google.common.base.Objects; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java index 92f62c226..4c69b4de8 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import jakarta.persistence.*; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java index 4a5954d1e..893f62cb3 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import com.google.common.base.Objects; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java index cc107c16c..93289333e 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java @@ -1,3 +1,16 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package com.google.cloud.pgadapter.tpcc.entities; import jakarta.persistence.Column; diff --git a/benchmarks/tpcc/src/main/resources/application.properties b/benchmarks/tpcc/src/main/resources/application.properties index 42e80e5ff..3a483e46e 100644 --- a/benchmarks/tpcc/src/main/resources/application.properties +++ b/benchmarks/tpcc/src/main/resources/application.properties @@ -3,6 +3,7 @@ logging.file.name=tpcc-log.log # Turn off console logging, so you can actually read the output. logging.threshold.console=OFF +logging.level.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=INFO # --- TPCC PROPERTIES --- # @@ -83,3 +84,8 @@ pgadapter.port=5432 # changed. Note that the application uses the PostgreSQL simple query protocol, so it # can explicitly create prepared statements on PGAdapter. pgadapter.connection-url=jdbc:postgresql://${pgadapter.host}:${pgadapter.port}/projects%2F${spanner.project}%2Finstances%2F${spanner.instance}%2Fdatabases%2F${spanner.database} + +# hibernate settings +hibernate.show-sql=false +hibernate.batch-size=20 +hibernate.pool-size=64 \ No newline at end of file diff --git a/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml b/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml index d14db4379..aa2909313 100644 --- a/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml +++ b/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml @@ -19,13 +19,14 @@ com.google.cloud.spanner.hibernate.SpannerDialect - false + true none 64 20 + true From 3bd10a9bedaa853bc72bcd4bd15747f1e5937219 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Fri, 22 Nov 2024 15:06:49 +0530 Subject: [PATCH 09/22] fix --- .../cloud/pgadapter/tpcc/BenchmarkApplication.java | 2 +- .../cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java index 5a443bab5..6c7b914fb 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java @@ -226,7 +226,7 @@ public void run(String... args) throws Exception { while (watch.elapsed().compareTo(tpccConfiguration.getBenchmarkDuration()) <= 0) { //noinspection BusyWait Thread.sleep(1_000L); - //statistics.print(watch.elapsed()); + statistics.print(watch.elapsed()); } executor.shutdownNow(); if (!executor.awaitTermination(60L, TimeUnit.SECONDS)) { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index e6c61804d..94046405f 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -38,6 +38,7 @@ import java.math.BigDecimal; import java.sql.SQLException; import java.sql.Timestamp; +import java.time.Instant; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -427,11 +428,9 @@ public void delivery() throws SQLException { // Update the corresponding order with the carrier ID order.setCarrierId(carrierId); - // // Update the delivery date in the order lines - for (OrderLine orderLine : order.getOrderLines()) { - Timestamp t = new Timestamp(System.currentTimeMillis()); - orderLine.setOlDeliveryD(t); - } + // Update the delivery date in the order lines + Timestamp t = new Timestamp(System.currentTimeMillis()); + order.getOrderLines().forEach(orderLine -> orderLine.setOlDeliveryD(t)); // // Update the delivery date in the order lines using Criteria API // CriteriaUpdate updateQuery = cb.createCriteriaUpdate(OrderLine.class); From 455d83f46c4e8645bf2ef15070312939f4efd73b Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Fri, 22 Nov 2024 18:48:11 +0530 Subject: [PATCH 10/22] dynamic update --- .../java/com/google/cloud/pgadapter/tpcc/entities/Customer.java | 2 ++ .../java/com/google/cloud/pgadapter/tpcc/entities/District.java | 2 ++ .../java/com/google/cloud/pgadapter/tpcc/entities/Item.java | 2 ++ .../java/com/google/cloud/pgadapter/tpcc/entities/Order.java | 2 ++ .../com/google/cloud/pgadapter/tpcc/entities/OrderLine.java | 2 ++ .../java/com/google/cloud/pgadapter/tpcc/entities/Stock.java | 2 ++ .../com/google/cloud/pgadapter/tpcc/entities/Warehouse.java | 2 ++ 7 files changed, 14 insertions(+) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java index d4d03cced..a3381865b 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java @@ -25,8 +25,10 @@ import jakarta.persistence.Table; import java.math.BigDecimal; import java.sql.Timestamp; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "customer") public class Customer { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java index f2b4793d5..965c00502 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java @@ -21,8 +21,10 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import java.math.BigDecimal; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "district") public class District { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java index a99be9b60..d06438e3a 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java @@ -18,8 +18,10 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import java.math.BigDecimal; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "item") public class Item { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java index c8c97595d..1ec9bea00 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java @@ -26,8 +26,10 @@ import jakarta.persistence.Table; import java.sql.Timestamp; import java.util.List; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "orders") public class Order { @EmbeddedId diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java index 09b247d09..92e899a0d 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java @@ -16,8 +16,10 @@ import jakarta.persistence.*; import java.math.BigDecimal; import java.sql.Timestamp; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "order_line") public class OrderLine { @EmbeddedId diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java index 4c69b4de8..85e0ce79d 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java @@ -15,8 +15,10 @@ import jakarta.persistence.*; import java.math.BigDecimal; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "stock") public class Stock { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java index 93289333e..bbd730369 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java @@ -18,8 +18,10 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import java.math.BigDecimal; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "warehouse") public class Warehouse { From 7b6de71000cd17885518f6d9540269649929f650 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Fri, 22 Nov 2024 21:53:01 +0530 Subject: [PATCH 11/22] remove dynamic update --- .../pgadapter/tpcc/BenchmarkApplication.java | 17 +-- .../tpcc/HibernateBenchmarkRunner.java | 114 +++++++++--------- .../pgadapter/tpcc/entities/Customer.java | 14 +-- .../pgadapter/tpcc/entities/CustomerId.java | 10 +- .../pgadapter/tpcc/entities/District.java | 3 - .../pgadapter/tpcc/entities/History.java | 33 +++-- .../pgadapter/tpcc/entities/HistoryId.java | 36 ++++-- .../cloud/pgadapter/tpcc/entities/Item.java | 2 - .../pgadapter/tpcc/entities/NewOrder.java | 23 +++- .../cloud/pgadapter/tpcc/entities/Order.java | 21 ++-- .../pgadapter/tpcc/entities/OrderId.java | 10 +- .../pgadapter/tpcc/entities/OrderLine.java | 25 ++-- .../pgadapter/tpcc/entities/OrderLineId.java | 7 +- .../cloud/pgadapter/tpcc/entities/Stock.java | 6 +- .../pgadapter/tpcc/entities/StockId.java | 3 +- .../pgadapter/tpcc/entities/Warehouse.java | 3 - 16 files changed, 174 insertions(+), 153 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java index 6c7b914fb..7d141fb5c 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java @@ -75,7 +75,8 @@ public static void main(String[] args) { public BenchmarkApplication( SpannerConfiguration spannerConfiguration, PGAdapterConfiguration pgAdapterConfiguration, - TpccConfiguration tpccConfiguration, HibernateConfiguration hibernateConfiguration) { + TpccConfiguration tpccConfiguration, + HibernateConfiguration hibernateConfiguration) { this.spannerConfiguration = spannerConfiguration; this.pgAdapterConfiguration = pgAdapterConfiguration; this.tpccConfiguration = tpccConfiguration; @@ -145,12 +146,14 @@ public void run(String... args) throws Exception { Executors.newFixedThreadPool(tpccConfiguration.getBenchmarkThreads()); if (tpccConfiguration.getBenchmarkRunner().equals(TpccConfiguration.HIBERNATE_RUNNER)) { - registry = new StandardServiceRegistryBuilder() - .configure() - .applySetting("hibernate.show_sql", hibernateConfiguration.isShowSql()) - .applySetting("hibernate.jdbc.batch_size", hibernateConfiguration.getBatchSize()) - .applySetting("hibernate.connection.pool_size", hibernateConfiguration.getPoolSize()) - .build(); + registry = + new StandardServiceRegistryBuilder() + .configure() + .applySetting("hibernate.show_sql", hibernateConfiguration.isShowSql()) + .applySetting("hibernate.jdbc.batch_size", hibernateConfiguration.getBatchSize()) + .applySetting( + "hibernate.connection.pool_size", hibernateConfiguration.getPoolSize()) + .build(); sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); } for (int i = 0; i < tpccConfiguration.getBenchmarkThreads(); i++) { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index 94046405f..6f1b911c0 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -38,7 +38,6 @@ import java.math.BigDecimal; import java.sql.SQLException; import java.sql.Timestamp; -import java.time.Instant; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -106,7 +105,8 @@ public void newOrder() throws SQLException { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { - Customer customer = session.get(Customer.class, new CustomerId(customerId, districtId, warehouseId)); + Customer customer = + session.get(Customer.class, new CustomerId(customerId, districtId, warehouseId)); District district = customer.getDistrict(); Warehouse warehouse = district.getWarehouse(); @@ -134,23 +134,24 @@ public void newOrder() throws SQLException { Item item = session.get(Item.class, orderLineItemId); if (item == null) { - //item not found, rollback(1% chance) + // item not found, rollback(1% chance) tx.rollback(); return; } - Stock stock = session.get(Stock.class, new StockId(orderLineItemId, supplyWarehouses[line])); + Stock stock = + session.get(Stock.class, new StockId(orderLineItemId, supplyWarehouses[line])); String[] stockDistrict = { - stock.getDist01(), - stock.getDist02(), - stock.getDist03(), - stock.getDist04(), - stock.getDist05(), - stock.getDist06(), - stock.getDist07(), - stock.getDist08(), - stock.getDist09(), - stock.getDist10() + stock.getDist01(), + stock.getDist02(), + stock.getDist03(), + stock.getDist04(), + stock.getDist05(), + stock.getDist06(), + stock.getDist07(), + stock.getDist08(), + stock.getDist09(), + stock.getDist10() }; String orderLineDistrictInfo = stockDistrict[(int) (Long.reverse(districtId) % stockDistrict.length)]; @@ -175,7 +176,8 @@ public void newOrder() throws SQLException { // Create and add order line to the list OrderLine orderLine = new OrderLine(); - orderLine.setId(new OrderLineId(districtNextOrderId, customerId, districtId, warehouseId, line)); + orderLine.setId( + new OrderLineId(districtNextOrderId, customerId, districtId, warehouseId, line)); orderLine.setOlIId(orderLineItemId); orderLine.setOlSupplyWId(supplyWarehouses[line]); orderLine.setOlQuantity(orderLineQuantity); @@ -238,8 +240,7 @@ public void payment() throws SQLException { countQuery.where( cb.equal(countRoot.get("id").get("wId"), customerWarehouseId), cb.equal(countRoot.get("id").get("dId"), customerDistrictId), - cb.equal(countRoot.get("last"), lastName) - ); + cb.equal(countRoot.get("last"), lastName)); int nameCount = session.createQuery(countQuery).getSingleResult().intValue(); if (nameCount % 2 == 0) { nameCount++; @@ -252,8 +253,7 @@ public void payment() throws SQLException { idQuery.where( cb.equal(idRoot.get("id").get("wId"), customerWarehouseId), cb.equal(idRoot.get("id").get("dId"), customerDistrictId), - cb.equal(idRoot.get("last"), lastName) - ); + cb.equal(idRoot.get("last"), lastName)); idQuery.orderBy(cb.asc(idRoot.get("first"))); List results = session.createQuery(idQuery).getResultList(); @@ -263,7 +263,9 @@ public void payment() throws SQLException { } // Update customer balance and Ytd amount - Customer customer = session.get(Customer.class, new CustomerId(customerId, customerDistrictId, customerWarehouseId)); + Customer customer = + session.get( + Customer.class, new CustomerId(customerId, customerDistrictId, customerWarehouseId)); customer.setBalance(customer.getBalance().subtract(amount)); customer.setYtdPayment(customer.getYtdPayment().add(amount)); @@ -277,16 +279,17 @@ public void payment() throws SQLException { if ("BC".equals(customer.getCredit())) { String customerData = customer.getData(); - String newCustomerData = String.format( - "| %4d %2d %4d %2d %4d $%7.2f %12s %24s", - customerId, - customerDistrictId, - customerWarehouseId, - districtId, - warehouseId, - amount, - LocalDateTime.now(), - customerData); + String newCustomerData = + String.format( + "| %4d %2d %4d %2d %4d $%7.2f %12s %24s", + customerId, + customerDistrictId, + customerWarehouseId, + districtId, + warehouseId, + amount, + LocalDateTime.now(), + customerData); if (newCustomerData.length() > 500) { newCustomerData = newCustomerData.substring(0, 500); } @@ -295,7 +298,14 @@ public void payment() throws SQLException { // Insert history History history = new History(); - history.setId(new HistoryId(customerId, customerDistrictId, customerWarehouseId, districtId, warehouseId, new Timestamp(System.currentTimeMillis()))); + history.setId( + new HistoryId( + customerId, + customerDistrictId, + customerWarehouseId, + districtId, + warehouseId, + new Timestamp(System.currentTimeMillis()))); history.setAmount(amount); history.setData(String.format("%10s %10s", warehouse.getwName(), district.getdName())); session.persist(history); @@ -335,8 +345,7 @@ public void orderStatus() throws SQLException { countQuery.where( cb.equal(countRoot.get("id").get("wId"), warehouseId), cb.equal(countRoot.get("id").get("dId"), districtId), - cb.equal(countRoot.get("last"), lastName) - ); + cb.equal(countRoot.get("last"), lastName)); int nameCount = session.createQuery(countQuery).getSingleResult().intValue(); if (nameCount % 2 == 0) { @@ -349,8 +358,7 @@ public void orderStatus() throws SQLException { customerQuery.where( cb.equal(idRoot.get("id").get("wId"), warehouseId), cb.equal(idRoot.get("id").get("dId"), districtId), - cb.equal(idRoot.get("last"), lastName) - ); + cb.equal(idRoot.get("last"), lastName)); customerQuery.orderBy(cb.asc(idRoot.get("first"))); List results = session.createQuery(customerQuery).getResultList(); @@ -370,13 +378,10 @@ public void orderStatus() throws SQLException { orderQuery.where( cb.equal(orderRoot.get("id").get("wId"), warehouseId), cb.equal(orderRoot.get("id").get("dId"), districtId), - cb.equal(orderRoot.get("id").get("cId"), customerId) - ); + cb.equal(orderRoot.get("id").get("cId"), customerId)); orderQuery.orderBy(cb.desc(orderRoot.get("id").get("oId"))); - Order order = session.createQuery(orderQuery) - .setMaxResults(1) - .getSingleResult(); + Order order = session.createQuery(orderQuery).setMaxResults(1).getSingleResult(); List orderLines = order.getOrderLines(); for (OrderLine orderLine : orderLines) { @@ -394,7 +399,6 @@ public void orderStatus() throws SQLException { } } - @Override public void delivery() throws SQLException { long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); @@ -402,7 +406,9 @@ public void delivery() throws SQLException { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { - for (long district = 0L; district < tpccConfiguration.getDistrictsPerWarehouse(); district++) { + for (long district = 0L; + district < tpccConfiguration.getDistrictsPerWarehouse(); + district++) { // find the oldest new_order in district long districtId = Long.reverse(district); CriteriaBuilder cb = session.getCriteriaBuilder(); @@ -410,13 +416,9 @@ public void delivery() throws SQLException { Root root = query.from(NewOrder.class); query.where( cb.equal(root.get("id").get("dId"), districtId), - cb.equal(root.get("id").get("wId"), warehouseId) - ); + cb.equal(root.get("id").get("wId"), warehouseId)); query.orderBy(cb.asc(root.get("id").get("oId"))); - NewOrder newOrder = session.createQuery(query) - .setMaxResults(1) - .getSingleResult(); - + NewOrder newOrder = session.createQuery(query).setMaxResults(1).getSingleResult(); if (newOrder != null) { // Get the Order and Customer entities @@ -440,9 +442,10 @@ public void delivery() throws SQLException { // session.createMutationQuery(updateQuery).executeUpdate(); // Calculate the sum of order line amounts - BigDecimal sumOrderLineAmount = order.getOrderLines().stream() - .map(OrderLine::getOlAmount) - .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal sumOrderLineAmount = + order.getOrderLines().stream() + .map(OrderLine::getOlAmount) + .reduce(BigDecimal.ZERO, BigDecimal::add); // CriteriaQuery sumQuery = cb.createQuery(BigDecimal.class); // Root orderLine = sumQuery.from(OrderLine.class); @@ -482,8 +485,7 @@ public void stockLevel() throws SQLException { nextOrderQuery.select(districtRoot.get("dNextOId")); nextOrderQuery.where( cb.equal(districtRoot.get("id").get("dId"), districtId), - cb.equal(districtRoot.get("id").get("wId"), warehouseId) - ); + cb.equal(districtRoot.get("id").get("wId"), warehouseId)); long nextOrderId = session.createQuery(nextOrderQuery).getSingleResult(); @@ -501,8 +503,7 @@ public void stockLevel() throws SQLException { cb.greaterThanOrEqualTo(orderLine.get("id").get("oId"), nextOrderId - 20), cb.equal(stock.get("id").get("wId"), warehouseId), cb.equal(orderLine.get("olIId"), stock.get("id").get("sIId")), - cb.lt(stock.get("quantity"), level) - ); + cb.lt(stock.get("quantity"), level)); List result = session.createQuery(query).getResultList(); // Iterate through the items and check stock items with quantity below threshold @@ -513,8 +514,7 @@ public void stockLevel() throws SQLException { stockQuery.where( cb.equal(stockRoot.get("id").get("wId"), warehouseId), cb.equal(stockRoot.get("id").get("sIId"), orderLineItemId), - cb.lt(stockRoot.get("quantity"), level) - ); + cb.lt(stockRoot.get("quantity"), level)); long stockCount = session.createQuery(stockQuery).getSingleResult(); } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java index a3381865b..f1f5c14f3 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java @@ -25,20 +25,21 @@ import jakarta.persistence.Table; import java.math.BigDecimal; import java.sql.Timestamp; -import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "customer") public class Customer { - @EmbeddedId - private CustomerId id; + @EmbeddedId private CustomerId id; @ManyToOne(fetch = FetchType.EAGER) @JoinColumns({ - @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false), - @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false) + @JoinColumn( + name = "w_id", + referencedColumnName = "w_id", + insertable = false, + updatable = false), + @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false) }) private District district; @@ -257,5 +258,4 @@ public String getData() { public void setData(String data) { this.data = data; } - } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java index d4f954367..53df480e1 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/CustomerId.java @@ -30,9 +30,7 @@ public class CustomerId implements Serializable { @Column(name = "w_id") private Long wId; - public CustomerId(){ - - } + public CustomerId() {} public CustomerId(long customerId, long districtId, long warehouseId) { this.cId = customerId; @@ -84,10 +82,6 @@ public int hashCode() { @Override public String toString() { - return "CustomerId{" + - "cId=" + cId + - ", dId=" + dId + - ", wId=" + wId + - '}'; + return "CustomerId{" + "cId=" + cId + ", dId=" + dId + ", wId=" + wId + '}'; } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java index 965c00502..a1088d5d4 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java @@ -21,10 +21,8 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import java.math.BigDecimal; -import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "district") public class District { @@ -148,5 +146,4 @@ public Long getdNextOId() { public void setdNextOId(Long dNextOId) { this.dNextOId = dNextOId; } - } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java index 09250aa5b..cd5b380a0 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/History.java @@ -17,35 +17,46 @@ import jakarta.persistence.EmbeddedId; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumns; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import java.math.BigDecimal; -import java.sql.Timestamp; @Entity @Table(name = "history") public class History { - @EmbeddedId - private HistoryId id; + @EmbeddedId private HistoryId id; @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ - @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false), - @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false), - @JoinColumn(name = "c_id", referencedColumnName = "c_id", insertable = false, updatable = false) + @JoinColumn( + name = "w_id", + referencedColumnName = "w_id", + insertable = false, + updatable = false), + @JoinColumn( + name = "d_id", + referencedColumnName = "d_id", + insertable = false, + updatable = false), + @JoinColumn(name = "c_id", referencedColumnName = "c_id", insertable = false, updatable = false) }) private Customer customer; @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ - @JoinColumn(name = "h_w_id", referencedColumnName = "w_id", insertable = false, updatable = false), - @JoinColumn(name = "h_d_id", referencedColumnName = "d_id", insertable = false, updatable = false) + @JoinColumn( + name = "h_w_id", + referencedColumnName = "w_id", + insertable = false, + updatable = false), + @JoinColumn( + name = "h_d_id", + referencedColumnName = "d_id", + insertable = false, + updatable = false) }) private District district; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java index 190489231..d503c3b96 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/HistoryId.java @@ -40,11 +40,15 @@ public class HistoryId implements Serializable { @Column(name = "h_date") private Timestamp hDate; - public HistoryId() { - } - - public HistoryId(long customerId, long customerDistrictId, long customerWarehouseId, - long districtId, long warehouseId, Timestamp timestamp) { + public HistoryId() {} + + public HistoryId( + long customerId, + long customerDistrictId, + long customerWarehouseId, + long districtId, + long warehouseId, + Timestamp timestamp) { this.cId = customerId; this.dId = customerDistrictId; this.wId = customerWarehouseId; @@ -124,13 +128,19 @@ public int hashCode() { @Override public String toString() { - return "HistoryId{" + - "cId=" + cId + - ", dId=" + dId + - ", wId=" + wId + - ", hDId=" + hDId + - ", hWId=" + hWId + - ", hDate=" + hDate + - '}'; + return "HistoryId{" + + "cId=" + + cId + + ", dId=" + + dId + + ", wId=" + + wId + + ", hDId=" + + hDId + + ", hWId=" + + hWId + + ", hDate=" + + hDate + + '}'; } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java index d06438e3a..a99be9b60 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Item.java @@ -18,10 +18,8 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import java.math.BigDecimal; -import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "item") public class Item { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java index 047d374cc..403ce75b7 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/NewOrder.java @@ -25,15 +25,26 @@ @Table(name = "new_orders") public class NewOrder { - @EmbeddedId - private OrderId id; + @EmbeddedId private OrderId id; @OneToOne(fetch = FetchType.EAGER) @JoinColumns({ - @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false), - @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false), - @JoinColumn(name = "c_id", referencedColumnName = "c_id", insertable = false, updatable = false), - @JoinColumn(name = "o_id", referencedColumnName = "o_id", insertable = false, updatable = false) + @JoinColumn( + name = "w_id", + referencedColumnName = "w_id", + insertable = false, + updatable = false), + @JoinColumn( + name = "d_id", + referencedColumnName = "d_id", + insertable = false, + updatable = false), + @JoinColumn( + name = "c_id", + referencedColumnName = "c_id", + insertable = false, + updatable = false), + @JoinColumn(name = "o_id", referencedColumnName = "o_id", insertable = false, updatable = false) }) private Order order; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java index 1ec9bea00..9b7ee301d 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java @@ -18,7 +18,6 @@ import jakarta.persistence.EmbeddedId; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; -import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumns; import jakarta.persistence.ManyToOne; @@ -26,15 +25,11 @@ import jakarta.persistence.Table; import java.sql.Timestamp; import java.util.List; -import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "orders") public class Order { - @EmbeddedId - private OrderId id; - + @EmbeddedId private OrderId id; @OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.ALL) private List orderLines; @@ -49,9 +44,17 @@ public void setCustomer(Customer customer) { @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ - @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable = false, updatable = false), - @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable = false, updatable = false), - @JoinColumn(name = "c_id", referencedColumnName = "c_id", insertable = false, updatable = false) + @JoinColumn( + name = "w_id", + referencedColumnName = "w_id", + insertable = false, + updatable = false), + @JoinColumn( + name = "d_id", + referencedColumnName = "d_id", + insertable = false, + updatable = false), + @JoinColumn(name = "c_id", referencedColumnName = "c_id", insertable = false, updatable = false) }) private Customer customer; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java index 806d33aad..e8f846cc7 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderId.java @@ -33,8 +33,7 @@ public class OrderId implements Serializable { @Column(name = "w_id") private Long wId; - public OrderId() { - } + public OrderId() {} public OrderId(long newOrderId, long customerId, long districtId, long warehouseId) { this.oId = newOrderId; @@ -96,11 +95,6 @@ public int hashCode() { @Override public String toString() { - return "OrderId{" + - "oId=" + oId + - ", cId=" + cId + - ", dId=" + dId + - ", wId=" + wId + - '}'; + return "OrderId{" + "oId=" + oId + ", cId=" + cId + ", dId=" + dId + ", wId=" + wId + '}'; } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java index 92e899a0d..bafdacc77 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java @@ -16,14 +16,11 @@ import jakarta.persistence.*; import java.math.BigDecimal; import java.sql.Timestamp; -import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "order_line") public class OrderLine { - @EmbeddedId - private OrderLineId id; + @EmbeddedId private OrderLineId id; @Column(name = "ol_i_id", insertable = false, updatable = false) private Long olIId; @@ -45,10 +42,22 @@ public class OrderLine { @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ - @JoinColumn(name = "w_id", referencedColumnName = "w_id", insertable=false, updatable=false), - @JoinColumn(name = "d_id", referencedColumnName = "d_id", insertable=false, updatable=false), - @JoinColumn(name = "c_id", referencedColumnName = "c_id", insertable=false, updatable=false), - @JoinColumn(name = "o_id", referencedColumnName = "o_id", insertable=false, updatable=false) + @JoinColumn( + name = "w_id", + referencedColumnName = "w_id", + insertable = false, + updatable = false), + @JoinColumn( + name = "d_id", + referencedColumnName = "d_id", + insertable = false, + updatable = false), + @JoinColumn( + name = "c_id", + referencedColumnName = "c_id", + insertable = false, + updatable = false), + @JoinColumn(name = "o_id", referencedColumnName = "o_id", insertable = false, updatable = false) }) private Order order; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java index 27e19ac1c..39fd143cf 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLineId.java @@ -37,11 +37,10 @@ public class OrderLineId implements java.io.Serializable { @Column(name = "ol_number") private Long olNumber; - public OrderLineId() { - } + public OrderLineId() {} - public OrderLineId(long districtNextOrderId, long customerId, long districtId, long warehouseId, - long l) { + public OrderLineId( + long districtNextOrderId, long customerId, long districtId, long warehouseId, long l) { this.oId = districtNextOrderId; this.cId = customerId; this.dId = districtId; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java index 85e0ce79d..a0e3fde36 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java @@ -15,15 +15,12 @@ import jakarta.persistence.*; import java.math.BigDecimal; -import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "stock") public class Stock { - @EmbeddedId - private StockId id; + @EmbeddedId private StockId id; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "w_id", insertable = false, updatable = false) @@ -221,5 +218,4 @@ public String getData() { public void setData(String data) { this.data = data; } - } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java index 893f62cb3..0b83dba39 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/StockId.java @@ -29,8 +29,7 @@ public class StockId implements Serializable { @Column(name = "w_id") private Long wId; - public StockId() { - } + public StockId() {} public StockId(long orderLineItemId, long supplyWarehouse) { this.sIId = orderLineItemId; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java index bbd730369..1531b4d86 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java @@ -18,10 +18,8 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import java.math.BigDecimal; -import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "warehouse") public class Warehouse { @@ -124,5 +122,4 @@ public BigDecimal getwYtd() { public void setwYtd(BigDecimal wYtd) { this.wYtd = wYtd; } - } From f6cda07f41ed85a20ec385e949c8deac67926fa4 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Mon, 25 Nov 2024 16:26:55 +0530 Subject: [PATCH 12/22] auto batch dml --- benchmarks/tpcc/pom.xml | 4 +- .../pgadapter/tpcc/BenchmarkApplication.java | 23 +++++++---- .../tpcc/HibernateBenchmarkRunner.java | 31 +++++++++++++- .../tpcc/config/HibernateConfiguration.java | 40 +++++++++++++++++++ .../src/main/resources/application.properties | 8 +++- 5 files changed, 92 insertions(+), 14 deletions(-) diff --git a/benchmarks/tpcc/pom.xml b/benchmarks/tpcc/pom.xml index b00879af2..24b3fbc91 100644 --- a/benchmarks/tpcc/pom.xml +++ b/benchmarks/tpcc/pom.xml @@ -21,7 +21,7 @@ com.google.cloud libraries-bom - 26.48.0 + 26.50.0 pom import @@ -67,7 +67,7 @@ com.google.cloud google-cloud-spanner-hibernate-dialect - 3.4.1 + 3.7.1 diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java index 7d141fb5c..75e2d27f5 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java @@ -146,14 +146,7 @@ public void run(String... args) throws Exception { Executors.newFixedThreadPool(tpccConfiguration.getBenchmarkThreads()); if (tpccConfiguration.getBenchmarkRunner().equals(TpccConfiguration.HIBERNATE_RUNNER)) { - registry = - new StandardServiceRegistryBuilder() - .configure() - .applySetting("hibernate.show_sql", hibernateConfiguration.isShowSql()) - .applySetting("hibernate.jdbc.batch_size", hibernateConfiguration.getBatchSize()) - .applySetting( - "hibernate.connection.pool_size", hibernateConfiguration.getPoolSize()) - .build(); + registry = buildHibernateConfiguration(); sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); } for (int i = 0; i < tpccConfiguration.getBenchmarkThreads(); i++) { @@ -220,6 +213,7 @@ public void run(String... args) throws Exception { tpccConfiguration, pgAdapterConfiguration, spannerConfiguration, + hibernateConfiguration, metrics, Dialect.GOOGLE_STANDARD_SQL)); } @@ -322,4 +316,17 @@ private ProxyServer startPGAdapter() { return server; } + + private StandardServiceRegistry buildHibernateConfiguration() { + return new StandardServiceRegistryBuilder() + .configure() + .applySetting("hibernate.show_sql", hibernateConfiguration.isShowSql()) + .applySetting("hibernate.jdbc.batch_size", hibernateConfiguration.getBatchSize()) + .applySetting("hibernate.connection.pool_size", hibernateConfiguration.getPoolSize()) + .applySetting("hibernate.order_inserts", hibernateConfiguration.isOrderInserts()) + .applySetting("hibernate.order_updates", hibernateConfiguration.isOrderUpdates()) + .applySetting( + "hibernate.jdbc.batch_versioned_data", hibernateConfiguration.isBatchVersionedData()) + .build(); + } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index 6f1b911c0..787fc8045 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -13,6 +13,7 @@ // limitations under the License. package com.google.cloud.pgadapter.tpcc; +import com.google.cloud.pgadapter.tpcc.config.HibernateConfiguration; import com.google.cloud.pgadapter.tpcc.config.PGAdapterConfiguration; import com.google.cloud.pgadapter.tpcc.config.SpannerConfiguration; import com.google.cloud.pgadapter.tpcc.config.TpccConfiguration; @@ -49,6 +50,8 @@ public class HibernateBenchmarkRunner extends AbstractBenchmarkRunner { private final SessionFactory sessionFactory; + + private final HibernateConfiguration hibernateConfiguration; private final Random random = new Random(); HibernateBenchmarkRunner( @@ -57,6 +60,7 @@ public class HibernateBenchmarkRunner extends AbstractBenchmarkRunner { TpccConfiguration tpccConfiguration, PGAdapterConfiguration pgAdapterConfiguration, SpannerConfiguration spannerConfiguration, + HibernateConfiguration hibernateConfiguration, Metrics metrics, Dialect dialect) { super( @@ -66,6 +70,7 @@ public class HibernateBenchmarkRunner extends AbstractBenchmarkRunner { spannerConfiguration, metrics, dialect); + this.hibernateConfiguration = hibernateConfiguration; this.sessionFactory = sessionFactory; } @@ -103,6 +108,9 @@ public void newOrder() throws SQLException { quantities[line] = random.nextInt(1, 10); } Session session = sessionFactory.openSession(); + if (hibernateConfiguration.isAutoBatchDml()) { + session.doWork(connection -> connection.createStatement().execute("set auto_batch_dml=true")); + } Transaction tx = session.beginTransaction(); try { Customer customer = @@ -191,9 +199,13 @@ public void newOrder() throws SQLException { order.setOrderLines(orderLines); session.persist(order); session.persist(newOrder); - + session.flush(); + session.doWork( + connection -> connection.createStatement().execute("set auto_batch_dml=false")); tx.commit(); } catch (Exception e) { + session.doWork( + connection -> connection.createStatement().execute("set auto_batch_dml=false")); if (tx != null) { tx.rollback(); } @@ -229,6 +241,9 @@ public void payment() throws SQLException { Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); } Session session = sessionFactory.openSession(); + if (hibernateConfiguration.isAutoBatchDml()) { + session.doWork(connection -> connection.createStatement().execute("set auto_batch_dml=true")); + } Transaction tx = session.beginTransaction(); try { if (byName) { @@ -309,9 +324,13 @@ public void payment() throws SQLException { history.setAmount(amount); history.setData(String.format("%10s %10s", warehouse.getwName(), district.getdName())); session.persist(history); - + session.flush(); + session.doWork( + connection -> connection.createStatement().execute("set auto_batch_dml=false")); tx.commit(); } catch (Exception e) { + session.doWork( + connection -> connection.createStatement().execute("set auto_batch_dml=false")); if (tx != null) { tx.rollback(); } @@ -404,6 +423,9 @@ public void delivery() throws SQLException { long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); long carrierId = Long.reverse(random.nextInt(10)); Session session = sessionFactory.openSession(); + if (hibernateConfiguration.isAutoBatchDml()) { + session.doWork(connection -> connection.createStatement().execute("set auto_batch_dml=true")); + } Transaction tx = session.beginTransaction(); try { for (long district = 0L; @@ -458,8 +480,13 @@ public void delivery() throws SQLException { customer.setDeliveryCnt(customer.getDeliveryCnt() + 1); } } + session.flush(); + session.doWork( + connection -> connection.createStatement().execute("set auto_batch_dml=false")); tx.commit(); } catch (Exception e) { + session.doWork( + connection -> connection.createStatement().execute("set auto_batch_dml=false")); if (tx != null) { tx.rollback(); } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java index 71137980a..03340a5ba 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java @@ -26,6 +26,46 @@ public class HibernateConfiguration { private int poolSize; + private boolean orderInserts; + + private boolean orderUpdates; + + private boolean batchVersionedData; + + private boolean autoBatchDml; + + public boolean isAutoBatchDml() { + return autoBatchDml; + } + + public void setAutoBatchDml(boolean autoBatchDml) { + this.autoBatchDml = autoBatchDml; + } + + public boolean isBatchVersionedData() { + return batchVersionedData; + } + + public void setBatchVersionedData(boolean batchVersionedData) { + this.batchVersionedData = batchVersionedData; + } + + public boolean isOrderInserts() { + return orderInserts; + } + + public void setOrderInserts(boolean orderInserts) { + this.orderInserts = orderInserts; + } + + public boolean isOrderUpdates() { + return orderUpdates; + } + + public void setOrderUpdates(boolean orderUpdates) { + this.orderUpdates = orderUpdates; + } + public boolean isShowSql() { return showSql; } diff --git a/benchmarks/tpcc/src/main/resources/application.properties b/benchmarks/tpcc/src/main/resources/application.properties index 3a483e46e..f2f1fbeaa 100644 --- a/benchmarks/tpcc/src/main/resources/application.properties +++ b/benchmarks/tpcc/src/main/resources/application.properties @@ -87,5 +87,9 @@ pgadapter.connection-url=jdbc:postgresql://${pgadapter.host}:${pgadapter.port}/p # hibernate settings hibernate.show-sql=false -hibernate.batch-size=20 -hibernate.pool-size=64 \ No newline at end of file +hibernate.batch-size=1 +hibernate.pool-size=64 +hibernate.order-inserts=false +hibernate.order-updates=false +hibernate.batch-versioned-data=false +hibernate.auto-batch-dml=false From 6f6662cfea2e86a3fda6baf61f6b08fb55d0c127 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Mon, 25 Nov 2024 20:48:11 +0530 Subject: [PATCH 13/22] performant version for delivery() transaction --- .../tpcc/HibernateBenchmarkRunner.java | 75 ++++++++++++------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index 787fc8045..a2ddf94e9 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -34,6 +34,7 @@ import com.google.cloud.spanner.Dialect; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.CriteriaUpdate; import jakarta.persistence.criteria.Root; import java.io.IOException; import java.math.BigDecimal; @@ -443,41 +444,57 @@ public void delivery() throws SQLException { NewOrder newOrder = session.createQuery(query).setMaxResults(1).getSingleResult(); if (newOrder != null) { - // Get the Order and Customer entities + // Get the Order entitie Order order = newOrder.getOrder(); - Customer customer = order.getCustomer(); // mark newOrder for removal session.remove(newOrder); - // Update the corresponding order with the carrier ID order.setCarrierId(carrierId); - // Update the delivery date in the order lines - Timestamp t = new Timestamp(System.currentTimeMillis()); - order.getOrderLines().forEach(orderLine -> orderLine.setOlDeliveryD(t)); - - // // Update the delivery date in the order lines using Criteria API - // CriteriaUpdate updateQuery = cb.createCriteriaUpdate(OrderLine.class); - // Root updateRoot = updateQuery.from(OrderLine.class); - // updateQuery.set(updateRoot.get("olDeliveryD"), cb.currentTimestamp()); - // updateQuery.where(cb.equal(updateRoot.get("order"), order)); - // session.createMutationQuery(updateQuery).executeUpdate(); - - // Calculate the sum of order line amounts - BigDecimal sumOrderLineAmount = - order.getOrderLines().stream() - .map(OrderLine::getOlAmount) - .reduce(BigDecimal.ZERO, BigDecimal::add); - - // CriteriaQuery sumQuery = cb.createQuery(BigDecimal.class); - // Root orderLine = sumQuery.from(OrderLine.class); - // sumQuery.select(cb.sum(orderLine.get("olAmount"))); - // sumQuery.where(cb.equal(orderLine.get("order"), order)); - // BigDecimal sumOrderLineAmount = session.createQuery(sumQuery).getSingleResult(); - - // Update the customer's balance and delivery count - customer.setBalance(customer.getBalance().add(sumOrderLineAmount)); - customer.setDeliveryCnt(customer.getDeliveryCnt() + 1); + // // Calculate the sum of order line amounts + // BigDecimal sumOrderLineAmount = + // order.getOrderLines().stream() + // .map(OrderLine::getOlAmount) + // .reduce(BigDecimal.ZERO, BigDecimal::add); + // + // // Update the delivery date in the order lines + // Timestamp t = new Timestamp(System.currentTimeMillis()); + // order.getOrderLines().forEach(orderLine -> orderLine.setOlDeliveryD(t)); + + // //Update the customer's balance and delivery count + // Customer customer = order.getCustomer(); + // customer.setBalance(customer.getBalance().add(sumOrderLineAmount)); + // customer.setDeliveryCnt(customer.getDeliveryCnt() + 1); + + // Update the delivery date in the order lines using Criteria API + CriteriaUpdate updateQuery = cb.createCriteriaUpdate(OrderLine.class); + Root updateRoot = updateQuery.from(OrderLine.class); + updateQuery.set(updateRoot.get("olDeliveryD"), cb.currentTimestamp()); + updateQuery.where(cb.equal(updateRoot.get("order"), order)); + session.createMutationQuery(updateQuery).executeUpdate(); + + CriteriaQuery sumQuery = cb.createQuery(BigDecimal.class); + Root orderLine = sumQuery.from(OrderLine.class); + sumQuery.select(cb.sum(orderLine.get("olAmount"))); + sumQuery.where(cb.equal(orderLine.get("order"), order)); + BigDecimal sumOrderLineAmount = session.createQuery(sumQuery).getSingleResult(); + + CriteriaUpdate customerUpdate = cb.createCriteriaUpdate(Customer.class); + Root customerRoot = customerUpdate.from(Customer.class); + customerUpdate.set( + customerRoot.get("balance"), + cb.sum(customerRoot.get("balance"), sumOrderLineAmount) + ); + customerUpdate.set( + customerRoot.get("deliveryCnt"), + cb.sum(customerRoot.get("deliveryCnt"), 1) + ); + CustomerId customerId = + new CustomerId( + order.getId().getcId(), order.getId().getdId(), order.getId().getwId()); + customerUpdate.where(cb.equal(customerRoot.get("id"), customerId)); + session.createMutationQuery(customerUpdate).executeUpdate(); + } } session.flush(); From c4054cec69422f3f7ccc6685e82325b8e3b7523a Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Mon, 25 Nov 2024 20:53:35 +0530 Subject: [PATCH 14/22] performant version for delivery() transaction --- benchmarks/tpcc/src/main/resources/hibernate.cfg.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml b/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml index aa2909313..08606dffc 100644 --- a/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml +++ b/benchmarks/tpcc/src/main/resources/hibernate.cfg.xml @@ -26,7 +26,7 @@ 64 20 - true + From adeb388cf4bed651e1d160e39750645ba92435f2 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Tue, 26 Nov 2024 11:04:24 +0530 Subject: [PATCH 15/22] performant version for delivery() transaction --- .../tpcc/HibernateBenchmarkRunner.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index a2ddf94e9..1ddce56a3 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -425,7 +425,10 @@ public void delivery() throws SQLException { long carrierId = Long.reverse(random.nextInt(10)); Session session = sessionFactory.openSession(); if (hibernateConfiguration.isAutoBatchDml()) { - session.doWork(connection -> connection.createStatement().execute("set auto_batch_dml=true")); + session.doWork(connection -> { + connection.createStatement().execute("set auto_batch_dml=true"); + connection.createStatement().execute("set auto_batch_dml_update_count_verification=false"); + }); } Transaction tx = session.beginTransaction(); try { @@ -460,13 +463,13 @@ public void delivery() throws SQLException { // // Update the delivery date in the order lines // Timestamp t = new Timestamp(System.currentTimeMillis()); // order.getOrderLines().forEach(orderLine -> orderLine.setOlDeliveryD(t)); - + // // //Update the customer's balance and delivery count // Customer customer = order.getCustomer(); // customer.setBalance(customer.getBalance().add(sumOrderLineAmount)); // customer.setDeliveryCnt(customer.getDeliveryCnt() + 1); - // Update the delivery date in the order lines using Criteria API + //Update the delivery date in the order lines using Criteria API CriteriaUpdate updateQuery = cb.createCriteriaUpdate(OrderLine.class); Root updateRoot = updateQuery.from(OrderLine.class); updateQuery.set(updateRoot.get("olDeliveryD"), cb.currentTimestamp()); @@ -499,11 +502,15 @@ public void delivery() throws SQLException { } session.flush(); session.doWork( - connection -> connection.createStatement().execute("set auto_batch_dml=false")); + connection -> {connection.createStatement().execute("set auto_batch_dml=false"); + connection.createStatement().execute("set auto_batch_dml_update_count_verification=true"); + }); tx.commit(); } catch (Exception e) { session.doWork( - connection -> connection.createStatement().execute("set auto_batch_dml=false")); + connection -> {connection.createStatement().execute("set auto_batch_dml=false"); + connection.createStatement().execute("set auto_batch_dml_update_count_verification=true"); + }); if (tx != null) { tx.rollback(); } From ae41ffc2aaacb9422f0eb2e282c456087615e101 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Tue, 26 Nov 2024 16:02:45 +0530 Subject: [PATCH 16/22] dynamic update --- .../tpcc/AbstractBenchmarkRunner.java | 8 +- .../pgadapter/tpcc/BenchmarkApplication.java | 2 +- .../tpcc/HibernateBenchmarkRunner.java | 82 +++++++++---------- .../pgadapter/tpcc/entities/Customer.java | 2 + .../pgadapter/tpcc/entities/District.java | 2 + .../cloud/pgadapter/tpcc/entities/Order.java | 2 + .../pgadapter/tpcc/entities/OrderLine.java | 2 + .../cloud/pgadapter/tpcc/entities/Stock.java | 2 + .../pgadapter/tpcc/entities/Warehouse.java | 2 + .../src/main/resources/application.properties | 10 +-- 10 files changed, 63 insertions(+), 51 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java index a521c084d..06e380907 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java @@ -96,17 +96,17 @@ private void runTransactions() throws SQLException, InterruptedException { int transaction = random.nextInt(23); Stopwatch stopwatch = Stopwatch.createStarted(); if (transaction < 10) { - newOrder(); + delivery(); Duration executionDuration = stopwatch.elapsed(); metrics.recordNewOrderLatency(executionDuration.toMillis()); statistics.incNewOrder(); } else if (transaction < 20) { - payment(); + delivery(); Duration executionDuration = stopwatch.elapsed(); metrics.recordPaymentLatency(executionDuration.toMillis()); statistics.incPayment(); } else if (transaction < 21) { - orderStatus(); + delivery(); Duration executionDuration = stopwatch.elapsed(); metrics.recordOrderStatusLatency(executionDuration.toMillis()); statistics.incOrderStatus(); @@ -116,7 +116,7 @@ private void runTransactions() throws SQLException, InterruptedException { metrics.recordDeliveryLatency(executionDuration.toMillis()); statistics.incDelivery(); } else if (transaction < 23) { - stockLevel(); + delivery(); Duration executionDuration = stopwatch.elapsed(); metrics.recordStockLevelLatency(executionDuration.toMillis()); statistics.incStockLevel(); diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java index 75e2d27f5..40c52bbc3 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java @@ -223,7 +223,7 @@ public void run(String... args) throws Exception { while (watch.elapsed().compareTo(tpccConfiguration.getBenchmarkDuration()) <= 0) { //noinspection BusyWait Thread.sleep(1_000L); - statistics.print(watch.elapsed()); + //statistics.print(watch.elapsed()); } executor.shutdownNow(); if (!executor.awaitTermination(60L, TimeUnit.SECONDS)) { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index 1ddce56a3..36cafc772 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -454,49 +454,49 @@ public void delivery() throws SQLException { // Update the corresponding order with the carrier ID order.setCarrierId(carrierId); - // // Calculate the sum of order line amounts - // BigDecimal sumOrderLineAmount = - // order.getOrderLines().stream() - // .map(OrderLine::getOlAmount) - // .reduce(BigDecimal.ZERO, BigDecimal::add); + // Calculate the sum of order line amounts + BigDecimal sumOrderLineAmount = + order.getOrderLines().stream() + .map(OrderLine::getOlAmount) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + // Update the delivery date in the order lines + Timestamp t = new Timestamp(System.currentTimeMillis()); + order.getOrderLines().forEach(orderLine -> orderLine.setOlDeliveryD(t)); + + //Update the customer's balance and delivery count + Customer customer = order.getCustomer(); + customer.setBalance(customer.getBalance().add(sumOrderLineAmount)); + customer.setDeliveryCnt(customer.getDeliveryCnt() + 1); + + // //Update the delivery date in the order lines using Criteria API + // CriteriaUpdate updateQuery = cb.createCriteriaUpdate(OrderLine.class); + // Root updateRoot = updateQuery.from(OrderLine.class); + // updateQuery.set(updateRoot.get("olDeliveryD"), cb.currentTimestamp()); + // updateQuery.where(cb.equal(updateRoot.get("order"), order)); + // session.createMutationQuery(updateQuery).executeUpdate(); // - // // Update the delivery date in the order lines - // Timestamp t = new Timestamp(System.currentTimeMillis()); - // order.getOrderLines().forEach(orderLine -> orderLine.setOlDeliveryD(t)); + // CriteriaQuery sumQuery = cb.createQuery(BigDecimal.class); + // Root orderLine = sumQuery.from(OrderLine.class); + // sumQuery.select(cb.sum(orderLine.get("olAmount"))); + // sumQuery.where(cb.equal(orderLine.get("order"), order)); + // BigDecimal sumOrderLineAmount = session.createQuery(sumQuery).getSingleResult(); // - // //Update the customer's balance and delivery count - // Customer customer = order.getCustomer(); - // customer.setBalance(customer.getBalance().add(sumOrderLineAmount)); - // customer.setDeliveryCnt(customer.getDeliveryCnt() + 1); - - //Update the delivery date in the order lines using Criteria API - CriteriaUpdate updateQuery = cb.createCriteriaUpdate(OrderLine.class); - Root updateRoot = updateQuery.from(OrderLine.class); - updateQuery.set(updateRoot.get("olDeliveryD"), cb.currentTimestamp()); - updateQuery.where(cb.equal(updateRoot.get("order"), order)); - session.createMutationQuery(updateQuery).executeUpdate(); - - CriteriaQuery sumQuery = cb.createQuery(BigDecimal.class); - Root orderLine = sumQuery.from(OrderLine.class); - sumQuery.select(cb.sum(orderLine.get("olAmount"))); - sumQuery.where(cb.equal(orderLine.get("order"), order)); - BigDecimal sumOrderLineAmount = session.createQuery(sumQuery).getSingleResult(); - - CriteriaUpdate customerUpdate = cb.createCriteriaUpdate(Customer.class); - Root customerRoot = customerUpdate.from(Customer.class); - customerUpdate.set( - customerRoot.get("balance"), - cb.sum(customerRoot.get("balance"), sumOrderLineAmount) - ); - customerUpdate.set( - customerRoot.get("deliveryCnt"), - cb.sum(customerRoot.get("deliveryCnt"), 1) - ); - CustomerId customerId = - new CustomerId( - order.getId().getcId(), order.getId().getdId(), order.getId().getwId()); - customerUpdate.where(cb.equal(customerRoot.get("id"), customerId)); - session.createMutationQuery(customerUpdate).executeUpdate(); + // CriteriaUpdate customerUpdate = cb.createCriteriaUpdate(Customer.class); + // Root customerRoot = customerUpdate.from(Customer.class); + // customerUpdate.set( + // customerRoot.get("balance"), + // cb.sum(customerRoot.get("balance"), sumOrderLineAmount) + // ); + // customerUpdate.set( + // customerRoot.get("deliveryCnt"), + // cb.sum(customerRoot.get("deliveryCnt"), 1) + // ); + // CustomerId customerId = + // new CustomerId( + // order.getId().getcId(), order.getId().getdId(), order.getId().getwId()); + // customerUpdate.where(cb.equal(customerRoot.get("id"), customerId)); + // session.createMutationQuery(customerUpdate).executeUpdate(); } } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java index f1f5c14f3..1e2cd77a1 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java @@ -25,8 +25,10 @@ import jakarta.persistence.Table; import java.math.BigDecimal; import java.sql.Timestamp; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "customer") public class Customer { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java index a1088d5d4..a05950feb 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java @@ -21,8 +21,10 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import java.math.BigDecimal; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "district") public class District { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java index 9b7ee301d..8e6ca38df 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java @@ -25,8 +25,10 @@ import jakarta.persistence.Table; import java.sql.Timestamp; import java.util.List; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "orders") public class Order { @EmbeddedId private OrderId id; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java index bafdacc77..38ff7d723 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java @@ -16,8 +16,10 @@ import jakarta.persistence.*; import java.math.BigDecimal; import java.sql.Timestamp; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "order_line") public class OrderLine { @EmbeddedId private OrderLineId id; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java index a0e3fde36..3612544ff 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java @@ -15,8 +15,10 @@ import jakarta.persistence.*; import java.math.BigDecimal; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "stock") public class Stock { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java index 1531b4d86..afec1d873 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java @@ -18,8 +18,10 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import java.math.BigDecimal; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "warehouse") public class Warehouse { diff --git a/benchmarks/tpcc/src/main/resources/application.properties b/benchmarks/tpcc/src/main/resources/application.properties index f2f1fbeaa..333eca98f 100644 --- a/benchmarks/tpcc/src/main/resources/application.properties +++ b/benchmarks/tpcc/src/main/resources/application.properties @@ -15,12 +15,12 @@ tpcc.load-data-threads=32 tpcc.truncate-before-load=false # Choose to use the runner (pgadapter/spanner_jdbc/client_lib_pg) -tpcc.benchmark-runner=pgadapter +tpcc.benchmark-runner=hibernate # Run the benchmark. tpcc.run-benchmark=true # The number of parallel threads running transactions. -tpcc.benchmark-threads=8 +tpcc.benchmark-threads=1 # The duration that the benchmark should run in ISO-8601 notation. # E.g. PT60S (60 seconds), PT10M (10 minutes), PT2H (2 hours), P1D (1 day) tpcc.benchmark-duration=PT300S @@ -55,7 +55,7 @@ spanner.database=my-database # Set this to true to instruct the benchmark runner to start a PGAdapter instance in-process with # the benchmark application. -pgadapter.in-process=true +pgadapter.in-process=false pgadapter.num-channels=32 # Set this if you want the in-process PGAdapter instance to use a specific service account @@ -86,10 +86,10 @@ pgadapter.port=5432 pgadapter.connection-url=jdbc:postgresql://${pgadapter.host}:${pgadapter.port}/projects%2F${spanner.project}%2Finstances%2F${spanner.instance}%2Fdatabases%2F${spanner.database} # hibernate settings -hibernate.show-sql=false +hibernate.show-sql=true hibernate.batch-size=1 hibernate.pool-size=64 hibernate.order-inserts=false hibernate.order-updates=false hibernate.batch-versioned-data=false -hibernate.auto-batch-dml=false +hibernate.auto-batch-dml=true From 723185ed544b1046b2e0f5c222e53164b736de24 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Tue, 26 Nov 2024 16:21:05 +0530 Subject: [PATCH 17/22] dynamic update --- .../cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java | 8 ++++---- .../google/cloud/pgadapter/tpcc/BenchmarkApplication.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java index 06e380907..a521c084d 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java @@ -96,17 +96,17 @@ private void runTransactions() throws SQLException, InterruptedException { int transaction = random.nextInt(23); Stopwatch stopwatch = Stopwatch.createStarted(); if (transaction < 10) { - delivery(); + newOrder(); Duration executionDuration = stopwatch.elapsed(); metrics.recordNewOrderLatency(executionDuration.toMillis()); statistics.incNewOrder(); } else if (transaction < 20) { - delivery(); + payment(); Duration executionDuration = stopwatch.elapsed(); metrics.recordPaymentLatency(executionDuration.toMillis()); statistics.incPayment(); } else if (transaction < 21) { - delivery(); + orderStatus(); Duration executionDuration = stopwatch.elapsed(); metrics.recordOrderStatusLatency(executionDuration.toMillis()); statistics.incOrderStatus(); @@ -116,7 +116,7 @@ private void runTransactions() throws SQLException, InterruptedException { metrics.recordDeliveryLatency(executionDuration.toMillis()); statistics.incDelivery(); } else if (transaction < 23) { - delivery(); + stockLevel(); Duration executionDuration = stopwatch.elapsed(); metrics.recordStockLevelLatency(executionDuration.toMillis()); statistics.incStockLevel(); diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java index 40c52bbc3..75e2d27f5 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java @@ -223,7 +223,7 @@ public void run(String... args) throws Exception { while (watch.elapsed().compareTo(tpccConfiguration.getBenchmarkDuration()) <= 0) { //noinspection BusyWait Thread.sleep(1_000L); - //statistics.print(watch.elapsed()); + statistics.print(watch.elapsed()); } executor.shutdownNow(); if (!executor.awaitTermination(60L, TimeUnit.SECONDS)) { From 5cebed97d48018e579b82044e992578347dbc6fa Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Tue, 26 Nov 2024 17:30:50 +0530 Subject: [PATCH 18/22] dynamic update remove --- .../java/com/google/cloud/pgadapter/tpcc/entities/Customer.java | 1 - .../java/com/google/cloud/pgadapter/tpcc/entities/District.java | 1 - .../java/com/google/cloud/pgadapter/tpcc/entities/Order.java | 1 - .../java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java | 1 - .../java/com/google/cloud/pgadapter/tpcc/entities/Stock.java | 1 - .../java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java | 1 - 6 files changed, 6 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java index 1e2cd77a1..4c7180ea7 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java @@ -28,7 +28,6 @@ import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "customer") public class Customer { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java index a05950feb..e3c08645d 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java @@ -24,7 +24,6 @@ import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "district") public class District { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java index 8e6ca38df..b96dd0021 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java @@ -28,7 +28,6 @@ import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "orders") public class Order { @EmbeddedId private OrderId id; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java index 38ff7d723..d66a01ea9 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java @@ -19,7 +19,6 @@ import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "order_line") public class OrderLine { @EmbeddedId private OrderLineId id; diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java index 3612544ff..a84efb0db 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java @@ -18,7 +18,6 @@ import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "stock") public class Stock { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java index afec1d873..bffcb8ffb 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java @@ -21,7 +21,6 @@ import org.hibernate.annotations.DynamicUpdate; @Entity -@DynamicUpdate @Table(name = "warehouse") public class Warehouse { From 908a0bcf0843f7a93dc8e27182c8fc39e85be51f Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Tue, 26 Nov 2024 20:35:04 +0530 Subject: [PATCH 19/22] helper for session creation --- .../pgadapter/tpcc/BenchmarkApplication.java | 2 +- .../tpcc/HibernateBenchmarkRunner.java | 49 ++++------------- .../cloud/pgadapter/tpcc/SessionHelper.java | 52 +++++++++++++++++++ .../tpcc/config/HibernateConfiguration.java | 10 ++++ .../pgadapter/tpcc/entities/Customer.java | 1 - .../pgadapter/tpcc/entities/District.java | 1 - .../cloud/pgadapter/tpcc/entities/Order.java | 1 - .../pgadapter/tpcc/entities/OrderLine.java | 1 - .../cloud/pgadapter/tpcc/entities/Stock.java | 1 - .../pgadapter/tpcc/entities/Warehouse.java | 1 - .../src/main/resources/application.properties | 5 +- 11 files changed, 75 insertions(+), 49 deletions(-) create mode 100644 benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java index 75e2d27f5..b00989162 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/BenchmarkApplication.java @@ -209,7 +209,7 @@ public void run(String... args) throws Exception { executor.submit( new HibernateBenchmarkRunner( statistics, - sessionFactory, + new SessionHelper(sessionFactory), tpccConfiguration, pgAdapterConfiguration, spannerConfiguration, diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index 36cafc772..e02173be0 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -34,7 +34,6 @@ import com.google.cloud.spanner.Dialect; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; -import jakarta.persistence.criteria.CriteriaUpdate; import jakarta.persistence.criteria.Root; import java.io.IOException; import java.math.BigDecimal; @@ -45,19 +44,18 @@ import java.util.List; import java.util.Random; import org.hibernate.Session; -import org.hibernate.SessionFactory; import org.hibernate.Transaction; public class HibernateBenchmarkRunner extends AbstractBenchmarkRunner { - private final SessionFactory sessionFactory; + private final SessionHelper sessionHelper; private final HibernateConfiguration hibernateConfiguration; private final Random random = new Random(); HibernateBenchmarkRunner( Statistics statistics, - SessionFactory sessionFactory, + SessionHelper sessionHelper, TpccConfiguration tpccConfiguration, PGAdapterConfiguration pgAdapterConfiguration, SpannerConfiguration spannerConfiguration, @@ -72,7 +70,7 @@ public class HibernateBenchmarkRunner extends AbstractBenchmarkRunner { metrics, dialect); this.hibernateConfiguration = hibernateConfiguration; - this.sessionFactory = sessionFactory; + this.sessionHelper = sessionHelper; } @Override @@ -108,10 +106,7 @@ public void newOrder() throws SQLException { } quantities[line] = random.nextInt(1, 10); } - Session session = sessionFactory.openSession(); - if (hibernateConfiguration.isAutoBatchDml()) { - session.doWork(connection -> connection.createStatement().execute("set auto_batch_dml=true")); - } + Session session = sessionHelper.createSession(false, hibernateConfiguration.isAutoBatchDml()); Transaction tx = session.beginTransaction(); try { Customer customer = @@ -201,12 +196,8 @@ public void newOrder() throws SQLException { session.persist(order); session.persist(newOrder); session.flush(); - session.doWork( - connection -> connection.createStatement().execute("set auto_batch_dml=false")); tx.commit(); } catch (Exception e) { - session.doWork( - connection -> connection.createStatement().execute("set auto_batch_dml=false")); if (tx != null) { tx.rollback(); } @@ -241,10 +232,7 @@ public void payment() throws SQLException { customerDistrictId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); } - Session session = sessionFactory.openSession(); - if (hibernateConfiguration.isAutoBatchDml()) { - session.doWork(connection -> connection.createStatement().execute("set auto_batch_dml=true")); - } + Session session = sessionHelper.createSession(false, hibernateConfiguration.isAutoBatchDml()); Transaction tx = session.beginTransaction(); try { if (byName) { @@ -326,12 +314,8 @@ public void payment() throws SQLException { history.setData(String.format("%10s %10s", warehouse.getwName(), district.getdName())); session.persist(history); session.flush(); - session.doWork( - connection -> connection.createStatement().execute("set auto_batch_dml=false")); tx.commit(); } catch (Exception e) { - session.doWork( - connection -> connection.createStatement().execute("set auto_batch_dml=false")); if (tx != null) { tx.rollback(); } @@ -350,7 +334,7 @@ public void orderStatus() throws SQLException { String lastName = LastNameGenerator.generateLastName(this.random, Long.MAX_VALUE); boolean byName = random.nextInt(100) < 60; - Session session = sessionFactory.openSession(); + Session session = sessionHelper.createSession(hibernateConfiguration.isReadOnly(), false); Transaction tx = session.beginTransaction(); try { Customer customer = null; @@ -423,13 +407,7 @@ public void orderStatus() throws SQLException { public void delivery() throws SQLException { long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); long carrierId = Long.reverse(random.nextInt(10)); - Session session = sessionFactory.openSession(); - if (hibernateConfiguration.isAutoBatchDml()) { - session.doWork(connection -> { - connection.createStatement().execute("set auto_batch_dml=true"); - connection.createStatement().execute("set auto_batch_dml_update_count_verification=false"); - }); - } + Session session = sessionHelper.createSession(false, hibernateConfiguration.isAutoBatchDml()); Transaction tx = session.beginTransaction(); try { for (long district = 0L; @@ -464,7 +442,7 @@ public void delivery() throws SQLException { Timestamp t = new Timestamp(System.currentTimeMillis()); order.getOrderLines().forEach(orderLine -> orderLine.setOlDeliveryD(t)); - //Update the customer's balance and delivery count + // Update the customer's balance and delivery count Customer customer = order.getCustomer(); customer.setBalance(customer.getBalance().add(sumOrderLineAmount)); customer.setDeliveryCnt(customer.getDeliveryCnt() + 1); @@ -501,16 +479,8 @@ public void delivery() throws SQLException { } } session.flush(); - session.doWork( - connection -> {connection.createStatement().execute("set auto_batch_dml=false"); - connection.createStatement().execute("set auto_batch_dml_update_count_verification=true"); - }); tx.commit(); } catch (Exception e) { - session.doWork( - connection -> {connection.createStatement().execute("set auto_batch_dml=false"); - connection.createStatement().execute("set auto_batch_dml_update_count_verification=true"); - }); if (tx != null) { tx.rollback(); } @@ -525,7 +495,7 @@ public void stockLevel() throws SQLException { long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); int level = random.nextInt(10, 21); - Session session = sessionFactory.openSession(); + Session session = sessionHelper.createSession(hibernateConfiguration.isReadOnly(), false); Transaction tx = session.beginTransaction(); try { CriteriaBuilder cb = session.getCriteriaBuilder(); @@ -568,7 +538,6 @@ public void stockLevel() throws SQLException { cb.lt(stockRoot.get("quantity"), level)); long stockCount = session.createQuery(stockQuery).getSingleResult(); } - tx.commit(); } catch (Exception e) { if (tx != null) { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java new file mode 100644 index 000000000..4dc3aaf2b --- /dev/null +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java @@ -0,0 +1,52 @@ +package com.google.cloud.pgadapter.tpcc; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; + +public class SessionHelper { + + private final SessionFactory sessionFactory; + + public SessionHelper(SessionFactory sessionFactory) { + this.sessionFactory = sessionFactory; + } + + public Session createSession(boolean readOnly, boolean autoBatchDml) { + if (readOnly) return createReadOnlySession(); + if (autoBatchDml) return createAutoBatchDmlSession(); + return createReadWriteSession(); + } + + public Session createReadWriteSession() { + Session session = sessionFactory.openSession(); + session.doWork( + conn -> { + conn.setReadOnly(false); + conn.createStatement().execute("set auto_batch_dml=false"); + conn.createStatement().execute("set auto_batch_dml_update_count_verification=true"); + }); + return session; + } + + public Session createAutoBatchDmlSession() { + Session session = sessionFactory.openSession(); + session.doWork( + conn -> { + conn.setReadOnly(false); + conn.createStatement().execute("set auto_batch_dml=true"); + conn.createStatement().execute("set auto_batch_dml_update_count_verification=false"); + }); + return session; + } + + public Session createReadOnlySession() { + Session session = sessionFactory.openSession(); + session.doWork( + conn -> { + conn.setReadOnly(true); + conn.createStatement().execute("set auto_batch_dml=false"); + conn.createStatement().execute("set auto_batch_dml_update_count_verification=false"); + }); + return session; + } +} diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java index 03340a5ba..d402ff2f0 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/config/HibernateConfiguration.java @@ -34,6 +34,16 @@ public class HibernateConfiguration { private boolean autoBatchDml; + private boolean readOnly; + + public boolean isReadOnly() { + return readOnly; + } + + public void setReadOnly(boolean readOnly) { + this.readOnly = readOnly; + } + public boolean isAutoBatchDml() { return autoBatchDml; } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java index 4c7180ea7..f1f5c14f3 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java @@ -25,7 +25,6 @@ import jakarta.persistence.Table; import java.math.BigDecimal; import java.sql.Timestamp; -import org.hibernate.annotations.DynamicUpdate; @Entity @Table(name = "customer") diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java index e3c08645d..a1088d5d4 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java @@ -21,7 +21,6 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import java.math.BigDecimal; -import org.hibernate.annotations.DynamicUpdate; @Entity @Table(name = "district") diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java index b96dd0021..9b7ee301d 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Order.java @@ -25,7 +25,6 @@ import jakarta.persistence.Table; import java.sql.Timestamp; import java.util.List; -import org.hibernate.annotations.DynamicUpdate; @Entity @Table(name = "orders") diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java index d66a01ea9..bafdacc77 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/OrderLine.java @@ -16,7 +16,6 @@ import jakarta.persistence.*; import java.math.BigDecimal; import java.sql.Timestamp; -import org.hibernate.annotations.DynamicUpdate; @Entity @Table(name = "order_line") diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java index a84efb0db..a0e3fde36 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Stock.java @@ -15,7 +15,6 @@ import jakarta.persistence.*; import java.math.BigDecimal; -import org.hibernate.annotations.DynamicUpdate; @Entity @Table(name = "stock") diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java index bffcb8ffb..1531b4d86 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java @@ -18,7 +18,6 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import java.math.BigDecimal; -import org.hibernate.annotations.DynamicUpdate; @Entity @Table(name = "warehouse") diff --git a/benchmarks/tpcc/src/main/resources/application.properties b/benchmarks/tpcc/src/main/resources/application.properties index 333eca98f..d94300936 100644 --- a/benchmarks/tpcc/src/main/resources/application.properties +++ b/benchmarks/tpcc/src/main/resources/application.properties @@ -86,10 +86,11 @@ pgadapter.port=5432 pgadapter.connection-url=jdbc:postgresql://${pgadapter.host}:${pgadapter.port}/projects%2F${spanner.project}%2Finstances%2F${spanner.instance}%2Fdatabases%2F${spanner.database} # hibernate settings -hibernate.show-sql=true +hibernate.read-only=false +hibernate.show-sql=false hibernate.batch-size=1 hibernate.pool-size=64 hibernate.order-inserts=false hibernate.order-updates=false hibernate.batch-versioned-data=false -hibernate.auto-batch-dml=true +hibernate.auto-batch-dml=false From 671ab3322686dfb1421ec1784be7adb0185f3ccf Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Tue, 26 Nov 2024 21:27:17 +0530 Subject: [PATCH 20/22] transaction tags --- .../tpcc/HibernateBenchmarkRunner.java | 15 ++++++++++----- .../cloud/pgadapter/tpcc/SessionHelper.java | 17 ++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java index e02173be0..18f0df25e 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/HibernateBenchmarkRunner.java @@ -106,7 +106,8 @@ public void newOrder() throws SQLException { } quantities[line] = random.nextInt(1, 10); } - Session session = sessionHelper.createSession(false, hibernateConfiguration.isAutoBatchDml()); + Session session = + sessionHelper.createSession(false, hibernateConfiguration.isAutoBatchDml(), "new_order"); Transaction tx = session.beginTransaction(); try { Customer customer = @@ -232,7 +233,8 @@ public void payment() throws SQLException { customerDistrictId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); } - Session session = sessionHelper.createSession(false, hibernateConfiguration.isAutoBatchDml()); + Session session = + sessionHelper.createSession(false, hibernateConfiguration.isAutoBatchDml(), "payment"); Transaction tx = session.beginTransaction(); try { if (byName) { @@ -334,7 +336,8 @@ public void orderStatus() throws SQLException { String lastName = LastNameGenerator.generateLastName(this.random, Long.MAX_VALUE); boolean byName = random.nextInt(100) < 60; - Session session = sessionHelper.createSession(hibernateConfiguration.isReadOnly(), false); + Session session = + sessionHelper.createSession(hibernateConfiguration.isReadOnly(), false, "order_status"); Transaction tx = session.beginTransaction(); try { Customer customer = null; @@ -407,7 +410,8 @@ public void orderStatus() throws SQLException { public void delivery() throws SQLException { long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses())); long carrierId = Long.reverse(random.nextInt(10)); - Session session = sessionHelper.createSession(false, hibernateConfiguration.isAutoBatchDml()); + Session session = + sessionHelper.createSession(false, hibernateConfiguration.isAutoBatchDml(), "delivery"); Transaction tx = session.beginTransaction(); try { for (long district = 0L; @@ -495,7 +499,8 @@ public void stockLevel() throws SQLException { long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse())); int level = random.nextInt(10, 21); - Session session = sessionHelper.createSession(hibernateConfiguration.isReadOnly(), false); + Session session = + sessionHelper.createSession(hibernateConfiguration.isReadOnly(), false, "stock_level"); Transaction tx = session.beginTransaction(); try { CriteriaBuilder cb = session.getCriteriaBuilder(); diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java index 4dc3aaf2b..680b2f8be 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java @@ -11,41 +11,44 @@ public SessionHelper(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } - public Session createSession(boolean readOnly, boolean autoBatchDml) { - if (readOnly) return createReadOnlySession(); - if (autoBatchDml) return createAutoBatchDmlSession(); - return createReadWriteSession(); + public Session createSession(boolean readOnly, boolean autoBatchDml, String transactionTag) { + if (readOnly) return createReadOnlySession(transactionTag); + if (autoBatchDml) return createAutoBatchDmlSession(transactionTag); + return createReadWriteSession(transactionTag); } - public Session createReadWriteSession() { + public Session createReadWriteSession(String transactionTag) { Session session = sessionFactory.openSession(); session.doWork( conn -> { conn.setReadOnly(false); conn.createStatement().execute("set auto_batch_dml=false"); conn.createStatement().execute("set auto_batch_dml_update_count_verification=true"); + conn.createStatement().execute("set transaction_tag='" + transactionTag + "'"); }); return session; } - public Session createAutoBatchDmlSession() { + public Session createAutoBatchDmlSession(String transactionTag) { Session session = sessionFactory.openSession(); session.doWork( conn -> { conn.setReadOnly(false); conn.createStatement().execute("set auto_batch_dml=true"); conn.createStatement().execute("set auto_batch_dml_update_count_verification=false"); + conn.createStatement().execute("set transaction_tag='" + transactionTag + "'"); }); return session; } - public Session createReadOnlySession() { + public Session createReadOnlySession(String transactionTag) { Session session = sessionFactory.openSession(); session.doWork( conn -> { conn.setReadOnly(true); conn.createStatement().execute("set auto_batch_dml=false"); conn.createStatement().execute("set auto_batch_dml_update_count_verification=false"); + conn.createStatement().execute("set transaction_tag='" + transactionTag + "'"); }); return session; } From 4b5dab65b09c3045ab3adef8a6a77ac39d5cf491 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Wed, 27 Nov 2024 10:53:48 +0530 Subject: [PATCH 21/22] fix --- .../cloud/pgadapter/tpcc/SessionHelper.java | 16 +++++++--------- .../cloud/pgadapter/tpcc/entities/Customer.java | 1 + 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java index 680b2f8be..96fd28866 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/SessionHelper.java @@ -12,7 +12,7 @@ public SessionHelper(SessionFactory sessionFactory) { } public Session createSession(boolean readOnly, boolean autoBatchDml, String transactionTag) { - if (readOnly) return createReadOnlySession(transactionTag); + if (readOnly) return createReadOnlySession(); if (autoBatchDml) return createAutoBatchDmlSession(transactionTag); return createReadWriteSession(transactionTag); } @@ -21,10 +21,9 @@ public Session createReadWriteSession(String transactionTag) { Session session = sessionFactory.openSession(); session.doWork( conn -> { + conn.createStatement().execute("RESET ALL"); conn.setReadOnly(false); - conn.createStatement().execute("set auto_batch_dml=false"); - conn.createStatement().execute("set auto_batch_dml_update_count_verification=true"); - conn.createStatement().execute("set transaction_tag='" + transactionTag + "'"); + //conn.createStatement().execute("set transaction_tag='" + transactionTag + "'"); }); return session; } @@ -33,22 +32,21 @@ public Session createAutoBatchDmlSession(String transactionTag) { Session session = sessionFactory.openSession(); session.doWork( conn -> { + conn.createStatement().execute("RESET ALL"); conn.setReadOnly(false); conn.createStatement().execute("set auto_batch_dml=true"); conn.createStatement().execute("set auto_batch_dml_update_count_verification=false"); - conn.createStatement().execute("set transaction_tag='" + transactionTag + "'"); + //conn.createStatement().execute("set transaction_tag='" + transactionTag + "'"); }); return session; } - public Session createReadOnlySession(String transactionTag) { + public Session createReadOnlySession() { Session session = sessionFactory.openSession(); session.doWork( conn -> { + conn.createStatement().execute("RESET ALL"); conn.setReadOnly(true); - conn.createStatement().execute("set auto_batch_dml=false"); - conn.createStatement().execute("set auto_batch_dml_update_count_verification=false"); - conn.createStatement().execute("set transaction_tag='" + transactionTag + "'"); }); return session; } diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java index f1f5c14f3..4c7180ea7 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java @@ -25,6 +25,7 @@ import jakarta.persistence.Table; import java.math.BigDecimal; import java.sql.Timestamp; +import org.hibernate.annotations.DynamicUpdate; @Entity @Table(name = "customer") From 9658450166db66a55b4fe53473a110f2170cad53 Mon Sep 17 00:00:00 2001 From: Rayudu A L P Date: Wed, 27 Nov 2024 15:28:20 +0530 Subject: [PATCH 22/22] dynamic update --- .../java/com/google/cloud/pgadapter/tpcc/entities/Customer.java | 1 + .../java/com/google/cloud/pgadapter/tpcc/entities/District.java | 2 ++ .../com/google/cloud/pgadapter/tpcc/entities/Warehouse.java | 2 ++ 3 files changed, 5 insertions(+) diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java index 4c7180ea7..1e2cd77a1 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Customer.java @@ -28,6 +28,7 @@ import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "customer") public class Customer { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java index a1088d5d4..a05950feb 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/District.java @@ -21,8 +21,10 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import java.math.BigDecimal; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "district") public class District { diff --git a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java index 1531b4d86..afec1d873 100644 --- a/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java +++ b/benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/entities/Warehouse.java @@ -18,8 +18,10 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import java.math.BigDecimal; +import org.hibernate.annotations.DynamicUpdate; @Entity +@DynamicUpdate @Table(name = "warehouse") public class Warehouse {