-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathTiDBProvider.java
More file actions
171 lines (152 loc) · 6.59 KB
/
TiDBProvider.java
File metadata and controls
171 lines (152 loc) · 6.59 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
package sqlancer.tidb;
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.SQLGlobalState;
import sqlancer.SQLProviderAdapter;
import sqlancer.StatementExecutor;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.common.query.SQLQueryProvider;
import sqlancer.tidb.TiDBProvider.TiDBGlobalState;
import sqlancer.tidb.gen.TiDBAlterTableGenerator;
import sqlancer.tidb.gen.TiDBAnalyzeTableGenerator;
import sqlancer.tidb.gen.TiDBDeleteGenerator;
import sqlancer.tidb.gen.TiDBIndexGenerator;
import sqlancer.tidb.gen.TiDBInsertGenerator;
import sqlancer.tidb.gen.TiDBRandomQuerySynthesizer;
import sqlancer.tidb.gen.TiDBSetGenerator;
import sqlancer.tidb.gen.TiDBTableGenerator;
import sqlancer.tidb.gen.TiDBUpdateGenerator;
import sqlancer.tidb.gen.TiDBViewGenerator;
public class TiDBProvider extends SQLProviderAdapter<TiDBGlobalState, TiDBOptions> {
public TiDBProvider() {
super(TiDBGlobalState.class, TiDBOptions.class);
}
public enum Action implements AbstractAction<TiDBGlobalState> {
INSERT(TiDBInsertGenerator::getQuery), //
ANALYZE_TABLE(TiDBAnalyzeTableGenerator::getQuery), //
TRUNCATE((g) -> new SQLQueryAdapter("TRUNCATE " + g.getSchema().getRandomTable(t -> !t.isView()).getName())), //
CREATE_INDEX(TiDBIndexGenerator::getQuery), //
DELETE(TiDBDeleteGenerator::getQuery), //
SET(TiDBSetGenerator::getQuery), //
UPDATE(TiDBUpdateGenerator::getQuery), //
ADMIN_CHECKSUM_TABLE(
(g) -> new SQLQueryAdapter("ADMIN CHECKSUM TABLE " + g.getSchema().getRandomTable().getName())), //
VIEW_GENERATOR(TiDBViewGenerator::getQuery), //
ALTER_TABLE(TiDBAlterTableGenerator::getQuery), //
EXPLAIN((g) -> {
ExpectedErrors errors = new ExpectedErrors();
TiDBErrors.addExpressionErrors(errors);
TiDBErrors.addExpressionHavingErrors(errors);
return new SQLQueryAdapter(
"EXPLAIN " + TiDBRandomQuerySynthesizer.generate(g, Randomly.smallNumber() + 1).getQueryString(),
errors);
});
private final SQLQueryProvider<TiDBGlobalState> sqlQueryProvider;
Action(SQLQueryProvider<TiDBGlobalState> sqlQueryProvider) {
this.sqlQueryProvider = sqlQueryProvider;
}
@Override
public SQLQueryAdapter getQuery(TiDBGlobalState state) throws Exception {
return sqlQueryProvider.getQuery(state);
}
}
public static class TiDBGlobalState extends SQLGlobalState<TiDBOptions, TiDBSchema> {
@Override
protected TiDBSchema readSchema() throws SQLException {
return TiDBSchema.fromConnection(getConnection(), getDatabaseName());
}
}
private static int mapActions(TiDBGlobalState globalState, Action a) {
Randomly r = globalState.getRandomly();
switch (a) {
case ANALYZE_TABLE:
case CREATE_INDEX:
return r.getInteger(0, 2);
case INSERT:
case EXPLAIN:
return r.getInteger(0, globalState.getOptions().getMaxNumberInserts());
case TRUNCATE:
case DELETE:
case ADMIN_CHECKSUM_TABLE:
return r.getInteger(0, 2);
case SET:
case UPDATE:
return r.getInteger(0, 5);
case VIEW_GENERATOR:
// https://github.com/tidb-challenge-program/bug-hunting-issue/issues/8
return r.getInteger(0, 2);
case ALTER_TABLE:
return r.getInteger(0, 10); // https://github.com/tidb-challenge-program/bug-hunting-issue/issues/10
default:
throw new AssertionError(a);
}
}
@Override
public void generateDatabase(TiDBGlobalState globalState) throws Exception {
for (int i = 0; i < Randomly.fromOptions(1, 2); i++) {
boolean success;
do {
SQLQueryAdapter qt = new TiDBTableGenerator().getQuery(globalState);
success = globalState.executeStatement(qt);
} while (!success);
}
StatementExecutor<TiDBGlobalState, Action> se = new StatementExecutor<>(globalState, Action.values(),
TiDBProvider::mapActions, (q) -> {
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
throw new IgnoreMeException();
}
});
try {
se.executeStatements();
} catch (SQLException e) {
if (e.getMessage().contains(
"references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them")) {
throw new IgnoreMeException(); // TODO: drop view instead
} else {
throw new AssertionError(e);
}
}
}
@Override
public SQLConnection createDatabase(TiDBGlobalState globalState) throws SQLException {
String host = globalState.getOptions().getHost();
int port = globalState.getOptions().getPort();
if (host == null) {
host = TiDBOptions.DEFAULT_HOST;
}
if (port == MainOptions.NO_SET_PORT) {
port = TiDBOptions.DEFAULT_PORT;
}
String databaseName = globalState.getDatabaseName();
String url = String.format("jdbc:mysql://%s:%d/", host, port);
Connection con = DriverManager.getConnection(url, globalState.getOptions().getUserName(),
globalState.getOptions().getPassword());
globalState.getState().logStatement("USE test");
globalState.getState().logStatement("DROP DATABASE IF EXISTS " + databaseName);
String createDatabaseCommand = "CREATE DATABASE " + databaseName;
globalState.getState().logStatement(createDatabaseCommand);
globalState.getState().logStatement("USE " + databaseName);
try (Statement s = con.createStatement()) {
s.execute("DROP DATABASE IF EXISTS " + databaseName);
}
try (Statement s = con.createStatement()) {
s.execute(createDatabaseCommand);
}
con.close();
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:4000/" + databaseName,
globalState.getOptions().getUserName(), globalState.getOptions().getPassword());
return new SQLConnection(con);
}
@Override
public String getDBMSName() {
return "tidb";
}
}