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

Introduce session setup #422

Merged
merged 25 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ jobs:
uses: docker://mcr.microsoft.com/mssql-tools:latest
with:
entrypoint: /opt/mssql-tools/bin/sqlcmd
args: -U sa -P SApassword1 -S sqlserver -b -Q "USE benchbase; CREATE USER benchuser01 FROM LOGIN benchuser01; EXEC sp_addrolemember 'db_owner', 'benchuser01';"
args: -U sa -P SApassword1 -S sqlserver -b -Q "USE benchbase; CREATE USER benchuser01 FROM LOGIN benchuser01; EXEC sp_addrolemember 'db_owner', 'benchuser01'; GRANT ALTER SERVER STATE to 'benchuser01';"

- name: Run benchmark
# Note: user/pass should match those used in sample configs.
Expand Down
4 changes: 4 additions & 0 deletions config/sqlserver/sample_noop_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<isolation>TRANSACTION_SERIALIZABLE</isolation>
<batchsize>128</batchsize>

<!-- Session setup statements file -->
<!-- uncommented here for CI pipeline testing -->
<sessionsetupfile>config/sqlserver/session_setup_sqlserver_cmds_example.sql</sessionsetupfile>

<!-- This parameter has no affect on this benchmark-->
<!-- There is no data to load -->
<scalefactor>1</scalefactor>
Expand Down
2 changes: 2 additions & 0 deletions config/sqlserver/sample_tpch_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<reconnectOnConnectionFailure>true</reconnectOnConnectionFailure>
<isolation>TRANSACTION_SERIALIZABLE</isolation>
<batchsize>1024</batchsize>
<!-- Session setup statements file -->
<!-- <sessionsetupfile>config/sqlserver/session_setup_sqlserver_cmds_example.sql</sessionsetupfile> -->

<scalefactor>0.1</scalefactor>

Expand Down
4 changes: 4 additions & 0 deletions config/sqlserver/session_setup_sqlserver_cmds_example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- SQL Server Database Console Command statements (DBCC)
-- NOTE: Requires "ALTER SERVER STATE" permission
DBCC DROPCLEANBUFFERS -- clear buffers (for cold runs)
DBCC FREEPROCCACHE -- clean plan cache
2 changes: 1 addition & 1 deletion docker/sqlserver-latest/up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ run_sqlcmd_in_docker -Q "CREATE DATABASE benchbase;"
run_sqlcmd_in_docker -Q "CREATE LOGIN benchuser01 WITH PASSWORD='P@ssw0rd';" || true

# Setup access
run_sqlcmd_in_docker -Q "USE benchbase; CREATE USER benchuser01 FROM LOGIN benchuser01; EXEC sp_addrolemember 'db_owner', 'benchuser01';" || true
run_sqlcmd_in_docker -Q "USE benchbase; CREATE USER benchuser01 FROM LOGIN benchuser01; EXEC sp_addrolemember 'db_owner', 'benchuser01'; GRANT ALTER SERVER STATE TO 'benchuser01';" || true
2 changes: 2 additions & 0 deletions src/main/java/com/oltpbenchmark/DBWorkload.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public static void main(String[] args) throws Exception {
wrkld.setPassword(xmlConfig.getString("password"));
wrkld.setRandomSeed(xmlConfig.getInt("randomSeed", -1));
wrkld.setBatchSize(xmlConfig.getInt("batchsize", 128));
wrkld.setSessionSetupFile(xmlConfig.getString("sessionsetupfile"));
wrkld.setMaxRetries(xmlConfig.getInt("retries", 3));
wrkld.setNewConnectionPerTxn(xmlConfig.getBoolean("newConnectionPerTxn", false));
wrkld.setReconnectOnConnectionFailure(
Expand Down Expand Up @@ -196,6 +197,7 @@ public static void main(String[] args) throws Exception {
initDebug.put("URL", wrkld.getUrl());
initDebug.put("Isolation", wrkld.getIsolationString());
initDebug.put("Batch Size", wrkld.getBatchSize());
initDebug.put("Session Setup File", wrkld.getSessionSetupFile());
initDebug.put("Scale Factor", wrkld.getScaleFactor());
initDebug.put("Terminals", wrkld.getTerminals());
initDebug.put("New Connection Per Txn", wrkld.getNewConnectionPerTxn());
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/com/oltpbenchmark/WorkloadConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import com.oltpbenchmark.api.TransactionTypes;
import com.oltpbenchmark.types.DatabaseType;
import com.oltpbenchmark.util.FileUtil;
import com.oltpbenchmark.util.ThreadUtil;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -33,6 +35,7 @@ public class WorkloadConfiguration {
private String password;
private String driverClass;
private int batchSize;
private String sessionSetupFile;
private int maxRetries;
private int randomSeed = -1;
private double scaleFactor = 1.0;
Expand Down Expand Up @@ -120,6 +123,27 @@ public void setBatchSize(int batchSize) {
this.batchSize = batchSize;
}

public String getSessionSetupFile() {
return sessionSetupFile;
}

private String checkPath(String path, String name) throws FileNotFoundException {
if (path == null) return null;
path = path.trim();
if (path.isEmpty()) return null;
assert path != null && !path.isEmpty();

if (FileUtil.exists(path)) {
return path;
} else {
throw new FileNotFoundException(name + " not found:" + sessionSetupFile);
}
}

public void setSessionSetupFile(String sessionSetupFile) throws FileNotFoundException {
this.sessionSetupFile = checkPath(sessionSetupFile, "sessionsetupfile");
}

public int getMaxRetries() {
return maxRetries;
}
Expand Down Expand Up @@ -292,8 +316,8 @@ public String getDDLPath() {
}

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

/** A utility method that init the phaseIterator and dialectMap */
Expand Down Expand Up @@ -396,6 +420,8 @@ public String toString() {
+ '\''
+ ", batchSize="
+ batchSize
+ ", sessionSetupFile="
+ sessionSetupFile
+ ", maxRetries="
+ maxRetries
+ ", scaleFactor="
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/oltpbenchmark/api/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import com.oltpbenchmark.types.TransactionStatus;
import com.oltpbenchmark.util.Histogram;
import com.oltpbenchmark.util.SQLUtil;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLRecoverableException;
Expand Down Expand Up @@ -188,6 +191,13 @@ public final void run() {
// In case of reuse reset the measurements
latencies = new LatencyRecord(workloadState.getTestStartNs());

// Invoke setup session
try {
this.setupSession();
} catch (Throwable ex) {
throw new RuntimeException("Unexpected error when setting up the session " + this, ex);
}

// Invoke initialize callback
try {
this.initialize();
Expand Down Expand Up @@ -723,6 +733,32 @@ protected void initialize() {
// The default is to do nothing
}

/**
* Set up the session by running a set of statements before benchmark execution begins. The path
* of the file where a set of statements defined should be added in &lt;sessionsetupfile&gt;
* &lt;/sessionsetupfile&gt;
*/
protected void setupSession() {
try {
String setupSessionFile = configuration.getSessionSetupFile();
if (setupSessionFile == null || setupSessionFile.isEmpty()) {
return;
}

String statements = new String(Files.readAllBytes(Paths.get(setupSessionFile)));
if (statements.isEmpty()) {
return;
}

try (Statement stmt = conn.createStatement()) {
stmt.execute(statements);
}
// conn.commit();
} catch (SQLException | IOException ex) {
throw new RuntimeException("Failed setting up session", ex);
}
}

/**
* Invoke a single transaction for the given TransactionType
*
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/benchmarks/noop/ddl-generic.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
DROP TABLE IF EXISTS FAKE;
CREATE TABLE FAKE (
ID INT PRIMARY KEY
);
DROP TABLE IF EXISTS FAKE2;
CREATE TABLE FAKE2 (
ID INT
);
16 changes: 16 additions & 0 deletions src/test/java/com/oltpbenchmark/api/AbstractTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public abstract class AbstractTestCase<T extends BenchmarkModule> {
protected final boolean createDatabase;
protected final boolean loadDatabase;
protected final String ddlOverridePath;
protected final String sessionSetupFile;

private static final AtomicInteger portCounter = new AtomicInteger(9001);
private static final int MAX_PORT_NUMBER = 65535;
Expand All @@ -77,13 +78,27 @@ public AbstractTestCase(boolean createDatabase, boolean loadDatabase) {
this.createDatabase = createDatabase;
this.loadDatabase = loadDatabase;
this.ddlOverridePath = null;
this.sessionSetupFile = null;
}

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

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

public abstract List<Class<? extends Procedure>> procedures();
Expand Down Expand Up @@ -127,6 +142,7 @@ public final void setUp() throws Exception {
this.workConf.setBenchmarkName(
BenchmarkModule.convertBenchmarkClassToBenchmarkName(benchmarkClass()));
this.workConf.setDDLPath(this.ddlOverridePath);
this.workConf.setSessionSetupFile(this.sessionSetupFile);

customWorkloadConfiguration(this.workConf);

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/oltpbenchmark/api/AbstractTestLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.oltpbenchmark.api;

import static org.junit.Assert.*;
import static org.junit.Assert.fail;

import com.oltpbenchmark.catalog.Table;
import com.oltpbenchmark.util.Histogram;
Expand Down Expand Up @@ -53,6 +52,8 @@ public void testLoad() throws Exception {
validateLoad();
}

// TODO: add another version of this test that uses external files (#600).

/** testLoad with after load script */
@Test
public void testLoadWithAfterLoad() throws Exception {
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/com/oltpbenchmark/api/AbstractTestWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public AbstractTestWorker(String ddlOverridePath) {
super(true, true, ddlOverridePath);
}

public AbstractTestWorker(String ddlOverridePath, String sessionSetupFile) {
super(true, true, ddlOverridePath, sessionSetupFile);
}

@Override
public List<String> ignorableTables() {
return null;
Expand All @@ -67,12 +71,14 @@ public void testGetProcedure() {
}
}

/** testExecuteWork */
/* testExecuteWork
* Similar to Worker.run()
*/
@Test
public void testExecuteWork() throws Exception {

Worker<?> w = workers.get(0);
assertNotNull(w);
w.setupSession();
w.initialize();
assertFalse(this.conn.isReadOnly());
for (TransactionType txnType : this.workConf.getTransTypes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public Class<NoOpBenchmark> benchmarkClass() {

@Override
public List<String> ignorableTables() {
return List.of("FAKE");
return List.of("FAKE", "FAKE2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@

package com.oltpbenchmark.benchmarks.noop;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.oltpbenchmark.api.AbstractTestWorker;
import com.oltpbenchmark.api.BenchmarkModule;
import com.oltpbenchmark.api.Procedure;
import com.oltpbenchmark.api.Worker;
import com.oltpbenchmark.catalog.Table;
import com.oltpbenchmark.util.SQLUtil;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import org.junit.Test;

public class TestNoOpWorker extends AbstractTestWorker<NoOpBenchmark> {

Expand All @@ -31,4 +43,33 @@ public List<Class<? extends Procedure>> procedures() {
public Class<NoOpBenchmark> benchmarkClass() {
return NoOpBenchmark.class;
}

@Test
public void testNoSessionSetupFile() throws Exception {
// Check that there is no session setup file assigned to the worker's config
assertNull("Session setup file should be null", this.workConf.getSessionSetupFile());

List<Worker<? extends BenchmarkModule>> workers = this.benchmark.makeWorkers();
Worker<?> worker = workers.get(0);
assertNull(
"Session setup file should be null",
worker.getWorkloadConfiguration().getSessionSetupFile());

// Make sure there are no rows in the table
this.testExecuteWork();

Table catalog_tbl = this.catalog.getTable("FAKE2");
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);
}
}
}
Loading
Loading