Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[connector-mysql] Allow user to pass custom JDBC URL parameters used … #921

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/content/connectors/mysql-cdc.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ During a snapshot operation, the connector will query each included table to pro
<td>Integer</td>
<td>The connection pool size.</td>
</tr>
<tr>
<td>jdbc.properties.*</td>
<td>optional</td>
<td style="word-wrap: break-word;">20</td>
<td>String</td>
<td>Option to pass custom JDBC URL properties. User can pass custom properties like 'jdbc.properties.useSSL' = 'false'.</td>
</tr>
<tr>
<td>heartbeat.interval</td>
<td>optional</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ public MySqlSourceBuilder<T> startupOptions(StartupOptions startupOptions) {
return this;
}

/** Custom properties that will overwrite the default JDBC connection URL. */
public MySqlSourceBuilder<T> jdbcProperties(Properties jdbcProperties) {
this.configFactory.jdbcProperties(jdbcProperties);
return this;
}

/** The Debezium MySQL connector properties. For example, "snapshot.mode". */
public MySqlSourceBuilder<T> debeziumProperties(Properties properties) {
this.configFactory.debeziumProperties(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class MySqlSourceConfig implements Serializable {
private final double distributionFactorLower;
private final boolean includeSchemaChanges;
private final boolean scanNewlyAddedTableEnabled;
private final Properties jdbcProperties;

// --------------------------------------------------------------------------------------------
// Debezium Configurations
Expand Down Expand Up @@ -84,7 +85,8 @@ public class MySqlSourceConfig implements Serializable {
double distributionFactorLower,
boolean includeSchemaChanges,
boolean scanNewlyAddedTableEnabled,
Properties dbzProperties) {
Properties dbzProperties,
Properties jdbcProperties) {
this.hostname = checkNotNull(hostname);
this.port = port;
this.username = checkNotNull(username);
Expand All @@ -107,6 +109,7 @@ public class MySqlSourceConfig implements Serializable {
this.dbzProperties = checkNotNull(dbzProperties);
this.dbzConfiguration = Configuration.from(dbzProperties);
this.dbzMySqlConfig = new MySqlConnectorConfig(dbzConfiguration);
this.jdbcProperties = jdbcProperties;
}

public String getHostname() {
Expand Down Expand Up @@ -201,4 +204,8 @@ public MySqlConnectorConfig getMySqlConnectorConfig() {
public RelationalTableFilters getTableFilters() {
return dbzMySqlConfig.getTableFilters();
}

public Properties getJdbcProperties() {
return jdbcProperties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class MySqlSourceConfigFactory implements Serializable {
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_LOWER_BOUND.defaultValue();
private boolean includeSchemaChanges = false;
private boolean scanNewlyAddedTableEnabled = false;
private Properties jdbcProperties;
private Duration heartbeatInterval = HEARTBEAT_INTERVAL.defaultValue();
private Properties dbzProperties;

Expand Down Expand Up @@ -216,6 +217,12 @@ public MySqlSourceConfigFactory scanNewlyAddedTableEnabled(boolean scanNewlyAdde
return this;
}

/** Custom properties that will overwrite the default JDBC connection URL. */
public MySqlSourceConfigFactory jdbcProperties(Properties jdbcProperties) {
this.jdbcProperties = jdbcProperties;
return this;
}

/** Specifies the startup options. */
public MySqlSourceConfigFactory startupOptions(StartupOptions startupOptions) {
switch (startupOptions.startupMode) {
Expand Down Expand Up @@ -299,6 +306,10 @@ public MySqlSourceConfig createConfig(int subtaskId) {
props.putAll(dbzProperties);
}

if (jdbcProperties == null) {
jdbcProperties = new Properties();
}

return new MySqlSourceConfig(
hostname,
port,
Expand All @@ -319,6 +330,7 @@ public MySqlSourceConfig createConfig(int subtaskId) {
distributionFactorLower,
includeSchemaChanges,
scanNewlyAddedTableEnabled,
props);
props,
jdbcProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
import com.zaxxer.hikari.HikariDataSource;
import io.debezium.connector.mysql.MySqlConnectorConfig;

import java.util.Properties;

/** A connection pool factory to create pooled DataSource {@link HikariDataSource}. */
public class PooledDataSourceFactory {

public static final String JDBC_URL_PATTERN =
"jdbc:mysql://%s:%s/?useInformationSchema=true&nullCatalogMeansCurrent=false&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL";
"jdbc:mysql://%s:%s/?useInformationSchema=true&nullCatalogMeansCurrent=false&useUnicode=true";
public static final String CONNECTION_POOL_PREFIX = "connection-pool-";
public static final String SERVER_TIMEZONE_KEY = "serverTimezone";
public static final int MINIMUM_POOL_SIZE = 1;
private static final Properties DEFAULT_JDBC_PROPERTIES = initializeDefaultJdbcProperties();

private PooledDataSourceFactory() {}

Expand All @@ -39,9 +42,10 @@ public static HikariDataSource createPooledDataSource(MySqlSourceConfig sourceCo

String hostName = sourceConfig.getHostname();
int port = sourceConfig.getPort();
Properties jdbcProperties = sourceConfig.getJdbcProperties();

config.setPoolName(CONNECTION_POOL_PREFIX + hostName + ":" + port);
config.setJdbcUrl(String.format(JDBC_URL_PATTERN, hostName, port));
config.setJdbcUrl(formatJdbcUrl(hostName, port, jdbcProperties));
config.setUsername(sourceConfig.getUsername());
config.setPassword(sourceConfig.getPassword());
config.setMinimumIdle(MINIMUM_POOL_SIZE);
Expand All @@ -58,4 +62,28 @@ public static HikariDataSource createPooledDataSource(MySqlSourceConfig sourceCo

return new HikariDataSource(config);
}

private static String formatJdbcUrl(String hostName, int port, Properties jdbcProperties) {
Properties combinedProperties = new Properties();
combinedProperties.putAll(DEFAULT_JDBC_PROPERTIES);
combinedProperties.putAll(jdbcProperties);

StringBuilder jdbcUrlStringBuilder =
new StringBuilder(String.format(JDBC_URL_PATTERN, hostName, port));

combinedProperties.forEach(
(key, value) -> {
jdbcUrlStringBuilder.append("&").append(key).append("=").append(value);
});

return jdbcUrlStringBuilder.toString();
}

private static Properties initializeDefaultJdbcProperties() {
Properties defaultJdbcProperties = new Properties();
defaultJdbcProperties.setProperty("zeroDateTimeBehavior", "CONVERT_TO_NULL");
defaultJdbcProperties.setProperty("characterEncoding", "UTF-8");
defaultJdbcProperties.setProperty("characterSetResults", "UTF-8");
return defaultJdbcProperties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.ververica.cdc.connectors.mysql.table;

import java.util.Map;
import java.util.Properties;

/** Option utils for JDBC URL properties. */
public class JdbcUrlUtils {

// Prefix for JDBC specific properties.
public static final String PROPERTIES_PREFIX = "jdbc.properties.";

public static Properties getJdbcProperties(Map<String, String> tableOptions) {
Properties jdbcProperties = new Properties();
if (hasJdbcProperties(tableOptions)) {
tableOptions.keySet().stream()
.filter(key -> key.startsWith(PROPERTIES_PREFIX))
.forEach(
key -> {
final String value = tableOptions.get(key);
final String subKey = key.substring((PROPERTIES_PREFIX).length());
jdbcProperties.put(subKey, value);
});
}
return jdbcProperties;
}

/**
* Decides if the table options contains JDBC properties that start with prefix
* 'jdbc.properties'.
*/
private static boolean hasJdbcProperties(Map<String, String> tableOptions) {
return tableOptions.keySet().stream().anyMatch(k -> k.startsWith(PROPERTIES_PREFIX));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class MySqlTableSource implements ScanTableSource, SupportsReadingMetadat
private final double distributionFactorLower;
private final StartupOptions startupOptions;
private final boolean scanNewlyAddedTableEnabled;
private final Properties jdbcProperties;
private final Duration heartbeatInterval;

// --------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -135,6 +136,7 @@ public MySqlTableSource(
distributionFactorLower,
startupOptions,
false,
new Properties(),
heartbeatInterval);
}

Expand All @@ -160,6 +162,7 @@ public MySqlTableSource(
double distributionFactorLower,
StartupOptions startupOptions,
boolean scanNewlyAddedTableEnabled,
Properties jdbcProperties,
Duration heartbeatInterval) {
this.physicalSchema = physicalSchema;
this.port = port;
Expand All @@ -182,6 +185,7 @@ public MySqlTableSource(
this.distributionFactorLower = distributionFactorLower;
this.startupOptions = startupOptions;
this.scanNewlyAddedTableEnabled = scanNewlyAddedTableEnabled;
this.jdbcProperties = jdbcProperties;
// Mutable attributes
this.producedDataType = physicalSchema.toPhysicalRowDataType();
this.metadataKeys = Collections.emptyList();
Expand Down Expand Up @@ -238,6 +242,7 @@ public ScanRuntimeProvider getScanRuntimeProvider(ScanContext scanContext) {
.startupOptions(startupOptions)
.deserializer(deserializer)
.scanNewlyAddedTableEnabled(scanNewlyAddedTableEnabled)
.jdbcProperties(jdbcProperties)
.heartbeatInterval(heartbeatInterval)
.build();
return SourceProvider.of(parallelSource);
Expand Down Expand Up @@ -316,6 +321,7 @@ public DynamicTableSource copy() {
distributionFactorLower,
startupOptions,
scanNewlyAddedTableEnabled,
jdbcProperties,
heartbeatInterval);
source.metadataKeys = metadataKeys;
source.producedDataType = producedDataType;
Expand Down Expand Up @@ -353,7 +359,8 @@ public boolean equals(Object o) {
&& Objects.equals(connectionPoolSize, that.connectionPoolSize)
&& Objects.equals(startupOptions, that.startupOptions)
&& Objects.equals(producedDataType, that.producedDataType)
&& Objects.equals(metadataKeys, that.metadataKeys);
&& Objects.equals(metadataKeys, that.metadataKeys)
&& Objects.equals(jdbcProperties, that.jdbcProperties);
}

@Override
Expand Down Expand Up @@ -381,7 +388,8 @@ public int hashCode() {
startupOptions,
producedDataType,
metadataKeys,
scanNewlyAddedTableEnabled);
scanNewlyAddedTableEnabled,
jdbcProperties);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public class MySqlTableSourceFactory implements DynamicTableSourceFactory {
public DynamicTableSource createDynamicTableSource(Context context) {
final FactoryUtil.TableFactoryHelper helper =
FactoryUtil.createTableFactoryHelper(this, context);
helper.validateExcept(DebeziumOptions.DEBEZIUM_OPTIONS_PREFIX);
helper.validateExcept(
DebeziumOptions.DEBEZIUM_OPTIONS_PREFIX, JdbcUrlUtils.PROPERTIES_PREFIX);

final ReadableConfig config = helper.getOptions();
String hostname = config.get(HOSTNAME);
Expand Down Expand Up @@ -135,6 +136,7 @@ public DynamicTableSource createDynamicTableSource(Context context) {
distributionFactorLower,
startupOptions,
scanNewlyAddedTableEnabled,
JdbcUrlUtils.getJdbcProperties(context.getCatalogTable().getOptions()),
heartbeatInterval);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public void testCommonProperties() {
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_UPPER_BOUND.defaultValue(),
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_LOWER_BOUND.defaultValue(),
StartupOptions.initial(),
false,
new Properties(),
HEARTBEAT_INTERVAL.defaultValue());
assertEquals(expectedSource, actualSource);
}
Expand Down Expand Up @@ -161,6 +163,8 @@ public void testEnableParallelReadSource() {
40.5d,
0.01d,
StartupOptions.initial(),
false,
new Properties(),
HEARTBEAT_INTERVAL.defaultValue());
assertEquals(expectedSource, actualSource);
}
Expand Down Expand Up @@ -198,6 +202,8 @@ public void testEnableParallelReadSourceWithSingleServerId() {
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_UPPER_BOUND.defaultValue(),
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_LOWER_BOUND.defaultValue(),
StartupOptions.initial(),
false,
new Properties(),
HEARTBEAT_INTERVAL.defaultValue());
assertEquals(expectedSource, actualSource);
}
Expand Down Expand Up @@ -233,6 +239,8 @@ public void testEnableParallelReadSourceLatestOffset() {
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_UPPER_BOUND.defaultValue(),
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_LOWER_BOUND.defaultValue(),
StartupOptions.latest(),
false,
new Properties(),
HEARTBEAT_INTERVAL.defaultValue());
assertEquals(expectedSource, actualSource);
}
Expand All @@ -245,11 +253,14 @@ public void testOptionalProperties() {
options.put("server-time-zone", "Asia/Shanghai");
options.put("scan.newly-added-table.enabled", "true");
options.put("debezium.snapshot.mode", "never");
options.put("jdbc.properties.useSSL", "false");
options.put("heartbeat.interval", "15213ms");

DynamicTableSource actualSource = createTableSource(options);
Properties dbzProperties = new Properties();
dbzProperties.put("snapshot.mode", "never");
Properties jdbcProperties = new Properties();
jdbcProperties.setProperty("useSSL", "false");
MySqlTableSource expectedSource =
new MySqlTableSource(
SCHEMA,
Expand All @@ -273,6 +284,7 @@ public void testOptionalProperties() {
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_LOWER_BOUND.defaultValue(),
StartupOptions.initial(),
true,
jdbcProperties,
Duration.ofMillis(15213));
assertEquals(expectedSource, actualSource);
}
Expand Down Expand Up @@ -330,6 +342,8 @@ public void testStartupFromInitial() {
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_UPPER_BOUND.defaultValue(),
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_LOWER_BOUND.defaultValue(),
StartupOptions.initial(),
false,
new Properties(),
HEARTBEAT_INTERVAL.defaultValue());
assertEquals(expectedSource, actualSource);
}
Expand Down Expand Up @@ -396,6 +410,8 @@ public void testStartupFromLatestOffset() {
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_UPPER_BOUND.defaultValue(),
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_LOWER_BOUND.defaultValue(),
StartupOptions.latest(),
false,
new Properties(),
HEARTBEAT_INTERVAL.defaultValue());
assertEquals(expectedSource, actualSource);
}
Expand Down Expand Up @@ -434,6 +450,8 @@ public void testMetadataColumns() {
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_UPPER_BOUND.defaultValue(),
SPLIT_KEY_EVEN_DISTRIBUTION_FACTOR_LOWER_BOUND.defaultValue(),
StartupOptions.initial(),
false,
new Properties(),
HEARTBEAT_INTERVAL.defaultValue());
expectedSource.producedDataType = SCHEMA_WITH_METADATA.toSourceRowDataType();
expectedSource.metadataKeys = Arrays.asList("op_ts", "database_name");
Expand Down