Skip to content

Commit 8a24f72

Browse files
PLUGIN-1640: add support for using different ports in cloud sql proxy VM
1 parent 37121fa commit 8a24f72

28 files changed

Lines changed: 251 additions & 16 deletions

cloudsql-mysql-plugin/docs/CloudSQLMySQL-action.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Properties
2323
**Connection Name:** The CloudSQL instance to connect to in the format <PROJECT_ID>:\<REGION>:<INSTANCE_NAME>.
2424
Can be found in the instance overview page.
2525

26+
**Port:** Port that MySQL is running on.
27+
2628
**CloudSQL Instance Type:** Whether the CloudSQL instance to connect to is private or public. Defaults to 'Public'.
2729

2830
**Username:** User identity for connecting to the specified database.

cloudsql-mysql-plugin/docs/CloudSQLMySQL-batchsink.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ You also can use the macro function ${conn(connection-name)}.
3232
**Connection Name:** The CloudSQL instance to connect to in the format <PROJECT_ID>:\<REGION>:<INSTANCE_NAME>.
3333
Can be found in the instance overview page.
3434

35+
**Port:** Port that MySQL is running on.
36+
3537
**CloudSQL Instance Type:** Whether the CloudSQL instance to connect to is private or public. Defaults to 'Public'.
3638

3739
**Table Name:** Name of the table to export to. Table must exist prior to running the pipeline.

cloudsql-mysql-plugin/docs/CloudSQLMySQL-batchsource.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ You also can use the macro function ${conn(connection-name)}.
3131
**Connection Name:** The CloudSQL instance to connect to in the format <PROJECT_ID>:\<REGION>:<INSTANCE_NAME>.
3232
Can be found in the instance overview page.
3333

34+
**Port:** Port that MySQL is running on.
35+
3436
**CloudSQL Instance Type:** Whether the CloudSQL instance to connect to is private or public. Defaults to 'Public'.
3537

3638
**Import Query:** The SELECT query to use to import data from the specified table.

cloudsql-mysql-plugin/docs/CloudSQLMySQL-connector.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Properties
1818
**Connection Name:** The CloudSQL instance to connect to in the format <PROJECT_ID>:\<REGION>:<INSTANCE_NAME>.
1919
Can be found in the instance overview page.
2020

21+
**Port:** Port that MySQL is running on.
22+
2123
**Database:** MySQL database name.
2224

2325
**Username:** User identity for connecting to the specified database. Required for databases that need

cloudsql-mysql-plugin/src/main/java/io/cdap/plugin/cloudsql/mysql/CloudSQLMySQLAction.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import com.google.common.collect.ImmutableMap;
2020
import io.cdap.cdap.api.annotation.Description;
21+
import io.cdap.cdap.api.annotation.Macro;
2122
import io.cdap.cdap.api.annotation.Name;
2223
import io.cdap.cdap.api.annotation.Plugin;
2324
import io.cdap.cdap.etl.api.FailureCollector;
2425
import io.cdap.cdap.etl.api.PipelineConfigurer;
2526
import io.cdap.cdap.etl.api.action.Action;
27+
import io.cdap.plugin.db.ConnectionConfig;
2628
import io.cdap.plugin.db.action.AbstractDBAction;
2729
import io.cdap.plugin.db.action.QueryConfig;
2830
import io.cdap.plugin.util.CloudSQLUtil;
@@ -71,6 +73,12 @@ public CloudSQLMySQLActionConfig() {
7173
+ "instance, enter the internal IP address of the Compute Engine VM cloudsql proxy is running on.")
7274
public String connectionName;
7375

76+
@Name(ConnectionConfig.PORT)
77+
@Description("Database port number")
78+
@Macro
79+
@Nullable
80+
private Integer port;
81+
7482
@Name(DATABASE)
7583
@Description("Database name to connect to")
7684
public String database;
@@ -94,6 +102,7 @@ public String getConnectionString() {
94102
return String.format(
95103
CloudSQLMySQLConstants.PRIVATE_CLOUDSQL_MYSQL_CONNECTION_STRING_FORMAT,
96104
connectionName,
105+
getPort(),
97106
database);
98107
}
99108

@@ -103,6 +112,10 @@ public String getConnectionString() {
103112
connectionName);
104113
}
105114

115+
public Integer getPort() {
116+
return port == null ? 3306 : port;
117+
}
118+
106119
@Override
107120
public Map<String, String> getDBSpecificArguments() {
108121
return ImmutableMap.of(

cloudsql-mysql-plugin/src/main/java/io/cdap/plugin/cloudsql/mysql/CloudSQLMySQLConnectorConfig.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.cdap.plugin.cloudsql.mysql;
1818

1919
import io.cdap.cdap.api.annotation.Description;
20+
import io.cdap.cdap.api.annotation.Macro;
2021
import io.cdap.cdap.api.annotation.Name;
2122
import io.cdap.plugin.db.ConnectionConfig;
2223
import io.cdap.plugin.db.connector.AbstractDBConnectorConfig;
@@ -40,6 +41,12 @@ public class CloudSQLMySQLConnectorConfig extends AbstractDBConnectorConfig {
4041
+ "instance, enter the internal IP address of the Compute Engine VM cloudsql proxy is running on.")
4142
private String connectionName;
4243

44+
@Name(ConnectionConfig.PORT)
45+
@Description("Database port number")
46+
@Macro
47+
@Nullable
48+
private Integer port;
49+
4350
@Name(ConnectionConfig.DATABASE)
4451
@Description("Database name to connect to")
4552
private String database;
@@ -49,14 +56,16 @@ public class CloudSQLMySQLConnectorConfig extends AbstractDBConnectorConfig {
4956
private String instanceType;
5057

5158
public CloudSQLMySQLConnectorConfig(String user, String password, String jdbcPluginName, String connectionArguments,
52-
String instanceType, String connectionName, String database) {
59+
String instanceType, String connectionName, String database,
60+
@Nullable Integer port) {
5361
this.user = user;
5462
this.password = password;
5563
this.jdbcPluginName = jdbcPluginName;
5664
this.connectionArguments = connectionArguments;
5765
this.instanceType = instanceType;
5866
this.connectionName = connectionName;
5967
this.database = database;
68+
this.port = port;
6069
}
6170

6271
public String getDatabase() {
@@ -71,12 +80,17 @@ public String getConnectionName() {
7180
return connectionName;
7281
}
7382

83+
public Integer getPort() {
84+
return port == null ? 3306 : port;
85+
}
86+
7487
@Override
7588
public String getConnectionString() {
7689
if (CloudSQLUtil.PRIVATE_INSTANCE.equalsIgnoreCase(instanceType)) {
7790
return String.format(
7891
CloudSQLMySQLConstants.PRIVATE_CLOUDSQL_MYSQL_CONNECTION_STRING_FORMAT,
7992
connectionName,
93+
getPort(),
8094
database);
8195
}
8296

cloudsql-mysql-plugin/src/main/java/io/cdap/plugin/cloudsql/mysql/CloudSQLMySQLConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ private CloudSQLMySQLConstants() {
2626
public static final String CONNECTION_TIMEOUT = "connectionTimeout";
2727
public static final String PUBLIC_CLOUDSQL_MYSQL_CONNECTION_STRING_FORMAT =
2828
"jdbc:mysql:///%s?cloudSqlInstance=%s&socketFactory=com.google.cloud.sql.mysql.SocketFactory";
29-
public static final String PRIVATE_CLOUDSQL_MYSQL_CONNECTION_STRING_FORMAT = "jdbc:mysql://%s/%s";
29+
public static final String PRIVATE_CLOUDSQL_MYSQL_CONNECTION_STRING_FORMAT = "jdbc:mysql://%s:%s/%s";
3030
}

cloudsql-mysql-plugin/src/main/java/io/cdap/plugin/cloudsql/mysql/CloudSQLMySQLSink.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ protected LineageRecorder getLineageRecorder(BatchSinkContext context) {
9191
host = connectionParams[2];
9292
location = connectionParams[1];
9393
}
94-
String fqn = DBUtils.constructFQN("mysql", host, 3306,
94+
String fqn = DBUtils.constructFQN("mysql", host,
95+
cloudsqlMysqlSinkConfig.getConnection().getPort(),
9596
cloudsqlMysqlSinkConfig.getConnection().getDatabase(),
9697
cloudsqlMysqlSinkConfig.getReferenceName());
9798
Asset.Builder assetBuilder = Asset.builder(cloudsqlMysqlSinkConfig.getReferenceName()).setFqn(fqn);

cloudsql-mysql-plugin/src/main/java/io/cdap/plugin/cloudsql/mysql/CloudSQLMySQLSource.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ protected String createConnectionString() {
8686
return String.format(
8787
CloudSQLMySQLConstants.PRIVATE_CLOUDSQL_MYSQL_CONNECTION_STRING_FORMAT,
8888
cloudsqlMysqlSourceConfig.connection.getConnectionName(),
89+
cloudsqlMysqlSourceConfig.connection.getPort(),
8990
cloudsqlMysqlSourceConfig.connection.getDatabase());
9091
}
9192

@@ -108,7 +109,8 @@ protected LineageRecorder getLineageRecorder(BatchSourceContext context) {
108109
host = connectionParams[2];
109110
location = connectionParams[1];
110111
}
111-
String fqn = DBUtils.constructFQN("mysql", host, 3306,
112+
String fqn = DBUtils.constructFQN("mysql", host,
113+
cloudsqlMysqlSourceConfig.getConnection().getPort(),
112114
cloudsqlMysqlSourceConfig.getConnection().getDatabase(),
113115
cloudsqlMysqlSourceConfig.getReferenceName());
114116
Asset.Builder assetBuilder = Asset.builder(cloudsqlMysqlSourceConfig.getReferenceName()).setFqn(fqn);

cloudsql-mysql-plugin/src/test/java/io/cdap/plugin/cloudsql/mysql/CloudSQLMySQLConnectorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void test() throws IOException, ClassNotFoundException, InstantiationExce
5454
test(
5555
new CloudSQLMySQLConnector(
5656
new CloudSQLMySQLConnectorConfig(username, password, JDBC_PLUGIN_NAME, connectionArguments, instanceType,
57-
connectionName, database)
57+
connectionName, database, null)
5858
),
5959
JDBC_DRIVER_CLASS_NAME,
6060
CloudSQLMySQLConstants.PLUGIN_NAME

0 commit comments

Comments
 (0)