Skip to content

Commit

Permalink
Add config option to load external DDL file instead of default (cmu-d…
Browse files Browse the repository at this point in the history
  • Loading branch information
jkosh44 authored Jun 28, 2022
1 parent 16e0ad6 commit 7c64657
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/oltpbenchmark/DBWorkload.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public static void main(String[] args) throws Exception {
wrkld.setIsolationMode(xmlConfig.getString("isolation" + pluginTest, isolationMode));
wrkld.setScaleFactor(xmlConfig.getDouble("scalefactor", 1.0));
wrkld.setDataDir(xmlConfig.getString("datadir", "."));
wrkld.setDDLPath(xmlConfig.getString("ddlpath", null));

double selectivity = -1;
try {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/oltpbenchmark/WorkloadConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class WorkloadConfiguration {
private TransactionTypes transTypes = null;
private int isolationMode = Connection.TRANSACTION_SERIALIZABLE;
private String dataDir = null;
private String ddlPath = null;

public String getBenchmarkName() {
return benchmarkName;
Expand Down Expand Up @@ -210,6 +211,20 @@ public void setDataDir(String dir) {
this.dataDir = dir;
}

/**
* Return the path in which we can find the ddl script.
*/
public String getDDLPath() {
return this.ddlPath;
}

/**
* Set the path in which we can find the ddl script.
*/
public void setDDLPath(String ddlPath) {
this.ddlPath = ddlPath;
}

/**
* A utility method that init the phaseIterator and dialectMap
*/
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/oltpbenchmark/api/BenchmarkModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
Expand Down Expand Up @@ -221,15 +220,18 @@ public final void createDatabase() throws SQLException, IOException {
*/
public final void createDatabase(DatabaseType dbType, Connection conn) throws SQLException, IOException {

String ddlPath = this.getDatabaseDDLPath(dbType);
ScriptRunner runner = new ScriptRunner(conn, true, true);

if (LOG.isDebugEnabled()) {
if (workConf.getDDLPath() != null) {
String ddlPath = workConf.getDDLPath();
LOG.warn("Overriding default DDL script path");
LOG.debug("Executing script [{}] for database type [{}]", ddlPath, dbType);
runner.runExternalScript(ddlPath);
} else {
String ddlPath = this.getDatabaseDDLPath(dbType);
LOG.debug("Executing script [{}] for database type [{}]", ddlPath, dbType);
runner.runScript(ddlPath);
}

runner.runScript(ddlPath);

}


Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/oltpbenchmark/catalog/HSQLDBCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.sql.*;
import java.util.*;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -206,13 +208,21 @@ private void init() throws SQLException, IOException {
*/
Map<String, String> getOriginalTableNames() {
// Get the contents of the HSQLDB DDL for the current benchmark.
String ddlPath = this.benchmarkModule.getDatabaseDDLPath(DatabaseType.HSQLDB);
String ddlContents;
try {
ddlContents = IOUtils.toString(Objects.requireNonNull(this.getClass().getResource(ddlPath)), Charset.defaultCharset());
String ddlPath = this.benchmarkModule.getWorkloadConfiguration().getDDLPath();
URL ddlURL;
if (ddlPath == null) {
ddlPath = this.benchmarkModule.getDatabaseDDLPath(DatabaseType.HSQLDB);
ddlURL = Objects.requireNonNull(this.getClass().getResource(ddlPath));
} else {
ddlURL = Path.of(ddlPath).toUri().toURL();
}
ddlContents = IOUtils.toString(ddlURL, Charset.defaultCharset());
} catch (IOException e) {
throw new RuntimeException(e);
}

// Extract and map the original table names to their uppercase versions.
Map<String, String> originalTableNames = new HashMap<>();
Pattern p = Pattern.compile("CREATE[\\s]+TABLE[\\s]+(.*?)[\\s]+", Pattern.CASE_INSENSITIVE);
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/com/oltpbenchmark/util/ScriptRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,37 @@ public ScriptRunner(Connection connection, boolean autoCommit, boolean stopOnErr
}


public void runExternalScript(String path) throws IOException, SQLException {

LOG.debug("trying to find external file by path {}", path);

try (FileReader reader = new FileReader(path)) {

runScript(reader);
}
}

public void runScript(String path) throws IOException, SQLException {

LOG.debug("trying to find file by path {}", path);

try (InputStream in = this.getClass().getResourceAsStream(path);
Reader reader = new InputStreamReader(in)) {

boolean originalAutoCommit = connection.getAutoCommit();
runScript(reader);
}
}

try {
if (originalAutoCommit != this.autoCommit) {
connection.setAutoCommit(this.autoCommit);
}
runScript(connection, reader);
} finally {
connection.setAutoCommit(originalAutoCommit);
private void runScript(Reader reader) throws IOException, SQLException {
boolean originalAutoCommit = connection.getAutoCommit();

try {
if (originalAutoCommit != this.autoCommit) {
connection.setAutoCommit(this.autoCommit);
}
runScript(connection, reader);
} finally {
connection.setAutoCommit(originalAutoCommit);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/oltpbenchmark/api/AbstractTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,21 @@ public abstract class AbstractTestCase<T extends BenchmarkModule> extends TestCa

protected final boolean createDatabase;
protected final boolean loadDatabase;
protected final String ddlOverridePath;

private static final AtomicInteger portCounter = new AtomicInteger(9001);


public AbstractTestCase(boolean createDatabase, boolean loadDatabase) {
this.createDatabase = createDatabase;
this.loadDatabase = loadDatabase;
this.ddlOverridePath = null;
}

public AbstractTestCase(boolean createDatabase, boolean loadDatabase, String ddlOverridePath) {
this.createDatabase = createDatabase;
this.loadDatabase = loadDatabase;
this.ddlOverridePath = ddlOverridePath;
}

public abstract List<Class<? extends Procedure>> procedures();
Expand Down Expand Up @@ -112,6 +120,7 @@ protected final void setUp() throws Exception {
this.workConf.setTerminals(1);
this.workConf.setBatchSize(128);
this.workConf.setBenchmarkName(BenchmarkModule.convertBenchmarkClassToBenchmarkName(benchmarkClass()));
this.workConf.setDDLPath(this.ddlOverridePath);

customWorkloadConfiguration(this.workConf);

Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/oltpbenchmark/api/MockBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public MockBenchmark() {
this.workConf.setBenchmarkName("mockbenchmark");
}

public MockBenchmark(WorkloadConfiguration workConf) {
super(workConf);
}

@Override
protected Package getProcedurePackageImpl() {
return null;
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/com/oltpbenchmark/api/TestDDLOverride.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.oltpbenchmark.api;

import com.oltpbenchmark.catalog.Table;
import com.oltpbenchmark.util.SQLUtil;

import java.nio.file.Paths;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class TestDDLOverride extends AbstractTestCase<MockBenchmark> {

public TestDDLOverride() {
super(false, false, Paths.get("src", "test", "resources", "benchmarks", "mockbenchmark", "ddl-hsqldb.sql").toAbsolutePath().toString());
}

@Override
public List<Class<? extends Procedure>> procedures() {
return new ArrayList<>();
}

@Override
public Class<MockBenchmark> benchmarkClass() {
return MockBenchmark.class;
}

@Override
public List<String> ignorableTables() {
return null;
}

public void testCreateWithDdlOverride() throws Exception {
this.benchmark.createDatabase();

assertFalse("Failed to get table names for " + benchmark.getBenchmarkName().toUpperCase(), this.catalog.getTables().isEmpty());
for (Table table : this.catalog.getTables()) {
String tableName = table.getName();
Table catalog_tbl = this.catalog.getTable(tableName);

String sql = SQLUtil.getCountSQL(this.workConf.getDatabaseType(), catalog_tbl);

try (Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery(sql);) {

assertNotNull(result);

boolean adv = result.next();
assertTrue(sql, adv);

int count = result.getInt(1);
assertEquals(0, count);
}
}
}
}

0 comments on commit 7c64657

Please sign in to comment.