-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathMySQLProvider.java
More file actions
187 lines (173 loc) · 7.25 KB
/
MySQLProvider.java
File metadata and controls
187 lines (173 loc) · 7.25 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package sqlancer.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import sqlancer.AbstractAction;
import sqlancer.IgnoreMeException;
import sqlancer.MainOptions;
import sqlancer.Randomly;
import sqlancer.SQLConnection;
import sqlancer.SQLProviderAdapter;
import sqlancer.StatementExecutor;
import sqlancer.common.DBMSCommon;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.common.query.SQLQueryProvider;
import sqlancer.mysql.gen.MySQLAlterTable;
import sqlancer.mysql.gen.MySQLDeleteGenerator;
import sqlancer.mysql.gen.MySQLDropIndex;
import sqlancer.mysql.gen.MySQLInsertGenerator;
import sqlancer.mysql.gen.MySQLSetGenerator;
import sqlancer.mysql.gen.MySQLTableGenerator;
import sqlancer.mysql.gen.MySQLTruncateTableGenerator;
import sqlancer.mysql.gen.admin.MySQLFlush;
import sqlancer.mysql.gen.admin.MySQLReset;
import sqlancer.mysql.gen.datadef.MySQLIndexGenerator;
import sqlancer.mysql.gen.tblmaintenance.MySQLAnalyzeTable;
import sqlancer.mysql.gen.tblmaintenance.MySQLCheckTable;
import sqlancer.mysql.gen.tblmaintenance.MySQLChecksum;
import sqlancer.mysql.gen.tblmaintenance.MySQLOptimize;
import sqlancer.mysql.gen.tblmaintenance.MySQLRepair;
public class MySQLProvider extends SQLProviderAdapter<MySQLGlobalState, MySQLOptions> {
public MySQLProvider() {
super(MySQLGlobalState.class, MySQLOptions.class);
}
enum Action implements AbstractAction<MySQLGlobalState> {
SHOW_TABLES((g) -> new SQLQueryAdapter("SHOW TABLES")), //
INSERT(MySQLInsertGenerator::insertRow), //
SET_VARIABLE(MySQLSetGenerator::set), //
REPAIR(MySQLRepair::repair), //
OPTIMIZE(MySQLOptimize::optimize), //
CHECKSUM(MySQLChecksum::checksum), //
CHECK_TABLE(MySQLCheckTable::check), //
ANALYZE_TABLE(MySQLAnalyzeTable::analyze), //
FLUSH(MySQLFlush::create), RESET(MySQLReset::create), CREATE_INDEX(MySQLIndexGenerator::create), //
ALTER_TABLE(MySQLAlterTable::create), //
TRUNCATE_TABLE(MySQLTruncateTableGenerator::generate), //
SELECT_INFO((g) -> new SQLQueryAdapter(
"select TABLE_NAME, ENGINE from information_schema.TABLES where table_schema = '" + g.getDatabaseName()
+ "'")), //
CREATE_TABLE((g) -> {
// TODO refactor
String tableName = DBMSCommon.createTableName(g.getSchema().getDatabaseTables().size());
return MySQLTableGenerator.generate(g, tableName);
}), //
DELETE(MySQLDeleteGenerator::delete), //
DROP_INDEX(MySQLDropIndex::generate);
private final SQLQueryProvider<MySQLGlobalState> sqlQueryProvider;
Action(SQLQueryProvider<MySQLGlobalState> sqlQueryProvider) {
this.sqlQueryProvider = sqlQueryProvider;
}
@Override
public SQLQueryAdapter getQuery(MySQLGlobalState globalState) throws Exception {
return sqlQueryProvider.getQuery(globalState);
}
}
private static int mapActions(MySQLGlobalState globalState, Action a) {
Randomly r = globalState.getRandomly();
int nrPerformed = 0;
switch (a) {
case DROP_INDEX:
nrPerformed = r.getInteger(0, 2);
break;
case SHOW_TABLES:
nrPerformed = r.getInteger(0, 1);
break;
case CREATE_TABLE:
nrPerformed = r.getInteger(0, 1);
break;
case INSERT:
nrPerformed = r.getInteger(0, globalState.getOptions().getMaxNumberInserts());
break;
case REPAIR:
nrPerformed = r.getInteger(0, 1);
break;
case SET_VARIABLE:
nrPerformed = r.getInteger(0, 5);
break;
case CREATE_INDEX:
nrPerformed = r.getInteger(0, 5);
break;
case FLUSH:
nrPerformed = Randomly.getBooleanWithSmallProbability() ? r.getInteger(0, 1) : 0;
break;
case OPTIMIZE:
// seems to yield low CPU utilization
nrPerformed = Randomly.getBooleanWithSmallProbability() ? r.getInteger(0, 1) : 0;
break;
case RESET:
// affects the global state, so do not execute
nrPerformed = globalState.getOptions().getNumberConcurrentThreads() == 1 ? r.getInteger(0, 1) : 0;
break;
case CHECKSUM:
case CHECK_TABLE:
case ANALYZE_TABLE:
nrPerformed = r.getInteger(0, 2);
break;
case ALTER_TABLE:
nrPerformed = r.getInteger(0, 5);
break;
case TRUNCATE_TABLE:
nrPerformed = r.getInteger(0, 2);
break;
case SELECT_INFO:
nrPerformed = r.getInteger(0, 10);
break;
case DELETE:
nrPerformed = r.getInteger(0, 10);
break;
default:
throw new AssertionError(a);
}
return nrPerformed;
}
@Override
public void generateDatabase(MySQLGlobalState globalState) throws Exception {
while (globalState.getSchema().getDatabaseTables().size() < Randomly.smallNumber() + 1) {
String tableName = DBMSCommon.createTableName(globalState.getSchema().getDatabaseTables().size());
SQLQueryAdapter createTable = MySQLTableGenerator.generate(globalState, tableName);
globalState.executeStatement(createTable);
}
StatementExecutor<MySQLGlobalState, Action> se = new StatementExecutor<>(globalState, Action.values(),
MySQLProvider::mapActions, (q) -> {
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
throw new IgnoreMeException();
}
});
se.executeStatements();
}
@Override
public SQLConnection createDatabase(MySQLGlobalState globalState) throws SQLException {
String username = globalState.getOptions().getUserName();
String password = globalState.getOptions().getPassword();
String host = globalState.getOptions().getHost();
int port = globalState.getOptions().getPort();
if (host == null) {
host = MySQLOptions.DEFAULT_HOST;
}
if (port == MainOptions.NO_SET_PORT) {
port = MySQLOptions.DEFAULT_PORT;
}
String databaseName = globalState.getDatabaseName();
globalState.getState().logStatement("DROP DATABASE IF EXISTS " + databaseName);
globalState.getState().logStatement("CREATE DATABASE " + databaseName);
globalState.getState().logStatement("USE " + databaseName);
String url = String.format("jdbc:mysql://%s:%d?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true",
host, port);
Connection con = DriverManager.getConnection(url, username, password);
try (Statement s = con.createStatement()) {
s.execute("DROP DATABASE IF EXISTS " + databaseName);
}
try (Statement s = con.createStatement()) {
s.execute("CREATE DATABASE " + databaseName);
}
try (Statement s = con.createStatement()) {
s.execute("USE " + databaseName);
}
return new SQLConnection(con);
}
@Override
public String getDBMSName() {
return "mysql";
}
}