Skip to content

Commit ad53453

Browse files
committed
Factor out common table functionality
1 parent 0ee67f2 commit ad53453

24 files changed

Lines changed: 348 additions & 282 deletions

src/sqlancer/DatabaseProvider.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@
88
import java.util.List;
99
import java.util.Set;
1010

11-
import sqlancer.Main.QueryManager;
12-
import sqlancer.Main.StateLogger;
13-
14-
public interface DatabaseProvider<G> {
15-
16-
void generateAndTestDatabase(final String databaseName, Connection con, StateLogger logger, StateToReproduce state,
17-
QueryManager manager, MainOptions options) throws SQLException;
11+
public interface DatabaseProvider<G extends GlobalState> {
1812

13+
void generateAndTestDatabase(G globalState) throws SQLException;
14+
1915
G generateGlobalState();
2016

2117
Connection createDatabase(String databaseName, StateToReproduce state) throws SQLException;
@@ -67,7 +63,5 @@ public static List<String> getResultSetFirstColumnAsString(String queryString, S
6763
}
6864
return resultSet;
6965
}
70-
71-
7266

7367
}

src/sqlancer/GlobalState.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class GlobalState {
1313
private StateLogger logger;
1414
private StateToReproduce state;
1515
private QueryManager manager;
16+
private String databaseName;
1617

1718
public void setConnection(Connection con) {
1819
this.con = con;
@@ -61,5 +62,13 @@ public QueryManager getManager() {
6162
public void setManager(QueryManager manager) {
6263
this.manager = manager;
6364
}
65+
66+
public String getDatabaseName() {
67+
return databaseName;
68+
}
69+
70+
public void setDatabaseName(String databaseName) {
71+
this.databaseName = databaseName;
72+
}
6473

6574
}

src/sqlancer/Main.java

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.io.PrintWriter;
77
import java.io.StringWriter;
88
import java.io.Writer;
9+
import java.lang.reflect.InvocationTargetException;
10+
import java.lang.reflect.Method;
911
import java.nio.file.Files;
1012
import java.sql.Connection;
1113
import java.sql.SQLException;
@@ -252,19 +254,19 @@ private void printState(FileWriter writer, StateToReproduce state) {
252254
public static class QueryManager {
253255

254256
private final Connection con;
255-
private final StateToReproduce state;
257+
private final StateToReproduce stateToRepro;
256258

257259
QueryManager(Connection con, StateToReproduce state) {
258260
if (con == null || state == null) {
259261
throw new IllegalArgumentException();
260262
}
261263
this.con = con;
262-
this.state = state;
264+
this.stateToRepro = state;
263265

264266
}
265267

266268
public boolean execute(Query q) throws SQLException {
267-
state.statements.add(q);
269+
stateToRepro.statements.add(q);
268270
boolean success = q.execute(con);
269271
Main.nrSuccessfulActions.addAndGet(1);
270272
return success;
@@ -311,12 +313,14 @@ public void run() {
311313
long currentNrDbs = nrDatabases.get();
312314
long nrCurrentDbs = currentNrDbs - lastNrDbs;
313315
double throughputDbs = nrCurrentDbs / (elapsedTimeMillis / 1000d);
314-
long successfulStatementsRatio = (long) (100.0 * nrSuccessfulActions.get() / (nrSuccessfulActions.get() + nrUnsuccessfulActions.get()));
316+
long successfulStatementsRatio = (long) (100.0 * nrSuccessfulActions.get()
317+
/ (nrSuccessfulActions.get() + nrUnsuccessfulActions.get()));
315318
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
316319
Date date = new Date();
317320
System.out.println(String.format(
318321
"[%s] Executed %d queries (%d queries/s; %.2f/s dbs, successful statements: %2d%%). Threads shut down: %d.",
319-
dateFormat.format(date), currentNrQueries, (int) throughput, throughputDbs, successfulStatementsRatio, threadsShutdown));
322+
dateFormat.format(date), currentNrQueries, (int) throughput, throughputDbs,
323+
successfulStatementsRatio, threadsShutdown));
320324
timeMillis = System.currentTimeMillis();
321325
lastNrQueries = currentNrQueries;
322326
lastNrDbs = currentNrDbs;
@@ -330,9 +334,9 @@ public void run() {
330334

331335
executor.execute(new Runnable() {
332336

333-
StateToReproduce state;
337+
StateToReproduce stateToRepro;
334338
StateLogger logger;
335-
DatabaseProvider provider;
339+
DatabaseProvider<?> provider;
336340

337341
@Override
338342
public void run() {
@@ -365,24 +369,43 @@ public void run() {
365369
private void runThread(final String databaseName) {
366370
Thread.currentThread().setName(databaseName);
367371
while (true) {
368-
state = provider.getStateToReproduce(databaseName);
372+
stateToRepro = provider.getStateToReproduce(databaseName);
369373
logger = new StateLogger(databaseName, provider, options);
370-
try (Connection con = provider.createDatabase(databaseName, state)) {
371-
QueryManager manager = new QueryManager(con, state);
374+
try (Connection con = provider.createDatabase(databaseName, stateToRepro)) {
375+
QueryManager manager = new QueryManager(con, stateToRepro);
372376
java.sql.DatabaseMetaData meta = con.getMetaData();
373-
state.databaseVersion = meta.getDatabaseProductVersion();
374-
provider.generateAndTestDatabase(databaseName, con, logger, state, manager, options);
377+
stateToRepro.databaseVersion = meta.getDatabaseProductVersion();
378+
GlobalState state = (GlobalState) provider.generateGlobalState();
379+
state.setState(stateToRepro);
380+
Randomly r = new Randomly();
381+
state.setDatabaseName(databaseName);
382+
state.setConnection(con);
383+
state.setRandomly(r);
384+
state.setMainOptions(options);
385+
state.setStateLogger(logger);
386+
state.setManager(manager);
387+
Method method = provider.getClass().getMethod("generateAndTestDatabase", state.getClass());
388+
method.setAccessible(true);
389+
method.invoke(provider, state);
390+
// provider.generateAndTestDatabase(state);
391+
// provider.generateAndTestDatabase(databaseName, con, logger, state, manager, options);
375392
} catch (IgnoreMeException e) {
376393
continue;
394+
} catch (InvocationTargetException e) {
395+
if (e.getCause() instanceof IgnoreMeException) {
396+
continue;
397+
} else {
398+
throw new AssertionError(e);
399+
}
377400
} catch (ReduceMeException reduce) {
378-
logger.logRowNotFound(state);
401+
logger.logRowNotFound(stateToRepro);
379402
threadsShutdown++;
380403
break;
381404
} catch (Throwable reduce) {
382405
reduce.printStackTrace();
383-
state.exception = reduce.getMessage();
406+
stateToRepro.exception = reduce.getMessage();
384407
logger.logFileWriter = null;
385-
logger.logException(reduce, state);
408+
logger.logException(reduce, stateToRepro);
386409
threadsShutdown++;
387410
break;
388411
} finally {

src/sqlancer/cockroachdb/CockroachDBProvider.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,15 @@ public CockroachDBOptions getCockroachdbOptions() {
121121
}
122122

123123
@Override
124-
public void generateAndTestDatabase(String databaseName, Connection con, StateLogger logger, StateToReproduce state,
125-
QueryManager manager, MainOptions options) throws SQLException {
124+
public void generateAndTestDatabase(CockroachDBGlobalState globalState) throws SQLException {
126125
Randomly r = new Randomly();
127-
CockroachDBGlobalState globalState = new CockroachDBGlobalState();
128-
globalState.setConnection(con);
126+
Connection con = globalState.getConnection();
127+
String databaseName = globalState.getDatabaseName();
128+
QueryManager manager = globalState.getManager();
129+
StateLogger logger = globalState.getLogger();
130+
StateToReproduce state = globalState.getState();
131+
MainOptions options = globalState.getOptions();
129132
globalState.setSchema(CockroachDBSchema.fromConnection(con, databaseName));
130-
globalState.setRandomly(r);
131-
globalState.setMainOptions(options);
132-
globalState.setStateLogger(logger);
133-
globalState.setState(state);
134133
CockroachDBOptions cockroachdbOptions = new CockroachDBOptions();
135134
JCommander.newBuilder().addObject(cockroachdbOptions).build().parse(options.getDbmsOptions().split(" "));
136135
globalState.setCockroachDBOptions(cockroachdbOptions);

0 commit comments

Comments
 (0)