Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hikari pool implementation in perfservice #109

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.4</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/oltpbenchmark/ThreadBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private int finalizeWorkers(ArrayList<Thread> workerThreads) throws InterruptedE

workers.get(i).tearDown();
}

this.workers.get(0).getBenchmark().closeDataSource();
return requests;
}

Expand Down
37 changes: 35 additions & 2 deletions src/main/java/com/oltpbenchmark/api/BenchmarkModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -73,15 +75,24 @@ public abstract class BenchmarkModule {
public BenchmarkModule(WorkloadConfiguration workConf) {
this.workConf = workConf;
this.dialects = new StatementDialects(workConf);
if (workConf.getXmlConfig().getBoolean("use_hikari_pool", false)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since worker inherits from benchmarkmodule are we creating one hikari pool per worker?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even though worker inherits from benchmarkmodule, the datasource is created in benchmarkmodule's constructor which is called only once. Hence, there is only one hikari pool.

try {
createDataSource();
} catch (Exception e) {
LOG.error("Failed to create Data Source", e);
throw e;
}
}
}

// --------------------------------------------------------------------------
// DATABASE CONNECTION
// --------------------------------------------------------------------------

public final Connection makeConnection() throws SQLException {

if (StringUtils.isEmpty(workConf.getUsername())) {
if (workConf.getXmlConfig().getBoolean("use_hikari_pool", false)) {
return hikariDataSource.getConnection();
} else if (StringUtils.isEmpty(workConf.getUsername())) {
return DriverManager.getConnection(workConf.getUrl());
} else {
return DriverManager.getConnection(
Expand All @@ -91,6 +102,28 @@ public final Connection makeConnection() throws SQLException {
}
}

protected HikariDataSource hikariDataSource;

public void createDataSource() {
HikariConfig config = new HikariConfig();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should shutdown the dataSource and its associated pool after the test is done. You can call datasource.close for the same.


Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented closeDataSource() method which will close it in ThreadBench finalizeWorkers()

config.setJdbcUrl(workConf.getUrl());
if (!StringUtils.isEmpty(workConf.getUsername())) {
config.setUsername(workConf.getUsername());
config.setPassword(workConf.getPassword());
/* take max_pool_size from xmlConfig, default is = number of terminals*/
config.setMaximumPoolSize(workConf.getXmlConfig().getInt("max_pool_size", workConf.getTerminals()));
config.setMinimumIdle(5);
}
config.setTransactionIsolation(workConf.getIsolationString());
hikariDataSource = new HikariDataSource(config);
}

public void closeDataSource() {
if (workConf.getXmlConfig().getBoolean("use_hikari_pool", false)) {
this.hikariDataSource.close();
}
}

// --------------------------------------------------------------------------
// IMPLEMENTING CLASS INTERFACE
// --------------------------------------------------------------------------
Expand Down
45 changes: 37 additions & 8 deletions src/main/java/com/oltpbenchmark/api/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,24 @@ public abstract class Worker<T extends BenchmarkModule> implements Runnable {
private boolean seenDone = false;
public final FeaturebenchAdditionalResults featurebenchAdditionalResults = new FeaturebenchAdditionalResults();

public boolean usingHikari = false;
public boolean autoCommitVal = false;

public Worker(T benchmark, int id) {
this.id = id;
this.benchmark = benchmark;
this.configuration = this.benchmark.getWorkloadConfiguration();
this.workloadState = this.configuration.getWorkloadState();
this.currStatement = null;
this.transactionTypes = this.configuration.getTransTypes();
boolean autoCommitVal = false;
this.autoCommitVal = false;
this.usingHikari = this.getWorkloadConfiguration().getXmlConfig().getBoolean("use_hikari_pool", false);
if (this.benchmark.getBenchmarkName().equalsIgnoreCase("featurebench") &&
this.benchmark.getWorkloadConfiguration().getXmlConfig().containsKey("microbenchmark/properties/setAutoCommit")) {
autoCommitVal = this.benchmark.getWorkloadConfiguration().getXmlConfig().getBoolean("microbenchmark/properties/setAutoCommit");
this.isFeaturebenchWorkload = true;
}
if (!this.configuration.getNewConnectionPerTxn()) {
if (!this.configuration.getNewConnectionPerTxn() && !usingHikari) {
try {
this.conn = this.benchmark.makeConnection();
this.conn.setAutoCommit(autoCommitVal);
Expand Down Expand Up @@ -198,7 +202,16 @@ public final void run() {

// Invoke initialize callback
try {
this.initialize();
if (!this.usingHikari)
this.initialize();
else {
if(this.conn == null || this.conn.isClosed()) {
this.conn = this.getBenchmark().makeConnection();
this.conn.setAutoCommit(this.autoCommitVal);
this.initialize();
this.conn.close();
}
}
} catch (Throwable ex) {
throw new RuntimeException("Unexpected error when initializing " + this, ex);
}
Expand Down Expand Up @@ -282,11 +295,27 @@ public final void run() {
}
}

long start = System.nanoTime();

doWork(configuration.getDatabaseType(), transactionType);

long end = System.nanoTime();
long start = 0;
long end = 0;
if (!this.usingHikari) {
start = System.nanoTime();
doWork(configuration.getDatabaseType(), transactionType);
end = System.nanoTime();
}
else {
try {
if (this.conn == null || this.conn.isClosed()) {
this.conn = this.getBenchmark().makeConnection();
this.conn.setAutoCommit(this.autoCommitVal);
start = System.nanoTime();
doWork(configuration.getDatabaseType(), transactionType);
end = System.nanoTime();
this.conn.close();
}
} catch(SQLException ex) {
throw new RuntimeException("Failed to connect to database", ex);
}
}

// PART 4: Record results

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ protected TransactionStatus executeWork(Connection conn, TransactionType txnType

@Override
public void tearDown() {
if (this.usingHikari) {
try {
if (this.conn == null || this.conn.isClosed()) {
this.conn = this.getBenchmark().makeConnection();
this.conn.setAutoCommit(true);
}
} catch (SQLException ex) {
throw new RuntimeException("Failed to connect to database", ex);
}
}
synchronized (FeatureBenchWorker.class) {
if (!this.configuration.getNewConnectionPerTxn() && this.configuration.getWorkloadState().getGlobalState() == State.EXIT && !isPGStatStatementCollected.get()) {

Expand Down Expand Up @@ -349,6 +359,15 @@ public void tearDown() {
}
}
}
if(usingHikari) {
try {
if (this.conn != null && !this.conn.isClosed()) {
this.conn.close();
}
} catch (SQLException ex) {
throw new RuntimeException("Failed to connect to database", ex);
}
}
}

private JSONObject callPGStats() throws SQLException{
Expand Down