forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYSQLCatalog.java
More file actions
96 lines (83 loc) · 3.93 KB
/
YSQLCatalog.java
File metadata and controls
96 lines (83 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package sqlancer.yugabyte.ysql.oracle;
import static sqlancer.yugabyte.ysql.YSQLProvider.DDL_LOCK;
import java.util.Arrays;
import java.util.List;
import sqlancer.IgnoreMeException;
import sqlancer.Main;
import sqlancer.MainOptions;
import sqlancer.SQLConnection;
import sqlancer.common.DBMSCommon;
import sqlancer.common.oracle.TestOracle;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.yugabyte.ysql.YSQLErrors;
import sqlancer.yugabyte.ysql.YSQLGlobalState;
import sqlancer.yugabyte.ysql.YSQLProvider;
import sqlancer.yugabyte.ysql.gen.YSQLTableGenerator;
public class YSQLCatalog implements TestOracle<YSQLGlobalState> {
protected final YSQLGlobalState state;
protected final ExpectedErrors errors = new ExpectedErrors();
protected final Main.StateLogger logger;
protected final MainOptions options;
protected final SQLConnection con;
private final List<YSQLProvider.Action> dmlActions = Arrays.asList(YSQLProvider.Action.INSERT,
YSQLProvider.Action.UPDATE, YSQLProvider.Action.DELETE);
private final List<YSQLProvider.Action> catalogActions = Arrays.asList(YSQLProvider.Action.CREATE_VIEW,
YSQLProvider.Action.CREATE_SEQUENCE, YSQLProvider.Action.ALTER_TABLE, YSQLProvider.Action.SET_CONSTRAINTS,
YSQLProvider.Action.DISCARD, YSQLProvider.Action.DROP_INDEX, YSQLProvider.Action.COMMENT_ON,
YSQLProvider.Action.RESET_ROLE, YSQLProvider.Action.RESET);
private final List<YSQLProvider.Action> diskActions = Arrays.asList(YSQLProvider.Action.TRUNCATE,
YSQLProvider.Action.VACUUM);
public YSQLCatalog(YSQLGlobalState globalState) {
this.state = globalState;
this.con = state.getConnection();
this.logger = state.getLogger();
this.options = state.getOptions();
YSQLErrors.addCommonExpressionErrors(errors);
YSQLErrors.addCommonFetchErrors(errors);
}
private YSQLProvider.Action getRandomAction(List<YSQLProvider.Action> actions) {
return actions.get(state.getRandomly().getInteger(0, actions.size()));
}
protected void createTables(YSQLGlobalState globalState, int numTables) throws Exception {
synchronized (DDL_LOCK) {
while (globalState.getSchema().getDatabaseTables().size() < numTables) {
// TODO concurrent DDLs may produce a lot of noise in test logs so its disabled right now
// added timeout to avoid possible catalog collisions
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new AssertionError();
}
try {
String tableName = DBMSCommon.createTableName(globalState.getSchema().getDatabaseTables().size());
SQLQueryAdapter createTable = YSQLTableGenerator.generate(tableName, true, globalState);
globalState.executeStatement(createTable);
globalState.getManager().incrementSelectQueryCount();
globalState.executeStatement(new SQLQueryAdapter("COMMIT", true));
} catch (IgnoreMeException e) {
// do nothing
}
}
}
}
@Override
public void check() throws Exception {
// create table or evaluate catalog test
int seed = state.getRandomly().getInteger(1, 100);
if (seed > 95) {
createTables(state, 1);
} else {
YSQLProvider.Action randomAction;
if (seed > 40) {
randomAction = getRandomAction(dmlActions);
} else if (seed > 10) {
randomAction = getRandomAction(catalogActions);
} else {
randomAction = getRandomAction(diskActions);
}
state.executeStatement(randomAction.getQuery(state));
}
state.getManager().incrementSelectQueryCount();
}
}