-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
MySqlJdbcContext.java
371 lines (328 loc) · 14.8 KB
/
MySqlJdbcContext.java
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.connector.mysql;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.kafka.connect.errors.ConnectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.debezium.config.Configuration;
import io.debezium.config.Configuration.Builder;
import io.debezium.config.Field;
import io.debezium.connector.mysql.MySqlConnectorConfig.EventProcessingFailureHandlingMode;
import io.debezium.connector.mysql.MySqlConnectorConfig.SecureConnectionMode;
import io.debezium.jdbc.JdbcConnection;
import io.debezium.jdbc.JdbcConnection.ConnectionFactory;
import io.debezium.relational.history.DatabaseHistory;
import io.debezium.util.Strings;
/**
* A context for a JDBC connection to MySQL.
*
* @author Randall Hauch
*/
public class MySqlJdbcContext implements AutoCloseable {
protected static final String MYSQL_CONNECTION_URL = "jdbc:mysql://${hostname}:${port}/?useInformationSchema=true&nullCatalogMeansCurrent=false&useSSL=${useSSL}&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL";
protected static final String JDBC_PROPERTY_LEGACY_DATETIME = "useLegacyDatetimeCode";
private static final String SQL_SHOW_SYSTEM_VARIABLES = "SHOW VARIABLES";
private static final String SQL_SHOW_SYSTEM_VARIABLES_CHARACTER_SET = "SHOW VARIABLES WHERE Variable_name IN ('character_set_server','collation_server')";
private static final String SQL_SHOW_SESSION_VARIABLE_SSL_VERSION = "SHOW SESSION STATUS LIKE 'Ssl_version'";
protected static ConnectionFactory FACTORY = JdbcConnection.patternBasedFactory(MYSQL_CONNECTION_URL);
protected final Logger logger = LoggerFactory.getLogger(getClass());
protected final Configuration config;
protected final JdbcConnection jdbc;
private final Map<String, String> originalSystemProperties = new HashMap<>();
public MySqlJdbcContext(Configuration config) {
this.config = config; // must be set before most methods are used
// Set up the JDBC connection without actually connecting, with extra MySQL-specific properties
// to give us better JDBC database metadata behavior, including using UTF-8 for the client-side character encoding
// per https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-charsets.html
boolean useSSL = sslModeEnabled();
Configuration jdbcConfig = config
.filter(x -> !(x.startsWith(DatabaseHistory.CONFIGURATION_FIELD_PREFIX_STRING) || x.equals(MySqlConnectorConfig.DATABASE_HISTORY.name())))
.subset("database.", true);
Builder jdbcConfigBuilder = jdbcConfig
.edit()
.with("useSSL", Boolean.toString(useSSL));
final String legacyDateTime = jdbcConfig.getString(JDBC_PROPERTY_LEGACY_DATETIME);
if (legacyDateTime == null) {
jdbcConfigBuilder.with(JDBC_PROPERTY_LEGACY_DATETIME, "false");
}
else if ("true".equals(legacyDateTime)) {
logger.warn("'{}' is set to 'true'. This setting is not recommended and can result in timezone issues.", JDBC_PROPERTY_LEGACY_DATETIME);
}
jdbcConfig = jdbcConfigBuilder.build();
String driverClassName = jdbcConfig.getString(MySqlConnectorConfig.JDBC_DRIVER);
this.jdbc = new JdbcConnection(jdbcConfig,
JdbcConnection.patternBasedFactory(MYSQL_CONNECTION_URL, driverClassName, getClass().getClassLoader()));
}
public Configuration config() {
return config;
}
public JdbcConnection jdbc() {
return jdbc;
}
public Logger logger() {
return logger;
}
public String username() {
return config.getString(MySqlConnectorConfig.USER);
}
public String password() {
return config.getString(MySqlConnectorConfig.PASSWORD);
}
public String hostname() {
return config.getString(MySqlConnectorConfig.HOSTNAME);
}
public int port() {
return config.getInteger(MySqlConnectorConfig.PORT);
}
public SecureConnectionMode sslMode() {
String mode = config.getString(MySqlConnectorConfig.SSL_MODE);
return SecureConnectionMode.parse(mode);
}
public boolean sslModeEnabled() {
return sslMode() != SecureConnectionMode.DISABLED;
}
public EventProcessingFailureHandlingMode eventDeserializationFailureHandlingMode() {
String mode = config.getString(MySqlConnectorConfig.EVENT_DESERIALIZATION_FAILURE_HANDLING_MODE);
return EventProcessingFailureHandlingMode.parse(mode);
}
public EventProcessingFailureHandlingMode inconsistentSchemaHandlingMode() {
String mode = config.getString(MySqlConnectorConfig.INCONSISTENT_SCHEMA_HANDLING_MODE);
return EventProcessingFailureHandlingMode.parse(mode);
}
public void start() {
if (sslModeEnabled()) {
originalSystemProperties.clear();
// Set the System properties for SSL for the MySQL driver ...
setSystemProperty("javax.net.ssl.keyStore", MySqlConnectorConfig.SSL_KEYSTORE, true);
setSystemProperty("javax.net.ssl.keyStorePassword", MySqlConnectorConfig.SSL_KEYSTORE_PASSWORD, false);
setSystemProperty("javax.net.ssl.trustStore", MySqlConnectorConfig.SSL_TRUSTSTORE, true);
setSystemProperty("javax.net.ssl.trustStorePassword", MySqlConnectorConfig.SSL_TRUSTSTORE_PASSWORD, false);
}
}
public void shutdown() {
try {
jdbc.close();
} catch (SQLException e) {
logger.error("Unexpected error shutting down the database connection", e);
} finally {
// Reset the system properties to their original value ...
originalSystemProperties.forEach((name, value) -> {
if (value != null) {
System.setProperty(name, value);
} else {
System.clearProperty(name);
}
});
}
}
@Override
public void close() {
shutdown();
}
/**
* Determine whether the MySQL server has GTIDs enabled.
*
* @return {@code false} if the server's {@code gtid_mode} is set and is {@code OFF}, or {@code true} otherwise
*/
public boolean isGtidModeEnabled() {
AtomicReference<String> mode = new AtomicReference<String>("off");
try {
jdbc().query("SHOW GLOBAL VARIABLES LIKE 'GTID_MODE'", rs -> {
if (rs.next()) {
mode.set(rs.getString(2));
}
});
} catch (SQLException e) {
throw new ConnectException("Unexpected error while connecting to MySQL and looking at GTID mode: ", e);
}
return !"OFF".equalsIgnoreCase(mode.get());
}
/**
* Determine the available GTID set for MySQL.
*
* @return the string representation of MySQL's GTID sets; never null but an empty string if the server does not use GTIDs
*/
public String knownGtidSet() {
AtomicReference<String> gtidSetStr = new AtomicReference<String>();
try {
jdbc.query("SHOW MASTER STATUS", rs -> {
if (rs.next() && rs.getMetaData().getColumnCount() > 4) {
gtidSetStr.set(rs.getString(5)); // GTID set, may be null, blank, or contain a GTID set
}
});
} catch (SQLException e) {
throw new ConnectException("Unexpected error while connecting to MySQL and looking at GTID mode: ", e);
}
String result = gtidSetStr.get();
return result != null ? result : "";
}
/**
* Get the purged GTID values from MySQL (gtid_purged value)
*
* @return A GTID set; may be empty if not using GTIDs or none have been purged yet
*/
public GtidSet purgedGtidSet() {
AtomicReference<String> gtidSetStr = new AtomicReference<String>();
try {
jdbc.query("SELECT @@global.gtid_purged", rs -> {
if (rs.next() && rs.getMetaData().getColumnCount() > 0) {
gtidSetStr.set(rs.getString(1)); // GTID set, may be null, blank, or contain a GTID set
}
});
}
catch (SQLException e) {
throw new ConnectException("Unexpected error while connecting to MySQL and looking at gtid_purged variable: ", e);
}
String result = gtidSetStr.get();
if (result == null) {
result = "";
}
return new GtidSet(result);
}
/**
* Determine if the current user has the named privilege. Note that if the user has the "ALL" privilege this method
* returns {@code true}.
*
* @param grantName the name of the MySQL privilege; may not be null
* @return {@code true} if the user has the named privilege, or {@code false} otherwise
*/
public boolean userHasPrivileges(String grantName) {
AtomicBoolean result = new AtomicBoolean(false);
try {
jdbc.query("SHOW GRANTS FOR CURRENT_USER", rs -> {
while (rs.next()) {
String grants = rs.getString(1);
logger.debug(grants);
if (grants == null) {
return;
}
grants = grants.toUpperCase();
if (grants.contains("ALL") || grants.contains(grantName.toUpperCase())) {
result.set(true);
}
}
});
} catch (SQLException e) {
throw new ConnectException("Unexpected error while connecting to MySQL and looking at privileges for current user: ", e);
}
return result.get();
}
protected String connectionString() {
return jdbc.connectionString(MYSQL_CONNECTION_URL);
}
/**
* Read the MySQL charset-related system variables.
*
* @return the system variables that are related to server character sets; never null
*/
protected Map<String, String> readMySqlCharsetSystemVariables() {
// Read the system variables from the MySQL instance and get the current database name ...
logger.debug("Reading MySQL charset-related system variables before parsing DDL history.");
return querySystemVariables(SQL_SHOW_SYSTEM_VARIABLES_CHARACTER_SET);
}
/**
* Read the MySQL system variables.
*
* @return the system variables that are related to server character sets; never null
*/
protected Map<String, String> readMySqlSystemVariables() {
// Read the system variables from the MySQL instance and get the current database name ...
logger.debug("Reading MySQL system variables");
return querySystemVariables(SQL_SHOW_SYSTEM_VARIABLES);
}
private Map<String, String> querySystemVariables(String statement) {
Map<String, String> variables = new HashMap<>();
try {
jdbc.connect().query(statement, rs -> {
while (rs.next()) {
String varName = rs.getString(1);
String value = rs.getString(2);
if (varName != null && value != null) {
variables.put(varName, value);
logger.debug("\t{} = {}",
Strings.pad(varName, 45, ' '),
Strings.pad(value, 45, ' '));
}
}
});
} catch (SQLException e) {
throw new ConnectException("Error reading MySQL variables: " + e.getMessage(), e);
}
return variables;
}
protected String setStatementFor(Map<String, String> variables) {
StringBuilder sb = new StringBuilder("SET ");
boolean first = true;
List<String> varNames = new ArrayList<>(variables.keySet());
Collections.sort(varNames);
for (String varName : varNames) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(varName).append("=");
String value = variables.get(varName);
if (value == null) {
value = "";
}
if (value.contains(",") || value.contains(";")) {
value = "'" + value + "'";
}
sb.append(value);
}
return sb.append(";").toString();
}
protected void setSystemProperty(String property, Field field, boolean showValueInError) {
String value = config.getString(field);
if (value != null) {
value = value.trim();
String existingValue = System.getProperty(property);
if (existingValue == null) {
// There was no existing property ...
String existing = System.setProperty(property, value);
originalSystemProperties.put(property, existing); // the existing value may be null
} else {
existingValue = existingValue.trim();
if (!existingValue.equalsIgnoreCase(value)) {
// There was an existing property, and the value is different ...
String msg = "System or JVM property '" + property + "' is already defined, but the configuration property '"
+ field.name()
+ "' defines a different value";
if (showValueInError) {
msg = "System or JVM property '" + property + "' is already defined as " + existingValue
+ ", but the configuration property '" + field.name() + "' defines a different value '" + value + "'";
}
throw new ConnectException(msg);
}
// Otherwise, there was an existing property, and the value is exactly the same (so do nothing!)
}
}
}
/**
* Read the Ssl Version session variable.
*
* @return the session variables that are related to sessions ssl version
*/
protected String getSessionVariableForSslVersion() {
final String SSL_VERSION = "Ssl_version";
logger.debug("Reading MySQL Session variable for Ssl Version");
Map<String, String> sessionVariables =
querySystemVariables(SQL_SHOW_SESSION_VARIABLE_SSL_VERSION);
if (!sessionVariables.isEmpty() && sessionVariables.containsKey(SSL_VERSION)) {
return sessionVariables.get(SSL_VERSION);
}
return null;
}
}