Skip to content

Commit ba2ac51

Browse files
ngbanguyenbpkroth
andauthored
After load functionality with test (#397)
Splitting after load functionality needed for Oracle config from PR #379. Add an after load option for benchmark configs. Benchmark configs can add an afterload tag specifying a path to an SQL file in resources. This file will be executed after loading database. Tested in `AbstractTestLoad.testLoadWithAfterLoad()` --------- Co-authored-by: Brian Kroth <[email protected]>
1 parent 1f57a5c commit ba2ac51

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

src/main/java/com/oltpbenchmark/DBWorkload.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ public static void main(String[] args) throws Exception {
214214
postExecutionWait = xmlConfig.getLong(key + "/postExecutionWait");
215215
}
216216

217+
// After load
218+
if (xmlConfig.containsKey("afterload")) {
219+
bench.setAfterLoadScriptPath(xmlConfig.getString("afterload"));
220+
}
221+
217222
TransactionType tmpType = bench.initTransactionType(txnName, txnId + txnIdOffset, preExecutionWait, postExecutionWait);
218223

219224
// Keep a reference for filtering
@@ -626,7 +631,7 @@ private static void runCreator(BenchmarkModule bench) throws SQLException, IOExc
626631
bench.createDatabase();
627632
}
628633

629-
private static void runLoader(BenchmarkModule bench) throws SQLException, InterruptedException {
634+
private static void runLoader(BenchmarkModule bench) throws IOException, SQLException, InterruptedException {
630635
LOG.debug(String.format("Loading %s Database", bench));
631636
bench.loadDatabase();
632637
}

src/main/java/com/oltpbenchmark/api/BenchmarkModule.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ public final Connection makeConnection() throws SQLException {
104104
}
105105
}
106106

107+
private String afterLoadScriptPath = null;
108+
109+
public final void setAfterLoadScriptPath(String scriptPath) {
110+
this.afterLoadScriptPath = scriptPath;
111+
}
112+
113+
public String getAfterLoadScriptPath() {
114+
return this.afterLoadScriptPath;
115+
}
116+
107117
// --------------------------------------------------------------------------
108118
// IMPLEMENTING CLASS INTERFACE
109119
// --------------------------------------------------------------------------
@@ -248,11 +258,19 @@ public final void createDatabase(DatabaseType dbType, Connection conn) throws SQ
248258
}
249259
}
250260

261+
public final void runScript(String scriptPath) throws SQLException, IOException {
262+
try (Connection conn = this.makeConnection()) {
263+
DatabaseType dbType = this.workConf.getDatabaseType();
264+
ScriptRunner runner = new ScriptRunner(conn, true, true);
265+
LOG.debug("Executing script [{}] for database type [{}]", scriptPath, dbType);
266+
runner.runScript(scriptPath);
267+
}
268+
}
251269

252270
/**
253271
* Invoke this benchmark's database loader
254272
*/
255-
public final Loader<? extends BenchmarkModule> loadDatabase() throws SQLException, InterruptedException {
273+
public final Loader<? extends BenchmarkModule> loadDatabase() throws IOException, SQLException, InterruptedException {
256274
Loader<? extends BenchmarkModule> loader;
257275

258276
loader = this.makeLoaderImpl();
@@ -275,6 +293,12 @@ public final Loader<? extends BenchmarkModule> loadDatabase() throws SQLExceptio
275293
}
276294
}
277295

296+
if (this.afterLoadScriptPath != null) {
297+
LOG.debug("Running script after load for {} benchmark...", this.workConf.getBenchmarkName().toUpperCase());
298+
runScript(this.afterLoadScriptPath);
299+
LOG.debug("Finished running script after load for {} benchmark...", this.workConf.getBenchmarkName().toUpperCase());
300+
}
301+
278302
return loader;
279303
}
280304

src/test/java/com/oltpbenchmark/api/AbstractTestLoader.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717

1818
package com.oltpbenchmark.api;
1919

20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertTrue;
23-
2420
import com.oltpbenchmark.catalog.Table;
2521
import com.oltpbenchmark.util.Histogram;
2622
import com.oltpbenchmark.util.SQLUtil;
2723
import org.junit.Test;
2824
import org.slf4j.Logger;
2925
import org.slf4j.LoggerFactory;
3026

27+
import java.sql.PreparedStatement;
3128
import java.sql.ResultSet;
3229
import java.sql.SQLException;
3330
import java.sql.Statement;
3431
import java.util.List;
3532

33+
import static org.junit.Assert.*;
34+
import static org.junit.Assert.fail;
35+
3636
public abstract class AbstractTestLoader<T extends BenchmarkModule> extends AbstractTestCase<T> {
3737

3838
private static final Logger LOG = LoggerFactory.getLogger(AbstractTestLoader.class);
@@ -58,6 +58,27 @@ public void testLoad() throws Exception {
5858

5959
}
6060

61+
/**
62+
* testLoad with after load script
63+
*/
64+
@Test
65+
public void testLoadWithAfterLoad() throws Exception {
66+
this.benchmark.setAfterLoadScriptPath("/after-load.sql");
67+
68+
this.benchmark.loadDatabase();
69+
70+
// A table called extra is added with after-load, with one entry zero
71+
try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM extra"); ResultSet rs = stmt.executeQuery()) {
72+
while (rs.next()) {
73+
assertEquals("Table 'extra' from after-load.sql has value different than 0", rs.getInt(1), 0);
74+
}
75+
} catch (Exception e) {
76+
fail("Table 'extra' from after-load.sql was not created");
77+
}
78+
79+
validateLoad();
80+
}
81+
6182
private void validateLoad() throws SQLException {
6283
assertFalse("Failed to get table names for " + benchmark.getBenchmarkName().toUpperCase(), this.catalog.getTables().isEmpty());
6384

src/test/resources/after-load.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
DROP TABLE IF EXISTS extra CASCADE;
2+
3+
CREATE TABLE extra (
4+
extra_pk int NOT NULL,
5+
PRIMARY KEY (extra_pk)
6+
);
7+
8+
INSERT INTO extra VALUES (0);

0 commit comments

Comments
 (0)