Skip to content

Commit 15ad613

Browse files
ranaalotaibiMSRana AlotaibiRana Alotaibibpkroth
authored
Introduce session setup (#422)
This PR introduces functionality for configuring a session by executing predefined statements before the benchmark execution begins. Users should specify the file containing these statements within the tag `<sessionsetupfile> </sessionsetupfile>` in the benchmarks' config files. CC: @bpkroth For example, ```xml <?xml version="1.0"?> <parameters> <!-- Connection details --> <type>sqlserver</type> ... <!-- Session setup statements file --> <sessionsetupfile>config/sqlserver/session_setup_sqlserver_cmds_example.sql</sessionsetupfile> ... ``` See Also: #192 --------- Co-authored-by: Rana Alotaibi <[email protected]> Co-authored-by: Rana Alotaibi <[email protected]> Co-authored-by: Brian Kroth <[email protected]> Co-authored-by: Brian Kroth <[email protected]>
1 parent c370656 commit 15ad613

File tree

19 files changed

+254
-7
lines changed

19 files changed

+254
-7
lines changed

.github/workflows/maven.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,13 @@ jobs:
689689
entrypoint: /opt/mssql-tools/bin/sqlcmd
690690
args: -U sa -P SApassword1 -S sqlserver -b -Q "USE benchbase; CREATE USER benchuser01 FROM LOGIN benchuser01; EXEC sp_addrolemember 'db_owner', 'benchuser01';"
691691

692+
- name: Setup privileged access for monitoring and session start tests
693+
uses: docker://mcr.microsoft.com/mssql-tools:latest
694+
with:
695+
entrypoint: /opt/mssql-tools/bin/sqlcmd
696+
args: -U sa -P SApassword1 -S sqlserver -b -Q "USE master; GRANT ALTER SERVER STATE, VIEW SERVER PERFORMANCE STATE to benchuser01;"
697+
698+
692699
- name: Run benchmark
693700
# Note: user/pass should match those used in sample configs.
694701
run: |

config/sqlserver/sample_noop_config.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
<isolation>TRANSACTION_SERIALIZABLE</isolation>
1212
<batchsize>128</batchsize>
1313

14+
<!-- Session setup statements file -->
15+
<!-- uncommented here for CI pipeline testing -->
16+
<sessionsetupfile>config/sqlserver/session_setup_sqlserver_cmds_example.sql</sessionsetupfile>
17+
1418
<!-- This parameter has no affect on this benchmark-->
1519
<!-- There is no data to load -->
1620
<scalefactor>1</scalefactor>

config/sqlserver/sample_tpch_config.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<reconnectOnConnectionFailure>true</reconnectOnConnectionFailure>
1111
<isolation>TRANSACTION_SERIALIZABLE</isolation>
1212
<batchsize>1024</batchsize>
13+
<!-- Session setup statements file -->
14+
<!-- <sessionsetupfile>config/sqlserver/session_setup_sqlserver_cmds_example.sql</sessionsetupfile> -->
1315

1416
<scalefactor>0.1</scalefactor>
1517

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- SQL Server Database Console Command statements (DBCC)
2+
-- NOTE: Requires "ALTER SERVER STATE" permission
3+
DBCC DROPCLEANBUFFERS -- clear buffers (for cold runs)
4+
DBCC FREEPROCCACHE -- clean plan cache

docker/sqlserver-latest/up.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ run_sqlcmd_in_docker -Q "CREATE LOGIN benchuser01 WITH PASSWORD='P@ssw0rd';" ||
3838

3939
# Setup access
4040
run_sqlcmd_in_docker -Q "USE benchbase; CREATE USER benchuser01 FROM LOGIN benchuser01; EXEC sp_addrolemember 'db_owner', 'benchuser01';" || true
41+
42+
# Setup privileged access for monitoring and session start tests
43+
run_sqlcmd_in_docker -Q "USE master; GRANT ALTER SERVER STATE, VIEW SERVER PERFORMANCE STATE TO benchuser01;" || true

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public static void main(String[] args) throws Exception {
138138
wrkld.setPassword(xmlConfig.getString("password"));
139139
wrkld.setRandomSeed(xmlConfig.getInt("randomSeed", -1));
140140
wrkld.setBatchSize(xmlConfig.getInt("batchsize", 128));
141+
wrkld.setSessionSetupFile(xmlConfig.getString("sessionsetupfile"));
141142
wrkld.setMaxRetries(xmlConfig.getInt("retries", 3));
142143
wrkld.setNewConnectionPerTxn(xmlConfig.getBoolean("newConnectionPerTxn", false));
143144
wrkld.setReconnectOnConnectionFailure(
@@ -196,6 +197,9 @@ public static void main(String[] args) throws Exception {
196197
initDebug.put("URL", wrkld.getUrl());
197198
initDebug.put("Isolation", wrkld.getIsolationString());
198199
initDebug.put("Batch Size", wrkld.getBatchSize());
200+
initDebug.put("DDL Path", wrkld.getDDLPath());
201+
initDebug.put("Loader Threads", wrkld.getLoaderThreads());
202+
initDebug.put("Session Setup File", wrkld.getSessionSetupFile());
199203
initDebug.put("Scale Factor", wrkld.getScaleFactor());
200204
initDebug.put("Terminals", wrkld.getTerminals());
201205
initDebug.put("New Connection Per Txn", wrkld.getNewConnectionPerTxn());
@@ -247,6 +251,7 @@ public static void main(String[] args) throws Exception {
247251
if (xmlConfig.containsKey("afterload")) {
248252
bench.setAfterLoadScriptPath(xmlConfig.getString("afterload"));
249253
}
254+
initDebug.put("After Load Script", bench.getAfterLoadScriptPath());
250255

251256
TransactionType tmpType =
252257
bench.initTransactionType(

src/main/java/com/oltpbenchmark/WorkloadConfiguration.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import com.oltpbenchmark.api.TransactionTypes;
1919
import com.oltpbenchmark.types.DatabaseType;
20+
import com.oltpbenchmark.util.FileUtil;
2021
import com.oltpbenchmark.util.ThreadUtil;
22+
import java.io.FileNotFoundException;
2123
import java.sql.Connection;
2224
import java.util.ArrayList;
2325
import java.util.List;
@@ -33,6 +35,7 @@ public class WorkloadConfiguration {
3335
private String password;
3436
private String driverClass;
3537
private int batchSize;
38+
private String sessionSetupFile;
3639
private int maxRetries;
3740
private int randomSeed = -1;
3841
private double scaleFactor = 1.0;
@@ -120,6 +123,14 @@ public void setBatchSize(int batchSize) {
120123
this.batchSize = batchSize;
121124
}
122125

126+
public String getSessionSetupFile() {
127+
return sessionSetupFile;
128+
}
129+
130+
public void setSessionSetupFile(String sessionSetupFile) throws FileNotFoundException {
131+
this.sessionSetupFile = FileUtil.checkPath(sessionSetupFile, "sessionsetupfile");
132+
}
133+
123134
public int getMaxRetries() {
124135
return maxRetries;
125136
}
@@ -292,8 +303,8 @@ public String getDDLPath() {
292303
}
293304

294305
/** Set the path in which we can find the ddl script. */
295-
public void setDDLPath(String ddlPath) {
296-
this.ddlPath = ddlPath;
306+
public void setDDLPath(String ddlPath) throws FileNotFoundException {
307+
this.ddlPath = FileUtil.checkPath(ddlPath, "ddlpath");
297308
}
298309

299310
/** A utility method that init the phaseIterator and dialectMap */
@@ -394,8 +405,16 @@ public String toString() {
394405
+ ", driverClass='"
395406
+ driverClass
396407
+ '\''
408+
+ ", reconnectOnFailure="
409+
+ reconnectOnConnectionFailure
410+
+ ", newConnectionPerTxn="
411+
+ newConnectionPerTxn
397412
+ ", batchSize="
398413
+ batchSize
414+
+ ", ddlpath="
415+
+ ddlPath
416+
+ ", sessionSetupFile="
417+
+ sessionSetupFile
399418
+ ", maxRetries="
400419
+ maxRetries
401420
+ ", scaleFactor="

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public final void setAfterLoadScriptPath(String scriptPath) {
9393
this.afterLoadScriptPath = scriptPath;
9494
}
9595

96+
public String getAfterLoadScriptPath() {
97+
return this.afterLoadScriptPath;
98+
}
99+
96100
// --------------------------------------------------------------------------
97101
// IMPLEMENTING CLASS INTERFACE
98102
// --------------------------------------------------------------------------

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import com.oltpbenchmark.types.TransactionStatus;
2727
import com.oltpbenchmark.util.Histogram;
2828
import com.oltpbenchmark.util.SQLUtil;
29+
import java.io.IOException;
30+
import java.nio.file.Files;
31+
import java.nio.file.Paths;
2932
import java.sql.Connection;
3033
import java.sql.SQLException;
3134
import java.sql.SQLRecoverableException;
@@ -188,6 +191,13 @@ public final void run() {
188191
// In case of reuse reset the measurements
189192
latencies = new LatencyRecord(workloadState.getTestStartNs());
190193

194+
// Invoke setup session
195+
try {
196+
this.setupSession();
197+
} catch (Throwable ex) {
198+
throw new RuntimeException("Unexpected error when setting up the session " + this, ex);
199+
}
200+
191201
// Invoke initialize callback
192202
try {
193203
this.initialize();
@@ -723,6 +733,32 @@ protected void initialize() {
723733
// The default is to do nothing
724734
}
725735

736+
/**
737+
* Set up the session by running a set of statements before benchmark execution begins. The path
738+
* of the file where a set of statements defined should be added in &lt;sessionsetupfile&gt;
739+
* &lt;/sessionsetupfile&gt;
740+
*/
741+
protected void setupSession() {
742+
try {
743+
String setupSessionFile = configuration.getSessionSetupFile();
744+
if (setupSessionFile == null || setupSessionFile.isEmpty()) {
745+
return;
746+
}
747+
748+
String statements = new String(Files.readAllBytes(Paths.get(setupSessionFile)));
749+
if (statements.isEmpty()) {
750+
return;
751+
}
752+
753+
try (Statement stmt = conn.createStatement()) {
754+
stmt.execute(statements);
755+
}
756+
// conn.commit();
757+
} catch (SQLException | IOException ex) {
758+
throw new RuntimeException("Failed setting up session", ex);
759+
}
760+
}
761+
726762
/**
727763
* Invoke a single transaction for the given TransactionType
728764
*

src/main/java/com/oltpbenchmark/api/collectors/monitoring/SQLServerMonitor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
/**
2323
* Implementation of a monitor specific to SQLServer. Uses SQLServer's system tables to extract
24-
* relevant query and system information.
24+
* relevant query and system information. Note: Requires "VIEW SERVER PERFORMANCE STATE"
25+
* permissions.
2526
*/
2627
public class SQLServerMonitor extends DatabaseMonitor {
2728

0 commit comments

Comments
 (0)